KVM: arm64: Add "struct kvm_page_fault"
cover KVM: arm64: Add "struct kvm_page_fault"
Add an arm64 version of "struct kvm_page_fault" to (hopefully) tidy up the abort path, and to pave the way for things like KVM Userfault[*] that want to consume kvm_page_fault in arch-neutral code.
This is essentially one giant nop of code shuffling.
RFC as this is only compile-tested. I didn't want to spend time testing until I got feedback on whether or not y'all are amenable to the general idea.
[*] https://lore.kernel.org/all/[email protected]
Sean Christopherson (16):
KVM: arm64: Drop nested "esr" to eliminate variable shadowing
KVM: arm64: Get iabt status on-demand
KVM: arm64: Move SRCU-protected region of kvm_handle_guest_abort() to
helper
KVM: arm64: Use guard(srcu) in kvm_handle_guest_abort()
KVM: arm64: Introduce "struct kvm_page_fault" for tracking abort state
KVM: arm64: Pass kvm_page_fault pointer to
transparent_hugepage_adjust()
KVM: arm64: Pass @fault to fault_supports_stage2_huge_mapping()
KVM: arm64: Add helper to get permission fault granule from ESR
KVM: arm64: Track perm fault granule in "struct kvm_page_fault"
KVM: arm64: Drop local vfio_allow_any_uc, use vm_flags snapshot
KVM: arm64: Drop local mte_allowed, use vm_flags snapshot
KVM: arm64: Move VMA-related information into "struct kvm_page_fault"
KVM: arm64: Stash "mmu_seq" in "struct kvm_page_fault"
KVM: arm64: Track "forced" information in "struct kvm_page_fault"
KVM: arm64: Extract mmap_lock-protected code to helper for user mem
aborts
KVM: arm64: Don't bother nullifying "vma" in mem abort path
arch/arm64/include/asm/esr.h | 6 + arch/arm64/include/asm/kvm_emulate.h | 9 - arch/arm64/include/asm/kvm_host.h | 32 ++ arch/arm64/kvm/mmu.c | 514 +++++++++++++-------------- 4 files changed, 282 insertions(+), 279 deletions(-)
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 1/16 KVM: arm64: Drop nested "esr" to eliminate variable shadowing
Drop a local "esr" variable in kvm_handle_guest_abort() that's used as a very temporary scratch variable when injecting nested stage-2 faults, to avoid creating a shadow of the function's overall "esr", which holds the original state provided by hardware.
No functional change intended.
--- arch/arm64/kvm/mmu.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
arch/arm64/kvm/mmu.c +2 -6
@@ -1904,19 +1904,15 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)*/ if (kvm_is_nested_s2_mmu (vcpu->kvm,vcpu->arch.hw_mmu) && vcpu->arch.hw_mmu->nested_stage2_enabled) { -u32 esr; - ret =kvm_walk_nested_s2 (vcpu, fault_ipa, &nested_trans);if (ret) { - esr =kvm_s2_trans_esr (&nested_trans); -kvm_inject_s2_fault (vcpu, esr); +kvm_inject_s2_fault (vcpu,kvm_s2_trans_esr (&nested_trans)); goto out_unlock; } ret =kvm_s2_handle_perm_fault (vcpu, &nested_trans);if (ret) { - esr =kvm_s2_trans_esr (&nested_trans); -kvm_inject_s2_fault (vcpu, esr); +kvm_inject_s2_fault (vcpu,kvm_s2_trans_esr (&nested_trans)); goto out_unlock; }
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 2/16 KVM: arm64: Get iabt status on-demand
In reply to Sean ChristophersonQuery if an abort was due to instruction execution only when necessary in anticipation of factoring out the SRCU-protected portion of abort handling to a separate helper. The happy path doesn't need to check for IAB, and eliminating the local variable will avoid having to pass a large pile of booleans to the inner helper.
No functional change intended.
--- arch/arm64/kvm/mmu.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
arch/arm64/kvm/mmu.c +4 -5
@@ -1830,7 +1830,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)phys_addr_t ipa;/* Always the IPA in the L1 guest phys space */ struct kvm_memory_slot *memslot;unsigned long hva; -bool is_iabt, write_fault, writable; +bool write_fault, writable;gfn_t gfn;int ret, idx;
@@ -1856,8 +1856,6 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)if (KVM_BUG_ON (ipa ==INVALID_GPA , vcpu->kvm))return -EFAULT ; - is_iabt =kvm_vcpu_trap_is_iabt (vcpu); -if (esr_fsc_is_translation_fault (esr)) {/* Beyond sanitised PARange (which is the IPA limit) */ if (fault_ipa >=BIT_ULL (get_kvm_ipa_limit ())) {
@@ -1869,7 +1867,8 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)if (fault_ipa >=BIT_ULL (VTCR_EL2_IPA (vcpu->arch.hw_mmu->vtcr))) { fault_ipa |=kvm_vcpu_get_hfar (vcpu) &GENMASK (11 ,0 ); -return kvm_inject_sea (vcpu, is_iabt, fault_ipa); +return kvm_inject_sea (vcpu,kvm_vcpu_trap_is_iabt (vcpu), + fault_ipa); } }
@@ -1931,7 +1930,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)* anything about this (there's no syndrome for a start), so * re-inject the abort back into the guest. */ -if (is_iabt) { +if (kvm_vcpu_trap_is_iabt (vcpu)) { ret = -ENOEXEC ; goto out; }
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 3/16 KVM: arm64: Move SRCU-protected region of kvm_handle_guest_abort() to helper
In reply to Sean ChristophersonMove the SRCU-protected portion of the abort handler to a separate helper in anticipation of adding "struct kvm_page_fault" to track state related to resolving the fault. Using a separate helper will allow making several fields in kvm_page_fault "const", without having to do something funky like create a temporary copy in the middle of kvm_handle_guest_abort().
No functional change intended.
--- arch/arm64/kvm/mmu.c | 172 ++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 84 deletions(-)
arch/arm64/kvm/mmu.c +88 -84
@@ -1811,82 +1811,16 @@ static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)read_unlock (&vcpu->kvm->mmu_lock); } -/** -* kvm_handle_guest_abort - handles all 2nd stage aborts -* @vcpu: the VCPU pointer -* -* Any abort that gets to the host is almost guaranteed to be caused by a -* missing second stage translation table entry, which can mean that either the -* guest simply needs more memory and we must allocate an appropriate page or it -* can mean that the guest tried to access I/O memory, which is emulated by user -* space. The distinction is based on the IPA causing the fault and whether this -* memory region has been registered as standard RAM by user space. -*/ -int kvm_handle_guest_abort (struct kvm_vcpu *vcpu) +static int __kvm_handle_guest_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa, +unsigned long esr) {struct kvm_s2_trans nested_trans, *nested =NULL ; -unsigned long esr; -phys_addr_t fault_ipa;/* The address we faulted on */ -phys_addr_t ipa;/* Always the IPA in the L1 guest phys space */ struct kvm_memory_slot *memslot; -unsigned long hva;bool write_fault, writable; +unsigned long hva; +phys_addr_t ipa;/* Always the IPA in the L1 guest phys space */ gfn_t gfn; -int ret, idx; - -/* Synchronous External Abort? */ -if (kvm_vcpu_abt_issea (vcpu)) { -/* -* For RAS the host kernel may handle this abort. -* There is no need to pass the error into the guest. -*/ -if (kvm_handle_guest_sea ()) -return kvm_inject_serror (vcpu); - -return 1 ; - } - - esr =kvm_vcpu_get_esr (vcpu); - -/* -* The fault IPA should be reliable at this point as we're not dealing -* with an SEA. -*/ - ipa = fault_ipa =kvm_vcpu_get_fault_ipa (vcpu); -if (KVM_BUG_ON (ipa ==INVALID_GPA , vcpu->kvm)) -return -EFAULT ; - -if (esr_fsc_is_translation_fault (esr)) { -/* Beyond sanitised PARange (which is the IPA limit) */ -if (fault_ipa >=BIT_ULL (get_kvm_ipa_limit ())) { -kvm_inject_size_fault (vcpu); -return 1 ; - } - -/* Falls between the IPA range and the PARange? */ -if (fault_ipa >=BIT_ULL (VTCR_EL2_IPA (vcpu->arch.hw_mmu->vtcr))) { - fault_ipa |=kvm_vcpu_get_hfar (vcpu) &GENMASK (11 ,0 ); - -return kvm_inject_sea (vcpu,kvm_vcpu_trap_is_iabt (vcpu), - fault_ipa); - } - } - -trace_kvm_guest_fault (*vcpu_pc (vcpu),kvm_vcpu_get_esr (vcpu), -kvm_vcpu_get_hfar (vcpu), fault_ipa); - -/* Check the stage-2 fault is trans. fault or write fault */ -if (!esr_fsc_is_translation_fault (esr) && - !esr_fsc_is_permission_fault (esr) && - !esr_fsc_is_access_flag_fault (esr)) { -kvm_err ("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n" , -kvm_vcpu_trap_get_class (vcpu), - (unsigned long )kvm_vcpu_trap_get_fault (vcpu), - (unsigned long )kvm_vcpu_get_esr (vcpu)); -return -EFAULT ; - } - - idx =srcu_read_lock (&vcpu->kvm->srcu); +int ret;/* * We may have faulted on a shadow stage 2 page table if we are
@@ -1906,13 +1840,13 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) ret =kvm_walk_nested_s2 (vcpu, fault_ipa, &nested_trans);if (ret) {kvm_inject_s2_fault (vcpu,kvm_s2_trans_esr (&nested_trans)); - goto out_unlock; +return ret; } ret =kvm_s2_handle_perm_fault (vcpu, &nested_trans);if (ret) {kvm_inject_s2_fault (vcpu,kvm_s2_trans_esr (&nested_trans)); - goto out_unlock; +return ret; } ipa =kvm_s2_trans_output (&nested_trans);
@@ -1935,10 +1869,8 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) goto out; } -if (kvm_vcpu_abt_iss1tw (vcpu)) { - ret =kvm_inject_sea_dabt (vcpu,kvm_vcpu_get_hfar (vcpu)); - goto out_unlock; - } +if (kvm_vcpu_abt_iss1tw (vcpu)) +return kvm_inject_sea_dabt (vcpu,kvm_vcpu_get_hfar (vcpu));/* * Check for a cache maintenance operation. Since we
@@ -1952,8 +1884,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)*/ if (kvm_is_error_hva (hva) &&kvm_vcpu_dabt_is_cm (vcpu)) {kvm_incr_pc (vcpu); - ret =1 ; - goto out_unlock; +return 1 ; }/*
@@ -1963,8 +1894,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)* of the page size. */ ipa |=kvm_vcpu_get_hfar (vcpu) &GENMASK (11 ,0 ); - ret =io_mem_abort (vcpu, ipa); - goto out_unlock; +return io_mem_abort (vcpu, ipa); }/* Userspace should not be able to register out-of-bounds IPAs */
@@ -1972,8 +1902,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)if (esr_fsc_is_access_flag_fault (esr)) {handle_access_fault (vcpu, fault_ipa); - ret =1 ; - goto out_unlock; +return 1 ; } ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, hva,
@@ -1983,7 +1912,82 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) out:if (ret == -ENOEXEC ) ret =kvm_inject_sea_iabt (vcpu,kvm_vcpu_get_hfar (vcpu)); -out_unlock: +return ret; +} + +/** +* kvm_handle_guest_abort - handles all 2nd stage aborts +* @vcpu: the VCPU pointer +* +* Any abort that gets to the host is almost guaranteed to be caused by a +* missing second stage translation table entry, which can mean that either the +* guest simply needs more memory and we must allocate an appropriate page or it +* can mean that the guest tried to access I/O memory, which is emulated by user +* space. The distinction is based on the IPA causing the fault and whether this +* memory region has been registered as standard RAM by user space. +*/ +int kvm_handle_guest_abort (struct kvm_vcpu *vcpu) +{ +unsigned long esr; +phys_addr_t fault_ipa;/* The address we faulted on */ +int ret, idx; + +/* Synchronous External Abort? */ +if (kvm_vcpu_abt_issea (vcpu)) { +/* +* For RAS the host kernel may handle this abort. +* There is no need to pass the error into the guest. +*/ +if (kvm_handle_guest_sea ()) +return kvm_inject_serror (vcpu); + +return 1 ; + } + + esr =kvm_vcpu_get_esr (vcpu); + +/* +* The fault IPA should be reliable at this point as we're not dealing +* with an SEA. +*/ + fault_ipa =kvm_vcpu_get_fault_ipa (vcpu); +if (KVM_BUG_ON (fault_ipa ==INVALID_GPA , vcpu->kvm)) +return -EFAULT ; + +if (esr_fsc_is_translation_fault (esr)) { +/* Beyond sanitised PARange (which is the IPA limit) */ +if (fault_ipa >=BIT_ULL (get_kvm_ipa_limit ())) { +kvm_inject_size_fault (vcpu); +return 1 ; + } + +/* Falls between the IPA range and the PARange? */ +if (fault_ipa >=BIT_ULL (VTCR_EL2_IPA (vcpu->arch.hw_mmu->vtcr))) { + fault_ipa |=kvm_vcpu_get_hfar (vcpu) &GENMASK (11 ,0 ); + +return kvm_inject_sea (vcpu,kvm_vcpu_trap_is_iabt (vcpu), + fault_ipa); + } + } + +trace_kvm_guest_fault (*vcpu_pc (vcpu),kvm_vcpu_get_esr (vcpu), +kvm_vcpu_get_hfar (vcpu), fault_ipa); + +/* Check the stage-2 fault is trans. fault or write fault */ +if (!esr_fsc_is_translation_fault (esr) && + !esr_fsc_is_permission_fault (esr) && + !esr_fsc_is_access_flag_fault (esr)) { +kvm_err ("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n" , +kvm_vcpu_trap_get_class (vcpu), + (unsigned long )kvm_vcpu_trap_get_fault (vcpu), + (unsigned long )kvm_vcpu_get_esr (vcpu)); +return -EFAULT ; + } + + idx =srcu_read_lock (&vcpu->kvm->srcu); + + ret =__kvm_handle_guest_abort (vcpu, fault_ipa, esr); +srcu_read_unlock (&vcpu->kvm->srcu, idx);return ret; }
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 4/16 KVM: arm64: Use guard(srcu) in kvm_handle_guest_abort()
In reply to Sean ChristophersonUse a guard() to acquire/release SRCU when handling guest aborts to simplify the code a bit.
No functional change intended.
--- arch/arm64/kvm/mmu.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
arch/arm64/kvm/mmu.c +2 -6
@@ -1930,7 +1930,6 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) {unsigned long esr;phys_addr_t fault_ipa;/* The address we faulted on */ -int ret, idx;/* Synchronous External Abort? */ if (kvm_vcpu_abt_issea (vcpu)) {
@@ -1984,12 +1983,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)return -EFAULT ; } - idx =srcu_read_lock (&vcpu->kvm->srcu); +guard (srcu)(&vcpu->kvm->srcu); - ret =__kvm_handle_guest_abort (vcpu, fault_ipa, esr); - -srcu_read_unlock (&vcpu->kvm->srcu, idx); -return ret; +return __kvm_handle_guest_abort (vcpu, fault_ipa, esr); }bool kvm_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range)
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 5/16 KVM: arm64: Introduce "struct kvm_page_fault" for tracking abort state
In reply to Sean ChristophersonAdd and use a kvm_page_fault structure to track state when handling a guest abort. Collecting everything in a single structure will enable a variety of cleanups (reduce the number of params passed to helpers), and will pave the way toward using "struct kvm_page_fault" in arch-neutral KVM code, e.g. to consolidate logic for KVM_EXIT_MEMORY_FAULT.
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 18 ++++ arch/arm64/kvm/mmu.c | 143 ++++++++++++++---------------- 2 files changed, 87 insertions(+), 74 deletions(-)
arch/arm64/include/asm/kvm_host.h +18 -0
@@ -413,6 +413,24 @@ struct kvm_vcpu_fault_info {u64 disr_el1;/* Deferred [SError] Status Register */ }; +struct kvm_page_fault { +const u64 esr; +const bool exec; +const bool write; +const bool is_perm; + +phys_addr_t fault_ipa;/* The address we faulted on */ +phys_addr_t ipa;/* Always the IPA in the L1 guest phys space */ + +struct kvm_s2_trans *nested; + +gfn_t gfn; +struct kvm_memory_slot *slot; +unsigned long hva; +kvm_pfn_t pfn; +struct page *page; +}; +/* * VNCR() just places the VNCR_capable registers in the enum after * __VNCR_START__, and the value (after correction) to be an 8-byte offset
arch/arm64/kvm/mmu.c +69 -74
335 lines not on this page · raw message
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
Re: [RFC PATCH 05/16] KVM: arm64: Introduce "struct kvm_page_fault" for tracking abort state
Hey Sean,
Quoted 21 lines · Sean Christopherson
Add and use a
kvm_page_faultstructure to track state when handling a guest abort. Collecting everything in a single structure will enable a variety of cleanups (reduce the number of params passed to helpers), and will pave the way toward using "structkvm_page_fault" in arch-neutral KVM code, e.g. to consolidate logic forKVM_EXIT_MEMORY_FAULT.No functional change intended.
Cc James Houghton <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 18 ++++ arch/arm64/kvm/mmu.c | 143 ++++++++++++++---------------- 2 files changed, 87 insertions(+), 74 deletions(-)arch/arm64/include/asm/kvm_host.h +0
-0
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_fault_info { · line 420
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +5
-0@@ -413,6 +413,24 @@ struct kvm_vcpu_fault_info {u64 disr_el1;/* Deferred [SError] Status Register */ }; +struct kvm_page_fault { +const u64 esr; +const bool exec; +const bool write; +const bool is_perm;
Hmm... these might be better represented as predicates that take a pointer to this struct and we just compute it based on ESR. That'd have the benefit in the arch-neutral code where 'struct kvm_page_fault' is an opaque type and we don't need to align field names/types.
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_fault_info { · line 423
Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +2
-0+phys_addr_t fault_ipa;/* The address we faulted on */ +phys_addr_t ipa;/* Always the IPA in the L1 guest phys space */
NYC, but this also seems like a good opportunity to rename + retype these guys. Specifically:
fault_ipa => ipa
ipa => canonical_ipa
would clarify these and align with the verbiage we currently use to talk about nested.
Thanks,
Oliver
Re: [RFC PATCH 05/16] KVM: arm64: Introduce "struct kvm_page_fault" for tracking abort state
Quoted 27 lines · Oliver Upton
Hey Sean,
Quoted 24 lines · Sean Christopherson
Add and use a
kvm_page_faultstructure to track state when handling a guest abort. Collecting everything in a single structure will enable a variety of cleanups (reduce the number of params passed to helpers), and will pave the way toward using "structkvm_page_fault" in arch-neutral KVM code, e.g. to consolidate logic forKVM_EXIT_MEMORY_FAULT.No functional change intended.
Cc James Houghton <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 18 ++++ arch/arm64/kvm/mmu.c | 143 ++++++++++++++---------------- 2 files changed, 87 insertions(+), 74 deletions(-)arch/arm64/include/asm/kvm_host.h +0
-0@@ -413,6 +413,24 @@ struct kvm_vcpu_fault_info {u64 disr_el1;/* Deferred [SError] Status Register */ };
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_fault_info { · line 420
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +5
-0+struct kvm_page_fault { +const u64 esr; +const bool exec; +const bool write; +const bool is_perm;Hmm... these might be better represented as predicates that take a pointer to this struct and we just compute it based on ESR. That'd have the benefit in the arch-neutral code where 'struct
kvm_page_fault' is an opaque type and we don't need to align field names/types.
We'd need to align function names/types though, so to some extent it's six of one, half dozen of the other. My slight preference would be to require kvm_page_fault to have certain fields, but I'm ok with making kvm_page_fault opaque to generic code and instead adding arch APIs. Having a handful of wrappers in x86 isn't the end of the world, and it would be more familiar for pretty much everyone.
Quoted 11 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_fault_info { · line 423
Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +2
-0+phys_addr_t fault_ipa;/* The address we faulted on */ +phys_addr_t ipa;/* Always the IPA in the L1 guest phys space */ NYC, but this also seems like a good opportunity to rename + retype these guys. Specifically:
fault_ipa=> ipa
ipa =>canonical_ipawould clarify these and align with the verbiage we currently use to talk about nested.
Heh, I'm so screwed. x86's use of "canonical" is wildly different. I can add a patch to do those renames (I think doing an "opportunistic" rename would be a bit much).
Re: [RFC PATCH 05/16] KVM: arm64: Introduce "struct kvm_page_fault" for tracking abort state
Quoted 39 lines · Sean Christopherson
Quoted 27 lines · Oliver Upton
Hey Sean,
Quoted 24 lines · Sean Christopherson
Add and use a
kvm_page_faultstructure to track state when handling a guest abort. Collecting everything in a single structure will enable a variety of cleanups (reduce the number of params passed to helpers), and will pave the way toward using "structkvm_page_fault" in arch-neutral KVM code, e.g. to consolidate logic forKVM_EXIT_MEMORY_FAULT.No functional change intended.
Cc James Houghton <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 18 ++++ arch/arm64/kvm/mmu.c | 143 ++++++++++++++---------------- 2 files changed, 87 insertions(+), 74 deletions(-)arch/arm64/include/asm/kvm_host.h +0
-0@@ -413,6 +413,24 @@ struct kvm_vcpu_fault_info {u64 disr_el1;/* Deferred [SError] Status Register */ };Quoted 10 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_fault_info { · line 420
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +5
-0+struct kvm_page_fault { +const u64 esr; +const bool exec; +const bool write; +const bool is_perm;Hmm... these might be better represented as predicates that take a pointer to this struct and we just compute it based on ESR. That'd have the benefit in the arch-neutral code where 'struct
kvm_page_fault' is an opaque type and we don't need to align field names/types.
Quoted 5 lines
We'd need to align function names/types though, so to some extent it's six of one, half dozen of the other. My slight preference would be to require
kvm_page_faultto have certain fields, but I'm ok with makingkvm_page_faultopaque to generic code and instead adding arch APIs. Having a handful of wrappers in x86 isn't the end of the world, and it would be more familiar for pretty much everyone.
To clarify my earlier point, my actual interest is in using ESR as the source of truth from the arch POV, interface to the arch-neutral code isn't that big of a deal either way.
Thanks,
Oliver
Re: [RFC PATCH 05/16] KVM: arm64: Introduce "struct kvm_page_fault" for tracking abort state
Quoted 19 lines · Oliver Upton
Quoted 12 lines · Sean Christopherson
Quoted 10 lines · Oliver Upton
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_fault_info { · line 420
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +5
-0+struct kvm_page_fault { +const u64 esr; +const bool exec; +const bool write; +const bool is_perm;Hmm... these might be better represented as predicates that take a pointer to this struct and we just compute it based on ESR. That'd have the benefit in the arch-neutral code where 'struct
kvm_page_fault' is an opaque type and we don't need to align field names/types.Quoted 5 lines
We'd need to align function names/types though, so to some extent it's six of one, half dozen of the other. My slight preference would be to require
kvm_page_faultto have certain fields, but I'm ok with makingkvm_page_faultopaque to generic code and instead adding arch APIs. Having a handful of wrappers in x86 isn't the end of the world, and it would be more familiar for pretty much everyone.
Quoted 3 lines
To clarify my earlier point, my actual interest is in using ESR as the source of truth from the arch POV, interface to the arch-neutral code isn't that big of a deal either way.
Ya, but that would mean having something like
static bool kvm_is_exec_fault(struct kvm_page_fault *fault)
{
return esr_trap_is_iabt(fault->esr) && !esr_abt_iss1tw(fault->esr);}
and
if (kvm_is_exec_fault(fault))
in arm64 code and then
if (fault->exec)
in arch-neutral code, which, eww.
I like the idea of having a single source of truth, but that's going to be a massive amount of work to do it "right", e.g. O(weeks) if not O(months). E.g. to replace fault->exec with kvm_is_exec_fault(), AFAICT it would require duplicating all of kvm_is_write_fault(). Rinse and repeat for 20+ APIs in kvm_emulate.h that take a vCPU and pull ESR from vcpu->arch.fault.esr_el2.
As an intermediate state, having that many duplicate APIs is tolerable, but I wouldn't want to leave that as the "end" state for any kernel release, and ideally not for any given series. That means adding a pile of esr-based APIs, converting _all_ users, then dropping the vcpu-based APIs. That's a lot of code and patches.
E.g. even if we convert all of kvm_handle_guest_abort(), which itself is a big task, there will still be usage of many of the APIs in at least kvm_translate_vncr(), io_mem_abort(), and kvm_handle_mmio_return(). Converting all of those is totally doable, e.g. through a combination of using kvm_page_fault and local snapshots of esr, but it will be a lot of work and churn.
The work+churn itself doesn't bother me, but I would prefer not to block arch-neutral usage of kvm_page_fault for months on end, nor do I want to leave KVM arm64 in a half-baked state, i.e. I wouldn't feel comfortable converting just __kvm_handle_guest_abort() and walking away.
What if we keep the exec, write, and is_perm fields for now, but add proper APIs to access kvm_page_fault from common code? The APIs would be largely duplicate code between x86 and arm64 (though I think kvm_get_fault_gpa() would be different, so yay), but that's not a big deal. That way common KVM can start building out functionality based on kvm_page_fault, and arm64 can independently convert to making fault->esr the single source of truth, without having to worry about perturbing common code.
RFC PATCH 6/16 KVM: arm64: Pass kvm_page_fault pointer to transparent_hugepage_adjust()
In reply to Sean ChristophersonUse the local kvm_page_fault structure to adjust for transparent hugepages when resolving guest aborts, to reduce the number of parameters from 5=>2, and to eliminate the less-than-pleasant pointer dereferences.
No functional change intended.
--- arch/arm64/kvm/mmu.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-)
arch/arm64/kvm/mmu.c +6 -14
@@ -1361,19 +1361,15 @@ static bool fault_supports_stage2_huge_mapping(struct kvm_memory_slot *memslot,* Returns the size of the mapping. */ static long -transparent_hugepage_adjust (struct kvm *kvm,struct kvm_memory_slot *memslot, -unsigned long hva,kvm_pfn_t *pfnp, -phys_addr_t *ipap) +transparent_hugepage_adjust (struct kvm *kvm,struct kvm_page_fault *fault) { -kvm_pfn_t pfn = *pfnp; -/* * Make sure the adjustment is done only for THP pages. Also make * sure that the HVA and IPA are sufficiently aligned and that the * block map is contained within the memslot. */ -if (fault_supports_stage2_huge_mapping (memslot, hva,PMD_SIZE )) { -int sz =get_user_mapping_size (kvm, hva); +if (fault_supports_stage2_huge_mapping (fault->slot, fault->hva,PMD_SIZE )) { +int sz =get_user_mapping_size (kvm, fault->hva);if (sz <0 )return sz;
@@ -1381,10 +1377,8 @@ transparent_hugepage_adjust(struct kvm *kvm, struct kvm_memory_slot *memslot,if (sz <PMD_SIZE )return PAGE_SIZE ; - *ipap &=PMD_MASK ; - pfn &= ~(PTRS_PER_PMD -1 ); - *pfnp = pfn; - + fault->ipa &=PMD_MASK ; + fault->pfn &= ~(PTRS_PER_PMD -1 );return PMD_SIZE ; }
@@ -1724,9 +1718,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (fault->is_perm && fault_granule >PAGE_SIZE ) vma_pagesize = fault_granule;else - vma_pagesize =transparent_hugepage_adjust (kvm, fault->slot, - fault->hva, &fault->pfn, - &fault->fault_ipa); + vma_pagesize =transparent_hugepage_adjust (kvm, fault);if (vma_pagesize <0 ) { ret = vma_pagesize;
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 7/16 KVM: arm64: Pass @fault to fault_supports_stage2_huge_mapping()
In reply to Sean ChristophersonPass the full kvm_page_fault object when querying if a fault supports a hugepage mapping instead of passing the separate slot+address pair.
No functional change intended.
--- arch/arm64/kvm/mmu.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
arch/arm64/kvm/mmu.c +7 -7
@@ -1287,10 +1287,10 @@ static void kvm_send_hwpoison_signal(unsigned long address, short lsb)send_sig_mceerr (BUS_MCEERR_AR , (void __user *)address, lsb, current); } -static bool fault_supports_stage2_huge_mapping (struct kvm_memory_slot *memslot, -unsigned long hva, +static bool fault_supports_stage2_huge_mapping (struct kvm_page_fault *fault,unsigned long map_size) { +struct kvm_memory_slot *memslot = fault->slot;gpa_t gpa_start;hva_t uaddr_start, uaddr_end;size_t size;
@@ -1348,8 +1348,8 @@ static bool fault_supports_stage2_huge_mapping(struct kvm_memory_slot *memslot,* userspace_addr or the base_gfn, as both are equally aligned (per * the check above) and equally sized. */ -return (hva & ~(map_size -1 )) >= uaddr_start && - (hva & ~(map_size -1 )) + map_size <= uaddr_end; +return (fault->hva & ~(map_size -1 )) >= uaddr_start && + (fault->hva & ~(map_size -1 )) + map_size <= uaddr_end; }/*
@@ -1368,7 +1368,7 @@ transparent_hugepage_adjust(struct kvm *kvm, struct kvm_page_fault *fault)* sure that the HVA and IPA are sufficiently aligned and that the * block map is contained within the memslot. */ -if (fault_supports_stage2_huge_mapping (fault->slot, fault->hva,PMD_SIZE )) { +if (fault_supports_stage2_huge_mapping (fault,PMD_SIZE )) {int sz =get_user_mapping_size (kvm, fault->hva);if (sz <0 )
@@ -1547,7 +1547,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)switch (vma_shift) {#ifndef __PAGETABLE_PMD_FOLDEDcase PUD_SHIFT : -if (fault_supports_stage2_huge_mapping (fault->slot, fault->hva,PUD_SIZE )) +if (fault_supports_stage2_huge_mapping (fault,PUD_SIZE ))break ; fallthrough;#endif
@@ -1555,7 +1555,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) vma_shift =PMD_SHIFT ; fallthrough;case PMD_SHIFT : -if (fault_supports_stage2_huge_mapping (fault->slot, fault->hva,PMD_SIZE )) +if (fault_supports_stage2_huge_mapping (fault,PMD_SIZE ))break ; fallthrough;case CONT_PTE_SHIFT :
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 8/16 KVM: arm64: Add helper to get permission fault granule from ESR
In reply to Sean ChristophersonExtract KVM's code for getting the granule for a permission fault into a standalone API that takes in a raw ESR, so that KVM can get the granule from a local copy of the ESR instead of re-retrieving the value from the vCPU.
No functional change intended.
--- arch/arm64/include/asm/esr.h | 6 ++++++ arch/arm64/include/asm/kvm_emulate.h | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-)
arch/arm64/include/asm/esr.h +6 -0
@@ -8,6 +8,7 @@#define __ASM_ESR_H#include <asm/memory.h> +#include <asm/pgtable-hwdef.h> #include <asm/sysreg.h> #define ESR_ELx_EC_UNKNOWN UL(0x00)
@@ -478,6 +479,11 @@ static inline bool esr_fsc_is_permission_fault(unsigned long esr) (esr ==ESR_ELx_FSC_PERM_L (0 )); } +static inline u64 esr_fsc_perm_fault_granule (unsigned long esr) +{ +return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (esr & ESR_ELx_FSC_LEVEL)); +} +static inline bool esr_fsc_is_access_flag_fault (unsigned long esr) { esr = esr & ESR_ELx_FSC;
arch/arm64/include/asm/kvm_emulate.h +1 -1
@@ -455,7 +455,7 @@ u64 kvm_vcpu_trap_get_perm_fault_granule(const struct kvm_vcpu *vcpu)unsigned long esr =kvm_vcpu_get_esr (vcpu);BUG_ON (!esr_fsc_is_permission_fault (esr)); -return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (esr & ESR_ELx_FSC_LEVEL)); +return esr_fsc_perm_fault_granule (esr); } static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 9/16 KVM: arm64: Track perm fault granule in "struct kvm_page_fault"
In reply to Sean ChristophersonAdd permission fault granule information to "struct kvm_page_fault", to help capture that the granule is a property of the fault, and to make the information readily available, e.g. without needing to be explicitly passed if it's needed by a helper.
Opportunistically drop kvm_vcpu_trap_get_perm_fault_granule() and simply grab the granule from the passed-in ESR.
No functional change intended.
--- arch/arm64/include/asm/kvm_emulate.h | 9 --------- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/mmu.c | 13 ++++++------- 3 files changed, 7 insertions(+), 16 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +0 -9
@@ -449,15 +449,6 @@ bool kvm_vcpu_trap_is_translation_fault(const struct kvm_vcpu *vcpu)return esr_fsc_is_translation_fault (kvm_vcpu_get_esr (vcpu)); } -static inline -u64 kvm_vcpu_trap_get_perm_fault_granule (const struct kvm_vcpu *vcpu) -{ -unsigned long esr =kvm_vcpu_get_esr (vcpu); - -BUG_ON (!esr_fsc_is_permission_fault (esr)); -return esr_fsc_perm_fault_granule (esr); -} - static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu) {switch (kvm_vcpu_trap_get_fault (vcpu)) {
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -418,6 +418,7 @@ struct kvm_page_fault {const bool exec;const bool write;const bool is_perm; +const u64 granule;phys_addr_t fault_ipa;/* The address we faulted on */ phys_addr_t ipa;/* Always the IPA in the L1 guest phys space */
arch/arm64/kvm/mmu.c +6 -7
@@ -1483,14 +1483,12 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)short vma_shift;void *memcache;bool logging_active =memslot_is_logging (fault->slot); -long vma_pagesize, fault_granule; +long vma_pagesize;enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ;struct kvm_pgtable *pgt;vm_flags_t vm_flags;enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_HANDLE_FAULT |KVM_PGTABLE_WALK_SHARED ; -if (fault->is_perm) - fault_granule =kvm_vcpu_trap_get_perm_fault_granule (vcpu);VM_BUG_ON (fault->write && fault->exec);if (fault->is_perm && !fault->write && !fault->exec) {
@@ -1715,8 +1713,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* backed by a THP and thus use block mapping if possible. */ if (vma_pagesize ==PAGE_SIZE && !(force_pte || s2_force_noncacheable)) { -if (fault->is_perm && fault_granule >PAGE_SIZE ) - vma_pagesize = fault_granule; +if (fault->is_perm && fault->granule >PAGE_SIZE ) + vma_pagesize = fault->granule;else vma_pagesize =transparent_hugepage_adjust (kvm, fault);
@@ -1754,10 +1752,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)/* * Under the premise of getting a FSC_PERM fault, we just need to relax -* permissions only if vma_pagesize equals fault_granule. Otherwise, +* permissions only if vma_pagesize equals fault->granule. Otherwise, * kvm_pgtable_stage2_map() should be called to change block size. */ -if (fault->is_perm && vma_pagesize == fault_granule) { +if (fault->is_perm && vma_pagesize == fault->granule) {/* * Drop the SW bits in favour of those stored in the * PTE, which will be preserved.
@@ -1806,6 +1804,7 @@ static int __kvm_handle_guest_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa .write = kvm_is_write_fault(vcpu), .exec = kvm_vcpu_trap_is_exec_fault(vcpu), .is_perm = esr_fsc_is_permission_fault(esr), + .granule = esr_fsc_is_permission_fault(esr) ? esr_fsc_perm_fault_granule(esr) : 0, };struct kvm_s2_trans nested_trans;bool writable;
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 10/16 KVM: arm64: Drop local vfio_allow_any_uc, use vm_flags snapshot
In reply to Sean ChristophersonDrop user_mem_abort()'s local vfio_allow_any_uc variable and instead use the vm_flags snapshot.
No functional change intended.
--- arch/arm64/kvm/mmu.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +2 -4
@@ -1476,7 +1476,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)int ret =0 ;bool writable, force_pte = false;bool mte_allowed, is_vma_cacheable; -bool s2_force_noncacheable = false, vfio_allow_any_uc = false; +bool s2_force_noncacheable = false;unsigned long mmu_seq;struct kvm *kvm = vcpu->kvm;struct vm_area_struct *vma;
@@ -1608,8 +1608,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) fault->gfn = fault->ipa >>PAGE_SHIFT ; mte_allowed =kvm_vma_mte_allowed (vma); - vfio_allow_any_uc = vma->vm_flags &VM_ALLOW_ANY_UNCACHED ; - vm_flags = vma->vm_flags; is_vma_cacheable =kvm_vma_is_cacheable (vma);
@@ -1741,7 +1739,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) prot |=KVM_PGTABLE_PROT_X ;if (s2_force_noncacheable) { -if (vfio_allow_any_uc) +if (vm_flags &VM_ALLOW_ANY_UNCACHED ) prot |=KVM_PGTABLE_PROT_NORMAL_NC ;else prot |=KVM_PGTABLE_PROT_DEVICE ;
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 11/16 KVM: arm64: Drop local mte_allowed, use vm_flags snapshot
In reply to Sean ChristophersonDrop user_mem_abort()'s local mte_allowed and instead use the vm_flags snapshot. The redundant variables aren't problematic per se, but will be quite awkward when a future change moves the vm_flags snapshot into "struct kvm_page_fault".
Opportunistically drop kvm_vma_mte_allowed() and open code the vm_flags check in the memslot preparation code, as there's little value in hiding VM_MTE_ALLOWED (arguably negative "value), and the fault path can't use the VMA-based helper (because looking at the VMA outside of mmap_lock is unsafe).
No functional change intended.
--- arch/arm64/kvm/mmu.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
arch/arm64/kvm/mmu.c +3 -9
@@ -1454,11 +1454,6 @@ static void sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn, } } -static bool kvm_vma_mte_allowed (struct vm_area_struct *vma) -{ -return vma->vm_flags &VM_MTE_ALLOWED ; -} -static bool kvm_vma_is_cacheable (struct vm_area_struct *vma) {switch (FIELD_GET (PTE_ATTRINDX_MASK ,pgprot_val (vma->vm_page_prot))) {
@@ -1475,7 +1470,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {int ret =0 ;bool writable, force_pte = false; -bool mte_allowed, is_vma_cacheable; +bool is_vma_cacheable;bool s2_force_noncacheable = false;unsigned long mmu_seq;struct kvm *kvm = vcpu->kvm;
@@ -1606,7 +1601,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) } fault->gfn = fault->ipa >>PAGE_SHIFT ; - mte_allowed =kvm_vma_mte_allowed (vma); vm_flags = vma->vm_flags;
@@ -1724,7 +1718,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (!fault->is_perm && !s2_force_noncacheable &&kvm_has_mte (kvm)) {/* Check the VMM hasn't introduced a new disallowed VMA */ -if (mte_allowed) { +if (vm_flags &VM_MTE_ALLOWED ) {sanitise_mte_tags (kvm, fault->pfn, vma_pagesize); }else { ret = -EFAULT ;
@@ -2215,7 +2209,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,if (!vma)break ; -if (kvm_has_mte (kvm) && !kvm_vma_mte_allowed (vma)) { +if (kvm_has_mte (kvm) && !(vma->vm_flags &VM_MTE_ALLOWED )) { ret = -EINVAL ;break ; }
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 12/16 KVM: arm64: Move VMA-related information into "struct kvm_page_fault"
In reply to Sean ChristophersonStash the "outputs" from parsing the VMA associated with an abort in kvm_page_fault. This will allow moving the mmap_lock-protected section to a separate helper without needing a pile of out-parameters.
Deliberately place "pagesize" outside of the "vma" sub-structure, as KVM manipulates (restricts) the pagesize based on other state, i.e. it's not a strict representation of the VMA.
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 9 +++++ arch/arm64/kvm/mmu.c | 67 +++++++++++++++---------------- 2 files changed, 41 insertions(+), 35 deletions(-)
arch/arm64/include/asm/kvm_host.h +9 -0
@@ -430,6 +430,15 @@ struct kvm_page_fault {unsigned long hva;kvm_pfn_t pfn;struct page *page; + +struct { +vm_flags_t vm_flags; +short pageshift; + +bool is_cacheable; + } vma; + +long pagesize; };/*
arch/arm64/kvm/mmu.c +32 -35
177 lines not on this page · raw message
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 13/16 KVM: arm64: Stash "mmu_seq" in "struct kvm_page_fault"
In reply to Sean ChristophersonTrack the MMU notifier sequence count snapshot in "struct kvm_page_fault" in anticipation of moving the mmap_lock-protected code to a separate helper. Attaching mmu_seq to the fault could also prove useful in the future, e.g. for additional refactorings.
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/mmu.c | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -430,6 +430,7 @@ struct kvm_page_fault {unsigned long hva;kvm_pfn_t pfn;struct page *page; +unsigned long mmu_seq;struct {vm_flags_t vm_flags;
arch/arm64/kvm/mmu.c +2 -3
@@ -1471,7 +1471,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)int ret =0 ;bool writable, force_pte = false;bool s2_force_noncacheable = false; -unsigned long mmu_seq;struct kvm *kvm = vcpu->kvm;struct vm_area_struct *vma;void *memcache;
@@ -1612,7 +1611,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs * with the smp_wmb() in kvm_mmu_invalidate_end(). */ - mmu_seq = vcpu->kvm->mmu_invalidate_seq; + fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq;mmap_read_unlock (current->mm); fault->pfn = __kvm_faultin_pfn(fault->slot, fault->gfn,
@@ -1691,7 +1690,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)kvm_fault_lock (kvm); pgt = vcpu->arch.hw_mmu->pgt; -if (mmu_invalidate_retry (kvm, mmu_seq)) { +if (mmu_invalidate_retry (kvm, fault->mmu_seq)) { ret = -EAGAIN ; goto out_unlock; }
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 14/16 KVM: arm64: Track "forced" information in "struct kvm_page_fault"
In reply to Sean ChristophersonMove the abort handler's local "force_pte" and "s2_force_noncacheable" variables into "struct kvm_page_fault" in anticipation of moving the mmap_lock-protected code to a separate helper.
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/mmu.c | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -440,6 +440,9 @@ struct kvm_page_fault { } vma;long pagesize; + +bool force_pte; +bool s2_force_noncacheable; };/*
arch/arm64/kvm/mmu.c +11 -11
@@ -1469,8 +1469,7 @@ static bool kvm_vma_is_cacheable(struct vm_area_struct *vma)static int user_mem_abort (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {int ret =0 ; -bool writable, force_pte = false; -bool s2_force_noncacheable = false; +bool writable;struct kvm *kvm = vcpu->kvm;struct vm_area_struct *vma;void *memcache;
@@ -1526,7 +1525,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* memslots. */ if (logging_active) { - force_pte = true; + fault->force_pte = true; fault->vma.pageshift =PAGE_SHIFT ; }else { fault->vma.pageshift =get_vma_page_shift (vma, fault->hva);
@@ -1548,7 +1547,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) fallthrough;case CONT_PTE_SHIFT : fault->vma.pageshift =PAGE_SHIFT ; - force_pte = true; + fault->force_pte = true; fallthrough;case PAGE_SHIFT :break ;
@@ -1561,7 +1560,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (fault->nested) {unsigned long max_map_size; - max_map_size = force_pte ?PAGE_SIZE :PUD_SIZE ; + max_map_size = fault->force_pte ?PAGE_SIZE :PUD_SIZE ;WARN_ON_ONCE (fault->ipa !=kvm_s2_trans_output (fault->nested));
@@ -1581,7 +1580,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)else if (max_map_size >=PAGE_SIZE && max_map_size <PMD_SIZE ) max_map_size =PAGE_SIZE ; - force_pte = (max_map_size ==PAGE_SIZE ); + fault->force_pte = (max_map_size ==PAGE_SIZE ); fault->pagesize =min (fault->pagesize, (long )max_map_size); }
@@ -1656,7 +1655,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* In both cases, we don't let transparent_hugepage_adjust() * change things at the last minute. */ - s2_force_noncacheable = true; + fault->s2_force_noncacheable = true; } }else if (logging_active && !fault->write) {/*
@@ -1666,7 +1665,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) writable = false; } -if (fault->exec && s2_force_noncacheable) +if (fault->exec && fault->s2_force_noncacheable)return -ENOEXEC ;/*
@@ -1699,7 +1698,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* If we are not forced to use page mapping, check if we are * backed by a THP and thus use block mapping if possible. */ -if (fault->pagesize ==PAGE_SIZE && !(force_pte || s2_force_noncacheable)) { +if (fault->pagesize ==PAGE_SIZE && + !(fault->force_pte || fault->s2_force_noncacheable)) {if (fault->is_perm && fault->granule >PAGE_SIZE ) fault->pagesize = fault->granule;else
@@ -1711,7 +1711,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) } } -if (!fault->is_perm && !s2_force_noncacheable &&kvm_has_mte (kvm)) { +if (!fault->is_perm && !fault->s2_force_noncacheable &&kvm_has_mte (kvm)) {/* Check the VMM hasn't introduced a new disallowed VMA */ if (fault->vma.vm_flags &VM_MTE_ALLOWED ) {sanitise_mte_tags (kvm, fault->pfn, fault->pagesize);
@@ -1727,7 +1727,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (fault->exec) prot |=KVM_PGTABLE_PROT_X ; -if (s2_force_noncacheable) { +if (fault->s2_force_noncacheable) {if (fault->vma.vm_flags &VM_ALLOW_ANY_UNCACHED ) prot |=KVM_PGTABLE_PROT_NORMAL_NC ;else
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 15/16 KVM: arm64: Extract mmap_lock-protected code to helper for user mem aborts
In reply to Sean ChristophersonFactor out the mmap_lock-protected portion of user_mem_abort() to a new helper, partly to make user_mem_abort() easier to follow, but mostly so that the scope of the mmap_lock-protected code is more explicitly bounded.
No functional change intended.
--- arch/arm64/kvm/mmu.c | 96 ++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 44 deletions(-)
arch/arm64/kvm/mmu.c +52 -44
@@ -1466,47 +1466,10 @@ static bool kvm_vma_is_cacheable(struct vm_area_struct *vma) } } -static int user_mem_abort (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) +static int kvm_gather_fault_vma_info (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) { -int ret =0 ; -bool writable; -struct kvm *kvm = vcpu->kvm;struct vm_area_struct *vma; -void *memcache; -bool logging_active =memslot_is_logging (fault->slot); -enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ; -struct kvm_pgtable *pgt; -enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_HANDLE_FAULT |KVM_PGTABLE_WALK_SHARED ; - -VM_BUG_ON (fault->write && fault->exec); - -if (fault->is_perm && !fault->write && !fault->exec) { -kvm_err ("Unexpected L2 read permission error\n" ); -return -EFAULT ; - } - -if (!is_protected_kvm_enabled ()) - memcache = &vcpu->arch.mmu_page_cache; -else - memcache = &vcpu->arch.pkvm_memcache; - -/* -* Permission faults just need to update the existing leaf entry, -* and so normally don't require allocations from the memcache. The -* only exception to this is when dirty logging is enabled at runtime -* and a write fault needs to collapse a block entry into a table. -*/ -if (!fault->is_perm || (logging_active && fault->write)) { -int min_pages =kvm_mmu_cache_min_pages (vcpu->arch.hw_mmu); - -if (!is_protected_kvm_enabled ()) - ret =kvm_mmu_topup_memory_cache (memcache, min_pages); -else - ret =topup_hyp_memcache (memcache, min_pages); - -if (ret) -return ret; - }/* * Let's check if we will get back a huge page backed by hugetlbfs, or
@@ -1520,11 +1483,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return -EFAULT ; } -/* -* logging_active is guaranteed to never be true for VM_PFNMAP -* memslots. -*/ -if (logging_active) { +/* Logging is guaranteed to never be active for VM_PFNMAP memslots. */ +if (memslot_is_logging (fault->slot)) { fault->force_pte = true; fault->vma.pageshift =PAGE_SHIFT ; }else {
@@ -1613,6 +1573,54 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq;mmap_read_unlock (current->mm); +return 0 ; +} + +static int user_mem_abort (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) +{ +int ret =0 ; +bool writable; +struct kvm *kvm = vcpu->kvm; +void *memcache; +bool logging_active =memslot_is_logging (fault->slot); +enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ; +struct kvm_pgtable *pgt; +enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_HANDLE_FAULT |KVM_PGTABLE_WALK_SHARED ; + +VM_BUG_ON (fault->write && fault->exec); + +if (fault->is_perm && !fault->write && !fault->exec) { +kvm_err ("Unexpected L2 read permission error\n" ); +return -EFAULT ; + } + +if (!is_protected_kvm_enabled ()) + memcache = &vcpu->arch.mmu_page_cache; +else + memcache = &vcpu->arch.pkvm_memcache; + +/* +* Permission faults just need to update the existing leaf entry, +* and so normally don't require allocations from the memcache. The +* only exception to this is when dirty logging is enabled at runtime +* and a write fault needs to collapse a block entry into a table. +*/ +if (!fault->is_perm || (logging_active && fault->write)) { +int min_pages =kvm_mmu_cache_min_pages (vcpu->arch.hw_mmu); + +if (!is_protected_kvm_enabled ()) + ret =kvm_mmu_topup_memory_cache (memcache, min_pages); +else + ret =topup_hyp_memcache (memcache, min_pages); + +if (ret) +return ret; + } + + ret =kvm_gather_fault_vma_info (vcpu, fault); +if (ret) +return ret; + fault->pfn =__kvm_faultin_pfn (fault->slot, fault->gfn, fault->write ?FOLL_WRITE :0 , &writable, &fault->page);
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
RFC PATCH 16/16 KVM: arm64: Don't bother nullifying "vma" in mem abort path
In reply to Sean ChristophersonNow that the local "vma" in kvm_gather_fault_vma_info() will naturally go out of scope when mmap_lock is dropped, don't bother nullifying the variable.
--- arch/arm64/kvm/mmu.c | 3 --- 1 file changed, 3 deletions(-)
arch/arm64/kvm/mmu.c +0 -3
@@ -1559,9 +1559,6 @@ static int kvm_gather_fault_vma_info(struct kvm_vcpu *vcpu, fault->vma.vm_flags = vma->vm_flags; fault->vma.is_cacheable =kvm_vma_is_cacheable (vma); -/* Don't use the VMA after the unlock -- it may have vanished */ - vma =NULL ; -/* * Read mmu_invalidate_seq so that KVM can detect if the results of * vma_lookup() or __kvm_faultin_pfn() become stale prior to
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
Quoted 8 lines · Sean Christopherson
Add an arm64 version of "struct
kvm_page_fault" to (hopefully) tidy up the abort path, and to pave the way for things like KVM Userfault[*] that want to consumekvm_page_faultin arch-neutral code.This is essentially one giant nop of code shuffling.
RFC as this is only compile-tested. I didn't want to spend time testing until I got feedback on whether or not y'all are amenable to the general idea.
I appreciate the improved scoping around things like mmap lock, so this seems like a net-win in terms of readability. Just want to clarify the way this gets consumed from arch-neutral code and actually read in detail.
Thanks,
Oliver