From 64a9620556f579c8d696dc11c21a2695a321f137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 12 Sep 2023 12:39:19 +0200 Subject: [PATCH] refactor(scheduler): move scheduler input to core-local storage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/arch/aarch64/kernel/core_local.rs | 10 ++++++++++ src/arch/x86_64/kernel/core_local.rs | 9 +++++++++ src/scheduler/mod.rs | 17 ++++++----------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/arch/aarch64/kernel/core_local.rs b/src/arch/aarch64/kernel/core_local.rs index 71c917d9f5..a700972923 100644 --- a/src/arch/aarch64/kernel/core_local.rs +++ b/src/arch/aarch64/kernel/core_local.rs @@ -5,9 +5,14 @@ use core::cell::{Cell, RefCell, RefMut}; use core::ptr; use core::sync::atomic::Ordering; +#[cfg(feature = "smp")] +use hermit_sync::InterruptTicketMutex; + use super::interrupts::{IrqStatistics, IRQ_COUNTERS}; use super::CPU_ONLINE; use crate::executor::task::AsyncTask; +#[cfg(feature = "smp")] +use crate::scheduler::SchedulerInput; use crate::scheduler::{CoreId, PerCoreScheduler}; pub(crate) struct CoreLocal { @@ -20,6 +25,9 @@ pub(crate) struct CoreLocal { irq_statistics: &'static IrqStatistics, /// Queue of async tasks async_tasks: RefCell>, + /// Queues to handle incoming requests from the other cores + #[cfg(feature = "smp")] + pub scheduler_input: InterruptTicketMutex, } impl CoreLocal { @@ -39,6 +47,8 @@ impl CoreLocal { scheduler: Cell::new(ptr::null_mut()), irq_statistics, async_tasks: RefCell::new(Vec::new()), + #[cfg(feature = "smp")] + scheduler_input: InterruptTicketMutex::new(SchedulerInput::new()), }; let this = if core_id == 0 { take_static::take_static! { diff --git a/src/arch/x86_64/kernel/core_local.rs b/src/arch/x86_64/kernel/core_local.rs index 323b396a48..bf6210f5fa 100644 --- a/src/arch/x86_64/kernel/core_local.rs +++ b/src/arch/x86_64/kernel/core_local.rs @@ -5,6 +5,8 @@ use core::cell::{Cell, RefCell, RefMut}; use core::ptr; use core::sync::atomic::Ordering; +#[cfg(feature = "smp")] +use hermit_sync::InterruptTicketMutex; use x86_64::registers::model_specific::GsBase; use x86_64::structures::tss::TaskStateSegment; use x86_64::VirtAddr; @@ -12,6 +14,8 @@ use x86_64::VirtAddr; use super::interrupts::{IrqStatistics, IRQ_COUNTERS}; use super::CPU_ONLINE; use crate::executor::task::AsyncTask; +#[cfg(feature = "smp")] +use crate::scheduler::SchedulerInput; use crate::scheduler::{CoreId, PerCoreScheduler}; #[repr(C)] @@ -29,6 +33,9 @@ pub(crate) struct CoreLocal { irq_statistics: &'static IrqStatistics, /// Queue of async tasks async_tasks: RefCell>, + /// Queues to handle incoming requests from the other cores + #[cfg(feature = "smp")] + pub scheduler_input: InterruptTicketMutex, } impl CoreLocal { @@ -52,6 +59,8 @@ impl CoreLocal { kernel_stack: Cell::new(0), irq_statistics, async_tasks: RefCell::new(Vec::new()), + #[cfg(feature = "smp")] + scheduler_input: InterruptTicketMutex::new(SchedulerInput::new()), }; let this = if core_id == 0 { take_static::take_static! { diff --git a/src/scheduler/mod.rs b/src/scheduler/mod.rs index e55032ce60..af687a65d4 100644 --- a/src/scheduler/mod.rs +++ b/src/scheduler/mod.rs @@ -35,7 +35,7 @@ static TASKS: InterruptTicketMutex> = pub type CoreId = u32; #[cfg(feature = "smp")] -struct SchedulerInput { +pub struct SchedulerInput { /// Queue of new tasks new_tasks: VecDeque, /// Queue of task, which are wakeup by another core @@ -74,9 +74,6 @@ pub struct PerCoreScheduler { finished_tasks: VecDeque>>, /// Queue of blocked tasks, sorted by wakeup time. blocked_tasks: BlockedTaskQueue, - /// Queues to handle incoming requests from the other cores - #[cfg(feature = "smp")] - input: InterruptTicketMutex, } struct NewTask { @@ -447,7 +444,7 @@ impl PerCoreScheduler { #[cfg(all(target_arch = "x86_64", feature = "smp"))] pub fn check_input(&mut self) { - let mut input_locked = self.input.lock(); + let mut input_locked = CoreLocal::get().scheduler_input.lock(); while let Some(task) = input_locked.wakeup_tasks.pop_front() { self.blocked_tasks.custom_wakeup(task); @@ -689,18 +686,16 @@ pub fn add_current_core() { ready_queue: PriorityTaskQueue::new(), finished_tasks: VecDeque::new(), blocked_tasks: BlockedTaskQueue::new(), - #[cfg(feature = "smp")] - input: InterruptTicketMutex::new(SchedulerInput::new()), }); let scheduler = Box::into_raw(boxed_scheduler); set_core_scheduler(scheduler); #[cfg(feature = "smp")] { - let scheduler = unsafe { scheduler.as_ref().unwrap() }; - SCHEDULER_INPUTS - .lock() - .insert(core_id.try_into().unwrap(), &scheduler.input); + SCHEDULER_INPUTS.lock().insert( + core_id.try_into().unwrap(), + &CoreLocal::get().scheduler_input, + ); } }