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

   ## Summary
   
   `file_setlk()` ignored the result of the wait for a conflicting lock and 
tried again. A signal ends the wait with `-EINTR`, so `fcntl()` with `F_SETLKW` 
did not return before the lock was free. In a kernel build the signal stays 
pending during the system call, so every new wait failed at once and the call 
used the CPU until the lock was free; the signal handler ran only after that. 
POSIX requires that `fcntl()` with `F_SETLKW` fails with EINTR when a signal 
interrupts the wait, and that the lock is not taken (the comment of `F_SETLKW` 
in fs/vfs/fs_fcntl.c says the same).
   
   `file_setlk()` now returns the error of the wait. A cancel of the thread 
ends the wait with `-ECANCELED` in the same way (before, the cancel also waited 
for the lock).
   
   ## Impact
   
   - What a program sees: `fcntl(F_SETLKW)`, and `flock(LOCK_EX)`, 
`flock(LOCK_SH)` and `lockf(F_LOCK)` that use it, now return -1 with EINTR when 
a caught signal interrupts the wait. Before, they never failed with EINTR: they 
returned 0 when the lock became free. The lock is not taken in this case.
   - A handler with SA_RESTART does not change this: NuttX does not restart 
system calls after a handler (the kernel never reads SA_RESTART), so the call 
fails with EINTR as the other blocking calls (for example `read()` of a pipe) 
already do. Linux restarts `F_SETLKW` and `flock()` for a handler with 
SA_RESTART.
   - File systems that handle the lock themselves (hostfs, rpmsgfs) do not use 
`file_setlk()` and do not change.
   - The code is common VFS code: the same in flat, protected and kernel 
builds, on every architecture. Only builds with `CONFIG_FS_LOCK_BUCKET_SIZE > 
0` (default 0) build it.
   - Size: +16 bytes of text (mps2-an500:knsh with `FS_LOCK_BUCKET_SIZE=16`, 
arm-none-eabi-gcc 15.3).
   - Documentation/ does not describe the wait of `F_SETLKW`, so there is no 
documentation change.
   
   ## Testing
   
   New test apache/nuttx-apps#NNNN (`testing/fs/filelock`): a thread waits with 
`fcntl(F_SETLKW)`, `flock(LOCK_EX)` and `lockf(F_LOCK)` for a lock that the 
main thread holds for 2 s, and gets SIGUSR1 (handler without SA_RESTART) after 
300 ms. 13 checks: EINTR, return before the unlock, handler ran (for each 
call), the non-blocking calls fail on a held lock, and a wait without a signal 
gets the lock after the unlock. `filelock /lk/lock` on a tmpfs, 
`CONFIG_FS_LOCK_BUCKET_SIZE=16`, `EXTRAFLAGS="-Wno-cpp -Werror"`, master 
80a052ff9cc vs this PR, apps master ee3d67104 + the test:
   
   | Configuration | master | this PR | ostest |
   |---|---|---|---|
   | qemu-armv8a:knsh (kernel, arm64, QEMU HVF) | 6 of 13 checks fail | 13 pass 
| status 0 (both) |
   | qemu-armv8a:nsh (flat, arm64, QEMU HVF) | 6 of 13 checks fail | 13 pass | 
status 0 (both) |
   | mps2-an500:knsh (protected, Cortex-M7, QEMU TCG, 32-bit) | 6 of 13 checks 
fail | 13 pass | status 0 (both) |
   | qemu-armv8a:nsh, CMake | not run | 13 pass | status 0 |
   
   On master each of the three calls returns 0 after 2001 ms, when the main 
thread unlocks (the handler ran, but the wait went on). With this PR each call 
returns -1 with EINTR after about 301 ms. All builds have no warnings with 
`-Werror`. `tools/checkpatch.sh -c -u -m -g origin/master..HEAD` passes.
   
   The cancel case (`-ECANCELED`) is from code reading 
(sched/task/task_cancelpt.c), not tested. Not run: SMP, other architectures.
   


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