* [PATCH v10 0/5] Rust bindings for gem shmem
@ 2026-04-09 0:12 Lyude Paul
2026-04-09 0:12 ` [PATCH v10 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs Lyude Paul
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Lyude Paul @ 2026-04-09 0:12 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:
(apparently it mistakenly was not sent to dri-devel, so no patchwork ):
https://patchwork.freedesktop.org/series/156093/
Branch with patches applied available here (+ a hack required to make
sure this builds:
https://gitlab.freedesktop.org/lyudess/linux/-/commits/rust/gem-shmem
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 | 553 ++++++++++++++++++++++++-
3 files changed, 590 insertions(+), 13 deletions(-)
base-commit: a7a080bb4236ebe577b6776d940d1717912ff6dd
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v10 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs
2026-04-09 0:12 [PATCH v10 0/5] Rust bindings for gem shmem Lyude Paul
@ 2026-04-09 0:12 ` Lyude Paul
2026-04-09 0:12 ` [PATCH v10 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Lyude Paul @ 2026-04-09 0:12 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>
---
rust/kernel/drm/gem/shmem.rs | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index d025fb0351954..c643f18b20838 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, //
+ 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] 6+ messages in thread
* [PATCH v10 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked()
2026-04-09 0:12 [PATCH v10 0/5] Rust bindings for gem shmem Lyude Paul
2026-04-09 0:12 ` [PATCH v10 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs Lyude Paul
@ 2026-04-09 0:12 ` Lyude Paul
2026-04-09 0:12 ` [PATCH v10 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked() Lyude Paul
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Lyude Paul @ 2026-04-09 0:12 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>
---
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..addf8c1e5341e 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] 6+ messages in thread
* [PATCH v10 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked()
2026-04-09 0:12 [PATCH v10 0/5] Rust bindings for gem shmem Lyude Paul
2026-04-09 0:12 ` [PATCH v10 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs 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-09 0:12 ` Lyude Paul
2026-04-09 0:12 ` [PATCH v10 4/5] rust: drm: gem: Introduce shmem::SGTable Lyude Paul
2026-04-09 0:12 ` [PATCH v10 5/5] rust: drm: gem: Add vmap functions to shmem bindings Lyude Paul
4 siblings, 0 replies; 6+ messages in thread
From: Lyude Paul @ 2026-04-09 0:12 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>
---
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 addf8c1e5341e..15de7f6cc4981 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_sg_table - 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 poitner 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] 6+ messages in thread
* [PATCH v10 4/5] rust: drm: gem: Introduce shmem::SGTable
2026-04-09 0:12 [PATCH v10 0/5] Rust bindings for gem shmem Lyude Paul
` (2 preceding siblings ...)
2026-04-09 0:12 ` [PATCH v10 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked() Lyude Paul
@ 2026-04-09 0:12 ` Lyude Paul
2026-04-09 0:12 ` [PATCH v10 5/5] rust: drm: gem: Add vmap functions to shmem bindings Lyude Paul
4 siblings, 0 replies; 6+ messages in thread
From: Lyude Paul @ 2026-04-09 0:12 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.
rust/kernel/drm/gem/shmem.rs | 191 ++++++++++++++++++++++++++++++++++-
1 file changed, 189 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index c643f18b20838..111be446213df 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,
@@ -65,6 +78,10 @@ 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.
+ ///
+ /// This is protected by the object's dma_resv lock.
+ sgt_res: UnsafeCell<Option<Devres<SGTableMap<T>>>>,
#[pin]
inner: T,
}
@@ -117,6 +134,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 +194,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 create_sg_table<'a>(
+ &'a self,
+ dev: &'a device::Device<Bound>,
+ ) -> Result<&'a Devres<SGTableMap<T>>> {
+ let ret;
+ 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() };
+ if let Some(sgt_res) = sgt_res {
+ // We already have a Devres object for this sg table, return it
+ ret = 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 {
+ ret = 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
+ ret = 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()) };
+
+ ret = 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.create_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.create_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 +338,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] 6+ messages in thread
* [PATCH v10 5/5] rust: drm: gem: Add vmap functions to shmem bindings
2026-04-09 0:12 [PATCH v10 0/5] Rust bindings for gem shmem Lyude Paul
` (3 preceding siblings ...)
2026-04-09 0:12 ` [PATCH v10 4/5] rust: drm: gem: Introduce shmem::SGTable Lyude Paul
@ 2026-04-09 0:12 ` Lyude Paul
4 siblings, 0 replies; 6+ messages in thread
From: Lyude Paul @ 2026-04-09 0:12 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.
rust/kernel/drm/gem/shmem.rs | 354 +++++++++++++++++++++++++++++++++++
1 file changed, 354 insertions(+)
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index 111be446213df..6c186d83f528e 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, //
@@ -288,6 +299,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_vunmap_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(VMapRef {
+ // 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<VMap<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> {
@@ -386,6 +475,154 @@ 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 $(, $lifetime:lifetime )?) => {
+ impl<$( $lifetime ,)? D: DriverObject, const SIZE: usize> IoCapable<$ty>
+ for $impl<$( $lifetime ,)? D, SIZE>
+ {
+ #[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) }
+ }
+ }
+ };
+}
+
+// Implement various traits common to both VMap types
+macro_rules! impl_vmap_common {
+ ($impl:ident $(, $lifetime:lifetime )?) => {
+ impl<$( $lifetime ,)? D, const SIZE: usize> $impl<$( $lifetime ,)? D, SIZE>
+ where
+ D: DriverObject,
+ {
+ /// Borrows a reference to the object that owns this virtual mapping.
+ #[inline(always)]
+ pub fn owner(&self) -> &Object<D> {
+ &self.owner
+ }
+ }
+
+ impl<$( $lifetime ,)? D, const SIZE: usize> Drop for $impl<$( $lifetime ,)? D, SIZE>
+ where
+ D: DriverObject,
+ {
+ #[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<$( $lifetime ,)? D, const SIZE: usize> Io for $impl<$( $lifetime ,)? D, SIZE>
+ where
+ D: DriverObject,
+ {
+ #[inline(always)]
+ fn addr(&self) -> usize {
+ self.addr as usize
+ }
+
+ #[inline(always)]
+ fn maxsize(&self) -> usize {
+ self.owner.size()
+ }
+ }
+
+ impl<$( $lifetime ,)? D, const SIZE: usize> IoKnownSize for $impl<$( $lifetime ,)? D, SIZE>
+ where
+ D: DriverObject,
+ {
+ const MIN_SIZE: usize = SIZE;
+ }
+
+ impl_vmap_io_capable!($impl, u8 $( , $lifetime )?);
+ impl_vmap_io_capable!($impl, u16 $( , $lifetime )?);
+ impl_vmap_io_capable!($impl, u32 $( , $lifetime )?);
+ #[cfg(CONFIG_64BIT)]
+ impl_vmap_io_capable!($impl, u64 $( , $lifetime )?);
+ };
+}
+
+/// An owned reference to a virtual mapping for a 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: DriverObject, const SIZE: usize = 0> {
+ addr: *mut c_void,
+ owner: ARef<Object<D>>,
+}
+
+impl_vmap_common!(VMap);
+
+impl<D: DriverObject, const SIZE: usize> Clone for VMap<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> From<VMapRef<'a, D, SIZE>> for VMap<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: addr is guaranteed to be valid and accessible for the lifetime of VMap, ensuring its
+// safe to send across threads.
+unsafe impl<D: DriverObject, const SIZE: usize> Send for VMap<D, SIZE> {}
+// SAFETY: addr is guaranteed to be valid and accessible for the lifetime of VMap, ensuring its
+// safe to send across threads.
+unsafe impl<D: DriverObject, const SIZE: usize> Sync for VMap<D, SIZE> {}
+
+/// A borrowed reference to a virtual mapping for a shmem-based GEM object in kernel address space.
+pub struct VMapRef<'a, D: DriverObject, const SIZE: usize = 0> {
+ addr: *mut c_void,
+ owner: &'a Object<D>,
+}
+
+impl_vmap_common!(VMapRef, 'a);
+
+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() }
+ }
+}
+
/// 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
@@ -413,3 +650,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] 6+ messages in thread
end of thread, other threads:[~2026-04-09 0:17 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-09 0:12 [PATCH v10 0/5] Rust bindings for gem shmem Lyude Paul
2026-04-09 0:12 ` [PATCH v10 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs 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-09 0:12 ` [PATCH v10 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked() Lyude Paul
2026-04-09 0:12 ` [PATCH v10 4/5] rust: drm: gem: Introduce shmem::SGTable Lyude Paul
2026-04-09 0:12 ` [PATCH v10 5/5] rust: drm: gem: Add vmap functions to shmem bindings Lyude Paul
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox