[PATCH v5 14/35] gpu: nova-core: mm: add BarMapping

From: Alok Kumar

Date: Tue Nov 24 2026 - 09:04:38 EST


From: Zhi Wang <zhiw@xxxxxxxxxx>

vGPU communication buffers need CPU access to regions within their
VramBlocks. Add BarMapping to map these regions through BAR1.

Retain the backing region and borrow BarUser and the MM mutex for the
mapping's lifetime, and unmap on drop using the retained MM dependency.

Provide an explicit unmap operation so owners can observe errors before
releasing resources. Keep the mapping and backing region on failure,
remember the first error and reject subsequent accesses. Owners must
retain failed mappings until device teardown; drop does not retry an
unmap that already failed.

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/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..243f8739dfe6 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 retains its backing VRAM region.
+#[must_use]
+pub(crate) struct BarMapping<'map, 'gpu> {
+ /// Mapping of `region`.
+ access: BarUserAccess<'map, 'gpu>,
+ mm: &'map Mutex<GpuMm<'gpu>>,
+ /// The VRAM region that is mapped.
+ region: VramRegion,
+ unmap_error: Option<Error>,
+}
+
+#[expect(dead_code)]
+impl<'map, 'gpu> BarMapping<'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<GpuMm<'gpu>>,
+ region: VramRegion,
+ writable: bool,
+ ) -> Result<Self> {
+ 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 {
+ &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<T>(&self) -> Result<Mmio<'_, T>> {
+ // 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`
--
2.53.0