From nobody Mon Feb 9 13:44:34 2026 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.zoho.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 1496412871865387.005668343003; Fri, 2 Jun 2017 07:14:31 -0700 (PDT) Received: from localhost ([::1]:49968 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnLQ-0000Mo-Ay for importer@patchew.org; Fri, 02 Jun 2017 10:14:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50750) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnJl-0007AS-Bo for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:12:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGnJk-00034w-Fb for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:12:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58464) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGnJk-00034S-9k for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:12:44 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4CB98142A61; Fri, 2 Jun 2017 14:12:43 +0000 (UTC) Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id D32CE7FEA0; Fri, 2 Jun 2017 14:12:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4CB98142A61 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 4CB98142A61 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 18:12:21 +0400 Message-Id: <20170602141229.15326-2-marcandre.lureau@redhat.com> In-Reply-To: <20170602141229.15326-1-marcandre.lureau@redhat.com> References: <20170602141229.15326-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 02 Jun 2017 14:12:43 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 1/9] exec: check kvm mmu notifiers earlier 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: ehabkost@redhat.com, Peter Crosthwaite , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , imammedo@redhat.com, Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Move kvm mmu notifiers check before calling file_ram_alloc(), with the other xen precondition. (file_ram_alloc() will be reused in other cases than -mem-path). Signed-off-by: Marc-Andr=C3=A9 Lureau --- exec.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exec.c b/exec.c index b1db12fe36..9c6c3cfe87 100644 --- a/exec.c +++ b/exec.c @@ -1496,12 +1496,6 @@ static void *file_ram_alloc(RAMBlock *block, int fd =3D -1; int64_t file_size; =20 - if (kvm_enabled() && !kvm_has_sync_mmu()) { - error_setg(errp, - "host lacks kvm mmu notifiers, -mem-path unsupported"); - return NULL; - } - for (;;) { fd =3D open(path, O_RDWR); if (fd >=3D 0) { @@ -1944,6 +1938,12 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, = MemoryRegion *mr, return NULL; } =20 + if (kvm_enabled() && !kvm_has_sync_mmu()) { + error_setg(errp, + "host lacks kvm mmu notifiers, -mem-path unsupported"); + return NULL; + } + if (phys_mem_alloc !=3D qemu_anon_ram_alloc) { /* * file_ram_alloc() needs to allocate just like --=20 2.13.0.91.g00982b8dd From nobody Mon Feb 9 13:44:34 2026 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.zoho.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 1496412880786727.8455279588678; Fri, 2 Jun 2017 07:14:40 -0700 (PDT) Received: from localhost ([::1]:49969 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnLa-0000W0-0n for importer@patchew.org; Fri, 02 Jun 2017 10:14:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50786) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnJt-0007Gd-3m for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:12:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGnJr-0003Bn-Rq for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:12:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58794) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGnJr-0003Au-Iz for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:12:51 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AAD7A142A6D; Fri, 2 Jun 2017 14:12:50 +0000 (UTC) Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id A71357D551; Fri, 2 Jun 2017 14:12:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com AAD7A142A6D Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com AAD7A142A6D From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 18:12:22 +0400 Message-Id: <20170602141229.15326-3-marcandre.lureau@redhat.com> In-Reply-To: <20170602141229.15326-1-marcandre.lureau@redhat.com> References: <20170602141229.15326-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 02 Jun 2017 14:12:50 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 2/9] exec: split file_ram_alloc() 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: ehabkost@redhat.com, Peter Crosthwaite , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , imammedo@redhat.com, Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Move file opening part in a seperate function, file_ram_open(). This allows for reuse of file_ram_alloc() with a given fd. Signed-off-by: Marc-Andr=C3=A9 Lureau --- exec.c | 83 +++++++++++++++++++++++++++++++++++++-------------------------= ---- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/exec.c b/exec.c index 9c6c3cfe87..97e01d75f0 100644 --- a/exec.c +++ b/exec.c @@ -1483,19 +1483,17 @@ static int64_t get_file_size(int fd) return size; } =20 -static void *file_ram_alloc(RAMBlock *block, - ram_addr_t memory, - const char *path, - Error **errp) +static int file_ram_open(const char *path, + const char *region_name, + bool *created, + Error **errp) { - bool unlink_on_error =3D false; char *filename; char *sanitized_name; char *c; - void *area =3D MAP_FAILED; int fd =3D -1; - int64_t file_size; =20 + *created =3D false; for (;;) { fd =3D open(path, O_RDWR); if (fd >=3D 0) { @@ -1506,13 +1504,13 @@ static void *file_ram_alloc(RAMBlock *block, /* @path names a file that doesn't exist, create it */ fd =3D open(path, O_RDWR | O_CREAT | O_EXCL, 0644); if (fd >=3D 0) { - unlink_on_error =3D true; + *created =3D true; break; } } else if (errno =3D=3D EISDIR) { /* @path names a directory, create a file there */ /* Make name safe to use with mkstemp by replacing '/' with '_= '. */ - sanitized_name =3D g_strdup(memory_region_name(block->mr)); + sanitized_name =3D g_strdup(region_name); for (c =3D sanitized_name; *c !=3D '\0'; c++) { if (*c =3D=3D '/') { *c =3D '_'; @@ -1535,7 +1533,7 @@ static void *file_ram_alloc(RAMBlock *block, error_setg_errno(errp, errno, "can't open backing store %s for guest RAM", path); - goto error; + return -1; } /* * Try again on EINTR and EEXIST. The latter happens when @@ -1543,6 +1541,17 @@ static void *file_ram_alloc(RAMBlock *block, */ } =20 + return fd; +} + +static void *file_ram_alloc(RAMBlock *block, + ram_addr_t memory, + int fd, + bool truncate, + Error **errp) +{ + void *area; + block->page_size =3D qemu_fd_getpagesize(fd); block->mr->align =3D block->page_size; #if defined(__s390x__) @@ -1551,20 +1560,11 @@ static void *file_ram_alloc(RAMBlock *block, } #endif =20 - file_size =3D get_file_size(fd); - if (memory < block->page_size) { error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to " "or larger than page size 0x%zx", memory, block->page_size); - goto error; - } - - if (file_size > 0 && file_size < memory) { - error_setg(errp, "backing store %s size 0x%" PRIx64 - " does not match 'size' option 0x" RAM_ADDR_FMT, - path, file_size, memory); - goto error; + return NULL; } =20 memory =3D ROUND_UP(memory, block->page_size); @@ -1583,7 +1583,7 @@ static void *file_ram_alloc(RAMBlock *block, * those labels. Therefore, extending the non-empty backend file * is disabled as well. */ - if (!file_size && ftruncate(fd, memory)) { + if (truncate && ftruncate(fd, memory)) { perror("ftruncate"); } =20 @@ -1592,30 +1592,19 @@ static void *file_ram_alloc(RAMBlock *block, if (area =3D=3D MAP_FAILED) { error_setg_errno(errp, errno, "unable to map backing store for guest RAM"); - goto error; + return NULL; } =20 if (mem_prealloc) { os_mem_prealloc(fd, area, memory, smp_cpus, errp); if (errp && *errp) { - goto error; + qemu_ram_munmap(area, memory); + return NULL; } } =20 block->fd =3D fd; return area; - -error: - if (area !=3D MAP_FAILED) { - qemu_ram_munmap(area, memory); - } - if (unlink_on_error) { - unlink(path); - } - if (fd !=3D -1) { - close(fd); - } - return NULL; } #endif =20 @@ -1932,6 +1921,9 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, M= emoryRegion *mr, { RAMBlock *new_block; Error *local_err =3D NULL; + int fd; + bool created; + int64_t file_size; =20 if (xen_enabled()) { error_setg(errp, "-mem-path not supported with Xen"); @@ -1955,15 +1947,32 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size,= MemoryRegion *mr, return NULL; } =20 + fd =3D file_ram_open(mem_path, memory_region_name(mr), &created, errp); + if (fd < 0) { + return NULL; + } + size =3D HOST_PAGE_ALIGN(size); + file_size =3D get_file_size(fd); + if (file_size > 0 && file_size < size) { + error_setg(errp, "backing store %s size 0x%" PRIx64 + " does not match 'size' option 0x" RAM_ADDR_FMT, + mem_path, file_size, size); + close(fd); + return NULL; + } + new_block =3D g_malloc0(sizeof(*new_block)); 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, - mem_path, errp); + new_block->host =3D file_ram_alloc(new_block, size, fd, !file_size, er= rp); if (!new_block->host) { + if (created) { + unlink(mem_path); + } + close(fd); g_free(new_block); return NULL; } --=20 2.13.0.91.g00982b8dd From nobody Mon Feb 9 13:44:34 2026 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.zoho.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 1496413095282493.395958455318; Fri, 2 Jun 2017 07:18:15 -0700 (PDT) Received: from localhost ([::1]:49989 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnP3-0003xs-Ul for importer@patchew.org; Fri, 02 Jun 2017 10:18:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50839) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnK2-0007P1-AJ for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGnJz-0003KU-2h for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59108) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGnJy-0003Io-Pd for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:12:58 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C6BF461D22; Fri, 2 Jun 2017 14:12:57 +0000 (UTC) Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB3A75C892; Fri, 2 Jun 2017 14:12:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C6BF461D22 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C6BF461D22 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 18:12:23 +0400 Message-Id: <20170602141229.15326-4-marcandre.lureau@redhat.com> In-Reply-To: <20170602141229.15326-1-marcandre.lureau@redhat.com> References: <20170602141229.15326-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 02 Jun 2017 14:12:57 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 3/9] exec: split qemu_ram_alloc_from_file() 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: ehabkost@redhat.com, Peter Crosthwaite , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , imammedo@redhat.com, Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add qemu_ram_alloc_from_fd(), which can be use to allocate ramblock from fd only. Signed-off-by: Marc-Andr=C3=A9 Lureau --- include/exec/ram_addr.h | 3 +++ exec.c | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 140efa840c..73d1bea8b6 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -65,6 +65,9 @@ unsigned long last_ram_page(void); RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, bool share, const char *mem_path, Error **errp); +RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, + bool share, 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/exec.c b/exec.c index 97e01d75f0..8cd3153450 100644 --- a/exec.c +++ b/exec.c @@ -1915,14 +1915,12 @@ static void ram_block_add(RAMBlock *new_block, Erro= r **errp) } =20 #ifdef __linux__ -RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, - bool share, const char *mem_path, - Error **errp) +RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, + bool share, int fd, + Error **errp) { RAMBlock *new_block; Error *local_err =3D NULL; - int fd; - bool created; int64_t file_size; =20 if (xen_enabled()) { @@ -1947,18 +1945,12 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size,= MemoryRegion *mr, return NULL; } =20 - fd =3D file_ram_open(mem_path, memory_region_name(mr), &created, errp); - if (fd < 0) { - return NULL; - } - size =3D HOST_PAGE_ALIGN(size); file_size =3D get_file_size(fd); if (file_size > 0 && file_size < size) { error_setg(errp, "backing store %s size 0x%" PRIx64 " does not match 'size' option 0x" RAM_ADDR_FMT, mem_path, file_size, size); - close(fd); return NULL; } =20 @@ -1969,10 +1961,6 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, = MemoryRegion *mr, new_block->flags =3D share ? RAM_SHARED : 0; new_block->host =3D file_ram_alloc(new_block, size, fd, !file_size, er= rp); if (!new_block->host) { - if (created) { - unlink(mem_path); - } - close(fd); g_free(new_block); return NULL; } @@ -1984,6 +1972,33 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, = MemoryRegion *mr, return NULL; } return new_block; + +} + + +RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, + bool share, const char *mem_path, + Error **errp) +{ + int fd; + bool created; + RAMBlock *block; + + fd =3D file_ram_open(mem_path, memory_region_name(mr), &created, errp); + if (fd < 0) { + return NULL; + } + + block =3D qemu_ram_alloc_from_fd(size, mr, share, fd, errp); + if (!block) { + if (created) { + unlink(mem_path); + } + close(fd); + return NULL; + } + + return block; } #endif =20 --=20 2.13.0.91.g00982b8dd From nobody Mon Feb 9 13:44:34 2026 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.zoho.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 1496412892856428.82670584591665; Fri, 2 Jun 2017 07:14:52 -0700 (PDT) Received: from localhost ([::1]:49971 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnLm-0000fx-Eg for importer@patchew.org; Fri, 02 Jun 2017 10:14:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50873) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnK6-0007WB-5F for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGnK5-0003UF-86 for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:06 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40316) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGnK5-0003TE-0K for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:05 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1F00523205E for ; Fri, 2 Jun 2017 14:13:04 +0000 (UTC) Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id CE4FC8441B; Fri, 2 Jun 2017 14:13:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1F00523205E Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1F00523205E From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 18:12:24 +0400 Message-Id: <20170602141229.15326-5-marcandre.lureau@redhat.com> In-Reply-To: <20170602141229.15326-1-marcandre.lureau@redhat.com> References: <20170602141229.15326-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 02 Jun 2017 14:13:04 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 4/9] Add memory_region_init_ram_from_fd() 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: imammedo@redhat.com, Paolo Bonzini , ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a new function to initialize a RAM memory region with a file descriptor to be mmap-ed. Signed-off-by: Marc-Andr=C3=A9 Lureau --- include/exec/memory.h | 20 ++++++++++++++++++++ memory.c | 16 ++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/exec/memory.h b/include/exec/memory.h index bfdc685f24..edf52d7f9e 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -458,6 +458,26 @@ void memory_region_init_ram_from_file(MemoryRegion *mr, bool share, const char *path, Error **errp); + +/** + * memory_region_init_ram_from_fd: Initialize RAM memory region with a + * mmap-ed backend. + * + * @mr: the #MemoryRegion to be initialized. + * @owner: the object that tracks the region's reference count + * @name: the name of the region. + * @size: size of the region. + * @share: %true if memory must be mmaped with the MAP_SHARED flag + * @fd: the fd to mmap. + * @errp: pointer to Error*, to store an error if it happens. + */ +void memory_region_init_ram_from_fd(MemoryRegion *mr, + struct Object *owner, + const char *name, + uint64_t size, + bool share, + int fd, + Error **errp); #endif =20 /** diff --git a/memory.c b/memory.c index 0ddc4cc28d..b2ace20bac 100644 --- a/memory.c +++ b/memory.c @@ -1397,6 +1397,22 @@ void memory_region_init_ram_from_file(MemoryRegion *= mr, mr->ram_block =3D qemu_ram_alloc_from_file(size, mr, share, path, errp= ); mr->dirty_log_mask =3D tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; } + +void memory_region_init_ram_from_fd(MemoryRegion *mr, + struct Object *owner, + const char *name, + uint64_t size, + bool share, + int fd, + Error **errp) +{ + memory_region_init(mr, owner, name, size); + 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->dirty_log_mask =3D tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; +} #endif =20 void memory_region_init_ram_ptr(MemoryRegion *mr, --=20 2.13.0.91.g00982b8dd From nobody Mon Feb 9 13:44:34 2026 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.zoho.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 1496413067637235.59254085430268; Fri, 2 Jun 2017 07:17:47 -0700 (PDT) Received: from localhost ([::1]:49988 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnOc-0003Fp-53 for importer@patchew.org; Fri, 02 Jun 2017 10:17:46 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50987) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnKG-0007nW-Ee for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGnKD-0003g3-6d for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54790) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGnKD-0003eb-0j for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:13 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 26EB783F45 for ; Fri, 2 Jun 2017 14:13:12 +0000 (UTC) Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 15C167FEB3; Fri, 2 Jun 2017 14:13:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 26EB783F45 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 26EB783F45 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 18:12:25 +0400 Message-Id: <20170602141229.15326-6-marcandre.lureau@redhat.com> In-Reply-To: <20170602141229.15326-1-marcandre.lureau@redhat.com> References: <20170602141229.15326-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 02 Jun 2017 14:13:12 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 5/9] ivshmem: use ram_from_fd() 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: imammedo@redhat.com, ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Instead of having its own mmap handling code, reuse the code from exec.c. Note: memory_region_init_ram_from_fd() adds some restrictions (check for xen, kvm sync-mmu, etc) and changes (such as size alignment). This may actually be more correct. Signed-off-by: Marc-Andr=C3=A9 Lureau --- hw/misc/ivshmem.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 475e36a4c7..c2f15907c1 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -491,9 +491,9 @@ static void setup_interrupt(IVShmemState *s, int vector= , Error **errp) =20 static void process_msg_shmem(IVShmemState *s, int fd, Error **errp) { + Error *local_err =3D NULL; struct stat buf; size_t size; - void *ptr; =20 if (s->ivshmem_bar2) { error_setg(errp, "server sent unexpected shared memory message"); @@ -522,15 +522,13 @@ static void process_msg_shmem(IVShmemState *s, int fd= , Error **errp) } =20 /* mmap the region and map into the BAR2 */ - ptr =3D mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (ptr =3D=3D MAP_FAILED) { - error_setg_errno(errp, errno, "Failed to mmap shared memory"); - close(fd); + memory_region_init_ram_from_fd(&s->server_bar2, OBJECT(s), + "ivshmem.bar2", size, true, fd, &local_= err); + if (local_err) { + error_propagate(errp, local_err); return; } - memory_region_init_ram_ptr(&s->server_bar2, OBJECT(s), - "ivshmem.bar2", size, ptr); - memory_region_set_fd(&s->server_bar2, fd); + s->ivshmem_bar2 =3D &s->server_bar2; } =20 --=20 2.13.0.91.g00982b8dd From nobody Mon Feb 9 13:44:34 2026 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.zoho.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 1496413193332999.5728914469893; Fri, 2 Jun 2017 07:19:53 -0700 (PDT) Received: from localhost ([::1]:49994 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnQc-00059j-Vl for importer@patchew.org; Fri, 02 Jun 2017 10:19:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51010) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnKL-0007vp-Bw for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGnKK-0003q9-EL for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:6302) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGnKK-0003oR-7o for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:20 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5CFCBC0587C3 for ; Fri, 2 Jun 2017 14:13:19 +0000 (UTC) Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3369B5C892; Fri, 2 Jun 2017 14:13:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5CFCBC0587C3 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 5CFCBC0587C3 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 18:12:26 +0400 Message-Id: <20170602141229.15326-7-marcandre.lureau@redhat.com> In-Reply-To: <20170602141229.15326-1-marcandre.lureau@redhat.com> References: <20170602141229.15326-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 02 Jun 2017 14:13:19 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 6/9] memory: remove memory_region_set_fd 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: imammedo@redhat.com, Paolo Bonzini , ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Now unnecessary since ivshmem uses memory_region_init_ram_from_fd. Signed-off-by: Marc-Andr=C3=A9 Lureau --- include/exec/memory.h | 11 ----------- memory.c | 10 ---------- 2 files changed, 21 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index edf52d7f9e..0ba6ea2b18 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -827,17 +827,6 @@ static inline bool memory_region_is_rom(MemoryRegion *= mr) int memory_region_get_fd(MemoryRegion *mr); =20 /** - * memory_region_set_fd: Mark a RAM memory region as backed by a - * file descriptor. - * - * This function is typically used after memory_region_init_ram_ptr(). - * - * @mr: the memory region being queried. - * @fd: the file descriptor that backs @mr. - */ -void memory_region_set_fd(MemoryRegion *mr, int fd); - -/** * memory_region_from_host: Convert a pointer into a RAM memory region * and an offset within it. * diff --git a/memory.c b/memory.c index b2ace20bac..e08fa0ae6c 100644 --- a/memory.c +++ b/memory.c @@ -1851,16 +1851,6 @@ int memory_region_get_fd(MemoryRegion *mr) return fd; } =20 -void memory_region_set_fd(MemoryRegion *mr, int fd) -{ - rcu_read_lock(); - while (mr->alias) { - mr =3D mr->alias; - } - mr->ram_block->fd =3D fd; - rcu_read_unlock(); -} - void *memory_region_get_ram_ptr(MemoryRegion *mr) { void *ptr; --=20 2.13.0.91.g00982b8dd From nobody Mon Feb 9 13:44:34 2026 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.zoho.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 1496413046796712.1305849829706; Fri, 2 Jun 2017 07:17:26 -0700 (PDT) Received: from localhost ([::1]:49987 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnOH-0002o3-Br for importer@patchew.org; Fri, 02 Jun 2017 10:17:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51100) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnKV-0008IC-OT for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGnKS-0003yN-I0 for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34696) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGnKS-0003xw-9K for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:28 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 647B27D0D4 for ; Fri, 2 Jun 2017 14:13:27 +0000 (UTC) Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 42DA691026; Fri, 2 Jun 2017 14:13:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 647B27D0D4 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 647B27D0D4 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 18:12:27 +0400 Message-Id: <20170602141229.15326-8-marcandre.lureau@redhat.com> In-Reply-To: <20170602141229.15326-1-marcandre.lureau@redhat.com> References: <20170602141229.15326-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 02 Jun 2017 14:13:27 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 7/9] memfd: split qemu_memfd_alloc() 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: imammedo@redhat.com, ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a function to only create a memfd, without mmap. The function is used in the following memory backend. Signed-off-by: Marc-Andr=C3=A9 Lureau --- include/qemu/memfd.h | 2 ++ util/memfd.c | 42 +++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h index 745a8c501e..30c1ab1d91 100644 --- a/include/qemu/memfd.h +++ b/include/qemu/memfd.h @@ -16,6 +16,8 @@ #define F_SEAL_WRITE 0x0008 /* prevent writes */ #endif =20 +int qemu_memfd_create(const char *name, size_t size, unsigned int seals, + bool must_seal); void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, int *fd); void qemu_memfd_free(void *ptr, size_t size, int fd); diff --git a/util/memfd.c b/util/memfd.c index 4571d1aba8..e6476df083 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -55,45 +55,53 @@ static int memfd_create(const char *name, unsigned int = flags) #define MFD_ALLOW_SEALING 0x0002U #endif =20 -/* - * This is a best-effort helper for shared memory allocation, with - * optional sealing. The helper will do his best to allocate using - * memfd with sealing, but may fallback on other methods without - * sealing. - */ -void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, - int *fd) +int qemu_memfd_create(const char *name, size_t size, unsigned int seals, + bool must_seal) { - void *ptr; int mfd =3D -1; =20 - *fd =3D -1; - #ifdef CONFIG_LINUX if (seals) { mfd =3D memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC); } =20 - if (mfd =3D=3D -1) { + if (mfd =3D=3D -1 && !must_seal) { /* some systems have memfd without sealing */ mfd =3D memfd_create(name, MFD_CLOEXEC); seals =3D 0; } -#endif =20 - if (mfd !=3D -1) { + if (mfd >=3D 0) { if (ftruncate(mfd, size) =3D=3D -1) { perror("ftruncate"); close(mfd); - return NULL; + return -1; } =20 if (seals && fcntl(mfd, F_ADD_SEALS, seals) =3D=3D -1) { perror("fcntl"); close(mfd); - return NULL; + return -1; } - } else { + } +#endif + + return mfd; +} + +/* + * This is a best-effort helper for shared memory allocation, with + * optional sealing. The helper will do his best to allocate using + * memfd with sealing, but may fallback on other methods without + * sealing. + */ +void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, + int *fd) +{ + void *ptr; + int mfd =3D qemu_memfd_create(name, size, seals, false); + + if (mfd =3D=3D -1) { const char *tmpdir =3D g_get_tmp_dir(); gchar *fname; =20 --=20 2.13.0.91.g00982b8dd From nobody Mon Feb 9 13:44:34 2026 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.zoho.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 1496413014513657.6868854041223; Fri, 2 Jun 2017 07:16:54 -0700 (PDT) Received: from localhost ([::1]:49985 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnNj-0002Ns-Ut for importer@patchew.org; Fri, 02 Jun 2017 10:16:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51163) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnKa-0008Me-5I for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGnKY-00045N-Vr for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33454) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGnKY-000452-Ms for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:34 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C944B61D22 for ; Fri, 2 Jun 2017 14:13:33 +0000 (UTC) Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id B06C64DA70; Fri, 2 Jun 2017 14:13:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C944B61D22 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C944B61D22 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 18:12:28 +0400 Message-Id: <20170602141229.15326-9-marcandre.lureau@redhat.com> In-Reply-To: <20170602141229.15326-1-marcandre.lureau@redhat.com> References: <20170602141229.15326-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 02 Jun 2017 14:13:33 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 8/9] Add memfd based hostmem 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: imammedo@redhat.com, ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a new memory backend, similar to hostmem-file, except that it doesn't need to create files. It also enforces memory sealing. This backend is mainly useful for sharing the memory with other processes. Note that Linux supports transparent huge-pages of shmem/memfd memory since 4.8. It is relatively easier to set up THP than a dedicate hugepage mount point by using "madvise" in /sys/kernel/mm/transparent_hugepage/shmem_enabled. Usage: -object memory-backend-memfd,id=3Dmem1,size=3D1G Signed-off-by: Marc-Andr=C3=A9 Lureau --- backends/hostmem-memfd.c | 67 ++++++++++++++++++++++++++++++++++++++++++++= ++++ backends/Makefile.objs | 2 ++ qemu-options.hx | 11 ++++++++ 3 files changed, 80 insertions(+) create mode 100644 backends/hostmem-memfd.c diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c new file mode 100644 index 0000000000..13d300d9ad --- /dev/null +++ b/backends/hostmem-memfd.c @@ -0,0 +1,67 @@ +/* + * QEMU host memfd memory backend + * + * Copyright (C) 2016 Red Hat Inc + * + * Authors: + * Marc-Andr=C3=A9 Lureau + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "sysemu/hostmem.h" +#include "sysemu/sysemu.h" +#include "qom/object_interfaces.h" +#include "qemu/memfd.h" +#include "qapi/error.h" + +#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" + +static void +memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) +{ + int fd; + + if (!backend->size) { + error_setg(errp, "can't create backend with size 0"); + return; + } + + if (!memory_region_size(&backend->mr)) { + backend->force_prealloc =3D mem_prealloc; + fd =3D qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, + backend->size, + F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL, + true); + if (fd =3D=3D -1) { + error_setg(errp, "can't allocate memfd backend"); + return; + } + memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), + object_get_canonical_path(OBJECT(backend)), + backend->size, true, fd, errp); + } +} + +static void +memfd_backend_class_init(ObjectClass *oc, void *data) +{ + HostMemoryBackendClass *bc =3D MEMORY_BACKEND_CLASS(oc); + + bc->alloc =3D memfd_backend_memory_alloc; +} + +static const TypeInfo memfd_backend_info =3D { + .name =3D TYPE_MEMORY_BACKEND_MEMFD, + .parent =3D TYPE_MEMORY_BACKEND, + .class_init =3D memfd_backend_class_init, +}; + +static void register_types(void) +{ + type_register_static(&memfd_backend_info); +} + +type_init(register_types); diff --git a/backends/Makefile.objs b/backends/Makefile.objs index 0e0f1567b2..63115e3192 100644 --- a/backends/Makefile.objs +++ b/backends/Makefile.objs @@ -12,3 +12,5 @@ common-obj-$(CONFIG_LINUX) +=3D hostmem-file.o =20 common-obj-y +=3D cryptodev.o common-obj-y +=3D cryptodev-builtin.o + +common-obj-$(CONFIG_LINUX) +=3D hostmem-memfd.o diff --git a/qemu-options.hx b/qemu-options.hx index a6c9b9e763..bfc4aff404 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3974,6 +3974,17 @@ The @option{share} boolean option determines whether= the memory region is marked as private to QEMU, or shared. The latter allows a co-operating external process to access the QEMU memory region. =20 +@item -object memory-backend-memfd,id=3D@var{id},size=3D@var{size} + +Creates an anonymous memory file backend object, which can be used to +share the memory with a co-operating external process. The memory is +allocated with memfd and sealing. (Linux only) + +The @option{id} parameter is a unique ID that will be used to +reference this memory region when configuring the @option{-numa} +argument. The @option{size} option provides the size of the memory +region, and accepts common suffixes, eg @option{500M}. + @item -object rng-random,id=3D@var{id},filename=3D@var{/dev/random} =20 Creates a random number generator backend which obtains entropy from --=20 2.13.0.91.g00982b8dd From nobody Mon Feb 9 13:44:34 2026 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.zoho.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 1496413331134859.2377370594251; Fri, 2 Jun 2017 07:22:11 -0700 (PDT) Received: from localhost ([::1]:50009 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnSo-0006tp-Iy for importer@patchew.org; Fri, 02 Jun 2017 10:22:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51242) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnKj-0008Uw-Ck for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGnKg-0004Db-4P for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33674) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dGnKf-0004CH-S3 for qemu-devel@nongnu.org; Fri, 02 Jun 2017 10:13:42 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EDBD0142A70 for ; Fri, 2 Jun 2017 14:13:40 +0000 (UTC) Received: from localhost (ovpn-112-28.ams2.redhat.com [10.36.112.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id E0CB47D551; Fri, 2 Jun 2017 14:13:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com EDBD0142A70 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com EDBD0142A70 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 2 Jun 2017 18:12:29 +0400 Message-Id: <20170602141229.15326-10-marcandre.lureau@redhat.com> In-Reply-To: <20170602141229.15326-1-marcandre.lureau@redhat.com> References: <20170602141229.15326-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 02 Jun 2017 14:13:41 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 9/9] tests: use memfd in vhost-user-test 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: imammedo@redhat.com, ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This will exercise the memfd memory backend and should generally be better for testing than memory-backend-file (thanks to anonymous files and sealing). Signed-off-by: Marc-Andr=C3=A9 Lureau --- tests/vhost-user-test.c | 68 ++++++++++++++++++++++++++++++++-------------= ---- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index 9095af267e..6924716a30 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -16,6 +16,7 @@ #include "qemu/option.h" #include "qemu/range.h" #include "qemu/sockets.h" +#include "qemu/memfd.h" #include "sysemu/char.h" #include "sysemu/sysemu.h" #include "libqos/libqos.h" @@ -40,15 +41,14 @@ #define HAVE_MONOTONIC_TIME #endif =20 -#define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=3Dmem,size= =3D%dM,"\ +#define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=3Dmem,size= =3D%dM," \ "mem-path=3D%s,share=3Don -numa node,memdev=3Dmem" +#define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=3Dmem,size= =3D%dM," \ + " -numa node,memdev=3Dmem" #define QEMU_CMD_CHR " -chardev socket,id=3D%s,path=3D%s%s" #define QEMU_CMD_NETDEV " -netdev vhost-user,id=3Dnet0,chardev=3D%s,vhostf= orce" #define QEMU_CMD_NET " -device virtio-net-pci,netdev=3Dnet0" =20 -#define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \ - QEMU_CMD_NETDEV QEMU_CMD_NET - #define HUGETLBFS_MAGIC 0x958458f6 =20 /*********** FROM hw/virtio/vhost-user.c *********************************= ****/ @@ -159,6 +159,22 @@ typedef struct TestServer { static const char *tmpfs; static const char *root; =20 +static char *get_qemu_cmd(TestServer *s, int mem, const char *mem_path, + const char *chr_opts, const char *extra) +{ + if (qemu_memfd_check()) { + return g_strdup_printf(QEMU_CMD_MEMFD QEMU_CMD_CHR + QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem, + s->chr_name, s->socket_path, + chr_opts, s->chr_name, extra); + } else { + return g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR + QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem, + mem_path, s->chr_name, s->socket_path, + chr_opts, s->chr_name, extra); + } +} + static void init_virtio_dev(TestServer *s) { QVirtioPCIDevice *dev; @@ -477,14 +493,6 @@ static inline void test_server_connect(TestServer *ser= ver) test_server_create_chr(server, ",reconnect=3D1"); } =20 -#define GET_QEMU_CMD(s) \ - g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \ - (s)->socket_path, "", (s)->chr_name) - -#define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \ - g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \ - (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_AR= GS__) - static gboolean _test_server_free(TestServer *server) { int i; @@ -630,7 +638,7 @@ static void test_migrate(void) char *uri =3D g_strdup_printf("%s%s", "unix:", dest->mig_path); QTestState *global =3D global_qtest, *from, *to; GSource *source; - gchar *cmd; + gchar *cmd, *tmp; QDict *rsp; guint8 *log; guint64 size; @@ -638,7 +646,7 @@ static void test_migrate(void) test_server_listen(s); test_server_listen(dest); =20 - cmd =3D GET_QEMU_CMDE(s, 2, "", ""); + cmd =3D get_qemu_cmd(s, 2, root, "", ""); from =3D qtest_start(cmd); g_free(cmd); =20 @@ -647,7 +655,9 @@ static void test_migrate(void) size =3D get_log_size(s); g_assert_cmpint(size, =3D=3D, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8)= ); =20 - cmd =3D GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri); + tmp =3D g_strdup_printf(" -incoming %s", uri); + cmd =3D get_qemu_cmd(dest, 2, root, "", tmp); + g_free(tmp); to =3D qtest_init(cmd); g_free(cmd); =20 @@ -753,7 +763,7 @@ static void test_reconnect_subprocess(void) char *cmd; =20 g_thread_new("connect", connect_thread, s); - cmd =3D GET_QEMU_CMDE(s, 2, ",server", ""); + cmd =3D get_qemu_cmd(s, 2, root, ",server", ""); qtest_start(cmd); g_free(cmd); =20 @@ -789,7 +799,7 @@ static void test_connect_fail_subprocess(void) =20 s->test_fail =3D true; g_thread_new("connect", connect_thread, s); - cmd =3D GET_QEMU_CMDE(s, 2, ",server", ""); + cmd =3D get_qemu_cmd(s, 2, root, ",server", ""); qtest_start(cmd); g_free(cmd); =20 @@ -817,7 +827,7 @@ static void test_flags_mismatch_subprocess(void) =20 s->test_flags =3D TEST_FLAGS_DISCONNECT; g_thread_new("connect", connect_thread, s); - cmd =3D GET_QEMU_CMDE(s, 2, ",server", ""); + cmd =3D get_qemu_cmd(s, 2, root, ",server", ""); qtest_start(cmd); g_free(cmd); =20 @@ -885,11 +895,21 @@ static void test_multiqueue(void) s->queues =3D queues; test_server_listen(s); =20 - cmd =3D g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",qu= eues=3D%d " - "-device virtio-net-pci,netdev=3Dnet0,mq=3Don,ve= ctors=3D%d", - 512, 512, root, s->chr_name, - s->socket_path, "", s->chr_name, - queues, queues * 2 + 2); + if (qemu_memfd_check()) { + cmd =3D g_strdup_printf( + QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=3D%d " + "-device virtio-net-pci,netdev=3Dnet0,mq=3Don,vectors=3D%d", + 512, 512, s->chr_name, + s->socket_path, "", s->chr_name, + queues, queues * 2 + 2); + } else { + cmd =3D g_strdup_printf( + QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=3D%d " + "-device virtio-net-pci,netdev=3Dnet0,mq=3Don,vectors=3D%d", + 512, 512, root, s->chr_name, + s->socket_path, "", s->chr_name, + queues, queues * 2 + 2); + } qtest_start(cmd); g_free(cmd); =20 @@ -955,7 +975,7 @@ int main(int argc, char **argv) /* run the main loop thread so the chardev may operate */ thread =3D g_thread_new(NULL, thread_function, loop); =20 - qemu_cmd =3D GET_QEMU_CMD(server); + qemu_cmd =3D get_qemu_cmd(server, 512, root, "", ""); =20 s =3D qtest_start(qemu_cmd); g_free(qemu_cmd); --=20 2.13.0.91.g00982b8dd