casaroli opened a new pull request, #22473:
URL: https://github.com/apache/nuttx/pull/22473

   ## 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 
https://github.com/apache/nuttx/pull/22164#issuecomment-7090844485: 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 #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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to