Skip to contentSkip to conversation

driver core: Fix concurrent problem of deferred_probe_extend_timeout()

PATCH In review Wang Wensheng 3 participants 5 messages Aug 14, 2025 – Aug 15, 2025 Driver core

lore
Share & tools
lore
#0 lore raw

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 CPU1
deferred_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.

Fixes 2b28a1a84a0e ("driver core: Extend deferred probe timeout on driver registration")
---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
drivers/base/dd.c +1 -0
index 13ab98e033ea..00419d2ee910 100644
@@ -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
maintainer
#1 lore raw
Quoted 39 lines · Wang Wensheng

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 CPU1
deferred_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.

Fixes 2b28a1a84a0e ("driver core: Extend deferred probe timeout on driver registration")
---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
drivers/base/dd.c +0 -0
index 13ab98e033ea..00419d2ee910 100644

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

#2 lore raw
Quoted 22 lines · Greg KH
Quoted 20 lines · Wang Wensheng

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 CPU1
deferred_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

Quoted 3 lines
Quoted 3 lines

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.

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_mutex to protect the cancel and queue operations for the deferred_probe_timeout_work to fix this problem.

Fixes 2b28a1a84a0e ("driver core: Extend deferred probe timeout on driver registration")
---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
drivers/base/dd.c +0 -0
index 13ab98e033ea..00419d2ee910 100644

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

#3 lore raw
Quoted 27 lines · Saravana Kannan
Quoted 22 lines · Greg KH
Quoted 20 lines · Wang Wensheng

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 CPU1
deferred_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

Quoted 3 lines
Quoted 3 lines

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.

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 of cancel_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_mutex to protect the cancel and queue operations for the deferred_probe_timeout_work to fix this problem.

Fixes 2b28a1a84a0e ("driver core: Extend deferred probe timeout on driver registration")
---
  drivers/base/dd.c | 1 +
  1 file changed, 1 insertion(+)
drivers/base/dd.c +0 -0
index 13ab98e033ea..00419d2ee910 100644

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

#4 lore raw
In reply to Greg KH
Quoted 41 lines · Greg KH
Quoted 40 lines · Wang Wensheng

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 CPU1
deferred_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.

Fixes 2b28a1a84a0e ("driver core: Extend deferred probe timeout on driver registration")
---
  drivers/base/dd.c | 1 +
  1 file changed, 1 insertion(+)
drivers/base/dd.c +0 -0
index 13ab98e033ea..00419d2ee910 100644
@@ -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 -0
  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?

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