Commit graph

3183 commits

Author SHA1 Message Date
Peter Mitsis
70520e7a4d kernel: Change z_handle_obj_poll_events() return type
Changes the return type of z_handle_obj_poll_events() so that it
returns true if there were polling events to handle (false
otherwise).

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-02-03 19:51:20 +01:00
Björn Bergman
b45e970388 kernel: fix void* to k_thread_entry_t conversion
Fix a void* to k_thread_entry_t conversion (that is silent in GCC but
not so in some other tools) in _is_valid_prio()

Signed-off-by: Björn Bergman <bjorn.bergman@iar.com>
2025-01-29 12:51:41 +01:00
Peter Mitsis
90e2498982 kernel: Add timeout_lock usage notes
Adds a note about the timeout_lock to aid future developers
in following the rules to help prevent deadlocks involving the
timeout and scheduler spinlocks.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-28 18:14:22 +01:00
Peter Mitsis
995ad43851 kernel: Streamline z_is_thread_ready()
The check for an active timeout in z_is_thread_ready() was originally
added to cover the case of a sleeping thread. However, since there is
now a bit in the thread state that indicates if the thread is sleeping
we can drop that superfluous check.

Making this change necessitates moving k_wakeup()'s call to
z_abort_thread_timeout() so that it is within the locked
_sched_spinlock section to ensure that we do not end up with
a stray thread timeout in the timeout list.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-28 18:14:22 +01:00
Erik Tamlin
07f5a6fa18 k_pipe: fix trace point for blocking writes
Fix the trace point in k_pipe wait_for.

Signed-off-by: Erik Tamlin <erik.tamlin@percepio.com>
2025-01-28 14:13:14 +01:00
Peter Mitsis
568b63859a kernel: Remove clearing of swap_ok in do_swap()
Removes an unnecessary clearing of the current CPU's swap_ok field
in do_swap() as that clearing is already done at the end of next_up()
which was just called by z_swap_next_thread() a little earlier.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-28 07:57:20 +01:00
Peter Mitsis
bfe0b74aad kernel: Do not mark thread as queued in k_yield()
SMP does not need to mark the current thread as queued in
k_yield() as that will naturally get done in do_swap().

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-28 07:57:20 +01:00
Kalle Kietäväinen
a2eb78c03b kernel: sched: Fix meta-IRQ preemption tracking for the idle thread
When the PM subsystem is enabled, the idle thread locks the scheduler for
the duration the system is suspended. If a meta-IRQ preempts the idle
thread in this state, the idle thread is tracked in `metairq_preempted`.
However, when returning from the preemption, the idle thread is not removed
from `metairq_preempted`, unlike all the other threads. As a result, the
scheduler keeps running the idle thread even if there are higher priority
threads ready to run.

This change treats the idle thread the same way as all other threads when
returning from a meta-IRQ preemption.

Fixes #64705

Signed-off-by: Kalle Kietäväinen <kalle.kietavainen@silabs.com>
2025-01-27 13:26:20 +01:00
Nicolas Pitre
f93c1ed291 kernel/pipe: squash compiler warning
The compiler complains that:

```
zephyr/kernel/include/kernel_internal.h:121:29:
error: 'reader' may be used uninitialized [-Werror=maybe-uninitialized]
  121 |         thread->swap_retval = value;
      |         ~~~~~~~~~~~~~~~~~~~~^~~~~~~
zephyr/kernel/pipe.c: In function 'copy_to_pending_readers':
zephyr/kernel/pipe.c:92:26: note: 'reader' was declared here
   92 |         struct k_thread *reader;
      |                          ^~~~~~
```

