[PATCH v4 3/5] rust: drm: gem: shmem: Do not free SG table of imported objects
From: Janne Grunau
Date: Sat Dec 05 2026 - 08:18:06 EST
The SG table of GEM shmem objects imported through
gem_prime_import_sg_table() is owned by the dma-buf and will be freed by
drm_prime_gem_destroy(). Do not free it in SGTableMap::drop()
Update shmem::Object<T> and SGTableMap<T> documentation to reflect the
differences between imported and an non-imported Objects as proposed in
[1].
Link: https://lore.kernel.org/rust-for-linux/20261204114914.2151379-1-aliceryhl@xxxxxxxxxx/ [1]
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>
Reviewed-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
Signed-off-by: Janne Grunau <j@xxxxxxxxxx>
---
rust/kernel/drm/gem/shmem.rs | 39 +++++++++++++++++++++++++++++----------
1 file changed, 29 insertions(+), 10 deletions(-)
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index 901960d71673..bb96b8ab5196 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -97,6 +97,11 @@ fn default() -> Self {
///
/// - `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> {
@@ -104,7 +109,8 @@ pub struct Object<T: DriverObject> {
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`.
@@ -513,14 +519,18 @@ unsafe impl<D, R, const SIZE: usize> Sync for VMap<D, R, SIZE>
/// 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> {
@@ -542,9 +552,15 @@ impl<T: DriverObject> Drop for SGTableMap<T> {
fn drop(&mut self) {
// SAFETY: `obj` is always valid via our type invariants
let obj = unsafe { self.obj.as_ref() };
+ if obj.is_imported() {
+ return;
+ }
let _lock = DmaResvGuard::new(obj);
- // SAFETY: We acquired the lock needed for calling this function above
+ // 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()) };
}
}
@@ -557,9 +573,12 @@ fn new(obj: &Object<T>) -> impl Init<Self, Error> + '_ {
from_err_ptr(unsafe { bindings::drm_gem_shmem_get_pages_sgt(obj.as_raw_shmem()) })?;
// INVARIANT:
- // - We call drm_gem_shmem_get_pages_sgt above and check whether or not it succeeds,
- // fulfilling the invariant of SGTableMap that the object's `sgt` field is
- // initialized.
+ // - `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(try_init!(Self { obj: obj.into() }))
})
}
--
2.56.1