driver core: Fix concurrent problem of deferred_probe_extend_timeout()
PATCH v2 1/1 driver core: Fix concurrent problem of deferred_probe_extend_timeout()
The deferred_probe_timeout_work may be canceled forever unexpected when deferred_probe_extend_timeout() executes concurrently. Start with deferred_probe_timeout_work pending, and the problem would occur after the following sequence.
CPU0 CPU1deferred_probe_extend_timeout
-> cancel_delayed_work => true
deferred_probe_extend_timeout
-> cancel_delayed_wrok
-> __cancel_work
-> try_grab_pending -> schedule_delayed_work
-> queue_delayed_work_on
since pending bit is grabbed,
just return without doing anything
-> set_work_pool_and_clear_pending
this __cancel_work return false and the work would never be queued again
The root cause is that the PENDING_BIT of the work_struct would be set temporaily in __cancel_work and this bit could prevent the work_struct to be queued in another CPU.
Use deferred_probe_mutex to protect the cancel and queue operations for the deferred_probe_timeout_work to fix this problem.
--- drivers/base/dd.c | 1 + 1 file changed, 1 insertion(+)
drivers/base/dd.c +1 -0
@@ -323,6 +323,7 @@ static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_void deferred_probe_extend_timeout (void ) { +guard (mutex)(&deferred_probe_mutex);/* * If the work hasn't been queued yet or if the work expired, don't * start a new one.
Signature · 1 line
2.22.0
Greg KH
maintainerQuoted 39 lines · Wang Wensheng
The
deferred_probe_timeout_workmay be canceled forever unexpected whendeferred_probe_extend_timeout()executes concurrently. Start withdeferred_probe_timeout_workpending, and the problem would occur after the following sequence.CPU0 CPU1
deferred_probe_extend_timeout
->cancel_delayed_work=> truedeferred_probe_extend_timeout -> cancel_delayed_wrok -> __cancel_work -> try_grab_pending->
schedule_delayed_work
->queue_delayed_work_on
since pending bit is grabbed,
just return without doing anything
->set_work_pool_and_clear_pending
this__cancel_workreturn false and the work would never be queued againThe root cause is that the
PENDING_BITof thework_structwould be set temporaily in__cancel_workand this bit could prevent thework_structto be queued in another CPU.Use
deferred_probe_mutexto protect the cancel and queue operations for thedeferred_probe_timeout_workto fix this problem.Fixes 2b28a1a84a0e ("driver core: Extend deferred probe timeout on driver registration")Cc <[email protected]>Signed-off-by Wang Wensheng <[email protected]>--- drivers/base/dd.c | 1 + 1 file changed, 1 insertion(+)drivers/base/dd.c +0
-0
drivers/base/dd.c static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_ · line 326
Quoted 4 lines
drivers/base/dd.c +1
-0@@ -323,6 +323,7 @@ static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_void deferred_probe_extend_timeout (void ) { +guard (mutex)(&deferred_probe_mutex);
But if you grab the lock here, in the probe timeout function, the lock will be grabbed again, causing a deadlock, right? If not, why not?
Have you run this patch with lockdep enabled?
This feels broken to me, what am I missing?
thanks,
greg k-h
Quoted 22 lines · Greg KH
Quoted 20 lines · Wang Wensheng
The
deferred_probe_timeout_workmay be canceled forever unexpected whendeferred_probe_extend_timeout()executes concurrently. Start withdeferred_probe_timeout_workpending, and the problem would occur after the following sequence.CPU0 CPU1
deferred_probe_extend_timeout
->cancel_delayed_work=> truedeferred_probe_extend_timeout -> cancel_delayed_wrok -> __cancel_work -> try_grab_pending->
schedule_delayed_work
->queue_delayed_work_on
since pending bit is grabbed,
just return without doing anything
->set_work_pool_and_clear_pending
this__cancel_workreturn false and the work would never be queued again
Quoted 3 lines
Quoted 3 lines
The root cause is that the
PENDING_BITof thework_structwould be set temporaily in__cancel_workand this bit could prevent thework_structto be queued in another CPU.
This feels more like a workqueue API issue (this isn't too obvious from the documentation) or me misusing the workqueue API.
Is this issue still there if you use cancel_delayed_work_sync() instead of cancel_delayed_work()? If so, just switch to that and add proper comment on why it needs to by "sync".
-Saravana
Quoted 31 lines
Quoted 16 lines
Use
deferred_probe_mutexto protect the cancel and queue operations for thedeferred_probe_timeout_workto fix this problem.Fixes 2b28a1a84a0e ("driver core: Extend deferred probe timeout on driver registration")Cc <[email protected]>Signed-off-by Wang Wensheng <[email protected]>--- drivers/base/dd.c | 1 + 1 file changed, 1 insertion(+)drivers/base/dd.c +0
-0drivers/base/dd.c static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_ · line 326
Quoted 4 lines
drivers/base/dd.c +1
-0@@ -323,6 +323,7 @@ static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_void deferred_probe_extend_timeout (void ) { +guard (mutex)(&deferred_probe_mutex);But if you grab the lock here, in the probe timeout function, the lock will be grabbed again, causing a deadlock, right? If not, why not?
Have you run this patch with lockdep enabled?
This feels broken to me, what am I missing?
thanks,
greg k-h
Quoted 27 lines · Saravana Kannan
Quoted 22 lines · Greg KH
Quoted 20 lines · Wang Wensheng
The
deferred_probe_timeout_workmay be canceled forever unexpected whendeferred_probe_extend_timeout()executes concurrently. Start withdeferred_probe_timeout_workpending, and the problem would occur after the following sequence.CPU0 CPU1
deferred_probe_extend_timeout
->cancel_delayed_work=> truedeferred_probe_extend_timeout -> cancel_delayed_wrok -> __cancel_work -> try_grab_pending -> schedule_delayed_work -> queue_delayed_work_onsince pending bit is grabbed,
just return without doing anything
->set_work_pool_and_clear_pending
this__cancel_workreturn false and the work would never be queued againQuoted 3 lines
Quoted 3 lines
The root cause is that the
PENDING_BITof thework_structwould be set temporaily in__cancel_workand this bit could prevent thework_structto be queued in another CPU.
Quoted 9 lines
This feels more like a workqueue API issue (this isn't too obvious from the documentation) or me misusing the workqueue API.
Is this issue still there if you use
cancel_delayed_work_sync()instead ofcancel_delayed_work()? If so, just switch to that and add proper comment on why it needs to by "sync".-Saravana
cancel_delayed_work_sync() cannot solve the issue. Becasue this issue is to do with the interaction between cancel and queue operations for a work. The synchronization of the single cancel operation doesn't matter.
Quoted 32 lines
Quoted 31 lines
Quoted 16 lines
Use
deferred_probe_mutexto protect the cancel and queue operations for thedeferred_probe_timeout_workto fix this problem.Fixes 2b28a1a84a0e ("driver core: Extend deferred probe timeout on driver registration")Cc <[email protected]>Signed-off-by Wang Wensheng <[email protected]>--- drivers/base/dd.c | 1 + 1 file changed, 1 insertion(+)drivers/base/dd.c +0
-0drivers/base/dd.c static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_ · line 326
Quoted 4 lines
drivers/base/dd.c +1
-0@@ -323,6 +323,7 @@ static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_void deferred_probe_extend_timeout (void ) { +guard (mutex)(&deferred_probe_mutex);But if you grab the lock here, in the probe timeout function, the lock will be grabbed again, causing a deadlock, right? If not, why not?
Have you run this patch with lockdep enabled?
This feels broken to me, what am I missing?
thanks,
greg k-h
Quoted 41 lines · Greg KH
Quoted 40 lines · Wang Wensheng
The
deferred_probe_timeout_workmay be canceled forever unexpected whendeferred_probe_extend_timeout()executes concurrently. Start withdeferred_probe_timeout_workpending, and the problem would occur after the following sequence.CPU0 CPU1
deferred_probe_extend_timeout
->cancel_delayed_work=> truedeferred_probe_extend_timeout -> cancel_delayed_wrok -> __cancel_work -> try_grab_pending -> schedule_delayed_work -> queue_delayed_work_onsince pending bit is grabbed,
just return without doing anything
->set_work_pool_and_clear_pending
this__cancel_workreturn false and the work would never be queued againThe root cause is that the
PENDING_BITof thework_structwould be set temporaily in__cancel_workand this bit could prevent thework_structto be queued in another CPU.Use
deferred_probe_mutexto protect the cancel and queue operations for thedeferred_probe_timeout_workto fix this problem.Fixes 2b28a1a84a0e ("driver core: Extend deferred probe timeout on driver registration")Cc <[email protected]>Signed-off-by Wang Wensheng <[email protected]>--- drivers/base/dd.c | 1 + 1 file changed, 1 insertion(+)drivers/base/dd.c +0
-0@@ -323,6 +323,7 @@ static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_
Quoted 6 lines
drivers/base/dd.c static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_ · line 326
Quoted 3 lines
drivers/base/dd.c +1
-0void deferred_probe_extend_timeout (void ) { +guard (mutex)(&deferred_probe_mutex);But if you grab the lock here, in the probe timeout function, the lock will be grabbed again, causing a deadlock, right? If not, why not?
It's not a sync version of cancel_work, so the execuation of the work function doesn't block us here, nor does the schedule_delayed_work does.
Indead, deferred_probe_mutex is used to protect the deferred_probe_*_list, it looks better to use a new lock here. Right?
Quoted 9 lines
Have you run this patch with lockdep enabled?
This feels broken to me, what am I missing?
thanks,
greg k-h