Skip to content

Commit 843e600

Browse files
Saravana Kannangregkh
authored andcommitted
driver core: Fix sleeping in invalid context during device link deletion
Marek and Guenter reported that commit 287905e ("driver core: Expose device link details in sysfs") caused sleeping/scheduling while atomic warnings. BUG: sleeping function called from invalid context at kernel/locking/mutex.c:935 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 12, name: kworker/0:1 2 locks held by kworker/0:1/12: #0: ee8074a8 ((wq_completion)rcu_gp){+.+.}-{0:0}, at: process_one_work+0x174/0x7dc #1: ee921f20 ((work_completion)(&sdp->work)){+.+.}-{0:0}, at: process_one_work+0x174/0x7dc Preemption disabled at: [<c01b10f0>] srcu_invoke_callbacks+0xc0/0x154 ----- 8< ----- SNIP [<c064590c>] (device_del) from [<c0645c9c>] (device_unregister+0x24/0x64) [<c0645c9c>] (device_unregister) from [<c01b10fc>] (srcu_invoke_callbacks+0xcc/0x154) [<c01b10fc>] (srcu_invoke_callbacks) from [<c01493c4>] (process_one_work+0x234/0x7dc) [<c01493c4>] (process_one_work) from [<c01499b0>] (worker_thread+0x44/0x51c) [<c01499b0>] (worker_thread) from [<c0150bf4>] (kthread+0x158/0x1a0) [<c0150bf4>] (kthread) from [<c0100114>] (ret_from_fork+0x14/0x20) Exception stack(0xee921fb0 to 0xee921ff8) This was caused by the device link device being released in the context of srcu_invoke_callbacks(). There is no need to wait till the RCU callback to release the device link device. So release the device earlier and move the call_srcu() into the device release code. That way, the memory will get freed only after the device is released AND the RCU callback is called. Fixes: 287905e ("driver core: Expose device link details in sysfs") Reported-by: Marek Szyprowski <[email protected]> Reported-by: Guenter Roeck <[email protected]> Reported-by: Naresh Kamboju <[email protected]> Signed-off-by: Saravana Kannan <[email protected]> Tested-by: Marek Szyprowski <[email protected]> Tested-by: Guenter Roeck <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 6bdb486 commit 843e600

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

drivers/base/core.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,34 @@ static struct attribute *devlink_attrs[] = {
307307
};
308308
ATTRIBUTE_GROUPS(devlink);
309309

310+
static void device_link_free(struct device_link *link)
311+
{
312+
while (refcount_dec_not_one(&link->rpm_active))
313+
pm_runtime_put(link->supplier);
314+
315+
put_device(link->consumer);
316+
put_device(link->supplier);
317+
kfree(link);
318+
}
319+
320+
#ifdef CONFIG_SRCU
321+
static void __device_link_free_srcu(struct rcu_head *rhead)
322+
{
323+
device_link_free(container_of(rhead, struct device_link, rcu_head));
324+
}
325+
310326
static void devlink_dev_release(struct device *dev)
311327
{
312-
kfree(to_devlink(dev));
328+
struct device_link *link = to_devlink(dev);
329+
330+
call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
313331
}
332+
#else
333+
static void devlink_dev_release(struct device *dev)
334+
{
335+
device_link_free(to_devlink(dev));
336+
}
337+
#endif
314338

315339
static struct class devlink_class = {
316340
.name = "devlink",
@@ -731,22 +755,7 @@ static void device_link_add_missing_supplier_links(void)
731755
mutex_unlock(&wfs_lock);
732756
}
733757

734-
static void device_link_free(struct device_link *link)
735-
{
736-
while (refcount_dec_not_one(&link->rpm_active))
737-
pm_runtime_put(link->supplier);
738-
739-
put_device(link->consumer);
740-
put_device(link->supplier);
741-
device_unregister(&link->link_dev);
742-
}
743-
744758
#ifdef CONFIG_SRCU
745-
static void __device_link_free_srcu(struct rcu_head *rhead)
746-
{
747-
device_link_free(container_of(rhead, struct device_link, rcu_head));
748-
}
749-
750759
static void __device_link_del(struct kref *kref)
751760
{
752761
struct device_link *link = container_of(kref, struct device_link, kref);
@@ -759,7 +768,7 @@ static void __device_link_del(struct kref *kref)
759768

760769
list_del_rcu(&link->s_node);
761770
list_del_rcu(&link->c_node);
762-
call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
771+
device_unregister(&link->link_dev);
763772
}
764773
#else /* !CONFIG_SRCU */
765774
static void __device_link_del(struct kref *kref)
@@ -774,7 +783,7 @@ static void __device_link_del(struct kref *kref)
774783

775784
list_del(&link->s_node);
776785
list_del(&link->c_node);
777-
device_link_free(link);
786+
device_unregister(&link->link_dev);
778787
}
779788
#endif /* !CONFIG_SRCU */
780789

0 commit comments

Comments
 (0)