* [PATCH 1/6] rust: iommu: move the protection flags to the module root
2026-11-12 17:00 [PATCH 0/6] rust: iommu: add a paging domain abstraction Daniel Almeida
@ 2026-11-12 17:00 ` Daniel Almeida
2026-11-12 17:00 ` [PATCH 2/6] rust: scatterlist: make SGTable::as_raw() visible to the crate Daniel Almeida
` (6 subsequent siblings)
7 siblings, 0 replies; 23+ messages in thread
From: Daniel Almeida @ 2026-11-12 17:00 UTC (permalink / raw)
To: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, 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, Abdiel Janulgue
Cc: iommu, rust-for-linux, linux-kernel, driver-core,
Detlev Casanova, Nicolas Dufresne
The protection flags are the IOMMU_* flags from include/linux/iommu.h,
and are not specific to io-pgtable. Domain::map(), added later in this
series, takes them too.
Move them to kernel::iommu and re-export them from the pgtable module,
so that existing users do not need to change.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
rust/kernel/iommu/mod.rs | 16 ++++++++++++++++
rust/kernel/iommu/pgtable.rs | 16 +---------------
2 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/rust/kernel/iommu/mod.rs b/rust/kernel/iommu/mod.rs
index 1423d7b19b57..afc9cfffb656 100644
--- a/rust/kernel/iommu/mod.rs
+++ b/rust/kernel/iommu/mod.rs
@@ -3,3 +3,19 @@
//! Rust support related to IOMMU.
pub mod pgtable;
+
+/// Protection flags used with IOMMU mappings.
+pub mod prot {
+ /// Read access.
+ pub const READ: u32 = bindings::IOMMU_READ;
+ /// Write access.
+ pub const WRITE: u32 = bindings::IOMMU_WRITE;
+ /// Request cache coherency.
+ pub const CACHE: u32 = bindings::IOMMU_CACHE;
+ /// Request no-execute permission.
+ pub const NOEXEC: u32 = bindings::IOMMU_NOEXEC;
+ /// MMIO peripheral mapping.
+ pub const MMIO: u32 = bindings::IOMMU_MMIO;
+ /// Privileged mapping.
+ pub const PRIVILEGED: u32 = bindings::IOMMU_PRIV;
+}
diff --git a/rust/kernel/iommu/pgtable.rs b/rust/kernel/iommu/pgtable.rs
index 5f9b42ca92c2..695b62eb4835 100644
--- a/rust/kernel/iommu/pgtable.rs
+++ b/rust/kernel/iommu/pgtable.rs
@@ -23,21 +23,7 @@
use bindings::io_pgtable_fmt;
-/// Protection flags used with IOMMU mappings.
-pub mod prot {
- /// Read access.
- pub const READ: u32 = bindings::IOMMU_READ;
- /// Write access.
- pub const WRITE: u32 = bindings::IOMMU_WRITE;
- /// Request cache coherency.
- pub const CACHE: u32 = bindings::IOMMU_CACHE;
- /// Request no-execute permission.
- pub const NOEXEC: u32 = bindings::IOMMU_NOEXEC;
- /// MMIO peripheral mapping.
- pub const MMIO: u32 = bindings::IOMMU_MMIO;
- /// Privileged mapping.
- pub const PRIVILEGED: u32 = bindings::IOMMU_PRIV;
-}
+pub use super::prot;
/// Represents a requested `io_pgtable` configuration.
pub struct Config {
--
2.56.0
^ permalink raw reply [flat|nested] 23+ messages in thread* [PATCH 2/6] rust: scatterlist: make SGTable::as_raw() visible to the crate
2026-11-12 17:00 [PATCH 0/6] rust: iommu: add a paging domain abstraction Daniel Almeida
2026-11-12 17:00 ` [PATCH 1/6] rust: iommu: move the protection flags to the module root Daniel Almeida
@ 2026-11-12 17:00 ` Daniel Almeida
2026-11-12 17:00 ` [PATCH 3/6] rust: iommu: add the paging domain abstraction Daniel Almeida
` (5 subsequent siblings)
7 siblings, 0 replies; 23+ messages in thread
From: Daniel Almeida @ 2026-11-12 17:00 UTC (permalink / raw)
To: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, 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, Abdiel Janulgue
Cc: iommu, rust-for-linux, linux-kernel, driver-core,
Detlev Casanova, Nicolas Dufresne
SGTable::as_raw() is private to the scatterlist module. Domain::map(),
added later in this series, needs the raw table to pass its scatterlist
to iommu_map_sg().
Make it pub(crate).
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
rust/kernel/scatterlist.rs | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/scatterlist.rs b/rust/kernel/scatterlist.rs
index 3a5250f85000..a68a9bbb38ed 100644
--- a/rust/kernel/scatterlist.rs
+++ b/rust/kernel/scatterlist.rs
@@ -147,8 +147,9 @@ pub unsafe fn from_raw<'a>(ptr: *mut bindings::sg_table) -> &'a Self {
unsafe { &*ptr.cast() }
}
+ /// Obtain the raw `struct sg_table *`.
#[inline]
- fn as_raw(&self) -> *mut bindings::sg_table {
+ pub(crate) fn as_raw(&self) -> *mut bindings::sg_table {
self.inner.0.get()
}
--
2.56.0
^ permalink raw reply [flat|nested] 23+ messages in thread* [PATCH 3/6] rust: iommu: add the paging domain abstraction
2026-11-12 17:00 [PATCH 0/6] rust: iommu: add a paging domain abstraction Daniel Almeida
2026-11-12 17:00 ` [PATCH 1/6] rust: iommu: move the protection flags to the module root Daniel Almeida
2026-11-12 17:00 ` [PATCH 2/6] rust: scatterlist: make SGTable::as_raw() visible to the crate Daniel Almeida
@ 2026-11-12 17:00 ` Daniel Almeida
2026-11-12 17:00 ` [PATCH 4/6] rust: iommu: add Domain::attach() Daniel Almeida
` (4 subsequent siblings)
7 siblings, 0 replies; 23+ messages in thread
From: Daniel Almeida @ 2026-11-12 17:00 UTC (permalink / raw)
To: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, 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, Abdiel Janulgue
Cc: iommu, rust-for-linux, linux-kernel, driver-core,
Detlev Casanova, Nicolas Dufresne
Add Domain, a paging domain that is allocated for a bound device and
freed on drop. It cannot outlive the device's binding. It will be used
by the upcoming rkvenc driver to give each session its own IOMMU domain.
This is needed as rkvenc's submission model will let userspace set most
of the encoder's registers directly, so the kernel cannot check every
memory access that a job makes the encoder do.
With a single domain, one session could make the encoder read or write
the memory of another session. With a domain per session, the encoder
can only reach the memory of the session whose job it is running, and
any other access faults.
Domain::new() is unsafe. The caller must ensure that the IOMMU driver
behind its device sets a domain up fully when it is allocated and
allows concurrent use of it.
Attaching, mapping and fault handling are added in the following
patches. Attaching and mapping rely on that requirement.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
rust/kernel/iommu/domain.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++
rust/kernel/iommu/mod.rs | 2 ++
2 files changed, 82 insertions(+)
diff --git a/rust/kernel/iommu/domain.rs b/rust/kernel/iommu/domain.rs
new file mode 100644
index 000000000000..2e66becde156
--- /dev/null
+++ b/rust/kernel/iommu/domain.rs
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! IOMMU paging domains.
+//!
+//! An IOMMU domain is an address space: the devices attached to it share one translation from
+//! I/O virtual addresses (IOVAs) to physical addresses. In a paging domain, that translation
+//! consists of ranges that are mapped with `iommu_map()` and unmapped with `iommu_unmap()`.
+//!
+//! C header: [`include/linux/iommu.h`](srctree/include/linux/iommu.h)
+
+use core::{
+ marker::PhantomData,
+ ptr::NonNull, //
+};
+
+use crate::{
+ bindings,
+ device::{
+ Bound,
+ Device, //
+ },
+ error::from_err_ptr,
+ prelude::*, //
+};
+
+/// An IOMMU paging domain.
+///
+/// An IOMMU domain is an address space: the devices attached to it share one translation from
+/// I/O virtual addresses (IOVAs) to physical addresses. In a paging domain, that translation
+/// consists of ranges that are mapped with `iommu_map()` and unmapped with `iommu_unmap()`.
+///
+/// The domain is allocated for a bound device and cannot outlive that device's binding. It is
+/// freed when dropped, which may sleep.
+///
+/// # Invariants
+///
+/// `ptr` points to a paging domain allocated by `iommu_paging_domain_alloc_flags()` and owned by
+/// this object.
+pub struct Domain<'a> {
+ ptr: NonNull<bindings::iommu_domain>,
+ _dev: PhantomData<&'a Device<Bound>>,
+}
+
+// SAFETY: A `struct iommu_domain` is not tied to the thread that allocated it.
+unsafe impl<'a> Send for Domain<'a> {}
+
+impl<'a> Domain<'a> {
+ /// Allocates a paging domain for `dev`.
+ ///
+ /// # Safety
+ ///
+ /// The IOMMU driver that `dev` is behind must:
+ ///
+ /// - Set a domain up fully when the domain is allocated, and keep it and its mappings until
+ /// the domain is freed, whether or not a device is attached.
+ /// - Allow one domain to be attached, detached, mapped and unmapped from several threads at
+ /// once.
+ pub unsafe fn new(dev: &'a Device<Bound>) -> Result<Self> {
+ // SAFETY: `dev` is a valid device, bound for `'a`.
+ let ptr =
+ from_err_ptr(unsafe { bindings::iommu_paging_domain_alloc_flags(dev.as_raw(), 0) })?;
+
+ // SAFETY: `iommu_paging_domain_alloc_flags()` never returns NULL.
+ let ptr = unsafe { NonNull::new_unchecked(ptr) };
+
+ // INVARIANT: `ptr` was allocated by `iommu_paging_domain_alloc_flags()` and is owned by
+ // the returned object.
+ Ok(Self {
+ ptr,
+ _dev: PhantomData,
+ })
+ }
+}
+
+impl<'a> Drop for Domain<'a> {
+ fn drop(&mut self) {
+ // SAFETY: By the type invariant, `self.ptr` is a domain owned by `self`.
+ unsafe { bindings::iommu_domain_free(self.ptr.as_ptr()) };
+ }
+}
diff --git a/rust/kernel/iommu/mod.rs b/rust/kernel/iommu/mod.rs
index afc9cfffb656..b6e96b8d7af8 100644
--- a/rust/kernel/iommu/mod.rs
+++ b/rust/kernel/iommu/mod.rs
@@ -2,6 +2,8 @@
//! Rust support related to IOMMU.
+#[cfg(CONFIG_IOMMU_API)]
+pub mod domain;
pub mod pgtable;
/// Protection flags used with IOMMU mappings.
--
2.56.0
^ permalink raw reply [flat|nested] 23+ messages in thread* [PATCH 4/6] rust: iommu: add Domain::attach()
2026-11-12 17:00 [PATCH 0/6] rust: iommu: add a paging domain abstraction Daniel Almeida
` (2 preceding siblings ...)
2026-11-12 17:00 ` [PATCH 3/6] rust: iommu: add the paging domain abstraction Daniel Almeida
@ 2026-11-12 17:00 ` Daniel Almeida
2026-11-16 21:54 ` Detlev Casanova
2026-11-12 17:00 ` [PATCH 5/6] rust: iommu: add Domain::map() Daniel Almeida
` (3 subsequent siblings)
7 siblings, 1 reply; 23+ messages in thread
From: Daniel Almeida @ 2026-11-12 17:00 UTC (permalink / raw)
To: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, 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, Abdiel Janulgue
Cc: iommu, rust-for-linux, linux-kernel, driver-core,
Detlev Casanova, Nicolas Dufresne
The upcoming rkvenc driver will use IOMMU domains to isolate different
sessions.
Add Domain::attach(), which attaches the domain to a device and returns
an Attachment that detaches it on drop. Devices that share their IOMMU
group with other devices are not supported.
attach() is unsafe. The caller must be the device's driver, and the
device must stop using the domain before the Attachment is dropped.
The Attachment must not be leaked, since the domain could then be
freed while it is still attached.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
rust/kernel/iommu/domain.rs | 57 +++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 55 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/iommu/domain.rs b/rust/kernel/iommu/domain.rs
index 2e66becde156..2a5b66f2a5d9 100644
--- a/rust/kernel/iommu/domain.rs
+++ b/rust/kernel/iommu/domain.rs
@@ -19,7 +19,10 @@
Bound,
Device, //
},
- error::from_err_ptr,
+ error::{
+ from_err_ptr,
+ to_result, //
+ },
prelude::*, //
};
@@ -44,6 +47,11 @@ pub struct Domain<'a> {
// SAFETY: A `struct iommu_domain` is not tied to the thread that allocated it.
unsafe impl<'a> Send for Domain<'a> {}
+// SAFETY: `&Domain` allows the following from several threads at once:
+// - Attaching and detaching the domain. `iommu_attach_device()` and `iommu_detach_device()` may
+// be called concurrently for the same domain.
+unsafe impl<'a> Sync for Domain<'a> {}
+
impl<'a> Domain<'a> {
/// Allocates a paging domain for `dev`.
///
@@ -70,11 +78,56 @@ pub unsafe fn new(dev: &'a Device<Bound>) -> Result<Self> {
_dev: PhantomData,
})
}
+
+ /// Attaches the domain to `dev`.
+ ///
+ /// This may sleep.
+ ///
+ /// # Safety
+ ///
+ /// - The caller must be the driver bound to `dev`.
+ /// - The caller must ensure that `dev` stops using the domain before the returned
+ /// [`Attachment`] is dropped.
+ /// - The caller must not `mem::forget()` the returned [`Attachment`] or otherwise prevent its
+ /// [`Drop`] implementation from running.
+ pub unsafe fn attach<'d>(&'d self, dev: &'d Device<Bound>) -> Result<Attachment<'d>> {
+ // SAFETY: By the type invariant, `self.ptr` is a valid domain, and `dev` is a valid
+ // device. The caller is the driver bound to `dev`.
+ to_result(unsafe { bindings::iommu_attach_device(self.ptr.as_ptr(), dev.as_raw()) })?;
+
+ // INVARIANT: The domain is attached to `dev`.
+ Ok(Attachment { domain: self, dev })
+ }
}
impl<'a> Drop for Domain<'a> {
fn drop(&mut self) {
- // SAFETY: By the type invariant, `self.ptr` is a domain owned by `self`.
+ // SAFETY: By the type invariant, `self.ptr` is a domain owned by `self`. Every
+ // `Attachment` borrows `self`, and the caller of `attach()` promised not to leak it, so
+ // the domain is attached to no device.
unsafe { bindings::iommu_domain_free(self.ptr.as_ptr()) };
}
}
+
+/// A [`Domain`] attached to a device.
+///
+/// An attachment cannot outlive the domain or the binding of the device it is attached to.
+/// Dropping it detaches the domain from the device, which may sleep.
+///
+/// # Invariants
+///
+/// `domain` is attached to `dev`.
+#[must_use = "the domain is detached when the attachment is dropped"]
+pub struct Attachment<'d> {
+ domain: &'d Domain<'d>,
+ dev: &'d Device<Bound>,
+}
+
+impl<'d> Drop for Attachment<'d> {
+ fn drop(&mut self) {
+ // SAFETY: By the type invariant, `self.domain` is attached to `self.dev`. The caller of
+ // `Domain::attach()` is the driver bound to `self.dev`, and promised that the device
+ // stopped using the domain.
+ unsafe { bindings::iommu_detach_device(self.domain.ptr.as_ptr(), self.dev.as_raw()) };
+ }
+}
--
2.56.0
^ permalink raw reply [flat|nested] 23+ messages in thread* Re: [PATCH 4/6] rust: iommu: add Domain::attach()
2026-11-12 17:00 ` [PATCH 4/6] rust: iommu: add Domain::attach() Daniel Almeida
@ 2026-11-16 21:54 ` Detlev Casanova
0 siblings, 0 replies; 23+ messages in thread
From: Detlev Casanova @ 2026-11-16 21:54 UTC (permalink / raw)
To: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, 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, Abdiel Janulgue,
Daniel Almeida
Cc: iommu, rust-for-linux, linux-kernel, driver-core, Nicolas Dufresne
Hi !
On Thursday, 12 November 2026 12:00:11 EST Daniel Almeida wrote:
> The upcoming rkvenc driver will use IOMMU domains to isolate different
> sessions.
>
> Add Domain::attach(), which attaches the domain to a device and returns
> an Attachment that detaches it on drop. Devices that share their IOMMU
> group with other devices are not supported.
Same wording issue here, and in the patch.
Even though this will work for our encoder usage (devices <->domain mappings
will be 1:1), this doesn't match what the iommu driver does, which can attach
multiple devices to a single iommu_domain.
Detlev.
> attach() is unsafe. The caller must be the device's driver, and the
> device must stop using the domain before the Attachment is dropped.
> The Attachment must not be leaked, since the domain could then be
> freed while it is still attached.
>
> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
> ---
> rust/kernel/iommu/domain.rs | 57
> +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55
> insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/iommu/domain.rs b/rust/kernel/iommu/domain.rs
> index 2e66becde156..2a5b66f2a5d9 100644
> --- a/rust/kernel/iommu/domain.rs
> +++ b/rust/kernel/iommu/domain.rs
> @@ -19,7 +19,10 @@
> Bound,
> Device, //
> },
> - error::from_err_ptr,
> + error::{
> + from_err_ptr,
> + to_result, //
> + },
> prelude::*, //
> };
>
> @@ -44,6 +47,11 @@ pub struct Domain<'a> {
> // SAFETY: A `struct iommu_domain` is not tied to the thread that allocated
> it. unsafe impl<'a> Send for Domain<'a> {}
>
> +// SAFETY: `&Domain` allows the following from several threads at once:
> +// - Attaching and detaching the domain. `iommu_attach_device()` and
> `iommu_detach_device()` may +// be called concurrently for the same
> domain.
> +unsafe impl<'a> Sync for Domain<'a> {}
> +
> impl<'a> Domain<'a> {
> /// Allocates a paging domain for `dev`.
> ///
> @@ -70,11 +78,56 @@ pub unsafe fn new(dev: &'a Device<Bound>) ->
> Result<Self> { _dev: PhantomData,
> })
> }
> +
> + /// Attaches the domain to `dev`.
> + ///
> + /// This may sleep.
> + ///
> + /// # Safety
> + ///
> + /// - The caller must be the driver bound to `dev`.
> + /// - The caller must ensure that `dev` stops using the domain before
> the returned + /// [`Attachment`] is dropped.
> + /// - The caller must not `mem::forget()` the returned [`Attachment`]
> or otherwise prevent its + /// [`Drop`] implementation from running.
> + pub unsafe fn attach<'d>(&'d self, dev: &'d Device<Bound>) ->
> Result<Attachment<'d>> { + // SAFETY: By the type invariant,
> `self.ptr` is a valid domain, and `dev` is a valid + // device. The
> caller is the driver bound to `dev`.
> + to_result(unsafe { bindings::iommu_attach_device(self.ptr.as_ptr(),
> dev.as_raw()) })?; +
> + // INVARIANT: The domain is attached to `dev`.
> + Ok(Attachment { domain: self, dev })
> + }
> }
>
> impl<'a> Drop for Domain<'a> {
> fn drop(&mut self) {
> - // SAFETY: By the type invariant, `self.ptr` is a domain owned by
> `self`. + // SAFETY: By the type invariant, `self.ptr` is a domain
> owned by `self`. Every + // `Attachment` borrows `self`, and the
> caller of `attach()` promised not to leak it, so + // the domain is
> attached to no device.
> unsafe { bindings::iommu_domain_free(self.ptr.as_ptr()) };
> }
> }
> +
> +/// A [`Domain`] attached to a device.
> +///
> +/// An attachment cannot outlive the domain or the binding of the device it
> is attached to. +/// Dropping it detaches the domain from the device, which
> may sleep. +///
> +/// # Invariants
> +///
> +/// `domain` is attached to `dev`.
> +#[must_use = "the domain is detached when the attachment is dropped"]
> +pub struct Attachment<'d> {
> + domain: &'d Domain<'d>,
> + dev: &'d Device<Bound>,
> +}
> +
> +impl<'d> Drop for Attachment<'d> {
> + fn drop(&mut self) {
> + // SAFETY: By the type invariant, `self.domain` is attached to
> `self.dev`. The caller of + // `Domain::attach()` is the driver
> bound to `self.dev`, and promised that the device + // stopped using
> the domain.
> + unsafe { bindings::iommu_detach_device(self.domain.ptr.as_ptr(),
> self.dev.as_raw()) }; + }
> +}
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 5/6] rust: iommu: add Domain::map()
2026-11-12 17:00 [PATCH 0/6] rust: iommu: add a paging domain abstraction Daniel Almeida
` (3 preceding siblings ...)
2026-11-12 17:00 ` [PATCH 4/6] rust: iommu: add Domain::attach() Daniel Almeida
@ 2026-11-12 17:00 ` Daniel Almeida
2026-11-12 17:00 ` [PATCH 6/6] rust: iommu: add Domain::with_fault_handler() Daniel Almeida
` (2 subsequent siblings)
7 siblings, 0 replies; 23+ messages in thread
From: Daniel Almeida @ 2026-11-12 17:00 UTC (permalink / raw)
To: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, 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, Abdiel Janulgue
Cc: iommu, rust-for-linux, linux-kernel, driver-core,
Detlev Casanova, Nicolas Dufresne
The upcoming rkvenc driver will map the buffers of a session into that
session's IOMMU domain.
Add Domain::map(), which maps pages into the domain at an IOVA chosen
by the caller, and returns a Mapping that unmaps them on drop.
The pages come from a MapSource, a new trait for anything that can
provide an SGTable and keep its pages allocated. The Mapping owns its
source, so the pages cannot be freed while they are mapped. MapSource
is initially implemented for a boxed SGTable that owns its pages.
map() is unsafe. As with IoPageTable::map_pages(), mapped ranges must
not overlap, and nothing else may access a range while it is being
mapped.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
rust/kernel/iommu/domain.rs | 182 +++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 181 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/iommu/domain.rs b/rust/kernel/iommu/domain.rs
index 2a5b66f2a5d9..ac9947d5063b 100644
--- a/rust/kernel/iommu/domain.rs
+++ b/rust/kernel/iommu/domain.rs
@@ -23,7 +23,11 @@
from_err_ptr,
to_result, //
},
- prelude::*, //
+ prelude::*,
+ scatterlist::{
+ Owned,
+ SGTable, //
+ }, //
};
/// An IOMMU paging domain.
@@ -39,6 +43,76 @@
///
/// `ptr` points to a paging domain allocated by `iommu_paging_domain_alloc_flags()` and owned by
/// this object.
+///
+/// # Examples
+///
+/// Two clients share one device. Each has its own domain, and the device is switched from one
+/// to the other between jobs.
+///
+/// ```
+/// use kernel::{
+/// device::{
+/// Bound,
+/// Device, //
+/// },
+/// dma,
+/// iommu::{
+/// domain::{
+/// Domain,
+/// MapSource, //
+/// },
+/// prot, //
+/// },
+/// page,
+/// prelude::*,
+/// scatterlist::SGTable, //
+/// };
+///
+/// fn new_buffer(dev: &Device<Bound>) -> Result<impl MapSource + 'static> {
+/// let pages = VVec::<u8>::with_capacity(4 * page::PAGE_SIZE, GFP_KERNEL)?;
+///
+/// KBox::pin_init(
+/// SGTable::new(dev, pages, dma::DataDirection::Bidirectional, GFP_KERNEL),
+/// GFP_KERNEL,
+/// )
+/// }
+///
+/// fn test(dev: &Device<Bound>) -> Result {
+/// // SAFETY: The IOMMU driver that `dev` is behind meets the requirements of `Domain::new()`.
+/// let domain_a = unsafe { Domain::new(dev)? };
+/// // SAFETY: The IOMMU driver that `dev` is behind meets the requirements of `Domain::new()`.
+/// let domain_b = unsafe { Domain::new(dev)? };
+///
+/// // Both clients map their buffer at the same IOVA.
+/// let iova = 0xdead_0000;
+///
+/// // SAFETY: We are the driver bound to `dev`. The attachment is dropped below, after `dev`
+/// // has stopped.
+/// let attachment = unsafe { domain_a.attach(dev)? };
+///
+/// let buffer = new_buffer(dev)?;
+/// // SAFETY: `domain_a` has no other mapping, and nothing else uses it.
+/// let _mapping_a = unsafe { domain_a.map(iova, buffer, prot::READ | prot::WRITE)? };
+///
+/// // Run a job of the first client, then stop the device.
+///
+/// // `attach()` allows one domain on a device at a time, so `domain_a` is detached first.
+/// drop(attachment);
+///
+/// // SAFETY: We are the driver bound to `dev`. The attachment is dropped when this function
+/// // returns, after `dev` has stopped.
+/// let _attachment = unsafe { domain_b.attach(dev)? };
+///
+/// let buffer = new_buffer(dev)?;
+/// // SAFETY: `domain_b` has no other mapping, and nothing else uses it.
+/// let _mapping_b = unsafe { domain_b.map(iova, buffer, prot::READ | prot::WRITE)? };
+///
+/// // Run a job of the second client, then stop the device. With `domain_b` attached, `iova`
+/// // leads to the second buffer.
+///
+/// Ok(())
+/// }
+/// ```
pub struct Domain<'a> {
ptr: NonNull<bindings::iommu_domain>,
_dev: PhantomData<&'a Device<Bound>>,
@@ -50,6 +124,8 @@ unsafe impl<'a> Send for Domain<'a> {}
// SAFETY: `&Domain` allows the following from several threads at once:
// - Attaching and detaching the domain. `iommu_attach_device()` and `iommu_detach_device()` may
// be called concurrently for the same domain.
+// - Mapping and unmapping ranges. `iommu_map_sg()` and `iommu_unmap()` may be called concurrently
+// for the same domain, except in the cases that the safety requirements of `map()` exclude.
unsafe impl<'a> Sync for Domain<'a> {}
impl<'a> Domain<'a> {
@@ -98,6 +174,60 @@ pub unsafe fn attach<'d>(&'d self, dev: &'d Device<Bound>) -> Result<Attachment<
// INVARIANT: The domain is attached to `dev`.
Ok(Attachment { domain: self, dev })
}
+
+ /// Maps the pages of `source` at `iova` with the protection flags `prot` (see [`prot`]).
+ ///
+ /// The returned [`Mapping`] owns `source` and unmaps the range when it is dropped. A leaked
+ /// [`Mapping`] leaves its range mapped. Its source is leaked with it and must keep the pages
+ /// allocated, so `S` cannot borrow them and has to be `'static`.
+ ///
+ /// This performs no cache coherency operations on the pages. It may sleep.
+ ///
+ /// [`prot`]: super::prot
+ ///
+ /// # Safety
+ ///
+ /// - The range must not overlap any other mapping in the domain.
+ /// - No other operation on the domain may access the range while this function runs.
+ pub unsafe fn map<'d, S: MapSource + 'static>(
+ &'d self,
+ iova: usize,
+ source: S,
+ prot: u32,
+ ) -> Result<Mapping<'d, S>> {
+ let sgt = source.sg_table().as_raw();
+
+ // SAFETY: `sgt` points to the table of `source`, which is valid.
+ let (sgl, nents) = unsafe { ((*sgt).sgl, (*sgt).orig_nents) };
+
+ // SAFETY:
+ // - By the type invariant, `self.ptr` is a valid domain.
+ // - `sgl` has `nents` entries. By the safety requirements of `MapSource`, they describe
+ // pages that stay allocated for as long as the returned `Mapping` owns `source`.
+ // - The caller meets the safety requirements of this function.
+ let ret = unsafe {
+ bindings::iommu_map_sg(
+ self.ptr.as_ptr(),
+ iova,
+ sgl,
+ nents,
+ prot as i32,
+ GFP_KERNEL.as_raw(),
+ )
+ };
+ if ret < 0 {
+ return Err(Error::from_errno(ret as i32));
+ }
+
+ // INVARIANT: `iommu_map_sg()` mapped `[iova, iova + ret)`, and only the returned object
+ // unmaps it.
+ Ok(Mapping {
+ domain: self,
+ iova,
+ size: ret as usize,
+ _source: source,
+ })
+ }
}
impl<'a> Drop for Domain<'a> {
@@ -131,3 +261,53 @@ fn drop(&mut self) {
unsafe { bindings::iommu_detach_device(self.domain.ptr.as_ptr(), self.dev.as_raw()) };
}
}
+
+/// A source of pages that can be mapped into a [`Domain`].
+///
+/// # Safety
+///
+/// Every entry of the table that [`MapSource::sg_table`] returns must describe pages. The table
+/// must not change and its pages must stay allocated for as long as `self` exists.
+pub unsafe trait MapSource {
+ /// Returns the table describing the pages to map.
+ fn sg_table(&self) -> &SGTable;
+}
+
+// SAFETY: An `SGTable<Owned<P>>` is built from pages, and keeps them and its table until it is
+// dropped.
+unsafe impl<P> MapSource for Pin<KBox<SGTable<Owned<P>>>> {
+ fn sg_table(&self) -> &SGTable {
+ self
+ }
+}
+
+/// Pages mapped into a [`Domain`].
+///
+/// Dropping a mapping unmaps its range, then drops its source.
+///
+/// # Invariants
+///
+/// `[iova, iova + size)` is mapped in `domain`, and only this object unmaps it.
+#[must_use = "the range is unmapped when the mapping is dropped"]
+pub struct Mapping<'d, S> {
+ domain: &'d Domain<'d>,
+ iova: usize,
+ size: usize,
+ _source: S,
+}
+
+impl<'d, S> Mapping<'d, S> {
+ /// Returns the first IOVA of the mapping.
+ #[inline]
+ pub fn iova(&self) -> usize {
+ self.iova
+ }
+}
+
+impl<'d, S> Drop for Mapping<'d, S> {
+ fn drop(&mut self) {
+ // SAFETY: By the type invariant, `[self.iova, self.iova + self.size)` is mapped in
+ // `self.domain`, and nothing else unmaps it.
+ unsafe { bindings::iommu_unmap(self.domain.ptr.as_ptr(), self.iova, self.size) };
+ }
+}
--
2.56.0
^ permalink raw reply [flat|nested] 23+ messages in thread* [PATCH 6/6] rust: iommu: add Domain::with_fault_handler()
2026-11-12 17:00 [PATCH 0/6] rust: iommu: add a paging domain abstraction Daniel Almeida
` (4 preceding siblings ...)
2026-11-12 17:00 ` [PATCH 5/6] rust: iommu: add Domain::map() Daniel Almeida
@ 2026-11-12 17:00 ` Daniel Almeida
2026-11-16 19:38 ` [PATCH 0/6] rust: iommu: add a paging domain abstraction Jason Gunthorpe
2026-11-16 21:54 ` Detlev Casanova
7 siblings, 0 replies; 23+ messages in thread
From: Daniel Almeida @ 2026-11-12 17:00 UTC (permalink / raw)
To: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, 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, Abdiel Janulgue
Cc: iommu, rust-for-linux, linux-kernel, driver-core,
Detlev Casanova, Nicolas Dufresne
A driver is not told when its device faults in the IOMMU unless its
domain has a fault handler. The upcoming rkvenc driver needs to know,
so that it can stop the encoder and fail the job of the client whose
domain faulted.
Add Domain::with_fault_handler(), which allocates a domain and installs
a FaultHandler on it with iommu_set_fault_handler(). The handler is
borrowed for the lifetime of the domain. One handler can serve several
domains, which it tells apart by the DomainId it receives.
The handler may run in atomic context.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
rust/kernel/iommu/domain.rs | 112 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 112 insertions(+)
diff --git a/rust/kernel/iommu/domain.rs b/rust/kernel/iommu/domain.rs
index ac9947d5063b..351a8e77fa71 100644
--- a/rust/kernel/iommu/domain.rs
+++ b/rust/kernel/iommu/domain.rs
@@ -21,6 +21,7 @@
},
error::{
from_err_ptr,
+ from_result,
to_result, //
},
prelude::*,
@@ -126,6 +127,8 @@ unsafe impl<'a> Send for Domain<'a> {}
// be called concurrently for the same domain.
// - Mapping and unmapping ranges. `iommu_map_sg()` and `iommu_unmap()` may be called concurrently
// for the same domain, except in the cases that the safety requirements of `map()` exclude.
+// - Getting the identifier of the domain. `id()` takes the address held in `ptr` and does not
+// dereference it.
unsafe impl<'a> Sync for Domain<'a> {}
impl<'a> Domain<'a> {
@@ -155,6 +158,39 @@ pub unsafe fn new(dev: &'a Device<Bound>) -> Result<Self> {
})
}
+ /// Allocates a paging domain for `dev` that reports its faults to `handler`.
+ ///
+ /// A handler can serve several domains, which [`Domain::id`] tells apart.
+ ///
+ /// # Safety
+ ///
+ /// This function has the same safety requirements as [`Domain::new`].
+ pub unsafe fn with_fault_handler<H: FaultHandler>(
+ dev: &'a Device<Bound>,
+ handler: &'a H,
+ ) -> Result<Self> {
+ // SAFETY: The caller meets the safety requirements of `Domain::new()`.
+ let domain = unsafe { Self::new(dev)? };
+
+ // SAFETY: By the type invariant, `domain.ptr` is a valid domain, which nothing else uses
+ // yet. `handler` outlives `'a`, and thus the domain.
+ unsafe {
+ bindings::iommu_set_fault_handler(
+ domain.ptr.as_ptr(),
+ Some(fault_callback::<H>),
+ core::ptr::from_ref(handler).cast_mut().cast(),
+ )
+ };
+
+ Ok(domain)
+ }
+
+ /// Returns the identifier that [`FaultHandler::fault`] receives for faults on this domain.
+ #[inline]
+ pub fn id(&self) -> DomainId {
+ DomainId(self.ptr.as_ptr().addr())
+ }
+
/// Attaches the domain to `dev`.
///
/// This may sleep.
@@ -311,3 +347,79 @@ fn drop(&mut self) {
unsafe { bindings::iommu_unmap(self.domain.ptr.as_ptr(), self.iova, self.size) };
}
}
+
+/// Identifies a [`Domain`] in fault reports.
+///
+/// Two identifiers are equal if they identify the same domain. An identifier can be reused once
+/// its domain is freed.
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub struct DomainId(usize);
+
+/// A handler for the IOMMU faults of a [`Domain`], see [`Domain::with_fault_handler`].
+///
+/// Only IOMMU drivers that report faults with `report_iommu_fault()` call it.
+///
+/// # Examples
+///
+/// ```
+/// use kernel::{
+/// device::{
+/// Bound,
+/// Device, //
+/// },
+/// iommu::domain::{
+/// Domain,
+/// DomainId,
+/// FaultHandler, //
+/// },
+/// prelude::*,
+/// sync::atomic::{
+/// Atomic,
+/// Relaxed, //
+/// }, //
+/// };
+///
+/// struct Faults {
+/// count: Atomic<u32>,
+/// }
+///
+/// impl FaultHandler for Faults {
+/// fn fault(&self, _domain: DomainId, _iova: usize, _write: bool) -> Result {
+/// self.count.add(1, Relaxed);
+/// Ok(())
+/// }
+/// }
+///
+/// fn test<'a>(dev: &'a Device<Bound>, faults: &'a Faults) -> Result<Domain<'a>> {
+/// // SAFETY: The IOMMU driver that `dev` is behind meets the requirements of `Domain::new()`.
+/// unsafe { Domain::with_fault_handler(dev, faults) }
+/// }
+/// ```
+pub trait FaultHandler: Sync {
+ /// Handles a faulting access to `iova` through the domain identified by `domain`. `write` is
+ /// true if the IOMMU driver reported a write access.
+ ///
+ /// This may be executed in atomic context and must not sleep. Implementations return `Ok(())`
+ /// if the fault was handled. The result is passed to the IOMMU driver.
+ fn fault(&self, domain: DomainId, iova: usize, write: bool) -> Result;
+}
+
+/// # Safety
+///
+/// `token` must be the `&H` that [`Domain::with_fault_handler`] registered for `domain`.
+unsafe extern "C" fn fault_callback<H: FaultHandler>(
+ domain: *mut bindings::iommu_domain,
+ _dev: *mut bindings::device,
+ iova: c_ulong,
+ flags: c_int,
+ token: *mut c_void,
+) -> c_int {
+ // SAFETY: By the safety requirements, `token` is a `&H` that outlives `domain`.
+ let handler = unsafe { &*token.cast_const().cast::<H>() };
+ let write = flags & cv!(bindings::IOMMU_FAULT_WRITE => c_int) != 0;
+
+ from_result(|| {
+ handler.fault(DomainId(domain.addr()), iova, write)?;
+ Ok(0)
+ })
+}
--
2.56.0
^ permalink raw reply [flat|nested] 23+ messages in thread* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-12 17:00 [PATCH 0/6] rust: iommu: add a paging domain abstraction Daniel Almeida
` (5 preceding siblings ...)
2026-11-12 17:00 ` [PATCH 6/6] rust: iommu: add Domain::with_fault_handler() Daniel Almeida
@ 2026-11-16 19:38 ` Jason Gunthorpe
2026-11-17 20:09 ` Daniel Almeida
2026-11-16 21:54 ` Detlev Casanova
7 siblings, 1 reply; 23+ messages in thread
From: Jason Gunthorpe @ 2026-11-16 19:38 UTC (permalink / raw)
To: Daniel Almeida
Cc: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova,
Nicolas Dufresne
On Thu, Nov 12, 2026 at 02:00:07PM -0300, Daniel Almeida wrote:
> This series adds a Rust abstraction for IOMMU paging domains. A driver
> can allocate a domain, attach it to its device, map pages into it, and
> install a handler for its faults.
>
> This will be used by the upcoming rkvenc driver that my colleagues
> Detlev Casanova, Nicolas Dufresne and I are working on. More details on
> this driver can be found by watching Nicolas Dufresne's talk at LPC 2026
> [0].
>
> As part of the security model, each client will have its own IOMMU
> domain. This is needed as the submission model will let userspace set
> most of the encoder's registers directly, so the kernel cannot check
> every memory access that a job makes the encoder do. With a single
> domain, one client could make the encoder read or write the memory of
> another client.
How do you plan to get the paging domain in the first place?
Using paging domains directly is very complicated, I'm not excite d to
see DRM trying to do this at all. IMHO there are many unanswered
questions how it could even do this safely.
It would be better to come with a user so we can try to understand
what you are even planning.
> Patch 6 adds Domain::with_fault_handler(), which allocates a domain
> with a FaultHandler installed. One handler can serve several domains.
You intend to use fault handler?? That's even more esoteric.
Jason
^ permalink raw reply [flat|nested] 23+ messages in thread* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-16 19:38 ` [PATCH 0/6] rust: iommu: add a paging domain abstraction Jason Gunthorpe
@ 2026-11-17 20:09 ` Daniel Almeida
2026-11-17 20:20 ` Jason Gunthorpe
0 siblings, 1 reply; 23+ messages in thread
From: Daniel Almeida @ 2026-11-17 20:09 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova,
Nicolas Dufresne
Hi Jason,
> On 16 Nov 2026, at 16:38, Jason Gunthorpe <jgg@ziepe.ca> wrote:
>
> On Thu, Nov 12, 2026 at 02:00:07PM -0300, Daniel Almeida wrote:
>> This series adds a Rust abstraction for IOMMU paging domains. A driver
>> can allocate a domain, attach it to its device, map pages into it, and
>> install a handler for its faults.
>>
>> This will be used by the upcoming rkvenc driver that my colleagues
>> Detlev Casanova, Nicolas Dufresne and I are working on. More details on
>> this driver can be found by watching Nicolas Dufresne's talk at LPC 2026
>> [0].
>>
>> As part of the security model, each client will have its own IOMMU
>> domain. This is needed as the submission model will let userspace set
>> most of the encoder's registers directly, so the kernel cannot check
>> every memory access that a job makes the encoder do. With a single
>> domain, one client could make the encoder read or write the memory of
>> another client.
>
> How do you plan to get the paging domain in the first place?
We plan to alloc a paging domain when a video encoder session is created. Then
we call iommu_attach_device() before a job runs, and detach back to the default
domain after it finishes so the next job can attach its own session's domain.
This is so each session is isolated from each other.
>
> Using paging domains directly is very complicated, I'm not excite d to
> see DRM trying to do this at all. IMHO there are many unanswered
> questions how it could even do this safely.
In a very simplified way, this is not unlike how GPU drivers currently work. In
fact, from a cursory look, this seems to be how rocket (the NPU driver) works
too. Since we are merely porting the same ideas to video codec hardware, I am a bit
surprised by your discontentment.
(Note, though, that I have never worked on IOMMU drivers, so it is possible that
I am missing some context or some history here, in which case, sorry.)
In any case, we need this isolation due to the submission model used.
Most Vulkan drivers know their hardware pretty well, so a level of abstraction
on top of it like V4L2 is unnecessary. Therefore, in order to achieve a leaner
kernel driver, userspace programs most of the encoder's registers directly, so
the kernel cannot validate every address a job makes the encoder access.
Instead of validating, we rely on each session having its own address space, so
a job from session X can only reach memory that was mapped for session X, and
so misbehaving sessions cannot affect one another. You cannot read or write
buffers from another session unless the buffer is explicitly shared.
Again, this is pretty similar to GPU drivers, where a VM_BIND ioctl can bind
a given buffer range to an address range in a specific address space.
> It would be better to come with a user so we can try to understand
> what you are even planning.
We do have a user, it is just work in progress. We will likely post an
RFC soon (i.e.: next few weeks I assume).
>
>> Patch 6 adds Domain::with_fault_handler(), which allocates a domain
>> with a FaultHandler installed. One handler can serve several domains.
>
> You intend to use fault handler?? That's even more esoteric.
>
> Jason
The fault handler is used in the same way it is used in GPU drivers: to kill
sessions if they fault. This way the driver can simply refuse further work from
a misbehaving client, for example. It is also the natural place to enqueue a
reset if the codec hardware is frozen as a result of the fault.
What in this model is fundamentally incompatible with how you expect paging
domains to be used? In other words, what exactly do you mean when you say:
> IMHO there are many unanswered questions how it could even do this safely.
-- Daniel
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-17 20:09 ` Daniel Almeida
@ 2026-11-17 20:20 ` Jason Gunthorpe
2026-11-18 15:25 ` Nicolas Dufresne
2026-11-18 19:11 ` Daniel Almeida
0 siblings, 2 replies; 23+ messages in thread
From: Jason Gunthorpe @ 2026-11-17 20:20 UTC (permalink / raw)
To: Daniel Almeida
Cc: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova,
Nicolas Dufresne
On Tue, Nov 17, 2026 at 05:09:11PM -0300, Daniel Almeida wrote:
> Hi Jason,
>
> > On 16 Nov 2026, at 16:38, Jason Gunthorpe <jgg@ziepe.ca> wrote:
> >
> > On Thu, Nov 12, 2026 at 02:00:07PM -0300, Daniel Almeida wrote:
> >> This series adds a Rust abstraction for IOMMU paging domains. A driver
> >> can allocate a domain, attach it to its device, map pages into it, and
> >> install a handler for its faults.
> >>
> >> This will be used by the upcoming rkvenc driver that my colleagues
> >> Detlev Casanova, Nicolas Dufresne and I are working on. More details on
> >> this driver can be found by watching Nicolas Dufresne's talk at LPC 2026
> >> [0].
> >>
> >> As part of the security model, each client will have its own IOMMU
> >> domain. This is needed as the submission model will let userspace set
> >> most of the encoder's registers directly, so the kernel cannot check
> >> every memory access that a job makes the encoder do. With a single
> >> domain, one client could make the encoder read or write the memory of
> >> another client.
> >
> > How do you plan to get the paging domain in the first place?
>
> We plan to alloc a paging domain when a video encoder session is created. Then
> we call iommu_attach_device() before a job runs, and detach back to the default
> domain after it finishes so the next job can attach its own session's domain.
> This is so each session is isolated from each other.
If you do that you can't use the DMA API at all in your driver as you
have to set 'driver_managed = true'. Is it OK? If it is why not just
use VFIO?
Or are you planning DT horrors to get yourself a DT created "DMA
struct" ?
> > Using paging domains directly is very complicated, I'm not excite d to
> > see DRM trying to do this at all. IMHO there are many unanswered
> > questions how it could even do this safely.
>
> In a very simplified way, this is not unlike how GPU drivers currently work. In
> fact, from a cursory look, this seems to be how rocket (the NPU driver) works
> too. Since we are merely porting the same ideas to video codec hardware, I am a bit
> surprised by your discontentment.
In my experiance any place I see DRM touching the iommu it is
immediately suspect. We keep uncovering horrors that people buried in
there. Yes, you can point to some DRM places doing something like
this, with a convoluted struct device scheme and DT hacks to make it
functional, no you should not copy it and certainly not entomb it in
rust. :(
> In any case, we need this isolation due to the submission model used.
> Most Vulkan drivers know their hardware pretty well, so a level of abstraction
> on top of it like V4L2 is unnecessary. Therefore, in order to achieve a leaner
> kernel driver, userspace programs most of the encoder's registers directly, so
> the kernel cannot validate every address a job makes the encoder access.
Sure, but you haven't explained what the iommu gets attached to (ie
where does the SID come from), how cache flushing works, who programs
the mappings, who is managing page pinning, etc.
Are you sure you shouldn't be using iommufd for this, tied into your
driver? It was intended for exactly this use case, to allow drivers to
delegate their translation to userspace. Though that doesn't relly
solve the questions about what it is even being attached to..
> The fault handler is used in the same way it is used in GPU drivers: to kill
> sessions if they fault. This way the driver can simply refuse further work from
> a misbehaving client, for example. It is also the natural place to enqueue a
> reset if the codec hardware is frozen as a result of the fault.
IIRC no GPU driver does that, they hook the obsolete fault handler not
supported in new iommu drivers not the new PRI based one... IDK we
have even wired this up to work this way.
It is nicer if the HW can tell you it faulted rather than expect the
iommu to do it.
Jason
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-17 20:20 ` Jason Gunthorpe
@ 2026-11-18 15:25 ` Nicolas Dufresne
2026-11-18 15:55 ` Daniel Almeida
2026-11-18 16:09 ` Jason Gunthorpe
2026-11-18 19:11 ` Daniel Almeida
1 sibling, 2 replies; 23+ messages in thread
From: Nicolas Dufresne @ 2026-11-18 15:25 UTC (permalink / raw)
To: Jason Gunthorpe, Daniel Almeida
Cc: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova
[-- Attachment #1: Type: text/plain, Size: 3456 bytes --]
Hi,
Le mardi 17 novembre 2026 à 16:20 -0400, Jason Gunthorpe a écrit :
> On Tue, Nov 17, 2026 at 05:09:11PM -0300, Daniel Almeida wrote:
> > Hi Jason,
> >
> > > On 16 Nov 2026, at 16:38, Jason Gunthorpe <jgg@ziepe.ca> wrote:
> > >
> > > On Thu, Nov 12, 2026 at 02:00:07PM -0300, Daniel Almeida wrote:
> > > > This series adds a Rust abstraction for IOMMU paging domains. A driver
> > > > can allocate a domain, attach it to its device, map pages into it, and
> > > > install a handler for its faults.
> > > >
> > > > This will be used by the upcoming rkvenc driver that my colleagues
> > > > Detlev Casanova, Nicolas Dufresne and I are working on. More details on
> > > > this driver can be found by watching Nicolas Dufresne's talk at LPC 2026
> > > > [0].
> > > >
> > > > As part of the security model, each client will have its own IOMMU
> > > > domain. This is needed as the submission model will let userspace set
> > > > most of the encoder's registers directly, so the kernel cannot check
> > > > every memory access that a job makes the encoder do. With a single
> > > > domain, one client could make the encoder read or write the memory of
> > > > another client.
> > >
> > > How do you plan to get the paging domain in the first place?
> >
> > We plan to alloc a paging domain when a video encoder session is created. Then
> > we call iommu_attach_device() before a job runs, and detach back to the default
> > domain after it finishes so the next job can attach its own session's domain.
> > This is so each session is isolated from each other.
>
> If you do that you can't use the DMA API at all in your driver as you
> have to set 'driver_managed = true'. Is it OK? If it is why not just
> use VFIO?
>
> Or are you planning DT horrors to get yourself a DT created "DMA
> struct" ?
This seem to digress. All we want to do is use the iommu subsystem as intended
for the case the iommu driver is not inside the DRM driver (which seems to have
never been the case so far in DRM). The existing infra for that (in C) is pretty
abstract an simple in my opinion, and built into the DMA API if you manage which
domain to attach to your device at the right time. To me, it fits CODEC HW
driver which are also simple. The DT horrors you refer to seems totally
unfounded, you just describe the iommu and the iommu subsystem will manage it
for your device.
>
> > The fault handler is used in the same way it is used in GPU drivers: to kill
> > sessions if they fault. This way the driver can simply refuse further work from
> > a misbehaving client, for example. It is also the natural place to enqueue a
> > reset if the codec hardware is frozen as a result of the fault.
>
> IIRC no GPU driver does that, they hook the obsolete fault handler not
> supported in new iommu drivers not the new PRI based one... IDK we
> have even wired this up to work this way.
>
> It is nicer if the HW can tell you it faulted rather than expect the
> iommu to do it.
We most likely don't need that, and should drop it from this series. We have
never used the fault handler in any linux-media codec in the past, the HW would
always endup producing an IRQ with a error or a timeout. Using the fault handler
would probably be a bit racy too. Pausing work from a faulty source though isn't
a bad idea, but more of a nice future addition, assuming it can work in a non-
racy way.
Nicolas
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-18 15:25 ` Nicolas Dufresne
@ 2026-11-18 15:55 ` Daniel Almeida
2026-11-18 15:59 ` Daniel Almeida
2026-11-18 16:09 ` Jason Gunthorpe
1 sibling, 1 reply; 23+ messages in thread
From: Daniel Almeida @ 2026-11-18 15:55 UTC (permalink / raw)
To: Nicolas Dufresne
Cc: Jason Gunthorpe, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova
>
>>
>>> The fault handler is used in the same way it is used in GPU drivers: to kill
>>> sessions if they fault. This way the driver can simply refuse further work from
>>> a misbehaving client, for example. It is also the natural place to enqueue a
>>> reset if the codec hardware is frozen as a result of the fault.
>>
>> IIRC no GPU driver does that, they hook the obsolete fault handler not
>> supported in new iommu drivers not the new PRI based one... IDK we
>> have even wired this up to work this way.
>>
>> It is nicer if the HW can tell you it faulted rather than expect the
>> iommu to do it.
>
> We most likely don't need that, and should drop it from this series. We have
> never used the fault handler in any linux-media codec in the past, the HW would
> always endup producing an IRQ with a error or a timeout. Using the fault handler
> would probably be a bit racy too. Pausing work from a faulty source though isn't
> a bad idea, but more of a nice future addition, assuming it can work in a non-
> racy way.
>
> Nicolas
I am still collecting my thoughts to reply to Jason, but I don't see why we
wouldn't want a fault handler. For example this:
> IIRC no GPU driver does that, they hook the obsolete fault handler not
> supported in new iommu drivers not the new PRI based one... IDK we have even
> wired this up to work this way.
Perhaps does not account for the fact that the fault handler in Mali doesn't go
through the iommu subsystem, but there is a fault handler. Not only did I write
the handler, I also had to use it constantly as tyr initially faulted all the
time :/
Also, saying that a handler was never used in media is not a good argument,
IMHO. Yes, eventually the decoder reports some error, but this is orthogonal to
knowing whether someone is trying to access invalid memory. And if you know
that, you can prevent further invalid accesses before they bring the whole
thing down. Quite often I had to reboot the whole machine because the hardware
was perpetually stuck, and if the objective is having all the users of the
encoder play nice together, then this is not very ideal.
This is not unlike my time reverse engineering the NVIDIA codec hardware:
nouveau would complain on faults and kill the channel too.
What I am trying to say is: if the argument is "not all iommu drivers properly
support fault handlers", then perhaps I can vonlunteer to fix this in the C
code, if they want it and if a "fix" is technically feasible(?). But the overall
desire to kill a faulting user is something we could definitely use, and the
remaining question is whether this is possible via the iommu API.
— Daniel
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-18 15:55 ` Daniel Almeida
@ 2026-11-18 15:59 ` Daniel Almeida
0 siblings, 0 replies; 23+ messages in thread
From: Daniel Almeida @ 2026-11-18 15:59 UTC (permalink / raw)
To: Nicolas Dufresne
Cc: Jason Gunthorpe, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova
> On 18 Nov 2026, at 12:55, Daniel Almeida <daniel.almeida@collabora.com> wrote:
>
>>
>>
>>>
>>>> The fault handler is used in the same way it is used in GPU drivers: to kill
>>>> sessions if they fault. This way the driver can simply refuse further work from
>>>> a misbehaving client, for example. It is also the natural place to enqueue a
>>>> reset if the codec hardware is frozen as a result of the fault.
>>>
>>> IIRC no GPU driver does that, they hook the obsolete fault handler not
>>> supported in new iommu drivers not the new PRI based one... IDK we
>>> have even wired this up to work this way.
>>>
>>> It is nicer if the HW can tell you it faulted rather than expect the
>>> iommu to do it.
>>
>> We most likely don't need that, and should drop it from this series. We have
>> never used the fault handler in any linux-media codec in the past, the HW would
>> always endup producing an IRQ with a error or a timeout. Using the fault handler
>> would probably be a bit racy too. Pausing work from a faulty source though isn't
>> a bad idea, but more of a nice future addition, assuming it can work in a non-
>> racy way.
>>
>> Nicolas
Sorry, forgot to address the "racy" part: if GPU drivers can do it, we can do
it too. I wouldn't worry about race conditions if we copy their pattern.
— Daniel
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-18 15:25 ` Nicolas Dufresne
2026-11-18 15:55 ` Daniel Almeida
@ 2026-11-18 16:09 ` Jason Gunthorpe
2026-11-18 19:31 ` Nicolas Dufresne
1 sibling, 1 reply; 23+ messages in thread
From: Jason Gunthorpe @ 2026-11-18 16:09 UTC (permalink / raw)
To: Nicolas Dufresne
Cc: Daniel Almeida, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova
On Wed, Nov 18, 2026 at 10:25:58AM -0500, Nicolas Dufresne wrote:
> Hi,
>
> Le mardi 17 novembre 2026 à 16:20 -0400, Jason Gunthorpe a écrit :
> > On Tue, Nov 17, 2026 at 05:09:11PM -0300, Daniel Almeida wrote:
> > > Hi Jason,
> > >
> > > > On 16 Nov 2026, at 16:38, Jason Gunthorpe <jgg@ziepe.ca> wrote:
> > > >
> > > > On Thu, Nov 12, 2026 at 02:00:07PM -0300, Daniel Almeida wrote:
> > > > > This series adds a Rust abstraction for IOMMU paging domains. A driver
> > > > > can allocate a domain, attach it to its device, map pages into it, and
> > > > > install a handler for its faults.
> > > > >
> > > > > This will be used by the upcoming rkvenc driver that my colleagues
> > > > > Detlev Casanova, Nicolas Dufresne and I are working on. More details on
> > > > > this driver can be found by watching Nicolas Dufresne's talk at LPC 2026
> > > > > [0].
> > > > >
> > > > > As part of the security model, each client will have its own IOMMU
> > > > > domain. This is needed as the submission model will let userspace set
> > > > > most of the encoder's registers directly, so the kernel cannot check
> > > > > every memory access that a job makes the encoder do. With a single
> > > > > domain, one client could make the encoder read or write the memory of
> > > > > another client.
> > > >
> > > > How do you plan to get the paging domain in the first place?
> > >
> > > We plan to alloc a paging domain when a video encoder session is created. Then
> > > we call iommu_attach_device() before a job runs, and detach back to the default
> > > domain after it finishes so the next job can attach its own session's domain.
> > > This is so each session is isolated from each other.
> >
> > If you do that you can't use the DMA API at all in your driver as you
> > have to set 'driver_managed = true'. Is it OK? If it is why not just
> > use VFIO?
> >
> > Or are you planning DT horrors to get yourself a DT created "DMA
> > struct" ?
>
> This seem to digress. All we want to do is use the iommu subsystem as intended
> for the case the iommu driver is not inside the DRM driver (which seems to have
> never been the case so far in DRM).
"As intended" means you use the DMA API and you do not touch
the paging domain.
"As intended" means you use iommufd if you want a domain full of user
controlled mappings.
> The existing infra for that (in C) is pretty abstract an simple in
> my opinion, and built into the DMA API if you manage which domain to
> attach to your device at the right time.
What does this even mean? The DMA API owns the domain, not the
driver.
All the cases using these APIs in DRM and media are troubled in one
way or another.
For example the recent revelation they have always been using DMABUF entirely wrong:
https://lore.kernel.org/all/20261009125338.GB13920@nvidia.com/
Tegra desires to undo their abuses.
It was never intended for things to work like this, there is missing
infrastructure.
> To me, it fits CODEC HW driver which are also simple. The DT horrors
> you refer to seems totally unfounded, you just describe the iommu
> and the iommu subsystem will manage it for your device.
"unfounded? You are not following along, I've written about this now
many times to many different people. Follow the links and discussion
from here:
https://lore.kernel.org/all/20260917144804.GI3196566@ziepe.ca/
If you use DT correctly there is no place to attach an iommu_domain to
and you can't even use these APIs. You get DMA API only and that's it.
You have to do something "wrong" in DT to create fake HW devices to
even give you a struct device to even attach something to.
Or you have to entirely give up DMA API and attach to your only struct
device.
Jason
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-18 16:09 ` Jason Gunthorpe
@ 2026-11-18 19:31 ` Nicolas Dufresne
2026-11-18 19:51 ` Jason Gunthorpe
0 siblings, 1 reply; 23+ messages in thread
From: Nicolas Dufresne @ 2026-11-18 19:31 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Daniel Almeida, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova
[-- Attachment #1: Type: text/plain, Size: 5576 bytes --]
Hi,
Le mercredi 18 novembre 2026 à 12:09 -0400, Jason Gunthorpe a écrit :
> On Wed, Nov 18, 2026 at 10:25:58AM -0500, Nicolas Dufresne wrote:
> > Hi,
> >
> > Le mardi 17 novembre 2026 à 16:20 -0400, Jason Gunthorpe a écrit :
> > > On Tue, Nov 17, 2026 at 05:09:11PM -0300, Daniel Almeida wrote:
> > > > Hi Jason,
> > > >
> > > > > On 16 Nov 2026, at 16:38, Jason Gunthorpe <jgg@ziepe.ca> wrote:
> > > > >
> > > > > On Thu, Nov 12, 2026 at 02:00:07PM -0300, Daniel Almeida wrote:
> > > > > > This series adds a Rust abstraction for IOMMU paging domains. A driver
> > > > > > can allocate a domain, attach it to its device, map pages into it, and
> > > > > > install a handler for its faults.
> > > > > >
> > > > > > This will be used by the upcoming rkvenc driver that my colleagues
> > > > > > Detlev Casanova, Nicolas Dufresne and I are working on. More details on
> > > > > > this driver can be found by watching Nicolas Dufresne's talk at LPC 2026
> > > > > > [0].
> > > > > >
> > > > > > As part of the security model, each client will have its own IOMMU
> > > > > > domain. This is needed as the submission model will let userspace set
> > > > > > most of the encoder's registers directly, so the kernel cannot check
> > > > > > every memory access that a job makes the encoder do. With a single
> > > > > > domain, one client could make the encoder read or write the memory of
> > > > > > another client.
> > > > >
> > > > > How do you plan to get the paging domain in the first place?
> > > >
> > > > We plan to alloc a paging domain when a video encoder session is created. Then
> > > > we call iommu_attach_device() before a job runs, and detach back to the default
> > > > domain after it finishes so the next job can attach its own session's domain.
> > > > This is so each session is isolated from each other.
> > >
> > > If you do that you can't use the DMA API at all in your driver as you
> > > have to set 'driver_managed = true'. Is it OK? If it is why not just
> > > use VFIO?
> > >
> > > Or are you planning DT horrors to get yourself a DT created "DMA
> > > struct" ?
> >
> > This seem to digress. All we want to do is use the iommu subsystem as intended
> > for the case the iommu driver is not inside the DRM driver (which seems to have
> > never been the case so far in DRM).
>
> "As intended" means you use the DMA API and you do not touch
> the paging domain.
>
> "As intended" means you use iommufd if you want a domain full of user
> controlled mappings.
As discussed at LPC, we believe its pointless for this type of device to give
any direct page choices to userspace. Can you explain why you believe
differently?
>
> > The existing infra for that (in C) is pretty abstract an simple in
> > my opinion, and built into the DMA API if you manage which domain to
> > attach to your device at the right time.
>
> What does this even mean? The DMA API owns the domain, not the
> driver.
>
> All the cases using these APIs in DRM and media are troubled in one
> way or another.
>
> For example the recent revelation they have always been using DMABUF entirely wrong:
>
> https://lore.kernel.org/all/20261009125338.GB13920@nvidia.com/
>
> Tegra desires to undo their abuses.
>
> It was never intended for things to work like this, there is missing
> infrastructure.
>
> > To me, it fits CODEC HW driver which are also simple. The DT horrors
> > you refer to seems totally unfounded, you just describe the iommu
> > and the iommu subsystem will manage it for your device.
>
> "unfounded? You are not following along, I've written about this now
> many times to many different people. Follow the links and discussion
> from here:
>
> https://lore.kernel.org/all/20260917144804.GI3196566@ziepe.ca/
>
> If you use DT correctly there is no place to attach an iommu_domain to
> and you can't even use these APIs. You get DMA API only and that's it.
>
> You have to do something "wrong" in DT to create fake HW devices to
> even give you a struct device to even attach something to.
>
> Or you have to entirely give up DMA API and attach to your only struct
> device.
I'm not sure how this applies to our work. But since they are all broken
according to you, let me give example of cores/iommu mapping we are using, and
perhaps you'll be able to explain us correctly? I've picked the Rockchip video
decoder, which is upstream but expose through V4L2 stateless API, but from a
hardware point of view is pretty much identical:
The DT (which I read you must be broken and in violation of everything, yet all
reviewed by DT maintainers):
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/boot/dts/rockchip/rk3588-base.dtsi#n1400
The multi-core support proposal, its not doing process isolation, its not
strictly needed in higher level abstraction (though could be better), but its
using domains so that each core can actually see the same memory:
https://lore.kernel.org/all/20260810-rkvdec-multicore-v2-0-986f89d22cdc@collabora.com/
The DMA abstraction that is used by the above driver:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/media/common/videobuf2/videobuf2-dma-contig.c
GEM kind of offer similar high level abstraction to DMA (with important
behaviour changes in of course, specially for write combine delivery), so this
is what we have for now in the WIP driver. Check the LPC presentation for the
rest of the context.
Nicolas
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-18 19:31 ` Nicolas Dufresne
@ 2026-11-18 19:51 ` Jason Gunthorpe
2026-11-18 21:56 ` Nicolas Dufresne
0 siblings, 1 reply; 23+ messages in thread
From: Jason Gunthorpe @ 2026-11-18 19:51 UTC (permalink / raw)
To: Nicolas Dufresne
Cc: Daniel Almeida, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova
On Wed, Nov 18, 2026 at 02:31:28PM -0500, Nicolas Dufresne wrote:
> > > This seem to digress. All we want to do is use the iommu subsystem as intended
> > > for the case the iommu driver is not inside the DRM driver (which seems to have
> > > never been the case so far in DRM).
> >
> > "As intended" means you use the DMA API and you do not touch
> > the paging domain.
> >
> > "As intended" means you use iommufd if you want a domain full of user
> > controlled mappings.
>
> As discussed at LPC, we believe its pointless for this type of device to give
> any direct page choices to userspace. Can you explain why you believe
> differently?
This is what was asked for a few messages back?
> I'm not sure how this applies to our work. But since they are all broken
> according to you, let me give example of cores/iommu mapping we are using, and
> perhaps you'll be able to explain us correctly? I've picked the Rockchip video
> decoder, which is upstream but expose through V4L2 stateless API, but from a
> hardware point of view is pretty much identical:
>
> The DT (which I read you must be broken and in violation of everything, yet all
> reviewed by DT maintainers):
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/boot/dts/rockchip/rk3588-base.dtsi#n1400
You linked to a clocks line, I have no idea what I'm supposed to look
at in this file. Can you explain what parts of the DT apply to what
you are intersted in?
> The multi-core support proposal, its not doing process isolation, its not
> strictly needed in higher level abstraction (though could be better), but its
> using domains so that each core can actually see the same memory:
> https://lore.kernel.org/all/20260810-rkvdec-multicore-v2-0-986f89d22cdc@collabora.com/
IIRC This is the driver that had the weird not-quite-an-iommu issue and
there were many complaints about it doing things with the API that
make no sense because it's HW is bad.
/*
* To rewrite mapping into the attached IOMMU core, attach a new empty domain that
* will program an empty table, then detach it to restore the default domain and
* all cached mappings.
* This is safely done in this interrupt handler to make sure no memory get mapped
* through the IOMMU while the empty domain is attached.
*/
But that doesn't excuse this:
As each core has its own IOMMU core, buffers must be mapped in each
core's IOMMU so that any run() call can use any core without having to
remap everything.
To do that, we use rockchip iommu domain's iommu devices list.
With that, one IOMMU domain can be mapped on multiple devices, meaning
that each call to iommu_map() will flush the new mapping on all devices
in the list.
We have DT for this already, you use iommus=[] and list all your
iommus and it sets everything up for you. Going around and changing
domains behind the scenes when the core already attached DMA API
Then WTF is this:
- ret = iommu_map(iommu_get_domain_for_dev(core->dev),
- virt_addr, phys_addr,
Seriously? You cannot directly map to a DMA API owned domain. It is
fully illegal.
> The DMA abstraction that is used by the above driver:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/media/common/videobuf2/videobuf2-dma-contig.c
Thats DMA API so it looks OK?
Jason
^ permalink raw reply [flat|nested] 23+ messages in thread* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-18 19:51 ` Jason Gunthorpe
@ 2026-11-18 21:56 ` Nicolas Dufresne
0 siblings, 0 replies; 23+ messages in thread
From: Nicolas Dufresne @ 2026-11-18 21:56 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Daniel Almeida, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova
[-- Attachment #1: Type: text/plain, Size: 8113 bytes --]
Le mercredi 18 novembre 2026 à 15:51 -0400, Jason Gunthorpe a écrit :
> On Wed, Nov 18, 2026 at 02:31:28PM -0500, Nicolas Dufresne wrote:
> > > > This seem to digress. All we want to do is use the iommu subsystem as intended
> > > > for the case the iommu driver is not inside the DRM driver (which seems to have
> > > > never been the case so far in DRM).
> > >
> > > "As intended" means you use the DMA API and you do not touch
> > > the paging domain.
> > >
> > > "As intended" means you use iommufd if you want a domain full of user
> > > controlled mappings.
> >
> > As discussed at LPC, we believe its pointless for this type of device to give
> > any direct page choices to userspace. Can you explain why you believe
> > differently?
>
> This is what was asked for a few messages back?
>
> > I'm not sure how this applies to our work. But since they are all broken
> > according to you, let me give example of cores/iommu mapping we are using, and
> > perhaps you'll be able to explain us correctly? I've picked the Rockchip video
> > decoder, which is upstream but expose through V4L2 stateless API, but from a
> > hardware point of view is pretty much identical:
> >
> > The DT (which I read you must be broken and in violation of everything, yet all
> > reviewed by DT maintainers):
> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/boot/dts/rockchip/rk3588-base.dtsi#n1400
>
> You linked to a clocks line, I have no idea what I'm supposed to look
> at in this file. Can you explain what parts of the DT apply to what
> you are intersted in?
No, I linked to the four nodes, 2 decoders, 2 iommus.
>
> > The multi-core support proposal, its not doing process isolation, its not
> > strictly needed in higher level abstraction (though could be better), but its
> > using domains so that each core can actually see the same memory:
> > https://lore.kernel.org/all/20260810-rkvdec-multicore-v2-0-986f89d22cdc@collabora.com/
>
> IIRC This is the driver that had the weird not-quite-an-iommu issue and
> there were many complaints about it doing things with the API that
> make no sense because it's HW is bad.
>
> /*
> * To rewrite mapping into the attached IOMMU core, attach a new empty domain that
> * will program an empty table, then detach it to restore the default domain and
> * all cached mappings.
> * This is safely done in this interrupt handler to make sure no memory get mapped
> * through the IOMMU while the empty domain is attached.
> */
We are aware of this, someone have to find the time to go back to it and spend
another year in discussion to try and fit the need to re-program the rkiommu
after a decoder fault.
Our best guess of the hardware problem is that its "self reset" mechanism on
fault spill over the mmio of the iommu. It pretty hard to create a rkiommu
function that does not look like a bigger hack. And until we had an agreement,
this made it work for the users.
>
> But that doesn't excuse this:
>
> As each core has its own IOMMU core, buffers must be mapped in each
> core's IOMMU so that any run() call can use any core without having to
> remap everything.
>
> To do that, we use rockchip iommu domain's iommu devices list.
> With that, one IOMMU domain can be mapped on multiple devices, meaning
> that each call to iommu_map() will flush the new mapping on all devices
> in the list.
>
> We have DT for this already, you use iommus=[] and list all your
> iommus and it sets everything up for you. Going around and changing
> domains behind the scenes when the core already attached DMA API
Mind linking examples and doc? No one mentioned this, maybe the DT maintainer
need to be educated, you guys don't participate when these bindings are made.
Can we group the iommus from driver instead of DT ? (these DT predates us trying
to use the HW unfortunatly, so backward compatibility is kind of needed). Note
that appart from the hack above the v4l2 mutli-core case has completely static
domain, just that there is N device with the same domain. It is less safe, since
it does not do process isolation.
>
> Then WTF is this:
>
> - ret = iommu_map(iommu_get_domain_for_dev(core->dev),
> - virt_addr, phys_addr,
>
> Seriously? You cannot directly map to a DMA API owned domain. It is
> fully illegal.
I guess I should fix that one, suggested change below (I'll make a patch later).
We should drop iommu_map() from any bindings. But that leaves us with no way to
use the rkiommu and do process isolation, which basically defeat the purpose of
an iommu no?
RFC:
---
diff --git a/drivers/media/platform/rockchip/rkvdec/rkvdec-rcb.c b/drivers/media/platform/rockchip/rkvdec/rkvdec-rcb.c
index fdcf1f1773797..37d5677e4c8f9 100644
--- a/drivers/media/platform/rockchip/rkvdec/rkvdec-rcb.c
+++ b/drivers/media/platform/rockchip/rkvdec/rkvdec-rcb.c
@@ -9,7 +9,7 @@
#include "rkvdec.h"
#include "rkvdec-rcb.h"
-#include <linux/iommu.h>
+#include <linux/dma-mapping.h>
#include <linux/genalloc.h>
#include <linux/sizes.h>
#include <linux/types.h>
@@ -44,7 +44,6 @@ void rkvdec_free_rcb(struct rkvdec_ctx *ctx)
{
struct rkvdec_dev *dev = ctx->dev;
struct rkvdec_rcb_config *cfg = ctx->rcb_config;
- unsigned long virt_addr;
int i;
if (!cfg)
@@ -58,11 +57,11 @@ void rkvdec_free_rcb(struct rkvdec_ctx *ctx)
switch (cfg->rcb_bufs[i].type) {
case RKVDEC_ALLOC_SRAM:
- virt_addr = (unsigned long)cfg->rcb_bufs[i].cpu;
-
- if (dev->iommu_domain)
- iommu_unmap(dev->iommu_domain, virt_addr, rcb_size);
- gen_pool_free(dev->sram_pool, virt_addr, rcb_size);
+ dma_unmap_resource(dev->dev, cfg->rcb_bufs[i].dma,
+ rcb_size, DMA_BIDIRECTIONAL, 0);
+ gen_pool_free(dev->sram_pool,
+ (unsigned long)cfg->rcb_bufs[i].cpu,
+ rcb_size);
break;
case RKVDEC_ALLOC_DMA:
dma_free_coherent(dev->dev,
@@ -125,29 +124,19 @@ int rkvdec_allocate_rcb(struct rkvdec_ctx *ctx,
SZ_4K);
}
- /* If an IOMMU is used, map the SRAM address through it */
- if (cpu && rkvdec->iommu_domain) {
- unsigned long virt_addr = (unsigned long)cpu;
+ if (cpu) {
phys_addr_t phys_addr = dma;
- ret = iommu_map(rkvdec->iommu_domain, virt_addr, phys_addr,
- rcb_size, IOMMU_READ | IOMMU_WRITE, 0);
- if (ret) {
+ dma = dma_map_resource(rkvdec->dev, phys_addr, rcb_size,
+ DMA_BIDIRECTIONAL, 0);
+ if (dma_mapping_error(rkvdec->dev, dma)) {
gen_pool_free(ctx->dev->sram_pool,
(unsigned long)cpu,
rcb_size);
cpu = NULL;
- goto ram_fallback;
}
-
- /*
- * The registers will be configured with the virtual
- * address so that it goes through the IOMMU
- */
- dma = virt_addr;
}
-ram_fallback:
/* Fallback to RAM */
if (!cpu) {
cpu = dma_alloc_coherent(ctx->dev->dev,
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-17 20:20 ` Jason Gunthorpe
2026-11-18 15:25 ` Nicolas Dufresne
@ 2026-11-18 19:11 ` Daniel Almeida
2026-11-18 19:35 ` Jason Gunthorpe
1 sibling, 1 reply; 23+ messages in thread
From: Daniel Almeida @ 2026-11-18 19:11 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova,
Nicolas Dufresne
> On 17 Nov 2026, at 17:20, Jason Gunthorpe <jgg@ziepe.ca> wrote:
>
> On Tue, Nov 17, 2026 at 05:09:11PM -0300, Daniel Almeida wrote:
>> Hi Jason,
>>
>>> On 16 Nov 2026, at 16:38, Jason Gunthorpe <jgg@ziepe.ca> wrote:
>>>
>>> On Thu, Nov 12, 2026 at 02:00:07PM -0300, Daniel Almeida wrote:
>>>> This series adds a Rust abstraction for IOMMU paging domains. A driver
>>>> can allocate a domain, attach it to its device, map pages into it, and
>>>> install a handler for its faults.
>>>>
>>>> This will be used by the upcoming rkvenc driver that my colleagues
>>>> Detlev Casanova, Nicolas Dufresne and I are working on. More details on
>>>> this driver can be found by watching Nicolas Dufresne's talk at LPC 2026
>>>> [0].
>>>>
>>>> As part of the security model, each client will have its own IOMMU
>>>> domain. This is needed as the submission model will let userspace set
>>>> most of the encoder's registers directly, so the kernel cannot check
>>>> every memory access that a job makes the encoder do. With a single
>>>> domain, one client could make the encoder read or write the memory of
>>>> another client.
>>>
>>> How do you plan to get the paging domain in the first place?
>>
>> We plan to alloc a paging domain when a video encoder session is created. Then
>> we call iommu_attach_device() before a job runs, and detach back to the default
>> domain after it finishes so the next job can attach its own session's domain.
>> This is so each session is isolated from each other.
>
> If you do that you can't use the DMA API at all in your driver as you
> have to set 'driver_managed = true'. Is it OK? If it is why not just
> use VFIO?
No, not OK :/ We want to use GEM shmem, which calls into the DMA API internally
through drm_gem_shmem_get_pages_sgt() -> dma_map_sgtable().
>
> Or are you planning DT horrors to get yourself a DT created "DMA
> struct" ?
I wasn't even aware such hacks existed. In any case, I am not proposing more
hacks, no.
>
>>> Using paging domains directly is very complicated, I'm not excite d to
>>> see DRM trying to do this at all. IMHO there are many unanswered
>>> questions how it could even do this safely.
>>
>> In a very simplified way, this is not unlike how GPU drivers currently work. In
>> fact, from a cursory look, this seems to be how rocket (the NPU driver) works
>> too. Since we are merely porting the same ideas to video codec hardware, I am a bit
>> surprised by your discontentment.
>
> In my experiance any place I see DRM touching the iommu it is
> immediately suspect. We keep uncovering horrors that people buried in
> there. Yes, you can point to some DRM places doing something like
> this, with a convoluted struct device scheme and DT hacks to make it
> functional, no you should not copy it and certainly not entomb it in
> rust. :(
Sounds reasonable.
>
>> In any case, we need this isolation due to the submission model used.
>> Most Vulkan drivers know their hardware pretty well, so a level of abstraction
>> on top of it like V4L2 is unnecessary. Therefore, in order to achieve a leaner
>> kernel driver, userspace programs most of the encoder's registers directly, so
>> the kernel cannot validate every address a job makes the encoder access.
>
> Sure, but you haven't explained what the iommu gets attached to (ie
> where does the SID come from), how cache flushing works, who programs
> the mappings, who is managing page pinning, etc.
The SID comes from the platform_device backing the encoder, as far as I am
aware. Pinning is handled by the GEM shmem layer.
The CPU mappings are write-combined, so the only cache maintenance is the clean
of newly allocated pages, which dma_map_sgtable() does in
drm_gem_shmem_get_pages_sgt(). The mappings themselves are chosen by the kernel
driver. We don't care about what the actual IOVA is. All we care about is that a
given GEM object is mapped to a given domain, and that we have some IOVA
representing the start of the allocation, such that we can pass that to the
device.
>
> Are you sure you shouldn't be using iommufd for this, tied into your
> driver? It was intended for exactly this use case, to allow drivers to
> delegate their translation to userspace. Though that doesn't relly
> solve the questions about what it is even being attached to..
I had a cursory look at iommufd, but it seems like:
a) it requires IOMMU_CAP_CACHE_COHERENCY, which is not offered by the rk_iommu
backing the RK3588 SoC we are currently targeting. In fact, IIUC, this does not
seem to be possible, since the encoder is not cache coherent with the CPU?
I.e., does iommufd expect hardware cache coherency/snooping?
b) there is no way to do what I said above about GEM, i.e.: there is no
connection between iommufd and GEM.
>
>> The fault handler is used in the same way it is used in GPU drivers: to kill
>> sessions if they fault. This way the driver can simply refuse further work from
>> a misbehaving client, for example. It is also the natural place to enqueue a
>> reset if the codec hardware is frozen as a result of the fault.
>
> IIRC no GPU driver does that, they hook the obsolete fault handler not
> supported in new iommu drivers not the new PRI based one... IDK we
> have even wired this up to work this way.
>
> It is nicer if the HW can tell you it faulted rather than expect the
> iommu to do it.
See my reply in another part of this thread about how handlers work in Mali,
just as a comparison point.
>
> Jason
You seem to be saying that what I'm proposing here has a few pitfalls that have
come up over the years. Perhaps we can massage iommufd to make our use case
work? I can volunteer to do whatever work is needed, of course.
Same caveat as before: feel free to correct me at any point above, as I have no
previous experience with the iommu internals.
— Daniel
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-18 19:11 ` Daniel Almeida
@ 2026-11-18 19:35 ` Jason Gunthorpe
2026-11-18 19:41 ` Nicolas Dufresne
0 siblings, 1 reply; 23+ messages in thread
From: Jason Gunthorpe @ 2026-11-18 19:35 UTC (permalink / raw)
To: Daniel Almeida
Cc: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova,
Nicolas Dufresne
On Wed, Nov 18, 2026 at 04:11:40PM -0300, Daniel Almeida wrote:
>
> > On 17 Nov 2026, at 17:20, Jason Gunthorpe <jgg@ziepe.ca> wrote:
> >
> > On Tue, Nov 17, 2026 at 05:09:11PM -0300, Daniel Almeida wrote:
> >> Hi Jason,
> >>
> >>> On 16 Nov 2026, at 16:38, Jason Gunthorpe <jgg@ziepe.ca> wrote:
> >>>
> >>> On Thu, Nov 12, 2026 at 02:00:07PM -0300, Daniel Almeida wrote:
> >>>> This series adds a Rust abstraction for IOMMU paging domains. A driver
> >>>> can allocate a domain, attach it to its device, map pages into it, and
> >>>> install a handler for its faults.
> >>>>
> >>>> This will be used by the upcoming rkvenc driver that my colleagues
> >>>> Detlev Casanova, Nicolas Dufresne and I are working on. More details on
> >>>> this driver can be found by watching Nicolas Dufresne's talk at LPC 2026
> >>>> [0].
> >>>>
> >>>> As part of the security model, each client will have its own IOMMU
> >>>> domain. This is needed as the submission model will let userspace set
> >>>> most of the encoder's registers directly, so the kernel cannot check
> >>>> every memory access that a job makes the encoder do. With a single
> >>>> domain, one client could make the encoder read or write the memory of
> >>>> another client.
> >>>
> >>> How do you plan to get the paging domain in the first place?
> >>
> >> We plan to alloc a paging domain when a video encoder session is created. Then
> >> we call iommu_attach_device() before a job runs, and detach back to the default
> >> domain after it finishes so the next job can attach its own session's domain.
> >> This is so each session is isolated from each other.
> >
> > If you do that you can't use the DMA API at all in your driver as you
> > have to set 'driver_managed = true'. Is it OK? If it is why not just
> > use VFIO?
>
> No, not OK :/ We want to use GEM shmem, which calls into the DMA API internally
> through drm_gem_shmem_get_pages_sgt() -> dma_map_sgtable().
You cannot continue to use DMA API once you start touching a paging
domain directly. It is why driver_managed exist.
If you want to use the DMA API you need two struct devices and now you
are into DT abuse land.
So what exactly are you planning on doing
And why do you need the paging domain at all if you are using the DMA
API?
> The CPU mappings are write-combined, so the only cache maintenance is the clean
> of newly allocated pages, which dma_map_sgtable() does in
> drm_gem_shmem_get_pages_sgt().
Yes, this is why you should be using the DMA API, that doesn't explain
how any direct use of a paging domain will work.
> The mappings themselves are chosen by the kernel driver. We don't
> care about what the actual IOVA is. All we care about is that a
> given GEM object is mapped to a given domain, and that we have some
> IOVA representing the start of the allocation, such that we can pass
> that to the device.
Wonderful, what is the paging domain for?
> > Are you sure you shouldn't be using iommufd for this, tied into your
> > driver? It was intended for exactly this use case, to allow drivers to
> > delegate their translation to userspace. Though that doesn't relly
> > solve the questions about what it is even being attached to..
>
> I had a cursory look at iommufd, but it seems like:
>
> a) it requires IOMMU_CAP_CACHE_COHERENCY, which is not offered by the rk_iommu
> backing the RK3588 SoC we are currently targeting. In fact, IIUC, this does not
> seem to be possible, since the encoder is not cache coherent with the CPU?
> I.e., does iommufd expect hardware cache coherency/snooping?
*Linux* expects coherency, if you are DMAing direcly to user cachable
memory and you aren't coherent there are issues, and minimally your
driver would have to do the cache flushing.
Which we have deliberately made it so drivers cannot do to force them
to use the DMA API.
This is one of the unresolved problems if a driver tries to use a
paging domain directly.
> b) there is no way to do what I said above about GEM, i.e.: there is no
> connection between iommufd and GEM.
Right, because you started out saying you wanted userspace to control
the mapping and put user memory into it. Kernel controlled GEM
mappings is something entirely different.
> You seem to be saying that what I'm proposing here has a few pitfalls that have
> come up over the years. Perhaps we can massage iommufd to make our use case
> work? I can volunteer to do whatever work is needed, of course.
I am very confused about what you are even doing. Can you explain more
clearly please? What does you DT look like? What do your attach calls
do? How many struct devices do you have? Who is calling the DMA API?
Who is mapping the paging domain? And who handles cache flushing
issues for your incoherent architecture?
Jason
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-18 19:35 ` Jason Gunthorpe
@ 2026-11-18 19:41 ` Nicolas Dufresne
2026-11-18 21:06 ` Jason Gunthorpe
0 siblings, 1 reply; 23+ messages in thread
From: Nicolas Dufresne @ 2026-11-18 19:41 UTC (permalink / raw)
To: Jason Gunthorpe, Daniel Almeida
Cc: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova
[-- Attachment #1: Type: text/plain, Size: 986 bytes --]
Jason,
Le mercredi 18 novembre 2026 à 15:35 -0400, Jason Gunthorpe a écrit :
> > No, not OK :/ We want to use GEM shmem, which calls into the DMA API internally
> > through drm_gem_shmem_get_pages_sgt() -> dma_map_sgtable().
>
> You cannot continue to use DMA API once you start touching a paging
> domain directly. It is why driver_managed exist.
You are building a strong argument around something that is not what we intent
this binding to be used for. If you had cared about the context you'd know we
don't want to touch paging from userspace in this type of drivers. At least not
until we have a really good reason to include this complexity. And I agree, its
quite likely the iommu subsystem API would just fall short, its far from
perfect.
There is a line to draw between GPU and VPUs, and memory management is worth
looking into. But seriously, let's have this discussion once the driver is ready
for the ML, so we can code to correlate to.
Nicolas
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-18 19:41 ` Nicolas Dufresne
@ 2026-11-18 21:06 ` Jason Gunthorpe
0 siblings, 0 replies; 23+ messages in thread
From: Jason Gunthorpe @ 2026-11-18 21:06 UTC (permalink / raw)
To: Nicolas Dufresne
Cc: Daniel Almeida, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Abdiel Janulgue, iommu,
rust-for-linux, linux-kernel, driver-core, Detlev Casanova
On Wed, Nov 18, 2026 at 02:41:32PM -0500, Nicolas Dufresne wrote:
> Jason,
>
> Le mercredi 18 novembre 2026 à 15:35 -0400, Jason Gunthorpe a écrit :
> > > No, not OK :/ We want to use GEM shmem, which calls into the DMA API internally
> > > through drm_gem_shmem_get_pages_sgt() -> dma_map_sgtable().
> >
> > You cannot continue to use DMA API once you start touching a paging
> > domain directly. It is why driver_managed exist.
>
> You are building a strong argument around something that is not what we intent
> this binding to be used for. If you had cared about the context you'd know we
> don't want to touch paging from userspace in this type of drivers.
Go re-read Daniel's answer and perhaps you can understand my confusion:
https://lore.kernel.org/all/3EB4C2ED-B699-4532-A562-68B78F6E29C6@collabora.com/
If you want to
"We plan to alloc a paging domain when a video encoder session is created. Then
we call iommu_attach_device() before a job runs"
You cannot use the DMA API.
You absolutely CANNOT attach a brand new driver controlled domain, use
the DMA API to populate it, and then detach it.
Then we have the issue that the rust bindings expose iommu_map, which
cannot even be used on any non-coherent arch due to lack of cache
flushing.
Further, from a Rust binding perspective all these unsafe things I
talked about have to be blocked by the Rust compiler:
1) You cannot attach a domain to a driver_managed_dma=false device
2) You cannot call DMA API functions on a driver_managed_dma=true device
3) I object to exposing iommu_get_domain_for_dev()/etc to Rust
due to how unsafe it is, but if you were to do it then the
returned pointer must never be used with attach or iommu_map/unmap
functions.
4) Since there is no cache flushing a non-coherent arch must never be
allowed to call iommu_map(), since it is unsafe to create memory
incoherence.
Thus, what exactly is the plan here? Do you see my concern?
Jason
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/6] rust: iommu: add a paging domain abstraction
2026-11-12 17:00 [PATCH 0/6] rust: iommu: add a paging domain abstraction Daniel Almeida
` (6 preceding siblings ...)
2026-11-16 19:38 ` [PATCH 0/6] rust: iommu: add a paging domain abstraction Jason Gunthorpe
@ 2026-11-16 21:54 ` Detlev Casanova
7 siblings, 0 replies; 23+ messages in thread
From: Detlev Casanova @ 2026-11-16 21:54 UTC (permalink / raw)
To: Joerg Roedel (AMD),
Will Deacon, Robin Murphy, 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, Abdiel Janulgue,
Daniel Almeida
Cc: iommu, rust-for-linux, linux-kernel, driver-core, Nicolas Dufresne
Hi !
On Thursday, 12 November 2026 12:00:07 EST Daniel Almeida wrote:
> This series adds a Rust abstraction for IOMMU paging domains. A driver
> can allocate a domain, attach it to its device, map pages into it, and
> install a handler for its faults.
Note that a domain is not attached to a device, but rather a device is
attached to a domain.
Small difference but it influences how domains are applied to devices and what
happens when a devices is detached from its domain.
A device must always be attached to a domain, so when the device is detached
from a domain, it will be reattached to its default domain.
IIRC, you cannot detach the default domain (it returns an error).
Of course this doesn't prevent us to use domains for the encoder the core we
want to use can be attached to the client-specific domain before starting a
job. When idle, it goes back to its default domain (most likely empty).
I also belive the patchset still works, but I'll have a deeper look into
Attachment and some rewording might be necessary to avoid being misleading.
Detlev.
> This will be used by the upcoming rkvenc driver that my colleagues
> Detlev Casanova, Nicolas Dufresne and I are working on. More details on
> this driver can be found by watching Nicolas Dufresne's talk at LPC 2026
> [0].
>
> As part of the security model, each client will have its own IOMMU
> domain. This is needed as the submission model will let userspace set
> most of the encoder's registers directly, so the kernel cannot check
> every memory access that a job makes the encoder do. With a single
> domain, one client could make the encoder read or write the memory of
> another client.
>
> With a domain per client, the encoder can only reach the memory of the
> client whose job it is running, and any other access faults. The fault
> handler added in patch 6 lets the driver identify the faulting client
> and terminate its session.
>
> Patches 1 and 2 prepare for Domain::map(). Patch 1 moves the protection
> flags from the io-pgtable module to kernel::iommu, and patch 2 makes
> SGTable::as_raw() visible to the crate.
>
> Patch 3 adds Domain, a paging domain that is allocated for a bound
> device and freed on drop. Domain::new() is unsafe: the caller must
> ensure that the IOMMU driver behind its device sets a domain up fully
> when it is allocated and allows concurrent use of it.
>
> Patch 4 adds Domain::attach(), which returns an Attachment that
> detaches the domain on drop. Devices that share their IOMMU group with
> other devices are not supported.
>
> Patch 5 adds Domain::map(), which maps pages at an IOVA chosen by the
> caller and returns a Mapping that unmaps them on drop. The pages come
> from a MapSource, initially a boxed SGTable that owns its pages.
>
> Patch 6 adds Domain::with_fault_handler(), which allocates a domain
> with a FaultHandler installed. One handler can serve several domains.
>
> This is based on v7.4-rc1.
>
> [0]: https://www.youtube.com/watch?v=75E15eHY7ac
>
> ---
> Daniel Almeida (6):
> rust: iommu: move the protection flags to the module root
> rust: scatterlist: make SGTable::as_raw() visible to the crate
> rust: iommu: add the paging domain abstraction
> rust: iommu: add Domain::attach()
> rust: iommu: add Domain::map()
> rust: iommu: add Domain::with_fault_handler()
>
> rust/kernel/iommu/domain.rs | 425
> +++++++++++++++++++++++++++++++++++++++++++ rust/kernel/iommu/mod.rs |
> 18 ++
> rust/kernel/iommu/pgtable.rs | 16 +-
> rust/kernel/scatterlist.rs | 3 +-
> 4 files changed, 446 insertions(+), 16 deletions(-)
> ---
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> change-id: 20261111-b4-iommu-domain-f46a6e450c0d
>
> Best regards,
> --
> Daniel Almeida <daniel.almeida@collabora.com>
^ permalink raw reply [flat|nested] 23+ messages in thread