From nobody Mon Feb 9 05:22:24 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 29B102BE7B0 for ; Wed, 21 May 2025 20:48: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=1747860531; cv=none; b=o3JGbVvl+FNPM1Gf3M9XDm+d7fWpT0/kr//pEo9vY2iHTHOWJv/rPzn+HIE60V22JND5/NN2TCKFI1lKqyONiLB/CZ3pBs439Fm2apOQiM0CUEyVv9wQmHu64CGPX8tBMKtVO22oYPV/v5/ZcEkHZl6i0WMw1zk4zjUxU2s5mb0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747860531; c=relaxed/simple; bh=Dw0lhtTBS6OW9ZQ2YGyoktsM6mdieP2QrcPR4fqFA6I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tIVL5lYLv0BN4SkU41eCkhIMubxM1loH/uS7Kc/GIXUR5dLXIsn8D+sCPxciT73gjDzN1ILK2qhudDAOkt2BsIlyF2HzLG73aTectYe6gH650dUlx/4BaINro+USaZMdOVgghCvV6XXeoKqvxakIM87UoQva7dKT82gyPk38t+g= 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=PLjUeNoZ; 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="PLjUeNoZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747860529; 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=0Xzufaa4aZgtFIYzj8zYYCFRxQVb/zwFe5HxzSZJWxY=; b=PLjUeNoZ+p2BvYEiMaeUiPmOM2xa7Zq7QPZG6/Pel4pOqKRfiyVWxdKSv8Ns35lYYB3Xvi /Kk1S29uF/Ym5Ry0Vigbozb5vitOsMzlmO4Sbdq6LHrLDBXzd6vox7mpYkhZYFXFnGsjOE Lmlq/JlhLDG/7snbVJopQHG9CmRA29k= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-641-cHEFoZDJMu2ztuwO7P5b-Q-1; Wed, 21 May 2025 16:48:45 -0400 X-MC-Unique: cHEFoZDJMu2ztuwO7P5b-Q-1 X-Mimecast-MFC-AGG-ID: cHEFoZDJMu2ztuwO7P5b-Q_1747860520 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A474D180036E; Wed, 21 May 2025 20:48:40 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.80.100]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AED9F19560B7; Wed, 21 May 2025 20:48:35 +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 , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Daniel Almeida , Asahi Lina , Alyssa Rosenzweig , nouveau@lists.freedesktop.org (open list:DRM DRIVER FOR NVIDIA GPUS [RUST]), linux-media@vger.kernel.org (open list:DMA BUFFER SHARING FRAMEWORK:Keyword:\bdma_(?:buf|fence|resv)\b), linaro-mm-sig@lists.linaro.org (moderated list:DMA BUFFER SHARING FRAMEWORK:Keyword:\bdma_(?:buf|fence|resv)\b) Subject: [PATCH v2 11/12] rust: drm: gem: Add export() callback Date: Wed, 21 May 2025 16:29:18 -0400 Message-ID: <20250521204654.1610607-12-lyude@redhat.com> In-Reply-To: <20250521204654.1610607-1-lyude@redhat.com> References: <20250521204654.1610607-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.40 Content-Type: text/plain; charset="utf-8" This introduces an optional export() callback for GEM objects, which is used to implement the drm_gem_object_funcs->export function. Signed-off-by: Lyude Paul --- drivers/gpu/drm/nova/gem.rs | 1 + rust/kernel/drm/gem/mod.rs | 73 +++++++++++++++++++++++++++++++++++- rust/kernel/drm/gem/shmem.rs | 6 ++- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs index f2f23320110dd..6cc7a65a50fae 100644 --- a/drivers/gpu/drm/nova/gem.rs +++ b/drivers/gpu/drm/nova/gem.rs @@ -16,6 +16,7 @@ #[pin_data] pub(crate) struct NovaObject {} =20 +#[vtable] impl gem::BaseDriverObject for NovaObject { type Driver =3D NovaDriver; type Object =3D gem::Object; diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index 6d87e75690d2f..ef697f323e52c 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -10,7 +10,8 @@ alloc::flags::*, bindings, drm::{self, private::Sealed}, drm::driver::{AllocImpl, AllocOps}, - error::{to_result, Result}, + dma_buf, + error::{to_result, from_err_ptr, Result}, prelude::*, types::{ARef, AlwaysRefCounted, Opaque}, }; @@ -44,6 +45,7 @@ fn as_ref(&self) -> &kernel::drm::gem::OpaqueObject { pub(crate) use impl_as_opaque; =20 /// GEM object functions, which must be implemented by drivers. +#[vtable] pub trait BaseDriverObject: Sync + Send + Sized { /// Parent `Driver` for this object. type Driver: drm::Driver; @@ -68,6 +70,11 @@ fn open(_obj: &Self::Object, _file: &DriverFile) -= > Result { =20 /// Close a handle to an existing object, associated with a File. fn close(_obj: &Self::Object, _file: &DriverFile) {} + + /// Optional handle for exporting a gem object. + fn export(_obj: &Self::Object, _flags: u32) -> Result> { + unimplemented!() + } } =20 /// Trait that represents a GEM object subtype @@ -137,6 +144,21 @@ extern "C" fn close_callback( T::close(obj, file); } =20 +extern "C" fn export_callback( + raw_obj: *mut bindings::drm_gem_object, + flags: i32, +) -> *mut bindings::dma_buf { + // SAFETY: `export_callback` is specified in the AllocOps structure fo= r `Object`, ensuring + // that `raw_obj` is contained within a `Object`. + let obj =3D unsafe { T::Object::as_ref(raw_obj) }; + + match T::export(obj, flags as _) { + // DRM takes a hold of the reference + Ok(buf) =3D> buf.into_raw(), + Err(e) =3D> e.to_ptr(), + } +} + impl IntoGEMObject for Object { fn as_raw(&self) -> *mut bindings::drm_gem_object { self.obj.get() @@ -247,7 +269,11 @@ impl Object { open: Some(open_callback::), close: Some(close_callback::), print_info: None, - export: None, + export: if T::HAS_EXPORT { + Some(export_callback::) + } else { + None + }, pin: None, unpin: None, get_sg_table: None, @@ -376,6 +402,49 @@ fn as_raw(&self) -> *mut bindings::drm_gem_object { =20 impl Sealed for OpaqueObject {} =20 +/// A [`dma_buf::DmaBuf`] which has been exported from a GEM object. +/// +/// The [`dma_buf::DmaBuf`] will be released when this type is dropped. +/// +/// # Invariants +/// +/// - `self.0` points to a valid initialized [`dma_buf::DmaBuf`] for the l= ifetime of this object. +/// - The GEM object from which this [`dma_buf::DmaBuf`] was exported from= is guaranteed to be of +/// type `T`. +pub struct DmaBuf(NonNull, PhantomData<= T>); + +impl Deref for DmaBuf { + type Target =3D dma_buf::DmaBuf; + + #[inline] + fn deref(&self) -> &Self::Target { + // SAFETY: This pointer is guaranteed to be valid by our type inva= riants. + unsafe { self.0.as_ref() } + } +} + +impl Drop for DmaBuf { + #[inline] + fn drop(&mut self) { + // SAFETY: + // - `dma_buf::DmaBuf` is guaranteed to have an identical layout t= o `struct dma_buf` + // by its type invariants. + // - We hold the last reference to this `DmaBuf`, making it safe t= o destroy. + unsafe { bindings::drm_gem_dmabuf_release(self.0.cast().as_ptr()) } + } +} + +impl DmaBuf { + /// Leak the reference for this [`DmaBuf`] and return a raw pointer to= it. + #[inline] + pub(crate) fn into_raw(self) -> *mut bindings::dma_buf { + let dma_ptr =3D self.as_raw(); + + core::mem::forget(self); + dma_ptr + } +} + 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. diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs index bff038df93334..799f15c38cc36 100644 --- a/rust/kernel/drm/gem/shmem.rs +++ b/rust/kernel/drm/gem/shmem.rs @@ -77,7 +77,11 @@ impl Object { open: Some(super::open_callback::), close: Some(super::close_callback::), print_info: Some(bindings::drm_gem_shmem_object_print_info), - export: None, + export: if T::HAS_EXPORT { + Some(super::export_callback::) + } else { + None + }, pin: Some(bindings::drm_gem_shmem_object_pin), unpin: Some(bindings::drm_gem_shmem_object_unpin), get_sg_table: Some(bindings::drm_gem_shmem_object_get_sg_table), --=20 2.49.0