From nobody Tue Feb 10 03:45:33 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1687266337; cv=none; d=zohomail.com; s=zohoarc; b=h2kO9AFDECCyKj9N2UADIgBJczW4mahwKwpFXhKfY69UhlnLog72ykji8vaqh1N6cYIeNKw/DejdMcG3uTE7C6Wm8zobJhc3bRBzC7hKnSk23IGs6SuP6UxkYKK1Z39svGHuTHaHZSATAA5cvfxn+xi8IVB0p9ss2taeH3LFy6s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1687266337; h=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=lq7Ws+6N6X35uoKMgSVWmcImJ1QheW9aZBUfubNT+gU=; b=G8T56Z6C9wyRfGuZjzTX2McVllKtNctoZ7DMwSMe3pmWtrhxoj4w/MujbkQPoitpVlRdTlZpRDduCGthIrM9k5/UxAcBdjFS++F+JQ+pK6B9oGvsf1oTeHz9enuzE+1JodrI4sbZanVWiI9H/HNTO3hLQBPXz/00LWJl4dA+jto= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687266337023419.70175453574734; Tue, 20 Jun 2023 06:05:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qBb1y-0002he-BM; Tue, 20 Jun 2023 09:04:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qBb1u-0002fr-Ag for qemu-devel@nongnu.org; Tue, 20 Jun 2023 09:04:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qBb1s-0006rk-Ry for qemu-devel@nongnu.org; Tue, 20 Jun 2023 09:04:18 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-424-qS-6i6dENg2DQfwh01eyhA-1; Tue, 20 Jun 2023 09:04:12 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F00B6805BFB; Tue, 20 Jun 2023 13:03:59 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.193.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED2C72166B26; Tue, 20 Jun 2023 13:03:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687266256; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lq7Ws+6N6X35uoKMgSVWmcImJ1QheW9aZBUfubNT+gU=; b=GBo66HnEXaRLs4Tffdx3TX/rHi6P3f/6yG5l7TQXSTraOgS/UJtym3YmMNgXaf75hJrVOy dxuQ5nVQXRUNa2COXSxH9kmtWpBtaNTGTcOYxrw/MKl/AUqAqb0XjHp9hInYMSM2CNYX0m 1mqSPnxyjOUYszzFWTAc04/XgB7zeQc= X-MC-Unique: qS-6i6dENg2DQfwh01eyhA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S. Tsirkin" , Juan Quintela , Peter Xu , Leonardo Bras , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Peng Tao Subject: [PATCH v1 1/4] softmmu/physmem: Warn with ram_block_discard_range() on MAP_PRIVATE file mapping Date: Tue, 20 Jun 2023 15:03:51 +0200 Message-Id: <20230620130354.322180-2-david@redhat.com> In-Reply-To: <20230620130354.322180-1-david@redhat.com> References: <20230620130354.322180-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1687266376386100001 Content-Type: text/plain; charset="utf-8" ram_block_discard_range() cannot possibly do the right thing in MAP_PRIVATE file mappings in the general case. To achieve the documented semantics, we also have to punch a hole into the file, possibly messing with other MAP_PRIVATE/MAP_SHARED mappings of such a file. For example, using VM templating -- see commit b17fbbe55cba ("migration: allow private destination ram with x-ignore-shared") -- in combination with any mechanism that relies on discarding of RAM is problematic. This includes: * Postcopy live migration * virtio-balloon inflation/deflation or free-page-reporting * virtio-mem So at least warn that there is something possibly dangerous is going on when using ram_block_discard_range() in these cases. Signed-off-by: David Hildenbrand Acked-by: Peter Xu Tested-by: Mario Casquero --- softmmu/physmem.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 6bdd944fe8..27c7219c82 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -3451,6 +3451,24 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t s= tart, size_t length) * so a userfault will trigger. */ #ifdef CONFIG_FALLOCATE_PUNCH_HOLE + /* + * We'll discard data from the actual file, even though we only + * have a MAP_PRIVATE mapping, possibly messing with other + * MAP_PRIVATE/MAP_SHARED mappings. There is no easy way to + * change that behavior whithout violating the promised + * semantics of ram_block_discard_range(). + * + * Only warn, because it work as long as nobody else uses that + * file. + */ + if (!qemu_ram_is_shared(rb)) { + warn_report_once("ram_block_discard_range: Discarding RAM" + " in private file mappings is possibly" + " dangerous, because it will modify the" + " underlying file and will affect other" + " users of the file"); + } + ret =3D fallocate(rb->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEE= P_SIZE, start, length); if (ret) { --=20 2.40.1 From nobody Tue Feb 10 03:45:33 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1687266337; cv=none; d=zohomail.com; s=zohoarc; b=naZM1Ay68ohd32BW5c3dT5N3t64pD2x3JTioo48RVhKpSaWaYtri9QJOn7rXHWzVFwPvlvfj7CVDcQHILhVoCf6kgFWWMfXBZnXE6PHfbPIbHoYZFfQDzOH5/a4HfM8oVHRkYhKxjtg4UViol7eNC1NJI08/bFdBfj8j0GvvReA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1687266337; h=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=9gikf4BE2vltak5Ude6T1zo0pu9yDG/S+fZmZfG7HRA=; b=ausDOjbt8if5at8HQNnyMhqaz4TrsM8xcXC8ZBKixMiUhTzgYpStXUx0WYeh2uk9UpPFS64v7Hcx36dFGxwd/2jPbO5uDrsc0FnZdrCxV8FiR4N5zbKzWDe57bt+fFp44jMgMpkL7+cWU+PLaJlOlOwdDmLBezC0MZlYhsiqerw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687266337186792.4102782935418; Tue, 20 Jun 2023 06:05:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qBb24-0002jF-B9; Tue, 20 Jun 2023 09:04:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qBb21-0002iW-Nn for qemu-devel@nongnu.org; Tue, 20 Jun 2023 09:04:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qBb20-0006tl-9R for qemu-devel@nongnu.org; Tue, 20 Jun 2023 09:04:25 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-278-z3Djug2QPWW3k3QjP9XUpA-1; Tue, 20 Jun 2023 09:04:21 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2C9B23C0F67B; Tue, 20 Jun 2023 13:04:02 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.193.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id 506622166B26; Tue, 20 Jun 2023 13:04:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687266263; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9gikf4BE2vltak5Ude6T1zo0pu9yDG/S+fZmZfG7HRA=; b=ZCp0BLqKr78Ua7//vIHIIO1HPAttlpmbtTXzWHMoE8dwEcYVxdv64Z9O90YfE3vhFMjb5z ZOMkqEdApk0/gwZNecMHud8MgASxJTrvJFHVG0Ol76EgftTpxkoKBsv8qiKX0dmURAF2I+ jFJhI3e7reoRZfhp5UDwFJUkBzZMtNo= X-MC-Unique: z3Djug2QPWW3k3QjP9XUpA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S. Tsirkin" , Juan Quintela , Peter Xu , Leonardo Bras , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Peng Tao Subject: [PATCH v1 2/4] virtio-mem: Skip most of virtio_mem_unplug_all() without plugged memory Date: Tue, 20 Jun 2023 15:03:52 +0200 Message-Id: <20230620130354.322180-3-david@redhat.com> In-Reply-To: <20230620130354.322180-1-david@redhat.com> References: <20230620130354.322180-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1687266341575100003 Content-Type: text/plain; charset="utf-8" Already when starting QEMU we perform one system reset that ends up triggering virtio_mem_unplug_all() with no actual memory plugged yet. That, in turn will trigger ram_block_discard_range() and perform some other actions that are not required in that case. Let's optimize virtio_mem_unplug_all() for the case that no memory is plugged. This will be beneficial for x-ignore-shared support as well. Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- hw/virtio/virtio-mem.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 538b695c29..9f6169af32 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -606,20 +606,20 @@ static int virtio_mem_unplug_all(VirtIOMEM *vmem) { RAMBlock *rb =3D vmem->memdev->mr.ram_block; =20 - if (virtio_mem_is_busy()) { - return -EBUSY; - } - - if (ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb))) { - return -EBUSY; - } - virtio_mem_notify_unplug_all(vmem); - - bitmap_clear(vmem->bitmap, 0, vmem->bitmap_size); if (vmem->size) { + if (virtio_mem_is_busy()) { + return -EBUSY; + } + if (ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb))) { + return -EBUSY; + } + virtio_mem_notify_unplug_all(vmem); + + bitmap_clear(vmem->bitmap, 0, vmem->bitmap_size); vmem->size =3D 0; notifier_list_notify(&vmem->size_change_notifiers, &vmem->size); } + trace_virtio_mem_unplugged_all(); virtio_mem_resize_usable_region(vmem, vmem->requested_size, true); return 0; --=20 2.40.1 From nobody Tue Feb 10 03:45:33 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1687266307; cv=none; d=zohomail.com; s=zohoarc; b=P91sm/8C2LuhanHgp0sxU1gc9fCnV5IWzYaY2w9aC6hpaF3tcBjHR9CDp+W2SzPr/tqeGfZJU2nqvPSqKTYHjn0bCL9Q8h2EEFdQwp0awXDmIss+bQZ1/g3dxIrlfDOuW5/mxy1O7kZSUhxBCSs/QqX54e8x5B/GDyo8OQCmXgA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1687266307; h=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=+uVChHehTSj/KFWUtBjAS9n/YLbw7wl8kGYZAi8Fb9U=; b=M6vD1i8e+oZekhmU31HsZTUWDxNgnNTIrzVXOppfB0cKhNMT8Ic/dr2r7i98HHB5mmTnrX9HCXDPqO59rAk64d7wxb51hJDmDkkmJTltRh1LlpyqiHMN+dBg/r1RUTCakQNHDY8uJh543e7aLq1rGIwFu8K/hsawVPlfIi4q3Qk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687266307294703.5383739464261; Tue, 20 Jun 2023 06:05:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qBb1q-0002f9-OG; Tue, 20 Jun 2023 09:04:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qBb1o-0002eZ-Us for qemu-devel@nongnu.org; Tue, 20 Jun 2023 09:04:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qBb1m-0006qO-V1 for qemu-devel@nongnu.org; Tue, 20 Jun 2023 09:04:12 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-62-PyYTVGgxMkuFEaIlnf-Jsg-1; Tue, 20 Jun 2023 09:04:06 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6C01489C7FD; Tue, 20 Jun 2023 13:04:04 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.193.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8DB022166B26; Tue, 20 Jun 2023 13:04:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687266249; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+uVChHehTSj/KFWUtBjAS9n/YLbw7wl8kGYZAi8Fb9U=; b=CBH0/TB2xUJ2Es7KzLgSEaWGykvj44no1Hy06hGe+on5aK/YqSpb4+ayeO8mXvQOIcj3ul 21VUivHME0wy8seuPyz2NVNWI42BE0Cy8qJCDIRJePPpt5DFa64sfTuzSgUKLE4mTooQSV QkS8IvPj5vItIGywx8fthIz400kkuTo= X-MC-Unique: PyYTVGgxMkuFEaIlnf-Jsg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S. Tsirkin" , Juan Quintela , Peter Xu , Leonardo Bras , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Peng Tao Subject: [PATCH v1 3/4] migration/ram: Expose ramblock_is_ignored() as migrate_ram_is_ignored() Date: Tue, 20 Jun 2023 15:03:53 +0200 Message-Id: <20230620130354.322180-4-david@redhat.com> In-Reply-To: <20230620130354.322180-1-david@redhat.com> References: <20230620130354.322180-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1687266321936100001 Content-Type: text/plain; charset="utf-8" virtio-mem wants to know whether it should not mess with the RAMBlock content (e.g., discard RAM, preallocate memory) on incoming migration. So let's expose that function as migrate_ram_is_ignored() in migration/misc.h Signed-off-by: David Hildenbrand Acked-by: Peter Xu Tested-by: Mario Casquero --- include/migration/misc.h | 1 + migration/postcopy-ram.c | 2 +- migration/ram.c | 14 +++++++------- migration/ram.h | 3 +-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/migration/misc.h b/include/migration/misc.h index 5ebe13b4b9..7dcc0b5c2c 100644 --- a/include/migration/misc.h +++ b/include/migration/misc.h @@ -40,6 +40,7 @@ int precopy_notify(PrecopyNotifyReason reason, Error **er= rp); =20 void ram_mig_init(void); void qemu_guest_free_page_hint(void *addr, size_t len); +bool migrate_ram_is_ignored(RAMBlock *block); =20 /* migration/block.c */ =20 diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index 5615ec29eb..29aea9456d 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -408,7 +408,7 @@ bool postcopy_ram_supported_by_host(MigrationIncomingSt= ate *mis, Error **errp) /* * We don't support postcopy with some type of ramblocks. * - * NOTE: we explicitly ignored ramblock_is_ignored() instead we checked + * NOTE: we explicitly ignored migrate_ram_is_ignored() instead we che= cked * all possible ramblocks. This is because this function can be called * when creating the migration object, during the phase RAM_MIGRATABLE * is not even properly set for all the ramblocks. diff --git a/migration/ram.c b/migration/ram.c index 5283a75f02..0ada6477e8 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -194,7 +194,7 @@ static bool postcopy_preempt_active(void) return migrate_postcopy_preempt() && migration_in_postcopy(); } =20 -bool ramblock_is_ignored(RAMBlock *block) +bool migrate_ram_is_ignored(RAMBlock *block) { return !qemu_ram_is_migratable(block) || (migrate_ignore_shared() && qemu_ram_is_shared(block) @@ -696,7 +696,7 @@ static void pss_find_next_dirty(PageSearchStatus *pss) unsigned long size =3D rb->used_length >> TARGET_PAGE_BITS; unsigned long *bitmap =3D rb->bmap; =20 - if (ramblock_is_ignored(rb)) { + if (migrate_ram_is_ignored(rb)) { /* Points directly to the end, so we know no dirty page */ pss->page =3D size; return; @@ -780,7 +780,7 @@ unsigned long colo_bitmap_find_dirty(RAMState *rs, RAMB= lock *rb, =20 *num =3D 0; =20 - if (ramblock_is_ignored(rb)) { + if (migrate_ram_is_ignored(rb)) { return size; } =20 @@ -2260,7 +2260,7 @@ static int ram_save_host_page(RAMState *rs, PageSearc= hStatus *pss) unsigned long start_page =3D pss->page; int res; =20 - if (ramblock_is_ignored(pss->block)) { + if (migrate_ram_is_ignored(pss->block)) { error_report("block %s should not be migrated !", pss->block->idst= r); return 0; } @@ -3347,7 +3347,7 @@ static inline RAMBlock *ram_block_from_stream(Migrati= onIncomingState *mis, return NULL; } =20 - if (ramblock_is_ignored(block)) { + if (migrate_ram_is_ignored(block)) { error_report("block %s should not be migrated !", id); return NULL; } @@ -3958,7 +3958,7 @@ static int ram_load_precopy(QEMUFile *f) } if (migrate_ignore_shared()) { hwaddr addr =3D qemu_get_be64(f); - if (ramblock_is_ignored(block) && + if (migrate_ram_is_ignored(block) && block->mr->addr !=3D addr) { error_report("Mismatched GPAs for block %s " "%" PRId64 "!=3D %" PRId64, @@ -4254,7 +4254,7 @@ static void ram_mig_ram_block_resized(RAMBlockNotifie= r *n, void *host, RAMBlock *rb =3D qemu_ram_block_from_host(host, false, &offset); Error *err =3D NULL; =20 - if (ramblock_is_ignored(rb)) { + if (migrate_ram_is_ignored(rb)) { return; } =20 diff --git a/migration/ram.h b/migration/ram.h index ea1f3c25b5..145c915ca7 100644 --- a/migration/ram.h +++ b/migration/ram.h @@ -36,11 +36,10 @@ extern XBZRLECacheStats xbzrle_counters; extern CompressionStats compression_counters; =20 -bool ramblock_is_ignored(RAMBlock *block); /* Should be holding either ram_list.mutex, or the RCU lock. */ #define RAMBLOCK_FOREACH_NOT_IGNORED(block) \ INTERNAL_RAMBLOCK_FOREACH(block) \ - if (ramblock_is_ignored(block)) {} else + if (migrate_ram_is_ignored(block)) {} else =20 #define RAMBLOCK_FOREACH_MIGRATABLE(block) \ INTERNAL_RAMBLOCK_FOREACH(block) \ --=20 2.40.1 From nobody Tue Feb 10 03:45:33 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1687266327; cv=none; d=zohomail.com; s=zohoarc; b=YORdsAhsB6/Zk/Q8mEFoRnn1Ro2JY+np0ioqPIZVdCdFSN0KiUnfXrqAfb9KvCij54TUcA7PRsQ86mCJhYxCvKMaHqD2bNOes9FPodZiM+XzS6OZzU2TZyKwkRW1ykWz5Amqmx5l8G0h+mgdJ/Z9JxdikA+Fivxm1QelnONz8TY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1687266327; h=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=rWUIrI8C9YsuJ+7+y+l3JJ4pfQWH+opp1AGMxC8VxUo=; b=f5AtVUYCa6HOCz0N+mBSjZgX2UyQjhXIEbe7AxYNj5nJibjVNXMYx6gt7flD59jIMYIF0zXgKbzOtV5v9VcYRTxmeh347fvjBn6XsjRrGIHINjyxl6o6f6GLrGT3yzgm84IZsZ3JDECah0KXM9hu0DgbaHPUMNED0kfk5o5WDtg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1687266327383402.99857913089454; Tue, 20 Jun 2023 06:05:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qBb20-0002i9-8w; Tue, 20 Jun 2023 09:04:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qBb1w-0002hc-Hb for qemu-devel@nongnu.org; Tue, 20 Jun 2023 09:04:20 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qBb1u-0006rw-Ry for qemu-devel@nongnu.org; Tue, 20 Jun 2023 09:04:20 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-460-I5uONs_aNM2hcBFhnlWvag-1; Tue, 20 Jun 2023 09:04:16 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DC8C53810D3B; Tue, 20 Jun 2023 13:04:06 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.193.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id CB4CD2166B26; Tue, 20 Jun 2023 13:04:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687266258; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rWUIrI8C9YsuJ+7+y+l3JJ4pfQWH+opp1AGMxC8VxUo=; b=UNcVS+RBv0FYDFuxhiZLlEFXsq46Ap9GkgAzicCCeQdzu+aTsqEks3m/63gZ/hEqK8ww6K ZBz2yN93rydDxvOyECO/smZBxu2LSzAaVHimwM+9FwQX4ETK5GS3lOgux8I5rDJwJl7iyN gxosyIs5lVPlmrP/7+Xnsft8y7rozLc= X-MC-Unique: I5uONs_aNM2hcBFhnlWvag-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S. Tsirkin" , Juan Quintela , Peter Xu , Leonardo Bras , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Peng Tao Subject: [PATCH v1 4/4] virtio-mem: Support "x-ignore-shared" migration Date: Tue, 20 Jun 2023 15:03:54 +0200 Message-Id: <20230620130354.322180-5-david@redhat.com> In-Reply-To: <20230620130354.322180-1-david@redhat.com> References: <20230620130354.322180-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1687266363253100001 Content-Type: text/plain; charset="utf-8" To achieve desired "x-ignore-shared" functionality, we should not discard all RAM when realizing the device and not mess with preallocation/postcopy when loading device state. In essence, we should not touch RAM content. As "x-ignore-shared" gets set after realizing the device, we cannot rely on that. Let's simply skip discarding of RAM on incoming migration. Note that virtio_mem_post_load() will call virtio_mem_restore_unplugged() -- unless "x-ignore-shared" is set. So once migration finished we'll have a consistent state. The initial system reset will also not discard any RAM, because virtio_mem_unplug_all() will not call virtio_mem_unplug_all() when no memory is plugged (which is the case before loading the device state). Note that something like VM templating -- see commit b17fbbe55cba ("migration: allow private destination ram with x-ignore-shared") -- is currently incompatible with virtio-mem and ram_block_discard_range() will warn in case a private file mapping is supplied by virtio-mem. For VM templating with virtio-mem, it makes more sense to either (a) Create the template without the virtio-mem device and hotplug a virtio-mem device to the new VM instances using proper own memory backend. (b) Use a virtio-mem device that doesn't provide any memory in the template (requested-size=3D0) and use private anonymous memory. Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- hw/virtio/virtio-mem.c | 47 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 9f6169af32..b013dfbaf0 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -18,6 +18,7 @@ #include "sysemu/numa.h" #include "sysemu/sysemu.h" #include "sysemu/reset.h" +#include "sysemu/runstate.h" #include "hw/virtio/virtio.h" #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-access.h" @@ -886,11 +887,23 @@ static void virtio_mem_device_realize(DeviceState *de= v, Error **errp) return; } =20 - ret =3D ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb)); - if (ret) { - error_setg_errno(errp, -ret, "Unexpected error discarding RAM"); - ram_block_coordinated_discard_require(false); - return; + /* + * We don't know at this point whether shared RAM is migrated using + * QEMU or migrated using the file content. "x-ignore-shared" will be + * configurated after realizing the device. So in case we have an + * incoming migration, simply always skip the discard step. + * + * Otherwise, make sure that we start with a clean slate: either the + * memory backend might get reused or the shared file might still have + * memory allocated. + */ + if (!runstate_check(RUN_STATE_INMIGRATE)) { + ret =3D ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb= )); + if (ret) { + error_setg_errno(errp, -ret, "Unexpected error discarding RAM"= ); + ram_block_coordinated_discard_require(false); + return; + } } =20 virtio_mem_resize_usable_region(vmem, vmem->requested_size, true); @@ -962,10 +975,6 @@ static int virtio_mem_post_load(void *opaque, int vers= ion_id) RamDiscardListener *rdl; int ret; =20 - if (vmem->prealloc && !vmem->early_migration) { - warn_report("Proper preallocation with migration requires a newer = QEMU machine"); - } - /* * We started out with all memory discarded and our memory region is m= apped * into an address space. Replay, now that we updated the bitmap. @@ -978,6 +987,18 @@ static int virtio_mem_post_load(void *opaque, int vers= ion_id) } } =20 + /* + * If shared RAM is migrated using the file content and not using QEMU, + * don't mess with preallocation and postcopy. + */ + if (migrate_ram_is_ignored(vmem->memdev->mr.ram_block)) { + return 0; + } + + if (vmem->prealloc && !vmem->early_migration) { + warn_report("Proper preallocation with migration requires a newer = QEMU machine"); + } + if (migration_in_incoming_postcopy()) { return 0; } @@ -1010,6 +1031,14 @@ static int virtio_mem_post_load_early(void *opaque, = int version_id) return 0; } =20 + /* + * If shared RAM is migrated using the file content and not using QEMU, + * don't mess with preallocation and postcopy. + */ + if (migrate_ram_is_ignored(rb)) { + return 0; + } + /* * We restored the bitmap and verified that the basic properties * match on source and destination, so we can go ahead and preallocate --=20 2.40.1