Skip to contentSkip to conversation

Rework PCI IRQ vector code

PATCH Superseded Danilo Krummrich 3 participants 9 messages Aug 10 – Aug 11

Subsystems (5)
PCI Driver core Rust PCI Rust +1 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

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. Add an anchor generic on IrqRequest and irq::Registration to create a borrow chain that prevents the vector allocation from being dropped while any IRQ handler is still registered.

Remove IrqVector; resolve a vector index to an IrqRequest directly via IrqVectorRegistration::request().

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

Danilo Krummrich (5):
rust: irq: add anchor generic to IrqRequest and Registration
rust: pci: convert IrqVectorRegistration to a lifetime-managed owning
type
rust: pci: remove IrqVector and resolve IrqRequest directly
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/irq.rs         |  10 +-
 rust/kernel/irq/request.rs |  86 +++++++++++----
 rust/kernel/pci.rs         |   2 +-
 rust/kernel/pci/irq.rs     | 213 ++++++++++++++-----------------------
 6 files changed, 182 insertions(+), 159 deletions(-)

base-commit: dbaafe9cc56a996931eedfe043eb34418cc9cd9b

Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 1/5 rust: irq: add anchor generic to IrqRequest and Registration

Add an IrqRequestAnchor marker trait and a generic parameter A (defaulting to ()) on IrqRequest, Registration, and ThreadedRegistration.

Bus-specific IRQ sources can implement IrqRequestAnchor for a reference to their allocation type, so that the Registration stores a real borrow of the allocation and prevents it from being dropped while the handler is live.

The default anchor () keeps all existing behavior unchanged; platform devices are unaffected.

Add a new constructor IrqRequest::new_anchored() that accepts the anchor value. The existing IrqRequest::new() continues to produce IrqRequest<'a, ()>.

---
 rust/kernel/irq.rs         | 10 ++++-
 rust/kernel/irq/request.rs | 86 ++++++++++++++++++++++++++++----------
 2 files changed, 71 insertions(+), 25 deletions(-)
rust/kernel/irq.rs +8 -2
index 09ef1e7f853c..dd3cc6969ebf 100644
@@ -19,6 +19,12 @@
 pub use flags::Flags;
 
 pub use request::{
-    Handler, IrqRequest, IrqReturn, Registration, ThreadedHandler, ThreadedIrqReturn,
-    ThreadedRegistration,
+    Handler,
+    IrqRequest,
+    IrqRequestAnchor,
+    IrqReturn,
+    Registration,
+    ThreadedHandler,
+    ThreadedIrqReturn,
+    ThreadedRegistration, //
 };
rust/kernel/irq/request.rs +63 -23

197 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#2 lore raw

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

In reply to Danilo Krummrich

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. This reference flows through IrqRequest (via the IrqRequestAnchor generic) into irq::Registration, creating a borrow chain that prevents the vector allocation from being dropped while any handler is still registered.

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

---
 rust/kernel/pci.rs     |   3 +-
 rust/kernel/pci/irq.rs | 149 ++++++++++++++++++++++-------------------
 2 files changed, 83 insertions(+), 69 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 +81 -68

240 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH 3/5 rust: pci: remove IrqVector and resolve IrqRequest directly

In reply to Danilo Krummrich

Remove the IrqVector intermediate type; IrqVectorRegistration::request() now resolves a vector index to an IrqRequest directly, combining the bounds check and pci_irq_vector() call in one step.

Also remove Device::request_irq() and Device::request_threaded_irq(), which only exist to convert IrqVector to IrqRequest. Drivers pass the IrqRequest from IrqVectorRegistration::request() to irq::Registration::new() directly.

---
 rust/kernel/pci.rs     |   1 -
 rust/kernel/pci/irq.rs | 109 +++++------------------------------------
 2 files changed, 13 insertions(+), 97 deletions(-)
rust/kernel/pci.rs +0 -1
index 2757a0cc0f11..f7f6b21f64bf 100644
@@ -51,7 +51,6 @@
 pub use self::irq::{
     IrqType,
     IrqTypes,
-    IrqVector,
     IrqVectorRegistration, //
 };
 
rust/kernel/pci/irq.rs +13 -96

151 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#6 lore raw

PATCH 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
author maintainer
#8 lore raw

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

In reply to Danilo Krummrich

Add irq_type() on IrqVectorRegistration, 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 | 17 +++++++++++++++++
 2 files changed, 22 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 +17 -0
index d69ab6435a80..618310e4535c 100644
@@ -36,6 +36,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.
@@ -99,6 +109,13 @@ pub fn vector_count(&self) -> usize {
         self.count.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()) })
+    }
+
     /// Resolves the vector at `index` to an [`IrqRequest`].
     ///
     /// The returned [`IrqRequest`] borrows from this registration, ensuring the vector allocation
Signature · 1 line
2.55.0
reviewer
#3 lore raw

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

In reply to Danilo Krummrich
Quoted 104 lines · Danilo Krummrich

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. This reference flows through IrqRequest (via the IrqRequestAnchor generic) into irq::Registration, creating a borrow chain that prevents the vector allocation from being dropped while any handler is still registered.

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

