pci: fix resource leak when secondary process mapping fails
authorTetsuya Mukawa <mukawa@igel.co.jp>
Thu, 16 Jun 2016 02:33:11 +0000 (11:33 +0900)
committerThomas Monjalon <thomas.monjalon@6wind.com>
Mon, 20 Jun 2016 08:44:43 +0000 (10:44 +0200)
This patch fixes resource leak of pci_uio_map_secondary().
If pci_map_resource() succeeds but mapped address is different from an
address primary process mapped, this should be error.
Then the addresses secondary process mapped should be freed.

Signed-off-by: Tetsuya Mukawa <mukawa@igel.co.jp>
Acked-by: David Marchand <david.marchand@6wind.com>
lib/librte_eal/common/eal_common_pci_uio.c

index f062e81..70c6155 100644 (file)
@@ -53,7 +53,7 @@ EAL_REGISTER_TAILQ(rte_uio_tailq)
 static int
 pci_uio_map_secondary(struct rte_pci_device *dev)
 {
-       int fd, i;
+       int fd, i, j;
        struct mapped_pci_resource *uio_res;
        struct mapped_pci_res_list *uio_res_list =
                        RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
@@ -85,6 +85,16 @@ pci_uio_map_secondary(struct rte_pci_device *dev)
                                        "Cannot mmap device resource file %s to address: %p\n",
                                        uio_res->maps[i].path,
                                        uio_res->maps[i].addr);
+                               if (mapaddr != MAP_FAILED) {
+                                       /* unmap addrs correctly mapped */
+                                       for (j = 0; j < i; j++)
+                                               pci_unmap_resource(
+                                                       uio_res->maps[j].addr,
+                                                       (size_t)uio_res->maps[j].size);
+                                       /* unmap addr wrongly mapped */
+                                       pci_unmap_resource(mapaddr,
+                                               (size_t)uio_res->maps[i].size);
+                               }
                                return -1;
                        }
                }