Skip to contentSkip to conversation

Rework PCI IRQ vector code

PATCH Applied Danilo Krummrich 3 participants 14 messages Aug 13 – Aug 16

Subsystems (4)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

v3 cover Rework PCI IRQ vector code

This series reworks the Rust PCI interrupt vector abstractions, motivated by review feedback on the nova-core interrupt support series [1].

Convert IrqVectorRegistration to a lifetime-managed owning type, replacing the devres-based approach. Since index() borrows the registration, the returned IrqVector inherits that lifetime, preventing the allocation from being dropped while any handler is live.

IrqVector embeds a resolved IrqRequest, making the conversion infallible. The request_irq()/request_threaded_irq() wrappers on Device are removed since their &self receiver could refer to an unrelated device.

Add pci_irq_type() as a C function in include/linux/pci.h, replacing open-coded checks across drivers [2], and wrap it for Rust.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://elixir.bootlin.com/linux/v7.1/source/drivers/net/ethernet/aquantia/atlantic/aq_pci_func.c#L196

Changes in v3:
- Rename s/count/len/, s/vector()/index()/, s/vector_count()/len()/.
- Remove redundant range check in index().
- Add missing #[inline].

Changes in v2:
- Drop the IrqRequestAnchor approach and keep IrqVector as a new type over IrqRequest.

Danilo Krummrich (5):
rust: pci: convert IrqVectorRegistration to a lifetime-managed owning
type
rust: pci: resolve IRQ in index() and embed IrqRequest in IrqVector
rust: pci: remove request_irq() and request_threaded_irq() from Device
PCI: Add pci_irq_type() to query the allocated interrupt type
rust: pci: expose the allocated interrupt type

 include/linux/pci.h    |  25 +++++
 rust/helpers/pci.c     |   5 +
 rust/kernel/pci.rs     |   3 +-
 rust/kernel/pci/irq.rs | 220 ++++++++++++++++++-----------------------
 4 files changed, 128 insertions(+), 125 deletions(-)

base-commit: dbaafe9cc56a996931eedfe043eb34418cc9cd9b

Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH v3 1/5 rust: pci: convert IrqVectorRegistration to a lifetime-managed owning type

Convert IrqVectorRegistration from a devres-managed internal type to a lifetime-annotated type that owns the PCI interrupt vector allocation. Dropping it frees the vectors.

IrqVector gains a reference to the IrqVectorRegistration it was derived from. Since index() borrows the registration, the compiler prevents the allocation from being dropped while any IrqVector (and hence any irq::Registration built from it) is still live.

alloc_irq_vectors() returns IrqVectorRegistration<'_> directly, giving drivers explicit control over the allocation lifetime, which is needed by net and block drivers that re-allocate vectors at runtime, e.g. during queue reconfiguration or device recovery.

---
 rust/kernel/pci.rs     |   3 +-
 rust/kernel/pci/irq.rs | 128 ++++++++++++++++++++++-------------------
 2 files changed, 70 insertions(+), 61 deletions(-)
rust/kernel/pci.rs +2 -1
index c6417af2bb17..2757a0cc0f11 100644
@@ -51,7 +51,8 @@
 pub use self::irq::{
     IrqType,
     IrqTypes,
-    IrqVector, //
+    IrqVector,
+    IrqVectorRegistration, //
 };
 
 /// An adapter for the registration of PCI drivers.
rust/kernel/pci/irq.rs +68 -60

208 lines not on this page · raw message

Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v3 1/5] rust: pci: convert IrqVectorRegistration to a lifetime-managed owning type

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The lifetime linkage between `IrqVectorRegistration`, `IrqVector`, and `irq::Registration` forces…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The lifetime linkage between IrqVectorRegistration, IrqVector, and irq::Registration forces a self-referential struct design, making the API unusable for device drivers. --

--- Patch [1]: [PATCH v3 1/5] rust: pci: convert IrqVectorRegistration to a lifetime-managed owning type --- commit d29a976d8fb0d09c0565565666049353a896a317
Author: Danilo Krummrich <[email protected]>

rust: pci: convert IrqVectorRegistration to a lifetime-managed owning type

This commit converts IrqVectorRegistration to a lifetime-annotated type that owns the PCI interrupt vector allocation. It modifies IrqVector to hold a reference to the registration, preventing the allocation from being dropped while vectors are still in use.

Quoted 4 lines
rust/kernel/pci/irq.rs +0 -0
index fea484dcf9cf2..daba86505cd2b 100644

[ ... ]

Quoted 7 lines
rust/kernel/pci/irq.rs +1 -0
@@ -78,6 +75,7 @@ const fn as_raw(self) -> u32 {
 #[derive(Clone, Copy)]
 pub struct IrqVector<'a> {
     dev: &'a Device<Bound>,
+    reg: &'a IrqVectorRegistration<'a>,
     index: u32,
 }

[ ... ]

rust/kernel/pci/irq.rs impl<'a> IrqVector<'a> { · line 156

Quoted 10 lines
rust/kernel/pci/irq.rs +7 -1
+    pub fn index(&self, index: usize) -> Result<IrqVector<'_>> {
+        if index >= self.len.get() {
+            return Err(EINVAL);
+        }
 
-        Ok(range)
+        // SAFETY: index is within bounds of this registration's allocation, and self.dev is
+        // the device it was allocated from.
+        Ok(unsafe { IrqVector::new(self.dev, self, index as u32) })
     }

