* [PATCH v2 0/5] Fix import of dma-buf buffers as DRM GEM shmem objects
@ 2026-11-24 11:37 Janne Grunau
2026-11-24 11:37 ` [PATCH v2 1/5] rust: drm: gem: shmem: Do not use drm_gem_shmem_dumb_create() Janne Grunau
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Janne Grunau @ 2026-11-24 11:37 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, David Airlie, Simona Vetter,
Sami Tolvanen, Lyude Paul, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: rust-for-linux, linux-kernel, dri-devel, nova-gpu, Janne Grunau,
stable, stable+noautosel
Hej,
this series is triggered by the discussion of gem/shmem.rs SGTableMap
issues in [1] which has an additional issue with imported objects.
Imported objects are supported since GEM shmem's AllocOps use
drm_gem_shmem_prime_import_sg_table() as gem_prime_import_sg_table which
sets the same function pointer field in struct drm_driver.
The code is broken in two ways. SGTableMap::drop() calls
__drm_gem_shmem_free_sgt_locked() uncondiftionally which it must not do
for imported objects as the object does not own `sgt`.
The second issue is that the imported object is allocated by
__drm_gem_shmem_create() with sizeof(struct drm_gem_shmem_object) as
size instead of the size of Rust's Object<T>.
The first issue is fixed in the Asahi downstream driver together with a
drm_gem_is_imported() wrapper change taken from the Tyr downstream
driver.
The second issue is fixed by avoiding to allocate Rust GEM objects
from C. In addition to size issue v1 addressed Rust gem objects may
contain fields with non-trivial `Drop` implementations. Those fields
won't be destroyed in error paths in C code which "destroys" the objects
with `kfree(obj)`. Solve this by re-implementing
gem_prime_import_sg_table() in Rust.
The same issues apply to drm_gem_shmem_dumb_create(). As Asahi and Tyr
as render only devices do not use the `dumb_create` callback just drop
it. When a future driver uses the callback drm_gem_shmem_dumb_create()
needs to be re-implemented in Rust as well.
The series is tested in the Asahi downstream driver. As the prime import
code path is unused in-tree I marked the fixes as not suitable for stable
backports. The known downstream users of the code are aware of the
issues and carry fixes downstream. I don't expect new Rust DRM drivers
to be based on (old) stable trees.
Link: https://lore.kernel.org/rust-for-linux/20261019-rust-shmem-fixes-v1-1-3f7f6de5b5e7@google.com/ [1]
Signed-off-by: Janne Grunau <j@jannau.net>
---
Changes in v2:
- Switch to Rust gem_prime_import_sg_table() implementation
- Drop unsound drm_gem_shmem_dumb_create() as well
- Add `#[inline] annotation to BaseObject::is_imported()
- Drop typoed mail address from To:
- Added Alice's Reviewed-by tags
- Link to v1: https://lore.kernel.org/r/20261026-rust-drm-gem-shmem-imported-sgt-v1-0-36f15ace04b1@jannau.net
---
Janne Grunau (4):
rust: drm: gem: shmem: Do not use drm_gem_shmem_dumb_create()
drm/gem/shmem: Initialize private objects via drm_gem_shmem_init() too
rust: drm: gem: shmem: Implement prime_import_sg_table callback
rust: drm: gem: shmem: Do not free SG table of imported objects
Sami Tolvanen (1):
rust: drm: gem: add an is_imported() accessor
drivers/gpu/drm/drm_gem_shmem_helper.c | 34 +++++++-------
drivers/gpu/drm/nova/gem.rs | 4 ++
drivers/gpu/drm/tyr/gem.rs | 4 ++
include/drm/drm_gem_shmem_helper.h | 3 +-
rust/helpers/drm.c | 6 +++
rust/kernel/drm/gem/mod.rs | 17 +++++++
rust/kernel/drm/gem/shmem.rs | 81 ++++++++++++++++++++++++++++++++--
7 files changed, 126 insertions(+), 23 deletions(-)
---
base-commit: 3497a403264b094ebd61b8b755165d56e7d9dc37
change-id: 20261026-rust-drm-gem-shmem-imported-sgt-85c4a45be283
Best regards,
--
Janne Grunau <j@jannau.net>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/5] rust: drm: gem: shmem: Do not use drm_gem_shmem_dumb_create()
2026-11-24 11:37 [PATCH v2 0/5] Fix import of dma-buf buffers as DRM GEM shmem objects Janne Grunau
@ 2026-11-24 11:37 ` Janne Grunau
2026-11-24 11:37 ` [PATCH v2 2/5] drm/gem/shmem: Initialize private objects via drm_gem_shmem_init() too Janne Grunau
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Janne Grunau @ 2026-11-24 11:37 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, David Airlie, Simona Vetter,
Sami Tolvanen, Lyude Paul, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: rust-for-linux, linux-kernel, dri-devel, nova-gpu, Janne Grunau, stable
The existing gem::shmem using Rust GPU drivers (Ashi and Tyr) are render
only devices which should not set the dumb_create callback. When a
driver with KMS functionality is added it probably will not be able to use
drm_gem_shmem_dumb_create().
The Rust gem::shmem::Object<T> wrapper should be allocated and destroyed
from Rust code. DRM drivers have a `gem_create_object` callback which
could be used to allocate objects from Rust. The C code will "destroy"
objects with `kfree(object)` on error paths which is unsound if the Rust
object's DriverObject contains fields with a non-trivial `Drop`
implementation.
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Fixes: 80df573af9ef ("rust: drm: gem: shmem: Add DRM shmem helper abstraction")
Cc: stable@vger.kernel.org
Signed-off-by: Janne Grunau <j@jannau.net>
---
rust/kernel/drm/gem/shmem.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index a687d46d170d..78a152f0f30c 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -394,7 +394,7 @@ impl<T: DriverObject> driver::AllocImpl for Object<T> {
prime_fd_to_handle: None,
gem_prime_import: None,
gem_prime_import_sg_table: Some(bindings::drm_gem_shmem_prime_import_sg_table),
- dumb_create: Some(bindings::drm_gem_shmem_dumb_create),
+ dumb_create: None,
dumb_map_offset: None,
};
}
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 2/5] drm/gem/shmem: Initialize private objects via drm_gem_shmem_init() too
2026-11-24 11:37 [PATCH v2 0/5] Fix import of dma-buf buffers as DRM GEM shmem objects Janne Grunau
2026-11-24 11:37 ` [PATCH v2 1/5] rust: drm: gem: shmem: Do not use drm_gem_shmem_dumb_create() Janne Grunau
@ 2026-11-24 11:37 ` Janne Grunau
2026-11-24 11:37 ` [PATCH v2 3/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback Janne Grunau
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Janne Grunau @ 2026-11-24 11:37 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, David Airlie, Simona Vetter,
Sami Tolvanen, Lyude Paul, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: rust-for-linux, linux-kernel, dri-devel, nova-gpu, Janne Grunau
Rust drivers need to initialize private gem objects when importing
DMA-bufs. As with regular objects the ideal way is to allocate the
object in Rust and initialize it via the DRM gem shmem helper. Expose
the `private` to the exported drm_gem_shmem_init() function.
Link: https://lore.kernel.org/rust-for-linux/20261118114102.490384-1-aliceryhl@google.com/ [1]
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Janne Grunau <j@jannau.net>
---
drivers/gpu/drm/drm_gem_shmem_helper.c | 34 +++++++++++++++-------------------
include/drm/drm_gem_shmem_helper.h | 3 ++-
rust/kernel/drm/gem/shmem.rs | 4 +++-
3 files changed, 20 insertions(+), 21 deletions(-)
diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index d654a22a30de..7042d59ef86d 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -51,8 +51,20 @@ static const struct drm_gem_object_funcs drm_gem_shmem_funcs = {
.vm_ops = &drm_gem_shmem_vm_ops,
};
-static int __drm_gem_shmem_init(struct drm_device *dev, struct drm_gem_shmem_object *shmem,
- size_t size, bool private)
+/**
+ * drm_gem_shmem_init - Initialize an allocated object.
+ * @dev: DRM device
+ * @shmem: shmem GEM object to initialize
+ * @size: Buffer size in bytes
+ * @private: Object w/o GEM backing store if true, for example dma-buf
+ *
+ * This function initializes an allocated shmem GEM object.
+ *
+ * Returns:
+ * 0 on success, or a negative error code on failure.
+ */
+int drm_gem_shmem_init(struct drm_device *dev, struct drm_gem_shmem_object *shmem,
+ size_t size, bool private)
{
struct drm_gem_object *obj = &shmem->base;
int ret = 0;
@@ -94,22 +106,6 @@ static int __drm_gem_shmem_init(struct drm_device *dev, struct drm_gem_shmem_obj
drm_gem_object_release(obj);
return ret;
}
-
-/**
- * drm_gem_shmem_init - Initialize an allocated object.
- * @dev: DRM device
- * @shmem: shmem GEM object to initialize
- * @size: Buffer size in bytes
- *
- * This function initializes an allocated shmem GEM object.
- *
- * Returns:
- * 0 on success, or a negative error code on failure.
- */
-int drm_gem_shmem_init(struct drm_device *dev, struct drm_gem_shmem_object *shmem, size_t size)
-{
- return __drm_gem_shmem_init(dev, shmem, size, false);
-}
EXPORT_SYMBOL_GPL(drm_gem_shmem_init);
static struct drm_gem_shmem_object *
@@ -133,7 +129,7 @@ __drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
obj = &shmem->base;
}
- ret = __drm_gem_shmem_init(dev, shmem, size, private);
+ ret = drm_gem_shmem_init(dev, shmem, size, private);
if (ret) {
kfree(obj);
return ERR_PTR(ret);
diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem_helper.h
index bcbb92f3fe9b..60a09a3289a7 100644
--- a/include/drm/drm_gem_shmem_helper.h
+++ b/include/drm/drm_gem_shmem_helper.h
@@ -107,7 +107,8 @@ struct drm_gem_shmem_object {
#define to_drm_gem_shmem_obj(obj) \
container_of(obj, struct drm_gem_shmem_object, base)
-int drm_gem_shmem_init(struct drm_device *dev, struct drm_gem_shmem_object *shmem, size_t size);
+int drm_gem_shmem_init(struct drm_device *dev, struct drm_gem_shmem_object *shmem,
+ size_t size, bool private);
struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size);
void drm_gem_shmem_release(struct drm_gem_shmem_object *shmem);
void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem);
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index 78a152f0f30c..264ab188f77d 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -320,7 +320,9 @@ pub fn new(
unsafe { (*new.as_raw()).funcs = &Self::VTABLE };
// SAFETY: The arguments are all valid via the type invariants.
- to_result(unsafe { bindings::drm_gem_shmem_init(dev.as_raw(), new.as_raw_shmem(), size) })?;
+ to_result(unsafe {
+ bindings::drm_gem_shmem_init(dev.as_raw(), new.as_raw_shmem(), size, false)
+ })?;
// SAFETY: We never move out of `self`.
let new = KBox::into_raw(unsafe { Pin::into_inner_unchecked(new) });
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 3/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback
2026-11-24 11:37 [PATCH v2 0/5] Fix import of dma-buf buffers as DRM GEM shmem objects Janne Grunau
2026-11-24 11:37 ` [PATCH v2 1/5] rust: drm: gem: shmem: Do not use drm_gem_shmem_dumb_create() Janne Grunau
2026-11-24 11:37 ` [PATCH v2 2/5] drm/gem/shmem: Initialize private objects via drm_gem_shmem_init() too Janne Grunau
@ 2026-11-24 11:37 ` Janne Grunau
2026-11-24 16:53 ` Alice Ryhl
2026-11-24 11:37 ` [PATCH v2 4/5] rust: drm: gem: add an is_imported() accessor Janne Grunau
2026-11-24 11:37 ` [PATCH v2 5/5] rust: drm: gem: shmem: Do not free SG table of imported objects Janne Grunau
4 siblings, 1 reply; 7+ messages in thread
From: Janne Grunau @ 2026-11-24 11:37 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, David Airlie, Simona Vetter,
Sami Tolvanen, Lyude Paul, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: rust-for-linux, linux-kernel, dri-devel, nova-gpu, Janne Grunau,
stable+noautosel
The C drm_gem_shmem_prime_import_sg_table() helper function uses
`__drm_gem_shmem_create()` to create shmem objects. In the absence of a
`gem_create_object` callback this allocates just a `struct
drm_gem_shmem_object` resulting in buffer overflows when using it as
Rust `Object<t>`.
Implementing the `gem_create_object` callback for RUST DRM drivers is
not sufficient as `__drm_gem_shmem_create()` uses `kfree(obj)` in error
paths. This is unsound if the DriverObject contains fields with
non-trivial `Drop` implementations as noted in [1].
Imported BOs have no userspace creation arguments, so extend
`DriverObject` with a `create_imported(dev, size)` constructor and
implement it for Tyr's `BoData`, Nova, and the in-tree KUnit driver.
Tested with the asahi downstream driver.
Link: https://lore.kernel.org/rust-for-linux/20261118114102.490384-1-aliceryhl@google.com/ [1]
Fixes: 80df573af9ef ("rust: drm: gem: shmem: Add DRM shmem helper abstraction")
Cc: <stable+noautosel@kernel.org> # no in tree users
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Co-developed-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Janne Grunau <j@jannau.net>
---
drivers/gpu/drm/nova/gem.rs | 4 +++
drivers/gpu/drm/tyr/gem.rs | 4 +++
rust/kernel/drm/gem/mod.rs | 9 ++++++
rust/kernel/drm/gem/shmem.rs | 71 +++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 87 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
index 2b6fe9dc0bfa..2e17422e76f5 100644
--- a/drivers/gpu/drm/nova/gem.rs
+++ b/drivers/gpu/drm/nova/gem.rs
@@ -27,6 +27,10 @@ impl gem::DriverObject for NovaObject {
fn new(_dev: &NovaDevice, _size: usize, _args: Self::Args) -> impl PinInit<Self, Error> {
try_pin_init!(NovaObject {})
}
+
+ fn create_imported(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
+ try_pin_init!(NovaObject {})
+ }
}
impl NovaObject {
diff --git a/drivers/gpu/drm/tyr/gem.rs b/drivers/gpu/drm/tyr/gem.rs
index 3bf3787f5c3f..4b61bb04c845 100644
--- a/drivers/gpu/drm/tyr/gem.rs
+++ b/drivers/gpu/drm/tyr/gem.rs
@@ -48,6 +48,10 @@ impl gem::DriverObject for BoData {
fn new(_dev: &TyrDrmDevice, _size: usize, args: BoCreateArgs) -> impl PinInit<Self, Error> {
try_pin_init!(Self { flags: args.flags })
}
+
+ fn create_imported(_dev: &TyrDrmDevice, _size: usize) -> impl PinInit<Self, Error> {
+ try_pin_init!(Self { flags: 0 })
+ }
}
/// Type alias for Tyr GEM buffer objects.
diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index e1ebad77ebe2..e01b8311dd89 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -98,6 +98,15 @@ fn new(
args: Self::Args,
) -> impl PinInit<Self, Error>;
+ /// Create a new driver data object for a GEM object that is being imported
+ /// from another driver (e.g. via dma-buf).
+ ///
+ /// Imported objects are allocated by the importing helper, which only
+ /// provides the device and size. There is no driver-supplied
+ /// [`DriverObject::Args`], so drivers must initialize the per-object
+ /// state without those arguments.
+ fn create_imported(dev: &drm::Device<Self::Driver>, size: usize) -> impl PinInit<Self, Error>;
+
/// Open a new handle to an existing object, associated with a File.
fn open(_obj: &DriverAllocImpl<Self>, _file: &DriverFile<Self>) -> Result {
Ok(())
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index 264ab188f77d..d6977673e21a 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -32,6 +32,7 @@
SysMem,
SysMemBackend, //
},
+ page::page_align,
prelude::*,
scatterlist,
sync::{
@@ -345,6 +346,67 @@ pub fn new(
Ok(obj)
}
+ /// Create a new shmem GEM object from another driver's scatter/gather table of pinned pages.
+ ///
+ /// This is a Rust re-implemetation of `drm_gem_shmem_prime_import_sg_table()` to avoid
+ /// creating and destroying `Self` in C code.
+ /// This function imports a scatter/gather table exported via DMA-BUF by another driver.
+ /// It should be only called via the &drm_driver.gem_prime_import_sg_table callback.
+ extern "C" fn prime_import_sg_table_callback(
+ raw_dev: *mut bindings::drm_device,
+ raw_attach: *mut bindings::dma_buf_attachment,
+ raw_sgt: *mut bindings::sg_table,
+ ) -> *mut bindings::drm_gem_object {
+ // SAFETY: raw_attach is a valid pointer to `bindings::dma_buf_attachment` created
+ // by `dma_buf_attach` in `drm_gem_prime_import_dev` the only caller of this
+ // callback.
+ let size = match page_align(unsafe { (*(*raw_attach).dmabuf).size }) {
+ Some(size) => size,
+ None => return EINVAL.to_ptr(),
+ };
+
+ // SAFETY: The DRM shmem helper only invokes this callback for a registered DRM device of
+ // type `T::Driver`, which is `Self`'s driver per the `AllocImpl` impl below.
+ let dev: &Device<T::Driver> = unsafe { Device::from_raw(raw_dev) };
+
+ let new: Pin<KBox<Self>> = match KBox::try_pin_init(
+ try_pin_init!(Self {
+ obj <- Opaque::init_zeroed(),
+ parent_resv_obj: None,
+ sgt_res: ManuallyDrop::new(SetOnce::new()),
+ sgt_lock <- new_mutex!(()),
+ inner <- T::create_imported(dev, size),
+ }),
+ GFP_KERNEL,
+ ) {
+ Ok(new) => new,
+ Err(e) => return e.to_ptr(),
+ };
+
+ // SAFETY: `new.as_raw()` is guaranteed to be valid by the initialization above.
+ unsafe { (*new.as_raw()).funcs = &Self::VTABLE };
+
+ // SAFETY: The arguments are all valid via the type invariants.
+ if let Err(e) = to_result(unsafe {
+ bindings::drm_gem_shmem_init(dev.as_raw(), new.as_raw_shmem(), size, true)
+ }) {
+ return e.to_ptr();
+ };
+
+ // SAFETY: `new` is an initialized `Self` never moved outside of this function so it is
+ // safe to initialized `drm_gem_shmem_object::sgt` with `raw_sgt` as expected by the caller
+ // of this function.
+ unsafe { (*new.as_raw_shmem()).sgt = raw_sgt };
+
+ // SAFETY: We never move out of `self`. Ownership of the allocation is transferred to the
+ // C side.
+ let new = KBox::into_raw(unsafe { Pin::into_inner_unchecked(new) });
+
+ // SAFETY: `new` was just produced by `KBox::into_raw` and so is a valid pointer to an
+ // initialized `Self`. `obj.base` is the embedded `drm_gem_object` the caller expects.
+ unsafe { &raw mut (*(*new).obj.get()).base }
+ }
+
/// Creates and returns an owned reference to a virtual kernel memory mapping for this object.
#[inline]
pub fn owned_vmap<const SIZE: usize>(&self) -> Result<VMapOwned<T, SIZE>> {
@@ -395,7 +457,7 @@ impl<T: DriverObject> driver::AllocImpl for Object<T> {
prime_handle_to_fd: None,
prime_fd_to_handle: None,
gem_prime_import: None,
- gem_prime_import_sg_table: Some(bindings::drm_gem_shmem_prime_import_sg_table),
+ gem_prime_import_sg_table: Some(Object::<T>::prime_import_sg_table_callback),
dumb_create: None,
dumb_map_offset: None,
};
@@ -625,6 +687,13 @@ fn new(
) -> impl PinInit<Self, Error> {
try_pin_init!(KunitObject {})
}
+
+ fn create_imported(
+ _dev: &drm::Device<KunitDriver>,
+ _size: usize,
+ ) -> impl PinInit<Self, Error> {
+ try_pin_init!(KunitObject {})
+ }
}
#[vtable]
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 4/5] rust: drm: gem: add an is_imported() accessor
2026-11-24 11:37 [PATCH v2 0/5] Fix import of dma-buf buffers as DRM GEM shmem objects Janne Grunau
` (2 preceding siblings ...)
2026-11-24 11:37 ` [PATCH v2 3/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback Janne Grunau
@ 2026-11-24 11:37 ` Janne Grunau
2026-11-24 11:37 ` [PATCH v2 5/5] rust: drm: gem: shmem: Do not free SG table of imported objects Janne Grunau
4 siblings, 0 replies; 7+ messages in thread
From: Janne Grunau @ 2026-11-24 11:37 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, David Airlie, Simona Vetter,
Sami Tolvanen, Lyude Paul, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: rust-for-linux, linux-kernel, dri-devel, nova-gpu, Janne Grunau
From: Sami Tolvanen <samitolvanen@google.com>
Drivers need to check whether a GEM object is PRIME-imported.
Add BaseObject::is_imported(), wrapping drm_gem_is_imported() through a
C helper shim.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Janne Grunau <j@jannau.net>
---
rust/helpers/drm.c | 6 ++++++
rust/kernel/drm/gem/mod.rs | 8 ++++++++
2 files changed, 14 insertions(+)
diff --git a/rust/helpers/drm.c b/rust/helpers/drm.c
index 65f3f22b0e1d..8cf3ca7aeeb2 100644
--- a/rust/helpers/drm.c
+++ b/rust/helpers/drm.c
@@ -16,6 +16,12 @@ __rust_helper void rust_helper_drm_gem_object_put(struct drm_gem_object *obj)
drm_gem_object_put(obj);
}
+__rust_helper bool
+rust_helper_drm_gem_is_imported(const struct drm_gem_object *obj)
+{
+ return drm_gem_is_imported(obj);
+}
+
__rust_helper __u64
rust_helper_drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
{
diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index e01b8311dd89..8cedfabe712b 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -187,6 +187,14 @@ fn size(&self) -> usize {
unsafe { (*self.as_raw()).size }
}
+ /// Returns whether the object's backing buffer is imported from another
+ /// driver.
+ #[inline]
+ fn is_imported(&self) -> bool {
+ // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a valid `struct drm_gem_object`.
+ unsafe { bindings::drm_gem_is_imported(self.as_raw()) }
+ }
+
/// Creates a new handle for the object associated with a given `File`
/// (or returns an existing one).
fn create_handle<D, F>(&self, file: &drm::File<F>) -> Result<u32>
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 5/5] rust: drm: gem: shmem: Do not free SG table of imported objects
2026-11-24 11:37 [PATCH v2 0/5] Fix import of dma-buf buffers as DRM GEM shmem objects Janne Grunau
` (3 preceding siblings ...)
2026-11-24 11:37 ` [PATCH v2 4/5] rust: drm: gem: add an is_imported() accessor Janne Grunau
@ 2026-11-24 11:37 ` Janne Grunau
4 siblings, 0 replies; 7+ messages in thread
From: Janne Grunau @ 2026-11-24 11:37 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, David Airlie, Simona Vetter,
Sami Tolvanen, Lyude Paul, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: rust-for-linux, linux-kernel, dri-devel, nova-gpu, Janne Grunau,
stable+noautosel
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
Signed-off-by: Janne Grunau <j@jannau.net>
---
rust/kernel/drm/gem/shmem.rs | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index d6977673e21a..d935384f5c1e 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -611,9 +611,13 @@ 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
+ // and the GEM object is not imported.
unsafe { bindings::__drm_gem_shmem_free_sgt_locked(obj.as_raw_shmem()) };
}
}
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 3/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback
2026-11-24 11:37 ` [PATCH v2 3/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback Janne Grunau
@ 2026-11-24 16:53 ` Alice Ryhl
0 siblings, 0 replies; 7+ messages in thread
From: Alice Ryhl @ 2026-11-24 16:53 UTC (permalink / raw)
To: Janne Grunau
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
Onur Özkan, David Airlie, Simona Vetter, Sami Tolvanen,
Lyude Paul, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
rust-for-linux, linux-kernel, dri-devel, nova-gpu,
stable+noautosel
On Tue, Nov 24, 2026 at 12:37:45PM +0100, Janne Grunau wrote:
> + // SAFETY: The DRM shmem helper only invokes this callback for a registered DRM device of
> + // type `T::Driver`, which is `Self`'s driver per the `AllocImpl` impl below.
> + let dev: &Device<T::Driver> = unsafe { Device::from_raw(raw_dev) };
`drm::Driver` in `rust/kernel/drm/driver.rs` currently declares:
type Object: AllocImpl;
without any constraint that `T::Object::Driver = T`. I think we should
introduce one similar to how Driver::File constrains this?
Alice
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-24 16:53 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 11:37 [PATCH v2 0/5] Fix import of dma-buf buffers as DRM GEM shmem objects Janne Grunau
2026-11-24 11:37 ` [PATCH v2 1/5] rust: drm: gem: shmem: Do not use drm_gem_shmem_dumb_create() Janne Grunau
2026-11-24 11:37 ` [PATCH v2 2/5] drm/gem/shmem: Initialize private objects via drm_gem_shmem_init() too Janne Grunau
2026-11-24 11:37 ` [PATCH v2 3/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback Janne Grunau
2026-11-24 16:53 ` Alice Ryhl
2026-11-24 11:37 ` [PATCH v2 4/5] rust: drm: gem: add an is_imported() accessor Janne Grunau
2026-11-24 11:37 ` [PATCH v2 5/5] rust: drm: gem: shmem: Do not free SG table of imported objects Janne Grunau
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®