vhost: fix realloc failure
authorTomasz Kulasek <tomaszx.kulasek@intel.com>
Fri, 9 Feb 2018 17:19:00 +0000 (18:19 +0100)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 30 Mar 2018 12:08:42 +0000 (14:08 +0200)
When reallocation of guest pages fails, vhost_user_set_mem_table() also
should fail.

Fixes: e246896178e6 ("vhost: get guest/host physical address mappings")
Cc: stable@dpdk.org
Signed-off-by: Ziye Yang <ziye.yang@intel.com>
Signed-off-by: Tomasz Kulasek <tomaszx.kulasek@intel.com>
Reviewed-by: Jianfeng Tan <jianfeng.tan@intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
lib/librte_vhost/vhost_user.c

index b4bd0f4..d686b5a 100644 (file)
@@ -553,7 +553,7 @@ vhost_user_set_vring_base(struct virtio_net *dev,
        return 0;
 }
 
-static void
+static int
 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
                   uint64_t host_phys_addr, uint64_t size)
 {
@@ -563,6 +563,10 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
                dev->max_guest_pages *= 2;
                dev->guest_pages = realloc(dev->guest_pages,
                                        dev->max_guest_pages * sizeof(*page));
+               if (!dev->guest_pages) {
+                       RTE_LOG(ERR, VHOST_CONFIG, "cannot realloc guest_pages\n");
+                       return -1;
+               }
        }
 
        if (dev->nr_guest_pages > 0) {
@@ -571,7 +575,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
                if (host_phys_addr == last_page->host_phys_addr +
                                      last_page->size) {
                        last_page->size += size;
-                       return;
+                       return 0;
                }
        }
 
@@ -579,9 +583,11 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
        page->guest_phys_addr = guest_phys_addr;
        page->host_phys_addr  = host_phys_addr;
        page->size = size;
+
+       return 0;
 }
 
-static void
+static int
 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
                uint64_t page_size)
 {
@@ -595,7 +601,9 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
        size = page_size - (guest_phys_addr & (page_size - 1));
        size = RTE_MIN(size, reg_size);
 
-       add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
+       if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
+               return -1;
+
        host_user_addr  += size;
        guest_phys_addr += size;
        reg_size -= size;
@@ -604,12 +612,16 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
                size = RTE_MIN(reg_size, page_size);
                host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
                                                  host_user_addr);
-               add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
+               if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
+                               size) < 0)
+                       return -1;
 
                host_user_addr  += size;
                guest_phys_addr += size;
                reg_size -= size;
        }
+
+       return 0;
 }
 
 #ifdef RTE_LIBRTE_VHOST_DEBUG
@@ -773,7 +785,12 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                                      mmap_offset;
 
                if (dev->dequeue_zero_copy)
-                       add_guest_pages(dev, reg, alignment);
+                       if (add_guest_pages(dev, reg, alignment) < 0) {
+                               RTE_LOG(ERR, VHOST_CONFIG,
+                                       "adding guest pages to region %u failed.\n",
+                                       i);
+                               goto err_mmap;
+                       }
 
                RTE_LOG(INFO, VHOST_CONFIG,
                        "guest memory region %u, size: 0x%" PRIx64 "\n"