mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Janne Grunau <j@jannau.net>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Sami Tolvanen" <samitolvanen@google.com>,
	"Lyude Paul" <lyude@redhat.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Deborah Brouwer" <deborah.brouwer@collabora.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev,
	stable+noautosel@kernel.org
Subject: Re: [PATCH v3 5/5] rust: drm: gem: shmem: Do not free SG table of imported objects
Date: Fri,  4 Dec 2026 11:49:14 +0000	[thread overview]
Message-ID: <20261204114914.2151379-1-aliceryhl@google.com> (raw)
In-Reply-To: <20261203-rust-drm-gem-shmem-imported-sgt-v3-5-8ca393e83e20@jannau.net>

On Thu, Dec 03, 2026 at 11:14:09AM +0100, Janne Grunau wrote:
> GEM shmem objects imported through gem_prime_import_sg_table wrapping
> drm_gem_shmem_prime_import_sg_table() do not own their SG table.
> SGTableMap::drop() must not free it since it will be freed on exporter
> side once the DMAbuf is not used anymore.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Fixes: 616c229ab010 ("rust: drm: gem: Introduce shmem::Object::sg_table()")
> Cc: stable+noautosel@kernel.org # no in-tree users
> Tested-by: Deborah Brouwer <deborah.brouwer@collabora.com>
> Signed-off-by: Janne Grunau <j@jannau.net>

The code change itself looks good to me. Could we also update the
`# Invariants` and doc comments on `Object<T>` and `SGTableMap<T>` to
document imported objects? Something along the lines of:

diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -98,6 +98,11 @@
 ///
 /// - `obj` contains a valid initialized `struct drm_gem_shmem_object` for the lifetime of this
 ///   object.
+/// - If `self.is_imported()` is true, `(*self.obj.get()).sgt` was initialized at import time to a
+///   valid, pinned, DMA-mapped `struct sg_table` that remains valid and unmodified until
+///   `drm_gem_shmem_release()` tears down the `dma_buf` attachment in `free_callback`.
+/// - If `self.is_imported()` is false, `(*self.obj.get()).sgt` is owned by `sgt_res` (initialized
+///   at most once by `SGTableMap::new()` and unmapped/freed when `SGTableMap` is dropped).
 #[repr(C)]
 #[pin_data]
 pub struct Object<T: DriverObject> {
@@ -105,7 +110,8 @@
     obj: Opaque<bindings::drm_gem_shmem_object>,
     /// Parent object that owns this object's DMA reservation object.
     parent_resv_obj: Option<ARef<Object<T>>>,
-    /// Devres object for unmapping any SGTable on driver-unbind.
+    /// Devres object revoking SGTable access on driver-unbind (and unmapping it for non-imported
+    /// objects).
     sgt_res: ManuallyDrop<SetOnce<Devres<SGTableMap<T>>>>,
     #[pin]
     /// Lock for protecting initialization of `sgt_res`.
@@ -582,14 +588,18 @@
 
 /// A reference to a GEM object that is known to have a mapped [`SGTable`].
 ///
-/// This is used by the Rust bindings with [`Devres`] in order to ensure that mappings for SGTables
-/// on GEM shmem objects are revoked on driver-unbind.
+/// This is used by the Rust bindings with [`Devres`] to revoke Rust access to a GEM shmem object's
+/// [`SGTable`] on driver-unbind. For non-imported objects, dropping this value also unmaps and
+/// frees the underlying `struct sg_table`; for PRIME-imported objects, the `dma_buf` attachment
+/// mapping is torn down by `drm_prime_gem_destroy()` when the GEM object is freed.
 ///
 /// # Invariants
 ///
-/// - `self.obj` always points to a valid GEM object.
-/// - This object is proof that `self.obj.owner.sgt_res` has an initialized and valid pointer to an
-///   [`SGTable`].
+/// - `self.obj` always points to a valid [`Object`] for the lifetime of `self`.
+/// - `(*self.obj.as_ref().as_raw_shmem()).sgt` points to an initialized, valid `struct sg_table`
+///   that is not modified or freed for the lifetime of `self`.
+/// - If `!self.obj.as_ref().is_imported()`, `self` owns `(*self.obj.as_ref().as_raw_shmem()).sgt`
+///   and is responsible for unmapping and freeing it on drop.
 ///
 /// [`SGTable`]: scatterlist::SGTable
 pub struct SGTableMap<T: DriverObject> {
@@ -616,21 +626,27 @@
         }
         let _lock = DmaResvGuard::new(obj);
 
-        // SAFETY: We acquired the lock needed for calling this function above
-        // and the GEM object is not imported.
+        // SAFETY:
+        // - We acquired the `dma_resv` lock above.
+        // - Per our type invariants, since `!obj.is_imported()`, `self` owns `shmem->sgt` and it
+        //   is valid and initialized.
         unsafe { bindings::__drm_gem_shmem_free_sgt_locked(obj.as_raw_shmem()) };
     }
 }
 
 impl<T: DriverObject> SGTableMap<T> {
     fn new(obj: &Object<T>) -> impl Init<Self, Error> {
-        // INVARIANT:
-        // - We call drm_gem_shmem_get_pages_sgt below and check whether or not it succeeds,
-        //   fulfilling the invariant of SGTableMap that the object's `sgt` field is initialized.
         // SAFETY:
         // - `obj` is fully initialized, making this function safe to call.
         from_err_ptr(unsafe { bindings::drm_gem_shmem_get_pages_sgt(obj.as_raw_shmem()) })?;
 
+        // INVARIANT:
+        // - `SGTableMap` is only stored in `obj.sgt_res` (initialized at most once under
+        //   `obj.sgt_lock` and dropped in `free_callback` before `obj` is released), so
+        //   `self.obj` remains valid for the lifetime of `self`.
+        // - `drm_gem_shmem_get_pages_sgt()` succeeded above: for imported objects this reuses
+        //   the pre-populated `sgt` (valid until `drm_gem_shmem_release()`); for non-imported
+        //   objects it allocates and DMA-maps `sgt` exclusively for `self`.
         Ok(Self { obj: obj.into() })
     }
 }

Alice

  reply	other threads:[~2026-12-04 11:49 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 10:14 [PATCH v3 0/5] Fix import of dma-buf buffers as DRM GEM shmem objects Janne Grunau
2026-12-03 10:14 ` [PATCH v3 1/5] rust: drm: gem: shmem: Do not use drm_gem_shmem_dumb_create() Janne Grunau
2026-12-04 20:30   ` Daniel Almeida
2026-12-03 10:14 ` [PATCH v3 2/5] drm/gem/shmem: Initialize private objects via drm_gem_shmem_init() too Janne Grunau
2026-12-04 20:40   ` Daniel Almeida
2026-12-03 10:14 ` [PATCH v3 3/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback Janne Grunau
2026-12-04 11:43   ` Alice Ryhl
2026-12-04 21:15   ` Daniel Almeida
2026-12-05 13:35     ` Janne Grunau
2026-12-05 13:36       ` Daniel Almeida
2026-12-03 10:14 ` [PATCH v3 4/5] rust: drm: gem: add an is_imported() accessor Janne Grunau
2026-12-04 21:16   ` Daniel Almeida
2026-12-03 10:14 ` [PATCH v3 5/5] rust: drm: gem: shmem: Do not free SG table of imported objects Janne Grunau
2026-12-04 11:49   ` Alice Ryhl [this message]
2026-12-04 13:51     ` Janne Grunau
2026-12-04 14:21       ` Alice Ryhl
2026-12-04 21:22   ` Daniel Almeida

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204114914.2151379-1-aliceryhl@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=deborah.brouwer@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=j@jannau.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=samitolvanen@google.com \
    --cc=simona@ffwll.ch \
    --cc=stable+noautosel@kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=tzimmermann@suse.de \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®