casaroli opened a new pull request, #21693: URL: https://github.com/apache/nuttx/pull/21693
## Summary `pthread_spin_*()` needed `CONFIG_SPINLOCK` and, outside a flat build, `CONFIG_BOARDCTL`: every lock and unlock was a `boardctl(BOARDIOC_SPINLOCK)` call, so a system call in protected and kernel builds, and the waiting thread spun inside the kernel. In a debug build, `pthread_spin_unlock()` by a thread that does not hold the lock ended the program with an assertion (`pthread_spinlock.c:294`) instead of returning `EPERM`. The first commit implements the functions in libc with an atomic compare-and-swap on the lock word (`nuttx/atomic.h`), so they need no system call: - A waiting thread reads the lock word and calls `sched_yield()` after 100 reads, so a holder that was preempted can run. - The lock word is an atomic variable in the memory of the program, so a lock in shared memory works between processes (`PTHREAD_PROCESS_SHARED`). - `pthread_spin_lock()` by the holder returns `EDEADLK` and `pthread_spin_trylock()` returns `EBUSY`, as before. `pthread_spin_unlock()` by another thread returns `EPERM`. `pthread_spin_init()` returns `EINVAL` for a `pshared` value that is not valid. - `libc.csv` lists the five functions. `BOARDIOC_SPINLOCK` stays for its other users. Outside a flat build the atomic operations run in user mode. `CONFIG_LIBC_ATOMIC_IRQ` and `CONFIG_LIBC_ATOMIC_HWSPINLOCK` make them with `up_irq_save()`, which user mode cannot do: on RISC-V `csrrc mstatus` is an illegal instruction in U-mode (`arch/risc-v/include/irq.h:870`), and on ARMv6-M/ARMv7-M an unprivileged `cpsid i` or `BASEPRI` write has no effect, so the operation is not atomic. So `CONFIG_PTHREAD_SPINLOCKS` now depends on `CONFIG_BUILD_FLAT || CONFIG_LIBC_ATOMIC_TOOLCHAIN || CONFIG_LIBC_ATOMIC_ARCH`. A flat build on such a chip (`bl602evb:wifi`) keeps the option. The second commit describes the spin locks and the dependency in `Documentation/standards/posix.rst`. ## BREAKING CHANGE - `CONFIG_PTHREAD_SPINLOCKS` no longer depends on `CONFIG_SPINLOCK` and `CONFIG_BOARDCTL`, and no longer selects `CONFIG_BOARDCTL_SPINLOCK`. - In a protected build on a chip with `CONFIG_LIBC_ATOMIC_IRQ` or `CONFIG_LIBC_ATOMIC_HWSPINLOCK` the option is not available. In the tree this is only `esp32c3-legacy-devkit:knsh`, which does not set it. - The lock word of `pthread_spinlock_t` is an `int32_t`, not a `spinlock_t`. The type has the same size as before, except with `CONFIG_TICKET_SPINLOCK` (4 bytes smaller). `<pthread.h>` no longer includes `<nuttx/spinlock_type.h>`. Quick fix: - A configuration that calls `boardctl(BOARDIOC_SPINLOCK)` itself sets `CONFIG_BOARDCTL_SPINLOCK=y` (and `CONFIG_SPINLOCK=y` if it needs it). - Code that used the `sp_lock` field as a `spinlock_t` (`SP_LOCKED`, `spin_lock()`) calls the `pthread_spin_*()` functions instead. Code that got `spinlock_t` from `<pthread.h>` includes `<nuttx/spinlock_type.h>`. - Programs and libraries built against the old `pthread_spinlock_t` must be compiled again. ## Impact - Users in the tree: `bl602evb:wifi`, `rv-virt:citest`, `sim:citest`, `sim:posix_test` set `CONFIG_PTHREAD_SPINLOCKS` (all flat). They keep it and need no change; `make savedefconfig` gives the same defconfigs. Their `CONFIG_SPINLOCK=y` is no longer needed for spin locks but stays valid. nuttx-apps (`examples/tlpi`, `testing/ltp`) only test `CONFIG_PTHREAD_SPINLOCKS`: no change. - Without `CONFIG_PTHREAD_SPINLOCKS` nothing changes. - With it: no system call per lock and unlock in protected and kernel builds (`qemu-armv8a:knsh`: 110 ns per lock/unlock pair before, 4-7 ns after), and `CONFIG_SPINLOCK` is no longer needed. Size, `nucleo-f072rb:nsh` (armv6-m) with `CONFIG_PTHREAD_SPINLOCKS`: text 89723 bytes before (with the `CONFIG_SPINLOCK` it needed), 87611 after. - Flat-build API: `pthread_spinlock_t` (see above), `pthread_spin_init()` with a bad `pshared` (`EINVAL`), `pthread_spin_unlock()` by another thread (`EPERM`, no assertion). ## Testing Test program (`spltest.c`, compiled against the export or as a builtin): 1) 4 threads x 100000 increments of a plain counter under one lock, with a check that no two threads are inside at once; 2) `trylock` by another thread: `EBUSY`; 3) `trylock`/`lock` by the holder: `EBUSY`/`EDEADLK`; 4) `unlock` by another thread and of a free lock: `EPERM`; 5) `pthread_spin_init()` with a bad `pshared`: `EINVAL`; 6) `PTHREAD_PROCESS_SHARED`: the lock and a counter in a `shm_open()` object, this program and a child (`posix_spawnp()`) 50000 increments each; 7) time of a lock/unlock pair. Then ostest. QEMU on macOS (HVF for arm64, TCG for the others), `CONFIG_PTHREAD_SPINLOCKS=y` (+ `CONFIG_FS_SHMFS`), `EXTRAFLAGS=-Werror`. | Configuration | master 03aea786390 / 6f47a1c3d66 | this PR | |---|---|---| | `qemu-armv8a:knsh` (kernel, arm64, 1 CPU) | 1-3 PASS; 4: assertion in `pthread_spinlock.c:294`, the program ends; 5 FAIL (0); 6 PASS; 110 ns per pair | all PASS, 4-7 ns per pair, ostest 0 | | `qemu-armv8a:nsh` (flat, arm64) | | all PASS, 19 ns, ostest 0 | | `qemu-armv8a:nsh_smp` (flat, arm64, 4 CPUs) | | all PASS (400000, no overlap), 10 ns, ostest 0 | | `rv-virt:knsh64` (kernel, rv64) | | 1-5 PASS, 6 SKIP (`CONFIG_FS_SHMFS` needs `CONFIG_ARCH_SHM_VBASE` there), 385 ns, ostest 0 | | `mps2-an500:knsh` (protected, armv7-m: `ldrex`/`strex` in unprivileged mode) | | all PASS, 202 ns | LTP open_posix_testsuite 20230516, the 15 `pthread_spin_*` cases, on `rv-virt:knsh64`: 12 PASS (some with the note that a "may fail" error was not returned); `pthread_spin_init/2-1` and `2-2` UNRESOLVED (no shared memory in that configuration; on `qemu-armv8a:knsh` with `CONFIG_FS_SHMFS` both PASS, before and after); `pthread_spin_unlock/3-1` FAIL: the case wants 0 from `pthread_spin_unlock()` by a thread that does not hold the lock, and NuttX returns `EPERM` (POSIX allows it), before and after. On `qemu-armv8a:knsh` the cases where a thread calls `exit()` while `main()` waits in `pthread_join()` (`pthread_spin_init/4-1`, `pthread_spin_destroy/3-1`) do not end, before and after; a program without spin locks does the same on master. Kconfig: with `CONFIG_PTHREAD_SPINLOCKS=y` added, `make olddefconfig` keeps it in `qemu-armv8a:knsh`, `rv-virt:knsh64`, `mps2-an500:knsh` and `raspberrypi-pico:nsh` (flat, `CONFIG_LIBC_ATOMIC_IRQ`), and drops it in `esp32c3-legacy-devkit:knsh` (protected, `CONFIG_LIBC_ATOMIC_IRQ`). `make savedefconfig` of `bl602evb:wifi`, `rv-virt:citest`, `sim:citest` and `sim:posix_test`: no change. Builds, each OK with `EXTRAFLAGS=-Werror`: `bl602evb:wifi` (flat, `CONFIG_LIBC_ATOMIC_IRQ`), `rv-virt:citest` (with the LTP spin cases), `nucleo-f072rb:nsh` + `CONFIG_PTHREAD_SPINLOCKS` (armv6-m, `CONFIG_LIBC_ATOMIC_IRQ`); `qemu-armv8a:nsh` + `CONFIG_PTHREAD_SPINLOCKS` with CMake (Ninja): no warning. Real hardware (CONTRIBUTING 1.14): not yet, help is welcome. `tools/checkpatch.sh -c -u -m -g -b origin/master..HEAD` passes. -- 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]
