From nobody Wed Aug 26 18:57:12 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1780580814; cv=none; d=zohomail.com; s=zohoarc; b=MttfAs5MzH9MFal8zuiWmQ++30x2fka/WqWhEN0sYrCcvS8tu2cqxqcg+CdCC//Yfknnxk+OPACSMynBMcI6UgZ8+7Q+YQS9iyGt6n/eMQquQ9mkBdDPcVRrz1YMB1s4ZDSqGBe25E89dts2CM6C25PSDJSMM27EPZ3fMCkWLSw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1780580814; 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=Xl9RCdMEAiNI/QszRDVHp8xycP9ssDJJLT37LguwrcY=; b=Bp0OksGxNhrqM5S+Ml3/k2MJvFyAl+BwJ6mp/KQ/pPE1D/PYUUOkSGsVP2NJEFBxRLoSEqWQLsJvDDtbJva5tZYa2n8d8e4SlhatnYXGy59ej4J/R9kfsgg+frwnRKvgh9sGwbYi37lRfcwrp0192jxJZpNe/9oe+BciI+KhSwo= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 178058081493489.24228463533393; Thu, 4 Jun 2026 06:46:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wV8OU-0001wd-Cw; Thu, 04 Jun 2026 09:45:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wV8NW-0001Mw-E5 for qemu-devel@nongnu.org; Thu, 04 Jun 2026 09:44:58 -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 1wV8NU-00008F-Re for qemu-devel@nongnu.org; Thu, 04 Jun 2026 09:44:58 -0400 Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-660-jyPHo_ETP0iXAeVQpKdpDg-1; Thu, 04 Jun 2026 09:44:54 -0400 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6D4B71944D2B; Thu, 4 Jun 2026 13:44:52 +0000 (UTC) Received: from localhost (unknown [10.44.24.6]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 426E61955D87; Thu, 4 Jun 2026 13:44:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780580696; 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=Xl9RCdMEAiNI/QszRDVHp8xycP9ssDJJLT37LguwrcY=; b=hlxjgqXnOhLpoUvMyzc2bGiaEBC3hNZoFgpcOtxDR/SNcw3WZ0u+LjAEboqCr26y+Z0bwc dYS2wn9MRIBRd1th0j8b5FUVr2ua6ILneUyQAmhLWAk5gpDC0zWetHmDBWXCHFULH6shYV odf1MM4sZO6TlVmNQTuKvWxpXLmtcc0= X-MC-Unique: jyPHo_ETP0iXAeVQpKdpDg-1 X-Mimecast-MFC-AGG-ID: jyPHo_ETP0iXAeVQpKdpDg_1780580692 From: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= Date: Thu, 04 Jun 2026 17:43:50 +0400 Subject: [PATCH v5 04/12] system/ram-discard-manager: implement replay via is_populated iteration MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260604-rdm5-v5-4-5768e6a0943d@redhat.com> References: <20260604-rdm5-v5-0-5768e6a0943d@redhat.com> In-Reply-To: <20260604-rdm5-v5-0-5768e6a0943d@redhat.com> To: qemu-devel@nongnu.org Cc: Zhenzhong Duan , "Michael S. Tsirkin" , David Hildenbrand , Paolo Bonzini , Peter Xu , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-rust@nongnu.org, Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_Goater?= , "Maciej S. Szmigiero" , Fabiano Rosas , Mark Kanda , Ben Chaney , Marcelo Tosatti , kvm@vger.kernel.org, "Dr. David Alan Gilbert" , Zhao Liu , Eric Blake , Markus Armbruster , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Developer-Signature: v=1; a=openpgp-sha256; l=6612; i=marcandre.lureau@redhat.com; h=from:subject:message-id; bh=znVW5921M7+0092ia+MeDcCo7lcpbF5fnLhhQFRfDqg=; b=owEBbQKS/ZANAwAKAdro4Ql1lpzlAcsmYgBqIYEwCZfScNDE5Vpjil/kVwVsL+hRHm2fLwKlH OvbKXyWk8qJAjMEAAEKAB0WIQSHqb2TP4fGBtJ29i3a6OEJdZac5QUCaiGBMAAKCRDa6OEJdZac 5aLTD/90+rze5UF4V7k4llDujSN41MD2hldcmVC8EUhex1DIX479ntPfkHKdl7IJJZVmZ5OyPAP 8TKsnksokw9Jfa4zNKQQhlZiRQHnB6J+blGcT7yUBGq0+gwK3qTwDauxD3WE70un2a3QxSgAYaP 2KT70j3Vl69CfiCGNrMcsgBWGix7ZZX559QgfDCiQ/goXk/T1bpEtpmf/ZvMpoKwm82FpZTrvpJ bwc9Krsq0kM17lM8smI4TmcYHN4u0l4SmB2wFqm9fmuU2107d2v/E5OAjaaQERa6M5YXjuTjdP0 0JajWufgj6u2gznVtyv/DxZaMnOwEqwEMiWOWey9VHjYfoGNzEsXDVRdAvsD7FjeIIStSgcMTul bxw0isQCUOKQxaMFe6tNLuxddIUHx+yiBXogdWnh1m6gVb4tYJMPTIwR3nF8skAYPIzqldKgcB0 zGb1R9oF7zkT4v+aphWkOF36NQ3DBsEV/QlsmVuteEp4zPkz+ikN1T71IE9pIiRMDseL1bf7S7G SpfwoSzxf3P/9IYRt/FZZkr8VxbzLbTgAUBMCLUCAbo3cuzTlnSWR1GdemNGKWIl8hgLuUu36yH iaaHkgmVwEm7Jlt0UqkKV4UcmbkeC948T2UXFU9iNIZW8X+3uqZtxlF5XK3D1m6TxPq2wILJ34+ kmNQF1xPgrW6pqQ== X-Developer-Key: i=marcandre.lureau@redhat.com; a=openpgp; fpr=87A9BD933F87C606D276F62DDAE8E10975969CE5 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 8 X-Spam_score: 0.8 X-Spam_bar: / X-Spam_report: (0.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.445, 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_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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: 1780580817337158500 Replace the source-level replay wrappers with a new replay_by_populated_state() helper that iterates the section at min-granularity, calls is_populated() for each chunk, and aggregates consecutive chunks of the same state before invoking the callback. This moves the iteration logic from individual sources into the manager, preparing for multi-source aggregation where the manager must combine state from multiple sources anyway. The replay_populated/replay_discarded vtable entries in RamDiscardSourceClass are no longer called but remain in the interface for now; they will be removed in follow-up commits along with the now-dead source implementations. Reviewed-by: Peter Xu Signed-off-by: Marc-Andr=C3=A9 Lureau Acked-by: David Hildenbrand --- system/ram-discard-manager.c | 85 +++++++++++++++++++++++++++++++---------= ---- 1 file changed, 61 insertions(+), 24 deletions(-) diff --git a/system/ram-discard-manager.c b/system/ram-discard-manager.c index 1c9ff7fda58..a907ddf3708 100644 --- a/system/ram-discard-manager.c +++ b/system/ram-discard-manager.c @@ -27,26 +27,65 @@ static bool ram_discard_source_is_populated(const RamDi= scardSource *rds, return rdsc->is_populated(rds, section); } =20 -static int ram_discard_source_replay_populated(const RamDiscardSource *rds, - const MemoryRegionSection *= section, - ReplayRamDiscardState repla= y_fn, - void *opaque) +/* + * Iterate the section at source granularity, aggregating consecutive chun= ks + * with matching populated state, and call replay_fn for each run. + */ +static int replay_by_populated_state(const RamDiscardManager *rdm, + const MemoryRegionSection *section, + bool replay_populated, + ReplayRamDiscardState replay_fn, + void *opaque) { - RamDiscardSourceClass *rdsc =3D RAM_DISCARD_SOURCE_GET_CLASS(rds); + uint64_t granularity, offset, size, end, pos, run_start =3D 0; + bool in_run =3D false; + int ret =3D 0; =20 - g_assert(rdsc->replay_populated); - return rdsc->replay_populated(rds, section, replay_fn, opaque); -} + granularity =3D ram_discard_source_get_min_granularity(rdm->rds, rdm->= mr); + offset =3D section->offset_within_region; + size =3D int128_get64(section->size); + end =3D offset + size; + + /* Align iteration to granularity boundaries */ + pos =3D QEMU_ALIGN_DOWN(offset, granularity); + + for (; pos < end; pos +=3D granularity) { + MemoryRegionSection chunk =3D { + .mr =3D section->mr, + .offset_within_region =3D pos, + .size =3D int128_make64(granularity), + }; + bool populated =3D ram_discard_source_is_populated(rdm->rds, &chun= k); + + if (populated =3D=3D replay_populated) { + if (!in_run) { + run_start =3D pos; + in_run =3D true; + } + } else if (in_run) { + MemoryRegionSection tmp =3D *section; + + if (memory_region_section_intersect_range(&tmp, run_start, + pos - run_start)) { + ret =3D replay_fn(&tmp, opaque); + if (ret) { + return ret; + } + } + in_run =3D false; + } + } =20 -static int ram_discard_source_replay_discarded(const RamDiscardSource *rds, - const MemoryRegionSection *= section, - ReplayRamDiscardState repla= y_fn, - void *opaque) -{ - RamDiscardSourceClass *rdsc =3D RAM_DISCARD_SOURCE_GET_CLASS(rds); + if (in_run) { + MemoryRegionSection tmp =3D *section; =20 - g_assert(rdsc->replay_discarded); - return rdsc->replay_discarded(rds, section, replay_fn, opaque); + if (memory_region_section_intersect_range(&tmp, run_start, + pos - run_start)) { + ret =3D replay_fn(&tmp, opaque); + } + } + + return ret; } =20 RamDiscardManager *ram_discard_manager_new(MemoryRegion *mr, @@ -78,8 +117,7 @@ int ram_discard_manager_replay_populated(const RamDiscar= dManager *rdm, ReplayRamDiscardState replay_fn, void *opaque) { - return ram_discard_source_replay_populated(rdm->rds, section, - replay_fn, opaque); + return replay_by_populated_state(rdm, section, true, replay_fn, opaque= ); } =20 int ram_discard_manager_replay_discarded(const RamDiscardManager *rdm, @@ -87,8 +125,7 @@ int ram_discard_manager_replay_discarded(const RamDiscar= dManager *rdm, ReplayRamDiscardState replay_fn, void *opaque) { - return ram_discard_source_replay_discarded(rdm->rds, section, - replay_fn, opaque); + return replay_by_populated_state(rdm, section, false, replay_fn, opaqu= e); } =20 static void ram_discard_manager_initfn(Object *obj) @@ -182,8 +219,8 @@ void ram_discard_manager_register_listener(RamDiscardMa= nager *rdm, rdl->section =3D memory_region_section_new_copy(section); QLIST_INSERT_HEAD(&rdm->rdl_list, rdl, next); =20 - ret =3D ram_discard_source_replay_populated(rdm->rds, rdl->section, - rdm_populate_cb, rdl); + ret =3D ram_discard_manager_replay_populated(rdm, rdl->section, + rdm_populate_cb, rdl); if (ret) { error_report("%s: Replaying populated ranges failed: %s", __func__, strerror(-ret)); @@ -208,8 +245,8 @@ int ram_discard_manager_replay_populated_to_listeners(R= amDiscardManager *rdm) int ret =3D 0; =20 QLIST_FOREACH(rdl, &rdm->rdl_list, next) { - ret =3D ram_discard_source_replay_populated(rdm->rds, rdl->section, - rdm_populate_cb, rdl); + ret =3D ram_discard_manager_replay_populated(rdm, rdl->section, + rdm_populate_cb, rdl); if (ret) { break; } --=20 2.54.0