cederom commented on PR #22164:
URL: https://github.com/apache/nuttx/pull/22164#issuecomment-7089159857

   **The deadlock is real on current master.** With the queue spinlock held, 
`work_qqueue()`, `work_qcancel()` (through `work_timer_reset()`) and 
`work_dispatch()` call `wd_start*()`, `wd_cancel()` and `nxsem_post()`, and all 
three enter the critical section. Code that already holds the critical section 
and then calls `work_queue()`/`work_cancel()` takes the locks in the opposite 
order. That's a plain ABBA on SMP. The notifier has the same problem one level 
up: `work_notifier_signal()` holds `g_notifier_lock`, then `work_queue()` takes 
the queue lock, then the critical section.
   
   **History worth putting in the commit message.** This looks like a 
regression from the interaction of two earlier changes, not a design flaw in 
either one:
   - 61f0c9719 "wqueue: wqueue remove csection" (2024-10) moved the work queues 
from the critical section to a spinlock. At that time wdog used its own 
spinlock, so no inversion was possible.
   - 43e0c50aa "sched/wdog: Revert the spin-lock to big-kernel-lock 
(enter_critical_section)" (2025-12) moved wdog back to the critical section. 
From then on, the queue spinlock → csection order exists.
   
   Citing both would show reviewers that this PR effectively reverts 61f0c9719 
because of 43e0c50aa, and why.
   
   **Things I checked:**
   - Every `wqueue->lock` and `g_notifier_lock` site is converted. No 
references remain in `sched/`, `tools/pynuttx/nxgdb` or `Documentation/`. The 
`libs/libc/wqueue` user queue has its own mutex and is not affected.
   - `work_unlock()` leaves the critical section first and then calls 
`sched_unlock()`. That's the same order as `spin_unlock_irqrestore_nopreempt()` 
and the fix in 67582acc1.
   - Callers that already hold the critical section (task exit, wdog callbacks, 
drivers) just nest. That is the point of the change, and it's fine.
   - From an ISR, `sched_lock()`/`sched_unlock()` do nothing, so `work_queue()` 
from interrupt context behaves as before.
   - The "Post while the queue lock keeps the semaphore alive" comment in 
`work_qqueue()` still holds, because `work_queue_free()` takes the same lock.
   - Without SMP, `enter_critical_section()` is `up_irq_save()`, so there is no 
change in cost (unless critmonitor/instrumentation is on).
   
   **Trade-offs to call out in the PR description:**
   1. **On SMP, this is now one global lock for all work queues** (HP, LP and 
every custom queue). Queue operations on different CPUs serialize with each 
other and with every other critical section user. Before, each queue had its 
own lock. People who worked on removing the csection may push back on this, so 
it helps to say why the alternative was rejected. The alternative is to keep 
the spinlock and move `wd_start()`/`wd_cancel()`/`nxsem_post()` out from under 
it. That runs into the semaphore lifetime problem against `work_queue_free()` 
and into timer-ordering races, which is presumably why you didn't do it.
   2. `work_notifier_signal()` now holds the critical section across the whole 
walk of the pending list, including one `work_queue()` per matching entry. With 
many notifiers pending, that's a long window for every CPU. It's probably fine 
in practice, but worth a sentence.
   3. Small behaviour change: `work_qcancel()` and the notifier 
setup/teardown/worker paths used plain `spin_lock_irqsave()` and now also 
disable preemption. That's harmless, but it's a change.
   4. With `CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION`, wqueue operations now 
count toward the critical-section time.
   
   **Nits:**
   - The `work_lock` doc block says "a separate spinlock, taken in the two 
orders, stops two CPUs". "...deadlocks two CPUs" would be clearer.
   - "Preemption stays disabled" reads as if it was already disabled. 
"Preemption is also disabled, so that..." is clearer.
   
   **Testing:** which configs reproduced the hang, and on what (sim:smp, 
qemu-armv8a:nsh_smp, rv-virt:smp)? Please add the ostest result with 
apache/nuttx-apps#4111 before and after this change to the description.
   
   Nothing blocking from me if the SMP scalability cost is accepted.


-- 
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