Skip to content

Commit 70ea13f

Browse files
committed
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 9a4506e ("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 9a4506e, 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: 9a4506e ("KVM: PPC: Book3S HV: Make radix handle process scoped LPID flush in C, with relocation on") Signed-off-by: Paul Mackerras <[email protected]>
1 parent 2940ba0 commit 70ea13f

File tree

4 files changed

+53
-63
lines changed

4 files changed

+53
-63
lines changed

arch/powerpc/include/asm/kvm_ppc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ extern void kvm_hv_vm_activated(void);
476476
extern void kvm_hv_vm_deactivated(void);
477477
extern bool kvm_hv_mode_active(void);
478478

479-
extern void kvmppc_hpt_check_need_tlb_flush(struct kvm *kvm);
479+
extern void kvmppc_check_need_tlb_flush(struct kvm *kvm, int pcpu,
480+
struct kvm_nested_guest *nested);
480481

481482
#else
482483
static inline void __init kvm_cma_reserve(void)

arch/powerpc/kvm/book3s_hv.c

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,37 +2584,6 @@ static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
25842584
}
25852585
}
25862586

2587-
static void kvmppc_radix_check_need_tlb_flush(struct kvm *kvm, int pcpu,
2588-
struct kvm_nested_guest *nested)
2589-
{
2590-
cpumask_t *need_tlb_flush;
2591-
int lpid;
2592-
2593-
if (!cpu_has_feature(CPU_FTR_HVMODE))
2594-
return;
2595-
2596-
if (cpu_has_feature(CPU_FTR_ARCH_300))
2597-
pcpu &= ~0x3UL;
2598-
2599-
if (nested) {
2600-
lpid = nested->shadow_lpid;
2601-
need_tlb_flush = &nested->need_tlb_flush;
2602-
} else {
2603-
lpid = kvm->arch.lpid;
2604-
need_tlb_flush = &kvm->arch.need_tlb_flush;
2605-
}
2606-
2607-
mtspr(SPRN_LPID, lpid);
2608-
isync();
2609-
smp_mb();
2610-
2611-
if (cpumask_test_cpu(pcpu, need_tlb_flush)) {
2612-
radix__local_flush_tlb_lpid_guest(lpid);
2613-
/* Clear the bit after the TLB flush */
2614-
cpumask_clear_cpu(pcpu, need_tlb_flush);
2615-
}
2616-
}
2617-
26182587
static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
26192588
{
26202589
int cpu;
@@ -3308,20 +3277,6 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
33083277
for (sub = 0; sub < core_info.n_subcores; ++sub)
33093278
spin_unlock(&core_info.vc[sub]->lock);
33103279

3311-
if (kvm_is_radix(vc->kvm)) {
3312-
/*
3313-
* Do we need to flush the process scoped TLB for the LPAR?
3314-
*
3315-
* On POWER9, individual threads can come in here, but the
3316-
* TLB is shared between the 4 threads in a core, hence
3317-
* invalidating on one thread invalidates for all.
3318-
* Thus we make all 4 threads use the same bit here.
3319-
*
3320-
* Hash must be flushed in realmode in order to use tlbiel.
3321-
*/
3322-
kvmppc_radix_check_need_tlb_flush(vc->kvm, pcpu, NULL);
3323-
}
3324-
33253280
guest_enter_irqoff();
33263281

33273282
srcu_idx = srcu_read_lock(&vc->kvm->srcu);
@@ -4050,7 +4005,7 @@ int kvmhv_run_single_vcpu(struct kvm_run *kvm_run,
40504005
unsigned long lpcr)
40514006
{
40524007
int trap, r, pcpu;
4053-
int srcu_idx;
4008+
int srcu_idx, lpid;
40544009
struct kvmppc_vcore *vc;
40554010
struct kvm *kvm = vcpu->kvm;
40564011
struct kvm_nested_guest *nested = vcpu->arch.nested;
@@ -4126,8 +4081,12 @@ int kvmhv_run_single_vcpu(struct kvm_run *kvm_run,
41264081
vc->vcore_state = VCORE_RUNNING;
41274082
trace_kvmppc_run_core(vc, 0);
41284083

4129-
if (cpu_has_feature(CPU_FTR_HVMODE))
4130-
kvmppc_radix_check_need_tlb_flush(kvm, pcpu, nested);
4084+
if (cpu_has_feature(CPU_FTR_HVMODE)) {
4085+
lpid = nested ? nested->shadow_lpid : kvm->arch.lpid;
4086+
mtspr(SPRN_LPID, lpid);
4087+
isync();
4088+
kvmppc_check_need_tlb_flush(kvm, pcpu, nested);
4089+
}
41314090

41324091
trace_hardirqs_on();
41334092
guest_enter_irqoff();

arch/powerpc/kvm/book3s_hv_builtin.c

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -806,11 +806,40 @@ void kvmppc_guest_entry_inject_int(struct kvm_vcpu *vcpu)
806806
}
807807
}
808808

809-
void kvmppc_hpt_check_need_tlb_flush(struct kvm *kvm)
809+
static void flush_guest_tlb(struct kvm *kvm)
810810
{
811-
int pcpu = raw_smp_processor_id();
812811
unsigned long rb, set;
813812

813+
rb = PPC_BIT(52); /* IS = 2 */
814+
if (kvm_is_radix(kvm)) {
815+
/* R=1 PRS=1 RIC=2 */
816+
asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
817+
: : "r" (rb), "i" (1), "i" (1), "i" (2),
818+
"r" (0) : "memory");
819+
for (set = 1; set < kvm->arch.tlb_sets; ++set) {
820+
rb += PPC_BIT(51); /* increment set number */
821+
/* R=1 PRS=1 RIC=0 */
822+
asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
823+
: : "r" (rb), "i" (1), "i" (1), "i" (0),
824+
"r" (0) : "memory");
825+
}
826+
} else {
827+
for (set = 0; set < kvm->arch.tlb_sets; ++set) {
828+
/* R=0 PRS=0 RIC=0 */
829+
asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
830+
: : "r" (rb), "i" (0), "i" (0), "i" (0),
831+
"r" (0) : "memory");
832+
rb += PPC_BIT(51); /* increment set number */
833+
}
834+
}
835+
asm volatile("ptesync": : :"memory");
836+
}
837+
838+
void kvmppc_check_need_tlb_flush(struct kvm *kvm, int pcpu,
839+
struct kvm_nested_guest *nested)
840+
{
841+
cpumask_t *need_tlb_flush;
842+
814843
/*
815844
* On POWER9, individual threads can come in here, but the
816845
* 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)
820849
if (cpu_has_feature(CPU_FTR_ARCH_300))
821850
pcpu = cpu_first_thread_sibling(pcpu);
822851

823-
if (cpumask_test_cpu(pcpu, &kvm->arch.need_tlb_flush)) {
824-
rb = PPC_BIT(52); /* IS = 2 */
825-
for (set = 0; set < kvm->arch.tlb_sets; ++set) {
826-
asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
827-
: : "r" (rb), "i" (0), "i" (0), "i" (0),
828-
"r" (0) : "memory");
829-
rb += PPC_BIT(51); /* increment set number */
830-
}
831-
asm volatile("ptesync": : :"memory");
852+
if (nested)
853+
need_tlb_flush = &nested->need_tlb_flush;
854+
else
855+
need_tlb_flush = &kvm->arch.need_tlb_flush;
856+
857+
if (cpumask_test_cpu(pcpu, need_tlb_flush)) {
858+
flush_guest_tlb(kvm);
832859

833860
/* Clear the bit after the TLB flush */
834-
cpumask_clear_cpu(pcpu, &kvm->arch.need_tlb_flush);
861+
cpumask_clear_cpu(pcpu, need_tlb_flush);
835862
}
836863
}
864+
EXPORT_SYMBOL_GPL(kvmppc_check_need_tlb_flush);

arch/powerpc/kvm/book3s_hv_rmhandlers.S

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,11 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
622622
mtspr SPRN_LPID,r7
623623
isync
624624

625-
/* See if we need to flush the TLB. Hash has to be done in RM */
625+
/* See if we need to flush the TLB. */
626626
mr r3, r9 /* kvm pointer */
627-
bl kvmppc_hpt_check_need_tlb_flush
627+
lhz r4, PACAPACAINDEX(r13) /* physical cpu number */
628+
li r5, 0 /* nested vcpu pointer */
629+
bl kvmppc_check_need_tlb_flush
628630
nop
629631
ld r5, HSTATE_KVM_VCORE(r13)
630632

0 commit comments

Comments
 (0)