[Severity: High]
Could this introduce a regression by creating a self-referential struct requirement for device drivers?

A typical device driver must maintain both the IrqVectorRegistration (to keep vectors allocated) and the irq::Registration (to keep handlers active) within its long-lived state struct across the driver lifecycle.

Looking at IrqVectorRegistration::index() in rust/kernel/pci/irq.rs, it returns an IrqVector tied to the local borrow of self. This lifetime dependency propagates to irq::Registration.

Storing both objects in the same driver struct requires a self-referential design, which cannot be safely modeled using the kernel's pin_init macro.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH v3 2/5 rust: pci: resolve IRQ in index() and embed IrqRequest in IrqVector

In reply to Danilo Krummrich

Move the pci_irq_vector() call from the TryInto<IrqRequest> impl into IrqVectorRegistration::index(), so the IRQ number is resolved eagerly.

IrqVector now embeds the resolved IrqRequest and a reference to the IrqVectorRegistration. The conversion to IrqRequest is infallible, which removes the need for pin_init_scope() in request_irq() / request_threaded_irq().

---
 rust/kernel/pci/irq.rs | 67 +++++++++++++++---------------------------
 1 file changed, 23 insertions(+), 44 deletions(-)
rust/kernel/pci/irq.rs +23 -44
index daba86505cd2..81b74c4c17d9 100644
@@ -68,32 +68,25 @@ const fn as_raw(self) -> u32 {
     }
 }
 
-/// Represents an allocated IRQ vector for a specific PCI device.
+/// A resolved IRQ vector from a PCI interrupt vector allocation.
 ///
-/// This type ties an IRQ vector to the device it was allocated for,
-/// ensuring the vector is only used with the correct device.
-#[derive(Clone, Copy)]
+/// Created by [`IrqVectorRegistration::index`] and consumed by [`Device::request_irq`] or
+/// [`Device::request_threaded_irq`]. Borrows the [`IrqVectorRegistration`] it was derived from,
+/// so the allocation stays live until the handler is freed.
 pub struct IrqVector<'a> {
-    dev: &'a Device<Bound>,
+    request: IrqRequest<'a>,
     reg: &'a IrqVectorRegistration<'a>,
-    index: u32,
 }
 
 impl<'a> IrqVector<'a> {
-    /// Creates a new [`IrqVector`] for the given device and index.
+    /// Creates a new [`IrqVector`] with an already resolved [`IrqRequest`].
     ///
     /// # Safety
     ///
-    /// - `index` must be a valid IRQ vector index for `reg`.
-    /// - `dev` must be the device `reg` was allocated from.
+    /// `request` must have been resolved from `reg`.
     #[inline]
-    unsafe fn new(dev: &'a Device<Bound>, reg: &'a IrqVectorRegistration<'a>, index: u32) -> Self {
-        Self { dev, reg, index }
-    }
-
-    /// Returns the raw vector index.
-    fn index(&self) -> u32 {
-        self.index
+    unsafe fn new(request: IrqRequest<'a>, reg: &'a IrqVectorRegistration<'a>) -> Self {
+        Self { request, reg }
     }
 
     /// Returns the [`IrqVectorRegistration`] this vector was derived from.
@@ -103,17 +96,10 @@ pub fn vectors(&self) -> &'a IrqVectorRegistration<'a> {
     }
 }
 
-impl<'a> TryInto<IrqRequest<'a>> for IrqVector<'a> {
-    type Error = Error;
-
-    fn try_into(self) -> Result<IrqRequest<'a>> {
-        // SAFETY: `self.dev.as_raw()` returns a valid pointer to a `struct pci_dev`.
-        let irq = unsafe { bindings::pci_irq_vector(self.dev.as_raw(), self.index()) };
-        if irq < 0 {
-            return Err(crate::error::Error::from_errno(irq));
-        }
-        // SAFETY: `irq` is guaranteed to be a valid IRQ number for `self.dev`.
-        Ok(unsafe { IrqRequest::new(self.dev.as_ref(), irq as u32) })
+impl<'a> From<IrqVector<'a>> for IrqRequest<'a> {
+    #[inline]
+    fn from(vector: IrqVector<'a>) -> Self {
+        vector.request
     }
 }
 
@@ -146,13 +132,14 @@ pub fn len(&self) -> usize {
     /// [`Self::len()`].
     #[inline]
     pub fn index(&self, index: usize) -> Result<IrqVector<'_>> {
-        if index >= self.len.get() {
-            return Err(EINVAL);
+        // SAFETY: `self.dev.as_raw()` is a valid pointer to a `struct pci_dev`.
+        let irq = unsafe { bindings::pci_irq_vector(self.dev.as_raw(), index as u32) };
+        if irq < 0 {
+            return Err(Error::from_errno(irq));
         }
 
-        // SAFETY: `index` is within bounds of this registration's allocation, and `self.dev` is
-        // the device it was allocated from.
-        Ok(unsafe { IrqVector::new(self.dev, self, index as u32) })
+        // SAFETY: `irq` is a valid IRQ number for `self.dev`, resolved from this registration.
+        Ok(unsafe { IrqVector::new(IrqRequest::new(self.dev.as_ref(), irq as u32), self) })
     }
 }
 
