Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2417727pxb; Sat, 30 Jan 2021 01:52:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJy9CJAKms/2DS1FkFCba0RlQVuOENGgGidft87yTzAJc++k1XsiYv+3CFoKZv8R2dNh+iNH X-Received: by 2002:a17:906:5618:: with SMTP id f24mr8305220ejq.517.1612000365453; Sat, 30 Jan 2021 01:52:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612000365; cv=none; d=google.com; s=arc-20160816; b=kQ5qOBivhHgHw+jXR6czD51M2C6UCrXpa4HV6MF4LmU4t/Tqz62ijRB0okuAGCvsd7 TAhvP2us74pBquN6akGblAmWdkh6hJggmteOcfANatcJfyZi5mPr5sh1fTTy7KzYkOMC xH6qDE+eVasJbnpZfT7p8JPsm5lTmsaY/YCjboARv8PE27NSuDHciMgR/7zlkB2Fi/Oo wBRoXVBXjXgpEmcPU7+fYEp38eQejIaZ61PmUYls8oKytZ8fLWjEaLLyKAU4P+Cj77J0 bQ9rriOtvRnVt8wf/uOeMvQJPZymtm8SfCaV9UtPEPB2DuyVb9+TblHsUN4MrMPX9z4N SRGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :ironport-sdr:ironport-sdr; bh=HEYCUEhk2SCnwt51wlwC34y1/TaDJZ/4X1ATLCr+wEI=; b=BvSDIrEXyAm5fSHot+dY2mYDxrec3TgzQDv0DrikDJjAJo4PECvQxn0orF2wPfuM7Y qmAi4Qb5gYZSSclsiFKMKGdFxKgI6K2X9u9+BgZtg5x8Zvea9f8TT4vaJZZ0fRZGC1e0 NC852n4H0vbSKcB/POlz3rC+crSOY+dIKxcv7Pt/LAttLPp0yVFoKEpEEoy5vWiRsgdi prGu7wwKfYQU3gmBWxOn62M0NN04BQ2aasX4++nLTt8vQEkwRiYyNLWlP5QVfjloJ8+j Z1gJE7XTKh20DIbmYMbmAed/78KCm9iOIxQP37RRL9IVGX7nMQ7FvZYT2uuhK6LRO4I+ MUBw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hp13si6293971ejc.593.2021.01.30.01.52.20; Sat, 30 Jan 2021 01:52:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231857AbhA3Jtd (ORCPT + 99 others); Sat, 30 Jan 2021 04:49:33 -0500 Received: from mga01.intel.com ([192.55.52.88]:47571 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233324AbhA3DDH (ORCPT ); Fri, 29 Jan 2021 22:03:07 -0500 IronPort-SDR: gyvrUS7eTgmBlCoKH6CvmDtaFFLTYQvoinMmf6di4TTOpgqzzffqD/UiIymslTik9juygwP9gy DqzHW6Z42t4Q== X-IronPort-AV: E=McAfee;i="6000,8403,9879"; a="199360067" X-IronPort-AV: E=Sophos;i="5.79,387,1602572400"; d="scan'208";a="199360067" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Jan 2021 19:00:58 -0800 IronPort-SDR: dv3SZExTmS40Bf0f/fm7IPUYwItnOjxkQaqzsKTDHCoT11S3zc2XL+iPhUjIaITTSI2IyOIT/C iLvBjuWlMXcQ== X-IronPort-AV: E=Sophos;i="5.79,387,1602572400"; d="scan'208";a="431275930" Received: from zhiyuanh-mobl.ccr.corp.intel.com (HELO localhost) ([10.249.169.213]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Jan 2021 19:00:54 -0800 Date: Sat, 30 Jan 2021 11:00:52 +0800 From: Yu Zhang To: Sean Christopherson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yu Zhang Subject: Re: [PATCH] KVM: x86/mmu: Remove the defunct update_pte() paging hook Message-ID: <20210130030052.dfktlebfrurkxqov@linux.intel.com> References: <20210115004051.4099250-1-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210115004051.4099250-1-seanjc@google.com> User-Agent: NeoMutt/20171215 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thanks a lot for the patch, Sean. I know this has been queued for quite a while. But I just realized I have another question of kvm_mmu_pte_write(): > Remove the update_pte() shadow paging logic, which was obsoleted by > commit 4731d4c7a077 ("KVM: MMU: out of sync shadow core"), but never > removed. As pointed out by Yu, KVM never write protects leaf page > tables for the purposes of shadow paging, and instead marks their > associated shadow page as unsync so that the guest can write PTEs at > will. > > The update_pte() path, which predates the unsync logic, optimizes COW > scenarios by refreshing leaf SPTEs when they are written, as opposed to > zapping the SPTE, restarting the guest, and installing the new SPTE on > the subsequent fault. Since KVM no longer write-protects leaf page > tables, update_pte() is unreachable and can be dropped. > > Reported-by: Yu Zhang > Signed-off-by: Sean Christopherson > --- > arch/x86/include/asm/kvm_host.h | 3 -- > arch/x86/kvm/mmu/mmu.c | 49 ++------------------------------- > arch/x86/kvm/x86.c | 1 - > 3 files changed, 2 insertions(+), 51 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 3d6616f6f6ef..ed575c5655dd 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -358,8 +358,6 @@ struct kvm_mmu { > int (*sync_page)(struct kvm_vcpu *vcpu, > struct kvm_mmu_page *sp); > void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa); > - void (*update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, > - u64 *spte, const void *pte); > hpa_t root_hpa; > gpa_t root_pgd; > union kvm_mmu_role mmu_role; > @@ -1031,7 +1029,6 @@ struct kvm_arch { > struct kvm_vm_stat { > ulong mmu_shadow_zapped; > ulong mmu_pte_write; > - ulong mmu_pte_updated; > ulong mmu_pde_zapped; > ulong mmu_flooded; > ulong mmu_recycled; > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index 6d16481aa29d..3a2c25852b1f 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -1723,13 +1723,6 @@ static int nonpaging_sync_page(struct kvm_vcpu *vcpu, > return 0; > } > > -static void nonpaging_update_pte(struct kvm_vcpu *vcpu, > - struct kvm_mmu_page *sp, u64 *spte, > - const void *pte) > -{ > - WARN_ON(1); > -} > - > #define KVM_PAGE_ARRAY_NR 16 > > struct kvm_mmu_pages { > @@ -3813,7 +3806,6 @@ static void nonpaging_init_context(struct kvm_vcpu *vcpu, > context->gva_to_gpa = nonpaging_gva_to_gpa; > context->sync_page = nonpaging_sync_page; > context->invlpg = NULL; > - context->update_pte = nonpaging_update_pte; > context->root_level = 0; > context->shadow_root_level = PT32E_ROOT_LEVEL; > context->direct_map = true; > @@ -4395,7 +4387,6 @@ static void paging64_init_context_common(struct kvm_vcpu *vcpu, > context->gva_to_gpa = paging64_gva_to_gpa; > context->sync_page = paging64_sync_page; > context->invlpg = paging64_invlpg; > - context->update_pte = paging64_update_pte; > context->shadow_root_level = level; > context->direct_map = false; > } > @@ -4424,7 +4415,6 @@ static void paging32_init_context(struct kvm_vcpu *vcpu, > context->gva_to_gpa = paging32_gva_to_gpa; > context->sync_page = paging32_sync_page; > context->invlpg = paging32_invlpg; > - context->update_pte = paging32_update_pte; > context->shadow_root_level = PT32E_ROOT_LEVEL; > context->direct_map = false; > } > @@ -4506,7 +4496,6 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) > context->page_fault = kvm_tdp_page_fault; > context->sync_page = nonpaging_sync_page; > context->invlpg = NULL; > - context->update_pte = nonpaging_update_pte; > context->shadow_root_level = kvm_mmu_get_tdp_level(vcpu); > context->direct_map = true; > context->get_guest_pgd = get_cr3; > @@ -4678,7 +4667,6 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, > context->gva_to_gpa = ept_gva_to_gpa; > context->sync_page = ept_sync_page; > context->invlpg = ept_invlpg; > - context->update_pte = ept_update_pte; > context->root_level = level; > context->direct_map = false; > context->mmu_role.as_u64 = new_role.as_u64; > @@ -4826,19 +4814,6 @@ void kvm_mmu_unload(struct kvm_vcpu *vcpu) > } > EXPORT_SYMBOL_GPL(kvm_mmu_unload); > > -static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu, > - struct kvm_mmu_page *sp, u64 *spte, > - const void *new) > -{ > - if (sp->role.level != PG_LEVEL_4K) { > - ++vcpu->kvm->stat.mmu_pde_zapped; > - return; > - } > - > - ++vcpu->kvm->stat.mmu_pte_updated; > - vcpu->arch.mmu->update_pte(vcpu, sp, spte, new); > -} > - > static bool need_remote_flush(u64 old, u64 new) > { > if (!is_shadow_present_pte(old)) > @@ -4954,22 +4929,6 @@ static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte) > return spte; > } > > -/* > - * Ignore various flags when determining if a SPTE can be immediately > - * overwritten for the current MMU. > - * - level: explicitly checked in mmu_pte_write_new_pte(), and will never > - * match the current MMU role, as MMU's level tracks the root level. > - * - access: updated based on the new guest PTE > - * - quadrant: handled by get_written_sptes() > - * - invalid: always false (loop only walks valid shadow pages) > - */ > -static const union kvm_mmu_page_role role_ign = { > - .level = 0xf, > - .access = 0x7, > - .quadrant = 0x3, > - .invalid = 0x1, > -}; > - > static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, > const u8 *new, int bytes, > struct kvm_page_track_notifier_node *node) > @@ -5020,14 +4979,10 @@ static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, > > local_flush = true; > while (npte--) { > - u32 base_role = vcpu->arch.mmu->mmu_role.base.word; > - > entry = *spte; > mmu_page_zap_pte(vcpu->kvm, sp, spte, NULL); > - if (gentry && > - !((sp->role.word ^ base_role) & ~role_ign.word) && > - rmap_can_add(vcpu)) > - mmu_pte_write_new_pte(vcpu, sp, spte, &gentry); > + if (gentry && sp->role.level != PG_LEVEL_4K) I am wondering, if there's any chance the sp->role.level would be PG_LEVEL_4K in kvm_mmu_pte_write()? My previous understanding was that, since the gfn of guest leaf page tables are never page tracked, the sp here shall only be with level greater than PG_LEVEL_4K. Did I miss anything here? Thanks! :) B.R. Yu > + ++vcpu->kvm->stat.mmu_pde_zapped; > if (need_remote_flush(entry, *spte)) > remote_flush = true; > ++spte; > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index a480804ae27a..d9f5d9acccc1 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -233,7 +233,6 @@ struct kvm_stats_debugfs_item debugfs_entries[] = { > VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns), > VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped), > VM_STAT("mmu_pte_write", mmu_pte_write), > - VM_STAT("mmu_pte_updated", mmu_pte_updated), > VM_STAT("mmu_pde_zapped", mmu_pde_zapped), > VM_STAT("mmu_flooded", mmu_flooded), > VM_STAT("mmu_recycled", mmu_recycled), > -- > 2.30.0.284.gd98b1dd5eaa7-goog >