In many cases, retrieving kvm_memslots is followed by a check on the generation of the slots. Introduce a helper function that either does the check alone, or compounds it with returning the struct kvm_memslots* to the caller. Signed-off-by: Paolo Bonzini Link: https://patch.msgid.link/20260918135030.171564-21-pbonzini@redhat.com Signed-off-by: Paolo Bonzini --- arch/loongarch/kvm/vcpu.c | 12 ++++-------- arch/x86/kvm/x86.c | 10 ++-------- include/linux/kvm_host.h | 13 +++++++++++++ virt/kvm/kvm_main.c | 8 ++++---- virt/kvm/pfncache.c | 10 ++++------ 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 059a6d605de7..c3e71ef983bc 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -154,7 +154,6 @@ static void kvm_update_stolen_time(struct kvm_vcpu *vcpu) u32 version; u64 steal; gpa_t gpa; - struct kvm_memslots *slots; struct kvm_steal_time __user *st; struct gfn_to_hva_cache *ghc; @@ -164,9 +163,8 @@ static void kvm_update_stolen_time(struct kvm_vcpu *vcpu) return; gpa &= KVM_STEAL_PHYS_MASK; - slots = kvm_memslots(vcpu->kvm); - if (unlikely(slots->generation != ghc->generation || gpa != ghc->gpa || - kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { + if (unlikely(!kvm_check_gen(vcpu->kvm, ghc->generation) || gpa != ghc->gpa || + kvm_is_error_hva(ghc->hva) || !ghc->memslot) { if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st))) { ghc->gpa = INVALID_GPA; return; @@ -1803,7 +1801,6 @@ static void kvm_vcpu_set_pv_preempted(struct kvm_vcpu *vcpu) { gpa_t gpa; struct gfn_to_hva_cache *ghc; - struct kvm_memslots *slots; struct kvm_steal_time __user *st; gpa = vcpu->arch.st.guest_addr; @@ -1820,9 +1817,8 @@ static void kvm_vcpu_set_pv_preempted(struct kvm_vcpu *vcpu) gpa &= KVM_STEAL_PHYS_MASK; ghc = &vcpu->arch.st.cache; - slots = kvm_memslots(vcpu->kvm); - if (unlikely(slots->generation != ghc->generation || gpa != ghc->gpa || - kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { + if (unlikely(!kvm_check_gen(vcpu->kvm, ghc->generation) || gpa != ghc->gpa || + kvm_is_error_hva(ghc->hva) || !ghc->memslot) { if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st))) { ghc->gpa = INVALID_GPA; return; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ab7a859dda79..8574d75df4dc 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2062,7 +2062,6 @@ static void record_steal_time(struct kvm_vcpu *vcpu) { struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; struct kvm_steal_time __user *st; - struct kvm_memslots *slots; gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; u64 steal; u32 version; @@ -2078,9 +2077,7 @@ static void record_steal_time(struct kvm_vcpu *vcpu) if (WARN_ON_ONCE(current->mm != vcpu->kvm->mm)) return; - slots = kvm_memslots(vcpu->kvm); - - if (unlikely(slots->generation != ghc->generation || + if (unlikely(!kvm_check_gen(vcpu->kvm, ghc->generation) || gpa != ghc->gpa || kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { /* We rely on the fact that it fits in a single page. */ @@ -2630,7 +2627,6 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) { struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; struct kvm_steal_time __user *st; - struct kvm_memslots *slots; static const u8 preempted = KVM_VCPU_PREEMPTED; gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; @@ -2657,9 +2653,7 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) if (unlikely(current->mm != vcpu->kvm->mm)) return; - slots = kvm_memslots(vcpu->kvm); - - if (unlikely(slots->generation != ghc->generation || + if (unlikely(!kvm_check_gen(vcpu->kvm, ghc->generation) || gpa != ghc->gpa || kvm_is_error_hva(ghc->hva) || !ghc->memslot)) return; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4a1985fe2737..5cbb50afdb94 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2676,6 +2676,19 @@ static inline int kvm_mem_attributes_may_write_gfn(struct kvm *kvm, gfn_t gfn) return kvm_mem_attributes_may_write(attrs); } +static inline bool kvm_memslots_check_gen(struct kvm *kvm, u64 slots_generation, struct kvm_memslots **p_slots) +{ + struct kvm_memslots *slots = *p_slots = kvm_memslots(kvm); + return slots->generation == slots_generation; +} + +static inline bool kvm_check_gen(struct kvm *kvm, u64 slots_generation) +{ + struct kvm_memslots *slots; + return kvm_memslots_check_gen(kvm, slots_generation, &slots); +} + + #ifdef CONFIG_KVM_GUEST_MEMFD bool kvm_gmem_is_private_gfn(struct kvm *kvm, gfn_t gfn); bool kvm_arch_supports_gmem_init_shared(struct kvm *kvm); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 635730b5930c..da12f4d43c91 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3635,14 +3635,14 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, void *data, unsigned int offset, unsigned long len) { - struct kvm_memslots *slots = kvm_memslots(kvm); + struct kvm_memslots *slots; int r; gpa_t gpa = ghc->gpa + offset; if (WARN_ON_ONCE(len + offset > ghc->len)) return -EINVAL; - if (slots->generation != ghc->generation) { + if (unlikely(!kvm_memslots_check_gen(kvm, ghc->generation, &slots))) { if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len)) return -EFAULT; } @@ -3673,14 +3673,14 @@ int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, void *data, unsigned int offset, unsigned long len) { - struct kvm_memslots *slots = kvm_memslots(kvm); + struct kvm_memslots *slots; int r; gpa_t gpa = ghc->gpa + offset; if (WARN_ON_ONCE(len + offset > ghc->len)) return -EINVAL; - if (slots->generation != ghc->generation) { + if (unlikely(!kvm_memslots_check_gen(kvm, ghc->generation, &slots))) { if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len)) return -EFAULT; } diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 3659686b97c2..ac02f6a065cc 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -72,8 +72,6 @@ static bool kvm_gpc_is_valid_len(gpa_t gpa, unsigned long uhva, bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) { - struct kvm_memslots *slots = kvm_memslots(gpc->kvm); - if (!gpc->active) return false; @@ -81,7 +79,7 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) * If the page was cached from a memslot, make sure the memslots have * not been re-configured. */ - if (!kvm_is_error_gpa(gpc->gpa) && gpc->generation != slots->generation) + if (!kvm_is_error_gpa(gpc->gpa) && !kvm_check_gen(gpc->kvm, gpc->generation)) return false; if (kvm_is_error_hva(gpc->uhva)) @@ -290,12 +288,12 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned l if (gpc->uhva != old_uhva) hva_change = true; } else { - struct kvm_memslots *slots = kvm_memslots(gpc->kvm); + struct kvm_memslots *slots; page_offset = offset_in_page(gpa); - if (gpc->gpa != gpa || gpc->generation != slots->generation || - kvm_is_error_hva(gpc->uhva)) { + if (!kvm_memslots_check_gen(gpc->kvm, gpc->generation, &slots) || + gpc->gpa != gpa || kvm_is_error_hva(gpc->uhva)) { gfn_t gfn = gpa_to_gfn(gpa); gpc->gpa = gpa; -- 2.52.0