The static analyzer fails to see through the `LOCK_SCHED_SPINLOCK`
construct that the `reader` pointer is always initialized.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-22 05:39:01 +01:00
Nicolas Pitre
0c0644bd68 kernel/pipe: disable direct-to-readers copy with CONFIG_KERNEL_COHERENCE
Systems that enabled this option don't have their stacks in coherent
memory. Given our pipe_buf_spec is stored on the stack, and readers may
also have their destination buffer on their stack too, it is not worth
going to the trouble of supporting direct-to-readers copy with them.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-21 19:44:57 +01:00
Nicolas Pitre
3da90f9d49 kernel/pipe: add missing calls to z_reschedule()
We are waking up threads but failed to let them run if they are
higher priority. Add missing calls to z_reschedule().

Also wake up all pending writers as we don't know how many there might
be. It is more efficient to wake them all when the ring buffer is full
before reading from it rather than waking them one by one whenever there is
more room in it.

Thanks to Peter Mitsis for noticing those issues.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-21 19:44:57 +01:00
Nicolas Pitre
29ae9e3435 kernel/pipe: implement direct-to-pending-readers data copy
If there are pending readers, it is best to perform a single data copy
directly into their final destination buffer rather than doing one copy
into the ring buffer just to immediately copy the same data out of it.

Incidentally, this allows for supporting pipes with no ring buffer at all.

The pipe implementation being deprecated has a similar capability so better
have it here too.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-21 19:44:57 +01:00
Nicolas Pitre
4af80d72bc kernel/pipe: remove extra hardware timer query on thread wakeup
Dispense with the call to sys_timepoint_expired() by leveraging
swap_retval to distinguish between notifications and timeouts when
z_pend_curr() returns.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-21 19:44:57 +01:00
Nicolas Pitre
46617644e7 kernel/pipe: code flow cleanup
Simplify the logic, avoid repeated conditionals, avoid superfluous
scheduler calls, make the code more efficient and easier to read.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-21 19:44:57 +01:00
David Brown
19a376aa88 kernel: poll: Allow 0 events for submitted work
Change:
    commit cc6317d7ac
    Author: Jukka Rissanen <jukka.rissanen@linux.intel.com>
    Date:   Fri Nov 1 14:03:32 2019 +0200

        kernel: poll: Allow 0 event input

Allows `k_poll` to be user with 0 events, which is useful for allowing just
a sleep without having to create artificial events.

Allow the same for `k_work_submit_to_queue()` and `k_work_submit()`.

Signed-off-by: David Brown <david.brown@linaro.org>
2025-01-21 19:44:07 +01:00
Måns Ansgariusson
efcc734b79 tracing: k_pipe: Add tracing support for reworked k_pipe API
Add tracing support for the reworked k_pipe API.

Signed-off-by: Måns Ansgariusson <Mansgariusson@gmail.com>
2025-01-17 19:43:44 +01:00
Måns Ansgariusson
6782c5381d tests: kernel: pipe: Add new test cases for pipe API rework
This commit adds new test cases for the pipe API rework.
* basic.c: Sanity check for pipe operations.
* concurrency.c: Test pipe operations with multiple threads.
* stress.c: Test pipe operations under stress conditions.

And moves the old pipe test cases to the deprecated folder.

Signed-off-by: Måns Ansgariusson <Mansgariusson@gmail.com>
2025-01-17 19:43:44 +01:00
Måns Ansgariusson
84584c579e kernel: k_pipe: Add object_core support for the k_pipe rework
This patch adds object_core support for the k_pipe api rework.

Signed-off-by: Måns Ansgariusson <Mansgariusson@gmail.com>
2025-01-17 19:43:44 +01:00
Måns Ansgariusson
a9ab8cb779 feat: enable polling support for k_pipe interface
This commit adds polling support to the newly rewritten k_pipe interface.
Changes include:

* Removed ifdef CONFIG_POLL from kernel/poll.c to let both implementations
  coexist.
* Added the needed datastructures to the new k_pipe struct.
* k_pipe_write(..) now notifies the poll subsystem that data is available.

