zephyr/kernel
Andy Ross f0fd54cb31 kernel/sched: Fix free-memory write when ISRs abort _current
After a k_thread_abort(), the resulting thread struct is documented as
unused/free memory that may be re-used (for example, to respawn a new
thread).

But in the special case of aborting the current thread from within an
ISR, that wasn't quite happening.  The scheduler cleanup would
complete, but the architecture layer would still try to context switch
away from the aborted thread on exit, and that can include writes to
the now-reused thread struct!  The specifics will depend on
architecture (some do a full context save on entry, most don't), but
in the case of USE_SWITCH=y it will at the very least write the
switch_handle field.

Fix this simply, with a per-cpu "switch dummy" thread struct for use
as a target for context switches like this.  There is some non-trivial
memory cost to that; thread structs on many architectures are large.

Pleasingly, this also addresses a known deadlock on SMP: because the
"spin in ISR" step now happens as the very last stage of
k_thread_abort() handling, the existing scheduler lock works to
serialize calls such that it's impossible for a cycle of threads to
independently decide to spin on each other: at least one will see
itself as "already aborting" and break the cycle.

Fixes #64646

Signed-off-by: Andy Ross <andyross@google.com>
2024-05-02 13:55:03 -04:00
..
include kernel/sched: Fix free-memory write when ISRs abort _current 2024-05-02 13:55:03 -04:00
paging kernel: mm: only include demand_paging.h if needed 2023-11-23 10:01:45 +01:00
atomic_c.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
banner.c kernel: banner: use BUILD_VERSION only if not empty 2024-04-04 23:47:33 +02:00
busy_wait.c kernel: Use k_us_to_cyc_ceil32 in k_busy_wait 2023-12-05 09:24:28 +01:00
CMakeLists.txt kernel: move nothread support to own file 2024-04-06 14:22:08 +03:00
compiler_stack_protect.c
condvar.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
cpu_mask.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
device.c syscall: rename Z_OOPS -> K_OOPS 2023-11-03 11:46:52 +01:00
dynamic_disabled.c
dynamic.c kernel: align kernel stack size declaration 2024-03-27 19:27:10 -04:00
errno.c
events.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
fatal.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
float.c kernel: move float operations out of thread.c 2024-03-06 19:27:28 -05:00
futex.c
idle.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
init.c kernel/sched: Fix free-memory write when ISRs abort _current 2024-05-02 13:55:03 -04:00
ipi.c kernel: rename sliceable -> thread_is_sliceable 2024-04-01 18:47:36 -04:00
irq_offload.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
Kconfig kernel: increase main stack size for ztest on ARC 2024-04-24 10:49:05 +02:00
Kconfig.device kernel: reorg Kconfigs and split them 2024-03-06 19:27:28 -05:00
Kconfig.obj_core kernel: reorg Kconfigs and split them 2024-03-06 19:27:28 -05:00
Kconfig.smp kernel: reorg Kconfigs and split them 2024-03-06 19:27:28 -05:00
Kconfig.vm
kheap.c kernel: rename h -> heap 2024-03-06 19:27:28 -05:00
mailbox.c kernel: Clean up mailbox async msg configuration 2024-04-09 11:05:55 +02:00
main_weak.c
mem_domain.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
mem_slab.c coding guidelines: comply with MISRA Rule 2.2 2024-05-02 09:32:46 +01:00
mempool.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
mmu.c kernel: mm: introduce k_mem_phys_map()/_unmap() 2024-04-10 07:44:27 -04:00
msg_q.c coding guidelines: comply with MISRA Rule 21.15 2024-05-01 10:52:43 -04:00
mutex.c kernel: move thread related helper function kthread.h 2024-04-01 18:47:36 -04:00
nothread.c kernel: move nothread support to own file 2024-04-06 14:22:08 +03:00
obj_core.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
pipes.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
poll.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
priority_queues.c kernel: priority queues: declare as static inlines 2024-04-22 16:40:11 -04:00
queue.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
sched.c kernel/sched: Fix free-memory write when ISRs abort _current 2024-05-02 13:55:03 -04:00
sem.c coding guidelines: comply with MISRA Rule 2.2 2024-05-02 09:32:46 +01:00
smp.c kernel/sched: Fix free-memory write when ISRs abort _current 2024-05-02 13:55:03 -04:00
spinlock_validate.c kernel/spinlock: Fix SPIN_VALIDATE in ISRs 2024-05-02 13:55:03 -04:00
stack.c kernel: stack: Check possible overflow 2024-04-22 15:20:39 -04:00
system_work_q.c kernel: system_work_q: Mark queue thread as essential 2024-04-25 21:40:24 +02:00
thread_monitor.c kernel: split thread monitor 2024-03-06 19:27:28 -05:00
thread.c coding guidelines: comply with MISRA Rule 20.9 2024-05-01 19:48:19 +01:00
timeout.c kernel: timeout: Optimize setting next alarm 2024-04-09 13:55:07 -04:00
timer.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
timeslicing.c kernel: timeslicing: add time slice reset in slice per thread api 2024-05-01 22:55:50 +01:00
usage.c kernel: Remove duplicate execution_cycles write and improve docstring 2024-04-28 13:04:20 -04:00
userspace_handler.c kernel: add closing comments to config endifs 2024-03-25 18:03:31 -04:00
userspace.c coding guidelines: comply with MISRA C:2012 Rule 11.2 2024-05-01 10:53:20 -04:00
version.c
work.c coding guidelines: comply with MISRA Rule 2.2 2024-05-02 09:32:46 +01:00
xip.c