Skip to content

Commit f9eaaa8

Browse files
fbqhtejun
authored andcommitted
workqueue: doc: Call out the non-reentrance conditions
The current doc of workqueue API suggests that work items are non-reentrant: any work item is guaranteed to be executed by at most one worker system-wide at any given time. However this is not true, the following case can cause a work item W executed by two workers at the same time: queue_work_on(0, WQ1, W); // after a worker picks up W and clear the pending bit queue_work_on(1, WQ2, W); // workers on CPU0 and CPU1 will execute W in the same time. , which means the non-reentrance of a work item is conditional, and Lai Jiangshan provided a nice summary[1] of the conditions, therefore use it to describe a work item instance and improve the doc. [1]: https://lore.kernel.org/lkml/CAJhGHyDudet_xyNk=8xnuO2==o-u06s0E0GZVP4Q67nmQ84Ceg@mail.gmail.com/ Suggested-by: Matthew Wilcox <[email protected]> Suggested-by: Tejun Heo <[email protected]> Signed-off-by: Boqun Feng <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 55df093 commit f9eaaa8

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Documentation/core-api/workqueue.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,6 @@ resources, scheduled and executed.
216216

217217
This flag is meaningless for unbound wq.
218218

219-
Note that the flag ``WQ_NON_REENTRANT`` no longer exists as all
220-
workqueues are now non-reentrant - any work item is guaranteed to be
221-
executed by at most one worker system-wide at any given time.
222-
223219

224220
``max_active``
225221
--------------
@@ -391,6 +387,23 @@ the stack trace of the offending worker thread. ::
391387
The work item's function should be trivially visible in the stack
392388
trace.
393389

390+
Non-reentrance Conditions
391+
=========================
392+
393+
Workqueue guarantees that a work item cannot be re-entrant if the following
394+
conditions hold after a work item gets queued:
395+
396+
1. The work function hasn't been changed.
397+
2. No one queues the work item to another workqueue.
398+
3. The work item hasn't been reinitiated.
399+
400+
In other words, if the above conditions hold, the work item is guaranteed to be
401+
executed by at most one worker system-wide at any given time.
402+
403+
Note that requeuing the work item (to the same queue) in the self function
404+
doesn't break these conditions, so it's safe to do. Otherwise, caution is
405+
required when breaking the conditions inside a work function.
406+
394407

395408
Kernel Inline Documentations Reference
396409
======================================

0 commit comments

Comments
 (0)