From patchwork Wed Jan 31 06:02:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 10193279 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 3EC2C60380 for ; Wed, 31 Jan 2018 06:05:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D1212236A for ; Wed, 31 Jan 2018 06:05:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 20ABC27165; Wed, 31 Jan 2018 06:05:33 +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 5DDF62236A for ; Wed, 31 Jan 2018 06:05:32 +0000 (UTC) Received: from localhost ([::1]:37529 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eglWV-0008IL-3M for patchwork-qemu-devel@patchwork.kernel.org; Wed, 31 Jan 2018 01:05:31 -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 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False 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 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,438,1511856000"; d="scan'208";a="200149413" 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 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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, } area = qemu_ram_mmap(fd, memory, block->mr->align, - block->flags & RAM_SHARED); + (block->flags & RAM_SHARED) ? QEMU_RAM_SHARE : 0); if (area == 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); size_t qemu_mempath_getpagesize(const char *mem_path); -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 address; + * otherwise, the alignment in use will be determined by QEMU. + * @flags: specifies additional properties of the mapping, which can be one 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); void qemu_ram_munmap(void *ptr, size_t size); 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" #define HUGETLBFS_MAGIC 0x958458f6 @@ -73,7 +74,7 @@ size_t qemu_mempath_getpagesize(const char *mem_path) return getpagesize(); } -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 address @@ -90,11 +91,12 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) * anonymous memory is OK. */ int anonfd = fd == -1 || qemu_fd_getpagesize(fd) == getpagesize() ? -1 : fd; - int flags = anonfd == -1 ? MAP_ANONYMOUS : MAP_NORESERVE; - void *ptr = mmap(0, total, PROT_NONE, flags | MAP_PRIVATE, anonfd, 0); + int mmap_flags = anonfd == -1 ? MAP_ANONYMOUS : MAP_NORESERVE; + void *ptr = mmap(0, total, PROT_NONE, mmap_flags | MAP_PRIVATE, anonfd, 0); #else void *ptr = mmap(0, total, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); #endif + bool shared = flags & QEMU_RAM_SHARE; size_t offset; void *ptr1; 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 = QEMU_VMALLOC_ALIGN; - void *ptr = qemu_ram_mmap(-1, size, align, false); + void *ptr = qemu_ram_mmap(-1, size, align, 0); if (ptr == MAP_FAILED) { return NULL;