Skip to contentSkip to conversation

rust: gpu: buddy: tie allocations to the allocator lifetime

PATCH In review Danilo Krummrich 2 participants 2 messages Nov 26 – Nov 27

Subsystems (6)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/1 rust: gpu: buddy: tie allocations to the allocator lifetime

Let the allocator's lifetime constrain the lifetime of its allocations, rather than having allocations keep the allocator alive through reference counting.

This also removes an unnecessary allocation and allows GpuBuddy to be embedded directly in driver private data.

---
Depends on [1].

[1] https://lore.kernel.org/nova-gpu/[email protected]/

---
 drivers/gpu/nova-core/driver.rs           |   2 +-
 drivers/gpu/nova-core/gpu.rs              |  51 +++++----
 drivers/gpu/nova-core/mm.rs               |   9 +-
 drivers/gpu/nova-core/mm/bar_user.rs      |  20 ++--
 drivers/gpu/nova-core/mm/pagetable/map.rs |  43 ++++----
 drivers/gpu/nova-core/mm/vmm.rs           |  12 +-
 rust/kernel/gpu/buddy.rs                  | 128 ++++++++++++----------
 7 files changed, 147 insertions(+), 118 deletions(-)
drivers/gpu/nova-core/driver.rs +1 -1
index d6bc972d47c7..f8f5d74d2742 100644
@@ -133,7 +133,7 @@ fn probe<'bound>(
 
             gpu <- Gpu::new(pdev, bar, bar1).pin_chain(|_gpu| {
                 #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
-                _gpu.run_selftests(pdev);
+                _gpu.as_ref().run_selftests(pdev);
                 Ok(())
             }),
 
drivers/gpu/nova-core/gpu.rs +32 -19

79 lines not on this page · raw message

