From patchwork Tue Apr 12 11:55:28 2016 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: 8809801 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 5D810C0553 for ; Tue, 12 Apr 2016 12:02:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 86C6720148 for ; Tue, 12 Apr 2016 12:02:17 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id 8C1C9200C1 for ; Tue, 12 Apr 2016 12:02:16 +0000 (UTC) Received: from localhost ([::1]:47978 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1apx1M-0001RW-1f for patchwork-qemu-devel@patchwork.kernel.org; Tue, 12 Apr 2016 08:02:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51416) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1apwvG-0004ue-5o for qemu-devel@nongnu.org; Tue, 12 Apr 2016 07:55:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1apwvE-0008Qf-UL for qemu-devel@nongnu.org; Tue, 12 Apr 2016 07:55:58 -0400 Received: from mail-qk0-x22b.google.com ([2607:f8b0:400d:c09::22b]:32955) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1apwvE-0008QX-M9 for qemu-devel@nongnu.org; Tue, 12 Apr 2016 07:55:56 -0400 Received: by mail-qk0-x22b.google.com with SMTP id k135so4913790qke.0 for ; Tue, 12 Apr 2016 04:55:56 -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-transfer-encoding; bh=E3aGjRDUdcqlO/FmUyrp6pkCwJt4jr3FBDnl523UCIE=; b=q9t1RfuO6dU3CbYGSDhtdQNPJB0Yx47xxdFycJ+g2LGk3fXaE8BkzVtQuPOtLaD9Rb MHYjSsCfcoWOspkDaHbIENGK5VRfRrvM7/jgTbwAm6jV03D6OuJ4g6l8hzkV7eoFXRMf h+k0oK+K6sTmXsGy0E2+U/phSg8ctJqjmy6Okww0LQqAoDpiv9E/v97OayPSu1tJZWx3 KHgC1hocSeYfH8mYcZZhH5ypYf/T0lthPavdffZ0V4fAIrKTdL9jqsBarPnMOstxNlud g3by+m+SIw4H+56ZjRU/T7D1Rd93/7FWi2Dvi1dOzDaeZrwmYIDab6ir/qu9DPz9+JMR +t1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=E3aGjRDUdcqlO/FmUyrp6pkCwJt4jr3FBDnl523UCIE=; b=Ckfi532CZj65eAzTFrbjjMRPwM6L6lzm+f6webBeAp0XEF36GSSMBLZlq8jkvTt5eM A7Nzx1MgnE5rbGY3wVXL45XQ55Xy0zPCa+2h0y6kvugxln9wo+hlOcYJ3+lglD/GmM7k REtX/7TX6ewXsB1G0yNkU16cYgw3n/hbAQoyFFJZUKaQg5x0O0v7gN4t2JWRjILmYzX7 2wc4s6auVW+Q5C9IBmPoR59JxmQxpPpFZIPGQQpsfJ85XrIX1pOoCjrbeVCdIXIKzmTT zlH84AD1/BrEdAqoelolMlCEvEPEroIW1XP3v13XxVR2HFKB+yTsXEoeGZ2eR56iZPUL lhJQ== X-Gm-Message-State: AOPr4FUsMn5vXKm8pMMfw4D1/k4H+UrLXKQSLhxU8rT2Ww4JtmYoIy7ALkZRc+RmkFsowA== X-Received: by 10.55.77.12 with SMTP id a12mr2742817qkb.44.1460462156040; Tue, 12 Apr 2016 04:55:56 -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 l33sm13402254qge.11.2016.04.12.04.55.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Apr 2016 04:55:55 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Tue, 12 Apr 2016 13:55:28 +0200 Message-Id: <1460462129-17363-9-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1460462129-17363-1-git-send-email-marcandre.lureau@redhat.com> References: <1460462129-17363-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400d:c09::22b Subject: [Qemu-devel] [PATCH 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: pbonzini@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Marc-André Lureau Add a new memory backend, similar to hostmem-file, except that it doesn't need a file path, or hugepages directory permissions. It also try to enforce memory sealing if available. This backend is mainly useful for easily sharing the memory with other processes, for example with vhost-user backend, when hugepages aren't available (for security reasons and other limitations). Usage: -object memory-backend-memfd,id=mem1,size=1G Signed-off-by: Marc-André Lureau --- backends/Makefile.objs | 1 + backends/hostmem-memfd.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 12 +++++++ 3 files changed, 98 insertions(+) create mode 100644 backends/hostmem-memfd.c diff --git a/backends/Makefile.objs b/backends/Makefile.objs index 31a3a89..32cfcdb 100644 --- a/backends/Makefile.objs +++ b/backends/Makefile.objs @@ -9,3 +9,4 @@ common-obj-$(CONFIG_TPM) += tpm.o common-obj-y += hostmem.o hostmem-ram.o common-obj-$(CONFIG_LINUX) += hostmem-file.o +common-obj-$(CONFIG_POSIX) += hostmem-memfd.o diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c new file mode 100644 index 0000000..e0e18d7 --- /dev/null +++ b/backends/hostmem-memfd.c @@ -0,0 +1,85 @@ +/* + * QEMU host memfd memory backend + * + * Copyright (C) 2016 Red Hat Inc + * + * Authors: + * Marc-André Lureau + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * 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" + +/** + * @TYPE_MEMORY_BACKEND_MEMFD: + * name of the memory backend that uses memfd mmap + */ +#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" + +#define MEMORY_BACKEND_MEMFD(obj) \ + OBJECT_CHECK(HostMemoryBackendMemfd, (obj), TYPE_MEMORY_BACKEND_MEMFD) + +typedef struct HostMemoryBackendMemfd HostMemoryBackendMemfd; + +struct HostMemoryBackendMemfd { + HostMemoryBackend parent_obj; + + int fd; +}; + +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; + } +#ifndef CONFIG_LINUX + error_setg(errp, "memfd not supported on this host"); +#else + if (!memory_region_size(&backend->mr)) { + backend->force_prealloc = mem_prealloc; + fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, + backend->size, + F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL); + if (fd == -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); + } +#endif +} + +static void +memfd_backend_class_init(ObjectClass *oc, void *data) +{ + HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); + + bc->alloc = memfd_backend_memory_alloc; +} + +static const TypeInfo memfd_backend_info = { + .name = TYPE_MEMORY_BACKEND_MEMFD, + .parent = TYPE_MEMORY_BACKEND, + .class_init = memfd_backend_class_init, + .instance_size = sizeof(HostMemoryBackendMemfd), +}; + +static void register_types(void) +{ + type_register_static(&memfd_backend_info); +} + +type_init(register_types); diff --git a/qemu-options.hx b/qemu-options.hx index 587de8f..a54af0f 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3756,6 +3756,18 @@ 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. +@item -object memory-backend-memfd,id=@var{id},size=@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 +created with memfd and sealing if possible, but has fallback mechanism +for systems that lack it. + +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=@var{id},filename=@var{/dev/random} Creates a random number generator backend which obtains entropy from