ax-task 0.7.1

OS-independent IRQ-safe SMP task scheduling core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! CPU-local scheduler allocation and online publication.

use super::*;

impl TaskSystem {
    /// Allocates one pinned CPU-local scheduler object without publishing it.
    pub fn create_cpu_local(
        &self,
        cpu: CpuId,
    ) -> Result<Pin<alloc::boxed::Box<CpuLocal>>, TaskError> {
        let remote = Arc::clone(&self.state.lock().cpu_registration(cpu)?.remote);
        Ok(CpuLocal::create(
            cpu,
            self.config,
            remote,
            Arc::clone(self.root_domain.rt_bandwidth()),
        ))
    }

    /// Returns the stable remote-publication endpoint of a placement-active CPU.
    pub fn cpu_remote(&self, cpu: CpuId) -> Option<&CpuRemote> {
        self.cpu_remotes
            .get(cpu.as_usize())
            .map(Arc::as_ref)
            .filter(|remote| remote.accepts_placement())
    }

    /// Returns the opaque runtime endpoint for a configured CPU.
    ///
    /// This bootstrap capability is available before online publication so a
    /// runtime can cache its current-CPU endpoint in architecture-owned
    /// storage. Ordinary scheduler producers must use [`Self::cpu_remote`],
    /// which rejects an offline CPU.
    #[doc(hidden)]
    pub fn runtime_cpu_remote_handle(&self, cpu: CpuId) -> CpuRemoteHandle {
        self.cpu_remotes
            .get(cpu.as_usize())
            .map_or(CpuRemoteHandle::NONE, |remote| {
                // SAFETY: TaskSystem retains this Arc allocation until the
                // system is destroyed. Runtime providers may publish the raw
                // handle only while they retain that TaskSystem lifetime.
                unsafe { CpuRemoteHandle::from_raw(Arc::as_ptr(remote).expose_provenance()) }
            })
    }

    /// Returns cumulative non-idle runtime charged by one online CPU.
    pub fn cpu_busy_runtime_ns(&self, cpu: CpuId) -> Result<u64, TaskError> {
        let remote = self
            .cpu_remotes
            .get(cpu.as_usize())
            .ok_or(TaskError::InvalidCpu(cpu.as_u32()))?;
        if !remote.is_online() {
            return Err(TaskError::CpuOffline(cpu.as_u32()));
        }
        Ok(remote.busy_runtime_ns())
    }

    pub(super) fn ensure_owner_cpu_online(&self, cpu: &CpuLocal) -> Result<(), TaskError> {
        self.ensure_owner_cpu_context(cpu)?;
        self.ensure_owner_cpu_registration_online(cpu)
    }

    /// Verifies the published owner/remote identity after the caller has
    /// established its CPU ownership context.
    pub(super) fn ensure_owner_cpu_registration_online(
        &self,
        cpu: &CpuLocal,
    ) -> Result<(), TaskError> {
        let remote = self
            .cpu_remotes
            .get(cpu.owner().as_usize())
            .ok_or(TaskError::InvalidCpu(cpu.owner().as_u32()))?;
        if Arc::ptr_eq(remote, cpu.remote()) && remote.is_online() {
            Ok(())
        } else {
            Err(TaskError::CpuOffline(cpu.owner().as_u32()))
        }
    }

