* [PATCH 0/6] rust: iommu: add a paging domain abstraction
@ 2026-11-12 17:00 Daniel Almeida
2026-11-12 17:00 ` [PATCH 1/6] rust: iommu: move the protection flags to the module root Daniel Almeida
` (7 more replies)
0 siblings, 8 replies; 10+ 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
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.
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] 10+ messages in thread
* [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; 10+ 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 related [flat|nested] 10+ 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; 10+ 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 related [flat|nested] 10+ 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; 10+ 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 related [flat|nested] 10+ 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; 10+ 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 related [flat|nested] 10+ 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; 10+ 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 related [flat|nested] 10+ 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; 10+ 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 related [flat|nested] 10+ 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-16 21:54 ` Detlev Casanova
7 siblings, 0 replies; 10+ 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] 10+ 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; 10+ 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] 10+ 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; 10+ 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] 10+ messages in thread
end of thread, other threads:[~2026-11-16 21:55 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 3/6] rust: iommu: add the paging domain abstraction Daniel Almeida
2026-11-12 17:00 ` [PATCH 4/6] rust: iommu: add Domain::attach() Daniel Almeida
2026-11-16 21:54 ` Detlev Casanova
2026-11-12 17:00 ` [PATCH 5/6] rust: iommu: add Domain::map() Daniel Almeida
2026-11-12 17:00 ` [PATCH 6/6] rust: iommu: add Domain::with_fault_handler() 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox