KVM: PPC: Book3S HV: Flush TLB on secondary radix threads

When running on POWER9 with kvm_hv.indep_threads_mode = N and the host
in SMT1 mode, KVM will run guest VCPUs on offline secondary threads.
If those guests are in radix mode, we fail to load the LPID and flush
the TLB if necessary, leading to the guest crashing with an
unsupported MMU fault.  This arises from commit 9a4506e11b ("KVM:
PPC: Book3S HV: Make radix handle process scoped LPID flush in C,
with relocation on", 2018-05-17), which didn't consider the case
where indep_threads_mode = N.

For simplicity, this makes the real-mode guest entry path flush the
TLB in the same place for both radix and hash guests, as we did before
9a4506e11b, though the code is now C code rather than assembly code.
We also have the radix TLB flush open-coded rather than calling
radix__local_flush_tlb_lpid_guest(), because the TLB flush can be
called in real mode, and in real mode we don't want to invoke the
tracepoint code.

Fixes: 9a4506e11b ("KVM: PPC: Book3S HV: Make radix handle process scoped LPID flush in C, with relocation on")
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
This commit is contained in:
Paul Mackerras 2019-04-29 19:02:58 +10:00
parent 2940ba0c48
commit 70ea13f6e6
4 changed files with 53 additions and 63 deletions

View File

@ -476,7 +476,8 @@ extern void kvm_hv_vm_activated(void);
extern void kvm_hv_vm_deactivated(void); extern void kvm_hv_vm_deactivated(void);
extern bool kvm_hv_mode_active(void); extern bool kvm_hv_mode_active(void);
extern void kvmppc_hpt_check_need_tlb_flush(struct kvm *kvm); extern void kvmppc_check_need_tlb_flush(struct kvm *kvm, int pcpu,
struct kvm_nested_guest *nested);
#else #else
static inline void __init kvm_cma_reserve(void) static inline void __init kvm_cma_reserve(void)

View File

@ -2584,37 +2584,6 @@ static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
} }
} }
static void kvmppc_radix_check_need_tlb_flush(struct kvm *kvm, int pcpu,
struct kvm_nested_guest *nested)
{
cpumask_t *need_tlb_flush;
int lpid;
if (!cpu_has_feature(CPU_FTR_HVMODE))
return;
if (cpu_has_feature(CPU_FTR_ARCH_300))
pcpu &= ~0x3UL;
if (nested) {
lpid = nested->shadow_lpid;
need_tlb_flush = &nested->need_tlb_flush;
} else {
lpid = kvm->arch.lpid;
need_tlb_flush = &kvm->arch.need_tlb_flush;
}
mtspr(SPRN_LPID, lpid);
isync();
smp_mb();
if (cpumask_test_cpu(pcpu, need_tlb_flush)) {
radix__local_flush_tlb_lpid_guest(lpid);
/* Clear the bit after the TLB flush */
cpumask_clear_cpu(pcpu, need_tlb_flush);
}
}
static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc) static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
{ {
int cpu; int cpu;
@ -3308,20 +3277,6 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
for (sub = 0; sub < core_info.n_subcores; ++sub) for (sub = 0; sub < core_info.n_subcores; ++sub)
spin_unlock(&core_info.vc[sub]->lock); spin_unlock(&core_info.vc[sub]->lock);
if (kvm_is_radix(vc->kvm)) {
/*
* Do we need to flush the process scoped TLB for the LPAR?
*
* On POWER9, individual threads can come in here, but the
* TLB is shared between the 4 threads in a core, hence
* invalidating on one thread invalidates for all.
* Thus we make all 4 threads use the same bit here.
*
* Hash must be flushed in realmode in order to use tlbiel.
*/
kvmppc_radix_check_need_tlb_flush(vc->kvm, pcpu, NULL);
}
guest_enter_irqoff(); guest_enter_irqoff();
srcu_idx = srcu_read_lock(&vc->kvm->srcu); srcu_idx = srcu_read_lock(&vc->kvm->srcu);
@ -4050,7 +4005,7 @@ int kvmhv_run_single_vcpu(struct kvm_run *kvm_run,
unsigned long lpcr) unsigned long lpcr)
{ {
int trap, r, pcpu; int trap, r, pcpu;
int srcu_idx; int srcu_idx, lpid;
struct kvmppc_vcore *vc; struct kvmppc_vcore *vc;
struct kvm *kvm = vcpu->kvm; struct kvm *kvm = vcpu->kvm;
struct kvm_nested_guest *nested = vcpu->arch.nested; struct kvm_nested_guest *nested = vcpu->arch.nested;
@ -4126,8 +4081,12 @@ int kvmhv_run_single_vcpu(struct kvm_run *kvm_run,
vc->vcore_state = VCORE_RUNNING; vc->vcore_state = VCORE_RUNNING;
trace_kvmppc_run_core(vc, 0); trace_kvmppc_run_core(vc, 0);
if (cpu_has_feature(CPU_FTR_HVMODE)) if (cpu_has_feature(CPU_FTR_HVMODE)) {
kvmppc_radix_check_need_tlb_flush(kvm, pcpu, nested); lpid = nested ? nested->shadow_lpid : kvm->arch.lpid;
mtspr(SPRN_LPID, lpid);
isync();
kvmppc_check_need_tlb_flush(kvm, pcpu, nested);
}
trace_hardirqs_on(); trace_hardirqs_on();
guest_enter_irqoff(); guest_enter_irqoff();

View File

@ -806,11 +806,40 @@ void kvmppc_guest_entry_inject_int(struct kvm_vcpu *vcpu)
} }
} }
void kvmppc_hpt_check_need_tlb_flush(struct kvm *kvm) static void flush_guest_tlb(struct kvm *kvm)
{ {
int pcpu = raw_smp_processor_id();
unsigned long rb, set; unsigned long rb, set;
rb = PPC_BIT(52); /* IS = 2 */
if (kvm_is_radix(kvm)) {
/* R=1 PRS=1 RIC=2 */
asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
: : "r" (rb), "i" (1), "i" (1), "i" (2),
"r" (0) : "memory");
for (set = 1; set < kvm->arch.tlb_sets; ++set) {
rb += PPC_BIT(51); /* increment set number */
/* R=1 PRS=1 RIC=0 */
asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
: : "r" (rb), "i" (1), "i" (1), "i" (0),
"r" (0) : "memory");
}
} else {
for (set = 0; set < kvm->arch.tlb_sets; ++set) {
/* R=0 PRS=0 RIC=0 */
asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
: : "r" (rb), "i" (0), "i" (0), "i" (0),
"r" (0) : "memory");
rb += PPC_BIT(51); /* increment set number */
}
}
asm volatile("ptesync": : :"memory");
}
void kvmppc_check_need_tlb_flush(struct kvm *kvm, int pcpu,
struct kvm_nested_guest *nested)
{
cpumask_t *need_tlb_flush;
/* /*
* On POWER9, individual threads can come in here, but the * On POWER9, individual threads can come in here, but the
* TLB is shared between the 4 threads in a core, hence * TLB is shared between the 4 threads in a core, hence
@ -820,17 +849,16 @@ void kvmppc_hpt_check_need_tlb_flush(struct kvm *kvm)
if (cpu_has_feature(CPU_FTR_ARCH_300)) if (cpu_has_feature(CPU_FTR_ARCH_300))
pcpu = cpu_first_thread_sibling(pcpu); pcpu = cpu_first_thread_sibling(pcpu);
if (cpumask_test_cpu(pcpu, &kvm->arch.need_tlb_flush)) { if (nested)
rb = PPC_BIT(52); /* IS = 2 */ need_tlb_flush = &nested->need_tlb_flush;
for (set = 0; set < kvm->arch.tlb_sets; ++set) { else
asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1) need_tlb_flush = &kvm->arch.need_tlb_flush;
: : "r" (rb), "i" (0), "i" (0), "i" (0),
"r" (0) : "memory"); if (cpumask_test_cpu(pcpu, need_tlb_flush)) {
rb += PPC_BIT(51); /* increment set number */ flush_guest_tlb(kvm);
}
asm volatile("ptesync": : :"memory");
/* Clear the bit after the TLB flush */ /* Clear the bit after the TLB flush */
cpumask_clear_cpu(pcpu, &kvm->arch.need_tlb_flush); cpumask_clear_cpu(pcpu, need_tlb_flush);
} }
} }
EXPORT_SYMBOL_GPL(kvmppc_check_need_tlb_flush);

View File

@ -622,9 +622,11 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
mtspr SPRN_LPID,r7 mtspr SPRN_LPID,r7
isync isync
/* See if we need to flush the TLB. Hash has to be done in RM */ /* See if we need to flush the TLB. */
mr r3, r9 /* kvm pointer */ mr r3, r9 /* kvm pointer */
bl kvmppc_hpt_check_need_tlb_flush lhz r4, PACAPACAINDEX(r13) /* physical cpu number */
li r5, 0 /* nested vcpu pointer */
bl kvmppc_check_need_tlb_flush
nop nop
ld r5, HSTATE_KVM_VCORE(r13) ld r5, HSTATE_KVM_VCORE(r13)