Repository navigation
Conversation
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>
casaroli
requested review from
Donny9,
GUIDINGLI and
xiaoxiang781216
as code owners
November 27, 2026 07:30
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>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On SMP, two CPUs can stop forever in the work queue code. The work queue holds its spinlock (
wqueue->lock) while it callswd_start_abstick(),wd_cancel()andnxsem_post(), and these functions enter the critical section. Code in the critical section (task exit, watchdog callbacks, drivers) callswork_queue()andwork_cancel(), which takewqueue->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.
CONFIG_HRTIMER, the work queue timer is an hrtimer.hrtimer_start()andhrtimer_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 withhrtimer_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, andwork_queue_free()waits for it (this is why sched/wqueue: Fix notifier and immediate-work races #20540 moved the post under the lock).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_HRTIMERthe timer is still a watchdog timer, andwd_start_abstick()still enters the critical section with the queue lock held. So the deadlock stays on SMP whenCONFIG_HRTIMERis off. A new paragraph inDocumentation/os/scheduling/wqueue.rstsays that an SMP system that queues or cancels work in the critical section must enableCONFIG_HRTIMER.CONFIG_HRTIMERis default n today, and enabling it moves every watchdog onto the hrtimer, so this PR does not select it.Impact
Only
sched/wqueueand 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). WithCONFIG_HRTIMER=nthe 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 withCONFIG_SCHED_HPWORK+176.Testing
qemu-armv8a, flat nsh with
CONFIG_SMP=yand 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.CONFIG_HRTIMER=yCONFIG_HRTIMER=yCONFIG_HRTIMER=nCONFIG_HRTIMER=n, ostest without the new caseCONFIG_HRTIMER=yand=nsim:smp (4 CPUs) in the CI image: with
CONFIG_HRTIMER=y, master stops in the new case and this PR passes ostest (exit 0); withCONFIG_HRTIMER=nand 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):CONFIG_HRTIMER=n)CONFIG_HRTIMER=yCONFIG_HRTIMER=nBuilds with
EXTRAFLAGS="-Wno-cpp -Werror", no new warning: qemu-armv8a:nsh (CONFIG_HRTIMERy and n), nsh_smp, citest_smp withCONFIG_HRTIMER=y, knsh with SMP (y and n), fvp-armv8r:citest, mps2-an500:nsh withCONFIG_SCHED_HPWORK(y and n), qemu-armv7a:smp (y and n), rv-virt:nsh64 (y and n), citest64, smp (32-bit) withCONFIG_HRTIMER=y, knsh64 with SMP (y and n), qemu-intel64:citest_smp (tickless) withCONFIG_HRTIMER=y, sim:ostest. The riscv and armv7-a images built in the CI image.tools/checkpatch.sh -c -u -m -gpasses.