Skip to content

sched/semaphore: Fix three errors in the wait of a semaphore - #21615

Merged
acassis merged 4 commits into
apache:masterfrom
casaroli:sem-fixes-up
Nov 11, 2026
Merged

acassis merged 4 commits into
apache:masterfrom
casaroli:sem-fixes-up

Conversation

@casaroli

@casaroli casaroli commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • sched/semaphore: Do not use a semaphore after its kernel mapping is gone. With 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, and kmm_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.
  • sched/semaphore: Add a blank line after a declaration in sem_recover.c. Whitespace only: an older nxstyle error in the file that the next commit changes.
  • sched/semaphore: Do not remove a killed waiter from the wait list twice. nxtask_terminate() removes a task from its list with nxsched_remove_readytorun() and then restores task_state, so nxsem_recover() 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: the other waiters never wake, and the next waiter that is killed or times out hits the assertion in nxsem_recover() or nxsem_wait_irq(). task_restart() has the same problem in the opposite order. nxsem_recover() now calls dq_rem() only if the task is still in the list (dq_inqueue()), and nxtask_reset_task() does not call nxsched_remove_readytorun() for a task that waits on a semaphore.
  • sched/semaphore: Use the memory of the waiter in a signal or a timeout. Without 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. After fork() 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() and nxsem_waiter_restore() select the address environment of the waiter around the access in nxsem_wait_irq() and in nxsched_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

  • Kernel builds: a pthread mutex across a page boundary (CONFIG_MM_KMAP), a kill or cancel of one of several waiters, and a signal, a timeout or sched_setparam() of a waiter in another process (without CONFIG_MM_KMAP) no longer fault, assert or change the memory of another process.
  • All build modes: other waiters of a semaphore or mutex now wake after one waiter is killed, cancelled or restarted.
  • Only a kernel build gives each process its own address space: in flat and protected builds the first fix does not change the code, and in the last fix nxsem_waiter_select() and nxsem_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 (one if became two). The CONFIG_MM_KMAP code changes only in the first fix.
  • No header, macro, function result or errno changes. No documentation change: Documentation/ does not describe these paths.

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: after fork(), the sender and the waiter have the semaphore at the same address. A sem_timedwait() and a pthread_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.
Configuration Test master this PR
qemu-armv8a:nsh (flat, arm64, HVF) restartsem the other task never wakes (sem and mutex) pass; ostest status 0
mps2-an500:knsh (protected, Cortex-M7, QEMU) restartsem the other task never wakes (sem and mutex) pass
qemu-armv8a:knsh (kernel, MM_KMAP, arm64, HVF) semcross data abort in the kmap area (FAR 0x7f001000), panic in the third mutex case 5 of 5 pass
qemu-armv8a:knsh (kernel, MM_KMAP) killmxn assertion at sem_recover.c:116 in the first round all cases pass
qemu-armv8a:knsh (kernel, MM_KMAP) semwaitenv - 5 of 5 pass; ostest status 0
qemu-armv8a:knsh without MM_KMAP semwaitenv, each case in its own boot timeout of sem_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: pass 5 of 5 pass
qemu-armv8a:knsh without MM_KMAP semcross, killmxn - 5 of 5 pass; all cases pass; ostest status 0 (knsh and nsh)
rv-virt:knsh64 (kernel, no MM_KMAP in the defconfig, riscv64, QEMU TCG) semwaitenv assertion at sem_waitirq.c:82 in the idle task (timer interrupt), first case 5 of 5 pass; ostest status 0

The knsh and rv-virt runs add PIPES (the test uses a pipe); rv-virt:knsh64 adds SIG_DEFAULT for 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, no MM_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 -g passes.

Built with EXTRAFLAGS=-Werror after the CONFIG_BUILD_KERNEL guard: fvp-armv8r:pnsh, fvp-armv8r:pnsh_smp (protected, MPU address environment), qemu-armv8a:knsh, qemu-armv8a:nsh: OK.

…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]>
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Nov 10, 2026
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +22 B (+0.0%, 66,935 B / 262,144 B, total: 26% used)

esp32-devkitc

  • ROM: .flash.text +8 B (+0.0%, 130,176 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +8 B (+0.0%, 96,112 B / 3,342,304 B, total: 3% used)

mirtoo

  • kseg0_progmem: .text +40 B (+0.1%, 69,092 B / 131,072 B, total: 53% used)

qemu-armv8a

  • Code: .text.nxsem_recover +20 B, .text.nxsem_wait_irq +16 B, .text.task_restart +12 B (+0.0%, 364,004 B)

qemu-intel64

  • Code: .text +54 B (+0.0%, 8,696,438 B)

s698pm-dkit

  • Code: .text +400 B (+0.1%, 390,944 B)

stm32-nucleo-f103rb

  • flash: .text +8 B (+0.0%, 35,280 B / 131,072 B, total: 27% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Comment thread sched/sched/sched_setpriority.c Resolved
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]>
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

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

@casaroli

Copy link
Copy Markdown
Contributor Author

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

@acassis
acassis merged commit 93fd4c6 into apache:master Nov 11, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants