Re: [PATCH v3 5/5] rust: drm: gem: shmem: Do not free SG table of imported objects

From: Janne Grunau

Date: Fri Dec 04 2026 - 08:51:22 EST


On Fri, Dec 04, 2026 at 11:49:14AM +0000, Alice Ryhl wrote:
> 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@xxxxxxxxxx>
> > Fixes: 616c229ab010 ("rust: drm: gem: Introduce shmem::Object::sg_table()")
> > Cc: stable+noautosel@xxxxxxxxxx # no in-tree users
> > Tested-by: Deborah Brouwer <deborah.brouwer@xxxxxxxxxxxxx>
> > Signed-off-by: Janne Grunau <j@xxxxxxxxxx>
>
> 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:

yes, thanks for the suggested changes.

> 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() })
> }
> }

The changes look good. If you want to apply them as separate commit:

Reviewed-by: Janne Grunau <j@xxxxxxxxxx>

or I can fold them into the relevant changes and send a new version.

Janne