From patchwork Tue Aug 20 14:13:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 1150184 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46CXt75rMQz9sBF for ; Wed, 21 Aug 2019 00:17:03 +1000 (AEST) Received: from localhost ([::1]:37916 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i04wX-00058r-Iu for incoming@patchwork.ozlabs.org; Tue, 20 Aug 2019 10:17:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51787) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i04tN-0002Rh-UD for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i04tM-0007LP-RA for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49050) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i04tM-0007Ku-Js for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:44 -0400 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (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 DC1AFC0467C3 for ; Tue, 20 Aug 2019 14:13:43 +0000 (UTC) Received: by mail-pf1-f198.google.com with SMTP id 191so4891379pfy.20 for ; Tue, 20 Aug 2019 07:13:43 -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=cZFoxwy7BgOSzGeOdH9WM+DUKNeOVDwNKzYU3L8feJA=; b=nPbo4iilvikd2RBiCrJvolHevhz65InrCna8P1TPa+1VXDXV5ZHcFL/SbQ/XtQu38n qPHI+N2nseP9kjcejhhI1FdLIZg8SoG2zVXUydtJ8A+m7ERssaZ9OFBbowMtyBhkqU7l KBf8o3WnOUZ5+Rs5SgKs0cqXiyopFts4TvTC1ySDpZPSWnRqjcQk+mAtT/suV49xJMP5 OXhaGwSVzV5oRPVFe3JMsycXXUYSHzz/rKdh0ALhn1X3PylLPZ390l0mmycvUvGhHuME TKRINus4luUuyjk3s7E1RYXd7bCnEqZVxiJBGZeseZT9jfGkDwDpQXxjbe921FQwGW1N 1BIA== X-Gm-Message-State: APjAAAUgPHL1V0KREYj2TijRA0G8gyqrkKG3MdRk6PKNKoaW6hQ4MHcV pkU3chsOVHfTXnjiINAKMixlu3YCDQyyENtqctow3GynkZkzL28rFRd+uFS30HRY1TBKWiiX5ff vakvRV8TbopWnMr4= X-Received: by 2002:aa7:9a5a:: with SMTP id x26mr29329594pfj.47.1566310422776; Tue, 20 Aug 2019 07:13:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqzQKgYqnxpqMDObrt2pbF3XE0Qi1egFttdRsx8+3xSfOupBH+bOgVMgL9UUcBGDor/8frnJhw== X-Received: by 2002:aa7:9a5a:: with SMTP id x26mr29329576pfj.47.1566310422588; Tue, 20 Aug 2019 07:13:42 -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.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2019 07:13:41 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 20 Aug 2019 22:13:25 +0800 Message-Id: <20190820141328.10009-2-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 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 1/4] memory: Split zones when do coalesced_io_del() 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" It is a workaround of current KVM's KVM_UNREGISTER_COALESCED_MMIO interface. The kernel interface only allows to unregister an mmio device with exactly the zone size when registered, or any smaller zone that is included in the device mmio zone. It does not support the userspace to specify a very large zone to remove all the small mmio devices within the zone covered. Logically speaking it would be nicer to fix this from KVM side, though in all cases we still need to coop with old kernels so let's do this. Fixes: 3ac7d43a6fbb5d4a3 Signed-off-by: Peter Xu --- memory.c | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/memory.c b/memory.c index 8141486832..8173f6be62 100644 --- a/memory.c +++ b/memory.c @@ -855,8 +855,39 @@ static void address_space_update_ioeventfds(AddressSpace *as) flatview_unref(view); } +/* + * Notify the memory listeners about the coalesced IO change events of + * range `cmr'. Only the part that has intersection of the specified + * FlatRange will be sent. + */ +static void flat_range_coalesced_io_notify(FlatRange *fr, AddressSpace *as, + CoalescedMemoryRange *cmr, bool add) +{ + AddrRange tmp; + + tmp = addrrange_shift(cmr->addr, + int128_sub(fr->addr.start, + int128_make64(fr->offset_in_region))); + if (!addrrange_intersects(tmp, fr->addr)) { + return; + } + tmp = addrrange_intersection(tmp, fr->addr); + + if (add) { + MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, coalesced_io_add, + int128_get64(tmp.start), + int128_get64(tmp.size)); + } else { + MEMORY_LISTENER_UPDATE_REGION(fr, as, Reverse, coalesced_io_del, + int128_get64(tmp.start), + int128_get64(tmp.size)); + } +} + static void flat_range_coalesced_io_del(FlatRange *fr, AddressSpace *as) { + CoalescedMemoryRange *cmr; + if (!fr->has_coalesced_range) { return; } @@ -865,16 +896,15 @@ static void flat_range_coalesced_io_del(FlatRange *fr, AddressSpace *as) return; } - MEMORY_LISTENER_UPDATE_REGION(fr, as, Reverse, coalesced_io_del, - int128_get64(fr->addr.start), - int128_get64(fr->addr.size)); + QTAILQ_FOREACH(cmr, &fr->mr->coalesced, link) { + flat_range_coalesced_io_notify(fr, as, cmr, false); + } } static void flat_range_coalesced_io_add(FlatRange *fr, AddressSpace *as) { MemoryRegion *mr = fr->mr; CoalescedMemoryRange *cmr; - AddrRange tmp; if (QTAILQ_EMPTY(&mr->coalesced)) { return; @@ -885,16 +915,7 @@ static void flat_range_coalesced_io_add(FlatRange *fr, AddressSpace *as) } QTAILQ_FOREACH(cmr, &mr->coalesced, link) { - tmp = addrrange_shift(cmr->addr, - int128_sub(fr->addr.start, - int128_make64(fr->offset_in_region))); - if (!addrrange_intersects(tmp, fr->addr)) { - continue; - } - tmp = addrrange_intersection(tmp, fr->addr); - MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, coalesced_io_add, - int128_get64(tmp.start), - int128_get64(tmp.size)); + flat_range_coalesced_io_notify(fr, as, cmr, true); } } From patchwork Tue Aug 20 14:13:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 1150180 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46CXpz4lBlz9sBF for ; Wed, 21 Aug 2019 00:14:19 +1000 (AEST) Received: from localhost ([::1]:37886 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i04tt-0002Uu-Gb for incoming@patchwork.ozlabs.org; Tue, 20 Aug 2019 10:14:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51803) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i04tQ-0002SM-8i for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i04tP-0007NM-7w for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40488) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i04tP-0007Mj-0z for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:47 -0400 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.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 52B6C793FF for ; Tue, 20 Aug 2019 14:13:46 +0000 (UTC) Received: by mail-pg1-f197.google.com with SMTP id l11so4682999pgc.14 for ; Tue, 20 Aug 2019 07:13:46 -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=ubAKcy3NY2gpBzdykwWOXUonxN5dGIVc7Web/xo6k04=; b=ULDxRpEtcuhO87PcJX/2Bx3UnAZhHx0bGdthDvLgteWAcId2iD1NsWJRgy9PRHFHT3 SXF0d6z27UJ4brfh/UJ4KGT6t2uRERN/gd9Me6M72es1sZOzKmJSHYtccIWudGhrJclY 7oK+r2gvCH/H0hoXYOxfygYUMrsn2yCpIrM9EJShn9qux0EGbE8Z7v8E+6MNRmftAA/C Mfh7QkHKB4HLcdAx5mY8yGgWZ65It+UuFnQnTWdw+9/eFMQYNAhqFEcKZ9JEHMQ/UIqP 8xDOZsLnTW/zVnN2p8JG/uKYUYRh3i/TKatlFG+fumiymLpjCVKYelP03r486FKxPhji Defg== X-Gm-Message-State: APjAAAXpv9oiBd99f2zzs6S5edX2luz6copSMFtMs5My61mxS7h2VBZW 2O0Dta1qxkOpRfj+HeL1UllnKY115CqCp5oqofKPygQ5UbWPVcawPXGV66HREGt8xXKHStiGydY xjYfF/GkTvnJJ6So= X-Received: by 2002:aa7:9a52:: with SMTP id x18mr30859839pfj.8.1566310425531; Tue, 20 Aug 2019 07:13:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqws+yms14Sv0pSupjYMPjs9nTbNvtj2TulTMrlvZNEzxSV6QEcGTN1yShiioMUKWqRl7MDArg== X-Received: by 2002:aa7:9a52:: with SMTP id x18mr30859827pfj.8.1566310425368; Tue, 20 Aug 2019 07:13:45 -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.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2019 07:13:44 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 20 Aug 2019 22:13:26 +0800 Message-Id: <20190820141328.10009-3-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 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 2/4] memory: Remove has_coalesced_range counter 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" The has_coalesced_range could potentially be problematic in that it only works for additions of coalesced mmio ranges but not deletions. The reason is that has_coalesced_range information can be lost when the FlatView updates the topology again when the updated region is not covering the coalesced regions. When that happens, due to flatrange_equal() is not checking against has_coalesced_range, the new FlatRange will be seen as the same one as the old and the new instance (whose has_coalesced_range will be zero) will replace the old instance (whose has_coalesced_range _could_ be non-zero). The counter was originally used to make sure every FlatRange will only notify once for coalesced_io_{add|del} memory listeners, because each FlatRange can be used by multiple address spaces, so logically speaking it could be called multiple times. However we should not limit that, because memory listeners should will only be registered with specific address space rather than multiple address spaces. So let's fix this up by simply removing the whole has_coalesced_range. Fixes: 3ac7d43a6fbb5d4a3 Signed-off-by: Peter Xu --- memory.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/memory.c b/memory.c index 8173f6be62..360e0cfa67 100644 --- a/memory.c +++ b/memory.c @@ -217,7 +217,6 @@ struct FlatRange { bool romd_mode; bool readonly; bool nonvolatile; - int has_coalesced_range; }; #define FOR_EACH_FLAT_RANGE(var, view) \ @@ -654,7 +653,6 @@ static void render_memory_region(FlatView *view, fr.romd_mode = mr->romd_mode; fr.readonly = readonly; fr.nonvolatile = nonvolatile; - fr.has_coalesced_range = 0; /* Render the region itself into any gaps left by the current view. */ for (i = 0; i < view->nr && int128_nz(remain); ++i) { @@ -888,14 +886,6 @@ static void flat_range_coalesced_io_del(FlatRange *fr, AddressSpace *as) { CoalescedMemoryRange *cmr; - if (!fr->has_coalesced_range) { - return; - } - - if (--fr->has_coalesced_range > 0) { - return; - } - QTAILQ_FOREACH(cmr, &fr->mr->coalesced, link) { flat_range_coalesced_io_notify(fr, as, cmr, false); } @@ -910,10 +900,6 @@ static void flat_range_coalesced_io_add(FlatRange *fr, AddressSpace *as) return; } - if (fr->has_coalesced_range++) { - return; - } - QTAILQ_FOREACH(cmr, &mr->coalesced, link) { flat_range_coalesced_io_notify(fr, as, cmr, true); } From patchwork Tue Aug 20 14:13:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 1150185 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46CXtC4M7Bz9sBF for ; Wed, 21 Aug 2019 00:17:07 +1000 (AEST) Received: from localhost ([::1]:37920 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i04wb-0005H6-2r for incoming@patchwork.ozlabs.org; Tue, 20 Aug 2019 10:17:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51823) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i04tS-0002Wl-Qx for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i04tR-0007Qu-SK for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49914) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i04tR-0007QO-NA for qemu-devel@nongnu.org; Tue, 20 Aug 2019 10:13:49 -0400 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (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 1006A89AC2 for ; Tue, 20 Aug 2019 14:13:49 +0000 (UTC) Received: by mail-pl1-f199.google.com with SMTP id f5so4174769plr.0 for ; Tue, 20 Aug 2019 07:13:49 -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=2PxH9JaNXkeuvOjhzmhEfzKK45LxMRv9xnnAwcfD0eo=; b=PMBywFCsFNwwcPRGNb3ro9t4B7gT5R1gAqHLZ1/OSZeYFAn+vD21Uck1jy9lX9CM8l dlv9hMvzD/dMEFlY9mwCFA0DS8PikG1xaz7rQLSzLVY22f4UdY8rdo00HN9cUVkH+bBD OBds7fUt0SXQLaG9Hs1VIud4D8C1Gb/8SW4M8O+k0ZMXINYK99m05CI5/dkJskp4RrWm kh9hLgInxATKIHaiwFtLXydk1U+qEB4ssCJ9O1dsy/6w1iRvRcE0siEQ4JzoLPkyZvff fet0M7Qwa+YphPv72J36/djJLrXruO4nBFpz7OA4fnjPclbgkwZQ6u9CU2iaZohY0XnG MkxQ== X-Gm-Message-State: APjAAAVod3p7TjTL/5477ybndLb83FkBskgadUvwOCj0rwngBzwNcU+Q 9HKDXP4QrkNKF7gxGRUcGsHvf54OHtrrhZU72muJOdlbcymdXr2J0F8bWjAjv4QN4mOyPhPpAS3 3il8ZSGAudZbMnwY= X-Received: by 2002:a17:902:7c8a:: with SMTP id y10mr27982683pll.65.1566310428281; Tue, 20 Aug 2019 07:13:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqzhd+5Ot7CHyzqOniQ30wJxCNFI+sApSy093OVkQHhamL8alwAGuGSUJefUBqzFqiIHcOkQRQ== X-Received: by 2002:a17:902:7c8a:: with SMTP id y10mr27982664pll.65.1566310428133; Tue, 20 Aug 2019 07:13:48 -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.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2019 07:13:47 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 20 Aug 2019 22:13:27 +0800 Message-Id: <20190820141328.10009-4-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 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 3/4] memory: Refactor memory_region_clear_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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Removing the update variable and quit earlier if the memory region has no coalesced range. This prepares for the next patch. Fixes: 3ac7d43a6fbb5d4a3 Signed-off-by: Peter Xu --- memory.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/memory.c b/memory.c index 360e0cfa67..2f7a67086a 100644 --- a/memory.c +++ b/memory.c @@ -2283,7 +2283,10 @@ void memory_region_add_coalescing(MemoryRegion *mr, void memory_region_clear_coalescing(MemoryRegion *mr) { CoalescedMemoryRange *cmr; - bool updated = false; + + if (QTAILQ_EMPTY(&mr->coalesced)) { + return; + } qemu_flush_coalesced_mmio_buffer(); mr->flush_coalesced_mmio = false; @@ -2292,12 +2295,9 @@ void memory_region_clear_coalescing(MemoryRegion *mr) cmr = QTAILQ_FIRST(&mr->coalesced); QTAILQ_REMOVE(&mr->coalesced, cmr, link); g_free(cmr); - updated = true; } - if (updated) { - memory_region_update_coalesced_range(mr); - } + memory_region_update_coalesced_range(mr); } void memory_region_set_flush_coalesced(MemoryRegion *mr) From patchwork Tue Aug 20 14:13:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 1150181 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46CXq72vDgz9s00 for ; Wed, 21 Aug 2019 00:14:27 +1000 (AEST) Received: from localhost ([::1]:37900 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i04u1-0002fU-4s for incoming@patchwork.ozlabs.org; Tue, 20 Aug 2019 10:14:25 -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) 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) 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) 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 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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); } -static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *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; - view = address_space_get_flatview(as); - FOR_EACH_FLAT_RANGE(fr, view) { - if (fr->mr == 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 = address_space_get_flatview(as); + FOR_EACH_FLAT_RANGE(fr, view) { + if (fr->mr == mr) { + flat_range_coalesced_io_notify(fr, as, cmr, add); + } + } + flatview_unref(view); } } @@ -2276,7 +2275,7 @@ void memory_region_add_coalescing(MemoryRegion *mr, cmr->addr = 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); } @@ -2294,10 +2293,9 @@ void memory_region_clear_coalescing(MemoryRegion *mr) while (!QTAILQ_EMPTY(&mr->coalesced)) { cmr = 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); } void memory_region_set_flush_coalesced(MemoryRegion *mr)