Skip to contentSkip to conversation

libvhost-user: Fix resource leak

PATCH In review Yongji Xie 3 participants 3 messages Jan 18, 2018 – Feb 8, 2018

lore
Share & tools
lore

Yongji Xie

author
#0 lore raw

PATCH 1/1 libvhost-user: Fix resource leak

Free the mmaped memory when we need to mmap new memory space on vu_set_mem_table_exec() and vu_set_log_base_exec() to avoid memory leak.

Also close the corresponding fd after mmap() on
vu_set_log_base_exec() to avoid fd leak.

---
 contrib/libvhost-user/libvhost-user.c |   14 ++++++++++++++
 1 file changed, 14 insertions(+)
contrib/libvhost-user/libvhost-user.c +14 -0
index f409bd3..cadced7 100644
@@ -405,6 +405,15 @@ vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg)
 {
     int i;
     VhostUserMemory *memory = &vmsg->payload.memory;
+
+    for (i = 0; i < dev->nregions; i++) {
+        VuDevRegion *r = &dev->regions[i];
+        void *m = (void *) (uintptr_t) r->mmap_addr;
+
+        if (m) {
+            munmap(m, r->size + r->mmap_offset);
+        }
+    }
     dev->nregions = memory->nregions;
 
     DPRINT("Nregions: %d\n", memory->nregions);
@@ -470,9 +479,14 @@ vu_set_log_base_exec(VuDev *dev, VhostUserMsg *vmsg)
 
     rc = mmap(0, log_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
               log_mmap_offset);
+    close(fd);
     if (rc == MAP_FAILED) {
         perror("log mmap error");
     }
+
+    if (dev->log_table) {
+        munmap(dev->log_table, dev->log_size);
+    }
     dev->log_table = rc;
     dev->log_size = log_mmap_size;
 
Signature · 1 line
1.7.9.5
Reviewed-by on v1 1/1
#1 lore raw

Hi

Quoted 33 lines · Yongji Xie

Free the mmaped memory when we need to mmap new memory space on vu_set_mem_table_exec() and vu_set_log_base_exec() to avoid memory leak.

Also close the corresponding fd after mmap() on
vu_set_log_base_exec() to avoid fd leak.

---
 contrib/libvhost-user/libvhost-user.c |   14 ++++++++++++++
 1 file changed, 14 insertions(+)
contrib/libvhost-user/libvhost-user.c +9 -0
index f409bd3..cadced7 100644
@@ -405,6 +405,15 @@ vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg)
 {
     int i;
     VhostUserMemory *memory = &vmsg->payload.memory;
+
+    for (i = 0; i < dev->nregions; i++) {
+        VuDevRegion *r = &dev->regions[i];
+        void *m = (void *) (uintptr_t) r->mmap_addr;
+
+        if (m) {
+            munmap(m, r->size + r->mmap_offset);
+        }
+    }
     dev->nregions = memory->nregions;
 
     DPRINT("Nregions: %d\n", memory->nregions);

contrib/libvhost-user/libvhost-user.c vu_set_log_base_exec(VuDev *dev, VhostUserMsg *vmsg) · line 482

Quoted 4 lines
contrib/libvhost-user/libvhost-user.c +1 -0
@@ -470,9 +479,14 @@ vu_set_log_base_exec(VuDev *dev, VhostUserMsg *vmsg)
 
     rc = mmap(0, log_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
               log_mmap_offset);
+    close(fd);

or call vmsg_close_fds() ?

Quoted 11 lines
contrib/libvhost-user/libvhost-user.c +4 -1
     if (rc == MAP_FAILED) {
         perror("log mmap error");
     }
+
+    if (dev->log_table) {
+        munmap(dev->log_table, dev->log_size);
+    }
     dev->log_table = rc;
     dev->log_size = log_mmap_size;
 
--
Quoted 3 lines

1.7.9.5

Signature · 1 line
Marc-André Lureau
Reviewed-by on v1 1/1
#2 lore raw
In reply to Yongji Xie
Quoted 11 lines · Yongji Xie

Free the mmaped memory when we need to mmap new memory space on vu_set_mem_table_exec() and vu_set_log_base_exec() to avoid memory leak.

Also close the corresponding fd after mmap() on
vu_set_log_base_exec() to avoid fd leak.

---
  contrib/libvhost-user/libvhost-user.c |   14 ++++++++++++++
  1 file changed, 14 insertions(+)

Thanks,
Maxime