Skip to content

Commit f558b83

Browse files
committed
Merge tag 'driver-core-5.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core
Pull driver core updates from Greg KH: "Here is the set of driver core patches for 5.8-rc1. Not all that huge this release, just a number of small fixes and updates: - software node fixes - kobject now sends KOBJ_REMOVE when it is removed from sysfs, not when it is removed from memory (which could come much later) - device link additions and fixes based on testing on more devices - firmware core cleanups - other minor changes, full details in the shortlog All have been in linux-next for a while with no reported issues" * tag 'driver-core-5.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core: (23 commits) driver core: Update device link status correctly for SYNC_STATE_ONLY links firmware_loader: change enum fw_opt to u32 software node: implement software_node_unregister() kobject: send KOBJ_REMOVE uevent when the object is removed from sysfs driver core: Remove unnecessary is_fwnode_dev variable in device_add() drivers property: When no children in primary, try secondary driver core: platform: Fix spelling errors in platform.c driver core: Remove check in driver_deferred_probe_force_trigger() of: platform: Batch fwnode parsing when adding all top level devices driver core: fw_devlink: Add support for batching fwnode parsing driver core: Look for waiting consumers only for a fwnode's primary device driver core: Move code to the right part of the file Revert "Revert "driver core: Set fw_devlink to "permissive" behavior by default"" drivers: base: Fix NULL pointer exception in __platform_driver_probe() if a driver developer is foolish firmware_loader: move fw_fallback_config to a private kernel symbol namespace driver core: Add missing '\n' in log messages driver/base/soc: Use kobj_to_dev() API Add documentation on meaning of -EPROBE_DEFER driver core: platform: remove redundant assignment to variable ret debugfs: Use the correct style for SPDX License Identifier ...
2 parents 80ef846 + 8c3e315 commit f558b83

23 files changed

Lines changed: 303 additions & 144 deletions

File tree

‎Documentation/driver-api/driver-model/driver.rst‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Device Drivers
44

55
See the kerneldoc for the struct device_driver.
66

7-
87
Allocation
98
~~~~~~~~~~
109

@@ -167,9 +166,26 @@ the driver to that device.
167166

168167
A driver's probe() may return a negative errno value to indicate that
169168
the driver did not bind to this device, in which case it should have
170-
released all resources it allocated::
169+
released all resources it allocated.
170+
171+
Optionally, probe() may return -EPROBE_DEFER if the driver depends on
172+
resources that are not yet available (e.g., supplied by a driver that
173+
hasn't initialized yet). The driver core will put the device onto the
174+
deferred probe list and will try to call it again later. If a driver
175+
must defer, it should return -EPROBE_DEFER as early as possible to
176+
reduce the amount of time spent on setup work that will need to be
177+
unwound and reexecuted at a later time.
178+
179+
.. warning::
180+
-EPROBE_DEFER must not be returned if probe() has already created
181+
child devices, even if those child devices are removed again
182+
in a cleanup path. If -EPROBE_DEFER is returned after a child
183+
device has been registered, it may result in an infinite loop of
184+
.probe() calls to the same driver.
185+
186+
::
171187

172-
void (*sync_state)(struct device *dev);
188+
void (*sync_state) (struct device *dev);
173189

174190
sync_state is called only once for a device. It's called when all the consumer
175191
devices of the device have successfully probed. The list of consumers of the
@@ -212,6 +228,8 @@ over management of devices from the bootloader, the usage of sync_state() is
212228
not restricted to that. Use it whenever it makes sense to take an action after
213229
all the consumers of a device have probed::
214230

231+
::
232+
215233
int (*remove) (struct device *dev);
216234

217235
remove is called to unbind a driver from a device. This may be
@@ -224,11 +242,15 @@ not. It should free any resources allocated specifically for the
224242
device; i.e. anything in the device's driver_data field.
225243

226244
If the device is still present, it should quiesce the device and place
227-
it into a supported low-power state::
245+
it into a supported low-power state.
246+
247+
::
228248

229249
int (*suspend) (struct device *dev, pm_message_t state);
230250

231-
suspend is called to put the device in a low power state::
251+
suspend is called to put the device in a low power state.
252+
253+
::
232254

233255
int (*resume) (struct device *dev);
234256

‎drivers/base/base.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ extern char *make_class_name(const char *name, struct kobject *kobj);
153153
extern int devres_release_all(struct device *dev);
154154
extern void device_block_probing(void);
155155
extern void device_unblock_probing(void);
156+
extern void driver_deferred_probe_force_trigger(void);
156157

157158
/* /sys/devices directory */
158159
extern struct kset *devices_kset;

‎drivers/base/core.c‎

Lines changed: 179 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ static LIST_HEAD(wait_for_suppliers);
4949
static DEFINE_MUTEX(wfs_lock);
5050
static LIST_HEAD(deferred_sync);
5151
static unsigned int defer_sync_state_count = 1;
52+
static unsigned int defer_fw_devlink_count;
53+
static DEFINE_MUTEX(defer_fw_devlink_lock);
54+
static bool fw_devlink_is_permissive(void);
5255

5356
#ifdef CONFIG_SRCU
5457
static DEFINE_MUTEX(device_links_lock);
@@ -529,7 +532,7 @@ static void device_link_add_missing_supplier_links(void)
529532
int ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
530533
if (!ret)
531534
list_del_init(&dev->links.needs_suppliers);
532-
else if (ret != -ENODEV)
535+
else if (ret != -ENODEV || fw_devlink_is_permissive())
533536
dev->links.need_for_probe = false;
534537
}
535538
mutex_unlock(&wfs_lock);
@@ -643,9 +646,17 @@ static void device_links_missing_supplier(struct device *dev)
643646
{
644647
struct device_link *link;
645648

646-
list_for_each_entry(link, &dev->links.suppliers, c_node)
647-
if (link->status == DL_STATE_CONSUMER_PROBE)
649+
list_for_each_entry(link, &dev->links.suppliers, c_node) {
650+
if (link->status != DL_STATE_CONSUMER_PROBE)
651+
continue;
652+
653+
if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
648654
WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
655+
} else {
656+
WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
657+
WRITE_ONCE(link->status, DL_STATE_DORMANT);
658+
}
659+
}
649660
}
650661

