From: Zhi Wang vGPU communication buffers need CPU access to regions within their VramBlocks. Add BarMapping to map these regions through BAR1. Store a VramRegion view that borrows its backing allocation, and carry that borrow through the mapping's lifetime. Borrow BarUser and the MM mutex for mapping operations, and unmap on drop using the retained MM dependency. The mapping does not acquire shared ownership of VramBlock. Provide an explicit unmap operation so owners can observe errors before releasing resources. Keep the mapping and borrowed region on failure, remember the first error and reject subsequent accesses. Owners must retain failed mappings and their backing allocation until device teardown; drop does not retry an unmap that already failed. Cc: Alistair Popple Signed-off-by: Zhi Wang Co-developed-by: Alexandre Courbot Signed-off-by: Alexandre Courbot Signed-off-by: Alok Kumar --- drivers/gpu/nova-core/mm/bar_user.rs | 100 +++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/drivers/gpu/nova-core/mm/bar_user.rs b/drivers/gpu/nova-core/mm/bar_user.rs index ff1d0e443281..64c63cf2e977 100644 --- a/drivers/gpu/nova-core/mm/bar_user.rs +++ b/drivers/gpu/nova-core/mm/bar_user.rs @@ -10,6 +10,7 @@ Mmio, // }, new_mutex, + num::casts::FromSafeCast, prelude::*, sync::Mutex, // }; @@ -22,6 +23,7 @@ MappedRange, Vmm, // }, + vram::VramRegion, GpuMm, Pfn, Vfn, @@ -171,6 +173,104 @@ fn drop(&mut self) { } } +/// A BAR mapping that borrows its backing VRAM allocation. +#[must_use] +pub(crate) struct BarMapping<'pool, 'map, 'gpu> { + /// Mapping of `region`. + access: BarUserAccess<'map, 'gpu>, + mm: &'map Mutex>, + /// The VRAM region that is mapped. + region: VramRegion<'pool>, + unmap_error: Option, +} + +#[expect(dead_code)] +impl<'pool, 'map, 'gpu> BarMapping<'pool, 'map, 'gpu> { + /// Maps `region`, which must start and end on a page boundary. + pub(crate) fn new( + bar_user: &'map BarUser<'gpu>, + mm: &'map Mutex>, + region: VramRegion<'pool>, + writable: bool, + ) -> Result { + const U64_PAGE_SIZE: u64 = cv!(PAGE_SIZE); + + let range = region.range(); + if !range.start.is_multiple_of(U64_PAGE_SIZE) || !range.end.is_multiple_of(U64_PAGE_SIZE) { + return Err(EINVAL); + } + + let num_pages = usize::try_from(region.len() / U64_PAGE_SIZE).map_err(|_| EOVERFLOW)?; + let mut pfns = KVec::with_capacity(num_pages, GFP_KERNEL)?; + for page in 0..num_pages { + let address = u64::from_safe_cast(page) + .checked_mul(U64_PAGE_SIZE) + .and_then(|byte_offset| byte_offset.checked_add(range.start)) + .ok_or(EOVERFLOW)?; + pfns.push(Pfn::from(VramAddress::from_raw(address)), GFP_KERNEL)?; + } + + let access = bar_user.map(&mut mm.lock(), &pfns, writable)?; + + Ok(Self { + access, + mm, + region, + unmap_error: None, + }) + } + + pub(crate) fn region(&self) -> &VramRegion<'pool> { + &self.region + } + + fn access(&self) -> Result<&BarUserAccess<'map, 'gpu>> { + if self.access.mapped.is_none() { + return Err(ENODEV); + } + if let Some(error) = self.unmap_error { + return Err(error); + } + Ok(&self.access) + } + + /// Returns a typed MMIO view of a `T` located at the start of the mapped region. + /// + /// Returns an error if the mapping is not live anymore, if a `T` does not fit within the + /// region, or if the region is not properly aligned for `T`. + pub(crate) fn view(&self) -> Result> { + // The mapping covers exactly the region, so this also checks that the `T` fits within it. + self.access()?.view(0) + } + + /// Unmap while retaining the mapping and its backing storage on failure. + /// + /// The first error is retained; later calls do not retry the operation. + pub(crate) fn unmap(&mut self) -> Result { + if let Some(error) = self.unmap_error { + return Err(error); + } + let result = self.access.unmap(&mut self.mm.lock()); + if let Err(error) = result { + self.unmap_error = Some(error); + } + result + } +} + +impl Drop for BarMapping<'_, '_, '_> { + /// Drop unmaps the region and may sleep. Call [`Self::unmap`] to observe errors; + /// on failure the owner must retain this object until device teardown. + fn drop(&mut self) { + if self.unmap_error.is_some() { + return; + } + if let Err(error) = self.unmap() { + kernel::pr_err!("failed to unmap BAR1 region: {:?}\n", error); + } + } +} + /// Run MM subsystem self-tests during probe. /// /// Tests page table infrastructure and `BAR1` MMIO access using the `BAR1`