Linux driver-core infrastructure
 help / color / mirror / Atom feed
From: Detlev Casanova <detlev.casanova@collabora.com>
To: "Joerg Roedel (AMD)" <joro@8bytes.org>,
	"Will Deacon" <will@kernel.org>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Abdiel Janulgue" <abdiel.janulgue@gmail.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>
Cc: iommu@lists.linux.dev, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org, driver-core@lists.linux.dev,
	Nicolas Dufresne <nicolas.dufresne@collabora.com>
Subject: Re: [PATCH 4/6] rust: iommu: add Domain::attach()
Date: Mon, 16 Nov 2026 16:54:41 -0500	[thread overview]
Message-ID: <mT5CXvCzTcmD0ecGNGQ1Hg@collabora.com> (raw)
In-Reply-To: <20261112-b4-iommu-domain-v1-4-c29e86788dda@collabora.com>

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()) }; +    }
> +}





  reply	other threads:[~2026-11-16 21:54 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=mT5CXvCzTcmD0ecGNGQ1Hg@collabora.com \
    --to=detlev.casanova@collabora.com \
    --cc=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.com \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nicolas.dufresne@collabora.com \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=will@kernel.org \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox