From patchwork Thu Sep 24 16:22:04 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: 522416 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 6CC1614010F for ; Fri, 25 Sep 2015 02:24:34 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=X8TZk+gN; dkim-atps=neutral Received: from localhost ([::1]:45857 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zf9Jv-0003Ni-SZ for incoming@patchwork.ozlabs.org; Thu, 24 Sep 2015 12:24:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43915) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zf9I6-0000Ta-NW for qemu-devel@nongnu.org; Thu, 24 Sep 2015 12:22:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zf9I2-00057A-Ir for qemu-devel@nongnu.org; Thu, 24 Sep 2015 12:22:38 -0400 Received: from mail-qg0-x232.google.com ([2607:f8b0:400d:c04::232]:34718) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zf9I2-000576-F2 for qemu-devel@nongnu.org; Thu, 24 Sep 2015 12:22:34 -0400 Received: by qgez77 with SMTP id z77so48700697qge.1 for ; Thu, 24 Sep 2015 09:22:34 -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=Z41qHt07QqIcZKwDEF35RUghBndFgpBrBB9UH8PzJvA=; b=X8TZk+gNVWAiPQhDn8+warC/8gG8rH3+nkmkHMkv+5H7NtAnUXyfUTz3l7EQVONy4X pfytXjFJyXq8DWNihcv9Q56F4khYe3yQ7rYPuQaqUvtzeZ0LyJiJwNbvnvAi5/7iox35 OA/gSM/PIC3VX5diJAQXotkXM1FQnLSGWz9KWprRfQyNtypN8gVypcioeqnu4bxnnY7u 9HDiEnvBz9hwrd1X+T6sWESxv4XhC2mGWm9MKLGffT6eGpl2lArbks0yxju5oeA7HWW4 5zJhgFr4SqwxejoHJUesgKQfDARGHaZfRGY2IS8iCTfBeLM8kvGoi+/b4bDLKcnLXeJM ptaQ== X-Received: by 10.140.83.202 with SMTP id j68mr870214qgd.46.1443111754177; Thu, 24 Sep 2015 09:22:34 -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 n82sm4771766qkh.31.2015.09.24.09.22.32 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 Sep 2015 09:22:33 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Thu, 24 Sep 2015 18:22:04 +0200 Message-Id: <1443111741-4736-5-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1443111741-4736-1-git-send-email-marcandre.lureau@redhat.com> References: <1443111741-4736-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::232 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 v5 04/21] 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 From: Marc-André Lureau Add qemu_memfd_alloc/free() helpers. The function helps to allocate and seal a memfd, and implements an open/unlink/mmap fallback for system that do not support memfd. Signed-off-by: Marc-André Lureau --- include/qemu/memfd.h | 4 +++ util/memfd.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 2 deletions(-) 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 a98d57e..8b2b785 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -27,6 +27,14 @@ #include "config-host.h" +#include +#include + +#include +#include +#include +#include + #include "qemu/memfd.h" #ifdef CONFIG_MEMFD @@ -44,13 +52,75 @@ #define MFD_ALLOW_SEALING 0x0002U #endif -static inline int memfd_create(const char *name, unsigned int flags) +static int memfd_create(const char *name, unsigned int flags) { return syscall(__NR_memfd_create, name, flags); } #else /* !LINUX */ -static inline int memfd_create(const char *name, unsigned int flags) +static int memfd_create(const char *name, unsigned int flags) { return -1; } #endif + +void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, + int *fd) +{ + void *ptr; + int mfd; + + mfd = memfd_create(name, MFD_ALLOW_SEALING|MFD_CLOEXEC); + if (mfd != -1) { + if (ftruncate(mfd, size) == -1) { + perror("ftruncate"); + close(mfd); + return NULL; + } + + if (fcntl(mfd, F_ADD_SEALS, seals) == -1) { + perror("fcntl"); + close(mfd); + return NULL; + } + } else { + const char *tmpdir = getenv("TMPDIR"); + gchar *fname; + + tmpdir = tmpdir ? tmpdir : "/tmp"; + + fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir); + mfd = mkstemp(fname); + unlink(fname); + g_free(fname); + + if (mfd == -1) { + perror("mkstemp"); + return NULL; + } + + if (ftruncate(mfd, size) == -1) { + perror("ftruncate"); + close(mfd); + 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); + } + + close(fd); +}