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
//! Linux-style `task_cpu`, `on_rq`, and `on_cpu` publication.

use alloc::sync::Arc;
use core::sync::atomic::{AtomicU64, Ordering};

use crate::{
    runtime::task_runtime,
    sched::{CpuId, CpuSet},
};

const ON_RQ_BITS: u32 = 2;
const CPU_BITS: u32 = 30;
const ON_RQ_MASK: u64 = (1 << ON_RQ_BITS) - 1;
const CPU_MASK: u64 = (1 << CPU_BITS) - 1;
const TASK_CPU_SHIFT: u32 = ON_RQ_BITS;

const ON_RQ_NONE: u64 = 0;
const ON_RQ_QUEUED: u64 = 1;
const ON_RQ_MIGRATING: u64 = 2;

/// Linux-compatible runqueue ownership state.
///
/// `Queued` is `TASK_ON_RQ_QUEUED`; it remains set while RT/Deadline current
/// stays linked in its class structure. `Migrating` is the only rq-to-rq
/// carrier state. Switch-out and exit are deliberately absent: those belong
/// exclusively to the owner CPU's move-only switch handoff.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum TaskOnRunQueue {
    None,
    Queued,
    Migrating,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct PlacementSnapshot {
    task_cpu: Option<CpuId>,
    on_rq: TaskOnRunQueue,
}

/// Publication of Linux's orthogonal task placement facts.
///
/// The owning rq transaction changes the packed `task_cpu`/`on_rq` pair.
/// `on_cpu` is a separate execution claim: selection publishes it with one
/// store and switch tail clears it with a Release store, matching Linux's
/// `prepare_task()`/`finish_task()` contract. Readers that need a compound
/// placement decision use the task scheduler lock and the owning rq lock;
/// `on_cpu` is not a transaction version for those other facts.
#[derive(Debug)]
pub(in crate::sched::system) struct SchedulerPlacement {
    state: AtomicU64,
    on_cpu: AtomicU64,
    requested_cpu: AtomicU64,
}

impl SchedulerPlacement {
    /// Initializes Linux's `task_cpu()` before the task is published.
    ///
    /// This mirrors `sched_cgroup_fork()`/`__set_task_cpu()`: a new task is
    /// not runnable yet, but PI and policy transactions already have one rq
    /// whose lock serializes its scheduler state.
    pub(super) const fn new(task_cpu: CpuId) -> Self {
        Self {
            state: AtomicU64::new(encode(PlacementSnapshot {
                task_cpu: Some(task_cpu),
                on_rq: TaskOnRunQueue::None,
            })),
            on_cpu: AtomicU64::new(0),
            requested_cpu: AtomicU64::new(0),
        }
    }

    fn snapshot(&self) -> PlacementSnapshot {
        decode(self.state.load(Ordering::Acquire))
    }

    pub(in crate::sched::system) fn queued_cpu(&self) -> Option<CpuId> {
        let state = self.snapshot();
        (state.on_rq == TaskOnRunQueue::Queued)
            .then_some(state.task_cpu)
            .flatten()
    }

    pub(in crate::sched::system) fn on_cpu(&self) -> Option<CpuId> {
        decode_cpu(self.on_cpu.load(Ordering::Acquire))
    }

    /// Waits until switch tail releases Linux's `p->on_cpu` execution claim.
    ///
    /// The waker holds the task scheduler lock while waiting, matching
    /// `try_to_wake_up()` under `p->pi_lock`. The acquire load pairs with
    /// `finish_task()` so runnable activation and enqueue cannot overtake the
    /// previous stack's final scheduler publications.
    pub(in crate::sched::system) fn wait_until_not_on_cpu(&self) {
        // Match Linux's `smp_cond_load_acquire()`: the first and final
        // observations are Acquire, while a prolonged wait only polls the
        // zero/non-zero publication with Relaxed loads. `finish_task()`'s
        // Release store remains the synchronization point before activation.
        // sync-lint: ignore suspicious_relaxed_wait_condition
        // sync-lint: ignore suspicious_relaxed_mixed_ordering
        while self.on_cpu.load(Ordering::Acquire) != 0 {
            while self.on_cpu.load(Ordering::Relaxed) != 0 {
                core::hint::spin_loop();
            }
            if self.on_cpu.load(Ordering::Acquire) == 0 {
                return;
            }
        }
    }

    pub(in crate::sched::system) fn committed_migration_target(&self) -> Option<CpuId> {
        let state = self.snapshot();
        (state.on_rq == TaskOnRunQueue::Migrating)
            .then_some(state.task_cpu)
            .flatten()
    }

