From nobody Tue Nov 11 22:40:33 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1566310546; cv=none; d=zoho.com; s=zohoarc; b=mdideYJmrQoFwqW9Yyu6A7TSbG+p4hY1IrXtyoWiB+qLKyzSaYXJVCY7RTVgqkp+Ilamax5RgWph0LC7uYh5AUjPBgNI87WiPVJ8QFU59GSr83w9Jky+1jf/rsJ9i8J2DY3WJS121AbowTAHp90jngp79BGxIyUt7/0nXJPsEvU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566310546; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=hBouWE8l7NbgVMb99cl2vWSbFT7rp2TkkkJ6JfwlSJM=; b=R43jpj2jx001l3t0NtC1cfyaBoWFSyFxMn8d38m66KEdHsXJn8irEtixWfgzha3n0kyXjMKxyg+mQKZJiYRWqhTrUCxZaMN7v6k8h5CihIIldixQaaOEySQeQPROBsZl/eWhU2dYk7ozxT9faOPnPl2JS8MAZvs+aGKyQ5zB8jk= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1566310546837577.9185586259935; Tue, 20 Aug 2019 07:15:46 -0700 (PDT) Received: from localhost ([::1]:37912 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i04vJ-0003wt-CJ for importer@patchew.org; Tue, 20 Aug 2019 10:15:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51841) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i04tW-0002be-3e for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i04tU-0007Wz-SF for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:54 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52804) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i04tU-0007TU-Hy for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:52 -0400 Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D3DD6C0546F2 for ; Tue, 20 Aug 2019 14:13:51 +0000 (UTC) Received: by mail-pf1-f197.google.com with SMTP id n186so4905558pfn.6 for ; Tue, 20 Aug 2019 07:13:51 -0700 (PDT) Received: from xz-x1.redhat.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id e185sm9057921pfa.119.2019.08.20.07.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2019 07:13:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=09T6APry/hKW1d7wgqIZDeiyq1OtzijdsQjH90lcRxY=; b=jAHwIrbuxSfuwcMCNeBU2jyd5X5VNza0Rd6/F0PMlY8AdR2ExP5terpLIcWKDwyqiM D2Xxftl2XzVFoxXk+2MbRaJcqhzUDtnISCN1whG2e7VazdUQA9yU3yF8fGf9CUJ/8HcV IdbKQ9DNAmviDW1SvMNcElg86ZP2U0UwChIuiOL574F9uGdrKaSwyR6PLkCAPL0ANIa5 pPyDBtsQzFiVW+6f45dhkjkXo2kPYWMKUp+BJbG+QSLhoNZIKkCMPdkaWjK4ArNJOGVL 5JcKaMNnHeGoz+bxiY9S0bla+4J82lRGV1geY3CP/Lb626f0LWS6kk82xnNOkfe5xHC0 nViQ== X-Gm-Message-State: APjAAAXe/lbJdY7WT5XGtONb2OPWuhmiYQ597GCpWCWx2uWEzZWWXhKZ Mm0O6LdP1Xw5L4x1iSHfGapTsh3amkTB+v5oxEQHALXyXDxbBUlc00xB0YeBaKRjYHIfsJ7ABKz aYGbiM5kTcaoxx/c= X-Received: by 2002:a17:902:7083:: with SMTP id z3mr28435065plk.87.1566310431047; Tue, 20 Aug 2019 07:13:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqwbCWRxHRwtCElFSTc/rf/zLBkKNbGRm8wwm4/t4pTfLKe9HmoWPuGkG0/aDlRxgTzKZ0WWrg== X-Received: by 2002:a17:902:7083:: with SMTP id z3mr28435047plk.87.1566310430899; Tue, 20 Aug 2019 07:13:50 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 20 Aug 2019 22:13:28 +0800 Message-Id: <20190820141328.10009-5-peterx@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190820141328.10009-1-peterx@redhat.com> References: <20190820141328.10009-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 4/4] memory: Fix up memory_region_{add|del}_coalescing X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , peterx@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The old memory_region_{add|clear}_coalescing() has some defects because they both changed mr->coalesced before updating the regions using memory_region_update_coalesced_range_as(). Then when the regions were updated in memory_region_update_coalesced_range_as() the mr->coalesced will always be either one more or one less. So: - For memory_region_add_coalescing: it'll always trying to remove the newly added coalesced region while it shouldn't, and, - For memory_region_clear_coalescing: when it calls the update there will be no coalesced ranges on mr->coalesced because they were all removed before hand so the update will probably do nothing for real. Let's fix this. Now we've got flat_range_coalesced_io_notify() to notify a single CoalescedMemoryRange instance change, so use it in the existing memory_region_update_coalesced_range() logic by only notify either an addition or deletion. Then we hammer both the memory_region_{add|clear}_coalescing() to use it. Fixes: 3ac7d43a6fbb5d4a3 Signed-off-by: Peter Xu --- memory.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/memory.c b/memory.c index 2f7a67086a..5d2194ebcc 100644 --- a/memory.c +++ b/memory.c @@ -2238,27 +2238,26 @@ void memory_region_ram_resize(MemoryRegion *mr, ram= _addr_t newsize, Error **errp qemu_ram_resize(mr->ram_block, newsize, errp); } =20 -static void memory_region_update_coalesced_range_as(MemoryRegion *mr, Addr= essSpace *as) +/* + * Call proper memory listeners about the change on the newly + * added/removed CoalescedMemoryRange. + */ +static void memory_region_update_coalesced_range(MemoryRegion *mr, + CoalescedMemoryRange *cmr, + bool add) { + AddressSpace *as; FlatView *view; FlatRange *fr; =20 - view =3D address_space_get_flatview(as); - FOR_EACH_FLAT_RANGE(fr, view) { - if (fr->mr =3D=3D mr) { - flat_range_coalesced_io_del(fr, as); - flat_range_coalesced_io_add(fr, as); - } - } - flatview_unref(view); -} - -static void memory_region_update_coalesced_range(MemoryRegion *mr) -{ - AddressSpace *as; - QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { - memory_region_update_coalesced_range_as(mr, as); + view =3D address_space_get_flatview(as); + FOR_EACH_FLAT_RANGE(fr, view) { + if (fr->mr =3D=3D mr) { + flat_range_coalesced_io_notify(fr, as, cmr, add); + } + } + flatview_unref(view); } } =20 @@ -2276,7 +2275,7 @@ void memory_region_add_coalescing(MemoryRegion *mr, =20 cmr->addr =3D addrrange_make(int128_make64(offset), int128_make64(size= )); QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link); - memory_region_update_coalesced_range(mr); + memory_region_update_coalesced_range(mr, cmr, true); memory_region_set_flush_coalesced(mr); } =20 @@ -2294,10 +2293,9 @@ void memory_region_clear_coalescing(MemoryRegion *mr) while (!QTAILQ_EMPTY(&mr->coalesced)) { cmr =3D QTAILQ_FIRST(&mr->coalesced); QTAILQ_REMOVE(&mr->coalesced, cmr, link); + memory_region_update_coalesced_range(mr, cmr, false); g_free(cmr); } - - memory_region_update_coalesced_range(mr); } =20 void memory_region_set_flush_coalesced(MemoryRegion *mr) --=20 2.21.0