Skip to contentSkip to conversation

rust: iommu: add device lifetime to IoPageTable

PATCH Applied Deborah Brouwer 4 participants 6 messages Jul 4 – Jul 31

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 rust: iommu: add device lifetime to IoPageTable

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 a lifetime parameter to IoPageTable. This ensures that the returned IoPageTable cannot outlive the bus device binding.

Also 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 and if a use-case appears for it.

---
Changes in v3:
- Rename 'bound to 'a since the page table could have a shorter
lifetime than the whole device bound.
- Remove the TODO comment about restoring the devres option.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Renamed new_raw() to new(), since the constructor is now safe.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]

---
 rust/kernel/iommu/pgtable.rs | 32 +++++++++-----------------------
 1 file changed, 9 insertions(+), 23 deletions(-)
rust/kernel/iommu/pgtable.rs +9 -23
index c88e38fd938a..f5f2706d72fb 100644
@@ -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<'a, F: IoPageTableFmt> {
     ptr: NonNull<bindings::io_pgtable_ops>,
+    _dev: PhantomData<&'a 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,10 @@ 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<'a, F: IoPageTableFmt> IoPageTable<'a, F> {
     /// 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(dev: &'a Device<Bound>, config: Config) -> Result<IoPageTable<'a, F>> {
         let mut raw_cfg = bindings::io_pgtable_cfg {
             quirks: config.quirks,
             pgsize_bitmap: config.pgsize_bitmap,
@@ -118,6 +103,7 @@ pub unsafe fn new_raw(dev: &Device<Bound>, config: Config) -> Result<IoPageTable
         // INVARIANT: We successfully created a valid page table.
         Ok(IoPageTable {
             ptr: NonNull::new(ops).ok_or(ENOMEM)?,
+            _dev: PhantomData,
             _marker: PhantomData,
         })
     }
@@ -240,7 +226,7 @@ extern "C" fn rust_tlb_flush_walk_noop(
 ) {
 }
 
-impl<F: IoPageTableFmt> Drop for IoPageTable<F> {
+impl<F: IoPageTableFmt> Drop for IoPageTable<'_, F> {
     fn drop(&mut self) {
         // SAFETY: The caller of `Self::ttbr()` promised that the page table is not live when this
         // destructor runs.
@@ -255,7 +241,7 @@ impl IoPageTableFmt for ARM64LPAES1 {
     const FORMAT: io_pgtable_fmt = bindings::io_pgtable_fmt_ARM_64_LPAE_S1 as io_pgtable_fmt;
 }
 
-impl IoPageTable<ARM64LPAES1> {
+impl IoPageTable<'_, ARM64LPAES1> {
     /// Access the `ttbr` field of the configuration.
     ///
     /// This is the physical address of the page table, which may be passed to the device that

---
base-commit: 2e75b259f4a4350f4d36026865be5a74554974aa
change-id: 20260703-pgtable_lt_b4-72db24dad3af

Best regards,

Signature · 1 line
Deborah Brouwer <[email protected]>
reviewer Reviewed-by on v3 1/1
#1 lore raw
Quoted 8 lines · Deborah Brouwer

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 a lifetime parameter to IoPageTable. This ensures that the returned IoPageTable cannot outlive the bus device binding.

Quoted 7 lines

Also 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 and if a use-case appears for it.

reviewer Reviewed-by on v3 1/1
#2 lore raw
In reply to Deborah Brouwer
Quoted 8 lines · Deborah Brouwer

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 a lifetime parameter to IoPageTable. This ensures that the returned IoPageTable cannot outlive the bus device binding.

Quoted 7 lines

Also 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 and if a use-case appears for it.

reviewer
#3 lore raw
In reply to Deborah Brouwer
Quoted 8 lines · Deborah Brouwer

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 a lifetime parameter to IoPageTable. This ensures that the returned IoPageTable cannot outlive the bus device binding.

Quoted 7 lines

Also 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 and if a use-case appears for it.

Joerg, this patch is a dependency for this other series:
https://lore.kernel.org/rust-for-linux/[email protected]

Would it be okay with you if I apply this patch via drm-rust-next together with the thing it's a dependency of?

It cleanly merges together with 'rust: iommu: replace core::mem::zeroed with Zeroable::zeroed' that you already picked up, so taking them through separate trees will not lead to a merge conflict.

Alice

maintainer Acked-by Joerg Roedel on v3 1/1
#4 lore raw
In reply to Deborah Brouwer
Quoted 22 lines · Deborah Brouwer

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 a lifetime parameter to IoPageTable. This ensures that the returned IoPageTable cannot outlive the bus device binding.

Also 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 and if a use-case appears for it.

---
Changes in v3:
- Rename 'bound to 'a since the page table could have a shorter
lifetime than the whole device bound.
- Remove the TODO comment about restoring the devres option.
- Link to v2: https://lore.kernel.org/r/[email protected]

Quoted 6 lines

Changes in v2:
- Renamed new_raw() to new(), since the constructor is now safe.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]

---
 rust/kernel/iommu/pgtable.rs | 32 +++++++++-----------------------
 1 file changed, 9 insertions(+), 23 deletions(-)
reviewer Applied
#5 lore raw
Quoted 23 lines · Joerg Roedel (AMD)
Quoted 22 lines · Deborah Brouwer

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 a lifetime parameter to IoPageTable. This ensures that the returned IoPageTable cannot outlive the bus device binding.

Also 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 and if a use-case appears for it.

---
Changes in v3:
- Rename 'bound to 'a since the page table could have a shorter
lifetime than the whole device bound.
- Remove the TODO comment about restoring the devres option.
- Link to v2: https://lore.kernel.org/r/[email protected]

Quoted 8 lines
Quoted 6 lines

Changes in v2:
- Renamed new_raw() to new(), since the constructor is now safe.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]

---
 rust/kernel/iommu/pgtable.rs | 32 +++++++++-----------------------
 1 file changed, 9 insertions(+), 23 deletions(-)

Applied to drm-rust-next. Thanks!