    pub(in crate::sched::system) fn has_pending_migration(&self) -> bool {
        self.committed_migration_target().is_some() || self.requested_migration().is_some()
    }

    /// Linux `task_cpu()`: the last committed rq assignment.
    pub(in crate::sched::system) fn assigned_cpu(&self) -> Option<CpuId> {
        self.snapshot().task_cpu
    }

    /// Returns the CPU that may mutate this task's physical rq/on_cpu state.
    ///
    /// A switching migration is still controlled by the source CPU until
    /// switch tail releases `on_cpu`; otherwise the rq named by `task_cpu`
    /// owns queued or migrating state. A sleeping task has neither physical
    /// owner even though Linux retains its last `task_cpu` as a wake hint.
    pub(in crate::sched::system) fn control_owner(&self) -> Option<CpuId> {
        if let Some(owner) = self.on_cpu() {
            return Some(owner);
        }
        let state = self.snapshot();
        match state.on_rq {
            TaskOnRunQueue::Queued | TaskOnRunQueue::Migrating => state.task_cpu,
            TaskOnRunQueue::None => None,
        }
    }

    /// Linux `activate_task()`.
    pub(in crate::sched::system) fn activate(&self, cpu: CpuId) {
        placement_invariant(self.on_cpu().is_none(), 0x504c_0001, cpu.as_u32() as usize);
        self.transition(0x504c_0001, cpu.as_u32() as usize, |state| {
            let valid = match state.on_rq {
                TaskOnRunQueue::None => true,
                TaskOnRunQueue::Migrating => state.task_cpu == Some(cpu),
                TaskOnRunQueue::Queued => false,
            };
            valid.then_some(PlacementSnapshot {
                task_cpu: Some(cpu),
                on_rq: TaskOnRunQueue::Queued,
            })
        });
        self.clear_requested_cpu(cpu);
    }

    /// Linux `init_idle()`: pins the per-CPU idle task to its rq without
    /// linking it into any scheduling-class queue or incrementing
    /// `rq->nr_running`.
    pub(in crate::sched::system) fn install_idle(&self, cpu: CpuId) {
        placement_invariant(self.on_cpu().is_none(), 0x504c_000d, cpu.as_u32() as usize);
        self.transition(0x504c_000d, cpu.as_u32() as usize, |state| {
            (state.task_cpu == Some(cpu) && state.on_rq == TaskOnRunQueue::None).then_some(
                PlacementSnapshot {
                    task_cpu: Some(cpu),
                    on_rq: TaskOnRunQueue::Queued,
                },
            )
        });
        self.clear_requested_cpu(cpu);
    }

    /// Removes a non-running task from its rq.
    pub(in crate::sched::system) fn deactivate(&self, cpu: CpuId) {
        placement_invariant(self.on_cpu().is_none(), 0x504c_0002, cpu.as_u32() as usize);
        self.transition(0x504c_0002, cpu.as_u32() as usize, |state| {
            (state.on_rq == TaskOnRunQueue::Queued && state.task_cpu == Some(cpu)).then_some(
                PlacementSnapshot {
                    task_cpu: Some(cpu),
                    on_rq: TaskOnRunQueue::None,
                },
            )
        });
        self.requested_cpu.store(0, Ordering::Release);
    }

    /// Reserves the immutable destination of an off-rq wake publication.
    pub(in crate::sched::system) fn begin_remote_wakeup(&self, target: CpuId) {
        placement_invariant(
            self.on_cpu().is_none(),
            0x504c_0003,
            target.as_u32() as usize,
        );
        self.transition(0x504c_0003, target.as_u32() as usize, |state| {
            (state.on_rq == TaskOnRunQueue::None).then_some(PlacementSnapshot {
                task_cpu: Some(target),
                on_rq: TaskOnRunQueue::Migrating,
            })
        });
        self.requested_cpu.store(0, Ordering::Release);
    }

    /// Records an affinity request without retargeting a committed carrier.
    pub(in crate::sched::system) fn request_migration(&self, target: Option<CpuId>) {
        let state = self.snapshot();
        let requested = match state.on_rq {
            TaskOnRunQueue::Queued | TaskOnRunQueue::Migrating => {
                target.filter(|target| Some(*target) != state.task_cpu)
            }
            TaskOnRunQueue::None => {
                placement_invariant(
                    self.on_cpu().is_none(),
                    0x504c_0004,
                    target.map_or(usize::MAX, |cpu| cpu.as_u32() as usize),
                );
                None
            }
        };
        self.requested_cpu
            .store(encode_cpu(requested), Ordering::Release);
    }

