frame-alloc 0.1.0

A no_std, dependency-free, const-constructible physical frame allocator for kernels
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! White-box, depot allocator specific tests.
//!
//! Allocator-agnostic API conformance (alloc/dealloc round-trips, holes, stats
//! invariants, concurrency) and the randomised occupancy oracle live in the
//! external black-box suite under `tests/`.

extern crate std;

use crate::tests::common::{N1, OwnedRegion, TestProvenance};
use crate::{
    CpuId, DepotAllocator, InitError, NoInterruptControl, PageSize, PhysicalAllocator, RegionInit,
    SummaryBuddyAllocator,
};
use alloc::vec::Vec;
use core::cell::Cell;
use core::num::NonZeroUsize;
use core::sync::atomic::AtomicUsize;
use core::sync::atomic::Ordering::Relaxed;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};

const BASE: PageSize = PageSize::from_log2(6);
const ORDERS: usize = 5;
const SLOTS: usize = 2;
const CAP: usize = 8;
const DEPOT_CAP: usize = 16;

// A CPU selector whose "current CPU" is settable.
std::thread_local! {
    static CPU: Cell<usize> = const { Cell::new(0) };
}

fn set_cpu(v: usize) {
    CPU.set(v);
}

struct TestCpuId;
impl CpuId for TestCpuId {
    fn current_cpu() -> usize {
        CPU.with(Cell::get)
    }
}

type Dep = DepotAllocator<
    SummaryBuddyAllocator<ORDERS, TestProvenance>,
    TestCpuId,
    SLOTS,
    CAP,
    DEPOT_CAP,
    NoInterruptControl,
>;

fn pool(total_frames: usize) -> (Dep, OwnedRegion) {
    let total_bytes = total_frames * BASE.bytes();
    let max_block = BASE.bytes() << (ORDERS - 1);
    let region = OwnedRegion::new(total_bytes, max_block);
    let alloc: Dep = DepotAllocator::new(
        BASE,
        SummaryBuddyAllocator::<ORDERS, TestProvenance>::new(BASE),
    );
    unsafe {
        alloc.init_region(region.addr(), total_bytes);
    }
    (alloc, region)
}

/// Fill the depot to capacity by allocating `CAP + DEPOT_CAP` base frames on CPU 0
/// and freeing them all back: the magazine keeps `CAP`, the rest overflow into the
/// shared depot. Returns once `depot_len() > 0`.
fn fill_depot<A: PhysicalAllocator>(a: &A) {
    let held: Vec<usize> = (0..(CAP + DEPOT_CAP))
        .map(|_| a.allocate_physical(BASE, N1).unwrap())
        .collect();
    for &p in &held {
        unsafe { a.deallocate_physical(BASE, N1, p) };
    }
}

fn find_aligned_run(frames: &[usize], count: usize) -> Vec<usize> {
    let available: HashSet<usize> = frames.iter().copied().collect();
    for &base in frames {
        if base % (count * BASE.bytes()) == 0
            && (0..count).all(|i| available.contains(&(base + i * BASE.bytes())))
        {
            return (0..count).map(|i| base + i * BASE.bytes()).collect();
        }
    }
    panic!("exhausted pool must contain an aligned {count}-frame run");
}

fn assert_multiframe_recovery(request_frames: usize) {
    set_cpu(0);
    let (a, _region) = pool(128);
    let mut held = Vec::new();
    while let Ok(p) = a.allocate_physical(BASE, N1) {
        held.push(p);
    }
    held.sort_unstable();

    let run = find_aligned_run(&held, request_frames);
    let release_count = (SLOTS * CAP + CAP / 2)
        .max(request_frames)
        .min(SLOTS * CAP + DEPOT_CAP);
    let mut released = run.clone();
    for &p in &held {
        if released.len() == release_count {
            break;
        }
        if !released.contains(&p) {
            released.push(p);
        }
    }
    held.retain(|p| !released.contains(p));

    for (i, &p) in released.iter().enumerate() {
        set_cpu(i % SLOTS);
        unsafe { a.deallocate_physical(BASE, N1, p) };
    }
    assert!(a.depot_len() > 0, "setup must populate the shared depot");
    assert!(
        a.cached_frames() > a.depot_len(),
        "setup must retain frames in magazines as well"
    );

    set_cpu(0);
    let count = NonZeroUsize::new(request_frames).unwrap();
    let p = a
        .allocate_physical(BASE, count)
        .expect("wrapper caches must be reclaimed after backend OOM");
    let released_set: HashSet<usize> = released.iter().copied().collect();
    assert!(
        (0..request_frames).all(|i| released_set.contains(&(p + i * BASE.bytes()))),
        "allocation must come from frames that were sequestered in wrapper caches"
    );
    assert!(a.frames_flushed() >= request_frames);
    if request_frames == 2 {
        assert!(
            a.cached_frames() > 0,
            "progressive recovery should stop once the request is satisfiable"
        );
    }

    unsafe { a.deallocate_physical(BASE, count, p) };
    for p in held {
        unsafe { a.deallocate_physical(BASE, N1, p) };
    }
}