Signed-off-by: Måns Ansgariusson <Mansgariusson@gmail.com>
2025-01-17 19:43:44 +01:00
Måns Ansgariusson
c8a2a080ac kernel: Rewrite k_pipe_* API
The `k_pipe_*` API has been reworked to provide a more consistent and
intuitive interface. The new API aims to provide a simple to use byte
stream interface that is more in line with the POSIX pipe API.
The previous API has been deprecated and will be removed in a future
release.

Signed-off-by: Måns Ansgariusson <Mansgariusson@gmail.com>
2025-01-17 19:43:44 +01:00
Nicolas Pitre
44d5d8aef2 kernel: uninline z_dummy_thread_init()
This function is getting quite involved and it also gained more callers
lately. This is not performance critical so Uninline it to save on
binary size.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-15 07:19:40 +01:00
Tom Hughes
0ec126c6d1 kernel: Add missing marshalling header for k_reschedule
Without this header, compiling the kernel.poll test with
-Werror=unused-function fails.

Signed-off-by: Tom Hughes <tomhughes@chromium.org>
2025-01-13 20:24:16 +01:00
Nicolas Pitre
bc6eded597 kernel: mark z_smp_current_get() with the const attribute
Repeated references to _current won't produce a different result as the
executing thread instance is always the same. Use the const attribute to
let the compiler know it may reuse a previously obtained value. This offset
the penalty for moving z_smp_current_get() out of line and provides yet
more binary size reduction.

This change is isolated in its own commit to ease bisecting in case some
unexpected misbehavior is eventually observed.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-10 07:49:08 +01:00
Nicolas Pitre
7a3124d866 kernel: move current thread pointer management to core code
Define the generic _current directly and get rid of the generic
arch_current_get().

The SMP default implementation is now known as z_smp_current_get().
It is no longer inlined which saves significant binary size (about 10%
for some random test case I checked).

Introduce z_current_thread_set() and use it in place of
arch_current_thread_set() for updating the current thread pointer
given this is not necessarily an architecture specific operation.
The architecture specific optimization, when enabled, should only care
about its own things and not have to also update the generic
_current_cpu->current copy.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-10 07:49:08 +01:00
Nicolas Pitre
46aa6717ff Revert "arch: deprecate _current"
Mostly a revert of commit b1def7145f ("arch: deprecate `_current`").

This commit was part of PR #80716 whose initial purpose was about providing
an architecture specific optimization for _current. The actual deprecation
was sneaked in later on without proper discussion.

The Zephyr core always used _current before and that was fine. It is quite
prevalent as well and the alternative is proving rather verbose.
Furthermore, as a concept, the "current thread" is not something that is
necessarily architecture specific. Therefore the primary abstraction
should not carry the arch_ prefix.

Hence this revert.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-10 07:49:08 +01:00
Peter Mitsis
bdb04dbfba kernel: Alter z_abort_thread_timeout() return type
No caller of the internal kernel routine z_abort_thread_timeout()
uses its return value anymore.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-09 04:04:36 +01:00
Peter Mitsis
f4f3b9378f kernel: Inline halt_thread() and z_thread_halt()
Inlining these routines helps to improve the
performance of k_thread_suspend()

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-07 18:24:09 +01:00
Peter Mitsis
d774594547 kernel: thread suspend/resume bail paths are unlikely
Gives a hint to the compiler that the bail-out paths in both
k_thread_suspend() and k_thread_resume() are unlikely events.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-07 18:24:09 +01:00
Peter Mitsis
af14e120a5 kernel: Simplify clear_halting() on UP systems
There is no need for clear_halting() to do anything on UP systems.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-07 18:24:09 +01:00
Peter Mitsis
85a9cffd0f kernel: cache priority queue index on UP multiq
Even though calculating the priority queue index in the priority
multiq is quick, caching it allows us to extract an extra 2% in
terms of performance as measured by the thread_metric cooperative
benchmark.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-07 18:24:09 +01:00
Peter Mitsis
ea6adb6726 kernel: Add custom scheduler yield routines
Adds customized yield implementations based upon the selected
scheduler (dumb, multiq or scalable). Although each follows the
same broad outline, some of them allow for additional tweaking
to extract maximal performance. For example, the multiq variant
improves the performance of k_yield() by about 20%.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-07 18:24:09 +01:00
Peter Mitsis
30f667bceb kernel: Add routines for _THREAD_QUEUED bit
Adds routines for setting and clearing the _THREAD_QUEUED
thread_state bit.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-07 18:24:09 +01:00
Peter Mitsis
472c71d3f4 kernel: Add method to dequeue from a dlist
Dequeuing from a doubly linked list is similar to removing an item
except that it does not re-initialize the dequeued node.

