Skip to contentSkip to conversation

Give more control of sync_state()

PATCH New Saravana Kannan 2 participants 4 messages Mar 4, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Give more control of sync_state()

In systems where some devices don't have drivers, sync_state() will never get called for suppliers of those devices. This is working as intended since those consumer devices might be powered on, and cutting resources to those consumer devices might make the system unstable.

However, not all systems will the same concern. For example, the consumer device might have been left powered off and unused. In such cases, sync_state() never getting called might cause an unnecessary power regression if the bootloader had left the supplier in a powered on state.

So give more control of sync_state() in the form of a kernel commandline for a global timeout or a per device sysfs control to trigger sync_state().

These patches have been tested on my end and seem to work well.

Thanks,
Saravana

v1->v2:
Patch 1: Updated commit text, documentation and log message. Patch 2: Check for "1" in the write, updated doc, fix error handling.

Cc Abel Vesa <[email protected]>
Cc Bjorn Andersson <[email protected]>
Cc Dmitry Baryshkov <[email protected]>
Cc Doug Anderson <[email protected]>
Cc Matthias Kaehlcke <[email protected]>

Saravana Kannan (2):
driver core: Add fw_devlink.sync_state command line param driver core: Make state_synced device attribute writeable

 .../ABI/testing/sysfs-devices-state_synced    |  5 ++
 .../admin-guide/kernel-parameters.txt         | 14 +++++
 drivers/base/base.h                           |  9 +++
 drivers/base/core.c                           | 63 +++++++++++++++++--
 drivers/base/dd.c                             | 29 ++++++++-
 5 files changed, 115 insertions(+), 5 deletions(-)
Signature · 1 line
2.40.0.rc0.216.gc4246ad0f0-goog
#1 lore raw

PATCH v2 1/2 driver core: Add fw_devlink.sync_state command line param

When all devices that could probe have finished probing (based on deferred_probe_timeout configuration or late_initcall() when !CONFIG_MODULES), this parameter controls what to do with devices that haven't yet received their sync_state() calls.

fw_devlink.sync_state=strict is the default and the driver core will continue waiting on all consumers of a device to probe successfully before sync_state() is called for the device. This is the default behavior since calling sync_state() on a device when all its consumers haven't probed could make some systems unusable/unstable. When this option is selected, we also print the list of devices that haven't had sync_state() called on them by the time all devices the could probe have finished probing.

fw_devlink.sync_state=timeout will cause the driver core to give up waiting on consumers and call sync_state() on any devices that haven't yet received their sync_state() calls. This option is provided for systems that won't become unusable/unstable as they might be able to save power (depends on state of hardware before kernel starts) if all devices get their sync_state().

---
 .../admin-guide/kernel-parameters.txt         | 14 +++++
 drivers/base/base.h                           |  1 +
 drivers/base/core.c                           | 58 +++++++++++++++++++
 drivers/base/dd.c                             |  6 ++
 4 files changed, 79 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +14 -0
index 6221a1d057dd..fc5dc4dc9a5c 100644
@@ -1582,6 +1582,20 @@
 			dependencies. This only applies for fw_devlink=on|rpm.
 			Format: <bool>
 
+	fw_devlink.sync_state =
+			[KNL] When all devices that could probe have finished
+			probing, this parameter controls what to do with
+			devices that haven't yet received their sync_state()
+			calls.
+			Format: { strict | timeout }
+			strict -- Default. Continue waiting on consumers to
+				probe successfully.
+			timeout -- Give up waiting on consumers and call
+				sync_state() on any devices that haven't yet
+				received their sync_state() calls after
+				deferred_probe_timeout has expired or by
+				late_initcall() if !CONFIG_MODULES.
+
 	gamecon.map[2|3]=
 			[HW,JOY] Multisystem joystick and NES/SNES/PSX pad
 			support via parallel port (up to 5 devices per port)
drivers/base/base.h +1 -0
index 726a12a244c0..6fcd71803d35 100644
@@ -209,6 +209,7 @@ extern void device_links_no_driver(struct device *dev);
 extern bool device_links_busy(struct device *dev);
 extern void device_links_unbind_consumers(struct device *dev);
 extern void fw_devlink_drivers_done(void);
+extern void fw_devlink_probing_done(void);
 
 /* device pm support */
 void device_pm_move_to_tail(struct device *dev);
drivers/base/core.c +58 -0

70 lines not on this page · raw message

drivers/base/dd.c +6 -0
index 8def2ba08a82..84f07e0050dd 100644
@@ -315,6 +315,8 @@ static void deferred_probe_timeout_work_func(struct work_struct *work)
 	list_for_each_entry(p, &deferred_probe_pending_list, deferred_probe)
 		dev_info(p->device, "deferred probe pending\n");
 	mutex_unlock(&deferred_probe_mutex);
+
+	fw_devlink_probing_done();
 }
 static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
 
@@ -364,6 +366,10 @@ static int deferred_probe_initcall(void)
 		schedule_delayed_work(&deferred_probe_timeout_work,
 			driver_deferred_probe_timeout * HZ);
 	}
+
+	if (!IS_ENABLED(CONFIG_MODULES))
+		fw_devlink_probing_done();
+
 	return 0;
 }
 late_initcall(deferred_probe_initcall);
Signature · 1 line
2.40.0.rc0.216.gc4246ad0f0-goog
#2 lore raw

[lkp] [+298 bytes kernel size regression] [i386-tinyconfig] [6c154242b9] driver core: Add fw_devlink.sync_state command line param

FYI, we noticed a +298 bytes kernel size regression due to commit:

FYI, we noticed a +298 bytes kernel size regression due to commit:

