Skip to content

Commit 0462c56

Browse files
hcodinarobherring
authored andcommitted
driver core: Introduce device_link_wait_removal()
The commit 80dd33c ("drivers: base: Fix device link removal") introduces a workqueue to release the consumer and supplier devices used in the devlink. In the job queued, devices are release and in turn, when all the references to these devices are dropped, the release function of the device itself is called. Nothing is present to provide some synchronisation with this workqueue in order to ensure that all ongoing releasing operations are done and so, some other operations can be started safely. For instance, in the following sequence: 1) of_platform_depopulate() 2) of_overlay_remove() During the step 1, devices are released and related devlinks are removed (jobs pushed in the workqueue). During the step 2, OF nodes are destroyed but, without any synchronisation with devlink removal jobs, of_overlay_remove() can raise warnings related to missing of_node_put(): ERROR: memory leak, expected refcount 1 instead of 2 Indeed, the missing of_node_put() call is going to be done, too late, from the workqueue job execution. Introduce device_link_wait_removal() to offer a way to synchronize operations waiting for the end of devlink removals (i.e. end of workqueue jobs). Also, as a flushing operation is done on the workqueue, the workqueue used is moved from a system-wide workqueue to a local one. Cc: [email protected] Signed-off-by: Herve Codina <[email protected]> Tested-by: Luca Ceresoli <[email protected]> Reviewed-by: Nuno Sa <[email protected]> Reviewed-by: Saravana Kannan <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rob Herring <[email protected]>
1 parent 3d9b8e6 commit 0462c56

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

drivers/base/core.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static bool fw_devlink_is_permissive(void);
4444
static void __fw_devlink_link_to_consumers(struct device *dev);
4545
static bool fw_devlink_drv_reg_done;
4646
static bool fw_devlink_best_effort;
47+
static struct workqueue_struct *device_link_wq;
4748

4849
/**
4950
* __fwnode_link_add - Create a link between two fwnode_handles.
@@ -533,12 +534,26 @@ static void devlink_dev_release(struct device *dev)
533534
/*
534535
* It may take a while to complete this work because of the SRCU
535536
* synchronization in device_link_release_fn() and if the consumer or
536-
* supplier devices get deleted when it runs, so put it into the "long"
537-
* workqueue.
537+
* supplier devices get deleted when it runs, so put it into the
538+
* dedicated workqueue.
538539
*/
539-
queue_work(system_long_wq, &link->rm_work);
540+
queue_work(device_link_wq, &link->rm_work);
540541
}
541542

543+
/**
544+
* device_link_wait_removal - Wait for ongoing devlink removal jobs to terminate
545+
*/
546+
void device_link_wait_removal(void)
547+
{
548+
/*
549+
* devlink removal jobs are queued in the dedicated work queue.
550+
* To be sure that all removal jobs are terminated, ensure that any
551+
* scheduled work has run to completion.
552+
*/
553+
flush_workqueue(device_link_wq);
554+
}
555+
EXPORT_SYMBOL_GPL(device_link_wait_removal);
556+
542557
static struct class devlink_class = {
543558
.name = "devlink",
544559
.dev_groups = devlink_groups,
@@ -4164,9 +4179,14 @@ int __init devices_init(void)
41644179
sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
41654180
if (!sysfs_dev_char_kobj)
41664181
goto char_kobj_err;
4182+
device_link_wq = alloc_workqueue("device_link_wq", 0, 0);
4183+
if (!device_link_wq)
4184+
goto wq_err;
41674185

41684186
return 0;
41694187

4188+
wq_err:
4189+
kobject_put(sysfs_dev_char_kobj);
41704190
char_kobj_err:
41714191
kobject_put(sysfs_dev_block_kobj);
41724192
block_kobj_err:

include/linux/device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,7 @@ void device_link_del(struct device_link *link);
12471247
void device_link_remove(void *consumer, struct device *supplier);
12481248
void device_links_supplier_sync_state_pause(void);
12491249
void device_links_supplier_sync_state_resume(void);
1250+
void device_link_wait_removal(void);
12501251

12511252
/* Create alias, so I can be autoloaded. */
12521253
#define MODULE_ALIAS_CHARDEV(major,minor) \

0 commit comments

Comments
 (0)