This comes in handy when sorting a doubly linked list (where the
node gets removed and re-added). In that circumstance, re-initializing
the node is required. Furthermore, the compiler does not always
'understand' this. Thus, when performance is critical, dequeuing
may be preferred to removing.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-07 18:24:09 +01:00
Peter Mitsis
d1c2fc0667 kernel: inline z_sched_prio_cmp()
Inlines z_sched_prio_cmp() to get better performance.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-07 18:24:09 +01:00
Peter Mitsis
c6693bfdae kernel: Clean up priority_q.h
Minor cleanups include ...
 1. Eliminating unnecessary if-defs and forward declarations
 2. Co-locating routines of the same queue type

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-01-07 18:24:09 +01:00
Dong Wang
dd5f11cb04 kernel: timeout: ensure next timeout is set when aborting the first timeout
This ensures that the system clock is correctly updated when the first
timeout is aborted, preventing unexpected early wake-up by the system clock
programmed previously.

Signed-off-by: Dong Wang <dong.d.wang@intel.com>
2025-01-06 22:36:07 +01:00
Simone Orru
37fd711a45 kernel: Add k_heap_calloc
Add the k_heap_calloc function. The
equivalent of the standard calloc for
the Zephyr kernel.

Signed-off-by: Simone Orru <simone.orru@secomind.com>
2024-12-30 17:32:03 +01:00
Yong Cong Sin
2f2dd9407c kernel/swap: remove redundant ARG_UNUSED from do_swap()
The `lock` arg is used multiple times in the function, making the
`ARG_UNUSED(lock);` redundant, remove it.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-12-24 13:53:49 +01:00
Peter Mitsis
669f2c489a kernel: Add k_reschedule()
The routine k_reschedule() allows an application to manually force
a schedule point. Although similar to k_yield(), it has different
properties. The most significant difference is that k_yield() if
invoked from a cooperative thread will voluntarily give up execution
control to the next thread of equal or higher priority while
k_reschedule() will not.

Applications that play with EDF deadlines via k_thread_deadline_set()
may need to use k_reschedule() to force a reschedule.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-12-24 04:22:51 +01:00
Flavio Ceolin
3e75c03cb2 security: Add default stack protection level
STACK_CANARIES was enabling canaries in all functions using the compiler
flag -fstack-protector-all. This became confuse with the addition of the
options STRONG and EXPLICIT.

This commit adds the missing option (default level) and disambiguous the
options mapping them close to the compiler flags.

Now we have the following options:

STACK_CANARIES            -> fstack-protector
STACK_CANARIES_STRONG     -> fstack-protector-strong
STACK_CANARIES_ALL        -> fstack-protector-all
STACK_CANARIES_EXPLICIT   -> fstack-protector-explicit

Note that from now on STACK_CANARIES_ALL is the symbol that adds canaries
for all functions.

Signed-off-by: Flavio Ceolin <flavio.ceolin@gmail.com>
2024-12-20 12:37:20 +01:00
Flavio Ceolin
0236f7c9aa security: Add option for explicit stack canaries
Add option to enable stack canaries only when explicitely
declared. It adds a new function attribute, __stack_protect, that
can be used to enable stack protection in a function.

