From 9176f0f59f3f0cb9db3af6080449208580ee8444 Mon Sep 17 00:00:00 2001 From: atsushi421 Date: Fri, 13 Nov 2026 14:06:30 +0900 Subject: [PATCH 1/4] fix(sched): preempt when queued EDF tasks claim every idle core Before this change, the EDF scheduler did not preempt if one core in the set of the woken task was idle. An idle core takes only one task, and it stays idle until the primary CPU wakes it. If several tasks woke in that window, all of them were enqueued, and only one of them got the idle core. The others waited behind running tasks with later deadlines. Now the scheduler skips preemption only if an idle core takes the task. An idle core takes the earliest queued task runnable on it. So it takes the woken task if the task is earlier than every queued task runnable on that core, or if the queue holds fewer tasks than the set has idle cores. Otherwise, the scheduler chooses a victim among the busy cores of the set. Signed-off-by: atsushi421 --- awkernel_async_lib/src/scheduler/edf.rs | 113 +++++++++++++++++++++--- mdbook/src/internal/scheduler.md | 2 +- 2 files changed, 100 insertions(+), 15 deletions(-) diff --git a/awkernel_async_lib/src/scheduler/edf.rs b/awkernel_async_lib/src/scheduler/edf.rs index 065a880e8..f11af72e5 100644 --- a/awkernel_async_lib/src/scheduler/edf.rs +++ b/awkernel_async_lib/src/scheduler/edf.rs @@ -75,7 +75,7 @@ impl Scheduler for EDFScheduler { let mut node = MCSNode::new(); let _guard = GLOBAL_WAKE_GET_MUTEX.lock(&mut node); - if !self.invoke_preemption(task.clone()) { + if !self.invoke_preemption(task.clone(), (absolute_deadline, wake_time)) { let mut node_inner = MCSNode::new(); let mut data = self.data.lock(&mut node_inner); let queue = data.get_or_insert_with(|| EDFQueue::new(num_cpu())); @@ -161,31 +161,45 @@ pub static SCHEDULER: EDFScheduler = EDFScheduler { }; impl EDFScheduler { - fn invoke_preemption(&self, task: Arc) -> bool { + /// `key` is the queue priority of `task`, `(absolute_deadline, wake_time)`. + fn invoke_preemption(&self, task: Arc, key: (u64, u64)) -> bool { if !PREEMPTION_ENABLED { return false; } let cpu_set = task.cpu_set.expect("Task has no CPU set"); - // Find the CPU whose target (running or pending-preemption) task has - // the lowest priority among the set. - let mut victim: Option<(usize, Arc)> = None; - + let mut idle_cpus = CpuSet::empty(); for cpu_id in cpu_set.iter() { - let task_running = get_task_running(cpu_id); + match get_task_running(cpu_id).task_id { + // If the task has already been running, preempt is not required. + id if id == task.id => return false, + 0 => idle_cpus.insert(cpu_id), + _ => (), + } + } - // If the task has already been running, preempt is not required. - if task_running.task_id == task.id { + if !idle_cpus.is_empty() { + let mut node = MCSNode::new(); + let data = self.data.lock(&mut node); + if (*data) + .as_ref() + .is_none_or(|queue| idle_cpu_takes(queue, idle_cpus, key)) + { return false; } + } - // An idle CPU in the set will pick the task up; no preemption needed. - if task_running.task_id == 0 { - return false; + // Find the CPU whose target (running or pending-preemption) task has + // the lowest priority among the busy CPUs of the set. + let mut victim: Option<(usize, Arc)> = None; + + for cpu_id in cpu_set.iter() { + if idle_cpus.contains(cpu_id) { + continue; } - let task_running = get_task(task_running.task_id); + let task_running = get_task(get_task_running(cpu_id).task_id); let task_pending = peek_preemption_pending(cpu_id); let target_task = match (task_running, task_pending) { @@ -221,7 +235,7 @@ impl EDFScheduler { // is not a lost wakeup: the task is eventually run either by the // victim's preemption handler (`do_preemption`), or re-woken by the // victim's `run_main` if its running task finishes first, in which - // case the idle check above routes it to the queue. Future work: + // case this function decides again. Future work: // an enqueue-instead-of-pin strategy (push to the affinity queue // and treat the IPI as a hint) would close this window, but it // requires redesigning the duplicate-IPI suppression that the @@ -233,3 +247,74 @@ impl EDFScheduler { false } } + +/// Whether a CPU of `idle_cpus` takes a task of priority `key` if the task is +/// pushed to `queue` now. +/// +/// An idle CPU takes the earliest queued task runnable on it. So the task is +/// taken if it is earlier than every queued task runnable on one of +/// `idle_cpus`, or if `queue` holds fewer tasks than `idle_cpus` holds CPUs. +/// Otherwise, the queued tasks can take every idle CPU first, and the task +/// must preempt to run now. +fn idle_cpu_takes( + queue: &AffinityBTreeQueue<(u64, u64), V, DEFAULT_MIN_DEGREE, CPU_SET_WORDS>, + idle_cpus: CpuSet, + key: (u64, u64), +) -> bool { + queue.len() < idle_cpus.iter().count() + || idle_cpus.iter().any(|cpu_id| { + queue + .peek_key_for_cpu(cpu_id) + .is_none_or(|queued| queued.priority > key) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + type Queue = AffinityBTreeQueue<(u64, u64), (), DEFAULT_MIN_DEGREE, CPU_SET_WORDS>; + + const IDLE_2: CpuSet = CpuSet::empty().with(2); + const ALL: CpuSet = CpuSet::empty().with(1).with(2).with(3); + + #[test] + fn idle_cpu_takes_task_from_empty_queue() { + assert!(idle_cpu_takes(&Queue::new(4), IDLE_2, (10, 0))); + } + + #[test] + fn earlier_queued_task_claims_the_only_idle_cpu() { + let mut queue = Queue::new(4); + queue.push((5, 0), ALL, ()).unwrap(); + assert!(!idle_cpu_takes(&queue, IDLE_2, (10, 0))); + } + + #[test] + fn equal_key_is_fifo() { + let mut queue = Queue::new(4); + queue.push((10, 0), ALL, ()).unwrap(); + assert!(!idle_cpu_takes(&queue, IDLE_2, (10, 0))); + } + + #[test] + fn task_earlier_than_queue_head_is_taken() { + let mut queue = Queue::new(4); + queue.push((20, 0), ALL, ()).unwrap(); + assert!(idle_cpu_takes(&queue, IDLE_2, (10, 0))); + } + + #[test] + fn spare_idle_cpu_takes_task() { + let mut queue = Queue::new(4); + queue.push((5, 0), ALL, ()).unwrap(); + assert!(idle_cpu_takes(&queue, IDLE_2.with(3), (10, 0))); + } + + #[test] + fn queued_task_of_another_cpu_does_not_claim_idle_cpu() { + let mut queue = Queue::new(4); + queue.push((5, 0), CpuSet::empty().with(3), ()).unwrap(); + assert!(idle_cpu_takes(&queue, IDLE_2, (10, 0))); + } +} diff --git a/mdbook/src/internal/scheduler.md b/mdbook/src/internal/scheduler.md index c15853e4e..d39a2fc3d 100644 --- a/mdbook/src/internal/scheduler.md +++ b/mdbook/src/internal/scheduler.md @@ -75,7 +75,7 @@ The Earliest Deadline First (EDF) scheduler is implemented in [edf.rs](https://g The scheduler holds a single affinity-aware priority queue, [`AffinityBTreeQueue`](https://crates.io/crates/affinity_btree_queue), backed by an augmented B-tree. Each entry carries `(priority, affinity, task)`, where the priority is the pair `(absolute_deadline, wake_time)`; smaller values dequeue first, so tasks are ordered by earliest deadline with wake time as a tie-breaker. Every B-tree node stores the OR of the affinities in its subtree, which lets `pop_for_cpu(cpu)` find the earliest-deadline task runnable on `cpu` in logarithmic time while skipping subtrees with no eligible entry. -When a task is enqueued via `wake_task()`, the scheduler reads the `SchedulerType::EDF(relative_deadline, cpu_set)` attached to the task and calculates the absolute deadline as `uptime + relative_deadline`. If the task is part of a DAG, `calculate_and_update_dag_deadline()` is used instead to propagate deadlines through the DAG. The task priority (`PriorityInfo`) becomes `MAX_TASK_PRIORITY - absolute_deadline`, so an earlier deadline gives a higher priority. The task is then pushed into the queue with its `cpu_set` as the affinity mask. Preemption is handled via `invoke_preemption()`: if no core in the set is idle and the task is not already running, the core running the lowest-priority task among the set is chosen, and an IPI is sent to it when the newly enqueued task has an earlier deadline than the task currently running (or pending preemption) on that core. +When a task is enqueued via `wake_task()`, the scheduler reads the `SchedulerType::EDF(relative_deadline, cpu_set)` attached to the task and calculates the absolute deadline as `uptime + relative_deadline`. If the task is part of a DAG, `calculate_and_update_dag_deadline()` is used instead to propagate deadlines through the DAG. The task priority (`PriorityInfo`) becomes `MAX_TASK_PRIORITY - absolute_deadline`, so an earlier deadline gives a higher priority. The task is then pushed into the queue with its `cpu_set` as the affinity mask. Preemption is handled via `invoke_preemption()`. If the task is already running, the scheduler does nothing. If an idle core in the set will take the task, the scheduler only enqueues it. An idle core takes the earliest queued task runnable on it, so it takes the new task if the task is earlier than every queued task runnable on that core, or if the queue holds fewer tasks than the set has idle cores. Otherwise, the scheduler chooses the core running the lowest-priority task among the busy cores of the set. It sends an IPI to that core when the new task has a higher priority than the task running (or pending preemption) on it. Against another EDF task, a higher priority means an earlier deadline. `get_next()` pops the earliest-deadline task whose `cpu_set` contains the calling CPU (`pop_for_cpu`), so a task is only ever dequeued by a core within its set. This guarantees CPU affinity. CPU 0 (the primary core) is always excluded from `cpu_set` when a task is spawned. The `cpu_set` is normalized at spawn time by removing CPU 0 and out-of-range bits; if this leaves the set empty (for example an empty set, or one naming only CPU 0 or out-of-range cores), the task is not rejected but falls back to all worker cores (`1..num_cpu()`) with a warning. From 7f2110fd391582c94102404360a3fe0b08f53c9c Mon Sep 17 00:00:00 2001 From: atsushi421 Date: Mon, 16 Nov 2026 10:12:19 +0900 Subject: [PATCH 2/4] fix(sched): ignore idle cores in EDF preemption, as GEDF did If an idle core of the set took the woken task, the previous commit skipped preemption. That rule judges only the woken task. It does not judge again a queued task that a later task pushes off the idle core. For example, take one idle core and two busy cores that run tasks with deadlines 50 and 40. If a task with deadline 12 wakes before a task with deadline 10, the rule queues both without preemption. The idle core takes the task with deadline 10, and the task with deadline 12 waits while the tasks with deadlines 50 and 40 run. If the sets differ, a rule that compares the queue length with the number of idle cores also fails. For example, cores 2 and 3 are idle, and the rule queues two tasks. Core 2 can take the one that core 3 can also run. Then the other task, whose set holds cores 1 and 2, waits behind a task with a later deadline on core 1. Now the scheduler ignores idle cores, as the GEDF scheduler did before PR #787. It chooses the victim among the busy cores of the set. If the woken task has a higher priority than the task on that core, the scheduler preempts it. If an idle core can take the task, the preemption is unneeded. But a sleeping core runs a queued task only after the primary CPU wakes it. With this rule, the preempted task waits for that wake instead of the woken task. If the set of a task holds one core, the behavior does not change. That set has no busy core while its core is idle. Signed-off-by: atsushi421 --- awkernel_async_lib/src/scheduler/edf.rs | 115 ++++-------------------- mdbook/src/internal/scheduler.md | 2 +- 2 files changed, 18 insertions(+), 99 deletions(-) diff --git a/awkernel_async_lib/src/scheduler/edf.rs b/awkernel_async_lib/src/scheduler/edf.rs index f11af72e5..3bfb66185 100644 --- a/awkernel_async_lib/src/scheduler/edf.rs +++ b/awkernel_async_lib/src/scheduler/edf.rs @@ -75,7 +75,7 @@ impl Scheduler for EDFScheduler { let mut node = MCSNode::new(); let _guard = GLOBAL_WAKE_GET_MUTEX.lock(&mut node); - if !self.invoke_preemption(task.clone(), (absolute_deadline, wake_time)) { + if !self.invoke_preemption(task.clone()) { let mut node_inner = MCSNode::new(); let mut data = self.data.lock(&mut node_inner); let queue = data.get_or_insert_with(|| EDFQueue::new(num_cpu())); @@ -161,45 +161,35 @@ pub static SCHEDULER: EDFScheduler = EDFScheduler { }; impl EDFScheduler { - /// `key` is the queue priority of `task`, `(absolute_deadline, wake_time)`. - fn invoke_preemption(&self, task: Arc, key: (u64, u64)) -> bool { + fn invoke_preemption(&self, task: Arc) -> bool { if !PREEMPTION_ENABLED { return false; } let cpu_set = task.cpu_set.expect("Task has no CPU set"); - let mut idle_cpus = CpuSet::empty(); - for cpu_id in cpu_set.iter() { - match get_task_running(cpu_id).task_id { - // If the task has already been running, preempt is not required. - id if id == task.id => return false, - 0 => idle_cpus.insert(cpu_id), - _ => (), - } - } - - if !idle_cpus.is_empty() { - let mut node = MCSNode::new(); - let data = self.data.lock(&mut node); - if (*data) - .as_ref() - .is_none_or(|queue| idle_cpu_takes(queue, idle_cpus, key)) - { - return false; - } - } - // Find the CPU whose target (running or pending-preemption) task has // the lowest priority among the busy CPUs of the set. let mut victim: Option<(usize, Arc)> = None; for cpu_id in cpu_set.iter() { - if idle_cpus.contains(cpu_id) { + let task_running = get_task_running(cpu_id); + + // If the task has already been running, preempt is not required. + if task_running.task_id == task.id { + return false; + } + + // An idle CPU does not stop the preemption. A task queued before + // or after this one can take the idle CPU first, and a sleeping + // CPU runs a queued task only after the primary CPU wakes it. So + // this task preempts the lowest-priority busy CPU even if an idle + // CPU can take it. + if task_running.task_id == 0 { continue; } - let task_running = get_task(get_task_running(cpu_id).task_id); + let task_running = get_task(task_running.task_id); let task_pending = peek_preemption_pending(cpu_id); let target_task = match (task_running, task_pending) { @@ -229,7 +219,7 @@ impl EDFScheduler { // heap and is not in the affinity queue, so it is invisible to the // other CPUs in its `cpu_set` (neither `pop_for_cpu`, the clustered // task counter, nor `queued_cpu_mask` sees it). If another CPU in - // the set becomes idle while the IPI is in flight, it cannot pick + // the set is or becomes idle while the IPI is in flight, it cannot pick // the task up and may go to sleep — a latency window bounded by the // IPI delivery plus the victim's interrupt-disabled sections. This // is not a lost wakeup: the task is eventually run either by the @@ -247,74 +237,3 @@ impl EDFScheduler { false } } - -/// Whether a CPU of `idle_cpus` takes a task of priority `key` if the task is -/// pushed to `queue` now. -/// -/// An idle CPU takes the earliest queued task runnable on it. So the task is -/// taken if it is earlier than every queued task runnable on one of -/// `idle_cpus`, or if `queue` holds fewer tasks than `idle_cpus` holds CPUs. -/// Otherwise, the queued tasks can take every idle CPU first, and the task -/// must preempt to run now. -fn idle_cpu_takes( - queue: &AffinityBTreeQueue<(u64, u64), V, DEFAULT_MIN_DEGREE, CPU_SET_WORDS>, - idle_cpus: CpuSet, - key: (u64, u64), -) -> bool { - queue.len() < idle_cpus.iter().count() - || idle_cpus.iter().any(|cpu_id| { - queue - .peek_key_for_cpu(cpu_id) - .is_none_or(|queued| queued.priority > key) - }) -} - -#[cfg(test)] -mod tests { - use super::*; - - type Queue = AffinityBTreeQueue<(u64, u64), (), DEFAULT_MIN_DEGREE, CPU_SET_WORDS>; - - const IDLE_2: CpuSet = CpuSet::empty().with(2); - const ALL: CpuSet = CpuSet::empty().with(1).with(2).with(3); - - #[test] - fn idle_cpu_takes_task_from_empty_queue() { - assert!(idle_cpu_takes(&Queue::new(4), IDLE_2, (10, 0))); - } - - #[test] - fn earlier_queued_task_claims_the_only_idle_cpu() { - let mut queue = Queue::new(4); - queue.push((5, 0), ALL, ()).unwrap(); - assert!(!idle_cpu_takes(&queue, IDLE_2, (10, 0))); - } - - #[test] - fn equal_key_is_fifo() { - let mut queue = Queue::new(4); - queue.push((10, 0), ALL, ()).unwrap(); - assert!(!idle_cpu_takes(&queue, IDLE_2, (10, 0))); - } - - #[test] - fn task_earlier_than_queue_head_is_taken() { - let mut queue = Queue::new(4); - queue.push((20, 0), ALL, ()).unwrap(); - assert!(idle_cpu_takes(&queue, IDLE_2, (10, 0))); - } - - #[test] - fn spare_idle_cpu_takes_task() { - let mut queue = Queue::new(4); - queue.push((5, 0), ALL, ()).unwrap(); - assert!(idle_cpu_takes(&queue, IDLE_2.with(3), (10, 0))); - } - - #[test] - fn queued_task_of_another_cpu_does_not_claim_idle_cpu() { - let mut queue = Queue::new(4); - queue.push((5, 0), CpuSet::empty().with(3), ()).unwrap(); - assert!(idle_cpu_takes(&queue, IDLE_2, (10, 0))); - } -} diff --git a/mdbook/src/internal/scheduler.md b/mdbook/src/internal/scheduler.md index d39a2fc3d..01aa3840f 100644 --- a/mdbook/src/internal/scheduler.md +++ b/mdbook/src/internal/scheduler.md @@ -75,7 +75,7 @@ The Earliest Deadline First (EDF) scheduler is implemented in [edf.rs](https://g The scheduler holds a single affinity-aware priority queue, [`AffinityBTreeQueue`](https://crates.io/crates/affinity_btree_queue), backed by an augmented B-tree. Each entry carries `(priority, affinity, task)`, where the priority is the pair `(absolute_deadline, wake_time)`; smaller values dequeue first, so tasks are ordered by earliest deadline with wake time as a tie-breaker. Every B-tree node stores the OR of the affinities in its subtree, which lets `pop_for_cpu(cpu)` find the earliest-deadline task runnable on `cpu` in logarithmic time while skipping subtrees with no eligible entry. -When a task is enqueued via `wake_task()`, the scheduler reads the `SchedulerType::EDF(relative_deadline, cpu_set)` attached to the task and calculates the absolute deadline as `uptime + relative_deadline`. If the task is part of a DAG, `calculate_and_update_dag_deadline()` is used instead to propagate deadlines through the DAG. The task priority (`PriorityInfo`) becomes `MAX_TASK_PRIORITY - absolute_deadline`, so an earlier deadline gives a higher priority. The task is then pushed into the queue with its `cpu_set` as the affinity mask. Preemption is handled via `invoke_preemption()`. If the task is already running, the scheduler does nothing. If an idle core in the set will take the task, the scheduler only enqueues it. An idle core takes the earliest queued task runnable on it, so it takes the new task if the task is earlier than every queued task runnable on that core, or if the queue holds fewer tasks than the set has idle cores. Otherwise, the scheduler chooses the core running the lowest-priority task among the busy cores of the set. It sends an IPI to that core when the new task has a higher priority than the task running (or pending preemption) on it. Against another EDF task, a higher priority means an earlier deadline. +When a task is enqueued via `wake_task()`, the scheduler reads the `SchedulerType::EDF(relative_deadline, cpu_set)` attached to the task and calculates the absolute deadline as `uptime + relative_deadline`. If the task is part of a DAG, `calculate_and_update_dag_deadline()` is used instead to propagate deadlines through the DAG. The task priority (`PriorityInfo`) becomes `MAX_TASK_PRIORITY - absolute_deadline`, so an earlier deadline gives a higher priority. The task is then pushed into the queue with its `cpu_set` as the affinity mask. `invoke_preemption()` handles preemption. If the task is already running, the scheduler enqueues it without preemption. Otherwise, the scheduler chooses the core running the lowest-priority task among the busy cores of the set. If the new task has a higher priority than the task running (or pending preemption) on that core, the scheduler sends an IPI to it. Against another EDF task, a higher priority means an earlier deadline. An EDF task preempts a task of a lower-priority scheduler whatever its deadline. An idle core in the set does not stop the preemption. A task queued before or after the new task can take the idle core first. Also, a sleeping core runs a queued task only after the primary CPU wakes it. `get_next()` pops the earliest-deadline task whose `cpu_set` contains the calling CPU (`pop_for_cpu`), so a task is only ever dequeued by a core within its set. This guarantees CPU affinity. CPU 0 (the primary core) is always excluded from `cpu_set` when a task is spawned. The `cpu_set` is normalized at spawn time by removing CPU 0 and out-of-range bits; if this leaves the set empty (for example an empty set, or one naming only CPU 0 or out-of-range cores), the task is not rejected but falls back to all worker cores (`1..num_cpu()`) with a warning. From 79e756e9cea5c0be5128d892b37d23a093ac144c Mon Sep 17 00:00:00 2001 From: atsushi421 Date: Mon, 16 Nov 2026 11:01:30 +0900 Subject: [PATCH 3/4] fix(sched): re-wake pinned tasks before an idle worker sleeps A waker reads `RUNNING` of a CPU and pins a task to the preemption-pending heap of that CPU under `GLOBAL_WAKE_GET_MUTEX`. But the CPU clears `RUNNING` and empties the heap without that mutex. If the CPU finishes its task and empties the heap between the read and the pin, it finds no queued task and sleeps. While the CPU runs no task, the preemption IPI can leave the task pinned, and `wake_workers()` does not see the heap. Then the task waits until an unrelated interrupt wakes the CPU. Now `run_main()` checks the heap again before the CPU sleeps. The check comes after `get_next_task()`, which takes `GLOBAL_WAKE_GET_MUTEX`. So the check sees the pin of every waker that read the CPU as busy. On main, `NUM_TASK_IN_QUEUE` underflows, so `wake_workers()` wakes every sleeping worker on each loop of the primary CPU. This hides the race. PR #792 fixes the underflow, so this fix must come first. With the previous commit, the EDF scheduler also pins a task while its set has an idle core. So the race can leave those idle cores asleep too. Signed-off-by: atsushi421 --- awkernel_async_lib/src/scheduler/edf.rs | 3 ++- awkernel_async_lib/src/task.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/awkernel_async_lib/src/scheduler/edf.rs b/awkernel_async_lib/src/scheduler/edf.rs index 3bfb66185..b1b3ec771 100644 --- a/awkernel_async_lib/src/scheduler/edf.rs +++ b/awkernel_async_lib/src/scheduler/edf.rs @@ -225,7 +225,8 @@ impl EDFScheduler { // is not a lost wakeup: the task is eventually run either by the // victim's preemption handler (`do_preemption`), or re-woken by the // victim's `run_main` if its running task finishes first, in which - // case this function decides again. Future work: + // case this function decides again. `run_main` checks the pending + // heap again before the victim sleeps. Future work: // an enqueue-instead-of-pin strategy (push to the affinity queue // and treat the IPI as a hint) would close this window, but it // requires redesigning the duplicate-IPI suppression that the diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 346c0c890..eee6a3a3f 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1411,6 +1411,16 @@ pub fn run_main() { } } } else { + // A waker that read this CPU as busy can pin a task here after the + // re-wake at the top of the loop. While this CPU runs no task, the + // preemption IPI can leave that task pinned, so re-wake it instead + // of sleeping. `get_next_task` took `GLOBAL_WAKE_GET_MUTEX`, so this + // check sees the pin of every such waker. + if scheduler::PREEMPTION_ENABLED && scheduler::peek_preemption_pending(cpu_id).is_some() + { + continue; + } + #[cfg(feature = "perf")] perf::start_idle(); From 232eb5fac6a221e09c41ef22ffffde216ecc1b85 Mon Sep 17 00:00:00 2001 From: atsushi421 Date: Mon, 16 Nov 2026 11:02:32 +0900 Subject: [PATCH 4/4] fix: address review findings - Skip a CPU whose running task is missing from the task table, as GEDF did. Before this change, such a CPU stopped the whole decision. - In the comment and the mdbook, say that the woken task can preempt even if the set has an idle core, and give the costs. The preemption can be unneeded, the victim waits for the primary CPU to wake a sleeping core, and a victim whose set holds no idle core waits for its own core. - In the mdbook, say that a preempting task goes to the preemption-pending heap of the victim core and not to the queue. Signed-off-by: atsushi421 --- awkernel_async_lib/src/scheduler/edf.rs | 22 +++++++++++++--------- mdbook/src/internal/scheduler.md | 4 +++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/awkernel_async_lib/src/scheduler/edf.rs b/awkernel_async_lib/src/scheduler/edf.rs index b1b3ec771..6539edc35 100644 --- a/awkernel_async_lib/src/scheduler/edf.rs +++ b/awkernel_async_lib/src/scheduler/edf.rs @@ -183,20 +183,24 @@ impl EDFScheduler { // An idle CPU does not stop the preemption. A task queued before // or after this one can take the idle CPU first, and a sleeping // CPU runs a queued task only after the primary CPU wakes it. So - // this task preempts the lowest-priority busy CPU even if an idle - // CPU can take it. + // this task can preempt the lowest-priority busy CPU even if the + // set has an idle CPU. The cost is that the preemption can be + // unneeded, and the victim waits for the primary CPU to wake a + // sleeping CPU. If the set of the victim holds no idle CPU, the + // victim waits for its own CPU while the idle CPU stays idle. if task_running.task_id == 0 { continue; } - let task_running = get_task(task_running.task_id); - let task_pending = peek_preemption_pending(cpu_id); + // A running task that is missing from the task table finished + // after the read above, so this CPU counts as idle too. + let Some(task_running) = get_task(task_running.task_id) else { + continue; + }; - let target_task = match (task_running, task_pending) { - (Some(running), Some(pending)) => running.max(pending), - (Some(running), None) => running, - (None, Some(pending)) => pending, - (None, None) => return false, + let target_task = match peek_preemption_pending(cpu_id) { + Some(pending) => task_running.max(pending), + None => task_running, }; match victim { diff --git a/mdbook/src/internal/scheduler.md b/mdbook/src/internal/scheduler.md index 01aa3840f..9ce66e97d 100644 --- a/mdbook/src/internal/scheduler.md +++ b/mdbook/src/internal/scheduler.md @@ -75,7 +75,9 @@ The Earliest Deadline First (EDF) scheduler is implemented in [edf.rs](https://g The scheduler holds a single affinity-aware priority queue, [`AffinityBTreeQueue`](https://crates.io/crates/affinity_btree_queue), backed by an augmented B-tree. Each entry carries `(priority, affinity, task)`, where the priority is the pair `(absolute_deadline, wake_time)`; smaller values dequeue first, so tasks are ordered by earliest deadline with wake time as a tie-breaker. Every B-tree node stores the OR of the affinities in its subtree, which lets `pop_for_cpu(cpu)` find the earliest-deadline task runnable on `cpu` in logarithmic time while skipping subtrees with no eligible entry. -When a task is enqueued via `wake_task()`, the scheduler reads the `SchedulerType::EDF(relative_deadline, cpu_set)` attached to the task and calculates the absolute deadline as `uptime + relative_deadline`. If the task is part of a DAG, `calculate_and_update_dag_deadline()` is used instead to propagate deadlines through the DAG. The task priority (`PriorityInfo`) becomes `MAX_TASK_PRIORITY - absolute_deadline`, so an earlier deadline gives a higher priority. The task is then pushed into the queue with its `cpu_set` as the affinity mask. `invoke_preemption()` handles preemption. If the task is already running, the scheduler enqueues it without preemption. Otherwise, the scheduler chooses the core running the lowest-priority task among the busy cores of the set. If the new task has a higher priority than the task running (or pending preemption) on that core, the scheduler sends an IPI to it. Against another EDF task, a higher priority means an earlier deadline. An EDF task preempts a task of a lower-priority scheduler whatever its deadline. An idle core in the set does not stop the preemption. A task queued before or after the new task can take the idle core first. Also, a sleeping core runs a queued task only after the primary CPU wakes it. +When a task is enqueued via `wake_task()`, the scheduler reads the `SchedulerType::EDF(relative_deadline, cpu_set)` attached to the task and calculates the absolute deadline as `uptime + relative_deadline`. If the task is part of a DAG, `calculate_and_update_dag_deadline()` is used instead to propagate deadlines through the DAG. The task priority (`PriorityInfo`) becomes `MAX_TASK_PRIORITY - absolute_deadline`, so an earlier deadline gives a higher priority. `invoke_preemption()` handles preemption. If the task is already running, the scheduler does not preempt. Otherwise, the scheduler chooses the core running the lowest-priority task among the busy cores of the set. If the new task has a higher priority than the task running (or pending preemption) on that core, the scheduler sends an IPI to it. In that case, the scheduler pins the new task to the preemption-pending heap of that core and does not push it into the queue. If the scheduler sends no IPI, it pushes the task into the queue with its `cpu_set` as the affinity mask. One such case is a set with no busy core. Against another EDF task, a higher priority means an earlier deadline. An EDF task preempts a task of a lower-priority scheduler whatever its deadline. + +An idle core in the set does not stop the preemption. A task queued before or after the new task can take the idle core first. Also, a sleeping core runs a queued task only after the primary CPU wakes it. The cost is that the preemption can be unneeded, and the preempted task waits for the primary CPU to wake a sleeping core. If the set of the preempted task holds no idle core, the preempted task waits for its own core while the idle core stays idle. `get_next()` pops the earliest-deadline task whose `cpu_set` contains the calling CPU (`pop_for_cpu`), so a task is only ever dequeued by a core within its set. This guarantees CPU affinity. CPU 0 (the primary core) is always excluded from `cpu_set` when a task is spawned. The `cpu_set` is normalized at spawn time by removing CPU 0 and out-of-range bits; if this leaves the set empty (for example an empty set, or one naming only CPU 0 or out-of-range cores), the task is not rejected but falls back to all worker cores (`1..num_cpu()`) with a warning.