From 8a3e42cec3bb0a583c0abab85b872143f6462406 Mon Sep 17 00:00:00 2001 From: Simon-CR <292446+Simon-CR@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:40:06 -0500 Subject: [PATCH] kernelx: keep AllocateTitlePhysicalPages out of coherent UMA when sparse is on When sparse windows are exhausted during AllocateTitlePhysicalPages, spilling physical page home reservations into the fixed coherent UMA window consumes scarce GPU-visible coherent RAM even though physical page homes only need virtual address space (already noted for the plain MEM_RESERVE fallback). Skip the coherent fallback when sparse windows are enabled and fall back directly to plain MEM_RESERVE. Part of #1566. Assisted-by: Antigravity --- crates/xcl/src/hle/kernelx.rs | 45 ++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index afa86c30c8..0418e3a1c2 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1206,6 +1206,14 @@ unsafe fn move_away(away: &mut Away, old: usize, at: usize, page: usize, in_uma: Ok(true) } +fn alloc_physical_home_block(u: &crate::gpu::uma::Uma, bytes: usize) -> Option { + if u.has_sparse() { + u.sparse_alloc(bytes, PHYS_PAGE).map(|(p, _)| p) + } else { + u.alloc(bytes, PHYS_PAGE).map(|p| p as usize) + } +} + pub unsafe extern "system" fn AllocateTitlePhysicalPages(_p: HANDLE, _t: u32, num: *mut usize, arr: *mut usize) -> BOOL { if num.is_null() || arr.is_null() || *num == 0 { fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER); @@ -1223,7 +1231,7 @@ pub unsafe extern "system" fn AllocateTitlePhysicalPages(_p: HANDLE, _t: u32, nu // (a sparse or plain home is committed when it is used: `home_again`); without room in // unified memory plain memory (pages mapped into the title's own reservations never use // their home: a retail game's 6.1 GB of them did not fit next to its GPU heaps) - let block = u.sparse_alloc(bytes, PHYS_PAGE).map(|(p, _)| p).or_else(|| u.alloc(bytes, PHYS_PAGE).map(|p| p as usize)).or_else(|| { + let block = alloc_physical_home_block(u, bytes).or_else(|| { let p = mem::VirtualAlloc(std::ptr::null(), bytes, mem::MEM_RESERVE, mem::PAGE_READWRITE) as usize; static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| crate::xlog_info!("AllocateTitlePhysicalPages({n}): no room in unified memory – their home is plain memory")); @@ -3080,4 +3088,39 @@ mod tests { assert_eq!(VirtualFree(cpu, 0, mem::MEM_RELEASE), 1); } } + + #[test] + fn physical_home_block_does_not_spill_into_coherent_when_sparse_enabled() { + let g = crate::gpu::uma::GRAIN; + let Some(u) = crate::gpu::uma::Uma::for_runtime_with(g * 4, g * 4) else { + return crate::gpu::recomp::runner::skip_test("no virtual address space"); + }; + assert!(u.has_sparse()); + let coherent_before = u.used(); + + // Exhaust sparse windows via zero-commit reservations so `u.sparse_alloc(PHYS_PAGE, PHYS_PAGE)` fails. + let mut reserved = Vec::new(); + while let Some(r) = u.sparse_reserve(1usize << 30) { + reserved.push(r); + } + while let Some((p, _)) = u.sparse_alloc(PHYS_PAGE, PHYS_PAGE) { + reserved.push(p); + } + assert!(u.sparse_alloc(PHYS_PAGE, PHYS_PAGE).is_none()); + + assert_eq!( + alloc_physical_home_block(&u, PHYS_PAGE), + None, + "failed sparse physical home allocation must never spill into coherent UMA" + ); + assert_eq!(u.used(), coherent_before, "coherent UMA must stay untouched when sparse is enabled"); + + for r in reserved { + let _ = u.sparse_release(r); + } + let ok = alloc_physical_home_block(&u, PHYS_PAGE).expect("sparse physical home succeeds once sparse space is free"); + assert!(u.is_sparse(ok)); + let _ = u.sparse_release(ok); + assert_eq!(u.used(), coherent_before); + } }