Skip to content

Commit 412d070

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull kvm fixes from Paolo Bonzini: "ARM: - Correctly save/restore PMUSERNR_EL0 when host userspace is using PMU counters directly - Fix GICv2 emulation on GICv3 after the locking rework - Don't use smp_processor_id() in kvm_pmu_probe_armpmu(), and document why Generic: - Avoid setting page table entries pointing to a deleted memslot if a host page table entry is changed concurrently with the deletion" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: KVM: Avoid illegal stage2 mapping on invalid memory slot KVM: arm64: Use raw_smp_processor_id() in kvm_pmu_probe_armpmu() KVM: arm64: Restore GICv2-on-GICv3 functionality KVM: arm64: PMU: Don't overwrite PMUSERENR with vcpu loaded KVM: arm64: PMU: Restore the host's PMUSERENR_EL0
2 parents e7758c0 + 2623b3d commit 412d070

File tree

9 files changed

+129
-11
lines changed

9 files changed

+129
-11
lines changed

arch/arm/include/asm/arm_pmuv3.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
222222
return false;
223223
}
224224

225+
static inline bool kvm_set_pmuserenr(u64 val)
226+
{
227+
return false;
228+
}
229+
225230
/* PMU Version in DFR Register */
226231
#define ARMV8_PMU_DFR_VER_NI 0
227232
#define ARMV8_PMU_DFR_VER_V3P4 0x5

arch/arm64/include/asm/kvm_host.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,8 @@ struct kvm_vcpu_arch {
699699
#define SYSREGS_ON_CPU __vcpu_single_flag(sflags, BIT(4))
700700
/* Software step state is Active-pending */
701701
#define DBG_SS_ACTIVE_PENDING __vcpu_single_flag(sflags, BIT(5))
702+
/* PMUSERENR for the guest EL0 is on physical CPU */
703+
#define PMUSERENR_ON_CPU __vcpu_single_flag(sflags, BIT(6))
702704

703705

704706
/* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */
@@ -1065,9 +1067,14 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
10651067
#ifdef CONFIG_KVM
10661068
void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr);
10671069
void kvm_clr_pmu_events(u32 clr);
1070+
bool kvm_set_pmuserenr(u64 val);
10681071
#else
10691072
static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
10701073
static inline void kvm_clr_pmu_events(u32 clr) {}
1074+
static inline bool kvm_set_pmuserenr(u64 val)
1075+
{
1076+
return false;
1077+
}
10711078
#endif
10721079

10731080
void kvm_vcpu_load_sysregs_vhe(struct kvm_vcpu *vcpu);

arch/arm64/kvm/hyp/include/hyp/switch.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
8282
* EL1 instead of being trapped to EL2.
8383
*/
8484
if (kvm_arm_support_pmu_v3()) {
85+
struct kvm_cpu_context *hctxt;
86+
8587
write_sysreg(0, pmselr_el0);
88+
89+
hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
90+
ctxt_sys_reg(hctxt, PMUSERENR_EL0) = read_sysreg(pmuserenr_el0);
8691
write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
92+
vcpu_set_flag(vcpu, PMUSERENR_ON_CPU);
8793
}
8894

8995
vcpu->arch.mdcr_el2_host = read_sysreg(mdcr_el2);
@@ -106,8 +112,13 @@ static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
106112
write_sysreg(vcpu->arch.mdcr_el2_host, mdcr_el2);
107113

108114
write_sysreg(0, hstr_el2);
109-
if (kvm_arm_support_pmu_v3())
110-
write_sysreg(0, pmuserenr_el0);
115+
if (kvm_arm_support_pmu_v3()) {
116+
struct kvm_cpu_context *hctxt;
117+
118+
hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
119+
write_sysreg(ctxt_sys_reg(hctxt, PMUSERENR_EL0), pmuserenr_el0);
120+
vcpu_clear_flag(vcpu, PMUSERENR_ON_CPU);
121+
}
111122