@@ -179,12 +166,8 @@ pub unsafe fn request_irq<'a, T: crate::irq::Handler + 'a>(
         name: &'static CStr,
         handler: impl PinInit<T, Error> + 'a,
     ) -> impl PinInit<irq::Registration<'a, T>, Error> + 'a {
-        pin_init::pin_init_scope(move || {
-            let request = vector.try_into()?;
-
-            // SAFETY: Caller guarantees the Registration will not be leaked.
-            Ok(unsafe { irq::Registration::<T>::new(request, flags, name, handler) })
-        })
+        // SAFETY: Caller guarantees the Registration will not be leaked.
+        unsafe { irq::Registration::<T>::new(vector.into(), flags, name, handler) }
     }
 
     /// Returns a [`kernel::irq::ThreadedRegistration`] for the given IRQ vector.
@@ -200,12 +183,8 @@ pub unsafe fn request_threaded_irq<'a, T: crate::irq::ThreadedHandler + 'a>(
         name: &'static CStr,
         handler: impl PinInit<T, Error> + 'a,
     ) -> impl PinInit<irq::ThreadedRegistration<'a, T>, Error> + 'a {
-        pin_init::pin_init_scope(move || {
-            let request = vector.try_into()?;
-
-            // SAFETY: Caller guarantees the Registration will not be leaked.
-            Ok(unsafe { irq::ThreadedRegistration::<T>::new(request, flags, name, handler) })
-        })
+        // SAFETY: Caller guarantees the Registration will not be leaked.
+        unsafe { irq::ThreadedRegistration::<T>::new(vector.into(), flags, name, handler) }
     }
 
     /// Allocate IRQ vectors for this PCI device.
Signature · 1 line
2.55.0
reviewer
#4 lore raw

Re: [PATCH v3 2/5] rust: pci: resolve IRQ in index() and embed IrqRequest in IrqVector

Quoted 68 lines · Danilo Krummrich

Move the pci_irq_vector() call from the TryInto<IrqRequest> impl into IrqVectorRegistration::index(), so the IRQ number is resolved eagerly.

IrqVector now embeds the resolved IrqRequest and a reference to the IrqVectorRegistration. The conversion to IrqRequest is infallible, which removes the need for pin_init_scope() in request_irq() / request_threaded_irq().

---
 rust/kernel/pci/irq.rs | 67 +++++++++++++++---------------------------
 1 file changed, 23 insertions(+), 44 deletions(-)
rust/kernel/pci/irq.rs +9 -18
index daba86505cd2..81b74c4c17d9 100644
@@ -68,32 +68,25 @@ const fn as_raw(self) -> u32 {
     }
 }
 
-/// Represents an allocated IRQ vector for a specific PCI device.
+/// A resolved IRQ vector from a PCI interrupt vector allocation.
 ///
-/// This type ties an IRQ vector to the device it was allocated for,
-/// ensuring the vector is only used with the correct device.
-#[derive(Clone, Copy)]
+/// Created by [`IrqVectorRegistration::index`] and consumed by [`Device::request_irq`] or
+/// [`Device::request_threaded_irq`]. Borrows the [`IrqVectorRegistration`] it was derived from,
+/// so the allocation stays live until the handler is freed.
 pub struct IrqVector<'a> {
-    dev: &'a Device<Bound>,
+    request: IrqRequest<'a>,
     reg: &'a IrqVectorRegistration<'a>,