    pub(in crate::sched::system) fn requested_migration(&self) -> Option<CpuId> {
        decode_cpu(self.requested_cpu.load(Ordering::Acquire))
    }

    /// Linux `put_prev_task()`: rq and execution ownership remain intact.
    pub(in crate::sched::system) fn put_prev(&self, cpu: CpuId) {
        let state = self.snapshot();
        placement_invariant(
            state.on_rq == TaskOnRunQueue::Queued
                && state.task_cpu == Some(cpu)
                && self.on_cpu() == Some(cpu)
                && self.requested_migration().is_none(),
            0x504c_0005,
            cpu.as_u32() as usize,
        );
    }

    /// Commits `TASK_ON_RQ_MIGRATING` and the destination `task_cpu()`.
    pub(in crate::sched::system) fn begin_migration(&self, source: CpuId, target: CpuId) {
        placement_invariant(
            self.on_cpu().is_none_or(|owner| owner == source),
            0x504c_0006,
            source.as_u32() as usize,
        );
        self.transition(0x504c_0006, source.as_u32() as usize, |state| {
            (source != target
                && state.on_rq == TaskOnRunQueue::Queued
                && state.task_cpu == Some(source))
            .then_some(PlacementSnapshot {
                task_cpu: Some(target),
                on_rq: TaskOnRunQueue::Migrating,
            })
        });
        self.requested_cpu.store(0, Ordering::Release);
    }

    /// Removes current from rq while switch tail retains `on_cpu`.
    pub(in crate::sched::system) fn block_current(&self, cpu: CpuId) {
        placement_invariant(
            self.on_cpu() == Some(cpu),
            0x504c_0007,
            cpu.as_u32() as usize,
        );
        self.transition(0x504c_0007, cpu.as_u32() as usize, |state| {
            (state.on_rq == TaskOnRunQueue::Queued && state.task_cpu == Some(cpu)).then_some(
                PlacementSnapshot {
                    task_cpu: Some(cpu),
                    on_rq: TaskOnRunQueue::None,
                },
            )
        });
        self.requested_cpu.store(0, Ordering::Release);
    }

    /// Retains `TASK_ON_RQ_QUEUED` for a Fair task delayed on sleep.
    ///
    /// Linux leaves both `on_rq` and `on_cpu` set until the architecture
    /// switch tail, while `sched_delayed` makes the task non-runnable. The rq
    /// node owns that extra state; this method validates the packed placement
    /// tuple without manufacturing another carrier state.
    pub(in crate::sched::system) fn delay_dequeue_current(&self, cpu: CpuId) {
        let state = self.snapshot();
        placement_invariant(
            state.on_rq == TaskOnRunQueue::Queued
                && state.task_cpu == Some(cpu)
                && self.on_cpu() == Some(cpu),
            0x504c_000e,
            cpu.as_u32() as usize,
        );
        self.requested_cpu.store(0, Ordering::Release);
    }

    /// Completes Linux `__block_task()` for a delayed Fair entity.
    ///
    /// The entity can still carry the outgoing `on_cpu` claim until switch
    /// tail. Only rq membership is cleared here.
    pub(in crate::sched::system) fn finish_delayed_dequeue(&self, cpu: CpuId) {
        placement_invariant(
            self.on_cpu().is_none_or(|owner| owner == cpu),
            0x504c_000f,
            cpu.as_u32() as usize,
        );
        self.transition(0x504c_000f, cpu.as_u32() as usize, |state| {
            (state.on_rq == TaskOnRunQueue::Queued && state.task_cpu == Some(cpu)).then_some(
                PlacementSnapshot {
                    on_rq: TaskOnRunQueue::None,
                    ..state
                },
            )
        });
        self.requested_cpu.store(0, Ordering::Release);
    }

    /// Linux `set_next_task()`.
    pub(in crate::sched::system) fn set_next_task(&self, cpu: CpuId) {
        self.publish_on_cpu(cpu);
    }

    /// Linux idle-class `set_next_task_idle()`: idle remains logically on its
    /// rq but is never represented in a scheduling-class queue.
    pub(in crate::sched::system) fn set_next_idle(&self, cpu: CpuId) {
        self.publish_on_cpu(cpu);
    }

    /// Linux idle-class `put_prev_task_idle()` retains logical rq membership
    /// and the physical `on_cpu` claim until switch tail.
    pub(in crate::sched::system) fn put_prev_idle(&self, cpu: CpuId) {
        let state = self.snapshot();
        placement_invariant(
            state.on_rq == TaskOnRunQueue::Queued
                && state.task_cpu == Some(cpu)
                && self.on_cpu() == Some(cpu),
            0x504c_000c,
            cpu.as_u32() as usize,
        );
    }

