Skip to content

Commit 80dd33c

Browse files
rafaeljwgregkh
authored andcommitted
drivers: base: Fix device link removal
When device_link_free() drops references to the supplier and consumer devices of the device link going away and the reference being dropped turns out to be the last one for any of those device objects, its ->release callback will be invoked and it may sleep which goes against the SRCU callback execution requirements. To address this issue, make the device link removal code carry out the device_link_free() actions preceded by SRCU synchronization from a separate work item (the "long" workqueue is used for that, because it does not matter when the device link memory is released and it may take time to get to that point) instead of using SRCU callbacks. While at it, make the code work analogously when SRCU is not enabled to reduce the differences between the SRCU and non-SRCU cases. Fixes: 843e600 ("driver core: Fix sleeping in invalid context during device link deletion") Cc: stable <[email protected]> Reported-by: chenxiang (M) <[email protected]> Tested-by: chenxiang (M) <[email protected]> Reviewed-by: Saravana Kannan <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]> Link: https://lore.kernel.org/r/5722787.lOV4Wx5bFT@kreacher Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 5881fa8 commit 80dd33c

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

drivers/base/core.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ int device_links_read_lock_held(void)
194194
{
195195
return srcu_read_lock_held(&device_links_srcu);
196196
}
197+
198+
static void device_link_synchronize_removal(void)
199+
{
200+
synchronize_srcu(&device_links_srcu);
201+
}
197202
#else /* !CONFIG_SRCU */
198203
static DECLARE_RWSEM(device_links_lock);
199204

@@ -224,6 +229,10 @@ int device_links_read_lock_held(void)
224229
return lockdep_is_held(&device_links_lock);
225230
}
226231
#endif
232+
233+
static inline void device_link_synchronize_removal(void)
234+
{
235+
}
227236
#endif /* !CONFIG_SRCU */
228237

229238
static bool device_is_ancestor(struct device *dev, struct device *target)
@@ -445,8 +454,13 @@ static struct attribute *devlink_attrs[] = {
445454
};
446455
ATTRIBUTE_GROUPS(devlink);
447456

448-
static void device_link_free(struct device_link *link)
457+
static void device_link_release_fn(struct work_struct *work)
449458
{
459+
struct device_link *link = container_of(work, struct device_link, rm_work);
460+
461+
/* Ensure that all references to the link object have been dropped. */
462+
device_link_synchronize_removal();
463+
450464
while (refcount_dec_not_one(&link->rpm_active))
451465
pm_runtime_put(link->supplier);
452466

@@ -455,24 +469,19 @@ static void device_link_free(struct device_link *link)
455469
kfree(link);
456470
}
457471

458-
#ifdef CONFIG_SRCU
459-
static void __device_link_free_srcu(struct rcu_head *rhead)
460-
{
461-
device_link_free(container_of(rhead, struct device_link, rcu_head));
462-
}
463-
464472
static void devlink_dev_release(struct device *dev)
465473
{
466474
struct device_link *link = to_devlink(dev);
467475

468-
call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
469-
}
470-
#else
471-
static void devlink_dev_release(struct device *dev)
472-
{
473-
device_link_free(to_devlink(dev));
476+
INIT_WORK(&link->rm_work, device_link_release_fn);
477+
/*
478+
* It may take a while to complete this work because of the SRCU
479+
* synchronization in device_link_release_fn() and if the consumer or
480+
* supplier devices get deleted when it runs, so put it into the "long"
481+
* workqueue.
482+
*/
483+
queue_work(system_long_wq, &link->rm_work);
474484
}
475-
#endif
476485

477486
static struct class devlink_class = {
478487
.name = "devlink",

include/linux/device.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ struct device {
570570
* @flags: Link flags.
571571
* @rpm_active: Whether or not the consumer device is runtime-PM-active.
572572
* @kref: Count repeated addition of the same link.
573-
* @rcu_head: An RCU head to use for deferred execution of SRCU callbacks.
573+
* @rm_work: Work structure used for removing the link.
574574
* @supplier_preactivated: Supplier has been made active before consumer probe.
575575
*/
576576
struct device_link {
@@ -583,9 +583,7 @@ struct device_link {
583583
u32 flags;
584584
refcount_t rpm_active;
585585
struct kref kref;
586-
#ifdef CONFIG_SRCU
587-
struct rcu_head rcu_head;
588-
#endif
586+
struct work_struct rm_work;
589587
bool supplier_preactivated; /* Owned by consumer probe. */
590588
};
591589

0 commit comments

Comments
 (0)