Commit graph zephyr/kernel
Author SHA1 Message Date
Brian Cain
5761d7d707 hexagon: add architecture port
Add Hexagon DSP architecture support to Zephyr RTOS, targeting the
Qualcomm Hexagon V67+ ISA running as a guest under the H2 hypervisor.

This includes:
- Architecture scaffolding (Kconfig, CMakeLists, arch selection)
- Public headers (arch.h, thread.h, exception.h, error.h)
- Core runtime: context switch, interrupt/exception handling, idle, TLS
- Memory management stubs for flat-memory H2 guest model
- SoC support for QEMU hexagon virt machine
- CMake/LLVM toolchain integration for hexagon cross-compilation

Hexagon uses H2's GEVB (Guest Event Vector Base) for interrupt dispatch
rather than a traditional function-pointer vector table, so
GEN_IRQ_VECTOR_TABLE is disabled by default.

Signed-off-by: Brian Cain <brian.cain@oss.qualcomm.com>
2026-09-12 07:52:39 -04:00
Daniel Leung
d4d4218794 kernel: mmu: recover from failed mappings in k_mem_map_phys_guard()
Previously, if a mapping operation failed partway through
k_mem_map_phys_guard(), the function would leak resources instead of
rolling back cleanly:

- map_anon_page() leaked the page frame it had just allocated when the
  subsequent arch_mem_map() call failed, since it was never returned
  to the free list.

- k_mem_map_phys_guard() never freed the virtual address region
  obtained from virt_region_alloc() when a mapping failure occurred,
  permanently leaking bitmap entries in the virtual address space.

- On partial failure, any pages already mapped before the failing one
  were left mapped and, for real page frames, still marked as in use.

Now that arch_mem_map()/arch_mem_unmap() return proper error codes,
use them to roll back on failure:

- map_anon_page() frees the page frame back to the free list before
  returning an error.

- A new unmap_anon_pages() helper reverses those pages previously
  established by map_anon_page(): unmap and remove them from eviction
  tracking if applicable, and free the page frames.

- k_mem_map_phys_guard() tracks the position of the first failed
  mapping in each of its three mapping branches (demand-mapped anon,
  regular anon, and known physical memory) and unwinds every
  previously-succeeded page with arch_mem_unmap(), undo_anon_pages()
  or undo_demand_mapped_pages() before releasing the whole virtual
  region with virt_region_free().

This replaces the previous TODO referencing #28990 about rolling back
partial anonymous mappings, which is now handled.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-09-11 05:23:20 -04:00
Daniel Leung
c21850fdd6 arch: change arch_mem_unmap() to return an error code
Mirror the arch_mem_map() change and give arch_mem_unmap() the same
treatment: change the prototype to return int instead of void, so
architecture code has a consistent way to report unmapping failures.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-09-11 05:23:20 -04:00
Daniel Leung
aec25bce22 arch: change arch_mem_map() to return an error code
Previously arch_mem_map() was a void function, so architecture
code had no consistent way to report mapping failures to callers
other than panicking or asserting. Change arch_mem_map() to
return int.

Note that this mostly maintains the k_panic() calls unless
the error is easy to handle (like invalid arguments). Each
architecture will need to be amended in the future to properly
handle the error conditions. Also some of the users are
converted to do k_panic() after failure. This is to keep
the signature change commit easier to review.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-09-11 05:23:20 -04:00
Parthiban Nallathambi
1abd829b3f kernel: timeout: initialize ticks_elapsed
GCC cannot see that ticks_elapsed is always set before use in
z_add_timeout and fails the build with -Werror=maybe-uninitialized.
Initialize it at declaration.

Signed-off-by: Parthiban Nallathambi <parthiban@linumiz.com>
2026-09-09 18:14:25 -04:00
Anas Nashif
d9ec92ccb1 kernel: fix leak of memory mapped thread stack on abort
k_thread_abort() on a thread which is not the current thread takes the
immediate cleanup path in k_thread_abort_cleanup(), which calls
do_thread_cleanup(). That function ignored its thread argument and only
ever unmapped the address stashed in thread_cleanup_stack_addr, which is
populated exclusively by defer_thread_cleanup() on the self-abort path.
The mapped stack of a thread aborted by another thread was therefore
never unmapped, leaking its virtual address region plus the two guard
pages on every create/abort cycle until the virtual address space was
exhausted.

Split the two paths so each releases what it owns:
do_deferred_thread_cleanup() unmaps the stashed region, while
do_thread_cleanup() unmaps the region recorded in the thread object,
which is still valid there as the thread is neither running nor the
current thread. The latter also clears stack_info.mapped so the now
unmapped stack is no longer reachable through e.g.
k_thread_stack_space_get(), and so that a repeated cleanup of the same
thread object is a no-op.

Add a regression test which creates and aborts a thread on the same
stack object 32 times and checks that the stack is released and its
virtual address region reused on every cycle. It fails on the first
cycle without the fix.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-09-09 10:37:59 -04:00
Hui Su
31a861492e kernel: queue: avoid spurious poll wakeups
k_queue_append_list() notified poll waiters even when every list node
was handed directly to blocked queue consumers. The queue was empty,
but poll reported data available.

