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

   ## Summary
   
   `pselect()` and `epoll_pwait()` call the poll method of each file inside the 
critical section. A poll method can take a spinlock of its driver: 
`pipecommon_poll()` takes `d_bflock`. The pipe driver also holds `d_bflock` 
while it calls `poll_notify()`, and the `nxsem_post()` of a waiting poller 
enters the critical section. With SMP, one CPU holds the critical section and 
waits for `d_bflock`, and another CPU holds `d_bflock` and waits for the 
critical section: both stop, and then every CPU stops at its next critical 
section. No message comes.
   
   - `pselect()` entered the critical section, set the temporary mask and 
called `select()` in it, so `poll_setup()` and `poll_teardown()` ran in the 
section.
   - `epoll_pwait()` enters the critical section before `epoll_setup()` and 
leaves it after `epoll_teardown()` since 457531ef741 (#21943).
   
   `ppoll()` has the correct order since e321f58a9ef: `nx_poll()` sets the mask 
after the poll setup and holds the critical section only from the test of the 
pending signals to the wait. This PR uses the same order:
   
   - `pselect()`: the new `nx_select()` in `fs_select.c` (the body of 
`select()`; it returns a negated errno) gives the mask to `nx_poll()`; 
`select()` and `pselect()` call it. A NULL mask keeps the mask of the caller, 
as in `ppoll()`.
   - `epoll_pwait()` sets up first, then enters the critical section, sets the 
mask and tests the pending signals, waits, restores the mask and leaves the 
section, then tears down. The retry after a wakeup with no event does the same 
steps. The old mask is saved only once: in a system call the temporary mask 
stays set after `nxsig_waitmask_restore()`.
   
   ## Impact
   
   Every build with signals, flat, protected and kernel; the hang needs SMP. 
The signal behaviour of the three calls does not change: a signal that the 
temporary mask unblocks and that comes before the wait stays pending and ends 
the call with `EINTR`, as in `ppoll()`. `nx_select()` is a new kernel-internal 
function in `include/nuttx/fs/fs.h`, next to `nx_poll()`. No API or ABI change.
   
   ## Testing
   
   A test program and a test-only change to `pipecommon_write()` (not in this 
PR: it holds `d_bflock` for 500 ms before its `poll_notify()`). Thread B on CPU 
1 writes one byte to a pipe and holds `d_bflock`; thread C waits in `poll()` on 
the read end; the main thread on CPU 0 then calls the function under test on 
the read end. qemu-armv8a:nsh_smp (flat, 2 CPUs, HVF):
   
   | call | before | after |
   |---|---|---|
   | `pselect()` | the system stops (no output after 20 s) | returns 1, 
`POLLIN`, 3/3 |
   | `epoll_pwait()` (the set reported one event before, so that 
`epoll_setup()` calls the poll method) | the system stops | returns 1, 
`EPOLLIN`, 3/3 |
   | `ppoll()`, `poll()`, `select()`, `epoll_wait()` (controls) | pass | pass, 
3/3 each |
   
   sim:smp (the same test and hook, host threads as CPUs): before, `pselect()` 
and `epoll_pwait()` stop the simulator; after, all six calls pass.
   
   ostest (it has the mask cases of `ppoll()`, `pselect()` and `epoll_pwait()`) 
passes on qemu-armv8a:nsh_smp (flat, 2 CPUs, HVF) and qemu-armv8a:knsh with 
`CONFIG_SMP` (kernel, 4 CPUs, TCG).
   
   Builds with `-Werror`: qemu-armv8a:nsh_smp and :knsh with `CONFIG_SMP` (Arm 
GNU 13.2); rv-virt:nsh64, :pnsh64, :knsh64 and rv-virt:nsh64 with 
`CONFIG_DISABLE_ALL_SIGNALS` (compile; the link fails on master too, for NSH) 
(CI image).
   


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