Skip to content

Commit

Permalink
drivers: base: Fix device link removal
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rafaeljw authored and gregkh committed Jun 3, 2021
1 parent 8d83f10 commit d007150
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
37 changes: 23 additions & 14 deletions drivers/base/core.c
Expand Up @@ -83,6 +83,11 @@ int device_links_read_lock_held(void)
{
return srcu_read_lock_held(&device_links_srcu);
}

static void device_link_synchronize_removal(void)
{
synchronize_srcu(&device_links_srcu);
}
#else /* !CONFIG_SRCU */
static DECLARE_RWSEM(device_links_lock);

Expand Down Expand Up @@ -113,6 +118,10 @@ int device_links_read_lock_held(void)
return lockdep_is_held(&device_links_lock);
}
#endif

static inline void device_link_synchronize_removal(void)
{
}
#endif /* !CONFIG_SRCU */

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

static void device_link_free(struct device_link *link)
static void device_link_release_fn(struct work_struct *work)
{
struct device_link *link = container_of(work, struct device_link, rm_work);

/* Ensure that all references to the link object have been dropped. */
device_link_synchronize_removal();

while (refcount_dec_not_one(&link->rpm_active))
pm_runtime_put(link->supplier);

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

#ifdef CONFIG_SRCU
static void __device_link_free_srcu(struct rcu_head *rhead)
{
device_link_free(container_of(rhead, struct device_link, rcu_head));
}

static void devlink_dev_release(struct device *dev)
{
struct device_link *link = to_devlink(dev);

call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
}
#else
static void devlink_dev_release(struct device *dev)
{
device_link_free(to_devlink(dev));
INIT_WORK(&link->rm_work, device_link_release_fn);
/*
* It may take a while to complete this work because of the SRCU
* synchronization in device_link_release_fn() and if the consumer or
* supplier devices get deleted when it runs, so put it into the "long"
* workqueue.
*/
queue_work(system_long_wq, &link->rm_work);
}
#endif

static struct class devlink_class = {
.name = "devlink",
Expand Down
6 changes: 2 additions & 4 deletions include/linux/device.h
Expand Up @@ -570,7 +570,7 @@ struct device {
* @flags: Link flags.
* @rpm_active: Whether or not the consumer device is runtime-PM-active.
* @kref: Count repeated addition of the same link.
* @rcu_head: An RCU head to use for deferred execution of SRCU callbacks.
* @rm_work: Work structure used for removing the link.
* @supplier_preactivated: Supplier has been made active before consumer probe.
*/
struct device_link {
Expand All @@ -583,9 +583,7 @@ struct device_link {
u32 flags;
refcount_t rpm_active;
struct kref kref;
#ifdef CONFIG_SRCU
struct rcu_head rcu_head;
#endif
struct work_struct rm_work;
bool supplier_preactivated; /* Owned by consumer probe. */
};

Expand Down

0 comments on commit d007150

Please sign in to comment.