From 03e4905402ae60aa1d7a65770076bb2c1df8f6ac Mon Sep 17 00:00:00 2001 From: Shawn Lin Date: Wed, 11 Feb 2026 16:24:57 +0800 Subject: [PATCH 1/2] PCI/MSI: Clarify pci_free_irq_vectors() usage for managed devices Update pci_free_irq_vectors() 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. Suggested-by: Philipp Stanner Signed-off-by: Shawn Lin [bhelgaas: squash both updates to pci_free_irq_vectors() documentation] Signed-off-by: Bjorn Helgaas Link: https://patch.msgid.link/1770798299-202288-2-git-send-email-shawn.lin@rock-chips.com Link: https://patch.msgid.link/1770798299-202288-3-git-send-email-shawn.lin@rock-chips.com --- Documentation/PCI/msi-howto.rst | 7 +++++-- drivers/pci/msi/api.c | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Documentation/PCI/msi-howto.rst b/Documentation/PCI/msi-howto.rst index 667ebe2156b4..844c1d3c395d 100644 --- a/Documentation/PCI/msi-howto.rst +++ b/Documentation/PCI/msi-howto.rst @@ -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); diff --git a/drivers/pci/msi/api.c b/drivers/pci/msi/api.c index 818d55fbad0d..c18559b6272c 100644 --- a/drivers/pci/msi/api.c +++ b/drivers/pci/msi/api.c @@ -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) { From 874b07eb0875729b9a47441e03b125d9fa735645 Mon Sep 17 00:00:00 2001 From: Shawn Lin Date: Wed, 11 Feb 2026 16:24:59 +0800 Subject: [PATCH 2/2] PCI/MSI: Add TODO comment about legacy pcim_enable_device() side-effect 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. Suggested-by: Philipp Stanner Signed-off-by: Shawn Lin Signed-off-by: Bjorn Helgaas Link: https://patch.msgid.link/1770798299-202288-4-git-send-email-shawn.lin@rock-chips.com --- drivers/pci/msi/msi.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/pci/msi/msi.c b/drivers/pci/msi/msi.c index e2412175d7af..81d24a270a79 100644 --- a/drivers/pci/msi/msi.c +++ b/drivers/pci/msi/msi.c @@ -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) {