From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf0-f172.google.com (mail-pf0-f172.google.com [209.85.192.172]) by dpdk.org (Postfix) with ESMTP id 4304556A9 for ; Wed, 10 Feb 2016 19:40:57 +0100 (CET) Received: by mail-pf0-f172.google.com with SMTP id c10so16302046pfc.2 for ; Wed, 10 Feb 2016 10:40:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bigswitch-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZsgtCVn3W7qr4FIAtlOJFr8DS0LIgCcMdM5AM/Wa9fo=; b=UUkk1+jIkLqFd7lbuNGHYMC5P4+Sy5kBxRURxsaHuL8PuEnXGRA4yotcEW27VUHik6 dZDkoSVbu2QIY4HwbFpsEKf5cQgu5WsaXkFmQLDfw+eXY3MBAIc0ZUSp/zYD+jqlwh3j lhboVknW+Q/cEh0SRQf5leSoMvlRTd1adIbPjA2w58xK+Zwfx+RTISIA/PJBiwW32l0b SEnVA/9xzyJ9u8HaVpWEzc4C3XOpws8TvZwanJygYfXTF9ZHAvGtvVMBC6FrMKoAdVNP PZP8mFu8KnSd7El9B37NgmIAojA7haINKO5MP6dsgkrTgNwi+Mxceh9O8hdnL6Lbu410 80mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZsgtCVn3W7qr4FIAtlOJFr8DS0LIgCcMdM5AM/Wa9fo=; b=ZnDtC4S/KchIjr2TVXHPa7+1mhLwpqUdDCNP47kvJ5cf8knstr7/88RQE/QEaK9znY 3Wju0ZuMHO2x0FV6hTZBaEsdpN88B7kcypAOgWpamUL3CWizt+oOKUKy5GvKAr4uNMow sqMfihAkoQ5/WDSKmiLjuq5oa4tgJGO8c9udKtq5AugMlPMAjDH/3wKjV37JrT6ulvFl 5TYUrEiuD8kTDfnunu1I8eH1c/iky4/UoUlm9dgHBajxgLocNsFkr6YXz5lJMB/HhGro 9iQQRPeuiEHp9KoMC7j9EYFb88265STm06s6q1VaK2jnJ6WURuy4dHgjYJEz2qFxOTrU u0sg== X-Gm-Message-State: AG10YOQTrT7kPESFubnQCpwCzXkhdFD9mcyz/84WnKxZ3icfyZ5RcorJlzu7dsYl6PKs9BRG X-Received: by 10.98.0.86 with SMTP id 83mr60843624pfa.84.1455129656655; Wed, 10 Feb 2016 10:40:56 -0800 (PST) Received: from rlane-work.eng.bigswitch.com (c-67-188-28-208.hsd1.ca.comcast.net. [67.188.28.208]) by smtp.gmail.com with ESMTPSA id xu1sm6828171pab.31.2016.02.10.10.40.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 10 Feb 2016 10:40:55 -0800 (PST) From: Rich Lane X-Google-Original-From: Rich Lane To: dev@dpdk.org Date: Wed, 10 Feb 2016 10:40:55 -0800 Message-Id: <1455129655-25520-1-git-send-email-rlane@bigswitch.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1453060638-58510-1-git-send-email-rlane@bigswitch.com> References: <1453060638-58510-1-git-send-email-rlane@bigswitch.com> Subject: [dpdk-dev] [PATCH v3] vhost: fix leak of fds and mmaps X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Feb 2016 18:40:57 -0000 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 --- 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(-) diff --git a/lib/librte_vhost/vhost-net.h b/lib/librte_vhost/vhost-net.h index c69b60b..affbd1a 100644 --- a/lib/librte_vhost/vhost-net.h +++ b/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_ */ diff --git a/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c b/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c index ae2c3fa..374c884 100644 --- a/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c +++ b/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c @@ -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; + } +} diff --git a/lib/librte_vhost/vhost_user/vhost-net-user.c b/lib/librte_vhost/vhost_user/vhost-net-user.c index 8b7a448..336efba 100644 --- a/lib/librte_vhost/vhost_user/vhost-net-user.c +++ b/lib/librte_vhost/vhost_user/vhost-net-user.c @@ -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; diff --git a/lib/librte_vhost/vhost_user/virtio-net-user.c b/lib/librte_vhost/vhost_user/virtio-net-user.c index 2934d1c..993ed71 100644 --- a/lib/librte_vhost/vhost_user/virtio-net-user.c +++ b/lib/librte_vhost/vhost_user/virtio-net-user.c @@ -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; + } +} diff --git a/lib/librte_vhost/vhost_user/virtio-net-user.h b/lib/librte_vhost/vhost_user/virtio-net-user.h index b82108d..1140ee1 100644 --- a/lib/librte_vhost/vhost_user/virtio-net-user.h +++ b/lib/librte_vhost/vhost_user/virtio-net-user.h @@ -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 diff --git a/lib/librte_vhost/virtio-net.c b/lib/librte_vhost/virtio-net.c index de78a0f..cf2560e 100644 --- a/lib/librte_vhost/virtio-net.c +++ b/lib/librte_vhost/virtio-net.c @@ -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); -- 1.9.1