From nobody Fri Oct 3 14:41:44 2025 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 761212D9485 for ; Fri, 29 Aug 2025 22:42:39 +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=1756507361; cv=none; b=s36ySlxU++FVcwLhOBVPoqdVoBGI5/TjOqtLGjRb7gUbMw4FN5DAnx50D37WtFZ+bxELeCuNUEgwBsomOAd64HoEfedtxZFooOOETstG83zzIMT/pEy12lj8lLrK7mRaQPIUITEUyBpISh4Mh3iIvyon4IYxZv+GEGWg5AqG9r0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756507361; c=relaxed/simple; bh=EtUsR3n0ijhYX7N98y4g5s32rGilNcHAFZRsCaZ/L7w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YF0tWXbGHui2KI9clLQQvVEKoUY86sE5AzdaFYSGyatSmRp+fKJnfBEWfEbRyZUa1aU/kgFLpS+S1x+876f4HOXcQCOoqGiRJyfhkRXjoE8si7D+/XvMkHewmZGViCLn77jBzFxbIOs1SEI2d8qyN3D+8O+hIuStR7+ZwW4o22A= 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=WgpYD7g3; 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="WgpYD7g3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1756507358; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8XT8+KxxRjgfrj31cXlfL66GVxfKzDgbyTpTZPzgyGw=; b=WgpYD7g3lGtBqrfg51gsAUbGG47NIaTgr1hgHYhO80KdKNzx/Qo/CviLMaYogCtnwzpJTs TGIyehPRk1/01ejGyIk7XUXFQWw0ImvY14VdDe0TsNCBBApHSfGQZXDjdVvpOadtKOIlkc qKOOpFZCZn2zeCZiB5onrZX0klkVyEQ= Received: from mx-prod-mc-03.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-687-gL577qKcMpaQEaDutDZZkw-1; Fri, 29 Aug 2025 18:42:34 -0400 X-MC-Unique: gL577qKcMpaQEaDutDZZkw-1 X-Mimecast-MFC-AGG-ID: gL577qKcMpaQEaDutDZZkw_1756507351 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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BDC9F19560AA; Fri, 29 Aug 2025 22:42:31 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.80.78]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 234BC1800280; Fri, 29 Aug 2025 22:42:26 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: 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=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Daniel Almeida , Asahi Lina , nouveau@lists.freedesktop.org (open list:DRM DRIVER FOR NVIDIA GPUS [RUST]) Subject: [PATCH v3 09/14] rust: gem: Introduce DriverObject::Args Date: Fri, 29 Aug 2025 18:35:24 -0400 Message-ID: <20250829224116.477990-10-lyude@redhat.com> In-Reply-To: <20250829224116.477990-1-lyude@redhat.com> References: <20250829224116.477990-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 This is an associated type that may be used in order to specify a data-type to pass to gem objects when construction them, allowing for drivers to more easily initialize their private-data for gem objects. Signed-off-by: Lyude Paul --- V3: * s/BaseDriverObject/DriverObject/ Signed-off-by: Lyude Paul --- drivers/gpu/drm/nova/gem.rs | 5 ++- rust/kernel/drm/gem/mod.rs | 75 +++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs index 10e3053f1a246..015cb56061a56 100644 --- a/drivers/gpu/drm/nova/gem.rs +++ b/drivers/gpu/drm/nova/gem.rs @@ -19,8 +19,9 @@ pub(crate) struct NovaObject {} impl gem::DriverObject for NovaObject { type Driver =3D NovaDriver; type Object =3D gem::Object; + type Args =3D (); =20 - fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit { + fn new(_dev: &NovaDevice, _size: usize, _args: Self::Args) -> impl Pin= Init { try_pin_init!(NovaObject {}) } } @@ -34,7 +35,7 @@ pub(crate) fn new(dev: &NovaDevice, size: usize) -> Resul= t =3D drm::File<<::Driver as drm::= Driver>::File>; =20 +/// A helper macro for implementing AsRef> +macro_rules! impl_as_opaque { + ($type:ty where $tparam:ident : $tparam_trait:ident) =3D> { + impl core::convert::AsRef> for $type + where + D: kernel::drm::driver::Driver, + Self: kernel::drm::gem::DriverObject, + Self: kernel::drm::gem::IntoGEMObject, + $tparam: $tparam_trait, + { + fn as_ref(&self) -> &kernel::drm::gem::OpaqueObject { + // SAFETY: This cast is safe via our type invariant. + unsafe { &*((self.as_raw().cast_const()).cast()) } + } + } + }; +} + +pub(crate) use impl_as_opaque; + /// GEM object functions, which must be implemented by drivers. pub trait DriverObject: Sync + Send + Sized { /// Parent `Driver` for this object. @@ -29,8 +50,15 @@ pub trait DriverObject: Sync + Send + Sized { /// The GEM object type that will be passed to various callbacks. type Object: AllocImpl; =20 + /// The data type to use for passing arguments to [`BaseDriverObject::= new`]. + type Args; + /// 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, + args: Self::Args, + ) -> impl PinInit; =20 /// Open a new handle to an existing object, associated with a File. fn open(_obj: &Self::Object, _file: &DriverFile) -> Result { @@ -42,7 +70,7 @@ fn close(_obj: &Self::Object, _file: &DriverFile) {} } =20 /// Trait that represents a GEM object subtype -pub trait IntoGEMObject: Sized + super::private::Sealed + AlwaysRefCounted= { +pub trait IntoGEMObject: Sized + Sealed + AlwaysRefCounted { /// Returns a reference to the raw `drm_gem_object` structure, which m= ust be valid as long as /// this owning object is valid. fn as_raw(&self) -> *mut bindings::drm_gem_object; @@ -233,11 +261,11 @@ 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, args: T::Args) -= > 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), + data <- T::new(dev, size, args), // INVARIANT: The drm subsystem guarantees that the `struc= t drm_device` will live // as long as the GEM object lives. dev: dev.into(), @@ -289,7 +317,7 @@ extern "C" fn free_callback(obj: *mut bindings::drm_gem= _object) { } } =20 -impl super::private::Sealed for Object {} +impl Sealed for Object {} =20 impl Deref for Object { type Target =3D T; @@ -313,6 +341,39 @@ impl AllocImpl for Object { }; } =20 +impl_as_opaque!(Object where T: DriverObject); + +/// A GEM object whose private-data layout is not known. +/// +/// Not all GEM objects are created equal, and subsequently drivers may oc= casionally need to deal +/// with situations where they are working with a GEM object but have no k= nowledge of its +/// private-data layout. +/// +/// It may be used just like a normal [`Object`], with the exception that = it cannot access +/// driver-private data. +/// +/// # Invariant +/// +/// Via `#[repr(transparent)]`, this type is guaranteed to have an identic= al data layout to +/// `struct drm_gem_object`. +#[repr(transparent)] +pub struct OpaqueObject(Opaque, = PhantomData); + +impl IntoGEMObject for OpaqueObject { + unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a= Self { + // SAFETY: + // - This cast is safe via our type invariant. + // - `self_ptr` is guaranteed to be a valid pointer to a gem objec= t by our safety contract. + unsafe { &*self_ptr.cast::().cast_const() } + } + + fn as_raw(&self) -> *mut bindings::drm_gem_object { + self.0.get() + } +} + +impl Sealed for OpaqueObject {} + pub(super) const fn create_fops() -> bindings::file_operations { // SAFETY: As by the type invariant, it is safe to initialize `binding= s::file_operations` // zeroed. --=20 2.50.0