Skip to content

Commit d007150

Browse files
rafaeljwgregkh
authored andcommitted
drivers: base: Fix device link removal
commit 80dd33c upstream. 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 <stable@vger.kernel.org> Reported-by: chenxiang (M) <chenxiang66@hisilicon.com> Tested-by: chenxiang (M) <chenxiang66@hisilicon.com> Reviewed-by: Saravana Kannan <saravanak@google.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Link: https://lore.kernel.org/r/5722787.lOV4Wx5bFT@kreacher Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8d83f10 commit d007150

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

‎drivers/base/core.c‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ int device_links_read_lock_held(void)
8383
{
8484
return srcu_read_lock_held(&device_links_srcu);
8585
}
86+
87+
static void device_link_synchronize_removal(void)
88+
{
89+
synchronize_srcu(&device_links_srcu);
90+
}
8691
#else /* !CONFIG_SRCU */
8792
static DECLARE_RWSEM(device_links_lock);
8893

@@ -113,6 +118,10 @@ int device_links_read_lock_held(void)
113118
return lockdep_is_held(&device_links_lock);
114119
}
115120
#endif
121+
122+
static inline void device_link_synchronize_removal(void)
123+
{
124+
}
116125
#endif /* !CONFIG_SRCU */
117126

118127
static bool device_is_ancestor(struct device *dev, struct device *target)
@@ -332,8 +341,13 @@ static struct attribute *devlink_attrs[] = {
332341
};
333342
ATTRIBUTE_GROUPS(devlink);
334343

335-
static void device_link_free(struct device_link *link)
344+
static void device_link_release_fn(struct work_struct *work)
336345
{
346+
struct device_link *link = container_of(work, struct device_link, rm_work);
347+
348+
/* Ensure that all references to the link object have been dropped. */
349+
device_link_synchronize_removal();
350+
337351
while (refcount_dec_not_one(&link->rpm_active))
338352
pm_runtime_put(link->supplier);
339353

@@ -342,24 +356,19 @@ static void device_link_free(struct device_link *link)
342356
kfree(link);
343357
}
344358

345-
#ifdef CONFIG_SRCU
346-
static void __device_link_free_srcu(struct rcu_head *rhead)
347-
{
348-
device_link_free(container_of(rhead, struct device_link, rcu_head));
349-
}
350-
351359
static void devlink_dev_release(struct device *dev)
352360
{
353361
struct device_link *link = to_devlink(dev);
354362

355-
call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
356-
}
357-
#else
358-
static void devlink_dev_release(struct device *dev)
359-
{
360-
device_link_free(to_devlink(dev));
363+
INIT_WORK(&link->rm_work, device_link_release_fn);
364+
/*
365+
* It may take a while to complete this work because of the SRCU
366+
* synchronization in device_link_release_fn() and if the consumer or
367+
* supplier devices get deleted when it runs, so put it into the "long"
368+
* workqueue.
369+
*/
370+
queue_work(system_long_wq, &link->rm_work);
361371
}
362-
#endif
363372

364373
static struct class devlink_class = {
365374
.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)