From nobody Sun May 19 08:26:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1677594397; cv=none; d=zohomail.com; s=zohoarc; b=dup9LTc0WhVgAS4TIdYhpgrWy7f0LuR3L1KVIXvsRyqt89NAXodbhspxFQDMzqfEZa5V1JNq3lb6DYIgD50RMcgVegeC7nA2I6iqYbnpIgNIXKZpa+I//lCZvWeXiaZVgJAnaa7vtv30v0luO6s1koUGZ0G9sY21PTT09eFt3/w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677594397; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=08YgNbuSOKnnEfbu6a+Dp7jZ/FE0T3YiPaHkjkuBMNo=; b=Q3l+vT2SepjIIn9RqKqpXK8nbYf8pkd7WesdyiSBw2jpQSH03goY8mpSGrDVkEzLDuje2XuzLWzfVvDJK/eEiFVlgqKMHqSMim/G0J5gJyTNlOBvOAYrL8G0lAWlisTmrGcMgzxhxH4JcjDa0ibblJLbg/PODpX1zMAAf8skgSg= ARC-Authentication-Results: i=1; mx.zohomail.com; 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 167759439789911.153392665825663; Tue, 28 Feb 2023 06:26:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pX0vg-0007be-74; Tue, 28 Feb 2023 09:26:08 -0500 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 1pX0vB-0007NC-Tm for qemu-devel@nongnu.org; Tue, 28 Feb 2023 09:25:44 -0500 Received: from szxga02-in.huawei.com ([45.249.212.188]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pX0v6-00049k-8V for qemu-devel@nongnu.org; Tue, 28 Feb 2023 09:25:35 -0500 Received: from kwepemi100025.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4PR02v61vhz9tDV; Tue, 28 Feb 2023 22:23:19 +0800 (CST) Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by kwepemi100025.china.huawei.com (7.221.188.158) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Tue, 28 Feb 2023 22:25:17 +0800 To: , , , , , CC: , , , , Longpeng Subject: [PATCH] memory: avoid unnecessary iteration when updating ioeventfds Date: Tue, 28 Feb 2023 22:25:14 +0800 Message-ID: <20230228142514.2582-1-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemi100025.china.huawei.com (7.221.188.158) X-CFilter-Loop: Reflected 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=45.249.212.188; envelope-from=longpeng2@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, 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: , Reply-to: "Longpeng(Mike)" From: "Longpeng(Mike)" via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1677594398675100001 Content-Type: text/plain; charset="utf-8" From: Longpeng When updating ioeventfds, we need to iterate all address spaces and iterate all flat ranges of each address space. There is so much redundant process that a FlatView would be iterated for so many times during one commit (memory_region_transaction_commit). We can mark a FlatView as UPDATED and then skip it in the next iteration and clear the UPDATED flag at the end of the commit. The overhead can be significantly reduced. For example, a VM with 16 vdpa net devices and each one has 65 vectors, can reduce the time spent on memory_region_transaction_commit by 95%. Signed-off-by: Longpeng --- include/exec/memory.h | 2 ++ softmmu/memory.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 2e602a2fad..974eabf765 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -1093,6 +1093,8 @@ struct FlatView { unsigned nr_allocated; struct AddressSpaceDispatch *dispatch; MemoryRegion *root; +#define FLATVIEW_FLAG_IOEVENTFD_UPDATED (1 << 0) + unsigned flags; }; =20 static inline FlatView *address_space_to_flatview(AddressSpace *as) diff --git a/softmmu/memory.c b/softmmu/memory.c index 9d64efca26..71ff996712 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -815,6 +815,15 @@ FlatView *address_space_get_flatview(AddressSpace *as) return view; } =20 +static void address_space_reset_view_flags(AddressSpace *as, unsigned mask) +{ + FlatView *view =3D address_space_get_flatview(as); + + if (view->flags & mask) { + view->flags &=3D ~mask; + } +} + static void address_space_update_ioeventfds(AddressSpace *as) { FlatView *view; @@ -825,6 +834,12 @@ static void address_space_update_ioeventfds(AddressSpa= ce *as) AddrRange tmp; unsigned i; =20 + view =3D address_space_get_flatview(as); + if (view->flags & FLATVIEW_FLAG_IOEVENTFD_UPDATED) { + return; + } + view->flags |=3D FLATVIEW_FLAG_IOEVENTFD_UPDATED; + /* * It is likely that the number of ioeventfds hasn't changed much, so = use * the previous size as the starting value, with some headroom to avoid @@ -833,7 +848,6 @@ static void address_space_update_ioeventfds(AddressSpac= e *as) ioeventfd_max =3D QEMU_ALIGN_UP(as->ioeventfd_nb, 4); ioeventfds =3D g_new(MemoryRegionIoeventfd, ioeventfd_max); =20 - view =3D address_space_get_flatview(as); FOR_EACH_FLAT_RANGE(fr, view) { for (i =3D 0; i < fr->mr->ioeventfd_nb; ++i) { tmp =3D addrrange_shift(fr->mr->ioeventfds[i].addr, @@ -1086,6 +1100,15 @@ void memory_region_transaction_begin(void) ++memory_region_transaction_depth; } =20 +static inline void address_space_update_ioeventfds_finish(void) +{ + AddressSpace *as; + + QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { + address_space_reset_view_flags(as, FLATVIEW_FLAG_IOEVENTFD_UPDATED= ); + } +} + void memory_region_transaction_commit(void) { AddressSpace *as; @@ -1106,12 +1129,14 @@ void memory_region_transaction_commit(void) } memory_region_update_pending =3D false; ioeventfd_update_pending =3D false; + address_space_update_ioeventfds_finish(); MEMORY_LISTENER_CALL_GLOBAL(commit, Forward); } else if (ioeventfd_update_pending) { QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { address_space_update_ioeventfds(as); } ioeventfd_update_pending =3D false; + address_space_update_ioeventfds_finish(); } } } @@ -3076,6 +3101,7 @@ void address_space_init(AddressSpace *as, MemoryRegio= n *root, const char *name) as->name =3D g_strdup(name ? name : "anonymous"); address_space_update_topology(as); address_space_update_ioeventfds(as); + address_space_reset_view_flags(as, FLATVIEW_FLAG_IOEVENTFD_UPDATED); } =20 static void do_address_space_destroy(AddressSpace *as) --=20 2.23.0