[PATCH v5 12/35] gpu: nova-core: mm: add VramBlock
From: Alok Kumar
Date: Tue Nov 24 2026 - 09:03:51 EST
From: Zhi Wang <zhiw@xxxxxxxxxx>
vGPU memory slots need allocations at fixed offsets within usable VRAM.
Add GpuMm::alloc_vram_range and VramBlock to own these exact allocations
and return their storage to the buddy allocator on drop.
Use the buddy allocator's range validation and retain the checks for
absolute physical address overflow and alignment.
Cc: Alistair Popple <apopple@xxxxxxxxxx>
Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx>
Co-developed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
Signed-off-by: Alok Kumar <alkumar@xxxxxxxxxx>
---
drivers/gpu/nova-core/mm.rs | 2 +
drivers/gpu/nova-core/mm/vram.rs | 85 ++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+)
create mode 100644 drivers/gpu/nova-core/mm/vram.rs
diff --git a/drivers/gpu/nova-core/mm.rs b/drivers/gpu/nova-core/mm.rs
index f53886f9ed91..a0aeb96bfa36 100644
--- a/drivers/gpu/nova-core/mm.rs
+++ b/drivers/gpu/nova-core/mm.rs
@@ -67,6 +67,8 @@ macro_rules! impl_pfn_bounded {
mod regs;
pub(super) mod tlb;
pub(super) mod vmm;
+#[expect(dead_code)]
+pub(crate) mod vram;
/// GPU Memory Manager - owns all core MM components.
///
diff --git a/drivers/gpu/nova-core/mm/vram.rs b/drivers/gpu/nova-core/mm/vram.rs
new file mode 100644
index 000000000000..6839960eeb2c
--- /dev/null
+++ b/drivers/gpu/nova-core/mm/vram.rs
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+
+//! VRAM allocation.
+
+use core::ops::Range;
+
+use kernel::{
+ gpu::buddy::{
+ AllocatedBlocks,
+ GpuBuddyAllocFlags,
+ GpuBuddyAllocMode, //
+ },
+ num::casts::FromSafeCast,
+ prelude::*,
+ ptr::Alignment,
+ sync::Arc, //
+};
+
+use super::{
+ GpuMm,
+ PAGE_SIZE, //
+};
+
+/// A physically contiguous VRAM allocation.
+///
+/// # Invariants
+///
+/// `range.start <= range.end`.
+pub(crate) struct VramBlock {
+ _blocks: Pin<KBox<AllocatedBlocks>>,
+ range: Range<u64>,
+}
+
+impl VramBlock {
+ /// Returns the range covered by this VRAM block.
+ pub(crate) const fn range(&self) -> &Range<u64> {
+ &self.range
+ }
+}
+
+impl GpuMm<'_> {
+ /// Allocates an exact byte range relative to the buddy allocator's base.
+ pub(crate) fn alloc_vram_range(
+ &self,
+ range: Range<u64>,
+ align: Alignment,
+ ) -> Result<Arc<VramBlock>> {
+ const PAGE_SIZE_ALIGN: Alignment = Alignment::new::<PAGE_SIZE>();
+
+ let align = if align > PAGE_SIZE_ALIGN {
+ align
+ } else {
+ PAGE_SIZE_ALIGN
+ };
+ let size = range.end.checked_sub(range.start).ok_or(EINVAL)?;
+ let buddy = self.buddy();
+ let base = buddy.base_offset();
+ let start = base.checked_add(range.start).ok_or(EOVERFLOW)?;
+ let end = base.checked_add(range.end).ok_or(EOVERFLOW)?;
+ if !start.is_multiple_of(u64::from_safe_cast(align.as_usize())) {
+ return Err(EINVAL);
+ }
+
+ let blocks = KBox::pin_init(
+ buddy.alloc_blocks(
+ GpuBuddyAllocMode::Range(range),
+ size,
+ align,
+ GpuBuddyAllocFlags::default(),
+ ),
+ GFP_KERNEL,
+ )?;
+
+ Ok(Arc::new(
+ VramBlock {
+ _blocks: blocks,
+ // INVARIANT: `range.start <= range.end` was checked above, hence
+ // `address <= end`.
+ range: start..end,
+ },
+ GFP_KERNEL,
+ )?)
+ }
+}
--
2.53.0