* Claude review: drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked()
2026-04-09 0:12 ` [PATCH v10 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
@ 2026-04-12 2:01 ` Claude Code Review Bot
0 siblings, 0 replies; 14+ messages in thread
From: Claude Code Review Bot @ 2026-04-12 2:01 UTC (permalink / raw)
To: dri-devel-reviews
Patch Review
This extracts the sgt teardown logic into a standalone function and exports it for the Rust bindings.
**Missing NULL-safety:** The new function unconditionally dereferences `shmem->sgt`:
```c
void __drm_gem_shmem_free_sgt_locked(struct drm_gem_shmem_object *shmem)
{
dma_resv_assert_held(shmem->base.resv);
dma_unmap_sgtable(shmem->base.dev->dev, shmem->sgt, DMA_BIDIRECTIONAL, 0);
sg_free_table(shmem->sgt);
kfree(shmem->sgt);
shmem->sgt = NULL;
}
```
The caller in `drm_gem_shmem_release` guards with `if (shmem->sgt)`, so the internal use is fine. But as an exported function (and one called from Rust `SGTableMap::drop`), it should either document the precondition that `sgt` must be non-NULL, or add a NULL check. Given the destruction-ordering concern I describe in patch 4's review, a NULL guard here would be defensive hardening.
**No pages put:** This function frees the sgt mapping but does not call `drm_gem_shmem_put_pages_locked()`. This is intentional — the pages remain pinned and are cleaned up when the gem object is ultimately freed — but it should be documented explicitly, since `drm_gem_shmem_get_pages_sgt_locked` paired `get_pages` with the sgt creation.
**Naming:** The double-underscore prefix (`__`) conventionally signals "internal, don't call directly" in the kernel, but this is exported with `EXPORT_SYMBOL_GPL` and has a public doc comment. Consider dropping the `__` prefix since it's part of the exported API.
**Doc comment vs. function name mismatch:** The kerneldoc title says `drm_gem_shmem_release_sgt_locked` but the function is named `__drm_gem_shmem_free_sgt_locked`.
---
---
Generated by Claude Code Patch Reviewer
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v12 0/5] Rust bindings for gem shmem
@ 2026-04-21 23:52 Lyude Paul
2026-04-21 23:52 ` [PATCH v12 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs Lyude Paul
` (5 more replies)
0 siblings, 6 replies; 14+ messages in thread
From: Lyude Paul @ 2026-04-21 23:52 UTC (permalink / raw)
To: nouveau, Gary Guo, Daniel Almeida, rust-for-linux,
Danilo Krummrich, dri-devel
Cc: Matthew Maurer, FUJITA Tomonori, Lorenzo Stoakes,
christian.koenig, Asahi Lina, Miguel Ojeda, Andreas Hindborg,
Simona Vetter, Alice Ryhl, Boqun Feng, Sumit Semwal,
Krishna Ketan Rai, linux-media, Shankari Anand, David Airlie,
Benno Lossin, Viresh Kumar, linaro-mm-sig, Asahi Lina,
Greg Kroah-Hartman, kernel
Most of this patch series has already been pushed upstream, this is just
the second half of the patch series that has not been pushed yet + some
additional changes which were required to implement changes requested by
the mailing list. This patch series is originally from Asahi, previously
posted by Daniel Almeida.
The previous version of the patch series can be found here:
https://patchwork.freedesktop.org/series/164580/
Branch with patches applied available here
sure this builds:
https://gitlab.freedesktop.org/lyudess/linux/-/commits/rust/gem-shmem
This patch series applies on top of drm-rust-next
Lyude Paul (5):
rust: drm: gem: s/device::Device/Device/ for shmem.rs
drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked()
drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked()
rust: drm: gem: Introduce shmem::SGTable
rust: drm: gem: Add vmap functions to shmem bindings
drivers/gpu/drm/drm_gem_shmem_helper.c | 48 ++-
include/drm/drm_gem_shmem_helper.h | 2 +
rust/kernel/drm/gem/shmem.rs | 557 ++++++++++++++++++++++++-
3 files changed, 593 insertions(+), 14 deletions(-)
base-commit: a7a080bb4236ebe577b6776d940d1717912ff6dd
--
2.53.0
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v12 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs
2026-04-21 23:52 [PATCH v12 0/5] Rust bindings for gem shmem Lyude Paul
@ 2026-04-21 23:52 ` Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-21 23:52 ` [PATCH v12 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
` (4 subsequent siblings)
5 siblings, 1 reply; 14+ messages in thread
From: Lyude Paul @ 2026-04-21 23:52 UTC (permalink / raw)
To: nouveau, Gary Guo, Daniel Almeida, rust-for-linux,
Danilo Krummrich, dri-devel
Cc: Matthew Maurer, FUJITA Tomonori, Lorenzo Stoakes,
christian.koenig, Asahi Lina, Miguel Ojeda, Andreas Hindborg,
Simona Vetter, Alice Ryhl, Boqun Feng, Sumit Semwal,
Krishna Ketan Rai, linux-media, Shankari Anand, David Airlie,
Benno Lossin, Viresh Kumar, linaro-mm-sig, Asahi Lina,
Greg Kroah-Hartman, kernel
We're about to start explicitly mentioning kernel devices as well in this
file, so this makes it easier to differentiate the two by allowing us to
import `device` as `kernel::device`.
Signed-off-by: Lyude Paul <lyude@redhat.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
---
V11:
* Fix location of //
rust/kernel/drm/gem/shmem.rs | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index d025fb0351954..11749c36e8695 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -12,10 +12,10 @@
use crate::{
container_of,
drm::{
- device,
driver,
gem,
- private::Sealed, //
+ private::Sealed,
+ Device, //
},
error::to_result,
prelude::*,
@@ -108,7 +108,7 @@ fn as_raw_shmem(&self) -> *mut bindings::drm_gem_shmem_object {
///
/// Additional config options can be specified using `config`.
pub fn new(
- dev: &device::Device<T::Driver>,
+ dev: &Device<T::Driver>,
size: usize,
config: ObjectConfig<'_, T>,
args: T::Args,
@@ -150,9 +150,9 @@ pub fn new(
}
/// Returns the `Device` that owns this GEM object.
- pub fn dev(&self) -> &device::Device<T::Driver> {
+ pub fn dev(&self) -> &Device<T::Driver> {
// SAFETY: `dev` will have been initialized in `Self::new()` by `drm_gem_shmem_init()`.
- unsafe { device::Device::from_raw((*self.as_raw()).dev) }
+ unsafe { Device::from_raw((*self.as_raw()).dev) }
}
extern "C" fn free_callback(obj: *mut bindings::drm_gem_object) {
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v12 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked()
2026-04-21 23:52 [PATCH v12 0/5] Rust bindings for gem shmem Lyude Paul
2026-04-21 23:52 ` [PATCH v12 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs Lyude Paul
@ 2026-04-21 23:52 ` Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-21 23:52 ` [PATCH v12 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked() Lyude Paul
` (3 subsequent siblings)
5 siblings, 1 reply; 14+ messages in thread
From: Lyude Paul @ 2026-04-21 23:52 UTC (permalink / raw)
To: nouveau, Gary Guo, Daniel Almeida, rust-for-linux,
Danilo Krummrich, dri-devel
Cc: Matthew Maurer, FUJITA Tomonori, Lorenzo Stoakes,
christian.koenig, Asahi Lina, Miguel Ojeda, Andreas Hindborg,
Simona Vetter, Alice Ryhl, Boqun Feng, Sumit Semwal,
Krishna Ketan Rai, linux-media, Shankari Anand, David Airlie,
Benno Lossin, Viresh Kumar, linaro-mm-sig, Asahi Lina,
Greg Kroah-Hartman, kernel
One of the complications of trying to use the shmem helpers to create a
scatterlist for shmem objects is that we need to be able to provide a
guarantee that the driver cannot be unbound for the lifetime of the
scatterlist.
The easiest way of handling this seems to be just hooking up an unmap
operation to devres the first time we create a scatterlist, which allows us
to still take advantage of gem shmem facilities without breaking that
guarantee. To allow for this, we extract __drm_gem_shmem_free_sgt_locked()
- which allows a caller (e.g. the rust bindings) to manually unmap the sgt
for a gem object as needed.
Signed-off-by: Lyude Paul <lyude@redhat.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
---
V10:
* Fix incorrect function name in documentation for
__drm_gem_shmem_release_sgt_locked()
drivers/gpu/drm/drm_gem_shmem_helper.c | 32 +++++++++++++++++++++-----
include/drm/drm_gem_shmem_helper.h | 1 +
2 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index 4500deef41278..d2c34a0e573a1 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -158,6 +158,30 @@ struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t
}
EXPORT_SYMBOL_GPL(drm_gem_shmem_create);
+/**
+ * __drm_gem_shmem_release_sgt_locked - Unpin and DMA unmap pages, and release the
+ * cached scatter/gather table for an shmem GEM object.
+ * @shmem: shmem GEM object
+ *
+ * If the passed shmem object has an active scatter/gather table for driver
+ * usage, this function will unmap it and release the memory associated with it.
+ * It is the responsibility of the caller to ensure it holds the dma_resv_lock
+ * for this object.
+ *
+ * Drivers should not need to call this function themselves, it is mainly
+ * intended for usage in the Rust shmem bindings.
+ */
+void __drm_gem_shmem_free_sgt_locked(struct drm_gem_shmem_object *shmem)
+{
+ dma_resv_assert_held(shmem->base.resv);
+
+ dma_unmap_sgtable(shmem->base.dev->dev, shmem->sgt, DMA_BIDIRECTIONAL, 0);
+ sg_free_table(shmem->sgt);
+ kfree(shmem->sgt);
+ shmem->sgt = NULL;
+}
+EXPORT_SYMBOL_GPL(__drm_gem_shmem_free_sgt_locked);
+
/**
* drm_gem_shmem_release - Release resources associated with a shmem GEM object.
* @shmem: shmem GEM object
@@ -176,12 +200,8 @@ void drm_gem_shmem_release(struct drm_gem_shmem_object *shmem)
drm_WARN_ON(obj->dev, refcount_read(&shmem->vmap_use_count));
- if (shmem->sgt) {
- dma_unmap_sgtable(obj->dev->dev, shmem->sgt,
- DMA_BIDIRECTIONAL, 0);
- sg_free_table(shmem->sgt);
- kfree(shmem->sgt);
- }
+ if (shmem->sgt)
+ __drm_gem_shmem_free_sgt_locked(shmem);
if (shmem->pages)
drm_gem_shmem_put_pages_locked(shmem);
diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem_helper.h
index 5ccdae21b94a9..b2c23af628e1a 100644
--- a/include/drm/drm_gem_shmem_helper.h
+++ b/include/drm/drm_gem_shmem_helper.h
@@ -111,6 +111,7 @@ int drm_gem_shmem_init(struct drm_device *dev, struct drm_gem_shmem_object *shme
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);
+void __drm_gem_shmem_free_sgt_locked(struct drm_gem_shmem_object *shmem);
void drm_gem_shmem_put_pages_locked(struct drm_gem_shmem_object *shmem);
int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem);
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v12 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked()
2026-04-21 23:52 [PATCH v12 0/5] Rust bindings for gem shmem Lyude Paul
2026-04-21 23:52 ` [PATCH v12 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs Lyude Paul
2026-04-21 23:52 ` [PATCH v12 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
@ 2026-04-21 23:52 ` Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-21 23:52 ` [PATCH v12 4/5] rust: drm: gem: Introduce shmem::SGTable Lyude Paul
` (2 subsequent siblings)
5 siblings, 1 reply; 14+ messages in thread
From: Lyude Paul @ 2026-04-21 23:52 UTC (permalink / raw)
To: nouveau, Gary Guo, Daniel Almeida, rust-for-linux,
Danilo Krummrich, dri-devel
Cc: Matthew Maurer, FUJITA Tomonori, Lorenzo Stoakes,
christian.koenig, Asahi Lina, Miguel Ojeda, Andreas Hindborg,
Simona Vetter, Alice Ryhl, Boqun Feng, Sumit Semwal,
Krishna Ketan Rai, linux-media, Shankari Anand, David Airlie,
Benno Lossin, Viresh Kumar, linaro-mm-sig, Asahi Lina,
Greg Kroah-Hartman, kernel
We will need this for implementing a set of SGTable bindings in Rust for
gem shmem objects, so that we can use the dma_resv lock to protect
additional resources in the shmem object.
Signed-off-by: Lyude Paul <lyude@redhat.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
---
drivers/gpu/drm/drm_gem_shmem_helper.c | 16 +++++++++++++++-
include/drm/drm_gem_shmem_helper.h | 1 +
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index d2c34a0e573a1..8003ede197eba 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -786,12 +786,25 @@ struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem)
}
EXPORT_SYMBOL_GPL(drm_gem_shmem_get_sg_table);
-static struct sg_table *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_object *shmem)
+/**
+ * drm_gem_shmem_get_pages_sgt_locked - Under dma_resv lock, provide a scatter/gather table of
+ * pinned pages for an shmem GEM object.
+ * @shmem: shmem GEM object
+ *
+ * This function is the same as drm_gem_shmem_get_pages_sgt, except that the caller is expected to
+ * already hold the dma_resv lock for @shmem.
+ *
+ * Returns:
+ * A pointer to the scatter/gather table of pinned pages, or error pointer on failure.
+ */
+struct sg_table *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_object *shmem)
{
struct drm_gem_object *obj = &shmem->base;
int ret;
struct sg_table *sgt;
+ dma_resv_assert_held(shmem->base.resv);
+
if (shmem->sgt)
return shmem->sgt;
@@ -822,6 +835,7 @@ static struct sg_table *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_
drm_gem_shmem_put_pages_locked(shmem);
return ERR_PTR(ret);
}
+EXPORT_SYMBOL_GPL(drm_gem_shmem_get_pages_sgt_locked);
/**
* drm_gem_shmem_get_pages_sgt - Pin pages, dma map them, and return a
diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem_helper.h
index b2c23af628e1a..682207ce9d1b5 100644
--- a/include/drm/drm_gem_shmem_helper.h
+++ b/include/drm/drm_gem_shmem_helper.h
@@ -138,6 +138,7 @@ void drm_gem_shmem_purge_locked(struct drm_gem_shmem_object *shmem);
struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem);
struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem);
+struct sg_table *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_object *shmem);
void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
struct drm_printer *p, unsigned int indent);
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v12 4/5] rust: drm: gem: Introduce shmem::SGTable
2026-04-21 23:52 [PATCH v12 0/5] Rust bindings for gem shmem Lyude Paul
` (2 preceding siblings ...)
2026-04-21 23:52 ` [PATCH v12 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked() Lyude Paul
@ 2026-04-21 23:52 ` Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-21 23:52 ` [PATCH v12 5/5] rust: drm: gem: Add vmap functions to shmem bindings Lyude Paul
2026-04-22 22:05 ` Claude review: Rust bindings for gem shmem Claude Code Review Bot
5 siblings, 1 reply; 14+ messages in thread
From: Lyude Paul @ 2026-04-21 23:52 UTC (permalink / raw)
To: nouveau, Gary Guo, Daniel Almeida, rust-for-linux,
Danilo Krummrich, dri-devel
Cc: Matthew Maurer, FUJITA Tomonori, Lorenzo Stoakes,
christian.koenig, Asahi Lina, Miguel Ojeda, Andreas Hindborg,
Simona Vetter, Alice Ryhl, Boqun Feng, Sumit Semwal,
Krishna Ketan Rai, linux-media, Shankari Anand, David Airlie,
Benno Lossin, Viresh Kumar, linaro-mm-sig, Asahi Lina,
Greg Kroah-Hartman, kernel
In order to do this, we need to be careful to ensure that any interface we
expose for scatterlists ensures that any mappings created from one are
destroyed on driver-unbind. To do this, we introduce a Devres resource into
shmem::Object that we use in order to ensure that we release any SGTable
mappings on driver-unbind. We store this in an UnsafeCell and protect
access to it using the dma_resv lock that we already have from the shmem
gem object, which is the same lock that currently protects
drm_gem_object_shmem->sgt.
We also provide two different methods for acquiring an sg table:
self.sg_table(), and self.owned_sg_table(). The first function is for
short-term uses of mapped SGTables, the second is for callers that need to
hold onto the mapped SGTable for an extended period of time. The second
variant uses Devres of course, whereas the first simply relies on rust's
borrow checker to prevent driver-unbind when using the mapped SGTable.
Signed-off-by: Lyude Paul <lyude@redhat.com>
---
V3:
* Rename OwnedSGTable to shmem::SGTable. Since the current version of the
SGTable abstractions now has a `Owned` and `Borrowed` variant, I think
renaming this to shmem::SGTable makes things less confusing.
We do however, keep the name of owned_sg_table() as-is.
V4:
* Clarify safety comments for SGTable to explain why the object is
thread-safe.
* Rename from SGTableRef to SGTable
V10:
* Use Devres in order to ensure that SGTables are revocable, and are
unmapped on driver-unbind.
V11:
* s/create_sg_table()/get_sg_table()
* Get rid of extraneous `ret = ` in shmem::Object::get_sg_table()
V12:
* Actually move sgt_res in this patch and not the next one
rust/kernel/drm/gem/shmem.rs | 192 ++++++++++++++++++++++++++++++++++-
1 file changed, 190 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index 11749c36e8695..a477312c8a09b 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -11,25 +11,38 @@
use crate::{
container_of,
+ device::{
+ self,
+ Bound, //
+ },
+ devres::*,
drm::{
driver,
gem,
private::Sealed,
Device, //
},
- error::to_result,
+ error::{
+ from_err_ptr,
+ to_result, //
+ },
prelude::*,
+ scatterlist,
types::{
ARef,
Opaque, //
}, //
};
use core::{
+ cell::UnsafeCell,
ops::{
Deref,
DerefMut, //
},
- ptr::NonNull,
+ ptr::{
+ self,
+ NonNull, //
+ },
};
use gem::{
BaseObjectPrivate,
@@ -61,6 +74,11 @@ pub struct ObjectConfig<'a, T: DriverObject> {
#[repr(C)]
#[pin_data]
pub struct Object<T: DriverObject> {
+ /// Devres object for unmapping any SGTable on driver-unbind.
+ ///
+ /// This is protected by the object's dma_resv lock. It needs to be before `obj` to ensure that
+ /// it is destroyed before `obj` on `Drop`.
+ sgt_res: UnsafeCell<Option<Devres<SGTableMap<T>>>>,
#[pin]
obj: Opaque<bindings::drm_gem_shmem_object>,
/// Parent object that owns this object's DMA reservation object.
@@ -117,6 +135,7 @@ pub fn new(
try_pin_init!(Self {
obj <- Opaque::init_zeroed(),
parent_resv_obj: config.parent_resv_obj.map(|p| p.into()),
+ sgt_res: UnsafeCell::new(None),
inner <- T::new(dev, size, args),
}),
GFP_KERNEL,
@@ -176,6 +195,100 @@ extern "C" fn free_callback(obj: *mut bindings::drm_gem_object) {
// SAFETY: We're recovering the Kbox<> we created in gem_create_object()
let _ = unsafe { KBox::from_raw(this) };
}
+
+ // If necessary, create an SGTable for the gem object and register a Devres for it to ensure
+ // that it is unmapped on driver unbind.
+ fn get_sg_table<'a>(
+ &'a self,
+ dev: &'a device::Device<Bound>,
+ ) -> Result<&'a Devres<SGTableMap<T>>> {
+ let sgt_res_ptr = self.sgt_res.get();
+
+ // SAFETY: This lock is initialized throughout the lifetime of the gem object
+ unsafe { bindings::dma_resv_lock(self.raw_dma_resv(), ptr::null_mut()) };
+
+ // SAFETY: We just grabbed the lock required for reading this data above.
+ let sgt_res = unsafe { (*sgt_res_ptr).as_ref() };
+
+ let ret = if let Some(sgt_res) = sgt_res {
+ // We already have a Devres object for this sg table, return it
+ Ok(sgt_res)
+ } else {
+ // SAFETY: We grabbed the lock required for calling this function above */
+ let sgt = from_err_ptr(unsafe {
+ bindings::drm_gem_shmem_get_pages_sgt_locked(self.as_raw_shmem())
+ });
+
+ if let Err(e) = sgt {
+ Err(e)
+ } else {
+ // INVARIANT:
+ // - We called drm_gem_shmem_get_pages_sgt_locked above and checked that it
+ // succeeded, fulfilling the invariant of SGTableRef that the object's `sgt` field
+ // is initialized.
+ // - We store this Devres in the object itself and don't move it, ensuring that the
+ // object it points to remains valid for the lifetime of the SGTableRef.
+ let devres = Devres::new(dev, init!(SGTableMap { obj: self.into() }));
+ match devres {
+ Ok(devres) => {
+ // SAFETY: We acquired the lock protecting this data above, making it safe
+ // to write into here
+ unsafe { (*sgt_res_ptr) = Some(devres) };
+
+ // SAFETY: We just write Some() into *sgt_res_ptr above
+ Ok(unsafe { (&*sgt_res_ptr).as_ref().unwrap_unchecked() })
+ }
+ Err(e) => {
+ // We can't make sure that the pages for this object are unmapped on
+ // driver-unbind, so we need to release the sgt
+ // SAFETY:
+ // - We grabbed the lock required for calling this function above
+ // - We checked above that get_pages_sgt_locked() was successful
+ unsafe { bindings::__drm_gem_shmem_free_sgt_locked(self.as_raw_shmem()) };
+
+ Err(e)
+ }
+ }
+ }
+ };
+
+ // SAFETY: We're releasing the lock that we grabbed above.
+ unsafe { bindings::dma_resv_unlock(self.raw_dma_resv()) };
+
+ ret
+ }
+
+ /// Creates (if necessary) and returns an immutable reference to a scatter-gather table of DMA
+ /// pages for this object.
+ ///
+ /// This will pin the object in memory.
+ #[inline]
+ pub fn sg_table<'a>(
+ &'a self,
+ dev: &'a device::Device<Bound>,
+ ) -> Result<&'a scatterlist::SGTable> {
+ let sgt = self.get_sg_table(dev)?;
+
+ Ok(sgt.access(dev)?.deref())
+ }
+
+ /// Creates (if necessary) and returns an owned reference to a scatter-gather table of DMA pages
+ /// for this object.
+ ///
+ /// This is the same as [`sg_table`](Self::sg_table), except that it instead returns an
+ /// [`shmem::SGTable`] which holds a reference to the associated gem object, instead of a
+ /// reference to an [`scatterlist::SGTable`].
+ ///
+ /// This will pin the object in memory.
+ ///
+ /// [`shmem::SGTable`]: SGTable
+ pub fn owned_sg_table(&self, dev: &device::Device<Bound>) -> Result<SGTable<T>> {
+ self.get_sg_table(dev)?;
+
+ // INVARIANT: We just ensured above that `self.sgt_res` is initialized with
+ // `Some(Devres<SGTableMap<T>>)`.
+ Ok(SGTable(self.into()))
+ }
}
impl<T: DriverObject> Deref for Object<T> {
@@ -226,3 +339,78 @@ impl<T: DriverObject> driver::AllocImpl for Object<T> {
dumb_map_offset: None,
};
}
+
+/// 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.
+///
+/// # Invariants
+///
+/// - `self.obj` always points to a valid GEM object.
+/// - This object is proof that `self.0.owner.sgt` has an initialized and valid SGTable.
+pub struct SGTableMap<T: DriverObject> {
+ obj: NonNull<Object<T>>,
+}
+
+impl<T: DriverObject> Deref for SGTableMap<T> {
+ type Target = scatterlist::SGTable;
+
+ fn deref(&self) -> &Self::Target {
+ // SAFETY:
+ // - The NonNull is guaranteed to be valid via our type invariants.
+ // - The sgt field is guaranteed to be initialized and valid via our type invariants.
+ unsafe { scatterlist::SGTable::from_raw((*self.obj.as_ref().as_raw_shmem()).sgt) }
+ }
+}
+
+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() };
+
+ // SAFETY: The dma_resv for GEM objects is initialized throughout its lifetime
+ unsafe { bindings::dma_resv_lock(obj.raw_dma_resv(), ptr::null_mut()) };
+
+ // SAFETY: We acquired the lock needed for calling this function above
+ unsafe { bindings::__drm_gem_shmem_free_sgt_locked(obj.as_raw_shmem()) };
+
+ // SAFETY: We are releasing the lock we acquired above.
+ unsafe { bindings::dma_resv_unlock(obj.raw_dma_resv()) };
+ }
+}
+
+// SAFETY: The NonNull in SGTableRef is guaranteed valid by our type invariants, and the GEM object
+// it points to is guaranteed to be thread-safe.
+unsafe impl<T: DriverObject> Send for SGTableMap<T> {}
+// SAFETY: The NonNull in SGTableRef is guaranteed valid by our type invariants, and the GEM object
+// it points to is guaranteed to be thread-safe.
+unsafe impl<T: DriverObject> Sync for SGTableMap<T> {}
+
+/// An owned reference to a scatter-gather table of DMA address spans for a GEM shmem object.
+///
+/// This object holds an owned reference to the underlying GEM shmem object, ensuring that the
+/// [`scatterlist::SGTable`] referenced by this type remains valid for the lifetime of this object.
+///
+/// # Invariants
+///
+/// - This type is proof that `self.0.sgt_res` is initialized with a `Some(Devres<SGTableMap<T>>)`.
+/// - This object is only exposed in situations where we know the underlying `SGTable` will not be
+/// modified for the lifetime of this object. Thus, it is safe to send/access this type across
+/// threads.
+pub struct SGTable<T: DriverObject>(ARef<Object<T>>);
+
+// SAFETY: This object is thread-safe via our type invariants.
+unsafe impl<T: DriverObject> Send for SGTable<T> {}
+// SAFETY: This object is thread-safe via our type invariants.
+unsafe impl<T: DriverObject> Sync for SGTable<T> {}
+
+impl<T: DriverObject> Deref for SGTable<T> {
+ type Target = Devres<SGTableMap<T>>;
+
+ fn deref(&self) -> &Self::Target {
+ // SAFETY: `self.owner.sgt_res` is guaranteed to be initialized with
+ // `Some(Devres<SGTableMap<T>>)` via our type invariants
+ unsafe { (*self.0.sgt_res.get()).as_ref().unwrap_unchecked() }
+ }
+}
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v12 5/5] rust: drm: gem: Add vmap functions to shmem bindings
2026-04-21 23:52 [PATCH v12 0/5] Rust bindings for gem shmem Lyude Paul
` (3 preceding siblings ...)
2026-04-21 23:52 ` [PATCH v12 4/5] rust: drm: gem: Introduce shmem::SGTable Lyude Paul
@ 2026-04-21 23:52 ` Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-22 22:05 ` Claude review: Rust bindings for gem shmem Claude Code Review Bot
5 siblings, 1 reply; 14+ messages in thread
From: Lyude Paul @ 2026-04-21 23:52 UTC (permalink / raw)
To: nouveau, Gary Guo, Daniel Almeida, rust-for-linux,
Danilo Krummrich, dri-devel
Cc: Matthew Maurer, FUJITA Tomonori, Lorenzo Stoakes,
christian.koenig, Asahi Lina, Miguel Ojeda, Andreas Hindborg,
Simona Vetter, Alice Ryhl, Boqun Feng, Sumit Semwal,
Krishna Ketan Rai, linux-media, Shankari Anand, David Airlie,
Benno Lossin, Viresh Kumar, linaro-mm-sig, Asahi Lina,
Greg Kroah-Hartman, kernel
One of the more obvious use cases for gem shmem objects is the ability to
create mappings into their contents. So, let's hook this up in our rust
bindings.
Similar to how we handle SGTables, we make sure there's two different types
of mappings: owned mappings (kernel::drm::gem::shmem::VMap) and borrowed
mappings (kernel::drm::gem::shmem::VMapRef).
Signed-off-by: Lyude Paul <lyude@redhat.com>
---
V7:
* Switch over to the new iosys map bindings that use the Io trait
V8:
* Get rid of iosys_map bindings for now, only support non-iomem types
* s/as_shmem()/as_raw_shmem()
V9:
* Get rid of some outdated comments I missed
* Add missing SIZE check to raw_vmap()
* Add a proper unit test that ensures that we actually validate SIZE at
compile-time.
Turns out it takes only 34 lines to make a boilerplate DRM driver for a
kunit test :)
* Add unit tests
* Add some missing #[inline]s
V10:
* Correct issue with iomem error path
We previously called raw_vunmap() if we got an iomem allocation, but
raw_vunmap() was written such that it assumed all allocations were sysmem
allocations. Fix this by just making raw_vunmap() accept a iosys_map.
V11:
* Use Alexandre's clever solution to remove the macros we were using for
maintaining two different VMap types.
* Change the order of items in Object<T> to ensure that sgt_res is always
dropped before obj.
* Fix typo in Object.raw_vmap()
rust/kernel/drm/gem/shmem.rs | 355 +++++++++++++++++++++++++++++++++++
1 file changed, 355 insertions(+)
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index a477312c8a09b..b96de8d33141d 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -26,6 +26,11 @@
from_err_ptr,
to_result, //
},
+ io::{
+ Io,
+ IoCapable,
+ IoKnownSize, //
+ },
prelude::*,
scatterlist,
types::{
@@ -35,6 +40,11 @@
};
use core::{
cell::UnsafeCell,
+ ffi::c_void,
+ mem::{
+ self,
+ MaybeUninit, //
+ },
ops::{
Deref,
DerefMut, //
@@ -45,6 +55,7 @@
},
};
use gem::{
+ BaseObject,
BaseObjectPrivate,
DriverObject,
IntoGEMObject, //
@@ -289,6 +300,84 @@ pub fn owned_sg_table(&self, dev: &device::Device<Bound>) -> Result<SGTable<T>>
// `Some(Devres<SGTableMap<T>>)`.
Ok(SGTable(self.into()))
}
+
+ /// Attempt to create a vmap from the gem object, and confirm the size of said vmap.
+ fn raw_vmap(&self, min_size: usize) -> Result<*mut c_void> {
+ if self.size() < min_size {
+ return Err(ENOSPC);
+ }
+
+ let mut map: MaybeUninit<bindings::iosys_map> = MaybeUninit::uninit();
+
+ // SAFETY: drm_gem_shmem_vmap can be called with the DMA reservation lock held
+ to_result(unsafe {
+ // TODO: see top of file
+ bindings::dma_resv_lock(self.raw_dma_resv(), ptr::null_mut());
+ let ret = bindings::drm_gem_shmem_vmap_locked(self.as_raw_shmem(), map.as_mut_ptr());
+ bindings::dma_resv_unlock(self.raw_dma_resv());
+ ret
+ })?;
+
+ // SAFETY: The call to drm_gem_shmem_vmap_locked succeeded above, so we are guaranteed that
+ // map is properly initialized.
+ let map = unsafe { map.assume_init() };
+
+ // XXX: We don't currently support iomem allocations
+ if map.is_iomem {
+ // SAFETY:
+ // - The vmap operation above succeeded, guaranteeing that `map` points to a valid
+ // memory mapping.
+ // - We checked that this is an iomem allocation, making it safe to read vaddr_iomem
+ unsafe { self.raw_vunmap(map) };
+
+ Err(ENOTSUPP)
+ } else {
+ // SAFETY: We checked that this is not an iomem allocation, making it safe to read vaddr
+ Ok(unsafe { map.__bindgen_anon_1.vaddr })
+ }
+ }
+
+ /// Unmap a vmap from the gem object.
+ ///
+ /// # Safety
+ ///
+ /// - The caller promises that `map` is a valid vmap on this gem object.
+ /// - The caller promises that the memory pointed to by map will no longer be accesed through
+ /// this instance.
+ unsafe fn raw_vunmap(&self, mut map: bindings::iosys_map) {
+ let resv = self.raw_dma_resv();
+
+ // SAFETY:
+ // - This function is safe to call with the DMA reservation lock held
+ // - Our `ARef` is proof that the underlying gem object here is initialized and thus safe to
+ // dereference.
+ unsafe {
+ // TODO: see top of file
+ bindings::dma_resv_lock(resv, ptr::null_mut());
+ bindings::drm_gem_shmem_vunmap_locked(self.as_raw_shmem(), &mut map);
+ bindings::dma_resv_unlock(resv);
+ }
+ }
+
+ /// Creates and returns a virtual kernel memory mapping for this object.
+ #[inline]
+ pub fn vmap<const SIZE: usize>(&self) -> Result<VMapRef<'_, T, SIZE>> {
+ Ok(VMap {
+ // INVARIANT: `raw_vmap()` checks that the gem object is at least as large as `SIZE`.
+ addr: self.raw_vmap(SIZE)?,
+ owner: self,
+ })
+ }
+
+ /// 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>> {
+ Ok(VMap {
+ // INVARIANT: `raw_vmap()` checks that the gem object is at least as large as `SIZE`.
+ addr: self.raw_vmap(SIZE)?,
+ owner: self.into(),
+ })
+ }
}
impl<T: DriverObject> Deref for Object<T> {
@@ -387,6 +476,155 @@ unsafe impl<T: DriverObject> Send for SGTableMap<T> {}
// it points to is guaranteed to be thread-safe.
unsafe impl<T: DriverObject> Sync for SGTableMap<T> {}
+macro_rules! impl_vmap_io_capable {
+ ($impl:ident, $ty:ty) => {
+ impl<D, R, const SIZE: usize> IoCapable<$ty> for $impl<D, R, SIZE>
+ where
+ D: DriverObject,
+ R: Deref<Target = Object<D>>,
+ {
+ #[inline(always)]
+ unsafe fn io_read(&self, address: usize) -> $ty {
+ let ptr = address as *mut $ty;
+
+ // SAFETY: The safety contract of `io_read` guarantees that address is a valid
+ // address within the bounds of `Self` of at least the size of $ty, and is properly
+ // aligned.
+ unsafe { ptr::read(ptr) }
+ }
+
+ #[inline(always)]
+ unsafe fn io_write(&self, value: $ty, address: usize) {
+ let ptr = address as *mut $ty;
+
+ // SAFETY: The safety contract of `io_write` guarantees that address is a valid
+ // address within the bounds of `Self` of at least the size of $ty, and is properly
+ // aligned.
+ unsafe { ptr::write(ptr, value) }
+ }
+ }
+ };
+}
+
+/// A reference to a virtual mapping for an shmem-based GEM object in kernel address space.
+///
+/// # Invariants
+///
+/// - The size of `owner` is >= SIZE.
+/// - The memory pointed to by addr remains valid at least until this object is dropped.
+pub struct VMap<D, R, const SIZE: usize = 0>
+where
+ D: DriverObject,
+ R: Deref<Target = Object<D>>,
+{
+ addr: *mut c_void,
+ owner: R,
+}
+
+/// An alias type for a reference to a shmem-based GEM object's VMap.
+pub type VMapRef<'a, D, const SIZE: usize = 0> = VMap<D, &'a Object<D>, SIZE>;
+
+/// An alias type for an owned reference to a shmem-based GEM object's VMap.
+pub type VMapOwned<D, const SIZE: usize = 0> = VMap<D, ARef<Object<D>>, SIZE>;
+
+impl<D, R, const SIZE: usize> VMap<D, R, SIZE>
+where
+ D: DriverObject,
+ R: Deref<Target = Object<D>>,
+{
+ /// Borrows a reference to the object that owns this virtual mapping.
+ #[inline(always)]
+ pub fn owner(&self) -> &Object<D> {
+ &self.owner
+ }
+}
+
+impl<D, R, const SIZE: usize> Drop for VMap<D, R, SIZE>
+where
+ D: DriverObject,
+ R: Deref<Target = Object<D>>,
+{
+ #[inline(always)]
+ fn drop(&mut self) {
+ // SAFETY:
+ // - Our existence is proof that this map was previously created using self.owner.
+ // - Since we are in Drop, we are guaranteed that no one will access the memory
+ // through this mapping after calling this.
+ unsafe {
+ self.owner.raw_vunmap(bindings::iosys_map {
+ is_iomem: false,
+ __bindgen_anon_1: bindings::iosys_map__bindgen_ty_1 { vaddr: self.addr },
+ })
+ };
+ }
+}
+
+impl<D, R, const SIZE: usize> Io for VMap<D, R, SIZE>
+where
+ D: DriverObject,
+ R: Deref<Target = Object<D>>,
+{
+ #[inline(always)]
+ fn addr(&self) -> usize {
+ self.addr as usize
+ }
+
+ #[inline(always)]
+ fn maxsize(&self) -> usize {
+ self.owner.size()
+ }
+}
+
+impl<D, R, const SIZE: usize> IoKnownSize for VMap<D, R, SIZE>
+where
+ D: DriverObject,
+ R: Deref<Target = Object<D>>,
+{
+ const MIN_SIZE: usize = SIZE;
+}
+
+impl_vmap_io_capable!(VMap, u8);
+impl_vmap_io_capable!(VMap, u16);
+impl_vmap_io_capable!(VMap, u32);
+#[cfg(CONFIG_64BIT)]
+impl_vmap_io_capable!(VMap, u64);
+
+impl<D: DriverObject, const SIZE: usize> Clone for VMapOwned<D, SIZE> {
+ #[inline]
+ fn clone(&self) -> Self {
+ // SAFETY: We have a successful vmap already, so this can't fail.
+ unsafe { self.owner.owned_vmap().unwrap_unchecked() }
+ }
+}
+
+impl<'a, D: DriverObject, const SIZE: usize> Clone for VMapRef<'a, D, SIZE> {
+ #[inline]
+ fn clone(&self) -> Self {
+ // SAFETY: We have a successful vmap already, so this can't fail.
+ unsafe { self.owner.vmap().unwrap_unchecked() }
+ }
+}
+
+impl<'a, D: DriverObject, const SIZE: usize> From<VMapRef<'a, D, SIZE>> for VMapOwned<D, SIZE> {
+ #[inline]
+ fn from(value: VMapRef<'a, D, SIZE>) -> Self {
+ let this = Self {
+ addr: value.addr,
+ owner: value.owner.into(),
+ };
+
+ mem::forget(value);
+ this
+ }
+}
+
+// SAFETY: VMap is thread-safe, and the fact that this VMap has an owned reference to the object
+// means this object will remain valid until dropped.
+unsafe impl<D: DriverObject, const SIZE: usize> Send for VMapOwned<D, SIZE> {}
+// SAFETY: VMap is thread-safe, and the fact that this VMap has an owned reference to the object
+// means this object will remain valid until dropped.
+unsafe impl<D: DriverObject, const SIZE: usize> Sync for VMapOwned<D, SIZE> {}
+
/// An owned reference to a scatter-gather table of DMA address spans for a GEM shmem object.
///
/// This object holds an owned reference to the underlying GEM shmem object, ensuring that the
@@ -414,3 +652,120 @@ fn deref(&self) -> &Self::Target {
unsafe { (*self.0.sgt_res.get()).as_ref().unwrap_unchecked() }
}
}
+
+#[kunit_tests(rust_drm_gem_shmem)]
+mod tests {
+ use super::*;
+ use crate::{
+ drm,
+ faux,
+ page::PAGE_SIZE, //
+ };
+
+ // The bare minimum needed to create a fake drm driver for kunit
+
+ #[pin_data]
+ struct KunitData {}
+ struct KunitDriver;
+ struct KunitFile;
+ #[pin_data]
+ struct KunitObject {}
+
+ const INFO: drm::DriverInfo = drm::DriverInfo {
+ major: 0,
+ minor: 0,
+ patchlevel: 0,
+ name: c"kunit",
+ desc: c"Kunit",
+ };
+
+ impl drm::file::DriverFile for KunitFile {
+ type Driver = KunitDriver;
+
+ fn open(_dev: &drm::Device<KunitDriver>) -> Result<Pin<KBox<Self>>> {
+ Ok(KBox::new(Self, GFP_KERNEL)?.into())
+ }
+ }
+
+ impl gem::DriverObject for KunitObject {
+ type Driver = KunitDriver;
+ type Args = ();
+
+ fn new(
+ _dev: &drm::Device<KunitDriver>,
+ _size: usize,
+ _args: Self::Args,
+ ) -> impl PinInit<Self, Error> {
+ try_pin_init!(KunitObject {})
+ }
+ }
+
+ #[vtable]
+ impl drm::Driver for KunitDriver {
+ type Data = KunitData;
+ type File = KunitFile;
+ type Object = Object<KunitObject>;
+
+ const INFO: drm::DriverInfo = INFO;
+ const IOCTLS: &'static [drm::ioctl::DrmIoctlDescriptor] = &[];
+ }
+
+ fn create_drm_dev() -> Result<(faux::Registration, ARef<drm::Device<KunitDriver>>)> {
+ // Create a faux DRM device so we can test gem object creation.
+ let data = try_pin_init!(KunitData {});
+ let dev = faux::Registration::new(c"Kunit", None)?;
+ let drm = drm::Device::<KunitDriver>::new(dev.as_ref(), data)?;
+
+ Ok((dev, drm))
+ }
+
+ #[test]
+ fn compile_time_vmap_sizes() -> Result {
+ let (_dev, drm) = create_drm_dev()?;
+
+ // Create a gem object to test with
+ let cfg_ = ObjectConfig::<KunitObject> {
+ map_wc: false,
+ parent_resv_obj: None,
+ };
+ let obj = Object::<KunitObject>::new(&drm, PAGE_SIZE, cfg_, ())?;
+
+ // Try creating a normal vmap
+ obj.vmap::<PAGE_SIZE>()?;
+
+ // Try creating a vmap that's smaller then the size we specified
+ obj.vmap::<{ PAGE_SIZE - 100 }>()?;
+
+ // Make sure creating a vmap that's too large fails
+ assert!(obj.vmap::<{ PAGE_SIZE + 200 }>().is_err());
+
+ Ok(())
+ }
+
+ #[test]
+ fn vmap_io() -> Result {
+ let (_dev, drm) = create_drm_dev()?;
+
+ // Create a gem object to test with
+ let cfg_ = ObjectConfig::<KunitObject> {
+ map_wc: false,
+ parent_resv_obj: None,
+ };
+ let obj = Object::<KunitObject>::new(&drm, PAGE_SIZE, cfg_, ())?;
+
+ let vmap = obj.vmap::<PAGE_SIZE>()?;
+
+ vmap.write8(0xDE, 0x0);
+ assert_eq!(vmap.read8(0x0), 0xDE);
+ vmap.write32(0xFFFFFFFF, 0x20);
+
+ assert_eq!(vmap.read32(0x20), 0xFFFFFFFF);
+
+ assert_eq!(vmap.read8(0x20), 0xFF);
+ assert_eq!(vmap.read8(0x21), 0xFF);
+ assert_eq!(vmap.read8(0x22), 0xFF);
+ assert_eq!(vmap.read8(0x23), 0xFF);
+
+ Ok(())
+ }
+}
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Claude review: Rust bindings for gem shmem
2026-04-21 23:52 [PATCH v12 0/5] Rust bindings for gem shmem Lyude Paul
` (4 preceding siblings ...)
2026-04-21 23:52 ` [PATCH v12 5/5] rust: drm: gem: Add vmap functions to shmem bindings Lyude Paul
@ 2026-04-22 22:05 ` Claude Code Review Bot
5 siblings, 0 replies; 14+ messages in thread
From: Claude Code Review Bot @ 2026-04-22 22:05 UTC (permalink / raw)
To: dri-devel-reviews
Overall Series Review
Subject: Rust bindings for gem shmem
Author: Lyude Paul <lyude@redhat.com>
Patches: 6
Reviewed: 2026-04-23T08:05:54.209470
---
This is v12 of the Rust bindings for GEM shmem, adding SGTable and VMap support on top of the existing shmem bindings. The series is well-structured: two C-side preparatory patches (extracting `__drm_gem_shmem_free_sgt_locked` and exporting `drm_gem_shmem_get_pages_sgt_locked`), followed by three Rust patches building SGTable and VMap abstractions.
The overall design is sound — using `Devres` to ensure SGTable mappings are revoked on driver unbind is the right approach for safety. The VMap abstraction with borrowed vs. owned variants and integration with the `Io` trait is clean.
There are a few concerns worth discussing, primarily around locking patterns in the Rust code (non-interruptible `dma_resv_lock` calls, lock not released on error paths), a naming mismatch in the C-side kdoc, and some safety reasoning that could be tightened. Nothing is a showstopper, but a couple of items warrant attention before merging.
---
Generated by Claude Code Patch Reviewer
^ permalink raw reply [flat|nested] 14+ messages in thread
* Claude review: rust: drm: gem: s/device::Device/Device/ for shmem.rs
2026-04-21 23:52 ` [PATCH v12 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs Lyude Paul
@ 2026-04-22 22:05 ` Claude Code Review Bot
0 siblings, 0 replies; 14+ messages in thread
From: Claude Code Review Bot @ 2026-04-22 22:05 UTC (permalink / raw)
To: dri-devel-reviews
Patch Review
Straightforward cleanup renaming `device::Device` to `Device` (using a direct import from `drm::Device`) and cleaning up a misplaced `//` comment. No issues.
Reviewed-by tag from Alexandre Courbot present. Looks good.
---
Generated by Claude Code Patch Reviewer
^ permalink raw reply [flat|nested] 14+ messages in thread
* Claude review: drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked()
2026-04-21 23:52 ` [PATCH v12 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
@ 2026-04-22 22:05 ` Claude Code Review Bot
0 siblings, 0 replies; 14+ messages in thread
From: Claude Code Review Bot @ 2026-04-22 22:05 UTC (permalink / raw)
To: dri-devel-reviews
Patch Review
Extracts the sgt teardown logic into a new exported function. The refactoring is clean and the function is correctly guarded with `dma_resv_assert_held()`.
**Naming mismatch in kdoc**: The kdoc comment header says `__drm_gem_shmem_release_sgt_locked` but the actual function is named `__drm_gem_shmem_free_sgt_locked`. The changelog mentions fixing this in V10, but the mismatch is still present:
```c
/**
* __drm_gem_shmem_release_sgt_locked - Unpin and DMA unmap pages, and release the
* cached scatter/gather table for an shmem GEM object.
```
vs:
```c
void __drm_gem_shmem_free_sgt_locked(struct drm_gem_shmem_object *shmem)
```
This should be `__drm_gem_shmem_free_sgt_locked` in the kdoc to match.
**No NULL check on shmem->sgt**: The original code in `drm_gem_shmem_release` checks `if (shmem->sgt)` before calling the teardown. The new `__drm_gem_shmem_free_sgt_locked()` does not check for NULL and will crash if called with `shmem->sgt == NULL`. The call site in `drm_gem_shmem_release` preserves the NULL guard, but external callers (the Rust bindings in patch 4) need to be careful. The Rust side does call this only after a successful `drm_gem_shmem_get_pages_sgt_locked`, so it should be fine in practice, but a defensive NULL check (or at least a `WARN_ON(!shmem->sgt)`) in the exported function would be safer.
**Kdoc says "Unpin" but function doesn't unpin**: The kdoc says "Unpin and DMA unmap pages" but the function only does DMA unmap + free the sgt structure. It does not call `drm_gem_shmem_put_pages_locked()`. This is accurate to the code behavior (the caller in `drm_gem_shmem_release` calls `put_pages_locked` separately), but the kdoc is misleading.
---
Generated by Claude Code Patch Reviewer
^ permalink raw reply [flat|nested] 14+ messages in thread
* Claude review: drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked()
2026-04-21 23:52 ` [PATCH v12 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked() Lyude Paul
@ 2026-04-22 22:05 ` Claude Code Review Bot
0 siblings, 0 replies; 14+ messages in thread
From: Claude Code Review Bot @ 2026-04-22 22:05 UTC (permalink / raw)
To: dri-devel-reviews
Patch Review
Clean export of the previously-static function with proper kdoc and `dma_resv_assert_held()` added. No issues. Looks good.
---
Generated by Claude Code Patch Reviewer
^ permalink raw reply [flat|nested] 14+ messages in thread
* Claude review: rust: drm: gem: Introduce shmem::SGTable
2026-04-21 23:52 ` [PATCH v12 4/5] rust: drm: gem: Introduce shmem::SGTable Lyude Paul
@ 2026-04-22 22:05 ` Claude Code Review Bot
0 siblings, 0 replies; 14+ messages in thread
From: Claude Code Review Bot @ 2026-04-22 22:05 UTC (permalink / raw)
To: dri-devel-reviews
Patch Review
This is the most complex and consequential patch. Several observations:
**1. Non-interruptible dma_resv_lock**: In `get_sg_table()` (line ~821 of the mbox):
```rust
unsafe { bindings::dma_resv_lock(self.raw_dma_resv(), ptr::null_mut()) };
```
This uses `dma_resv_lock()` which is non-interruptible. The C side uses `dma_resv_lock_interruptible()` in `drm_gem_shmem_get_pages_sgt()`. Using a non-interruptible lock means a user process cannot be killed while waiting for this lock. The TODO at the top of the file mentions future WW mutex support, but it's worth noting that this is a deliberate choice. At minimum, this should be documented as intentional.
**2. Lock not released on Devres::new failure before __drm_gem_shmem_free_sgt_locked**: Looking at the error path in `get_sg_table()`:
```rust
Err(e) => {
unsafe { bindings::__drm_gem_shmem_free_sgt_locked(self.as_raw_shmem()) };
Err(e)
}
```
This calls `__drm_gem_shmem_free_sgt_locked` while the dma_resv lock is still held (good, as the function requires it), and then the lock is released at the end of the function. This is correct.
**3. UnsafeCell + dma_resv lock for interior mutability**: The pattern of protecting `sgt_res: UnsafeCell<Option<Devres<SGTableMap<T>>>>` with the dma_resv lock is reasonable but unusual for Rust. The safety invariants are documented. However, there's a subtle concern: when `get_sg_table()` returns the `&Devres<SGTableMap<T>>` reference, the dma_resv lock has been *released*. The reference remains valid because the `Option` transitions from `None` to `Some` but never back (the `Devres` handles revocation internally). This reasoning should perhaps be made more explicit in a safety comment.
**4. SGTableMap::Drop acquires dma_resv_lock**: The `Drop` impl for `SGTableMap` acquires the dma_resv lock:
```rust
fn drop(&mut self) {
let obj = unsafe { self.obj.as_ref() };
unsafe { bindings::dma_resv_lock(obj.raw_dma_resv(), ptr::null_mut()) };
unsafe { bindings::__drm_gem_shmem_free_sgt_locked(obj.as_raw_shmem()) };
unsafe { bindings::dma_resv_unlock(obj.raw_dma_resv()) };
}
```
This is called from `Devres` on driver unbind. There's a question of whether the `NonNull<Object<T>>` in `SGTableMap` is still valid at that point. The `sgt_res` field in `Object<T>` is declared *before* `obj`, which the patch comment says "ensures that it is destroyed before `obj` on `Drop`". But `SGTableMap` holds a raw `NonNull` pointer back to the `Object<T>` — the `Drop` for `SGTableMap` (triggered via `Devres`) would be called during driver unbind, not during `Object<T>`'s drop. So this should be fine as long as the GEM object outlives the devres teardown, which it should via the `ARef` stored in `SGTable`. But the `SGTableMap` itself doesn't hold an `ARef` — it holds a raw `NonNull`. This seems like a potential use-after-free risk if the `Devres` fires after the GEM object has been freed. The safety argument needs to be that the GEM object's refcount keeps it alive as long as anyone holds an `SGTable` (which holds an `ARef`), and on driver unbind the `Devres` fires while the object is still alive. This is worth double-checking.
**5. C-style comment in Rust**: Minor nit — there's a C-style comment marker `*/` at the end of a Rust safety comment:
```rust
// SAFETY: We grabbed the lock required for calling this function above */
```
---
Generated by Claude Code Patch Reviewer
^ permalink raw reply [flat|nested] 14+ messages in thread
* Claude review: rust: drm: gem: Add vmap functions to shmem bindings
2026-04-21 23:52 ` [PATCH v12 5/5] rust: drm: gem: Add vmap functions to shmem bindings Lyude Paul
@ 2026-04-22 22:05 ` Claude Code Review Bot
0 siblings, 0 replies; 14+ messages in thread
From: Claude Code Review Bot @ 2026-04-22 22:05 UTC (permalink / raw)
To: dri-devel-reviews
Patch Review
**1. Clone for VMapOwned calls unwrap_unchecked**:
```rust
impl<D: DriverObject, const SIZE: usize> Clone for VMapOwned<D, SIZE> {
fn clone(&self) -> Self {
unsafe { self.owner.owned_vmap().unwrap_unchecked() }
}
}
```
The safety comment says "We have a successful vmap already, so this can't fail." This relies on the fact that `drm_gem_shmem_vmap_locked` uses a refcount (`vmap_use_count`) and will succeed if there's already an active vmap. This is true for the non-iomem path but should be documented more carefully — if the existing vmap was non-iomem, the cloned one will also be non-iomem (since it's the same cached mapping). The real risk is that between clone attempts the vmap could theoretically be released and re-acquired as iomem, but since we hold a reference (`self` exists), the refcount prevents that. The reasoning is sound but subtle.
**2. VMap::From<VMapRef> uses mem::forget**:
```rust
impl<'a, D: DriverObject, const SIZE: usize> From<VMapRef<'a, D, SIZE>> for VMapOwned<D, SIZE> {
fn from(value: VMapRef<'a, D, SIZE>) -> Self {
let this = Self {
addr: value.addr,
owner: value.owner.into(),
};
mem::forget(value);
this
}
}
```
This is correct — the `mem::forget` prevents double-vunmap since we're transferring ownership of the mapping. Good pattern.
**3. No Send/Sync for VMapRef**: `Send` and `Sync` are only implemented for `VMapOwned`, not `VMapRef`. This is correct since `VMapRef` borrows from the object and the lifetime prevents sending across threads.
**4. Tests are well-structured**: The kunit tests verify compile-time size checking and basic I/O operations. The test for `vmap::<{ PAGE_SIZE + 200 }>().is_err()` is a good sanity check. The `vmap_io` test verifying byte-level reads within a u32 write is good for catching endianness assumptions — though the expected byte values (`0xFF` at all four positions) would be the same on both big and little endian for `0xFFFFFFFF`, so it doesn't actually test endianness. A value like `0xDEADBEEF` would be more revealing, but this is minor.
**5. ENOTSUPP for iomem**: The use of `ENOTSUPP` (which is the internal kernel variant, not `EOPNOTSUPP`) is fine for internal kernel use. The comment `// XXX: We don't currently support iomem allocations` appropriately flags this as a known limitation.
---
Generated by Claude Code Patch Reviewer
^ permalink raw reply [flat|nested] 14+ messages in thread
* Claude review: drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked()
2026-04-21 23:40 ` [PATCH v11 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
@ 2026-04-22 22:09 ` Claude Code Review Bot
0 siblings, 0 replies; 14+ messages in thread
From: Claude Code Review Bot @ 2026-04-22 22:09 UTC (permalink / raw)
To: dri-devel-reviews
Patch Review
**Concern: Missing NULL check on `shmem->sgt`.**
The extracted function unconditionally dereferences `shmem->sgt`:
```c
void __drm_gem_shmem_free_sgt_locked(struct drm_gem_shmem_object *shmem)
{
dma_resv_assert_held(shmem->base.resv);
dma_unmap_sgtable(shmem->base.dev->dev, shmem->sgt, DMA_BIDIRECTIONAL, 0);
sg_free_table(shmem->sgt);
kfree(shmem->sgt);
shmem->sgt = NULL;
}
```
The original code in `drm_gem_shmem_release()` wrapped this with `if (shmem->sgt)`. The call site in `drm_gem_shmem_release` still checks `if (shmem->sgt)` before calling the new function, and the Rust callers (patch 4) also only call it when the sgt is known-valid. So this is safe in practice, but since the function is exported and documented, it would be more defensive to either:
- Add a NULL check inside the function, or
- Document in the kdoc that `shmem->sgt` must be non-NULL.
Currently the kdoc says "If the passed shmem object has an active scatter/gather table..." which implies it handles the NULL case, but it doesn't.
**Minor: Doc/function name mismatch in commit message.** The commit message references `__drm_gem_shmem_release_sgt_locked()` in the V10 changelog, but the actual function is `__drm_gem_shmem_free_sgt_locked()`. This is just the changelog, not the code, so it's a cosmetic issue only.
---
---
Generated by Claude Code Patch Reviewer
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2026-04-22 22:09 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-21 23:52 [PATCH v12 0/5] Rust bindings for gem shmem Lyude Paul
2026-04-21 23:52 ` [PATCH v12 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-21 23:52 ` [PATCH v12 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-21 23:52 ` [PATCH v12 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked() Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-21 23:52 ` [PATCH v12 4/5] rust: drm: gem: Introduce shmem::SGTable Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-21 23:52 ` [PATCH v12 5/5] rust: drm: gem: Add vmap functions to shmem bindings Lyude Paul
2026-04-22 22:05 ` Claude review: " Claude Code Review Bot
2026-04-22 22:05 ` Claude review: Rust bindings for gem shmem Claude Code Review Bot
-- strict thread matches above, loose matches on Subject: below --
2026-04-21 23:40 [PATCH v11 0/5] " Lyude Paul
2026-04-21 23:40 ` [PATCH v11 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
2026-04-22 22:09 ` Claude review: " Claude Code Review Bot
2026-04-09 0:12 [PATCH v10 0/5] Rust bindings for gem shmem Lyude Paul
2026-04-09 0:12 ` [PATCH v10 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
2026-04-12 2:01 ` Claude review: " Claude Code Review Bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox