From nobody Sat Feb 7 11:39:08 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 C4960283686 for ; Sat, 31 Jan 2026 00:16:49 +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=1769818611; cv=none; b=ZVAIFeJ0O6PKQLevXE+fnyyN7BAhXLujyh1HevDA3oD8SKZeC/HxFbBF4nUWEYJLoOJYicCAdfaRHsM+VfQrLVYCKZVZZfRIwQMWINztFFYhipORCITOvW+tRUJ0SlI3V75cLyFHma+UvsOPEKa5/RLc6+qEjrcPLG0n3o+oVzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769818611; c=relaxed/simple; bh=siHhh36pFz9NsQpVGSSGcaaIuscNSJTlYt1Tt7agRIg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gzn3svphErkStV/RfAk60ZpXLu2U9zWSBDwZd4y+i2DCZ4rggfYfvMMlAu36vrX0ZZq6v4NjfHrNQT4M3bNlz9kbnV6pm3P2kENLNVnWKBTRNiVAqL3r0/vcvqyCUzmQYT7pkeduIrarztPp6+/A8HGeh4ZFmgfZmQrESEelRpo= 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=XFJ7ujKV; 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="XFJ7ujKV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769818608; 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=da0FFapy5iIe6XBCdsDajk4OHLVDoI629hj6tPRAAUQ=; b=XFJ7ujKVAGyK1TrVLhzRovkUysykP+u4msWAfCCW0u7KcObxpUkF4zFSs4mDh1mFHDg4SA KRBeOjC9trGsbFJJWxg3wJZge1iBY14vo5F+PqeN9diRI0siwYugUByQFC05ZD2+AuODQ5 sSzssq8pWLSx6z/wFV9azujWI5RmK6Y= Received: from mx-prod-mc-01.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-255-Jl480zMyM66vD7GhhSfpSg-1; Fri, 30 Jan 2026 19:16:45 -0500 X-MC-Unique: Jl480zMyM66vD7GhhSfpSg-1 X-Mimecast-MFC-AGG-ID: Jl480zMyM66vD7GhhSfpSg_1769818603 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 537321956054; Sat, 31 Jan 2026 00:16:43 +0000 (UTC) Received: from GoldenWind.lan (unknown [10.22.81.64]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7D889180086E; Sat, 31 Jan 2026 00:16:41 +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 v5 4/4] rust/drm/gem: Use DeviceContext with GEM objects Date: Fri, 30 Jan 2026 19:13:33 -0500 Message-ID: <20260131001602.2095470-5-lyude@redhat.com> In-Reply-To: <20260131001602.2095470-1-lyude@redhat.com> References: <20260131001602.2095470-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.111 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) 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 | 57 ++++++++++++++++++++++------------ 7 files changed, 61 insertions(+), 34 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 ad74c5159f725..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, // }; @@ -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: 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. @@ -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.52.0