From patchwork Tue Dec 18 08:17:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Yi" X-Patchwork-Id: 10735083 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7A190924 for ; Tue, 18 Dec 2018 08:19:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 678E629895 for ; Tue, 18 Dec 2018 08:19:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5816A298B6; Tue, 18 Dec 2018 08:19:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BB71829895 for ; Tue, 18 Dec 2018 08:19:01 +0000 (UTC) Received: from localhost ([::1]:52451 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZAai-0001hB-U7 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 18 Dec 2018 03:19:00 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53640) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZAZ8-0001g1-9J for qemu-devel@nongnu.org; Tue, 18 Dec 2018 03:17:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gZAZ5-0001cQ-3C for qemu-devel@nongnu.org; Tue, 18 Dec 2018 03:17:22 -0500 Received: from mga11.intel.com ([192.55.52.93]:61479) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gZAZ4-0001bZ-Lp for qemu-devel@nongnu.org; Tue, 18 Dec 2018 03:17:18 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Dec 2018 00:17:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,367,1539673200"; d="scan'208";a="110304085" Received: from linux.intel.com ([10.54.29.200]) by fmsmga008.fm.intel.com with ESMTP; 18 Dec 2018 00:17:17 -0800 Received: from dazhang1-ssd.sh.intel.com (unknown [10.239.48.128]) by linux.intel.com (Postfix) with ESMTP id BB6DE5805FC; Tue, 18 Dec 2018 00:17:14 -0800 (PST) From: Zhang Yi To: mst@redhat.com, peter.maydell@linaro.org, yu.c.zhang@linux.intel.com, ehabkost@redhat.com, imammedo@redhat.com Date: Tue, 18 Dec 2018 16:17:12 +0800 Message-Id: <2dda518be27a44e26dc8b45d6e2dc7f46c890926.1545120100.git.yi.z.zhang@linux.intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 Subject: [Qemu-devel] [PATCH V7 3/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: xiaoguangrong.eric@gmail.com, crosthwaite.peter@gmail.com, richard.henderson@linaro.org, qemu-devel@nongnu.org, Zhang Yi , stefanha@redhat.com, pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 RAM_SYNC flags are added to qemu_ram_mmap(): Signed-off-by: Haozhong Zhang Signed-off-by: Zhang Yi --- exec.c | 2 +- include/exec/memory.h | 3 +++ include/exec/ram_addr.h | 1 + include/qemu/mmap-alloc.h | 1 + include/qemu/osdep.h | 29 +++++++++++++++++++++++++++++ util/mmap-alloc.c | 14 ++++++++++---- 6 files changed, 45 insertions(+), 5 deletions(-) diff --git a/exec.c b/exec.c index e92a7da..dc4d180 100644 --- a/exec.c +++ b/exec.c @@ -2241,7 +2241,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, int64_t file_size; /* Just support these ram flags by now. */ - assert((ram_flags & ~(RAM_SHARED | RAM_PMEM)) == 0); + assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_SYNC)) == 0); if (xen_enabled()) { error_setg(errp, "-mem-path not supported with Xen"); diff --git a/include/exec/memory.h b/include/exec/memory.h index 667466b..33a4e2c 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -126,6 +126,9 @@ typedef struct IOMMUNotifier IOMMUNotifier; /* RAM is a persistent kind memory */ #define RAM_PMEM (1 << 5) +/* RAM can be mmap by a MAP_SYNC flag */ +#define RAM_SYNC (1 << 6) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end, diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 9ecd911..d239ce7 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -87,6 +87,7 @@ long qemu_getrampagesize(void); * or bit-or of following values * - RAM_SHARED: mmap the backing file or device with MAP_SHARED * - RAM_PMEM: the backend @mem_path or @fd is persistent memory + * - RAM_SYNC: mmap with MAP_SYNC flag * Other bits are ignored. * @mem_path or @fd: specify the backing 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 6fe6ed4..1755a8b 100644 --- a/include/qemu/mmap-alloc.h +++ b/include/qemu/mmap-alloc.h @@ -18,6 +18,7 @@ size_t qemu_mempath_getpagesize(const char *mem_path); * @flags: specifies additional properties of the mapping, which can be one or * bit-or of following values * - RAM_SHARED: mmap with MAP_SHARED flag + * - RAM_SYNC: mmap with MAP_SYNC flag * Other bits are ignored. * * Return: diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 3bf48bc..f94ea68 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -410,6 +410,35 @@ void qemu_anon_ram_free(void *ptr, size_t size); # define QEMU_VMALLOC_ALIGN getpagesize() #endif +/* + * 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 + +/* MAP_SYNC is only available with MAP_SHARED_VALIDATE. */ +#define MAP_SYNC_FLAGS (MAP_SYNC | MAP_SHARED_VALIDATE) + +#else /* !CONFIG_LINUX */ + +#define MAP_SHARED_VALIDATE 0x0 +#define MAP_SYNC 0x0 + +#define QEMU_HAS_MAP_SYNC false +#define MAP_SYNC_FLAGS 0 + +#endif /* CONFIG_LINUX */ + #ifdef CONFIG_POSIX struct qemu_signalfd_siginfo { uint32_t ssi_signo; /* Signal number */ diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 8f0a740..89ae862 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -99,6 +99,8 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, uint32_t flags) void *ptr = mmap(0, total, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); #endif bool shared = flags & RAM_SHARED; + bool is_pmem = flags & RAM_PMEM; + int mmap_xflags = 0; size_t offset; void *ptr1; @@ -109,16 +111,20 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, uint32_t flags) assert(is_power_of_2(align)); /* Always align to host page size */ assert(align >= getpagesize()); + if ((flags & RAM_SYNC) && shared && is_pmem) { + mmap_xflags |= MAP_SYNC_FLAGS; + } offset = QEMU_ALIGN_UP((uintptr_t)ptr, align) - (uintptr_t)ptr; + retry_mmap_fd: ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE, MAP_FIXED | (fd == -1 ? MAP_ANONYMOUS : 0) | - (shared ? MAP_SHARED : MAP_PRIVATE), + (shared ? MAP_SHARED : MAP_PRIVATE) | mmap_xflags, fd, 0); - if (ptr1 == MAP_FAILED) { - munmap(ptr, total); - return MAP_FAILED; + if ((ptr1 == MAP_FAILED) && (mmap_xflags & MAP_SYNC_FLAGS)) { + mmap_xflags &= ~MAP_SYNC_FLAGS; + goto retry_mmap_fd; } if (offset > 0) {