From nobody Sun May 5 10:33:08 2024 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=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1677840033; cv=none; d=zohomail.com; s=zohoarc; b=XraNNPV6QjirJIesfI4ktAY6JZGx0+6bL8TPQMcXWc44ixB4v8nITdgh/M9D+Mi7ya8zUhDuvp6hOIfteRp/ZNy3shBuqgrW/jhBYcUKjAj1eMBzWnnkC57KHHWl67vZsvahwirPZl6geOzt/NSXboJtNaQ6CW6EYmK5SlKXvdA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677840033; 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; bh=z7tZa1mBhRjtuXfAzw01UoTlbuOJ1Zi3UEwNndiwjX8=; b=mImbodNOi3cKZHG9hqcOsSudIzvstAU7faEuaeuzkUnxuKTn0RAhL8DBR/EPYur44ukB+hwh5p7BTfB+Gw+A0wW83v3i6ulr+M5YLdD99mzYQgubdV4AP5VHp9JjOFJXlMajWUvcU21q1/mEH1m09zrWN9RV9PGGHSHSiI7UtLE= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677840032917635.6159141283283; Fri, 3 Mar 2023 02:40:32 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY2pS-0001qR-Tr; Fri, 03 Mar 2023 05:39:58 -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 1pY2pQ-0001pj-T2 for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:39:56 -0500 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pY2pP-0007wt-Al for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:39:56 -0500 Received: by mail-pf1-x432.google.com with SMTP id n5so1231158pfv.11 for ; Fri, 03 Mar 2023 02:39:47 -0800 (PST) Received: from n250-032-048.byted.org ([221.194.189.12]) by smtp.gmail.com with ESMTPSA id 6-20020aa79106000000b0059085684b54sm1316420pfh.140.2023.03.03.02.39.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 02:39:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1677839986; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=z7tZa1mBhRjtuXfAzw01UoTlbuOJ1Zi3UEwNndiwjX8=; b=Nf1adukXOvGtTyCHwUR6d8BeNrgi8vhO2Kk8ugkzs74jJYPDrXMRnNmY6jr7Mowoyi 4JCYmGOkjGLLEo7kQqoHinK7dh8h1KI85a7E27dRXPk7QeAz0fuoDhqghN4WIeSjj+93 Xt48uiHfGooU+yaLdt0njnmHm7wER6S7Yjkj/AVA2ge7pulWQzHc4e85qg5ji5vqSpyi 3qc2ZpF5/emQUphu0Nv4wDsymaPLJGxJEFGXNXo+LlUj7IymUsKXusnUPz+/mORfSMam G9luhrCgdJXSQP9bxNX6FVpljKbaeo+hslBCLTjJvodoG9QgZu++Un1ihWWkomf7WDhR m20w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677839986; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z7tZa1mBhRjtuXfAzw01UoTlbuOJ1Zi3UEwNndiwjX8=; b=ci3m2+kpkrDXVG4CsDxt26TQUlDmg60NII7e4+wFknGDfc8oQQ5uAmVHzjmYUdTayd KFZZFSvAq2ynZjsPFR6lO4eXCg8DSh+oHvEZu1kVuZpjf4eVlsZovC3CnKZsBqQ6ePk/ 2KgxZ7EpV+c9DiZxvNfJkBn6hX1NepZ3/LXLPBhR4UypaLaVCbVaDdIo2jhwG8fuHajB kv9W+C3Qas5O98nD30AKKFZf0ew2jHujltTcMDJ9amCI20lC8SeHHi7jsK/WnvWWhTXk 4W+WgCh20Yz45Cri+4f9Sr3EXYfqLel/kTEPVc952apm2RsCsmScWycL7Tk2XeEYj63p 9eVg== X-Gm-Message-State: AO0yUKUZA0pSA3tSK6w0s2CWQSMAzcDItd3kiDBr2Jj1iZAFJn5qnj0j AF0nVeuy4zpY0Flv7xCQczt1pJumNSkfu4qX X-Google-Smtp-Source: AK7set/f60K7hCFgkrvkVtPxhkOrit7ZpdWvolkcr0QjZFlP7sUqzpn94hS1M47+y7AgnY4d1crSBw== X-Received: by 2002:aa7:9ae9:0:b0:5a8:a973:450a with SMTP id y9-20020aa79ae9000000b005a8a973450amr1571532pfp.2.1677839986643; Fri, 03 Mar 2023 02:39:46 -0800 (PST) From: Chuang Xu To: qemu-devel@nongnu.org Cc: dgilbert@redhat.com, quintela@redhat.com, pbonzini@redhat.com, peterx@redhat.com, david@redhat.com, philmd@linaro.org, zhouyibo@bytedance.com Subject: [PATCH v6 1/5] memory: Reference as->current_map directly in memory commit Date: Fri, 3 Mar 2023 18:39:31 +0800 Message-Id: <20230303103935.370903-2-xuchuangxclwt@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> References: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::432; envelope-from=xuchuangxclwt@bytedance.com; helo=mail-pf1-x432.google.com X-Spam_score_int: -5 X-Spam_score: -0.6 X-Spam_bar: / X-Spam_report: (-0.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FROM_LOCAL_NOVOWEL=0.5, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.999, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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: 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 @bytedance.com) X-ZM-MESSAGEID: 1677840034767100003 Content-Type: text/plain; charset="utf-8" From: Peter Xu Calling RCU variance of address_space_get|to_flatview() during memory commit (flatview updates, triggering memory listeners, or updating ioeventfds, etc.) is not 100% accurate, because commit() requires BQL rather than RCU read lock, so the context exclusively owns current_map and can be directly referenced. Neither does it need a refcount to current_map because it cannot be freed from under the caller. Add address_space_get_flatview_raw() for the case where the context holds BQL rather than RCU read lock and use it across the core memory updates, Drop the extra refcounts on FlatView*. Signed-off-by: Peter Xu --- softmmu/memory.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/softmmu/memory.c b/softmmu/memory.c index 9d64efca26..213496802b 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -61,6 +61,13 @@ struct AddrRange { Int128 size; }; =20 +/* Called with BQL held */ +static inline FlatView *address_space_to_flatview_raw(AddressSpace *as) +{ + assert(qemu_mutex_iothread_locked()); + return as->current_map; +} + static AddrRange addrrange_make(Int128 start, Int128 size) { return (AddrRange) { start, size }; @@ -155,7 +162,7 @@ enum ListenerDirection { Forward, Reverse }; #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \ do { \ MemoryRegionSection mrs =3D section_from_flat_range(fr, \ - address_space_to_flatview(as)); \ + address_space_to_flatview_raw(as)); \ MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \ } while(0) =20 @@ -753,6 +760,7 @@ static FlatView *generate_memory_topology(MemoryRegion = *mr) } =20 static void address_space_add_del_ioeventfds(AddressSpace *as, + FlatView *view, MemoryRegionIoeventfd *fds_ne= w, unsigned fds_new_nb, MemoryRegionIoeventfd *fds_ol= d, @@ -774,7 +782,7 @@ static void address_space_add_del_ioeventfds(AddressSpa= ce *as, &fds_new[inew]))) { fd =3D &fds_old[iold]; section =3D (MemoryRegionSection) { - .fv =3D address_space_to_flatview(as), + .fv =3D view, .offset_within_address_space =3D int128_get64(fd->addr.sta= rt), .size =3D fd->addr.size, }; @@ -787,7 +795,7 @@ static void address_space_add_del_ioeventfds(AddressSpa= ce *as, &fds_old[iold])))= { fd =3D &fds_new[inew]; section =3D (MemoryRegionSection) { - .fv =3D address_space_to_flatview(as), + .fv =3D view, .offset_within_address_space =3D int128_get64(fd->addr.sta= rt), .size =3D fd->addr.size, }; @@ -833,7 +841,7 @@ 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); + view =3D address_space_to_flatview_raw(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, @@ -852,13 +860,12 @@ static void address_space_update_ioeventfds(AddressSp= ace *as) } } =20 - address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb, + address_space_add_del_ioeventfds(as, view, ioeventfds, ioeventfd_nb, as->ioeventfds, as->ioeventfd_nb); =20 g_free(as->ioeventfds); as->ioeventfds =3D ioeventfds; as->ioeventfd_nb =3D ioeventfd_nb; - flatview_unref(view); } =20 /* @@ -1026,7 +1033,7 @@ static void flatviews_reset(void) =20 static void address_space_set_flatview(AddressSpace *as) { - FlatView *old_view =3D address_space_to_flatview(as); + FlatView *old_view =3D address_space_to_flatview_raw(as); MemoryRegion *physmr =3D memory_region_get_flatview_root(as->root); FlatView *new_view =3D g_hash_table_lookup(flat_views, physmr); =20 --=20 2.20.1 From nobody Sun May 5 10:33:08 2024 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=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1677840084; cv=none; d=zohomail.com; s=zohoarc; b=W9t0rfieH/Tro/pNt/0PTYE0E3aP6JYhoLmaYpSQOkQnwWzkZJqDRJb1XBQ9VQ0G8SKAAjcWfTkkuvrnvPKbFUzYChMgh2CoqEv0fuTT19r4u5FJ+NDgyNWCGCZ1guftoBL2T3bXBhgDjWWP/02c9EoaWl6hhPuK4sESenKMJgE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677840084; 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; bh=oybiXPgSNhTbwAYyTDdPbCgWTqNqgxckr+HdWHzShIw=; b=NEodcWOd1FmK78gys9AEKnff7eR8dE3LXK4xYPdmsl6/mjvIkENiOTXZz12UXGzFOxm6S/DOvOEbVhSXVyMwhJ6fL3MWc7gq2oKl4MbxtvCD3p8Suvuas0OpeKc7P4/v6Wg00EWekUsu3FHxsua5UC22GAZ/l57NPHbgkFowIBo= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677840084939441.54987653293074; Fri, 3 Mar 2023 02:41:24 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY2pT-0001qV-Lz; Fri, 03 Mar 2023 05:39:59 -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 1pY2pR-0001q4-Vq for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:39:58 -0500 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pY2pQ-0007wy-3Q for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:39:57 -0500 Received: by mail-pl1-x631.google.com with SMTP id i10so2217716plr.9 for ; Fri, 03 Mar 2023 02:39:50 -0800 (PST) Received: from n250-032-048.byted.org ([221.194.189.12]) by smtp.gmail.com with ESMTPSA id 6-20020aa79106000000b0059085684b54sm1316420pfh.140.2023.03.03.02.39.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 02:39:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1677839989; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oybiXPgSNhTbwAYyTDdPbCgWTqNqgxckr+HdWHzShIw=; b=JwN5AT7baeX/ppTBGFiXmTutZ5s8cgKWbf8eR9wLQwbmHquHpd0GrnnYtd2hOFthue 5lX+Uhw8oMeejb/Nrio3NJSmIeoe0PfKVlAbFdnKJX0l/EBVnBM8hjrWkYJHsSmHd6fD t1Sy76mEeMxZl8ciLbUuC6tTAYWMEgFC+U1/s7dbjfRsBJEIKvNd9hUsU9/eWEkYpEAY 8gXWATftxNOfnmEcFp7e/Rn85fpyU5FuNQGLXzyCuUw9gEoSYHA7RKSLTv60i3vlmTCG mPPDZiUMfKZDVOPXw1ADojwzn5YONMWxUdWwyED1C4iqQIcOh+lmfjByom2Ko4O0vnvo BhZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677839989; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oybiXPgSNhTbwAYyTDdPbCgWTqNqgxckr+HdWHzShIw=; b=Tku131FC6WlfMcUFn0CkDoZv0UA7Xx3IJY6p0xvS2/wYsJQ+rCJPC0g68DoBSOPrma 632HZcwDxYdmLyW2wWvOyxQvcgYF+ocR5bvV13/Nos35WpkcoHjMd+S8xIL06tU+ifa+ JiapiaQv5X+8pFrJgKAxm2TEJMpDWe7pf1tIInEpL8EqYuhLJ9Vgl8MZiw0PX60CNfVk zAZPelxZnPesOztEjFv74cUr0ZpyNu5Y+W+8iFEs7LMkcbxqEp5uYED3ZXXIW4QaxZjP G/HvfgWKlMo+NWLBElCT0EuUL5x6dVkdAXQelrQs7xs+PYmAxOhG87/7Y/upOOQM9t0V aJ8Q== X-Gm-Message-State: AO0yUKWudTr9P3SorG+lJtf8Dhac9zIEmY5dih387E1DY+GfA7SMXs9p XOEDeuKO/BgHNUGBgy/sowUjzVAJPdS5eme3 X-Google-Smtp-Source: AK7set8MHj30gQCcrkkdpyJ+7PDKXZhIHyC7M7C1OY1ABGzGJvg15ml1LkgEo/qlZ4CL3wJECEEb9g== X-Received: by 2002:a05:6a20:507:b0:cb:867d:1592 with SMTP id 7-20020a056a20050700b000cb867d1592mr2360743pzp.4.1677839989287; Fri, 03 Mar 2023 02:39:49 -0800 (PST) From: Chuang Xu To: qemu-devel@nongnu.org Cc: dgilbert@redhat.com, quintela@redhat.com, pbonzini@redhat.com, peterx@redhat.com, david@redhat.com, philmd@linaro.org, zhouyibo@bytedance.com, Chuang Xu Subject: [PATCH v6 2/5] rcu: Introduce rcu_read_is_locked() Date: Fri, 3 Mar 2023 18:39:32 +0800 Message-Id: <20230303103935.370903-3-xuchuangxclwt@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> References: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::631; envelope-from=xuchuangxclwt@bytedance.com; helo=mail-pl1-x631.google.com X-Spam_score_int: -5 X-Spam_score: -0.6 X-Spam_bar: / X-Spam_report: (-0.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FROM_LOCAL_NOVOWEL=0.5, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.999, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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: 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 @bytedance.com) X-ZM-MESSAGEID: 1677840086853100003 Content-Type: text/plain; charset="utf-8" Add rcu_read_is_locked() to detect holding of rcu lock. Signed-off-by: Chuang Xu --- include/qemu/rcu.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/qemu/rcu.h b/include/qemu/rcu.h index b063c6fde8..719916d9d3 100644 --- a/include/qemu/rcu.h +++ b/include/qemu/rcu.h @@ -119,6 +119,13 @@ static inline void rcu_read_unlock(void) } } =20 +static inline bool rcu_read_is_locked(void) +{ + struct rcu_reader_data *p_rcu_reader =3D get_ptr_rcu_reader(); + + return p_rcu_reader->depth > 0; +} + extern void synchronize_rcu(void); =20 /* --=20 2.20.1 From nobody Sun May 5 10:33:08 2024 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=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1677840059; cv=none; d=zohomail.com; s=zohoarc; b=WFk+5CHM9GqjxtU2HBNQccZx6HDOcSeG5qD0UpzE/HSTjYYGwpx6VNLuHGiCeqjbC3QShNd/grvBZATaC9qX4O0g3ANJmJtjGR9o4DNVTO1Hdf9JfJB7Jl7GZvI7gCX6H9ri11tnOuw5NNNU5bBg0OtWw+ZI7zQXILb27aJSX1E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677840059; 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; bh=Hz+uaABV0KE9Rq3NUQUeNyj98Vrl/3eewrNbYhH9fiA=; b=Bjb0ffAuFyL22fdZ21+RoFWYi6wW6BLrU0JdZUsl5BFtKOEvMpnl05tQMaK5SCvgZLR8iX+SiEtovNbVWyoQRWxB9pvcGbhWhfOKXVsi1d8kTH+gFPGafjWwu4SnKKMlvS+NYw3/Yl68tt1d8aZTLrP556zXbVD3wIXqSUzPjg8= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677840059111964.3965562743974; Fri, 3 Mar 2023 02:40:59 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY2pQ-0001pP-1p; Fri, 03 Mar 2023 05:39:56 -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 1pY2pP-0001p0-5P for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:39:55 -0500 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pY2pN-0007xP-IA for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:39:54 -0500 Received: by mail-pg1-x52a.google.com with SMTP id 132so1157378pgh.13 for ; Fri, 03 Mar 2023 02:39:53 -0800 (PST) Received: from n250-032-048.byted.org ([221.194.189.12]) by smtp.gmail.com with ESMTPSA id 6-20020aa79106000000b0059085684b54sm1316420pfh.140.2023.03.03.02.39.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 02:39:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1677839992; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Hz+uaABV0KE9Rq3NUQUeNyj98Vrl/3eewrNbYhH9fiA=; b=YBab7g7D+wahBU0Yfqbthai/q2Fg1PdmM92dz7cetuf93PfIVYg9z5hAbaRqdIa0bf 9qW8mZKSczr6E6sES56mGc82OeSVSt+iAQePPRnZSUM3Ak1mPBG+UDtwNm4M9nDtL4UA ZTBzlt7KxmT43L2eP9urr5AVZTWdzshOUytRgg/mjkNeCIQCchUj2cUp6vJKri4PaDTv oY2TdvQxN0vYw4QJr6wqc0sKWT9tLfGdOQg023GXhs9JCrCoZ47pgH6HQ44s77Iww7RE ZRf4k89Dj9drFY+tRxX/XTdWxP+VLDUZq0DovKL4hr7JZ3tYDbyNCuZl6a7lEKwS8Tdm nHDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677839992; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Hz+uaABV0KE9Rq3NUQUeNyj98Vrl/3eewrNbYhH9fiA=; b=otq9pyZ5WoLRGZLsRP59z5kJ01m+oY2f9H4H9LqVWTZ5SZVTahvhuOjK1TBf+KYijM DY7g/9wDGESWJ5HM7NRbTQ3eGQVcORaFLnxDH+DYGMY0W4Gla/zDpsBlgc6ULc3sGKgh FqDY9ugoJjaxnlq1hZ68d+7OXZWZLJ6TSL48pAqOdQCqgwRRlCTBkJM6lszC4nZ4Pkwn 31TlAF6sFbpuqXHWOD+4g6Xbo+SdSjnwSAKJJQUzBjVW2hSC2mO/WpEPVWryIs+jPOvA 13dskQshDeSi3sVw+zBPnJee7/N+82Gl+9C+SBB8qgzUX+MAiR28z/yH/VKaIbtnha78 duAg== X-Gm-Message-State: AO0yUKVBMkSvIU2e7W1UFcLAb6tG8eK0+BRJIUsszJNX5Dj0wkBBYtjH bSInMenyq5iizmHSc8dgI2F4RPKQoFEohzV/ X-Google-Smtp-Source: AK7set/18Nr/UEttfiax9PhZ2jVOMaMM8wJcV1wdoP+g655I/cWG4ljgVTpgNHJ8M29Jo2pIYVDy6g== X-Received: by 2002:a62:7905:0:b0:5a9:c75f:bcc4 with SMTP id u5-20020a627905000000b005a9c75fbcc4mr1762204pfc.25.1677839991995; Fri, 03 Mar 2023 02:39:51 -0800 (PST) From: Chuang Xu To: qemu-devel@nongnu.org Cc: dgilbert@redhat.com, quintela@redhat.com, pbonzini@redhat.com, peterx@redhat.com, david@redhat.com, philmd@linaro.org, zhouyibo@bytedance.com, Chuang Xu Subject: [PATCH v6 3/5] memory: Introduce memory_region_transaction_do_commit() Date: Fri, 3 Mar 2023 18:39:33 +0800 Message-Id: <20230303103935.370903-4-xuchuangxclwt@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> References: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::52a; envelope-from=xuchuangxclwt@bytedance.com; helo=mail-pg1-x52a.google.com X-Spam_score_int: -5 X-Spam_score: -0.6 X-Spam_bar: / X-Spam_report: (-0.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FROM_LOCAL_NOVOWEL=0.5, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.999, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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: 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 @bytedance.com) X-ZM-MESSAGEID: 1677840060700100003 Content-Type: text/plain; charset="utf-8" Split memory_region_transaction_do_commit() from memory_region_transaction_commit(). We'll call do_commit() in address_space_to_flatview() in the later patch. Signed-off-by: Chuang Xu --- softmmu/memory.c | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/softmmu/memory.c b/softmmu/memory.c index 213496802b..b89abf400e 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1093,34 +1093,41 @@ void memory_region_transaction_begin(void) ++memory_region_transaction_depth; } =20 -void memory_region_transaction_commit(void) +void memory_region_transaction_do_commit(void) { AddressSpace *as; =20 - assert(memory_region_transaction_depth); assert(qemu_mutex_iothread_locked()); =20 - --memory_region_transaction_depth; - if (!memory_region_transaction_depth) { - if (memory_region_update_pending) { - flatviews_reset(); + if (memory_region_update_pending) { + flatviews_reset(); =20 - MEMORY_LISTENER_CALL_GLOBAL(begin, Forward); + MEMORY_LISTENER_CALL_GLOBAL(begin, Forward); =20 - QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { - address_space_set_flatview(as); - address_space_update_ioeventfds(as); - } - memory_region_update_pending =3D false; - ioeventfd_update_pending =3D false; - 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; + QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { + address_space_set_flatview(as); + address_space_update_ioeventfds(as); + } + memory_region_update_pending =3D false; + ioeventfd_update_pending =3D false; + 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; + } +} + +void memory_region_transaction_commit(void) +{ + assert(memory_region_transaction_depth); + assert(qemu_mutex_iothread_locked()); + + --memory_region_transaction_depth; + if (!memory_region_transaction_depth) { + memory_region_transaction_do_commit(); + } } =20 static void memory_region_destructor_none(MemoryRegion *mr) --=20 2.20.1 From nobody Sun May 5 10:33:08 2024 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=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1677840035; cv=none; d=zohomail.com; s=zohoarc; b=TBX9qCtdkgBODbfYj36lOKuuKjJyZXcZVO62nZpRl0u5l88BC/q99Y9uUeHKK2J08uPdb39XclkKgaWasi5hqubVDnejdkZmCcIeFcdbmgMpW7RQx9UMJKIilx/jmSAfpBd7jhpOsc3KTb8VbzWTIs7YChYCMLs4reqacf8cYZ0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677840035; 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; bh=OioqYVW2wWPI0FbDvxDEk2G5WH+pqlZIjQGBZIZ5kBI=; b=PGd7/hZPPnk/qNx99/AL2BntGc3odsQuM9Kl2L95Pz9UZfag3kbeK0GyXsYypP5cIr0GvqIVTJAjyKj96usLRrpexnMoGeOKSuoH2AzPqM4X5r7XpOtiOherE53OlHJ5t1SxK709pvzKQRgRVtWeb07jzJqtmXwaAoYc0vPzeos= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677840035151556.6713738162555; Fri, 3 Mar 2023 02:40:35 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY2pU-0001qy-1G; Fri, 03 Mar 2023 05:40:00 -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 1pY2pR-0001pv-If for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:39:57 -0500 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pY2pQ-0007xn-26 for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:39:57 -0500 Received: by mail-pg1-x52c.google.com with SMTP id h31so1179005pgl.6 for ; Fri, 03 Mar 2023 02:39:55 -0800 (PST) Received: from n250-032-048.byted.org ([221.194.189.12]) by smtp.gmail.com with ESMTPSA id 6-20020aa79106000000b0059085684b54sm1316420pfh.140.2023.03.03.02.39.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 02:39:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1677839994; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OioqYVW2wWPI0FbDvxDEk2G5WH+pqlZIjQGBZIZ5kBI=; b=fs5ed9rg9IQRfHH8aUOa5aRpjkMO7JTghTLb10uh9tMNK8vBcQzUzqyBbNCEybuHLj z0lEJc6GDiHAr5/0RJjsPPYaVbSUWHpIAsSEjpQPlK83hLkTOZRK41Ei1ubu/Jlz0j3B H0ViHvUjC2Wq0uvOAzlKBF2Gz7lepoxUvKkcUS22AkMSWYJvsD1o4w0AJIAkPGyELwIf k5AHzYY6ShXhOeVnCwv5h+vCyHDkdliZa2bBbDQRchgvSaEsSzVg2EFgNcfe/2pWPi8b QMXocWIkjOic34CmvisGxKVNDzL3gQ52GieohTcVMbg1u59JWVbtVRy/XKtFhto3Wg15 7mlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677839994; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OioqYVW2wWPI0FbDvxDEk2G5WH+pqlZIjQGBZIZ5kBI=; b=4/bVTZCALZz21vxGhShMRHUYttev5CRzEK8TssfPhL17XbmT9n8cdxUsq43eB/aEWW FJfpBhiYy7pB49CdQMw8mV9dI78/xUKXk86EC8EoVHiUIVTh2aYK/BIOIBfc9TXN2DcM 63SevbiyXZbWKAD9rL8shWJphX/rlqjE6e3ijz3m9Nhan9Mq9U+hWQBREzvlF3FMJS/+ H8kGUkGwYIDT/3HAtRGEnjpwH43qGyVjpTC3LnU618SZw2R0WMLDLsfsi0AAbrbbBsCl xZBCIPMoC5rKEpBpAdIwVm7q0vQ3SgGJR0ch3wxUHAtXrdP/w8HK1nEbICmNcenkO/L9 +Vzg== X-Gm-Message-State: AO0yUKUKiCM5PpLmB1g+RkJGGhcET76w8Ft1KjKYkzK7HYo1/zml7NSC NVFv/4/p5tvBW2Y8Sb5dc0xldbMhNzfhMaeJ X-Google-Smtp-Source: AK7set8LTew48cQ1vazfdCLhuDe/0mRaIQk5s/ovp7hjgTRKPABukptJpkiYtZbDxIvTN+t3pyifIQ== X-Received: by 2002:a62:1758:0:b0:5dd:3f84:7d7d with SMTP id 85-20020a621758000000b005dd3f847d7dmr1653124pfx.15.1677839994578; Fri, 03 Mar 2023 02:39:54 -0800 (PST) From: Chuang Xu To: qemu-devel@nongnu.org Cc: dgilbert@redhat.com, quintela@redhat.com, pbonzini@redhat.com, peterx@redhat.com, david@redhat.com, philmd@linaro.org, zhouyibo@bytedance.com, Chuang Xu Subject: [PATCH v6 4/5] memory: Add sanity check in address_space_to_flatview Date: Fri, 3 Mar 2023 18:39:34 +0800 Message-Id: <20230303103935.370903-5-xuchuangxclwt@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> References: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::52c; envelope-from=xuchuangxclwt@bytedance.com; helo=mail-pg1-x52c.google.com X-Spam_score_int: -5 X-Spam_score: -0.6 X-Spam_bar: / X-Spam_report: (-0.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FROM_LOCAL_NOVOWEL=0.5, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.999, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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: 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 @bytedance.com) X-ZM-MESSAGEID: 1677840036637100010 Content-Type: text/plain; charset="utf-8" Before using any flatview, sanity check whether BQL or rcu is held. And if we're during a memory region transaction, try to immediately update mappings, or the map can be invalid. Signed-off-by: Chuang Xu --- include/exec/memory.h | 23 +++++++++++++++++++++++ softmmu/memory.c | 5 +++++ 2 files changed, 28 insertions(+) diff --git a/include/exec/memory.h b/include/exec/memory.h index 2e602a2fad..84b531c6ff 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -27,6 +27,7 @@ #include "qemu/notify.h" #include "qom/object.h" #include "qemu/rcu.h" +#include "qemu/main-loop.h" =20 #define RAM_ADDR_INVALID (~(ram_addr_t)0) =20 @@ -1095,8 +1096,30 @@ struct FlatView { MemoryRegion *root; }; =20 +bool memory_region_transaction_in_progress(void); + +void memory_region_transaction_do_commit(void); + static inline FlatView *address_space_to_flatview(AddressSpace *as) { + if (qemu_mutex_iothread_locked()) { + /* We exclusively own the flatview now.. */ + if (memory_region_transaction_in_progress()) { + /* + * Fetch the flatview within a transaction in-progress, it + * means current_map may not be the latest, we need to update + * immediately to make sure the caller won't see obsolete + * mapping. + */ + memory_region_transaction_do_commit(); + } + + /* No further protection needed to access current_map */ + return as->current_map; + } + + /* Otherwise we must have had the RCU lock or something went wrong */ + assert(rcu_read_is_locked()); return qatomic_rcu_read(&as->current_map); } =20 diff --git a/softmmu/memory.c b/softmmu/memory.c index b89abf400e..1834e14cc8 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1130,6 +1130,11 @@ void memory_region_transaction_commit(void) } } =20 +bool memory_region_transaction_in_progress(void) +{ + return memory_region_transaction_depth !=3D 0; +} + static void memory_region_destructor_none(MemoryRegion *mr) { } --=20 2.20.1 From nobody Sun May 5 10:33:08 2024 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=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1677840035; cv=none; d=zohomail.com; s=zohoarc; b=UL2FOVUE4niryVFvhw1Khz5Nf2ARDsAFqbskOlMXprq7c5FsW5o0u2VZTfQDhXATuo2ieEUntp4JDLlcS0ncKbjqUHuxgSY4+jt9a9N76GXJ2YTgJYfTX0eMAJYiEuSFXgl2K1F8dvRybA0d4v22PDFB1JY+Wfbe3hqUqVOOXfc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677840035; 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; bh=tVlZ/xZyNlPE7qiZ1njM6/w0Wck5zKTu3W5xPbj3hMg=; b=Cqw4Fi/XlQ+ULi6KZFHWuwP1cCuHJ9zeLgsddo7+0F83KxLLQT0ku/IhSCDleHZkP8PjlmVkUZCX2BaIO7ybRPRDVopESagrOZY59UzJIM1dhIhpKkLdAbxDmya04cChxUh3TNlQvzwsBxUg0RonBUodke0LhweUHZ0BeCsSeVc= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677840035637309.6730240541775; Fri, 3 Mar 2023 02:40:35 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY2pV-0001rA-IO; Fri, 03 Mar 2023 05:40:01 -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 1pY2pU-0001r1-Bq for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:40:00 -0500 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pY2pS-0007yH-Px for qemu-devel@nongnu.org; Fri, 03 Mar 2023 05:40:00 -0500 Received: by mail-pg1-x531.google.com with SMTP id q189so1169906pga.9 for ; Fri, 03 Mar 2023 02:39:58 -0800 (PST) Received: from n250-032-048.byted.org ([221.194.189.12]) by smtp.gmail.com with ESMTPSA id 6-20020aa79106000000b0059085684b54sm1316420pfh.140.2023.03.03.02.39.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 02:39:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1677839997; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tVlZ/xZyNlPE7qiZ1njM6/w0Wck5zKTu3W5xPbj3hMg=; b=JmBQGWWTMPEvPGeupLssiuhDTfAEK3V56Y+1T6+eYGhwXB7GJ0kLnqoSewJYz/zvDO 6Mqg3T7VsVAJNBthVvmEBoKc8Xbz7kiPmTV+TS0zC12fGTTmG5ahy1yq1jk+B9DP69ZH Io7D9YfL0kjcamk3zS8UbQt3JMwrZPFn10q3H0uc3J3eZopApT/3PIS+QiHbAwmN2Bd3 vLo0Hsv54aGseUJXhVbZDuHM9gSNMQ5VVe+oiLtU4fHhyhQ9YvzIfegF743m2GS9FWJ2 b7cdd0oeh7iRobEHA6XjiVYt9sHYh4vjoOYXEPzfcnmnKCajC7kuZXv+6sM3g7E4zd8L vohw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677839997; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tVlZ/xZyNlPE7qiZ1njM6/w0Wck5zKTu3W5xPbj3hMg=; b=ascErboYaojv9c3nHczOuWEBFRNtauvfrypEiYLZuD40J+niZwMB+nrv8Hjt+dOAXD t5fNSVzhmTJ0WDCYbATsOGrdpRW9YSc00Sq4Bbq1iMa0zcLjqgrIfhhAo3zR3E1quyzR ykfcsiyhE3InmqxUJcs1P/6r0qNbhM4zFKRtxEn15mwqdCbps5ckV7tZkFThP2J0o6gA +9gfxTyq0+tKGez02qDjOUanmrgToGG9bb9lOBCJe3izvjntAtR7pYX910xmMSSh7dAK NLmW4VeNpM6gb6/GmXH/7JM6Ep2UweY/XCUV43xBegx8EsBuuHEWrjA5DKeVEAZzmTxx 8A7w== X-Gm-Message-State: AO0yUKUN0kF6GMcAp70kSY5iVTY2QH2iGSERMnnj5fmzj+4CbGeFoSVB nH+5zED+zS05FKYCjCXQqv/ACGjrzlabSjGd X-Google-Smtp-Source: AK7set+iquhi/AeSquBxg6Y2vZ/vECBOKZsqWihJ+pv3O8nNyT6OYLCojC5vNiTQ6MVj9IbS9/K+Iw== X-Received: by 2002:a62:5243:0:b0:5dc:6dec:e9d0 with SMTP id g64-20020a625243000000b005dc6dece9d0mr1704783pfb.3.1677839997367; Fri, 03 Mar 2023 02:39:57 -0800 (PST) From: Chuang Xu To: qemu-devel@nongnu.org Cc: dgilbert@redhat.com, quintela@redhat.com, pbonzini@redhat.com, peterx@redhat.com, david@redhat.com, philmd@linaro.org, zhouyibo@bytedance.com, Chuang Xu Subject: [PATCH v6 5/5] migration: Reduce time of loading non-iterable vmstate Date: Fri, 3 Mar 2023 18:39:35 +0800 Message-Id: <20230303103935.370903-6-xuchuangxclwt@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> References: <20230303103935.370903-1-xuchuangxclwt@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::531; envelope-from=xuchuangxclwt@bytedance.com; helo=mail-pg1-x531.google.com X-Spam_score_int: -5 X-Spam_score: -0.6 X-Spam_bar: / X-Spam_report: (-0.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FROM_LOCAL_NOVOWEL=0.5, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.999, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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: 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 @bytedance.com) X-ZM-MESSAGEID: 1677840036607100009 Content-Type: text/plain; charset="utf-8" The duration of loading non-iterable vmstate accounts for a significant portion of downtime (starting with the timestamp of source qemu stop and ending with the timestamp of target qemu start). Most of the time is spent committing memory region changes repeatedly. This patch packs all the changes to memory region during the period of loading non-iterable vmstate in a single memory transaction. With the increase of devices, this patch will greatly improve the performance. Here are the test1 results: test info: - Host - Intel(R) Xeon(R) Platinum 8260 CPU - NVIDIA Mellanox ConnectX-5 - VM - 32 CPUs 128GB RAM VM - 8 16-queue vhost-net device - 16 4-queue vhost-user-blk device. time of loading non-iterable vmstate downtime before about 150 ms 740+ ms after about 30 ms 630+ ms In test2, we keep the number of the device the same as test1, reduce the number of queues per device: Here are the test2 results: test info: - Host - Intel(R) Xeon(R) Platinum 8260 CPU - NVIDIA Mellanox ConnectX-5 - VM - 32 CPUs 128GB RAM VM - 8 1-queue vhost-net device - 16 1-queue vhost-user-blk device. time of loading non-iterable vmstate downtime before about 90 ms about 250 ms after about 25 ms about 160 ms In test3, we keep the number of queues per device the same as test1, reduce the number of devices: Here are the test3 results: test info: - Host - Intel(R) Xeon(R) Platinum 8260 CPU - NVIDIA Mellanox ConnectX-5 - VM - 32 CPUs 128GB RAM VM - 1 16-queue vhost-net device - 1 4-queue vhost-user-blk device. time of loading non-iterable vmstate downtime before about 20 ms about 70 ms after about 11 ms about 60 ms As we can see from the test results above, both the number of queues and the number of devices have a great impact on the time of loading non-iterab= le vmstate. The growth of the number of devices and queues will lead to more mr commits, and the time consumption caused by the flatview reconstruction will also increase. Signed-off-by: Chuang Xu --- migration/savevm.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/migration/savevm.c b/migration/savevm.c index b5e6962bb6..3dd9daabd8 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -2770,6 +2770,7 @@ out: goto retry; } } + return ret; } =20 @@ -2795,7 +2796,25 @@ int qemu_loadvm_state(QEMUFile *f) =20 cpu_synchronize_all_pre_loadvm(); =20 + /* + * Call memory_region_transaction_begin() before loading vmstate. + * This call is paired with memory_region_transaction_commit() at + * the end of qemu_loadvm_state_main(), in order to pack all the + * changes to memory region during the period of loading + * non-iterable vmstate in a single memory transaction. + * This operation will reduce time of loading non-iterable vmstate + */ + memory_region_transaction_begin(); + ret =3D qemu_loadvm_state_main(f, mis); + + /* + * Call memory_region_transaction_commit() after loading vmstate. + * At this point, qemu actually completes all the previous memory + * region transactions. + */ + memory_region_transaction_commit(); + qemu_event_set(&mis->main_thread_load_event); =20 trace_qemu_loadvm_state_post_main(ret); --=20 2.20.1