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

   ## Summary
   
   A deferred cancellation (the default) of a thread that waits in a 
cancellation point ends each semaphore wait of that thread with `ECANCELED`. 
This includes an uninterruptible wait in a system call, which a signal does not 
end. The kernel uses such a wait when the memory of the wait must stay valid 
until the wait is complete. For example, the 9p client puts the request, the 
reply buffers and the semaphore that the completion posts on the kernel stack 
of the caller (`v9fs_client_rpc()`). After `ECANCELED` the system call 
returned, the thread ended and its stack was freed. The virtio completion 
(`v9fs_transport_done()`) then wrote into the freed stack: a kernel data abort 
in the interrupt handler, or memory that changed behind the kernel.
   
   This PR gives the deferred cancellation the rule that `nxsig_tcbdispatch()` 
already has for a signal: an uninterruptible wait in a system call goes on 
until it is complete. The cancellation stays pending 
(`CANCEL_FLAG_CANCEL_PENDING`) and acts when the thread leaves the cancellation 
point. The rule applies to each caller of `nxnotify_cancellation()`: 
`pthread_cancel()`, `task_delete()`, and the cancellation of the other threads 
when a process exits (`exit()`, a fatal default action of a signal, a fault in 
user code).
   
   The wait of `pthread_join()` on the join semaphore of the thread still ends 
at once. `pthread_join()` is a cancellation point that can wait for ever, and 
its semaphore is in the TCB. `group_kill_children()` uses the same test.
   
   The fix is in the cancellation, not in the 9p client or in 
`nxsem_wait_uninterruptible()`. rpmsgfs has the same pattern: 
`rpmsgfs_send_recv()` waits in `rpmsg_wait()` on a cookie on the stack. A retry 
on `ECANCELED` in `nxsem_wait_uninterruptible()` would also stop a cancellation 
from ending `pthread_cond_wait()` in a flat build.
   
   ## Impact
   
   - Protected and kernel builds with `CONFIG_CANCELLATION_POINTS`: a deferred 
cancellation of a thread in an uninterruptible kernel wait acts when the wait 
is complete. Before, the wait ended with `ECANCELED`. A signal already waits 
for such a wait. A driver that waits uninterruptibly for an event that never 
comes keeps such a thread until the event comes, as for a signal.
   - Flat builds: no change. There is no system call, and there 
`pthread_cond_wait()` also waits uninterruptibly 
(`libs/libc/pthread/pthread_condwait.c:96`): a cancellation must end it. The 9p 
case in a flat build stays as on master (a follow-up).
   - Without `CONFIG_CANCELLATION_POINTS`: no change (the code is not compiled).
   - No change to an API, a header or a Kconfig option. Common code 
(`sched/task`); the architectures that set `TCB_FLAG_SYSCALL` (arm, arm64, 
mips, misoc, risc-v, xtensa) get the new rule. The others do not change, also 
x86_64, which does not set `TCB_FLAG_SYSCALL`.
   - Documentation: a second commit adds this exception to 
`Documentation/os/scheduling/cancellation_points.rst`, which says that a 
cancellation ends each semaphore wait with `-ECANCELED`.
   
   ## Testing
   
   Test program `cancelio.c`: 4 threads with deferred cancellation write, 
`fsync()` and read back their own file of 64 KiB on the 9p mount. The main 
thread cancels one of them at a random time of 0 to 20 ms, joins it 
(`PTHREAD_CANCELED`) and starts it again, 1000 times. qemu-armv8a:knsh (kernel 
build), QEMU with HVF, `/system` on virtio 9p (writable), 
`CONFIG_CANCELLATION_POINTS=y`:
   
   | | master | this PR |
   |---|---|---|
   | `cancelio /system/bin 1000 4 65536 20` | kernel data abort at the first 
cancellation (`arm64_fatal.c:637`) | PASS: 1000 cancellations, 138239 file 
rounds |
   | ostest, qemu-armv8a:knsh | not reached (the system stopped) | status 0 |
   | ostest, qemu-armv8a:nsh (flat) | status 0 | status 0 |
   
   With `CONFIG_CANCELLATION_POINTS=y`, also: ostest on qemu-armv8a:nsh_smp 
(flat, SMP, 4 CPUs, HVF): status 0. Builds of rv-virt:pnsh64 (protected) and 
rv-virt:knsh64 (kernel): OK, no new warning.
   
   ostest (its cancellation tests) covers the cancellation paths that this PR 
keeps. No CI configuration runs a 9p mount with cancellation points, so 
`cancelio.c` is not added to nuttx-apps.
   
   `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