// `new` must be `const fn`.
static _STATIC_DEPOT: Dep = DepotAllocator::new(
    BASE,
    SummaryBuddyAllocator::<ORDERS, TestProvenance>::new(BASE),
);

#[test]
fn alloc_dealloc_single_is_lifo_and_cached() {
    set_cpu(0);
    let (a, _region) = pool(64);

    let p = a.allocate_physical(BASE, N1).expect("alloc failed");
    assert_eq!(p % BASE.bytes(), 0, "address not frame-aligned");
    assert!(a.cached_frames() > 0, "refill should leave frames cached");

    unsafe { a.deallocate_physical(BASE, N1, p) };
    let p2 = a.allocate_physical(BASE, N1).expect("re-alloc failed");
    assert_eq!(p2, p, "expected the just-freed frame back (LIFO)");
    unsafe { a.deallocate_physical(BASE, N1, p2) };
}

#[test]
fn multiframe_requests_pass_through() {
    set_cpu(0);
    let (a, _region) = pool(64);

    let n4 = NonZeroUsize::new(4).unwrap();
    let p = a
        .allocate_physical(BASE, n4)
        .expect("contiguous alloc failed");
    assert_eq!(p % (4 * BASE.bytes()), 0, "order-2 block not aligned");
    assert_eq!(
        a.cached_frames(),
        0,
        "multi-frame traffic must not touch the magazines or depot"
    );
    unsafe { a.deallocate_physical(BASE, n4, p) };
    assert_eq!(
        a.cached_frames(),
        0,
        "multi-frame free must not touch the magazines or depot"
    );
}

#[test]
fn dealloc_caches_in_current_cpu_magazine() {
    let (a, _region) = pool(64);

    set_cpu(0);
    let p = a.allocate_physical(BASE, N1).expect("alloc failed");
    let cached_after_alloc = a.cached_frames();

    set_cpu(1);
    unsafe { a.deallocate_physical(BASE, N1, p) };
    assert_eq!(
        a.cached_frames(),
        cached_after_alloc + 1,
        "free must cache in the current CPU's magazine"
    );

    let p1 = a.allocate_physical(BASE, N1).expect("re-alloc failed");
    assert_eq!(p1, p, "slot 1 should return the frame it just cached");
    unsafe { a.deallocate_physical(BASE, N1, p1) };
}

#[test]
fn frees_overflow_into_depot_and_other_cpu_reuses() {
    set_cpu(0);
    let (a, _region) = pool(256);

    let held: Vec<usize> = (0..(CAP + DEPOT_CAP))
        .map(|_| a.allocate_physical(BASE, N1).unwrap())
        .collect();
    for &p in &held {
        unsafe { a.deallocate_physical(BASE, N1, p) };
    }
    assert!(
        a.depot_len() > 0,
        "magazine overflow should have populated the depot"
    );
    assert!(a.peak_depot_len() >= a.depot_len());

    set_cpu(1);
    let freed: HashSet<usize> = held.iter().copied().collect();
    let p = a
        .allocate_physical(BASE, N1)
        .expect("cross-CPU alloc failed");
    assert!(
        freed.contains(&p),
        "CPU 1 should reuse a frame CPU 0 freed via the depot"
    );
    unsafe { a.deallocate_physical(BASE, N1, p) };
}

#[test]
fn successful_backend_request_preserves_caches() {
    set_cpu(0);
    let (a, _region) = pool(256);

    let held: Vec<usize> = (0..(CAP + DEPOT_CAP))
        .map(|_| a.allocate_physical(BASE, N1).unwrap())
        .collect();
    for &p in &held {
        unsafe { a.deallocate_physical(BASE, N1, p) };
    }
    let cached_before = a.cached_frames();
    let depot_before = a.depot_len();
    assert!(depot_before > 0, "depot should be populated");

    let big = NonZeroUsize::new(16).unwrap();
    let p = a
        .allocate_physical(BASE, big)
        .expect("contiguous allocation failed");
    assert_eq!(
        a.depot_len(),
        depot_before,
        "recovery must not drain before the backend reports OOM"
    );
    assert_eq!(a.cached_frames(), cached_before);
    assert_eq!(a.frames_flushed(), 0);
    unsafe { a.deallocate_physical(BASE, big, p) };
}

