casaroli opened a new pull request, #21943: URL: https://github.com/apache/nuttx/pull/21943
## Summary A program blocks a signal and unblocks it only in `epoll_pwait()`, so that it cannot lose the signal between a test and the wait. `epoll_pwait()` does not handle this mask correctly: - Kernel build: in a system call, a signal that the temporary mask unblocks is dispatched at the end of the call. `epoll_pwait()` restored the old mask before that, so the handler did not run and the signal stayed pending. The call returned `EINTR` and the program saw no signal. - Flat build: a signal that was pending before the call ran its handler, but the call did not stop. It waited until the timeout and returned 0. `ppoll()` and `pselect()` do this correctly since #20720. This PR makes `epoll_pwait()` do the same steps: 1. `sched/signal: Add nxsig_waitmask_set() ...` moves the mask setup that `ppoll()`, `pselect()` and `sigsuspend()` repeat (save the mask, set the new one, clear the wait mask, dispatch the pending signals that the new mask unblocks) into one function. There is no change in behavior. 2. `fs/vfs/fs_epoll: ...` uses it in `epoll_pwait()`: return `EINTR` at once if the mask unblocks a pending signal, hold a critical section from that test to the wait, and keep the temporary mask until the end of the system call. The new `nxsig_waitmask_restore()` does the last step with the existing `nxsig_restoremask()`. The mask is set once, before the retry loop: a second set after a wakeup with no event would save the temporary mask as the mask to restore. ## Impact `epoll_pwait()`: a signal that its mask unblocks now runs its handler before the call returns `EINTR`, in flat, protected and kernel builds. A pending signal that the mask unblocks makes the call return `EINTR` at once, as `ppoll()` and `pselect()` do. `ppoll()`, `pselect()`, `sigsuspend()`: same code, now in one function. `epoll_wait()` does not change. With `CONFIG_DISABLE_ALL_SIGNALS` nothing changes. Potential breaking change: no. No public API, macro, structure or errno changes. The two new functions are kernel internal (`include/nuttx/signal.h`). Size: `lm3s6965-ek:qemu-protected` (arm-none-eabi-gcc 13.2.Rel1) kflash 130984 B on master, 131024 B with this PR (128 KiB region). ## Testing Test: `pwait_test()` in ostest, apache/nuttx-apps#NNNN. The thread blocks `SIGUSR1` and unblocks it only in the mask of `ppoll()`, `pselect()` or `epoll_pwait()`. `SIGUSR1` is pending before the call, or another thread sends it during the wait. Runtime: QEMU with HVF on an Apple Mac (arm64), `qemu-armv8a:knsh` (kernel build) and `qemu-armv8a:nsh` (flat build), with the apps PR above. master, knsh: ``` pwait_test: ERROR: epoll_pwait, signal pending: ret -1 errno 4 handler ran 0 times pwait_test: ERROR: epoll_pwait, signal during the wait: ret -1 errno 4 handler ran 0 times ostest_main: Exiting with status 256 ``` master, nsh: ``` pwait_test: ERROR: epoll_pwait, signal pending: ret 0 errno 4 handler ran 1 times ostest_main: Exiting with status 256 ``` This PR, knsh and nsh: ``` pwait_test: PASS ostest_main: Exiting with status 0 ``` A second program on knsh with `CONFIG_PIPES=y`: `SIGCHLD` from a child of `fork()`, blocked by the caller and unblocked by the mask of `epoll_pwait()`, with a pipe in the epoll set. master: 7 checks passed, 2 failed (pending signal: no handler; signal during the wait: no handler). This PR: 9 passed. Builds: `rv-virt:knsh64`, `rv-virt:knsh`, `rv-virt:nsh64`, `lm3s6965-ek:qemu-protected`, `lm3s6965-ek:qemu-kostest`, `lm3s6965-ek:qemu-flat`, `nucleo-f302r8:ihm07m1_f32` (`CONFIG_DISABLE_ALL_SIGNALS=y`). `tools/checkpatch.sh -c -u -m -g` passes. Not tested at runtime: SMP, RISC-V, protected build. -- 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]
