From nobody Sun Apr 28 08:17:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517378760320624.0964645336891; Tue, 30 Jan 2018 22:06:00 -0800 (PST) Received: from localhost ([::1]:37529 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglWx-0008IL-Ho for importer@patchew.org; Wed, 31 Jan 2018 01:05:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37890) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglUQ-0006SR-Kq for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eglUN-0007ZX-3I for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:22 -0500 Received: from mga12.intel.com ([192.55.52.136]:50365) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eglUM-0007Sc-Qd for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:19 -0500 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Jan 2018 22:03:13 -0800 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.13.35]) by fmsmga006.fm.intel.com with ESMTP; 30 Jan 2018 22:03:11 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,438,1511856000"; d="scan'208";a="200149413" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Wed, 31 Jan 2018 14:02:24 +0800 Message-Id: <20180131060229.9294-2-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180131060229.9294-1-haozhong.zhang@intel.com> References: <20180131060229.9294-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.136 Subject: [Qemu-devel] [PATCH v4 1/6] util/mmap-alloc: switch qemu_ram_mmap() to 'flags' parameter X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , Xiao Guangrong , mst@redhat.com, dgilbert@redhat.com, Stefan Hajnoczi , Paolo Bonzini , Igor Mammedov , Dan Williams , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" As more flag parameters besides the existing 'shared' are going to be added to qemu_ram_mmap(), let's switch 'shared' to a 'flags' parameter in advance, so as to ease the further additions. Signed-off-by: Haozhong Zhang Suggested-by: "Michael S. Tsirkin" --- exec.c | 2 +- include/exec/memory.h | 3 +++ include/qemu/mmap-alloc.h | 19 ++++++++++++++++++- util/mmap-alloc.c | 8 +++++--- util/oslib-posix.c | 2 +- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/exec.c b/exec.c index 629a508385..c0a5a52c4a 100644 --- a/exec.c +++ b/exec.c @@ -1653,7 +1653,7 @@ static void *file_ram_alloc(RAMBlock *block, } =20 area =3D qemu_ram_mmap(fd, memory, block->mr->align, - block->flags & RAM_SHARED); + (block->flags & RAM_SHARED) ? QEMU_RAM_SHARE : 0); if (area =3D=3D MAP_FAILED) { error_setg_errno(errp, errno, "unable to map backing store for guest RAM"); diff --git a/include/exec/memory.h b/include/exec/memory.h index 07c5d6d597..4790cd9e13 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -455,6 +455,9 @@ void memory_region_init_resizeable_ram(MemoryRegion *mr, uint64_t length, void *host), Error **errp); + +#define QEMU_RAM_SHARE (1UL << 0) + #ifdef __linux__ /** * memory_region_init_ram_from_file: Initialize RAM memory region with a diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h index 50385e3f81..dc5e8b5efb 100644 --- a/include/qemu/mmap-alloc.h +++ b/include/qemu/mmap-alloc.h @@ -7,7 +7,24 @@ size_t qemu_fd_getpagesize(int fd); =20 size_t qemu_mempath_getpagesize(const char *mem_path); =20 -void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared); +/** + * qemu_ram_mmap: mmap the specified file or device. + * + * Parameters: + * @fd: the file or the device to mmap + * @size: the number of bytes to be mmaped + * @align: if not zero, specify the alignment of the starting mapping add= ress; + * otherwise, the alignment in use will be determined by QEMU. + * @flags: specifies additional properties of the mapping, which can be o= ne or + * bit-or of following values + * - QEMU_RAM_SHARE: mmap with MAP_SHARED flag + * Other bits are ignored. + * + * Return: + * On success, return a pointer to the mapped area. + * On failure, return MAP_FAILED. + */ +void *qemu_ram_mmap(int fd, size_t size, size_t align, uint64_t flags); =20 void qemu_ram_munmap(void *ptr, size_t size); =20 diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 2fd8cbcc6f..cd95566800 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -13,6 +13,7 @@ #include "qemu/osdep.h" #include "qemu/mmap-alloc.h" #include "qemu/host-utils.h" +#include "exec/memory.h" =20 #define HUGETLBFS_MAGIC 0x958458f6 =20 @@ -73,7 +74,7 @@ size_t qemu_mempath_getpagesize(const char *mem_path) return getpagesize(); } =20 -void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) +void *qemu_ram_mmap(int fd, size_t size, size_t align, uint64_t flags) { /* * Note: this always allocates at least one extra page of virtual addr= ess @@ -90,11 +91,12 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, = bool shared) * anonymous memory is OK. */ int anonfd =3D fd =3D=3D -1 || qemu_fd_getpagesize(fd) =3D=3D getpages= ize() ? -1 : fd; - int flags =3D anonfd =3D=3D -1 ? MAP_ANONYMOUS : MAP_NORESERVE; - void *ptr =3D mmap(0, total, PROT_NONE, flags | MAP_PRIVATE, anonfd, 0= ); + int mmap_flags =3D anonfd =3D=3D -1 ? MAP_ANONYMOUS : MAP_NORESERVE; + void *ptr =3D mmap(0, total, PROT_NONE, mmap_flags | MAP_PRIVATE, anon= fd, 0); #else void *ptr =3D mmap(0, total, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -= 1, 0); #endif + bool shared =3D flags & QEMU_RAM_SHARE; size_t offset; void *ptr1; =20 diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 77369c92ce..2a78cfb67e 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -130,7 +130,7 @@ void *qemu_memalign(size_t alignment, size_t size) void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment) { size_t align =3D QEMU_VMALLOC_ALIGN; - void *ptr =3D qemu_ram_mmap(-1, size, align, false); + void *ptr =3D qemu_ram_mmap(-1, size, align, 0); =20 if (ptr =3D=3D MAP_FAILED) { return NULL; --=20 2.14.1 From nobody Sun Apr 28 08:17:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517378877893764.5844866289243; Tue, 30 Jan 2018 22:07:57 -0800 (PST) Received: from localhost ([::1]:37612 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglYr-0001RD-3A for importer@patchew.org; Wed, 31 Jan 2018 01:07:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37888) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglUQ-0006SD-FK for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eglUL-0007Xg-NN for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:22 -0500 Received: from mga12.intel.com ([192.55.52.136]:50368) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eglUL-0007VH-AF for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:17 -0500 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Jan 2018 22:03:15 -0800 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.13.35]) by fmsmga006.fm.intel.com with ESMTP; 30 Jan 2018 22:03:13 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,438,1511856000"; d="scan'208";a="200149418" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Wed, 31 Jan 2018 14:02:25 +0800 Message-Id: <20180131060229.9294-3-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180131060229.9294-1-haozhong.zhang@intel.com> References: <20180131060229.9294-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.136 Subject: [Qemu-devel] [PATCH v4 2/6] exec: switch qemu_ram_alloc_from_{file, fd} to the 'flags' parameter X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , Xiao Guangrong , mst@redhat.com, dgilbert@redhat.com, Stefan Hajnoczi , Paolo Bonzini , Igor Mammedov , Dan Williams , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" As more flag parameters besides the existing 'share' are going to be added to qemu_ram_alloc_from_{file,fd}(), let's swith 'share' to a 'flags' parameters in advance, so as to ease the further additions. Signed-off-by: Haozhong Zhang --- exec.c | 15 ++++++++------- include/exec/ram_addr.h | 25 +++++++++++++++++++++++-- memory.c | 8 ++++++-- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/exec.c b/exec.c index c0a5a52c4a..0b46b03d87 100644 --- a/exec.c +++ b/exec.c @@ -1607,6 +1607,7 @@ static void *file_ram_alloc(RAMBlock *block, ram_addr_t memory, int fd, bool truncate, + uint64_t flags, Error **errp) { void *area; @@ -1652,8 +1653,7 @@ static void *file_ram_alloc(RAMBlock *block, perror("ftruncate"); } =20 - area =3D qemu_ram_mmap(fd, memory, block->mr->align, - (block->flags & RAM_SHARED) ? QEMU_RAM_SHARE : 0); + area =3D qemu_ram_mmap(fd, memory, block->mr->align, flags); if (area =3D=3D MAP_FAILED) { error_setg_errno(errp, errno, "unable to map backing store for guest RAM"); @@ -2000,7 +2000,7 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp) =20 #ifdef __linux__ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, - bool share, int fd, + uint64_t flags, int fd, Error **errp) { RAMBlock *new_block; @@ -2042,8 +2042,9 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Mem= oryRegion *mr, new_block->mr =3D mr; new_block->used_length =3D size; new_block->max_length =3D size; - new_block->flags =3D share ? RAM_SHARED : 0; - new_block->host =3D file_ram_alloc(new_block, size, fd, !file_size, er= rp); + new_block->flags =3D (flags & QEMU_RAM_SHARE) ? RAM_SHARED : 0; + new_block->host =3D file_ram_alloc(new_block, size, fd, !file_size, fl= ags, + errp); if (!new_block->host) { g_free(new_block); return NULL; @@ -2061,7 +2062,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Mem= oryRegion *mr, =20 =20 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, - bool share, const char *mem_path, + uint64_t flags, const char *mem_path, Error **errp) { int fd; @@ -2073,7 +2074,7 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, M= emoryRegion *mr, return NULL; } =20 - block =3D qemu_ram_alloc_from_fd(size, mr, share, fd, errp); + block =3D qemu_ram_alloc_from_fd(size, mr, flags, fd, errp); if (!block) { if (created) { unlink(mem_path); diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 7633ef6342..e24aae75a2 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -72,12 +72,33 @@ static inline unsigned long int ramblock_recv_bitmap_of= fset(void *host_addr, =20 long qemu_getrampagesize(void); unsigned long last_ram_page(void); + +/** + * qemu_ram_alloc_from_file, + * qemu_ram_alloc_from_fd: Allocate a ram block from the specified back + * file or device + * + * Parameters: + * @size: the size in bytes of the ram block + * @mr: the memory region where the ram block is + * @flags: specify the properties of the ram block, which can be one + * or bit-or of following values + * - QEMU_RAM_SHARE: mmap the back file or device with MAP_SHARED + * Other bits are ignored. + * @mem_path or @fd: specify the back file or device + * @errp: pointer to Error*, to store an error if it happens + * + * Return: + * On success, return a pointer to the ram block. + * On failure, return NULL. + */ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, - bool share, const char *mem_path, + uint64_t flags, const char *mem_path, Error **errp); RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, - bool share, int fd, + uint64_t flags, int fd, Error **errp); + RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, MemoryRegion *mr, Error **errp); RAMBlock *qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error **errp); diff --git a/memory.c b/memory.c index 449a1429b9..1ac4ebcaca 100644 --- a/memory.c +++ b/memory.c @@ -1580,7 +1580,9 @@ void memory_region_init_ram_from_file(MemoryRegion *m= r, mr->terminates =3D true; mr->destructor =3D memory_region_destructor_ram; mr->align =3D align; - mr->ram_block =3D qemu_ram_alloc_from_file(size, mr, share, path, errp= ); + mr->ram_block =3D qemu_ram_alloc_from_file(size, mr, + share ? QEMU_RAM_SHARE : 0, + path, errp); mr->dirty_log_mask =3D tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; } =20 @@ -1596,7 +1598,9 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr, mr->ram =3D true; mr->terminates =3D true; mr->destructor =3D memory_region_destructor_ram; - mr->ram_block =3D qemu_ram_alloc_from_fd(size, mr, share, fd, errp); + mr->ram_block =3D qemu_ram_alloc_from_fd(size, mr, + share ? QEMU_RAM_SHARE : 0, + fd, errp); mr->dirty_log_mask =3D tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; } #endif --=20 2.14.1 From nobody Sun Apr 28 08:17:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517378702072868.0912788350794; Tue, 30 Jan 2018 22:05:02 -0800 (PST) Received: from localhost ([::1]:37514 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglW0-0007SF-SK for importer@patchew.org; Wed, 31 Jan 2018 01:05:00 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37893) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglUQ-0006SX-Lg for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eglUM-0007Z0-FU for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:22 -0500 Received: from mga12.intel.com ([192.55.52.136]:50368) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eglUM-0007VH-4k for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:18 -0500 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Jan 2018 22:03:17 -0800 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.13.35]) by fmsmga006.fm.intel.com with ESMTP; 30 Jan 2018 22:03:16 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,438,1511856000"; d="scan'208";a="200149423" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Wed, 31 Jan 2018 14:02:26 +0800 Message-Id: <20180131060229.9294-4-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180131060229.9294-1-haozhong.zhang@intel.com> References: <20180131060229.9294-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.136 Subject: [Qemu-devel] [PATCH v4 3/6] memory: switch memory_region_init_ram_from_file() to 'flags' parameter X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , Xiao Guangrong , mst@redhat.com, dgilbert@redhat.com, Stefan Hajnoczi , Paolo Bonzini , Igor Mammedov , Dan Williams , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" As more flag parameters besides the existing 'share' are going to be added to memory_region_init_ram_from_file(), let's switch 'share' to a 'flags' parameter in advance, so as to ease the further additions. Signed-off-by: Haozhong Zhang --- backends/hostmem-file.c | 3 ++- include/exec/memory.h | 7 +++++-- memory.c | 6 ++---- numa.c | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index e319ec1ad8..67ecfed895 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -59,7 +59,8 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Err= or **errp) path =3D object_get_canonical_path(OBJECT(backend)); memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), path, - backend->size, fb->align, fb->share, + backend->size, fb->align, + fb->share ? QEMU_RAM_SHARE : 0, fb->mem_path, errp); g_free(path); } diff --git a/include/exec/memory.h b/include/exec/memory.h index 4790cd9e13..6b547da6a3 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -470,7 +470,10 @@ void memory_region_init_resizeable_ram(MemoryRegion *m= r, * @size: size of the region. * @align: alignment of the region base address; if 0, the default alignme= nt * (getpagesize()) will be used. - * @share: %true if memory must be mmaped with the MAP_SHARED flag + * @flags: specify properties of this memory region, which can be one or b= it-or + * of following values: + * - QEMU_RAM_SHARE: memory must be mmaped with the MAP_SHARED flag + * Other bits are ignored. * @path: the path in which to allocate the RAM. * @errp: pointer to Error*, to store an error if it happens. * @@ -482,7 +485,7 @@ void memory_region_init_ram_from_file(MemoryRegion *mr, const char *name, uint64_t size, uint64_t align, - bool share, + uint64_t flags, const char *path, Error **errp); =20 diff --git a/memory.c b/memory.c index 1ac4ebcaca..a4f19a5d30 100644 --- a/memory.c +++ b/memory.c @@ -1571,7 +1571,7 @@ void memory_region_init_ram_from_file(MemoryRegion *m= r, const char *name, uint64_t size, uint64_t align, - bool share, + uint64_t flags, const char *path, Error **errp) { @@ -1580,9 +1580,7 @@ void memory_region_init_ram_from_file(MemoryRegion *m= r, mr->terminates =3D true; mr->destructor =3D memory_region_destructor_ram; mr->align =3D align; - mr->ram_block =3D qemu_ram_alloc_from_file(size, mr, - share ? QEMU_RAM_SHARE : 0, - path, errp); + mr->ram_block =3D qemu_ram_alloc_from_file(size, mr, flags, path, errp= ); mr->dirty_log_mask =3D tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; } =20 diff --git a/numa.c b/numa.c index 83675a03f3..fa202a376d 100644 --- a/numa.c +++ b/numa.c @@ -456,7 +456,7 @@ static void allocate_system_memory_nonnuma(MemoryRegion= *mr, Object *owner, if (mem_path) { #ifdef __linux__ Error *err =3D NULL; - memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, fal= se, + memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, 0, mem_path, &err); if (err) { error_report_err(err); --=20 2.14.1 From nobody Sun Apr 28 08:17:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517378945808349.435441797624; Tue, 30 Jan 2018 22:09:05 -0800 (PST) Received: from localhost ([::1]:37713 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglZp-0002OC-8L for importer@patchew.org; Wed, 31 Jan 2018 01:08:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37892) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglUQ-0006SW-La for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eglUO-0007ay-R6 for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:22 -0500 Received: from mga12.intel.com ([192.55.52.136]:50362) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eglUO-0007QP-Ff for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:20 -0500 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Jan 2018 22:03:20 -0800 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.13.35]) by fmsmga006.fm.intel.com with ESMTP; 30 Jan 2018 22:03:18 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,438,1511856000"; d="scan'208";a="200149428" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Wed, 31 Jan 2018 14:02:27 +0800 Message-Id: <20180131060229.9294-5-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180131060229.9294-1-haozhong.zhang@intel.com> References: <20180131060229.9294-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.136 Subject: [Qemu-devel] [PATCH v4 4/6] util/mmap-alloc: support MAP_SYNC in qemu_ram_mmap() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , Xiao Guangrong , mst@redhat.com, dgilbert@redhat.com, Stefan Hajnoczi , Paolo Bonzini , Igor Mammedov , Dan Williams , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When a file supporting DAX is used as vNVDIMM backend, mmap it with MAP_SYNC flag in addition can guarantee the persistence of guest write to the backend file without other QEMU actions (e.g., periodic fsync() by QEMU). A set of QEMU_RAM_SYNC_{AUTO,ON,OFF} flags are added to qemu_ram_mmap(): - If QEMU_RAM_SYNC_ON is present, qemu_ram_mmap() will try to pass MAP_SYNC to mmap(). It will then fail if the host OS or the backend file do not support MAP_SYNC, or MAP_SYNC is conflict with other flags. - If QEMU_RAM_SYNC_OFF is present, qemu_ram_mmap() will never pass MAP_SYNC to mmap(). - If QEMU_RAM_SYNC_AUTO is present, and * if the host OS and the backend file support MAP_SYNC, and MAP_SYNC is not conflict with other flags, qemu_ram_mmap() will work as if QEMU_RAM_SYNC_ON is present; * otherwise, qemu_ram_mmap() will work as if QEMU_RAM_SYNC_OFF is present. Signed-off-by: Haozhong Zhang --- include/exec/memory.h | 26 ++++++++++++++++++++++ include/exec/ram_addr.h | 4 ++++ include/qemu/mmap-alloc.h | 4 ++++ include/standard-headers/linux/mman.h | 42 +++++++++++++++++++++++++++++++= ++++ util/mmap-alloc.c | 23 ++++++++++++++++++- 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 include/standard-headers/linux/mman.h diff --git a/include/exec/memory.h b/include/exec/memory.h index 6b547da6a3..96a60e9c1d 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -458,6 +458,28 @@ void memory_region_init_resizeable_ram(MemoryRegion *m= r, =20 #define QEMU_RAM_SHARE (1UL << 0) =20 +#define QEMU_RAM_SYNC_SHIFT 1 +#define QEMU_RAM_SYNC_MASK 0x6 +#define QEMU_RAM_SYNC_OFF ((0UL << QEMU_RAM_SYNC_SHIFT) & QEMU_RAM_SYNC_= MASK) +#define QEMU_RAM_SYNC_ON ((1UL << QEMU_RAM_SYNC_SHIFT) & QEMU_RAM_SYNC_= MASK) +#define QEMU_RAM_SYNC_AUTO ((2UL << QEMU_RAM_SYNC_SHIFT) & QEMU_RAM_SYNC_= MASK) + +static inline uint64_t qemu_ram_sync_flags(OnOffAuto v) +{ + return v =3D=3D ON_OFF_AUTO_OFF ? QEMU_RAM_SYNC_OFF : + v =3D=3D ON_OFF_AUTO_ON ? QEMU_RAM_SYNC_ON : QEMU_RAM_SYNC_AUTO; +} + +static inline OnOffAuto qemu_ram_sync_val(uint64_t flags) +{ + unsigned int v =3D (flags & QEMU_RAM_SYNC_MASK) >> QEMU_RAM_SYNC_SHIFT; + + assert(v < 3); + + return v =3D=3D 0 ? ON_OFF_AUTO_OFF : + v =3D=3D 1 ? ON_OFF_AUTO_ON : ON_OFF_AUTO_AUTO; +} + #ifdef __linux__ /** * memory_region_init_ram_from_file: Initialize RAM memory region with a @@ -473,6 +495,10 @@ void memory_region_init_resizeable_ram(MemoryRegion *m= r, * @flags: specify properties of this memory region, which can be one or b= it-or * of following values: * - QEMU_RAM_SHARE: memory must be mmaped with the MAP_SHARED flag + * - One of + * QEMU_RAM_SYNC_ON: mmap with MAP_SYNC flag + * QEMU_RAM_SYNC_OFF: do not mmap with MAP_SYNC flag + * QEMU_RAM_SYNC_AUTO: automatically decide the use of MAP_SYNC = flag * Other bits are ignored. * @path: the path in which to allocate the RAM. * @errp: pointer to Error*, to store an error if it happens. diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index e24aae75a2..a2cc5a9f60 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -84,6 +84,10 @@ unsigned long last_ram_page(void); * @flags: specify the properties of the ram block, which can be one * or bit-or of following values * - QEMU_RAM_SHARE: mmap the back file or device with MAP_SHARED + * - One of + * QEMU_RAM_SYNC_ON: mmap with MAP_SYNC flag + * QEMU_RAM_SYNC_OFF: do not mmap with MAP_SYNC flag + * QEMU_RAM_SYNC_AUTO: automatically decide the use of MAP_SYNC= flag * Other bits are ignored. * @mem_path or @fd: specify the back file or device * @errp: pointer to Error*, to store an error if it happens diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h index dc5e8b5efb..74346bdd3a 100644 --- a/include/qemu/mmap-alloc.h +++ b/include/qemu/mmap-alloc.h @@ -18,6 +18,10 @@ size_t qemu_mempath_getpagesize(const char *mem_path); * @flags: specifies additional properties of the mapping, which can be o= ne or * bit-or of following values * - QEMU_RAM_SHARE: mmap with MAP_SHARED flag + * - One of + * QEMU_RAM_SYNC_ON: mmap with MAP_SYNC flag + * QEMU_RAM_SYNC_OFF: do not mmap with MAP_SYNC flag + * QEMU_RAM_SYNC_AUTO: automatically decide the use of MAP_SYNC= flag * Other bits are ignored. * * Return: diff --git a/include/standard-headers/linux/mman.h b/include/standard-heade= rs/linux/mman.h new file mode 100644 index 0000000000..033332ad4f --- /dev/null +++ b/include/standard-headers/linux/mman.h @@ -0,0 +1,42 @@ +/* + * Definitions of Linux-specific mmap flags. + * + * Copyright Intel Corporation, 2018 + * + * Author: Haozhong Zhang + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#ifndef _LINUX_MMAN_H +#define _LINUX_MMAN_H + +/* + * MAP_SHARED_VALIDATE and MAP_SYNC are introduced in Linux kernel + * 4.15, so they may not be defined when compiling on older kernels. + */ +#ifdef CONFIG_LINUX + +#include + +#ifndef MAP_SHARED_VALIDATE +#define MAP_SHARED_VALIDATE 0x3 +#endif + +#ifndef MAP_SYNC +#define MAP_SYNC 0x80000 +#endif + +#define QEMU_HAS_MAP_SYNC true + +#else /* !CONFIG_LINUX */ + +#define MAP_SHARED_VALIDATE 0x0 +#define MAP_SYNC 0x0 + +#define QEMU_HAS_MAP_SYNC false + +#endif /* CONFIG_LINUX */ + +#endif /* !_LINUX_MMAN_H */ diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index cd95566800..6df2f6d2c4 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -14,6 +14,7 @@ #include "qemu/mmap-alloc.h" #include "qemu/host-utils.h" #include "exec/memory.h" +#include "standard-headers/linux/mman.h" =20 #define HUGETLBFS_MAGIC 0x958458f6 =20 @@ -97,6 +98,8 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, ui= nt64_t flags) void *ptr =3D mmap(0, total, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -= 1, 0); #endif bool shared =3D flags & QEMU_RAM_SHARE; + OnOffAuto sync =3D qemu_ram_sync_val(flags); + int mmap_xflags =3D 0; size_t offset; void *ptr1; =20 @@ -108,13 +111,31 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align= , uint64_t flags) /* Always align to host page size */ assert(align >=3D getpagesize()); =20 + if (!QEMU_HAS_MAP_SYNC || !shared) { + if (sync =3D=3D ON_OFF_AUTO_ON) { + return MAP_FAILED; + } + sync =3D ON_OFF_AUTO_OFF; + } + if (sync !=3D ON_OFF_AUTO_OFF) { + /* MAP_SYNC is only available with MAP_SHARED_VALIDATE. */ + mmap_xflags |=3D MAP_SYNC | MAP_SHARED_VALIDATE; + } + offset =3D QEMU_ALIGN_UP((uintptr_t)ptr, align) - (uintptr_t)ptr; + retry_mmap_fd: ptr1 =3D mmap(ptr + offset, size, PROT_READ | PROT_WRITE, MAP_FIXED | (fd =3D=3D -1 ? MAP_ANONYMOUS : 0) | - (shared ? MAP_SHARED : MAP_PRIVATE), + (shared ? MAP_SHARED : MAP_PRIVATE) | mmap_xflags, fd, 0); if (ptr1 =3D=3D MAP_FAILED) { + if (sync =3D=3D ON_OFF_AUTO_AUTO) { + mmap_xflags &=3D ~(MAP_SYNC | MAP_SHARED_VALIDATE); + sync =3D ON_OFF_AUTO_OFF; + goto retry_mmap_fd; + } + munmap(ptr, total); return MAP_FAILED; } --=20 2.14.1 From nobody Sun Apr 28 08:17:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517379015371520.9211980174307; Tue, 30 Jan 2018 22:10:15 -0800 (PST) Received: from localhost ([::1]:37766 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglb3-0003D2-N7 for importer@patchew.org; Wed, 31 Jan 2018 01:10:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37925) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglUT-0006Tj-BV for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eglUS-0007g1-Bo for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:25 -0500 Received: from mga12.intel.com ([192.55.52.136]:50362) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eglUR-0007QP-Vu for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:24 -0500 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Jan 2018 22:03:23 -0800 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.13.35]) by fmsmga006.fm.intel.com with ESMTP; 30 Jan 2018 22:03:20 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,438,1511856000"; d="scan'208";a="200149433" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Wed, 31 Jan 2018 14:02:28 +0800 Message-Id: <20180131060229.9294-6-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180131060229.9294-1-haozhong.zhang@intel.com> References: <20180131060229.9294-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.136 Subject: [Qemu-devel] [PATCH v4 5/6] hostmem: add more information in error messages X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , Xiao Guangrong , mst@redhat.com, dgilbert@redhat.com, Stefan Hajnoczi , Paolo Bonzini , Igor Mammedov , Dan Williams , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When there are multiple memory backends in use, including the object type name, ID and the property name in the error message can help users to locate the error. Signed-off-by: Haozhong Zhang Suggested-by: "Dr. David Alan Gilbert" Reviewed-by: Michael S. Tsirkin --- backends/hostmem-file.c | 9 ++++++--- backends/hostmem.c | 11 +++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index 67ecfed895..df06b547a6 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -80,7 +80,8 @@ static void set_mem_path(Object *o, const char *str, Erro= r **errp) HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(o); =20 if (host_memory_backend_mr_inited(backend)) { - error_setg(errp, "cannot change property value"); + error_setg(errp, "cannot change property 'mem-path' of %s '%s'", + object_get_typename(o), backend->id); return; } g_free(fb->mem_path); @@ -100,7 +101,8 @@ static void file_memory_backend_set_share(Object *o, bo= ol value, Error **errp) HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(o); =20 if (host_memory_backend_mr_inited(backend)) { - error_setg(errp, "cannot change property value"); + error_setg(errp, "cannot change property 'share' of %s '%s'", + object_get_typename(o), backend->id); return; } fb->share =3D value; @@ -137,7 +139,8 @@ static void file_memory_backend_set_align(Object *o, Vi= sitor *v, uint64_t val; =20 if (host_memory_backend_mr_inited(backend)) { - error_setg(&local_err, "cannot change property value"); + error_setg(&local_err, "cannot change property '%s' of %s '%s'", + name, object_get_typename(o), backend->id); goto out; } =20 diff --git a/backends/hostmem.c b/backends/hostmem.c index ee2c2d5bfd..6853d19bc5 100644 --- a/backends/hostmem.c +++ b/backends/hostmem.c @@ -46,7 +46,8 @@ host_memory_backend_set_size(Object *obj, Visitor *v, con= st char *name, uint64_t value; =20 if (host_memory_backend_mr_inited(backend)) { - error_setg(&local_err, "cannot change property value"); + error_setg(&local_err, "cannot change property %s of %s '%s'", + name, object_get_typename(obj), backend->id); goto out; } =20 @@ -55,8 +56,9 @@ host_memory_backend_set_size(Object *obj, Visitor *v, con= st char *name, goto out; } if (!value) { - error_setg(&local_err, "Property '%s.%s' doesn't take value '%" - PRIu64 "'", object_get_typename(obj), name, value); + error_setg(&local_err, + "property '%s' of %s '%s' doesn't take value '%" PRIu64= "'", + name, object_get_typename(obj), backend->id, value); goto out; } backend->size =3D value; @@ -363,7 +365,8 @@ static void set_id(Object *o, const char *str, Error **= errp) HostMemoryBackend *backend =3D MEMORY_BACKEND(o); =20 if (backend->id) { - error_setg(errp, "cannot change property value"); + error_setg(errp, "cannot change property 'id' of %s '%s'", + object_get_typename(o), backend->id); return; } backend->id =3D g_strdup(str); --=20 2.14.1 From nobody Sun Apr 28 08:17:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517378718648975.3273633890417; Tue, 30 Jan 2018 22:05:18 -0800 (PST) Received: from localhost ([::1]:37516 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglWH-0007gz-QD for importer@patchew.org; Wed, 31 Jan 2018 01:05:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37940) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglUW-0006WJ-Ay for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eglUU-0007mN-Tq for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:28 -0500 Received: from mga12.intel.com ([192.55.52.136]:50362) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eglUU-0007QP-I2 for qemu-devel@nongnu.org; Wed, 31 Jan 2018 01:03:26 -0500 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Jan 2018 22:03:26 -0800 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.13.35]) by fmsmga006.fm.intel.com with ESMTP; 30 Jan 2018 22:03:24 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,438,1511856000"; d="scan'208";a="200149441" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Wed, 31 Jan 2018 14:02:29 +0800 Message-Id: <20180131060229.9294-7-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180131060229.9294-1-haozhong.zhang@intel.com> References: <20180131060229.9294-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.136 Subject: [Qemu-devel] [PATCH v4 6/6] hostmem-file: add 'sync' option X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , Xiao Guangrong , mst@redhat.com, dgilbert@redhat.com, Stefan Hajnoczi , Paolo Bonzini , Igor Mammedov , Dan Williams , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This option controls whether QEMU mmap(2) the memory backend file with MAP_SYNC flag, which can fully guarantee the guest write persistence to the backend, if MAP_SYNC flag is supported by the host kernel (Linux kernel 4.15 and later) and the backend is a file supporting DAX (e.g., file on ext4/xfs file system mounted with '-o dax'). It can take one of following values: - on: try to pass MAP_SYNC to mmap(2); if MAP_SYNC is not supported or 'share=3Doff', QEMU will abort - off: never pass MAP_SYNC to mmap(2) - auto (default): if MAP_SYNC is supported and 'share=3Don', work as if 'sync=3Don'; otherwise, work as if 'sync=3Doff' Signed-off-by: Haozhong Zhang Suggested-by: Eduardo Habkost Reviewed-by: Michael S. Tsirkin --- backends/hostmem-file.c | 41 ++++++++++++++++++++++++++++++++++++++++- docs/nvdimm.txt | 20 +++++++++++++++++++- qemu-options.hx | 22 +++++++++++++++++++++- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index df06b547a6..ade80d76f1 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -15,6 +15,7 @@ #include "sysemu/hostmem.h" #include "sysemu/sysemu.h" #include "qom/object_interfaces.h" +#include "qapi-visit.h" =20 /* hostmem-file.c */ /** @@ -35,6 +36,7 @@ struct HostMemoryBackendFile { bool discard_data; char *mem_path; uint64_t align; + OnOffAuto sync; }; =20 static void @@ -60,7 +62,8 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Err= or **errp) memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), path, backend->size, fb->align, - fb->share ? QEMU_RAM_SHARE : 0, + (fb->share ? QEMU_RAM_SHARE : 0) | + qemu_ram_sync_flags(fb->sync), fb->mem_path, errp); g_free(path); } @@ -154,6 +157,39 @@ static void file_memory_backend_set_align(Object *o, V= isitor *v, error_propagate(errp, local_err); } =20 +static void file_memory_backend_get_sync( + Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) +{ + HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(obj); + OnOffAuto value =3D fb->sync; + + visit_type_OnOffAuto(v, name, &value, errp); +} + +static void file_memory_backend_set_sync( + Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) +{ + HostMemoryBackend *backend =3D MEMORY_BACKEND(obj); + HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(obj); + Error *local_err =3D NULL; + OnOffAuto value; + + if (host_memory_backend_mr_inited(backend)) { + error_setg(&local_err, "cannot change property '%s' of %s '%s'", + name, object_get_typename(obj), backend->id); + goto out; + } + + visit_type_OnOffAuto(v, name, &value, &local_err); + if (local_err) { + goto out; + } + fb->sync =3D value; + + out: + error_propagate(errp, local_err); +} + static void file_backend_unparent(Object *obj) { HostMemoryBackend *backend =3D MEMORY_BACKEND(obj); @@ -188,6 +224,9 @@ file_backend_class_init(ObjectClass *oc, void *data) file_memory_backend_get_align, file_memory_backend_set_align, NULL, NULL, &error_abort); + object_class_property_add(oc, "sync", "OnOffAuto", + file_memory_backend_get_sync, file_memory_backend_set_sync, + NULL, NULL, &error_abort); } =20 static void file_backend_instance_finalize(Object *o) diff --git a/docs/nvdimm.txt b/docs/nvdimm.txt index e903d8bb09..5e9cee5f5e 100644 --- a/docs/nvdimm.txt +++ b/docs/nvdimm.txt @@ -142,11 +142,29 @@ backend of vNVDIMM: Guest Data Persistence ---------------------- =20 +vNVDIMM is designed and implemented to guarantee the guest data +persistence on the backends even on the host crash and power +failures. However, there are still some requirements and limitations +as explained below. + Though QEMU supports multiple types of vNVDIMM backends on Linux, -currently the only one that can guarantee the guest write persistence +if MAP_SYNC is not supported by the host kernel and the backends, +the only backend that can guarantee the guest write persistence is the device DAX on the real NVDIMM device (e.g., /dev/dax0.0), to which all guest access do not involve any host-side kernel cache. =20 +mmap(2) flag MAP_SYNC is added since Linux kernel 4.15. On such +systems, QEMU can mmap(2) the backend with MAP_SYNC, which can +guarantee the guest write persistence to vNVDIMM. Besides the host +kernel support, enabling MAP_SYNC in QEMU also requires: + + - the backend is a file supporting DAX, e.g., a file on an ext4 or + xfs file system mounted with '-o dax', + + - 'sync' option of memory-backend-file is not 'off', and + + - 'share' option of memory-backend-file is 'on'. + When using other types of backends, it's suggested to set 'unarmed' option of '-device nvdimm' to 'on', which sets the unarmed flag of the guest NVDIMM region mapping structure. This unarmed flag indicates diff --git a/qemu-options.hx b/qemu-options.hx index 8ce427da78..4e8ec5ed97 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3957,7 +3957,7 @@ property must be set. These objects are placed in the =20 @table @option =20 -@item -object memory-backend-file,id=3D@var{id},size=3D@var{size},mem-path= =3D@var{dir},share=3D@var{on|off},discard-data=3D@var{on|off},merge=3D@var{= on|off},dump=3D@var{on|off},prealloc=3D@var{on|off},host-nodes=3D@var{host-= nodes},policy=3D@var{default|preferred|bind|interleave},align=3D@var{align} +@item -object memory-backend-file,id=3D@var{id},size=3D@var{size},mem-path= =3D@var{dir},share=3D@var{on|off},discard-data=3D@var{on|off},merge=3D@var{= on|off},dump=3D@var{on|off},prealloc=3D@var{on|off},host-nodes=3D@var{host-= nodes},policy=3D@var{default|preferred|bind|interleave},align=3D@var{align}= ,sync=3D@var{on|off|auto} =20 Creates a memory file backend object, which can be used to back the guest RAM with huge pages. @@ -4017,6 +4017,26 @@ requires an alignment different than the default one= used by QEMU, eg the device DAX /dev/dax0.0 requires 2M alignment rather than 4K. In such cases, users can specify the required alignment via this option. =20 +The @option{sync} option specifies whether QEMU mmap(2) @option{mem-path} +with MAP_SYNC flag, which can guarantee the guest write persistence to +@option{mem-path} even on the host crash and power failures. MAP_SYNC +requires supports from both the host kernel (since Linux kernel 4.15) +and @option{mem-path} (only files supporting DAX). It can take one of +following values: + +@table @option +@item @var{on} +try to pass MAP_SYNC to mmap(2); if MAP_SYNC is not supported or +@option{share}=3D@var{off}, QEMU will abort + +@item @var{off} +never pass MAP_SYNC to mmap(2) + +@item @var{auto} (default) +if MAP_SYNC is supported and @option{share}=3D@var{on}, work as if +@option{sync}=3D@var{on}; otherwise, work as if @option{sync}=3D@var{off} +@end table + @item -object memory-backend-ram,id=3D@var{id},merge=3D@var{on|off},dump= =3D@var{on|off},prealloc=3D@var{on|off},size=3D@var{size},host-nodes=3D@var= {host-nodes},policy=3D@var{default|preferred|bind|interleave} =20 Creates a memory backend object, which can be used to back the guest RAM. --=20 2.14.1