---
 rust/kernel/pci.rs     |   3 +-
 rust/kernel/pci/irq.rs | 149 ++++++++++++++++++++++-------------------
 2 files changed, 83 insertions(+), 69 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 +20 -13
index fea484dcf9cf..38a7d72dcda7 100644
@@ -7,17 +7,15 @@
     bindings,
     device,
     device::Bound,
-    devres,
     error::to_result,
     irq::{
         self,
-        IrqRequest, //
+        IrqRequest,
+        IrqRequestAnchor, //
     },
-    prelude::*,
-    str::CStr,
-    sync::aref::ARef, //
+    prelude::*, //
 };
-use core::ops::RangeInclusive;
+use core::num::NonZero;
 
 /// IRQ type flags for PCI interrupt allocation.
 #[derive(Debug, Clone, Copy)]
@@ -78,6 +76,7 @@ const fn as_raw(self) -> u32 {
 #[derive(Clone, Copy)]
 pub struct IrqVector<'a> {
     dev: &'a Device<Bound>,
+    reg: &'a IrqVectorRegistration<'a>,
     index: u32,
 }
 
@@ -86,10 +85,11 @@ impl<'a> IrqVector<'a> {
     ///
     /// # Safety
     ///
-    /// - `index` must be a valid IRQ vector index for `dev`.
-    /// - `dev` must point to a [`Device`] that has successfully allocated IRQ vectors.
-    unsafe fn new(dev: &'a Device<Bound>, index: u32) -> Self {
-        Self { dev, index }
+    /// - `index` must be a valid IRQ vector index for `reg`.
+    /// - `dev` must be the device `reg` was allocated from.
+    #[inline]
+    unsafe fn new(dev: &'a Device<Bound>, reg: &'a IrqVectorRegistration<'a>, index: u32) -> Self {
+        Self { dev, reg, index }
     }
 
     /// Returns the raw vector index.
@@ -98,75 +98,72 @@ fn index(&self) -> u32 {
     }
 }
 
-impl<'a> TryInto<IrqRequest<'a>> for IrqVector<'a> {
+impl IrqRequestAnchor for &IrqVectorRegistration<'_> {}
+
+impl<'a> IrqRequest<'a, &'a IrqVectorRegistration<'a>> {
+    /// Returns the [`IrqVectorRegistration`] this request was derived from.
+    pub fn vectors(&self) -> &'a IrqVectorRegistration<'a> {
+        self.anchor()
+    }
+}
+
+impl<'a> TryInto<IrqRequest<'a, &'a IrqVectorRegistration<'a>>> for IrqVector<'a> {
     type Error = Error;
 
-    fn try_into(self) -> Result<IrqRequest<'a>> {
-        // SAFETY: `self.as_raw` returns a valid pointer to a `struct pci_dev`.

rust/kernel/pci/irq.rs fn index(&self) -> u32 { · line 120

Quoted 10 lines
rust/kernel/pci/irq.rs +4 -2
+    fn try_into(self) -> Result<IrqRequest<'a, &'a IrqVectorRegistration<'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`.
-        Ok(unsafe { IrqRequest::new(self.dev.as_ref(), irq as u32) })
+        // SAFETY: `irq` is guaranteed to be a valid IRQ number for `self.dev`.
+        Ok(unsafe { IrqRequest::new_anchored(self.dev.as_ref(), irq as u32, self.reg) })

Why is this anchoring thing needed instead apart from the lifetime that already exists on IrqRequest?

Best,
Gary

Quoted 21 lines
rust/kernel/pci/irq.rs +7 -6
     }
 }
 
-/// Represents an IRQ vector allocation for a PCI device.
+/// An allocation of PCI interrupt vectors for a device.
 ///
-/// This type ensures that IRQ vectors are properly allocated and freed by
-/// tying the allocation to the lifetime of this registration object.
+/// This type owns the vector allocation; dropping it frees the vectors. IRQ handlers borrow from
+/// this registration and must be dropped before it is.
 ///
 /// # Invariants
 ///
-/// The [`Device`] has successfully allocated IRQ vectors.
-struct IrqVectorRegistration {
-    dev: ARef<Device>,
+/// `dev` has an allocation of `count` interrupt vectors.
+pub struct IrqVectorRegistration<'a> {
+    dev: &'a Device<Bound>,
+    count: NonZero<usize>,
 }
author maintainer
#4 lore raw

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

Quoted 2 lines · Gary Guo

Why is this anchoring thing needed instead apart from the lifetime that already exists on IrqRequest?

The idea was to preserve the source of the IRQ via this anchor type. But at a second glance this morning I wasn't overly happy with that; ultimately I think it doesn't really add a lot of value.

I took a different approach now with keeping IrqVector as a new type over IrqRequest; I will send out a v2 soon.

Thanks,
Danilo

Acked-by on v1 4/5
#7 lore raw

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

In reply to Danilo Krummrich
Quoted 7 lines · 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].

Quoted 7 lines

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

In the subject line, to match drivers/pci/ history,

PCI: Add pci_irq_type() ...

Quoted 50 lines
---
 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