Notify poll waiters only when at least one node remains queued. Add a
regression test with two blocked consumers and one poll waiter.

The regression test fails on the parent revision and passes with this
fix.

Signed-off-by: Hui Su <3164683437@qq.com>
2026-09-08 19:17:16 -04:00
Hui Su
09a0cd7896 kernel: events: honor reset for zero wait masks
k_event_wait_internal() returned before acquiring the event lock when
the requested event mask was zero. As a result, reset=true did not
clear previously posted events.

Apply reset under the event lock before handling the zero-mask fast
path.

Add a regression test that verifies stale event bits are cleared.

The regression test fails on the parent revision and passes with this
fix.

Test: native_sim/native/64
- events_api: 12 passed

Signed-off-by: Hui Su <3164683437@qq.com>
2026-09-08 19:17:12 -04:00
Nicolas Pitre
cae85560fc kernel: pipe: refuse a length that the return value cannot express
k_pipe_read() and k_pipe_write() take a size_t length but report a
successful transfer as an int, and both loop until the whole request is
satisfied, accumulating across as many blocking waits as it takes. The
count is therefore not bounded by the pipe's ring buffer, and a transfer
of more than INT_MAX bytes is truncated on the way out: at exactly 2 GiB
it comes back as INT_MIN, so a caller testing for a negative result sees
a failure after every byte was moved correctly, and at 4 GiB it reports
zero.

Refuse such a request with -EOVERFLOW instead, rather than accepting it
and misreporting the outcome.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-09-08 11:12:06 +02:00
Hui Su
a08e6b9597 kernel: sem: do not wake pollers on reset
k_sem_reset() clears the semaphore count to zero. It must not report
K_POLL_STATE_SEM_AVAILABLE to poll waiters.

The previous implementation removed and woke poll events even though no
token was available. Stop notifying semaphore poll events from
k_sem_reset(). Regular semaphore waiters still wake with -EAGAIN.

Add regression tests for single and multiple poll waiters, and for a
subsequent k_sem_give() waking a waiter after reset.

The regression tests fail on the parent revision and pass with this fix.

Test: native_sim/native/64
- poll_api: 7 passed
- poll_api_1cpu: 7 passed

Signed-off-by: Hui Su <3164683437@qq.com>
2026-09-07 10:30:37 +02:00
Aaron Wisner
be27dd7d93 kernel: avoid unnecessary time-slice scheduler locking
On ARMv8 a53 quad core SMP application running benchmarks, we see 54% of
sampled CPU cycles in spinlock related instructions. ~70% of these cycles
are related to the global scheduler spinlock.

Reduce global scheduler spinlock contention by short-circuiting the global
scheduler lock in the timer and IPI time-slice hook with atomic flag when
context switching is atomic and this CPU's slice has not expired.
Currently, every `z_time_slice()` acquires the scheduler spinlock, and
`z_time_slice()` is called every scheduler IPI and
`sys_clock_announce_locked()`. In practice, the vast majority of
z_time_slice calls are not time slice expiration, and `z_time_slice()` is a
NOP. Use a plain atomic flag per CPU because another CPU can publish an
expiration while the hook reads the flag without the scheduler lock.

Preserve the locked path for non-atomic context switches, including
pending_current synchronization. Keep expired-slice processing, per-thread
callbacks, round-robin rotation and timeout reset behavior.

We observe a 2.9% to 4.7% latency improvement and up to 10.8% throughput
improvement.

Assisted-by: Codex:GPT-5
Signed-off-by: Aaron Wisner <aaronwisner@gmail.com>
2026-09-04 21:45:23 -04:00
Appana Durga Kedareswara rao
3b3bd23a9d debug: coredump: arm64: freeze other CPUs and capture live registers
A thread actively running on another CPU at panic time was previously
invisible to real debugging: coredump only captured the panicking CPU's
exception frame, so every other thread's registers came from its
k_thread.callee_saved struct -- correct for genuinely sleeping threads,
but stale (reflecting whenever it last voluntarily context switched) for
one that's actually running elsewhere right now.

Add CONFIG_DEBUG_COREDUMP_SMP_FREEZE_CPUS (default y where the arch sets
ARCH_SUPPORTS_COREDUMP_SMP_FREEZE, i.e. arm64 Cortex-A SMP). On panic,
coredump() freezes every other online CPU before dumping and thaws them
after the memory-region walk, and emits each frozen CPU's live registers
as a new coredump section (COREDUMP_CPU_SNAPSHOT_HDR_ID, tagged with CPU
index and thread pointer, reusing the arch-info block register layout).
The panicking CPU (never frozen, since freeze only targets other CPUs)
emits a zero-payload marker recording only which thread was panicking
there, which the host side uses to identify it unambiguously.

