]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
PCI: endpoint: pci-ep-msi: Fix error unwind and prevent double alloc
authorKoichiro Den <den@valinux.co.jp>
Tue, 17 Feb 2026 06:38:56 +0000 (15:38 +0900)
committerManivannan Sadhasivam <mani@kernel.org>
Tue, 24 Feb 2026 10:24:04 +0000 (15:54 +0530)
pci_epf_alloc_doorbell() stores the allocated doorbell message array in
epf->db_msg/epf->num_db before requesting MSI vectors. If MSI allocation
fails, the array is freed but the EPF state may still point to freed
memory.

Clear epf->db_msg and epf->num_db on the MSI allocation failure path so
that later cleanup cannot double-free the array and callers can retry
allocation.

Also return -EBUSY when doorbells have already been allocated to prevent
leaking or overwriting an existing allocation.

Fixes: 1c3b002c6bf6 ("PCI: endpoint: Add RC-to-EP doorbell support using platform MSI controller")
Signed-off-by: Koichiro Den <den@valinux.co.jp>
Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Reviewed-by: Niklas Cassel <cassel@kernel.org>
Link: https://patch.msgid.link/20260217063856.3759713-4-den@valinux.co.jp
drivers/pci/endpoint/pci-ep-msi.c

index 51c19942a81efabdf9edbc7af09f97f74b1edbc1..1395919571f83f0ad24648ff43897b932a5c589a 100644 (file)
@@ -50,6 +50,9 @@ int pci_epf_alloc_doorbell(struct pci_epf *epf, u16 num_db)
                return -EINVAL;
        }
 
+       if (epf->db_msg)
+               return -EBUSY;
+
        domain = of_msi_map_get_device_domain(epc->dev.parent, 0,
                                              DOMAIN_BUS_PLATFORM_MSI);
        if (!domain) {
@@ -79,6 +82,8 @@ int pci_epf_alloc_doorbell(struct pci_epf *epf, u16 num_db)
        if (ret) {
                dev_err(dev, "Failed to allocate MSI\n");
                kfree(msg);
+               epf->db_msg = NULL;
+               epf->num_db = 0;
                return ret;
        }