Skip to content

Commit 39fda5d

Browse files
Lai Jiangshanbonzini
authored andcommitted
KVM: x86/mmu: Detect write #PF to shadow pages during FNAME(fetch) walk
Move the detection of write #PF to shadow pages, i.e. a fault on a write to a page table that is being shadowed by KVM that is used to translate the write itself, from FNAME(is_self_change_mapping) to FNAME(fetch). There is no need to detect the self-referential write before kvm_faultin_pfn() as KVM does not consume EMULTYPE_WRITE_PF_TO_SP for accesses that resolve to "error or no-slot" pfns, i.e. KVM doesn't allow retrying MMIO accesses or writes to read-only memslots. Detecting the EMULTYPE_WRITE_PF_TO_SP scenario in FNAME(fetch) will allow dropping FNAME(is_self_change_mapping) entirely, as the hugepage interaction can be deferred to kvm_mmu_hugepage_adjust(). Cc: Huang Hang <hhuang@linux.alibaba.com> Signed-off-by: Lai Jiangshan <jiangshan.ljs@antgroup.com> Link: https://lore.kernel.org/r/20221213125538.81209-1-jiangshanlai@gmail.com [sean: split to separate patch, write changelog] Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20230202182817.407394-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 258d985 commit 39fda5d

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

arch/x86/kvm/mmu/paging_tmpl.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
685685

686686
if (sp != ERR_PTR(-EEXIST))
687687
link_shadow_page(vcpu, it.sptep, sp);
688+
689+
if (fault->write && table_gfn == fault->gfn)
690+
fault->write_fault_to_shadow_pgtable = true;
688691
}
689692

690693
kvm_mmu_hugepage_adjust(vcpu, fault);
@@ -741,17 +744,13 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
741744
* created when kvm establishes shadow page table that stop kvm using large
742745
* page size. Do it early can avoid unnecessary #PF and emulation.
743746
*
744-
* @write_fault_to_shadow_pgtable will return true if the fault gfn is
745-
* currently used as its page table.
746-
*
747747
* Note: the PDPT page table is not checked for PAE-32 bit guest. It is ok
748748
* since the PDPT is always shadowed, that means, we can not use large page
749749
* size to map the gfn which is used as PDPT.
750750
*/
751751
static bool
752752
FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,
753-
struct guest_walker *walker, bool user_fault,
754-
bool *write_fault_to_shadow_pgtable)
753+
struct guest_walker *walker, bool user_fault)
755754
{
756755
int level;
757756
gfn_t mask = ~(KVM_PAGES_PER_HPAGE(walker->level) - 1);
@@ -765,7 +764,6 @@ FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,
765764
gfn_t gfn = walker->gfn ^ walker->table_gfn[level - 1];
766765

767766
self_changed |= !(gfn & mask);
768-
*write_fault_to_shadow_pgtable |= !gfn;
769767
}
770768

771769
return self_changed;
@@ -826,7 +824,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
826824
return r;
827825

828826
is_self_change_mapping = FNAME(is_self_change_mapping)(vcpu,
829-
&walker, fault->user, &fault->write_fault_to_shadow_pgtable);
827+
&walker, fault->user);
830828

831829
if (is_self_change_mapping)
832830
fault->max_level = PG_LEVEL_4K;

0 commit comments

Comments
 (0)