From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732499; cv=none; d=zohomail.com; s=zohoarc; b=dQW6KkCEfGUt8d1EObzAG3erfsKbSmln7qWBp9F6RT08f9rrCtIbFMzO8TYBn3d/GWYN9xKGXhiFVQNFiYdDpfx9m5HpyM141X+tpF4aFFSh20aAob+ujquiQUU2LenYlGhBuXxMARUQN+fU2Zvox5wW+2r2wP6LjiTqUl/55W8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732499; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4OCFmF6ltN/CWDF6D+mlr5dH63y+a50VJFncESwHm6I=; b=EPux8v3y2OuCaEZZzXzbTD+YyL+wg8/P4BfDD1BJsA0/vv1W0VCb09YW67yIjtJdJzsGNYij6bdJBRN5dphCbQT1Q+SeTArshoF6lm+dXVg6WpAujYcDoz4Al88ltUZQ7xmncJ1j9FSX8fShfaV2OFyzqqMVJJq2NKtH9ug8xgE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732499869393.8695540645847; Wed, 26 Feb 2020 07:54:59 -0800 (PST) Received: from localhost ([::1]:46198 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z1W-000420-Cj for importer@patchew.org; Wed, 26 Feb 2020 10:54:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33866) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z06-0002QV-Px for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z04-0002Ed-Jg for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:29 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:39644 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 1j6z04-0002Dg-9U for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:28 -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-39-k2EybYVRPi6n97NCc--TLw-1; Wed, 26 Feb 2020 10:53:25 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 67BFF190B2B7; Wed, 26 Feb 2020 15:53:24 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id E8D72101D482; Wed, 26 Feb 2020 15:53:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732407; 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=4OCFmF6ltN/CWDF6D+mlr5dH63y+a50VJFncESwHm6I=; b=QSOtY/IB/9FY+BeJqUKJzCVaFIzP5MipblaMpi1lg3WV2w14/yF7cgKo8nMmQSj+D6ad6g IcQDStMPZesQE6qkA8Wy/tHm07vnyQs6ph6DBBWj6aSHe3jKflbWLl7a+HXlyP6A0XWygo x4P16itpEUBasjuTC12qKMy/jsiXaao= X-MC-Unique: k2EybYVRPi6n97NCc--TLw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 01/13] util: vfio-helpers: Factor out and fix processing of existing ram blocks Date: Wed, 26 Feb 2020 16:52:52 +0100 Message-Id: <20200226155304.60219-2-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , Stefan Hajnoczi , "Dr . David Alan Gilbert" , Peter Xu , 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 for now. 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(). Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Alex Williamson Cc: Stefan Hajnoczi Cc: Peter Xu Signed-off-by: David Hildenbrand --- exec.c | 5 +++++ hw/core/numa.c | 14 ++++++++++++++ include/exec/cpu-common.h | 1 + util/vfio-helpers.c | 29 ++++++++--------------------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/exec.c b/exec.c index 231d6e5641..b2a65b793f 100644 --- a/exec.c +++ b/exec.c @@ -1965,6 +1965,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 316bc50d75..dc5e5b4046 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -854,9 +854,23 @@ void query_numa_node_mem(NumaNodeMem node_mem[], Machi= neState *ms) } } =20 +static int ram_block_notify_add_single(RAMBlock *rb, void *opaque) +{ + const ram_addr_t max_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, max_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 b47e5630e7..09decb8d93 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/util/vfio-helpers.c b/util/vfio-helpers.c index ddd9a96e76..260570ae19 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -376,8 +376,14 @@ 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: %s", host, size, + strerror(-ret)); + } } =20 static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n, @@ -390,33 +396,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 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732500; cv=none; d=zohomail.com; s=zohoarc; b=d3kQcvwc1uWHkv7iKGdxnD7CAOyGBXbgHBXz3nNY0zVMV/1CN3O/HnEQg/GK17r/BjeeZNeuQMOwMAJ18QWo5EMj43EW+sarCHQrZMgflfGtfclSmlQPu+yGmQou4UuVfrFtXTgZfJFvGkOCuseNjfRjeEy5s7QXfpkJKs3H+IE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732500; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=u4+w8Dl/8EJ3e5XX4k6et7mBYqdEHtW545t5121MYbE=; b=T495ZR5fgmP1o3W1uonhYvNKhzWnwx8bjrym+qDZaKNN4Tdk3n+BjvrYBcsW5rC2Glw0hxwwjpPh610k1Q6bDez/rL0UL/Ojb+Ef2C2KE3H4YsVkcdD+ndpNfaW0v2gKpSZxFluaEmxRuZmzKKQ77ucKF6WreGANizdGjeXi1mU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 158273250072845.28818828808551; Wed, 26 Feb 2020 07:55:00 -0800 (PST) Received: from localhost ([::1]:46200 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z1X-00047j-He for importer@patchew.org; Wed, 26 Feb 2020 10:54:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33922) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0F-0002RZ-7A for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0B-0002OQ-6f for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:39 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:27860 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 1j6z0B-0002NO-2t for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:35 -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-320-cm8NNNWBMK6FYiV-8Pb7CA-1; Wed, 26 Feb 2020 10:53:32 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9EAE8107ACC7; Wed, 26 Feb 2020 15:53:31 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id D1197101D482; Wed, 26 Feb 2020 15:53:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732414; 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=u4+w8Dl/8EJ3e5XX4k6et7mBYqdEHtW545t5121MYbE=; b=guXvnQVVd3ts9ZktAk4AftKWi7mrTw2wWWtmc69mekttfXzJzeRZI7zDy87+Oycyd/2vc4 Zb1dPVETEhLyaqOntiIRNYSNamyYkNXPetD+B2gR/fFRkgK5rzKZYcU+h0TW0HiHzsh8Ah gEPkPQ0hipTRtCUdRkmgU9YP6+5AXvI= X-MC-Unique: cm8NNNWBMK6FYiV-8Pb7CA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 02/13] stubs/ram-block: Remove stubs that are no longer needed Date: Wed, 26 Feb 2020 16:52:53 +0100 Message-Id: <20200226155304.60219-3-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , 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" Current code no longer needs these stubs to compile. Let's just remove them. Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: Eduardo Habkost Cc: Peter Xu Signed-off-by: David Hildenbrand --- stubs/ram-block.c | 20 -------------------- 1 file changed, 20 deletions(-) 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; -} --=20 2.24.1 From nobody Thu Nov 13 20:38:15 2025 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582732459; cv=none; d=zohomail.com; s=zohoarc; b=d3Y2k6z8yX9gE4bHWFICE2zu7T7N4VXjqhSTeHxj7kDzVpI1pURgB3P1sVKX24CilL5g28+/bHLJhkBRqWOUCxORrVnWTcPKNBXR3Z1wRdtW5bbz4XGQVmc4El3YXwvf/kdH7EAgrPMgid75jdDmfPPX6nDwRScWImyrOFrvGug= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732459; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=tWmKcEPozboP5GxxKGtaxvmfI3o/iOEGL/A16RCP/XE=; b=EjX0LAuxGzAEXbW+9aA0UkJiNvuVD7MrOtXVrC4oya0C8Ft0ISvuob9sE/OxbGpAcKY2N83MaOOkFiE3CxJBV/d1X1OApA+MxjnPkl9GKSx5Cl7eY6+PF/UfZljcm4ISJvXw0sHClmU8F5CLOI4SqdzwsiFbpdL27/geMhFt6yk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1582732459672983.9584622477435; Wed, 26 Feb 2020 07:54:19 -0800 (PST) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j6z0J-0004bX-94; Wed, 26 Feb 2020 15:53:43 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j6z0I-0004bS-Lm for xen-devel@lists.xenproject.org; Wed, 26 Feb 2020 15:53:42 +0000 Received: from us-smtp-delivery-1.mimecast.com (unknown [207.211.31.120]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id 2835666c-58b0-11ea-9471-12813bfff9fa; Wed, 26 Feb 2020 15:53:41 +0000 (UTC) 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-255-UfpxVpTxPGiiAisvxOWLQw-1; Wed, 26 Feb 2020 10:53:39 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BDBCC881293; Wed, 26 Feb 2020 15:53:37 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id EDE10100E7E3; Wed, 26 Feb 2020 15:53:31 +0000 (UTC) X-Inumbo-ID: 2835666c-58b0-11ea-9471-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732420; 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=M/NO9ujX8YYZ3QrrhXQKFjMK8w9A/HGCMrKEjwOf1Ew=; b=C3Did1uFkJM9oLhxmynw0EF2eFZVziIfOdLGMSoRLu7lF4QzY/Mj06EUS8Fd0ftQoSpEJf ozjSMwkgF4/1H2t/hcAanZS5d1k9cJqYICTpJb6XvaEXP6PzondRUrRtdEd6KSQ3XDaKpy sa0c6umxB8+VJ5PY75xBYRDeykDXiVs= X-MC-Unique: UfpxVpTxPGiiAisvxOWLQw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Date: Wed, 26 Feb 2020 16:52:54 +0100 Message-Id: <20200226155304.60219-4-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [Xen-devel] [PATCH v3 03/13] numa: Teach ram block notifiers about resizeable ram blocks X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Eduardo Habkost , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Paul Durrant , Igor Mammedov , "Michael S. Tsirkin" , Marcel Apfelbaum , xen-devel@lists.xenproject.org, Anthony Perard , Paolo Bonzini , Richard Henderson Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Ram block notifiers are currently not aware of resizes. Especially to handle resizes during migration, but also to implement actually resizeable ram blocks (make everything between used_length and max_length inaccessible), we want to teach ram block notifiers about resizeable ram. Introduce the basic infrastructure but keep using max_size in the existing notifiers. Supply the max_size when adding and removing ram blocks. Also, notify on resizes. Acked-by: Paul Durrant Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: "Dr. David Alan Gilbert" Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Stefano Stabellini Cc: Anthony Perard Cc: Paul Durrant Cc: "Michael S. Tsirkin" Cc: xen-devel@lists.xenproject.org Cc: Igor Mammedov Signed-off-by: David Hildenbrand --- exec.c | 13 +++++++++++-- hw/core/numa.c | 22 +++++++++++++++++----- hw/i386/xen/xen-mapcache.c | 7 ++++--- include/exec/ramlist.h | 13 +++++++++---- target/i386/hax-mem.c | 5 +++-- target/i386/sev.c | 18 ++++++++++-------- util/vfio-helpers.c | 16 ++++++++-------- 7 files changed, 62 insertions(+), 32 deletions(-) diff --git a/exec.c b/exec.c index b2a65b793f..3ee1498761 100644 --- a/exec.c +++ b/exec.c @@ -2078,6 +2078,8 @@ static int memory_try_enable_merging(void *addr, size= _t len) */ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp) { + const ram_addr_t oldsize =3D block->used_length; + assert(block); =20 newsize =3D HOST_PAGE_ALIGN(newsize); @@ -2102,6 +2104,11 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t news= ize, Error **errp) return -EINVAL; } =20 + /* Notify before modifying the ram block and touching the bitmaps. */ + if (block->host) { + ram_block_notify_resize(block->host, oldsize, newsize); + } + 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, @@ -2268,7 +2275,8 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp, bool shared) qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_DONTFORK); } - ram_block_notify_add(new_block->host, new_block->max_length); + ram_block_notify_add(new_block->host, new_block->used_length, + new_block->max_length); } } =20 @@ -2448,7 +2456,8 @@ void qemu_ram_free(RAMBlock *block) } =20 if (block->host) { - ram_block_notify_remove(block->host, block->max_length); + ram_block_notify_remove(block->host, block->used_length, + block->max_length); } =20 qemu_mutex_lock_ramlist(); diff --git a/hw/core/numa.c b/hw/core/numa.c index dc5e5b4046..fe6ca5c50d 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -857,11 +857,12 @@ void query_numa_node_mem(NumaNodeMem node_mem[], Mach= ineState *ms) static int ram_block_notify_add_single(RAMBlock *rb, void *opaque) { const ram_addr_t max_size =3D qemu_ram_get_max_length(rb); + const ram_addr_t size =3D qemu_ram_get_used_length(rb); void *host =3D qemu_ram_get_host_addr(rb); RAMBlockNotifier *notifier =3D opaque; =20 if (host) { - notifier->ram_block_added(notifier, host, max_size); + notifier->ram_block_added(notifier, host, size, max_size); } return 0; } @@ -878,20 +879,31 @@ void ram_block_notifier_remove(RAMBlockNotifier *n) QLIST_REMOVE(n, next); } =20 -void ram_block_notify_add(void *host, size_t size) +void ram_block_notify_add(void *host, size_t size, size_t max_size) { RAMBlockNotifier *notifier; =20 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { - notifier->ram_block_added(notifier, host, size); + notifier->ram_block_added(notifier, host, size, max_size); } } =20 -void ram_block_notify_remove(void *host, size_t size) +void ram_block_notify_remove(void *host, size_t size, size_t max_size) { RAMBlockNotifier *notifier; =20 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { - notifier->ram_block_removed(notifier, host, size); + notifier->ram_block_removed(notifier, host, size, max_size); + } +} + +void ram_block_notify_resize(void *host, size_t old_size, size_t new_size) +{ + RAMBlockNotifier *notifier; + + QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { + if (notifier->ram_block_resized) { + notifier->ram_block_resized(notifier, host, old_size, new_size= ); + } } } diff --git a/hw/i386/xen/xen-mapcache.c b/hw/i386/xen/xen-mapcache.c index 5b120ed44b..d6dcea65d1 100644 --- a/hw/i386/xen/xen-mapcache.c +++ b/hw/i386/xen/xen-mapcache.c @@ -169,7 +169,8 @@ static void xen_remap_bucket(MapCacheEntry *entry, =20 if (entry->vaddr_base !=3D NULL) { if (!(entry->flags & XEN_MAPCACHE_ENTRY_DUMMY)) { - ram_block_notify_remove(entry->vaddr_base, entry->size); + ram_block_notify_remove(entry->vaddr_base, entry->size, + entry->size); } if (munmap(entry->vaddr_base, entry->size) !=3D 0) { perror("unmap fails"); @@ -211,7 +212,7 @@ static void xen_remap_bucket(MapCacheEntry *entry, } =20 if (!(entry->flags & XEN_MAPCACHE_ENTRY_DUMMY)) { - ram_block_notify_add(vaddr_base, size); + ram_block_notify_add(vaddr_base, size, size); } =20 entry->vaddr_base =3D vaddr_base; @@ -452,7 +453,7 @@ static void xen_invalidate_map_cache_entry_unlocked(uin= t8_t *buffer) } =20 pentry->next =3D entry->next; - ram_block_notify_remove(entry->vaddr_base, entry->size); + ram_block_notify_remove(entry->vaddr_base, entry->size, entry->size); if (munmap(entry->vaddr_base, entry->size) !=3D 0) { perror("unmap fails"); exit(-1); diff --git a/include/exec/ramlist.h b/include/exec/ramlist.h index bc4faa1b00..293c0ddabe 100644 --- a/include/exec/ramlist.h +++ b/include/exec/ramlist.h @@ -65,15 +65,20 @@ void qemu_mutex_lock_ramlist(void); void qemu_mutex_unlock_ramlist(void); =20 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_added)(RAMBlockNotifier *n, void *host, size_t size, + size_t max_size); + void (*ram_block_removed)(RAMBlockNotifier *n, void *host, size_t size, + size_t max_size); + void (*ram_block_resized)(RAMBlockNotifier *n, void *host, size_t old_= size, + size_t new_size); QLIST_ENTRY(RAMBlockNotifier) next; }; =20 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_add(void *host, size_t size, size_t max_size); +void ram_block_notify_remove(void *host, size_t size, size_t max_size); +void ram_block_notify_resize(void *host, size_t old_size, size_t new_size); =20 void ram_block_dump(Monitor *mon); =20 diff --git a/target/i386/hax-mem.c b/target/i386/hax-mem.c index 6bb5a24917..454d7fb212 100644 --- a/target/i386/hax-mem.c +++ b/target/i386/hax-mem.c @@ -293,7 +293,8 @@ static MemoryListener hax_memory_listener =3D { .priority =3D 10, }; =20 -static void hax_ram_block_added(RAMBlockNotifier *n, void *host, size_t si= ze) +static void hax_ram_block_added(RAMBlockNotifier *n, void *host, size_t si= ze, + size_t max_size) { /* * We must register each RAM block with the HAXM kernel module, or @@ -304,7 +305,7 @@ static void hax_ram_block_added(RAMBlockNotifier *n, vo= id *host, size_t size) * host physical pages for the RAM block as part of this registration * process, hence the name hax_populate_ram(). */ - if (hax_populate_ram((uint64_t)(uintptr_t)host, size) < 0) { + if (hax_populate_ram((uint64_t)(uintptr_t)host, max_size) < 0) { fprintf(stderr, "HAX failed to populate RAM\n"); abort(); } diff --git a/target/i386/sev.c b/target/i386/sev.c index 024bb24e51..6b4cee24a2 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -129,7 +129,8 @@ sev_set_guest_state(SevState new_state) } =20 static void -sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size) +sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size, + size_t max_size) { int r; struct kvm_enc_region range; @@ -146,19 +147,20 @@ sev_ram_block_added(RAMBlockNotifier *n, void *host, = size_t size) } =20 range.addr =3D (__u64)(unsigned long)host; - range.size =3D size; + range.size =3D max_size; =20 - trace_kvm_memcrypt_register_region(host, size); + trace_kvm_memcrypt_register_region(host, max_size); r =3D kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_REG_REGION, &range); if (r) { error_report("%s: failed to register region (%p+%#zx) error '%s'", - __func__, host, size, strerror(errno)); + __func__, host, max_size, strerror(errno)); exit(1); } } =20 static void -sev_ram_block_removed(RAMBlockNotifier *n, void *host, size_t size) +sev_ram_block_removed(RAMBlockNotifier *n, void *host, size_t size, + size_t max_size) { int r; struct kvm_enc_region range; @@ -175,13 +177,13 @@ sev_ram_block_removed(RAMBlockNotifier *n, void *host= , size_t size) } =20 range.addr =3D (__u64)(unsigned long)host; - range.size =3D size; + range.size =3D max_size; =20 - trace_kvm_memcrypt_unregister_region(host, size); + trace_kvm_memcrypt_unregister_region(host, max_size); r =3D kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_UNREG_REGION, &range); if (r) { error_report("%s: failed to unregister region (%p+%#zx)", - __func__, host, size); + __func__, host, max_size); } } =20 diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c index 260570ae19..9ec01bfe26 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -372,26 +372,26 @@ fail_container: return ret; } =20 -static void qemu_vfio_ram_block_added(RAMBlockNotifier *n, - void *host, size_t size) +static void qemu_vfio_ram_block_added(RAMBlockNotifier *n, void *host, + size_t size, size_t max_size) { QEMUVFIOState *s =3D container_of(n, QEMUVFIOState, ram_notifier); int ret; =20 - trace_qemu_vfio_ram_block_added(s, host, size); - ret =3D qemu_vfio_dma_map(s, host, size, false, NULL); + trace_qemu_vfio_ram_block_added(s, host, max_size); + ret =3D qemu_vfio_dma_map(s, host, max_size, false, NULL); if (ret) { - error_report("qemu_vfio_dma_map(%p, %zu) failed: %s", host, size, + error_report("qemu_vfio_dma_map(%p, %zu) failed: %s", host, max_si= ze, strerror(-ret)); } } =20 -static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n, - void *host, size_t size) +static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n, void *host, + size_t size, size_t max_size) { QEMUVFIOState *s =3D container_of(n, QEMUVFIOState, ram_notifier); if (host) { - trace_qemu_vfio_ram_block_removed(s, host, size); + trace_qemu_vfio_ram_block_removed(s, host, max_size); qemu_vfio_dma_unmap(s, host); } } --=20 2.24.1 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732507; cv=none; d=zohomail.com; s=zohoarc; b=UCa4RBpHMYWzfAgtGLiqPzpT4/uub/VTAgUfYkfAh90fXZpi16EYFI+A7hsifC49cYWeVZ6StXfFkET/cv5esktcO7iB4Y0GwZ/q2oZHdk21mdNZJSvffbReH7oruMv11yATGjFVRYAX7cgi45eQGkUl3XjBUTUDYFnzyKvApuI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732507; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5SAil29C2B/8RRVuO+5knjn0nj8iH/o7OggV0iKssVQ=; b=LDcBHwTzw7uGv8WsTCVogCXkfywZfMjx6nE967OL8M+e1AfOX6dn2h6ituni7B6IdFJ989e+9z6e6kSIPnPdcr80r1tjTDjmbwbzIPCL2qSjz6lgLJ/xmC365FiWyNicKTrGHGm1DKQVnQwJMa6NINJKysW7jZicODCb7FbdEDg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732507458915.4636834145945; Wed, 26 Feb 2020 07:55:07 -0800 (PST) Received: from localhost ([::1]:46202 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z1e-0004QO-9j for importer@patchew.org; Wed, 26 Feb 2020 10:55:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33966) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0M-0002gM-HZ for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0L-0002oG-F5 for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:46 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:48494 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 1j6z0L-0002mc-Aj for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53: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-160-lpyoPsZ_PieWswSaBSDR9w-1; Wed, 26 Feb 2020 10:53:41 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 325DC1005513; Wed, 26 Feb 2020 15:53:40 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 25728100EBAF; Wed, 26 Feb 2020 15:53:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732424; 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=5SAil29C2B/8RRVuO+5knjn0nj8iH/o7OggV0iKssVQ=; b=GobRxe3BCNl9IfkhZU1LDg628fdnbLA+Vv/t7eso0+YV6XOCiKAT+VIWhk6qI+JYW75+RD glmlSHbjLaBkiKKRS8kDuRwwQh0+pZaUlbxnIeQgFncrNh8qiQFq+J9cVS6ClpnXj8kvey W23bXYPS8IKaBTQHbof7V4hoiNch8BM= X-MC-Unique: lpyoPsZ_PieWswSaBSDR9w-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 04/13] numa: Make all callbacks of ram block notifiers optional Date: Wed, 26 Feb 2020 16:52:55 +0100 Message-Id: <20200226155304.60219-5-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , 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 make add/remove optional. We want to introduce a RAM block notifier for RAM migration, that's only interested in resizes. Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Peter Xu Signed-off-by: David Hildenbrand --- hw/core/numa.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/hw/core/numa.c b/hw/core/numa.c index fe6ca5c50d..37ce175e13 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -870,8 +870,11 @@ static int ram_block_notify_add_single(RAMBlock *rb, v= oid *opaque) 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); + if (n->ram_block_added) { + qemu_ram_foreach_block(ram_block_notify_add_single, n); + } } =20 void ram_block_notifier_remove(RAMBlockNotifier *n) @@ -884,7 +887,9 @@ void ram_block_notify_add(void *host, size_t size, size= _t max_size) RAMBlockNotifier *notifier; =20 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { - notifier->ram_block_added(notifier, host, size, max_size); + if (notifier->ram_block_added) { + notifier->ram_block_added(notifier, host, size, max_size); + } } } =20 @@ -893,7 +898,9 @@ void ram_block_notify_remove(void *host, size_t size, s= ize_t max_size) RAMBlockNotifier *notifier; =20 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { - notifier->ram_block_removed(notifier, host, size, max_size); + if (notifier->ram_block_removed) { + notifier->ram_block_removed(notifier, host, size, max_size); + } } } =20 --=20 2.24.1 From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732579; cv=none; d=zohomail.com; s=zohoarc; b=JUz7aN3T87JM3Xtj+0dHSJWktg23i3wLZmaj0fN3jFeQP5TSbyc3YFJZGtbEyy9r44FMKSrkVK/cw4RUJqjbystXFoQJHtiuyFJepH95w5oY2SecKE0WAbGpv0vD2B0dcPkgZDnodQ9StYWMHP6ms5ZKydCngWdM/fD3Q2TB8uo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732579; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Wb2tpZhcNHuK86Bu9rarf6gO7w2/fY5IC7TQBrIOsrs=; b=GtNLjKujs0HgoqBXutZj62V4NJ/Gnv4u5cycScvN3an2w+8ziWEwsqwBc6nVmc/nQy7U3pCw4RKGd1ki6RW+Mm3UZ+WS8GL6uG94WKsJpTy1vdcow/M49hnQkKvF4EJiZ1U3BXxv5sQQqW0vS949gXH36xLorPB8sSVusGDW3YY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732579802721.7323938519918; Wed, 26 Feb 2020 07:56:19 -0800 (PST) Received: from localhost ([::1]:46236 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z2o-0007R3-Mu for importer@patchew.org; Wed, 26 Feb 2020 10:56:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33981) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0N-0002ji-TV for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0M-0002s8-Du for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:47 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:47211 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 1j6z0M-0002q5-8X for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:46 -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-213-1YkChqa4PeKK6jC8JBcsKA-1; Wed, 26 Feb 2020 10:53:44 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1B69B8018CE; Wed, 26 Feb 2020 15:53:43 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7EA4A100E7E3; Wed, 26 Feb 2020 15:53:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732425; 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=Wb2tpZhcNHuK86Bu9rarf6gO7w2/fY5IC7TQBrIOsrs=; b=MtRXZZExh5uFpieDDGMY94YwXNIXeCdFbzAdUPsThsoaUdBd3ulk9Lk8/OemVqRIkQqg9u xbWlQ/L9rQrfobAz+toGeKducU+XBr2+DmMyUIdDjL9/3qouP6Xpcl4+24eFDETO7W7FaT JGH12reXfgkVki1ETUjquzwKjyHCu+I= X-MC-Unique: 1YkChqa4PeKK6jC8JBcsKA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 05/13] migration/ram: Handle RAM block resizes during precopy Date: Wed, 26 Feb 2020 16:52:56 +0100 Message-Id: <20200226155304.60219-6-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , Richard Henderson , "Dr . David Alan Gilbert" , Peter Xu , "Michael S. Tsirkin" , Shannon Zhao , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , 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" Resizing while migrating is dangerous and does not work as expected. The whole migration code works on the usable_length of ram blocks and does not expect this to change at random points in time. In the case of precopy, the ram block size must not change on the source, after syncing the RAM block list in ram_save_setup(), so as long as the guest is still running on the source. Resizing can be trigger *after* (but not during) a reset in ACPI code by the guest - hw/arm/virt-acpi-build.c:acpi_ram_update() - hw/i386/acpi-build.c:acpi_ram_update() Use the ram block notifier to get notified about resizes. Let's simply cancel migration and indicate the reason. We'll continue running on the source. No harm done. Update the documentation. Postcopy will be handled separately. Reviewed-by: Peter Xu Cc: "Dr. David Alan Gilbert" Cc: Juan Quintela Cc: Eduardo Habkost Cc: Paolo Bonzini Cc: Igor Mammedov Cc: "Michael S. Tsirkin" Cc: Richard Henderson Cc: Shannon Zhao Cc: Alex Benn=C3=A9e Cc: Peter Xu Signed-off-by: David Hildenbrand --- exec.c | 5 +++-- include/exec/memory.h | 10 ++++++---- migration/migration.c | 9 +++++++-- migration/migration.h | 1 + migration/ram.c | 31 +++++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/exec.c b/exec.c index 3ee1498761..d30a5d297a 100644 --- a/exec.c +++ b/exec.c @@ -2069,8 +2069,9 @@ static int memory_try_enable_merging(void *addr, size= _t len) return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE); } =20 -/* Only legal before guest might have detected the memory size: e.g. on - * incoming migration, or right after reset. +/* + * Resizing RAM while migrating can result in the migration being canceled. + * Care has to be taken if the guest might have already detected the memor= y. * * As memory core doesn't know how is memory accessed, it is up to * resize callback to update device state and/or add assertions to detect diff --git a/include/exec/memory.h b/include/exec/memory.h index 1614d9a02c..b9b9470a56 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -113,7 +113,7 @@ typedef struct IOMMUNotifier IOMMUNotifier; #define RAM_SHARED (1 << 1) =20 /* Only a portion of RAM (used_length) is actually used, and migrated. - * This used_length size can change across reboots. + * Resizing RAM while migrating can result in the migration being canceled. */ #define RAM_RESIZEABLE (1 << 2) =20 @@ -843,7 +843,9 @@ void memory_region_init_ram_shared_nomigrate(MemoryRegi= on *mr, * RAM. Accesses into the region will * modify memory directly. Only an in= itial * portion of this RAM is actually use= d. - * The used size can change across reb= oots. + * Changing the size while migrating + * can result in the migration being + * canceled. * * @mr: the #MemoryRegion to be initialized. * @owner: the object that tracks the region's reference count @@ -1464,8 +1466,8 @@ void *memory_region_get_ram_ptr(MemoryRegion *mr); =20 /* memory_region_ram_resize: Resize a RAM region. * - * Only legal before guest might have detected the memory size: e.g. on - * incoming migration, or right after reset. + * Resizing RAM while migrating can result in the migration being canceled. + * Care has to be taken if the guest might have already detected the memor= y. * * @mr: a memory region created with @memory_region_init_resizeable_ram. * @newsize: the new size the region diff --git a/migration/migration.c b/migration/migration.c index 8fb68795dc..ac9751dbe5 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -175,13 +175,18 @@ void migration_object_init(void) } } =20 +void migration_cancel(void) +{ + migrate_fd_cancel(current_migration); +} + void migration_shutdown(void) { /* * Cancel the current migration - that will (eventually) * stop the migration using this structure */ - migrate_fd_cancel(current_migration); + migration_cancel(); object_unref(OBJECT(current_migration)); } =20 @@ -2019,7 +2024,7 @@ void qmp_migrate(const char *uri, bool has_blk, bool = blk, =20 void qmp_migrate_cancel(Error **errp) { - migrate_fd_cancel(migrate_get_current()); + migration_cancel(); } =20 void qmp_migrate_continue(MigrationStatus state, Error **errp) diff --git a/migration/migration.h b/migration/migration.h index 8473ddfc88..79fd74afa5 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -343,5 +343,6 @@ int foreach_not_ignored_block(RAMBlockIterFunc func, vo= id *opaque); void migration_make_urgent_request(void); void migration_consume_urgent_request(void); bool migration_rate_limit(void); +void migration_cancel(void); =20 #endif diff --git a/migration/ram.c b/migration/ram.c index ed23ed1c7c..39c7d1c4a6 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -52,6 +52,7 @@ #include "migration/colo.h" #include "block.h" #include "sysemu/sysemu.h" +#include "sysemu/runstate.h" #include "savevm.h" #include "qemu/iov.h" #include "multifd.h" @@ -3710,8 +3711,38 @@ static SaveVMHandlers savevm_ram_handlers =3D { .resume_prepare =3D ram_resume_prepare, }; =20 +static void ram_mig_ram_block_resized(RAMBlockNotifier *n, void *host, + size_t old_size, size_t new_size) +{ + ram_addr_t offset; + Error *err =3D NULL; + RAMBlock *rb =3D qemu_ram_block_from_host(host, false, &offset); + + if (ramblock_is_ignored(rb)) { + return; + } + + if (!migration_is_idle()) { + /* + * Precopy code on the source cannot deal with the size of RAM blo= cks + * changing at random points in time - especially after sending the + * RAM block sizes to the migration stream, they must no longer ch= ange. + * Abort and indicate a proper reason. + */ + error_setg(&err, "RAM block '%s' resized during precopy.", rb->ids= tr); + migrate_set_error(migrate_get_current(), err); + error_free(err); + migration_cancel(); + } +} + +static RAMBlockNotifier ram_mig_ram_notifier =3D { + .ram_block_resized =3D ram_mig_ram_block_resized, +}; + void ram_mig_init(void) { qemu_mutex_init(&XBZRLE.lock); register_savevm_live("ram", 0, 4, &savevm_ram_handlers, &ram_state); + ram_block_notifier_add(&ram_mig_ram_notifier); } --=20 2.24.1 From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732600; cv=none; d=zohomail.com; s=zohoarc; b=hoCehuhYOn2cH1Uin/+d4ollmPRI8vF+RF1Qj5wo8OL0s3XNuTYD1TJCukCbzg6auTRkokhg2rlxU8TpkhHfTtFZPnulGhYePxzxPPUpZWyq+Crei5WXb+erf2fOTXDEwKc8EACtkwILwvSuYgisl/SNEikHCjejWuzn+k1VjuQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732600; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/kEgul7L6JaBdLS5nxEDOuGO0ikOJi+PGHq3Ia4gucw=; b=JTj2ZZ0f56Ap7GkhdwPaFyJEJM0StGg0BfjiI3R0jDP/wV6QDTNLepc3ZeDNiUQpJKYD5jgU2xZN/7pa6hn/3or0/VHVpEcnu1XNxlLGpqtV4CIoXzTFzu5A82oaN26qxbkJksGbunoyei9hrLCb4RhmJFJSWgDPkshUNQ8DTpQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732600397758.6487367976406; Wed, 26 Feb 2020 07:56:40 -0800 (PST) Received: from localhost ([::1]:46242 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z39-0007mR-BT for importer@patchew.org; Wed, 26 Feb 2020 10:56:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34032) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0W-00037F-6l for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0V-000309-79 for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:56 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:57048 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 1j6z0V-0002zs-3P for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:55 -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-325-1Jb_Mj-GNf6PvjhlYmzQBw-1; Wed, 26 Feb 2020 10:53:51 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 19605107ACC4; Wed, 26 Feb 2020 15:53:50 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 66F8A100EBAF; Wed, 26 Feb 2020 15:53:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732434; 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=/kEgul7L6JaBdLS5nxEDOuGO0ikOJi+PGHq3Ia4gucw=; b=NoS63jTEuT3g1jPEjDP+VgZav6D1NJaPWGQTaRYezRbuLI3PaGfVMgWqX0KXTaViFl37uv 4lSrT1P9wVzMNwDxTQUUWyCTyrUh9NK7Gi/5aY4/0sCRaVpMmJZUNSI7fforSYJcNo5nI/ zcw36gJAj6nmLlPv1ieR+9tsFwUWLww= X-MC-Unique: 1Jb_Mj-GNf6PvjhlYmzQBw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 06/13] exec: Relax range check in ram_block_discard_range() Date: Wed, 26 Feb 2020 16:52:57 +0100 Message-Id: <20200226155304.60219-7-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , 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 make use of ram_block_discard_range() in the RAM block resize callback when growing a RAM block, *before* used_length is changed. Let's relax the check. We always have a reserved mapping for the whole max_length, so we cannot corrupt unrelated data. Reviewed-by: Peter Xu Cc: Richard Henderson Cc: Paolo Bonzini Cc: Eduardo Habkost Cc: Peter Xu Signed-off-by: David Hildenbrand --- exec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exec.c b/exec.c index d30a5d297a..9d351a7492 100644 --- a/exec.c +++ b/exec.c @@ -3876,7 +3876,7 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t st= art, size_t length) goto err; } =20 - if ((start + length) <=3D rb->used_length) { + if ((start + length) <=3D rb->max_length) { bool need_madvise, need_fallocate; if (!QEMU_IS_ALIGNED(length, rb->page_size)) { error_report("ram_block_discard_range: Unaligned length: %zx", @@ -3943,7 +3943,7 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t st= art, size_t length) } else { error_report("ram_block_discard_range: Overrun block '%s' (%" PRIu= 64 "/%zx/" RAM_ADDR_FMT")", - rb->idstr, start, length, rb->used_length); + rb->idstr, start, length, rb->max_length); } =20 err: --=20 2.24.1 From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732581; cv=none; d=zohomail.com; s=zohoarc; b=btObwqALicxpJpxbKFj2qEpuDIScKRnNCJ1BVCKpcigyF51XPiQZat71sQXbY8UkVGtz85OUVqASgWRPVl1oQtfghsrEyXeGbI3BgOGQot3+sRWv3t0yuK7ZeINWOVgUkm/uBIg1XLlpLvGvE3qGN+Lqyxq/TPUG23c149/Xz2g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732581; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=a+WbEc94kzZTn0s1M0dOc18mDrDhDwcLtFc6gH1PXWk=; b=YMT6bqOZ9HHo8CE72HxCHXRUoCDe2gEPHtZYxL8TFCO9YhM9vzNYTqrCYns5o13Vrd5Adu+fq2n1bhLldYwPnamzJ6iBHpplwuDQtQ8V8VQeh5XahE+Raz9rebEkDIxSJYo2YVDGy8uWpWpGLqe73F5DaFj/QWDP6tTBSRjKIaU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15827325819862.897565000763393; Wed, 26 Feb 2020 07:56:21 -0800 (PST) Received: from localhost ([::1]:46240 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z2q-0007Wn-St for importer@patchew.org; Wed, 26 Feb 2020 10:56:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34042) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0W-00038N-Od for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0V-00030Q-Dv for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:56 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:56826 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 1j6z0V-0002zu-9U for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:55 -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-272-FTNVMfvpOcuEta5Vcv0fIQ-1; Wed, 26 Feb 2020 10:53:53 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 224BE102C8BA; Wed, 26 Feb 2020 15:53:52 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 632D31001B2C; Wed, 26 Feb 2020 15:53:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732434; 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=a+WbEc94kzZTn0s1M0dOc18mDrDhDwcLtFc6gH1PXWk=; b=LNHfYJC9kblM0LZQGxow7PP5hRU5FFeW3W1am9OSzvu+6wfhrSQqSlWsXPZUkbkO2jcLkq vZ94jaFUqHdV5IH6Ru+5F3vh05J1aFRdnHM+vQMFSpPqUhctbl2jRDUU5R9tXaBLwTl2Ya tpDmqniv+JBncsEGvSs3aKJEIHIqj5s= X-MC-Unique: FTNVMfvpOcuEta5Vcv0fIQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 07/13] migration/ram: Discard RAM when growing RAM blocks after ram_postcopy_incoming_init() Date: Wed, 26 Feb 2020 16:52:58 +0100 Message-Id: <20200226155304.60219-8-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , 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" In case we grow our RAM after ram_postcopy_incoming_init() (e.g., when synchronizing the RAM block state with the migration source), the resized part would not get discarded. Let's perform that when being notified about a resize while postcopy has been advised, but is not listening yet. With precopy, the process is as following: 1. VM created - RAM blocks are created 2. Incomming migration started - Postcopy is advised - All pages in RAM blocks are discarded 3. Precopy starts - RAM blocks are resized to match the size on the migration source. - RAM pages from precopy stream are loaded - Uffd handler is registered, postcopy starts listening 4. Guest started, postcopy running - Pagefaults get resolved, pages get placed Reviewed-by: Peter Xu Cc: "Dr. David Alan Gilbert" Cc: Juan Quintela Cc: Peter Xu Signed-off-by: David Hildenbrand --- migration/ram.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/migration/ram.c b/migration/ram.c index 39c7d1c4a6..d5a4d69e1c 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -3714,6 +3714,7 @@ static SaveVMHandlers savevm_ram_handlers =3D { static void ram_mig_ram_block_resized(RAMBlockNotifier *n, void *host, size_t old_size, size_t new_size) { + PostcopyState ps =3D postcopy_state_get(); ram_addr_t offset; Error *err =3D NULL; RAMBlock *rb =3D qemu_ram_block_from_host(host, false, &offset); @@ -3734,6 +3735,35 @@ static void ram_mig_ram_block_resized(RAMBlockNotifi= er *n, void *host, error_free(err); migration_cancel(); } + + switch (ps) { + case POSTCOPY_INCOMING_ADVISE: + /* + * Update what ram_postcopy_incoming_init()->init_range() does at = the + * time postcopy was advised. Syncing RAM blocks with the source w= ill + * result in RAM resizes. + */ + if (old_size < new_size) { + if (ram_discard_range(rb->idstr, old_size, new_size - old_size= )) { + error_report("RAM block '%s' discard of resized RAM failed= ", + rb->idstr); + } + } + break; + case POSTCOPY_INCOMING_NONE: + case POSTCOPY_INCOMING_RUNNING: + case POSTCOPY_INCOMING_END: + /* + * Once our guest is running, postcopy does no longer care about + * resizes. When growing, the new memory was not available on the + * source, no handler needed. + */ + break; + default: + error_report("RAM block '%s' resized during postcopy state: %d", + rb->idstr, ps); + exit(-1); + } } =20 static RAMBlockNotifier ram_mig_ram_notifier =3D { --=20 2.24.1 From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732732; cv=none; d=zohomail.com; s=zohoarc; b=YDE3qglyFG0lm3GfA60rxqTbCP6s/kRJTOIv7GQDbel4HwsLWdZscHd1cepRIo3/QEXNIZsPZNugIURNUFdJNLmPf3o3FlueVl1RO9XHCTOenXOXGTZXnMd+p8YK+iqjgMQ45g5UJBrEsMnMa1T72kNgPrNxs3qne++IFT4yNs8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732732; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4lo7TzPtbse+6KrfKG4j+3yQgCIcw9GUEJgSgD8BmtU=; b=LeLqn3Zii2+9gXZYTjjF+mXlrJDBAU0OvHBMdrVRUAB8lUo7DwVvforvH+Pg50mbTknnEX90wYbRMCYk1n0aXlXuBJOhlD04zzSz+7trSsoEu74D9oypjIOPd0K6wtxRBmZD/TcF8tngiV/1ZShGqhwPxcuGCsG51eDD/64Zj4M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732732188333.6170204740282; Wed, 26 Feb 2020 07:58:52 -0800 (PST) Received: from localhost ([::1]:46272 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z5H-0002hH-0I for importer@patchew.org; Wed, 26 Feb 2020 10:58:51 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34074) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0c-0003CW-6b for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0X-00032o-CD for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:02 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:55378 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 1j6z0X-00032Z-7j for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:53:57 -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-216-jpc_k4mTOaKbu1eb7lozXQ-1; Wed, 26 Feb 2020 10:53:55 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 318AE102C88C; Wed, 26 Feb 2020 15:53:54 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 702C4100E7E3; Wed, 26 Feb 2020 15:53:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732436; 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=4lo7TzPtbse+6KrfKG4j+3yQgCIcw9GUEJgSgD8BmtU=; b=EYvEC+Z88nbbkijQUhEESYWgnsixWPGIcNwoetQLkZA0ivCUhdVgVeq62x5uqzgIzD7LEQ DGDxRDHuon2cSUTbPZNkqPxfy3FJo4JX27AsoQL1fyBKsQPLMNXZKMzt2B6K7i6t6pWAy+ FNlgXBlb2osjlwAApgMKHWuMJ/qHZdw= X-MC-Unique: jpc_k4mTOaKbu1eb7lozXQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 08/13] migration/ram: Simplify host page handling in ram_load_postcopy() Date: Wed, 26 Feb 2020 16:52:59 +0100 Message-Id: <20200226155304.60219-9-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , 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" Add two new helper functions. This will in come handy once we want to handle ram block resizes while postcopy is active. Cc: "Dr. David Alan Gilbert" Cc: Juan Quintela Cc: Peter Xu Signed-off-by: David Hildenbrand Reviewed-by: Dr. David Alan Gilbert --- migration/ram.c | 54 ++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index d5a4d69e1c..f815f4e532 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -2734,6 +2734,20 @@ static inline void *host_from_ram_block_offset(RAMBl= ock *block, return block->host + offset; } =20 +static void *host_page_from_ram_block_offset(RAMBlock *block, + ram_addr_t offset) +{ + /* Note: Explicitly no check against offset_in_ramblock(). */ + return (void *)QEMU_ALIGN_DOWN((uintptr_t)block->host + offset, + block->page_size); +} + +static ram_addr_t host_page_offset_from_ram_block_offset(RAMBlock *block, + ram_addr_t offset) +{ + return ((uintptr_t)block->host + offset) & (block->page_size - 1); +} + static inline void *colo_cache_from_block_offset(RAMBlock *block, ram_addr_t offset) { @@ -3111,13 +3125,12 @@ static int ram_load_postcopy(QEMUFile *f) MigrationIncomingState *mis =3D migration_incoming_get_current(); /* Temporary page that is later 'placed' */ void *postcopy_host_page =3D mis->postcopy_tmp_page; - void *this_host =3D NULL; + void *host_page =3D NULL; bool all_zero =3D false; int target_pages =3D 0; =20 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { ram_addr_t addr; - void *host =3D NULL; void *page_buffer =3D NULL; void *place_source =3D NULL; RAMBlock *block =3D NULL; @@ -3143,9 +3156,12 @@ static int ram_load_postcopy(QEMUFile *f) if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | RAM_SAVE_FLAG_COMPRESS_PAGE)) { block =3D ram_block_from_stream(f, flags); + if (!block) { + ret =3D -EINVAL; + break; + } =20 - host =3D host_from_ram_block_offset(block, addr); - if (!host) { + if (!offset_in_ramblock(block, addr)) { error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); ret =3D -EINVAL; break; @@ -3163,21 +3179,18 @@ static int ram_load_postcopy(QEMUFile *f) * of a host page in one chunk. */ page_buffer =3D postcopy_host_page + - ((uintptr_t)host & (block->page_size - 1)); + host_page_offset_from_ram_block_offset(block, ad= dr); /* If all TP are zero then we can optimise the place */ if (target_pages =3D=3D 1) { all_zero =3D true; - this_host =3D (void *)QEMU_ALIGN_DOWN((uintptr_t)host, - block->page_size); - } else { + host_page =3D host_page_from_ram_block_offset(block, addr); + } else if (host_page !=3D host_page_from_ram_block_offset(bloc= k, + addr))= { /* not the 1st TP within the HP */ - if (QEMU_ALIGN_DOWN((uintptr_t)host, block->page_size) != =3D - (uintptr_t)this_host) { - error_report("Non-same host page %p/%p", - host, this_host); - ret =3D -EINVAL; - break; - } + error_report("Non-same host page %p/%p", host_page, + host_page_from_ram_block_offset(block, addr)); + ret =3D -EINVAL; + break; } =20 /* @@ -3257,16 +3270,11 @@ static int ram_load_postcopy(QEMUFile *f) } =20 if (!ret && place_needed) { - /* This gets called at the last target page in the host page */ - void *place_dest =3D (void *)QEMU_ALIGN_DOWN((uintptr_t)host, - block->page_size); - if (all_zero) { - ret =3D postcopy_place_page_zero(mis, place_dest, - block); + ret =3D postcopy_place_page_zero(mis, host_page, block); } else { - ret =3D postcopy_place_page(mis, place_dest, - place_source, block); + ret =3D postcopy_place_page(mis, host_page, place_source, + block); } } } --=20 2.24.1 From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732726; cv=none; d=zohomail.com; s=zohoarc; b=Pfit2rqW1gKCRsIPkil/8wEEhtCOOY6NecphWDTTUZzGbSpXNwalEnBI+E4Gs+CVq0n56ldBtZmtBtIY/Jr2V2rR3HqsHyyZ28CH5NSEQRTSr3hDfs4jUyliZpnjcjVp7LSpymJHM5pVwZEDP6QBLcX9Ny3uAPqyL3Bmof8THes= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732726; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ud93Vcxnd+Xp4+9u4iMrukyPfYJtI2St2nU+UuD+Msw=; b=jMpg7fa7R42gZItD0oOP4aaF44aIZn/cBPGm9YxySqRdkV9KJpU4YEG+JyClp7V9ipl2Wch56GuiLfdL+A2r/WLvRn+93NASD54Bvo8o7rmCoME0qZMo+iSVsYf7TQM1sq9+lkCodqwkL3VJDz5BiXUi2sSobARaaMXzNuA7kA4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732726880485.67942073479253; Wed, 26 Feb 2020 07:58:46 -0800 (PST) Received: from localhost ([::1]:46270 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z5B-0002U8-Kh for importer@patchew.org; Wed, 26 Feb 2020 10:58:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34107) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0d-0003GM-Ka for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0c-00034k-B2 for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:03 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:36074 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 1j6z0c-00033v-6V for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:02 -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-axvByKmyO7mv4qQ7GsZlcg-1; Wed, 26 Feb 2020 10:53:57 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3CBFB107ACC7; Wed, 26 Feb 2020 15:53:56 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7E52B101D482; Wed, 26 Feb 2020 15:53:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732441; 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=ud93Vcxnd+Xp4+9u4iMrukyPfYJtI2St2nU+UuD+Msw=; b=iTHsClPhDEc8WzdeZhU1rl1/NtOp/HXodufEyXSRoUZ1YoF4jSFxeiNwoToLkWWp764j7t zA8dH1ZZoxjfWVDKERbstOtNLIqLRCqPo3NHX6SrCupYQRADmY9IW1yFjO5b040+TJ3SOP oEmbJstBh9z952c+ONRVwuxDKErYm/k= X-MC-Unique: axvByKmyO7mv4qQ7GsZlcg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 09/13] migration/ram: Consolidate variable reset after placement in ram_load_postcopy() Date: Wed, 26 Feb 2020 16:53:00 +0100 Message-Id: <20200226155304.60219-10-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , 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 consolidate resetting the variables. Cc: "Dr. David Alan Gilbert" Cc: Juan Quintela Cc: Peter Xu Signed-off-by: David Hildenbrand --- migration/ram.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index f815f4e532..1a5ff07997 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -3126,7 +3126,7 @@ static int ram_load_postcopy(QEMUFile *f) /* Temporary page that is later 'placed' */ void *postcopy_host_page =3D mis->postcopy_tmp_page; void *host_page =3D NULL; - bool all_zero =3D false; + bool all_zero =3D true; int target_pages =3D 0; =20 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { @@ -3152,7 +3152,6 @@ static int ram_load_postcopy(QEMUFile *f) addr &=3D TARGET_PAGE_MASK; =20 trace_ram_load_postcopy_loop((uint64_t)addr, flags); - place_needed =3D false; if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | RAM_SAVE_FLAG_COMPRESS_PAGE)) { block =3D ram_block_from_stream(f, flags); @@ -3180,9 +3179,7 @@ static int ram_load_postcopy(QEMUFile *f) */ page_buffer =3D postcopy_host_page + host_page_offset_from_ram_block_offset(block, ad= dr); - /* If all TP are zero then we can optimise the place */ if (target_pages =3D=3D 1) { - all_zero =3D true; host_page =3D host_page_from_ram_block_offset(block, addr); } else if (host_page !=3D host_page_from_ram_block_offset(bloc= k, addr))= { @@ -3199,7 +3196,6 @@ static int ram_load_postcopy(QEMUFile *f) */ if (target_pages =3D=3D (block->page_size / TARGET_PAGE_SIZE))= { place_needed =3D true; - target_pages =3D 0; } place_source =3D postcopy_host_page; } @@ -3276,6 +3272,10 @@ static int ram_load_postcopy(QEMUFile *f) ret =3D postcopy_place_page(mis, host_page, place_source, block); } + place_needed =3D false; + target_pages =3D 0; + /* Assume we have a zero page until we detect something differ= ent */ + all_zero =3D true; } } =20 --=20 2.24.1 From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732724; cv=none; d=zohomail.com; s=zohoarc; b=OZaZ1MtBsHY0DUs/v1C3GWmx49NXS9pH8PPtAt+JKrIMlWdCOcFROO7dXVyfqiMFv20B5UTDnSJxjk0LwwaL7JaDwLyBOcCEaB0Kte6MdPxZaobRMABmz44n6H8thV3PVg/x4IGGR70vNO0Zyn1CFbyBbgeiSmfH9WIHmK9boeE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732724; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=jMU92ageiju/KM9IcMJHHwrw+bCHKXRrCpa6R63pejE=; b=RXnF5LY2JuW6/h4wGKEa7BRwmJPD0iDnBUG2SyFXFCjP1LBBSmLX6QcPi5cqZdwk8AREi1lYfCX+mREZ3FezRD63F9V1FTB+RXvcyEtEF6J33Cn1fBXio7CbSWXnBDTjUCCh1nBsE17v+m75fTBDM70rSvN9OlMhEVuX7cAJFz4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732724447683.4541218444856; Wed, 26 Feb 2020 07:58:44 -0800 (PST) Received: from localhost ([::1]:46268 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z59-0002QF-2R for importer@patchew.org; Wed, 26 Feb 2020 10:58:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34149) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0g-0003OP-Jh for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0f-00036Q-4R for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:06 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:40110 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 1j6z0f-00036E-0H for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:05 -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-N2FJ90KnPH-zi3hoxDSi9g-1; Wed, 26 Feb 2020 10:54:03 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 89B94107ACC4; Wed, 26 Feb 2020 15:54:01 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C4651001B2C; Wed, 26 Feb 2020 15:53:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732444; 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=jMU92ageiju/KM9IcMJHHwrw+bCHKXRrCpa6R63pejE=; b=dmTUIhqjcjUwWuO1hSMsO08zDLH1aU+jw/C2n4eN8AjMfxnu7pmHE412BH8oJxmL21NBZj 7cYonWrtFNMDwtHdnEbkMRRFwboC1Ih0eJPbIN5tlbNc0/qW3o1ic7IXdvlzfZV3cUPJTA j9xrj95w1ofn03tSms0yBahcHVZpLO8= X-MC-Unique: N2FJ90KnPH-zi3hoxDSi9g-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 10/13] migration/ram: Handle RAM block resizes during postcopy Date: Wed, 26 Feb 2020 16:53:01 +0100 Message-Id: <20200226155304.60219-11-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , Richard Henderson , "Dr . David Alan Gilbert" , Peter Xu , "Michael S. Tsirkin" , Shannon Zhao , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , 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" Resizing while migrating is dangerous and does not work as expected. The whole migration code works on the usable_length of ram blocks and does not expect this to change at random points in time. In the case of postcopy, relying on used_length is racy as soon as the guest is running. Also, when used_length changes we might leave the uffd handler registered for some memory regions, reject valid pages when migrating and fail when sending the recv bitmap to the source. Resizing can be trigger *after* (but not during) a reset in ACPI code by the guest - hw/arm/virt-acpi-build.c:acpi_ram_update() - hw/i386/acpi-build.c:acpi_ram_update() Let's remember the original used_length in a separate variable and use it in relevant postcopy code. Make sure to update it when we resize during precopy, when synchronizing the RAM block sizes with the source. Reviewed-by: Peter Xu Cc: "Dr. David Alan Gilbert" Cc: Juan Quintela Cc: Eduardo Habkost Cc: Paolo Bonzini Cc: Igor Mammedov Cc: "Michael S. Tsirkin" Cc: Richard Henderson Cc: Shannon Zhao Cc: Alex Benn=C3=A9e Cc: Peter Xu Signed-off-by: David Hildenbrand Reviewed-by: Dr. David Alan Gilbert --- include/exec/ramblock.h | 10 ++++++++++ migration/postcopy-ram.c | 15 ++++++++++++--- migration/ram.c | 11 +++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/include/exec/ramblock.h b/include/exec/ramblock.h index 07d50864d8..664701b759 100644 --- a/include/exec/ramblock.h +++ b/include/exec/ramblock.h @@ -59,6 +59,16 @@ struct RAMBlock { */ unsigned long *clear_bmap; uint8_t clear_bmap_shift; + + /* + * RAM block length that corresponds to the used_length on the migrati= on + * source (after RAM block sizes were synchronized). Especially, after + * starting to run the guest, used_length and postcopy_length can diff= er. + * Used to register/unregister uffd handlers and as the size of the re= ceived + * bitmap. Receiving any page beyond this length will bail out, as it + * could not have been valid on the source. + */ + ram_addr_t postcopy_length; }; #endif #endif diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index a36402722b..c68caf4e42 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -17,6 +17,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/rcu.h" #include "exec/target_page.h" #include "migration.h" #include "qemu-file.h" @@ -31,6 +32,7 @@ #include "qemu/error-report.h" #include "trace.h" #include "hw/boards.h" +#include "exec/ramblock.h" =20 /* Arbitrary limit on size of each discard command, * keeps them around ~200 bytes @@ -456,6 +458,13 @@ static int init_range(RAMBlock *rb, void *opaque) ram_addr_t length =3D qemu_ram_get_used_length(rb); trace_postcopy_init_range(block_name, host_addr, offset, length); =20 + /* + * Save the used_length before running the guest. In case we have to + * resize RAM blocks when syncing RAM block sizes from the source duri= ng + * precopy, we'll update it manually via the ram block notifier. + */ + rb->postcopy_length =3D length; + /* * We need the whole of RAM to be truly empty for postcopy, so things * like ROMs and any data tables built during init must be zero'd @@ -478,7 +487,7 @@ static int cleanup_range(RAMBlock *rb, void *opaque) const char *block_name =3D qemu_ram_get_idstr(rb); void *host_addr =3D qemu_ram_get_host_addr(rb); ram_addr_t offset =3D qemu_ram_get_offset(rb); - ram_addr_t length =3D qemu_ram_get_used_length(rb); + ram_addr_t length =3D rb->postcopy_length; MigrationIncomingState *mis =3D opaque; struct uffdio_range range_struct; trace_postcopy_cleanup_range(block_name, host_addr, offset, length); @@ -600,7 +609,7 @@ static int nhp_range(RAMBlock *rb, void *opaque) const char *block_name =3D qemu_ram_get_idstr(rb); void *host_addr =3D qemu_ram_get_host_addr(rb); ram_addr_t offset =3D qemu_ram_get_offset(rb); - ram_addr_t length =3D qemu_ram_get_used_length(rb); + ram_addr_t length =3D rb->postcopy_length; trace_postcopy_nhp_range(block_name, host_addr, offset, length); =20 /* @@ -644,7 +653,7 @@ static int ram_block_enable_notify(RAMBlock *rb, void *= opaque) struct uffdio_register reg_struct; =20 reg_struct.range.start =3D (uintptr_t)qemu_ram_get_host_addr(rb); - reg_struct.range.len =3D qemu_ram_get_used_length(rb); + reg_struct.range.len =3D rb->postcopy_length; reg_struct.mode =3D UFFDIO_REGISTER_MODE_MISSING; =20 /* Now tell our userfault_fd that it's responsible for this area */ diff --git a/migration/ram.c b/migration/ram.c index 1a5ff07997..ee5c3d5784 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -244,7 +244,7 @@ int64_t ramblock_recv_bitmap_send(QEMUFile *file, return -1; } =20 - nbits =3D block->used_length >> TARGET_PAGE_BITS; + nbits =3D block->postcopy_length >> TARGET_PAGE_BITS; =20 /* * Make sure the tmp bitmap buffer is big enough, e.g., on 32bit @@ -3160,7 +3160,13 @@ static int ram_load_postcopy(QEMUFile *f) break; } =20 - if (!offset_in_ramblock(block, addr)) { + /* + * Relying on used_length is racy and can result in false posi= tives. + * We might place pages beyond used_length in case RAM was shr= unk + * while in postcopy, which is fine - trying to place via + * UFFDIO_COPY/UFFDIO_ZEROPAGE will never segfault. + */ + if (!block->host || addr >=3D block->postcopy_length) { error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); ret =3D -EINVAL; break; @@ -3757,6 +3763,7 @@ static void ram_mig_ram_block_resized(RAMBlockNotifie= r *n, void *host, rb->idstr); } } + rb->postcopy_length =3D new_size; break; case POSTCOPY_INCOMING_NONE: case POSTCOPY_INCOMING_RUNNING: --=20 2.24.1 From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732716; cv=none; d=zohomail.com; s=zohoarc; b=mPzYBXYm9I0+qqfI5VYgOPc8aWP/ly78IHOGaYDJIKghOequXTHUYXunBVdt23X0er9svyeH/k7IKTDb/SYDiKrA5JCxt4/BpXH4MnoUDw75d0J0xy7dVu2K+QWqYp3dYyQDWPIRJ7IpmZSVw9quksfxMkv69cNr5KK3qlsZ7Us= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732716; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=w/z7m5Qm4HaPOtKr9rMP0YxTInrel2WDYqHS0dY6eVE=; b=nOLNUwCktX261OckmIVRYnB3omM6SfrH2Lxbbd9+Xt5M5fs9eqo5P5YwMDSaNERG/s0x8YsHz3H0G8KrfV2862YK8HG2ZtmZabRdrWs2cBo0EO1ra5COj6Kr+bE8mRCw6vp7sGepvbQ9HEiI2rWy7NHT64Xb66fzmpRnGCKP37I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732716398649.0463315244278; Wed, 26 Feb 2020 07:58:36 -0800 (PST) Received: from localhost ([::1]:46266 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z51-0002NX-9K for importer@patchew.org; Wed, 26 Feb 2020 10:58:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34183) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0m-0003WZ-Mn for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0l-0003Hd-O0 for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:12 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:20593 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 1j6z0l-0003Fw-JE for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:11 -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-350-bQ-L45BdPZSsIVkG5-HeJA-1; Wed, 26 Feb 2020 10:54:09 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7AFD71084437; Wed, 26 Feb 2020 15:54:08 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id D7D431001B2C; Wed, 26 Feb 2020 15:54:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732451; 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=w/z7m5Qm4HaPOtKr9rMP0YxTInrel2WDYqHS0dY6eVE=; b=hU6e8sETdH/5YKJEHRgJplF4+YuNCl9CYpWhfTKs+Wcp8d5UvbFliDDrm5fsV/izoSttAS LWSnCVA2wsXFvfs3j3cX1GgyTtBHOhjgImqsjLQwiZ3vhCs7ofh10VAh7AKAP21g947k5m OWBJbJmmWsFC9ov4ZutzK4AIm3yhGqo= X-MC-Unique: bQ-L45BdPZSsIVkG5-HeJA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 11/13] migration/multifd: Print used_length of memory block Date: Wed, 26 Feb 2020 16:53:02 +0100 Message-Id: <20200226155304.60219-12-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , 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 actually want to print the used_length, against which we check. Cc: "Dr. David Alan Gilbert" Cc: Juan Quintela Cc: Peter Xu Signed-off-by: David Hildenbrand Reviewed-by: Dr. David Alan Gilbert --- migration/multifd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migration/multifd.c b/migration/multifd.c index b3e8ae9bcc..dd9e88c5f1 100644 --- a/migration/multifd.c +++ b/migration/multifd.c @@ -222,7 +222,7 @@ static int multifd_recv_unfill_packet(MultiFDRecvParams= *p, Error **errp) if (offset > (block->used_length - qemu_target_page_size())) { error_setg(errp, "multifd: offset too long %" PRIu64 " (max " RAM_ADDR_FMT ")", - offset, block->max_length); + offset, block->used_length); return -1; } p->pages->iov[i].iov_base =3D block->host + offset; --=20 2.24.1 From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732868; cv=none; d=zohomail.com; s=zohoarc; b=JUDpyYQUjpXTkgnBpc9NSWLp4jW87XzLNZ6G8HdeBB84gzXsWfgVMBPCWd9w9dR9edUJXRM+qhK5RjR5k4DFq1wMf2CqdnECwI72hBKMj8eksuPgpb/dGZt8V2oB5vpU+1Y0oHtHvZaLOIPRfQXR7CIS4c1/a3rdAQIkucCy3TY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732868; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=AbD2MWw/nLDVkxUemdyaLogLxwGW4nzWXsGitr5n5fs=; b=RpyMdE0ov+cz55VgmthPPEV6k2DANETQLtRM9kbr6GTZDuQkMCMWdOrXNPf/tWAWhxf0QoiPibyRjMiRU5w1AXKz0Rc92yYwffDmMgeyEBwWbePbFnsG7DlPVgdbCbFWTFPTe2J8l+S2p+9u2S6LmpMaOo5PbEOaDrY1GQMP6XI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732868995618.8897543442122; Wed, 26 Feb 2020 08:01:08 -0800 (PST) Received: from localhost ([::1]:46334 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z7T-0005uJ-Mc for importer@patchew.org; Wed, 26 Feb 2020 11:01:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34211) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0s-0003c4-NS for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0r-0003PZ-MB for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:18 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:22159 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 1j6z0r-0003Nz-GT for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:17 -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-135-Jeba8bDEMN-nr7k7eH3IJQ-1; Wed, 26 Feb 2020 10:54:11 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8BA5A800D48; Wed, 26 Feb 2020 15:54:10 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id CA2F11001B2C; Wed, 26 Feb 2020 15:54:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732455; 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=AbD2MWw/nLDVkxUemdyaLogLxwGW4nzWXsGitr5n5fs=; b=UxFY7OjHWOeTU8hS0pX8A1tlrttOpQEqX99C+yAqBuei4yYbG0x0/Ke6G5XA1wpRhyNXO3 jZIOhn/Yh/zYGgcNrEIGunS5C76dZGhoz/BvX11JCILOmL7vOxHDXGJ5mrBd7z74UvJomN d6P16iyGenNYV2wkFCPQ6ESAMX2mV+Y= X-MC-Unique: Jeba8bDEMN-nr7k7eH3IJQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 12/13] migration/ram: Use offset_in_ramblock() in range checks Date: Wed, 26 Feb 2020 16:53:03 +0100 Message-Id: <20200226155304.60219-13-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , 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 never read or write beyond the used_length of memory blocks when migrating. Make this clearer by using offset_in_ramblock() consistently. Cc: "Dr. David Alan Gilbert" Cc: Juan Quintela Cc: Peter Xu Signed-off-by: David Hildenbrand Reviewed-by: Dr. David Alan Gilbert --- migration/ram.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index ee5c3d5784..5cc9993899 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -1309,8 +1309,8 @@ static bool find_dirty_block(RAMState *rs, PageSearch= Status *pss, bool *again) *again =3D false; return false; } - if ((((ram_addr_t)pss->page) << TARGET_PAGE_BITS) - >=3D pss->block->used_length) { + if (!offset_in_ramblock(pss->block, + ((ram_addr_t)pss->page) << TARGET_PAGE_BITS)) { /* Didn't find anything in this RAM Block */ pss->page =3D 0; pss->block =3D QLIST_NEXT_RCU(pss->block, next); @@ -1514,7 +1514,7 @@ int ram_save_queue_pages(const char *rbname, ram_addr= _t start, ram_addr_t len) rs->last_req_rb =3D ramblock; } trace_ram_save_queue_pages(ramblock->idstr, start, len); - if (start+len > ramblock->used_length) { + if (!offset_in_ramblock(ramblock, start + len - 1)) { error_report("%s request overrun start=3D" RAM_ADDR_FMT " len=3D" RAM_ADDR_FMT " blocklen=3D" RAM_ADDR_FMT, __func__, start, len, ramblock->used_length); @@ -3325,8 +3325,8 @@ static void colo_flush_ram_cache(void) while (block) { offset =3D migration_bitmap_find_dirty(ram_state, block, offse= t); =20 - if (((ram_addr_t)offset) << TARGET_PAGE_BITS - >=3D block->used_length) { + if (!offset_in_ramblock(block, + ((ram_addr_t)offset) << TARGET_PAGE_BI= TS)) { offset =3D 0; block =3D QLIST_NEXT_RCU(block, next); } else { --=20 2.24.1 From nobody Thu Nov 13 20:38:15 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 ARC-Seal: i=1; a=rsa-sha256; t=1582732868; cv=none; d=zohomail.com; s=zohoarc; b=FkMFOfo2tmBm9sN5GTN54Gr/dhVL9LrLuLEjFpwkTQ5+4HnO3HdqpaN6l7evBLaJH3OAv4fwGZ6YOWqeDXu/OLMSfDHsTjs9/TqGSrzp+QsErciG0SMQPjax1m6PlHOfR6zxQ9HdXRfudFTqJNIpYomRZxX/F+68NnbTXsKpgGU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582732868; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=BHCVavcwdU9hOi5U0JBlZicFww6IvMYsjJ29cufnJwY=; b=nZ/XsAsCs/EwUtxTYU4zNbZRWFj9Pu8kYYkFagWfNqA0Vt6NKibq4LZCP8TCQ1nNwbpWizkLM0ClTTWZ/H7OsI7S/cYUXSmRHFDdrojcX+mTLKCIJqYnQWZRwU6KCzx0lIitzkywJzxXsXz+Z+K+msodzUzlZACyL2EsVEv8IPA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582732868358712.2959600868771; Wed, 26 Feb 2020 08:01:08 -0800 (PST) Received: from localhost ([::1]:46326 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z7S-0005q4-Di for importer@patchew.org; Wed, 26 Feb 2020 11:01:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34214) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6z0t-0003d5-0O for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6z0r-0003QD-Pl for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:18 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:39930 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 1j6z0r-0003Oa-Lf for qemu-devel@nongnu.org; Wed, 26 Feb 2020 10:54:17 -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-452-IiFkBAvAMrysDeSHjumZrw-1; Wed, 26 Feb 2020 10:54:13 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C6248DB65; Wed, 26 Feb 2020 15:54:12 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-196.ams2.redhat.com [10.36.117.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id D735B101D482; Wed, 26 Feb 2020 15:54:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582732457; 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=BHCVavcwdU9hOi5U0JBlZicFww6IvMYsjJ29cufnJwY=; b=G89klb/py8AYTuY2PaSeJslH3n2F4A0gzMa0tuqdBiVRJcV2QJa33Ws0cZYZOKjMswzgmO iqw3AJVrBswmz+fujO0J/WPjBPnsxqKXBEh8o7BcK2yTDF7HLKy/EMQX4zq7I282uLR9H7 mGSZeDNe+rxbxuRBZw0YbPIv/+rRHbc= X-MC-Unique: IiFkBAvAMrysDeSHjumZrw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v3 13/13] migration/ram: Tolerate partially changed mappings in postcopy code Date: Wed, 26 Feb 2020 16:53:04 +0100 Message-Id: <20200226155304.60219-14-david@redhat.com> In-Reply-To: <20200226155304.60219-1-david@redhat.com> References: <20200226155304.60219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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: Andrea Arcangeli , Eduardo Habkost , Juan Quintela , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , 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 we partially change mappings (esp., mmap over parts of an existing mmap like qemu_ram_remap() does) where we have a userfaultfd handler registered, the handler will implicitly be unregistered from the parts that changed. Trying to place pages onto mappings where there is no longer a handler registered will fail. Let's make sure that any waiter is woken up - we have to do that manually. Let's also document how UFFDIO_UNREGISTER will handle this scenario. This is mainly a preparation for RAM blocks with resizable allcoations, where the mapping of the invalid RAM range will change. The source will keep sending pages that are outside of the new (shrunk) RAM size. We have to treat these pages like they would have been migrated, but can essentially simply drop the content (ignore the placement error). Keep printing a warning when we hit EINVAL, to avoid hiding other (programming) issues. ENOENT is unique. Cc: "Dr. David Alan Gilbert" Cc: Juan Quintela Cc: Peter Xu Cc: Andrea Arcangeli Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu --- migration/postcopy-ram.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index c68caf4e42..f39c6304de 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -506,6 +506,14 @@ static int cleanup_range(RAMBlock *rb, void *opaque) range_struct.start =3D (uintptr_t)host_addr; range_struct.len =3D length; =20 + /* + * In case the mapping was partially changed since we enabled userfault + * (e.g., via qemu_ram_remap()), the userfaultfd handler was already r= emoved + * for the mappings that changed. Unregistering will, however, still w= ork + * and ignore mappings without a registered handler. There could only = be + * an issue if we would suddenly encounter a mapping that's incompatib= le + * with UFFD - which cannot happen within a single RAM block. + */ if (ioctl(mis->userfault_fd, UFFDIO_UNREGISTER, &range_struct)) { error_report("%s: userfault unregister %s", __func__, strerror(err= no)); =20 @@ -1180,6 +1188,17 @@ int postcopy_ram_incoming_setup(MigrationIncomingSta= te *mis) return 0; } =20 +static int qemu_ufd_wake_ioctl(int userfault_fd, void *host_addr, + uint64_t pagesize) +{ + struct uffdio_range range =3D { + .start =3D (uint64_t)(uintptr_t)host_addr, + .len =3D pagesize, + }; + + return ioctl(userfault_fd, UFFDIO_WAKE, &range); +} + static int qemu_ufd_copy_ioctl(int userfault_fd, void *host_addr, void *from_addr, uint64_t pagesize, RAMBloc= k *rb) { @@ -1198,6 +1217,26 @@ static int qemu_ufd_copy_ioctl(int userfault_fd, voi= d *host_addr, zero_struct.mode =3D 0; ret =3D ioctl(userfault_fd, UFFDIO_ZEROPAGE, &zero_struct); } + + /* + * When the mapping gets partially changed (e.g., qemu_ram_remap()) be= fore + * we try to place a page, the userfaultfd handler will be removed for= the + * changed mappings and placing pages will fail. We can safely ignore = this, + * because mappings that changed on the destination don't need data fr= om the + * source (e.g., qemu_ram_remap()). Wake up any waiter waiting for tha= t page + * (unlikely but possible). Waking up waiters is always possible, even + * without a registered userfaultfd handler. + * + * Old kernels report EINVAL, new kernels report ENOENT in case there = is + * no longer a userfaultfd handler for a mapping. + */ + if (ret && (errno =3D=3D ENOENT || errno =3D=3D EINVAL)) { + if (errno =3D=3D EINVAL) { + warn_report("%s: Failed to place page %p. Waking up any waiter= s.", + __func__, host_addr); + } + ret =3D qemu_ufd_wake_ioctl(userfault_fd, host_addr, pagesize); + } if (!ret) { ramblock_recv_bitmap_set_range(rb, host_addr, pagesize / qemu_target_page_size()); --=20 2.24.1