[PATCH v5 2/5] devcoredump: Add per device sysfs entry to enable/disable coredump
From: Manish Mandlik
Date: Wed Aug 10 2022 - 12:01:11 EST
The /sys/class/devcoredump/disabled provides only one-way disable
functionality. Also, disabling devcoredump using it disables the
devcoredump functionality for everyone who is using it.
Provide a way to selectively enable/disable devcoredump for the device
which is bound to a driver that implements the '.coredump()' callback.
This adds the 'coredump_disabled' driver attribute. When the driver
implements the '.coredump()' callback, 'coredump_disabled' file is added
along with the 'coredump' file in the sysfs folder of the device upon
driver binding. The file is removed when the driver is unbound.
Drivers can use this attribute to enable/disable devcoredump and the
userspace can write 0 or 1 to /sys/devices/.../coredump_disabled sysfs
entry to control enabling/disabling of devcoredump for that device.
Signed-off-by: Manish Mandlik <mmandlik@xxxxxxxxxx>
Reviewed-by: Abhishek Pandit-Subedi <abhishekpandit@xxxxxxxxxxxx>
---
Changes in v5:
- Use sysfs_emit(), kstrtobool() and attribute groups
- Move 'coredump_disabled' at appropriate location in 'struct device'
Changes in v4:
- New patch in the series
drivers/base/dd.c | 33 +++++++++++++++++++++++++++++++--
drivers/base/devcoredump.c | 2 +-
include/linux/device.h | 3 +++
3 files changed, 35 insertions(+), 3 deletions(-)
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 11b0fb6414d3..fa01901983c8 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -426,6 +426,35 @@ static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
}
static DEVICE_ATTR_WO(coredump);
+static ssize_t coredump_disabled_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ return sysfs_emit(buf, "%d\n", dev->coredump_disabled);
+}
+
+static ssize_t coredump_disabled_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ bool disabled;
+
+ if (kstrtobool(buf, &disabled) < 0)
+ return -EINVAL;
+
+ dev->coredump_disabled = disabled;
+
+ return count;
+}
+static DEVICE_ATTR_RW(coredump_disabled);
+
+static struct attribute *dev_coredump_attrs[] = {
+ &dev_attr_coredump.attr,
+ &dev_attr_coredump_disabled.attr,
+ NULL,
+};
+ATTRIBUTE_GROUPS(dev_coredump);
+
static int driver_sysfs_add(struct device *dev)
{
int ret;
@@ -447,7 +476,7 @@ static int driver_sysfs_add(struct device *dev)
if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump)
return 0;
- ret = device_create_file(dev, &dev_attr_coredump);
+ ret = device_add_groups(dev, dev_coredump_groups);
if (!ret)
return 0;
@@ -467,7 +496,7 @@ static void driver_sysfs_remove(struct device *dev)
if (drv) {
if (drv->coredump)
- device_remove_file(dev, &dev_attr_coredump);
+ device_remove_groups(dev, dev_coredump_groups);
sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
sysfs_remove_link(&dev->kobj, "driver");
}
diff --git a/drivers/base/devcoredump.c b/drivers/base/devcoredump.c
index f4d794d6bb85..c5e9af9f3181 100644
--- a/drivers/base/devcoredump.c
+++ b/drivers/base/devcoredump.c
@@ -255,7 +255,7 @@ void dev_coredumpm(struct device *dev, struct module *owner,
struct devcd_entry *devcd;
struct device *existing;
- if (devcd_disabled)
+ if (devcd_disabled || dev->coredump_disabled)
goto free;
existing = class_find_device(&devcd_class, NULL, dev,
diff --git a/include/linux/device.h b/include/linux/device.h
index dc941997795c..41aedc74a5a8 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -526,6 +526,8 @@ struct device_physical_location {
* should be set by the subsystem / bus driver that discovered
* the device.
*
+ * @coredump_disabled: Can be used to selectively enable/disable the coredump
+ * functionality for a particular device via sysfs entry.
* @offline_disabled: If set, the device is permanently online.
* @offline: Set after successful invocation of bus type's .offline().
* @of_node_reused: Set if the device-tree node is shared with an ancestor
@@ -637,6 +639,7 @@ struct device {
enum device_removable removable;
+ bool coredump_disabled:1;
bool offline_disabled:1;
bool offline:1;
bool of_node_reused:1;
--
2.37.1.559.g78731f0fdb-goog