Re: [PATCH v5 12/35] gpu: nova-core: mm: add VramBlock

From: Zhi Wang

Date: Fri Nov 27 2026 - 05:02:39 EST


On Thu, 26 Nov 2026 23:12:47 +0100
"Danilo Krummrich" <dakr@xxxxxxxxxx> wrote:

> On Tue Nov 24, 2026 at 2:59 PM CET, Alok Kumar wrote:
> > +/// A physically contiguous VRAM allocation.
> > +///
> > +/// # Invariants
> > +///
> > +/// `range.start <= range.end`.
> > +pub(crate) struct VramBlock {
> > + _blocks: Pin<KBox<AllocatedBlocks>>,
> > + range: Range<u64>,
> > +}
>
> This allocation seems unnecessary, VramBlock can itself be
> constructed as pin initializer...
>

make sense.

> > +
> > +impl VramBlock {
> > + /// Returns the range covered by this VRAM block.
> > + pub(crate) const fn range(&self) -> &Range<u64> {
> > + &self.range
> > + }
> > +}

snip

> > + Ok(Arc::new(
> > + VramBlock {
> > + _blocks: blocks,
> > + // INVARIANT: `range.start <= range.end` was
> > checked above, hence
> > + // `address <= end`.
> > + range: start..end,
> > + },
> > + GFP_KERNEL,
> > + )?)
>
> To me it seems that reference counting is not needed here.
>

This patch was cooked long time before the VgpuVramSlotPool and mm
settled. At that time I didn't know how the final ownership view will
look like among those components. So I just put an Arc to ensure the
backing allocation stayed alive for as long as any region or BAR
mapping needed it, without tying those users to a particual owner's
lifetime at that stage.

And you are right, looking at the current ownership view, there
shouldn't be any user of the backing allocation needs to outlive struct
VgpuInstances at this time so we can definitely remove the Arc.

> AFAICS, a VramBlock does not actually have shared ownership, it is
> owned by the VgpuVramSlotPool of the VgpuInstances.
>
> A VgpuInstance owns a VgpuVramSlot, and a VgpuVramSlot owns a
> VramRegion. The VramRegion borrows (a range) from the
> VgpuVramSlotPool's VramBlock.
>
> Finally, a BarMapping (also owned by a VgpuInstance) also borrows
> from the VgpuVramSlotPool's VramBlock.
>
> Do I miss anything? I tried to double check and asked my clanker to
> inline all the relevant structures, and it came up with this:
>
> struct VgpuInstances {
> vram_slots: Option<VgpuVramSlotPool {
> backing: VramBlock {
> _blocks: Pin<KBox<AllocatedBlocks {
> buddy: &'mm GpuBuddy,
> // Allocated buddy blocks.
> }>>,
> range: Range<u64>,
> },
> layout: VgpuVramLayout,
> fb_region_size: u64,
> }>,
>
> instances: KVec<VgpuInstance {
> vram_slot: VgpuVramSlot {
> index: u32,
> fbmem: VramRegion {
> backing: &'pool VramBlock,
> range: Range<u64>,
> },
> mgmt_heap: VramRegion {
> backing: &'pool VramBlock,
> range: Range<u64>,
> },
> },
>
> plugin_rpc: PluginRpc {
> comm: CommBufferRegion {
> map: BarMapping {
> region: VramRegion {
> backing: &'pool VramBlock,
> range: Range<u64>,
> },
> access: BarUserAccess {
> bar_user: &BarUser,
> mapped: Option<MappedRange>,
> },
> mm: &SharedGpuMm,
> unmap_error: Option<Error>,
> },
> },
> // Other RPC state omitted.
> },
> }>,
> }
>
> (In case you wonder why it says &'a GpuBuddy for AllocatedBlocks, I
> have [1] applied locally.)
>
> [1]
> https://lore.kernel.org/nova-gpu/20261126221108.3768637-1-dakr@xxxxxxxxxx/