#[test]
fn single_frame_oom_steals_from_sibling_magazine() {
    set_cpu(0);
    let (a, _region) = pool(64);
    let mut held = Vec::new();
    while let Ok(p) = a.allocate_physical(BASE, N1) {
        held.push(p);
    }

    let stranded = held.pop().expect("pool must contain a frame");
    set_cpu(1);
    unsafe { a.deallocate_physical(BASE, N1, stranded) };

    set_cpu(0);
    assert_eq!(
        a.allocate_physical(BASE, N1),
        Ok(stranded),
        "backend OOM must not hide a frame in a sibling magazine"
    );
}

#[test]
fn progressive_recovery_handles_requests_below_equal_and_above_cap() {
    for request_frames in [2, CAP, 2 * CAP] {
        assert_multiframe_recovery(request_frames);
    }
}

#[test]
fn explicit_flush_drains_depot_and_magazines() {
    set_cpu(0);
    let (a, _region) = pool(256);
    fill_depot(&a);
    let cached_before = a.cached_frames();
    assert!(a.depot_len() > 0);
    assert!(cached_before > a.depot_len());

    a.flush();

    assert_eq!(a.depot_len(), 0);
    assert_eq!(a.cached_frames(), 0);
    assert_eq!(a.frames_flushed(), cached_before);
}

#[test]
fn non_oom_error_preserves_all_caches() {
    set_cpu(0);
    let (a, _region) = pool(256);
    fill_depot(&a);
    let cached_before = a.cached_frames();
    let flushed_before = a.frames_flushed();

    let too_large = NonZeroUsize::new(1usize << ORDERS).unwrap();
    assert_eq!(
        a.allocate_physical(BASE, too_large),
        Err(crate::AllocError::RequestTooLarge)
    );
    assert_eq!(a.cached_frames(), cached_before);
    assert_eq!(a.frames_flushed(), flushed_before);
}

#[test]
fn small_multiframe_bypasses_and_keeps_depot() {
    set_cpu(0);
    let (a, _region) = pool(256);

    let held: Vec<usize> = (0..(CAP + DEPOT_CAP))
        .map(|_| a.allocate_physical(BASE, N1).unwrap())
        .collect();
    for &p in &held {
        unsafe { a.deallocate_physical(BASE, N1, p) };
    }
    let depot_before = a.depot_len();
    assert!(depot_before > 0, "depot should be populated");

    let n4 = NonZeroUsize::new(4).unwrap();
    let p = a
        .allocate_physical(BASE, n4)
        .expect("multi-frame alloc failed");
    assert_eq!(
        a.depot_len(),
        depot_before,
        "a small multi-frame request must not flush the depot"
    );
    unsafe { a.deallocate_physical(BASE, n4, p) };
}

#[test]
fn satisfiable_contiguous_request_preserves_depot() {
    set_cpu(0);
    let (a, _region) = pool(256);
    fill_depot(&a);

    let depot_before = a.depot_len();
    assert!(depot_before > 0, "depot should be populated");

    let big = NonZeroUsize::new(16).unwrap();
    let p = a
        .allocate_physical(BASE, big)
        .expect("contiguous alloc should succeed from the untouched backend");

    assert_eq!(
        a.depot_len(),
        depot_before,
        "recovery must not drain the depot when the request is already satisfiable"
    );
    assert_eq!(
        a.frames_flushed(),
        0,
        "no frames should have been returned to the backend"
    );
    unsafe { a.deallocate_physical(BASE, big, p) };
}

#[test]
fn unsatisfiable_request_drains_all_caches() {
    set_cpu(0);
    let (a, _region) = pool(16);
    let mut held = Vec::new();
    while let Ok(p) = a.allocate_physical(BASE, N1) {
        held.push(p);
    }
    for &p in &held {
        unsafe { a.deallocate_physical(BASE, N1, p) };
    }

    let depot_before = a.depot_len();
    let cached_before = a.cached_frames();
    assert!(depot_before > 0, "depot should be populated");

    let block = NonZeroUsize::new(16).unwrap(); // order-4 = pool's max block
    let r = a.allocate_physical(BASE, block);
    assert!(
        r.is_err(),
        "request must OOM — the cache cannot rebuild order-4"
    );

    assert_eq!(
        a.depot_len(),
        0,
        "the failing request should have drained the whole depot"
    );
    assert_eq!(
        a.cached_frames(),
        0,
        "the failing request should also have drained every magazine"
    );
    assert_eq!(
        a.frames_flushed(),
        cached_before,
        "all cached frames should have been returned"
    );
}