-    index: u32,
 }
 
 impl<'a> IrqVector<'a> {
-    /// Creates a new [`IrqVector`] for the given device and index.
+    /// Creates a new [`IrqVector`] with an already resolved [`IrqRequest`].
     ///
     /// # Safety
     ///
-    /// - `index` must be a valid IRQ vector index for `reg`.
-    /// - `dev` must be the device `reg` was allocated from.
+    /// `request` must have been resolved from `reg`.
     #[inline]
-    unsafe fn new(dev: &'a Device<Bound>, reg: &'a IrqVectorRegistration<'a>, index: u32) -> Self {
-        Self { dev, reg, index }
-    }
-
-    /// Returns the raw vector index.
-    fn index(&self) -> u32 {
-        self.index
+    unsafe fn new(request: IrqRequest<'a>, reg: &'a IrqVectorRegistration<'a>) -> Self {
+        Self { request, reg }
     }
 
     /// Returns the [`IrqVectorRegistration`] this vector was derived from.
@@ -103,17 +96,10 @@ pub fn vectors(&self) -> &'a IrqVectorRegistration<'a> {
     }
 }
 
-impl<'a> TryInto<IrqRequest<'a>> for IrqVector<'a> {
-    type Error = Error;

rust/kernel/pci/irq.rs pub fn vectors(&self) -> &'a IrqVectorRegistration<'a> { · line 99

Quoted 10 lines
rust/kernel/pci/irq.rs +1 -9
-
-    fn try_into(self) -> Result<IrqRequest<'a>> {
-        // SAFETY: `self.dev.as_raw()` returns a valid pointer to a `struct pci_dev`.
-        let irq = unsafe { bindings::pci_irq_vector(self.dev.as_raw(), self.index()) };
-        if irq < 0 {
-            return Err(crate::error::Error::from_errno(irq));
-        }
-        // SAFETY: `irq` is guaranteed to be a valid IRQ number for `self.dev`.
-        Ok(unsafe { IrqRequest::new(self.dev.as_ref(), irq as u32) })
+impl<'a> From<IrqVector<'a>> for IrqRequest<'a> {

I feel that this is actually one of the prime candidate of DerefMove when (or if) Rust adds that.

If we have !Leak in the langauge, then we can drop the unsafe on irq::Registration::new, then we can move that to become a method on IrqRequest; if we also have DerefMove`, then you'd be able to do

    irq_vector.request_thread_irq(...)

and this will look super clean.

That said, we have neither DerefMove nor !Leak, so a unsafe constructor + a .into() does sound like the best option so far. But one can dream :)

Best,
Gary

Quoted 5 lines
rust/kernel/pci/irq.rs +3 -0
+    #[inline]
+    fn from(vector: IrqVector<'a>) -> Self {
+        vector.request
     }
 }
#5 lore raw

Re: [PATCH v3 2/5] rust: pci: resolve IRQ in index() and embed IrqRequest in IrqVector

In reply to Danilo Krummrich
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] The API design forces a self-referential lifetime bound, making it impossible to safely store both the vector registration and the IRQ handler in a driver struct, naturally leading to Use-After-Free during teardown.

Pre-existing issues:
- [High] Multiple concurrent IrqVectorRegistration instances can be created, leading to a Use-After-Free of active IRQ vectors on teardown. --

--- Patch [2]: [PATCH v3 2/5] rust: pci: resolve IRQ in index() and embed IrqRequest in IrqVector --- commit 517346220f40fc029bc56afcf3a1427744cf6f09
Author: Danilo Krummrich <[email protected]>

rust: pci: resolve IRQ in index() and embed IrqRequest in IrqVector

This commit eagerly resolves the IRQ number by moving the pci_irq_vector() call into IrqVectorRegistration::index(). It embeds the resolved IrqRequest into IrqVector, making the conversion infallible. This removes the need for pin_init_scope() in request_irq() and request_threaded_irq().

Quoted 5 lines
rust/kernel/pci/irq.rs +0 -0
index daba86505cd2b..81b74c4c17d98 100644
@@ -68,32 +68,25 @@ const fn as_raw(self) -> u32 {

[ ... ]

rust/kernel/pci/irq.rs const fn as_raw(self) -> u32 { · line 79

Quoted 6 lines

pub struct IrqVector<'a> {

rust/kernel/pci/irq.rs +1 -2
-    dev: &'a Device<Bound>,
+    request: IrqRequest<'a>,
     reg: &'a IrqVectorRegistration<'a>,
-    index: u32,
 }

[Severity: High]
Does this struct definition force a self-referential lifetime bound that makes it impossible to safely store both the vector registration and the IRQ handler in a driver struct?

Because IrqVector<'a> borrows IrqVectorRegistration<'a> with the exact same lifetime 'a as the device, request_irq() requires the resulting irq::Registration to share this lifetime. A long-lived driver struct would have to hold both the IrqVectorRegistration and the Registration that borrows it.

This creates a self-referential struct forbidden by Safe Rust. If a developer uses unsafe to bypass the restrictions to get it to compile, Rust drops struct fields in declaration order. If IrqVectorRegistration is declared before Registration, pci_free_irq_vectors() will execute before free_irq(), destroying MSI vectors while the IRQ handler is still registered and active in the system.

[ ... ]

Quoted 5 lines
rust/kernel/pci/irq.rs +0 -1
@@ -200,12 +183,8 @@ pub unsafe fn request_threaded_irq<'a, T: crate::irq::ThreadedHandler + 'a>(
         name: &'static CStr,
         handler: impl PinInit<T, Error> + 'a,
     ) -> impl PinInit<irq::ThreadedRegistration<'a, T>, Error> + 'a {
-        pin_init::pin_init_scope(move || {

rust/kernel/pci/irq.rs pub unsafe fn request_threaded_irq<'a, T: crate::irq::ThreadedHandler + 'a>( · line 190

Quoted 10 lines
rust/kernel/pci/irq.rs +2 -5
-            let request = vector.try_into()?;
-
-            // SAFETY: Caller guarantees the Registration will not be leaked.
-            Ok(unsafe { irq::ThreadedRegistration::<T>::new(request, flags, name, handler) })
-        })
+        // SAFETY: Caller guarantees the Registration will not be leaked.
+        unsafe { irq::ThreadedRegistration::<T>::new(vector.into(), flags, name, handler) }
     }
 
     /// Allocate IRQ vectors for this PCI device.

[Severity: High]
This is a pre-existing issue, but can multiple concurrent IrqVectorRegistration instances be created, leading to a Use-After-Free of active IRQ vectors on teardown?

Since Device::alloc_irq_vectors() takes an immutable reference (&self), a driver can call it multiple times. If a driver allocates MSI vectors and then falls back to allocating INTx vectors, the C API (pci_alloc_irq_vectors_affinity) successfully processes the INTx request without checking if MSI is already enabled. This returns a second IrqVectorRegistration.

When this second registration goes out of scope and drops, its Drop implementation calls pci_free_irq_vectors(), which disables and frees the global MSI state. The first registration's active IRQ handlers are then left operating on freed hardware state and descriptors.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#6 lore raw

PATCH v3 3/5 rust: pci: remove request_irq() and request_threaded_irq() from Device

In reply to Danilo Krummrich

Remove the thin wrappers on Device<Bound> that only forwarded to irq::Registration::new() and irq::ThreadedRegistration::new(). With IrqVector embedding a resolved IrqRequest, the conversion is infallible and drivers call irq::Registration::new(vector.into(), ...) directly.

Unlike the platform equivalents, which combine a fallible IRQ lookup with handler registration, the PCI wrappers add no value beyond namespacing. They also introduce a redundant device reference. IrqVector already carries a device borrow through its embedded IrqRequest, yet the wrappers required a second, potentially unrelated, &self receiver.

---
 rust/kernel/pci/irq.rs | 48 +++++-------------------------------------
 1 file changed, 5 insertions(+), 43 deletions(-)
rust/kernel/pci/irq.rs +5 -43
index 81b74c4c17d9..b6d1699ee3eb 100644
@@ -8,10 +8,7 @@
     device,
     device::Bound,
     error::to_result,
-    irq::{
-        self,
-        IrqRequest, //
-    },
+    irq::IrqRequest,
     prelude::*, //
 };
 use core::num::NonZero;
@@ -70,9 +67,8 @@ const fn as_raw(self) -> u32 {
 
 /// A resolved IRQ vector from a PCI interrupt vector allocation.
 ///
-/// Created by [`IrqVectorRegistration::index`] and consumed by [`Device::request_irq`] or
-/// [`Device::request_threaded_irq`]. Borrows the [`IrqVectorRegistration`] it was derived from,
-/// so the allocation stays live until the handler is freed.
+/// Created by [`IrqVectorRegistration::index`]. Convert to [`IrqRequest`] via [`From`] to register
+/// a handler with [`irq::Registration::new`](crate::irq::Registration::new).
 pub struct IrqVector<'a> {
     request: IrqRequest<'a>,
     reg: &'a IrqVectorRegistration<'a>,
@@ -153,40 +149,6 @@ fn drop(&mut self) {
 }
 
 impl Device<device::Bound> {
-    /// Returns a [`kernel::irq::Registration`] for the given IRQ vector.
-    ///
-    /// # Safety
-    ///
-    /// Callers must not `mem::forget()` the resulting [`irq::Registration`] or otherwise prevent
-    /// its [`Drop`] implementation from running.
-    pub unsafe fn request_irq<'a, T: crate::irq::Handler + 'a>(
-        &'a self,
-        vector: IrqVector<'a>,
-        flags: irq::Flags,
-        name: &'static CStr,
-        handler: impl PinInit<T, Error> + 'a,
-    ) -> impl PinInit<irq::Registration<'a, T>, Error> + 'a {
-        // SAFETY: Caller guarantees the Registration will not be leaked.
-        unsafe { irq::Registration::<T>::new(vector.into(), flags, name, handler) }
-    }
-
-    /// Returns a [`kernel::irq::ThreadedRegistration`] for the given IRQ vector.
-    ///
-    /// # Safety
-    ///
-    /// Callers must not `mem::forget()` the resulting [`irq::ThreadedRegistration`] or otherwise
-    /// prevent its [`Drop`] implementation from running.
-    pub unsafe fn request_threaded_irq<'a, T: crate::irq::ThreadedHandler + 'a>(
-        &'a self,
-        vector: IrqVector<'a>,
-        flags: irq::Flags,
-        name: &'static CStr,
-        handler: impl PinInit<T, Error> + 'a,
-    ) -> impl PinInit<irq::ThreadedRegistration<'a, T>, Error> + 'a {
-        // SAFETY: Caller guarantees the Registration will not be leaked.
-        unsafe { irq::ThreadedRegistration::<T>::new(vector.into(), flags, name, handler) }
-    }
-
     /// Allocate IRQ vectors for this PCI device.
     ///
     /// Allocates between `min_vecs` and `max_vecs` interrupt vectors for the device.
@@ -195,8 +157,8 @@ pub unsafe fn request_threaded_irq<'a, T: crate::irq::ThreadedHandler + 'a>(
     /// will try them in order of preference: MSI-X first, then MSI, then INTx interrupts.
     ///
     /// The allocated vectors are freed when the returned [`IrqVectorRegistration`] is dropped.
-    /// IRQ handlers registered via [`Self::request_irq`] or [`Self::request_threaded_irq`]
-    /// borrow from the registration, so the compiler ensures they are freed first.
+    /// Use [`IrqVectorRegistration::index`] to obtain an [`IrqVector`] for a given vector
+    /// index.
     ///
     /// # Arguments
     ///
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH v3 3/5] rust: pci: remove request_irq() and request_threaded_irq() from Device

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The new `IrqVectorRegistration` lifetime design creates a self-referential lifetime trap, making…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The new IrqVectorRegistration lifetime design creates a self-referential lifetime trap, making it practically impossible for drivers to safely store their IRQ handler registrations in their driver data structures. --

--- Patch [3]: [PATCH v3 3/5] rust: pci: remove request_irq() and request_threaded_irq() from Device --- commit 391f2070be9b225ebd0a122e28fa0091732c94e1
Author: Danilo Krummrich <[email protected]>

rust: pci: remove request_irq() and request_threaded_irq() from Device

Remove the thin wrappers on Device<Bound> that only forwarded to irq::Registration::new() and irq::ThreadedRegistration::new(). With IrqVector embedding a resolved IrqRequest, the conversion is infallible and drivers call irq::Registration::new(vector.into(), ...) directly.

Quoted 16 lines
rust/kernel/pci/irq.rs +2 -3
index 81b74c4c17d98..b6d1699ee3ebf 100644
@@ -70,9 +67,8 @@ const fn as_raw(self) -> u32 {
 
 /// A resolved IRQ vector from a PCI interrupt vector allocation.
 ///
-/// Created by [`IrqVectorRegistration::index`] and consumed by [`Device::request_irq`] or
-/// [`Device::request_threaded_irq`]. Borrows the [`IrqVectorRegistration`] it was derived from,
-/// so the allocation stays live until the handler is freed.
+/// Created by [`IrqVectorRegistration::index`]. Convert to [`IrqRequest`] via [`From`] to register
+/// a handler with [`irq::Registration::new`](crate::irq::Registration::new).
 pub struct IrqVector<'a> {
     request: IrqRequest<'a>,
     reg: &'a IrqVectorRegistration<'a>,
Quoted 1 line

}

[Severity: High]
Does this new IrqVectorRegistration lifetime design create a self-referential lifetime trap that makes it impossible for drivers to safely store their IRQ handler registrations?

Because IrqVector uses a single lifetime parameter 'a for both its embedded IrqRequest<'a> and its borrow of the registration &'a IrqVectorRegistration<'a>, the returned IrqRequest is forced to inherit the short lifetime of the method call's borrow of the IrqVectorRegistration when converted via From:

rust/kernel/pci/irq.rs:

impl<'a> From<IrqVector<'a>> for IrqRequest<'a> {
    #[inline]
    fn from(vector: IrqVector<'a>) -> Self {
        vector.request
    }
}

This conversion propagates the strictly constrained borrow lifetime into the IrqRequest, permanently tying any resulting irq::Registration to the local scope of the IrqVectorRegistration.

For a driver to return from its probe() function, it must store both the vector registration and the IRQ handler registration in its main device struct. However, this creates a self-referential struct (the IRQ registration borrows the sibling vector registration field), which is forbidden in safe Rust.

There is no devres escape hatch because devres::register requires 'static, and DevresLt cannot encapsulate a borrow of a local field.

This affects all PCI drivers attempting to use the new API, breaking the primary use case of the subsystem.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#8 lore raw

PATCH v3 4/5 PCI: Add pci_irq_type() to query the allocated interrupt type

In reply to Danilo Krummrich

Add a helper that returns PCI_IRQ_MSIX, PCI_IRQ_MSI, or PCI_IRQ_INTX based on the interrupt type the PCI core selected after pci_alloc_irq_vectors().

Several drivers already open-code this check against pdev->msix_enabled and pdev->msi_enabled, or even open code this helper [1].

A common helper avoids the duplication and keeps drivers from accessing the bitfield directly (see also [2]).

---
 include/linux/pci.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/pci.h +25 -0
index 64b308b6e61c..80b8561b5be0 100644
@@ -1783,6 +1783,26 @@ void pci_free_irq_vectors(struct pci_dev *dev);
 int pci_irq_vector(struct pci_dev *dev, unsigned int nr);
 const struct cpumask *pci_irq_get_affinity(struct pci_dev *pdev, int vec);
 
+/**
+ * pci_irq_type - Get the interrupt type of a PCI device
+ * @pdev: the PCI device to operate on
+ *
+ * Discriminate the interrupt type the PCI core selected for this device
+ * after a successful pci_alloc_irq_vectors() call.
+ *
+ * Return: %PCI_IRQ_MSIX, %PCI_IRQ_MSI, or %PCI_IRQ_INTX.
+ */
+static inline unsigned int pci_irq_type(struct pci_dev *pdev)
+{
+	if (pdev->msix_enabled)
+		return PCI_IRQ_MSIX;
+
+	if (pdev->msi_enabled)
+		return PCI_IRQ_MSI;
+
+	return PCI_IRQ_INTX;
+}
+
 #else
 static inline int pci_msi_vec_count(struct pci_dev *dev) { return -ENOSYS; }
 static inline void pci_disable_msi(struct pci_dev *dev) { }
@@ -1845,6 +1865,11 @@ static inline const struct cpumask *pci_irq_get_affinity(struct pci_dev *pdev,
 {
 	return cpu_possible_mask;
 }
+
+static inline unsigned int pci_irq_type(struct pci_dev *pdev)
+{
+	return PCI_IRQ_INTX;
+}
 #endif
 
 /**
Signature · 1 line
2.55.0
#9 lore raw

Re: [PATCH v3 4/5] PCI: Add pci_irq_type() to query the allocated interrupt type

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing dummy implementation of pci_irq_type() for !CONFIG_PCI configurations --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing dummy implementation of pci_irq_type() for !CONFIG_PCI configurations --

--- Patch [4]: [PATCH v3 4/5] PCI: Add pci_irq_type() to query the allocated interrupt type --- commit dc3ae5a104b31d99bffe0b8a2992cb68e0130731
Author: Danilo Krummrich <[email protected]>

PCI: Add pci_irq_type() to query the allocated interrupt type

This patch adds a helper function to return the interrupt type (MSI-X, MSI, or INTX) that the PCI core selected after a successful call to pci_alloc_irq_vectors(). It provides a common way for drivers to query this information without directly accessing the pci_dev bitfields.

Quoted 4 lines
include/linux/pci.h +0 -0
index 64b308b6e61c1..80b8561b5be0b 100644

[ ... ]

include/linux/pci.h static inline const struct cpumask *pci_irq_get_affinity(struct pci_dev *pdev, · line 1873

Quoted 10 lines
include/linux/pci.h +5 -0
@@ -1845,6 +1865,11 @@ static inline const struct cpumask *pci_irq_get_affinity(struct pci_dev *pdev,
 {
 	return cpu_possible_mask;
 }
+
+static inline unsigned int pci_irq_type(struct pci_dev *pdev)
+{
+	return PCI_IRQ_INTX;
+}
 #endif

[Severity: Low]
Does this also need a dummy implementation in the !CONFIG_PCI block?

This patch adds a fallback implementation for when CONFIG_PCI_MSI is disabled, but misses adding a stub for when CONFIG_PCI is completely disabled. Other IRQ vector API functions provide stubs in the !CONFIG_PCI block to ensure drivers compile cleanly on architectures without PCI support:

include/linux/pci.h:
static inline int
pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
unsigned int max_vecs, unsigned int flags)

{
	return -ENOSPC;
}

static inline void pci_free_irq_vectors(struct pci_dev *dev) {
}
#endif /* CONFIG_PCI */

Could the lack of a pci_irq_type() stub in include/linux/pci.h introduce a compile regression when building a kernel with CONFIG_PCI=n where a generic driver calls pci_irq_type()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#10 lore raw

PATCH v3 5/5 rust: pci: expose the allocated interrupt type

In reply to Danilo Krummrich

Add irq_type() on IrqVectorRegistration and IrqVector, wrapping the new pci_irq_type() C function. A driver whose interrupt acknowledgment depends on the type (MSI-X vs MSI vs INTx) queries it here rather than assuming which type the PCI core selected.

---
 rust/helpers/pci.c     |  5 +++++
 rust/kernel/pci/irq.rs | 23 +++++++++++++++++++++++
 2 files changed, 28 insertions(+)
rust/helpers/pci.c +5 -0
index e44905317d75..23b06becb448 100644
@@ -24,6 +24,11 @@ __rust_helper bool rust_helper_dev_is_pci(const struct device *dev)
 	return dev_is_pci(dev);
 }
 
+__rust_helper unsigned int rust_helper_pci_irq_type(struct pci_dev *pdev)
+{
+	return pci_irq_type(pdev);
+}
+
 #ifndef CONFIG_PCI_MSI
 __rust_helper int rust_helper_pci_alloc_irq_vectors(struct pci_dev *dev,
 						    unsigned int min_vecs,
rust/kernel/pci/irq.rs +23 -0
index b6d1699ee3eb..6741046ec1c0 100644
@@ -33,6 +33,16 @@ const fn as_raw(self) -> u32 {
             IrqType::MsiX => bindings::PCI_IRQ_MSIX,
         }
     }
+
+    /// Construct from raw value.
+    #[inline]
+    const fn from_raw(raw: u32) -> Self {
+        match raw {
+            bindings::PCI_IRQ_MSIX => IrqType::MsiX,
+            bindings::PCI_IRQ_MSI => IrqType::Msi,
+            _ => IrqType::Intx,
+        }
+    }
 }
 
 /// Set of IRQ types that can be used for PCI interrupt allocation.
@@ -90,6 +100,12 @@ unsafe fn new(request: IrqRequest<'a>, reg: &'a IrqVectorRegistration<'a>) -> Se
     pub fn vectors(&self) -> &'a IrqVectorRegistration<'a> {
         self.reg
     }
+
+    /// Returns the interrupt type the PCI core selected for this vector's allocation.
+    #[inline]
+    pub fn irq_type(&self) -> IrqType {
+        self.reg.irq_type()
+    }
 }
 
 impl<'a> From<IrqVector<'a>> for IrqRequest<'a> {
@@ -122,6 +138,13 @@ pub fn len(&self) -> usize {
         self.len.get()
     }
 
+    /// Returns the interrupt type the PCI core selected for this allocation.
+    #[inline]
+    pub fn irq_type(&self) -> IrqType {
+        // SAFETY: `self.dev.as_raw()` is a valid pointer to a `struct pci_dev`.
+        IrqType::from_raw(unsafe { bindings::pci_irq_type(self.dev.as_raw()) })
+    }
+
     /// Returns the [`IrqVector`] at `index`.
     ///
     /// Returns [`EINVAL`] if the `index` is out of bounds for the length reported by
Signature · 1 line
2.55.0
#11 lore raw

Re: [PATCH v3 5/5] rust: pci: expose the allocated interrupt type

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] rust_helper_pci_irq_type() causes a build failure when CONFIG_PCI is disabled because…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] rust_helper_pci_irq_type() causes a build failure when CONFIG_PCI is disabled because pci_irq_type() is undefined. --

--- Patch [5]: [PATCH v3 5/5] rust: pci: expose the allocated interrupt type --- commit 60bd93f7488eb98904a6a089158302157e26da1d
Author: Danilo Krummrich <[email protected]>

rust: pci: expose the allocated interrupt type

This commit adds irq_type() on IrqVectorRegistration and IrqVector, wrapping the new pci_irq_type() C function. It allows a driver to query the allocated interrupt type (MSI-X, MSI, or INTx).

rust/helpers/pci.c __rust_helper bool rust_helper_dev_is_pci(const struct device *dev) · line 31

Quoted 13 lines
rust/helpers/pci.c +5 -0
index e44905317d759..23b06becb4489 100644
@@ -24,6 +24,11 @@ __rust_helper bool rust_helper_dev_is_pci(const struct device *dev)
 	return dev_is_pci(dev);
 }
 
+__rust_helper unsigned int rust_helper_pci_irq_type(struct pci_dev *pdev)
+{
+	return pci_irq_type(pdev);
+}
+

[Severity: Low]
Does this code cause a build failure when CONFIG_PCI is disabled?

It looks like this regression could result in an implicit declaration error, because there is no fallback stub for pci_irq_type() in the CONFIG_PCI=n block of include/linux/pci.h, and this helper is unconditionally compiled here.

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
reviewer Reviewed-by on the v3 series
#12 lore raw
In reply to Danilo Krummrich
Quoted 27 lines · Danilo Krummrich

This series reworks the Rust PCI interrupt vector abstractions, motivated by review feedback on the nova-core interrupt support series [1].

Convert IrqVectorRegistration to a lifetime-managed owning type, replacing the devres-based approach. Since index() borrows the registration, the returned IrqVector inherits that lifetime, preventing the allocation from being dropped while any handler is live.

IrqVector embeds a resolved IrqRequest, making the conversion infallible. The request_irq()/request_threaded_irq() wrappers on Device are removed since their &self receiver could refer to an unrelated device.

Add pci_irq_type() as a C function in include/linux/pci.h, replacing open-coded checks across drivers [2], and wrap it for Rust.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://elixir.bootlin.com/linux/v7.1/source/drivers/net/ethernet/aquantia/atlantic/aq_pci_func.c#L196

Changes in v3:
- Rename s/count/len/, s/vector()/index()/, s/vector_count()/len()/.
- Remove redundant range check in index().
- Add missing #[inline].

Changes in v2:
- Drop the IrqRequestAnchor approach and keep IrqVector as a new type over IrqRequest.

Quoted 6 lines

Danilo Krummrich (5):
rust: pci: convert IrqVectorRegistration to a lifetime-managed owning
type
rust: pci: resolve IRQ in index() and embed IrqRequest in IrqVector
rust: pci: remove request_irq() and request_threaded_irq() from Device
PCI: Add pci_irq_type() to query the allocated interrupt type

For the series:

I think the duplicate irq alloc issue is still worth solving, but I agree that it should be a separate series.

Quoted 10 lines

rust: pci: expose the allocated interrupt type

 include/linux/pci.h    |  25 +++++
 rust/helpers/pci.c     |   5 +
 rust/kernel/pci.rs     |   3 +-
 rust/kernel/pci/irq.rs | 220 ++++++++++++++++++-----------------------
 4 files changed, 128 insertions(+), 125 deletions(-)

base-commit: dbaafe9cc56a996931eedfe043eb34418cc9cd9b

author maintainer Applied
#13 lore raw
In reply to Danilo Krummrich
Quoted 1 line · Danilo Krummrich

[PATCH v3 0/5] Rework PCI IRQ vector code

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/5] rust: pci: convert IrqVectorRegistration to a lifetime-managed owning type

      commit: 17d4a399e8b8
[2/5] rust: pci: resolve IRQ in index() and embed IrqRequest in IrqVector
      commit: 2fb7755b0a7e
[3/5] rust: pci: remove request_irq() and request_threaded_irq() from Device
      commit: 6ca38086b4ee

[4/5] PCI: Add pci_irq_type() to query the allocated interrupt type
commit: f146c7bc85a5

[ Add missing pci_irq_type() stub for CONFIG_PCI=n. ]

[5/5] rust: pci: expose the allocated interrupt type

      commit: 3b5ea0f078e1

The patches will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patches are in the driver-core-testing branch and will be promoted to driver-core-next after validation.