From nobody Wed May 15 00:24:39 2024 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=1672922797; cv=none; d=zohomail.com; s=zohoarc; b=QKk3hMGQP9d08gRpKAohi6xy9y2Jw89yC0NVPcxDCh12U3op0Yp5F0Yu1r4u4UhQ3vJa/L+tFWD3FiC5FJ7Ol3lGJodJKnr3iz2pSx7XqNR3U+t9qsoY2XdnJ2vYpquzex4I/AcssJBlxu/HWvIAFqzV0SeiGL++U0sMrBe9bEs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672922797; 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=V+IS4Y6fGGsCuR9XOBO1PNaZzDstFV80dEiNW+nhajw=; b=cleiYdDfzhRYFIG3vJt4NEBkCeL7Zvk6OcVwFVejeuSCpjTGZkh8OdKTs8OcJ3tFCvQB024W63LeRrjEzp9z/A/rZlAHZgbBu1u6qce3iYrazjWNMxPYuLuYxla04DA1FYYwcP5tbMzco9Y1oL3TtAjcHZ/BgswTUIHuH3CxUl8= 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 16729227970891015.6556655274542; Thu, 5 Jan 2023 04:46:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDPd3-0000Z6-Bh; Thu, 05 Jan 2023 07:45:53 -0500 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 1pDPco-0000Y0-Es for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:39 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDPcl-0005n3-Ic for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:37 -0500 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-383-qiut-mYEOc-zKoqEgjT7Aw-1; Thu, 05 Jan 2023 07:45:33 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1E08B811E9C; Thu, 5 Jan 2023 12:45:33 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.193]) by smtp.corp.redhat.com (Postfix) with ESMTP id 59C061121314; Thu, 5 Jan 2023 12:45:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672922734; 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=V+IS4Y6fGGsCuR9XOBO1PNaZzDstFV80dEiNW+nhajw=; b=bDymI19Tjaw3uGsyJO8kQUwgq4XFbQ7Tmh7kGIF0JOPG6VZtzaIjZItjxidmMCQ3JTEuye /TwK9GlM51YKt3tBKt80aMY/acF1vAZD2c9vb8Qy4q64hcMl5RaB7GnnxjMxHllt55ByjJ N9skPpesLhUI0LwWv7Vm12g3dln6sqk= X-MC-Unique: qiut-mYEOc-zKoqEgjT7Aw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Juan Quintela , "Dr. David Alan Gilbert" , Peter Xu , Andrey Gruzdev , qemu-stable@nongnu.org Subject: [PATCH v1 1/5] migration/ram: Fix populate_read_range() Date: Thu, 5 Jan 2023 13:45:24 +0100 Message-Id: <20230105124528.93813-2-david@redhat.com> In-Reply-To: <20230105124528.93813-1-david@redhat.com> References: <20230105124528.93813-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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.129.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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: 1672922799445100001 Content-Type: text/plain; charset="utf-8" Unfortunately, commit f7b9dcfbcf44 broke populate_read_range(): the loop end condition is very wrong, resulting in that function not populating the full range. Lets' fix that. Fixes: f7b9dcfbcf44 ("migration/ram: Factor out populating pages readable i= n ram_block_populate_pages()") Cc: qemu-stable@nongnu.org Signed-off-by: David Hildenbrand Acked-by: Peter Xu Reviewed-by: Juan Quintela --- migration/ram.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/migration/ram.c b/migration/ram.c index 334309f1c6..b8f58d2a40 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -1774,13 +1774,15 @@ out: static inline void populate_read_range(RAMBlock *block, ram_addr_t offset, ram_addr_t size) { + const ram_addr_t end =3D offset + size; + /* * We read one byte of each page; this will preallocate page tables if * required and populate the shared zeropage on MAP_PRIVATE anonymous = memory * where no page was populated yet. This might require adaption when * supporting other mappings, like shmem. */ - for (; offset < size; offset +=3D block->page_size) { + for (; offset < end; offset +=3D block->page_size) { char tmp =3D *((char *)block->host + offset); =20 /* Don't optimize the read out */ --=20 2.39.0 From nobody Wed May 15 00:24:39 2024 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=1672922896; cv=none; d=zohomail.com; s=zohoarc; b=ROUpPLAWAe2GUoBObfAXEEZc805Ce2HHJ7jZRUgjzfaStJ81NfNsBI08BGLdvUUKaiK/Ql7fpVp9d+pCNE33tx+t81plyEki3kxPInAUTCHs/Yyx5YGzMFYjMuw6PkN+jXHySSnPHZriBNrGT9l6+gb/xrLqvzpaaArVh/uyk9Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672922896; 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=BILeqYNwEW1d4LTBUMoxKSfm/ZcEhG+psMo92quVnUc=; b=ZQZRNqCxKLO13/FSmvQ9jh5BZEllp+SyCc3+mAv28/4t64pDlhOEh3OX7XoO/JP8i0KHphA317UTWhvYSSU49dYMIy1pGUxsKFzRh9sAM/NY/rdsVXcSWvm88RntkaOS8zNdLRfwuTTUApUTWXuso1uQJROYJ3GSPWMqmx1X218= 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 1672922896492534.241404489496; Thu, 5 Jan 2023 04:48:16 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDPd5-0000co-4j; Thu, 05 Jan 2023 07:45:55 -0500 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 1pDPcs-0000YM-LU for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDPcp-0005oC-TE for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:42 -0500 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-361-GoYPr8NhNR-Ox2hHJCMY8w-1; Thu, 05 Jan 2023 07:45:35 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2932D8588E0; Thu, 5 Jan 2023 12:45:35 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.193]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7DD231121314; Thu, 5 Jan 2023 12:45:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672922738; 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=BILeqYNwEW1d4LTBUMoxKSfm/ZcEhG+psMo92quVnUc=; b=Wv2kPg7FgQd78r3uDs1IJb/7z65I+Ial0HBLR3mhDBrAQynRYi1wV3wkWeJsF1ad+AOzAo uqNYI+f4VdtJ13jCEK1hhk2H34Wn2He8DQohksSSlfa6AymFHg07JKm/xZPafFIeZ46KdT /nIFtXfXScX7wQfPDyv2NikWN3m7nkc= X-MC-Unique: GoYPr8NhNR-Ox2hHJCMY8w-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Juan Quintela , "Dr. David Alan Gilbert" , Peter Xu , Andrey Gruzdev , qemu-stable@nongnu.org Subject: [PATCH v1 2/5] migration/ram: Fix error handling in ram_write_tracking_start() Date: Thu, 5 Jan 2023 13:45:25 +0100 Message-Id: <20230105124528.93813-3-david@redhat.com> In-Reply-To: <20230105124528.93813-1-david@redhat.com> References: <20230105124528.93813-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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.129.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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: 1672922897491100001 Content-Type: text/plain; charset="utf-8" If something goes wrong during uffd_change_protection(), we would miss to unregister uffd-wp and not release our reference. Fix it by performing the uffd_change_protection(true) last. Note that a uffd_change_protection(false) on the recovery path without a prior uffd_change_protection(false) is fine. Fixes: 278e2f551a09 ("migration: support UFFD write fault processing in ram= _save_iterate()") Cc: qemu-stable@nongnu.org Signed-off-by: David Hildenbrand Acked-by: Peter Xu Reviewed-by: Juan Quintela --- migration/ram.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index b8f58d2a40..6e4e41952a 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -1896,13 +1896,14 @@ int ram_write_tracking_start(void) block->max_length, UFFDIO_REGISTER_MODE_WP, NULL)) { goto fail; } + block->flags |=3D RAM_UF_WRITEPROTECT; + memory_region_ref(block->mr); + /* Apply UFFD write protection to the block memory range */ if (uffd_change_protection(rs->uffdio_fd, block->host, block->max_length, true, false)) { goto fail; } - block->flags |=3D RAM_UF_WRITEPROTECT; - memory_region_ref(block->mr); =20 trace_ram_write_tracking_ramblock_start(block->idstr, block->page_= size, block->host, block->max_length); --=20 2.39.0 From nobody Wed May 15 00:24:39 2024 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=1672922828; cv=none; d=zohomail.com; s=zohoarc; b=NcpuwkCgpEAotkjNOiHPD5OOG3KPJgF8q/yJUviXHmjzkVYDW8wqz0WbJ1WV8ZkHvBK/oT2q9ADaIvAY/MjVnqoVxmTvW+2Dm1XMlMv4OmudfKXuLKAToI/Qp1vnbSkjEH6A2mfff4AkLNJhYIQO2/UheumE0+Wl/NgRF/BOdtc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672922828; 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=2GfLUSb47cFW7GGHwnXHzE6IrxP6X33v/rRHtLW5mzY=; b=SXLPLJWyIqm2uHd4IbHc/1sEU6ruFcoEXQxLYCYKv8mMkDQu9IesGgK4JUGR799s4pde63NTUyZlyChTelYZTbNWYmIhf0CTYe4rtx8bYulgHwxOVmxXgWsHwg1W3+PQDRRq8Y/81n/E+y0BXkiwvykSMLQduxXwNbQy+RFpYEI= 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 1672922828051287.7786954098507; Thu, 5 Jan 2023 04:47:08 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDPd5-0000cp-5K; Thu, 05 Jan 2023 07:45:55 -0500 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 1pDPct-0000YP-UW for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:45 -0500 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 1pDPcr-0005of-BB for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:43 -0500 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-282-yr2vg_tOMNSQxoShwVKfMw-1; Thu, 05 Jan 2023 07:45:37 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 126F51C0518A; Thu, 5 Jan 2023 12:45:37 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.193]) by smtp.corp.redhat.com (Postfix) with ESMTP id 896621121314; Thu, 5 Jan 2023 12:45:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672922740; 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=2GfLUSb47cFW7GGHwnXHzE6IrxP6X33v/rRHtLW5mzY=; b=DyUDA911yFooXyD2wk0DbbkXPhfIcczpRf7Zy43qPilh4I0Tw3JkEG1fHGOrNGzDAViFXD NqzRAqBxy87odQF8fBeudQhrMa3M65C/fJsHr89gaORLFe3f3mTCK7BiOt0h0jL5s9gN60 QIv1Njujq+bHuDL0HnuwVVBqWf1psfA= X-MC-Unique: yr2vg_tOMNSQxoShwVKfMw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Juan Quintela , "Dr. David Alan Gilbert" , Peter Xu , Andrey Gruzdev Subject: [PATCH v1 3/5] migration/ram: Don't explicitly unprotect when unregistering uffd-wp Date: Thu, 5 Jan 2023 13:45:26 +0100 Message-Id: <20230105124528.93813-4-david@redhat.com> In-Reply-To: <20230105124528.93813-1-david@redhat.com> References: <20230105124528.93813-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: 1672922829033100001 Content-Type: text/plain; charset="utf-8" When unregistering uffd-wp, older kernels before commit f369b07c86143 ("mm/uffd:reset write protection when unregister with wp-mode") won't clear the uffd-wp PTE bit. When re-registering uffd-wp, the previous uffd-wp PTE bits would trigger again. With above commit, the kernel will clear the uffd-wp PTE bits when unregistering itself. Consequently, we'll clear the uffd-wp PTE bits now twice -- whereby we don't care about clearing them at all: a new background snapshot will re-register uffd-wp and re-protect all memory either way. So let's skip the manual clearing of uffd-wp. If ever relevant, we could clear conditionally in uffd_unregister_memory() -- we just need a way to figure out more recent kernels. Signed-off-by: David Hildenbrand Acked-by: Peter Xu Reviewed-by: Juan Quintela --- migration/ram.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index 6e4e41952a..6a3dbee2c3 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -1918,12 +1918,6 @@ fail: if ((block->flags & RAM_UF_WRITEPROTECT) =3D=3D 0) { continue; } - /* - * In case some memory block failed to be write-protected - * remove protection and unregister all succeeded RAM blocks - */ - uffd_change_protection(rs->uffdio_fd, block->host, block->max_leng= th, - false, false); uffd_unregister_memory(rs->uffdio_fd, block->host, block->max_leng= th); /* Cleanup flags and remove reference */ block->flags &=3D ~RAM_UF_WRITEPROTECT; @@ -1949,9 +1943,6 @@ void ram_write_tracking_stop(void) if ((block->flags & RAM_UF_WRITEPROTECT) =3D=3D 0) { continue; } - /* Remove protection and unregister all affected RAM blocks */ - uffd_change_protection(rs->uffdio_fd, block->host, block->max_leng= th, - false, false); uffd_unregister_memory(rs->uffdio_fd, block->host, block->max_leng= th); =20 trace_ram_write_tracking_ramblock_stop(block->idstr, block->page_s= ize, --=20 2.39.0 From nobody Wed May 15 00:24:39 2024 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=1672922810; cv=none; d=zohomail.com; s=zohoarc; b=VDfYjx+JaMShTlusg6q0sP3vE1/iBhZtPMcNTCKFIs3ngdIPOi6iWUx+Bj8m4i1E9eBMKi4A/rJ3ojzkBkAUFWFcfmVrDlENxSRjlxVO/sUiAOtyBre5jdO1bTmAMS9mhQwg+vDbIy8AOAIrnYbi7toJI0joNTzM/oOEV54nXEI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672922810; 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=OKjPvhnoNBzLZXaSESBUhCck4dzrCgas7ZbjAuGp4CU=; b=jyysKYprWK3b8zX8QwFPw4Gu1JdKF3H1O+gaBv1kk8fXnyrjurtWx/fz0raPG/712RIRbhz6ff26ffGppMq9ZpnQdIPV9c2e+O26VZ6EZ6U5kExiOdHtoUaC2pnQ0Kem4mSO2wiGdK0hXbqLTtakWLlIkwwPLFoa1b3gJAkSBy0= 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 1672922809903164.679121893382; Thu, 5 Jan 2023 04:46:49 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDPd6-0000dA-0D; Thu, 05 Jan 2023 07:45:56 -0500 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 1pDPcv-0000YW-13 for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDPcr-0005ob-9y for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:44 -0500 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-297-15utGDaQMXqAuac0AH87-A-1; Thu, 05 Jan 2023 07:45:38 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8706D3C0D18E; Thu, 5 Jan 2023 12:45:38 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.193]) by smtp.corp.redhat.com (Postfix) with ESMTP id 514D01121314; Thu, 5 Jan 2023 12:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672922740; 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=OKjPvhnoNBzLZXaSESBUhCck4dzrCgas7ZbjAuGp4CU=; b=GlhMQfbwSGtF3nYtz+BNXDoKTZGGdFD9yppc4NbH/+CHgL5/V0lj/uUtJQf1El3sTfnlYp DbCCeJbTOCik22XkPIcddYb1a+auV6Xh+iNDIZa+/KThjA2bej/cQL/l2/faTJEkBTEiat aiBY9Y9DSfCPf4okU8x1MD7sixVCJw8= X-MC-Unique: 15utGDaQMXqAuac0AH87-A-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Juan Quintela , "Dr. David Alan Gilbert" , Peter Xu , Andrey Gruzdev Subject: [PATCH v1 4/5] migration/ram: Rely on used_length for uffd_change_protection() Date: Thu, 5 Jan 2023 13:45:27 +0100 Message-Id: <20230105124528.93813-5-david@redhat.com> In-Reply-To: <20230105124528.93813-1-david@redhat.com> References: <20230105124528.93813-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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.129.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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: 1672922811009100001 Content-Type: text/plain; charset="utf-8" ram_mig_ram_block_resized() will abort migration (including background snapshots) when resizing a RAMBlock. ram_block_populate_read() will only populate RAM up to used_length, so at least for anonymous memory protecting everything between used_length and max_length won't actually be protected and is just a NOP. So let's only protect everything up to used_length. Note: it still makes sense to register uffd-wp for max_length, such that RAM_UF_WRITEPROTECT is independent of a changing used_length. Signed-off-by: David Hildenbrand Acked-by: Peter Xu Reviewed-by: Juan Quintela --- migration/ram.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migration/ram.c b/migration/ram.c index 6a3dbee2c3..73a443f683 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -1901,7 +1901,7 @@ int ram_write_tracking_start(void) =20 /* Apply UFFD write protection to the block memory range */ if (uffd_change_protection(rs->uffdio_fd, block->host, - block->max_length, true, false)) { + block->used_length, true, false)) { goto fail; } =20 --=20 2.39.0 From nobody Wed May 15 00:24:39 2024 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=1672922886; cv=none; d=zohomail.com; s=zohoarc; b=bazWHzmTbj6SScDmFZJ5yz1h6vf9ZnhbRSC2ZhIBpjqV+lRsJUbuZxHadpoaq3vqyfjOEwlVpc0Xx099BR/dWIV8HoQzh/kpWTGsVKsuiviYkGh+EMHEo8AU+mQmu7tswj+fOXY/VVhirouBxNKCUGuxBC+ShE+fX9JAzJBxi5A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672922886; 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=WPmYAC4LzXplNOMFY/mdBp/PDJUQoccYj65ItuzdQOI=; b=jxyjBQ7N2IfnB4iq0ucS9vRHGS0Pjf9h8SIlPHI1JX5vBvjPkpFORuwNlBCEIM9rjv/fLn/6iBHrSMvArwYcyGFUT1G0dBPWP5nKyr1a+VJ3J1X9z/2tMnTZuUp8Q/8OuhSczvUb+cq60XuJG59anWTtTDOG/Qd3t5+ZuWJXtpk= 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 1672922886349517.1435802997887; Thu, 5 Jan 2023 04:48:06 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDPd7-0000dd-Lt; Thu, 05 Jan 2023 07:45:57 -0500 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 1pDPcw-0000ZL-Eg for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDPcu-0005pE-9r for qemu-devel@nongnu.org; Thu, 05 Jan 2023 07:45:45 -0500 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-371-uRWryMsKOoGjX7HvRuwu9Q-1; Thu, 05 Jan 2023 07:45:40 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 024BC1C0518A; Thu, 5 Jan 2023 12:45:40 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.193]) by smtp.corp.redhat.com (Postfix) with ESMTP id CD14A1121314; Thu, 5 Jan 2023 12:45:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672922743; 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=WPmYAC4LzXplNOMFY/mdBp/PDJUQoccYj65ItuzdQOI=; b=EQK90VTbNaRm2z3rWxcAzsLRIG2XUIzz8NYRmm3OaaHGLlIS/k+usssNLfgU9d9tcy3C2m SO+kH5vTbVFzj4qoqzxO8bnXuZsLhft9IiCS62BqodMOGLJYzC6zH8hl287Bla9QTjVAKJ XjlI8HyJC9yU/nff5wLnkR79e6W85xQ= X-MC-Unique: uRWryMsKOoGjX7HvRuwu9Q-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Juan Quintela , "Dr. David Alan Gilbert" , Peter Xu , Andrey Gruzdev Subject: [PATCH v1 5/5] migration/ram: Optimize ram_write_tracking_start() for RamDiscardManager Date: Thu, 5 Jan 2023 13:45:28 +0100 Message-Id: <20230105124528.93813-6-david@redhat.com> In-Reply-To: <20230105124528.93813-1-david@redhat.com> References: <20230105124528.93813-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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.129.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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: 1672922887485100001 Content-Type: text/plain; charset="utf-8" ram_block_populate_read() already optimizes for RamDiscardManager. However, ram_write_tracking_start() will still try protecting discarded memory ranges. Let's optimize, because discarded ranges don't map any pages and (1) For anonymous memory, trying to protect using uffd-wp without a mapped page is ignored by the kernel and consequently a NOP. (2) For shared/file-backed memory, we will fill present page tables in the range with PTE markers. However, we will even allocate page tables just to fill them with unnecessary PTE markers and effectively waste memory. So let's exclude these ranges, just like ram_block_populate_read() already does. Signed-off-by: David Hildenbrand Acked-by: Peter Xu Reviewed-by: Juan Quintela --- migration/ram.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index 73a443f683..50ee1fa147 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -1865,6 +1865,39 @@ void ram_write_tracking_prepare(void) } } =20 +static inline int uffd_protect_section(MemoryRegionSection *section, + void *opaque) +{ + const hwaddr size =3D int128_get64(section->size); + const hwaddr offset =3D section->offset_within_region; + RAMBlock *rb =3D section->mr->ram_block; + int uffd_fd =3D (uintptr_t)opaque; + + return uffd_change_protection(uffd_fd, rb->host + offset, size, true, + false); +} + +static int ram_block_uffd_protect(RAMBlock *rb, int uffd_fd) +{ + assert(rb->flags & RAM_UF_WRITEPROTECT); + + /* See ram_block_populate_read() */ + if (rb->mr && memory_region_has_ram_discard_manager(rb->mr)) { + RamDiscardManager *rdm =3D memory_region_get_ram_discard_manager(r= b->mr); + MemoryRegionSection section =3D { + .mr =3D rb->mr, + .offset_within_region =3D 0, + .size =3D rb->mr->size, + }; + + return ram_discard_manager_replay_populated(rdm, §ion, + uffd_protect_section, + (void *)(uintptr_t)uff= d_fd); + } + return uffd_change_protection(uffd_fd, rb->host, + rb->used_length, true, false); +} + /* * ram_write_tracking_start: start UFFD-WP memory tracking * @@ -1900,8 +1933,7 @@ int ram_write_tracking_start(void) memory_region_ref(block->mr); =20 /* Apply UFFD write protection to the block memory range */ - if (uffd_change_protection(rs->uffdio_fd, block->host, - block->used_length, true, false)) { + if (ram_block_uffd_protect(block, uffd_fd)) { goto fail; } =20 --=20 2.39.0