From 0a7c316ffb06059af8555475ab524408cb34d3ea Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 5 Aug 2024 16:58:10 +0300 Subject: [PATCH] Kernel: Fix race condition when blocking threads If thread was blocked, but had not reached block queue, you might already get an unblock request which would fail on an assertion. If blocked thread was load balanced to another processor and unblocked simultaneously, there was a race condition. --- kernel/kernel/Scheduler.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kernel/kernel/Scheduler.cpp b/kernel/kernel/Scheduler.cpp index 197d93ad..02d9c4cc 100644 --- a/kernel/kernel/Scheduler.cpp +++ b/kernel/kernel/Scheduler.cpp @@ -330,11 +330,13 @@ namespace Kernel { if (!node->blocked) return; - m_block_queue.remove_node(node); + if (node != m_current) + m_block_queue.remove_node(node); if (node->blocker) node->blocker->remove_blocked_thread(node); node->blocked = false; - m_run_queue.add_thread_to_back(node); + if (node != m_current) + m_run_queue.add_thread_to_back(node); } else { @@ -467,6 +469,9 @@ namespace Kernel break; if (thread_info.node == m_current || thread_info.queue == nullptr) continue; + // FIXME: allow load balancing with blocked threads, with this algorithm there is a race condition + if (thread_info.node->blocked) + continue; auto least_loaded_id = find_least_loaded_processor(); if (least_loaded_id == Processor::current_id())