From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (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 6990A3EF0B3; Fri, 6 Feb 2026 14:08:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770386926; cv=pass; b=nkLWVlT2tdH6KcPAR/uRQpLvb1aYWDVrE6Ab+YBSZU/SJ9mlD88qZZgtKrtsMD7Q1RVdzpv4rAEjkuithltPJDv8ZbpCopDPWvQRz6iX0+KwPgjqpE5ZYbD+oTy44E95o8L1bJMF6OHgDkqScfnqMsrSCeeUEPVLHlRrqSaNrY8= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770386926; c=relaxed/simple; bh=nqsVzfppwf8LXWmHyyBgG46eF8Y/gxQa9KGiQlKBoT0=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc: Message-Id:References:To; b=Mhj2r0ujHbWZlLFlA/UShzXYGGPKl8D9PKBYeKJsqLg67Yy99kvItjdA70oPxPS/LqOj6u6wt49pUAGFFEjUv18BU8XHsPP5g9gKl4N+ZjzdUkvHSRYQZg7AW9eI86Olwm5qUlVwEZas2ayzW4B4JpPX21UahdPpfmSeou0cSbw= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b=VWJiaexE; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b="VWJiaexE" ARC-Seal: i=1; a=rsa-sha256; t=1770386916; cv=none; d=zohomail.com; s=zohoarc; b=hu3siLgJzCQ413QEuJPxtMNGEqfa8Ynr4abt4v/wDMs4AoC2p/nKYBguCsHRvOFZcIJcc94ElfVGQhkFCI+Dn3yHW6lICms/bPprk0JR6SEse651L0vaEx0nA0QE8Mbxa7ZWOeoNTxCxjZYEZd1Etw4tV+w0anLHWWq23NESE78= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1770386916; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=fjVILBM2ktsVp85wwK6bnQYSbjvZ28007Q/fc1W+zVk=; b=nD5f+EUOLe4oLCkH7kCDoDDuuydhkWG9f75RmBo1rcQhsvLNPRKNR/GDJ6VCY1/ufaWPWp5rXiTCBaH5VPuB9HRLQ6kolebYPW6J7HgjO4bfHEE/hfJpZTnHjzgKOMbWce4WXhVTdY+9Tdb2/EMr7gMoBqF75nIxb3cVi4GJ3PM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=daniel.almeida@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1770386916; s=zohomail; d=collabora.com; i=daniel.almeida@collabora.com; h=Content-Type:Mime-Version:Subject:Subject:From:From:In-Reply-To:Date:Date:Cc:Cc:Content-Transfer-Encoding:Message-Id:Message-Id:References:To:To:Reply-To; bh=fjVILBM2ktsVp85wwK6bnQYSbjvZ28007Q/fc1W+zVk=; b=VWJiaexEPUPRx9/S6lgRd0wE12s5r3SrgVUjrx5c28s0B9cVeIHpWLbRVBTUJIn0 v8nSOI+K6DMVrvc8p9mJdiYHYOLCFRAI1V1gmhlhlYl5PIcymjmTif1RhN2WHbgFrMs r6RUTeezTWPwI3amTy/nIaiffklnqppLOs3w83iY= Received: by mx.zohomail.com with SMTPS id 1770386914603927.541128059737; Fri, 6 Feb 2026 06:08:34 -0800 (PST) Content-Type: text/plain; charset=utf-8 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: [PATCH v5 4/4] rust/drm/gem: Use DeviceContext with GEM objects From: Daniel Almeida In-Reply-To: <20260131001602.2095470-5-lyude@redhat.com> Date: Fri, 6 Feb 2026 11:08:18 -0300 Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, Danilo Krummrich , nouveau@lists.freedesktop.org, Miguel Ojeda , Simona Vetter , Alice Ryhl , Shankari Anand , David Airlie , Benno Lossin , Asahi Lina Content-Transfer-Encoding: quoted-printable Message-Id: <43885D9A-4D61-43FC-9BFB-31DDB8A9446E@collabora.com> References: <20260131001602.2095470-1-lyude@redhat.com> <20260131001602.2095470-5-lyude@redhat.com> To: Lyude Paul X-Mailer: Apple Mail (2.3826.700.81) X-ZohoMailClient: External > On 30 Jan 2026, at 21:13, Lyude Paul wrote: >=20 > 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. >=20 > Signed-off-by: Lyude Paul >=20 > --- > V4: > * Add a comment to explain the Uninit DeviceContext usage in the GEM = object > vtable (tl;dr: the DeviceContext is meaningless here) >=20 > 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(-) >=20 > 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::IdInfo) -> impl PinInit impl drm::Driver for NovaDriver { > type Data =3D NovaData; > type File =3D File; > - type Object =3D gem::Object; > + 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) = -> impl 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) = -> impl 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_sg_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 = because it is required by the > + // compiler, and it is not actually used by these functions. > + 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_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 = [`DriverObject`]. > /// > /// [`Driver`]: drm::Driver > -pub type DriverAllocImpl =3D <::Driver as = drm::Driver>::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) -> = Result { > @@ -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 = IntoGEMObject>::from_raw(raw_obj) }; > + // SAFETY: > + // * `open_callback` is specified in the AllocOps structure for = `DriverObject`, 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 = IntoGEMObject>::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 = safety contract of this > // function > - unsafe { &*crate::container_of!(Opaque::cast_from(self_ptr), = Object, obj) } > + unsafe { &*crate::container_of!(Opaque::cast_from(self_ptr), = Object, 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) = -> Result > 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_gem_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 known to be in state `Ctx` - > + // ensuring that the GEM object's pointer to the DRM device = is always in the same state. I=E2=80=99d rewrite this, i.e.: they are in the same state at this = moment, because upgrading the typestate in drm::Device will not carry over to the = 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 = always 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 long 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 `Ctx` 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 = Object { > +unsafe impl AlwaysRefCounted for = Object { > 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 = Object {} >=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 >=20 Reviewed-by: Daniel Almeida