From patchwork Wed Dec 27 06:56:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 10133455 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E44426037D for ; Wed, 27 Dec 2017 06:57:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CC43A2DA71 for ; Wed, 27 Dec 2017 06:57:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C00C12DA7F; Wed, 27 Dec 2017 06:57:54 +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=-6.9 required=2.0 tests=BAYES_00,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 93FBF2DA71 for ; Wed, 27 Dec 2017 06:57:53 +0000 (UTC) Received: from localhost ([::1]:48474 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eU5ey-0000nP-9I for patchwork-qemu-devel@patchwork.kernel.org; Wed, 27 Dec 2017 01:57:52 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34671) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eU5eJ-0000Pe-OD for qemu-devel@nongnu.org; Wed, 27 Dec 2017 01:57:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eU5eF-0002QG-3P for qemu-devel@nongnu.org; Wed, 27 Dec 2017 01:57:11 -0500 Received: from mga11.intel.com ([192.55.52.93]:40921) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eU5eE-0002Ng-Rb for qemu-devel@nongnu.org; Wed, 27 Dec 2017 01:57:07 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 26 Dec 2017 22:57:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,464,1508828400"; d="scan'208";a="6034794" Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.142]) by orsmga006.jf.intel.com with ESMTP; 26 Dec 2017 22:57:02 -0800 From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Wed, 27 Dec 2017 14:56:20 +0800 Message-Id: <20171227065620.20889-1-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.14.1 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] 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, Stefan Hajnoczi , Igor Mammedov , Dan Williams , Eduardo Habkost 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). By using MAP_SHARED_VALIDATE flag with MAP_SYNC, we can ensure mmap with MAP_SYNC fails if MAP_SYNC is not supported by the kernel or the backend file. On such failures, QEMU retries mmap without MAP_SYNC and MAP_SHARED_VALIDATE. Signed-off-by: Haozhong Zhang --- util/mmap-alloc.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 2fd8cbcc6f..37b302f057 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -18,7 +18,18 @@ #ifdef CONFIG_LINUX #include + +/* + * MAP_SHARED_VALIDATE and MAP_SYNC were introduced in 4.15 kernel, so + * they may not be defined when compiling on older kernels. + */ +#ifndef MAP_SHARED_VALIDATE +#define MAP_SHARED_VALIDATE 0x3 #endif +#ifndef MAP_SYNC +#define MAP_SYNC 0x80000 +#endif +#endif /* CONFIG_LINUX */ size_t qemu_fd_getpagesize(int fd) { @@ -97,6 +108,7 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) #endif size_t offset; void *ptr1; + int xflags = 0; if (ptr == MAP_FAILED) { return MAP_FAILED; @@ -107,12 +119,34 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) assert(align >= getpagesize()); offset = QEMU_ALIGN_UP((uintptr_t)ptr, align) - (uintptr_t)ptr; + +#if defined(__linux__) + /* + * If 'fd' refers to a file supporting DAX, mmap it with MAP_SYNC + * will guarantee the guest write persistence without other + * actions in QEMU (e.g., fsync() in QEMU). + * + * MAP_SHARED_VALIDATE ensures mmap with MAP_SYNC fails if + * MAP_SYNC is not supported by the kernel or the file. + * + * On failures of mmap with xflags, QEMU will retry mmap without + * xflags. + */ + xflags = shared ? (MAP_SHARED_VALIDATE | MAP_SYNC) : 0; +#endif + + 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) | xflags, fd, 0); if (ptr1 == MAP_FAILED) { + if (xflags) { + xflags = 0; + goto retry_mmap_fd; + } + munmap(ptr, total); return MAP_FAILED; }