From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by dpdk.org (Postfix) with ESMTP id 081F62952 for ; Sun, 10 Apr 2016 19:29:01 +0200 (CEST) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP; 10 Apr 2016 10:29:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.24,462,1455004800"; d="scan'208";a="955676227" Received: from shvmail01.sh.intel.com ([10.239.29.42]) by fmsmga002.fm.intel.com with ESMTP; 10 Apr 2016 10:29:00 -0700 Received: from shecgisg004.sh.intel.com (shecgisg004.sh.intel.com [10.239.29.89]) by shvmail01.sh.intel.com with ESMTP id u3AHSwq3000391; Mon, 11 Apr 2016 01:28:58 +0800 Received: from shecgisg004.sh.intel.com (localhost [127.0.0.1]) by shecgisg004.sh.intel.com (8.13.6/8.13.6/SuSE Linux 0.8) with ESMTP id u3AHStQv022017; Mon, 11 Apr 2016 01:28:57 +0800 Received: (from zhetao@localhost) by shecgisg004.sh.intel.com (8.13.6/8.13.6/Submit) id u3AHStw6022013; Mon, 11 Apr 2016 01:28:55 +0800 From: Zhe Tao To: dev@dpdk.org Cc: zhe.tao@intel.com Date: Mon, 11 Apr 2016 01:28:52 +0800 Message-Id: <1460309332-21981-1-git-send-email-zhe.tao@intel.com> X-Mailer: git-send-email 1.7.4.1 Issue: If we didn't set the share option as "on" for the Subject: [dpdk-dev] [PATCH] vhost: fix mem share between VM and host 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: Sun, 10 Apr 2016 17:29:02 -0000 The reason cause this problem is that in QEMU, when assign the memory-backend-file without share option, will cause QEMU mmap the mem file without using the MAP_SHARED flag, so the page cache for that file will not shared between other processes, all the upated to the mapping area in the VM will not carry through to the vhost-user process. According to kernel implementation, data for the new hugetlbfs file will be all zero, so check the first RX virtqueue descriptor next field to see whether the mem is shared or not, if the mem is shared, the next field should not equal to zero, otherwise this mem is not shared between VM and host. Signed-off-by: Zhe Tao --- lib/librte_vhost/vhost-net.h | 1 + lib/librte_vhost/vhost_user/virtio-net-user.c | 7 ++++--- lib/librte_vhost/virtio-net.c | 29 ++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/librte_vhost/vhost-net.h b/lib/librte_vhost/vhost-net.h index f193a1f..588008f 100644 --- a/lib/librte_vhost/vhost-net.h +++ b/lib/librte_vhost/vhost-net.h @@ -105,6 +105,7 @@ int vhost_set_backend(struct vhost_device_ctx, struct vhost_vring_file *); int vhost_set_owner(struct vhost_device_ctx); int vhost_reset_owner(struct vhost_device_ctx); +int vhost_check_mem_shared(struct vhost_device_ctx); /* * Backend-specific cleanup. Defined by vhost-cuse and vhost-user. diff --git a/lib/librte_vhost/vhost_user/virtio-net-user.c b/lib/librte_vhost/vhost_user/virtio-net-user.c index f5248bc..08dd2dd 100644 --- a/lib/librte_vhost/vhost_user/virtio-net-user.c +++ b/lib/librte_vhost/vhost_user/virtio-net-user.c @@ -286,9 +286,10 @@ user_set_vring_kick(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg) "vring kick idx:%d file:%d\n", file.index, file.fd); vhost_set_vring_kick(ctx, &file); - if (virtio_is_ready(dev) && - !(dev->flags & VIRTIO_DEV_RUNNING)) - notify_ops->new_device(dev); + if (!vhost_check_mem_shared(ctx) && + virtio_is_ready(dev) && + !(dev->flags & VIRTIO_DEV_RUNNING)) + notify_ops->new_device(dev); } /* diff --git a/lib/librte_vhost/virtio-net.c b/lib/librte_vhost/virtio-net.c index 90da9ba..6b1a59d 100644 --- a/lib/librte_vhost/virtio-net.c +++ b/lib/librte_vhost/virtio-net.c @@ -712,7 +712,8 @@ vhost_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file) * If the device isn't already running and both backend fds are set, * we add the device. */ - if (!(dev->flags & VIRTIO_DEV_RUNNING)) { + if (!vhost_check_mem_shared(ctx) && + !(dev->flags & VIRTIO_DEV_RUNNING)) { if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) && ((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED)) { return notify_ops->new_device(dev); @@ -724,6 +725,32 @@ vhost_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file) return 0; } +/* Check the share memory in case the QEMU doesn't set the share option + * as on for the memory-backend-file object in the QEMU command line. + */ + +int +vhost_check_mem_shared(struct vhost_device_ctx ctx) +{ + struct virtio_net *dev; + struct vhost_virtqueue *vq; + int ret = 0; + + dev = get_device(ctx); + if ((dev == NULL) || (dev->mem == NULL)) + return -1; + + /* check first virtqueue 0 rxq. */ + vq = dev->virtqueue[VIRTIO_RXQ]; + ret = vq->desc[0].next == 0 ? -1 : 0; + + if (ret) + RTE_LOG(ERR, VHOST_CONFIG, + "The mem is not shared between VM and host\n"); + + return ret; +} + int rte_vhost_enable_guest_notification(struct virtio_net *dev, uint16_t queue_id, int enable) { -- 2.1.4