651662
/**
@@ -684,11 +695,11 @@ int device_links_check_suppliers(struct device *dev)
684695
device_links_write_lock();
685696

686697
list_for_each_entry(link, &dev->links.suppliers, c_node) {
687-
if (!(link->flags & DL_FLAG_MANAGED) ||
688-
link->flags & DL_FLAG_SYNC_STATE_ONLY)
698+
if (!(link->flags & DL_FLAG_MANAGED))
689699
continue;
690700

691-
if (link->status != DL_STATE_AVAILABLE) {
701+
if (link->status != DL_STATE_AVAILABLE &&
702+
!(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
692703
device_links_missing_supplier(dev);
693704
ret = -EPROBE_DEFER;
694705
break;
@@ -949,11 +960,21 @@ static void __device_links_no_driver(struct device *dev)
949960
if (!(link->flags & DL_FLAG_MANAGED))
950961
continue;
951962

952-
if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
963+
if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
953964
device_link_drop_managed(link);
954-
else if (link->status == DL_STATE_CONSUMER_PROBE ||
955-
link->status == DL_STATE_ACTIVE)
965+
continue;
966+
}
967+
968+
if (link->status != DL_STATE_CONSUMER_PROBE &&
969+
link->status != DL_STATE_ACTIVE)
970+
continue;
971+
972+
if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
956973
WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
974+
} else {
975+
WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
976+
WRITE_ONCE(link->status, DL_STATE_DORMANT);
977+
}
957978
}
958979

959980
dev->links.status = DL_DEV_NO_DRIVER;
@@ -1162,6 +1183,150 @@ static void device_links_purge(struct device *dev)
11621183
device_links_write_unlock();
11631184
}
11641185

1186+
static u32 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1187+
static int __init fw_devlink_setup(char *arg)
1188+
{
1189+
if (!arg)
1190+
return -EINVAL;
1191+
1192+
if (strcmp(arg, "off") == 0) {
1193+
fw_devlink_flags = 0;
1194+
} else if (strcmp(arg, "permissive") == 0) {
1195+
fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1196+
} else if (strcmp(arg, "on") == 0) {
1197+
fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER;
1198+
} else if (strcmp(arg, "rpm") == 0) {
1199+
fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
1200+
DL_FLAG_PM_RUNTIME;
1201+
}
1202+
return 0;
1203+
}
1204+
early_param("fw_devlink", fw_devlink_setup);
1205+
1206+
u32 fw_devlink_get_flags(void)
1207+
{
1208+
return fw_devlink_flags;
1209+
}
1210+
1211+
static bool fw_devlink_is_permissive(void)
1212+
{
1213+
return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY;
1214+
}
1215+
1216+
static void fw_devlink_link_device(struct device *dev)
1217+
{
1218+
int fw_ret;
1219+
1220+
if (!fw_devlink_flags)
1221+
return;
1222+
1223+
mutex_lock(&defer_fw_devlink_lock);
1224+
if (!defer_fw_devlink_count)
1225+
device_link_add_missing_supplier_links();
1226+
1227+
/*
1228+
* The device's fwnode not having add_links() doesn't affect if other
1229+
* consumers can find this device as a supplier. So, this check is
1230+
* intentionally placed after device_link_add_missing_supplier_links().
1231+
*/
1232+
if (!fwnode_has_op(dev->fwnode, add_links))
1233+
goto out;
1234+
1235+
/*
1236+
* If fw_devlink is being deferred, assume all devices have mandatory
1237+
* suppliers they need to link to later. Then, when the fw_devlink is
1238+
* resumed, all these devices will get a chance to try and link to any
1239+
* suppliers they have.
1240+
*/
1241+
if (!defer_fw_devlink_count) {
1242+
fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
1243+
if (fw_ret == -ENODEV && fw_devlink_is_permissive())
1244+
fw_ret = -EAGAIN;
1245+
} else {
1246+
fw_ret = -ENODEV;
1247+
}
1248+
1249+
if (fw_ret == -ENODEV)
1250+
device_link_wait_for_mandatory_supplier(dev);
1251+
else if (fw_ret)
1252+
device_link_wait_for_optional_supplier(dev);
1253+
1254+
out:
1255+
mutex_unlock(&defer_fw_devlink_lock);
1256+
}
1257+
1258+
/**
1259+
* fw_devlink_pause - Pause parsing of fwnode to create device links
1260+
*
1261+
* Calling this function defers any fwnode parsing to create device links until
1262+
* fw_devlink_resume() is called. Both these functions are ref counted and the
1263+
* caller needs to match the calls.
1264+
*
1265+
* While fw_devlink is paused:
1266+
* - Any device that is added won't have its fwnode parsed to create device
1267+
* links.
1268+
* - The probe of the device will also be deferred during this period.
1269+
* - Any devices that were already added, but waiting for suppliers won't be
1270+
* able to link to newly added devices.
1271+
*
1272+
* Once fw_devlink_resume():
1273+
* - All the fwnodes that was not parsed will be parsed.
1274+
* - All the devices that were deferred probing will be reattempted if they
1275+
* aren't waiting for any more suppliers.
1276+
*
1277+
* This pair of functions, is mainly meant to optimize the parsing of fwnodes
1278+
* when a lot of devices that need to link to each other are added in a short
1279+
* interval of time. For example, adding all the top level devices in a system.
1280+
*
1281+
* For example, if N devices are added and:
1282+
* - All the consumers are added before their suppliers
1283+
* - All the suppliers of the N devices are part of the N devices
1284+
*
1285+
* Then:
1286+
*
1287+
* - With the use of fw_devlink_pause() and fw_devlink_resume(), each device
1288+
* will only need one parsing of its fwnode because it is guaranteed to find
1289+
* all the supplier devices already registered and ready to link to. It won't
1290+
* have to do another pass later to find one or more suppliers it couldn't
1291+
* find in the first parse of the fwnode. So, we'll only need O(N) fwnode
1292+
* parses.
1293+
*
1294+
* - Without the use of fw_devlink_pause() and fw_devlink_resume(), we would
1295+
* end up doing O(N^2) parses of fwnodes because every device that's added is
1296+
* guaranteed to trigger a parse of the fwnode of every device added before
1297+
* it. This O(N^2) parse is made worse by the fact that when a fwnode of a
1298+
* device is parsed, all it descendant devices might need to have their
1299+
* fwnodes parsed too (even if the devices themselves aren't added).
1300+
*/
1301+
void fw_devlink_pause(void)
1302+
{
1303+
mutex_lock(&defer_fw_devlink_lock);
1304+
defer_fw_devlink_count++;
1305+
mutex_unlock(&defer_fw_devlink_lock);
1306+
}
1307+
1308+
/** fw_devlink_resume - Resume parsing of fwnode to create device links
1309+
*
1310+
* This function is used in conjunction with fw_devlink_pause() and is ref
1311+
* counted. See documentation for fw_devlink_pause() for more details.
1312+
*/
1313+
void fw_devlink_resume(void)
1314+
{
1315+
mutex_lock(&defer_fw_devlink_lock);
1316+
if (!defer_fw_devlink_count) {
1317+
WARN(true, "Unmatched fw_devlink pause/resume!");
1318+
goto out;
1319+
}
1320+
1321+
defer_fw_devlink_count--;
1322+
if (defer_fw_devlink_count)
1323+
goto out;
1324+
1325+
device_link_add_missing_supplier_links();
1326+
driver_deferred_probe_force_trigger();
1327+
out:
1328+
mutex_unlock(&defer_fw_devlink_lock);
1329+
}
11651330
/* Device links support end. */
11661331