arm64 implementation (arch/arm64/core/smp.c, coredump.c): send a new SGI
to every other online CPU. Each handler captures its exact live state --
x0-x18/lr/spsr/elr via the arch_esf that _isr_wrapper() stashes at a
fixed, nesting-independent slot on its IRQ stack, plus x19-x29 via inline
asm (valid per AAPCS64) -- then spins holding whatever it held before
being frozen, untouched, until the dump finishes, and resumes via the
standard exception-return path exactly where it was interrupted. This
matters because a panic here can be recoverable (this tree's own
coredump_threads test doesn't halt/reboot after dumping).

The SGI uses a deliberately low priority (SGI_COREDUMP_FREEZE_PRIO), not
IRQ_DEFAULT_PRIORITY: a frozen CPU never returns from its handler so
_isr_wrapper() never EOIs, and per GIC priority rules only a strictly
higher-priority interrupt can preempt a still-active one. At the default
priority a frozen CPU could not service same-priority peripheral
interrupts (confirmed on hardware as Ethernet TX-done starvation during
the UDP backend); the lowest usable priority lets everything else keep
making progress while a CPU waits.

The freeze/thaw handshake uses two separate per-CPU signals: freeze_state
(IDLE/REQUESTED/CAPTURED) and a dedicated release_requested flag that
only thaw writes. Kept separate so a late-arriving CPU (whose SGI was
delayed past the freeze-side timeout) still sees a release signal instead
of stomping it with its own CAPTURED update and spinning forever; thaw
sets release_requested unconditionally for every other CPU. The handler
wait is also bounded by a 60-second wall-clock backstop (k_cycle_get_32(),
safe from any context) so no CPU is ever stuck indefinitely. A CPU that
never responds within the freeze-side timeout (never booted, or stuck
with IRQs masked) is skipped and never blocks the dump.

The host coredump log parser learns to parse the new snapshot section.

Signed-off-by: Appana Durga Kedareswara rao <appana.durga.kedareswara.rao@amd.com>
2026-09-04 21:45:11 -04:00
Christoph Busold
158ebbd44c kernel: userspace: Support arbitrary futex addresses
This removes k_futex from the kobject table and allows any address
accessible to userspace to be used as futex and fixes #25497.

Instead of different wait queues per futex, this solution uses a
single wait queue for all threads waiting on any futex. The futex
address is stored in the thread structure. On wake, the kernel
walks through the queue and wakes either the highest priority or
all threads waiting on the specified futex address.

This is more lightweight than #42087 and more efficient for smaller
numbers of threads, but does not scale well to large numbers of
threads waiting on different futexes.

Add a test for the correct order when waking only one thread.

Signed-off-by: Christoph Busold <cbusold@qti.qualcomm.com>
2026-09-04 21:45:07 -04:00
Aditya Ganesh
1223ad1c35 kernel: Fix unused parameter warnings
Apply ARG_UNUSED() macro to resolve unused parameter warning

Signed-off-by: Aditya Ganesh <aditya.ganesh@intel.com>
2026-09-03 09:41:26 +02:00
Jinming Zhao
22d76caba5 kernel: sched: optimize scheduling IPIs for idle CPU wakeups
Add CONFIG_IPI_OPTIMIZE_IDLE to avoid waking multiple idle CPUs for a
single runnable thread. Select one active, affinity-compatible idle CPU
and record which thread its outstanding scheduling opportunity covers.

Track coverage with a reserved CPU bitmap and a per-CPU target array.
Each idle CPU and runnable thread can have at most one reservation. The
mapping represents coverage rather than binding: any CPU may select any
eligible runnable thread.

When a reserved CPU selects a different thread, transfer the selected
thread's existing reservation to the original target when possible.
Otherwise, issue a replacement IPI for the still-runnable target. Clear
reservations when their target leaves the run queue.

Apply the same optimization to MetaIRQ wakeups. The MetaIRQ can preempt
its local waker while one idle CPU is notified to run the displaced
preemptible work.

Signed-off-by: Jinming Zhao <jinmzhao@qti.qualcomm.com>
2026-09-01 11:03:59 +01:00
Dimitri Varpusvuori
52b4b49644 sys: sflist: make flag capacity ABI-dependent
sys_sflist stores flags in unused low bits of node links. The number of
available bits follows the ABI's natural pointer alignment, but the generic
header currently forces pointer-size alignment and globally requires two
bits.

That guarantee does not hold for borrowed nodes such as intrusive k_queue
items, which are caller-owned objects cast to sys_sfnode_t. On ABIs with
four-byte pointers and two-byte pointer alignment, only bit 0 is reliably
free.

Remove the forced alignment, expose SYS_SFLIST_FLAG_BITS, and let consumers
assert their own requirements. k_queue and the MMU free-page list each
require one bit. Make the unit test honor the capacity reported by the ABI
and clarify the intrusive queue alignment documentation.

This preserves generic flagged lists and safely supports one-bit ABIs.

Assisted-by: ChatGPT:gpt-5.6
Signed-off-by: Dimitri Varpusvuori <dimitri.varpusvuori@gmail.com>
2026-08-31 21:36:01 -04:00
Dhruv Menon
5a0e1f9eb7 kernel: add Pugh skip list timeout backend
This commit introduces a new timeout backend using a
Pugh skip list, which allows for O(log N) insertion
and removal of timeouts. The skip list maintains
a same-tick firing order and has no capacity
limit, making it suitable for systems with many
concurrent timeouts. The implementation includes
necessary Kconfig options and updates to the timeout
management code to support this new backend.

Signed-off-by: Dhruv Menon <dhruvmenon1104@gmail.com>
2026-08-30 16:43:01 -04:00
Aaron Wisner
4d042d3103 kernel: add configurable striped semaphore locking
Replace the system-wide semaphore spinlock with a configurable array
of naturally aligned lock stripes. Hash each semaphore by its natural
alignment without changing the semaphore object ABI.

Default CONFIG_SEM_LOCK_STRIPES to one so SMP and uniprocessor builds
preserve existing global-lock behavior and static memory use. Use a
scalar lock unless SMP striping is explicitly enabled, so UP builds
retain zero-sized spinlocks and avoid non-portable empty-struct arrays.

Require a power-of-two stripe count so the compiler reduces stripe
selection to a bitmask. Divide by the alignment of struct k_sem before
hashing to discard redundant address bits.

Do not impose cache-line alignment or padding. Explain how spinlock
and cache-line sizes affect the stripe count needed to reduce cache
contention.

Validate upstream semaphore regressions with zero-sized uniprocessor
spinlocks and SMP configurations using one, 16, and 32 stripes.
Reject non-power-of-two stripe counts at compile time.

An internal throughput microbenchmark on a quad-core ARM Cortex-A53
SMP system observed an improvement of approximately 3.1%.

Assisted-by: Codex:GPT-5
Signed-off-by: Aaron Wisner <awiz@openai.com>
2026-08-27 22:11:20 -04:00
Fin Maaß
84e82eae4c kernel: Kconfig: remove prompts from SoC and Board Hooks
The SoC and board hooks are selected by the
SoC or the board. They also implemt them.
Remove the ability to enable them regularly
by the user as the build would then fail, because
the board or SoC has not implemented them.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2026-08-27 21:26:07 +01:00
Måns Ansgariusson
839c682963 sys: __assert: migrate users away from __ASSERT_ON
Remove remaining uses of the internal __ASSERT_ON macro. Let __ASSERT()
handle disabled assertions, mark assert-only values as unused where needed,
and use CONFIG_ASSERT for assertion-only state.

Signed-off-by: Måns Ansgariusson <mansgariusson@gmail.com>
2026-08-27 12:12:11 +01:00
Fin Maaß
befb09db72 kernel: init: remove reference to legacy api
Remove reference to legacy api, where the delay
is fixed to K_FOREVER, as that, since quite some time,
no longer exists.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2026-08-26 18:14:02 -07:00
Lokesh Gundu
60d6e9ac24 kernel: mmu: use overflow-free inclusive-end form for address range asserts
Replace the unsigned-arithmetic range checks in k_mem_map_phys_bare()
and k_mem_unmap_phys_bare() with the equivalent overflow-free form:

    __ASSERT(aligned_size - 1 <= (UINTPTR_MAX - aligned_addr), ...)

The original checks were correct, but the explicit form avoids
relying on unsigned wraparound behavior and is consistent across
all assert sites in mmu.c.

Signed-off-by: Lokesh Gundu <lgundu@qti.qualcomm.com>
2026-08-26 15:14:44 -04:00
Magnus Strømme
3ac8ef05fc kernel: queue: skip blocking trace for no-wait get
An empty k_queue_get() emits the blocking trace before checking
K_NO_WAIT. This records a block that never occurs and invokes the
tracing backend on a non-blocking fast path.

Move the trace below the no-wait return so only calls that can pend
emit it. Add CTF coverage for an empty no-wait get.

Signed-off-by: Magnus Strømme <magnus.henrik@hotmail.com>
2026-08-24 14:06:45 -07:00
Hui Su
374352fef2 kernel: stack: fix allocation size overflow
k_stack_alloc_init() calculates the allocation size as
num_entries * sizeof(stack_data_t). On targets where size_t is 32 bits,
a sufficiently large entry count can overflow the multiplication and
result in an allocation much smaller than the capacity recorded in the
stack object.

The userspace verifier already checks this multiplication, but supervisor
callers invoke z_impl_k_stack_alloc_init() directly and bypass that check.

Check the multiplication in the implementation before allocating the
buffer. Treat an overflowing request as an allocation failure and return
-ENOMEM.

Add regression coverage for the supervisor allocation path. The complete
qemu_cortex_m3 stack validation passed after the fix:

    16 tests passed, 0 failed
    PROJECT EXECUTION SUCCESSFUL

Fixes: be3d4232c2 ("kernel: fix k_stack_alloc_init()")
Signed-off-by: Hui Su <3164683437@qq.com>
2026-08-22 19:20:09 -04:00
Hui Su
6468c07692 kernel: dynamic: fix kernel stack size overflow
k_thread_stack_alloc() passed K_KERNEL_STACK_LEN(size) directly to the
allocator. The alignment and reserved-size arithmetic could wrap for an
oversized request, causing it to be turned into a much smaller allocation
instead of being rejected.

Before the fix, the regression test accepted SIZE_MAX and reported:

    Assertion failed ... (stack is not NULL)
    overflowing stack size must be rejected
    FAIL - test_dynamic_thread_stack_size_overflow

Reject the allocation when K_KERNEL_STACK_LEN(size) is smaller than the
requested size, which indicates that the size calculation wrapped. The
existing userspace dynamic-object overflow check remains unchanged.

Fixes: 7b1b2576ac ("kernel: support dynamic thread stack allocation")
Signed-off-by: Hui Su <3164683437@qq.com>
2026-08-21 16:20:34 +02:00
Hui Su
0ea1a26fed kernel: queue: remove alloc nodes by payload
k_queue_alloc_append() and k_queue_alloc_prepend() wrap the caller's data
in an internal alloc_node before inserting it into the queue. The old
k_queue_remove() implementation searched for the caller data pointer as
if it were the linked-list node, so removal returned false and left the
allocated queue item in place.

Walk the queue and compare the unwrapped payload pointer instead. Free
the alloc_node wrapper after removing a matching item, while preserving
the existing behavior for directly inserted queue nodes.

Add coverage for removing items inserted through both alloc APIs.

Tested:
- ZEPHYR_TOOLCHAIN_VARIANT=gnuarmemb GNUARMEMB_TOOLCHAIN_PATH=/usr
  west build -d /tmp/zephyr-queue-gnuarmemb
- qemu_cortex_m3: queue_api 15/15 and queue_api_1cpu 7/7

Signed-off-by: Hui Su <3164683437@qq.com>
2026-08-21 16:20:05 +02:00
Peter Mitsis
a9b21fb354 kernel: userspace: Add z_waitq_head_locked()
Splits z_waitq_head() into two versions: z_waitq_head_locked() and
z_waitq_head(). When the scheduler's spinlock is known to be already
held, z_waitq_head_locked() should be used--otherwise, z_waitq_head()
is to be used.

However, this approach uncovered a path where the scheduler spinlock
could be recursively taken when a thread is aborted. To work around the
recursion (see k_thread_perms_all_clear), knowledge of the scheduler's
spinlock state must be passed to the lower layers for use in the
cleanup routines for message queues, stacks and timers.

Fixes #115756

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-21 16:17:54 +02:00
Peter Mitsis
b4ac294bf5 kernel: k_free() no longer calls z_waitq_head()
As k_malloc(), k_calloc(), k_realloc() are non-blocking calls,
k_free() should not be calling z_waitq_head() to look for blocked
threads. Developers that want the blocking/waking capabilities
should use the k_heap_xxx() routines instead.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-21 16:17:54 +02:00
Peter Mitsis
13d8b5eb23 kernel: Apply lock in stack and msgq cleanup routines
Updates the k_stack_cleanup() and k_msgq_cleanup() routines
to lock the appropriate lock to prevent two execution contexts
from trying to clean up the same object at the same time.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-21 16:17:54 +02:00
Peter Mitsis
36f1a8515a kernel: Update _sched_spinlock comment references
Updates the comments that reference _sched_spinlock to refer instead
to the scheduler's spinlock.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-21 16:17:54 +02:00
Peter Mitsis
52301ff761 kernel: Add and use z_reschedule_locked()
As part of an effort to abstract away the use of _sched_spinlock in
the kernel, this commit introduces z_reschedule_locked(). Callers
must already have _sched_spinlock held.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-21 16:17:54 +02:00
Peter Mitsis
285361130e kernel: Add and use z_swap_locked()
As part of an effort to abstract away the use of _sched_spinlock in
the kernel, this commit introduces z_swap_locked(). Callers must
already have _sched_spinlock held.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-21 16:17:54 +02:00
Peter Mitsis
772e73c8aa kernel: Add private kernel header file kspinlock.h
The scheduler's spinlock was originally confined to sched.c. However,
as the kernel has evolved and functionality has been moved around,
not only have its references proliferated, but the header files that
reference it have become somewhat brittle. This commits adds a new
private header that will abstract away most of the scheduler's
spinlock references to help keep things cleaner and applies them to
the kernel.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-21 16:17:54 +02:00
Nicolas Pitre
a672a717ca tracing: give k_sleep_ticks() its own trace point
The sleep trace hooks were attached to k_sleep(), k_msleep() and
k_usleep().  Those are inline wrappers now, and the only function left to
instrument is the k_sleep_ticks() primitive underneath them, so the three
hook families no longer have anything emitting them.  The msleep pair had
in fact been unused for some time already.

Replace all three with one k_thread_sleep_ticks pair reporting the timeout
and the time left to sleep in ticks, and drop what is left behind in each
backend: the CTF events and their top level helpers, the entry in
SYSVIEW_Zephyr.txt, and the user and test hooks.  The new CTF and
SystemView ids are fresh rather than reused, so a recording made by an
older build cannot be misread by a newer decoder.  The retired SystemView
ids stay defined, since out of tree code may refer to them.

The profiling example in the instrumentation documentation named two
symbols that no longer exist, and is updated to the ones that replace
them.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-08-20 08:52:31 +02:00
Nicolas Pitre
1d32fe9153 kernel: inline k_sleep() and k_usleep() over a tick primitive
k_sleep() and k_usleep() resolve to an out of line implementation living in
another translation unit, a system call under CONFIG_USERSPACE and a plain
call otherwise.  Either way the compiler sees neither whether the requested
duration is a constant nor whether the caller cares about the returned
value, so it always emits the tick to millisecond and microsecond
conversions.  Those conversions are the only thing that drags the 64 bit
division helper into many small builds: CONFIG_SYS_CLOCK_TICKS_PER_SEC
defaults to 10000 on tickless platforms, which lands in the integer
division path and divides a 64 bit value by ten, something GCC will not
strength reduce on a 32 bit target.

Promote the former z_tick_sleep() helper to a k_sleep_ticks() system call
that reports the remaining time in ticks, and rebuild k_sleep() and
k_usleep() as inlines on top of it.  The conversions now live at the call
site where the compiler can fold or discard them.  Nearly every caller
discards them: of the 4036 sleep call sites in the tree, exactly six use
the returned value, and only three of those are outside of tests.

What this removes is best seen in the two out of line entry points that
disappear, which on a nucleo_f030r8 (Cortex-M0, 10000 ticks/s) were the
only two callers of the 64 bit division helper in the whole image:

  08002594 <z_impl_k_sleep>:
        bl      8001... <z_tick_sleep>
        ...
        movs    r0, #9            /* the ceil() bias, 10 - 1 */
        adds    r0, r0, r2
        adcs    r1, r3
        movs    r2, #10           /* ticks / 10 -> milliseconds */
        bl      8000180 <__aeabi_uldivmod>

  080025c0 <z_impl_k_usleep>:
        movs    r0, #99           /* the ceil() bias, 100 - 1 */
        ...
        movs    r2, #100          /* microseconds / 100 -> ticks */
        bl      8000180 <__aeabi_uldivmod>
        bl      8001... <z_tick_sleep>

Both are gone, and so are __aeabi_uldivmod and __udivmoddi4, which are no
longer referenced anywhere.  A k_usleep(250) call site now passes the
literal 3 ticks the compiler worked out on its own, where it used to pass
250 microseconds to a helper that divided by 100 at runtime.  For a loop
calling k_msleep(100), k_sleep(K_SECONDS(1)) and k_usleep(250) without
using the results, FLASH drops from 11028 to 10600 bytes.

No trace point is emitted here.  The existing sleep hooks take a duration
in milliseconds or microseconds, which no longer describes what this
function is handed, and tracing cannot live in the inlines either: every
tracing backend header includes kernel.h, so a translation unit reaching a
backend header first would expand SYS_PORT_TRACING_* before those macros
exist.  The following commit gives k_sleep_ticks() a hook of its own.

The duration goes through Z_TIMEOUT_US() rather than an open coded
conversion.  That clamps a negative argument to zero, which k_usleep()
never did: it used to truncate the 64 bit conversion into an int32_t
first, which hid the worst of it, and passing it through whole would turn a
negative duration into a sleep of roughly 1.8e17 ticks rather than a bogus
absolute deadline.  The macro also carries the cast to k_ticks_t that C++
needs, since Z_TIMEOUT_TICKS_INIT() is a braced initializer there and an
unsigned to signed conversion inside one is a narrowing error.

This also removes a potential link error.  k_usleep() had no
implementation at all with CONFIG_MULTITHREADING=n, since nothread.c only
ever provided z_impl_k_sleep(), so any caller failed to link.  Nothing in
the tree happens to call it in that configuration, which is presumably why
it went unnoticed; a single z_impl_k_sleep_ticks() now serves both.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-08-20 08:52:31 +02:00
Nicolas Pitre
6484418a3a kernel: move the sleep API declarations to their own header
k_sleep(), k_msleep() and k_usleep() are declared in kernel.h today.
Later commits grow that API with a tick based primitive plus inline unit
conversions, which is more than kernel.h ought to carry for a single
service.

Move the three declarations verbatim into a new include/zephyr/sleep.h
and have kernel.h include it, so existing users need no change. The new
header has to be registered with zephyr_syscall_header(), otherwise the
marshalling stubs for k_sleep() and k_usleep() stop being generated and
CONFIG_USERSPACE builds fail to find k_sleep_mrsh.c.

The unit_testing platform fabricates empty stand ins for the generated
syscall headers rather than running the generator, so sleep.h has to be
added to that list too.  Without it any unit test reaching kernel.h fails
to find zephyr/syscalls/sleep.h.

The sleep syscalls also have to be excluded from the automatic syscall
tracing in gen_syscalls.py, as kernel.h already is.  They carry hand
written trace points in kernel/sleep.c, and without the exclusion the
generated wrapper would wrap every call in a second set.

No functional change. A test application built for nucleo_f030r8 comes
out byte identical, at 10952 bytes of FLASH before and after.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-08-20 08:52:31 +02:00
Yiren Guo
470f468760 kernel: thread_entry: fix include for TLS stack canaries
sys_rand_get() is used to initialize __stack_chk_guard under
CONFIG_STACK_CANARIES_TLS, but <zephyr/random/random.h> was included
under CONFIG_CURRENT_THREAD_USE_TLS. Builds fail when
STACK_CANARIES_TLS=y and CURRENT_THREAD_USE_TLS=n. Also fix the
mismatched endif comment.

Signed-off-by: Yiren Guo <guoyr_2013@hotmail.com>
2026-08-19 18:41:05 -04:00
Yiren Guo
279f7fc171 kernel: usage: fix unused-variable warning when THREAD_USAGE_ALL=n
With CONFIG_SCHED_THREAD_USAGE_ANALYSIS=y and
CONFIG_SCHED_THREAD_USAGE_ALL=n, sched_cpu_update_usage() is a no-op
macro, so the cycles local in k_thread_runtime_stats_enable() and
z_thread_stats_reset() is set but never read, triggering a compiler
warning. Pass the expression straight to the macro to keep
that configuration warning-free.

Signed-off-by: Yiren Guo <guoyr_2013@hotmail.com>
2026-08-19 17:12:24 +01:00
Nicolas Pitre
46f25cffc5 sys: printk: stop locking once a fatal error is being reported
printk_unlocked() serves callers that know they cannot lock, not the ones
that find out too late. A fault taken inside printk() leaves the spinlock
held by a context about to die, so every later printk() blocks and the
crash report is lost along with everything after it. CONFIG_SPIN_VALIDATE
makes it louder rather than better: validation fails, the assertion is
reported through printk, and the system spins there repeating itself.
That happens on a uniprocessor too, where the lock is uncontended but
still recorded as held.

Converting call sites cannot fix it, because code reached after the
failure does not know it is in a crash. So make printk() itself aware:
printk_panic() switches it to the unlocked path and the reporting paths
call it before emitting anything.

The switch is one way. Clearing it would also require releasing the
orphaned lock, and telling "orphaned" from "held by a CPU that is still
running" needs the owner, which only CONFIG_SPIN_VALIDATE records. So a
system that has reported a fatal error keeps unserialized output for the
rest of its life, which costs interleaving and no content.

Linux does the same: bust_spinlocks(), called from its oops and panic
paths, raises oops_in_progress so printk skips console locking. This
mechanism is narrower, printk simply stops locking, but the reasoning is
identical.

Because it cannot be undone, it must only be thrown once the fault is
terminal, which rules out the architecture entry points: several return.
z_arm64_fatal_error() resumes once demand paging has serviced the fault,
so switching there would cost a paging system its printk locking on the
first page fault. EXCEPTION_DUMP() runs only after the recoverable cases
are ruled out and also covers arch-specific dumps such as the Cortex-M
one behind PR_FAULT_INFO, and z_fatal_error() covers reports with no
dump, like k_panic() from a failed assertion. OpenRISC switches at entry
because it reports with LOG_ERR(), which is safe there since its handler
never returns.

k_str_out() shares the lock and honours the switch, so crashes reported
through printf() or puts() are covered too.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-08-19 17:09:07 +01:00
Peter Mitsis
4395894020 kernel: Fix race in mbox_message_dispose()
The internal kernel routine mbox_message_dispose() was non-atomically
modifying the internal thread fields 'thread_state'. To correct this,
_sched_spinlock must be held to prevent corruption of this field from
either an ISR or another CPU acting upon the thread.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-18 20:22:32 -04:00
Peter Mitsis
cbc956aa64 kernel: mailbox code to use z_sched_waitq_walk()
The mailbox code must search the waiting threads for a matching
message. Prior to this, it was doing so the using _WAIT_Q_FOR_EACH()
macro while holding the mailbox' spinlock. That pattern, however,
does not provide sufficient protection on an SMP system as another
CPU could process a timer that expires one of those waiting threads
thereby unexpectedly mutating the wait queue during the search. This
in turn could cause corruption and a crash.

To work around this, z_sched_waitq_walk() must be used to iterate
through the wait queue to search for and subsequently act upon
a matching message.

Note: z_sched_waitq_walk() locks _sched_spinlock for the duration of
the search. Consequently, if a thread expiration were to occur during
the search, it would spin in z_thread_timeout() until it wins
_sched_spinlock (after the search is complete).

Fixes #111643

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-18 20:22:32 -04:00
Peter Mitsis
4a767996eb kernel: mailbox code to use is_thread_dummy()
The mailbox code no longer reaches deep into the thread structure
to determine if a thread is a dummy thread. The kernel has an
existing helper routine called 'is_thread_dummy()'. Use that instead.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-18 20:22:32 -04:00
Peter Mitsis
b6bfe9fc6a kernel: Update z_sched_waitq_walk() warning
Updates the z_sched_waitq_walk() documentation to indicate that the
the walk_func callback may safefly remove the thread identified by
the callback's argument from the wait queue on the final iteration.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2026-08-18 20:22:32 -04:00
Mayur Salve
1b60add1ac kernel: mutex: fix priority inheritance with multiple held mutexes
The existing implementation unconditionally restores owner_orig_prio on
unlock, ignoring other held mutexes that still have high-priority waiters.
This causes incorrect priority restoration in nested mutex scenarios and
requires mutexes to be released in strict reverse acquisition order.

Additionally, priority boosts were not propagated through ownership chains
when the mutex owner was itself blocked on another mutex.

This change adds per-thread held_mutexes tracking and mutex_pended_on
pointer to enable:
- Correct priority recalculation on unlock by scanning all remaining held
  mutexes
- Chained priority inheritance through the full ownership chain
- Deadlock detection: assert on K_FOREVER circular ownership where
  every cycle member also waits forever; bounded chain walk prevents
  livelock on cycles not involving the current thread
- Per-thread orig_prio field records true pre-inheritance priority,
  fixing priority floor when mutexes are released in non-LIFO order

struct k_thread grows by three pointers plus a byte in default builds
(held_mutexes, mutex_pended_on, orig_prio).

Signed-off-by: Mayur Salve <msalve@qti.qualcomm.com>
2026-08-18 17:19:28 -04:00
Emil Hammarström
40b8ee1f11 kernel: Remove redundant K_FOREVER timeout check from thread schedule
Calling thread_schedule_new with a timeout not equal to K_NO_WAIT will
result in a z_add_timeout call, in the much common configuration
CONFIG_SYS_CLOCK_EXISTS=y, which returns early on K_FOREVER making the
compare redundant.

When !CONFIG_SYS_CLOCK_EXISTS the behavior is kept such that a timeout
value of K_FOREVER still results in a nop.

Signed-off-by: Emil Hammarström <emil.a.hammarstrom@gmail.com>
2026-08-17 14:16:59 +02:00
Anas Nashif
1583ed5a4b kernel: mmu: name parameter in virt_region_foreach
MISRA C:2012 Rule 8.2 requires every parameter in a function type to be
named.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-08-17 10:26:20 +02:00
Anas Nashif
f22b738c3b kernel: init: name the main() parameters
MISRA C:2012 Rule 8.2 requires every parameter in a function type to be
named. The local prototype for main() used when CONFIG_BOOTARGS is
enabled declared its two parameters with bare types.

Declaration only, no functional change.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-08-17 10:26:20 +02:00
Anas Nashif
8742067a7a kernel: obj_core: name the walk callback parameters
MISRA C:2012 Rule 8.2 requires every parameter in a function type to be
named, and that includes definitions, not just declarations. The two
object core walk functions were declared with named callback parameters
but still defined with unnamed ones.

Use the same names as the header so declaration and definition agree.
No functional change.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-08-17 10:26:20 +02:00
Anas Nashif
5058917ea6 kernel: thread: validate cpu in k_thread_runtime_stats_cpu_get()
The non-SMP branch only asserted that cpu is 0 and silently
returned CPU 0 statistics for any out-of-range value with
CONFIG_ASSERT=n. The SMP branch had no validation at all and
passed the value through to z_sched_cpu_usage(), indexing the
per-CPU array out of bounds.

Validate cpu against arch_num_cpus() with CHECKIF() returning
-EINVAL, consistent with the existing NULL check for stats, and
unify the SMP and non-SMP branches since arch_num_cpus() is 1 in
the latter case.

Assisted-by: Claude:claude-fable-5
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-08-15 15:03:10 -04:00
Anas Nashif
ef674b52f8 kernel: mmu: handle demand paging failures at runtime
Several demand paging failure conditions were only caught by
assertions, although they are reachable at runtime in a correct
program and were silently ignored with CONFIG_ASSERT=n:

- k_mem_paging_eviction_select() returning NULL (all evictable
  page frames pinned or busy) in map_anon_page() and
  do_page_fault() led to a NULL dereference. Return -ENOMEM from
  map_anon_page() and fail the fault in do_page_fault().

- page_frame_prepare_locked() failing with -ENOMEM (backing store
  full) in do_page_fault() continued with an unprepared page frame
  and garbage page-out location. Fail the fault instead, matching
  the handling map_anon_page() and do_mem_evict() already have.

- do_page_in()/do_mem_pin() ignored a false return from
  do_page_fault() (unmapped address, and now also paging resource
  exhaustion). k_mem_page_in()/k_mem_pin() return void, so the
  caller would continue believing memory is resident/pinned and
  the failure would surface at an arbitrary later access, possibly
  from a context that cannot handle a page fault. Log an error and
  panic instead.

Also drop a tautological assertion in do_mem_evict() that was
only reachable when its condition was already true.

Failed page faults are reported as fatal errors by the arch fault
handler, as already done for ARCH_PAGE_LOCATION_BAD.

Assisted-by: Claude:claude-fable-5
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-08-15 15:03:10 -04:00