    /// Linux `finish_task()`: the switch tail release of `on_cpu`.
    pub(in crate::sched::system) fn finish_task(&self, cpu: CpuId) {
        placement_invariant(
            self.on_cpu() == Some(cpu),
            0x504c_0009,
            cpu.as_u32() as usize,
        );
        self.on_cpu.store(0, Ordering::Release);
    }

    /// Cancels only an unconsumed off-rq carrier during task exit.
    pub(in crate::sched::system) fn cancel_remote_handoff_for_exit(&self) {
        let state = self.snapshot();
        placement_invariant(
            self.on_cpu().is_none() && state.on_rq != TaskOnRunQueue::Queued,
            0x504c_000a,
            state
                .task_cpu
                .map_or(usize::MAX, |cpu| cpu.as_u32() as usize),
        );
        self.transition(0x504c_000a, 0, |current| {
            (current == state).then_some(PlacementSnapshot {
                on_rq: TaskOnRunQueue::None,
                ..current
            })
        });
        self.requested_cpu.store(0, Ordering::Release);
    }

    fn publish_on_cpu(&self, cpu: CpuId) {
        #[cfg(debug_assertions)]
        {
            let state = self.snapshot();
            debug_assert!(
                state.on_rq == TaskOnRunQueue::Queued
                    && state.task_cpu == Some(cpu)
                    && self.on_cpu().is_none_or(|owner| owner == cpu),
                "set_next_task requires an owner-rq selected task"
            );
        }
        self.on_cpu.store(encode_cpu(Some(cpu)), Ordering::Release);
    }

    fn clear_requested_cpu(&self, committed: CpuId) {
        let encoded = encode_cpu(Some(committed));
        let _ =
            self.requested_cpu
                .compare_exchange(encoded, 0, Ordering::AcqRel, Ordering::Acquire);
    }

    fn transition(
        &self,
        code: u32,
        detail: usize,
        mut operation: impl FnMut(PlacementSnapshot) -> Option<PlacementSnapshot>,
    ) {
        let mut encoded = self.state.load(Ordering::Acquire);
        loop {
            let current = decode(encoded);
            let Some(next) = operation(current) else {
                task_runtime::fatal_invariant(code, detail);
            };
            let next = encode(next);
            if next == encoded {
                return;
            }
            match self.state.compare_exchange_weak(
                encoded,
                next,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => return,
                Err(actual) => encoded = actual,
            }
        }
    }
}

/// Affinity policy remains under the task control lock.
#[derive(Debug)]
pub(in crate::sched::system) struct ThreadAffinityState {
    pub(in crate::sched::system) affinity: Arc<CpuSet>,
    pub(in crate::sched::system) affinity_generation: u64,
}

impl ThreadAffinityState {
    pub(super) fn new(affinity: CpuSet) -> Self {
        Self {
            affinity: Arc::new(affinity),
            affinity_generation: 1,
        }
    }
}

const fn encode_cpu(cpu: Option<CpuId>) -> u64 {
    match cpu {
        Some(cpu) => cpu.as_u32() as u64 + 1,
        None => 0,
    }
}

const fn decode_cpu(encoded: u64) -> Option<CpuId> {
    if encoded == 0 {
        None
    } else {
        Some(CpuId::new((encoded - 1) as u32))
    }
}

const fn encode(state: PlacementSnapshot) -> u64 {
    let on_rq = match state.on_rq {
        TaskOnRunQueue::None => ON_RQ_NONE,
        TaskOnRunQueue::Queued => ON_RQ_QUEUED,
        TaskOnRunQueue::Migrating => ON_RQ_MIGRATING,
    };
    on_rq | ((encode_cpu(state.task_cpu) & CPU_MASK) << TASK_CPU_SHIFT)
}

fn decode(encoded: u64) -> PlacementSnapshot {
    let on_rq = match encoded & ON_RQ_MASK {
        ON_RQ_NONE => TaskOnRunQueue::None,
        ON_RQ_QUEUED => TaskOnRunQueue::Queued,
        ON_RQ_MIGRATING => TaskOnRunQueue::Migrating,
        _ => task_runtime::fatal_invariant(0x504c_00fe, encoded as usize),
    };
    PlacementSnapshot {
        task_cpu: decode_cpu((encoded >> TASK_CPU_SHIFT) & CPU_MASK),
        on_rq,
    }
}

fn placement_invariant(valid: bool, code: u32, detail: usize) {
    if !valid {
        task_runtime::fatal_invariant(code, detail);
    }
}