kernel/sched: Fix race between thread wakeup timeout and abort

Aborted threads will cancel their timeouts, but the timeout subsystem
isn't protected under the same lock so it's possible for a timeout to
fire just as a thread is being aborted and wake it up unexpectedly.
Check the state before blowing anything up.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
This commit is contained in:
Andy Ross 2021-02-17 10:12:36 -08:00 committed by Anas Nashif
commit 37866336f9

View file

@ -765,16 +765,21 @@ ALWAYS_INLINE void z_unpend_thread_no_timeout(struct k_thread *thread)
/* Timeout handler for *_thread_timeout() APIs */
void z_thread_timeout(struct _timeout *timeout)
{
LOCKED(&sched_spinlock) {
struct k_thread *thread = CONTAINER_OF(timeout,
struct k_thread, base.timeout);
struct k_thread *thread = CONTAINER_OF(timeout,
struct k_thread, base.timeout);
if (thread->base.pended_on != NULL) {
unpend_thread_no_timeout(thread);
LOCKED(&sched_spinlock) {
bool killed = ((thread->base.thread_state & _THREAD_DEAD) ||
(thread->base.thread_state & _THREAD_ABORTING));
if (!killed) {
if (thread->base.pended_on != NULL) {
unpend_thread_no_timeout(thread);
}
z_mark_thread_as_started(thread);
z_mark_thread_as_not_suspended(thread);
ready_thread(thread);
}
z_mark_thread_as_started(thread);
z_mark_thread_as_not_suspended(thread);
ready_thread(thread);
}
}
#endif