From nobody Wed Nov 5 18:23:04 2025 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