Skip to content

sched/wqueue: Do not enter the critical section with the queue lock. - #22473

Open
casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:wqueue-hrtimer-up
Open

casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:wqueue-hrtimer-up

Conversation

@casaroli

Copy link
Copy Markdown
Contributor

Summary

On SMP, two CPUs can stop forever in the work queue code. The work queue holds its spinlock (wqueue->lock) while it calls wd_start_abstick(), wd_cancel() and nxsem_post(), and these functions enter the critical section. Code in the critical section (task exit, watchdog callbacks, drivers) calls work_queue() and work_cancel(), which take wqueue->lock. So one CPU holds the critical section and spins on the queue lock, and the other CPU holds the queue lock and spins on the critical section.

This PR follows the design that @Fix-Point proposed in #22164 (comment): keep the queue spinlock and remove the edge from the queue lock to the critical section.

  • With CONFIG_HRTIMER, the work queue timer is an hrtimer. hrtimer_start() and hrtimer_cancel() take only the hrtimer lock, which never takes the critical section or the queue lock. The timer callback runs without the hrtimer lock and only posts the semaphore. work_queue_free() waits for a running callback with hrtimer_cancel_sync().
  • work_queue() posts the semaphore of zero-delay work after it releases the queue lock. Preemption stays disabled until the post is done, so a thread cannot delay the post. On SMP, a counter keeps the semaphore alive until the post is done, and work_queue_free() waits for it (this is why sched/wqueue: Fix notifier and immediate-work races #20540 moved the post under the lock).
  • A worker wakes the other workers and the work_cancel_sync() waiters after it releases the queue lock. The worker keeps the queue alive.

One difference from the proposal: zero-delay work does not go through the timer. Without CONFIG_SCHED_TICKLESS, hrtimer_reprogram() does nothing and an hrtimer expires only at the next tick, so zero-delay work would wait up to one tick (see the numbers below). 91 of the 97 SMP defconfigs are not tickless.

Without CONFIG_HRTIMER the timer is still a watchdog timer, and wd_start_abstick() still enters the critical section with the queue lock held. So the deadlock stays on SMP when CONFIG_HRTIMER is off. A new paragraph in Documentation/os/scheduling/wqueue.rst says that an SMP system that queues or cancels work in the critical section must enable CONFIG_HRTIMER. CONFIG_HRTIMER is default n today, and enabling it moves every watchdog onto the hrtimer, so this PR does not select it.

Impact

Only sched/wqueue and the work queue documentation. No API change. Without SMP the behavior is the same, except that the semaphore post comes after the interrupts are restored (preemption still disabled). With CONFIG_HRTIMER=n the watchdog timer is used as before.

Size (CONFIG_HRTIMER=n, text): qemu-armv8a:nsh +84 bytes, qemu-armv8a:nsh_smp +220 (bss +72), mps2-an500:nsh with CONFIG_SCHED_HPWORK +176.

Testing

qemu-armv8a, flat nsh with CONFIG_SMP=y and 4 CPUs, under HVF. The new ostest case of apache/nuttx-apps#4111 runs two threads per CPU; each queues delayed and immediate work and cancels the delayed work in a loop, and half of the threads do it in the critical section.

ostest master this PR
tick, CONFIG_HRTIMER=y stops in the new case (2 runs) passes (3 runs)
tickless, CONFIG_HRTIMER=y passes
tick, CONFIG_HRTIMER=n stops in the new case stops in the new case (watchdog path, as described)
tick, CONFIG_HRTIMER=n, ostest without the new case passes
1 CPU, CONFIG_HRTIMER=y and =n passes

sim:smp (4 CPUs) in the CI image: with CONFIG_HRTIMER=y, master stops in the new case and this PR passes ostest (exit 0); with CONFIG_HRTIMER=n and ostest without the new case, this PR passes. sim:ostest (1 CPU, CONFIG_HRTIMER=y): exit 0 on master and with this PR.

Latency of zero-delay work on HPWORK, from work_queue() to the start of the callback, 2000 samples per run, 2 runs, qemu-armv8a 4 CPUs under HVF, USEC_PER_TICK=1000 (median / p99 in microseconds):

tick tickless
master (CONFIG_HRTIMER=n) 12.6 / 19.6 13.4-17.0 / 20.1-22.5
this PR, CONFIG_HRTIMER=y 12.6-12.7 / 19.9-20.6 13.5-14.0 / 20.5-23.2
this PR, CONFIG_HRTIMER=n 12.6-13.2 / 18.9-20.9
zero-delay work through the hrtimer (test patch, not in this PR) 994-995 / 1063-1111 15.8-16.7 / 23.7-25.0

Builds with EXTRAFLAGS="-Wno-cpp -Werror", no new warning: qemu-armv8a:nsh (CONFIG_HRTIMER y and n), nsh_smp, citest_smp with CONFIG_HRTIMER=y, knsh with SMP (y and n), fvp-armv8r:citest, mps2-an500:nsh with CONFIG_SCHED_HPWORK (y and n), qemu-armv7a:smp (y and n), rv-virt:nsh64 (y and n), citest64, smp (32-bit) with CONFIG_HRTIMER=y, knsh64 with SMP (y and n), qemu-intel64:citest_smp (tickless) with CONFIG_HRTIMER=y, sim:ostest. The riscv and armv7-a images built in the CI image.

tools/checkpatch.sh -c -u -m -g passes.

On SMP, the work queue held wqueue->lock while it called
wd_start_abstick(), wd_cancel() and nxsem_post().  These functions
enter the critical section.  Code in the critical section (task exit,
drivers, watchdog callbacks) calls work_queue() and work_cancel(),
which take wqueue->lock.  Two CPUs could take the two locks in the
opposite order and wait for each other forever.

Remove the edge from wqueue->lock to the critical section:

- With CONFIG_HRTIMER, the work queue timer is an hrtimer.
  hrtimer_start() and hrtimer_cancel() take only the hrtimer lock,
  which never takes the critical section or wqueue->lock.  The timer
  callback runs without the hrtimer lock and only posts the semaphore.
  work_queue_free() waits for a running callback with
  hrtimer_cancel_sync().
- work_queue() posts the semaphore after it releases wqueue->lock.
  Preemption stays disabled until the post is done, so the post is not
  delayed.  On SMP, a counter keeps the semaphore alive until the post
  is done, and work_queue_free() waits for it.
- A worker wakes the other workers and the work_cancel_sync() waiters
  after it releases wqueue->lock.  The worker keeps the queue alive.

Zero-delay work does not go through the timer: without
CONFIG_SCHED_TICKLESS an hrtimer expires only at the next tick.

Without CONFIG_HRTIMER the timer is still a watchdog timer, and
wd_start_abstick() still enters the critical section with
wqueue->lock held.  An SMP system that queues or cancels work in the
critical section must enable CONFIG_HRTIMER.

The hrtimer design is from Fix-Point in apache#22164.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
Say that the timer is an hrtimer with CONFIG_HRTIMER and a watchdog
timer without it, and that an SMP system that queues or cancels work in
the critical section must enable CONFIG_HRTIMER.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium labels Nov 27, 2026
casaroli added a commit to casaroli/nuttx-apps that referenced this pull request Nov 27, 2026
Add a work queue test: 2 * CONFIG_SMP_NCPUS threads (2 threads without
SMP) queue delayed and immediate work and cancel the delayed work in a
loop.  Half of the threads do it in the critical section.  The threads
are not pinned to a CPU.  The work queue enters the critical section to
start its timer and to wake a worker, so this checks that the two orders
do not stop the CPUs on SMP.

The case needs CONFIG_HRTIMER: the work queue fix of apache/nuttx#22473
is for the hrtimer work queue only.  Without CONFIG_HRTIMER the case is
not built.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

mps2-an500-citest

  • flash: .text +64 B (+0.0%, 248,504 B / 4,194,304 B, total: 6% used)

mps2-an500-citest-protected

  • flash: .text +80 B (+0.1%, 117,024 B / 4,194,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.work_qqueue -8 B, .text.work_thread +84 B (+0.0%, 379,304 B)

stm32-nucleo-f103rb

  • flash: .text +72 B (+0.2%, 34,056 B / 131,072 B, total: 26% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant