From 237b7181b9b973a781dad8825101f03506237858 Mon Sep 17 00:00:00 2001 From: Decodere <98278826+Decodere@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:57:10 +0300 Subject: [PATCH] Memory: keep single-window sparse_reserve allocations inside one window After 3292b096 placed physical page homes in windows 1+ first via `Uma::sparse_alloc_above`, the start of window 1 holds 664+ MiB of homes on Series S sparse-heap layouts, leaving under 3.35 GiB in window 1 while window 2 has a full 4 GiB free. Because `Uma::sparse_reserve` always passed `single_window = false`, a title reservation of at most one window (for example ~3.4 GiB) straddled the window 1 / window 2 boundary (0x12_0000_0000) instead of landing wholly in window 2. Round `size` with checked arithmetic (`checked_next_multiple_of(GRAIN)?`), try `take_sparse_in(..., above_0, end, size, GRAIN, true)` first when `size <= WINDOW`, and fall back to cross-window placement in windows 1+ and then `lo..end` so window 0 stays uncluttered for 32-bit descriptor heaps and fragmented or multi-window reservations keep working. Related to #2867. Assisted-by: Google Antigravity (Gemini 4 Neon) --- crates/xcl/src/gpu/uma.rs | 63 +++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/gpu/uma.rs b/crates/xcl/src/gpu/uma.rs index b8e1816be4..7cac59bda0 100644 --- a/crates/xcl/src/gpu/uma.rs +++ b/crates/xcl/src/gpu/uma.rs @@ -546,19 +546,22 @@ impl Uma { /// Address space for a title reservation of `size` bytes in the sparse windows (64 KiB /// aligned, first fit; not committed). Prefers windows 1+ so window 0 stays uncluttered for - /// small allocations, falling back to all of the title's sparse space (never `desc_range`). + /// small allocations, keeping reservations of at most one window (`size <= WINDOW`) inside a + /// single window when one has room before falling back to cross-window placement or all of the + /// title's sparse space (never `desc_range`). pub fn sparse_reserve(&self, size: usize) -> Option { if !self.has_sparse() { return None; } - let size = (size.max(1) + GRAIN - 1) & !(GRAIN - 1); + let size = size.max(1).checked_next_multiple_of(GRAIN)?; let mut g = self.sparse_free.lock().unwrap(); let end = self.sparse_end(); let lo = self.title_sparse_start(); - if let Some(at) = take_sparse_in(&mut g, (self.base + WINDOW).max(lo), end, size, GRAIN, false) { - return Some(at); - } - take_sparse_in(&mut g, lo, end, size, GRAIN, false) + let above_0 = (self.base + WINDOW).max(lo); + let single = if size <= WINDOW { take_sparse_in(&mut g, above_0, end, size, GRAIN, true) } else { None }; + single + .or_else(|| take_sparse_in(&mut g, above_0, end, size, GRAIN, false)) + .or_else(|| take_sparse_in(&mut g, lo, end, size, GRAIN, false)) } /// Reserve an aligned range strictly inside sparse window `k` (window 0: 32-bit descriptor @@ -1508,6 +1511,54 @@ mod tests { assert!(u.sparse_alloc_above(WINDOW + GRAIN, GRAIN).is_none(), "no room above a window"); } + /// After physical page homes (`sparse_alloc_above`) take the start of window 1, a title + /// `sparse_reserve` of at most one window that no longer fits in window 1 lands wholly in + /// window 2 instead of straddling the window 1 / window 2 boundary; smaller reservations + /// still fit in window 1, fragmented windows 1+ still fall back to cross-window placement + /// without cluttering window 0, and overflow sizes return `None`. + #[test] + fn sparse_reserve_keeps_single_window_reservations_inside_one_window() { + let u = Uma::for_runtime_windows(16 << 20, RUNTIME_COMMITTED_CAP, MAX_HEAP_WINDOWS).expect("16-window aperture"); + let b = u.base(); + // Series S layout: 2892 MiB GPU heaps + 38 MiB view heap in window 0, 664 MiB homes in window 1. + let (heaps, _) = u.sparse_alloc(2892 << 20, 2 << 20).unwrap(); + let (homes, _) = u.sparse_alloc_above(664 << 20, GRAIN).unwrap(); + let (view_heap, _) = u.sparse_alloc(0x260_0000, GRAIN).unwrap(); + assert_eq!((u.window_of(heaps), u.window_of(homes), u.window_of(view_heap)), (Some(0), Some(1), Some(0))); + + // A ~3.42 GiB reservation exceeds window 1's remaining 3432 MiB and lands wholly in window 2. + let arena = 3500 << 20; + let r = u.sparse_reserve(arena).expect("single-window reservation in window 2"); + assert_eq!(r, b + 2 * WINDOW); + assert_eq!((u.window_of(r), u.window_of(r + arena - 1)), (Some(2), Some(2))); + + // Smaller reservations still fit in window 1's remaining tail without touching window 0. + let small = 256 << 20; + let s = u.sparse_reserve(small).expect("fits in window 1 tail"); + assert_eq!((u.window_of(s), u.window_of(s + small - 1)), (Some(1), Some(1))); + + for p in [s, r, homes] { + assert!(u.sparse_release(p).is_some()); + } + + // Fragmented windows 1+ (2 GiB free at end of window 1 + 2 GiB free at start of window 2): + // a 3 GiB reservation still succeeds via cross-window fallback in windows 1+ without touching window 0. + let (w1_head, _) = u.sparse_alloc_above(2 << 30, GRAIN).unwrap(); + let (w2_head, _) = u.sparse_alloc_in(2, 2 << 30, GRAIN).unwrap(); + let (w2_tail, _) = u.sparse_alloc_in(2, 2 << 30, GRAIN).unwrap(); + assert_eq!(u.sparse_release(w2_head), Some(2 << 30)); + let cross = 3 << 30; + let c = u.sparse_reserve(cross).expect("cross-window fallback in windows 1+"); + assert_eq!((u.window_of(c), u.window_of(c + cross - 1)), (Some(1), Some(2))); + + assert_eq!(u.sparse_reserve(usize::MAX), None); + + for p in [c, w2_tail, w1_head, view_heap, heaps] { + assert!(u.sparse_release(p).is_some()); + } + assert_eq!(u.sparse_used(), 0); + } + /// With 16 windows nothing of Windows' lands in the aperture: shaders read every window of /// it, and a window without memory of its own once read whatever Windows had put there. #[test]