diff --git a/crates/stela-boot-core/src/host_memory.rs b/crates/stela-boot-core/src/host_memory.rs index 491e079981..ad87520a8d 100644 --- a/crates/stela-boot-core/src/host_memory.rs +++ b/crates/stela-boot-core/src/host_memory.rs @@ -123,18 +123,23 @@ const PACK_MARGIN: u64 = 16 * MIB; /// about two thirds, but those are limits on memory the system takes back /// under pressure; here the share is held from the guest for the boot, so /// it is a sixth: 2.6 GiB on a 16 GB PC, 5.2 GiB on a 32 GB PC, and the -/// floor's 2 GiB below about 12 GiB free. +/// floor's 2.5 GiB below about 15 GiB free. const GPU_SHARE: u64 = 6; /// The share a macOS desktop needs. A 16 GiB VM's AMD integrated GPU (512 /// MiB carve-out, 3272 MiB of GTT) held 2058 MiB of RADV's heaps after a -/// sign-in and seven app launches (`driver heaps use/budget/size: -/// 277/1176/1176M/host 1781/2352/2352M/local`), and the 12 GiB VM's 1635 MiB -/// one minute in, with only WindowServer and loginwindow presenting. RADV's -/// heaps are the GTT and the carve-out's CPU-visible part (256 MiB there), so -/// 2 GiB of share tells the guest about 2.3 GiB. A host that leaves the -/// guest its floor only with less gets less, down to [`GPU_LEAST`], and says -/// so. -pub const GPU_FLOOR: u64 = 2 * GIB; +/// sign-in and seven app launches, the guest holding 1326 MiB (`driver +/// heaps use/budget/size: 277/1176/1176M/host 1781/2352/2352M/local`). A 12 +/// GiB VM's heaps held 489 MiB at the Metal device's first allocation and +/// grew 1.22 bytes for each byte the guest held, to 1763 MiB for 1045 MiB, +/// where its GTT, half of a 2 GiB share, ran out. The device budgets each +/// allocation at 1.25 bytes of the share and keeps a sixteenth free +/// (`metal::device::share` in the GPU crate), and Linux's own pages and the +/// driver's first buffers take about 480 MiB of it, so the guest is told +/// about (share - 480 MiB - share/16) * 0.8: 1.2 GiB of a 2 GiB share, short +/// of the session above, and 1.5 GiB of 2.5 GiB, which holds it. A host +/// that leaves the guest its floor only with less gets less, down to +/// [`GPU_LEAST`], and says so. +pub const GPU_FLOOR: u64 = 2560 * MIB; /// The most an integrated GPU's share grows to. A GPU whose firmware gives /// it a large carve-out (up to many GiB on recent processors) works in that /// beside the share; the guest keeps the rest of a large host. diff --git a/crates/stela-boot-core/src/host_memory/tests.rs b/crates/stela-boot-core/src/host_memory/tests.rs index 268603c35d..4d784709ed 100644 --- a/crates/stela-boot-core/src/host_memory/tests.rs +++ b/crates/stela-boot-core/src/host_memory/tests.rs @@ -584,8 +584,8 @@ fn each_runners_pool_gives_the_kernel_half_what_it_needs_and_the_user_half_the_r /// short of [`GPU_FLOOR`]; /// - the 12 GiB VM of the AMD port's first run (`free=10842M heap=3300M /// code=688M pool=710M guest=6144M`, where the heap ran out holding the -/// GPU's pages) gives it the floor, 2 GiB, beside its 512 MiB carve-out; -/// its heap is that of a host with 8.6 GiB free, the floor at its window. +/// GPU's pages) gives it the floor, 2.5 GiB, beside its 512 MiB carve-out; +/// its heap is that of a host with 8.1 GiB free, the heap's floor. /// A 16 GiB VM's heap peaked at 4482 MiB with 2058 MiB of driver heaps in /// a session of seven app launches, so its own part held 2.4 GiB, or up /// to 2.9 GiB where the carve-out held some of the driver's buffers; @@ -613,7 +613,7 @@ fn integrated_gpu_hosts_split_as_expected() { [ [2704, 416, 382, 3072, 780], [2704, 416, 382, 3072, 1362], - [2848, 560, 598, 4788, 2048], + [2816, 528, 548, 4390, 2560], [4400, 832, 724, 7272, 2644], [5232, 1664, 858, 18700, 5290], ] diff --git a/crates/stela-boot/src/native_memory.rs b/crates/stela-boot/src/native_memory.rs index cb04515b2a..0c49d4de66 100644 --- a/crates/stela-boot/src/native_memory.rs +++ b/crates/stela-boot/src/native_memory.rs @@ -55,8 +55,25 @@ pub unsafe fn initialize(cpu: usize, physical: u64, bytes: usize, table_offset: static GPU_SHARE: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0); /// Records the integrated GPU's share of the heap the loader planned. +/// With one, the Metal device budgets its allocations by what Linux holds +/// of the share ([`share_state`]). pub(crate) fn set_gpu_share(bytes: u64) { GPU_SHARE.store(bytes, core::sync::atomic::Ordering::Relaxed); + #[cfg(feature = "nvk")] + if bytes != 0 { + stela_drivers::gpu::metal::device::share::set_probe(share_state); + } +} + +/// What Linux holds of the integrated GPU's share now, and its size, from +/// the page catalog's counts; none before the catalog has its limit. +#[cfg(feature = "nvk")] +fn share_state() -> Option { + let (held, limit) = stela_linux::pages::held_under_limit()?; + Some(stela_drivers::gpu::metal::device::share::ShareState { + held: held as u64 * PAGE as u64, + limit: limit as u64 * PAGE as u64, + }) } /// The ordinary pages Linux may hold at once: an integrated GPU's share of diff --git a/crates/stela-driver-gpu/src/metal/capabilities.rs b/crates/stela-driver-gpu/src/metal/capabilities.rs index f405f7deba..869146257f 100644 --- a/crates/stela-driver-gpu/src/metal/capabilities.rs +++ b/crates/stela-driver-gpu/src/metal/capabilities.rs @@ -1005,14 +1005,24 @@ impl Derivation<'_> { low_power: integrated, removable: self.platform.removable, has_unified_memory: integrated, - recommended_max_working_set_size: device - .memory - .working_set_budget(self.platform.heaps, integrated) - .unwrap_or_else(|| { - device - .memory - .working_set_bytes(self.platform.heaps, integrated) - }), + recommended_max_working_set_size: { + let heaps = device + .memory + .working_set_budget(self.platform.heaps, integrated) + .unwrap_or_else(|| { + device + .memory + .working_set_bytes(self.platform.heaps, integrated) + }); + // A GPU with a share of RAM: what the share backs, as the + // device budgets its allocations. + match crate::metal::device::share::probe().and_then(|probe| probe()) { + Some(state) if integrated => { + heaps.min(crate::metal::device::share::working_set(state)) + } + _ => heaps, + } + }, limits: limits_out, argument_buffers_tier: argument_tier, read_write_texture_tier: read_write_tier, diff --git a/crates/stela-driver-gpu/src/metal/device.rs b/crates/stela-driver-gpu/src/metal/device.rs index c29f54d3f4..944ea6c862 100644 --- a/crates/stela-driver-gpu/src/metal/device.rs +++ b/crates/stela-driver-gpu/src/metal/device.rs @@ -49,6 +49,7 @@ pub mod pins; pub mod pipelines; pub mod queue; mod reclaim; +pub mod share; pub use reclaim::Reclaimed; pub mod resources; pub use resources::SurfaceNote; @@ -180,6 +181,9 @@ pub struct Device { /// ([`VRAM_CAP`](crate::metal::vulkan::physical::VRAM_CAP)): at or over /// them an allocation runs out of memory without asking the driver. memory_cap: Option, + /// An integrated GPU's share of RAM: the platform's probe of it, and + /// what the device saw at its first allocation ([`share`]). + share: Option, resources: resources::Resources, pipelines: pipelines::Pipelines, execution_sets: execution_sets::ExecutionSets, @@ -393,6 +397,7 @@ impl Device { shortage: reclaim::Shortage::default(), host_calls: None, memory_cap: crate::metal::vulkan::physical::VRAM_CAP, + share: share::probe().map(|probe| share::Budget { probe, base: None }), resources: resources::Resources::default(), pipelines: pipelines::Pipelines::default(), execution_sets: execution_sets::ExecutionSets::default(), diff --git a/crates/stela-driver-gpu/src/metal/device/reclaim.rs b/crates/stela-driver-gpu/src/metal/device/reclaim.rs index 6307ed1c21..4301f206dd 100644 --- a/crates/stela-driver-gpu/src/metal/device/reclaim.rs +++ b/crates/stela-driver-gpu/src/metal/device/reclaim.rs @@ -76,7 +76,17 @@ impl Device { info: &ImageInfo, request: MemoryRequest, ) -> Result<(Image, Memory, u64)> { + // What the image's texels take, for the share's budget: four bytes + // each, a third more for its levels, by its samples. + let [width, height, depth] = info.extent; + let texels = u64::from(width) * u64::from(height) * u64::from(depth); + let bytes = texels + .saturating_mul(4 * u64::from(info.array_layers.max(1))) + .saturating_mul(u64::from(info.samples.bits().max(1))) + .saturating_mul(if info.mip_levels > 1 { 4 } else { 3 }) + / 3; self.reclaiming( + bytes, |backend| backend.create_image(info, request), || { let [width, height, depth] = info.extent; @@ -95,6 +105,7 @@ impl Device { request: MemoryRequest, ) -> Result { self.reclaiming( + info.size, |backend| backend.create_buffer(info, request), || { let place = if request.mapped { "mapped" } else { "device" }; @@ -111,6 +122,7 @@ impl Device { memory_types: u32, ) -> Result { self.reclaiming( + bytes, |backend| backend.allocate_memory(bytes, request, memory_types), || format!("heap memory of {bytes} bytes"), ) @@ -118,10 +130,11 @@ impl Device { fn reclaiming( &mut self, + bytes: u64, mut make: impl FnMut(&mut B) -> Result, what: impl FnOnce() -> String, ) -> Result { - let first = if self.over_cap() { + let first = if self.over_cap() || self.share_refuses(bytes) { Err(Error::OutOfMemory) } else { make(&mut self.backend) @@ -130,7 +143,7 @@ impl Device { return first; } let reclaimed = self.reclaim()?; - let made = if reclaimed.any() && !self.over_cap() { + let made = if reclaimed.any() && !self.over_cap() && !self.share_refuses(bytes) { make(&mut self.backend) } else { first @@ -142,9 +155,49 @@ impl Device { /// Whether a test build's cap leaves the device no memory: it holds as /// much as the cap in resources, recycled textures and upload chunks. fn over_cap(&self) -> bool { - self.memory_cap.is_some_and(|cap| { - self.allocated + self.recycled().bytes + self.upload_chunks().bytes >= cap - }) + self.memory_cap.is_some_and(|cap| self.guest_bytes() >= cap) + } + + /// The bytes the device holds: resources, recycled textures and upload + /// chunks. + fn guest_bytes(&self) -> u64 { + self.allocated + self.recycled().bytes + self.upload_chunks().bytes + } + + /// Whether an integrated GPU's share of RAM would not hold an + /// allocation of `bytes` ([`super::share::refuses`]). The first asks + /// the share's state is the base the device's growth starts from. + fn share_refuses(&mut self, bytes: u64) -> bool { + let guest = self.guest_bytes(); + let Some(budget) = &mut self.share else { + return false; + }; + let Some(state) = (budget.probe)() else { + return false; + }; + let base = *budget.base.get_or_insert(super::share::Base { + held: state.held, + guest, + }); + super::share::refuses(state, base, guest, bytes) + } + + /// The share's state for a note: Linux's pages of it and its size. + fn share_note(&self) -> String { + match self.share.and_then(|budget| (budget.probe)()) { + Some(state) => format!( + "; the GPU's share of RAM: Linux holds {}M of {}M", + state.held >> 20, + state.limit >> 20 + ), + None => String::new(), + } + } + + /// Ties the device to a share with `probe`, as the platform's does. + #[cfg(test)] + pub(super) fn share_memory(&mut self, probe: super::share::Probe) { + self.share = Some(super::share::Budget { probe, base: None }); } /// Holds the device to `bytes`, as a test build's cap does. @@ -230,13 +283,14 @@ impl Device { let driver = self.driver_heaps(); self.shortage.notes.push_back(format!( "OUT OF MEMORY {count}: {} failed; {outcome}; the device holds allocated={}M \ - deferred={}M behind {} command buffers recycled={}M chunks={}M; {driver}", + deferred={}M behind {} command buffers recycled={}M chunks={}M; {driver}{}", what(), mib(held.allocated), mib(held.deferred_bytes), held.in_flight, mib(held.recycled.bytes), mib(held.chunks.bytes), + self.share_note(), )); } @@ -288,7 +342,7 @@ impl Device { }); self.shortage.notes.push_back(format!( "VRAM holds {}M of the {}M the guest works in: allocated={}M recycled={}M \ - chunks={}M blocks={}/{}M out_of_memory={}; {driver}{cap}", + chunks={}M blocks={}/{}M out_of_memory={}; {driver}{}{cap}", mib(held + memory.chunks.bytes), mib(budget), mib(memory.allocated), @@ -297,6 +351,7 @@ impl Device { memory.blocks.blocks, mib(memory.blocks.block_bytes), memory.out_of_memory, + self.share_note(), )); } diff --git a/crates/stela-driver-gpu/src/metal/device/share.rs b/crates/stela-driver-gpu/src/metal/device/share.rs new file mode 100644 index 0000000000..10fee6b219 --- /dev/null +++ b/crates/stela-driver-gpu/src/metal/device/share.rs @@ -0,0 +1,158 @@ +//! An integrated GPU's share of RAM, as the device budgets it. +//! +//! The GPU's buffers are Linux's pages, and Linux may hold no more than the +//! share (the page catalog's limit). Neither the driver's heap sizes nor its +//! budget bound what it takes: RADV's budget is advisory and follows its own +//! use, and amdgpu only fails a submission (`Not enough memory for command +//! submission`) once a buffer it must place finds no page. So the device +//! refuses an allocation itself, at its out-of-memory answer, before the +//! share is gone, from what the platform says Linux holds of it now +//! ([`ShareState`]). +//! +//! The driver's heaps hold more than the guest's resources: its own +//! buffers (command streams, descriptor and shader arenas, queries), the +//! device's block slack and upload chunks, and the images' metadata. On +//! one 12 GB host with a 2 GiB share the heaps held 489M at the device's +//! first allocation and then grew 1.22 bytes for each byte the guest held +//! (1763M for 1045M held); on another session, 2058M for 1326M. So an +//! allocation is taken to cost [`GROWTH`] times its bytes in pages, and +//! what the guest held at the device's first allocation, with Linux's pages +//! then, is the base the growth starts from. The share's VRAM carve-out is +//! not counted in: it holds what the driver placed first. +//! +//! The pages an allocation may take are then the larger of what Linux holds +//! now and what the guest's growth since the base takes, plus its own +//! growth; past the share less a margin ([`margin`]) it is refused. +use core::sync::atomic::{AtomicPtr, Ordering}; + +/// Bytes of driver memory each byte the guest holds costs, as a fraction: +/// 1.25, above the 1.07 to 1.22 measured. +pub const GROWTH: (u64, u64) = (5, 4); + +/// What the platform says of an integrated GPU's share now. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct ShareState { + /// The bytes Linux holds of it: every page, the GPU's buffers' and its own. + pub held: u64, + /// The share's bytes. + pub limit: u64, +} + +/// What the device saw at its first allocation. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct Base { + /// Linux's bytes then. + pub held: u64, + /// The bytes the device held then. + pub guest: u64, +} + +/// The platform's probe of a share: its state now, when there is one. +pub type Probe = fn() -> Option; + +/// A device's share: the probe, and what the device saw at its first +/// allocation, once it made one. +#[derive(Clone, Copy, Debug)] +pub struct Budget { + pub probe: Probe, + pub base: Option, +} + +static PROBE: AtomicPtr<()> = AtomicPtr::new(core::ptr::null_mut()); + +/// Sets what the device asks for the share's state: the platform's count +/// of Linux's pages, which it sets before the device opens when an +/// integrated GPU has a share. +pub fn set_probe(probe: Probe) { + PROBE.store(probe as *mut (), Ordering::Release); +} + +/// The platform's probe of the share, when one was set. +pub fn probe() -> Option { + let probe = PROBE.load(Ordering::Acquire); + // SAFETY: only `set_probe` stores here, a function of this type. + (!probe.is_null()).then(|| unsafe { core::mem::transmute::<*mut (), Probe>(probe) }) +} + +/// The share kept free of allocations: a sixteenth, at least 64 MiB, for +/// Linux's own pages and the work a refused allocation leaves in flight. +pub fn margin(limit: u64) -> u64 { + (limit / 16).max(64 << 20) +} + +fn growth(bytes: u64) -> u64 { + bytes.saturating_mul(GROWTH.0) / GROWTH.1 +} + +/// The memory the guest is told it works in: what the share has left past +/// the margin, at [`GROWTH`] driver bytes for each of the guest's. +pub fn working_set(state: ShareState) -> u64 { + let left = state + .limit + .saturating_sub(state.held) + .saturating_sub(margin(state.limit)); + left.saturating_mul(GROWTH.1) / GROWTH.0 +} + +/// Whether an allocation of `request` bytes must be refused, the device +/// holding `guest` bytes now and the share being in `state`. +pub fn refuses(state: ShareState, base: Base, guest: u64, request: u64) -> bool { + let committed = base + .held + .saturating_add(growth(guest.saturating_sub(base.guest))); + let pages = state.held.max(committed).saturating_add(growth(request)); + pages.saturating_add(margin(state.limit)) > state.limit +} + +#[cfg(test)] +mod tests { + use super::*; + + const MIB: u64 = 1 << 20; + + #[test] + fn the_guest_is_told_what_the_share_backs() { + // A 2.5 GiB share with 480M of Linux's pages at the device's open. + let state = ShareState { + held: 480 * MIB, + limit: 2560 * MIB, + }; + assert_eq!(margin(state.limit), 160 * MIB); + assert_eq!(working_set(state), 1536 * MIB); + // A small share keeps its 64 MiB margin; a full one tells nothing. + let small = ShareState { + held: 200 * MIB, + limit: 512 * MIB, + }; + assert_eq!(working_set(small), 198 * MIB + MIB * 2 / 5); + let full = ShareState { + held: 2560 * MIB, + limit: 2560 * MIB, + }; + assert_eq!(working_set(full), 0); + } + + #[test] + fn allocations_stop_before_the_share_is_gone() { + let limit = 2048 * MIB; + let base = Base { + held: 489 * MIB, + guest: 0, + }; + let now = |held| ShareState { held, limit }; + // The run that filled its GTT held 1045M, which a whole share + // backs; at 1300M the growth passes it, with Linux's pages not yet + // all taken. + assert!(!refuses(now(1400 * MIB), base, 1045 * MIB, 4 * MIB)); + assert!(refuses(now(1400 * MIB), base, 1300 * MIB, 4 * MIB)); + // Early, with room, an allocation is made. + assert!(!refuses(now(600 * MIB), base, 300 * MIB, 64 * MIB)); + // Linux's own count decides when it is past the growth: pages the + // guest freed but the driver kept, or Linux's own. + assert!(refuses(now(1950 * MIB), base, 100 * MIB, MIB)); + // A request too large for what is left is refused at once. + assert!(refuses(now(600 * MIB), base, 300 * MIB, 1024 * MIB)); + // The device holding less than at its base takes the base's pages. + assert!(!refuses(now(0), base, 0, MIB)); + } +} diff --git a/crates/stela-driver-gpu/src/metal/device/tests.rs b/crates/stela-driver-gpu/src/metal/device/tests.rs index 119e4649d5..d79fcff980 100644 --- a/crates/stela-driver-gpu/src/metal/device/tests.rs +++ b/crates/stela-driver-gpu/src/metal/device/tests.rs @@ -3707,6 +3707,59 @@ fn a_device_out_of_memory_frees_at_once_what_work_in_flight_never_read() { complete(&mut device, running); } +/// A device on an integrated GPU's share of RAM refuses an allocation +/// the share would not hold, at its out-of-memory answer and with a note +/// naming the share, before the driver's own allocation is asked: the +/// share's state is what Linux holds of it, not the driver's budget. +#[test] +fn a_device_on_a_share_refuses_what_the_share_would_not_hold() { + use super::share::ShareState; + use core::sync::atomic::{AtomicU64, Ordering}; + static HELD: AtomicU64 = AtomicU64::new(400 << 20); + fn state() -> Option { + Some(ShareState { + held: HELD.load(Ordering::Relaxed), + limit: 1024 << 20, + }) + } + let mut device = device_on(MockBackend::new()); + device.share_memory(state); + // 1024M less a 64M margin and Linux's 400M leave 560M, which holds + // 448M of the guest's at 1.25 bytes each: seven 64M textures. + let large = TextureDescriptor { + width: 4096, + height: 4096, + ..texture(PixelFormat::Rgba8Unorm, TextureUsage::SHADER_READ) + }; + let mut textures = Vec::new(); + for _ in 0..7 { + textures.push(device.new_texture(&large).unwrap()); + } + assert_eq!(device.new_texture(&large), Err(Error::OutOfMemory)); + assert_eq!(device.memory().out_of_memory, 1); + let note = device.take_memory_note().unwrap(); + assert!( + note.ends_with("; the GPU's share of RAM: Linux holds 400M of 1024M"), + "{note}" + ); + // Linux's own count decides too: with its pages near the limit even a + // small buffer is refused. + for texture in textures.drain(..) { + device.destroy_texture(texture).unwrap(); + } + HELD.store(1000 << 20, Ordering::Relaxed); + assert_eq!( + device + .new_buffer(1 << 20, options(StorageMode::Private)) + .err(), + Some(Error::OutOfMemory) + ); + HELD.store(400 << 20, Ordering::Relaxed); + device + .new_buffer(1 << 20, options(StorageMode::Private)) + .unwrap(); +} + /// A test build's cap makes the device's allocations run out of memory at /// that many bytes held, with the same freeing and the same notes as a /// driver that is full. diff --git a/crates/stela-linux/src/compat/kernel/page.c b/crates/stela-linux/src/compat/kernel/page.c index 39b4be6003..6a82125458 100644 --- a/crates/stela-linux/src/compat/kernel/page.c +++ b/crates/stela-linux/src/compat/kernel/page.c @@ -279,6 +279,15 @@ int stela_linux_page_catalog_limit(u64 generation, size_t pages) return 0; } +/* The ordinary pages Linux holds and its limit, 0 for none, read from any + * processor without the catalog's owner: a snapshot that may be a page or + * two behind, for a budget that keeps a margin. */ +void stela_linux_page_catalog_use(size_t *held, size_t *limit) +{ + *held = READ_ONCE(catalog.ordinary_live); + *limit = READ_ONCE(catalog.page_limit); +} + int stela_linux_page_owner_context(const struct stela_linux_owner *expected) { struct stela_linux_lease lease; @@ -2225,6 +2234,12 @@ static void page_limit(void) assert(held); assert(stela_linux_page_catalog_limit(catalog.generation, 3) == -EBUSY); assert(!stela_linux_page_catalog_limit(catalog.generation, 6)); + { + size_t held, limit; + + stela_linux_page_catalog_use(&held, &limit); + assert(limit == 6 && held == catalog.ordinary_live); + } assert(totalram_pages() == 6 && zone_managed_pages(zone) == 6); assert(stela_linux_page_boot_capacity() == 6); unsigned long available = 0; diff --git a/crates/stela-linux/src/compat/stela/page.h b/crates/stela-linux/src/compat/stela/page.h index 83ef166365..5dfca92009 100644 --- a/crates/stela-linux/src/compat/stela/page.h +++ b/crates/stela-linux/src/compat/stela/page.h @@ -168,6 +168,7 @@ unsigned long stela_linux_page_boot_capacity(void); * -EINVAL for zero or more than the ordinary inventory, -EBUSY when more is * held already. */ int stela_linux_page_catalog_limit(u64 generation, size_t pages); +void stela_linux_page_catalog_use(size_t *held, size_t *limit); /* Publish the ordinary free-frame snapshot only on success. */ int stela_linux_available_page_frames(unsigned long *available); /* Entered serialized owner only. Pin one live aligned physical page's actual diff --git a/crates/stela-linux/src/pages.rs b/crates/stela-linux/src/pages.rs index 9091602e98..3a7918a4c5 100644 --- a/crates/stela-linux/src/pages.rs +++ b/crates/stela-linux/src/pages.rs @@ -254,6 +254,7 @@ unsafe extern "C" { fn stela_linux_page_catalog_unregister(generation: u64) -> c_int; fn stela_linux_page_catalog_verify(generation: u64) -> c_int; fn stela_linux_page_catalog_limit(generation: u64, pages: usize) -> c_int; + fn stela_linux_page_catalog_use(held: *mut usize, limit: *mut usize); fn stela_linux_page_owner_context(owner: *const super::owner::RawOwner) -> c_int; fn stela_linux_kernel_catalogue_active() -> bool; fn stela_linux_page_request_basic(gfp: u32) -> bool; @@ -886,3 +887,13 @@ impl Drop for Catalog

{ #[cfg(test)] mod tests; + +/// The ordinary pages Linux holds now and the limit on them, when its +/// catalog has one (an integrated GPU's share): a snapshot from any +/// processor, which may be a page or two behind. +pub fn held_under_limit() -> Option<(usize, usize)> { + let (mut held, mut limit) = (0, 0); + // SAFETY: two plain reads of the catalog's counts into locals. + unsafe { stela_linux_page_catalog_use(&raw mut held, &raw mut limit) }; + (limit != 0).then_some((held, limit)) +}