Description: backport of dpdk 16.04-rc1 fix for LP: #1559981 Forwarded: n/a (already upstream) Author: Christian Ehrhardt Last-Update: 2016-03-20 From ca67ed289a76f38d6c4a4021985a36eaf1d77e28 Mon Sep 17 00:00:00 2001 From: Rich Lane Date: Wed, 10 Feb 2016 10:40:55 -0800 Subject: [PATCH] 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 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 Acked-by: Yuanhan Liu --- 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(-) Index: dpdk/lib/librte_vhost/vhost-net.h =================================================================== --- dpdk.orig/lib/librte_vhost/vhost-net.h +++ dpdk/lib/librte_vhost/vhost-net.h @@ -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_ */ Index: dpdk/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c =================================================================== --- dpdk.orig/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c +++ dpdk/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c @@ -421,3 +421,15 @@ int cuse_set_backend(struct vhost_device 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; + } +} Index: dpdk/lib/librte_vhost/vhost_user/vhost-net-user.c =================================================================== --- dpdk.orig/lib/librte_vhost/vhost_user/vhost-net-user.c +++ dpdk/lib/librte_vhost/vhost_user/vhost-net-user.c @@ -347,7 +347,6 @@ vserver_message_handler(int connfd, void close(connfd); *remove = 1; free(cfd_ctx); - user_destroy_device(ctx); ops->destroy_device(ctx); return; Index: dpdk/lib/librte_vhost/vhost_user/virtio-net-user.c =================================================================== --- dpdk.orig/lib/librte_vhost/vhost_user/virtio-net-user.c +++ dpdk/lib/librte_vhost/vhost_user/virtio-net-user.c @@ -88,6 +88,16 @@ free_mem_region(struct virtio_net *dev) } } +void +vhost_backend_cleanup(struct virtio_net *dev) +{ + if (dev->mem) { + free_mem_region(dev); + free(dev->mem); + dev->mem = NULL; + } +} + int user_set_mem_table(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg) { @@ -339,21 +349,6 @@ user_set_vring_enable(struct vhost_devic } 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) { Index: dpdk/lib/librte_vhost/vhost_user/virtio-net-user.h =================================================================== --- dpdk.orig/lib/librte_vhost/vhost_user/virtio-net-user.h +++ dpdk/lib/librte_vhost/vhost_user/virtio-net-user.h @@ -55,5 +55,4 @@ int user_get_vring_base(struct vhost_dev int user_set_vring_enable(struct vhost_device_ctx ctx, struct vhost_vring_state *state); -void user_destroy_device(struct vhost_device_ctx); #endif Index: dpdk/lib/librte_vhost/virtio-net.c =================================================================== --- dpdk.orig/lib/librte_vhost/virtio-net.c +++ dpdk/lib/librte_vhost/virtio-net.c @@ -199,13 +199,7 @@ cleanup_device(struct virtio_net *dev, i { 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);