Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <willy@infradead.org>
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: Tejun Heo <tj@kernel.org>

authored by

Boqun Feng and committed by
Tejun Heo
f9eaaa82 55df0933

+17 -4
+17 -4
Documentation/core-api/workqueue.rst
··· 216 216 217 217 This flag is meaningless for unbound wq. 218 218 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 - 223 219 224 220 ``max_active`` 225 221 -------------- ··· 386 390 387 391 The work item's function should be trivially visible in the stack 388 392 trace. 393 + 394 + Non-reentrance Conditions 395 + ========================= 396 + 397 + Workqueue guarantees that a work item cannot be re-entrant if the following 398 + conditions hold after a work item gets queued: 399 + 400 + 1. The work function hasn't been changed. 401 + 2. No one queues the work item to another workqueue. 402 + 3. The work item hasn't been reinitiated. 403 + 404 + In other words, if the above conditions hold, the work item is guaranteed to be 405 + executed by at most one worker system-wide at any given time. 406 + 407 + Note that requeuing the work item (to the same queue) in the self function 408 + doesn't break these conditions, so it's safe to do. Otherwise, caution is 409 + required when breaking the conditions inside a work function. 389 410 390 411 391 412 Kernel Inline Documentations Reference