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

   ## Summary
   
   `sync()` calls the file methods of every open file inside the critical 
section. It calls `nxsched_foreach(task_fssync)`, and `nxsched_foreach()` holds 
the critical section and the scheduler lock for the whole walk. `task_fssync()` 
calls `file_fsync()` and `file_put()` for each descriptor of the task. For a 
driver, `file_fsync()` calls the ioctl method with `BIOC_FLUSH`, and 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 caller of `sync()` 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:
   
   - Each walk of `nxsched_foreach()` only collects the IDs of up to 16 tasks, 
the smallest IDs above the last one, in order. The walk reads only `tcb->pid`.
   - After the walk, for each ID and each descriptor, `sync()` enters the 
critical section, finds the task again with `nxsched_get_tcb()` and takes a 
reference to the file with `fdlist_get()`. Then it leaves the section.
   - `file_fsync()` and `file_put()` come after the critical section.
   - `sync()` walks again until a walk finds fewer than 16 IDs, so it reaches 
every task. It needs no memory allocation.
   
   The TCB and the descriptor list are used only in the critical section that 
found them. 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, and the last `file_put()` 
closes the file outside the critical section. If the task is gone, `sync()` 
goes to the next ID. A task that starts after `sync()` began can be left out.
   
   ## Impact
   
   `sync()` only, every build mode; the hang needs SMP. `sync()` reaches the 
same files as before: every open file of every task, then the `syncfs()` of 
each mount point. No API or ABI change.
   
   ## Testing
   
   A test program and two test-only changes (not in this PR): 
`pipecommon_write()` holds `d_bflock` for 500 ms before its `poll_notify()`, 
and `file_fsync()` counts its calls for drivers and for files on a mount point. 
Race case: 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 
`sync()`. Count case: 40 child tasks each open a pipe and a file on a tmpfs, 
`sync()`; then each child opens one more pipe and one more file, `sync()` 
again. qemu-armv8a:nsh_smp (flat, TCG):
   
   | case | before | after |
   |---|---|---|
   | `sync()` while B holds `d_bflock`, 2 CPUs | the system stops after "ARMED" 
(no output after 30 s) | `sync()` returns, B and C complete, 3/3 (500 ms, the 
hold) |
   | the same, 4 CPUs (`CONFIG_SMP_NCPUS=4`) | - | 3/3 |
   | count case: files that the second `sync()` adds | 80 pipe ends + 40 files 
| 80 pipe ends + 40 files (2 and 4 CPUs) |
   | the race sequence without the hold, 200 rounds | pass (50 rounds) | pass |
   
   ostest passes on qemu-armv8a:nsh_smp (flat, 2 and 4 CPUs) and 
qemu-armv8a:knsh with `CONFIG_SMP` (kernel, 4 CPUs).
   
   Builds with `-Werror` in the CI image: qemu-armv8a:nsh, :nsh_smp, :knsh; 
rv-virt:nsh64 (flat), :pnsh64 (protected), :knsh64, :ksmp64 (kernel); sim:smp 
(without ostest).
   


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

  • [PR] fs/vfs: Call no file met... casaroli (via GitHub) via commits

Reply via email to