Repository navigation
sched/semaphore: Fix three errors in the wait of a semaphore - #21615
Conversation
…one. With CONFIG_MM_KMAP, nxsem_wait_slow() maps a semaphore in user memory into the kernel while the thread waits. A semaphore in one page needs no new mapping, but a semaphore that crosses a page boundary gets a temporary mapping. After the wait, kmm_unmap() removes it. The code for a mutex then used the same pointer to read the wait list and to set the holder, so the kernel faulted and panicked. A program can cause this with a pthread mutex in ordinary memory: it is enough that the mutex crosses a page boundary and that a thread waits on it. Keep the address of the caller, and use it after the unmap. The address environment of the caller is the current one at that point. Without CONFIG_MM_KMAP, the code does not change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
nxstyle reports "Missing blank line after declarations" in nxsem_recover(). The error is older than the next change, which edits this file. Whitespace only. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
nxtask_terminate() removes a task from its list with nxsched_remove_readytorun() and then restores task_state. For a task that waits on a semaphore, nxtask_exithook() then calls nxsem_recover(), which sees TSTATE_WAIT_SEM and calls dq_rem() again. The links of the task are NULL after the first removal, so the second dq_rem() sets the head and the tail of the wait list to NULL. This has these effects when other threads wait on the same semaphore or mutex: - The other waiters are no longer in the list, so a post does not wake them. They stay blocked. - For a mutex, the list looks empty, so nxsem_recover() clears the blocking bit, and the next unlock does not go to the kernel. When the next waiter is killed or times out, the assertion in nxsem_recover() or nxsem_wait_irq() fails. This occurs, for example, when a process with several threads blocked on one mutex is killed. Call dq_rem() only if the task is still in the list. The blocking bit is now cleared only when the list is really empty. task_restart() has the same problem in the opposite order: nxtask_recover() removes the task from the wait list, then nxsched_remove_readytorun() removes it again and empties the list. Do not call nxsched_remove_readytorun() for a task that waits on a semaphore. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
|
A semaphore in user memory has its address in the address environment of the waiter. nxsem_wait_irq() runs in the address environment of its caller. The caller can be a thread of another process that sends a signal, or a timer interrupt that comes while another process runs. At that address the caller sees other memory, or no memory. After fork(), both processes have the semaphore at the same address. So a timeout of sem_timedwait() in one process, when the timer interrupt comes while the other process runs, changes the wait list and the count of the semaphore of the other process. The assertion in nxsem_wait_irq() fires. Without assertions, the waiter stays in the wait list of its own semaphore. A signal to a thread that waits on a semaphore does the same in the process of the sender. This does not occur with CONFIG_MM_KMAP: the wait keeps a kernel address of the semaphore. Without it, select the address environment of the waiter before the access, and restore it before the waiter becomes ready to run. nxsem_waiter_select() and nxsem_waiter_restore() do this. They do nothing when the semaphore is not at a user address, or when the waiter uses the current address environment. nxsched_set_priority() of a waiter moves the waiter in the wait list of the semaphore, so it does the same. Only a kernel build gives each process its own address space, so the two functions are empty inline functions in the other builds. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
4dcc3bd to
3777f8b
Compare
|
@casaroli the switch of addrenv is a slow operation, it could be better to refine the implementation of semaphore to split the representation of userspace and kernel like Linux futex later. |
I agree. The switch is only on the slow path: a signal, a timeout or sched_setparam() of a waiter from another process. A wait and a post in the same process do not switch. A futex-like split also removes the temporary kmap of the first fix and the wait list that fork() copies today, so it is a good follow-up. |
Summary
Three fixes in the semaphore wait code. Each one makes the kernel use the wrong memory for the wait list or the count of a semaphore: in a kernel build a user program can stop the kernel with an ordinary pthread mutex or semaphore. One commit for each fix, and one whitespace commit:
CONFIG_MM_KMAP,nxsem_wait_slow()maps a semaphore in user memory into the kernel while the thread waits. A semaphore that crosses a page boundary gets a temporary mapping, andkmm_unmap()removes it after the wait. The code for a mutex then used the same pointer to read the wait list and to set the holder: the kernel faulted. The fix keeps the address of the caller and uses it after the unmap.nxtask_terminate()removes a task from its list withnxsched_remove_readytorun()and then restorestask_state, sonxsem_recover()seesTSTATE_WAIT_SEMand callsdq_rem()again. The links of the task are NULL after the first removal, so the seconddq_rem()sets the head and the tail of the wait list to NULL: the other waiters never wake, and the next waiter that is killed or times out hits the assertion innxsem_recover()ornxsem_wait_irq().task_restart()has the same problem in the opposite order.nxsem_recover()now callsdq_rem()only if the task is still in the list (dq_inqueue()), andnxtask_reset_task()does not callnxsched_remove_readytorun()for a task that waits on a semaphore.CONFIG_MM_KMAP, the wait keeps the user address of the semaphore.nxsem_wait_irq()runs in the address environment of its caller: the thread of another process that sends a signal, or a timer interrupt that comes while another process runs. Afterfork()both processes have the semaphore at the same address, so the kernel changed the wait list and the count of the semaphore of the other process.nxsem_waiter_select()andnxsem_waiter_restore()select the address environment of the waiter around the access innxsem_wait_irq()and innxsched_set_priority()of a waiter (it moves the waiter in the wait list). They do nothing for a semaphore that is not at a user address, or when the caller already uses the address environment of the waiter.Impact
CONFIG_MM_KMAP), a kill or cancel of one of several waiters, and a signal, a timeout orsched_setparam()of a waiter in another process (withoutCONFIG_MM_KMAP) no longer fault, assert or change the memory of another process.nxsem_waiter_select()andnxsem_waiter_restore()are empty inline functions (CONFIG_BUILD_KERNEL; a protected build with an MPU address environment, such as fvp-armv8r:pnsh, builds them empty too). Text size with optimization: mps2-an500:nsh (-Os) the same as without this commit; qemu-armv8a:nsh (DEBUG_FULLOPT) +16 bytes (oneifbecame two). TheCONFIG_MM_KMAPcode changes only in the first fix.Testing
Test programs (POSIX, one line per case; the last two need
fork(), so a kernel build):restartsem.c(flat and protected:task_restart()is not in a kernel build): a task that waits on a semaphore or a mutex is restarted while another task waits on the same object; both must wake.semcross.c: three threads contend for a pthread mutex at three offsets across a page boundary; then a thread waits on a semaphore across a page boundary and gets a signal, and a wait on it times out.killmxn.c: a child process with 3 threads on one mutex is killed with SIGTERM and SIGKILL, with the threads busy or blocked, 8 rounds each; one of 4 waiters on a mutex is cancelled; the threads of a process that wait on a process-shared mutex or semaphore are killed, and two other processes that wait on it must still get it.semwaitenv.c: afterfork(), the sender and the waiter have the semaphore at the same address. Asem_timedwait()and apthread_mutex_timedlock()time out while the other process runs;sched_setparam()of a waiter from the other process; a signal with a handler to a waiter on a semaphore (EINTR); SIGKILL to a waiter on a semaphore. The copy of the sender must not change.MM_KMAP, arm64, HVF)MM_KMAP)MM_KMAP)MM_KMAPsem_timedwait(): assertion at sem_waitirq.c:82; signal with a handler: the same; SIGKILL: the same;sched_setparam(): the copy of the semaphore in the sender changes;pthread_mutex_timedlock()timeout: passMM_KMAPMM_KMAPin the defconfig, riscv64, QEMU TCG)The knsh and rv-virt runs add
PIPES(the test uses a pipe); rv-virt:knsh64 addsSIG_DEFAULTfor the SIGKILL case. The master runs were on 02ba322, d215909 and c8c0661 (no commit between them changes sched/ or mm/); the runs of this PR on the same masters (the rebase did not change the patches).Build only, with
-Werror: qemu-armv7a:knsh and qemu-armv7a:knsh_smp (armv7-a, noMM_KMAP), rv-virt:knsh (riscv32,MM_KMAP); arm-none-eabi-gcc 15.3, riscv-none-elf-gcc 14.3.No test in nuttx-apps covers these cases.
Not run: x86_64 and xtensa kernel builds, SMP kernel builds at run time, CMake builds, sim.
tools/checkpatch.sh -c -u -m -gpasses.Built with
EXTRAFLAGS=-Werrorafter theCONFIG_BUILD_KERNELguard: fvp-armv8r:pnsh, fvp-armv8r:pnsh_smp (protected, MPU address environment), qemu-armv8a:knsh, qemu-armv8a:nsh: OK.