From nobody Fri Nov 7 13:04:08 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547734145965608.1264662515559; Thu, 17 Jan 2019 06:09:05 -0800 (PST) Received: from localhost ([127.0.0.1]:45506 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gk8Lw-0003xB-PM for importer@patchew.org; Thu, 17 Jan 2019 09:09:04 -0500 Received: from eggs.gnu.org ([209.51.188.92]:42543) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gk8E3-0007W9-PU for qemu-devel@nongnu.org; Thu, 17 Jan 2019 09:00:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gk8Dt-0002Qb-B7 for qemu-devel@nongnu.org; Thu, 17 Jan 2019 09:00:55 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:2172 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gk8Dr-0001mr-79 for qemu-devel@nongnu.org; Thu, 17 Jan 2019 09:00:44 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 9800F2660A04C23BC4D6; Thu, 17 Jan 2019 22:00:34 +0800 (CST) Received: from localhost (10.177.21.2) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.408.0; Thu, 17 Jan 2019 22:00:27 +0800 From: Zhuangyanying To: , , Date: Thu, 17 Jan 2019 13:55:31 +0000 Message-ID: <1547733331-16140-5-git-send-email-ann.zhuangyanying@huawei.com> X-Mailer: git-send-email 2.6.4.windows.1 In-Reply-To: <1547733331-16140-1-git-send-email-ann.zhuangyanying@huawei.com> References: <1547733331-16140-1-git-send-email-ann.zhuangyanying@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.21.2] X-CFilter-Loop: Reflected X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 45.249.212.190 Subject: [Qemu-devel] [PATCH 4/4] KVM: MMU: fast cleanup D bit based on fast write protect X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: liu.jinsong@huawei.com, wangxinxin.wang@huawei.com, Zhuang Yanying , qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Zhuang Yanying When live-migration with large-memory guests, vcpu may hang for a long time while starting migration, such as 9s for 2T (linux-5.0.0-rc2+qemu-3.1.0). The reason is memory_global_dirty_log_start() taking too long, and the vcpu is waiting for BQL. The page-by-page D bit clearup is the main time consumption. I think that the idea of "KVM: MMU: fast write protect" by xiaoguangrong, especially the function kvm_mmu_write_protect_all_pages(), is very helpful. After a little modifcation, on his patch, can solve this problem, 9s to 0.5s. At the beginning of live migration, write protection is only applied to the top-level SPTE. Then the write from vm trigger the EPT violation, with for_each_shadow_entry write protection is performed at dirct_map. Finally the Dirty bit of the target page(at level 1 page table) is cleared, and the dirty page tracking is started. Of coure, the page where GPA is located is marked dirty when mmu_set_spte. A similar implementation on xen, just emt instead of write protection. Signed-off-by: Zhuang Yanying --- arch/x86/kvm/mmu.c | 8 +++++--- arch/x86/kvm/vmx/vmx.c | 3 +-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index 047b897..a18bcc0 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -3257,7 +3257,10 @@ static bool mmu_load_shadow_page(struct kvm *kvm, st= ruct kvm_mmu_page *sp) break; =20 if (is_last_spte(spte, sp->role.level)) { - flush |=3D spte_write_protect(sptep, false); + if (sp->role.level =3D=3D PT_PAGE_TABLE_LEVEL) + flush |=3D spte_clear_dirty(sptep); + else + flush |=3D spte_write_protect(sptep, false); continue; } =20 @@ -6114,7 +6117,6 @@ void kvm_mmu_write_protect_all_pages(struct kvm *kvm,= bool write_protect) { u64 wp_all_indicator, kvm_wp_all_gen; =20 - mutex_lock(&kvm->slots_lock); wp_all_indicator =3D get_write_protect_all_indicator(kvm); kvm_wp_all_gen =3D get_write_protect_all_gen(wp_all_indicator); =20 @@ -6134,8 +6136,8 @@ void kvm_mmu_write_protect_all_pages(struct kvm *kvm,= bool write_protect) */ if (write_protect) kvm_reload_remote_mmus(kvm); - mutex_unlock(&kvm->slots_lock); } +EXPORT_SYMBOL_GPL(kvm_mmu_write_protect_all_pages); =20 static unsigned long mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f6915f1..5236a07 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7180,8 +7180,7 @@ static void vmx_sched_in(struct kvm_vcpu *vcpu, int c= pu) static void vmx_slot_enable_log_dirty(struct kvm *kvm, struct kvm_memory_slot *slot) { - kvm_mmu_slot_leaf_clear_dirty(kvm, slot); - kvm_mmu_slot_largepage_remove_write_access(kvm, slot); + kvm_mmu_write_protect_all_pages(kvm, true); } =20 static void vmx_slot_disable_log_dirty(struct kvm *kvm, --=20 1.8.3.1