    /// Enforces the post-publication owner-CPU access contract.
    ///
    /// Standalone scheduler models deliberately operate on an unpublished
    /// `TaskSystem` and retain their direct pinned CpuLocal allocation. Once a
    /// runtime publishes this exact system handle, every online owner access
    /// must instead retain either its IRQ pin or scheduler baton. This mirrors
    /// Linux's rq-lock assertion and closes interrupt-return re-entry over a
    /// live mutable runqueue borrow.
    pub(super) fn ensure_owner_cpu_context(&self, cpu: &CpuLocal) -> Result<(), TaskError> {
        if !cpu.is_online() {
            return Ok(());
        }
        // SAFETY: reading the opaque handle neither dereferences it nor extends
        // its lifetime. Equality only determines whether this model instance
        // has crossed the runtime publication boundary.
        let published = unsafe { task_runtime::task_system_handle() }.into_raw();
        let this = (self as *const Self).expose_provenance();
        if published == 0 || published != this {
            return Ok(());
        }
        match task_runtime::validate_owner_cpu_context() {
            RuntimeStatus::Success => Ok(()),
            RuntimeStatus::UnsafeContext => Err(TaskError::UnsafeContext),
            status => Err(TaskError::RuntimeFailure(status as u32)),
        }
    }

    /// Completes CPU registration and publishes it in the online root domain.
    pub fn bring_cpu_online(&self, mut cpu: Pin<&mut CpuLocal>) -> Result<(), TaskError> {
        let _irq = IrqScope::enter();
        self.ensure_owner_cpu_context(&cpu)?;
        let id = cpu.owner();
        let state = self.state.lock();
        let mut root_domain = self.root_domain.lock();
        let registration = state.cpu_registration(id)?;
        if registration.remote.lifecycle_state() != crate::runtime::cpu::CpuLifecycleState::Offline
        {
            return Err(TaskError::CpuAlreadyOnline(id.as_u32()));
        }
        if !Arc::ptr_eq(&registration.remote, cpu.remote()) {
            return Err(TaskError::InvalidRuntimeHandle);
        }
        if root_domain.online.contains(id) {
            return Err(TaskError::InvalidConfiguration);
        }
        if state
            .slots
            .iter()
            .filter_map(|slot| slot.record.as_ref())
            .any(|record| {
                let sched = record.sched.lock();
                (matches!(sched.policy.base, SchedulePolicy::Deadline(_))
                    || matches!(sched.policy.requested_policy(), SchedulePolicy::Deadline(_)))
                    && !sched.affinity.affinity.contains(id)
            })
        {
            return Err(TaskError::DeadlineAffinity);
        }
        ensure_runtime_success(task_runtime::prepare_cpu_online(RuntimeCpuId::new(
            id.as_u32(),
        )))?;
        let monotonic_now = task_runtime::monotonic_now();
        cpu.as_mut()
            .reset_fair_balance(monotonic_now, self.config.balance_interval_ns());
        let online_count = root_domain
            .online
            .count()
            .checked_add(1)
            .ok_or(TaskError::InvalidConfiguration)?;
        let deadline_rebuild = state.deadline_bandwidth_rebuild(online_count)?;
        self.root_domain.enable_rt_runtime(id);
        assert!(
            root_domain.insert_online(id, deadline_rebuild),
            "validated offline CPU must be absent from the root domain"
        );
        assert!(
            cpu.as_ref().get_ref().remote().mark_online(),
            "validated offline CPU must accept final publication"
        );
        OwnerRqTxn::begin(self, cpu.remote()).commit();
        if cpu
            .lock_run_queue(RunQueueGuardSource::Lifecycle)
            .has_runnable_rt()
        {
            self.root_domain.activate_rt_period(id, || monotonic_now);
        }
        Ok(())
    }