#[cfg_attr(audit, ignore = "audit builds are sequential-only")]
#[test]
fn concurrent_cross_cpu_no_double_alloc_or_leak() {
    const ITERS: usize = if cfg!(miri) { 16 } else { 5_000 };
    let (alloc, _region) = pool(if cfg!(miri) { 128 } else { 1024 });
    let alloc = Arc::new(alloc);
    let checked_out: Arc<Mutex<HashSet<usize>>> = Arc::new(Mutex::new(HashSet::new()));
    let next_cpu = Arc::new(AtomicUsize::new(0));

    let handles: Vec<_> = (0..4)
        .map(|_| {
            let alloc = Arc::clone(&alloc);
            let checked_out = Arc::clone(&checked_out);
            let next_cpu = Arc::clone(&next_cpu);
            std::thread::spawn(move || {
                let cpu = next_cpu.fetch_add(1, Relaxed);
                let neighbour = (cpu + 1) % SLOTS;
                for i in 0..ITERS {
                    set_cpu(cpu);
                    if let Ok(p) = alloc.allocate_physical(BASE, N1) {
                        assert!(
                            checked_out.lock().unwrap().insert(p),
                            "frame {p:#x} handed out twice"
                        );
                        assert!(checked_out.lock().unwrap().remove(&p), "frame vanished");
                        set_cpu(neighbour);
                        unsafe { alloc.deallocate_physical(BASE, N1, p) };
                    }
                    let big = NonZeroUsize::new(16).unwrap();
                    if i % 512 == 0
                        && let Ok(p) = alloc.allocate_physical(BASE, big)
                    {
                        unsafe { alloc.deallocate_physical(BASE, big, p) };
                    }
                }
            })
        })
        .collect();
    for h in handles {
        h.join().unwrap();
    }

    assert!(
        checked_out.lock().unwrap().is_empty(),
        "frames still checked out after all workers finished"
    );

    set_cpu(0);
    let mut remaining = 0;
    while alloc.allocate_physical(BASE, N1).is_ok() {
        remaining += 1;
    }
    assert!(remaining > 0, "pool empty after churn — frames leaked");
}

#[test]
fn try_init_forwards_backend_error() {
    // The wrapper's `try_init` forwards the backend's `InitError` unchanged.
    let total_bytes = 16 * BASE.bytes();
    let max_block = BASE.bytes() << (ORDERS - 1);
    let region = OwnedRegion::new(total_bytes, max_block);
    let alloc: Dep = DepotAllocator::new(
        BASE,
        SummaryBuddyAllocator::<ORDERS, TestProvenance>::new(BASE),
    );
    // phys_base + 1 is not base-frame aligned — a backend `Misaligned`.
    let e = unsafe { alloc.try_init_region(region.addr() + 1, total_bytes) };
    assert_eq!(
        e,
        Err(InitError::Misaligned {
            required: BASE.bytes()
        })
    );
    // Untouched: a corrected retry succeeds and the allocator works.
    let ok = unsafe { alloc.try_init_region(region.addr(), total_bytes) };
    assert_eq!(ok, Ok(()));
    assert!(alloc.allocate_physical(BASE, N1).is_ok());
}

#[test]
#[should_panic(expected = "SLOTS must be > 0")]
fn zero_slots_panics() {
    let _ = DepotAllocator::<
        SummaryBuddyAllocator<ORDERS, TestProvenance>,
        TestCpuId,
        0,
        CAP,
        DEPOT_CAP,
    >::new(
        BASE,
        SummaryBuddyAllocator::<ORDERS, TestProvenance>::new(BASE),
    );
}

#[test]
#[should_panic(expected = "CAP must be >= 2")]
fn cap_below_two_panics() {
    let _ = DepotAllocator::<
        SummaryBuddyAllocator<ORDERS, TestProvenance>,
        TestCpuId,
        SLOTS,
        1,
        DEPOT_CAP,
    >::new(
        BASE,
        SummaryBuddyAllocator::<ORDERS, TestProvenance>::new(BASE),
    );
}

#[test]
#[should_panic(expected = "DEPOT_CAP must be >= 1")]
fn depot_cap_zero_panics() {
    let _ = DepotAllocator::<SummaryBuddyAllocator<ORDERS, TestProvenance>, TestCpuId, SLOTS, CAP, 0>::new(
        BASE,
        SummaryBuddyAllocator::<ORDERS, TestProvenance>::new(BASE),
    );
}