libvhost-user: Fix resource leak
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() onvu_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
@@ -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
Hi
Quoted 33 lines · Yongji Xie
Free the mmaped memory when we need to mmap new memory space on
vu_set_mem_table_exec()andvu_set_log_base_exec()to avoid memory leak.Also close the corresponding fd after
mmap()onvu_set_log_base_exec()to avoid fd leak.Signed-off-by Yongji Xie <[email protected]>--- contrib/libvhost-user/libvhost-user.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)contrib/libvhost-user/libvhost-user.c +9
-0@@ -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
-1if (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
Quoted 11 lines · Yongji Xie
Free the mmaped memory when we need to mmap new memory space on
vu_set_mem_table_exec()andvu_set_log_base_exec()to avoid memory leak.Also close the corresponding fd after
mmap()onvu_set_log_base_exec()to avoid fd leak.Signed-off-by Yongji Xie <[email protected]>--- contrib/libvhost-user/libvhost-user.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Thanks,
Maxime