From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 952E53DF00C for ; Thu, 9 Apr 2026 15:26:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775748385; cv=none; b=WFBCS8iF/O0IVPEQ9noSz/Yw2q4ASVuKPFnq2umEwysMOX/B/ljOOHjStvSi2T+nFUdRjTmgivZEacDUEvBHcKV9in44Khr30fOdeZgJ3YLIOgcwzpHP+m5yr0sb1HfiQSqas97CO2Ur8h53CcPWDD7YudMtA3VvI4wEwPV5dhw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775748385; c=relaxed/simple; bh=yxjLLW+eyiG9le+2ww647p7emAqKkJd9fk+bDVYvWVY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ncB+0DDxhl+lpkKB59zE9YIriarO8gsrhYxFsqp8JcVn72C98L4RX4iUHl/leF+3n5dxopw4LQc8RxFP3KTdt9BWb9dz1KO3Rosc5Kr0O5mqo8NE8bLTdFzmZCLAvHPQqRqtFMniyG/r3OT8N510EDFFzEwVT0XkG8q5wRD0nrw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aGUiliMP; arc=none smtp.client-ip=209.85.218.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aGUiliMP" Received: by mail-ej1-f73.google.com with SMTP id a640c23a62f3a-b97f04b44b9so144350066b.0 for ; Thu, 09 Apr 2026 08:26:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775748381; x=1776353181; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=cmCMVFn7A8FBsU7JrtIy9Z3tXezUBNiPB/QWhv76Fgw=; b=aGUiliMPHtt/y0LpnPU2uheom9LTpPwXk6nhISE7FjuHpvaZH26k78AAV1432iGluj Xl2Tptin90U/xAjDTrb5xcX299BchB28/DTHqMd7gIpzbMr96J1E/1br49HduCpT69p6 JIC5Cd9Y0QKrTIyI16sAS6UnJvrwcjpvix6MRqt/RxCHxjrENX1Wr5OoUBYgU8L5M4uy ZEHRhaPfknTiSGtmIQkE2662Srw0Hq16FiznMWqQf+mKgBT+OZFdXDtI2pxreVnVps40 JH1XcSyCTsjNZcO0a9gFZ10XGcJgIk3Rro6zC/b3+Rbrww27CNXhS6y0vVjv6AQsoDyB hDOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775748381; x=1776353181; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cmCMVFn7A8FBsU7JrtIy9Z3tXezUBNiPB/QWhv76Fgw=; b=luIUi+86HafhK0MyvytN/gHEbNHHcVG2AdWoffb021ZvhXDY+HKmNGvhMkoUsnbogr ZZfOREUoQAtQ5UgRhVbCf3AXbCiorJB0PvZARCPh0E/+A1YNTkDFchpf1RYqBaTtzef7 eHjXzvXZtjZ2LGWxUjMV+s6+MLRQWijlCgAt1RwzkArIbppBdcgwKvmv6a7svFC/gVfF q0MIn8Ehw6rwPOHFM6f0SCQQZzEVWG3GhPruH+m6aXXZuWHcARhu0jCKzTPA4LQeBK7o KJflR9r+ljqKxnkTxdvfIFUFCcRNpGzliZ44joGJC8jzhJh3+KK090LJ2XQBz0jLa9if D0XQ== X-Forwarded-Encrypted: i=1; AJvYcCUhA3uzwZE8WZUv5UlafxnXs62ym64Q+zqWC9DT5FCuzRtONJOe/yyzCkuyvxxYVrb3rqqAaPBUOg5AARU=@vger.kernel.org X-Gm-Message-State: AOJu0YyxmzzOHzOyTG3SZhzh3uXykkX48nCfin/OeFCNlZhMwPcVzKoi WdSlQcyCdDTgCiWEDxCIxUSyBALxZNWQfxLsJDQchkA+3DvRyhfrNDz1M6JW37dSqb326zaz4X+ k1H2DZoGDQWNcCP8tXQ== X-Received: from ejcqk37.prod.google.com ([2002:a17:907:7fa5:b0:b9b:ff97:4914]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:6d1d:b0:b87:7485:b4a8 with SMTP id a640c23a62f3a-b9c66ed856emr1315762866b.0.1775748380848; Thu, 09 Apr 2026 08:26:20 -0700 (PDT) Date: Thu, 09 Apr 2026 15:26:08 +0000 In-Reply-To: <20260409-gpuvm-rust-v6-0-b16e6ada7261@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260409-gpuvm-rust-v6-0-b16e6ada7261@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=8866; i=aliceryhl@google.com; h=from:subject:message-id; bh=yxjLLW+eyiG9le+2ww647p7emAqKkJd9fk+bDVYvWVY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBp18UX8ZfyFfn7kV5FI2v7kmz+lyzYgsLoVvsdo ec1Rqwau+CJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCadfFFwAKCRAEWL7uWMY5 RtKfD/9Td4e1lxNHzVDyLZ2crdh03mOpA7vi/oiLGOvuTfyMYIUm8WjP3XSS8wIKFOXeboACYv1 w3xQWohNglCVF7GQ3qjjDxjSPeSN3/mILQJS1580LkrVOGgItflxtCz/NEBvo4A8PPyKpnNAqJd MVda24R3CgHN3EGnAtJcVeYdEavX36CX/vsU8xNmVCNhHvlHtPLRbMRPlcYZBXURJRkuHvouZjn 0xTcacguzXr+eibS7bVDcKxs/CLYDFuNBKTd4grproUybNheMlYjbRg2Dn04MMMrdQklw1qtywK lkDF7QH6hMoaQP+UIDU45zTbhTFV7Y0RYjk7OrKZDuNnuzQcJzm6r8F8pug1ASi8MjsAniK/lrn kh4YApgYpm58a5c2TYkfwF+TpaPgi3xpXcNjaeA4XJjoX6GxXZzt+ToPtXZfNLMtW4jdBw+aS6T JfWaoJ7xGDsJ8hjCFBm9/xwv2+UpcK5LCW7cZ85eQ17HtPGrTEMPwXrYNeRONS0ORWNk7jGy3ki EwDf0enr5TE7TcejQZTxvaEiVNAzJmGuwz+knoRrqfAqwhxvL3LD1NpP1jj479ApuoMrB+y66v1 OXUrIGDEXlHpS7vklhgzTiXPtmphzw0ri6R9z1SSzh3s8+7jXdfnYOxkWHiwk5lvcR4gG8IZYER D6XBwRtlsUIeTCA== X-Mailer: b4 0.14.3 Message-ID: <20260409-gpuvm-rust-v6-3-b16e6ada7261@google.com> Subject: [PATCH v6 3/5] rust: gpuvm: add GpuVa struct From: Alice Ryhl To: Danilo Krummrich , Daniel Almeida Cc: Boris Brezillon , Janne Grunau , Matthew Brost , "=?utf-8?q?Thomas_Hellstr=C3=B6m?=" , Lyude Paul , Asahi Lina , Sumit Semwal , "=?utf-8?q?Christian_K=C3=B6nig?=" , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-media@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" This struct will be used to keep track of individual mapped ranges in the GPU's virtual memory. Sparse VAs are not yet supported. Co-developed-by: Asahi Lina Signed-off-by: Asahi Lina Co-developed-by: Daniel Almeida Signed-off-by: Daniel Almeida Reviewed-by: Daniel Almeida Signed-off-by: Alice Ryhl --- rust/kernel/drm/gpuvm/mod.rs | 19 ++++- rust/kernel/drm/gpuvm/va.rs | 169 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/drm/gpuvm/vm_bo.rs | 1 - 3 files changed, 185 insertions(+), 4 deletions(-) diff --git a/rust/kernel/drm/gpuvm/mod.rs b/rust/kernel/drm/gpuvm/mod.rs index 56e02b49a581..78951e8aa5d3 100644 --- a/rust/kernel/drm/gpuvm/mod.rs +++ b/rust/kernel/drm/gpuvm/mod.rs @@ -11,7 +11,10 @@ //! C header: [`include/drm/drm_gpuvm.h`](srctree/include/drm/drm_gpuvm.h) use kernel::{ - alloc::AllocError, + alloc::{ + AllocError, + Flags as AllocFlags, // + }, bindings, drm, drm::gem::IntoGEMObject, @@ -25,9 +28,13 @@ use core::{ cell::UnsafeCell, - mem::ManuallyDrop, + mem::{ + ManuallyDrop, + MaybeUninit, // + }, ops::{ Deref, + DerefMut, Range, // }, ptr::{ @@ -36,6 +43,9 @@ }, // }; +mod va; +pub use self::va::*; + mod vm_bo; pub use self::vm_bo::*; @@ -48,7 +58,7 @@ /// /// * Stored in an allocation managed by the refcount in `self.vm`. /// * Access to `data` and the gpuvm interval tree is controlled via the [`UniqueRefGpuVm`] type. -/// * Does not contain any sparse `GpuVa` instances. +/// * Does not contain any sparse [`GpuVa`] instances. #[pin_data] pub struct GpuVm { #[pin] @@ -242,6 +252,9 @@ pub trait DriverGpuVm: Sized + Send { /// The kind of GEM object stored in this GPUVM. type Object: IntoGEMObject; + /// Data stored with each [`struct drm_gpuva`](struct@GpuVa). + type VaData; + /// Data stored with each [`struct drm_gpuvm_bo`](struct@GpuVmBo). type VmBoData; } diff --git a/rust/kernel/drm/gpuvm/va.rs b/rust/kernel/drm/gpuvm/va.rs new file mode 100644 index 000000000000..227c259f7db9 --- /dev/null +++ b/rust/kernel/drm/gpuvm/va.rs @@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT + +#![expect(dead_code)] +use super::*; + +/// Represents that a range of a GEM object is mapped in this [`GpuVm`] instance. +/// +/// Does not assume that GEM lock is held. +/// +/// # Invariants +/// +/// * This is a valid `drm_gpuva` object that is resident in a [`GpuVm`] instance. +/// * It is associated with a [`GpuVmBo`]. Or in other words, it's not an +/// `gpuvm->kernel_alloc_node` and `DRM_GPUVA_SPARSE` is not set. +/// * The associated [`GpuVmBo`] is part of the GEM list. +#[repr(C)] +#[pin_data] +pub struct GpuVa { + #[pin] + inner: Opaque, + #[pin] + data: T::VaData, +} + +impl PartialEq for GpuVa { + #[inline] + fn eq(&self, other: &Self) -> bool { + core::ptr::eq(self.as_raw(), other.as_raw()) + } +} +impl Eq for GpuVa {} + +impl GpuVa { + /// Access this [`GpuVa`] from a raw pointer. + /// + /// # Safety + /// + /// * For the duration of `'a`, the pointer must reference a valid `drm_gpuva` associated with + /// a [`GpuVm`]. + /// * It must be associated with a [`GpuVmBo`]. + /// * The associated [`GpuVmBo`] is part of the GEM list. + #[inline] + pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuva) -> &'a Self { + // CAST: `drm_gpuva` is first field and `repr(C)`. + // SAFETY: The safety requirements match the invariants of `GpuVa`. + unsafe { &*ptr.cast() } + } + + /// Returns a raw pointer to underlying C value. + #[inline] + pub fn as_raw(&self) -> *mut bindings::drm_gpuva { + self.inner.get() + } + + /// Returns the address of this mapping in the GPU virtual address space. + #[inline] + pub fn addr(&self) -> u64 { + // SAFETY: The `va.addr` field of `drm_gpuva` is immutable. + unsafe { (*self.as_raw()).va.addr } + } + + /// Returns the length of this mapping. + #[inline] + pub fn length(&self) -> u64 { + // SAFETY: The `va.range` field of `drm_gpuva` is immutable. + unsafe { (*self.as_raw()).va.range } + } + + /// Returns `addr..addr+length`. + #[inline] + pub fn range(&self) -> Range { + let addr = self.addr(); + addr..addr + self.length() + } + + /// Returns the offset within the GEM object. + #[inline] + pub fn gem_offset(&self) -> u64 { + // SAFETY: The `gem.offset` field of `drm_gpuva` is immutable. + unsafe { (*self.as_raw()).gem.offset } + } + + /// Returns the GEM object. + #[inline] + pub fn obj(&self) -> &T::Object { + // SAFETY: The `gem.obj` field of `drm_gpuva` is immutable. We know that it's not null + // because this VA is associated with a `GpuVmBo`. + unsafe { ::from_raw((*self.as_raw()).gem.obj) } + } + + /// Returns the underlying [`GpuVmBo`] object that backs this [`GpuVa`]. + #[inline] + pub fn vm_bo(&self) -> &GpuVmBo { + // SAFETY: The `vm_bo` field of `drm_gpuva` is immutable. We know that it's not null + // because this VA is associated with a `GpuVmBo`. The BO is in the GEM list by the type + // invariants. + unsafe { GpuVmBo::from_raw((*self.as_raw()).vm_bo) } + } +} + +/// A pre-allocated [`GpuVa`] object. +/// +/// # Invariants +/// +/// The memory is zeroed. +pub struct GpuVaAlloc(KBox>>); + +impl GpuVaAlloc { + /// Pre-allocate a [`GpuVa`] object. + pub fn new(flags: AllocFlags) -> Result, AllocError> { + // INVARIANTS: Memory allocated with __GFP_ZERO. + Ok(GpuVaAlloc(KBox::new_uninit(flags | __GFP_ZERO)?)) + } + + /// Prepare this `drm_gpuva` for insertion into the GPUVM. + #[must_use] + pub(super) fn prepare(mut self, va_data: impl PinInit) -> *mut bindings::drm_gpuva { + let va_ptr = MaybeUninit::as_mut_ptr(&mut self.0); + // SAFETY: The `data` field is pinned. + let Ok(()) = unsafe { va_data.__pinned_init(&raw mut (*va_ptr).data) }; + KBox::into_raw(self.0).cast() + } +} + +/// A [`GpuVa`] object that has been removed. +/// +/// # Invariants +/// +/// The `drm_gpuva` is not resident in the [`GpuVm`]. +pub struct GpuVaRemoved(KBox>); + +impl GpuVaRemoved { + /// Convert a raw pointer into a [`GpuVaRemoved`]. + /// + /// # Safety + /// + /// * Must have been removed from a [`GpuVm`]. + /// * It must not be a `gpuvm->kernel_alloc_node` va. + pub(super) unsafe fn from_raw(ptr: *mut bindings::drm_gpuva) -> Self { + // SAFETY: Since it used to be a VA in a `GpuVm` and it's not a kernel_alloc_node, this + // pointer references a `GpuVa` with a valid `T::VaData`. Since it has been removed, we + // can take ownership of the allocation. + GpuVaRemoved(unsafe { KBox::from_raw(ptr.cast()) }) + } + + /// Take ownership of the VA data. + pub fn into_inner(self) -> T::VaData + where + T::VaData: Unpin, + { + KBox::into_inner(self.0).data + } +} + +impl Deref for GpuVaRemoved { + type Target = T::VaData; + fn deref(&self) -> &T::VaData { + &self.0.data + } +} + +impl DerefMut for GpuVaRemoved +where + T::VaData: Unpin, +{ + fn deref_mut(&mut self) -> &mut T::VaData { + &mut self.0.data + } +} diff --git a/rust/kernel/drm/gpuvm/vm_bo.rs b/rust/kernel/drm/gpuvm/vm_bo.rs index 65f03f93bd21..05fd7998f4bd 100644 --- a/rust/kernel/drm/gpuvm/vm_bo.rs +++ b/rust/kernel/drm/gpuvm/vm_bo.rs @@ -114,7 +114,6 @@ impl GpuVmBo { /// For the duration of `'a`, the pointer must reference a valid `drm_gpuvm_bo` associated with /// a [`GpuVm`]. The BO must also be present in the GEM list. #[inline] - #[expect(dead_code)] pub(crate) unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm_bo) -> &'a Self { // SAFETY: `drm_gpuvm_bo` is first field and `repr(C)`. unsafe { &*ptr.cast() } -- 2.53.0.1213.gd9a14994de-goog