From nobody Mon Feb 9 18:17:47 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616149295; cv=none; d=zohomail.com; s=zohoarc; b=Ld3FukBWJ2Ul8MF2F6jlJWPiGszGJYIK2GDycgFNpgufmmTkefaktu5KnPkCr5+STRfK2fcbycoINHxdA5xzOXZq8CvTe18GqiqSJePFedmik9IT4zZynBH2blzP6GbhfJCDyMhL0eGolRyv65zaW/rjKVmx35GF/6dH7bAyG0E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616149295; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=zK9hhhHxbWWPxb79BkDcnoV4ZWXfg43du8jJ26GFzAc=; b=UcHzTt815pajc02tGSwbpkOeySlWBbyNXdhazLzVY+EnR4B8J0jzbJ9Im9onYfxol0Y+PBA7ID4ex8GUfC0LE0xJ6z5HNob+iu6Thxqr9BLU+e89DKtXi15SGVSfCFFiNBWUpKDnYHcdNCuydsr7WLSf1BzkGL5GAv1FO0W/R9g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 161614929531377.98016121673732; Fri, 19 Mar 2021 03:21:35 -0700 (PDT) Received: from localhost ([::1]:36696 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lNCG6-0001VW-9o for importer@patchew.org; Fri, 19 Mar 2021 06:21:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48054) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lNC9a-0002Ur-L4 for qemu-devel@nongnu.org; Fri, 19 Mar 2021 06:14:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:23795) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lNC9Y-0000s5-04 for qemu-devel@nongnu.org; Fri, 19 Mar 2021 06:14:50 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-557-at6AjTbRPHm5QV8XMdJCsQ-1; Fri, 19 Mar 2021 06:14:46 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7ED7D8189C7; Fri, 19 Mar 2021 10:14:44 +0000 (UTC) Received: from t480s.redhat.com (ovpn-112-11.ams2.redhat.com [10.36.112.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4122910013C1; Fri, 19 Mar 2021 10:14:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616148887; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zK9hhhHxbWWPxb79BkDcnoV4ZWXfg43du8jJ26GFzAc=; b=gHX1VmFnNZ2EJo1HIimG68geSjTlqXP4Og3mCsWtFEJLh4J3SoMnRu9JeFRUKnKsBJQ9r4 mFXMrFBmVIiOgicxsvYDWEfshrzpsH+E5xgP4JEY/br8it5l46AlHQ8i2CnxBD20256EZl fDgOB9Yk01slJ8ttQv5gNTlsb8APBjQ= X-MC-Unique: at6AjTbRPHm5QV8XMdJCsQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v4 12/14] hostmem: Wire up RAM_NORESERVE via "reserve" property Date: Fri, 19 Mar 2021 11:12:28 +0100 Message-Id: <20210319101230.21531-13-david@redhat.com> In-Reply-To: <20210319101230.21531-1-david@redhat.com> References: <20210319101230.21531-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.249, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_FILL_THIS_FORM_SHORT=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Marcel Apfelbaum , Murilo Opsfelder Araujo , Igor Kotrasinski , Eduardo Habkost , "Michael S. Tsirkin" , David Hildenbrand , Richard Henderson , "Dr. David Alan Gilbert" , Peter Xu , Greg Kurz , Stefan Hajnoczi , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Markus Armbruster Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's provide a way to control the use of RAM_NORESERVE via memory backends using the "reserve" property which defaults to true (old behavior). Only Linux currently supports setting the flag (and support is checked at runtime, depending on the setting of "/proc/sys/vm/overcommit_memory"). Windows and other POSIX systems will bail out. The target use case is virtio-mem, which dynamically exposes memory inside a large, sparse memory area to the VM. This essentially allows avoiding to set "/proc/sys/vm/overcommit_memory =3D=3D 0") when using virtio-mem and also supporting hugetlbfs in the future. Reviewed-by: Peter Xu Reviewed-by: Eduardo Habkost Signed-off-by: David Hildenbrand --- backends/hostmem-file.c | 11 ++++++----- backends/hostmem-memfd.c | 1 + backends/hostmem-ram.c | 1 + backends/hostmem.c | 33 +++++++++++++++++++++++++++++++++ include/sysemu/hostmem.h | 2 +- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index b683da9daf..9d550e53d4 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -40,6 +40,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Err= or **errp) object_get_typename(OBJECT(backend))); #else HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(backend); + uint32_t ram_flags; gchar *name; =20 if (!backend->size) { @@ -52,11 +53,11 @@ file_backend_memory_alloc(HostMemoryBackend *backend, E= rror **errp) } =20 name =3D host_memory_backend_get_name(backend); - memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), - name, - backend->size, fb->align, - (backend->share ? RAM_SHARED : 0) | - (fb->is_pmem ? RAM_PMEM : 0), + ram_flags =3D backend->share ? RAM_SHARED : 0; + ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; + ram_flags |=3D fb->is_pmem ? RAM_PMEM : 0; + memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name, + backend->size, fb->align, ram_flags, fb->mem_path, fb->readonly, errp); g_free(name); #endif diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c index 93b5d1a4cf..f3436b623d 100644 --- a/backends/hostmem-memfd.c +++ b/backends/hostmem-memfd.c @@ -55,6 +55,7 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Er= ror **errp) =20 name =3D host_memory_backend_get_name(backend); ram_flags =3D backend->share ? RAM_SHARED : 0; + ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name, backend->size, ram_flags, fd, 0, errp); g_free(name); diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c index 741e701062..b8e55cdbd0 100644 --- a/backends/hostmem-ram.c +++ b/backends/hostmem-ram.c @@ -29,6 +29,7 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Erro= r **errp) =20 name =3D host_memory_backend_get_name(backend); ram_flags =3D backend->share ? RAM_SHARED : 0; + ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend), = name, backend->size, ram_flags, errp); g_free(name); diff --git a/backends/hostmem.c b/backends/hostmem.c index c6c1ff5b99..9ec5141112 100644 --- a/backends/hostmem.c +++ b/backends/hostmem.c @@ -217,6 +217,11 @@ static void host_memory_backend_set_prealloc(Object *o= bj, bool value, Error *local_err =3D NULL; HostMemoryBackend *backend =3D MEMORY_BACKEND(obj); =20 + if (!backend->reserve && value) { + error_setg(errp, "'prealloc=3Don' and 'reserve=3Doff' are incompat= ible"); + return; + } + if (!host_memory_backend_mr_inited(backend)) { backend->prealloc =3D value; return; @@ -268,6 +273,7 @@ static void host_memory_backend_init(Object *obj) /* TODO: convert access to globals to compat properties */ backend->merge =3D machine_mem_merge(machine); backend->dump =3D machine_dump_guest_core(machine); + backend->reserve =3D true; backend->prealloc_threads =3D 1; } =20 @@ -426,6 +432,28 @@ static void host_memory_backend_set_share(Object *o, b= ool value, Error **errp) backend->share =3D value; } =20 +static bool host_memory_backend_get_reserve(Object *o, Error **errp) +{ + HostMemoryBackend *backend =3D MEMORY_BACKEND(o); + + return backend->reserve; +} + +static void host_memory_backend_set_reserve(Object *o, bool value, Error *= *errp) +{ + HostMemoryBackend *backend =3D MEMORY_BACKEND(o); + + if (host_memory_backend_mr_inited(backend)) { + error_setg(errp, "cannot change property value"); + return; + } + if (backend->prealloc && !value) { + error_setg(errp, "'prealloc=3Don' and 'reserve=3Doff' are incompat= ible"); + return; + } + backend->reserve =3D value; +} + static bool host_memory_backend_get_use_canonical_path(Object *obj, Error **errp) { @@ -494,6 +522,11 @@ host_memory_backend_class_init(ObjectClass *oc, void *= data) host_memory_backend_get_share, host_memory_backend_set_share); object_class_property_set_description(oc, "share", "Mark the memory as private to QEMU or shared"); + object_class_property_add_bool(oc, "reserve", + host_memory_backend_get_reserve, host_memory_backend_set_reserve); + object_class_property_set_description(oc, "reserve", + "Reserve swap space (or huge pages if applicable) for the whole me= mory" + " backend, if supported by the OS."); /* * Do not delete/rename option. This option must be considered stable * (as if it didn't have the 'x-' prefix including deprecation period)= as diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h index df5644723a..9ff5c16963 100644 --- a/include/sysemu/hostmem.h +++ b/include/sysemu/hostmem.h @@ -64,7 +64,7 @@ struct HostMemoryBackend { /* protected */ uint64_t size; bool merge, dump, use_canonical_path; - bool prealloc, is_mapped, share; + bool prealloc, is_mapped, share, reserve; uint32_t prealloc_threads; DECLARE_BITMAP(host_nodes, MAX_NODES + 1); HostMemPolicy policy; --=20 2.29.2