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...

> +
> +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,
> +        )?;

...so we avoid this allocation.

> +        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.

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/[email protected]/

Reply via email to