Rework PCI IRQ vector code
Subsystems (5)
Danilo Krummrich
maintainercover 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
Danilo Krummrich
maintainerPATCH 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
@@ -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
Danilo Krummrich
maintainerPATCH 2/5 rust: pci: convert IrqVectorRegistration to a lifetime-managed owning type
In reply to Danilo KrummrichConvert 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
@@ -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
Gary Guo
reviewerRe: [PATCH 2/5] rust: pci: convert IrqVectorRegistration to a lifetime-managed owning type
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.Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/pci.rs | 3 +- rust/kernel/pci/irq.rs | 149 ++++++++++++++++++++++------------------- 2 files changed, 83 insertions(+), 69 deletions(-)rust/kernel/pci.rs +2
-1@@ -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@@ -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: &'aDevice <Bound >, + reg: &'aIrqVectorRegistration <'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: &'aDevice <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: &'aDevice <Bound >, reg: &'aIrqVectorRegistration <'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, &'aIrqVectorRegistration <'a>> { +/// Returns the [`IrqVectorRegistration`] this request was derived from. +pub fn vectors (&self) -> &'aIrqVectorRegistration <'a> { + self.anchor () + } +} + +impl <'a>TryInto <IrqRequest <'a, &'aIrqVectorRegistration <'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, &'aIrqVectorRegistration <'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 (), irqas u32 ) }) +// SAFETY: `irq` is guaranteed to be a valid IRQ number for `self.dev`. +Ok (unsafe {IrqRequest ::new_anchored (self.dev.as_ref (), irqas 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: &'aDevice <Bound >, + count:NonZero <usize >, }
Danilo Krummrich
maintainerRe: [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
Danilo Krummrich
maintainerPATCH 3/5 rust: pci: remove IrqVector and resolve IrqRequest directly
In reply to Danilo KrummrichRemove 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
@@ -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
Danilo Krummrich
maintainerPATCH 4/5 PCI: add pci_irq_type() to query the allocated interrupt type
In reply to Danilo KrummrichAdd 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
@@ -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
Re: [PATCH 4/5] PCI: add pci_irq_type() to query the allocated interrupt type
Quoted 7 lines · Danilo Krummrich
Add a helper that returns
PCI_IRQ_MSIX,PCI_IRQ_MSI, orPCI_IRQ_INTXbased on the interrupt type the PCI core selected afterpci_alloc_irq_vectors().Several drivers already open-code this check against
pdev->msix_enabledandpdev->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@@ -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
Danilo Krummrich
maintainerPATCH 5/5 rust: pci: expose the allocated interrupt type
In reply to Danilo KrummrichAdd 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
@@ -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
@@ -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