Skip to contentSkip to conversation

Clarify pci_free_irq_vectors() usage constraints

PATCH In review Shawn Lin 2 participants 5 messages Feb 11 – Feb 12

Subsystems (2)
lore
Share & tools
lore

Shawn Lin

author
#0 lore raw

v2 cover Clarify pci_free_irq_vectors() usage constraints

pcim_enable_device() automatically manages IRQ vectors, which can cause double-free issues if drivers manually call pci_free_irq_vectors().

This series adds documentation and warnings to prevent this:
1. msi-howto.rst guidance
2. kernel-doc warning
3. TODO comment for future cleanup

Changes in v2:
- rework the commit message and documentation

Shawn Lin (3):
Documentation: PCI: Clarify pci_free_irq_vectors() usage for managed
devices
PCI/MSI: Add warning to pci_free_irq_vectors() documentation PCI/MSI: Add TODO comment about legacy pcim_enable_device()
side-effect

 Documentation/PCI/msi-howto.rst |  7 +++++--
 drivers/pci/msi/api.c           |  5 +++++
 drivers/pci/msi/msi.c           | 10 ++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v2 1/3 Documentation: PCI: Clarify pci_free_irq_vectors() usage for managed devices

Update the msi-howto.rst documentation to clarify that drivers using pcim_enable_device() must not call pci_free_irq_vectors().

For legacy reasons, pcim_enable_device() switches several normally un-managed functions into managed mode. Currently, the only function affected in this way is pcim_setup_msi_release(), which results in automatic IRQ vector management.

This behavior is dangerous and confusing. Drivers using pcim_enable_device() should rely on the automatic IRQ vector management and avoid calling pci_free_irq_vectors() manually.

---

Changes in v2:
- rework the commit message and documentation

 Documentation/PCI/msi-howto.rst | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Documentation/PCI/msi-howto.rst +5 -2
index 667ebe2..844c1d3 100644
@@ -113,8 +113,11 @@ vectors, use the following function::
 
   int pci_irq_vector(struct pci_dev *dev, unsigned int nr);
 
-Any allocated resources should be freed before removing the device using
-the following function::
+If the driver enables the device using pcim_enable_device(), the driver
+shouldn't call pci_free_irq_vectors() because pcim_enable_device()
+activates automatic management for IRQ vectors. Otherwise, the driver should
+free any allocated IRQ vectors before removing the device using the following
+function::
 
   void pci_free_irq_vectors(struct pci_dev *dev);
 
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH v2 2/3 PCI/MSI: Add warning to pci_free_irq_vectors() documentation

In reply to Shawn Lin

Update the kernel-doc comment for pci_free_irq_vectors() to warn drivers that this function should not be called when the device has been enabled with pcim_enable_device().

For legacy reasons, pcim_enable_device() activates automatic management of IRQ vectors via pcim_msi_release(). Calling pci_free_irq_vectors() on such devices can cause double-free issues.

This warning complements the documentation in msi-howto.rst and helps developers who primarily read function API documentation when working with the PCI/MSI subsystem.

---

Changes in v2: None

 drivers/pci/msi/api.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/pci/msi/api.c +5 -0
index 818d55f..c18559b 100644
@@ -370,6 +370,11 @@ EXPORT_SYMBOL(pci_irq_get_affinity);
  * Undo the interrupt vector allocations and possible device MSI/MSI-X
  * enablement earlier done through pci_alloc_irq_vectors_affinity() or
  * pci_alloc_irq_vectors().
+ *
+ * WARNING: Do not call this function if the device has been enabled
+ * with pcim_enable_device(). In that case, IRQ vectors are automatically
+ * managed via pcim_msi_release() and calling pci_free_irq_vectors() can
+ * lead to double-free issues.
  */
 void pci_free_irq_vectors(struct pci_dev *dev)
 {
Signature · 1 line
2.7.4
author
#3 lore raw

PATCH v2 3/3 PCI/MSI: Add TODO comment about legacy pcim_enable_device() side-effect

In reply to Shawn Lin

Add a TODO comment in pci/msi/msi.c to document that the automatic IRQ vector management activated by pcim_enable_device() is a dangerous and confusing.

The comment is placed near pcim_setup_msi_release() where the automatic management is implemented, serving as a marker for future cleanup work.

---

Changes in v2: None

 drivers/pci/msi/msi.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/pci/msi/msi.c +10 -0
index e241217..81d24a2 100644
@@ -77,6 +77,16 @@ static void pcim_msi_release(void *pcidev)
 /*
  * Needs to be separate from pcim_release to prevent an ordering problem
  * vs. msi_device_data_release() in the MSI core code.
+ *
+ * TODO: Remove the legacy side-effect of pcim_enable_device() that
+ * activates automatic IRQ vector management. This design is dangerous
+ * and confusing because it switches normally un-managed functions
+ * into managed mode. Drivers should explicitly manage their IRQ vectors
+ * without this implicit behavior.
+ *
+ * The current implementation uses both pdev->is_managed and
+ * pdev->is_msi_managed flags, which adds unnecessary complexity.
+ * This should be simplified in a future kernel version.
  */
 static int pcim_setup_msi_release(struct pci_dev *dev)
 {
Signature · 1 line
2.7.4
#4 lore raw
In reply to Shawn Lin
Quoted 19 lines · Shawn Lin

pcim_enable_device() automatically manages IRQ vectors, which can cause double-free issues if drivers manually call pci_free_irq_vectors().

This series adds documentation and warnings to prevent this:
1. msi-howto.rst guidance
2. kernel-doc warning
3. TODO comment for future cleanup

Changes in v2:
- rework the commit message and documentation

Shawn Lin (3):
Documentation: PCI: Clarify pci_free_irq_vectors() usage for managed
devices
PCI/MSI: Add warning to pci_free_irq_vectors() documentation PCI/MSI: Add TODO comment about legacy pcim_enable_device()
side-effect

Quoted 4 lines
 Documentation/PCI/msi-howto.rst |  7 +++++--
 drivers/pci/msi/api.c           |  5 +++++
 drivers/pci/msi/msi.c           | 10 ++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)

Squashed the first two patches and applied to pci/msi for v7.1, thanks!

This will be rebased to v7.0-rc1 after it is tagged.