From nobody Sun Feb 8 23:23:21 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 6657129E10F for ; Fri, 23 Jan 2026 23:13:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769209982; cv=none; b=NZKeuTe0TpNCLAtsyXWcWwvHz5RdGZCxYCIxl37qDiU1bStMpDgjY942z6Aa+MPa+s3y4ASpswJcsCE92dQpMdx3x6yIpR4CLPMJKpRbQxJOZaI/6DdRnT8LChk9j4mOoMHqTt4jmGaLjq3ZvIJUZ+0c81ULUe8t7MZheWam0cY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769209982; c=relaxed/simple; bh=X/v6D+9GWLU0aUW53G8AZXIe9Sm15PvF4lpAMh5IK1c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KUJBr3DqmqFf5K4V7g8EUNg2/CGFu0ypQZghV8cOrzOQW/LRAFPwqp7YHlsm7pIqCp6FDfgeBHez4UYc6Sd9czDbwQ2jsefThEHXJoqvc3JKbBt3BZQnUmvVgDD6wtZxhBeAZRMbszpe/yInEPEIaGTpawxAscwykiNb5VLDg5A= 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=YqYKLXMN; arc=none smtp.client-ip=170.10.129.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="YqYKLXMN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769209979; 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=f+g1RZTyXYuUyYeRZUMLatjKr1duJlfQy9Oc+Brrce8=; b=YqYKLXMNJ6jTGkMaAWeSjMHg7yd1LkUrMPbgaKvj/5zIiBCnxFA3j6YO/2u85RF8g1olfa 36KEDqne8GWQKTwLuV1dXIo1DET5btTK6DrG+I7LlFUnQFRCpbhBp4+IYy84mbu5mbBIdX X5VQJPShh8s1QCPKW5SuRSHB6hkWIJU= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-459-zZK0R8oSMmmQ1oQhjEEtCA-1; Fri, 23 Jan 2026 18:12:54 -0500 X-MC-Unique: zZK0R8oSMmmQ1oQhjEEtCA-1 X-Mimecast-MFC-AGG-ID: zZK0R8oSMmmQ1oQhjEEtCA_1769209972 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0C12F19775A5; Fri, 23 Jan 2026 23:12:52 +0000 (UTC) Received: from GoldenWind.redhat.com (unknown [10.22.89.232]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1724B1958DC4; Fri, 23 Jan 2026 23:12:49 +0000 (UTC) From: Lyude Paul To: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Cc: nouveau@lists.freedesktop.org, "Miguel Ojeda" , "Simona Vetter" , "Alice Ryhl" , "Shankari Anand" , "David Airlie" , "Benno Lossin" , "Asahi Lina" , "Daniel Almeida" , "Lyude Paul" Subject: [PATCH v4 3/3] rust/drm/gem: Use DeviceContext with GEM objects Date: Fri, 23 Jan 2026 18:10:15 -0500 Message-ID: <20260123231230.248500-4-lyude@redhat.com> In-Reply-To: <20260123231230.248500-1-lyude@redhat.com> References: <20260123231230.248500-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.0 on 10.30.177.12 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 --- V4: * Add a comment to explain the Uninit DeviceContext usage in the GEM object vtable (tl;dr: the DeviceContext is meaningless here) Signed-off-by: Lyude Paul --- 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 | 3 +- rust/kernel/drm/device.rs | 18 ++++++---- rust/kernel/drm/driver.rs | 2 +- rust/kernel/drm/gem/mod.rs | 64 +++++++++++++++++++++++----------- 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 b574ee1c24587..170ffdfd92a18 100644 --- a/drivers/gpu/drm/tyr/driver.rs +++ b/drivers/gpu/drm/tyr/driver.rs @@ -177,7 +177,7 @@ fn drop(self: Pin<&mut Self>) { impl drm::Driver for TyrDriver { type Data =3D TyrData; type File =3D File; - 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 1273bf89dbd5d..00804f8c14bd4 100644 --- a/drivers/gpu/drm/tyr/gem.rs +++ b/drivers/gpu/drm/tyr/gem.rs @@ -3,6 +3,7 @@ use crate::driver::TyrDevice; use crate::driver::TyrDriver; use kernel::drm::gem; +use kernel::drm::DeviceContext; use kernel::prelude::*; =20 /// GEM Object inner driver data @@ -12,7 +13,7 @@ pub(crate) struct TyrObject {} impl gem::DriverObject for TyrObject { type Driver =3D TyrDriver; =20 - fn new(_dev: &TyrDevice, _size: usize) -> impl PinInit { + fn new(_dev: &TyrDevice, _size: usize) -> imp= l PinInit { try_pin_init!(TyrObject {}) } } diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index a1d9b0e92f3fd..8dd1508c51cec 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 b4199945db378..3af9f52f8eda4 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, // }; @@ -33,21 +38,30 @@ /// [`DriverFile`]: drm::file::DriverFile pub type DriverFile =3D drm::File<<::Driver as drm::= Driver>::File>; =20 +/// A type alias for retrieving the current [`AllocImpl`] for a given [`Dr= iverObject`]. +/// +/// [`Driver`]: drm::Driver +pub type DriverAllocImpl =3D + <::Driver as drm::Driver>::Object; + /// GEM object functions, which must be implemented by drivers. pub trait DriverObject: Sync + Send + Sized { /// Parent `Driver` for this object. 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: &::Object, _file: &DriverFi= le) -> Result { + fn open(_obj: &DriverAllocImpl, _file: &DriverFile) -> Res= ult { Ok(()) } =20 /// Close a handle to an existing object, associated with a File. - fn close(_obj: &::Object, _file: &DriverF= ile) {} + fn close(_obj: &DriverAllocImpl, _file: &DriverFile) {} } =20 /// Trait that represents a GEM object subtype @@ -73,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(), @@ -92,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() } @@ -105,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 @@ -122,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; @@ -137,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. @@ -177,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::), @@ -206,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, )?; @@ -219,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: We use `dev` for creating the GEM object, which is k= nown to be in state `Ctx` - + // ensuring that the GEM object's pointer to the DRM device is alw= ays in the same state. 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. @@ -232,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 @@ -264,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()) }; @@ -279,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 { @@ -289,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.52.0