Currently, when mapping device BARs in the primary process fails, there
are two things we do not release:

- when allocating the list of sparse areas for a BAR fails, we leak region
  info for that BAR (other branches do `free(reg)`)
- when mapping a BAR fails, we release the lists of sparse areas for all
  BARs that were mapped so far, but not for the BAR that failed, because
  cleanup stops just short of it.

Fix it by releasing region info when allocation fails, and by releasing
the list of sparse areas of the failed BAR as part of cleanup.

Fixes: f60abf9757f2 ("bus/pci: add VFIO sparse mmap")
Cc: [email protected]
Cc: [email protected]

Signed-off-by: Anatoly Burakov <[email protected]>
---
 drivers/bus/pci/linux/pci_vfio.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c
index 986b0322588..1c88d403d25 100644
--- a/drivers/bus/pci/linux/pci_vfio.c
+++ b/drivers/bus/pci/linux/pci_vfio.c
@@ -863,6 +863,7 @@ pci_vfio_map_resource_primary(struct rte_pci_device *dev)
                                        sizeof(*maps[i].areas) * 
maps[i].nr_areas, 0);
                                if (maps[i].areas == NULL) {
                                        PCI_LOG(ERR, "Cannot alloc memory for 
sparse map areas");
+                                       free(reg);
                                        goto err_map;
                                }
                                memcpy(maps[i].areas, sparse->areas,
@@ -913,6 +914,9 @@ pci_vfio_map_resource_primary(struct rte_pci_device *dev)
                if (maps[j].nr_areas > 0)
                        rte_free(maps[j].areas);
        }
+       /* the loop above stops short of the BAR that failed to map */
+       if (i < vfio_res->nb_maps)
+               rte_free(maps[i].areas);
 err_vfio_res:
        rte_free(vfio_res);
 err_vfio_dev_fd:
-- 
2.52.0

Reply via email to