From patchwork Fri Oct 9 15:17:20 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 528371 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:4830:134:3::10]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A25BF1406AA for ; Sat, 10 Oct 2015 05:06:22 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=pye/DG8J; dkim-atps=neutral Received: from lists.gnu.org ([208.118.235.17]:55845) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zkc3f-0004Gd-Rf for incoming@patchwork.ozlabs.org; Fri, 09 Oct 2015 14:06:20 -0400 Received: from localhost ([::1]:40907 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zkc3f-0001ZI-G9 for incoming@patchwork.ozlabs.org; Fri, 09 Oct 2015 14:06:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36312) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZkZQv-0001qv-Ty for qemu-devel@nongnu.org; Fri, 09 Oct 2015 11:18:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZkZQu-0007qT-RQ for qemu-devel@nongnu.org; Fri, 09 Oct 2015 11:18:09 -0400 Received: from mail-qg0-x231.google.com ([2607:f8b0:400d:c04::231]:35757) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZkZQu-0007qN-NJ for qemu-devel@nongnu.org; Fri, 09 Oct 2015 11:18:08 -0400 Received: by qgt47 with SMTP id 47so71191128qgt.2 for ; Fri, 09 Oct 2015 08:18:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=QMC9mIf8aN5O8wWGXk/K8EXjfjK8NIF5w8CAONaXnlE=; b=pye/DG8JKQ7JGka1vbhgSNP/pgxwpJYpzmaP8El6jlJXT+njcmOkO3h1M7Mnu3jMfI gDfKAUefEZyEpL8PNGMCw7iB6QbRrc/wuq8Mu7Xh2XVnGp9+v2NT44V783b9J86EFo3A 8BfVzNE0RtiyZMM086wuClQho2U6n0bmE6IbJbi3j2DauMX3oN39hvm7225o2/+UUPZ0 3bJ195z5BLQmStw/kB27bnuTvHB57EHBL7CjLFoyBLy2a1qwHkfAP+ZQbbPmrhYRWp6M TotEnEb+Q9YfwHVyaikWYcNwpEUONg1dzm+CS+SQNnY0soWEZ572MP1Jw2kC1S+POEZW eqvw== X-Received: by 10.140.29.164 with SMTP id b33mr16088226qgb.37.1444403888270; Fri, 09 Oct 2015 08:18:08 -0700 (PDT) Received: from localhost (bne75-h02-31-39-163-232.dsl.sta.abo.bbox.fr. [31.39.163.232]) by smtp.gmail.com with ESMTPSA id p1sm818826qkh.44.2015.10.09.08.18.06 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Oct 2015 08:18:06 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Fri, 9 Oct 2015 17:17:20 +0200 Message-Id: <1444403861-3240-7-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1444403861-3240-1-git-send-email-marcandre.lureau@redhat.com> References: <1444403861-3240-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::231 Cc: haifeng.lin@huawei.com, mst@redhat.com, thibaut.collet@6wind.com, jasowang@redhat.com, pbonzini@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Subject: [Qemu-devel] [PATCH v8 06/27] util: add memfd helpers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x From: Marc-André Lureau Add qemu_memfd_alloc/free() helpers. The function helps to allocate and seal shared memory. Signed-off-by: Marc-André Lureau --- include/qemu/memfd.h | 4 +++ util/memfd.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h index 8b1fe6a..950fb88 100644 --- a/include/qemu/memfd.h +++ b/include/qemu/memfd.h @@ -17,4 +17,8 @@ #define F_SEAL_WRITE 0x0008 /* prevent writes */ #endif +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); + #endif /* QEMU_MEMFD_H */ diff --git a/util/memfd.c b/util/memfd.c index c509692..c119483 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -27,6 +27,11 @@ #include "qemu/osdep.h" +#include +#include + +#include + #include "qemu/memfd.h" #ifdef CONFIG_MEMFD @@ -35,7 +40,7 @@ #include #include -inline static int memfd_create(const char *name, unsigned int flags) +static int memfd_create(const char *name, unsigned int flags) { #ifdef __NR_memfd_create return syscall(__NR_memfd_create, name, flags); @@ -52,3 +57,68 @@ inline static int memfd_create(const char *name, unsigned int flags) #ifndef MFD_ALLOW_SEALING #define MFD_ALLOW_SEALING 0x0002U #endif + +/* + * 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 = -1; + + *fd = -1; + +#ifdef CONFIG_LINUX + if (seals) { + mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC); + } + + if (mfd == -1) { + /* some systems have memfd without sealing */ + mfd = memfd_create(name, MFD_CLOEXEC); + seals = 0; + } +#endif + + if (mfd != -1) { + if (ftruncate(mfd, size) == -1) { + perror("ftruncate"); + close(mfd); + return NULL; + } + + if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) { + perror("fcntl"); + close(mfd); + return NULL; + } + } else { + perror("memfd"); + return NULL; + } + + ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); + if (ptr == MAP_FAILED) { + perror("mmap"); + close(mfd); + return NULL; + } + + *fd = mfd; + return ptr; +} + +void qemu_memfd_free(void *ptr, size_t size, int fd) +{ + if (ptr) { + munmap(ptr, size); + } + + if (fd != -1) { + close(fd); + } +}