vhost: fix leak of fds and mmaps
PATCH 1/1 vhost: fix leak of fds and mmaps
The common vhost code only supported a single mmap per device. vhost-user worked around this by saving the address/length/fd of each mmap after the end of the rte_virtio_memory struct. This only works if the vhost-user code frees dev->mem, since the common code is unaware of the extra info. The VHOST_USER_RESET_OWNER message is one situation where the common code frees dev->mem and leaks the fds and mappings. This happens every time I shut down a VM.
The new code does not keep the fds around since they aren't required for munmap. It saves the address/length in a new structure which is read by the common code.
The vhost-cuse changes are only compile tested.
--- lib/librte_vhost/rte_virtio_net.h | 14 +++-- lib/librte_vhost/vhost_cuse/virtio-net-cdev.c | 24 ++++--- lib/librte_vhost/vhost_user/virtio-net-user.c | 90 ++++++++------------------- lib/librte_vhost/virtio-net.c | 24 ++++++- lib/librte_vhost/virtio-net.h | 3 + 5 files changed, 75 insertions(+), 80 deletions(-)
lib/librte_vhost/rte_virtio_net.h +10 -4
@@ -144,16 +144,22 @@ struct virtio_memory_regions {uint64_t address_offset;/**< Offset of region for address translation. */ }; +/** +* Record a memory mapping so that it can be munmap'd later. +*/ +struct virtio_memory_mapping { +void *addr; +size_t length; +};/** * Memory structure includes region and mapping information. */ struct virtio_memory { -uint64_t base_address;/**< Base QEMU userspace address of the memory file. */ -uint64_t mapped_address;/**< Mapped address of memory file base in our applications memory space. */ -uint64_t mapped_size;/**< Total size of memory file. */ uint32_t nregions;/**< Number of memory regions. */ -struct virtio_memory_regions regions[0 ];/**< Memory region information. */ +uint32_t nmappings;/**< Number of memory mappings */ +struct virtio_memory_regions regions[VHOST_MEMORY_MAX_NREGIONS ];/**< Memory region information. */ +struct virtio_memory_mapping mappings[VHOST_MEMORY_MAX_NREGIONS ];/**< Memory mappings */ };/**
lib/librte_vhost/vhost_cuse/virtio-net-cdev.c +15 -9
@@ -278,15 +278,20 @@ cuse_set_mem_table(struct vhost_device_ctx ctx,if (dev ==NULL )return -1 ; -if (dev->mem && dev->mem->mapped_address) { -munmap ((void *)(uintptr_t )dev->mem->mapped_address, - (size_t )dev->mem->mapped_size); -free (dev->mem); +if (nregions >VHOST_MEMORY_MAX_NREGIONS ) { +RTE_LOG (ERR ,VHOST_CONFIG , +"(%" PRIu64") Too many memory regions (%u, max %u)\n" , + dev->device_fh, nregions, +VHOST_MEMORY_MAX_NREGIONS ); +return -1 ; + } + +if (dev->mem) { +rte_vhost_free_mem (dev->mem); dev->mem =NULL ; } - dev->mem =calloc (1 ,sizeof (struct virtio_memory ) + -sizeof (struct virtio_memory_regions ) * nregions); + dev->mem =calloc (1 ,sizeof (*dev->mem));if (dev->mem ==NULL ) { RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to allocate memory for dev->mem\n",
@@ -325,9 +330,10 @@ cuse_set_mem_table(struct vhost_device_ctx ctx, dev->mem =NULL ;return -1 ; } - dev->mem->mapped_address = mapped_address; - dev->mem->base_address = base_address; - dev->mem->mapped_size = mapped_size; + +rte_vhost_add_mapping (dev->mem, + (void *)(uintptr_t )mapped_address, + mapped_size); } }
lib/librte_vhost/vhost_user/virtio-net-user.c +26 -64
167 lines not on this page · raw message
lib/librte_vhost/virtio-net.c +21 -3
@@ -201,9 +201,7 @@ cleanup_device(struct virtio_net *dev, int destroy)/* Unmap QEMU memory file if mapped. */ if (dev->mem) { -munmap ((void *)(uintptr_t )dev->mem->mapped_address, - (size_t )dev->mem->mapped_size); -free (dev->mem); +rte_vhost_free_mem (dev->mem); dev->mem =NULL ; }
@@ -897,3 +895,23 @@ rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const opreturn 0 ; } + +void +rte_vhost_free_mem (struct virtio_memory *mem) +{ +unsigned i; + +for (i =0 ; i < mem->nmappings; i++) +munmap (mem->mappings[i].addr, mem->mappings[i].length); + +free (mem); +} + +void +rte_vhost_add_mapping (struct virtio_memory *mem,void *addr,size_t length) +{ +struct virtio_memory_mapping *m = &mem->mappings[mem->nmappings++]; + + m->addr = addr; + m->length = length; +}
lib/librte_vhost/virtio-net.h +3 -0
@@ -40,4 +40,7 @@struct virtio_net_device_ops const *notify_ops;struct virtio_net *get_device (struct vhost_device_ctx ctx); +void rte_vhost_free_mem (struct virtio_memory *mem); +void rte_vhost_add_mapping (struct virtio_memory *mem,void *addr,size_t length); +#endif
Signature · 1 line
1.9.1
Quoted 7 lines · Rich Lane
The common vhost code only supported a single mmap per device. vhost-user worked around this by saving the address/length/fd of each mmap after the end of the
rte_virtio_memorystruct. This only works if the vhost-user code freesdev->mem, since the common code is unaware of the extra info. TheVHOST_USER_RESET_OWNERmessage is one situation where the common code freesdev->memand leaks the fds and mappings. This happens every time I shut down a VM.
That is a good catch! But I don't think it needs the complexity your patch has to fix it. Besides that, your patch has ABI change, which is not acceptable at this stage.
Back to the issue, the thing is that, mmap/unmap is vhost-user/vhost-cuse specific, thus we'd better to handle them inside vhost-user/vhost-cuse differently, but not in the common path, virtio-net.c: let the common path handle common things only. That would make the logic clear, and hence less error prone.
Note that we have already handled the mmap inside vhost-user/vhost-cuse, thinking of that way, there is no reason to handle unmap at virtio-net.c:
it should be a historic issue while we added vhost-cuse first, which will not be an issue until we added vhost-user.
Another note is that you should not name an internal function with "rte_" prefix; it should be reserved for public functions.
--yliu
Quoted 18 lines · Yuanhan Liu
Quoted 7 lines · Rich Lane
The common vhost code only supported a single mmap per device. vhost-user worked around this by saving the address/length/fd of each mmap after the end of the
rte_virtio_memorystruct. This only works if the vhost-user code freesdev->mem, since the common code is unaware of the extra info. TheVHOST_USER_RESET_OWNERmessage is one situation where the common code freesdev->memand leaks the fds and mappings. This happens every time I shut down a VM.That is a good catch! But I don't think it needs the complexity your patch has to fix it. Besides that, your patch has ABI change, which is not acceptable at this stage.
Back to the issue, the thing is that, mmap/unmap is vhost-user/vhost-cuse specific, thus we'd better to handle them inside vhost-user/vhost-cuse differently, but not in the common path,
virtio-net.c: let the common path handle common things only. That would make the logic clear, and hence less error prone.
Quoted 4 lines
Note that we have already handled the mmap inside vhost-user/vhost-cuse, thinking of that way, there is no reason to handle unmap at
virtio-net.c:
it should be a historic issue while we added vhost-cuse first, which will not be an issue until we added vhost-user.
Agree. Let vhost-user part handle its specific cleanup and let the virtio-net.c handle the common logic.
Quoted 6 lines
Another note is that you should not name an internal function with "
rte_" prefix; it should be reserved for public functions.--yliu
PATCH v2 1/1 vhost: fix leak of fds and mmaps
In reply to Rich LaneThe common vhost code only supported a single mmap per device. vhost-user worked around this by saving the address/length/fd of each mmap after the end of the rte_virtio_memory struct. This only works if the vhost-user code frees dev->mem, since the common code is unaware of the extra info. The VHOST_USER_RESET_OWNER message is one situation where the common code frees dev->mem and leaks the fds and mappings. This happens every time I shut down a VM.
The new code calls back into the implementation (vhost-user or vhost-cuse) to clean up these resources.
The vhost-cuse changes are only compile tested.
---v1->v2:
- Call into vhost-user/vhost-cuse to free mmaps.
lib/librte_vhost/vhost-net.h | 6 ++++++ lib/librte_vhost/vhost_cuse/virtio-net-cdev.c | 12 ++++++++++++ lib/librte_vhost/vhost_user/vhost-net-user.c | 1 - lib/librte_vhost/vhost_user/virtio-net-user.c | 25 ++++++++++--------------- lib/librte_vhost/vhost_user/virtio-net-user.h | 1 - lib/librte_vhost/virtio-net.c | 8 +------- 6 files changed, 29 insertions(+), 24 deletions(-)
lib/librte_vhost/vhost-net.h +6 -0
@@ -115,4 +115,10 @@ struct vhost_net_device_ops {struct vhost_net_device_ops const *get_virtio_net_callbacks (void ); + +/* +* Implementation-specific cleanup. Defined by vhost-cuse and vhost-user. +*/ +void vhost_impl_cleanup (struct virtio_net *dev); +#endif /* _VHOST_NET_CDEV_H_ */
lib/librte_vhost/vhost_cuse/virtio-net-cdev.c +12 -0
@@ -421,3 +421,15 @@ int cuse_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)return ops->set_backend (ctx, file); } + +void +vhost_impl_cleanup (struct virtio_net *dev) +{ +/* Unmap QEMU memory file if mapped. */ +if (dev->mem) { +munmap ((void *)(uintptr_t )dev->mem->mapped_address, + (size_t )dev->mem->mapped_size); +free (dev->mem); + dev->mem =NULL ; + } +}
lib/librte_vhost/vhost_user/vhost-net-user.c +0 -1
@@ -347,7 +347,6 @@ vserver_message_handler(int connfd, void *dat, int *remove)close (connfd); *remove =1 ;free (cfd_ctx); -user_destroy_device (ctx); ops->destroy_device (ctx);return ;
lib/librte_vhost/vhost_user/virtio-net-user.c +10 -15
@@ -339,21 +339,6 @@ user_set_vring_enable(struct vhost_device_ctx ctx, }void -user_destroy_device (struct vhost_device_ctx ctx) -{ -struct virtio_net *dev =get_device (ctx); - -if (dev && (dev->flags &VIRTIO_DEV_RUNNING )) - notify_ops->destroy_device (dev); - -if (dev && dev->mem) { -free_mem_region (dev); -free (dev->mem); - dev->mem =NULL ; - } -} - -void user_set_protocol_features (struct vhost_device_ctx ctx,uint64_t protocol_features) {
@@ -365,3 +350,13 @@ user_set_protocol_features(struct vhost_device_ctx ctx, dev->protocol_features = protocol_features; } + +void +vhost_impl_cleanup (struct virtio_net *dev) +{ +if (dev->mem) { +free_mem_region (dev); +free (dev->mem); + dev->mem =NULL ; + } +}
lib/librte_vhost/vhost_user/virtio-net-user.h +0 -1
@@ -55,5 +55,4 @@ int user_get_vring_base(struct vhost_device_ctx, struct vhost_vring_state *);int user_set_vring_enable (struct vhost_device_ctx ctx,struct vhost_vring_state *state); -void user_destroy_device (struct vhost_device_ctx );#endif
lib/librte_vhost/virtio-net.c +1 -7
@@ -199,13 +199,7 @@ cleanup_device(struct virtio_net *dev, int destroy) {uint32_t i; -/* Unmap QEMU memory file if mapped. */ -if (dev->mem) { -munmap ((void *)(uintptr_t )dev->mem->mapped_address, - (size_t )dev->mem->mapped_size); -free (dev->mem); - dev->mem =NULL ; - } +vhost_impl_cleanup (dev);for (i =0 ; i < dev->virt_qp_nb; i++) {cleanup_vq (dev->virtqueue[i *VIRTIO_QNUM +VIRTIO_RXQ ], destroy);
Signature · 1 line
1.9.1
Quoted 31 lines · Rich Lane
The common vhost code only supported a single mmap per device. vhost-user worked around this by saving the address/length/fd of each mmap after the end of the
rte_virtio_memorystruct. This only works if the vhost-user code freesdev->mem, since the common code is unaware of the extra info. TheVHOST_USER_RESET_OWNERmessage is one situation where the common code freesdev->memand leaks the fds and mappings. This happens every time I shut down a VM.The new code calls back into the implementation (vhost-user or vhost-cuse) to clean up these resources.
The vhost-cuse changes are only compile tested.
Signed-off-by Rich Lane <[email protected]>---
v1->v2:
- Call into vhost-user/vhost-cuse to free mmaps.lib/librte_vhost/vhost-net.h | 6 ++++++ lib/librte_vhost/vhost_cuse/virtio-net-cdev.c | 12 ++++++++++++ lib/librte_vhost/vhost_user/vhost-net-user.c | 1 - lib/librte_vhost/vhost_user/virtio-net-user.c | 25 ++++++++++--------------- lib/librte_vhost/vhost_user/virtio-net-user.h | 1 - lib/librte_vhost/virtio-net.c | 8 +------- 6 files changed, 29 insertions(+), 24 deletions(-)lib/librte_vhost/vhost-net.h +0
-0
lib/librte_vhost/vhost-net.h struct vhost_net_device_ops { · line 122
Quoted 8 lines
lib/librte_vhost/vhost-net.h +5
-0@@ -115,4 +115,10 @@ struct vhost_net_device_ops {struct vhost_net_device_ops const *get_virtio_net_callbacks (void ); + +/* +* Implementation-specific cleanup. Defined by vhost-cuse and vhost-user. +*/ +void vhost_impl_cleanup (struct virtio_net *dev);
TBH, I am not quite like "_impl_"; maybe "_backend_" is better?
OTOH, what I thought of has slight difference than yours: not necessary to export a function, but instead, call the vhost backend specific unmap function inside the backend itself. Say, call vhost_user_unmap() on RESET_OWNER and connection close. What do you think of that?
--yliu
Quoted 45 lines · Yuanhan Liu
Quoted 2 lines · Rich Lane
The common vhost code only supported a single mmap per device. vhost-user worked around this by saving the address/length/fd of each mmap after
the end
Quoted 1 line
of the
rte_virtio_memorystruct. This only works if the vhost-user codefrees
Quoted 2 lines
dev->mem, since the common code is unaware of the extra info. TheVHOST_USER_RESET_OWNERmessage is one situation where the common codefrees
Quoted 1 line
dev->memand leaks the fds and mappings. This happens every time I shutdown a
Quoted 3 lines
VM.
The new code calls back into the implementation (vhost-user or
vhost-cuse) to
Quoted 5 lines
clean up these resources.
The vhost-cuse changes are only compile tested.
Signed-off-by Rich Lane <[email protected]>Quoted 8 lines
---
v1->v2:
- Call into vhost-user/vhost-cuse to free mmaps.lib/librte_vhost/vhost-net.h | 6 ++++++ lib/librte_vhost/vhost_cuse/virtio-net-cdev.c | 12 ++++++++++++ lib/librte_vhost/vhost_user/vhost-net-user.c | 1 - lib/librte_vhost/vhost_user/virtio-net-user.c | 25++++++++++---------------
Quoted 9 lines
lib/librte_vhost/vhost_user/virtio-net-user.h | 1 - lib/librte_vhost/virtio-net.c | 8 +------- 6 files changed, 29 insertions(+), 24 deletions(-)lib/librte_vhost/vhost-net.h +0
-0lib/librte_vhost/vhost-net.h struct vhost_net_device_ops { · line 120
Quoted 6 lines
lib/librte_vhost/vhost-net.h +3
-0@@ -115,4 +115,10 @@ struct vhost_net_device_ops {struct vhost_net_device_ops const *get_virtio_net_callbacks (void ); + +/* +* Implementation-specific cleanup. Defined by vhost-cuse and vhost-user.
Quoted 5 lines
lib/librte_vhost/vhost-net.h struct vhost_net_device_ops { · line 122
Quoted 2 lines
lib/librte_vhost/vhost-net.h +2
-0+*/ +void vhost_impl_cleanup (struct virtio_net *dev);TBH, I am not quite like "_impl_"; maybe "_backend_" is better?
If you have a strong preference I will change it. Let me know.
Quoted 5 lines
OTOH, what I thought of has slight difference than yours: not necessary to export a function, but instead, call the vhost backend specific unmap function inside the backend itself. Say, call
vhost_user_unmap()onRESET_OWNERand connection close. What do you think of that?
The munmap must be done after the notify_ops->destroy_device callback. That means
the backend can't call it before reset_owner() or destroy_device(). The munmap could
be done afterwards, but that requires saving dev->mem in the caller in the case ofdestroy_device. The cleanest solution is for the vhost common code to ask the
backend to clean up at the correct time.
Yuanhan Liu
Acked-byHey Rich,
Sorry for the long delay; I barely forgot it :(
Quoted 13 lines · Rich Lane
On Sun, Jan 17, 2016 at 11:58 PM, Yuanhan Liu <[email protected]> wrote:
On Sun, Jan 17, 2016 at 11:57:18AM -0800, Rich Lane wrote: > +/* > + * Implementation-specific cleanup. Defined by vhost-cuse and vhost-user. > + */ > +void vhost_impl_cleanup(struct virtio_net *dev); TBH, I am not quite like "_impl_"; maybe "_backend_" is better?
Quoted 1 line
If you have a strong preference I will change it. Let me know.
"backend" is just a more common word to me, as well as to QEMU. So, I would suggest you to do such change, and if so, you could add my ACK:
Quoted 11 lines
OTOH, what I thought of has slight difference than yours: not necessary to export a function, but instead, call the vhost backend specific unmap function inside the backend itself. Say, call
vhost_user_unmap()onRESET_OWNERand connection close. What do you think of that?The munmap must be done after the
notify_ops->destroy_devicecallback. That means
the backend can't call it beforereset_owner()ordestroy_device().
Well, you could:
case VHOST_USER_RESET_OWNER: ops->reset_owner(); vhost_user_unmap(); break;
Anyway, it's not a big deal. Let's go with your solution first.
--yliu
Quoted 6 lines
The munmap
could
be done afterwards, but that requires savingdev->memin the caller in the case ofdestroy_device. The cleanest solution is for the vhost common code to ask the backend to clean up at the correct time.
PATCH v3 1/1 vhost: fix leak of fds and mmaps
In reply to Rich LaneThe common vhost code only supported a single mmap per device. vhost-user worked around this by saving the address/length/fd of each mmap after the end of the rte_virtio_memory struct. This only works if the vhost-user code frees dev->mem, since the common code is unaware of the extra info. The VHOST_USER_RESET_OWNER message is one situation where the common code frees dev->mem and leaks the fds and mappings. This happens every time I shut down a VM.
The new code calls back into the implementation (vhost-user or vhost-cuse) to clean up these resources.
The vhost-cuse changes are only compile tested.
---v2->v3:
- Rename "impl" to "backend".
v1->v2:
- Call into vhost-user/vhost-cuse to free mmaps.
lib/librte_vhost/vhost-net.h | 6 ++++++ lib/librte_vhost/vhost_cuse/virtio-net-cdev.c | 12 ++++++++++++ lib/librte_vhost/vhost_user/vhost-net-user.c | 1 - lib/librte_vhost/vhost_user/virtio-net-user.c | 25 ++++++++++--------------- lib/librte_vhost/vhost_user/virtio-net-user.h | 1 - lib/librte_vhost/virtio-net.c | 8 +------- 6 files changed, 29 insertions(+), 24 deletions(-)
lib/librte_vhost/vhost-net.h +6 -0
@@ -115,4 +115,10 @@ struct vhost_net_device_ops {struct vhost_net_device_ops const *get_virtio_net_callbacks (void ); + +/* +* Backend-specific cleanup. Defined by vhost-cuse and vhost-user. +*/ +void vhost_backend_cleanup (struct virtio_net *dev); +#endif /* _VHOST_NET_CDEV_H_ */
lib/librte_vhost/vhost_cuse/virtio-net-cdev.c +12 -0
@@ -421,3 +421,15 @@ int cuse_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)return ops->set_backend (ctx, file); } + +void +vhost_backend_cleanup (struct virtio_net *dev) +{ +/* Unmap QEMU memory file if mapped. */ +if (dev->mem) { +munmap ((void *)(uintptr_t )dev->mem->mapped_address, + (size_t )dev->mem->mapped_size); +free (dev->mem); + dev->mem =NULL ; + } +}
lib/librte_vhost/vhost_user/vhost-net-user.c +0 -1
@@ -347,7 +347,6 @@ vserver_message_handler(int connfd, void *dat, int *remove)close (connfd); *remove =1 ;free (cfd_ctx); -user_destroy_device (ctx); ops->destroy_device (ctx);return ;
lib/librte_vhost/vhost_user/virtio-net-user.c +10 -15
@@ -339,21 +339,6 @@ user_set_vring_enable(struct vhost_device_ctx ctx, }void -user_destroy_device (struct vhost_device_ctx ctx) -{ -struct virtio_net *dev =get_device (ctx); - -if (dev && (dev->flags &VIRTIO_DEV_RUNNING )) - notify_ops->destroy_device (dev); - -if (dev && dev->mem) { -free_mem_region (dev); -free (dev->mem); - dev->mem =NULL ; - } -} - -void user_set_protocol_features (struct vhost_device_ctx ctx,uint64_t protocol_features) {
@@ -365,3 +350,13 @@ user_set_protocol_features(struct vhost_device_ctx ctx, dev->protocol_features = protocol_features; } + +void +vhost_backend_cleanup (struct virtio_net *dev) +{ +if (dev->mem) { +free_mem_region (dev); +free (dev->mem); + dev->mem =NULL ; + } +}
lib/librte_vhost/vhost_user/virtio-net-user.h +0 -1
@@ -55,5 +55,4 @@ int user_get_vring_base(struct vhost_device_ctx, struct vhost_vring_state *);int user_set_vring_enable (struct vhost_device_ctx ctx,struct vhost_vring_state *state); -void user_destroy_device (struct vhost_device_ctx );#endif
lib/librte_vhost/virtio-net.c +1 -7
@@ -199,13 +199,7 @@ cleanup_device(struct virtio_net *dev, int destroy) {uint32_t i; -/* Unmap QEMU memory file if mapped. */ -if (dev->mem) { -munmap ((void *)(uintptr_t )dev->mem->mapped_address, - (size_t )dev->mem->mapped_size); -free (dev->mem); - dev->mem =NULL ; - } +vhost_backend_cleanup (dev);for (i =0 ; i < dev->virt_qp_nb; i++) {cleanup_vq (dev->virtqueue[i *VIRTIO_QNUM +VIRTIO_RXQ ], destroy);
Signature · 1 line
1.9.1
Thomas Monjalon
Applied2016-02-10 10:40, Rich Lane:
Quoted 8 lines
The common vhost code only supported a single mmap per device. vhost-user worked around this by saving the address/length/fd of each mmap after the end of the
rte_virtio_memorystruct. This only works if the vhost-user code freesdev->mem, since the common code is unaware of the extra info. TheVHOST_USER_RESET_OWNERmessage is one situation where the common code freesdev->memand leaks the fds and mappings. This happens every time I shut down a VM.
Quoted 7 lines
The new code calls back into the implementation (vhost-user or vhost-cuse) to clean up these resources.
The vhost-cuse changes are only compile tested.
Signed-off-by Rich Lane <[email protected]>Acked-by Yuanhan Liu <[email protected]>
Applied, thanks