112123
if (cpus_have_final_cap(ARM64_SME)) {
113124
sysreg_clear_set_s(SYS_HFGRTR_EL2, 0,

arch/arm64/kvm/hyp/vhe/switch.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,28 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
9292
}
9393
NOKPROBE_SYMBOL(__deactivate_traps);
9494

95+
/*
96+
* Disable IRQs in {activate,deactivate}_traps_vhe_{load,put}() to
97+
* prevent a race condition between context switching of PMUSERENR_EL0
98+
* in __{activate,deactivate}_traps_common() and IPIs that attempts to
99+
* update PMUSERENR_EL0. See also kvm_set_pmuserenr().
100+
*/
95101
void activate_traps_vhe_load(struct kvm_vcpu *vcpu)
96102
{
103+
unsigned long flags;
104+
105+
local_irq_save(flags);
97106
__activate_traps_common(vcpu);
107+
local_irq_restore(flags);
98108
}
99109

100110
void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu)
101111
{
112+
unsigned long flags;
113+
114+
local_irq_save(flags);
102115
__deactivate_traps_common(vcpu);
116+
local_irq_restore(flags);
103117
}
104118

105119
static const exit_handler_fn hyp_exit_handlers[] = {

arch/arm64/kvm/pmu-emul.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,25 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(void)
700700

701701
mutex_lock(&arm_pmus_lock);
702702

703-
cpu = smp_processor_id();
703+
/*
704+
* It is safe to use a stale cpu to iterate the list of PMUs so long as
705+
* the same value is used for the entirety of the loop. Given this, and
706+
* the fact that no percpu data is used for the lookup there is no need
707+
* to disable preemption.
708+
*
709+
* It is still necessary to get a valid cpu, though, to probe for the
710+
* default PMU instance as userspace is not required to specify a PMU
711+
* type. In order to uphold the preexisting behavior KVM selects the
712+
* PMU instance for the core where the first call to the
713+
* KVM_ARM_VCPU_PMU_V3_CTRL attribute group occurs. A dependent use case
714+
* would be a user with disdain of all things big.LITTLE that affines
715+
* the VMM to a particular cluster of cores.
716+
*
717+
* In any case, userspace should just do the sane thing and use the UAPI
718+
* to select a PMU type directly. But, be wary of the baggage being
719+
* carried here.
720+
*/
721+
cpu = raw_smp_processor_id();
704722
list_for_each_entry(entry, &arm_pmus, entry) {
705723
tmp = entry->arm_pmu;
706724

arch/arm64/kvm/pmu.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,30 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu)
209209
kvm_vcpu_pmu_enable_el0(events_host);
210210
kvm_vcpu_pmu_disable_el0(events_guest);
211211
}
212+
213+
/*
214+
* With VHE, keep track of the PMUSERENR_EL0 value for the host EL0 on the pCPU
215+
* where PMUSERENR_EL0 for the guest is loaded, since PMUSERENR_EL0 is switched
216+
* to the value for the guest on vcpu_load(). The value for the host EL0
217+
* will be restored on vcpu_put(), before returning to userspace.
218+
* This isn't necessary for nVHE, as the register is context switched for
219+
* every guest enter/exit.
220+
*
221+
* Return true if KVM takes care of the register. Otherwise return false.
222+
*/
223+
bool kvm_set_pmuserenr(u64 val)
224+
{
225+
struct kvm_cpu_context *hctxt;
226+
struct kvm_vcpu *vcpu;
227+
228+
if (!kvm_arm_support_pmu_v3() || !has_vhe())
229+
return false;
230+
231+
vcpu = kvm_get_running_vcpu();
232+
if (!vcpu || !vcpu_get_flag(vcpu, PMUSERENR_ON_CPU))
233+
return false;
234+
235+
hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
236+
ctxt_sys_reg(hctxt, PMUSERENR_EL0) = val;
237+
return true;
238+
}

