kernel: Fix race condition in z_time_slice()

Instead of directly calling the current thread-specific time slice
handler in z_time_slice(), we must call a saved copy of the handler
that was made when _sched_spinlock was still held. Otherwise there
is a small window of time where another CPU could change the handler
to NULL just before we call it.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
This commit is contained in:
Peter Mitsis
2025-12-12 14:19:07 -08:00
committed by Henrik Brix Andersen
parent c4e2db088f
commit 3affd0385e

View File

@@ -144,9 +144,11 @@ void z_time_slice(void)
if (slice_expired[_current_cpu->id] && (z_time_slice_size(curr) != 0)) {
#ifdef CONFIG_TIMESLICE_PER_THREAD
if (curr->base.slice_expired) {
k_thread_timeslice_fn_t handler = curr->base.slice_expired;
if (handler != NULL) {
k_spin_unlock(&_sched_spinlock, key);
curr->base.slice_expired(curr, curr->base.slice_data);
handler(curr, curr->base.slice_data);
key = k_spin_lock(&_sched_spinlock);
}
#endif