Re: [PATCH] rust: gpu: buddy: tie allocations to the allocator lifetime

From: Gary Guo

Date: Thu Nov 26 2026 - 19:09:30 EST


On Thu Nov 26, 2026 at 10:11 PM GMT, Danilo Krummrich wrote:
> 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.
>
> Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
> ---
> Depends on [1].
>
> [1] https://lore.kernel.org/nova-gpu/20261126213652.3716162-1-dakr@xxxxxxxxxx/
> ---
> 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(-)
>
> diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs
> index d6bc972d47c7..f8f5d74d2742 100644
> --- a/drivers/gpu/nova-core/driver.rs
> +++ b/drivers/gpu/nova-core/driver.rs
> @@ -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(())
> }),
>
> diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs
> index 2f0399100885..65290cedde11 100644
> --- a/drivers/gpu/nova-core/gpu.rs
> +++ b/drivers/gpu/nova-core/gpu.rs
> @@ -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

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