    /// Removes a quiescent owner CPU from placement and remote publication.
    ///
    /// The caller must first migrate or retire every non-idle thread, cancel
    /// local task deadlines, and consume the CPU's scheduler IPI. The packed
    /// remote lifecycle closes publication only when its active publisher count
    /// is zero, so a successful transition cannot strand an inbox node between
    /// queue insertion and its doorbell.
    pub fn take_cpu_offline(&self, mut cpu: Pin<&mut CpuLocal>) -> Result<(), TaskError> {
        self.ensure_owner_cpu_context(&cpu)?;
        let _irq = IrqScope::enter();
        let id = cpu.owner();
        let state = self.state.lock();
        let mut root_domain = self.root_domain.lock();
        let remote = Arc::clone(&state.cpu_registration(id)?.remote);
        if !Arc::ptr_eq(&remote, cpu.remote()) {
            return Err(TaskError::InvalidRuntimeHandle);
        }
        match remote.lifecycle_state() {
            crate::runtime::cpu::CpuLifecycleState::Offline => {
                return Err(TaskError::CpuOffline(id.as_u32()));
            }
            crate::runtime::cpu::CpuLifecycleState::Inactive
            | crate::runtime::cpu::CpuLifecycleState::Draining => {
                return Err(TaskError::CpuNotQuiescent(id.as_u32()));
            }
            crate::runtime::cpu::CpuLifecycleState::Online => {}
        }
        if root_domain.online.count() <= 1 {
            return Err(TaskError::LastOnlineCpu(id.as_u32()));
        }
        if !root_domain.can_deactivate_cpu(id) {
            return Err(TaskError::DeadlineAdmission);
        }
        let remaining_online = root_domain
            .online
            .count()
            .checked_sub(1)
            .ok_or(TaskError::InvalidConfiguration)?;
        let deadline_rebuild = state.deadline_bandwidth_rebuild(remaining_online)?;
        let rt_period_replacement = (0..root_domain.online.topology_len())
            .map(|index| CpuId::new(index as u32))
            .find(|candidate| *candidate != id && root_domain.online.contains(*candidate))
            .ok_or(TaskError::LastOnlineCpu(id.as_u32()))?;

        self.migrate_dormant_deadline_bandwidth_for_cpu_offline(&state, &root_domain, id)?;

        if !remote.try_deactivate() {
            Err(TaskError::CpuNotQuiescent(id.as_u32()))
        } else if !Self::prepare_thread_targets_for_cpu_offline(&state, &root_domain, id)
            || !remote.try_begin_draining()
        {
            remote.cancel_deactivation();
            Err(TaskError::CpuNotQuiescent(id.as_u32()))
        } else if !cpu.is_quiescent_for_offline()
            || !Self::threads_allow_cpu_offline(&state, &root_domain, id)
        {
            remote.cancel_draining();
            Err(TaskError::CpuNotQuiescent(id.as_u32()))
        } else if let Err(error) = ensure_runtime_success(task_runtime::prepare_cpu_offline(
            RuntimeCpuId::new(id.as_u32()),
        )) {
            remote.cancel_draining();
            Err(error)
        } else if !root_domain.remove_online(id, deadline_rebuild) {
            remote.cancel_draining();
            Err(TaskError::InvalidConfiguration)
        } else {
            cpu.as_mut().clear_fair_balance();
            self.root_domain.disable_rt_runtime(id);
            remote.finish_offline();
            remote
                .lock_run_queue(RunQueueGuardSource::Lifecycle)
                .invalidate_domain_publication();
            self.root_domain.publish_offline(id);
            if self
                .root_domain
                .rt_bandwidth()
                .migrate_owner(id, rt_period_replacement)
            {
                self.cpu_remotes[rt_period_replacement.as_usize()].kick_scheduler_work();
            }
            Ok(())
        }
    }

