Re: [PATCH v6 12/34] gpu: nova-core: mm: add VramRegion

From: Danilo Krummrich

Date: Fri Dec 04 2026 - 13:59:52 EST


On Wed Dec 2, 2026 at 9:28 AM CET, Alok Kumar wrote:
> diff --git a/drivers/gpu/nova-core/mm/vram.rs b/drivers/gpu/nova-core/mm/vram.rs
> index 63bc0ce93663..be3076d065c6 100644
> --- a/drivers/gpu/nova-core/mm/vram.rs
> +++ b/drivers/gpu/nova-core/mm/vram.rs
> @@ -21,6 +21,21 @@
> PAGE_SIZE, //
> };
>
> +/// Rebases the nonempty range `rel`, relative to the start of `base`, onto `base`.
> +///
> +/// Returns `EINVAL` if `rel` is empty or does not fit within `base`.
> +fn rebase(base: &Range<u64>, rel: Range<u64>) -> Result<Range<u64>> {
> + let start = base.start.checked_add(rel.start).ok_or(EINVAL)?;
> + let end = base.start.checked_add(rel.end).ok_or(EINVAL)?;
> +
> + // Check that the rebased range on not empty and fits within `base`.
> + if start >= end || end > base.end {
> + return Err(EINVAL);
> + }
> +
> + Ok(start..end)
> +}
> +
> /// A physically contiguous VRAM allocation.
> ///
> /// # Invariants
> @@ -38,6 +53,47 @@ impl VramBlock {
> pub(crate) const fn range(&self) -> &Range<u64> {
> &self.range
> }
> +
> + /// Creates a byte range relative to this allocation, borrowing its backing storage.
> + pub(crate) fn region(&self, range: Range<u64>) -> Result<VramRegion<'_>> {
> + Ok(VramRegion {
> + backing: self,
> + // INVARIANT: `rebase` returns a nonempty range.
> + range: rebase(&self.range, range)?,
> + })
> + }
> +}
> +
> +/// A byte range borrowing its backing VRAM allocation.
> +///
> +/// # Invariants
> +///
> +/// `range.start < range.end`, and `range` lies within the range of `backing`.
> +#[derive(Clone)]
> +pub(crate) struct VramRegion<'pool> {
> + backing: &'pool VramBlock,

'pool seems odd here, I think 'block would be reasonable, but in this case I'd
just use 'a.

> + range: Range<u64>,
> +}
> +
> +impl VramRegion<'_> {
> + /// Returns the range covered by this region.
> + pub(crate) const fn range(&self) -> &Range<u64> {
> + &self.range
> + }
> +
> + /// Returns the length of this region in bytes.
> + pub(crate) const fn len(&self) -> u64 {
> + self.range.end.saturating_sub(self.range.start)

saturating_sub() seems unnecessary given the type invariant.

> + }
> +
> + /// Creates a nonempty subregion relative to this view's start.
> + pub(crate) fn subregion(&self, range: Range<u64>) -> Result<Self> {
> + Ok(Self {
> + backing: self.backing,
> + // INVARIANT: `rebase` returns a nonempty range.
> + range: rebase(&self.range, range)?,
> + })
> + }
> }
>
> impl GpuMm<'_> {