commit: 6c154242b90e7467a9df29368ee05a542f3e1d20 (driver core: Add fw_devlink.sync_state command line param)
url: https://github.com/intel-lab-lkp/linux/commits/Saravana-Kannan/driver-core-Add-fw_devlink-sync_state-command-line-param/20230304-085552
base: https://git.kernel.org/cgit/linux/kernel/git/gregkh/driver-core.git 2eb29d59ddf02e39774abfb60b2030b0b7e27c1f patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH v2 1/2] driver core: Add fw_devlink.sync_state command line param

Details as below (size data is obtained by nm --size-sort vmlinux):

2eb29d59: Merge tag 'drm-next-2023-03-03-1' of git://anongit.freedesktop.org/drm/drm
6c154242: driver core: Add fw_devlink.sync_state command line param

+----------------------------------------------+----------+----------+-------+
|                    symbol                    | 2eb29d59 | 6c154242 | delta |
+----------------------------------------------+----------+----------+-------+
| bzImage                                      | 501536   | 501760   | 224   |
| nm.t.fw_devlink_dev_sync_state               | 0        | 117      | 117   |
| nm.t.fw_devlink_sync_state_setup             | 0        | 74       | 74    |
| nm.T.fw_devlink_probing_done                 | 0        | 56       | 56    |
| nm.d.__setup_str_fw_devlink_sync_state_setup | 0        | 22       | 22    |
| nm.d.__setup_fw_devlink_sync_state_setup     | 0        | 12       | 12    |
| nm.t.deferred_probe_timeout_work_func        | 70       | 75       | 5     |
| nm.t.deferred_probe_initcall                 | 72       | 77       | 5     |
| nm.b.fw_devlink_sync_state                   | 0        | 4        | 4     |
| nm.t.klist_children_get                      | 5        | 8        | 3     |
+----------------------------------------------+----------+----------+-------+

Thanks

#3 lore raw

PATCH v2 2/2 driver core: Make state_synced device attribute writeable

In reply to Saravana Kannan

If the file is written to and sync_state() hasn't been called for the device yet, then call sync_state() for the device independent of the state of its consumers.

This is useful for supplier devices that have one or more consumers that don't have a driver but the consumers are in a state that don't use the resources supplied by the supplier device.

This gives finer grained control than using the fw_devlink.sync_state=timeout kernel commandline parameter.

---
 .../ABI/testing/sysfs-devices-state_synced    |  5 ++++
 drivers/base/base.h                           |  8 +++++++
 drivers/base/core.c                           |  5 +---
 drivers/base/dd.c                             | 23 ++++++++++++++++++-
 4 files changed, 36 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-devices-state_synced +5 -0
index 0c922d7d02fc..c64636ddac41 100644
@@ -21,4 +21,9 @@ Description:
 		at the time the kernel starts are not affected or limited in
 		any way by sync_state() callbacks.
 
+		Writing "1" to this file will force a call to the device's
+		sync_state() function if it hasn't been called already. The
+		sync_state() call happens independent of the state of the
+		consumer devices.
+
 
drivers/base/base.h +8 -0
index 6fcd71803d35..b055eba1ec30 100644
@@ -164,6 +164,14 @@ static inline int driver_match_device(struct device_driver *drv,
 	return drv->bus->match ? drv->bus->match(dev, drv) : 1;
 }
 
+static inline void dev_sync_state(struct device *dev)
+{
+	if (dev->bus->sync_state)
+		dev->bus->sync_state(dev);
+	else if (dev->driver && dev->driver->sync_state)
+		dev->driver->sync_state(dev);
+}
+
 extern int driver_add_groups(struct device_driver *drv,
 			     const struct attribute_group **groups);
 extern void driver_remove_groups(struct device_driver *drv,
drivers/base/core.c +1 -4
index 4c7e1550bb02..c74e6a40f0de 100644
@@ -1173,10 +1173,7 @@ static void device_links_flush_sync_list(struct list_head *list,
 		if (dev != dont_lock_dev)
 			device_lock(dev);
 
-		if (dev->bus->sync_state)
-			dev->bus->sync_state(dev);
-		else if (dev->driver && dev->driver->sync_state)
-			dev->driver->sync_state(dev);
+		dev_sync_state(dev);
 
 		if (dev != dont_lock_dev)
 			device_unlock(dev);
drivers/base/dd.c +22 -1
index 84f07e0050dd..7b9ab2050b84 100644
@@ -510,6 +510,27 @@ EXPORT_SYMBOL_GPL(device_bind_driver);
 static atomic_t probe_count = ATOMIC_INIT(0);
 static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
 
+static ssize_t state_synced_store(struct device *dev,
+				  struct device_attribute *attr,
+				  const char *buf, size_t count)
+{
+	int ret = 0;
+
+	if (strcmp("1", buf))
+		return -EINVAL;
+
+	device_lock(dev);
+	if (!dev->state_synced) {
+		dev->state_synced = true;
+		dev_sync_state(dev);
+	} else {
+		ret = -EINVAL;
+	}
+	device_unlock(dev);
+
+	return ret ? ret : count;
+}
+
 static ssize_t state_synced_show(struct device *dev,
 				 struct device_attribute *attr, char *buf)
 {
@@ -521,7 +542,7 @@ static ssize_t state_synced_show(struct device *dev,
 
 	return sysfs_emit(buf, "%u\n", val);
 }
-static DEVICE_ATTR_RO(state_synced);
+static DEVICE_ATTR_RW(state_synced);
 
 static void device_unbind_cleanup(struct device *dev)
 {
Signature · 1 line
2.40.0.rc0.216.gc4246ad0f0-goog