arch/arm64/kvm/vgic/vgic-init.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ int vgic_lazy_init(struct kvm *kvm)
446446
int kvm_vgic_map_resources(struct kvm *kvm)
447447
{
448448
struct vgic_dist *dist = &kvm->arch.vgic;
449+
enum vgic_type type;
449450
gpa_t dist_base;
450451
int ret = 0;
451452

@@ -460,10 +461,13 @@ int kvm_vgic_map_resources(struct kvm *kvm)
460461
if (!irqchip_in_kernel(kvm))
461462
goto out;
462463

463-
if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
464+
if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2) {
464465
ret = vgic_v2_map_resources(kvm);
465-
else
466+
type = VGIC_V2;
467+
} else {
466468
ret = vgic_v3_map_resources(kvm);
469+
type = VGIC_V3;
470+
}
467471

468472
if (ret) {
469473
__kvm_vgic_destroy(kvm);
@@ -473,8 +477,7 @@ int kvm_vgic_map_resources(struct kvm *kvm)
473477
dist_base = dist->vgic_dist_base;
474478
mutex_unlock(&kvm->arch.config_lock);
475479

476-
ret = vgic_register_dist_iodev(kvm, dist_base,
477-
kvm_vgic_global_state.type);
480+
ret = vgic_register_dist_iodev(kvm, dist_base, type);
478481
if (ret) {
479482
kvm_err("Unable to register VGIC dist MMIO regions\n");
480483
kvm_vgic_destroy(kvm);

drivers/perf/arm_pmuv3.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,25 @@ static inline u32 armv8pmu_getreset_flags(void)
677677
return value;
678678
}
679679

680+
static void update_pmuserenr(u64 val)
681+
{
682+
lockdep_assert_irqs_disabled();
683+
684+
/*
685+
* The current PMUSERENR_EL0 value might be the value for the guest.
686+
* If that's the case, have KVM keep tracking of the register value
687+
* for the host EL0 so that KVM can restore it before returning to
688+
* the host EL0. Otherwise, update the register now.
689+
*/
690+
if (kvm_set_pmuserenr(val))
691+
return;
692+
693+
write_pmuserenr(val);
694+
}
695+
680696
static void armv8pmu_disable_user_access(void)
681697
{
682-
write_pmuserenr(0);
698+
update_pmuserenr(0);
683699
}
684700

685701
static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu)
@@ -695,8 +711,7 @@ static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu)
695711
armv8pmu_write_evcntr(i, 0);
696712
}
697713

698-
write_pmuserenr(0);
699-
write_pmuserenr(ARMV8_PMU_USERENR_ER | ARMV8_PMU_USERENR_CR);
714+
update_pmuserenr(ARMV8_PMU_USERENR_ER | ARMV8_PMU_USERENR_CR);
700715
}
701716

702717
static void armv8pmu_enable_event(struct perf_event *event)

virt/kvm/kvm_main.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,24 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn
686686

687687
return __kvm_handle_hva_range(kvm, &range);
688688
}
689+
690+
static bool kvm_change_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
691+
{
692+
/*
693+
* Skipping invalid memslots is correct if and only change_pte() is
694+
* surrounded by invalidate_range_{start,end}(), which is currently
695+
* guaranteed by the primary MMU. If that ever changes, KVM needs to
696+
* unmap the memslot instead of skipping the memslot to ensure that KVM
697+
* doesn't hold references to the old PFN.
698+
*/
699+
WARN_ON_ONCE(!READ_ONCE(kvm->mn_active_invalidate_count));
700+
701+
if (range->slot->flags & KVM_MEMSLOT_INVALID)
702+
return false;
703+
704+
return kvm_set_spte_gfn(kvm, range);
705+
}
706+
689707
static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
690708
struct mm_struct *mm,
691709
unsigned long address,
@@ -707,7 +725,7 @@ static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
707725
if (!READ_ONCE(kvm->mmu_invalidate_in_progress))
708726
return;
709727

710-
kvm_handle_hva_range(mn, address, address + 1, pte, kvm_set_spte_gfn);
728+
kvm_handle_hva_range(mn, address, address + 1, pte, kvm_change_spte_gfn);
711729
}
712730

713731
void kvm_mmu_invalidate_begin(struct kvm *kvm, unsigned long start,

0 commit comments

Comments
 (0)