casaroli opened a new pull request, #22521: URL: https://github.com/apache/nuttx/pull/22521
## Summary A read of `/proc/<pid>/group/fd` calls the file methods of the task's open files inside the critical section. `proc_read()` enters the critical section to keep the TCB, and `proc_groupfd()` calls `file_ioctl(FIOC_FILEPATH)` and `file_put()` for each descriptor. A file method can take a spinlock of its driver: `pipecommon_ioctl()` and `pipecommon_close()` take `d_bflock`. The pipe driver also holds `d_bflock` while it posts the semaphore of a waiting reader or poller, and `nxsem_post()` then enters the critical section. With SMP, the reader of `/proc/<pid>/group/fd` holds the critical section and waits for `d_bflock`, and the writer holds `d_bflock` and waits for the critical section: both stop, and then every CPU stops at its next critical section. This PR calls no file method in the critical section: - `proc_read()` gets only the number of descriptors in its critical section, and calls `proc_groupfd()` after it leaves the section. - For each descriptor, `proc_groupfd()` enters the critical section, finds the task again by its pid, takes a reference to the file with `fdlist_get2()` and copies the backtrace of the open (`CONFIG_FS_BACKTRACE`). Then it leaves the section. - `file_ioctl()`, the line and `file_put()` come after the critical section. The TCB, the group and the descriptor row are used only in the critical section that found them, as in the other entries of `proc_read()`. The file reference keeps the file when the task closes the descriptor or ends: `fdlist_free()` empties the table under `fl_lock` before it closes a file, so a later look-up finds no file. If the task is gone, the listing ends. ## Impact procfs only, every build mode; the hang needs SMP. The output of `/proc/<pid>/group/fd` does not change. 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 reads `/proc/<own pid>/group/fd`. qemu-armv8a:nsh_smp (flat, 2 CPUs, HVF): | case | before | after | |---|---|---| | read `/proc/<pid>/group/fd` while B holds `d_bflock` | the system stops after "ARMED" (no output after 20 s) | the listing has both pipe descriptors, 3/3 (the read takes 500 ms, the hold) | | read `/proc/<pid>/group/fd` without the hold, and `cat /proc/self/group/fd` | pass | pass, the same lines | ostest passes and `cat /proc/self/group/fd` gives the same listing on qemu-armv8a:nsh_smp (flat, 2 CPUs, HVF) and qemu-armv8a:knsh with `CONFIG_SMP` (kernel, 4 CPUs, TCG). qemu-armv8a:nsh with `CONFIG_FS_BACKTRACE=4`: the listing shows the backtrace column. Builds with `-Werror`: qemu-armv8a:nsh_smp, :nsh with `CONFIG_FS_BACKTRACE=4`, :knsh with `CONFIG_SMP` (Arm GNU 13.2); rv-virt:nsh64 (flat), :pnsh64 (protected), :knsh64 (kernel) (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]
