The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Deborah Brouwer <deborah.brouwer@collabora.com>
Cc: "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>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	iommu@lists.linux.dev, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org, samitolvanen@google.com,
	boris.brezillon@collabora.com, laura.nao@collabora.com,
	daniel.almeida@collabora.com
Subject: Re: [PATCH] rust: iommu: add device lifetime to IoPageTable
Date: Fri, 3 Jul 2026 06:44:01 +0000	[thread overview]
Message-ID: <akdaMa7hQbAjWbox@google.com> (raw)
In-Reply-To: <20260702-pgtable_lt_v1-v1-1-18d9c9812a1a@collabora.com>

On Thu, Jul 02, 2026 at 04:47:39PM -0700, Deborah Brouwer wrote:
> Currently, using a raw IoPageTable is unsafe because the returned
> IoPageTable is not tied to the device driver binding lifetime.
> 
> Since device drivers now receive a lifetime parameter <'bound>
> representing the interval during which a device driver is bound to its bus
> device, add this lifetime parameter to IoPageTable. This ensures that
> the returned IoPageTable cannot outlive the bus device binding.
> 
> Also temporarily remove the option to create a page table as a device
> resource since currently Devres is not compatible with resources that have
> a lifetime parameter. This option can be restored once the lifetime-aware
> wrapper for devres is available.
> 
> Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
> Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
> ---
>  rust/kernel/iommu/pgtable.rs | 32 ++++++++++----------------------
>  1 file changed, 10 insertions(+), 22 deletions(-)
> 
> diff --git a/rust/kernel/iommu/pgtable.rs b/rust/kernel/iommu/pgtable.rs
> index c88e38fd938a..d4a16283b5a7 100644
> --- a/rust/kernel/iommu/pgtable.rs
> +++ b/rust/kernel/iommu/pgtable.rs
> @@ -16,7 +16,6 @@
>          Bound,
>          Device, //
>      },
> -    devres::Devres,
>      error::to_result,
>      io::PhysAddr,
>      prelude::*, //
> @@ -59,15 +58,16 @@ pub struct Config {
>  /// # Invariants
>  ///
>  /// The pointer references a valid io page table.
> -pub struct IoPageTable<F: IoPageTableFmt> {
> +pub struct IoPageTable<'bound, F: IoPageTableFmt> {
>      ptr: NonNull<bindings::io_pgtable_ops>,
> +    _dev: PhantomData<&'bound Device<Bound>>,
>      _marker: PhantomData<F>,
>  }
>  
>  // SAFETY: `struct io_pgtable_ops` is not restricted to a single thread.
> -unsafe impl<F: IoPageTableFmt> Send for IoPageTable<F> {}
> +unsafe impl<F: IoPageTableFmt> Send for IoPageTable<'_, F> {}
>  // SAFETY: `struct io_pgtable_ops` may be accessed concurrently.
> -unsafe impl<F: IoPageTableFmt> Sync for IoPageTable<F> {}
> +unsafe impl<F: IoPageTableFmt> Sync for IoPageTable<'_, F> {}
>  
>  /// The format used by this page table.
>  pub trait IoPageTableFmt: 'static {
> @@ -75,25 +75,12 @@ pub trait IoPageTableFmt: 'static {
>      const FORMAT: io_pgtable_fmt;
>  }
>  
> -impl<F: IoPageTableFmt> IoPageTable<F> {
> -    /// Create a new `IoPageTable` as a device resource.
> -    #[inline]
> -    pub fn new(
> -        dev: &Device<Bound>,
> -        config: Config,
> -    ) -> impl PinInit<Devres<IoPageTable<F>>, Error> + '_ {
> -        // SAFETY: Devres ensures that the value is dropped during device unbind.
> -        Devres::new(dev, unsafe { Self::new_raw(dev, config) })
> -    }
> +impl<'bound, F: IoPageTableFmt> IoPageTable<'bound, F> {
> +    // TODO: Create a new `IoPageTable` as a device resource when DevresLt is available.
>  
>      /// Create a new `IoPageTable`.
> -    ///
> -    /// # Safety
> -    ///
> -    /// If successful, then the returned `IoPageTable` must be dropped before the device is
> -    /// unbound.
>      #[inline]
> -    pub unsafe fn new_raw(dev: &Device<Bound>, config: Config) -> Result<IoPageTable<F>> {
> +    pub fn new_raw(dev: &'bound Device<Bound>, config: Config) -> Result<IoPageTable<'bound, F>> {

The name new_raw() indicates that this is something you only use in rare
cases when you are doing weird stuff. But with this change, I no longer
think that's the case.

I would suggest renaming this method to just `new()`.

As for creating a device resoure version with DevresLt, I think that can
be the secondary constructor and be called `new_devres()` or similar.

Otherwise LGTM.

Alice

  reply	other threads:[~2026-07-03  6:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-02 23:47 [PATCH] rust: iommu: add device lifetime to IoPageTable Deborah Brouwer
2026-07-03  6:44 ` Alice Ryhl [this message]
2026-07-03 16:53   ` Danilo Krummrich

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=akdaMa7hQbAjWbox@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=boris.brezillon@collabora.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=deborah.brouwer@collabora.com \
    --cc=gary@garyguo.net \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=laura.nao@collabora.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=samitolvanen@google.com \
    --cc=tmgross@umich.edu \
    --cc=will@kernel.org \
    /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