From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580754834320209.94926988389886; Mon, 3 Feb 2020 10:33:54 -0800 (PST) Received: from localhost ([::1]:45790 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygXg-0000x4-Qp for importer@patchew.org; Mon, 03 Feb 2020 13:33:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58669) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygVi-0007kx-14 for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:31:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygVf-00013x-Sx for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:31:49 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:55319 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygVf-00013C-N7 for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:31:47 -0500 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-397-MJyn8rsxN-61JswfFvrQnw-1; Mon, 03 Feb 2020 13:31:45 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A3D5F107ACC9; Mon, 3 Feb 2020 18:31:44 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id D80675D9CA; Mon, 3 Feb 2020 18:31:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754707; 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=cyd6R23774IED/ujbSfCJcmg9iCCsZzi5iIEqbWErIk=; b=MUEDp/DMuSutU2gOh6zmAvKK6521k64MI6ZrKv7lYMhzIKncwmR8RWw1GN9F28cWylT2EI pwnzOPyB3A+YO6z2Y6a2l6Ti3Rz/fd8t+PIyp4IuW9bxNblctm4gfxtcMlvfw/eIogheIu DwXn1Mwxb8u9h5gbzQ7NRNpCtJALPvU= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 01/13] util: vfio-helpers: Factor out and fix processing of existings ram blocks Date: Mon, 3 Feb 2020 19:31:13 +0100 Message-Id: <20200203183125.164879-2-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: MJyn8rsxN-61JswfFvrQnw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Alex Williamson , Paolo Bonzini , Richard Henderson 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" Factor it out into common code when a new notifier is registered, just as done with the memory region notifier. This allows us to have the logic about how to process existing ram blocks at a central place (which will be extended soon). Just like when adding a new ram block, we have to register the max_length. We don't have a way to get notified about resizes yet, and some memory would not be mapped when growing the ram block. Note: Currently, ram blocks are only "fake resized". All memory (max_length) is accessible. We can get rid of a bunch of functions in stubs/ram-block.c . Print the warning from inside qemu_vfio_ram_block_added(). Cc: Richard Henderson Cc: Paolo Bonzini Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Alex Williamson Signed-off-by: David Hildenbrand --- exec.c | 5 +++++ hw/core/numa.c | 14 ++++++++++++++ include/exec/cpu-common.h | 1 + stubs/ram-block.c | 20 -------------------- util/vfio-helpers.c | 28 +++++++--------------------- 5 files changed, 27 insertions(+), 41 deletions(-) diff --git a/exec.c b/exec.c index 67e520d18e..05cfe868ab 100644 --- a/exec.c +++ b/exec.c @@ -2017,6 +2017,11 @@ ram_addr_t qemu_ram_get_used_length(RAMBlock *rb) return rb->used_length; } =20 +ram_addr_t qemu_ram_get_max_length(RAMBlock *rb) +{ + return rb->max_length; +} + bool qemu_ram_is_shared(RAMBlock *rb) { return rb->flags & RAM_SHARED; diff --git a/hw/core/numa.c b/hw/core/numa.c index 0d1b4be76a..fed4046680 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -899,9 +899,23 @@ void query_numa_node_mem(NumaNodeMem node_mem[], Machi= neState *ms) } } =20 +static int ram_block_notify_add_single(RAMBlock *rb, void *opaque) +{ + ram_addr_t size =3D qemu_ram_get_max_length(rb); + void *host =3D qemu_ram_get_host_addr(rb); + RAMBlockNotifier *notifier =3D opaque; + + if (host) { + notifier->ram_block_added(notifier, host, size); + } + return 0; +} + void ram_block_notifier_add(RAMBlockNotifier *n) { QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next); + /* Notify about all existing ram blocks. */ + qemu_ram_foreach_block(ram_block_notify_add_single, n); } =20 void ram_block_notifier_remove(RAMBlockNotifier *n) diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index 81753bbb34..9760ac9068 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -59,6 +59,7 @@ const char *qemu_ram_get_idstr(RAMBlock *rb); void *qemu_ram_get_host_addr(RAMBlock *rb); ram_addr_t qemu_ram_get_offset(RAMBlock *rb); ram_addr_t qemu_ram_get_used_length(RAMBlock *rb); +ram_addr_t qemu_ram_get_max_length(RAMBlock *rb); bool qemu_ram_is_shared(RAMBlock *rb); bool qemu_ram_is_uf_zeroable(RAMBlock *rb); void qemu_ram_set_uf_zeroable(RAMBlock *rb); diff --git a/stubs/ram-block.c b/stubs/ram-block.c index 73c0a3ee08..10855b52dd 100644 --- a/stubs/ram-block.c +++ b/stubs/ram-block.c @@ -2,21 +2,6 @@ #include "exec/ramlist.h" #include "exec/cpu-common.h" =20 -void *qemu_ram_get_host_addr(RAMBlock *rb) -{ - return 0; -} - -ram_addr_t qemu_ram_get_offset(RAMBlock *rb) -{ - return 0; -} - -ram_addr_t qemu_ram_get_used_length(RAMBlock *rb) -{ - return 0; -} - void ram_block_notifier_add(RAMBlockNotifier *n) { } @@ -24,8 +9,3 @@ void ram_block_notifier_add(RAMBlockNotifier *n) void ram_block_notifier_remove(RAMBlockNotifier *n) { } - -int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque) -{ - return 0; -} diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c index 813f7ec564..71e02e7f35 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -376,8 +376,13 @@ static void qemu_vfio_ram_block_added(RAMBlockNotifier= *n, void *host, size_t size) { QEMUVFIOState *s =3D container_of(n, QEMUVFIOState, ram_notifier); + int ret; + trace_qemu_vfio_ram_block_added(s, host, size); - qemu_vfio_dma_map(s, host, size, false, NULL); + ret =3D qemu_vfio_dma_map(s, host, size, false, NULL); + if (ret) { + error_report("qemu_vfio_dma_map(%p, %zu) failed: %d", host, size, = ret); + } } =20 static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n, @@ -390,33 +395,14 @@ static void qemu_vfio_ram_block_removed(RAMBlockNotif= ier *n, } } =20 -static int qemu_vfio_init_ramblock(RAMBlock *rb, void *opaque) -{ - void *host_addr =3D qemu_ram_get_host_addr(rb); - ram_addr_t length =3D qemu_ram_get_used_length(rb); - int ret; - QEMUVFIOState *s =3D opaque; - - if (!host_addr) { - return 0; - } - ret =3D qemu_vfio_dma_map(s, host_addr, length, false, NULL); - if (ret) { - fprintf(stderr, "qemu_vfio_init_ramblock: failed %p %" PRId64 "\n", - host_addr, (uint64_t)length); - } - return 0; -} - static void qemu_vfio_open_common(QEMUVFIOState *s) { qemu_mutex_init(&s->lock); s->ram_notifier.ram_block_added =3D qemu_vfio_ram_block_added; s->ram_notifier.ram_block_removed =3D qemu_vfio_ram_block_removed; - ram_block_notifier_add(&s->ram_notifier); s->low_water_mark =3D QEMU_VFIO_IOVA_MIN; s->high_water_mark =3D QEMU_VFIO_IOVA_MAX; - qemu_ram_foreach_block(qemu_vfio_init_ramblock, s); + ram_block_notifier_add(&s->ram_notifier); } =20 /** --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580754961197537.2204179160262; Mon, 3 Feb 2020 10:36:01 -0800 (PST) Received: from localhost ([::1]:45826 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygZj-000458-4V for importer@patchew.org; Mon, 03 Feb 2020 13:35:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58767) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygVj-0007kz-6q for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:31:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygVi-00019D-3H for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:31:51 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:27237 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygVh-00017y-UG for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:31:49 -0500 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-87-8TepOKHpM62NcqZmZOBzHA-1; Mon, 03 Feb 2020 13:31:47 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7C2E0108442A; Mon, 3 Feb 2020 18:31:46 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0D635D9CA; Mon, 3 Feb 2020 18:31:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754709; 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=kvMA1iTUz7bWWg5YdYdLUqhskNqbO7FnJlpl8r7fymQ=; b=dSpHe/mng+dhp4hL4eazonk+vyIRivbMTDVwFBwFkWC0VA29kluiukh5AnKJBqgjO2GLjP LoZrrTz+cZ08uqwrNMAinkJ+CTofzgCnG0ZLCp7lwQxBWEb98I89BzWgmHnsenN6Uu3qcr XzfrhOWnT6F5t7yBaWHvGn02gORn9NQ= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 02/13] exec: Factor out setting ram settings (madvise ...) into qemu_ram_apply_settings() Date: Mon, 3 Feb 2020 19:31:14 +0100 Message-Id: <20200203183125.164879-3-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: 8TepOKHpM62NcqZmZOBzHA-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Paolo Bonzini , Richard Henderson 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" Factor all settings out into qemu_ram_apply_settings(). For memory_try_enable_merging(), the important bit is that it won't be called with XEN - which is now still the case as new_block->host will remain NULL. Cc: Richard Henderson Cc: Paolo Bonzini Signed-off-by: David Hildenbrand Reviewed-by: Richard Henderson --- exec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/exec.c b/exec.c index 05cfe868ab..31a462a7d3 100644 --- a/exec.c +++ b/exec.c @@ -2121,6 +2121,15 @@ static int memory_try_enable_merging(void *addr, siz= e_t len) return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE); } =20 +static void qemu_ram_apply_settings(void *host, size_t length) +{ + memory_try_enable_merging(host, length); + qemu_ram_setup_dump(host, length); + qemu_madvise(host, length, QEMU_MADV_HUGEPAGE); + /* MADV_DONTFORK is also needed by KVM in absence of synchronous MMU */ + qemu_madvise(host, length, QEMU_MADV_DONTFORK); +} + /* Only legal before guest might have detected the memory size: e.g. on * incoming migration, or right after reset. * @@ -2271,7 +2280,6 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp, bool shared) qemu_mutex_unlock_ramlist(); return; } - memory_try_enable_merging(new_block->host, new_block->max_leng= th); } } =20 @@ -2309,10 +2317,7 @@ static void ram_block_add(RAMBlock *new_block, Error= **errp, bool shared) DIRTY_CLIENTS_ALL); =20 if (new_block->host) { - qemu_ram_setup_dump(new_block->host, new_block->max_length); - qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_HUG= EPAGE); - /* MADV_DONTFORK is also needed by KVM in absence of synchronous M= MU */ - qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_DON= TFORK); + qemu_ram_apply_settings(new_block->host, new_block->max_length); ram_block_notify_add(new_block->host, new_block->max_length); } } --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580754836822896.0860758764367; Mon, 3 Feb 2020 10:33:56 -0800 (PST) Received: from localhost ([::1]:45794 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygXi-00012K-Jk for importer@patchew.org; Mon, 03 Feb 2020 13:33:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58888) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygVy-0007nA-It for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygVn-0001Ff-Er for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:31:56 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:47290 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygVm-0001E3-2P for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:31:54 -0500 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-56-m7BJjnKiOYCscYWBgb6JZg-1; Mon, 03 Feb 2020 13:31:49 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 556B918B9FC1; Mon, 3 Feb 2020 18:31:48 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id CA5825D9CA; Mon, 3 Feb 2020 18:31:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754712; 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=8y7Epc8q3phPmfn/4rrMSnCW8X62O8N9XOI00NeWjWE=; b=dFVLAaNMqu2cDZh6DYqf8yCT3YROD7dvOH12qszZXThQ8laMXoldyLX55qGqsTiTaijyAh 0FFjv5VDk4pnZ7x6ht/ASuVWB5msriC+kjQ0h6TxmHEqgzDpy6oANC+LoQh9sZQAaBunJq X/Jc0O1m8RQ8iXeI3hSb1MFPjedz+UA= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 03/13] exec: Reuse qemu_ram_apply_settings() in qemu_ram_remap() Date: Mon, 3 Feb 2020 19:31:15 +0100 Message-Id: <20200203183125.164879-4-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: m7BJjnKiOYCscYWBgb6JZg-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Paolo Bonzini , Richard Henderson 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" I don't see why we shouldn't apply all settings to make it look like the surrounding RAM (and enable proper VMA merging). Note: memory backend settings might have overridden these settings. We would need a callback to let the memory backend fix that up. Cc: Richard Henderson Cc: Paolo Bonzini Signed-off-by: David Hildenbrand Reviewed-by: Richard Henderson --- exec.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/exec.c b/exec.c index 31a462a7d3..f7525867ec 100644 --- a/exec.c +++ b/exec.c @@ -2552,8 +2552,7 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t lengt= h) length, addr); exit(1); } - memory_try_enable_merging(vaddr, length); - qemu_ram_setup_dump(vaddr, length); + qemu_ram_apply_settings(vaddr, length); } } } --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580755149047367.68368350030846; Mon, 3 Feb 2020 10:39:09 -0800 (PST) Received: from localhost ([::1]:45876 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygcl-0000pV-AM for importer@patchew.org; Mon, 03 Feb 2020 13:39:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59041) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygW3-0007qD-J9 for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygW1-0001Lh-ET for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:10 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:52486 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygW0-0001Hk-VT for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:09 -0500 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-294-liybGsylMjarD6ZVlDWtbA-1; Mon, 03 Feb 2020 13:31:59 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2F23CDB60; Mon, 3 Feb 2020 18:31:58 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id A35D75D9CA; Mon, 3 Feb 2020 18:31:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754721; 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=ThkhX9ejOkX/i/BPydq3cIMSP9rpzdG3sVMxhKILgnk=; b=O7HtwPok8lySGOYLy5MMlmFL2TfvuP3nLDkzxDNP6yx3jh2W0aaBfcw2aU0bwjByi1O3K+ +2OpabkvnHpaY26Bn8pprQs0mXlcbrpC0sPGw3+uxoj7pYya5+/SfbUUGNtbvAtXQsUP3J GP12Tq/paAsPRnjyJlJVH6tQS6pS0po= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 04/13] exec: Drop "shared" parameter from ram_block_add() Date: Mon, 3 Feb 2020 19:31:16 +0100 Message-Id: <20200203183125.164879-5-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: liybGsylMjarD6ZVlDWtbA-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Paolo Bonzini , Richard Henderson 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" Properly store it in the flags of the ram block instead (and the flag even already exists and is used). E.g., qemu_ram_is_shared() now properly succeeds on all ram blocks that are actually shared. Cc: Richard Henderson Cc: Paolo Bonzini Signed-off-by: David Hildenbrand Reviewed-by: Richard Henderson --- exec.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/exec.c b/exec.c index f7525867ec..fc65c4f7ca 100644 --- a/exec.c +++ b/exec.c @@ -2249,7 +2249,7 @@ static void dirty_memory_extend(ram_addr_t old_ram_si= ze, } } =20 -static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared) +static void ram_block_add(RAMBlock *new_block, Error **errp) { RAMBlock *block; RAMBlock *last_block =3D NULL; @@ -2272,7 +2272,8 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp, bool shared) } } else { new_block->host =3D phys_mem_alloc(new_block->max_length, - &new_block->mr->align, shared= ); + &new_block->mr->align, + qemu_ram_is_shared(new_block)= ); if (!new_block->host) { error_setg_errno(errp, errno, "cannot set up guest memory '%s'", @@ -2376,7 +2377,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Mem= oryRegion *mr, return NULL; } =20 - ram_block_add(new_block, &local_err, ram_flags & RAM_SHARED); + ram_block_add(new_block, &local_err); if (local_err) { g_free(new_block); error_propagate(errp, local_err); @@ -2438,10 +2439,13 @@ RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, = ram_addr_t max_size, if (host) { new_block->flags |=3D RAM_PREALLOC; } + if (share) { + new_block->flags |=3D RAM_SHARED; + } if (resizeable) { new_block->flags |=3D RAM_RESIZEABLE; } - ram_block_add(new_block, &local_err, share); + ram_block_add(new_block, &local_err); if (local_err) { g_free(new_block); error_propagate(errp, local_err); --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580755051183419.03407984388923; Mon, 3 Feb 2020 10:37:31 -0800 (PST) Received: from localhost ([::1]:45848 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygbB-0006gB-W8 for importer@patchew.org; Mon, 03 Feb 2020 13:37:30 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59003) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygW3-0007p7-Fr for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygW1-0001La-DD for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:10 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:44616 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygW0-0001II-19 for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:09 -0500 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-95-4ngYOA2oPT-TIHuunSrXgA-1; Mon, 03 Feb 2020 13:32:01 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5CC33107ACC9; Mon, 3 Feb 2020 18:32:00 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7C9045D9CA; Mon, 3 Feb 2020 18:31:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754723; 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=9L9xxZ9ZeBStGdB1kO0OkTwJU7PxXep2rUjWZkoEjvQ=; b=KclWinwTy0eD/2AsouMdXa+ZdeQm3aLKmz9dVD8ePcT96gVBdTx43M4gvcEh/ByGeIaqPb 7cZYbtqDpnprzwLhZlKaCYtL2guv63PYBlrZ0BXVnNOneXbIK/jRt98h4gjfRbxpXBv0qI mNEs2i4mODgdmWhilkHlScGcji3NpMw= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 05/13] util/mmap-alloc: Factor out calculation of pagesize to mmap_pagesize() Date: Mon, 3 Feb 2020 19:31:17 +0100 Message-Id: <20200203183125.164879-6-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: 4ngYOA2oPT-TIHuunSrXgA-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Greg Kurz , Murilo Opsfelder Araujo , Paolo Bonzini , Richard Henderson 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" Factor it out and add a comment. Cc: "Michael S. Tsirkin" Cc: Murilo Opsfelder Araujo Cc: Greg Kurz Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Signed-off-by: David Hildenbrand Acked-by: Murilo Opsfelder Araujo Reviewed-by: Richard Henderson --- util/mmap-alloc.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 27dcccd8ec..82f02a2cec 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -82,17 +82,27 @@ size_t qemu_mempath_getpagesize(const char *mem_path) return qemu_real_host_page_size; } =20 +static inline size_t mmap_pagesize(int fd) +{ +#if defined(__powerpc64__) && defined(__linux__) + /* Mappings in the same segment must share the same page size */ + return qemu_fd_getpagesize(fd); +#else + return qemu_real_host_page_size; +#endif +} + void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared, bool is_pmem) { + const size_t pagesize =3D mmap_pagesize(fd); int flags; int map_sync_flags =3D 0; int guardfd; size_t offset; - size_t pagesize; size_t total; void *guardptr; void *ptr; @@ -113,7 +123,6 @@ void *qemu_ram_mmap(int fd, * anonymous memory is OK. */ flags =3D MAP_PRIVATE; - pagesize =3D qemu_fd_getpagesize(fd); if (fd =3D=3D -1 || pagesize =3D=3D qemu_real_host_page_size) { guardfd =3D -1; flags |=3D MAP_ANONYMOUS; @@ -123,7 +132,6 @@ void *qemu_ram_mmap(int fd, } #else guardfd =3D -1; - pagesize =3D qemu_real_host_page_size; flags =3D MAP_PRIVATE | MAP_ANONYMOUS; #endif =20 @@ -198,15 +206,10 @@ void *qemu_ram_mmap(int fd, =20 void qemu_ram_munmap(int fd, void *ptr, size_t size) { - size_t pagesize; + const size_t pagesize =3D mmap_pagesize(fd); =20 if (ptr) { /* Unmap both the RAM block and the guard page */ -#if defined(__powerpc64__) && defined(__linux__) - pagesize =3D qemu_fd_getpagesize(fd); -#else - pagesize =3D qemu_real_host_page_size; -#endif munmap(ptr, size + pagesize); } } --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580755149111893.464069436202; Mon, 3 Feb 2020 10:39:09 -0800 (PST) Received: from localhost ([::1]:45874 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygcl-0000or-DG for importer@patchew.org; Mon, 03 Feb 2020 13:39:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:60167) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygWS-0008Oa-1T for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygWQ-0002DI-Pa for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:35 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:53706 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygWQ-0002C6-HC for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:34 -0500 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-167-Qcra12-CNWCRuh0kVxhnbg-1; Mon, 03 Feb 2020 13:32:03 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 967D5107ACC7; Mon, 3 Feb 2020 18:32:02 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id A85225D9CA; Mon, 3 Feb 2020 18:32:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754754; 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=sYsEQi96eGtN1bqf4EOyLA5PXTJXlAtfqWxl3nyMgzY=; b=XkOSZF3xcCRBRvyQWeeBr47x0az4o0441Fflehj3/gni76m1u0NnEXZvMozIuntynBrW72 rMYVHWmIG8SDhkay92YAA641B5CH5Yv3OKV1T9uHt1DwUBHxEiLrt9sLkuEREUq6g2VO2B jgjEZYwt8MOlKfVZLD2sxrba3F7ExWo= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 06/13] util/mmap-alloc: Factor out reserving of a memory region to mmap_reserve() Date: Mon, 3 Feb 2020 19:31:18 +0100 Message-Id: <20200203183125.164879-7-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: Qcra12-CNWCRuh0kVxhnbg-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Greg Kurz , Murilo Opsfelder Araujo , Paolo Bonzini , Richard Henderson 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" We want to reserve a memory region without actually populating memory. Let's factor that out. Cc: "Michael S. Tsirkin" Cc: Greg Kurz Cc: Murilo Opsfelder Araujo Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Signed-off-by: David Hildenbrand Acked-by: Murilo Opsfelder Araujo Reviewed-by: Richard Henderson --- util/mmap-alloc.c | 58 +++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 82f02a2cec..43a26f38a8 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -82,6 +82,38 @@ size_t qemu_mempath_getpagesize(const char *mem_path) return qemu_real_host_page_size; } =20 +/* + * Reserve a new memory region of the requested size to be used for mapping + * from the given fd (if any). + */ +static void *mmap_reserve(size_t size, int fd) +{ + int flags =3D MAP_PRIVATE; + +#if defined(__powerpc64__) && defined(__linux__) + /* + * On ppc64 mappings in the same segment (aka slice) must share the sa= me + * page size. Since we will be re-allocating part of this segment + * from the supplied fd, we should make sure to use the same page size= , to + * this end we mmap the supplied fd. In this case, set MAP_NORESERVE = to + * avoid allocating backing store memory. + * We do this unless we are using the system page size, in which case + * anonymous memory is OK. + */ + if (fd =3D=3D -1 || qemu_fd_getpagesize(fd) =3D=3D qemu_real_host_page= _size) { + fd =3D -1; + flags |=3D MAP_ANONYMOUS; + } else { + flags |=3D MAP_NORESERVE; + } +#else + fd =3D -1; + flags |=3D MAP_ANONYMOUS; +#endif + + return mmap(0, size, PROT_NONE, flags, fd, 0); +} + static inline size_t mmap_pagesize(int fd) { #if defined(__powerpc64__) && defined(__linux__) @@ -101,7 +133,6 @@ void *qemu_ram_mmap(int fd, const size_t pagesize =3D mmap_pagesize(fd); int flags; int map_sync_flags =3D 0; - int guardfd; size_t offset; size_t total; void *guardptr; @@ -113,30 +144,7 @@ void *qemu_ram_mmap(int fd, */ total =3D size + align; =20 -#if defined(__powerpc64__) && defined(__linux__) - /* On ppc64 mappings in the same segment (aka slice) must share the sa= me - * page size. Since we will be re-allocating part of this segment - * from the supplied fd, we should make sure to use the same page size= , to - * this end we mmap the supplied fd. In this case, set MAP_NORESERVE = to - * avoid allocating backing store memory. - * We do this unless we are using the system page size, in which case - * anonymous memory is OK. - */ - flags =3D MAP_PRIVATE; - if (fd =3D=3D -1 || pagesize =3D=3D qemu_real_host_page_size) { - guardfd =3D -1; - flags |=3D MAP_ANONYMOUS; - } else { - guardfd =3D fd; - flags |=3D MAP_NORESERVE; - } -#else - guardfd =3D -1; - flags =3D MAP_PRIVATE | MAP_ANONYMOUS; -#endif - - guardptr =3D mmap(0, total, PROT_NONE, flags, guardfd, 0); - + guardptr =3D mmap_reserve(total, fd); if (guardptr =3D=3D MAP_FAILED) { return MAP_FAILED; } --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580754857398133.23474090917136; Mon, 3 Feb 2020 10:34:17 -0800 (PST) Received: from localhost ([::1]:45796 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygY4-0001Q7-9v for importer@patchew.org; Mon, 03 Feb 2020 13:34:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59625) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygWH-000848-2W for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygWD-0001jk-Mf for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:22 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:24087 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygWD-0001fA-DQ for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:21 -0500 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-64-dBTHPXKnNYetJeBZc9Skug-1; Mon, 03 Feb 2020 13:32:07 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B8256800D4E; Mon, 3 Feb 2020 18:32:05 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id D63345D9CA; Mon, 3 Feb 2020 18:32:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754738; 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=iotoBDjapXBODfuOrmI0IiH4IjpUHAEYPrSI8vW5zX4=; b=YcUvN3tBQ6OaHRf1MyGXRlQsmoIlHpRXfv+RBVqDacdGAQtPYl+ItxAzfTLV/9sz4y63ef BMaD2W1piKgcrFNapbcfOaziSHevZfSbjB0btFOOhSnWXGuaW5y252IlG0PiUTjwdsiyz4 +jidr44e1KMLZznmaMINl0xqqKQNg3k= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 07/13] util/mmap-alloc: Factor out populating of memory to mmap_populate() Date: Mon, 3 Feb 2020 19:31:19 +0100 Message-Id: <20200203183125.164879-8-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: dBTHPXKnNYetJeBZc9Skug-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Greg Kurz , Murilo Opsfelder Araujo , Paolo Bonzini , Richard Henderson 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" We want to populate memory within a reserved memory region. Let's factor that out. Cc: "Michael S. Tsirkin" Cc: Greg Kurz Cc: Murilo Opsfelder Araujo Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Signed-off-by: David Hildenbrand Acked-by: Murilo Opsfelder Araujo Reviewed-by: Richard Henderson --- util/mmap-alloc.c | 89 +++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 43a26f38a8..f043ccb0ab 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -114,6 +114,50 @@ static void *mmap_reserve(size_t size, int fd) return mmap(0, size, PROT_NONE, flags, fd, 0); } =20 +/* + * Populate memory in a reserved region from the given fd (if any). + */ +static void *mmap_populate(void *ptr, size_t size, int fd, bool shared, + bool is_pmem) +{ + int map_sync_flags =3D 0; + int flags =3D MAP_FIXED; + void *new_ptr; + + flags |=3D fd =3D=3D -1 ? MAP_ANONYMOUS : 0; + flags |=3D shared ? MAP_SHARED : MAP_PRIVATE; + if (shared && is_pmem) { + map_sync_flags =3D MAP_SYNC | MAP_SHARED_VALIDATE; + } + + new_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, flags | map_sync_f= lags, + fd, 0); + if (new_ptr =3D=3D MAP_FAILED && map_sync_flags) { + if (errno =3D=3D ENOTSUP) { + char *proc_link =3D g_strdup_printf("/proc/self/fd/%d", fd); + char *file_name =3D g_malloc0(PATH_MAX); + int len =3D readlink(proc_link, file_name, PATH_MAX - 1); + + if (len < 0) { + len =3D 0; + } + file_name[len] =3D '\0'; + fprintf(stderr, "Warning: requesting persistence across crashe= s " + "for backend file %s failed. Proceeding without " + "persistence, data might become corrupted in case of h= ost " + "crash.\n", file_name); + g_free(proc_link); + g_free(file_name); + } + /* + * If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try + * again without these flags to handle backwards compatibility. + */ + new_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, flags, fd, 0); + } + return new_ptr; +} + static inline size_t mmap_pagesize(int fd) { #if defined(__powerpc64__) && defined(__linux__) @@ -131,12 +175,8 @@ void *qemu_ram_mmap(int fd, bool is_pmem) { const size_t pagesize =3D mmap_pagesize(fd); - int flags; - int map_sync_flags =3D 0; - size_t offset; - size_t total; - void *guardptr; - void *ptr; + size_t offset, total; + void *ptr, *guardptr; =20 /* * Note: this always allocates at least one extra page of virtual addr= ess @@ -153,44 +193,9 @@ void *qemu_ram_mmap(int fd, /* Always align to host page size */ assert(align >=3D pagesize); =20 - flags =3D MAP_FIXED; - flags |=3D fd =3D=3D -1 ? MAP_ANONYMOUS : 0; - flags |=3D shared ? MAP_SHARED : MAP_PRIVATE; - if (shared && is_pmem) { - map_sync_flags =3D MAP_SYNC | MAP_SHARED_VALIDATE; - } - offset =3D QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guar= dptr; =20 - ptr =3D mmap(guardptr + offset, size, PROT_READ | PROT_WRITE, - flags | map_sync_flags, fd, 0); - - if (ptr =3D=3D MAP_FAILED && map_sync_flags) { - if (errno =3D=3D ENOTSUP) { - char *proc_link, *file_name; - int len; - proc_link =3D g_strdup_printf("/proc/self/fd/%d", fd); - file_name =3D g_malloc0(PATH_MAX); - len =3D readlink(proc_link, file_name, PATH_MAX - 1); - if (len < 0) { - len =3D 0; - } - file_name[len] =3D '\0'; - fprintf(stderr, "Warning: requesting persistence across crashe= s " - "for backend file %s failed. Proceeding without " - "persistence, data might become corrupted in case of h= ost " - "crash.\n", file_name); - g_free(proc_link); - g_free(file_name); - } - /* - * if map failed with MAP_SHARED_VALIDATE | MAP_SYNC, - * we will remove these flags to handle compatibility. - */ - ptr =3D mmap(guardptr + offset, size, PROT_READ | PROT_WRITE, - flags, fd, 0); - } - + ptr =3D mmap_populate(guardptr + offset, size, fd, shared, is_pmem); if (ptr =3D=3D MAP_FAILED) { munmap(guardptr, total); return MAP_FAILED; --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580754973433128.2840297812038; Mon, 3 Feb 2020 10:36:13 -0800 (PST) Received: from localhost ([::1]:45832 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygZw-0004WK-Ci for importer@patchew.org; Mon, 03 Feb 2020 13:36:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59750) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygWI-00085r-Py for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygWH-0001tE-Cx for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:26 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:31025 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygWH-0001pT-5Y for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:25 -0500 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-43-fg9lsnfdOPu6brdhN-Beiw-1; Mon, 03 Feb 2020 13:32:16 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9501B800D48; Mon, 3 Feb 2020 18:32:15 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id 11B965D9CA; Mon, 3 Feb 2020 18:32:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754743; 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=uvHeRWBrLxpLh5CubflKqh/5Wa9JFfhXcBP4obYJe6k=; b=TKqPiHsOiOoG32f0sfr/RKu07Cnc1ybf0ehD+Fe3gLpgGNPagV2WHdtZMejY58XwvgQBJZ Fu09IXylAmOz+t4Yj9ijBWhUWXWd0xuiAGnGrbHX7iWGqqnU4g2qhG7ofsBjJL4jRGCMjr 3DMr5bJ2hUNSkB5RCd01Y+WNmDWFf0U= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 08/13] util/mmap-alloc: Prepare for resizable mmaps Date: Mon, 3 Feb 2020 19:31:20 +0100 Message-Id: <20200203183125.164879-9-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: fg9lsnfdOPu6brdhN-Beiw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Greg Kurz , Murilo Opsfelder Araujo , Paolo Bonzini , Richard Henderson 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" When shrinking a mmap we want to re-reserve the already populated area. When growing a memory region, we want to populate starting with a given fd_offset. Prepare by allowing to pass these parameters. Also, let's make sure we always process full pages, to avoid unmapping/remapping pages that are already in use when growing/shrinking. (existing callers seem to guarantee this, but that's not obvious) Cc: "Michael S. Tsirkin" Cc: Greg Kurz Cc: Murilo Opsfelder Araujo Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Signed-off-by: David Hildenbrand Reviewed-by: Richard Henderson --- util/mmap-alloc.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index f043ccb0ab..63ad6893b7 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -83,12 +83,12 @@ size_t qemu_mempath_getpagesize(const char *mem_path) } =20 /* - * Reserve a new memory region of the requested size to be used for mapping - * from the given fd (if any). + * Reserve a new memory region of the requested size or re-reserve parts + * of an existing region to be used for mapping from the given fd (if any). */ -static void *mmap_reserve(size_t size, int fd) +static void *mmap_reserve(void *ptr, size_t size, int fd) { - int flags =3D MAP_PRIVATE; + int flags =3D MAP_PRIVATE | (ptr ? MAP_FIXED : 0); =20 #if defined(__powerpc64__) && defined(__linux__) /* @@ -111,19 +111,23 @@ static void *mmap_reserve(size_t size, int fd) flags |=3D MAP_ANONYMOUS; #endif =20 - return mmap(0, size, PROT_NONE, flags, fd, 0); + return mmap(ptr, size, PROT_NONE, flags, fd, 0); } =20 /* * Populate memory in a reserved region from the given fd (if any). */ -static void *mmap_populate(void *ptr, size_t size, int fd, bool shared, - bool is_pmem) +static void *mmap_populate(void *ptr, size_t size, int fd, size_t fd_offse= t, + bool shared, bool is_pmem) { int map_sync_flags =3D 0; int flags =3D MAP_FIXED; void *new_ptr; =20 + if (fd =3D=3D -1) { + fd_offset =3D 0; + } + flags |=3D fd =3D=3D -1 ? MAP_ANONYMOUS : 0; flags |=3D shared ? MAP_SHARED : MAP_PRIVATE; if (shared && is_pmem) { @@ -131,7 +135,7 @@ static void *mmap_populate(void *ptr, size_t size, int = fd, bool shared, } =20 new_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, flags | map_sync_f= lags, - fd, 0); + fd, fd_offset); if (new_ptr =3D=3D MAP_FAILED && map_sync_flags) { if (errno =3D=3D ENOTSUP) { char *proc_link =3D g_strdup_printf("/proc/self/fd/%d", fd); @@ -153,7 +157,7 @@ static void *mmap_populate(void *ptr, size_t size, int = fd, bool shared, * If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try * again without these flags to handle backwards compatibility. */ - new_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, flags, fd, 0); + new_ptr =3D mmap(ptr, size, PROT_READ | PROT_WRITE, flags, fd, fd_= offset); } return new_ptr; } @@ -178,13 +182,16 @@ void *qemu_ram_mmap(int fd, size_t offset, total; void *ptr, *guardptr; =20 + /* we can only map whole pages */ + size =3D QEMU_ALIGN_UP(size, pagesize); + /* * Note: this always allocates at least one extra page of virtual addr= ess * space, even if size is already aligned. */ total =3D size + align; =20 - guardptr =3D mmap_reserve(total, fd); + guardptr =3D mmap_reserve(0, total, fd); if (guardptr =3D=3D MAP_FAILED) { return MAP_FAILED; } @@ -195,7 +202,7 @@ void *qemu_ram_mmap(int fd, =20 offset =3D QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guar= dptr; =20 - ptr =3D mmap_populate(guardptr + offset, size, fd, shared, is_pmem); + ptr =3D mmap_populate(guardptr + offset, size, fd, 0, shared, is_pmem); if (ptr =3D=3D MAP_FAILED) { munmap(guardptr, total); return MAP_FAILED; @@ -221,6 +228,9 @@ void qemu_ram_munmap(int fd, void *ptr, size_t size) { const size_t pagesize =3D mmap_pagesize(fd); =20 + /* we can only map whole pages */ + size =3D QEMU_ALIGN_UP(size, pagesize); + if (ptr) { /* Unmap both the RAM block and the guard page */ munmap(ptr, size + pagesize); --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580754960205596.0841143977879; Mon, 3 Feb 2020 10:36:00 -0800 (PST) Received: from localhost ([::1]:45828 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygZj-00047M-0G for importer@patchew.org; Mon, 03 Feb 2020 13:35:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59741) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygWI-00085b-LY for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygWG-0001q4-4Z for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:25 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:29400 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygWF-0001nU-Vg for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:24 -0500 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-289-asMZ0xMGPcqcRBB7iWYjgw-1; Mon, 03 Feb 2020 13:32:19 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C49041084433; Mon, 3 Feb 2020 18:32:17 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id E02C85DAB0; Mon, 3 Feb 2020 18:32:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754742; 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=yiFlwfNNxg1lbxKs5NIEPv1MehTMQy7VN8Wu+aD6HJk=; b=QHxVOwAekrzW1dcSQyp+x+X+fF1HN9CIeJqmOXYWAqh62XOqkIO/rrjf5AI4UiKtvFNyjl tdTOQH4ijt6QrveZw8S8b7w6HkNhaux8seb4va4zADHJIZ6YBX7t8BzwfKIaq3ZydkxeGQ 1l8AL3cwR7toNwX2/+yik7NIN3v1dok= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 09/13] util/mmap-alloc: Implement resizable mmaps Date: Mon, 3 Feb 2020 19:31:21 +0100 Message-Id: <20200203183125.164879-10-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: asMZ0xMGPcqcRBB7iWYjgw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Greg Kurz , Murilo Opsfelder Araujo , Paolo Bonzini , Richard Henderson 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" Implement resizable mmaps. For now, the actual resizing is not wired up. Introduce qemu_ram_mmap_resizable() and qemu_ram_mmap_resize(). Make qemu_ram_mmap() a wrapper of qemu_ram_mmap_resizable(). Cc: "Michael S. Tsirkin" Cc: Greg Kurz Cc: Murilo Opsfelder Araujo Cc: Eduardo Habkost Cc: "Dr. David Alan Gilbert" Signed-off-by: David Hildenbrand --- include/qemu/mmap-alloc.h | 21 ++++++++++++------- util/mmap-alloc.c | 44 ++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h index e786266b92..70bc8e9637 100644 --- a/include/qemu/mmap-alloc.h +++ b/include/qemu/mmap-alloc.h @@ -7,11 +7,13 @@ size_t qemu_fd_getpagesize(int fd); size_t qemu_mempath_getpagesize(const char *mem_path); =20 /** - * qemu_ram_mmap: mmap the specified file or device. + * qemu_ram_mmap_resizable: reserve a memory region of @max_size to mmap t= he + * specified file or device and mmap @size of it. * * Parameters: * @fd: the file or the device to mmap * @size: the number of bytes to be mmaped + * @max_size: the number of bytes to be reserved * @align: if not zero, specify the alignment of the starting mapping add= ress; * otherwise, the alignment in use will be determined by QEMU. * @shared: map has RAM_SHARED flag. @@ -21,12 +23,15 @@ size_t qemu_mempath_getpagesize(const char *mem_path); * On success, return a pointer to the mapped area. * On failure, return MAP_FAILED. */ -void *qemu_ram_mmap(int fd, - size_t size, - size_t align, - bool shared, - bool is_pmem); - -void qemu_ram_munmap(int fd, void *ptr, size_t size); +void *qemu_ram_mmap_resizable(int fd, size_t size, size_t max_size, + size_t align, bool shared, bool is_pmem); +void *qemu_ram_mmap_resize(void *ptr, int fd, size_t old_size, size_t new_= size, + bool shared, bool is_pmem); +static inline void *qemu_ram_mmap(int fd, size_t size, size_t align, + bool shared, bool is_pmem) +{ + return qemu_ram_mmap_resizable(fd, size, size, align, shared, is_pmem); +} +void qemu_ram_munmap(int fd, void *ptr, size_t max_size); =20 #endif diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 63ad6893b7..2d562145e9 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -172,11 +172,8 @@ static inline size_t mmap_pagesize(int fd) #endif } =20 -void *qemu_ram_mmap(int fd, - size_t size, - size_t align, - bool shared, - bool is_pmem) +void *qemu_ram_mmap_resizable(int fd, size_t size, size_t max_size, + size_t align, bool shared, bool is_pmem) { const size_t pagesize =3D mmap_pagesize(fd); size_t offset, total; @@ -184,12 +181,14 @@ void *qemu_ram_mmap(int fd, =20 /* we can only map whole pages */ size =3D QEMU_ALIGN_UP(size, pagesize); + max_size =3D QEMU_ALIGN_UP(max_size, pagesize); =20 /* * Note: this always allocates at least one extra page of virtual addr= ess - * space, even if size is already aligned. + * space, even if the size is already aligned. We will reserve an area= of + * at least max_size, but only populate the requested part of it. */ - total =3D size + align; + total =3D max_size + align; =20 guardptr =3D mmap_reserve(0, total, fd); if (guardptr =3D=3D MAP_FAILED) { @@ -217,22 +216,43 @@ void *qemu_ram_mmap(int fd, * a guard page guarding against potential buffer overflows. */ total -=3D offset; - if (total > size + pagesize) { - munmap(ptr + size + pagesize, total - size - pagesize); + if (total > max_size + pagesize) { + munmap(ptr + max_size + pagesize, total - max_size - pagesize); } =20 return ptr; } =20 -void qemu_ram_munmap(int fd, void *ptr, size_t size) +void *qemu_ram_mmap_resize(void *ptr, int fd, size_t old_size, size_t new_= size, + bool shared, bool is_pmem) { const size_t pagesize =3D mmap_pagesize(fd); =20 /* we can only map whole pages */ - size =3D QEMU_ALIGN_UP(size, pagesize); + old_size =3D QEMU_ALIGN_UP(old_size, pagesize); + new_size =3D QEMU_ALIGN_UP(new_size, pagesize); + + /* we support actually resizable memory regions only on Linux */ + if (old_size < new_size) { + /* populate the missing piece into the reserved area */ + ptr =3D mmap_populate(ptr + old_size, new_size - old_size, fd, old= _size, + shared, is_pmem); + } else if (old_size > new_size) { + /* discard this piece, keeping the area reserved (should never fai= l) */ + ptr =3D mmap_reserve(ptr + new_size, old_size - new_size, fd); + } + return ptr; +} + +void qemu_ram_munmap(int fd, void *ptr, size_t max_size) +{ + const size_t pagesize =3D mmap_pagesize(fd); + + /* we can only map whole pages */ + max_size =3D QEMU_ALIGN_UP(max_size, pagesize); =20 if (ptr) { /* Unmap both the RAM block and the guard page */ - munmap(ptr, size + pagesize); + munmap(ptr, max_size + pagesize); } } --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580755051184949.6225823492051; Mon, 3 Feb 2020 10:37:31 -0800 (PST) Received: from localhost ([::1]:45846 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygbC-0006fo-1T for importer@patchew.org; Mon, 03 Feb 2020 13:37:30 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59738) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygWI-00085W-Hj for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygWH-0001sK-7v for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:26 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:52085 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygWH-0001nS-2S for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:25 -0500 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-191-Z27IfSNpPGKynlNRQI0Ozw-1; Mon, 03 Feb 2020 13:32:21 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C702218B9FC3; Mon, 3 Feb 2020 18:32:19 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C3FF5D9CA; Mon, 3 Feb 2020 18:32:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754742; 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=uSoRvfHZP0DIGem7R7am7rIs7Mbai+dIKdgAkYMJUDg=; b=DR5yf2i+bbPiGj8la1EDouDtTIqOvCev1x3LVpXS7BOqOquaxSuwbj2v1d3t8QcVDSnHzo 5lxhn9ho25aoGMH2eiJebJkuuKvHrNJsGrGdqIZCW9SsHpXLQAtrScp6QE4z56D2SMgN28 304ofSO9JPRMIHQwa9G8iDadaizHQVc= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 10/13] numa: Introduce ram_block_notify_resized() and ram_block_notifiers_support_resize() Date: Mon, 3 Feb 2020 19:31:22 +0100 Message-Id: <20200203183125.164879-11-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: Z27IfSNpPGKynlNRQI0Ozw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Paolo Bonzini , Richard Henderson 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" We want to actually resize ram blocks (make everything between used_length and max_length inaccessible) - however, not all ram block notifiers will support that. So introduce a way to detect if any registered notifier does not support it and add a way to notify all notifiers that support it. Using ram_block_notifiers_support_resize(), we can keep the existing handling for these special cases until they implement support (e.g., SEV, HAX) to resize. Cc: Richard Henderson Cc: Paolo Bonzini Cc: "Dr. David Alan Gilbert" Cc: Eduardo Habkost Cc: Marcel Apfelbaum Signed-off-by: David Hildenbrand --- hw/core/numa.c | 21 +++++++++++++++++++++ include/exec/ramlist.h | 4 ++++ util/trace-events | 1 + 3 files changed, 26 insertions(+) diff --git a/hw/core/numa.c b/hw/core/numa.c index fed4046680..5ccfcbcd41 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -940,3 +940,24 @@ void ram_block_notify_remove(void *host, size_t size) notifier->ram_block_removed(notifier, host, size); } } + +void ram_block_notify_resized(void *host, size_t oldsize, size_t newsize) +{ + RAMBlockNotifier *notifier; + + QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { + notifier->ram_block_resized(notifier, host, oldsize, newsize); + } +} + +bool ram_block_notifiers_support_resize(void) +{ + RAMBlockNotifier *notifier; + + QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { + if (!notifier->ram_block_resized) { + return false; + } + } + return true; +} diff --git a/include/exec/ramlist.h b/include/exec/ramlist.h index bc4faa1b00..33a380cbee 100644 --- a/include/exec/ramlist.h +++ b/include/exec/ramlist.h @@ -67,6 +67,8 @@ void qemu_mutex_unlock_ramlist(void); struct RAMBlockNotifier { void (*ram_block_added)(RAMBlockNotifier *n, void *host, size_t size); void (*ram_block_removed)(RAMBlockNotifier *n, void *host, size_t size= ); + void (*ram_block_resized)(RAMBlockNotifier *n, void *host, size_t olds= ize, + size_t newsize); QLIST_ENTRY(RAMBlockNotifier) next; }; =20 @@ -74,6 +76,8 @@ void ram_block_notifier_add(RAMBlockNotifier *n); void ram_block_notifier_remove(RAMBlockNotifier *n); void ram_block_notify_add(void *host, size_t size); void ram_block_notify_remove(void *host, size_t size); +void ram_block_notify_resized(void *host, size_t oldsize, size_t newsize); +bool ram_block_notifiers_support_resize(void); =20 void ram_block_dump(Monitor *mon); =20 diff --git a/util/trace-events b/util/trace-events index 83b6639018..226f406c46 100644 --- a/util/trace-events +++ b/util/trace-events @@ -76,6 +76,7 @@ qemu_mutex_unlock(void *mutex, const char *file, const in= t line) "released mutex qemu_vfio_dma_reset_temporary(void *s) "s %p" qemu_vfio_ram_block_added(void *s, void *p, size_t size) "s %p host %p siz= e 0x%zx" qemu_vfio_ram_block_removed(void *s, void *p, size_t size) "s %p host %p s= ize 0x%zx" +qemu_vfio_ram_block_resized(void *s, void *p, size_t oldsize, size_t newsi= zze) "s %p host %p oldsize 0x%zx newsize 0x%zx" qemu_vfio_find_mapping(void *s, void *p) "s %p host %p" qemu_vfio_new_mapping(void *s, void *host, size_t size, int index, uint64_= t iova) "s %p host %p size %zu index %d iova 0x%"PRIx64 qemu_vfio_do_mapping(void *s, void *host, size_t size, uint64_t iova) "s %= p host %p size %zu iova 0x%"PRIx64 --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580755284862514.4388180954311; Mon, 3 Feb 2020 10:41:24 -0800 (PST) Received: from localhost ([::1]:45908 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygex-00039i-DP for importer@patchew.org; Mon, 03 Feb 2020 13:41:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59945) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygWN-0008FV-IB for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygWM-00023p-FE for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:31 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:37319 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygWM-00023B-9S for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:30 -0500 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-299-83nBqtnxPUKyle1h-hBmHQ-1; Mon, 03 Feb 2020 13:32:25 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 62D511005502; Mon, 3 Feb 2020 18:32:24 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id 229B45DA83; Mon, 3 Feb 2020 18:32:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754749; 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=31XOYPT0uBnH+X75iJWU6zJ+I/jmJ21OKKjKlfz8Quo=; b=d9SIO80UYwz8c2GIKlEzRDhcZ955mQPpPPg2+O6xtQ3yYIX9YSP1FfrtcN8Syn/YH672Me YLTkcoJ4gsj5vdiIbeQfmuDgvK3hz1vGVWd/UaeyTcbsVVITcEvNAktY/3m25wrEI7snZ+ 9p4Yo6ziCkTSXPB1UN9Sr0/wO1OeB+A= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 11/13] util: vfio-helpers: Implement ram_block_resized() Date: Mon, 3 Feb 2020 19:31:23 +0100 Message-Id: <20200203183125.164879-12-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: 83nBqtnxPUKyle1h-hBmHQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , "Dr . David Alan Gilbert" , Markus Armbruster , Alex Williamson , Paolo Bonzini , Richard Henderson 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 implement ram_block_resized(). Note: Resizing is currently only allowed during reboot or when migration starts. Cc: "Dr. David Alan Gilbert" Cc: Paolo Bonzini Cc: Markus Armbruster Cc: Alex Williamson Signed-off-by: David Hildenbrand --- util/vfio-helpers.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c index 71e02e7f35..57d77e9480 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -395,11 +395,24 @@ static void qemu_vfio_ram_block_removed(RAMBlockNotif= ier *n, } } =20 +static void qemu_vfio_ram_block_resized(RAMBlockNotifier *n, void *host, + size_t oldsize, size_t newsize) +{ + QEMUVFIOState *s =3D container_of(n, QEMUVFIOState, ram_notifier); + if (host) { + trace_qemu_vfio_ram_block_resized(s, host, oldsize, newsize); + /* Note: Not atomic - we need a new ioctl for that. */ + qemu_vfio_ram_block_removed(n, host, oldsize); + qemu_vfio_ram_block_added(n, host, newsize); + } +} + static void qemu_vfio_open_common(QEMUVFIOState *s) { qemu_mutex_init(&s->lock); s->ram_notifier.ram_block_added =3D qemu_vfio_ram_block_added; s->ram_notifier.ram_block_removed =3D qemu_vfio_ram_block_removed; + s->ram_notifier.ram_block_resized =3D qemu_vfio_ram_block_resized; s->low_water_mark =3D QEMU_VFIO_IOVA_MIN; s->high_water_mark =3D QEMU_VFIO_IOVA_MAX; ram_block_notifier_add(&s->ram_notifier); --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580755389832908.3239102609905; Mon, 3 Feb 2020 10:43:09 -0800 (PST) Received: from localhost ([::1]:45934 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygge-000609-HZ for importer@patchew.org; Mon, 03 Feb 2020 13:43:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:60087) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygWQ-0008LB-K6 for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygWP-00029o-5b for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:34 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:23202 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygWO-00028L-Vq for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:33 -0500 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-115-46IHe5muO7aZykon3JLYSg-1; Mon, 03 Feb 2020 13:32:29 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E16EF800D4C; Mon, 3 Feb 2020 18:32:27 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id B19EA5D9CA; Mon, 3 Feb 2020 18:32:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754752; 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=pvpRP4j40p+bep+sPtrcb2kETzpSStuVctnhP/5qKq8=; b=XMf2jpOm3WfbzX5/WeA0YUouI8uS+m6nytrYJ0Ma+NtYWIkVllmsUNCBz0d2r7ZAYmU9bh 6s5D7/2/NXwwizbH6tkZlp/MGrU+7MAcZNPK2A02iAwbIfGkEaZS55gwZqUkxiiBICfois HTOWUcPA25QCnjzXbVRI6u6tD+86jzw= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 12/13] util: oslib: Resizable anonymous allocations under POSIX Date: Mon, 3 Feb 2020 19:31:24 +0100 Message-Id: <20200203183125.164879-13-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: 46IHe5muO7aZykon3JLYSg-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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: Eduardo Habkost , "Michael S . Tsirkin" , Stefan Weil , David Hildenbrand , "Dr . David Alan Gilbert" , Paolo Bonzini , Richard Henderson 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" Introduce qemu_anon_ram_alloc_resizable() and qemu_anon_ram_resize(). Implement them under POSIX and make them return NULL under WIN32. Under POSIX, we make use of resizable mmaps. An implementation under WIN32 is theoretically possible AFAIK and can be added later. In qemu_anon_ram_free(), rename the size parameter to max_size, to make it clearer that we have to use the maximum size when freeing resizable anonymous allocations. Cc: Richard Henderson Cc: Paolo Bonzini Cc: "Dr. David Alan Gilbert" Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Stefan Weil Signed-off-by: David Hildenbrand --- include/qemu/osdep.h | 6 +++++- util/oslib-posix.c | 37 ++++++++++++++++++++++++++++++++++--- util/oslib-win32.c | 14 ++++++++++++++ util/trace-events | 4 +++- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 9bd3dcfd13..57b7f40f56 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -311,8 +311,12 @@ int qemu_daemon(int nochdir, int noclose); void *qemu_try_memalign(size_t alignment, size_t size); void *qemu_memalign(size_t alignment, size_t size); void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared); +void *qemu_anon_ram_alloc_resizable(size_t size, size_t max_size, + uint64_t *align, bool shared); +void *qemu_anon_ram_resize(void *ptr, size_t old_size, size_t new_size, + bool shared); void qemu_vfree(void *ptr); -void qemu_anon_ram_free(void *ptr, size_t size); +void qemu_anon_ram_free(void *ptr, size_t max_size); =20 #define QEMU_MADV_INVALID -1 =20 diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 5a291cc982..e487a0e2c2 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -219,16 +219,47 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *alig= nment, bool shared) return ptr; } =20 +void *qemu_anon_ram_alloc_resizable(size_t size, size_t max_size, + uint64_t *alignment, bool shared) +{ + size_t align =3D QEMU_VMALLOC_ALIGN; + void *ptr =3D qemu_ram_mmap_resizable(-1, size, max_size, align, share= d, + false); + + if (ptr =3D=3D MAP_FAILED) { + return NULL; + } + + if (alignment) { + *alignment =3D align; + } + + trace_qemu_anon_ram_alloc_resizable(size, max_size, ptr); + return ptr; +} + +void *qemu_anon_ram_resize(void *ptr, size_t old_size, size_t new_size, + bool shared) +{ + ptr =3D qemu_ram_mmap_resize(ptr, -1, old_size, new_size, shared, fals= e); + if (ptr =3D=3D MAP_FAILED) { + return NULL; + } + + trace_qemu_anon_ram_resize(old_size, new_size, ptr); + return ptr; +} + void qemu_vfree(void *ptr) { trace_qemu_vfree(ptr); free(ptr); } =20 -void qemu_anon_ram_free(void *ptr, size_t size) +void qemu_anon_ram_free(void *ptr, size_t max_size) { - trace_qemu_anon_ram_free(ptr, size); - qemu_ram_munmap(-1, ptr, size); + trace_qemu_anon_ram_free(ptr, max_size); + qemu_ram_munmap(-1, ptr, max_size); } =20 void qemu_set_block(int fd) diff --git a/util/oslib-win32.c b/util/oslib-win32.c index e9b14ab178..caec028041 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -90,6 +90,20 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, = bool shared) return ptr; } =20 +void *qemu_anon_ram_alloc_resizable(size_t size, size_t max_size, + uint64_t *align, bool shared) +{ + /* resizable ram not implemented yet */ + return NULL; +} + +void *qemu_anon_ram_resize(void *ptr, size_t old_size, size_t new_size, + bool shared) +{ + /* resizable ram not implemented yet */ + return NULL; +} + void qemu_vfree(void *ptr) { trace_qemu_vfree(ptr); diff --git a/util/trace-events b/util/trace-events index 226f406c46..05ec1eb9f3 100644 --- a/util/trace-events +++ b/util/trace-events @@ -46,8 +46,10 @@ qemu_co_mutex_unlock_return(void *mutex, void *self) "mu= tex %p self %p" # oslib-posix.c qemu_memalign(size_t alignment, size_t size, void *ptr) "alignment %zu siz= e %zu ptr %p" qemu_anon_ram_alloc(size_t size, void *ptr) "size %zu ptr %p" +qemu_anon_ram_alloc_resizable(size_t size, size_t max_size, void *ptr) "si= ze %zu max_size %zu ptr %p" +qemu_anon_ram_resize(size_t old_size, size_t new_size, void *ptr) "old_siz= e %zu new_size %zu ptr %p" qemu_vfree(void *ptr) "ptr %p" -qemu_anon_ram_free(void *ptr, size_t size) "ptr %p size %zu" +qemu_anon_ram_free(void *ptr, size_t max_size) "ptr %p max_size %zu" =20 # hbitmap.c hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned = long cur) "hb %p hbi %p pos %"PRId64" cur 0x%lx" --=20 2.24.1 From nobody Thu Nov 13 13:21:22 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580755285531536.8780133410852; Mon, 3 Feb 2020 10:41:25 -0800 (PST) Received: from localhost ([::1]:45910 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygey-0003At-FZ for importer@patchew.org; Mon, 03 Feb 2020 13:41:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:60541) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iygWd-0000HT-DU for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iygWb-0002Wf-Ki for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:47 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:28195 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iygWb-0002Vs-Eh for qemu-devel@nongnu.org; Mon, 03 Feb 2020 13:32:45 -0500 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-306-nRMtSB4_O56jRFtQH_uOFQ-1; Mon, 03 Feb 2020 13:32:38 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8F15618B9FC3; Mon, 3 Feb 2020 18:32:37 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-77.ams2.redhat.com [10.36.117.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3D01B5DA83; Mon, 3 Feb 2020 18:32:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754765; 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=KBM8jyQDagczZDqEQCbJ/V7WjaqPEAqfu2ia6ljBAj4=; b=W+v49/QMcJm5iA/QhcbymQm0FAawFeceLjAoD+t9QMuaf+/4Xrun8hi63sVol+XjKnLc61 K9PFL18ko8paoWaD3Ep8Xa6jIWg+YMts3nD1G4ggAyxJVaKBNwVUY5YfkmepGg30duZbRQ 0FkuDNdga7GsMpI82fuMmdr4Na2OZeg= From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v1 13/13] exec: Ram blocks with resizable anonymous allocations under POSIX Date: Mon, 3 Feb 2020 19:31:25 +0100 Message-Id: <20200203183125.164879-14-david@redhat.com> In-Reply-To: <20200203183125.164879-1-david@redhat.com> References: <20200203183125.164879-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: nRMtSB4_O56jRFtQH_uOFQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 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: Eduardo Habkost , "Michael S . Tsirkin" , Stefan Weil , David Hildenbrand , "Dr . David Alan Gilbert" , Paolo Bonzini , Richard Henderson 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" We can now make use of resizable anonymous allocations to implement actually resizable ram blocks. Resizable anonymous allocations are not implemented under WIN32 yet and are not available when using alternative allocators. Fall back to the existing handling. We also have to fallback to the existing handling in case any ram block notifier does not support resizing (esp., AMD SEV, HAX) yet. Remember in RAM_RESIZEABLE_ALLOC if we are using resizable anonymous allocations. As the mmap()-hackery will invalidate some madvise settings, we have to re-apply them after resizing. After resizing, notify the ram block notifiers. The benefit of actually resizable ram blocks is that e.g., under Linux, only the actual size will be reserved (even if "/proc/sys/vm/overcommit_memory" is set to "never"). Additional memory will be reserved when trying to resize, which allows to have ram blocks that start small but can theoretically grow very large. Cc: Richard Henderson Cc: Paolo Bonzini Cc: "Dr. David Alan Gilbert" Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Stefan Weil Signed-off-by: David Hildenbrand --- exec.c | 68 +++++++++++++++++++++++++++++++++++---- hw/core/numa.c | 10 ++++-- include/exec/cpu-common.h | 2 ++ include/exec/memory.h | 8 +++++ 4 files changed, 79 insertions(+), 9 deletions(-) diff --git a/exec.c b/exec.c index fc65c4f7ca..a59d1efde3 100644 --- a/exec.c +++ b/exec.c @@ -2053,6 +2053,16 @@ void qemu_ram_unset_migratable(RAMBlock *rb) rb->flags &=3D ~RAM_MIGRATABLE; } =20 +bool qemu_ram_is_resizable(RAMBlock *rb) +{ + return rb->flags & RAM_RESIZEABLE; +} + +bool qemu_ram_is_resizable_alloc(RAMBlock *rb) +{ + return rb->flags & RAM_RESIZEABLE_ALLOC; +} + /* Called with iothread lock held. */ void qemu_ram_set_idstr(RAMBlock *new_block, const char *name, DeviceState= *dev) { @@ -2139,6 +2149,8 @@ static void qemu_ram_apply_settings(void *host, size_= t length) */ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp) { + const uint64_t oldsize =3D block->used_length; + assert(block); =20 newsize =3D HOST_PAGE_ALIGN(newsize); @@ -2147,7 +2159,7 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsi= ze, Error **errp) return 0; } =20 - if (!(block->flags & RAM_RESIZEABLE)) { + if (!qemu_ram_is_resizable(block)) { error_setg_errno(errp, EINVAL, "Length mismatch: %s: 0x" RAM_ADDR_FMT " in !=3D 0x" RAM_ADDR_FMT, block->idstr, @@ -2163,10 +2175,26 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t new= size, Error **errp) return -EINVAL; } =20 + if (qemu_ram_is_resizable_alloc(block)) { + g_assert(ram_block_notifiers_support_resize()); + if (qemu_anon_ram_resize(block->host, block->used_length, + newsize, block->flags & RAM_SHARED) =3D= =3D NULL) { + error_setg_errno(errp, -ENOMEM, + "Could not allocate enough memory."); + return -ENOMEM; + } + } + cpu_physical_memory_clear_dirty_range(block->offset, block->used_lengt= h); block->used_length =3D newsize; cpu_physical_memory_set_dirty_range(block->offset, block->used_length, DIRTY_CLIENTS_ALL); + if (block->host && qemu_ram_is_resizable_alloc(block)) { + /* re-apply settings that might have been overriden by the resize = */ + qemu_ram_apply_settings(block->host, block->max_length); + ram_block_notify_resized(block->host, oldsize, block->used_length); + } + memory_region_set_size(block->mr, newsize); if (block->resized) { block->resized(block->idstr, newsize, block->host); @@ -2249,6 +2277,28 @@ static void dirty_memory_extend(ram_addr_t old_ram_s= ize, } } =20 +static void ram_block_alloc_ram(RAMBlock *rb) +{ + const bool shared =3D qemu_ram_is_shared(rb); + + /* + * If we can, try to allocate actually resizable ram. Will also fail + * if qemu_anon_ram_alloc_resizable() is not implemented. + */ + if (phys_mem_alloc =3D=3D qemu_anon_ram_alloc && + qemu_ram_is_resizable(rb) && + ram_block_notifiers_support_resize()) { + rb->host =3D qemu_anon_ram_alloc_resizable(rb->used_length, + rb->max_length, &rb->mr->= align, + shared); + if (rb->host) { + rb->flags |=3D RAM_RESIZEABLE_ALLOC; + return; + } + } + rb->host =3D phys_mem_alloc(rb->max_length, &rb->mr->align, shared); +} + static void ram_block_add(RAMBlock *new_block, Error **errp) { RAMBlock *block; @@ -2271,9 +2321,7 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp) return; } } else { - new_block->host =3D phys_mem_alloc(new_block->max_length, - &new_block->mr->align, - qemu_ram_is_shared(new_block)= ); + ram_block_alloc_ram(new_block); if (!new_block->host) { error_setg_errno(errp, errno, "cannot set up guest memory '%s'", @@ -2319,7 +2367,11 @@ static void ram_block_add(RAMBlock *new_block, Error= **errp) =20 if (new_block->host) { qemu_ram_apply_settings(new_block->host, new_block->max_length); - ram_block_notify_add(new_block->host, new_block->max_length); + if (qemu_ram_is_resizable_alloc(new_block)) { + ram_block_notify_add(new_block->host, new_block->used_length); + } else { + ram_block_notify_add(new_block->host, new_block->max_length); + } } } =20 @@ -2502,7 +2554,11 @@ void qemu_ram_free(RAMBlock *block) } =20 if (block->host) { - ram_block_notify_remove(block->host, block->max_length); + if (qemu_ram_is_resizable_alloc(block)) { + ram_block_notify_remove(block->host, block->used_length); + } else { + ram_block_notify_remove(block->host, block->max_length); + } } =20 qemu_mutex_lock_ramlist(); diff --git a/hw/core/numa.c b/hw/core/numa.c index 5ccfcbcd41..cb75097b26 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -901,12 +901,16 @@ void query_numa_node_mem(NumaNodeMem node_mem[], Mach= ineState *ms) =20 static int ram_block_notify_add_single(RAMBlock *rb, void *opaque) { - ram_addr_t size =3D qemu_ram_get_max_length(rb); void *host =3D qemu_ram_get_host_addr(rb); RAMBlockNotifier *notifier =3D opaque; =20 - if (host) { - notifier->ram_block_added(notifier, host, size); + if (!host) { + return 0; + } + if (qemu_ram_is_resizable_alloc(rb)) { + notifier->ram_block_added(notifier, host, qemu_ram_get_used_length= (rb)); + } else { + notifier->ram_block_added(notifier, host, qemu_ram_get_max_length(= rb)); } return 0; } diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index 9760ac9068..a9c76bd5ef 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -66,6 +66,8 @@ void qemu_ram_set_uf_zeroable(RAMBlock *rb); bool qemu_ram_is_migratable(RAMBlock *rb); void qemu_ram_set_migratable(RAMBlock *rb); void qemu_ram_unset_migratable(RAMBlock *rb); +bool qemu_ram_is_resizable(RAMBlock *rb); +bool qemu_ram_is_resizable_alloc(RAMBlock *rb); =20 size_t qemu_ram_pagesize(RAMBlock *block); size_t qemu_ram_pagesize_largest(void); diff --git a/include/exec/memory.h b/include/exec/memory.h index e85b7de99a..19417943a2 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -129,6 +129,14 @@ typedef struct IOMMUNotifier IOMMUNotifier; /* RAM is a persistent kind memory */ #define RAM_PMEM (1 << 5) =20 +/* + * Implies RAM_RESIZEABLE. Memory beyond the used_length is inaccessible + * (esp. initially and after resizing). For such memory blocks, only the + * used_length is reserved in the OS - resizing might fail. Will only be + * used with host OS support and if all ram block notifiers support resizi= ng. + */ +#define RAM_RESIZEABLE_ALLOC (1 << 6) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end, --=20 2.24.1