drivers/gpu/nova-core/mm.rs +5 -4
index 7aa8906d2cb2..e3ccd248ab48 100644
@@ -82,6 +82,7 @@ pub(crate) struct GpuMm<'gpu>
     // Hide the mutex's invariant resource lifetime so `GpuMm` remains covariant.
     exists<'res>: 'gpu,
 {
+    #[pin]
     buddy: GpuBuddy,
     #[pin]
     #[uses('res: invariant)]
@@ -103,7 +104,7 @@ pub(crate) fn new(
         let vram_region = VramAddress::ZERO..total_fb_end;
 
         try_pin_init!(Self {
-            buddy: GpuBuddy::new(buddy_params)?,
+            buddy <- GpuBuddy::new(buddy_params),
             pramin <- new_mutex!(pramin::Pramin::new(bar, chipset, vram_region)?, "pramin"),
             tlb <- Tlb::new(bar),
         })
@@ -316,11 +317,11 @@ pub(crate) mod selftest {
     use super::*;
 
     /// Run MM subsystem self-tests during probe.
-    pub(crate) fn run(
+    pub(crate) fn run<'gpu>(
         dev: &device::Device<device::Bound>,
-        mm: &GpuMm<'_>,
+        mm: &'gpu GpuMm<'_>,
         usable_fb_regions: &[Range<u64>],
-        bar_user: &bar_user::BarUser<'_>,
+        bar_user: &bar_user::BarUser<'gpu>,
         bar1_pdb: u64,
         chipset: Chipset,
     ) -> Result {
drivers/gpu/nova-core/mm/bar_user.rs +10 -10
index 483129d3d0f7..31a19219d032 100644
@@ -37,7 +37,7 @@
 #[pin_data]
 pub(crate) struct BarUser<'a> {
     #[pin]
-    vmm: Mutex<Vmm>,
+    vmm: Mutex<Vmm<'a>>,
     bar1: Bar1<'a>,
 }
 
@@ -61,10 +61,10 @@ pub(crate) fn new(
     /// Map physical pages to a contiguous BAR1 virtual range.
     pub(crate) fn map(
         &self,
-        mm: &GpuMm<'_>,
+        mm: &'a GpuMm<'_>,
         pfns: &[Pfn],
         writable: bool,
-    ) -> Result<BarUserAccess<'_>> {
+    ) -> Result<BarUserAccess<'_, 'a>> {
         if pfns.is_empty() {
             return Err(EINVAL);
         }
@@ -81,15 +81,15 @@ pub(crate) fn map(
 /// Access object for a mapped BAR1 region.
 ///
 /// Borrows the [`BarUser`] managing the mapping for the lifetime of the access object.
-pub(crate) struct BarUserAccess<'a> {
-    bar_user: &'a BarUser<'a>,
+pub(crate) struct BarUserAccess<'a, 'gpu> {
+    bar_user: &'a BarUser<'gpu>,
     /// [`BarUserAccess::release`] [`Option::take`]s this; `Some` at
     /// drop time means `release()` was never called.
     mapped: Option<MappedRange>,
 }
 
 #[expect(dead_code)]
-impl BarUserAccess<'_> {
+impl BarUserAccess<'_, '_> {
     /// Tear down the BAR1 mapping.
     pub(crate) fn release(mut self, mm: &GpuMm<'_>) -> Result {
         let mapped = self.mapped.take().ok_or(EINVAL)?;
@@ -163,7 +163,7 @@ pub(crate) fn try_write64(&self, value: u64, offset: usize) -> Result {
     }
 }
 
-impl Drop for BarUserAccess<'_> {
+impl Drop for BarUserAccess<'_, '_> {
     fn drop(&mut self) {
         if self.mapped.is_some() {
             kernel::pr_warn!(
@@ -181,10 +181,10 @@ fn drop(&mut self) {
 /// address space. Uses the `GpuMm`'s buddy allocator to allocate page tables
 /// and test pages as needed.
 #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
-pub(crate) fn run_self_test(
+pub(crate) fn run_self_test<'gpu>(
     dev: &device::Device<device::Bound>,
-    mm: &GpuMm<'_>,
-    bar_user: &BarUser<'_>,
+    mm: &'gpu GpuMm<'_>,
+    bar_user: &BarUser<'gpu>,
     bar1_pdb: u64,
     chipset: Chipset,
 ) -> Result {
drivers/gpu/nova-core/mm/pagetable/map.rs +23 -20

98 lines not on this page · raw message

drivers/gpu/nova-core/mm/vmm.rs +6 -6
index c8258e579f8b..2ce6dca777ec 100644
@@ -124,7 +124,7 @@ fn drop(&mut self) {
 ///
 /// Each [`Vmm`] instance manages a single address space identified by its Page
 /// Directory Base (`PDB`) address. Used for Channel, BAR1 and BAR2 mappings.
-pub(crate) struct Vmm {
+pub(crate) struct Vmm<'gpu> {
     /// Page Directory Base address for this address space.
     #[expect(dead_code)]
     pdb_addr: VramAddress,
@@ -133,7 +133,7 @@ pub(crate) struct Vmm {
     /// Page table mapper for prepare/execute operations.
     pt_map: PtMap,
     /// Page table allocations required for mappings.
-    page_table_allocs: KVec<Pin<KBox<AllocatedBlocks>>>,
+    page_table_allocs: KVec<Pin<KBox<AllocatedBlocks<'gpu>>>>,
     /// Maple tree allocator for virtual address range tracking.
     virt_alloc: Pin<KBox<MapleTreeAlloc<()>>>,
     /// Total number of pages in the virtual address space.
@@ -142,10 +142,10 @@ pub(crate) struct Vmm {
     ///
     /// Populated during prepare phase and drained in execute phase. Shared by all
     /// pending maps, preventing races on the same PDE slot.
-    pt_pages: RBTree<VramAddress, super::pagetable::map::PreparedPtPage>,
+    pt_pages: RBTree<VramAddress, super::pagetable::map::PreparedPtPage<'gpu>>,
 }
 
-impl Vmm {
+impl<'gpu> Vmm<'gpu> {
     /// Create a new [`Vmm`] for the given Page Directory Base address.
     ///
     /// The [`Vmm`] will manage a virtual address space of `va_size` bytes.
@@ -220,7 +220,7 @@ pub(super) fn read_mapping(&self, mm: &GpuMm<'_>, vfn: Vfn) -> Result<Option<Pfn
     /// to call outside the fence signalling critical path.
     pub(crate) fn prepare_map(
         &mut self,
-        mm: &GpuMm<'_>,
+        mm: &'gpu GpuMm<'_>,
         num_pages: usize,
         va_range: Option<Range<u64>>,
     ) -> Result<PreparedMapping> {
@@ -297,7 +297,7 @@ pub(crate) fn execute_map(
     /// [`Vmm::execute_map()`] will be called separately.
     pub(crate) fn map_pages(
         &mut self,
-        mm: &GpuMm<'_>,
+        mm: &'gpu GpuMm<'_>,
         pfns: &[Pfn],
         va_range: Option<Range<u64>>,
         writable: bool,
rust/kernel/gpu/buddy.rs +70 -58

248 lines not on this page · raw message

base-commit: 2a874fc59fdeb07159b627ca7681cfe6b4cff1de

Signature · 1 line
2.56.1
reviewer
#1 lore raw
Quoted 40 lines · Danilo Krummrich

Let the allocator's lifetime constrain the lifetime of its allocations, rather than having allocations keep the allocator alive through reference counting.

This also removes an unnecessary allocation and allows GpuBuddy to be embedded directly in driver private data.

---
Depends on [1].

[1] https://lore.kernel.org/nova-gpu/[email protected]/

---
 drivers/gpu/nova-core/driver.rs           |   2 +-
 drivers/gpu/nova-core/gpu.rs              |  51 +++++----
 drivers/gpu/nova-core/mm.rs               |   9 +-
 drivers/gpu/nova-core/mm/bar_user.rs      |  20 ++--
 drivers/gpu/nova-core/mm/pagetable/map.rs |  43 ++++----
 drivers/gpu/nova-core/mm/vmm.rs           |  12 +-
 rust/kernel/gpu/buddy.rs                  | 128 ++++++++++++----------
 7 files changed, 147 insertions(+), 118 deletions(-)
drivers/gpu/nova-core/driver.rs +1 -1
index d6bc972d47c7..f8f5d74d2742 100644
@@ -133,7 +133,7 @@ fn probe<'bound>(
 
             gpu <- Gpu::new(pdev, bar, bar1).pin_chain(|_gpu| {
                 #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
-                _gpu.run_selftests(pdev);
+                _gpu.as_ref().run_selftests(pdev);
                 Ok(())
             }),
 
drivers/gpu/nova-core/gpu.rs +0 -0
index 2f0399100885..65290cedde11 100644

drivers/gpu/nova-core/gpu.rs line 6

Quoted 6 lines
drivers/gpu/nova-core/gpu.rs +4 -0
@@ -1,5 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#![expect(
+    clippy::multiple_bound_locations,
+    reason = "pin_data generates additional outlives bounds for the borrowed MM field"
+)]

Please use allow so it won't cause a semantic conflict in case I fix it this cycle.

I'll take a look of the rest later.

Best,
Gary

Quoted 17 lines
drivers/gpu/nova-core/gpu.rs +6 -1
+
 use core::ops::Range;
 
 use kernel::{
@@ -383,7 +388,11 @@ unsafe fn new(
 
 /// Structure holding the resources required to operate the GPU.
 #[pin_data]
-pub(crate) struct Gpu<'gpu> {
+pub(crate) struct Gpu<'gpu>
+where
+    // Hide the borrowed memory manager's resource lifetime so `Gpu` remains covariant.
+    exists<'gpu_mm>: 'gpu,
+{
     pub(crate) spec: Spec,
     /// GSP event interrupt registration, and the enable of its subtree.
     ///