From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709277; cv=none; d=zohomail.com; s=zohoarc; b=fJsohw8LQHWr1mXXHBentEP8XG7i7OTpu1GTy/TJUjwpnYsSkQsoTzlFkA/gWuIQidDdraldj8fX39rRbnsUoMz3KXsUyFCX3jF5C6x0w2VNHrzdWLE7Y0QgA5x7xSIiV05KclyCbFmzpOJyBLVP1JqY2osH0eFz4Dv3WP0rNAo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709277; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=EAQgpEBtNd61PFcFDZwuAl96RIgWN8ZVT+yu/6lUlPg=; b=fba/7jDT5MRkre6ScPsTMNczvKrRDS9lS8cPePQnWy1q8HzVx7t4zNiHNVG7P3pG8vMjK+eVoyiLNA/hXkGdo94lFEiKqmpw2/87npH4EYZRnUFealA8/yIPetG2JyLIfTz+amZgha730X5aMuIJu8bH5G7ido1NXXbKlBUj5Bs= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709277569850.2857730093413; Mon, 23 Jun 2025 13:07:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRc-0005xy-M5; Mon, 23 Jun 2025 16:07:08 -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 1uTnRb-0005xj-9a for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:07 -0400 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 1uTnRZ-0003TX-Fh for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:07 -0400 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-91-eYv-padIMDqFLYfLqa8pfg-1; Mon, 23 Jun 2025 16:07:02 -0400 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6fabb9286f9so101368776d6.1 for ; Mon, 23 Jun 2025 13:07:02 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.06.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709224; 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=EAQgpEBtNd61PFcFDZwuAl96RIgWN8ZVT+yu/6lUlPg=; b=dwVGaYkJ9Y5lYmsJ/+s11R/e+uEV6l1JNOMfQ/bzKmqE6/ePCaHeLpaEukXwUWKCv36yAa 5rlZQyP1x8ved0kCZjzL3kTExvb80vP8X+QH9VulQhRxHn20SmqQWaGQcIG/vnzW65v2bn oSSESyRGrF7Am6BUcvYnnlGwoIxLi9M= X-MC-Unique: eYv-padIMDqFLYfLqa8pfg-1 X-Mimecast-MFC-AGG-ID: eYv-padIMDqFLYfLqa8pfg_1750709222 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709221; x=1751314021; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EAQgpEBtNd61PFcFDZwuAl96RIgWN8ZVT+yu/6lUlPg=; b=I3+44qxXVC91Zg3rQJ/GZUaTA8AbyS4gwtkjV4+Nmz7DcZGdBW/H9A9yFeby2wwgep 6c9Zv6nZctVxQP/A5sQOF6Bar2+4gfxLY1hvt5wADeXv5rAL3M9F6HWLr+XU7IpmZMTD YfvfQSTYtlWMZcsX7Da2mXdR5PVFUUDmgveFAdzoziE/PLjkRKeVbF+mq13zeDf8OUrf S9hl9Ji9fj6Bs7SmYTHiK5N3fe/HZUPBI3jD7VCf0KBeKx+ckPaBLuQx4EmZiS5NvHhE +9/LPjw4rHph7DkWjTcBRjDhtLeDnl4Tv4zXRTsYDTRHyDkrDWClw8L7V48fkVMO31Nk tTuw== X-Gm-Message-State: AOJu0Yx/hNjRdwA9q4Y47N3wZoghvT8+RRipLjtDmsi7eE6ac+WqClEG 2FE1UazIEQG+RSkua87xGrEcHGpzItzyxbcTgKB6079OWwI9k1hoB49hNVG+KAB0IStNKEp6Tpr FjqMGyoaJsMe8zkMql7l/bqk9JE0tFPbKiY4KoLwWDcYWE66wFzj2hl7vkSaiKY5km6gD6GNfVW d8/9oBmlQYTQVde1q8JA9sfDBe7foPVm0kEEvLXQ== X-Gm-Gg: ASbGncu461A7VmbG5sgrbzvrTQ1sZMnsSl/1jx4vksuL5GMH0Ll2qcZbe1RLe3ZtUtm dP1i5IaNWI/K7n7GVIOGzU9M6wiSvEDFgr+G3+EGsQHW4yg+WSmnpaqOouW4UoC9KUFccx07zu/ yhTKujjNE85dlupuU6Hv230Ud78B/kCwvT85d4AHVHW7hSeyq/UAR05aXMImN4fSNuLnnueh7Uc 3nnRpWGP3NQ7L5IgvZebrn+t2pjHlpOqps5r35LvHoYur/zjAtv8q/J9AzinR2cOfppTOSacRk6 tbOUQYOFlCE= X-Received: by 2002:a05:6214:2427:b0:6fb:43d:65b7 with SMTP id 6a1803df08f44-6fd0a5c1776mr253069626d6.36.1750709221528; Mon, 23 Jun 2025 13:07:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFvtDS3RNGAUkS/Uvi1RMDCbRBsjpqYwzYqvh4v0pkgYN9iN3kzYE4+MlZ/Vnl6oWYqWjkHlA== X-Received: by 2002:a05:6214:2427:b0:6fb:43d:65b7 with SMTP id 6a1803df08f44-6fd0a5c1776mr253069156d6.36.1750709221025; Mon, 23 Jun 2025 13:07:01 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Yanfei Xu Subject: [PULL 01/11] migration/ram: avoid to do log clear in the last round Date: Mon, 23 Jun 2025 16:06:46 -0400 Message-ID: <20250623200656.1882674-2-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709279257116600 Content-Type: text/plain; charset="utf-8" From: Yanfei Xu There won't be any ram sync after the stage of save_complete, therefore it's unnecessary to do manually protect for dirty pages being sent. Skip to do this in last round can reduce noticeable downtime. Signed-off-by: Yanfei Xu Tested-by: Fabiano Rosas Reviewed-by: Fabiano Rosas Link: https://lore.kernel.org/r/20250514115827.3216082-1-yanfei.xu@bytedanc= e.com [peterx: add comments] Signed-off-by: Peter Xu --- migration/ram.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index d26dbd37c4..fd8d83b63c 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -831,14 +831,22 @@ static inline bool migration_bitmap_clear_dirty(RAMSt= ate *rs, bool ret; =20 /* - * Clear dirty bitmap if needed. This _must_ be called before we - * send any of the page in the chunk because we need to make sure - * we can capture further page content changes when we sync dirty - * log the next time. So as long as we are going to send any of - * the page in the chunk we clear the remote dirty bitmap for all. - * Clearing it earlier won't be a problem, but too late will. + * During the last stage (after source VM stopped), resetting the write + * protections isn't needed as we know there will be either (1) no + * further writes if migration will complete, or (2) migration fails + * at last then tracking isn't needed either. */ - migration_clear_memory_region_dirty_bitmap(rb, page); + if (!rs->last_stage) { + /* + * Clear dirty bitmap if needed. This _must_ be called before we + * send any of the page in the chunk because we need to make sure + * we can capture further page content changes when we sync dirty + * log the next time. So as long as we are going to send any of + * the page in the chunk we clear the remote dirty bitmap for all. + * Clearing it earlier won't be a problem, but too late will. + */ + migration_clear_memory_region_dirty_bitmap(rb, page); + } =20 ret =3D test_and_clear_bit(page, rb->bmap); if (ret) { --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709304; cv=none; d=zohomail.com; s=zohoarc; b=P9CIRiEWl9pD3kFeg+L8SSb+m66d+nt3382HtHM1e5RnTPY7MBCdicfRIyyp13gJBGNtzuUFzuKvOuQSlaFf7CD3VLiKKIw2nl/VH/W/jqXh0d34+HDQKfOpfv5T2kDX04DV46p9oJJqAb48mGLTQxuszXakFY9z77MwGY0BDKs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709304; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Gyi67L/mCVdqWaAB2hOwjtOrHcsq7jHl0MC/l/bPYKQ=; b=JHqLXI21/62U7WhT56l72YEXa9ovy7sVXBY5K1nuyLN7Lap7ui1y61ckMZnvzxn1k2Mkh1edpMDermHeyKkFDpn6N6cY/RoF4Mbcm1csszIzyX9MqUHNEkzexusFktOH3E8a8tSHd7T/IXxub7h0ov7Cwqq1zeBCg5APIINGCYw= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709304423239.5751721678373; Mon, 23 Jun 2025 13:08:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRg-0005zS-LW; Mon, 23 Jun 2025 16:07:12 -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 1uTnRd-0005y1-KB for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:09 -0400 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 1uTnRa-0003U6-Q1 for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:09 -0400 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-533-DdJULZaBOvuPrf0fu_uShg-1; Mon, 23 Jun 2025 16:07:04 -0400 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7caee990721so1046139885a.1 for ; Mon, 23 Jun 2025 13:07:04 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709226; 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=Gyi67L/mCVdqWaAB2hOwjtOrHcsq7jHl0MC/l/bPYKQ=; b=PFrXBDxxTm3hJhcC8QYmNWH/MLnLUdSFIsr+Hvmz327exa5iqAX+ab046FdJnh+D+XFGjc RF433TLS+cFzNhOUwNRcAdMBrGga9+VQdRHbKFB9yQgixsMsFqO4S753VZF/QFzlyxpgez EKinRn4eVhlyk4mhMEyTkP4iDGvRJwc= X-MC-Unique: DdJULZaBOvuPrf0fu_uShg-1 X-Mimecast-MFC-AGG-ID: DdJULZaBOvuPrf0fu_uShg_1750709224 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709223; x=1751314023; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Gyi67L/mCVdqWaAB2hOwjtOrHcsq7jHl0MC/l/bPYKQ=; b=gv94kAljrfAMugCCnA1c7h82qvqa/xQihOk0mii0h2gK1PcRgtOoY6Ze3eF0OzgEgv H8tO7q3Nu2cRdF+Q7rsXDUf5jiGTFWB2wlESzG43LMUtLALeQ6losc4qDiy7zM8geukj jGkhxcXvPlUBgn2jTyoaDAjGYM9NMvN3dx2Whv77MTqZ4xungd7e2YST7YfceBHD56zP ZtrofmlfGhgx/5AeG+TXC2ZBbu358o09n9O0d2FWJwACE5FNSq3CfsN4mqOFI+SHkd+8 xkLHpEu0my/M0ZQ3eLvacVuLIUAimTKArqmEGmkK62uPDxekSiUYoQJncS4sh6CB8sxf l/1A== X-Gm-Message-State: AOJu0YyHGjYL7VDtfC79ob2t/H1tZVwE5Ux9scyLd8SXNm8Kx9vGPYOO RnY87HLXh3vZz3JMfdXFdGdpsPigr1GmWU9CJFPafW1lJKQ8b0YeNkCaxkHsSUFCtRRcQ2+60k1 qBa9BFz9z1z1JqkzQ5D0aCQxTW8cO+HwhHJX/wLQLK7J8Lgpa7ni6gg2h4vQ45Vuk0+iUPEya6k 5kfALgNMy48WiPk5xc+B01xuxjYUAdyiLjYPb4fQ== X-Gm-Gg: ASbGncvpud6o6YX0af7BXT/76hYwwdkmB4QgNAtOaOhR/COBIdJxyD6e5J3CgK8ynvJ 6atCy3uwlrsOrDLi3XPxmDkqPUEOaffvBuHHSGq3nmgJt/Dh3f7lqJasxml1zhsHcrJmBxtMf0J VACpnsT8Jij8HCkV2n54CpQCptqP7Z0ioRVyLQzV3rQNtE/xQ1Jb3oycQcLX8NCrWgQQS2nJd6J Am1opRpX1cJD+Ultbfpg+K6a4ADkU75jcjWM4haB39pMCamI/1GqMyGZ40gzqDVc+gwGHeJJz8y F4tmEqph98g= X-Received: by 2002:a05:620a:190d:b0:7c5:3d60:7f91 with SMTP id af79cd13be357-7d3f98dce2cmr1873790985a.15.1750709223257; Mon, 23 Jun 2025 13:07:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGcPWRko38gPId8sI1wzYPZRCKHVhK99EGO6/kWY6aVxIuit0K6kkAbHE/7wtvKS1R634p+Qg== X-Received: by 2002:a05:620a:190d:b0:7c5:3d60:7f91 with SMTP id af79cd13be357-7d3f98dce2cmr1873785885a.15.1750709222605; Mon, 23 Jun 2025 13:07:02 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Markus Armbruster Subject: [PULL 02/11] tests/qtest: Remove migration-helpers.c Date: Mon, 23 Jun 2025 16:06:47 -0400 Message-ID: <20250623200656.1882674-3-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709305580116600 Content-Type: text/plain; charset="utf-8" From: Fabiano Rosas Commit 407bc4bf90 ("qapi: Move include/qapi/qmp/ to include/qobject/") brought the migration-helpers.c back by mistake. This file has been replaced with migration/migration-qmp.c and migration/migration-util.c. Fixes: 407bc4bf90 ("qapi: Move include/qapi/qmp/ to include/qobject/") Signed-off-by: Fabiano Rosas Message-id: 20200310152141.13959-1-peter.maydell@linaro.org Reviewed-by: Markus Armbruster Link: https://lore.kernel.org/r/20250523123023.19284-1-farosas@suse.de Signed-off-by: Peter Xu --- tests/qtest/migration-helpers.c | 530 -------------------------------- 1 file changed, 530 deletions(-) delete mode 100644 tests/qtest/migration-helpers.c diff --git a/tests/qtest/migration-helpers.c b/tests/qtest/migration-helper= s.c deleted file mode 100644 index b08b49bd43..0000000000 --- a/tests/qtest/migration-helpers.c +++ /dev/null @@ -1,530 +0,0 @@ -/* - * QTest migration helpers - * - * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates - * based on the vhost-user-test.c that is: - * Copyright (c) 2014 Virtual Open Systems Sarl. - * - * This work is licensed under the terms of the GNU GPL, version 2 or late= r. - * See the COPYING file in the top-level directory. - * - */ - -#include "qemu/osdep.h" -#include "qemu/ctype.h" -#include "qobject/qjson.h" -#include "qapi/qapi-visit-sockets.h" -#include "qapi/qobject-input-visitor.h" -#include "qapi/error.h" -#include "qobject/qlist.h" -#include "qemu/cutils.h" -#include "qemu/memalign.h" - -#include "migration-helpers.h" - -/* - * Number of seconds we wait when looking for migration - * status changes, to avoid test suite hanging forever - * when things go wrong. Needs to be higher enough to - * avoid false positives on loaded hosts. - */ -#define MIGRATION_STATUS_WAIT_TIMEOUT 120 - -static char *SocketAddress_to_str(SocketAddress *addr) -{ - switch (addr->type) { - case SOCKET_ADDRESS_TYPE_INET: - return g_strdup_printf("tcp:%s:%s", - addr->u.inet.host, - addr->u.inet.port); - case SOCKET_ADDRESS_TYPE_UNIX: - return g_strdup_printf("unix:%s", - addr->u.q_unix.path); - case SOCKET_ADDRESS_TYPE_FD: - return g_strdup_printf("fd:%s", addr->u.fd.str); - case SOCKET_ADDRESS_TYPE_VSOCK: - return g_strdup_printf("vsock:%s:%s", - addr->u.vsock.cid, - addr->u.vsock.port); - default: - return g_strdup("unknown address type"); - } -} - -static QDict *SocketAddress_to_qdict(SocketAddress *addr) -{ - QDict *dict =3D qdict_new(); - - switch (addr->type) { - case SOCKET_ADDRESS_TYPE_INET: - qdict_put_str(dict, "type", "inet"); - qdict_put_str(dict, "host", addr->u.inet.host); - qdict_put_str(dict, "port", addr->u.inet.port); - break; - case SOCKET_ADDRESS_TYPE_UNIX: - qdict_put_str(dict, "type", "unix"); - qdict_put_str(dict, "path", addr->u.q_unix.path); - break; - case SOCKET_ADDRESS_TYPE_FD: - qdict_put_str(dict, "type", "fd"); - qdict_put_str(dict, "str", addr->u.fd.str); - break; - case SOCKET_ADDRESS_TYPE_VSOCK: - qdict_put_str(dict, "type", "vsock"); - qdict_put_str(dict, "cid", addr->u.vsock.cid); - qdict_put_str(dict, "port", addr->u.vsock.port); - break; - default: - g_assert_not_reached(); - } - - return dict; -} - -static SocketAddressList *migrate_get_socket_address(QTestState *who) -{ - QDict *rsp; - SocketAddressList *addrs; - Visitor *iv =3D NULL; - QObject *object; - - rsp =3D migrate_query(who); - object =3D qdict_get(rsp, "socket-address"); - - iv =3D qobject_input_visitor_new(object); - visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort); - visit_free(iv); - - qobject_unref(rsp); - return addrs; -} - -static char * -migrate_get_connect_uri(QTestState *who) -{ - SocketAddressList *addrs; - char *connect_uri; - - addrs =3D migrate_get_socket_address(who); - connect_uri =3D SocketAddress_to_str(addrs->value); - - qapi_free_SocketAddressList(addrs); - return connect_uri; -} - -static QDict * -migrate_get_connect_qdict(QTestState *who) -{ - SocketAddressList *addrs; - QDict *connect_qdict; - - addrs =3D migrate_get_socket_address(who); - connect_qdict =3D SocketAddress_to_qdict(addrs->value); - - qapi_free_SocketAddressList(addrs); - return connect_qdict; -} - -static void migrate_set_ports(QTestState *to, QList *channel_list) -{ - QDict *addr; - QListEntry *entry; - const char *addr_port =3D NULL; - - addr =3D migrate_get_connect_qdict(to); - - QLIST_FOREACH_ENTRY(channel_list, entry) { - QDict *channel =3D qobject_to(QDict, qlist_entry_obj(entry)); - QDict *addrdict =3D qdict_get_qdict(channel, "addr"); - - if (qdict_haskey(addrdict, "port") && - qdict_haskey(addr, "port") && - (strcmp(qdict_get_str(addrdict, "port"), "0") =3D=3D 0)) { - addr_port =3D qdict_get_str(addr, "port"); - qdict_put_str(addrdict, "port", addr_port); - } - } - - qobject_unref(addr); -} - -bool migrate_watch_for_events(QTestState *who, const char *name, - QDict *event, void *opaque) -{ - QTestMigrationState *state =3D opaque; - - if (g_str_equal(name, "STOP")) { - state->stop_seen =3D true; - return true; - } else if (g_str_equal(name, "SUSPEND")) { - state->suspend_seen =3D true; - return true; - } else if (g_str_equal(name, "RESUME")) { - state->resume_seen =3D true; - return true; - } - - return false; -} - -void migrate_qmp_fail(QTestState *who, const char *uri, - const char *channels, const char *fmt, ...) -{ - va_list ap; - QDict *args, *err; - - va_start(ap, fmt); - args =3D qdict_from_vjsonf_nofail(fmt, ap); - va_end(ap); - - g_assert(!qdict_haskey(args, "uri")); - if (uri) { - qdict_put_str(args, "uri", uri); - } - - g_assert(!qdict_haskey(args, "channels")); - if (channels) { - QObject *channels_obj =3D qobject_from_json(channels, &error_abort= ); - qdict_put_obj(args, "channels", channels_obj); - } - - err =3D qtest_qmp_assert_failure_ref( - who, "{ 'execute': 'migrate', 'arguments': %p}", args); - - g_assert(qdict_haskey(err, "desc")); - - qobject_unref(err); -} - -/* - * Send QMP command "migrate". - * Arguments are built from @fmt... (formatted like - * qobject_from_jsonf_nofail()) with "uri": @uri spliced in. - */ -void migrate_qmp(QTestState *who, QTestState *to, const char *uri, - const char *channels, const char *fmt, ...) -{ - va_list ap; - QDict *args; - g_autofree char *connect_uri =3D NULL; - - va_start(ap, fmt); - args =3D qdict_from_vjsonf_nofail(fmt, ap); - va_end(ap); - - g_assert(!qdict_haskey(args, "uri")); - if (uri) { - qdict_put_str(args, "uri", uri); - } else if (!channels) { - connect_uri =3D migrate_get_connect_uri(to); - qdict_put_str(args, "uri", connect_uri); - } - - g_assert(!qdict_haskey(args, "channels")); - if (channels) { - QObject *channels_obj =3D qobject_from_json(channels, &error_abort= ); - QList *channel_list =3D qobject_to(QList, channels_obj); - migrate_set_ports(to, channel_list); - qdict_put_obj(args, "channels", channels_obj); - } - - qtest_qmp_assert_success(who, - "{ 'execute': 'migrate', 'arguments': %p}", a= rgs); -} - -void migrate_set_capability(QTestState *who, const char *capability, - bool value) -{ - qtest_qmp_assert_success(who, - "{ 'execute': 'migrate-set-capabilities'," - "'arguments': { " - "'capabilities': [ { " - "'capability': %s, 'state': %i } ] } }", - capability, value); -} - -void migrate_incoming_qmp(QTestState *to, const char *uri, const char *fmt= , ...) -{ - va_list ap; - QDict *args, *rsp; - - va_start(ap, fmt); - args =3D qdict_from_vjsonf_nofail(fmt, ap); - va_end(ap); - - g_assert(!qdict_haskey(args, "uri")); - qdict_put_str(args, "uri", uri); - - /* This function relies on the event to work, make sure it's enabled */ - migrate_set_capability(to, "events", true); - - rsp =3D qtest_qmp(to, "{ 'execute': 'migrate-incoming', 'arguments': %= p}", - args); - - if (!qdict_haskey(rsp, "return")) { - g_autoptr(GString) s =3D qobject_to_json_pretty(QOBJECT(rsp), true= ); - g_test_message("%s", s->str); - } - - g_assert(qdict_haskey(rsp, "return")); - qobject_unref(rsp); - - migration_event_wait(to, "setup"); -} - -/* - * Note: caller is responsible to free the returned object via - * qobject_unref() after use - */ -QDict *migrate_query(QTestState *who) -{ - return qtest_qmp_assert_success_ref(who, "{ 'execute': 'query-migrate'= }"); -} - -QDict *migrate_query_not_failed(QTestState *who) -{ - const char *status; - QDict *rsp =3D migrate_query(who); - status =3D qdict_get_str(rsp, "status"); - if (g_str_equal(status, "failed")) { - g_printerr("query-migrate shows failed migration: %s\n", - qdict_get_str(rsp, "error-desc")); - } - g_assert(!g_str_equal(status, "failed")); - return rsp; -} - -/* - * Note: caller is responsible to free the returned object via - * g_free() after use - */ -static gchar *migrate_query_status(QTestState *who) -{ - QDict *rsp_return =3D migrate_query(who); - gchar *status =3D g_strdup(qdict_get_str(rsp_return, "status")); - - g_assert(status); - qobject_unref(rsp_return); - - return status; -} - -static bool check_migration_status(QTestState *who, const char *goal, - const char **ungoals) -{ - bool ready; - char *current_status; - const char **ungoal; - - current_status =3D migrate_query_status(who); - ready =3D strcmp(current_status, goal) =3D=3D 0; - if (!ungoals) { - g_assert_cmpstr(current_status, !=3D, "failed"); - /* - * If looking for a state other than completed, - * completion of migration would cause the test to - * hang. - */ - if (strcmp(goal, "completed") !=3D 0) { - g_assert_cmpstr(current_status, !=3D, "completed"); - } - } else { - for (ungoal =3D ungoals; *ungoal; ungoal++) { - g_assert_cmpstr(current_status, !=3D, *ungoal); - } - } - g_free(current_status); - return ready; -} - -void wait_for_migration_status(QTestState *who, - const char *goal, const char **ungoals) -{ - g_test_timer_start(); - while (!check_migration_status(who, goal, ungoals)) { - usleep(1000); - - g_assert(g_test_timer_elapsed() < MIGRATION_STATUS_WAIT_TIMEOUT); - } -} - -void wait_for_migration_complete(QTestState *who) -{ - wait_for_migration_status(who, "completed", NULL); -} - -void wait_for_migration_fail(QTestState *from, bool allow_active) -{ - g_test_timer_start(); - QDict *rsp_return; - char *status; - bool failed; - - do { - status =3D migrate_query_status(from); - bool result =3D !strcmp(status, "setup") || !strcmp(status, "faile= d") || - (allow_active && !strcmp(status, "active")); - if (!result) { - fprintf(stderr, "%s: unexpected status status=3D%s allow_activ= e=3D%d\n", - __func__, status, allow_active); - } - g_assert(result); - failed =3D !strcmp(status, "failed"); - g_free(status); - - g_assert(g_test_timer_elapsed() < MIGRATION_STATUS_WAIT_TIMEOUT); - } while (!failed); - - /* Is the machine currently running? */ - rsp_return =3D qtest_qmp_assert_success_ref(from, - "{ 'execute': 'query-status'= }"); - g_assert(qdict_haskey(rsp_return, "running")); - g_assert(qdict_get_bool(rsp_return, "running")); - qobject_unref(rsp_return); -} - -char *find_common_machine_version(const char *mtype, const char *var1, - const char *var2) -{ - g_autofree char *type1 =3D qtest_resolve_machine_alias(var1, mtype); - g_autofree char *type2 =3D qtest_resolve_machine_alias(var2, mtype); - - g_assert(type1 && type2); - - if (g_str_equal(type1, type2)) { - /* either can be used */ - return g_strdup(type1); - } - - if (qtest_has_machine_with_env(var2, type1)) { - return g_strdup(type1); - } - - if (qtest_has_machine_with_env(var1, type2)) { - return g_strdup(type2); - } - - g_test_message("No common machine version for machine type '%s' betwee= n " - "binaries %s and %s", mtype, getenv(var1), getenv(var2)= ); - g_assert_not_reached(); -} - -char *resolve_machine_version(const char *alias, const char *var1, - const char *var2) -{ - const char *mname =3D g_getenv("QTEST_QEMU_MACHINE_TYPE"); - g_autofree char *machine_name =3D NULL; - - if (mname) { - const char *dash =3D strrchr(mname, '-'); - const char *dot =3D strrchr(mname, '.'); - - machine_name =3D g_strdup(mname); - - if (dash && dot) { - assert(qtest_has_machine(machine_name)); - return g_steal_pointer(&machine_name); - } - /* else: probably an alias, let it be resolved below */ - } else { - /* use the hardcoded alias */ - machine_name =3D g_strdup(alias); - } - - return find_common_machine_version(machine_name, var1, var2); -} - -typedef struct { - char *name; - void (*func)(void); -} MigrationTest; - -static void migration_test_destroy(gpointer data) -{ - MigrationTest *test =3D (MigrationTest *)data; - - g_free(test->name); - g_free(test); -} - -static void migration_test_wrapper(const void *data) -{ - MigrationTest *test =3D (MigrationTest *)data; - - g_test_message("Running /%s%s", qtest_get_arch(), test->name); - test->func(); -} - -void migration_test_add(const char *path, void (*fn)(void)) -{ - MigrationTest *test =3D g_new0(MigrationTest, 1); - - test->func =3D fn; - test->name =3D g_strdup(path); - - qtest_add_data_func_full(path, test, migration_test_wrapper, - migration_test_destroy); -} - -#ifdef O_DIRECT -/* - * Probe for O_DIRECT support on the filesystem. Since this is used - * for tests, be conservative, if anything fails, assume it's - * unsupported. - */ -bool probe_o_direct_support(const char *tmpfs) -{ - g_autofree char *filename =3D g_strdup_printf("%s/probe-o-direct", tmp= fs); - int fd, flags =3D O_CREAT | O_RDWR | O_TRUNC | O_DIRECT; - void *buf; - ssize_t ret, len; - uint64_t offset; - - fd =3D open(filename, flags, 0660); - if (fd < 0) { - unlink(filename); - return false; - } - - /* - * Using 1MB alignment as conservative choice to satisfy any - * plausible architecture default page size, and/or filesystem - * alignment restrictions. - */ - len =3D 0x100000; - offset =3D 0x100000; - - buf =3D qemu_try_memalign(len, len); - g_assert(buf); - - ret =3D pwrite(fd, buf, len, offset); - unlink(filename); - g_free(buf); - - if (ret < 0) { - return false; - } - - return true; -} -#endif - -/* - * Wait for a "MIGRATION" event. This is what Libvirt uses to track - * migration status changes. - */ -void migration_event_wait(QTestState *s, const char *target) -{ - QDict *response, *data; - const char *status; - bool found; - - do { - response =3D qtest_qmp_eventwait_ref(s, "MIGRATION"); - data =3D qdict_get_qdict(response, "data"); - g_assert(data); - status =3D qdict_get_str(data, "status"); - found =3D (strcmp(status, target) =3D=3D 0); - qobject_unref(response); - } while (!found); -} --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709299; cv=none; d=zohomail.com; s=zohoarc; b=BTQ0xHDUqKUd7rAHGcufbBlDg8oKjIbEzPDKXD6EpO6NnNPuemx35WCJGf3sOnuFvxzkiBAsU4tGON1LEkD4o8A5stEI9u16O64AdEN7KSzAMR733Yd2fkDglEjGJwui16NdiL2KH2ZQNtwMXejfHvcoXfrvFJ3NZfQ1sNu2ifQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709299; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NNWaOvix6j1L9tvmAvEpaUfwzb48imC5jSimySunfZg=; b=fGYAJnm5UYtc8NjMEZERShFuvdr5uZUoeBhZOzrCPOz1MCXBtOtjGYs6VT7WHBSfhzfFvNJc/d7piehfP3hw+g64tegN2TX6AF5MNiGHxrCK+8v5XgCU1st4v1n0K8lMoW/nicBktpyiWYV42rMFJmq6WRL5oKq+pzjyhCD8PXE= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709299650765.7898105890031; Mon, 23 Jun 2025 13:08:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRf-0005zA-NE; Mon, 23 Jun 2025 16:07:11 -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 1uTnRe-0005yE-4Z for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:10 -0400 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 1uTnRc-0003UY-H7 for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:09 -0400 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-683-7zBEaCfuO9WZiMXfxrjazQ-1; Mon, 23 Jun 2025 16:07:05 -0400 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6fb0e344e3eso40878026d6.1 for ; Mon, 23 Jun 2025 13:07:05 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709226; 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=NNWaOvix6j1L9tvmAvEpaUfwzb48imC5jSimySunfZg=; b=JRAGU6VrVZTBXRNmMouO0+DuPSrNRc8ssJjk51NcyDQYp0Hu5WZvswP81EAeM5CC/7qvHf z6HJx91YLZM5QVDrT4ojaU6OlE+wJHdLXRJznRpUXBRYbQq7ZU/s3w2PDyRzo8Vzn733VK pHHp5pcHJQfwH4gAknRsPY+9Uc1K/LI= X-MC-Unique: 7zBEaCfuO9WZiMXfxrjazQ-1 X-Mimecast-MFC-AGG-ID: 7zBEaCfuO9WZiMXfxrjazQ_1750709225 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709224; x=1751314024; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NNWaOvix6j1L9tvmAvEpaUfwzb48imC5jSimySunfZg=; b=oLIUthQ7CK0pVBZZfAqI2Bsw7ZNw1mo0qZKKt1IJoDmDSGLbiFOGE/aMDDf8jWe7J1 b+oP5lgPK+Xfqz7MUkO9zmuHYzf151v9Ly5uWtn/gMwmcWcFyDKClmJrz/vVwtZA4xhZ 0exYLysOAnDxq5/WX38oN5KQdvBHKXeSPJNCY/XC1Y87CxyvdgR2IS/bsxhg/f4Ob2/n yzS7KMzfSGnxOsBQuYl9mI/dMrCNVRrONmR2orItbWMHq96RdPwWncd09kaBCcBpYAZ0 vLUOp3yrOaNbTcs3jYtGowkmn5tS3q7WiyQf5ss2EjCNDIN/wGKXzEpmcqZLBNO1RJyV xaLw== X-Gm-Message-State: AOJu0YzV7bNTY573FrK/DUgeLH05JyHtPQkQFJ2/MDus3B4rcV0LHp8r FTvgoEJbZm5ZFANNNi2iN+nlDFnXjpyQ9qkcDurt8/JPK9q9EAb3RADw0RVSfsFDieSpl23f+YH zZf5Ej8/lkX21dqKlj0fXb46xzcwD1BGd2RF/N5s60551Dqbs9rjPsZXmyLbQ23GhxUS+8jc1Oi W8T4M6yBnbCd57GyVxUYfKl0ySoC8KWAEVWAYngQ== X-Gm-Gg: ASbGncvz4G/nBjKrPsPt0jDArM1vlccaGLKJeREr8+40s1aAJYSktuLIGl5cFG264PB GjEiKbxo1XOH3hNxq8qij7jGl1jFkfheL+546q+ax0ityNjbUnm6/qNofwjxl9UFsbJliIhXasr CvigBMburXuOWnp+hRvVrNSugIWXgCBvKEhe6vPwWyS+BWzsHL7oHZ0wVqdKP/mdaHdF8ko5Dma 1QWxkb+LgPgkSLu4iLG5FedXUMXltevlocY7P8SnU8MxY0d8DIvT3Xpnuf0JKkXqcSMdQn7HiDd WTJgwfy1YYc= X-Received: by 2002:a05:6214:e8c:b0:6e4:2f7f:d0bb with SMTP id 6a1803df08f44-6fd0a46a4c4mr233970206d6.4.1750709224553; Mon, 23 Jun 2025 13:07:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGv2pBGphPlBx8e2SR5g7JEOIHzLeXyVV49tws6HAcqgMeKMzI2v5EJ9TAQE8dWf1boLZzWNQ== X-Received: by 2002:a05:6214:e8c:b0:6e4:2f7f:d0bb with SMTP id 6a1803df08f44-6fd0a46a4c4mr233969556d6.4.1750709223838; Mon, 23 Jun 2025 13:07:03 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Juraj Marcin , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PULL 03/11] ui/vnc: Update display update interval when VM state changes to RUNNING Date: Mon, 23 Jun 2025 16:06:48 -0400 Message-ID: <20250623200656.1882674-4-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709301246116600 From: Juraj Marcin If a virtual machine is paused for an extended period time, for example, due to an incoming migration, there are also no changes on the screen. VNC in such case increases the display update interval by VNC_REFRESH_INTERVAL_INC (50 ms). The update interval can then grow up to VNC_REFRESH_INTERVAL_MAX (3000 ms). When the machine resumes, it can then take up to 3 seconds for the first display update. Furthermore, the update interval is then halved with each display update with changes on the screen. If there are moving elements on the screen, such as a video, this can be perceived as freezing and stuttering for few seconds before the movement is smooth again. This patch resolves this issue, by adding a listener to VM state changes and changing the update interval when the VM state changes to RUNNING. The update_displaychangelistener() function updates the internal timer, and the display is refreshed immediately if the timer is expired. Signed-off-by: Juraj Marcin Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Peter Xu Reviewed-by: Daniel P. Berrang=C3=A9 Link: https://lore.kernel.org/r/20250521151616.3951178-1-jmarcin@redhat.com Signed-off-by: Peter Xu --- ui/vnc.h | 2 ++ ui/vnc.c | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/ui/vnc.h b/ui/vnc.h index 02613aa63a..b3e07269bb 100644 --- a/ui/vnc.h +++ b/ui/vnc.h @@ -185,6 +185,8 @@ struct VncDisplay #endif =20 AudioState *audio_state; + + VMChangeStateEntry *vmstate_handler_entry; }; =20 typedef struct VncTight { diff --git a/ui/vnc.c b/ui/vnc.c index d095cd7da3..e9c30aad62 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -3385,6 +3385,16 @@ static const DisplayChangeListenerOps dcl_ops =3D { .dpy_cursor_define =3D vnc_dpy_cursor_define, }; =20 +static void vmstate_change_handler(void *opaque, bool running, RunState st= ate) +{ + VncDisplay *vd =3D opaque; + + if (state !=3D RUN_STATE_RUNNING) { + return; + } + update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); +} + void vnc_display_init(const char *id, Error **errp) { VncDisplay *vd; @@ -3421,6 +3431,8 @@ void vnc_display_init(const char *id, Error **errp) vd->dcl.ops =3D &dcl_ops; register_displaychangelistener(&vd->dcl); vd->kbd =3D qkbd_state_init(vd->dcl.con); + vd->vmstate_handler_entry =3D qemu_add_vm_change_state_handler( + &vmstate_change_handler, vd); } =20 =20 --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709365; cv=none; d=zohomail.com; s=zohoarc; b=ac7mqZErEra90F3YibVkmuhrvfStU6K63g/hF+0eVjG2AofGBN9PYzGvnGeXXj2E62Tyd53kQo/trI1eEjLFmbon1RNHe8AFaX3cXjmdY9WYnIJGLtYEaLgxH2hTN2n4PFOp7uI3FmENRrtoSuXXfbxpJjpN2mmQoW2AGyBCWnQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709365; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=gkEJjraRCepMoGaVxmMxS16KxsRSxOPvQtdhhkypr5k=; b=cfPY3PPBEABKwPuPguNwsQLg4QsAWrrqPEgQh3uS9PhVSsVIqy/TDsp/BH3+e25PXAdJ3m7WKn8ne7eOlwgwYPMCb3Xo3Ez3iN48YH+B+5Pd2f6HGddG2zwaWBPeEMtJkD7bAjF7pZJHKuUs6u32TdRNeqEiiJq1hbvmnYsgDts= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709365396443.82037659219657; Mon, 23 Jun 2025 13:09:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRh-0005zV-I0; Mon, 23 Jun 2025 16:07:13 -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 1uTnRf-0005zD-Rq for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:11 -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 1uTnRe-0003VJ-8z for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:11 -0400 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-180-93JwXokYP1-sRec5D-kNkw-1; Mon, 23 Jun 2025 16:07:07 -0400 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7ceb5b5140eso683163485a.2 for ; Mon, 23 Jun 2025 13:07:07 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709228; 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=gkEJjraRCepMoGaVxmMxS16KxsRSxOPvQtdhhkypr5k=; b=TXRVAtUhm7pcL0ATaICTwuAGxeGMQ/aNwT2Q78RgZSPsO/K/B+DGnytVryyy6bPMwLY8ge 54i7xp85atSRBrHp7+HyXzvjbvyCSROyItCUBDfMsB25QFNXy7bwulpT33tYJHm6CjtFna orVqeHKIvBcKmdCWm4lF2+Xh4uQZhAA= X-MC-Unique: 93JwXokYP1-sRec5D-kNkw-1 X-Mimecast-MFC-AGG-ID: 93JwXokYP1-sRec5D-kNkw_1750709227 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709226; x=1751314026; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gkEJjraRCepMoGaVxmMxS16KxsRSxOPvQtdhhkypr5k=; b=JH582AZdZrcHtr1gKIXyVuXbLZMZl7Valus3FinlJuVlOiWKPsWiZePjkVjDFMOgTx K1QiEA2v6ry/xT45zYXsC9/gQJxSIAXe7R6rRaX0JVCqVDsz3aHIzznNe9ebcBxqk8iu 81ueAmnu8xQtU/0/g1Nfn5yFI1rwljXWh7wPZbl7s0R+ek5ydveOU7Lsz2xKe38hb0po U1D7p9YlWdNaEyRZhv3uwDonmRCXQGSelkQCxm5UmabHHkUjex+6DDVD1L64vSvh9wug H6a6xlWamwOyleO+YqLBZrLgUrEtW0GI66mdcg47SCveAzfTQAVj3vWq32wvoR1dnnwc gCvg== X-Gm-Message-State: AOJu0YxMtS7aF9vdMxVqE1xa5ejAvngpjdYAvNtpAGqtK8JV2ffQfTwh lSnapwi5kVKkfV9NxyL6GMHmRODi2f95RRBXCTIz371FymqZvH3cxlTb2FZ5/AEpqfLLqvvJ03M hjFO7Ud6UmsdlsTCFhgILeithDNVFeMz0zZP4r4K6oZTWxBbx+fQjpUJujNQ2lRpANzcxEZTNyL 6nbU69rzFJbD/NNTeT/g7arTsDig5MuVrBw3rAfw== X-Gm-Gg: ASbGncsY4hplJf0BqXqn6mn3FjS5wp0xdiPlc6aDOZHdzFPKkxW8q2tCtoS0J7FshuL RWdx0cuDlxcdjsg+Iea9Y1GR9i0NwLPO1ganc2RZt8wYb8HPTNnZM025XnJQf8obWxkO5lOk57z gvHizn4adBfth6M6nWgPY+zgquEEzhRqI44pDZN2+OuHX8hRJhhAmut+O5PpciGbTcdRUkYVrTO nLKVWzsAnGAFbVoKBggdbAugS8uXx/bW1G9zpp8IbA8mHR5uS9WnGJpmM1OCfsy5KShX3pZT3d/ hQRb+uV9Jx8= X-Received: by 2002:a05:620a:171e:b0:7c5:49b7:237c with SMTP id af79cd13be357-7d3f992402cmr2069795185a.27.1750709226528; Mon, 23 Jun 2025 13:07:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGTfKa1IUadawlTW9uDTrfarIxoxeLKBJksU2SOYhPutnFjX7X0hdq7KO189bVVUHJm3J65ew== X-Received: by 2002:a05:620a:171e:b0:7c5:49b7:237c with SMTP id af79cd13be357-7d3f992402cmr2069790685a.27.1750709226028; Mon, 23 Jun 2025 13:07:06 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Jaehoon Kim , "Jason J. Herne" , Steve Sistare Subject: [PULL 04/11] migration: Support fd-based socket address in cpr_transfer_input Date: Mon, 23 Jun 2025 16:06:49 -0400 Message-ID: <20250623200656.1882674-5-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709365993116600 Content-Type: text/plain; charset="utf-8" From: Jaehoon Kim Extend cpr_transfer_input to handle SOCKET_ADDRESS_TYPE_FD alongside SOCKET_ADDRESS_TYPE_UNIX. This change supports the use of pre-listened socket file descriptors for cpr migration channels. This change is particularly useful in qtest environments, where the socket may be created externally and passed via fd. Reviewed-by: Jason J. Herne Reviewed-by: Steve Sistare Signed-off-by: Jaehoon Kim Link: https://lore.kernel.org/r/20250611205610.147008-3-jhkim@linux.ibm.com Signed-off-by: Peter Xu --- migration/cpr-transfer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/migration/cpr-transfer.c b/migration/cpr-transfer.c index e1f140359c..00371d17c3 100644 --- a/migration/cpr-transfer.c +++ b/migration/cpr-transfer.c @@ -46,7 +46,8 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, E= rror **errp) MigrationAddress *addr =3D channel->addr; =20 if (addr->transport =3D=3D MIGRATION_ADDRESS_TYPE_SOCKET && - addr->u.socket.type =3D=3D SOCKET_ADDRESS_TYPE_UNIX) { + (addr->u.socket.type =3D=3D SOCKET_ADDRESS_TYPE_UNIX || + addr->u.socket.type =3D=3D SOCKET_ADDRESS_TYPE_FD)) { =20 g_autoptr(QIOChannelSocket) sioc =3D NULL; SocketAddress *saddr =3D &addr->u.socket; @@ -60,7 +61,9 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, E= rror **errp) =20 sioc =3D qio_net_listener_wait_client(listener); ioc =3D QIO_CHANNEL(sioc); - trace_cpr_transfer_input(addr->u.socket.u.q_unix.path); + trace_cpr_transfer_input( + addr->u.socket.type =3D=3D SOCKET_ADDRESS_TYPE_UNIX ? + addr->u.socket.u.q_unix.path : addr->u.socket.u.fd.str); qio_channel_set_name(ioc, "cpr-in"); return qemu_file_new_input(ioc); =20 --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709285; cv=none; d=zohomail.com; s=zohoarc; b=NlKJvBYUJXSbTwjigrXUrjqhSNWxzSfpO/HUwrjoxidd2+LAk+F6y3ZfhD1zqlF3VW+qF3VMPeqBJ6zI/ostuNgkRnVPCr6fMRK3t91lJa0yGZ9cfiHj8ZU1b3Pv+wH7nALZXiax37Hgv8YQBemI4G5rRryYhZJglJIEUFxX1uI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709285; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=S0hAo7uObo/yCTF5mbeDUJEe0VzTLQTBlXgVDJht0Fk=; b=b3CTQEj2bo8IMwbfFvD7eANVOUa2gPdFVSAXb/o08rJOk+qHCfAyuw/VqmWiNtXm95j1N2by9qd8JsKpIh+ge8u0n+hhW/6ADlJr6FuhHYM8ZqhkdMBvpAVlmAO5Vv75yudxe9PE8AafC+TKVqUz9YJbQVpT6+IrADn1gdOKG+o= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709285680688.8478070720499; Mon, 23 Jun 2025 13:08:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRk-00061L-FV; Mon, 23 Jun 2025 16:07:16 -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 1uTnRi-0005zm-7X for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:14 -0400 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 1uTnRg-0003WE-CY for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:13 -0400 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-611-YydpmtMEOmiDWv412om_mg-1; Mon, 23 Jun 2025 16:07:09 -0400 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6fb3466271fso73772056d6.0 for ; Mon, 23 Jun 2025 13:07:08 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709231; 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=S0hAo7uObo/yCTF5mbeDUJEe0VzTLQTBlXgVDJht0Fk=; b=B6Zyxuz8bgqRpxxbx2R0BhtvC9eQEV2B6BUAjfoBP+zDZIrAc1K+Qr7zdeyDGihYOpxOuW 7HWKMW8hVqsiU0nyVnL6TvGbPofN7HlZ0IejS6ZCYgnUzgUt/Uq4bZak/PiKrjV1NwDLjv SbKpEwt+8t/SIgN0KFVezgUf5uwOnYc= X-MC-Unique: YydpmtMEOmiDWv412om_mg-1 X-Mimecast-MFC-AGG-ID: YydpmtMEOmiDWv412om_mg_1750709228 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709228; x=1751314028; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=S0hAo7uObo/yCTF5mbeDUJEe0VzTLQTBlXgVDJht0Fk=; b=gZuEGQ8L8NwqT2DCwgfrZE5HqU2elSkhhchc0d7QMgRoz25a+j4TquPCWYeFhaW46i xu4g2PX0JJDD83I4/D4sYfOuIKJGzRe94F7apD3R9/Q3Fm5PEeZ8VOQR1BLH4H4K2aUY mpWLm+QOjjpy3iTD+ZONOcw04g3knn5RF3fs8KJSP7Gj5w9iYVDEpXCp10jG2uDb0wol 38Jtbt35ms1w2Dxyk21WGhw8rGKUWZuG764P2EeoKZbRvnRvQnmJzPmupqXTRQeTtt9d FaqhZuGrE2W//Ifr9z0ZebZ/3l28Jo1/BVVOV8uWpwu5dzrfSxjmCa9lS0Qbz+ydBvDz BhXA== X-Gm-Message-State: AOJu0Ywc7yvrimUzd1EfdmB7ZC9xGdjla62sSxywTdsrsrLFp+A5qmvo 4oKH7lDbj9luiRMw+E88f9lMD7e+cGhdnBQe4vF7coLQnrc2FFZZ71WikEN3g0vCgbIwWGMzAKU bHTRwqGwA5Zd0yyQh4uMf0dEcMpXlx5WApUlM9aN6fH9XQCZjsORXhKBmeGIwcE4R0Bet5GrSWJ qyK43myc9hHoF4eAQ6P626RfXosAb3O5pcKLY1Ig== X-Gm-Gg: ASbGncvyHF3HoTK48yjXPYxDUNdGKS5ofR/GpPKkw2x5YuIGODt96hD1JaArdGxYxKr NigsDfRDl5YFhi+eZVfksvkwo1G2ZbfgBApzyW1paNb8plE/ZTsUSj2zOoSVDAmNTAjdjO3BpsZ TFlck3m5XzAN2SzFNYTfbPyNDAK9Rwu+ZiKm35+gxg9hJzxyAotS1V8+P2deBYY+qhJBfUU6SqK oClVHymuEvwGsKhCO6wiI8ignkeHF/3ZX+Hb+eb+N5Zzjypa7xwNORbmSGEIrLCBTsa3+jO45PK zczn/dJFglk= X-Received: by 2002:a05:6214:2685:b0:6fa:fc4b:8ea7 with SMTP id 6a1803df08f44-6fd3889809emr12340226d6.21.1750709227592; Mon, 23 Jun 2025 13:07:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE8WGyXrgpUhkjz4fkFrFvMntq1BPtDJ/A5tMfg0LDz4blDM8G6kgLS4da+VP3COftIKWfueg== X-Received: by 2002:a05:6214:2685:b0:6fa:fc4b:8ea7 with SMTP id 6a1803df08f44-6fd3889809emr12339696d6.21.1750709227044; Mon, 23 Jun 2025 13:07:07 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Jaehoon Kim , "Jason J. Herne" , Steve Sistare Subject: [PULL 05/11] tests/migration: Setup pre-listened cpr.sock to remove race-condition. Date: Mon, 23 Jun 2025 16:06:50 -0400 Message-ID: <20250623200656.1882674-6-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709287294116600 Content-Type: text/plain; charset="utf-8" From: Jaehoon Kim When the source VM attempts to connect to the destination VM's Unix domain socket (cpr.sock) during a cpr-transfer test, race conditions can occur if the socket file isn't ready. This can lead to connection failures when running tests. This patch creates and listens on the socket in advance, and passes the pre-listened FD directly. This avoids timing issues and improves the reliability of CPR tests. Reviewed-by: Jason J. Herne Signed-off-by: Jaehoon Kim Reviewed-by: Steve Sistare Link: https://lore.kernel.org/r/20250611205610.147008-2-jhkim@linux.ibm.com [peterx: null-initialize opts_target, per Steve] Signed-off-by: Peter Xu --- tests/qtest/migration/cpr-tests.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/qtest/migration/cpr-tests.c b/tests/qtest/migration/cpr-= tests.c index 5536e14610..5e764a6787 100644 --- a/tests/qtest/migration/cpr-tests.c +++ b/tests/qtest/migration/cpr-tests.c @@ -60,13 +60,12 @@ static void test_mode_transfer_common(bool incoming_def= er) g_autofree char *cpr_path =3D g_strdup_printf("%s/cpr.sock", tmpfs); g_autofree char *mig_path =3D g_strdup_printf("%s/migsocket", tmpfs); g_autofree char *uri =3D g_strdup_printf("unix:%s", mig_path); + g_autofree char *opts_target =3D NULL; =20 const char *opts =3D "-machine aux-ram-share=3Don -nodefaults"; g_autofree const char *cpr_channel =3D g_strdup_printf( "cpr,addr.transport=3Dsocket,addr.type=3Dunix,addr.path=3D%s", cpr_path); - g_autofree char *opts_target =3D g_strdup_printf("-incoming %s %s", - cpr_channel, opts); =20 g_autofree char *connect_channels =3D g_strdup_printf( "[ { 'channel-type': 'main'," @@ -75,6 +74,17 @@ static void test_mode_transfer_common(bool incoming_defe= r) " 'path': '%s' } } ]", mig_path); =20 + /* + * Set up a UNIX domain socket for the CPR channel before + * launching the destination VM, to avoid timing issues + * during connection setup. + */ + int cpr_sockfd =3D qtest_socket_server(cpr_path); + g_assert(cpr_sockfd >=3D 0); + + opts_target =3D g_strdup_printf("-incoming cpr,addr.transport=3Dsocket= ," + "addr.type=3Dfd,addr.str=3D%d %s", + cpr_sockfd, opts); MigrateCommon args =3D { .start.opts_source =3D opts, .start.opts_target =3D opts_target, --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709314; cv=none; d=zohomail.com; s=zohoarc; b=B796IO0N+JcyKP0F33RGtHRdETgwJZcCpXRrzB1TgFNVLiaPtN8y1o7JW72hMFiCpnZ1yoVQDIUD+JXnOT9/PoVvGWlOEeITWB+TVQmb0sdQTxFBhGwrZ7OLSrrOhsAAmsbFPAeDVzYgLn4jyDpSkrmDepI+GC7ZXJEUkVCODhU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709314; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=JPAbS7kbUQ6XF3VFXAZzn4GVMjZi6hGPV20GzFZdjtU=; b=jfh7lSKlaYfyrdSPwBV0myqDeRPlw5LqNByErbWpMckqhPZvw//q4PCWsclApeyI/jTD2k5FH9l3NqZtai1iJirpEskS9hmuxlZFSi31xXVt2FwtYP3tXpEg5LnE4pA1H/tPyEqrHps2xDL8gKjZSj3T1tqk2W5aMUHatOxRr7g= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709314559450.1318437313146; Mon, 23 Jun 2025 13:08:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRj-000602-Gk; Mon, 23 Jun 2025 16:07:15 -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 1uTnRi-0005zn-7d for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:14 -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 1uTnRg-0003Wa-PW for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:13 -0400 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-198-g1OqxblbOemHS4cXVOnwpw-1; Mon, 23 Jun 2025 16:07:10 -0400 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6fb4eed1914so101735176d6.0 for ; Mon, 23 Jun 2025 13:07:09 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709232; 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=JPAbS7kbUQ6XF3VFXAZzn4GVMjZi6hGPV20GzFZdjtU=; b=eeI58a7I0UJOSOOuU3DaSGa0X9r5IXBQ9bPtqLBzZJfVaWckudUq+CtMBp3gT3TKOoAjPN f+7vaufxYsjQ+45Ud3PrXfM5yEwglEyD5O2Y/B5XN7P+gFAaPWKqrXGyzB8ChrBNNoAq5M H1v57v0YKjmMRetGIVqGYJXQXmT+syk= X-MC-Unique: g1OqxblbOemHS4cXVOnwpw-1 X-Mimecast-MFC-AGG-ID: g1OqxblbOemHS4cXVOnwpw_1750709229 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709228; x=1751314028; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JPAbS7kbUQ6XF3VFXAZzn4GVMjZi6hGPV20GzFZdjtU=; b=qDxaQc3qHa1pQCLJQaAvvolvdH0GLhmXEUfUEl6svJmT3z4mEB/lfr8gmkm8IhexuZ +6AVT/znLBuXda27GloR9wrsyT5R41N5JNRklPfiMU3Zhyt4Dn6YNMn2FWz6JQ6PLl2T 2ObFzv+VQiSZDyHLfhtmMIql/fjp/XvvMFWPkSBfYwUd1Zp8934R30nt+Ny3VdVdzSOc Jb2QGh7BMsjHdVKImg34heZk63ryZK2sPCV+GMaslVpAKgZuiKFy5tmYwT+4L6Ucm/lR OAVs8q/WXgYuDl7H7wSR7sqLST+6d6SKV7UvXNhjvq2RSfvFk6vg9H5iPsxORJ370ccN yGbA== X-Gm-Message-State: AOJu0YxzQwwTfUWkR7Gm+X4iyh1Qg/Zyckit2QrEmXVLU7oOgtPTgzFD V7n8eXcpWv2WPAaV4XkXDyx2gjajP0j0X0z8OKah/ynVSXG1uUAFK0/GK5E+VrpTgBHxNM2sut4 Ptjo+tCXOTILwwVOrr66nPMVZNdYyuHYjGYHJS+R/hQWDeIfvq8hf/wWQgNQViyHvyxgUvX7QFF pafUXiDAcVLX174tH1EcZmrPdzfmX5y4p+478UXg== X-Gm-Gg: ASbGncsCrBFbgEwjWooR3xGkWP8cN7zeLSikXkMeiXqWD7H62iGV4del1psOOTMUQVN Q6HTdbR9QhNntAqKdZKinYAewqb5x0ALawZ5dHv59pOclDs4U5nc5gRdpFg4Wzp6QPtXkfMC/q+ D/0GC9Km7XUBS+8O+IF9c5EEaGSHVqlqnMASq/apKs6Mi55sBrRpXRYUb6LwY5lbIcytMv4g+by 37HSFehXIKiizioldHAZ5dPKCrVfiUlMHfTUaLfv7rwD4ouJnz3REIJzn5tRx+NlRsvI/MJhnv+ BEb4ZZ3O+IE= X-Received: by 2002:a05:6214:4b11:b0:6f5:426c:1d1 with SMTP id 6a1803df08f44-6fd0a57a411mr228481396d6.34.1750709228583; Mon, 23 Jun 2025 13:07:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGdEHpYd1fnxyu2z6UdQOpxBhS4MkasrO9OTK306GaXLjJou91TYcK6DhaU5cTT5riboBG2HQ== X-Received: by 2002:a05:6214:4b11:b0:6f5:426c:1d1 with SMTP id 6a1803df08f44-6fd0a57a411mr228481036d6.34.1750709228131; Mon, 23 Jun 2025 13:07:08 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , "Chaney, Ben" Subject: [PULL 06/11] migration: Don't sync volatile memory after migration completes Date: Mon, 23 Jun 2025 16:06:51 -0400 Message-ID: <20250623200656.1882674-7-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709315360116600 Content-Type: text/plain; charset="utf-8" From: "Chaney, Ben" Syncing volatile memory provides no benefit, instead it can cause performance issues in some cases. Only sync memory that is marked as non-volatile after migration completes on destination. Signed-off-by: Ben Chaney Fixes: bd108a44bc29 (migration: ram: Switch to ram block writeback) Link: https://lore.kernel.org/r/1CC43F59-336F-4A12-84AD-DB89E0A17A95@akamai= .com Signed-off-by: Peter Xu --- migration/ram.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/migration/ram.c b/migration/ram.c index fd8d83b63c..024d8b3d03 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -3680,7 +3680,9 @@ static int ram_load_cleanup(void *opaque) RAMBlock *rb; =20 RAMBLOCK_FOREACH_NOT_IGNORED(rb) { - qemu_ram_block_writeback(rb); + if (memory_region_is_nonvolatile(rb->mr)) { + qemu_ram_block_writeback(rb); + } } =20 xbzrle_load_cleanup(); --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709359; cv=none; d=zohomail.com; s=zohoarc; b=Yx1MbBvPOap8mc4c4ig/fqRSCCqHgjVyxLLTc6rPvC2lY3MCAEGOXPqzTWTuqV81bvAZ7E7zHadCdTx2BqDC0Elk7iGBlU2dWfYR/baufb9nIgKaV+g5RBo8Jr9XBncatKrxAFPfhhLPTdrzkKBx9tH+Y7kkLQ75o6Zq65ay1qs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709359; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=sa8C9j/5zo9gPDkTFjiv5qUB1g2PT33c0z+8t2nsN4g=; b=b7krsMq04d7jCNHNRRBNbAYgL+K5Ge3wGLTE6UJaxwP/Q2DQccTNJ1cUdDn+IOkuqKk4dMB9qKmf7UAnUKR2ajpJvqjdBkdms73PC2Xw05K6YNCzvhb3KppmjUojtaZTfwsUAjJ2yffYs7xJtodF5YvOaQpfG8AhANbM8DoDD1Q= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709359211674.0895876905237; Mon, 23 Jun 2025 13:09:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRl-00061Z-9b; Mon, 23 Jun 2025 16:07:17 -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 1uTnRj-00060X-HE for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:15 -0400 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 1uTnRh-0003Wm-BO for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:15 -0400 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-477-L3EqyVKaOL-g2dBihDU1rw-1; Mon, 23 Jun 2025 16:07:11 -0400 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6facf4cf5e1so81558966d6.2 for ; Mon, 23 Jun 2025 13:07:11 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709232; 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=sa8C9j/5zo9gPDkTFjiv5qUB1g2PT33c0z+8t2nsN4g=; b=PDI+2/9KRtoPlNpE8THY1Fy3K715+x3YTT+RDHsB0I67VA1y4O/MQNN8HrUgrfoJDO8tSk djNqF0vCGxx7VO8od5G/arNM673AfEk4ykhSNdLFl0lSad6zaqOI0AfFUMRfVtmpahSc9G GHtQKiMN0oHLykLjBTWtRnJCVNYtICE= X-MC-Unique: L3EqyVKaOL-g2dBihDU1rw-1 X-Mimecast-MFC-AGG-ID: L3EqyVKaOL-g2dBihDU1rw_1750709230 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709230; x=1751314030; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sa8C9j/5zo9gPDkTFjiv5qUB1g2PT33c0z+8t2nsN4g=; b=PTAsBqqqNOqnNP6U2ZdXr91wH8WpJ6VjbEohWVQnY0al/JrmUhe+oaprhFVcT1pvxQ 0pifXlY8ksG+0SjzJ7zbPOV1UTOiJx0TYlbzu8qsmCbbNEH+iQiP8Ediknq/gsOXWCdK V4s5cuKjz1gDq75y9Gaj8b5wkK2UjqC85hq27R85Q71n6XiGT/70b2+UBJHeV2/EXEMP VJN7zwg+6V+w9n2lAR7TFRxVOfbWXL/Vtprg/EHkJ0fmMUiVwIGDH6W3LMPFwg9kN1zg sl8lYCYoDEEdVrbk2Z6tijgRvj4VsXeWhztm1jjVaBlw8DI8xE53zRDpd/2c9SxjshEp bbow== X-Gm-Message-State: AOJu0Yzm65Iyj9TbWibpqj99mUr+r2dhllTkRu1rgEvmhD0VmgKCLicz KbGzqt4xF4oc985Cwl6UWrXN6FpemJNXC1FR6DZysCMA1GBFdcLd6h8166ibNf2gc03KalHZUl2 H7xfQpb13h2+BLHTjdoo9GoqfMW3VXNxqkeKqy5J3AnMle8YN+YG1IcF2+TD2FInm/Xc8+6XO04 zg1whanlgk+6QEmtO+vLrTYQA4M+rR0KyQalU4BQ== X-Gm-Gg: ASbGnctgyRcb2WrZt5NQRZooCpTR7BUh5MNAuykHwo6H5Nq4q+aMvIegtGcekfSrV6B 0mZgJ3npuy1+Wnwb9k7xojwea9ZiFT9lrLfY3wojnjOd11ha3jfwb1Tne1Xm7dui7/IID64CWs6 4UhkRkoVJtFEW8EV8tkHE2Jpf53jAG1IaewOLR2paOaTdElfMgd49+3b4jYY7/JSyrDw1WhXIES AhOzag4GFAGCQ/1TQtyfgciTRA+zNH8aUZovI5dDM9Y8BW9mJXGtJsA8VGmAkZpACInMJ/9xlyY mOmhaXF6q0g= X-Received: by 2002:a05:6214:21ef:b0:6fa:c6e6:11f9 with SMTP id 6a1803df08f44-6fd0a4aa3d3mr200507506d6.11.1750709230057; Mon, 23 Jun 2025 13:07:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IElNXihsJbWDTH0G7V8Yo16+7C6fXN5aUPZlP9N15mlyikHefQXzEVUQ68REoCDlMfBCqrtYA== X-Received: by 2002:a05:6214:21ef:b0:6fa:c6e6:11f9 with SMTP id 6a1803df08f44-6fd0a4aa3d3mr200507076d6.11.1750709229624; Mon, 23 Jun 2025 13:07:09 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Chenyi Qiang , Alexey Kardashevskiy , Pankaj Gupta , Zhao Liu , Xiaoyao Li Subject: [PULL 07/11] memory: Export a helper to get intersection of a MemoryRegionSection with a given range Date: Mon, 23 Jun 2025 16:06:52 -0400 Message-ID: <20250623200656.1882674-8-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709360075116600 Content-Type: text/plain; charset="utf-8" From: Chenyi Qiang Rename the helper to memory_region_section_intersect_range() to make it more generic. Meanwhile, define the @end as Int128 and replace the related operations with Int128_* format since the helper is exported as a wider API. Suggested-by: Alexey Kardashevskiy Reviewed-by: Alexey Kardashevskiy Reviewed-by: Pankaj Gupta Reviewed-by: David Hildenbrand Reviewed-by: Zhao Liu Reviewed-by: Xiaoyao Li Signed-off-by: Chenyi Qiang Link: https://lore.kernel.org/r/20250612082747.51539-2-chenyi.qiang@intel.c= om Signed-off-by: Peter Xu --- include/system/memory.h | 30 ++++++++++++++++++++++++++++++ hw/virtio/virtio-mem.c | 32 +++++--------------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/include/system/memory.h b/include/system/memory.h index 0848690ea4..da97753e28 100644 --- a/include/system/memory.h +++ b/include/system/memory.h @@ -1211,6 +1211,36 @@ MemoryRegionSection *memory_region_section_new_copy(= MemoryRegionSection *s); */ void memory_region_section_free_copy(MemoryRegionSection *s); =20 +/** + * memory_region_section_intersect_range: Adjust the memory section to cov= er + * the intersection with the given range. + * + * @s: the #MemoryRegionSection to be adjusted + * @offset: the offset of the given range in the memory region + * @size: the size of the given range + * + * Returns false if the intersection is empty, otherwise returns true. + */ +static inline bool memory_region_section_intersect_range(MemoryRegionSecti= on *s, + uint64_t offset, + uint64_t size) +{ + uint64_t start =3D MAX(s->offset_within_region, offset); + Int128 end =3D int128_min(int128_add(int128_make64(s->offset_within_re= gion), + s->size), + int128_add(int128_make64(offset), + int128_make64(size))); + + if (int128_le(end, int128_make64(start))) { + return false; + } + + s->offset_within_address_space +=3D start - s->offset_within_region; + s->offset_within_region =3D start; + s->size =3D int128_sub(end, int128_make64(start)); + return true; +} + /** * memory_region_init: Initialize a memory region * diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index a3d1a676e7..b3c126ea1e 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -244,28 +244,6 @@ static int virtio_mem_for_each_plugged_range(VirtIOMEM= *vmem, void *arg, return ret; } =20 -/* - * Adjust the memory section to cover the intersection with the given rang= e. - * - * Returns false if the intersection is empty, otherwise returns true. - */ -static bool virtio_mem_intersect_memory_section(MemoryRegionSection *s, - uint64_t offset, uint64_t = size) -{ - uint64_t start =3D MAX(s->offset_within_region, offset); - uint64_t end =3D MIN(s->offset_within_region + int128_get64(s->size), - offset + size); - - if (end <=3D start) { - return false; - } - - s->offset_within_address_space +=3D start - s->offset_within_region; - s->offset_within_region =3D start; - s->size =3D int128_make64(end - start); - return true; -} - typedef int (*virtio_mem_section_cb)(MemoryRegionSection *s, void *arg); =20 static int virtio_mem_for_each_plugged_section(const VirtIOMEM *vmem, @@ -287,7 +265,7 @@ static int virtio_mem_for_each_plugged_section(const Vi= rtIOMEM *vmem, first_bit + 1) - 1; size =3D (last_bit - first_bit + 1) * vmem->block_size; =20 - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)) { break; } ret =3D cb(&tmp, arg); @@ -319,7 +297,7 @@ static int virtio_mem_for_each_unplugged_section(const = VirtIOMEM *vmem, first_bit + 1) - 1; size =3D (last_bit - first_bit + 1) * vmem->block_size; =20 - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)) { break; } ret =3D cb(&tmp, arg); @@ -355,7 +333,7 @@ static void virtio_mem_notify_unplug(VirtIOMEM *vmem, u= int64_t offset, QLIST_FOREACH(rdl, &vmem->rdl_list, next) { MemoryRegionSection tmp =3D *rdl->section; =20 - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)) { continue; } rdl->notify_discard(rdl, &tmp); @@ -371,7 +349,7 @@ static int virtio_mem_notify_plug(VirtIOMEM *vmem, uint= 64_t offset, QLIST_FOREACH(rdl, &vmem->rdl_list, next) { MemoryRegionSection tmp =3D *rdl->section; =20 - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)) { continue; } ret =3D rdl->notify_populate(rdl, &tmp); @@ -388,7 +366,7 @@ static int virtio_mem_notify_plug(VirtIOMEM *vmem, uint= 64_t offset, if (rdl2 =3D=3D rdl) { break; } - if (!virtio_mem_intersect_memory_section(&tmp, offset, size)) { + if (!memory_region_section_intersect_range(&tmp, offset, size)= ) { continue; } rdl2->notify_discard(rdl2, &tmp); --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709379; cv=none; d=zohomail.com; s=zohoarc; b=n31JqYgC7roDGPaEVK5feGDvVxnI6yWPgKLK96EPV9DU7IpmRdkJk/qytiHCg39PFO8Z/qx0RJzsGNk07hPhH9oUHSnDhZLitlZ1aZUIeObqSBe7Ph7WzbUB0il1iyxuMaqbac+9s4LXdhlT4Tk8eeqUPx5Dr6t2OMKwJDRz33o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709379; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=A/3L0JgRIIpPvnyIt/DuhPM++hXdQ8FkNjZdS8qxC18=; b=JT3oKnS+/k27XOiL14TDRlL8NnE6FZ6mBNHmMeBhhU6gBO2yEifeaVamvVBFt2HocDYFl/fghemZJHmavqeZJ8nhbceYla/2KmqSCcaT8muOZjF7+6MrtC2TGH/YFG/tQpNSmgBFBKiWGktRiDYCqS3si+Jgvd+lz25CgaCSLFQ= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709379504114.86672977559238; Mon, 23 Jun 2025 13:09:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRn-000626-7s; Mon, 23 Jun 2025 16:07:19 -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 1uTnRl-00061d-By for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:17 -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 1uTnRj-0003XN-0F for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:17 -0400 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-582-JjMvXiGEP2i2IrlYlQkQ1w-1; Mon, 23 Jun 2025 16:07:13 -0400 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6facf4cf5e1so81559436d6.2 for ; Mon, 23 Jun 2025 13:07:13 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709234; 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=A/3L0JgRIIpPvnyIt/DuhPM++hXdQ8FkNjZdS8qxC18=; b=cn9KHomSOlzJZUE4YTFXT0Z1iQXiO6MbET6rdLdj7BAJDBwxEXbbTHskq8YMsm/iAvEqLV 1sa7AU2eFsNGAg+lZXXZf12W2jZj2omR6jpiUtnCMEobuvCa+uzcqSL2MGWWS4CheGE6wp tOonT3+v91dFAAh1mdntLWwwCRrXmqc= X-MC-Unique: JjMvXiGEP2i2IrlYlQkQ1w-1 X-Mimecast-MFC-AGG-ID: JjMvXiGEP2i2IrlYlQkQ1w_1750709233 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709232; x=1751314032; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=A/3L0JgRIIpPvnyIt/DuhPM++hXdQ8FkNjZdS8qxC18=; b=W3OBhv3EVH0a7H9yBaC5QEZSBj+lfss6zoBIkCI3ZomqAKjJqm1HeRHW3S6UjqnhA+ kV6kIiN/3TANPtrlAQTLH7CBT8fin8wYTgdFd3RLa130Frnhxqr55nT990dBOc+1OKOZ XkkPWXjsQ48BMLomjO+cbtUMjdq6n0ZWpZoRIpWQNUc//IQCRWuUxAQA7t/iunl/3Pbi HnJFqKDDA8JYPpphoXGuRpxxF8/PGYoGP8BUFlJa8UYt94brTIuM2FmsZbouaTMS4mqu AI8Tb4sdfADTRuYgCOAvinzlmW0slxB0cVyS7VwigSXj5H3GSrThozB1th2VpCvpHjP2 L3TA== X-Gm-Message-State: AOJu0YyPl2sXZGEejyLIrjztvubAIyXrMdzjslEk4ECdT6lOPIl2WPH1 2Wv5e+9J56XlhiRNs0RflXDcUeNAzIOQYxXZm+9iC3FYr3xcP/xBBNmylUb2u5mpG9ynOtrGUqm ft9pkGcYZkDxs9XsFbR4MCY23/lU0meOcTTVA4BBuTRvUcypp2wJ3bQbc/d6Yc1dFVzmszqI6WS KtWIdH7KeTZdzuxE4oE7wqgpMUJuXJ7kGcRGGs5g== X-Gm-Gg: ASbGnct8PeZqmQcPVmhkSM8H4XXnuRLBvkvabIAvTsoVNtkJ/93kaRO2un7NfUC9ell 5cjzUT0zGHgJT4hKLRxXilTWzh7iKi8+oYuWJ4ORJeCVtqL/yKirSLnPWsej4GTRTwIXwIhK5G9 HMSqlhj0mD/lSHt3Hg4utWdHdnLwTz8wjB60Se67UoSeKLU34Npc5Ba21m8Of53F40dpE7eZHUQ nQlN2h2hX188znh8e0Z+DlIvXBMLkPcfjzvORSHEiV5/SZkvhkgqaCQDefEfiaG9StchFWdCwHt 29oTT5+n8ok= X-Received: by 2002:a05:6214:2427:b0:6fb:43d:65b7 with SMTP id 6a1803df08f44-6fd0a5c1776mr253078406d6.36.1750709231835; Mon, 23 Jun 2025 13:07:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHq5HPDjvueENdrq/gVsj5vd+l29loefqeDGS8NXsvUWup9Z1OAaICTKWFBM9aqx+t2oxjYlQ== X-Received: by 2002:a05:6214:2427:b0:6fb:43d:65b7 with SMTP id 6a1803df08f44-6fd0a5c1776mr253077936d6.36.1750709231376; Mon, 23 Jun 2025 13:07:11 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Chenyi Qiang , Pankaj Gupta , Alexey Kardashevskiy , Xiaoyao Li Subject: [PULL 08/11] memory: Change memory_region_set_ram_discard_manager() to return the result Date: Mon, 23 Jun 2025 16:06:53 -0400 Message-ID: <20250623200656.1882674-9-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709380588116600 Content-Type: text/plain; charset="utf-8" From: Chenyi Qiang Modify memory_region_set_ram_discard_manager() to return -EBUSY if a RamDiscardManager is already set in the MemoryRegion. The caller must handle this failure, such as having virtio-mem undo its actions and fail the realize() process. Opportunistically move the call earlier to avoid complex error handling. This change is beneficial when introducing a new RamDiscardManager instance besides virtio-mem. After ram_block_coordinated_discard_require(true) unlocks all RamDiscardManager instances, only one instance is allowed to be set for one MemoryRegion at present. Suggested-by: David Hildenbrand Reviewed-by: David Hildenbrand Reviewed-by: Pankaj Gupta Tested-by: Alexey Kardashevskiy Reviewed-by: Alexey Kardashevskiy Reviewed-by: Xiaoyao Li Signed-off-by: Chenyi Qiang Link: https://lore.kernel.org/r/20250612082747.51539-3-chenyi.qiang@intel.c= om Signed-off-by: Peter Xu --- include/system/memory.h | 6 +++--- hw/virtio/virtio-mem.c | 30 +++++++++++++++++------------- system/memory.c | 10 +++++++--- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/include/system/memory.h b/include/system/memory.h index da97753e28..60983d4977 100644 --- a/include/system/memory.h +++ b/include/system/memory.h @@ -2499,13 +2499,13 @@ static inline bool memory_region_has_ram_discard_ma= nager(MemoryRegion *mr) * * This function must not be called for a mapped #MemoryRegion, a #MemoryR= egion * that does not cover RAM, or a #MemoryRegion that already has a - * #RamDiscardManager assigned. + * #RamDiscardManager assigned. Return 0 if the rdm is set successfully. * * @mr: the #MemoryRegion * @rdm: #RamDiscardManager to set */ -void memory_region_set_ram_discard_manager(MemoryRegion *mr, - RamDiscardManager *rdm); +int memory_region_set_ram_discard_manager(MemoryRegion *mr, + RamDiscardManager *rdm); =20 /** * memory_region_find: translate an address/size relative to a diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index b3c126ea1e..2e491e8c44 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -1047,6 +1047,17 @@ static void virtio_mem_device_realize(DeviceState *d= ev, Error **errp) return; } =20 + /* + * Set ourselves as RamDiscardManager before the plug handler maps the + * memory region and exposes it via an address space. + */ + if (memory_region_set_ram_discard_manager(&vmem->memdev->mr, + RAM_DISCARD_MANAGER(vmem))) { + error_setg(errp, "Failed to set RamDiscardManager"); + 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 @@ -1061,6 +1072,7 @@ static void virtio_mem_device_realize(DeviceState *de= v, Error **errp) 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"= ); + memory_region_set_ram_discard_manager(&vmem->memdev->mr, NULL); ram_block_coordinated_discard_require(false); return; } @@ -1122,13 +1134,6 @@ static void virtio_mem_device_realize(DeviceState *d= ev, Error **errp) vmem->system_reset =3D VIRTIO_MEM_SYSTEM_RESET(obj); vmem->system_reset->vmem =3D vmem; qemu_register_resettable(obj); - - /* - * Set ourselves as RamDiscardManager before the plug handler maps the - * memory region and exposes it via an address space. - */ - memory_region_set_ram_discard_manager(&vmem->memdev->mr, - RAM_DISCARD_MANAGER(vmem)); } =20 static void virtio_mem_device_unrealize(DeviceState *dev) @@ -1136,12 +1141,6 @@ static void virtio_mem_device_unrealize(DeviceState = *dev) VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VirtIOMEM *vmem =3D VIRTIO_MEM(dev); =20 - /* - * The unplug handler unmapped the memory region, it cannot be - * found via an address space anymore. Unset ourselves. - */ - memory_region_set_ram_discard_manager(&vmem->memdev->mr, NULL); - qemu_unregister_resettable(OBJECT(vmem->system_reset)); object_unref(OBJECT(vmem->system_reset)); =20 @@ -1154,6 +1153,11 @@ static void virtio_mem_device_unrealize(DeviceState = *dev) virtio_del_queue(vdev, 0); virtio_cleanup(vdev); g_free(vmem->bitmap); + /* + * The unplug handler unmapped the memory region, it cannot be + * found via an address space anymore. Unset ourselves. + */ + memory_region_set_ram_discard_manager(&vmem->memdev->mr, NULL); ram_block_coordinated_discard_require(false); } =20 diff --git a/system/memory.c b/system/memory.c index 306e9ff9eb..d0c186e9f6 100644 --- a/system/memory.c +++ b/system/memory.c @@ -2106,12 +2106,16 @@ RamDiscardManager *memory_region_get_ram_discard_ma= nager(MemoryRegion *mr) return mr->rdm; } =20 -void memory_region_set_ram_discard_manager(MemoryRegion *mr, - RamDiscardManager *rdm) +int memory_region_set_ram_discard_manager(MemoryRegion *mr, + RamDiscardManager *rdm) { g_assert(memory_region_is_ram(mr)); - g_assert(!rdm || !mr->rdm); + if (mr->rdm && rdm) { + return -EBUSY; + } + mr->rdm =3D rdm; + return 0; } =20 uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager *= rdm, --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709379; cv=none; d=zohomail.com; s=zohoarc; b=VLK33X8feWKXX5wTLZVHxxdN2+2cU0Z61nOFQReDwgUPqvPir4qjNBIvIhxalLIiewF/ivm/cmwRsnmk2FauZbeSprgaT1h9FDbMJ3hsPL3iONfrHPHCTUbA8olur0jjwdnwNNqwodtyVTFAeqDzpK56GzNXFT+aonhhRgWbbP8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709379; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=lUT4tA+I1mPhrKrO2EQ1v652RlxqlKsmxz8JH5eS1nQ=; b=hsOaeWaAMwaXHoPJQydDKHU7dCCKbfa6ceP5fGC+46YFFcH3fT9OqCOmAm0SHiqTyveaj3vCthFX+TNL+o3dbmNcvzqtvu40hgW/lrJp8E53WQOgcAN/f7cB5E20gBR+ayB6pNqMIWO+09o2DUQK04v56W2xJuU94hkyjqKNuYs= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 175070937900240.430923681901845; Mon, 23 Jun 2025 13:09:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRo-00062L-65; Mon, 23 Jun 2025 16:07:20 -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 1uTnRm-00061v-6G for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:18 -0400 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 1uTnRk-0003XX-1H for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:17 -0400 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-685-RVoaFDRMNW-CXeo4f8bMfg-1; Mon, 23 Jun 2025 16:07:14 -0400 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6faca0f2677so119562226d6.1 for ; Mon, 23 Jun 2025 13:07:14 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709235; 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=lUT4tA+I1mPhrKrO2EQ1v652RlxqlKsmxz8JH5eS1nQ=; b=RyAzGsMZppPbWUxtRjRVzCvBYrt8QQ32yTeNxJKAV9bLrOnljRZiUoq9M+JT7hQzvn774k gIquxcZhiKfrNXDPzGQH6Mofbn9ICTVEkiBlo+0nt9eiN4E2LyolP7WXgpVeBZTRn9a+rB HNVLoSNvEbTMMpasRwpE0nm9ilk+rQA= X-MC-Unique: RVoaFDRMNW-CXeo4f8bMfg-1 X-Mimecast-MFC-AGG-ID: RVoaFDRMNW-CXeo4f8bMfg_1750709233 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709233; x=1751314033; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lUT4tA+I1mPhrKrO2EQ1v652RlxqlKsmxz8JH5eS1nQ=; b=oYN3eV4Sm8qIxsSD5KhYzucJenGUNH3kC3pWt97WZTRGF68lsUxFs9uWLHqOfu7sEx 5Ymz2ScCx4gZOOrEroWNNey6/D2FRbIvkEa/NOVIddLeZ3o1297HuMl7ZCVy9cDEKuod xzd3X9VbossDWfXH22zeEU/y0GePZUje44itWgNc0bdlmpGgaWxsk7eZRKMYpAMTnvjC vc416lk6zhX7GLTtKpSQEWShYlXV3oIwc3ngEFf4Cjr7nA4l6pXvcbrJPDkquzSN/Elg u7N4qq73K4LdWKu5lvAMWwhC/qOJyB4Zr3HG3XuwHyGJGxADgJH4k/7/gShPFiWtuhl7 A8Nw== X-Gm-Message-State: AOJu0YwuaJ37SW+PiyqTbYDDh+Qn048AxZafzvlboldEsCM3T7ZhPVk6 FSgpITgU5QMbQf2vD9FdU60864sjBMy1oTUBdArX77fcBsAHZ+lGvzJNYnNFXpEB2NifEvkyLF1 GTnnRlHcbv8s1voLNL9WClIYHLxbZsfCl2j3+ggao/vPpMijnD8JmEwIm7x+ocQcVnQW+O9bkRt My6ah/RurpKfc66OpfrmC0W5f+n81flxdRgXa1eg== X-Gm-Gg: ASbGnctucTUE79oJU4ZCMCKyvIELKsWQqwUGi9wNInnKNcSX8OesR4CyPB8F6t0yFn2 whQE5oIbDO0rkph4oXEcR588PolcvD3RkWNKvaAKubBj0zfdNCwoLL9LZsrohbhswv3yFJAKDFu dmTVl/qlEe3wkEHBcfO4LntWkN9vBeQPY7c8hUi+BXTJo/5wvWJibqvG0nPAylhUK5G51BZlvCt +P4zUvfgjkamSsNRIeVqDmU8urIcL21iRcfGi5gNkEG/afvkSMy2ds8Y4rVzypq7PSLpsMsIM+z TH5L+2qGcvQ= X-Received: by 2002:a05:6214:3215:b0:6e8:ddf6:d11e with SMTP id 6a1803df08f44-6fd0a51c380mr259710916d6.21.1750709233124; Mon, 23 Jun 2025 13:07:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF/Z83eO7wheNim+h2DDKT/NnR2LLQgNr7jwC1IqZ+qELfvGFTqRrrwtg+gWVtyUR0AuinwaQ== X-Received: by 2002:a05:6214:3215:b0:6e8:ddf6:d11e with SMTP id 6a1803df08f44-6fd0a51c380mr259710396d6.21.1750709232584; Mon, 23 Jun 2025 13:07:12 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Chenyi Qiang , Pankaj Gupta , Xiaoyao Li Subject: [PULL 09/11] memory: Unify the definiton of ReplayRamPopulate() and ReplayRamDiscard() Date: Mon, 23 Jun 2025 16:06:54 -0400 Message-ID: <20250623200656.1882674-10-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709380801116600 Content-Type: text/plain; charset="utf-8" From: Chenyi Qiang Update ReplayRamDiscard() function to return the result and unify the ReplayRamPopulate() and ReplayRamDiscard() to ReplayRamDiscardState() at the same time due to their identical definitions. This unification simplifies related structures, such as VirtIOMEMReplayData, which makes it cleaner. Reviewed-by: David Hildenbrand Reviewed-by: Pankaj Gupta Reviewed-by: Xiaoyao Li Signed-off-by: Chenyi Qiang Link: https://lore.kernel.org/r/20250612082747.51539-4-chenyi.qiang@intel.c= om Signed-off-by: Peter Xu --- include/system/memory.h | 74 +++++++++++++++++++++++++++++++---------- hw/virtio/virtio-mem.c | 21 ++++++------ migration/ram.c | 5 +-- system/memory.c | 12 +++---- 4 files changed, 76 insertions(+), 36 deletions(-) diff --git a/include/system/memory.h b/include/system/memory.h index 60983d4977..46248d4a52 100644 --- a/include/system/memory.h +++ b/include/system/memory.h @@ -576,8 +576,20 @@ static inline void ram_discard_listener_init(RamDiscar= dListener *rdl, rdl->double_discard_supported =3D double_discard_supported; } =20 -typedef int (*ReplayRamPopulate)(MemoryRegionSection *section, void *opaqu= e); -typedef void (*ReplayRamDiscard)(MemoryRegionSection *section, void *opaqu= e); +/** + * typedef ReplayRamDiscardState: + * + * The callback handler for #RamDiscardManagerClass.replay_populated/ + * #RamDiscardManagerClass.replay_discarded to invoke on populated/discard= ed + * parts. + * + * @section: the #MemoryRegionSection of populated/discarded part + * @opaque: pointer to forward to the callback + * + * Returns 0 on success, or a negative error if failed. + */ +typedef int (*ReplayRamDiscardState)(MemoryRegionSection *section, + void *opaque); =20 /* * RamDiscardManagerClass: @@ -651,36 +663,38 @@ struct RamDiscardManagerClass { /** * @replay_populated: * - * Call the #ReplayRamPopulate callback for all populated parts within= the - * #MemoryRegionSection via the #RamDiscardManager. + * Call the #ReplayRamDiscardState callback for all populated parts wi= thin + * the #MemoryRegionSection via the #RamDiscardManager. * * In case any call fails, no further calls are made. * * @rdm: the #RamDiscardManager * @section: the #MemoryRegionSection - * @replay_fn: the #ReplayRamPopulate callback + * @replay_fn: the #ReplayRamDiscardState callback * @opaque: pointer to forward to the callback * * Returns 0 on success, or a negative error if any notification faile= d. */ int (*replay_populated)(const RamDiscardManager *rdm, MemoryRegionSection *section, - ReplayRamPopulate replay_fn, void *opaque); + ReplayRamDiscardState replay_fn, void *opaque); =20 /** * @replay_discarded: * - * Call the #ReplayRamDiscard callback for all discarded parts within = the - * #MemoryRegionSection via the #RamDiscardManager. + * Call the #ReplayRamDiscardState callback for all discarded parts wi= thin + * the #MemoryRegionSection via the #RamDiscardManager. * * @rdm: the #RamDiscardManager * @section: the #MemoryRegionSection - * @replay_fn: the #ReplayRamDiscard callback + * @replay_fn: the #ReplayRamDiscardState callback * @opaque: pointer to forward to the callback + * + * Returns 0 on success, or a negative error if any notification faile= d. */ - void (*replay_discarded)(const RamDiscardManager *rdm, - MemoryRegionSection *section, - ReplayRamDiscard replay_fn, void *opaque); + int (*replay_discarded)(const RamDiscardManager *rdm, + MemoryRegionSection *section, + ReplayRamDiscardState replay_fn, void *opaque); =20 /** * @register_listener: @@ -721,15 +735,41 @@ uint64_t ram_discard_manager_get_min_granularity(cons= t RamDiscardManager *rdm, bool ram_discard_manager_is_populated(const RamDiscardManager *rdm, const MemoryRegionSection *section); =20 +/** + * ram_discard_manager_replay_populated: + * + * A wrapper to call the #RamDiscardManagerClass.replay_populated callback + * of the #RamDiscardManager. + * + * @rdm: the #RamDiscardManager + * @section: the #MemoryRegionSection + * @replay_fn: the #ReplayRamDiscardState callback + * @opaque: pointer to forward to the callback + * + * Returns 0 on success, or a negative error if any notification failed. + */ int ram_discard_manager_replay_populated(const RamDiscardManager *rdm, MemoryRegionSection *section, - ReplayRamPopulate replay_fn, + ReplayRamDiscardState replay_fn, void *opaque); =20 -void ram_discard_manager_replay_discarded(const RamDiscardManager *rdm, - MemoryRegionSection *section, - ReplayRamDiscard replay_fn, - void *opaque); +/** + * ram_discard_manager_replay_discarded: + * + * A wrapper to call the #RamDiscardManagerClass.replay_discarded callback + * of the #RamDiscardManager. + * + * @rdm: the #RamDiscardManager + * @section: the #MemoryRegionSection + * @replay_fn: the #ReplayRamDiscardState callback + * @opaque: pointer to forward to the callback + * + * Returns 0 on success, or a negative error if any notification failed. + */ +int ram_discard_manager_replay_discarded(const RamDiscardManager *rdm, + MemoryRegionSection *section, + ReplayRamDiscardState replay_fn, + void *opaque); =20 void ram_discard_manager_register_listener(RamDiscardManager *rdm, RamDiscardListener *rdl, diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 2e491e8c44..c46f6f9c3e 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -1732,7 +1732,7 @@ static bool virtio_mem_rdm_is_populated(const RamDisc= ardManager *rdm, } =20 struct VirtIOMEMReplayData { - void *fn; + ReplayRamDiscardState fn; void *opaque; }; =20 @@ -1740,12 +1740,12 @@ static int virtio_mem_rdm_replay_populated_cb(Memor= yRegionSection *s, void *arg) { struct VirtIOMEMReplayData *data =3D arg; =20 - return ((ReplayRamPopulate)data->fn)(s, data->opaque); + return data->fn(s, data->opaque); } =20 static int virtio_mem_rdm_replay_populated(const RamDiscardManager *rdm, MemoryRegionSection *s, - ReplayRamPopulate replay_fn, + ReplayRamDiscardState replay_fn, void *opaque) { const VirtIOMEM *vmem =3D VIRTIO_MEM(rdm); @@ -1764,14 +1764,13 @@ static int virtio_mem_rdm_replay_discarded_cb(Memor= yRegionSection *s, { struct VirtIOMEMReplayData *data =3D arg; =20 - ((ReplayRamDiscard)data->fn)(s, data->opaque); - return 0; + return data->fn(s, data->opaque); } =20 -static void virtio_mem_rdm_replay_discarded(const RamDiscardManager *rdm, - MemoryRegionSection *s, - ReplayRamDiscard replay_fn, - void *opaque) +static int virtio_mem_rdm_replay_discarded(const RamDiscardManager *rdm, + MemoryRegionSection *s, + ReplayRamDiscardState replay_fn, + void *opaque) { const VirtIOMEM *vmem =3D VIRTIO_MEM(rdm); struct VirtIOMEMReplayData data =3D { @@ -1780,8 +1779,8 @@ static void virtio_mem_rdm_replay_discarded(const Ram= DiscardManager *rdm, }; =20 g_assert(s->mr =3D=3D &vmem->memdev->mr); - virtio_mem_for_each_unplugged_section(vmem, s, &data, - virtio_mem_rdm_replay_discarded_= cb); + return virtio_mem_for_each_unplugged_section(vmem, s, &data, + virtio_mem_rdm_replay_dis= carded_cb); } =20 static void virtio_mem_rdm_register_listener(RamDiscardManager *rdm, diff --git a/migration/ram.c b/migration/ram.c index 024d8b3d03..2140785a05 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -856,8 +856,8 @@ static inline bool migration_bitmap_clear_dirty(RAMStat= e *rs, return ret; } =20 -static void dirty_bitmap_clear_section(MemoryRegionSection *section, - void *opaque) +static int dirty_bitmap_clear_section(MemoryRegionSection *section, + void *opaque) { const hwaddr offset =3D section->offset_within_region; const hwaddr size =3D int128_get64(section->size); @@ -876,6 +876,7 @@ static void dirty_bitmap_clear_section(MemoryRegionSect= ion *section, } *cleared_bits +=3D bitmap_count_one_with_offset(rb->bmap, start, npage= s); bitmap_clear(rb->bmap, start, npages); + return 0; } =20 /* diff --git a/system/memory.c b/system/memory.c index d0c186e9f6..76b44b8220 100644 --- a/system/memory.c +++ b/system/memory.c @@ -2138,7 +2138,7 @@ bool ram_discard_manager_is_populated(const RamDiscar= dManager *rdm, =20 int ram_discard_manager_replay_populated(const RamDiscardManager *rdm, MemoryRegionSection *section, - ReplayRamPopulate replay_fn, + ReplayRamDiscardState replay_fn, void *opaque) { RamDiscardManagerClass *rdmc =3D RAM_DISCARD_MANAGER_GET_CLASS(rdm); @@ -2147,15 +2147,15 @@ int ram_discard_manager_replay_populated(const RamD= iscardManager *rdm, return rdmc->replay_populated(rdm, section, replay_fn, opaque); } =20 -void ram_discard_manager_replay_discarded(const RamDiscardManager *rdm, - MemoryRegionSection *section, - ReplayRamDiscard replay_fn, - void *opaque) +int ram_discard_manager_replay_discarded(const RamDiscardManager *rdm, + MemoryRegionSection *section, + ReplayRamDiscardState replay_fn, + void *opaque) { RamDiscardManagerClass *rdmc =3D RAM_DISCARD_MANAGER_GET_CLASS(rdm); =20 g_assert(rdmc->replay_discarded); - rdmc->replay_discarded(rdm, section, replay_fn, opaque); + return rdmc->replay_discarded(rdm, section, replay_fn, opaque); } =20 void ram_discard_manager_register_listener(RamDiscardManager *rdm, --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709321; cv=none; d=zohomail.com; s=zohoarc; b=OeOM7WYiR6t3oMwXPZhGeucGpgX6j54ZsbVAvlACz0cJBbPwS8/fRf/lyXCjSxHL1Bgl6sou6Ph4KBxNalTa81AAUQDcW3bEOtMYcheVJyC/HPyLNoFEzZJZV1JaLW8NL6K6My7E1nFmyiIxiiXlp6ykFGIv2pFu8Gl4kGpTs+s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709321; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=5TZkEPcKUD0IF2Ws/fLavANEe8cHL59ROo6m22DohYw=; b=DK4vQnNRXPCegg6bQFU2qc/7pd64UU5vlyDRNP5nv/f34S9+95z1JylAXUehsaxaJtxpuqpWWCgpTpJOSWb2U1wVQNiLdWFnfwDF9qdISvMRvwOTrYHCGki21F2s1+XUc59r2dK/Ji3sSvG+2XKiq2HSgqrHTH2DwrL4PQjmTWA= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709321332507.2078428703952; Mon, 23 Jun 2025 13:08:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRt-00063r-L4; Mon, 23 Jun 2025 16:07:25 -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 1uTnRq-00063D-T3 for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:22 -0400 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 1uTnRo-0003YV-9h for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:22 -0400 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-97-PAze08d5MLyDQ2nET_RuoQ-1; Mon, 23 Jun 2025 16:07:15 -0400 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6faeebe9c5bso39565096d6.2 for ; Mon, 23 Jun 2025 13:07:15 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709238; 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=5TZkEPcKUD0IF2Ws/fLavANEe8cHL59ROo6m22DohYw=; b=RbkUstfi3Fo/A2Mo8LILuLDj48x+nQKBDIeyQc8MtBlCvbvGJ5hDPECAI0FM4xsj9O1N/d VRTET0NRFM7gO16nnfpuryG5ZlRVBzuaM8de8SoxBl8TfZ+snisNiOP79A2H71DAxYRGdb smQR5cyJp+xiapFAXIcRs2ozDbi4Lqk= X-MC-Unique: PAze08d5MLyDQ2nET_RuoQ-1 X-Mimecast-MFC-AGG-ID: PAze08d5MLyDQ2nET_RuoQ_1750709235 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709235; x=1751314035; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5TZkEPcKUD0IF2Ws/fLavANEe8cHL59ROo6m22DohYw=; b=aHfhajgFmS0Ai/rutBBCGuWpEZ4joiDMGkhIbeMKNS7+1/S/rvsfpKeyBTCXTd9snX iuCdtVvwy8ELt4fYCDDXQDdVk6hRBZqrhaP1zAl2UTB3iNA1VKo9wq1/G9g3JUJRRePD 7V07joAzPbrRopIpKqeEZvQPkmcr2eTGw+/qcApt5JtzLQjomoTltiyw7KNFN1TR8bBX yXOa3G7qgI0hrpHkD1SjuguPDa9ZOPewwusOoE1Cfm0hw6LDtBM4qoq4exiv6q3M/asL /1qsg2xh3AVIhloR1Cc6Am4ddG7ht0r4GObrz9xkUxRu6ACuZpSS3NwI8hqb8xZVs3YE +bWg== X-Gm-Message-State: AOJu0Yx3bl4rpMBQqZsevNs/7uhjE5x2VJEKIxijbVrORVJXwoTQmdHK Q2yh0OD9g+ZFboFV81umBe62edH2TwMq0dj8oCPVemcqCCqxO36xdj3LLuY2rCb/U4+NrAnzNaK je7pXxHKkm6cN2N/5I8sUNj1hlbtshzMZXOjP2QCmQNoBY7nEtTF8Efki/mwzFAd4UnOskjDOXa gF5vqUIANG5NpwvNw0BpiLI62n7xIN5kuzj6XX7g== X-Gm-Gg: ASbGnctMagorayO6BTaqTXS3IXsBVxOh3NYs8VyBPd2h1ioYJinqsREJtpcGGc4TJSp 1Y6R8iuzt1QGOlYi7LTGv2c5z6atDx+UIZg7YFjznefwqvakHBEJodqaOIePhUyl8eqEx+wC99l SF333VINpDStvO48RBGp0DAupoK8JdtISq/3L7pCQhFsGy+/R8j3rCSfvLLVEzTy8DzYbPon9dC SK0DRw7BSBqIsR4yhktUqvL01mqfvZfnAeD+GEhVWtICq28lERa4FEUxsl5pA/NXDOy9TrfVumD vRr4KhlfZdw= X-Received: by 2002:a05:6214:e4a:b0:6fa:ccb6:6036 with SMTP id 6a1803df08f44-6fd0a591efbmr225451886d6.35.1750709234574; Mon, 23 Jun 2025 13:07:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3TN3WkHGTyhYJHWfv4+tN4GuJlRo6W5GEPVNknNlrNa2p3zvRp/ov9wjd3lJAl1/zV8HjTQ== X-Received: by 2002:a05:6214:e4a:b0:6fa:ccb6:6036 with SMTP id 6a1803df08f44-6fd0a591efbmr225450936d6.35.1750709233687; Mon, 23 Jun 2025 13:07:13 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Chenyi Qiang , Alexey Kardashevskiy , Pankaj Gupta Subject: [PULL 10/11] ram-block-attributes: Introduce RamBlockAttributes to manage RAMBlock with guest_memfd Date: Mon, 23 Jun 2025 16:06:55 -0400 Message-ID: <20250623200656.1882674-11-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709323768116600 Content-Type: text/plain; charset="utf-8" From: Chenyi Qiang Commit 852f0048f3 ("RAMBlock: make guest_memfd require uncoordinated discard") highlighted that subsystems like VFIO may disable RAM block discard. However, guest_memfd relies on discard operations for page conversion between private and shared memory, potentially leading to the stale IOMMU mapping issue when assigning hardware devices to confidential VMs via shared memory. To address this and allow shared device assignement, it is crucial to ensure the VFIO system refreshes its IOMMU mappings. RamDiscardManager is an existing interface (used by virtio-mem) to adjust VFIO mappings in relation to VM page assignment. Effectively page conversion is similar to hot-removing a page in one mode and adding it back in the other. Therefore, similar actions are required for page conversion events. Introduce the RamDiscardManager to guest_memfd to facilitate this process. Since guest_memfd is not an object, it cannot directly implement the RamDiscardManager interface. Implementing it in HostMemoryBackend is not appropriate because guest_memfd is per RAMBlock, and some RAMBlocks have a memory backend while others do not. Notably, virtual BIOS RAMBlocks using memory_region_init_ram_guest_memfd() do not have a backend. To manage RAMBlocks with guest_memfd, define a new object named RamBlockAttributes to implement the RamDiscardManager interface. This object can store the guest_memfd information such as the bitmap for shared memory and the registered listeners for event notifications. A new state_change() helper function is provided to notify listeners, such as VFIO, allowing VFIO to do dynamically DMA map and unmap for the shared memory according to conversion events. Note that in the current context of RamDiscardManager for guest_memfd, the shared state is analogous to being populated, while the private state can be considered discarded for simplicity. In the future, it would be more complicated if considering more states like private/shared/discarded at the same time. In current implementation, memory state tracking is performed at the host page size granularity, as the minimum conversion size can be one page per request. Additionally, VFIO expected the DMA mapping for a specific IOVA to be mapped and unmapped with the same granularity. Confidential VMs may perform partial conversions, such as conversions on small regions within a larger one. To prevent such invalid cases and until support for DMA mapping cut operations is available, all operations are performed with 4K granularity. In addition, memory conversion failures cause QEMU to quit rather than resuming the guest or retrying the operation at present. It would be future work to add more error handling or rollback mechanisms once conversion failures are allowed. For example, in-place conversion of guest_memfd could retry the unmap operation during the conversion from shared to private. For now, keep the complex error handling out of the picture as it is not required. Tested-by: Alexey Kardashevskiy Reviewed-by: Alexey Kardashevskiy Reviewed-by: Pankaj Gupta Signed-off-by: Chenyi Qiang Link: https://lore.kernel.org/r/20250612082747.51539-5-chenyi.qiang@intel.c= om [peterx: squash fixup from Chenyi to fix builds] Signed-off-by: Peter Xu --- MAINTAINERS | 1 + include/system/ramblock.h | 21 ++ system/ram-block-attributes.c | 444 ++++++++++++++++++++++++++++++++++ system/meson.build | 1 + system/trace-events | 3 + 5 files changed, 470 insertions(+) create mode 100644 system/ram-block-attributes.c diff --git a/MAINTAINERS b/MAINTAINERS index 94c4076127..27f4fe3f25 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3175,6 +3175,7 @@ F: system/memory.c F: system/memory_mapping.c F: system/physmem.c F: system/memory-internal.h +F: system/ram-block-attributes.c F: scripts/coccinelle/memory-region-housekeeping.cocci =20 Memory devices diff --git a/include/system/ramblock.h b/include/system/ramblock.h index d8a116ba99..1bab9e2dac 100644 --- a/include/system/ramblock.h +++ b/include/system/ramblock.h @@ -22,6 +22,10 @@ #include "exec/cpu-common.h" #include "qemu/rcu.h" #include "exec/ramlist.h" +#include "system/hostmem.h" + +#define TYPE_RAM_BLOCK_ATTRIBUTES "ram-block-attributes" +OBJECT_DECLARE_SIMPLE_TYPE(RamBlockAttributes, RAM_BLOCK_ATTRIBUTES) =20 struct RAMBlock { struct rcu_head rcu; @@ -91,4 +95,21 @@ struct RAMBlock { ram_addr_t postcopy_length; }; =20 +struct RamBlockAttributes { + Object parent; + + RAMBlock *ram_block; + + /* 1-setting of the bitmap represents ram is populated (shared) */ + unsigned bitmap_size; + unsigned long *bitmap; + + QLIST_HEAD(, RamDiscardListener) rdl_list; +}; + +RamBlockAttributes *ram_block_attributes_create(RAMBlock *ram_block); +void ram_block_attributes_destroy(RamBlockAttributes *attr); +int ram_block_attributes_state_change(RamBlockAttributes *attr, uint64_t o= ffset, + uint64_t size, bool to_discard); + #endif diff --git a/system/ram-block-attributes.c b/system/ram-block-attributes.c new file mode 100644 index 0000000000..68e8a02703 --- /dev/null +++ b/system/ram-block-attributes.c @@ -0,0 +1,444 @@ +/* + * QEMU ram block attributes + * + * Copyright Intel + * + * Author: + * Chenyi Qiang + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "system/ramblock.h" +#include "trace.h" + +OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(RamBlockAttributes, + ram_block_attributes, + RAM_BLOCK_ATTRIBUTES, + OBJECT, + { TYPE_RAM_DISCARD_MANAGER }, + { }) + +static size_t +ram_block_attributes_get_block_size(const RamBlockAttributes *attr) +{ + /* + * Because page conversion could be manipulated in the size of at leas= t 4K + * or 4K aligned, Use the host page size as the granularity to track t= he + * memory attribute. + */ + g_assert(attr && attr->ram_block); + g_assert(attr->ram_block->page_size =3D=3D qemu_real_host_page_size()); + return attr->ram_block->page_size; +} + + +static bool +ram_block_attributes_rdm_is_populated(const RamDiscardManager *rdm, + const MemoryRegionSection *section) +{ + const RamBlockAttributes *attr =3D RAM_BLOCK_ATTRIBUTES(rdm); + const size_t block_size =3D ram_block_attributes_get_block_size(attr); + const uint64_t first_bit =3D section->offset_within_region / block_siz= e; + const uint64_t last_bit =3D + first_bit + int128_get64(section->size) / block_size - 1; + unsigned long first_discarded_bit; + + first_discarded_bit =3D find_next_zero_bit(attr->bitmap, last_bit + 1, + first_bit); + return first_discarded_bit > last_bit; +} + +typedef int (*ram_block_attributes_section_cb)(MemoryRegionSection *s, + void *arg); + +static int +ram_block_attributes_notify_populate_cb(MemoryRegionSection *section, + void *arg) +{ + RamDiscardListener *rdl =3D arg; + + return rdl->notify_populate(rdl, section); +} + +static int +ram_block_attributes_notify_discard_cb(MemoryRegionSection *section, + void *arg) +{ + RamDiscardListener *rdl =3D arg; + + rdl->notify_discard(rdl, section); + return 0; +} + +static int +ram_block_attributes_for_each_populated_section(const RamBlockAttributes *= attr, + MemoryRegionSection *secti= on, + void *arg, + ram_block_attributes_secti= on_cb cb) +{ + unsigned long first_bit, last_bit; + uint64_t offset, size; + const size_t block_size =3D ram_block_attributes_get_block_size(attr); + int ret =3D 0; + + first_bit =3D section->offset_within_region / block_size; + first_bit =3D find_next_bit(attr->bitmap, attr->bitmap_size, + first_bit); + + while (first_bit < attr->bitmap_size) { + MemoryRegionSection tmp =3D *section; + + offset =3D first_bit * block_size; + last_bit =3D find_next_zero_bit(attr->bitmap, attr->bitmap_size, + first_bit + 1) - 1; + size =3D (last_bit - first_bit + 1) * block_size; + + if (!memory_region_section_intersect_range(&tmp, offset, size)) { + break; + } + + ret =3D cb(&tmp, arg); + if (ret) { + error_report("%s: Failed to notify RAM discard listener: %s", + __func__, strerror(-ret)); + break; + } + + first_bit =3D find_next_bit(attr->bitmap, attr->bitmap_size, + last_bit + 2); + } + + return ret; +} + +static int +ram_block_attributes_for_each_discarded_section(const RamBlockAttributes *= attr, + MemoryRegionSection *secti= on, + void *arg, + ram_block_attributes_secti= on_cb cb) +{ + unsigned long first_bit, last_bit; + uint64_t offset, size; + const size_t block_size =3D ram_block_attributes_get_block_size(attr); + int ret =3D 0; + + first_bit =3D section->offset_within_region / block_size; + first_bit =3D find_next_zero_bit(attr->bitmap, attr->bitmap_size, + first_bit); + + while (first_bit < attr->bitmap_size) { + MemoryRegionSection tmp =3D *section; + + offset =3D first_bit * block_size; + last_bit =3D find_next_bit(attr->bitmap, attr->bitmap_size, + first_bit + 1) - 1; + size =3D (last_bit - first_bit + 1) * block_size; + + if (!memory_region_section_intersect_range(&tmp, offset, size)) { + break; + } + + ret =3D cb(&tmp, arg); + if (ret) { + error_report("%s: Failed to notify RAM discard listener: %s", + __func__, strerror(-ret)); + break; + } + + first_bit =3D find_next_zero_bit(attr->bitmap, + attr->bitmap_size, + last_bit + 2); + } + + return ret; +} + +static uint64_t +ram_block_attributes_rdm_get_min_granularity(const RamDiscardManager *rdm, + const MemoryRegion *mr) +{ + const RamBlockAttributes *attr =3D RAM_BLOCK_ATTRIBUTES(rdm); + + g_assert(mr =3D=3D attr->ram_block->mr); + return ram_block_attributes_get_block_size(attr); +} + +static void +ram_block_attributes_rdm_register_listener(RamDiscardManager *rdm, + RamDiscardListener *rdl, + MemoryRegionSection *section) +{ + RamBlockAttributes *attr =3D RAM_BLOCK_ATTRIBUTES(rdm); + int ret; + + g_assert(section->mr =3D=3D attr->ram_block->mr); + rdl->section =3D memory_region_section_new_copy(section); + + QLIST_INSERT_HEAD(&attr->rdl_list, rdl, next); + + ret =3D ram_block_attributes_for_each_populated_section(attr, section,= rdl, + ram_block_attributes_notify_populate_c= b); + if (ret) { + error_report("%s: Failed to register RAM discard listener: %s", + __func__, strerror(-ret)); + exit(1); + } +} + +static void +ram_block_attributes_rdm_unregister_listener(RamDiscardManager *rdm, + RamDiscardListener *rdl) +{ + RamBlockAttributes *attr =3D RAM_BLOCK_ATTRIBUTES(rdm); + int ret; + + g_assert(rdl->section); + g_assert(rdl->section->mr =3D=3D attr->ram_block->mr); + + if (rdl->double_discard_supported) { + rdl->notify_discard(rdl, rdl->section); + } else { + ret =3D ram_block_attributes_for_each_populated_section(attr, + rdl->section, rdl, ram_block_attributes_notify_discard_cb); + if (ret) { + error_report("%s: Failed to unregister RAM discard listener: %= s", + __func__, strerror(-ret)); + exit(1); + } + } + + memory_region_section_free_copy(rdl->section); + rdl->section =3D NULL; + QLIST_REMOVE(rdl, next); +} + +typedef struct RamBlockAttributesReplayData { + ReplayRamDiscardState fn; + void *opaque; +} RamBlockAttributesReplayData; + +static int ram_block_attributes_rdm_replay_cb(MemoryRegionSection *section, + void *arg) +{ + RamBlockAttributesReplayData *data =3D arg; + + return data->fn(section, data->opaque); +} + +static int +ram_block_attributes_rdm_replay_populated(const RamDiscardManager *rdm, + MemoryRegionSection *section, + ReplayRamDiscardState replay_fn, + void *opaque) +{ + RamBlockAttributes *attr =3D RAM_BLOCK_ATTRIBUTES(rdm); + RamBlockAttributesReplayData data =3D { .fn =3D replay_fn, .opaque =3D= opaque }; + + g_assert(section->mr =3D=3D attr->ram_block->mr); + return ram_block_attributes_for_each_populated_section(attr, section, = &data, + ram_block_attributes_rdm_repla= y_cb); +} + +static int +ram_block_attributes_rdm_replay_discarded(const RamDiscardManager *rdm, + MemoryRegionSection *section, + ReplayRamDiscardState replay_fn, + void *opaque) +{ + RamBlockAttributes *attr =3D RAM_BLOCK_ATTRIBUTES(rdm); + RamBlockAttributesReplayData data =3D { .fn =3D replay_fn, .opaque =3D= opaque }; + + g_assert(section->mr =3D=3D attr->ram_block->mr); + return ram_block_attributes_for_each_discarded_section(attr, section, = &data, + ram_block_attributes_rdm_repla= y_cb); +} + +static bool +ram_block_attributes_is_valid_range(RamBlockAttributes *attr, uint64_t off= set, + uint64_t size) +{ + MemoryRegion *mr =3D attr->ram_block->mr; + + g_assert(mr); + + uint64_t region_size =3D memory_region_size(mr); + const size_t block_size =3D ram_block_attributes_get_block_size(attr); + + if (!QEMU_IS_ALIGNED(offset, block_size) || + !QEMU_IS_ALIGNED(size, block_size)) { + return false; + } + if (offset + size <=3D offset) { + return false; + } + if (offset + size > region_size) { + return false; + } + return true; +} + +static void ram_block_attributes_notify_discard(RamBlockAttributes *attr, + uint64_t offset, + uint64_t size) +{ + RamDiscardListener *rdl; + + QLIST_FOREACH(rdl, &attr->rdl_list, next) { + MemoryRegionSection tmp =3D *rdl->section; + + if (!memory_region_section_intersect_range(&tmp, offset, size)) { + continue; + } + rdl->notify_discard(rdl, &tmp); + } +} + +static int +ram_block_attributes_notify_populate(RamBlockAttributes *attr, + uint64_t offset, uint64_t size) +{ + RamDiscardListener *rdl; + int ret =3D 0; + + QLIST_FOREACH(rdl, &attr->rdl_list, next) { + MemoryRegionSection tmp =3D *rdl->section; + + if (!memory_region_section_intersect_range(&tmp, offset, size)) { + continue; + } + ret =3D rdl->notify_populate(rdl, &tmp); + if (ret) { + break; + } + } + + return ret; +} + +int ram_block_attributes_state_change(RamBlockAttributes *attr, + uint64_t offset, uint64_t size, + bool to_discard) +{ + const size_t block_size =3D ram_block_attributes_get_block_size(attr); + const unsigned long first_bit =3D offset / block_size; + const unsigned long nbits =3D size / block_size; + const unsigned long last_bit =3D first_bit + nbits - 1; + const bool is_discarded =3D find_next_bit(attr->bitmap, attr->bitmap_s= ize, + first_bit) > last_bit; + const bool is_populated =3D find_next_zero_bit(attr->bitmap, + attr->bitmap_size, first_bit) > last_bit; + unsigned long bit; + int ret =3D 0; + + if (!ram_block_attributes_is_valid_range(attr, offset, size)) { + error_report("%s, invalid range: offset 0x%" PRIx64 ", size " + "0x%" PRIx64, __func__, offset, size); + return -EINVAL; + } + + trace_ram_block_attributes_state_change(offset, size, + is_discarded ? "discarded" : + is_populated ? "populated" : + "mixture", + to_discard ? "discarded" : + "populated"); + if (to_discard) { + if (is_discarded) { + /* Already private */ + } else if (is_populated) { + /* Completely shared */ + bitmap_clear(attr->bitmap, first_bit, nbits); + ram_block_attributes_notify_discard(attr, offset, size); + } else { + /* Unexpected mixture: process individual blocks */ + for (bit =3D first_bit; bit < first_bit + nbits; bit++) { + if (!test_bit(bit, attr->bitmap)) { + continue; + } + clear_bit(bit, attr->bitmap); + ram_block_attributes_notify_discard(attr, bit * block_size, + block_size); + } + } + } else { + if (is_populated) { + /* Already shared */ + } else if (is_discarded) { + /* Completely private */ + bitmap_set(attr->bitmap, first_bit, nbits); + ret =3D ram_block_attributes_notify_populate(attr, offset, siz= e); + } else { + /* Unexpected mixture: process individual blocks */ + for (bit =3D first_bit; bit < first_bit + nbits; bit++) { + if (test_bit(bit, attr->bitmap)) { + continue; + } + set_bit(bit, attr->bitmap); + ret =3D ram_block_attributes_notify_populate(attr, + bit * block_siz= e, + block_size); + if (ret) { + break; + } + } + } + } + + return ret; +} + +RamBlockAttributes *ram_block_attributes_create(RAMBlock *ram_block) +{ + const int block_size =3D qemu_real_host_page_size(); + RamBlockAttributes *attr; + MemoryRegion *mr =3D ram_block->mr; + + attr =3D RAM_BLOCK_ATTRIBUTES(object_new(TYPE_RAM_BLOCK_ATTRIBUTES)); + + attr->ram_block =3D ram_block; + if (memory_region_set_ram_discard_manager(mr, RAM_DISCARD_MANAGER(attr= ))) { + object_unref(OBJECT(attr)); + return NULL; + } + attr->bitmap_size =3D + ROUND_UP(int128_get64(mr->size), block_size) / block_size; + attr->bitmap =3D bitmap_new(attr->bitmap_size); + + return attr; +} + +void ram_block_attributes_destroy(RamBlockAttributes *attr) +{ + g_assert(attr); + + g_free(attr->bitmap); + memory_region_set_ram_discard_manager(attr->ram_block->mr, NULL); + object_unref(OBJECT(attr)); +} + +static void ram_block_attributes_init(Object *obj) +{ + RamBlockAttributes *attr =3D RAM_BLOCK_ATTRIBUTES(obj); + + QLIST_INIT(&attr->rdl_list); +} + +static void ram_block_attributes_finalize(Object *obj) +{ +} + +static void ram_block_attributes_class_init(ObjectClass *klass, + const void *data) +{ + RamDiscardManagerClass *rdmc =3D RAM_DISCARD_MANAGER_CLASS(klass); + + rdmc->get_min_granularity =3D ram_block_attributes_rdm_get_min_granula= rity; + rdmc->register_listener =3D ram_block_attributes_rdm_register_listener; + rdmc->unregister_listener =3D ram_block_attributes_rdm_unregister_list= ener; + rdmc->is_populated =3D ram_block_attributes_rdm_is_populated; + rdmc->replay_populated =3D ram_block_attributes_rdm_replay_populated; + rdmc->replay_discarded =3D ram_block_attributes_rdm_replay_discarded; +} diff --git a/system/meson.build b/system/meson.build index 7514bf3455..6d21ff9faa 100644 --- a/system/meson.build +++ b/system/meson.build @@ -17,6 +17,7 @@ system_ss.add(files( 'dma-helpers.c', 'globals.c', 'ioport.c', + 'ram-block-attributes.c', 'memory_mapping.c', 'memory.c', 'physmem.c', diff --git a/system/trace-events b/system/trace-events index be12ebfb41..82856e44f2 100644 --- a/system/trace-events +++ b/system/trace-events @@ -52,3 +52,6 @@ dirtylimit_state_finalize(void) dirtylimit_throttle_pct(int cpu_index, uint64_t pct, int64_t time_us) "CPU= [%d] throttle percent: %" PRIu64 ", throttle adjust time %"PRIi64 " us" dirtylimit_set_vcpu(int cpu_index, uint64_t quota) "CPU[%d] set dirty page= rate limit %"PRIu64 dirtylimit_vcpu_execute(int cpu_index, int64_t sleep_time_us) "CPU[%d] sle= ep %"PRIi64 " us" + +# ram-block-attributes.c +ram_block_attributes_state_change(uint64_t offset, uint64_t size, const ch= ar *from, const char *to) "offset 0x%"PRIx64" size 0x%"PRIx64" from '%s' to= '%s'" --=20 2.49.0 From nobody Sat Nov 15 14:11:17 2025 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750709314; cv=none; d=zohomail.com; s=zohoarc; b=cBqLHThs/xVf2nDvg25BpFsdZUDi8Jqnlm868amcvP//824S8UQhX38fw53uBkyrSOgEzV46WT4CqiHxmFusRG1SYgTOmNhEskPIrCa3XhNgPewy2uGOrda6kPdne1PNpaxnfMVOo4JPUhSmxUoaAnoY35+/o5gcjmlIFeg9U1M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750709314; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=UfapCsZl0kYjzSD6EX3sg9lpxnZK3jQJXXQJ8voRQa8=; b=nHCK9pMzuv80B2vCkw4fnc5ZN/ZSFzAQ5doHDJYAhyKrRXgrS6nfsNlA5Iq3IxZF4eRQNkDjjZP0INMOD+DG/N/pbolIwxGD1U1gWlq5/YJLJETyZfitfIiNdu2gCYfjOfwdCTH02jvZZG4l+zoH7160oXUVYEvyl0wIo4iYmM4= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750709314521854.3747025667268; Mon, 23 Jun 2025 13:08:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uTnRp-00062x-Vf; Mon, 23 Jun 2025 16:07:21 -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 1uTnRo-00062P-Bg for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:20 -0400 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 1uTnRm-0003YK-H8 for qemu-devel@nongnu.org; Mon, 23 Jun 2025 16:07:20 -0400 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-113-xa774GB-MpqDT_LNxgq0kQ-1; Mon, 23 Jun 2025 16:07:16 -0400 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6fb32203ca6so99042556d6.3 for ; Mon, 23 Jun 2025 13:07:16 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fd095a775bsm48427976d6.112.2025.06.23.13.07.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jun 2025 13:07:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750709237; 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=UfapCsZl0kYjzSD6EX3sg9lpxnZK3jQJXXQJ8voRQa8=; b=L4Aot1g3HEj43XB42V+pvWVTk3yDLVg8U5V/3jhs8fDMddEKjNnLYhSzmmePtyHGts0bxy 6ATnZkOcpWoE6huid88ThpfUC5v4oQbeEDNJ0tszD6SavFZoeEHisCuvqhETnEL6a6lGf4 tSwTvp9H55Mv1e+JjdDFkV5OgxhOcdU= X-MC-Unique: xa774GB-MpqDT_LNxgq0kQ-1 X-Mimecast-MFC-AGG-ID: xa774GB-MpqDT_LNxgq0kQ_1750709235 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750709235; x=1751314035; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UfapCsZl0kYjzSD6EX3sg9lpxnZK3jQJXXQJ8voRQa8=; b=vQMaQ8CWmmP1DQ6O2SA3MAQ8IgCAodRMdhWLhNVPohIMGA2w5JYCZA9A4LX6/TFob/ FtKgiE39Ermh9OrthZoPF1eC5K1oZB/4pj37SGnP7yVmRL43+ElWq7ZR/JB73Qr6R4HX 18S41nQxlni7iHUcUiZ4rm48Jw4fP3lD3Qi++nBeNt8p8HjCWuLx/RqiaSjc9vSxIUGW TT8IFDZGrYFt0u6Xcb+6pyGK2sQtvBE3v20eop9T8X8pT4SMuFC05t0syRjCG6+i3rhC kIEKpFSZ/ST96XRnUTW6h52PEUkZwQq+Q67HwkjyXreADh5TrtCb/I4czB6AtqQ2edj9 AF1A== X-Gm-Message-State: AOJu0YwpRaRY0cFTXEOxDLOwOrwbDvPHpiiUcKJJ1asTFKpkH2N/laF6 2LVjHwjYRj31UyC08gYTRP56o7Tm5aOmEEefbH1dEWFm0hbKBocAg5VyL/PTo8PM4bIwxpU8DHs JMj/UmIFZFeV37nS9AijGuqKEM2bPxnbx/sIQcFi+4gNNIP6kv8hLA+VqgGrwNdYWSz9mHQL31u wWW+ckt8M1UPfsA9NlQuhmphDE5WTNZzdPICe/9Q== X-Gm-Gg: ASbGnctvov+kK+u5XQZmV4iXRnFRMwLXEciYagQFCKqlxnYJh8dWsP1eu4oyPs9lzXH WFnc2bFeej9KVW3R7ynXPpZNqv5YczUFuL+7//7eOYd23dzGubZaoSW8H4rg+XciVoTaJMkALFP R4lTQzhfmFvdBGLqInA8VetO93NlLXxROcWz00h/+1CPCN2QTMF6kL/h8yLHtGhcE6+lQDchA62 EIIg76zkv7p0o+uy2Rc4d0Nmf9QswFj3djCuUlDk7zB38mThn9LNuyE3S6nZEgrAskmjB088AxS 8LZ+Ia3PwhU= X-Received: by 2002:a05:6214:2f92:b0:6fa:c3e4:4251 with SMTP id 6a1803df08f44-6fd0a48c2b9mr239956496d6.15.1750709235260; Mon, 23 Jun 2025 13:07:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWyP1V/g+yzfyABeqhnxTNWZcPgH7gHU3iOvPv6n7FaHcfi+1VPnEz6HlCV6QxBELaHZc66w== X-Received: by 2002:a05:6214:2f92:b0:6fa:c3e4:4251 with SMTP id 6a1803df08f44-6fd0a48c2b9mr239955876d6.15.1750709234802; Mon, 23 Jun 2025 13:07:14 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Chenyi Qiang , Alexey Kardashevskiy Subject: [PULL 11/11] physmem: Support coordinated discarding of RAM with guest_memfd Date: Mon, 23 Jun 2025 16:06:56 -0400 Message-ID: <20250623200656.1882674-12-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250623200656.1882674-1-peterx@redhat.com> References: <20250623200656.1882674-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@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, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: 1750709315545116600 Content-Type: text/plain; charset="utf-8" From: Chenyi Qiang A new field, attributes, was introduced in RAMBlock to link to a RamBlockAttributes object, which centralizes all guest_memfd related information (such as fd and status bitmap) within a RAMBlock. Create and initialize the RamBlockAttributes object upon ram_block_add(). Meanwhile, register the object in the target RAMBlock's MemoryRegion. After that, guest_memfd-backed RAMBlock is associated with the RamDiscardManager interface, and the users can execute RamDiscardManager specific handling. For example, VFIO will register the RamDiscardListener and get notifications when the state_change() helper invokes. As coordinate discarding of RAM with guest_memfd is now supported, only block uncoordinated discard. Tested-by: Alexey Kardashevskiy Reviewed-by: Alexey Kardashevskiy Acked-by: David Hildenbrand Signed-off-by: Chenyi Qiang Link: https://lore.kernel.org/r/20250612082747.51539-6-chenyi.qiang@intel.c= om Signed-off-by: Peter Xu --- include/system/ramblock.h | 1 + accel/kvm/kvm-all.c | 9 +++++++++ system/physmem.c | 23 +++++++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/system/ramblock.h b/include/system/ramblock.h index 1bab9e2dac..87e847e184 100644 --- a/include/system/ramblock.h +++ b/include/system/ramblock.h @@ -46,6 +46,7 @@ struct RAMBlock { int fd; uint64_t fd_offset; int guest_memfd; + RamBlockAttributes *attributes; size_t page_size; /* dirty bitmap used during migration */ unsigned long *bmap; diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index a31778341c..d095d1b98f 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -3091,6 +3091,15 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bo= ol to_private) addr =3D memory_region_get_ram_ptr(mr) + section.offset_within_region; rb =3D qemu_ram_block_from_host(addr, false, &offset); =20 + ret =3D ram_block_attributes_state_change(RAM_BLOCK_ATTRIBUTES(mr->rdm= ), + offset, size, to_private); + if (ret) { + error_report("Failed to notify the listener the state change of " + "(0x%"HWADDR_PRIx" + 0x%"HWADDR_PRIx") to %s", + start, size, to_private ? "private" : "shared"); + goto out_unref; + } + if (to_private) { if (rb->page_size !=3D qemu_real_host_page_size()) { /* diff --git a/system/physmem.c b/system/physmem.c index a8a9ca309e..ff0ca40222 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -1916,7 +1916,7 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp) } assert(new_block->guest_memfd < 0); =20 - ret =3D ram_block_discard_require(true); + ret =3D ram_block_coordinated_discard_require(true); if (ret < 0) { error_setg_errno(errp, -ret, "cannot set up private guest memory: discard = currently blocked"); @@ -1931,6 +1931,24 @@ static void ram_block_add(RAMBlock *new_block, Error= **errp) goto out_free; } =20 + /* + * The attribute bitmap of the RamBlockAttributes is default to + * discarded, which mimics the behavior of kvm_set_phys_mem() when= it + * calls kvm_set_memory_attributes_private(). This leads to a brief + * period of inconsistency between the creation of the RAMBlock an= d its + * mapping into the physical address space. However, this is not + * problematic, as no users rely on the attribute status to perform + * any actions during this interval. + */ + new_block->attributes =3D ram_block_attributes_create(new_block); + if (!new_block->attributes) { + error_setg(errp, "Failed to create ram block attribute"); + close(new_block->guest_memfd); + ram_block_coordinated_discard_require(false); + qemu_mutex_unlock_ramlist(); + goto out_free; + } + /* * Add a specific guest_memfd blocker if a generic one would not be * added by ram_block_add_cpr_blocker. @@ -2287,8 +2305,9 @@ static void reclaim_ramblock(RAMBlock *block) } =20 if (block->guest_memfd >=3D 0) { + ram_block_attributes_destroy(block->attributes); close(block->guest_memfd); - ram_block_discard_require(false); + ram_block_coordinated_discard_require(false); } =20 g_free(block); --=20 2.49.0