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

   ## Summary
   
   On SMP, two CPUs can stop forever in the work queue code. One CPU is in the 
critical section and calls `work_queue()` or `work_cancel()`, which wait for 
the queue spinlock. The other CPU holds the queue spinlock and calls 
`wd_start()`, `wd_cancel()` or `nxsem_post()`, which wait for the critical 
section.
   
   We see it on qemu-armv8a:knsh with 4 CPUs, when a process with many TCP 
sockets exits while other TCP connections are busy. `_exit()` enters the 
critical section and closes the sockets; `tcp_free()` calls 
`work_cancel(LPWORK)` and spins on the LPWORK queue lock. The LPWORK thread 
holds that lock in `work_qcancel()`, and `work_timer_reset()` calls 
`wd_start_abstick()`, which spins on the critical section. Both CPUs spin with 
interrupts off, and the system stops.
   
   #14623 gave the work queues and the work notifier their own spinlocks, but 
the code under them still wakes workers with `nxsem_post()`, which enters the 
critical section. #17640 made `wd_start()` and `wd_cancel()` enter the critical 
section again, and the queue timer is started and stopped under the queue lock. 
#20540 moved the `nxsem_post()` of immediate work under the queue lock, so that 
a queue that is freed at the same time keeps its semaphore. The other order is 
normal: task exit, watchdog callbacks (they run in the critical section) and 
many drivers queue and cancel work in the critical section.
   
   This PR makes the critical section the lock of the work queues and of the 
work notifier (`work_lock()` and `work_unlock()` in `sched/wqueue/wqueue.h`). 
Preemption stays disabled while the lock is held, as before. The `nxsem_post()` 
stays under the lock, as #20540 needs; it enters the critical section, so the 
critical section must come first, and then a spinlock inside it protects 
nothing more. The timer cannot be set after the unlock either: another CPU can 
queue earlier work between the unlock and `wd_start()`, and the late 
`wd_start()` then delays that work.
   
   ## Impact
   
   Only `sched/wqueue`. No API change. The two spinlocks (`lock` in `struct 
kwork_wqueue_s`, and `g_notifier_lock`) are removed.
   
   Without SMP, the critical section disables the interrupts, as 
`spin_lock_irqsave()` did, so the behavior is the same. With SMP, 
`work_cancel()` of work that is not queued, and the queueing of delayed work 
that does not expire first, now take the critical section; the other paths took 
it already in `wd_start()`, `wd_cancel()` or `nxsem_post()`.
   
   ## Testing
   
   qemu-armv8a under HVF. A new case of the ostest work queue test 
(apache/nuttx-apps#NNNN) 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.
   
   | Test | master | this PR |
   |---|---|---|
   | ostest, flat nsh, 4 CPUs | stops in the new case: CPU1 is in the critical 
section and spins on the queue lock in `work_qqueue()`; CPU0 holds the queue 
lock in `work_qcancel()` and spins on the critical section in `wd_cancel()` | 
passes |
   | ostest, knsh, 4 CPUs | passes | passes |
   | ostest, knsh, 1 CPU | | passes |
   | `cmocka_net_tcp`, flat nsh 4 CPUs, knsh 4 CPUs, knsh 1 CPU | 12 of 13 pass 
| the same |
   | `/proc/net/tcp` read with connection churn, knsh 4 CPUs, 15 boots | all 
pass | all pass |
   
   The knsh runs with 4 CPUs also have #22145: on master these TCP tests stop 
first in a TCP lock order that #22145 fixes. `test_tcp_connect_ipv4` fails on 
master and with this PR: our apps tree did not have apache/nuttx-apps#4103 yet, 
which fixes the test server.
   
   Build only, with this PR: qemu-armv7a:smp, rv-virt:smp (32-bit SMP), 
qemu-armv7a:nsh (no SMP), sabre-6quad:knsh_smp (kernel build, SMP). No new 
warning.
   
   `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