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 722032DF68; Thu, 4 Sep 2025 12:52:31 +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=1756990353; cv=pass; b=qMr0wi1DT3vqoLR47nlyfRXf+0XvHCpXRbl/zR0TEDT3RjYVjg3+lipkW/UikEmYFrXiqb5R0YNAbFjQeJia7x7eWRtUbDnNOwK1LNg5OJ1fpo5GpeaiZcXDuytYZs0FAQsrQS6loUTDZAsryoRPz2UEGCUn9jwfgNA4nE/ikrk= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756990353; c=relaxed/simple; bh=+TT5ySWOKrVWAKONpVGSQHta0E4euLIRsgOZrE0m4U4=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc: Message-Id:References:To; b=cHN9oApa/1ZcyBZUj9WmIS2bQFoijy5f+4JXzzmLvue2puJZ9fz6NbZSK4pq+jZSlVsEq6H/c3WOfVgWkiGgGpXxkYRzas3cWEv1aJARuAVAMep9VGrmNGTNi6eIiIq7P0wVOHZDQID5SA/TXi0lL9KsNEWiVJ6UBJCa7Np+58M= 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=LF/mHckQ; 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="LF/mHckQ" ARC-Seal: i=1; a=rsa-sha256; t=1756990319; cv=none; d=zohomail.com; s=zohoarc; b=kNwweftCdsYYW2NBJCaQsWb4UkBVa5bU0hmU/LiNdP7ZsGrCGoiwq6qJwHn4PUzuZNV0VC4++iilge29IltjBuljI1t21pJFW9YNEMoNULFuKbPaLRgl44wGW5Jk/nxgEOoNfRbxSIhZ4NMYaRZtJRLPD9tPAgiZg4Fd5zqP5sY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756990319; 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=LS+WHaA6mqYgA1Bato0TNTAkG5Ennz86of9TLR85Mk0=; b=mt7tdbNsL0S7mTd+5eQZAW/dIjT1eVRJglLgN2M2Z8H1OgumKXwemPNPZBTB29LeCpKxFA5FFCaRA3aop3R7WhO1bMCCAkK3NkK9XnsRcg5F/eBClw4tvJPBvMwxNDxngSuL6u6ysf5pxV6sF7yFHBNm2Ps9ldJixJ68wEGxscQ= 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=1756990319; 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=LS+WHaA6mqYgA1Bato0TNTAkG5Ennz86of9TLR85Mk0=; b=LF/mHckQA+m+dSjpj5Iye1LGHmScXT0NdGuMNbveAL+4ZXItfRs3CmVyoCdWtYov oJMiQMv1iAFKEfeJ8pZ7tn1VYNFE68yZ5Mqpm8Ch7wEKvc+Joa++ZtxGT1ty2R3NgPm j4I9itq1YgCoG4/jXWjLcp/PPv0AyYHS5NMXoAnc= Received: by mx.zohomail.com with SMTPS id 1756990318302309.3482528512816; Thu, 4 Sep 2025 05:51:58 -0700 (PDT) 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 v3 04/14] rust: drm: gem: Support driver-private GEM object types From: Daniel Almeida In-Reply-To: <20250829224116.477990-5-lyude@redhat.com> Date: Thu, 4 Sep 2025 09:51:41 -0300 Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Asahi Lina , "open list:DRM DRIVER FOR NVIDIA GPUS [RUST]" Content-Transfer-Encoding: quoted-printable Message-Id: <856327A4-2D29-4340-984E-97E2002CCAE8@collabora.com> References: <20250829224116.477990-1-lyude@redhat.com> <20250829224116.477990-5-lyude@redhat.com> To: Lyude Paul X-Mailer: Apple Mail (2.3826.700.81) X-ZohoMailClient: External > On 29 Aug 2025, at 19:35, Lyude Paul wrote: >=20 > One of the original intents with the gem bindings was that drivers = could > specify additional gem implementations, in order to enable for driver > private gem objects. This wasn't really possible however, as up until = now > our GEM bindings have always assumed that the only GEM object we would = run > into was driver::Driver::Object - meaning that implementing another = GEM > object type would result in all of the BaseDriverObject callbacks = assuming > the wrong type. >=20 > This is a pretty easy fix though, all we need to do is specify a > BaseDriverObject in driver::Driver instead of an AllocImpl, and then = add an > associated type for AllocImpl in BaseDriverObject. That way each > BaseDriverObject has its own AllocImpl allowing it to know which type = to > provide in BaseDriverObject callbacks, and driver::Driver can simply = go > through the BaseDriverObject to its AllocImpl type in order to get = access > to ALLOC_OPS. >=20 > So, let's do this and update Nova for these changes. >=20 > Signed-off-by: Lyude Paul >=20 > --- > V4: > * Update trait bounds. This looks gnarlier then it is: >=20 > Self: AllocImpl, <-- Get the driver for this GEM = object > D: drm::Driver, <-- Get the driver's = Object, File > impl > F: drm::file::DriverFile, > O: BaseDriverObject, <-- Make sure we're the = driver's > main GEM object impl. > (don't worry, the compiler can always figure out what D, F, O are) > * Also, rename the commit. I realized I should be clearer about what = this > does so people can stop me if this isn't what was meant by private = gem > object implementations :). >=20 > Signed-off-by: Lyude Paul > --- > drivers/gpu/drm/nova/driver.rs | 8 ++++++-- > drivers/gpu/drm/nova/gem.rs | 1 + > rust/kernel/drm/device.rs | 17 ++++++++++------- > rust/kernel/drm/driver.rs | 2 +- > rust/kernel/drm/gem/mod.rs | 21 +++++++++++++-------- > 5 files changed, 31 insertions(+), 18 deletions(-) >=20 > diff --git a/drivers/gpu/drm/nova/driver.rs = b/drivers/gpu/drm/nova/driver.rs > index 91b7380f83ab4..4c252426056c5 100644 > --- a/drivers/gpu/drm/nova/driver.rs > +++ b/drivers/gpu/drm/nova/driver.rs > @@ -1,7 +1,11 @@ > // SPDX-License-Identifier: GPL-2.0 >=20 > use kernel::{ > - auxiliary, c_str, device::Core, drm, drm::gem, drm::ioctl, = prelude::*, sync::aref::ARef, > + auxiliary, c_str, > + device::Core, > + drm::{self, gem, ioctl}, > + prelude::*, > + types::ARef, > }; >=20 > use crate::file::File; > @@ -59,7 +63,7 @@ fn probe(adev: &auxiliary::Device, _info: = &Self::IdInfo) -> Result impl drm::Driver for NovaDriver { > type Data =3D NovaData; > type File =3D File; > - type Object =3D gem::Object; > + type Object =3D NovaObject; >=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 2760ba4f3450b..10e3053f1a246 100644 > --- a/drivers/gpu/drm/nova/gem.rs > +++ b/drivers/gpu/drm/nova/gem.rs > @@ -18,6 +18,7 @@ pub(crate) struct NovaObject {} >=20 > impl gem::DriverObject for NovaObject { > type Driver =3D NovaDriver; > + type Object =3D gem::Object; >=20 > fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit { > try_pin_init!(NovaObject {}) > diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs > index 3bb7c83966cf2..16cf6cb53d9a7 100644 > --- a/rust/kernel/drm/device.rs > +++ b/rust/kernel/drm/device.rs > @@ -60,6 +60,9 @@ pub struct Device { > data: T::Data, > } >=20 > +/// A type alias for referring to the [`AllocImpl`] implementation = for a DRM driver. > +type DriverAllocImpl =3D <::Object as = drm::gem::DriverObject>::Object; > + > impl Device { > const VTABLE: bindings::drm_driver =3D drm_legacy_fields! { > load: None, > @@ -70,13 +73,13 @@ impl Device { > 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, > + gem_create_object: = DriverAllocImpl::::ALLOC_OPS.gem_create_object, > + prime_handle_to_fd: = DriverAllocImpl::::ALLOC_OPS.prime_handle_to_fd, > + prime_fd_to_handle: = DriverAllocImpl::::ALLOC_OPS.prime_fd_to_handle, > + gem_prime_import: = DriverAllocImpl::::ALLOC_OPS.gem_prime_import, > + gem_prime_import_sg_table: = DriverAllocImpl::::ALLOC_OPS.gem_prime_import_sg_table, > + dumb_create: DriverAllocImpl::::ALLOC_OPS.dumb_create, > + dumb_map_offset: = DriverAllocImpl::::ALLOC_OPS.dumb_map_offset, There are now possibly infinitely many ALLOC_OPS. Is it a problem that we=E2=80=99re choosing a particular one here? > show_fdinfo: None, > fbdev_probe: None, >=20 > diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs > index dae0f4d1bbe3c..2500a61f45a6a 100644 > --- a/rust/kernel/drm/driver.rs > +++ b/rust/kernel/drm/driver.rs > @@ -103,7 +103,7 @@ pub trait Driver { > type Data: Sync + Send; >=20 > /// The type used to manage memory for this driver. > - type Object: AllocImpl; > + type Object: drm::gem::DriverObject; >=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 b27b9fbf28bbb..ec36cd9ea69ed 100644 > --- a/rust/kernel/drm/gem/mod.rs > +++ b/rust/kernel/drm/gem/mod.rs > @@ -26,16 +26,19 @@ pub trait DriverObject: Sync + Send + Sized { > /// Parent `Driver` for this object. > type Driver: drm::Driver; >=20 > + /// The GEM object type that will be passed to various callbacks. > + type Object: AllocImpl; > + > /// Create a new driver data object for a GEM object of a given = size. > 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: = &DriverFile) -> Result { > + fn open(_obj: &Self::Object, _file: &DriverFile) -> Result = { > Ok(()) > } >=20 > /// Close a handle to an existing object, associated with a File. > - fn close(_obj: &::Object, _file: = &DriverFile) {} > + fn close(_obj: &Self::Object, _file: &DriverFile) {} > } >=20 > /// Trait that represents a GEM object subtype > @@ -83,7 +86,7 @@ extern "C" fn open_callback( >=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) }; > + let obj =3D unsafe { T::Object::from_raw(raw_obj) }; >=20 > match T::open(obj, file) { > Err(e) =3D> e.to_errno(), > @@ -100,7 +103,7 @@ 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 =3D unsafe { T::Object::from_raw(raw_obj) }; >=20 > T::close(obj, file); > } > @@ -127,11 +130,12 @@ fn size(&self) -> usize { >=20 > /// Creates a new handle for the object associated with a given = `File` > /// (or returns an existing one). > - fn create_handle(&self, file: &drm::File) -> Result > + fn create_handle(&self, file: &drm::File) -> = Result > where > Self: AllocImpl, > - D: drm::Driver, > + D: drm::Driver, > F: drm::file::DriverFile, > + O: DriverObject, > { > let mut handle: u32 =3D 0; > // SAFETY: The arguments are all valid per the type = invariants. > @@ -142,11 +146,12 @@ fn create_handle(&self, file: = &drm::File) -> Result > } >=20 > /// Looks up an object by its handle for a given `File`. > - fn lookup_handle(file: &drm::File, handle: u32) -> = Result> > + fn lookup_handle(file: &drm::File, handle: u32) -> = Result> > where > Self: AllocImpl, > - D: drm::Driver, > + D: drm::Driver, > F: drm::file::DriverFile, > + O: DriverObject, > { > // SAFETY: The arguments are all valid per the type = invariants. > let ptr =3D unsafe { = bindings::drm_gem_object_lookup(file.as_raw().cast(), handle) }; > --=20 > 2.50.0 >=20