[PATCH 1/2] PCI: hotplug: Manage hotplug slot attributes as group
From: Thomas Weißschuh
Date: Thu May 14 2026 - 02:37:22 EST
Make use of 'struct attribute_group' to manage all attribute together,
simplifying the lifecycle management.
Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
drivers/pci/hotplug/pci_hotplug_core.c | 114 +++++++++++++--------------------
1 file changed, 43 insertions(+), 71 deletions(-)
diff --git a/drivers/pci/hotplug/pci_hotplug_core.c b/drivers/pci/hotplug/pci_hotplug_core.c
index fadcf98a8a66..7c4e197f5581 100644
--- a/drivers/pci/hotplug/pci_hotplug_core.c
+++ b/drivers/pci/hotplug/pci_hotplug_core.c
@@ -239,10 +239,46 @@ static bool has_test_file(struct hotplug_slot *slot)
return false;
}
+static struct attribute *hotplug_slot_attrs[] = {
+ &hotplug_slot_attr_power.attr,
+ &hotplug_slot_attr_attention.attr,
+ &hotplug_slot_attr_latch.attr,
+ &hotplug_slot_attr_presence.attr,
+ &hotplug_slot_attr_test.attr,
+ NULL
+};
+
+static umode_t hotplug_slot_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
+{
+ struct hotplug_slot *slot = to_pci_slot(kobj)->hotplug;
+
+ if (attr == &hotplug_slot_attr_power.attr && !has_power_file(slot))
+ return 0;
+
+ if (attr == &hotplug_slot_attr_attention.attr && !has_attention_file(slot))
+ return 0;
+
+ if (attr == &hotplug_slot_attr_latch.attr && !has_latch_file(slot))
+ return 0;
+
+ if (attr == &hotplug_slot_attr_presence.attr && !has_adapter_file(slot))
+ return 0;
+
+ if (attr == &hotplug_slot_attr_test.attr && !has_test_file(slot))
+ return 0;
+
+ return attr->mode;
+}
+
+static const struct attribute_group hotplug_slot_group = {
+ .is_visible = hotplug_slot_attr_is_visible,
+ .attrs = hotplug_slot_attrs,
+};
+
static int fs_add_slot(struct hotplug_slot *slot, struct pci_slot *pci_slot)
{
struct kobject *kobj;
- int retval = 0;
+ int retval;
/* Create symbolic link to the hotplug driver module */
kobj = kset_find_obj(module_kset, slot->mod_name);
@@ -254,82 +290,18 @@ static int fs_add_slot(struct hotplug_slot *slot, struct pci_slot *pci_slot)
kobject_put(kobj);
}
- if (has_power_file(slot)) {
- retval = sysfs_create_file(&pci_slot->kobj,
- &hotplug_slot_attr_power.attr);
- if (retval)
- goto exit_power;
- }
-
- if (has_attention_file(slot)) {
- retval = sysfs_create_file(&pci_slot->kobj,
- &hotplug_slot_attr_attention.attr);
- if (retval)
- goto exit_attention;
- }
-
- if (has_latch_file(slot)) {
- retval = sysfs_create_file(&pci_slot->kobj,
- &hotplug_slot_attr_latch.attr);
- if (retval)
- goto exit_latch;
- }
-
- if (has_adapter_file(slot)) {
- retval = sysfs_create_file(&pci_slot->kobj,
- &hotplug_slot_attr_presence.attr);
- if (retval)
- goto exit_adapter;
- }
-
- if (has_test_file(slot)) {
- retval = sysfs_create_file(&pci_slot->kobj,
- &hotplug_slot_attr_test.attr);
- if (retval)
- goto exit_test;
+ retval = sysfs_create_group(&pci_slot->kobj, &hotplug_slot_group);
+ if (retval) {
+ sysfs_remove_link(&pci_slot->kobj, "module");
+ return retval;
}
- goto exit;
-
-exit_test:
- if (has_adapter_file(slot))
- sysfs_remove_file(&pci_slot->kobj,
- &hotplug_slot_attr_presence.attr);
-exit_adapter:
- if (has_latch_file(slot))
- sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
-exit_latch:
- if (has_attention_file(slot))
- sysfs_remove_file(&pci_slot->kobj,
- &hotplug_slot_attr_attention.attr);
-exit_attention:
- if (has_power_file(slot))
- sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
-exit_power:
- sysfs_remove_link(&pci_slot->kobj, "module");
-exit:
- return retval;
+ return 0;
}
static void fs_remove_slot(struct hotplug_slot *slot, struct pci_slot *pci_slot)
{
- if (has_power_file(slot))
- sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
-
- if (has_attention_file(slot))
- sysfs_remove_file(&pci_slot->kobj,
- &hotplug_slot_attr_attention.attr);
-
- if (has_latch_file(slot))
- sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
-
- if (has_adapter_file(slot))
- sysfs_remove_file(&pci_slot->kobj,
- &hotplug_slot_attr_presence.attr);
-
- if (has_test_file(slot))
- sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_test.attr);
-
+ sysfs_remove_group(&pci_slot->kobj, &hotplug_slot_group);
sysfs_remove_link(&pci_slot->kobj, "module");
}
--
2.54.0