    /// Mirrors Linux `dl_task_offline_migration()` for blocked DL tasks.
    ///
    /// Runnable/on-CPU tasks must already leave through the normal placement
    /// carrier. A dormant reservation, however, still owns `this_bw`, optional
    /// `running_bw`, and inactive/CBS timer entries. Those facts move together
    /// before the source CPU closes placement publication.
    fn migrate_dormant_deadline_bandwidth_for_cpu_offline(
        &self,
        state: &TaskSystemState,
        root_domain: &RootDomainState,
        source: CpuId,
    ) -> Result<(), TaskError> {
        let source_remote = &self.cpu_remotes[source.as_usize()];
        for record in state.slots.iter().filter_map(|slot| slot.record.as_ref()) {
            let core = &record.core;
            let mut sched = record.sched.lock();
            if sched.deadline.bandwidth.reservation_owner() != Some(source) {
                continue;
            }
            if sched.placement.queued_cpu().is_some()
                || sched.placement.on_cpu().is_some()
                || sched.placement.has_pending_migration()
            {
                return Err(TaskError::CpuNotQuiescent(source.as_u32()));
            }
            let target = (0..root_domain.online.topology_len())
                .map(|index| CpuId::new(index as u32))
                .find(|candidate| {
                    *candidate != source
                        && root_domain.online.contains(*candidate)
                        && sched.affinity.affinity.contains(*candidate)
                        && self.cpu_remotes[candidate.as_usize()].accepts_placement()
                })
                .ok_or(TaskError::DeadlineAffinity)?;
            let target_remote = &self.cpu_remotes[target.as_usize()];
            let publication = target_remote
                .begin_publication()
                .ok_or(TaskError::CpuNotQuiescent(target.as_u32()))?;

            let mut source_rq = OwnerRqTxn::begin(self, source_remote);
            Self::detach_owner_deadline_bandwidth_in_rq(
                core,
                &mut sched,
                source_remote,
                &mut source_rq,
            );
            source_rq.commit();

            let active = sched.deadline.bandwidth.is_active();
            let mut target_rq = OwnerRqTxn::begin(self, target_remote);
            Self::attach_deadline_bandwidth_locked(
                core,
                &mut sched,
                &mut target_rq,
                target,
                active,
            );
            target_rq.commit();
            core.set_wake_cpu_hint(target);
            drop(sched);
            drop(publication);
            self.publish_owner_deadline_refresh(core, target);
        }
        Ok(())
    }

    /// Stops dormant threads from retaining an inactive preferred target.
    ///
    /// Placement is closed before this pass. A producer that sampled the old
    /// target either finishes its runqueue publication before final draining,
    /// making the CPU non-quiescent, or revalidates and selects an online CPU.
    /// This is the active-before-online split used by Linux CPU hotplug around
    /// `task_cpu()` placement.
    fn prepare_thread_targets_for_cpu_offline(
        state: &TaskSystemState,
        root_domain: &RootDomainState,
        cpu: CpuId,
    ) -> bool {
        let is_idle = |id| {
            state
                .cpus
                .iter()
                .any(|registration| registration.remote.idle_thread() == Some(id))
        };
        let fallback_for = |affinity: &CpuSet| {
            state
                .cpus
                .iter()
                .enumerate()
                .map(|(index, registration)| (CpuId::new(index as u32), registration))
                .find(|(candidate, registration)| {
                    *candidate != cpu
                        && root_domain.online.contains(*candidate)
                        && registration.remote.accepts_placement()
                        && affinity.contains(*candidate)
                })
                .map(|(candidate, _)| candidate)
        };

        for record in state.slots.iter().filter_map(|slot| slot.record.as_ref()) {
            if is_idle(record.core.id()) {
                continue;
            }

            let sched = record.sched.lock();
            if sched.lifecycle.state() == ThreadState::Exited {
                continue;
            }
            if Self::is_parked_ktimer_worker(state, cpu, &record.core, &sched) {
                continue;
            }
            if fallback_for(&sched.affinity.affinity).is_none() {
                return false;
            }
            let physically_owned = sched.placement.queued_cpu() == Some(cpu)
                || sched.placement.on_cpu() == Some(cpu)
                || sched.placement.committed_migration_target() == Some(cpu)
                || sched.deadline.bandwidth.reservation_owner() == Some(cpu)
                || record.core.sleep_timer_cpu() == Some(cpu);
            if physically_owned {
                return false;
            }
            let has_other_placement = sched.placement.queued_cpu().is_some()
                || sched.placement.on_cpu().is_some()
                || sched.placement.has_pending_migration()
                || sched.deadline.bandwidth.reservation_owner().is_some()
                || record.core.sleep_timer_cpu().is_some();
            if record.core.wake_cpu_hint() == Some(cpu) && has_other_placement {
                return false;
            }
        }

        for record in state.slots.iter().filter_map(|slot| slot.record.as_ref()) {
            if is_idle(record.core.id()) {
                continue;
            }
            let sched = record.sched.lock();
            if Self::is_parked_ktimer_worker(state, cpu, &record.core, &sched) {
                continue;
            }
            drop(sched);
            if record.core.wake_cpu_hint() != Some(cpu) {
                continue;
            }
            let sched = record.sched.lock();
            if sched.lifecycle.state() == ThreadState::Exited {
                continue;
            }
            let Some(fallback) = fallback_for(&sched.affinity.affinity) else {
                return false;
            };
            // Linux deliberately leaves task_cpu() unchanged for blocked
            // tasks; the next wakeup selects from the then-current active mask.
            record.core.set_wake_cpu_hint(fallback);
        }
        true
    }