11671332
int (*platform_notify)(struct device *dev) = NULL;
@@ -2364,36 +2529,6 @@ static int device_private_init(struct device *dev)
23642529
return 0;
23652530
}
23662531

2367-
static u32 fw_devlink_flags;
2368-
static int __init fw_devlink_setup(char *arg)
2369-
{
2370-
if (!arg)
2371-
return -EINVAL;
2372-
2373-
if (strcmp(arg, "off") == 0) {
2374-
fw_devlink_flags = 0;
2375-
} else if (strcmp(arg, "permissive") == 0) {
2376-
fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
2377-
} else if (strcmp(arg, "on") == 0) {
2378-
fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER;
2379-
} else if (strcmp(arg, "rpm") == 0) {
2380-
fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
2381-
DL_FLAG_PM_RUNTIME;
2382-
}
2383-
return 0;
2384-
}
2385-
early_param("fw_devlink", fw_devlink_setup);
2386-
2387-
u32 fw_devlink_get_flags(void)
2388-
{
2389-
return fw_devlink_flags;
2390-
}
2391-
2392-
static bool fw_devlink_is_permissive(void)
2393-
{
2394-
return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY;
2395-
}
2396-
23972532
/**
23982533
* device_add - add device to device hierarchy.
23992534
* @dev: device.
@@ -2426,9 +2561,8 @@ int device_add(struct device *dev)
24262561
struct device *parent;
24272562
struct kobject *kobj;
24282563
struct class_interface *class_intf;
2429-
int error = -EINVAL, fw_ret;
2564+
int error = -EINVAL;
24302565
struct kobject *glue_dir = NULL;
2431-
bool is_fwnode_dev = false;
24322566

24332567
dev = get_device(dev);
24342568
if (!dev)
@@ -2526,11 +2660,6 @@ int device_add(struct device *dev)
25262660

25272661
kobject_uevent(&dev->kobj, KOBJ_ADD);
25282662

2529-
if (dev->fwnode && !dev->fwnode->dev) {
2530-
dev->fwnode->dev = dev;
2531-
is_fwnode_dev = true;
2532-
}
2533-
25342663
/*
25352664
* Check if any of the other devices (consumers) have been waiting for
25362665
* this device (supplier) to be added so that they can create a device
@@ -2539,19 +2668,13 @@ int device_add(struct device *dev)
25392668
* This needs to happen after device_pm_add() because device_link_add()
25402669
* requires the supplier be registered before it's called.
25412670
*
2542-
* But this also needs to happe before bus_probe_device() to make sure
2671+
* But this also needs to happen before bus_probe_device() to make sure
25432672
* waiting consumers can link to it before the driver is bound to the
25442673
* device and the driver sync_state callback is called for this device.
25452674
*/
2546-
device_link_add_missing_supplier_links();
2547-
2548-
if (fw_devlink_flags && is_fwnode_dev &&
2549-
fwnode_has_op(dev->fwnode, add_links)) {
2550-
fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
2551-
if (fw_ret == -ENODEV && !fw_devlink_is_permissive())
2552-
device_link_wait_for_mandatory_supplier(dev);
2553-
else if (fw_ret)
2554-
device_link_wait_for_optional_supplier(dev);
2675+
if (dev->fwnode && !dev->fwnode->dev) {
2676+
dev->fwnode->dev = dev;
2677+
fw_devlink_link_device(dev);
25552678
}
25562679

25572680
bus_probe_device(dev);

0 commit comments

Comments
 (0)