From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43916) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zf9I6-0000Tb-Ng for qemu-devel@nongnu.org; Thu, 24 Sep 2015 12:22:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zf9I5-00058c-Fe for qemu-devel@nongnu.org; Thu, 24 Sep 2015 12:22:38 -0400 Received: from mail-qk0-x236.google.com ([2607:f8b0:400d:c09::236]:34106) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zf9I5-00058R-3h for qemu-devel@nongnu.org; Thu, 24 Sep 2015 12:22:37 -0400 Received: by qkfq186 with SMTP id q186so31684701qkf.1 for ; Thu, 24 Sep 2015 09:22:36 -0700 (PDT) Sender: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= From: marcandre.lureau@redhat.com Date: Thu, 24 Sep 2015 18:22:05 +0200 Message-Id: <1443111741-4736-6-git-send-email-marcandre.lureau@redhat.com> In-Reply-To: <1443111741-4736-1-git-send-email-marcandre.lureau@redhat.com> References: <1443111741-4736-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH v5 05/21] vhost: alloc shareable log List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: haifeng.lin@huawei.com, mst@redhat.com, thibaut.collet@6wind.com, jasowang@redhat.com, pbonzini@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= From: Marc-André Lureau If the backend is of type VHOST_BACKEND_TYPE_USER, allocate shareable memory. Next patch will only allocate when the backend has the required feature. vhost_log_get() now uses 2 globals "vhost_log" and "vhost_log_shm", that way there is a common non-shareable log and a common shareable one. Signed-off-by: Marc-André Lureau --- hw/virtio/vhost.c | 49 ++++++++++++++++++++++++++++++++++++++--------- include/hw/virtio/vhost.h | 3 ++- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index c0ed5b2..bf88618 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -18,6 +18,7 @@ #include "qemu/atomic.h" #include "qemu/range.h" #include "qemu/error-report.h" +#include "qemu/memfd.h" #include #include "exec/address-spaces.h" #include "hw/virtio/virtio-bus.h" @@ -25,6 +26,7 @@ #include "migration/migration.h" static struct vhost_log *vhost_log; +static struct vhost_log *vhost_log_shm; static void vhost_dev_sync_region(struct vhost_dev *dev, MemoryRegionSection *section, @@ -286,25 +288,45 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev) } return log_size; } -static struct vhost_log *vhost_log_alloc(uint64_t size) + +static struct vhost_log *vhost_log_alloc(uint64_t size, bool share) { - struct vhost_log *log = g_malloc0(sizeof *log + size * sizeof(*(log->log))); + struct vhost_log *log; + uint64_t logsize = size * sizeof(*(log->log)); + int fd = -1; + + log = g_new0(struct vhost_log, 1); + if (share) { + log->log = qemu_memfd_alloc("vhost-log", logsize, + F_SEAL_GROW|F_SEAL_SHRINK|F_SEAL_SEAL, &fd); + memset(log->log, 0, logsize); + } else { + log->log = g_malloc0(logsize); + } log->size = size; log->refcnt = 1; + log->fd = fd; return log; } -static struct vhost_log *vhost_log_get(uint64_t size) +static struct vhost_log *vhost_log_get(uint64_t size, bool share) { - if (!vhost_log || vhost_log->size != size) { - vhost_log = vhost_log_alloc(size); + struct vhost_log *log = share ? vhost_log_shm : vhost_log; + + if (!log || log->size != size) { + log = vhost_log_alloc(size, share); + if (share) { + vhost_log_shm = log; + } else { + vhost_log = log; + } } else { - ++vhost_log->refcnt; + ++log->refcnt; } - return vhost_log; + return log; } static void vhost_log_put(struct vhost_dev *dev, bool sync) @@ -321,16 +343,24 @@ static void vhost_log_put(struct vhost_dev *dev, bool sync) if (dev->log_size && sync) { vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1); } + if (vhost_log == log) { + g_free(log->log); vhost_log = NULL; + } else if (vhost_log_shm == log) { + qemu_memfd_free(log->log, log->size * sizeof(*(log->log)), + log->fd); + vhost_log_shm = NULL; } + g_free(log); } } static inline void vhost_dev_log_resize(struct vhost_dev* dev, uint64_t size) { - struct vhost_log *log = vhost_log_get(size); + bool share = dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER; + struct vhost_log *log = vhost_log_get(size, share); uint64_t log_base = (uintptr_t)log->log; int r; @@ -1134,9 +1164,10 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) if (hdev->log_enabled) { uint64_t log_base; + bool share = hdev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER; hdev->log_size = vhost_get_log_size(hdev); - hdev->log = vhost_log_get(hdev->log_size); + hdev->log = vhost_log_get(hdev->log_size, share); log_base = (uintptr_t)hdev->log->log; r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE, hdev->log_size ? &log_base : NULL); diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index c3758f3..7e7dc45 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -31,7 +31,8 @@ typedef unsigned long vhost_log_chunk_t; struct vhost_log { unsigned long long size; int refcnt; - vhost_log_chunk_t log[0]; + int fd; + vhost_log_chunk_t *log; }; struct vhost_memory; -- 2.4.3