    fn threads_allow_cpu_offline(
        state: &TaskSystemState,
        root_domain: &RootDomainState,
        cpu: CpuId,
    ) -> bool {
        state
            .slots
            .iter()
            .filter_map(|slot| slot.record.as_ref())
            .all(|record| {
                let id = record.core.id();
                let is_idle = state
                    .cpus
                    .iter()
                    .any(|registration| registration.remote.idle_thread() == Some(id));
                if is_idle {
                    return true;
                }

                let sched = record.sched.lock();
                if sched.lifecycle.state() == ThreadState::Exited {
                    return true;
                }
                if Self::is_parked_ktimer_worker(state, cpu, &record.core, &sched) {
                    return true;
                }
                let has_remaining_destination = (0..state.cpus.len()).any(|index| {
                    let candidate = CpuId::new(index as u32);
                    candidate != cpu
                        && root_domain.online.contains(candidate)
                        && sched.affinity.affinity.contains(candidate)
                });
                let owned_by_cpu = sched.placement.queued_cpu() == Some(cpu)
                    || sched.placement.on_cpu() == Some(cpu)
                    || sched.placement.committed_migration_target() == Some(cpu)
                    || sched.deadline.bandwidth.reservation_owner() == Some(cpu)
                    || record.core.sleep_timer_cpu() == Some(cpu)
                    || record.core.wake_cpu_hint() == Some(cpu);
                has_remaining_destination && !owned_by_cpu
            })
    }

    /// Linux keeps each `ktimers/%u` task allocated while its CPU is offline.
    /// The fixed task is hotplug-quiescent only after it has parked on its IRQ
    /// event and relinquished every rq, timer, and Deadline ownership record.
    fn is_parked_ktimer_worker(
        state: &TaskSystemState,
        cpu: CpuId,
        core: &ThreadCore,
        sched: &ThreadSchedState,
    ) -> bool {
        state.cpus.get(cpu.as_usize()).is_some_and(|registration| {
            registration.remote.ktimer_worker() == Some(core.id())
                && sched.lifecycle.state() == ThreadState::Blocked
                && sched.placement.queued_cpu().is_none()
                && sched.placement.on_cpu().is_none()
                && !sched.placement.has_pending_migration()
                && sched.deadline.bandwidth.reservation_owner().is_none()
                && core.sleep_timer_cpu().is_none()
        })
    }

    /// Installs an idle thread for a CPU; idle is selected only when queues empty.
    pub fn install_idle_thread(
        &self,
        mut cpu: Pin<&mut CpuLocal>,
        thread: ThreadId,
    ) -> Result<(), TaskError> {
        self.ensure_owner_cpu_context(&cpu)?;
        let core = {
            let state = self.state.lock();
            state.cpu_registration(cpu.owner())?;
            Arc::clone(&state.thread_record(thread)?.core)
        };
        self.install_idle_core(cpu.as_mut(), core)
    }
}