Signed-off-by: Flavio Ceolin <flavio.ceolin@gmail.com>
2024-12-20 12:37:20 +01:00
Flavio Ceolin
82ace41da4 security: Additional option for stack canaries
Previously, when stack canaries were enabled, Zephyr applied this
protection to all functions. This commit introduces a new option that
allows stack canary protection to be applied selectively to specific
functions based on certain criteria.

Signed-off-by: Flavio Ceolin <flavio.ceolin@gmail.com>
2024-12-20 12:37:20 +01:00
Peter Mitsis
35435928c2 kernel: Decouple sleep from suspend
Sleeping and suspended are now orthogonal states. That is, a thread
may be both sleeping and suspended and the two do not interact. One
repercussion of this is that suspending a thread will no longer
abort its timeout.

Threads are now created in the 'sleeping' state instead of a
'suspended' state. This dovetails nicely with the start delay that
can be given to a newly created thread--it is as though the very
first operation that a thread with a start delay is a sleep.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-12-18 18:17:03 +01:00
Peter Mitsis
5710e034e7 kernel: Introduce _THREAD_SLEEPING state bit
At the present time, Zephyr does has overlap between sleeping and
suspending. Not only should sleeping and suspended be orthogonal
states, but we should ensure users always employ the correct API.
For example, to wake a sleeping thread, k_wakeup() should be used,
and to resume a suspended thread, k_thread_resume() should be used.
However, at the present time k_thread_resume() can be used on a
thread that called k_sleep(K_FOREVER). Sleeping should have nothing
to do with suspension.

This commit introduces the new _THREAD_SLEEPING thread state along
with some prep-work to facilitate the decoupling of the sleeping and
suspended thread states.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-12-18 18:17:03 +01:00
TaiJu Wu
fc47180850 kernel: k_msgq_put should trigger reschedule
Make `k_msgq_put` as scheduler point to resolve
high priority thread can not preempt low priority
thread.

Signed-off-by: TaiJu Wu <tjwu1217@gmail.com>
2024-12-14 01:04:43 +01:00
Peter Mitsis
815d669971 kernel: Optimize k_mem_slab_free()
Optimizes k_mem_slab_free() for the case where the slab's memory is
not exhausted.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-12-14 01:03:28 +01:00
Peter Mitsis
869acdbbab kernel: Add CONFIG_MEM_SLAB_POINTER_VALIDATE
Makes the validation of both allocated memory slab pointer and the
memory slab pointer to free configurable.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-12-14 01:03:28 +01:00
Måns Ansgariusson
7545e8d582 tracing: kernel: Add support for stopping workqueues
Adds tracing support for stopping workqueues.

Signed-off-by: Måns Ansgariusson <Mansgariusson@gmail.com>
2024-12-12 11:07:39 +01:00
Måns Ansgariusson
82a9bc4589 kernel: Add support for stopping workqueues
This patch adds support for stopping workqueues. This is useful for freeing
resources from workqueues when subsystems/modules is deactivated or
cleaning up the system between tests in ztest to reach a fully normalized
state.

The patch adds a new function k_work_queue_stop() that releases the
workqueues thread and stack when a workqueue is unwanted.
k_work_queue_stop(...) should be viewed as a counterpart to
k_work_queue_start(...).

This would allow to:
k_work_queue_start(...);

k_work_drain(..., true);
k_work_queue_stop(...);

Signed-off-by: Måns Ansgariusson <Mansgariusson@gmail.com>
2024-12-12 11:07:39 +01:00
Peter Mitsis
909ff45f0c kernel: arch: move arch_swap() declaration
Moves the arch_swap() declaration out of kernel_arch_interface.h
and into the various architectures' kernel_arch_func.h. This
permits the arch_swap() to be inlined on ARM, but extern'd on
the other architectures that still implement arch_swap().

Inlining this function on ARM has shown at least a +5% performance
boost according to the thread_metric benchmark on the disco_l475_iot1
board.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-12-11 21:33:49 +01:00