From nobody Fri Oct 3 14:42: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 ECCA62DA757 for ; Fri, 29 Aug 2025 22:43:18 +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=1756507400; cv=none; b=eFKMTwyX0O98XxkVx0M9TR15234P14EsbynRpMFGXldrK5bdck4tU/SYrvp2AuiysCMo91obGsgwo9t3aYwO+HYcKHhxhtIj2SJ4IqH1pQDhaPoiP3Pwdbbum8k/gTajWgj3S/KwlmKZDSkgsDQhnPVVH3G88hVbB4wnqkt53Bw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756507400; c=relaxed/simple; bh=N8kxUBPd55YKdu/xSoioFdUgZRLBsIqnkmLbhSBJ8zI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XDG10mxIJ5aEti+BQtFXxEPWs4VE5Wht1v0wlfzlcziq18naTNQrwhh1jZnsPk7ITUniPZlYA15QWsPf2ysNAI0bRPZnc9mN5ToUBmlDCWh7PUT8oimviqSpuFJMJj9x5KSQJXIrd1uy1vWPytjQ/l5l8PztDZv6tKu5a4J507E= 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=J6b09Qd7; 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="J6b09Qd7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1756507397; 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=8eKMZA2jSOMS+3g9kP9wxgZR4GNqJmM76RSX3YLcOg0=; b=J6b09Qd7JGURrQEQsAoWZamfeZSy5MvRD0AliYL/a+uH04GXziGBCh7kGre2CBSfa9QcPp NL1ZM80YeFkg5OW0hHiCTaS/9Yvuv6YRNN6F9ZWp1WF8frk2JC6i7fHATl7uxGmoc2ltf2 zg/DyvpRKRhUmicjsF6GSukCywQDOpc= Received: from mx-prod-mc-05.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-251-cwbB1E0PNcKyrTfAt0tiVQ-1; Fri, 29 Aug 2025 18:43:16 -0400 X-MC-Unique: cwbB1E0PNcKyrTfAt0tiVQ-1 X-Mimecast-MFC-AGG-ID: cwbB1E0PNcKyrTfAt0tiVQ_1756507393 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0BF75195E914; Fri, 29 Aug 2025 22:43:13 +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 2C6BC1800280; Fri, 29 Aug 2025 22:43:06 +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 , 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 v3 13/14] rust: drm: gem: Add export() callback Date: Fri, 29 Aug 2025 18:35:28 -0400 Message-ID: <20250829224116.477990-14-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 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 | 72 +++++++++++++++++++++++++++++++++++- rust/kernel/drm/gem/shmem.rs | 6 ++- 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs index 015cb56061a56..bbce6b0f4e6a4 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::DriverObject 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 f9f9727f14e4a..1ac25fc6d527b 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -8,7 +8,7 @@ =20 use crate::{ alloc::flags::*, - bindings, + bindings, dma_buf, drm::driver::{AllocImpl, AllocOps}, drm::{self, private::Sealed}, error::{to_result, Result}, @@ -45,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 DriverObject: Sync + Send + Sized { /// Parent `Driver` for this object. type Driver: drm::Driver; @@ -69,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 @@ -138,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::from_raw(raw_obj) }; + + match T::export(obj, flags as u32) { + // 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() @@ -248,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, @@ -375,6 +400,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 1437cda27a22c..b3a70e6001842 100644 --- a/rust/kernel/drm/gem/shmem.rs +++ b/rust/kernel/drm/gem/shmem.rs @@ -66,7 +66,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.50.0