From patchwork Fri Jun 2 14:12:28 2017 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: 770361 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 3wfR850mfKz9s3T for ; Sat, 3 Jun 2017 00:15:49 +1000 (AEST) Received: from localhost ([::1]:49976 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGnMg-0001EE-Ph for incoming@patchwork.ozlabs.org; Fri, 02 Jun 2017 10:15:46 -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) 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 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) 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) 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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=mem1,size=1G Signed-off-by: Marc-André 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é 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" + +#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 = mem_prealloc; + fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, + backend->size, + F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL, + true); + 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); + } +} + +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, +}; + +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) += hostmem-file.o common-obj-y += cryptodev.o common-obj-y += cryptodev-builtin.o + +common-obj-$(CONFIG_LINUX) += 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. +@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 +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=@var{id},filename=@var{/dev/random} Creates a random number generator backend which obtains entropy from