From nobody Thu Apr 2 19:13:10 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF98E38E5C1 for ; Fri, 20 Mar 2026 23:37:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774049864; cv=none; b=l1yT3N5YrXT9QPy/yVOHyqe+kaigVp5cbadn7gVeG32zMpaZO7GXadZzW9mTDWUiNe0xwxoGKPJVHa7sxLUP6eF6UaAPzx+17LFhtnJ92fiGRsb8ccYCygeIbowNa4GQpCYSUtPX+b4DIvGi8ruDRwfHPy740FqFFVv0yMkWQOw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774049864; c=relaxed/simple; bh=69mYU2c4z3R5nunTOCeCQl0BGvwlVBQyT3TDLzmBQc8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=neOCXNzKABcwl2ESMvDuXQQB2+5eLu7r8MHIbib4ucK1FYPrHMSd2EG9U1gTICt6JNTvUbEtvHlhjug0Kf1WiZqnKGUWuXURDrbVICK83AdzYpf81znCAgmjRQLS4EDvYtFJ6PRS3uBiWE/YevRzesY6bxYGag473ep8FfZxYRU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZGEPWzRr; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZGEPWzRr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774049862; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wTujU8KlZfO8hSxf0gPug84kE4qsLXMCF+IRUSiCktA=; b=ZGEPWzRrn4A9PFOZAqd1mIBCHuT0SjEZqht+DrA0kRzc6/dhZNseSJ7h2PvutwI7xTbAwg lZpuGyj0H6dE9SPmmV3U2om9rkXTQzLBMGgMHxWc3VJYB07eC7o+1QiEIkldo7tmN37q/R NsVt7mCScp+J30mSKw2Pis1lEC8Xk80= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-304-J_YPp4UUN7ib7HmSSONqlQ-1; Fri, 20 Mar 2026 19:37:38 -0400 X-MC-Unique: J_YPp4UUN7ib7HmSSONqlQ-1 X-Mimecast-MFC-AGG-ID: J_YPp4UUN7ib7HmSSONqlQ_1774049856 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6C01818005B2; Fri, 20 Mar 2026 23:37:36 +0000 (UTC) Received: from GoldenWind.redhat.com (unknown [10.22.80.37]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 08980180075C; Fri, 20 Mar 2026 23:37:33 +0000 (UTC) From: Lyude Paul To: linux-kernel@vger.kernel.org, Danilo Krummrich , rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: Daniel Almeida , nouveau@lists.freedesktop.org, "Gary Guo" , "Miguel Ojeda" , "Alice Ryhl" , "Simona Vetter" , "Shankari Anand" , "Maxime Ripard" , "David Airlie" , "Benno Lossin" , "Asahi Lina" , "Lyude Paul" Subject: [PATCH v6 5/5] rust/drm/gem: Use DeviceContext with GEM objects Date: Fri, 20 Mar 2026 19:34:30 -0400 Message-ID: <20260320233645.950190-6-lyude@redhat.com> In-Reply-To: <20260320233645.950190-1-lyude@redhat.com> References: <20260320233645.950190-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Now that we have the ability to represent the context in which a DRM device is in at compile-time, we can start carrying around this context with GEM object types in order to allow a driver to safely create GEM objects before a DRM device has registered with userspace. Signed-off-by: Lyude Paul Reviewed-by: Daniel Almeida --- V4: * Add a comment to explain the Uninit DeviceContext usage in the GEM object vtable (tl;dr: the DeviceContext is meaningless here) V5: * Improve invariant comment above drm_gem_object_init() drivers/gpu/drm/nova/driver.rs | 2 +- drivers/gpu/drm/nova/gem.rs | 11 ++++--- drivers/gpu/drm/tyr/driver.rs | 2 +- drivers/gpu/drm/tyr/gem.rs | 10 ++++-- rust/kernel/drm/device.rs | 18 ++++++----- rust/kernel/drm/driver.rs | 2 +- rust/kernel/drm/gem/mod.rs | 57 ++++++++++++++++++++++------------ 7 files changed, 67 insertions(+), 35 deletions(-) diff --git a/drivers/gpu/drm/nova/driver.rs b/drivers/gpu/drm/nova/driver.rs index 8cea5f68c3b04..2c13261450406 100644 --- a/drivers/gpu/drm/nova/driver.rs +++ b/drivers/gpu/drm/nova/driver.rs @@ -67,7 +67,7 @@ fn probe(adev: &auxiliary::Device, _info: &Self::Id= Info) -> impl PinInit; + type Object =3D gem::Object; =20 const INFO: drm::DriverInfo =3D INFO; =20 diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs index 6ccfa5da57617..f6e98b9db58d8 100644 --- a/drivers/gpu/drm/nova/gem.rs +++ b/drivers/gpu/drm/nova/gem.rs @@ -2,7 +2,7 @@ =20 use kernel::{ drm, - drm::{gem, gem::BaseObject}, + drm::{gem, gem::BaseObject, DeviceContext}, page, prelude::*, sync::aref::ARef, @@ -20,20 +20,23 @@ pub(crate) struct NovaObject {} impl gem::DriverObject for NovaObject { type Driver =3D NovaDriver; =20 - fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit { + fn new(_dev: &NovaDevice, _size: usize) -> im= pl PinInit { try_pin_init!(NovaObject {}) } } =20 impl NovaObject { /// Create a new DRM GEM object. - pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result>> { + pub(crate) fn new( + dev: &NovaDevice, + size: usize, + ) -> Result>> { if size =3D=3D 0 { return Err(EINVAL); } let aligned_size =3D page::page_align(size).ok_or(EINVAL)?; =20 - gem::Object::new(dev, aligned_size) + gem::Object::::new(dev, aligned_size) } =20 /// Look up a GEM object handle for a `File` and return an `ObjectRef`= for it. diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs index 76c5aed1171b1..157329fd09ea7 100644 --- a/drivers/gpu/drm/tyr/driver.rs +++ b/drivers/gpu/drm/tyr/driver.rs @@ -189,7 +189,7 @@ fn drop(self: Pin<&mut Self>) { impl drm::Driver for TyrDrmDriver { type Data =3D TyrDrmDeviceData; type File =3D TyrDrmFileData; - type Object =3D drm::gem::Object; + type Object =3D drm::gem::Object; =20 const INFO: drm::DriverInfo =3D INFO; =20 diff --git a/drivers/gpu/drm/tyr/gem.rs b/drivers/gpu/drm/tyr/gem.rs index 5cd0cd9585e8d..7499c38c7cde4 100644 --- a/drivers/gpu/drm/tyr/gem.rs +++ b/drivers/gpu/drm/tyr/gem.rs @@ -1,7 +1,10 @@ // SPDX-License-Identifier: GPL-2.0 or MIT =20 use kernel::{ - drm::gem, + drm::{ + gem, + DeviceContext, // + }, prelude::*, // }; =20 @@ -17,7 +20,10 @@ pub(crate) struct TyrObject {} impl gem::DriverObject for TyrObject { type Driver =3D TyrDrmDriver; =20 - fn new(_dev: &TyrDrmDevice, _size: usize) -> impl PinInit= { + fn new( + _dev: &TyrDrmDevice, + _size: usize, + ) -> impl PinInit { try_pin_init!(TyrObject {}) } } diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index 64b1089ba00d4..9842556efd8f8 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -163,13 +163,17 @@ impl UnregisteredDevice { master_set: None, master_drop: None, debugfs_init: None, - gem_create_object: T::Object::ALLOC_OPS.gem_create_object, - prime_handle_to_fd: T::Object::ALLOC_OPS.prime_handle_to_fd, - prime_fd_to_handle: T::Object::ALLOC_OPS.prime_fd_to_handle, - gem_prime_import: T::Object::ALLOC_OPS.gem_prime_import, - gem_prime_import_sg_table: T::Object::ALLOC_OPS.gem_prime_import_s= g_table, - dumb_create: T::Object::ALLOC_OPS.dumb_create, - dumb_map_offset: T::Object::ALLOC_OPS.dumb_map_offset, + + // Ignore the Uninit DeviceContext below. It is only provided beca= use it is required by the + // compiler, and it is not actually used by these functions. + gem_create_object: T::Object::::ALLOC_OPS.gem_create_objec= t, + prime_handle_to_fd: T::Object::::ALLOC_OPS.prime_handle_to= _fd, + prime_fd_to_handle: T::Object::::ALLOC_OPS.prime_fd_to_han= dle, + gem_prime_import: T::Object::::ALLOC_OPS.gem_prime_import, + gem_prime_import_sg_table: T::Object::::ALLOC_OPS.gem_prim= e_import_sg_table, + dumb_create: T::Object::::ALLOC_OPS.dumb_create, + dumb_map_offset: T::Object::::ALLOC_OPS.dumb_map_offset, + show_fdinfo: None, fbdev_probe: None, =20 diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs index cfb8884ece700..e6893f089733d 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -110,7 +110,7 @@ pub trait Driver { type Data: Sync + Send; =20 /// The type used to manage memory for this driver. - type Object: AllocImpl; + type Object: AllocImpl; =20 /// The type used to represent a DRM File (client) type File: drm::file::DriverFile; diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index ad74c5159f725..1bae623d74492 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -8,6 +8,10 @@ bindings, drm::{ self, + device::{ + DeviceContext, + Registered, // + }, driver::{ AllocImpl, AllocOps, // @@ -22,6 +26,7 @@ types::Opaque, }; use core::{ + marker::PhantomData, ops::Deref, ptr::NonNull, // }; @@ -36,7 +41,8 @@ /// A type alias for retrieving the current [`AllocImpl`] for a given [`Dr= iverObject`]. /// /// [`Driver`]: drm::Driver -pub type DriverAllocImpl =3D <::Driver as drm::Drive= r>::Object; +pub type DriverAllocImpl =3D + <::Driver as drm::Driver>::Object; =20 /// GEM object functions, which must be implemented by drivers. pub trait DriverObject: Sync + Send + Sized { @@ -44,7 +50,10 @@ pub trait DriverObject: Sync + Send + Sized { type Driver: drm::Driver; =20 /// Create a new driver data object for a GEM object of a given size. - fn new(dev: &drm::Device, size: usize) -> impl PinInit; + fn new( + dev: &drm::Device, + size: usize, + ) -> impl PinInit; =20 /// Open a new handle to an existing object, associated with a File. fn open(_obj: &DriverAllocImpl, _file: &DriverFile) -> Res= ult { @@ -78,9 +87,12 @@ extern "C" fn open_callback( // SAFETY: `open_callback` is only ever called with a valid pointer to= a `struct drm_file`. let file =3D unsafe { DriverFile::::from_raw(raw_file) }; =20 - // SAFETY: `open_callback` is specified in the AllocOps structure for = `DriverObject`, - // ensuring that `raw_obj` is contained within a `DriverObject` - let obj =3D unsafe { <::Object as IntoGEMObj= ect>::from_raw(raw_obj) }; + // SAFETY: + // * `open_callback` is specified in the AllocOps structure for `Drive= rObject`, ensuring that + // `raw_obj` is contained within a `DriverAllocImpl` + // * It is only possible for `open_callback` to be called after device= registration, ensuring + // that the object's device is in the `Registered` state. + let obj: &DriverAllocImpl =3D unsafe { IntoGEMObject::from_raw(raw_= obj) }; =20 match T::open(obj, file) { Err(e) =3D> e.to_errno(), @@ -97,12 +109,12 @@ extern "C" fn close_callback( =20 // SAFETY: `close_callback` is specified in the AllocOps structure for= `Object`, ensuring // that `raw_obj` is indeed contained within a `Object`. - let obj =3D unsafe { <::Object as IntoGEMObj= ect>::from_raw(raw_obj) }; + let obj: &DriverAllocImpl =3D unsafe { IntoGEMObject::from_raw(raw_= obj) }; =20 T::close(obj, file); } =20 -impl IntoGEMObject for Object { +impl IntoGEMObject for Object= { fn as_raw(&self) -> *mut bindings::drm_gem_object { self.obj.get() } @@ -110,7 +122,7 @@ fn as_raw(&self) -> *mut bindings::drm_gem_object { unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a= Self { // SAFETY: `obj` is guaranteed to be in an `Object` via the saf= ety contract of this // function - unsafe { &*crate::container_of!(Opaque::cast_from(self_ptr), Objec= t, obj) } + unsafe { &*crate::container_of!(Opaque::cast_from(self_ptr), Objec= t, obj) } } } =20 @@ -127,7 +139,7 @@ fn size(&self) -> usize { fn create_handle(&self, file: &drm::File) -> Result where Self: AllocImpl, - D: drm::Driver, + D: drm::Driver =3D Self, File =3D F>, F: drm::file::DriverFile, { let mut handle: u32 =3D 0; @@ -142,7 +154,7 @@ fn create_handle(&self, file: &drm::File) -> R= esult fn lookup_handle(file: &drm::File, handle: u32) -> Result> where Self: AllocImpl, - D: drm::Driver, + D: drm::Driver =3D Self, File =3D F>, F: drm::file::DriverFile, { // SAFETY: The arguments are all valid per the type invariants. @@ -182,16 +194,18 @@ impl BaseObject for T {} /// /// # Invariants /// -/// - `self.obj` is a valid instance of a `struct drm_gem_object`. +/// * `self.obj` is a valid instance of a `struct drm_gem_object`. +/// * Any type invariants of `Ctx` apply to the parent DRM device for this= GEM object. #[repr(C)] #[pin_data] -pub struct Object { +pub struct Object { obj: Opaque, #[pin] data: T, + _ctx: PhantomData, } =20 -impl Object { +impl Object { const OBJECT_FUNCS: bindings::drm_gem_object_funcs =3D bindings::drm_g= em_object_funcs { free: Some(Self::free_callback), open: Some(open_callback::), @@ -211,11 +225,12 @@ impl Object { }; =20 /// Create a new GEM object. - pub fn new(dev: &drm::Device, size: usize) -> Result> { + pub fn new(dev: &drm::Device, size: usize) -> Result> { let obj: Pin> =3D KBox::pin_init( try_pin_init!(Self { obj: Opaque::new(bindings::drm_gem_object::default()), data <- T::new(dev, size), + _ctx: PhantomData, }), GFP_KERNEL, )?; @@ -224,6 +239,8 @@ pub fn new(dev: &drm::Device, size: usize) -= > Result> { unsafe { (*obj.as_raw()).funcs =3D &Self::OBJECT_FUNCS }; =20 // SAFETY: The arguments are all valid per the type invariants. + // INVARIANT: `dev` and the GEM object are in the same state at th= e moment, and upgrading + // the typestate in `dev` will not carry over to the GEM object. to_result(unsafe { bindings::drm_gem_object_init(dev.as_raw(), obj= .obj.get(), size) })?; =20 // SAFETY: We will never move out of `Self` as `ARef` is alw= ays treated as pinned. @@ -237,13 +254,15 @@ pub fn new(dev: &drm::Device, size: usize)= -> Result> { } =20 /// Returns the `Device` that owns this GEM object. - pub fn dev(&self) -> &drm::Device { + pub fn dev(&self) -> &drm::Device { // SAFETY: // - `struct drm_gem_object.dev` is initialized and valid for as l= ong as the GEM // object lives. // - The device we used for creating the gem object is passed as &= drm::Device to // Object::::new(), so we know that `T::Driver` is the right = generic parameter to use // here. + // - Any type invariants of `Ctx` are upheld by using the same `Ct= x` for the `Device` we + // return. unsafe { drm::Device::from_raw((*self.as_raw()).dev) } } =20 @@ -269,7 +288,7 @@ extern "C" fn free_callback(obj: *mut bindings::drm_gem= _object) { } =20 // SAFETY: Instances of `Object` are always reference-counted. -unsafe impl crate::sync::aref::AlwaysRefCounted for Objec= t { +unsafe impl AlwaysRefCounted for Obje= ct { fn inc_ref(&self) { // SAFETY: The existence of a shared reference guarantees that the= refcount is non-zero. unsafe { bindings::drm_gem_object_get(self.as_raw()) }; @@ -284,9 +303,9 @@ unsafe fn dec_ref(obj: NonNull) { } } =20 -impl super::private::Sealed for Object {} +impl super::private::Sealed for Objec= t {} =20 -impl Deref for Object { +impl Deref for Object { type Target =3D T; =20 fn deref(&self) -> &Self::Target { @@ -294,7 +313,7 @@ fn deref(&self) -> &Self::Target { } } =20 -impl AllocImpl for Object { +impl AllocImpl for Object { type Driver =3D T::Driver; =20 const ALLOC_OPS: AllocOps =3D AllocOps { --=20 2.53.0