From nobody Sun Feb 8 09:05:54 2026 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=1566278285; cv=none; d=zoho.com; s=zohoarc; b=h4KsvslAzkuhZXqsY8FadILfhIBF3nhRZyLFVZizAndKAxQE70M5aufMP3IKZ8ET9l4Qct9dum4UckvJVe177c3wMuU2dUReCM0V0s8FBvfcRVTNF7PoVbW2CACQ71W3zT+o6MpOUcZ70S+A+DLPH5GYl2cSW/bU0hOMqnBh/ps= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566278285; 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=rGToBesED9otCcHiQcYdE5rjh29iH8yIX7lmdLwEhpY=; b=FdTSfviClLJR7cIifBmqK4LD/Fqr4V5ROq8/a2iMj4DJ66i63eUQpxl/K94kolJOg4kuL4rOyfW1yoQXgfp+8sfbM+xCxBdOZy0y7lRG79wXAhnLVMNFrIvVsGGS417XKo84nd9XPxjCW13TITN3SEMcVEbuTQMDQF5HFi0206A= 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 1566278285809756.96850618262; Mon, 19 Aug 2019 22:18:05 -0700 (PDT) Received: from localhost ([::1]:33056 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hzwWs-0008M2-Vm for importer@patchew.org; Tue, 20 Aug 2019 01:17:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57916) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hzwVU-0006oc-AJ for qemu-devel@nongnu.org; Tue, 20 Aug 2019 01:16:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hzwVT-0004ye-91 for qemu-devel@nongnu.org; Tue, 20 Aug 2019 01:16:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42276) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hzwVT-0004yL-0s for qemu-devel@nongnu.org; Tue, 20 Aug 2019 01:16:31 -0400 Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) (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 59C5C31D8B6 for ; Tue, 20 Aug 2019 05:16:30 +0000 (UTC) Received: by mail-pf1-f200.google.com with SMTP id q67so4002697pfc.10 for ; Mon, 19 Aug 2019 22:16:30 -0700 (PDT) Received: from xz-x1.redhat.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id e6sm17462610pfn.71.2019.08.19.22.16.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Aug 2019 22:16:28 -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=0AIXG9YRNIS5SGJJHqBAwB0d5gG3AkFqHgKovn1v0A8=; b=jGm8btlZtmYzK482t81+TS5yu3oAz9xjPAMVj6hoIBD6M9rfdyu+MYyIHtSagA1vcY MYdh1ud98aTebIYWJEvHpydM8aQATA+eB93TwJs9hQiF40KVLaGen5tZdMjIgc1TSNQE OLeE9hS6V5zkLSZFvYBONWLmvtLTJVopJiw9rwKeAjfyx2fikQBmZZfM/Vv5L/6rb41M QycDBsojS1HYTM5hHTPLj1TYedyJhrZI85bfAtewZNuv7ZXD+HTFZu6KsaeNnlOfb3Js REAbqHJbGYsFiLxuIk9UdNSS38c3L2cJZLBodxxnBgn7sifD0++pOMkLzDyQCUFkxbDU hymQ== X-Gm-Message-State: APjAAAXttYwA7yt+aO3u55tcpYaCGS4kmpcq/kF3DBUZ/ZjO3XAK/CqA ZQtknEHQw2dqj6X7uPTujGb0ppAcytTSbo+zI585Cf9D0Ywg8Dkr6UUZbl7x5+RcxOCmMyn0xDL uFWt0POit9++XPHw= X-Received: by 2002:a62:7641:: with SMTP id r62mr27039470pfc.201.1566278189225; Mon, 19 Aug 2019 22:16:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqyyFdwoHpQ1+vrz8r30/Titx8drXstHIaRhv8tHi75qTA5d9sEnL8MyQ//CmgkGPj3nIr/7Vw== X-Received: by 2002:a62:7641:: with SMTP id r62mr27039455pfc.201.1566278189068; Mon, 19 Aug 2019 22:16:29 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 20 Aug 2019 13:16:14 +0800 Message-Id: <20190820051615.1210-2-peterx@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190820051615.1210-1-peterx@redhat.com> References: <20190820051615.1210-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 v2 1/2] memory: Inherit has_coalesced_range from the same old FlatRange 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 previous has_coalesced_range counter has a problem 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). To fix it, we inherit the has_coalesced_range value from the old FlatRange to the new one if it's describing the identical range when updating the topology. Without this patch, MemoryListener.coalesced_io_del is hardly being called due to has_coalesced_range will be mostly zero in flat_range_coalesced_io_del() when topologies frequently change for the "memory" address space. Fixes: 3ac7d43a6fbb5d4a3 Suggested-by: Paolo Bonzini Signed-off-by: Peter Xu --- memory.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/memory.c b/memory.c index 8141486832..c53dcfc092 100644 --- a/memory.c +++ b/memory.c @@ -939,6 +939,15 @@ static void address_space_update_topology_pass(Address= Space *as, /* In both and unchanged (except logging may have changed) */ =20 if (adding) { + /* + * We must inherit the has_coalesced_range information + * if the new FlatRange is exactly the same as the old + * one, because it'll be used to conditionally call + * the coalesced mmio deletion listeners correctly in + * flat_range_coalesced_io_del() when the FlatRange + * needs to really go away. + */ + frnew->has_coalesced_range =3D frold->has_coalesced_range; MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_n= op); if (frnew->dirty_log_mask & ~frold->dirty_log_mask) { MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_= start, --=20 2.21.0 From nobody Sun Feb 8 09:05:54 2026 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=1566278287; cv=none; d=zoho.com; s=zohoarc; b=LgCMB4YiZFDyxvJbHChV/JQ3Xg6a1aM0gNQt7ac8w4tLPW250xcR++e48FPrEunJGGUaoA4IfAEJYXn3zl1CIynbN7tojlc7+AsZwYrV3ks5vyhoj/6+N3+IxuNT/UXCYpectg4l+XFhouP5VUU/Cr3UiPPZhJUkkZVl2euCRMA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566278287; 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=X5hShJJoARdqHFXBCVMHgCpojOVFbF2w5SC2VoId/EU=; b=ZUaiOmw+E20ORRMKiDP+ph+WAMyIJPr0V4A/HoWFStE5RvluvCDV8nlcRBN2ClorwiSr8C8XXFjfSj/WgIccT6iEd35C+jhy81Jd7+VEkeTZ4SCRqncS51tk+duwKBHluXh6Jp2j4jsyFSEcwVvP+0/Iqjzpp6Shh8LoQKhRQpk= 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 1566278287237777.5531415866612; Mon, 19 Aug 2019 22:18:07 -0700 (PDT) Received: from localhost ([::1]:33058 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hzwWz-0008VT-NM for importer@patchew.org; Tue, 20 Aug 2019 01:18:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57932) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hzwVW-0006ql-VB for qemu-devel@nongnu.org; Tue, 20 Aug 2019 01:16:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hzwVV-000500-Rr for qemu-devel@nongnu.org; Tue, 20 Aug 2019 01:16:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:15181) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hzwVV-0004zh-Ke for qemu-devel@nongnu.org; Tue, 20 Aug 2019 01:16:33 -0400 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.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 B976491764 for ; Tue, 20 Aug 2019 05:16:32 +0000 (UTC) Received: by mail-pg1-f199.google.com with SMTP id l12so3851313pgt.9 for ; Mon, 19 Aug 2019 22:16:32 -0700 (PDT) Received: from xz-x1.redhat.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id e6sm17462610pfn.71.2019.08.19.22.16.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Aug 2019 22:16:31 -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=r+wqbognQ8MrfKU7lUNMA5ZfpEUba5ufEKo6ZHWFaYs=; b=gnVDwDMyAU+c2X516P+WvhZNRo0hPHnxt3QGwE+SwoterZp6lmBThFMERhDHlmWFRK CNl3INoxjGIayVnMTFSC/F8heWhubODK+bgmyovgkeSTK0IVq8HqFJP5XPFXtPaxX6TL fTXzsrpND03PV7L6fnLUI2HSlZmI3+fnWXPeKfICLPfJaZ0znNKatA3PD8ljBNRDUmz9 goUR/VLypJkR2ypz5/vG0d3O8jNfllDVpe1up3mVvsBOFrPC5ZS3R79IjHRHXLYzANbh rVCQF+NZ5NbTvEJGe0JY5ex9yyc+DKtbIrB74mNw0+0aSpOvJS0D7l4IoQqL4SDjTTM3 fXrg== X-Gm-Message-State: APjAAAVyqTjtHgp2VTKlFZd0/d+RHtcxlI8R4gt73dWKKsBR9PwC/nxm WyDDlW0/0ygvujNJ7NYqPWaVOq1qKlffrb3WgEEWRTygGmdgZhGNyzici/8tmGBTyMsx6uhvIMC 6riQdZTz7wx9Qw08= X-Received: by 2002:a17:902:20cc:: with SMTP id v12mr24444600plg.188.1566278191956; Mon, 19 Aug 2019 22:16:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqy0LlzcFm81NBcBS6R/JCPOgYE4CSrCi6IrEy4ui1/NPHZCXnSDDI1f2EGmnyK2fASTPxLuAA== X-Received: by 2002:a17:902:20cc:: with SMTP id v12mr24444585plg.188.1566278191791; Mon, 19 Aug 2019 22:16:31 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 20 Aug 2019 13:16:15 +0800 Message-Id: <20190820051615.1210-3-peterx@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190820051615.1210-1-peterx@redhat.com> References: <20190820051615.1210-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 v2 2/2] 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" 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. This patch has nothing to do with 3ac7d43a6fbb5d4a3 because this is probably broken from the very beginning when the KVM_UNREGISTER_COALESCED_MMIO interface is introduced in kernel. However to make the backport to stables easier, I'm still using the commit 3ac7d43a6fbb5d4a3 to track this problem because this will depend on that otherwise even additions of mmio devices won't work. Fixes: 3ac7d43a6fbb5d4a3 Signed-off-by: Peter Xu --- memory.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/memory.c b/memory.c index c53dcfc092..7684b423f8 100644 --- a/memory.c +++ b/memory.c @@ -857,6 +857,9 @@ static void address_space_update_ioeventfds(AddressSpac= e *as) =20 static void flat_range_coalesced_io_del(FlatRange *fr, AddressSpace *as) { + CoalescedMemoryRange *cmr; + AddrRange tmp; + if (!fr->has_coalesced_range) { return; } @@ -865,9 +868,30 @@ static void flat_range_coalesced_io_del(FlatRange *fr,= AddressSpace *as) return; } =20 - MEMORY_LISTENER_UPDATE_REGION(fr, as, Reverse, coalesced_io_del, - int128_get64(fr->addr.start), - int128_get64(fr->addr.size)); + /* + * We split the big region into smaller ones to satisfy KVM's + * KVM_UNREGISTER_COALESCED_MMIO interface, where it does not + * allow to specify a large region to unregister all the devices + * under that zone instead it only accepts exact zones or even a + * smaller zone of previously registered mmio device. Logically + * speaking we should better fix KVM to allow the userspace to + * unregister multiple mmio devices within a large requested zone, + * but in all cases we'll still need to live with old kernels. So + * let's simply break the zones into exactly the small pieces when + * we do coalesced_io_add(). + */ + QTAILQ_FOREACH(cmr, &fr->mr->coalesced, link) { + tmp =3D addrrange_shift(cmr->addr, + int128_sub(fr->addr.start, + int128_make64(fr->offset_in_regio= n))); + if (!addrrange_intersects(tmp, fr->addr)) { + continue; + } + tmp =3D addrrange_intersection(tmp, fr->addr); + MEMORY_LISTENER_UPDATE_REGION(fr, as, Reverse, coalesced_io_del, + int128_get64(tmp.start), + int128_get64(tmp.size)); + } } =20 static void flat_range_coalesced_io_add(FlatRange *fr, AddressSpace *as) --=20 2.21.0