Skip to contentSkip to conversation

PCI: hotplug: Add 'uevent' sysfs attribute to trigger slot events

PATCH Superseded Ramesh Errabolu 1 participant 2 messages Apr 1 – Apr 2

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: hotplug: Add 'uevent' sysfs attribute to trigger slot events

Add a write-only 'uevent' sysfs attribute for synthesizing uevents for a PCI slot. This extends the existing uevent support which emits a KOBJ_ADD uevent in pci_hp_add() with the ability to replay such uevents for cold plugged devices. As such events are only emitted by hotplug capable PCI slots so is the support for synthesizing them.

The change was validated by manually triggering 'add' uevent for a specific hotplug PCI slot:

    $ echo "add $(uuidgen)" | sudo tee   \
                /sys/bus/pci/slots/<slot-id>/uevent
---
 drivers/pci/hotplug/pci_hotplug_core.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/pci/hotplug/pci_hotplug_core.c +25 -0
index fadcf98a8a66..c3634b1cc7a8 100644
@@ -176,6 +176,21 @@ static struct pci_slot_attribute hotplug_slot_attr_presence = {
 	.show = presence_read_file,
 };
 
+static ssize_t uevent_write_file(struct pci_slot *slot,
+				 const char *buf, size_t len)
+{
+	int rc;
+
+	rc = kobject_synth_uevent(&slot->kobj, buf, len);
+	return rc ? rc : len;
+}
+
+static struct pci_slot_attribute hotplug_slot_attr_uevent = {
+	.attr = {.name = "uevent", .mode = S_IFREG | 0200},
+	.show = NULL,
+	.store = uevent_write_file
+};
+
 static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
 			       size_t count)
 {
@@ -254,6 +269,11 @@ static int fs_add_slot(struct hotplug_slot *slot, struct pci_slot *pci_slot)
 		kobject_put(kobj);
 	}
 
+	retval = sysfs_create_file(&pci_slot->kobj,
+				   &hotplug_slot_attr_uevent.attr);
+	if (retval)
+		goto exit_uevent;
+
 	if (has_power_file(slot)) {
 		retval = sysfs_create_file(&pci_slot->kobj,
 					   &hotplug_slot_attr_power.attr);
@@ -306,6 +326,9 @@ static int fs_add_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);
 exit_power:
+	sysfs_remove_file(&pci_slot->kobj,
+			  &hotplug_slot_attr_uevent.attr);
+exit_uevent:
 	sysfs_remove_link(&pci_slot->kobj, "module");
 exit:
 	return retval;
@@ -313,6 +336,8 @@ static int fs_add_slot(struct hotplug_slot *slot, struct pci_slot *pci_slot)
 
 static void fs_remove_slot(struct hotplug_slot *slot, struct pci_slot *pci_slot)
 {
+	sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_uevent.attr);
+
 	if (has_power_file(slot))
 		sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
 
Signature · 1 line
2.43.0
#1 lore raw

All,

I realized this patch should have been sent as revision (v2) rather than as new submission. The original v1 that prompted this update can be found here:

https://lore.kernel.org/all/[email protected]/

Thanks,
Ramesh

Quoted 75 lines · Ramesh Errabolu

Add a write-only 'uevent' sysfs attribute for synthesizing uevents for a PCI slot. This extends the existing uevent support which emits a KOBJ_ADD uevent in pci_hp_add() with the ability to replay such uevents for cold plugged devices. As such events are only emitted by hotplug capable PCI slots so is the support for synthesizing them.

The change was validated by manually triggering 'add' uevent for a specific hotplug PCI slot:

     $ echo "add $(uuidgen)" | sudo tee   \
                 /sys/bus/pci/slots/<slot-id>/uevent
---
  drivers/pci/hotplug/pci_hotplug_core.c | 25 +++++++++++++++++++++++++
  1 file changed, 25 insertions(+)
drivers/pci/hotplug/pci_hotplug_core.c +25 -0
index fadcf98a8a66..c3634b1cc7a8 100644
@@ -176,6 +176,21 @@ static struct pci_slot_attribute hotplug_slot_attr_presence = {
  	.show = presence_read_file,
  };
 
+static ssize_t uevent_write_file(struct pci_slot *slot,
+				 const char *buf, size_t len)
+{
+	int rc;
+
+	rc = kobject_synth_uevent(&slot->kobj, buf, len);
+	return rc ? rc : len;
+}
+
+static struct pci_slot_attribute hotplug_slot_attr_uevent = {
+	.attr = {.name = "uevent", .mode = S_IFREG | 0200},
+	.show = NULL,
+	.store = uevent_write_file
+};
+
  static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
  			       size_t count)
  {
@@ -254,6 +269,11 @@ static int fs_add_slot(struct hotplug_slot *slot, struct pci_slot *pci_slot)
  		kobject_put(kobj);
  	}
 
+	retval = sysfs_create_file(&pci_slot->kobj,
+				   &hotplug_slot_attr_uevent.attr);
+	if (retval)
+		goto exit_uevent;
+
  	if (has_power_file(slot)) {
  		retval = sysfs_create_file(&pci_slot->kobj,
  					   &hotplug_slot_attr_power.attr);
@@ -306,6 +326,9 @@ static int fs_add_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);
  exit_power:
+	sysfs_remove_file(&pci_slot->kobj,
+			  &hotplug_slot_attr_uevent.attr);
+exit_uevent:
  	sysfs_remove_link(&pci_slot->kobj, "module");
  exit:
  	return retval;
@@ -313,6 +336,8 @@ static int fs_add_slot(struct hotplug_slot *slot, struct pci_slot *pci_slot)
 
  static void fs_remove_slot(struct hotplug_slot *slot, struct pci_slot *pci_slot)
  {
+	sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_uevent.attr);
+
  	if (has_power_file(slot))
  		sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);