From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:53:20
This reduces radix guest full entry/exit latency on POWER9 and POWER10
by 2x.
Nested HV guests should see smaller improvements in their L1 entry/exit,
but this is also combined with most L0 speedups also applying to nested
entry. nginx localhost throughput test in a SMP nested guest is improved
about 10% (in a direct guest it doesn't change much because it uses XIVE
for IPIs) when L0 and L1 are patched.
It does this in several main ways:
- Rearrange code to optimise SPR accesses. Mainly, avoid scoreboard
stalls.
- Test SPR values to avoid mtSPRs where possible. mtSPRs are expensive.
- Reduce mftb. mftb is expensive.
- Demand fault certain facilities to avoid saving and/or restoring them
(at the cost of fault when they are used, but this is mitigated over
a number of entries, like the facilities when context switching
processes). PM, TM, and EBB so far.
- Defer some sequences that are made just in case a guest is interrupted
in the middle of a critical section to the case where the guest is
scheduled on a different CPU, rather than every time (at the cost of
an extra IPI in this case). Namely the tlbsync sequence for radix with
GTSE, which is very expensive.
- Reduce locking, barriers, atomics related to the vcpus-per-vcore > 1
handling that the P9 path does not require.
Changes since v3:
- Fix a possible bug in "Avoid tlbsync sequence on radix guest exit"
where the TLB flushing optimisation (1 thread TLBIEL flushes TLB for
entire core) might break because 'ptesync' was no longer guaranteed
to be executed on all threads (via regular exit path). Now the TLB
flush keeps track of all threads and whether they need to do a TLBIEL
or a PTESYNC. Fixing this requires a new patch "Split P8 from P9 path
guest vCPU TLB flushing".
Changes since v2:
- Rebased, several patches from the series were merged in the previous
merge window.
- Fixed some compile errors noticed by kernel test robot.
- Added RB from Athira for the PMU stuff (thanks!)
- Split TIDR ftr check (patch 2) out into its own patch.
- Added a missed license tag on new file.
Changes since v1:
- Verified DPDES changes still work with msgsndp SMT emulation.
- Fixed HMI handling bug.
- Split softpatch handling fixes into smaller pieces.
- Rebased with Fabiano's latest HV sanitising patches.
- Fix TM demand faulting bug causing nested guest TM tests to TM Bad
Thing the host in rare cases.
- Re-name new "pmu=" command line option to "pmu_override=" and update
documentation wording.
- Add default=y config option rather than unconditionally removing the
L0 nested PMU workaround.
- Remove unnecessary MSR[RI] updates in entry/exit. Down to about 4700
cycles now.
- Another bugfix from Alexey's testing.
Changes since RFC:
- Rebased with Fabiano's HV sanitising patches at the front.
- Several demand faulting bug fixes mostly relating to nested guests.
- Removed facility demand-faulting from L0 nested entry/exit handler.
Demand faulting is still done in the L1, but not the L0. The reason
is to reduce complexity (although it's only a small amount of
complexity), reduce demand faulting overhead that may require several
Thanks,
Nick
Nicholas Piggin (53):
powerpc/64s: Remove WORT SPR from POWER9/10 (take 2)
powerpc/64s: guard optional TIDR SPR with CPU ftr test
KMV: PPC: Book3S HV P9: Use set_dec to set decrementer to host
KVM: PPC: Book3S HV P9: Use host timer accounting to avoid decrementer
read
KVM: PPC: Book3S HV P9: Use large decrementer for HDEC
KVM: PPC: Book3S HV P9: Reduce mftb per guest entry/exit
powerpc/time: add API for KVM to re-arm the host timer/decrementer
KVM: PPC: Book3S HV: POWER10 enable HAIL when running radix guests
powerpc/64s: Keep AMOR SPR a constant ~0 at runtime
KVM: PPC: Book3S HV: Don't always save PMU for guest capable of
nesting
powerpc/64s: Always set PMU control registers to frozen/disabled when
not in use
powerpc/64s: Implement PMU override command line option
KVM: PPC: Book3S HV P9: Implement PMU save/restore in C
KVM: PPC: Book3S HV P9: Factor PMU save/load into context switch
functions
KVM: PPC: Book3S HV P9: Demand fault PMU SPRs when marked not inuse
KVM: PPC: Book3S HV P9: Factor out yield_count increment
KVM: PPC: Book3S HV: CTRL SPR does not require read-modify-write
KVM: PPC: Book3S HV P9: Move SPRG restore to restore_p9_host_os_sprs
KVM: PPC: Book3S HV P9: Reduce mtmsrd instructions required to save
host SPRs
KVM: PPC: Book3S HV P9: Improve mtmsrd scheduling by delaying MSR[EE]
disable
KVM: PPC: Book3S HV P9: Add kvmppc_stop_thread to match
kvmppc_start_thread
KVM: PPC: Book3S HV: Change dec_expires to be relative to guest
timebase
KVM: PPC: Book3S HV P9: Move TB updates
KVM: PPC: Book3S HV P9: Optimise timebase reads
KVM: PPC: Book3S HV P9: Avoid SPR scoreboard stalls
KVM: PPC: Book3S HV P9: Only execute mtSPR if the value changed
KVM: PPC: Book3S HV P9: Juggle SPR switching around
KVM: PPC: Book3S HV P9: Move vcpu register save/restore into functions
KVM: PPC: Book3S HV P9: Move host OS save/restore functions to
built-in
KVM: PPC: Book3S HV P9: Move nested guest entry into its own function
KVM: PPC: Book3S HV P9: Move remaining SPR and MSR access into low
level entry
KVM: PPC: Book3S HV P9: Implement TM fastpath for guest entry/exit
KVM: PPC: Book3S HV P9: Switch PMU to guest as late as possible
KVM: PPC: Book3S HV P9: Restrict DSISR canary workaround to processors
that require it
KVM: PPC: Book3S HV P9: More SPR speed improvements
KVM: PPC: Book3S HV P9: Demand fault EBB facility registers
KVM: PPC: Book3S HV P9: Demand fault TM facility registers
KVM: PPC: Book3S HV P9: Use Linux SPR save/restore to manage some host
SPRs
KVM: PPC: Book3S HV P9: Comment and fix MMU context switching code
KVM: PPC: Book3S HV P9: Test dawr_enabled() before saving host DAWR
SPRs
KVM: PPC: Book3S HV P9: Don't restore PSSCR if not needed
KVM: PPC: Book3S HV: Split P8 from P9 path guest vCPU TLB flushing
KVM: PPC: Book3S HV P9: Avoid tlbsync sequence on radix guest exit
KVM: PPC: Book3S HV Nested: Avoid extra mftb() in nested entry
KVM: PPC: Book3S HV P9: Improve mfmsr performance on entry
KVM: PPC: Book3S HV P9: Optimise hash guest SLB saving
KVM: PPC: Book3S HV P9: Avoid changing MSR[RI] in entry and exit
KVM: PPC: Book3S HV P9: Add unlikely annotation for !mmu_ready
KVM: PPC: Book3S HV P9: Avoid cpu_in_guest atomics on entry and exit
KVM: PPC: Book3S HV P9: Remove most of the vcore logic
KVM: PPC: Book3S HV P9: Tidy kvmppc_create_dtl_entry
KVM: PPC: Book3S HV P9: Stop using vc->dpdes
KVM: PPC: Book3S HV P9: Remove subcore HMI handling
.../admin-guide/kernel-parameters.txt | 8 +
arch/powerpc/include/asm/asm-prototypes.h | 5 -
arch/powerpc/include/asm/kvm_asm.h | 1 +
arch/powerpc/include/asm/kvm_book3s.h | 6 +
arch/powerpc/include/asm/kvm_book3s_64.h | 5 +-
arch/powerpc/include/asm/kvm_host.h | 7 +-
arch/powerpc/include/asm/kvm_ppc.h | 4 +-
arch/powerpc/include/asm/switch_to.h | 3 +
arch/powerpc/include/asm/time.h | 19 +-
arch/powerpc/kernel/cpu_setup_power.c | 12 +-
arch/powerpc/kernel/dt_cpu_ftrs.c | 8 +-
arch/powerpc/kernel/process.c | 34 +
arch/powerpc/kernel/time.c | 54 +-
arch/powerpc/kvm/Kconfig | 15 +
arch/powerpc/kvm/book3s_64_entry.S | 11 +-
arch/powerpc/kvm/book3s_64_mmu_radix.c | 4 +
arch/powerpc/kvm/book3s_hv.c | 851 +++++++++--------
arch/powerpc/kvm/book3s_hv.h | 42 +
arch/powerpc/kvm/book3s_hv_builtin.c | 55 +-
arch/powerpc/kvm/book3s_hv_hmi.c | 7 +-
arch/powerpc/kvm/book3s_hv_interrupts.S | 13 +-
arch/powerpc/kvm/book3s_hv_nested.c | 8 +-
arch/powerpc/kvm/book3s_hv_p9_entry.c | 898 +++++++++++++++---
arch/powerpc/kvm/book3s_hv_ras.c | 54 ++
arch/powerpc/kvm/book3s_hv_rm_mmu.c | 6 -
arch/powerpc/kvm/book3s_hv_rmhandlers.S | 73 +-
arch/powerpc/mm/book3s64/radix_pgtable.c | 15 -
arch/powerpc/perf/core-book3s.c | 35 +
arch/powerpc/platforms/powernv/idle.c | 9 +-
arch/powerpc/xmon/xmon.c | 10 +-
30 files changed, 1555 insertions(+), 717 deletions(-)
create mode 100644 arch/powerpc/kvm/book3s_hv.h
--
2.23.0
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:53:51
This removes a missed remnant of the WORT SPR.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/platforms/powernv/idle.c | 1 -
1 file changed, 1 deletion(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:54:19
The TIDR SPR only exists on POWER9. Avoid accessing it when the
feature bit for it is not set.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 12 ++++++++----
arch/powerpc/xmon/xmon.c | 10 ++++++++--
2 files changed, 16 insertions(+), 6 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:54:50
The host Linux timer code arms the decrementer with the value
'decrementers_next_tb - current_tb' using set_dec(), which stores
val - 1 on Book3S-64, which is not quite the same as what KVM does
to re-arm the host decrementer when exiting the guest.
This shouldn't be a significant change, but it makes the logic match
and avoids this small extra change being brought into the next patch.
Suggested-by: Alexey Kardashevskiy <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
@@ -4063,7 +4063,7 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vc->entry_exit_map=0x101;vc->in_guest=0;-mtspr(SPRN_DEC,local_paca->kvm_hstate.dec_expires-mftb());+set_dec(local_paca->kvm_hstate.dec_expires-mftb());/* We may have raced with new irq work */if(test_irq_work_pending())set_dec(1);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:55:27
There is no need to save away the host DEC value, as it is derived
from the host timer subsystem which maintains the next timer time,
so it can be restored from there.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/time.h | 5 +++++
arch/powerpc/kernel/time.c | 1 +
arch/powerpc/kvm/book3s_hv.c | 14 +++++++-------
3 files changed, 13 insertions(+), 7 deletions(-)
@@ -4063,7 +4062,8 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vc->entry_exit_map=0x101;vc->in_guest=0;-set_dec(local_paca->kvm_hstate.dec_expires-mftb());+next_timer=timer_get_next_tb();+set_dec(next_timer-mftb());/* We may have raced with new irq work */if(test_irq_work_pending())set_dec(1);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:56:04
On processors that don't suppress the HDEC exceptions when LPCR[HDICE]=0,
this could help reduce needless guest exits due to leftover exceptions on
entering the guest.
Reviewed-by: Alexey Kardashevskiy <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/time.h | 2 ++
arch/powerpc/kernel/time.c | 1 +
arch/powerpc/kvm/book3s_hv_p9_entry.c | 3 ++-
3 files changed, 5 insertions(+), 1 deletion(-)
@@ -504,7 +504,8 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvc->tb_offset_applied=0;}-mtspr(SPRN_HDEC,0x7fffffff);+/* HDEC must be at least as large as DEC, so decrementer_max fits */+mtspr(SPRN_HDEC,decrementer_max);save_clear_guest_mmu(kvm,vcpu);switch_mmu_to_host(kvm,host_pidr);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:56:42
mftb is serialising (dispatch next-to-complete) so it is heavy weight
for a mfspr. Avoid reading it multiple times in the entry or exit paths.
A small number of cycles delay to timers is tolerable.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 4 ++--
arch/powerpc/kvm/book3s_hv_p9_entry.c | 5 +++--
2 files changed, 5 insertions(+), 4 deletions(-)
@@ -4063,7 +4063,7 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vc->in_guest=0;next_timer=timer_get_next_tb();-set_dec(next_timer-mftb());+set_dec(next_timer-tb);/* We may have raced with new irq work */if(test_irq_work_pending())set_dec(1);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:57:28
Rather than have KVM look up the host timer and fiddle with the
irq-work internal details, have the powerpc/time.c code provide a
function for KVM to re-arm the Linux timer code when exiting a
guest.
This is implementation has an improvement over existing code of
marking a decrementer interrupt as soft-pending if a timer has
expired, rather than setting DEC to a -ve value, which tended to
cause host timers to take two interrupts (first hdec to exit the
guest, then the immediate dec).
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/time.h | 16 +++-------
arch/powerpc/kernel/time.c | 52 +++++++++++++++++++++++++++------
arch/powerpc/kvm/book3s_hv.c | 7 ++---
3 files changed, 49 insertions(+), 26 deletions(-)
@@ -541,13 +551,44 @@ void arch_irq_work_raise(void)preempt_enable();}+staticvoidset_dec_or_work(u64val)+{+set_dec(val);+/* We may have raced with new irq work */+if(unlikely(test_irq_work_pending()))+set_dec(1);+}+#else /* CONFIG_IRQ_WORK */#define test_irq_work_pending() 0#define clear_irq_work_pending()+staticvoidset_dec_or_work(u64val)+{+set_dec(val);+}#endif /* CONFIG_IRQ_WORK */+#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE+voidtimer_rearm_host_dec(u64now)+{+u64*next_tb=this_cpu_ptr(&decrementers_next_tb);++WARN_ON_ONCE(!arch_irqs_disabled());+WARN_ON_ONCE(mfmsr()&MSR_EE);++if(now>=*next_tb){+local_paca->irq_happened|=PACA_IRQ_DEC;+}else{+now=*next_tb-now;+if(now<=decrementer_max)+set_dec_or_work(now);+}+}+EXPORT_SYMBOL_GPL(timer_rearm_host_dec);+#endif+/**timer_interrupt-getscalledwhenthedecrementeroverflows,*withinterruptsdisabled.
@@ -608,10 +649,7 @@ DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt)}else{now=*next_tb-now;if(now<=decrementer_max)-set_dec(now);-/* We may have raced with new irq work */-if(test_irq_work_pending())-set_dec(1);+set_dec_or_work(now);__this_cpu_inc(irq_stat.timer_irqs_others);}
@@ -845,11 +883,7 @@ static int decrementer_set_next_event(unsigned long evt,structclock_event_device*dev){__this_cpu_write(decrementers_next_tb,get_tb()+evt);-set_dec(evt);--/* We may have raced with new irq work */-if(test_irq_work_pending())-set_dec(1);+set_dec_or_work(evt);return0;}
@@ -4062,11 +4062,8 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vc->entry_exit_map=0x101;vc->in_guest=0;-next_timer=timer_get_next_tb();-set_dec(next_timer-tb);-/* We may have raced with new irq work */-if(test_irq_work_pending())-set_dec(1);+timer_rearm_host_dec(tb);+mtspr(SPRN_SPRG_VDSO_WRITE,local_paca->sprg_vdso);kvmhv_load_host_pmu();
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:58:06
HV interrupts may be taken with the MMU enabled when radix guests are
running. Enable LPCR[HAIL] on ISA v3.1 processors for radix guests.
Make this depend on the host LPCR[HAIL] being enabled. Currently that is
always enabled, but having this test means any issue that might require
LPCR[HAIL] to be disabled in the host will not have to be duplicated in
KVM.
This optimisation takes 1380 cycles off a NULL hcall entry+exit micro
benchmark on a POWER10.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 29 +++++++++++++++++++++++++----
1 file changed, 25 insertions(+), 4 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:58:45
This register controls supervisor SPR modifications, and as such is only
relevant for KVM. KVM always sets AMOR to ~0 on guest entry, and never
restores it coming back out to the host, so it can be kept constant and
avoid the mtSPR in KVM guest entry.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kernel/cpu_setup_power.c | 8 ++++++++
arch/powerpc/kernel/dt_cpu_ftrs.c | 2 ++
arch/powerpc/kvm/book3s_hv_p9_entry.c | 2 --
arch/powerpc/kvm/book3s_hv_rmhandlers.S | 2 --
arch/powerpc/mm/book3s64/radix_pgtable.c | 15 ---------------
arch/powerpc/platforms/powernv/idle.c | 8 +++-----
6 files changed, 13 insertions(+), 24 deletions(-)
@@ -306,8 +306,8 @@ struct p7_sprs {/* per thread SPRs that get lost in shallow states */u64amr;u64iamr;-u64amor;u64uamor;+/* amor is restored to constant ~0 */};staticunsignedlongpower7_idle_insn(unsignedlongtype)
@@ -378,7 +378,6 @@ static unsigned long power7_idle_insn(unsigned long type)if(cpu_has_feature(CPU_FTR_ARCH_207S)){sprs.amr=mfspr(SPRN_AMR);sprs.iamr=mfspr(SPRN_IAMR);-sprs.amor=mfspr(SPRN_AMOR);sprs.uamor=mfspr(SPRN_UAMOR);}
@@ -397,7 +396,7 @@ static unsigned long power7_idle_insn(unsigned long type)*/mtspr(SPRN_AMR,sprs.amr);mtspr(SPRN_IAMR,sprs.iamr);-mtspr(SPRN_AMOR,sprs.amor);+mtspr(SPRN_AMOR,~0);mtspr(SPRN_UAMOR,sprs.uamor);}}
@@ -686,7 +685,6 @@ static unsigned long power9_idle_stop(unsigned long psscr)sprs.amr=mfspr(SPRN_AMR);sprs.iamr=mfspr(SPRN_IAMR);-sprs.amor=mfspr(SPRN_AMOR);sprs.uamor=mfspr(SPRN_UAMOR);srr1=isa300_idle_stop_mayloss(psscr);/* go idle */
@@ -707,7 +705,7 @@ static unsigned long power9_idle_stop(unsigned long psscr)*/mtspr(SPRN_AMR,sprs.amr);mtspr(SPRN_IAMR,sprs.iamr);-mtspr(SPRN_AMOR,sprs.amor);+mtspr(SPRN_AMOR,~0);mtspr(SPRN_UAMOR,sprs.uamor);/*
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 09:59:29
Provide a config option that controls the workaround added by commit
63279eeb7f93 ("KVM: PPC: Book3S HV: Always save guest pmu for guest
capable of nesting"). The option defaults to y for now, but is expected
to go away within a few releases.
Nested capable guests running with the earlier commit 178266389794
("KVM: PPC: Book3S HV Nested: Reflect guest PMU in-use to L0 when guest
SPRs are live") will now indicate the PMU in-use status of their guests,
which means the parent does not need to unconditionally save the PMU for
nested capable guests.
After this latest round of performance optimisations, this option costs
about 540 cycles or 10% entry/exit performance on a POWER9 nested-capable
guest.
References: 178266389794 ("KVM: PPC: Book3S HV Nested: Reflect guest PMU in-use to L0 when guest SPRs are live")
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/Kconfig | 15 +++++++++++++++
arch/powerpc/kvm/book3s_hv.c | 10 ++++++++--
2 files changed, 23 insertions(+), 2 deletions(-)
@@ -4047,8 +4047,14 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vcpu->arch.vpa.dirty=1;save_pmu=lp->pmcregs_in_use;}-/* Must save pmu if this guest is capable of running nested guests */-save_pmu|=nesting_enabled(vcpu->kvm);+if(IS_ENABLED(CONFIG_KVM_BOOK3S_HV_NESTED_PMU_WORKAROUND)){+/*+*Savepmuifthisguestiscapableofrunningnestedguests.+*ThisisoptionisforoldL1sthatdonotsettheir+*lppaca->pmcregs_in_useproperlywhenenteringtheirL2.+*/+save_pmu|=nesting_enabled(vcpu->kvm);+}kvmhv_save_guest_pmu(vcpu,save_pmu);#ifdef CONFIG_PPC_PSERIES
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:00:07
KVM PMU management code looks for particular frozen/disabled bits in
the PMU registers so it knows whether it must clear them when coming
out of a guest or not. Setting this up helps KVM make these optimisations
without getting confused. Longer term the better approach might be to
move guest/host PMU switching to the perf subsystem.
Cc: Madhavan Srinivasan <redacted>
Reviewed-by: Athira Jajeev <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kernel/cpu_setup_power.c | 4 ++--
arch/powerpc/kernel/dt_cpu_ftrs.c | 6 +++---
arch/powerpc/kvm/book3s_hv.c | 5 +++++
3 files changed, 10 insertions(+), 5 deletions(-)
@@ -2715,6 +2715,11 @@ static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu)#endif#endifvcpu->arch.mmcr[0]=MMCR0_FC;+if(cpu_has_feature(CPU_FTR_ARCH_31)){+vcpu->arch.mmcr[0]|=MMCR0_PMCCEXT;+vcpu->arch.mmcra=MMCRA_BHRB_DISABLE;+}+vcpu->arch.ctrl=CTRL_RUNLATCH;/* default to host PVR, since we can't spoof it */kvmppc_set_pvr_hv(vcpu,mfspr(SPRN_PVR));
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:00:44
It can be useful in simulators (with very constrained environments)
to allow some PMCs to run from boot so they can be sampled directly
by a test harness, rather than having to run perf.
A previous change freezes counters at boot by default, so provide
a boot time option to un-freeze (plus a bit more flexibility).
Cc: Madhavan Srinivasan <redacted>
Reviewed-by: Athira Jajeev <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
.../admin-guide/kernel-parameters.txt | 8 +++++
arch/powerpc/perf/core-book3s.c | 35 +++++++++++++++++++
2 files changed, 43 insertions(+)
@@ -4144,6 +4144,14 @@ Override pmtimer IOPort with a hex value. e.g. pmtmr=0x508+ pmu_override= [PPC] Override the PMU.+ This option takes over the PMU facility, so it is no+ longer usable by perf. Setting this option starts the+ PMU counters by setting MMCR0 to 0 (the FC bit is+ cleared). If a number is given, then MMCR1 is set to+ that number, otherwise (e.g., 'pmu_override=on'), MMCR1+ remains 0.+ pm_debug_messages [SUSPEND,KNL] Enable suspend/resume debug messages during boot up.
@@ -2419,8 +2419,24 @@ int register_power_pmu(struct power_pmu *pmu)}#ifdef CONFIG_PPC64+staticboolpmu_override=false;+staticunsignedlongpmu_override_val;+staticvoiddo_pmu_override(void*data)+{+ppc_set_pmu_inuse(1);+if(pmu_override_val)+mtspr(SPRN_MMCR1,pmu_override_val);+mtspr(SPRN_MMCR0,mfspr(SPRN_MMCR0)&~MMCR0_FC);+}+staticint__initinit_ppc64_pmu(void){+if(cpu_has_feature(CPU_FTR_HVMODE)&&pmu_override){+pr_warn("disabling perf due to pmu_override= command line option.\n");+on_each_cpu(do_pmu_override,NULL,1);+return0;+}+/* run through all the pmu drivers one at a time */if(!init_power5_pmu())return0;
@@ -2442,4 +2458,23 @@ static int __init init_ppc64_pmu(void)returninit_generic_compat_pmu();}early_initcall(init_ppc64_pmu);++staticint__initpmu_setup(char*str)+{+unsignedlongval;++if(!early_cpu_has_feature(CPU_FTR_HVMODE))+return0;++pmu_override=true;++if(kstrtoul(str,0,&val))+val=0;++pmu_override_val=val;++return1;+}+__setup("pmu_override=",pmu_setup);+#endif
@@ -3775,6 +3775,196 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)trace_kvmppc_run_core(vc,1);}+/*+*Privileged(non-hypervisor)hostregisterstosave.+*/+structp9_host_os_sprs{+unsignedlongdscr;+unsignedlongtidr;+unsignedlongiamr;+unsignedlongamr;+unsignedlongfscr;++unsignedintpmc1;+unsignedintpmc2;+unsignedintpmc3;+unsignedintpmc4;+unsignedintpmc5;+unsignedintpmc6;+unsignedlongmmcr0;+unsignedlongmmcr1;+unsignedlongmmcr2;+unsignedlongmmcr3;+unsignedlongmmcra;+unsignedlongsiar;+unsignedlongsier1;+unsignedlongsier2;+unsignedlongsier3;+unsignedlongsdar;+};++staticvoidfreeze_pmu(unsignedlongmmcr0,unsignedlongmmcra)+{+if(!(mmcr0&MMCR0_FC))+gotodo_freeze;+if(mmcra&MMCRA_SAMPLE_ENABLE)+gotodo_freeze;+if(cpu_has_feature(CPU_FTR_ARCH_31)){+if(!(mmcr0&MMCR0_PMCCEXT))+gotodo_freeze;+if(!(mmcra&MMCRA_BHRB_DISABLE))+gotodo_freeze;+}+return;++do_freeze:+mmcr0=MMCR0_FC;+mmcra=0;+if(cpu_has_feature(CPU_FTR_ARCH_31)){+mmcr0|=MMCR0_PMCCEXT;+mmcra=MMCRA_BHRB_DISABLE;+}++mtspr(SPRN_MMCR0,mmcr0);+mtspr(SPRN_MMCRA,mmcra);+isync();+}++staticvoidsave_p9_host_pmu(structp9_host_os_sprs*host_os_sprs)+{+if(ppc_get_pmu_inuse()){+/*+*ItmightbebettertoputPMUhandling(atleastforthe+*host)intheperfsubsystembecauseitknowsmoreaboutwhat+*isbeingused.+*/++/* POWER9, POWER10 do not implement HPMC or SPMC */++host_os_sprs->mmcr0=mfspr(SPRN_MMCR0);+host_os_sprs->mmcra=mfspr(SPRN_MMCRA);++freeze_pmu(host_os_sprs->mmcr0,host_os_sprs->mmcra);++host_os_sprs->pmc1=mfspr(SPRN_PMC1);+host_os_sprs->pmc2=mfspr(SPRN_PMC2);+host_os_sprs->pmc3=mfspr(SPRN_PMC3);+host_os_sprs->pmc4=mfspr(SPRN_PMC4);+host_os_sprs->pmc5=mfspr(SPRN_PMC5);+host_os_sprs->pmc6=mfspr(SPRN_PMC6);+host_os_sprs->mmcr1=mfspr(SPRN_MMCR1);+host_os_sprs->mmcr2=mfspr(SPRN_MMCR2);+host_os_sprs->sdar=mfspr(SPRN_SDAR);+host_os_sprs->siar=mfspr(SPRN_SIAR);+host_os_sprs->sier1=mfspr(SPRN_SIER);++if(cpu_has_feature(CPU_FTR_ARCH_31)){+host_os_sprs->mmcr3=mfspr(SPRN_MMCR3);+host_os_sprs->sier2=mfspr(SPRN_SIER2);+host_os_sprs->sier3=mfspr(SPRN_SIER3);+}+}+}++staticvoidload_p9_guest_pmu(structkvm_vcpu*vcpu)+{+mtspr(SPRN_PMC1,vcpu->arch.pmc[0]);+mtspr(SPRN_PMC2,vcpu->arch.pmc[1]);+mtspr(SPRN_PMC3,vcpu->arch.pmc[2]);+mtspr(SPRN_PMC4,vcpu->arch.pmc[3]);+mtspr(SPRN_PMC5,vcpu->arch.pmc[4]);+mtspr(SPRN_PMC6,vcpu->arch.pmc[5]);+mtspr(SPRN_MMCR1,vcpu->arch.mmcr[1]);+mtspr(SPRN_MMCR2,vcpu->arch.mmcr[2]);+mtspr(SPRN_SDAR,vcpu->arch.sdar);+mtspr(SPRN_SIAR,vcpu->arch.siar);+mtspr(SPRN_SIER,vcpu->arch.sier[0]);++if(cpu_has_feature(CPU_FTR_ARCH_31)){+mtspr(SPRN_MMCR3,vcpu->arch.mmcr[3]);+mtspr(SPRN_SIER2,vcpu->arch.sier[1]);+mtspr(SPRN_SIER3,vcpu->arch.sier[2]);+}++/* Set MMCRA then MMCR0 last */+mtspr(SPRN_MMCRA,vcpu->arch.mmcra);+mtspr(SPRN_MMCR0,vcpu->arch.mmcr[0]);+/* No isync necessary because we're starting counters */+}++staticvoidsave_p9_guest_pmu(structkvm_vcpu*vcpu)+{+structlppaca*lp;+intsave_pmu=1;++lp=vcpu->arch.vpa.pinned_addr;+if(lp)+save_pmu=lp->pmcregs_in_use;+if(IS_ENABLED(CONFIG_KVM_BOOK3S_HV_NESTED_PMU_WORKAROUND)){+/*+*Savepmuifthisguestiscapableofrunningnestedguests.+*ThisisoptionisforoldL1sthatdonotsettheir+*lppaca->pmcregs_in_useproperlywhenenteringtheirL2.+*/+save_pmu|=nesting_enabled(vcpu->kvm);+}++if(save_pmu){+vcpu->arch.mmcr[0]=mfspr(SPRN_MMCR0);+vcpu->arch.mmcra=mfspr(SPRN_MMCRA);++freeze_pmu(vcpu->arch.mmcr[0],vcpu->arch.mmcra);++vcpu->arch.pmc[0]=mfspr(SPRN_PMC1);+vcpu->arch.pmc[1]=mfspr(SPRN_PMC2);+vcpu->arch.pmc[2]=mfspr(SPRN_PMC3);+vcpu->arch.pmc[3]=mfspr(SPRN_PMC4);+vcpu->arch.pmc[4]=mfspr(SPRN_PMC5);+vcpu->arch.pmc[5]=mfspr(SPRN_PMC6);+vcpu->arch.mmcr[1]=mfspr(SPRN_MMCR1);+vcpu->arch.mmcr[2]=mfspr(SPRN_MMCR2);+vcpu->arch.sdar=mfspr(SPRN_SDAR);+vcpu->arch.siar=mfspr(SPRN_SIAR);+vcpu->arch.sier[0]=mfspr(SPRN_SIER);++if(cpu_has_feature(CPU_FTR_ARCH_31)){+vcpu->arch.mmcr[3]=mfspr(SPRN_MMCR3);+vcpu->arch.sier[1]=mfspr(SPRN_SIER2);+vcpu->arch.sier[2]=mfspr(SPRN_SIER3);+}+}else{+freeze_pmu(mfspr(SPRN_MMCR0),mfspr(SPRN_MMCRA));+}+}++staticvoidload_p9_host_pmu(structp9_host_os_sprs*host_os_sprs)+{+if(ppc_get_pmu_inuse()){+mtspr(SPRN_PMC1,host_os_sprs->pmc1);+mtspr(SPRN_PMC2,host_os_sprs->pmc2);+mtspr(SPRN_PMC3,host_os_sprs->pmc3);+mtspr(SPRN_PMC4,host_os_sprs->pmc4);+mtspr(SPRN_PMC5,host_os_sprs->pmc5);+mtspr(SPRN_PMC6,host_os_sprs->pmc6);+mtspr(SPRN_MMCR1,host_os_sprs->mmcr1);+mtspr(SPRN_MMCR2,host_os_sprs->mmcr2);+mtspr(SPRN_SDAR,host_os_sprs->sdar);+mtspr(SPRN_SIAR,host_os_sprs->siar);+mtspr(SPRN_SIER,host_os_sprs->sier1);++if(cpu_has_feature(CPU_FTR_ARCH_31)){+mtspr(SPRN_MMCR3,host_os_sprs->mmcr3);+mtspr(SPRN_SIER2,host_os_sprs->sier2);+mtspr(SPRN_SIER3,host_os_sprs->sier3);+}++/* Set MMCRA then MMCR0 last */+mtspr(SPRN_MMCRA,host_os_sprs->mmcra);+mtspr(SPRN_MMCR0,host_os_sprs->mmcr0);+isync();+}+}+staticvoidload_spr_state(structkvm_vcpu*vcpu){mtspr(SPRN_DSCR,vcpu->arch.dscr);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:02:06
Rather than guest/host save/retsore functions, implement context switch
functions that take care of details like the VPA update for nested.
The reason to split these kind of helpers into explicit save/load
functions is mainly to schedule SPR access nicely, but PMU is a special
case where the load requires mtSPR (to stop counters) and other
difficulties, so there's less possibility to schedule those nicely. The
SPR accesses also have side-effects if the PMU is running, and in later
changes we keep the host PMU running as long as possible so this code
can be better profiled, which also complicates scheduling.
Reviewed-by: Athira Jajeev <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 61 +++++++++++++++++-------------------
1 file changed, 28 insertions(+), 33 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:02:35
The pmcregs_in_use field in the guest VPA can not be trusted to reflect
what the guest is doing with PMU SPRs, so the PMU must always be managed
(stopped) when exiting the guest, and SPR values set when entering the
guest to ensure it can't cause a covert channel or otherwise cause other
guests or the host to misbehave.
So prevent guest access to the PMU with HFSCR[PM] if pmcregs_in_use is
clear, and avoid the PMU SPR access on every partition switch. Guests
that set pmcregs_in_use incorrectly or when first setting it and using
the PMU will take a hypervisor facility unavailable interrupt that will
bring in the PMU SPRs.
Reviewed-by: Athira Jajeev <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 131 ++++++++++++++++++++++++++---------
1 file changed, 98 insertions(+), 33 deletions(-)
@@ -1421,6 +1421,23 @@ static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)returnRESUME_GUEST;}+/*+*Ifthelppacahadpmcregs_in_useclearwhenweexitedtheguest,then+*HFSCR_PMisclearedfornextentry.Iftheguestthentriestoaccess+*thePMUSPRs,wegetthisfacilityunavailableinterrupt.PuttingHFSCR_PM+*backintheguestHFSCRwillcausethenextentrytoloadthePMUSPRsand+*allowtheguestaccesstocontinue.+*/+staticintkvmppc_pmu_unavailable(structkvm_vcpu*vcpu)+{+if(!(vcpu->arch.hfscr_permitted&HFSCR_PM))+returnEMULATE_FAIL;++vcpu->arch.hfscr|=HFSCR_PM;++returnRESUME_GUEST;+}+staticintkvmppc_handle_exit_hv(structkvm_vcpu*vcpu,structtask_struct*tsk){
@@ -1702,16 +1719,22 @@ static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,*toemulate.*Otherwise,wejustgenerateaprograminterrupttotheguest.*/-caseBOOK3S_INTERRUPT_H_FAC_UNAVAIL:+caseBOOK3S_INTERRUPT_H_FAC_UNAVAIL:{+u64cause=vcpu->arch.hfscr>>56;+r=EMULATE_FAIL;-if(((vcpu->arch.hfscr>>56)==FSCR_MSGP_LG)&&-cpu_has_feature(CPU_FTR_ARCH_300))-r=kvmppc_emulate_doorbell_instr(vcpu);+if(cpu_has_feature(CPU_FTR_ARCH_300)){+if(cause==FSCR_MSGP_LG)+r=kvmppc_emulate_doorbell_instr(vcpu);+if(cause==FSCR_PM_LG)+r=kvmppc_pmu_unavailable(vcpu);+}if(r==EMULATE_FAIL){kvmppc_core_queue_program(vcpu,SRR1_PROGILL);r=RESUME_GUEST;}break;+}caseBOOK3S_INTERRUPT_HV_RM_HARD:r=RESUME_PASSTHROUGH;
@@ -2750,6 +2773,11 @@ static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu)vcpu->arch.hfscr_permitted=vcpu->arch.hfscr;+/*+*PMisdemand-faultedsostartwithitclear.+*/+vcpu->arch.hfscr&=~HFSCR_PM;+kvmppc_mmu_book3s_hv_init(vcpu);vcpu->arch.state=KVMPPC_VCPU_NOTREADY;
@@ -3833,6 +3861,14 @@ static void freeze_pmu(unsigned long mmcr0, unsigned long mmcra)staticvoidswitch_pmu_to_guest(structkvm_vcpu*vcpu,structp9_host_os_sprs*host_os_sprs){+structlppaca*lp;+intload_pmu=1;++lp=vcpu->arch.vpa.pinned_addr;+if(lp)+load_pmu=lp->pmcregs_in_use;++/* Save host */if(ppc_get_pmu_inuse()){/**ItmightbebettertoputPMUhandling(atleastforthe
@@ -3867,41 +3903,47 @@ static void switch_pmu_to_guest(struct kvm_vcpu *vcpu,}#ifdef CONFIG_PPC_PSERIES+/* After saving PMU, before loading guest PMU, flip pmcregs_in_use */if(kvmhv_on_pseries()){barrier();-if(vcpu->arch.vpa.pinned_addr){-structlppaca*lp=vcpu->arch.vpa.pinned_addr;-get_lppaca()->pmcregs_in_use=lp->pmcregs_in_use;-}else{-get_lppaca()->pmcregs_in_use=1;-}+get_lppaca()->pmcregs_in_use=load_pmu;barrier();}#endif-/* load guest */-mtspr(SPRN_PMC1,vcpu->arch.pmc[0]);-mtspr(SPRN_PMC2,vcpu->arch.pmc[1]);-mtspr(SPRN_PMC3,vcpu->arch.pmc[2]);-mtspr(SPRN_PMC4,vcpu->arch.pmc[3]);-mtspr(SPRN_PMC5,vcpu->arch.pmc[4]);-mtspr(SPRN_PMC6,vcpu->arch.pmc[5]);-mtspr(SPRN_MMCR1,vcpu->arch.mmcr[1]);-mtspr(SPRN_MMCR2,vcpu->arch.mmcr[2]);-mtspr(SPRN_SDAR,vcpu->arch.sdar);-mtspr(SPRN_SIAR,vcpu->arch.siar);-mtspr(SPRN_SIER,vcpu->arch.sier[0]);+/*+*Loadguest.IftheVPAsaidthePMCsarenotinusebuttheguest+*triedtoaccessthemanyway,HFSCR[PM]willbesetbytheHFAC+*faultsowecanmakeforwardprogress.+*/+if(load_pmu||(vcpu->arch.hfscr&HFSCR_PM)){+mtspr(SPRN_PMC1,vcpu->arch.pmc[0]);+mtspr(SPRN_PMC2,vcpu->arch.pmc[1]);+mtspr(SPRN_PMC3,vcpu->arch.pmc[2]);+mtspr(SPRN_PMC4,vcpu->arch.pmc[3]);+mtspr(SPRN_PMC5,vcpu->arch.pmc[4]);+mtspr(SPRN_PMC6,vcpu->arch.pmc[5]);+mtspr(SPRN_MMCR1,vcpu->arch.mmcr[1]);+mtspr(SPRN_MMCR2,vcpu->arch.mmcr[2]);+mtspr(SPRN_SDAR,vcpu->arch.sdar);+mtspr(SPRN_SIAR,vcpu->arch.siar);+mtspr(SPRN_SIER,vcpu->arch.sier[0]);++if(cpu_has_feature(CPU_FTR_ARCH_31)){+mtspr(SPRN_MMCR3,vcpu->arch.mmcr[3]);+mtspr(SPRN_SIER2,vcpu->arch.sier[1]);+mtspr(SPRN_SIER3,vcpu->arch.sier[2]);+}-if(cpu_has_feature(CPU_FTR_ARCH_31)){-mtspr(SPRN_MMCR3,vcpu->arch.mmcr[3]);-mtspr(SPRN_SIER2,vcpu->arch.sier[1]);-mtspr(SPRN_SIER3,vcpu->arch.sier[2]);-}+/* Set MMCRA then MMCR0 last */+mtspr(SPRN_MMCRA,vcpu->arch.mmcra);+mtspr(SPRN_MMCR0,vcpu->arch.mmcr[0]);+/* No isync necessary because we're starting counters */-/* Set MMCRA then MMCR0 last */-mtspr(SPRN_MMCRA,vcpu->arch.mmcra);-mtspr(SPRN_MMCR0,vcpu->arch.mmcr[0]);-/* No isync necessary because we're starting counters */+if(!vcpu->arch.nested&&+(vcpu->arch.hfscr_permitted&HFSCR_PM))+vcpu->arch.hfscr|=HFSCR_PM;+}}staticvoidswitch_pmu_to_host(structkvm_vcpu*vcpu,
@@ -3945,9 +3987,32 @@ static void switch_pmu_to_host(struct kvm_vcpu *vcpu,vcpu->arch.sier[1]=mfspr(SPRN_SIER2);vcpu->arch.sier[2]=mfspr(SPRN_SIER3);}-}else{++}elseif(vcpu->arch.hfscr&HFSCR_PM){+/*+*TheguestaccessedPMCSPRswithoutspecifyingtheyshould+*bepreserved,oritclearedpmcregs_in_useafterthelast+*access.Justensuretheyarefrozen.+*/freeze_pmu(mfspr(SPRN_MMCR0),mfspr(SPRN_MMCRA));-}++/*+*Demand-faultPMUregisteraccessintheguest.+*+*Thisisusedtograbtheguest'sVPApmcregs_in_usevalue+*andreflectitintothehost'sVPAinthecaseofanested+*hypervisor.+*+*Italsoavoidshavingtozero-outSPRsaftereachguest+*exittoavoidside-channelswhen.+*+*Thisisclearedherewhenweexittheguest,solaterHFSCR+*interrupthandlingcanadditbacktoruntheguestwith+*PMenablednexttime.+*/+if(!vcpu->arch.nested)+vcpu->arch.hfscr&=~HFSCR_PM;+}/* otherwise the PMU should still be frozen */#ifdef CONFIG_PPC_PSERIESif(kvmhv_on_pseries()){
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:03:59
Processors that support KVM HV do not require read-modify-write of
the CTRL SPR to set/clear their thread's runlatch. Just write 1 or 0
to it.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 2 +-
arch/powerpc/kvm/book3s_hv_rmhandlers.S | 15 ++++++---------
2 files changed, 7 insertions(+), 10 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:04:36
Move the SPR update into its relevant helper function. This will
help with SPR scheduling improvements in later changes.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:05:13
This reduces the number of mtmsrd required to enable facility bits when
saving/restoring registers, by having the KVM code set all bits up front
rather than using individual facility functions that set their particular
MSR bits.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/switch_to.h | 2 +
arch/powerpc/kernel/process.c | 28 +++++++++++++
arch/powerpc/kvm/book3s_hv.c | 59 ++++++++++++++++++---------
arch/powerpc/kvm/book3s_hv_p9_entry.c | 1 +
4 files changed, 71 insertions(+), 19 deletions(-)
@@ -4164,8 +4165,23 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,if(next_timer<time_limit)time_limit=next_timer;+vcpu->arch.ceded=0;+save_p9_host_os_sprs(&host_os_sprs);+/* MSR bits may have been cleared by context switch */+msr=0;+if(IS_ENABLED(CONFIG_PPC_FPU))+msr|=MSR_FP;+if(cpu_has_feature(CPU_FTR_ALTIVEC))+msr|=MSR_VEC;+if(cpu_has_feature(CPU_FTR_VSX))+msr|=MSR_VSX;+if(cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))+msr|=MSR_TM;+msr=msr_check_and_set(msr);+kvmppc_subcore_enter_guest();vc->entry_exit_map=1;
@@ -4174,12 +4190,13 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vcpu_vpa_increment_dispatch(vcpu);if(cpu_has_feature(CPU_FTR_TM)||-cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)){kvmppc_restore_tm_hv(vcpu,vcpu->arch.shregs.msr,true);+msr=mfmsr();/* TM restore can update msr */+}switch_pmu_to_guest(vcpu,&host_os_sprs);-msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);load_fp_state(&vcpu->arch.fp);#ifdef CONFIG_ALTIVECload_vr_state(&vcpu->arch.vr);
@@ -4851,19 +4867,24 @@ static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)unsignedlonguser_tar=0;unsignedintuser_vrsave;structkvm*kvm;+unsignedlongmsr;if(!vcpu->arch.sane){run->exit_reason=KVM_EXIT_INTERNAL_ERROR;return-EINVAL;}+/* No need to go into the guest when all we'll do is come back out */+if(signal_pending(current)){+run->exit_reason=KVM_EXIT_INTR;+return-EINTR;+}++#ifdef CONFIG_PPC_TRANSACTIONAL_MEM/**Don'tallowentrywithasuspendedtransaction,because*theguestentry/exitcodewillloseit.-*IftheguesthasTMenabled,saveawaytheirTM-relatedSPRs-*(theywillgetrestoredbytheTMunavailableinterrupt).*/-#ifdef CONFIG_PPC_TRANSACTIONAL_MEMif(cpu_has_feature(CPU_FTR_TM)&¤t->thread.regs&&(current->thread.regs->msr&MSR_TM)){if(MSR_TM_ACTIVE(current->thread.regs->msr)){
@@ -4871,12 +4892,6 @@ static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)run->fail_entry.hardware_entry_failure_reason=0;return-EINVAL;}-/* Enable TM so we can read the TM SPRs */-mtmsr(mfmsr()|MSR_TM);-current->thread.tm_tfhar=mfspr(SPRN_TFHAR);-current->thread.tm_tfiar=mfspr(SPRN_TFIAR);-current->thread.tm_texasr=mfspr(SPRN_TEXASR);-current->thread.regs->msr&=~MSR_TM;}#endif
@@ -4891,18 +4906,24 @@ static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)kvmppc_core_prepare_to_enter(vcpu);-/* No need to go into the guest when all we'll do is come back out */-if(signal_pending(current)){-run->exit_reason=KVM_EXIT_INTR;-return-EINTR;-}-kvm=vcpu->kvm;atomic_inc(&kvm->arch.vcpus_running);/* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */smp_mb();-flush_all_to_thread(current);+msr=0;+if(IS_ENABLED(CONFIG_PPC_FPU))+msr|=MSR_FP;+if(cpu_has_feature(CPU_FTR_ALTIVEC))+msr|=MSR_VEC;+if(cpu_has_feature(CPU_FTR_VSX))+msr|=MSR_VSX;+if(cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))+msr|=MSR_TM;+msr=msr_check_and_set(msr);++kvmppc_save_user_regs();/* Save userspace EBB and other register values */if(cpu_has_feature(CPU_FTR_ARCH_207S)){
@@ -224,6 +224,7 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvc->tb_offset_applied=vc->tb_offset;}+/* Could avoid mfmsr by passing around, but probably no big deal */msr=mfmsr();host_hfscr=mfspr(SPRN_HFSCR);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:05:51
Moving the mtmsrd after the host SPRs are saved and before the guest
SPRs start to be loaded can prevent an SPR scoreboard stall (because
the mtmsrd is L=1 type which does not cause context synchronisation.
This is also now more convenient to combined with the mtmsrd L=0
instruction to enable facilities just below, but that is not done yet.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
@@ -4169,6 +4169,18 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,save_p9_host_os_sprs(&host_os_sprs);+/*+*ThiscouldbecombinedwithMSR[RI]clearing,butthatexpands+*theunrecoverablewindow.Itwouldbebettertocoverunrecoverable+*withKVMbadinterrupthandlingratherthanuseMSR[RI]atall.+*+*MuchmoredifficultandlessworthwhiletocombinewithIR/DR+*disable.+*/+hard_irq_disable();+if(lazy_irq_pending())+return0;+/* MSR bits may have been cleared by context switch */msr=0;if(IS_ENABLED(CONFIG_PPC_FPU))
@@ -4680,6 +4692,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,structkvmppc_vcore*vc;structkvm*kvm=vcpu->kvm;structkvm_nested_guest*nested=vcpu->arch.nested;+unsignedlongflags;trace_kvmppc_run_vcpu_enter(vcpu);
@@ -4723,11 +4736,11 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,if(kvm_is_radix(kvm))kvmppc_prepare_radix_vcpu(vcpu,pcpu);-local_irq_disable();-hard_irq_disable();+/* flags save not required, but irq_pmu has no disable/enable API */+powerpc_local_irq_pmu_save(flags);if(signal_pending(current))gotosigpend;-if(lazy_irq_pending()||need_resched()||!kvm->arch.mmu_ready)+if(need_resched()||!kvm->arch.mmu_ready)gotoout;if(!nested){
@@ -4795,7 +4808,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,}vtime_account_guest_exit();-local_irq_enable();+powerpc_local_irq_pmu_restore(flags);cpumask_clear_cpu(pcpu,&kvm->arch.cpu_in_guest);
@@ -4853,7 +4866,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,run->exit_reason=KVM_EXIT_INTR;vcpu->arch.ret=-EINTR;out:-local_irq_enable();+powerpc_local_irq_pmu_restore(flags);preempt_enable();gotodone;}
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:06:29
Small cleanup makes it a bit easier to match up entry and exit
operations.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
@@ -3070,6 +3070,13 @@ static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)kvmppc_ipi_thread(cpu);}+/* Old path does this in asm */+staticvoidkvmppc_stop_thread(structkvm_vcpu*vcpu)+{+vcpu->cpu=-1;+vcpu->arch.thread_cpu=-1;+}+staticvoidkvmppc_wait_for_nap(intn_threads){intcpu=smp_processor_id();
@@ -406,6 +406,12 @@ static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)returnvcpu->arch.fault_dar;}+/* Expiry time of vcpu DEC relative to host TB */+staticinlineu64kvmppc_dec_expires_host_tb(structkvm_vcpu*vcpu)+{+returnvcpu->arch.dec_expires-vcpu->arch.vcore->tb_offset;+}+staticinlineboolis_kvmppc_resume_guest(intr){return(r==RESUME_GUEST||r==RESUME_GUEST_NV);
@@ -3380,7 +3378,7 @@ static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)*/spin_unlock(&vc->lock);/* cancel pending dec exception if dec is positive */-if(now<vcpu->arch.dec_expires&&+if(now<kvmppc_dec_expires_host_tb(vcpu)&&kvmppc_core_pending_dec(vcpu))kvmppc_core_dequeue_dec(vcpu);
@@ -4274,6 +4275,12 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vcpu->arch.psscr=mfspr(SPRN_PSSCR_PR);mtspr(SPRN_PSSCR_PR,host_psscr);+dec=mfspr(SPRN_DEC);+if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */+dec=(s32)dec;+tb=mftb();+vcpu->arch.dec_expires=dec+(tb+vc->tb_offset);+/* H_CEDE has to be handled now, not later */if(trap==BOOK3S_INTERRUPT_SYSCALL&&!vcpu->arch.nested&&kvmppc_get_gpr(vcpu,3)==H_CEDE){
@@ -4312,12 +4320,6 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vcpu->arch.slb_max=0;}-dec=mfspr(SPRN_DEC);-if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */-dec=(s32)dec;-tb=mftb();-vcpu->arch.dec_expires=dec+tb;-store_spr_state(vcpu);restore_p9_host_os_sprs(vcpu,&host_os_sprs);
@@ -4827,7 +4829,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,*byL2andtheL1decrementerisprovidedinhdec_expires*/if(kvmppc_core_pending_dec(vcpu)&&-((get_tb()<vcpu->arch.dec_expires)||+((get_tb()<kvmppc_dec_expires_host_tb(vcpu))||(trap==BOOK3S_INTERRUPT_SYSCALL&&kvmppc_get_gpr(vcpu,3)==H_ENTER_NESTED)))kvmppc_core_dequeue_dec(vcpu);
@@ -358,6 +358,7 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)/* convert TB values/offsets to host (L0) values */hdec_exp=l2_hv.hdec_expiry-vc->tb_offset;vc->tb_offset+=l2_hv.tb_offset;+vcpu->arch.dec_expires+=l2_hv.tb_offset;/* set L1 state to L2 state */vcpu->arch.nested=l2;
@@ -399,6 +400,8 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)if(l2_regs.msr&MSR_TS_MASK)vcpu->arch.shregs.msr|=MSR_TS_S;vc->tb_offset=saved_l1_hv.tb_offset;+/* XXX: is this always the same delta as saved_l1_hv.tb_offset? */+vcpu->arch.dec_expires-=l2_hv.tb_offset;restore_hv_regs(vcpu,&saved_l1_hv);vcpu->arch.purr+=delta_purr;vcpu->arch.spurr+=delta_spurr;
@@ -188,7 +188,7 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcstructkvm*kvm=vcpu->kvm;structkvm_nested_guest*nested=vcpu->arch.nested;structkvmppc_vcore*vc=vcpu->arch.vcore;-s64hdec;+s64hdec,dec;u64tb,purr,spurr;u64*exsave;boolri_set;
@@ -317,6 +317,8 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpc*/mtspr(SPRN_HDEC,hdec);+mtspr(SPRN_DEC,vcpu->arch.dec_expires-tb);+#ifdef CONFIG_PPC_TRANSACTIONAL_MEMtm_return_to_guest:#endif
@@ -461,6 +463,12 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvcpu->arch.shregs.sprg2=mfspr(SPRN_SPRG2);vcpu->arch.shregs.sprg3=mfspr(SPRN_SPRG3);+dec=mfspr(SPRN_DEC);+if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */+dec=(s32)dec;+tb=mftb();+vcpu->arch.dec_expires=dec+tb;+/* Preserve PSSCR[FAKE_SUSPEND] until we've called kvmppc_save_tm_hv */mtspr(SPRN_PSSCR,host_psscr|(local_paca->kvm_hstate.fake_suspend<<PSSCR_FAKE_SUSPEND_LG));
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:07:54
Move the TB updates between saving and loading guest and host SPRs,
to improve scheduling by keeping issue-NTC operations together as
much as possible.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv_p9_entry.c | 36 +++++++++++++--------------
1 file changed, 18 insertions(+), 18 deletions(-)
@@ -215,15 +215,6 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvcpu->arch.ceded=0;-if(vc->tb_offset){-u64new_tb=tb+vc->tb_offset;-mtspr(SPRN_TBU40,new_tb);-tb=mftb();-if((tb&0xffffff)<(new_tb&0xffffff))-mtspr(SPRN_TBU40,new_tb+0x1000000);-vc->tb_offset_applied=vc->tb_offset;-}-/* Could avoid mfmsr by passing around, but probably no big deal */msr=mfmsr();
@@ -238,6 +229,15 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpchost_dawrx1=mfspr(SPRN_DAWRX1);}+if(vc->tb_offset){+u64new_tb=tb+vc->tb_offset;+mtspr(SPRN_TBU40,new_tb);+tb=mftb();+if((tb&0xffffff)<(new_tb&0xffffff))+mtspr(SPRN_TBU40,new_tb+0x1000000);+vc->tb_offset_applied=vc->tb_offset;+}+if(vc->pcr)mtspr(SPRN_PCR,vc->pcr|PCR_MASK);mtspr(SPRN_DPDES,vc->dpdes);
@@ -469,6 +469,15 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpctb=mftb();vcpu->arch.dec_expires=dec+tb;+if(vc->tb_offset_applied){+u64new_tb=tb-vc->tb_offset_applied;+mtspr(SPRN_TBU40,new_tb);+tb=mftb();+if((tb&0xffffff)<(new_tb&0xffffff))+mtspr(SPRN_TBU40,new_tb+0x1000000);+vc->tb_offset_applied=0;+}+/* Preserve PSSCR[FAKE_SUSPEND] until we've called kvmppc_save_tm_hv */mtspr(SPRN_PSSCR,host_psscr|(local_paca->kvm_hstate.fake_suspend<<PSSCR_FAKE_SUSPEND_LG));
@@ -503,15 +512,6 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcif(vc->pcr)mtspr(SPRN_PCR,PCR_MASK);-if(vc->tb_offset_applied){-u64new_tb=mftb()-vc->tb_offset_applied;-mtspr(SPRN_TBU40,new_tb);-tb=mftb();-if((tb&0xffffff)<(new_tb&0xffffff))-mtspr(SPRN_TBU40,new_tb+0x1000000);-vc->tb_offset_applied=0;-}-/* HDEC must be at least as large as DEC, so decrementer_max fits */mtspr(SPRN_HDEC,decrementer_max);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:08:34
Reduce the number of mfTB executed by passing the current timebase
around entry and exit code rather than read it multiple times.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/kvm_book3s_64.h | 2 +-
arch/powerpc/kvm/book3s_hv.c | 88 +++++++++++++-----------
arch/powerpc/kvm/book3s_hv_p9_entry.c | 33 +++++----
3 files changed, 65 insertions(+), 58 deletions(-)
@@ -4278,8 +4277,8 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,dec=mfspr(SPRN_DEC);if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */dec=(s32)dec;-tb=mftb();-vcpu->arch.dec_expires=dec+(tb+vc->tb_offset);+*tb=mftb();+vcpu->arch.dec_expires=dec+(*tb+vc->tb_offset);/* H_CEDE has to be handled now, not later */if(trap==BOOK3S_INTERRUPT_SYSCALL&&!vcpu->arch.nested&&
@@ -4596,7 +4595,7 @@ static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu)if((vc->vcore_state==VCORE_PIGGYBACK||vc->vcore_state==VCORE_RUNNING)&&!VCORE_IS_EXITING(vc)){-kvmppc_create_dtl_entry(vcpu,vc);+kvmppc_create_dtl_entry(vcpu,vc,mftb());kvmppc_start_thread(vcpu,vc);trace_kvm_guest_enter(vcpu);}elseif(vc->vcore_state==VCORE_SLEEPING){
@@ -4631,7 +4630,7 @@ static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu)for_each_runnable_thread(i,v,vc){kvmppc_core_prepare_to_enter(v);if(signal_pending(v->arch.run_task)){-kvmppc_remove_runnable(vc,v);+kvmppc_remove_runnable(vc,v,mftb());v->stat.signal_exits++;v->run->exit_reason=KVM_EXIT_INTR;v->arch.ret=-EINTR;
@@ -4672,7 +4671,7 @@ static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu)kvmppc_vcore_end_preempt(vc);if(vcpu->arch.state==KVMPPC_VCPU_RUNNABLE){-kvmppc_remove_runnable(vc,vcpu);+kvmppc_remove_runnable(vc,vcpu,mftb());vcpu->stat.signal_exits++;run->exit_reason=KVM_EXIT_INTR;vcpu->arch.ret=-EINTR;
@@ -4700,6 +4699,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,structkvm*kvm=vcpu->kvm;structkvm_nested_guest*nested=vcpu->arch.nested;unsignedlongflags;+u64tb;trace_kvmppc_run_vcpu_enter(vcpu);
@@ -4710,7 +4710,6 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,vc=vcpu->arch.vcore;vcpu->arch.ceded=0;vcpu->arch.run_task=current;-vcpu->arch.stolen_logged=vcore_stolen_time(vc,mftb());vcpu->arch.state=KVMPPC_VCPU_RUNNABLE;vcpu->arch.busy_preempt=TB_NIL;vcpu->arch.last_inst=KVM_INST_FETCH_FAILED;
@@ -4735,7 +4734,6 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,kvmppc_update_vpas(vcpu);init_vcore_to_run(vc);-vc->preempt_tb=TB_NIL;preempt_disable();pcpu=smp_processor_id();
@@ -4745,6 +4743,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,/* flags save not required, but irq_pmu has no disable/enable API */powerpc_local_irq_pmu_save(flags);+if(signal_pending(current))gotosigpend;if(need_resched()||!kvm->arch.mmu_ready)
@@ -4767,12 +4766,17 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,gotoout;}+tb=mftb();++vcpu->arch.stolen_logged=vcore_stolen_time(vc,tb);+vc->preempt_tb=TB_NIL;+kvmppc_clear_host_core(pcpu);local_paca->kvm_hstate.napping=0;local_paca->kvm_hstate.kvm_split_mode=NULL;kvmppc_start_thread(vcpu,vc);-kvmppc_create_dtl_entry(vcpu,vc);+kvmppc_create_dtl_entry(vcpu,vc,tb);trace_kvm_guest_enter(vcpu);vc->vcore_state=VCORE_RUNNING;
@@ -4787,7 +4791,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,/* Tell lockdep that we're about to enable interrupts */trace_hardirqs_on();-trap=kvmhv_p9_guest_entry(vcpu,time_limit,lpcr);+trap=kvmhv_p9_guest_entry(vcpu,time_limit,lpcr,&tb);vcpu->arch.trap=trap;trace_hardirqs_off();
@@ -4829,7 +4833,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,*byL2andtheL1decrementerisprovidedinhdec_expires*/if(kvmppc_core_pending_dec(vcpu)&&-((get_tb()<kvmppc_dec_expires_host_tb(vcpu))||+((tb<kvmppc_dec_expires_host_tb(vcpu))||(trap==BOOK3S_INTERRUPT_SYSCALL&&kvmppc_get_gpr(vcpu,3)==H_ENTER_NESTED)))kvmppc_core_dequeue_dec(vcpu);
@@ -4865,7 +4869,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,trace_kvmppc_run_core(vc,1);done:-kvmppc_remove_runnable(vc,vcpu);+kvmppc_remove_runnable(vc,vcpu,tb);trace_kvmppc_run_vcpu_exit(vcpu);returnvcpu->arch.ret;
@@ -203,8 +203,7 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcunsignedlonghost_dawr1;unsignedlonghost_dawrx1;-tb=mftb();-hdec=time_limit-tb;+hdec=time_limit-*tb;if(hdec<0)returnBOOK3S_INTERRUPT_HV_DECREMENTER;
@@ -230,11 +229,13 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpc}if(vc->tb_offset){-u64new_tb=tb+vc->tb_offset;+u64new_tb=*tb+vc->tb_offset;mtspr(SPRN_TBU40,new_tb);-tb=mftb();-if((tb&0xffffff)<(new_tb&0xffffff))-mtspr(SPRN_TBU40,new_tb+0x1000000);+if((mftb()&0xffffff)<(new_tb&0xffffff)){+new_tb+=0x1000000;+mtspr(SPRN_TBU40,new_tb);+}+*tb=new_tb;vc->tb_offset_applied=vc->tb_offset;}
@@ -317,7 +318,7 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpc*/mtspr(SPRN_HDEC,hdec);-mtspr(SPRN_DEC,vcpu->arch.dec_expires-tb);+mtspr(SPRN_DEC,vcpu->arch.dec_expires-*tb);#ifdef CONFIG_PPC_TRANSACTIONAL_MEMtm_return_to_guest:
@@ -466,15 +467,17 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcdec=mfspr(SPRN_DEC);if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */dec=(s32)dec;-tb=mftb();-vcpu->arch.dec_expires=dec+tb;+*tb=mftb();+vcpu->arch.dec_expires=dec+*tb;if(vc->tb_offset_applied){-u64new_tb=tb-vc->tb_offset_applied;+u64new_tb=*tb-vc->tb_offset_applied;mtspr(SPRN_TBU40,new_tb);-tb=mftb();-if((tb&0xffffff)<(new_tb&0xffffff))-mtspr(SPRN_TBU40,new_tb+0x1000000);+if((mftb()&0xffffff)<(new_tb&0xffffff)){+new_tb+=0x1000000;+mtspr(SPRN_TBU40,new_tb);+}+*tb=new_tb;vc->tb_offset_applied=0;}
@@ -228,6 +228,9 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpchost_dawrx1=mfspr(SPRN_DAWRX1);}+local_paca->kvm_hstate.host_purr=mfspr(SPRN_PURR);+local_paca->kvm_hstate.host_spurr=mfspr(SPRN_SPURR);+if(vc->tb_offset){u64new_tb=*tb+vc->tb_offset;mtspr(SPRN_TBU40,new_tb);
@@ -244,8 +247,6 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcmtspr(SPRN_DPDES,vc->dpdes);mtspr(SPRN_VTB,vc->vtb);-local_paca->kvm_hstate.host_purr=mfspr(SPRN_PURR);-local_paca->kvm_hstate.host_spurr=mfspr(SPRN_SPURR);mtspr(SPRN_PURR,vcpu->arch.purr);mtspr(SPRN_SPURR,vcpu->arch.spurr);
@@ -448,10 +449,8 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpc/* Advance host PURR/SPURR by the amount used by guest */purr=mfspr(SPRN_PURR);spurr=mfspr(SPRN_SPURR);-mtspr(SPRN_PURR,local_paca->kvm_hstate.host_purr+-purr-vcpu->arch.purr);-mtspr(SPRN_SPURR,local_paca->kvm_hstate.host_spurr+-spurr-vcpu->arch.spurr);+local_paca->kvm_hstate.host_purr+=purr-vcpu->arch.purr;+local_paca->kvm_hstate.host_spurr+=spurr-vcpu->arch.spurr;vcpu->arch.purr=purr;vcpu->arch.spurr=spurr;
@@ -464,6 +463,9 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvcpu->arch.shregs.sprg2=mfspr(SPRN_SPRG2);vcpu->arch.shregs.sprg3=mfspr(SPRN_SPRG3);+vc->dpdes=mfspr(SPRN_DPDES);+vc->vtb=mfspr(SPRN_VTB);+dec=mfspr(SPRN_DEC);if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */dec=(s32)dec;
@@ -481,6 +483,9 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvc->tb_offset_applied=0;}+mtspr(SPRN_PURR,local_paca->kvm_hstate.host_purr);+mtspr(SPRN_SPURR,local_paca->kvm_hstate.host_spurr);+/* Preserve PSSCR[FAKE_SUSPEND] until we've called kvmppc_save_tm_hv */mtspr(SPRN_PSSCR,host_psscr|(local_paca->kvm_hstate.fake_suspend<<PSSCR_FAKE_SUSPEND_LG));
@@ -509,8 +514,6 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcif(cpu_has_feature(CPU_FTR_ARCH_31))asmvolatile(PPC_CP_ABORT);-vc->dpdes=mfspr(SPRN_DPDES);-vc->vtb=mfspr(SPRN_VTB);mtspr(SPRN_DPDES,0);if(vc->pcr)mtspr(SPRN_PCR,PCR_MASK);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:09:59
Keep better track of the current SPR value in places where
they are to be loaded with a new context, to reduce expensive
mtSPR operations.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 51 ++++++++++++++++++++++--------------
1 file changed, 31 insertions(+), 20 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:10:37
This juggles SPR switching on the entry and exit sides to be more
symmetric, which makes the next refactoring patch possible with no
functional change.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:11:15
This should be no functional difference but makes the caller easier
to read.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 65 +++++++++++++++++++++++-------------
1 file changed, 41 insertions(+), 24 deletions(-)
@@ -4108,6 +4108,44 @@ static void store_spr_state(struct kvm_vcpu *vcpu)vcpu->arch.ctrl=mfspr(SPRN_CTRLF);}+/* Returns true if current MSR and/or guest MSR may have changed */+staticboolload_vcpu_state(structkvm_vcpu*vcpu,+structp9_host_os_sprs*host_os_sprs)+{+boolret=false;++if(cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)){+kvmppc_restore_tm_hv(vcpu,vcpu->arch.shregs.msr,true);+ret=true;+}++load_spr_state(vcpu,host_os_sprs);++load_fp_state(&vcpu->arch.fp);+#ifdef CONFIG_ALTIVEC+load_vr_state(&vcpu->arch.vr);+#endif+mtspr(SPRN_VRSAVE,vcpu->arch.vrsave);++returnret;+}++staticvoidstore_vcpu_state(structkvm_vcpu*vcpu)+{+store_spr_state(vcpu);++store_fp_state(&vcpu->arch.fp);+#ifdef CONFIG_ALTIVEC+store_vr_state(&vcpu->arch.vr);+#endif+vcpu->arch.vrsave=mfspr(SPRN_VRSAVE);++if(cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))+kvmppc_save_tm_hv(vcpu,vcpu->arch.shregs.msr,true);+}+staticvoidsave_p9_host_os_sprs(structp9_host_os_sprs*host_os_sprs){host_os_sprs->dscr=mfspr(SPRN_DSCR);
@@ -4216,19 +4254,8 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vcpu_vpa_increment_dispatch(vcpu);-if(cpu_has_feature(CPU_FTR_TM)||-cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)){-kvmppc_restore_tm_hv(vcpu,vcpu->arch.shregs.msr,true);-msr=mfmsr();/* TM restore can update msr */-}--load_spr_state(vcpu,&host_os_sprs);--load_fp_state(&vcpu->arch.fp);-#ifdef CONFIG_ALTIVEC-load_vr_state(&vcpu->arch.vr);-#endif-mtspr(SPRN_VRSAVE,vcpu->arch.vrsave);+if(unlikely(load_vcpu_state(vcpu,&host_os_sprs)))+msr=mfmsr();/* MSR may have been updated */switch_pmu_to_guest(vcpu,&host_os_sprs);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:11:56
Move the P9 guest/host register switching functions to the built-in
P9 entry code, and export it for nested to use as well.
This allows more flexibility in scheduling these supervisor privileged
SPR accesses with the HV privileged and PR SPR accesses in the low level
entry code.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 379 +-------------------------
arch/powerpc/kvm/book3s_hv.h | 45 +++
arch/powerpc/kvm/book3s_hv_p9_entry.c | 353 ++++++++++++++++++++++++
3 files changed, 399 insertions(+), 378 deletions(-)
create mode 100644 arch/powerpc/kvm/book3s_hv.h
@@ -3810,379 +3806,6 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)trace_kvmppc_run_core(vc,1);}-/*-*Privileged(non-hypervisor)hostregisterstosave.-*/-structp9_host_os_sprs{-unsignedlongdscr;-unsignedlongtidr;-unsignedlongiamr;-unsignedlongamr;-unsignedlongfscr;--unsignedintpmc1;-unsignedintpmc2;-unsignedintpmc3;-unsignedintpmc4;-unsignedintpmc5;-unsignedintpmc6;-unsignedlongmmcr0;-unsignedlongmmcr1;-unsignedlongmmcr2;-unsignedlongmmcr3;-unsignedlongmmcra;-unsignedlongsiar;-unsignedlongsier1;-unsignedlongsier2;-unsignedlongsier3;-unsignedlongsdar;-};--staticvoidfreeze_pmu(unsignedlongmmcr0,unsignedlongmmcra)-{-if(!(mmcr0&MMCR0_FC))-gotodo_freeze;-if(mmcra&MMCRA_SAMPLE_ENABLE)-gotodo_freeze;-if(cpu_has_feature(CPU_FTR_ARCH_31)){-if(!(mmcr0&MMCR0_PMCCEXT))-gotodo_freeze;-if(!(mmcra&MMCRA_BHRB_DISABLE))-gotodo_freeze;-}-return;--do_freeze:-mmcr0=MMCR0_FC;-mmcra=0;-if(cpu_has_feature(CPU_FTR_ARCH_31)){-mmcr0|=MMCR0_PMCCEXT;-mmcra=MMCRA_BHRB_DISABLE;-}--mtspr(SPRN_MMCR0,mmcr0);-mtspr(SPRN_MMCRA,mmcra);-isync();-}--staticvoidswitch_pmu_to_guest(structkvm_vcpu*vcpu,-structp9_host_os_sprs*host_os_sprs)-{-structlppaca*lp;-intload_pmu=1;--lp=vcpu->arch.vpa.pinned_addr;-if(lp)-load_pmu=lp->pmcregs_in_use;--/* Save host */-if(ppc_get_pmu_inuse()){-/*-*ItmightbebettertoputPMUhandling(atleastforthe-*host)intheperfsubsystembecauseitknowsmoreaboutwhat-*isbeingused.-*/--/* POWER9, POWER10 do not implement HPMC or SPMC */--host_os_sprs->mmcr0=mfspr(SPRN_MMCR0);-host_os_sprs->mmcra=mfspr(SPRN_MMCRA);--freeze_pmu(host_os_sprs->mmcr0,host_os_sprs->mmcra);--host_os_sprs->pmc1=mfspr(SPRN_PMC1);-host_os_sprs->pmc2=mfspr(SPRN_PMC2);-host_os_sprs->pmc3=mfspr(SPRN_PMC3);-host_os_sprs->pmc4=mfspr(SPRN_PMC4);-host_os_sprs->pmc5=mfspr(SPRN_PMC5);-host_os_sprs->pmc6=mfspr(SPRN_PMC6);-host_os_sprs->mmcr1=mfspr(SPRN_MMCR1);-host_os_sprs->mmcr2=mfspr(SPRN_MMCR2);-host_os_sprs->sdar=mfspr(SPRN_SDAR);-host_os_sprs->siar=mfspr(SPRN_SIAR);-host_os_sprs->sier1=mfspr(SPRN_SIER);--if(cpu_has_feature(CPU_FTR_ARCH_31)){-host_os_sprs->mmcr3=mfspr(SPRN_MMCR3);-host_os_sprs->sier2=mfspr(SPRN_SIER2);-host_os_sprs->sier3=mfspr(SPRN_SIER3);-}-}--#ifdef CONFIG_PPC_PSERIES-/* After saving PMU, before loading guest PMU, flip pmcregs_in_use */-if(kvmhv_on_pseries()){-barrier();-get_lppaca()->pmcregs_in_use=load_pmu;-barrier();-}-#endif--/*-*Loadguest.IftheVPAsaidthePMCsarenotinusebuttheguest-*triedtoaccessthemanyway,HFSCR[PM]willbesetbytheHFAC-*faultsowecanmakeforwardprogress.-*/-if(load_pmu||(vcpu->arch.hfscr&HFSCR_PM)){-mtspr(SPRN_PMC1,vcpu->arch.pmc[0]);-mtspr(SPRN_PMC2,vcpu->arch.pmc[1]);-mtspr(SPRN_PMC3,vcpu->arch.pmc[2]);-mtspr(SPRN_PMC4,vcpu->arch.pmc[3]);-mtspr(SPRN_PMC5,vcpu->arch.pmc[4]);-mtspr(SPRN_PMC6,vcpu->arch.pmc[5]);-mtspr(SPRN_MMCR1,vcpu->arch.mmcr[1]);-mtspr(SPRN_MMCR2,vcpu->arch.mmcr[2]);-mtspr(SPRN_SDAR,vcpu->arch.sdar);-mtspr(SPRN_SIAR,vcpu->arch.siar);-mtspr(SPRN_SIER,vcpu->arch.sier[0]);--if(cpu_has_feature(CPU_FTR_ARCH_31)){-mtspr(SPRN_MMCR3,vcpu->arch.mmcr[3]);-mtspr(SPRN_SIER2,vcpu->arch.sier[1]);-mtspr(SPRN_SIER3,vcpu->arch.sier[2]);-}--/* Set MMCRA then MMCR0 last */-mtspr(SPRN_MMCRA,vcpu->arch.mmcra);-mtspr(SPRN_MMCR0,vcpu->arch.mmcr[0]);-/* No isync necessary because we're starting counters */--if(!vcpu->arch.nested&&-(vcpu->arch.hfscr_permitted&HFSCR_PM))-vcpu->arch.hfscr|=HFSCR_PM;-}-}--staticvoidswitch_pmu_to_host(structkvm_vcpu*vcpu,-structp9_host_os_sprs*host_os_sprs)-{-structlppaca*lp;-intsave_pmu=1;--lp=vcpu->arch.vpa.pinned_addr;-if(lp)-save_pmu=lp->pmcregs_in_use;-if(IS_ENABLED(CONFIG_KVM_BOOK3S_HV_NESTED_PMU_WORKAROUND)){-/*-*Savepmuifthisguestiscapableofrunningnestedguests.-*ThisisoptionisforoldL1sthatdonotsettheir-*lppaca->pmcregs_in_useproperlywhenenteringtheirL2.-*/-save_pmu|=nesting_enabled(vcpu->kvm);-}--if(save_pmu){-vcpu->arch.mmcr[0]=mfspr(SPRN_MMCR0);-vcpu->arch.mmcra=mfspr(SPRN_MMCRA);--freeze_pmu(vcpu->arch.mmcr[0],vcpu->arch.mmcra);--vcpu->arch.pmc[0]=mfspr(SPRN_PMC1);-vcpu->arch.pmc[1]=mfspr(SPRN_PMC2);-vcpu->arch.pmc[2]=mfspr(SPRN_PMC3);-vcpu->arch.pmc[3]=mfspr(SPRN_PMC4);-vcpu->arch.pmc[4]=mfspr(SPRN_PMC5);-vcpu->arch.pmc[5]=mfspr(SPRN_PMC6);-vcpu->arch.mmcr[1]=mfspr(SPRN_MMCR1);-vcpu->arch.mmcr[2]=mfspr(SPRN_MMCR2);-vcpu->arch.sdar=mfspr(SPRN_SDAR);-vcpu->arch.siar=mfspr(SPRN_SIAR);-vcpu->arch.sier[0]=mfspr(SPRN_SIER);--if(cpu_has_feature(CPU_FTR_ARCH_31)){-vcpu->arch.mmcr[3]=mfspr(SPRN_MMCR3);-vcpu->arch.sier[1]=mfspr(SPRN_SIER2);-vcpu->arch.sier[2]=mfspr(SPRN_SIER3);-}--}elseif(vcpu->arch.hfscr&HFSCR_PM){-/*-*TheguestaccessedPMCSPRswithoutspecifyingtheyshould-*bepreserved,oritclearedpmcregs_in_useafterthelast-*access.Justensuretheyarefrozen.-*/-freeze_pmu(mfspr(SPRN_MMCR0),mfspr(SPRN_MMCRA));--/*-*Demand-faultPMUregisteraccessintheguest.-*-*Thisisusedtograbtheguest'sVPApmcregs_in_usevalue-*andreflectitintothehost'sVPAinthecaseofanested-*hypervisor.-*-*Italsoavoidshavingtozero-outSPRsaftereachguest-*exittoavoidside-channelswhen.-*-*Thisisclearedherewhenweexittheguest,solaterHFSCR-*interrupthandlingcanadditbacktoruntheguestwith-*PMenablednexttime.-*/-if(!vcpu->arch.nested)-vcpu->arch.hfscr&=~HFSCR_PM;-}/* otherwise the PMU should still be frozen */--#ifdef CONFIG_PPC_PSERIES-if(kvmhv_on_pseries()){-barrier();-get_lppaca()->pmcregs_in_use=ppc_get_pmu_inuse();-barrier();-}-#endif--if(ppc_get_pmu_inuse()){-mtspr(SPRN_PMC1,host_os_sprs->pmc1);-mtspr(SPRN_PMC2,host_os_sprs->pmc2);-mtspr(SPRN_PMC3,host_os_sprs->pmc3);-mtspr(SPRN_PMC4,host_os_sprs->pmc4);-mtspr(SPRN_PMC5,host_os_sprs->pmc5);-mtspr(SPRN_PMC6,host_os_sprs->pmc6);-mtspr(SPRN_MMCR1,host_os_sprs->mmcr1);-mtspr(SPRN_MMCR2,host_os_sprs->mmcr2);-mtspr(SPRN_SDAR,host_os_sprs->sdar);-mtspr(SPRN_SIAR,host_os_sprs->siar);-mtspr(SPRN_SIER,host_os_sprs->sier1);--if(cpu_has_feature(CPU_FTR_ARCH_31)){-mtspr(SPRN_MMCR3,host_os_sprs->mmcr3);-mtspr(SPRN_SIER2,host_os_sprs->sier2);-mtspr(SPRN_SIER3,host_os_sprs->sier3);-}--/* Set MMCRA then MMCR0 last */-mtspr(SPRN_MMCRA,host_os_sprs->mmcra);-mtspr(SPRN_MMCR0,host_os_sprs->mmcr0);-isync();-}-}--staticvoidload_spr_state(structkvm_vcpu*vcpu,-structp9_host_os_sprs*host_os_sprs)-{-mtspr(SPRN_TAR,vcpu->arch.tar);-mtspr(SPRN_EBBHR,vcpu->arch.ebbhr);-mtspr(SPRN_EBBRR,vcpu->arch.ebbrr);-mtspr(SPRN_BESCR,vcpu->arch.bescr);--if(cpu_has_feature(CPU_FTR_P9_TIDR))-mtspr(SPRN_TIDR,vcpu->arch.tid);-if(host_os_sprs->iamr!=vcpu->arch.iamr)-mtspr(SPRN_IAMR,vcpu->arch.iamr);-if(host_os_sprs->amr!=vcpu->arch.amr)-mtspr(SPRN_AMR,vcpu->arch.amr);-if(vcpu->arch.uamor!=0)-mtspr(SPRN_UAMOR,vcpu->arch.uamor);-if(host_os_sprs->fscr!=vcpu->arch.fscr)-mtspr(SPRN_FSCR,vcpu->arch.fscr);-if(host_os_sprs->dscr!=vcpu->arch.dscr)-mtspr(SPRN_DSCR,vcpu->arch.dscr);-if(vcpu->arch.pspb!=0)-mtspr(SPRN_PSPB,vcpu->arch.pspb);--/*-*DAR,DSISR,andfornestedHV,SPRGsmustbesetwithMSR[RI]-*clear(orhstatesetappropriatelytocatchthoseregisters-*beingclobberedifwetakeaMCEorSRESET),sothosearedone-*later.-*/--if(!(vcpu->arch.ctrl&1))-mtspr(SPRN_CTRLT,0);-}--staticvoidstore_spr_state(structkvm_vcpu*vcpu)-{-vcpu->arch.tar=mfspr(SPRN_TAR);-vcpu->arch.ebbhr=mfspr(SPRN_EBBHR);-vcpu->arch.ebbrr=mfspr(SPRN_EBBRR);-vcpu->arch.bescr=mfspr(SPRN_BESCR);--if(cpu_has_feature(CPU_FTR_P9_TIDR))-vcpu->arch.tid=mfspr(SPRN_TIDR);-vcpu->arch.iamr=mfspr(SPRN_IAMR);-vcpu->arch.amr=mfspr(SPRN_AMR);-vcpu->arch.uamor=mfspr(SPRN_UAMOR);-vcpu->arch.fscr=mfspr(SPRN_FSCR);-vcpu->arch.dscr=mfspr(SPRN_DSCR);-vcpu->arch.pspb=mfspr(SPRN_PSPB);--vcpu->arch.ctrl=mfspr(SPRN_CTRLF);-}--/* Returns true if current MSR and/or guest MSR may have changed */-staticboolload_vcpu_state(structkvm_vcpu*vcpu,-structp9_host_os_sprs*host_os_sprs)-{-boolret=false;--if(cpu_has_feature(CPU_FTR_TM)||-cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)){-kvmppc_restore_tm_hv(vcpu,vcpu->arch.shregs.msr,true);-ret=true;-}--load_spr_state(vcpu,host_os_sprs);--load_fp_state(&vcpu->arch.fp);-#ifdef CONFIG_ALTIVEC-load_vr_state(&vcpu->arch.vr);-#endif-mtspr(SPRN_VRSAVE,vcpu->arch.vrsave);--returnret;-}--staticvoidstore_vcpu_state(structkvm_vcpu*vcpu)-{-store_spr_state(vcpu);--store_fp_state(&vcpu->arch.fp);-#ifdef CONFIG_ALTIVEC-store_vr_state(&vcpu->arch.vr);-#endif-vcpu->arch.vrsave=mfspr(SPRN_VRSAVE);--if(cpu_has_feature(CPU_FTR_TM)||-cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))-kvmppc_save_tm_hv(vcpu,vcpu->arch.shregs.msr,true);-}--staticvoidsave_p9_host_os_sprs(structp9_host_os_sprs*host_os_sprs)-{-host_os_sprs->dscr=mfspr(SPRN_DSCR);-if(cpu_has_feature(CPU_FTR_P9_TIDR))-host_os_sprs->tidr=mfspr(SPRN_TIDR);-host_os_sprs->iamr=mfspr(SPRN_IAMR);-host_os_sprs->amr=mfspr(SPRN_AMR);-host_os_sprs->fscr=mfspr(SPRN_FSCR);-host_os_sprs->dscr=mfspr(SPRN_DSCR);-}--/* vcpu guest regs must already be saved */-staticvoidrestore_p9_host_os_sprs(structkvm_vcpu*vcpu,-structp9_host_os_sprs*host_os_sprs)-{-mtspr(SPRN_SPRG_VDSO_WRITE,local_paca->sprg_vdso);--if(cpu_has_feature(CPU_FTR_P9_TIDR))-mtspr(SPRN_TIDR,host_os_sprs->tidr);-if(host_os_sprs->iamr!=vcpu->arch.iamr)-mtspr(SPRN_IAMR,host_os_sprs->iamr);-if(vcpu->arch.uamor!=0)-mtspr(SPRN_UAMOR,0);-if(host_os_sprs->amr!=vcpu->arch.amr)-mtspr(SPRN_AMR,host_os_sprs->amr);-if(host_os_sprs->fscr!=vcpu->arch.fscr)-mtspr(SPRN_FSCR,host_os_sprs->fscr);-if(host_os_sprs->dscr!=vcpu->arch.dscr)-mtspr(SPRN_DSCR,host_os_sprs->dscr);-if(vcpu->arch.pspb!=0)-mtspr(SPRN_PSPB,0);--/* Save guest CTRL register, set runlatch to 1 */-if(!(vcpu->arch.ctrl&1))-mtspr(SPRN_CTRLT,1);-}-staticinlineboolhcall_is_xics(unsignedlongreq){returnreq==H_EOI||req==H_CPPR||req==H_IPI||
@@ -4,8 +4,361 @@#include<asm/asm-prototypes.h>#include<asm/dbell.h>#include<asm/kvm_ppc.h>+#include<asm/pmc.h>#include<asm/ppc-opcode.h>+#include"book3s_hv.h"++staticvoidfreeze_pmu(unsignedlongmmcr0,unsignedlongmmcra)+{+if(!(mmcr0&MMCR0_FC))+gotodo_freeze;+if(mmcra&MMCRA_SAMPLE_ENABLE)+gotodo_freeze;+if(cpu_has_feature(CPU_FTR_ARCH_31)){+if(!(mmcr0&MMCR0_PMCCEXT))+gotodo_freeze;+if(!(mmcra&MMCRA_BHRB_DISABLE))+gotodo_freeze;+}+return;++do_freeze:+mmcr0=MMCR0_FC;+mmcra=0;+if(cpu_has_feature(CPU_FTR_ARCH_31)){+mmcr0|=MMCR0_PMCCEXT;+mmcra=MMCRA_BHRB_DISABLE;+}++mtspr(SPRN_MMCR0,mmcr0);+mtspr(SPRN_MMCRA,mmcra);+isync();+}++voidswitch_pmu_to_guest(structkvm_vcpu*vcpu,+structp9_host_os_sprs*host_os_sprs)+{+structlppaca*lp;+intload_pmu=1;++lp=vcpu->arch.vpa.pinned_addr;+if(lp)+load_pmu=lp->pmcregs_in_use;++/* Save host */+if(ppc_get_pmu_inuse()){+/*+*ItmightbebettertoputPMUhandling(atleastforthe+*host)intheperfsubsystembecauseitknowsmoreaboutwhat+*isbeingused.+*/++/* POWER9, POWER10 do not implement HPMC or SPMC */++host_os_sprs->mmcr0=mfspr(SPRN_MMCR0);+host_os_sprs->mmcra=mfspr(SPRN_MMCRA);++freeze_pmu(host_os_sprs->mmcr0,host_os_sprs->mmcra);++host_os_sprs->pmc1=mfspr(SPRN_PMC1);+host_os_sprs->pmc2=mfspr(SPRN_PMC2);+host_os_sprs->pmc3=mfspr(SPRN_PMC3);+host_os_sprs->pmc4=mfspr(SPRN_PMC4);+host_os_sprs->pmc5=mfspr(SPRN_PMC5);+host_os_sprs->pmc6=mfspr(SPRN_PMC6);+host_os_sprs->mmcr1=mfspr(SPRN_MMCR1);+host_os_sprs->mmcr2=mfspr(SPRN_MMCR2);+host_os_sprs->sdar=mfspr(SPRN_SDAR);+host_os_sprs->siar=mfspr(SPRN_SIAR);+host_os_sprs->sier1=mfspr(SPRN_SIER);++if(cpu_has_feature(CPU_FTR_ARCH_31)){+host_os_sprs->mmcr3=mfspr(SPRN_MMCR3);+host_os_sprs->sier2=mfspr(SPRN_SIER2);+host_os_sprs->sier3=mfspr(SPRN_SIER3);+}+}++#ifdef CONFIG_PPC_PSERIES+/* After saving PMU, before loading guest PMU, flip pmcregs_in_use */+if(kvmhv_on_pseries()){+barrier();+get_lppaca()->pmcregs_in_use=load_pmu;+barrier();+}+#endif++/*+*Loadguest.IftheVPAsaidthePMCsarenotinusebuttheguest+*triedtoaccessthemanyway,HFSCR[PM]willbesetbytheHFAC+*faultsowecanmakeforwardprogress.+*/+if(load_pmu||(vcpu->arch.hfscr&HFSCR_PM)){+mtspr(SPRN_PMC1,vcpu->arch.pmc[0]);+mtspr(SPRN_PMC2,vcpu->arch.pmc[1]);+mtspr(SPRN_PMC3,vcpu->arch.pmc[2]);+mtspr(SPRN_PMC4,vcpu->arch.pmc[3]);+mtspr(SPRN_PMC5,vcpu->arch.pmc[4]);+mtspr(SPRN_PMC6,vcpu->arch.pmc[5]);+mtspr(SPRN_MMCR1,vcpu->arch.mmcr[1]);+mtspr(SPRN_MMCR2,vcpu->arch.mmcr[2]);+mtspr(SPRN_SDAR,vcpu->arch.sdar);+mtspr(SPRN_SIAR,vcpu->arch.siar);+mtspr(SPRN_SIER,vcpu->arch.sier[0]);++if(cpu_has_feature(CPU_FTR_ARCH_31)){+mtspr(SPRN_MMCR3,vcpu->arch.mmcr[3]);+mtspr(SPRN_SIER2,vcpu->arch.sier[1]);+mtspr(SPRN_SIER3,vcpu->arch.sier[2]);+}++/* Set MMCRA then MMCR0 last */+mtspr(SPRN_MMCRA,vcpu->arch.mmcra);+mtspr(SPRN_MMCR0,vcpu->arch.mmcr[0]);+/* No isync necessary because we're starting counters */++if(!vcpu->arch.nested&&+(vcpu->arch.hfscr_permitted&HFSCR_PM))+vcpu->arch.hfscr|=HFSCR_PM;+}+}+EXPORT_SYMBOL_GPL(switch_pmu_to_guest);++voidswitch_pmu_to_host(structkvm_vcpu*vcpu,+structp9_host_os_sprs*host_os_sprs)+{+structlppaca*lp;+intsave_pmu=1;++lp=vcpu->arch.vpa.pinned_addr;+if(lp)+save_pmu=lp->pmcregs_in_use;+if(IS_ENABLED(CONFIG_KVM_BOOK3S_HV_NESTED_PMU_WORKAROUND)){+/*+*Savepmuifthisguestiscapableofrunningnestedguests.+*ThisisoptionisforoldL1sthatdonotsettheir+*lppaca->pmcregs_in_useproperlywhenenteringtheirL2.+*/+save_pmu|=nesting_enabled(vcpu->kvm);+}++if(save_pmu){+vcpu->arch.mmcr[0]=mfspr(SPRN_MMCR0);+vcpu->arch.mmcra=mfspr(SPRN_MMCRA);++freeze_pmu(vcpu->arch.mmcr[0],vcpu->arch.mmcra);++vcpu->arch.pmc[0]=mfspr(SPRN_PMC1);+vcpu->arch.pmc[1]=mfspr(SPRN_PMC2);+vcpu->arch.pmc[2]=mfspr(SPRN_PMC3);+vcpu->arch.pmc[3]=mfspr(SPRN_PMC4);+vcpu->arch.pmc[4]=mfspr(SPRN_PMC5);+vcpu->arch.pmc[5]=mfspr(SPRN_PMC6);+vcpu->arch.mmcr[1]=mfspr(SPRN_MMCR1);+vcpu->arch.mmcr[2]=mfspr(SPRN_MMCR2);+vcpu->arch.sdar=mfspr(SPRN_SDAR);+vcpu->arch.siar=mfspr(SPRN_SIAR);+vcpu->arch.sier[0]=mfspr(SPRN_SIER);++if(cpu_has_feature(CPU_FTR_ARCH_31)){+vcpu->arch.mmcr[3]=mfspr(SPRN_MMCR3);+vcpu->arch.sier[1]=mfspr(SPRN_SIER2);+vcpu->arch.sier[2]=mfspr(SPRN_SIER3);+}++}elseif(vcpu->arch.hfscr&HFSCR_PM){+/*+*TheguestaccessedPMCSPRswithoutspecifyingtheyshould+*bepreserved,oritclearedpmcregs_in_useafterthelast+*access.Justensuretheyarefrozen.+*/+freeze_pmu(mfspr(SPRN_MMCR0),mfspr(SPRN_MMCRA));++/*+*Demand-faultPMUregisteraccessintheguest.+*+*Thisisusedtograbtheguest'sVPApmcregs_in_usevalue+*andreflectitintothehost'sVPAinthecaseofanested+*hypervisor.+*+*Italsoavoidshavingtozero-outSPRsaftereachguest+*exittoavoidside-channelswhen.+*+*Thisisclearedherewhenweexittheguest,solaterHFSCR+*interrupthandlingcanadditbacktoruntheguestwith+*PMenablednexttime.+*/+if(!vcpu->arch.nested)+vcpu->arch.hfscr&=~HFSCR_PM;+}/* otherwise the PMU should still be frozen */++#ifdef CONFIG_PPC_PSERIES+if(kvmhv_on_pseries()){+barrier();+get_lppaca()->pmcregs_in_use=ppc_get_pmu_inuse();+barrier();+}+#endif++if(ppc_get_pmu_inuse()){+mtspr(SPRN_PMC1,host_os_sprs->pmc1);+mtspr(SPRN_PMC2,host_os_sprs->pmc2);+mtspr(SPRN_PMC3,host_os_sprs->pmc3);+mtspr(SPRN_PMC4,host_os_sprs->pmc4);+mtspr(SPRN_PMC5,host_os_sprs->pmc5);+mtspr(SPRN_PMC6,host_os_sprs->pmc6);+mtspr(SPRN_MMCR1,host_os_sprs->mmcr1);+mtspr(SPRN_MMCR2,host_os_sprs->mmcr2);+mtspr(SPRN_SDAR,host_os_sprs->sdar);+mtspr(SPRN_SIAR,host_os_sprs->siar);+mtspr(SPRN_SIER,host_os_sprs->sier1);++if(cpu_has_feature(CPU_FTR_ARCH_31)){+mtspr(SPRN_MMCR3,host_os_sprs->mmcr3);+mtspr(SPRN_SIER2,host_os_sprs->sier2);+mtspr(SPRN_SIER3,host_os_sprs->sier3);+}++/* Set MMCRA then MMCR0 last */+mtspr(SPRN_MMCRA,host_os_sprs->mmcra);+mtspr(SPRN_MMCR0,host_os_sprs->mmcr0);+isync();+}+}+EXPORT_SYMBOL_GPL(switch_pmu_to_host);++staticvoidload_spr_state(structkvm_vcpu*vcpu,+structp9_host_os_sprs*host_os_sprs)+{+mtspr(SPRN_TAR,vcpu->arch.tar);+mtspr(SPRN_EBBHR,vcpu->arch.ebbhr);+mtspr(SPRN_EBBRR,vcpu->arch.ebbrr);+mtspr(SPRN_BESCR,vcpu->arch.bescr);++if(cpu_has_feature(CPU_FTR_P9_TIDR))+mtspr(SPRN_TIDR,vcpu->arch.tid);+if(host_os_sprs->iamr!=vcpu->arch.iamr)+mtspr(SPRN_IAMR,vcpu->arch.iamr);+if(host_os_sprs->amr!=vcpu->arch.amr)+mtspr(SPRN_AMR,vcpu->arch.amr);+if(vcpu->arch.uamor!=0)+mtspr(SPRN_UAMOR,vcpu->arch.uamor);+if(host_os_sprs->fscr!=vcpu->arch.fscr)+mtspr(SPRN_FSCR,vcpu->arch.fscr);+if(host_os_sprs->dscr!=vcpu->arch.dscr)+mtspr(SPRN_DSCR,vcpu->arch.dscr);+if(vcpu->arch.pspb!=0)+mtspr(SPRN_PSPB,vcpu->arch.pspb);++/*+*DAR,DSISR,andfornestedHV,SPRGsmustbesetwithMSR[RI]+*clear(orhstatesetappropriatelytocatchthoseregisters+*beingclobberedifwetakeaMCEorSRESET),sothosearedone+*later.+*/++if(!(vcpu->arch.ctrl&1))+mtspr(SPRN_CTRLT,0);+}++staticvoidstore_spr_state(structkvm_vcpu*vcpu)+{+vcpu->arch.tar=mfspr(SPRN_TAR);+vcpu->arch.ebbhr=mfspr(SPRN_EBBHR);+vcpu->arch.ebbrr=mfspr(SPRN_EBBRR);+vcpu->arch.bescr=mfspr(SPRN_BESCR);++if(cpu_has_feature(CPU_FTR_P9_TIDR))+vcpu->arch.tid=mfspr(SPRN_TIDR);+vcpu->arch.iamr=mfspr(SPRN_IAMR);+vcpu->arch.amr=mfspr(SPRN_AMR);+vcpu->arch.uamor=mfspr(SPRN_UAMOR);+vcpu->arch.fscr=mfspr(SPRN_FSCR);+vcpu->arch.dscr=mfspr(SPRN_DSCR);+vcpu->arch.pspb=mfspr(SPRN_PSPB);++vcpu->arch.ctrl=mfspr(SPRN_CTRLF);+}++/* Returns true if current MSR and/or guest MSR may have changed */+boolload_vcpu_state(structkvm_vcpu*vcpu,+structp9_host_os_sprs*host_os_sprs)+{+boolret=false;++if(cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)){+kvmppc_restore_tm_hv(vcpu,vcpu->arch.shregs.msr,true);+ret=true;+}++load_spr_state(vcpu,host_os_sprs);++load_fp_state(&vcpu->arch.fp);+#ifdef CONFIG_ALTIVEC+load_vr_state(&vcpu->arch.vr);+#endif+mtspr(SPRN_VRSAVE,vcpu->arch.vrsave);++returnret;+}+EXPORT_SYMBOL_GPL(load_vcpu_state);++voidstore_vcpu_state(structkvm_vcpu*vcpu)+{+store_spr_state(vcpu);++store_fp_state(&vcpu->arch.fp);+#ifdef CONFIG_ALTIVEC+store_vr_state(&vcpu->arch.vr);+#endif+vcpu->arch.vrsave=mfspr(SPRN_VRSAVE);++if(cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))+kvmppc_save_tm_hv(vcpu,vcpu->arch.shregs.msr,true);+}+EXPORT_SYMBOL_GPL(store_vcpu_state);++voidsave_p9_host_os_sprs(structp9_host_os_sprs*host_os_sprs)+{+if(cpu_has_feature(CPU_FTR_P9_TIDR))+host_os_sprs->tidr=mfspr(SPRN_TIDR);+host_os_sprs->iamr=mfspr(SPRN_IAMR);+host_os_sprs->amr=mfspr(SPRN_AMR);+host_os_sprs->fscr=mfspr(SPRN_FSCR);+host_os_sprs->dscr=mfspr(SPRN_DSCR);+}+EXPORT_SYMBOL_GPL(save_p9_host_os_sprs);++/* vcpu guest regs must already be saved */+voidrestore_p9_host_os_sprs(structkvm_vcpu*vcpu,+structp9_host_os_sprs*host_os_sprs)+{+mtspr(SPRN_SPRG_VDSO_WRITE,local_paca->sprg_vdso);++if(cpu_has_feature(CPU_FTR_P9_TIDR))+mtspr(SPRN_TIDR,host_os_sprs->tidr);+if(host_os_sprs->iamr!=vcpu->arch.iamr)+mtspr(SPRN_IAMR,host_os_sprs->iamr);+if(vcpu->arch.uamor!=0)+mtspr(SPRN_UAMOR,0);+if(host_os_sprs->amr!=vcpu->arch.amr)+mtspr(SPRN_AMR,host_os_sprs->amr);+if(host_os_sprs->fscr!=vcpu->arch.fscr)+mtspr(SPRN_FSCR,host_os_sprs->fscr);+if(host_os_sprs->dscr!=vcpu->arch.dscr)+mtspr(SPRN_DSCR,host_os_sprs->dscr);+if(vcpu->arch.pspb!=0)+mtspr(SPRN_PSPB,0);++/* Save guest CTRL register, set runlatch to 1 */+if(!(vcpu->arch.ctrl&1))+mtspr(SPRN_CTRLT,1);+}+EXPORT_SYMBOL_GPL(restore_p9_host_os_sprs);+#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMINGstaticvoid__start_timing(structkvm_vcpu*vcpu,structkvmhv_tb_accumulator*next){
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:12:34
Move the part of the guest entry which is specific to nested HV into its
own function. This is just refactoring.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 125 +++++++++++++++++++----------------
1 file changed, 67 insertions(+), 58 deletions(-)
@@ -3822,6 +3822,72 @@ static void vcpu_vpa_increment_dispatch(struct kvm_vcpu *vcpu)}}+/* call our hypervisor to load up HV regs and go */+staticintkvmhv_vcpu_entry_p9_nested(structkvm_vcpu*vcpu,u64time_limit,unsignedlonglpcr,u64*tb)+{+structkvmppc_vcore*vc=vcpu->arch.vcore;+unsignedlonghost_psscr;+structhv_guest_statehvregs;+inttrap;+s64dec;++/*+*Weneedtosaveandrestoretheguestvisiblepartofthe+*psscr(i.e.usingSPRN_PSSCR_PR)sincethehypervisor+*doesn'tdothisforus.Noteonlyrequiredifpseriessince+*thisisdoneinkvmhv_vcpu_entry_p9()belowotherwise.+*/+host_psscr=mfspr(SPRN_PSSCR_PR);+mtspr(SPRN_PSSCR_PR,vcpu->arch.psscr);+kvmhv_save_hv_regs(vcpu,&hvregs);+hvregs.lpcr=lpcr;+vcpu->arch.regs.msr=vcpu->arch.shregs.msr;+hvregs.version=HV_GUEST_STATE_VERSION;+if(vcpu->arch.nested){+hvregs.lpid=vcpu->arch.nested->shadow_lpid;+hvregs.vcpu_token=vcpu->arch.nested_vcpu_id;+}else{+hvregs.lpid=vcpu->kvm->arch.lpid;+hvregs.vcpu_token=vcpu->vcpu_id;+}+hvregs.hdec_expiry=time_limit;++/*+*WhensettingDEC,wemustalwaysdealwithirq_work_raise+*viaNMIvssettingDEC.Theproblemoccursrightaswe+*switchintoguestmodeifaNMIhitsandsetspendingwork+*andsetsDEC,thenthatwillapplytotheguestandnot+*bringusbacktothehost.+*+*irq_work_raisecouldcheckaflag(orpossiblyLPCR[HDICE]+*forexample)andsetHDECto1?Thatwouldn'tsolvethe+*nestedhvcasewhichneedstoabortthehcallorzerothe+*timelimit.+*+*XXX:Anotherday'sproblem.+*/+mtspr(SPRN_DEC,kvmppc_dec_expires_host_tb(vcpu)-*tb);++mtspr(SPRN_DAR,vcpu->arch.shregs.dar);+mtspr(SPRN_DSISR,vcpu->arch.shregs.dsisr);+trap=plpar_hcall_norets(H_ENTER_NESTED,__pa(&hvregs),+__pa(&vcpu->arch.regs));+kvmhv_restore_hv_return_state(vcpu,&hvregs);+vcpu->arch.shregs.msr=vcpu->arch.regs.msr;+vcpu->arch.shregs.dar=mfspr(SPRN_DAR);+vcpu->arch.shregs.dsisr=mfspr(SPRN_DSISR);+vcpu->arch.psscr=mfspr(SPRN_PSSCR_PR);+mtspr(SPRN_PSSCR_PR,host_psscr);++dec=mfspr(SPRN_DEC);+if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */+dec=(s32)dec;+*tb=mftb();+vcpu->arch.dec_expires=dec+(*tb+vc->tb_offset);++returntrap;+}+/**GuestentryforPOWER9andlaterCPUs.*/
@@ -3883,63 +3948,7 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,switch_pmu_to_guest(vcpu,&host_os_sprs);if(kvmhv_on_pseries()){-/*-*Weneedtosaveandrestoretheguestvisiblepartofthe-*psscr(i.e.usingSPRN_PSSCR_PR)sincethehypervisor-*doesn'tdothisforus.Noteonlyrequiredifpseriessince-*thisisdoneinkvmhv_vcpu_entry_p9()belowotherwise.-*/-unsignedlonghost_psscr;-/* call our hypervisor to load up HV regs and go */-structhv_guest_statehvregs;--host_psscr=mfspr(SPRN_PSSCR_PR);-mtspr(SPRN_PSSCR_PR,vcpu->arch.psscr);-kvmhv_save_hv_regs(vcpu,&hvregs);-hvregs.lpcr=lpcr;-vcpu->arch.regs.msr=vcpu->arch.shregs.msr;-hvregs.version=HV_GUEST_STATE_VERSION;-if(vcpu->arch.nested){-hvregs.lpid=vcpu->arch.nested->shadow_lpid;-hvregs.vcpu_token=vcpu->arch.nested_vcpu_id;-}else{-hvregs.lpid=vcpu->kvm->arch.lpid;-hvregs.vcpu_token=vcpu->vcpu_id;-}-hvregs.hdec_expiry=time_limit;--/*-*WhensettingDEC,wemustalwaysdealwithirq_work_raise-*viaNMIvssettingDEC.Theproblemoccursrightaswe-*switchintoguestmodeifaNMIhitsandsetspendingwork-*andsetsDEC,thenthatwillapplytotheguestandnot-*bringusbacktothehost.-*-*irq_work_raisecouldcheckaflag(orpossiblyLPCR[HDICE]-*forexample)andsetHDECto1?Thatwouldn'tsolvethe-*nestedhvcasewhichneedstoabortthehcallorzerothe-*timelimit.-*-*XXX:Anotherday'sproblem.-*/-mtspr(SPRN_DEC,kvmppc_dec_expires_host_tb(vcpu)-*tb);--mtspr(SPRN_DAR,vcpu->arch.shregs.dar);-mtspr(SPRN_DSISR,vcpu->arch.shregs.dsisr);-trap=plpar_hcall_norets(H_ENTER_NESTED,__pa(&hvregs),-__pa(&vcpu->arch.regs));-kvmhv_restore_hv_return_state(vcpu,&hvregs);-vcpu->arch.shregs.msr=vcpu->arch.regs.msr;-vcpu->arch.shregs.dar=mfspr(SPRN_DAR);-vcpu->arch.shregs.dsisr=mfspr(SPRN_DSISR);-vcpu->arch.psscr=mfspr(SPRN_PSSCR_PR);-mtspr(SPRN_PSSCR_PR,host_psscr);--dec=mfspr(SPRN_DEC);-if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */-dec=(s32)dec;-*tb=mftb();-vcpu->arch.dec_expires=dec+(*tb+vc->tb_offset);+trap=kvmhv_vcpu_entry_p9_nested(vcpu,time_limit,lpcr,tb);/* H_CEDE has to be handled now, not later */if(trap==BOOK3S_INTERRUPT_SYSCALL&&!vcpu->arch.nested&&
@@ -3838,6 +3844,27 @@ static int kvmhv_vcpu_entry_p9_nested(struct kvm_vcpu *vcpu, u64 time_limit, uns*thisisdoneinkvmhv_vcpu_entry_p9()belowotherwise.*/host_psscr=mfspr(SPRN_PSSCR_PR);++hard_irq_disable();+if(lazy_irq_pending())+return0;++/* MSR bits may have been cleared by context switch */+msr=0;+if(IS_ENABLED(CONFIG_PPC_FPU))+msr|=MSR_FP;+if(cpu_has_feature(CPU_FTR_ALTIVEC))+msr|=MSR_VEC;+if(cpu_has_feature(CPU_FTR_VSX))+msr|=MSR_VSX;+if(cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))+msr|=MSR_TM;+msr=msr_check_and_set(msr);++if(unlikely(load_vcpu_state(vcpu,&host_os_sprs)))+msr=mfmsr();/* TM restore can update msr */+mtspr(SPRN_PSSCR_PR,vcpu->arch.psscr);kvmhv_save_hv_regs(vcpu,&hvregs);hvregs.lpcr=lpcr;
@@ -3879,12 +3906,20 @@ static int kvmhv_vcpu_entry_p9_nested(struct kvm_vcpu *vcpu, u64 time_limit, unsvcpu->arch.psscr=mfspr(SPRN_PSSCR_PR);mtspr(SPRN_PSSCR_PR,host_psscr);+store_vcpu_state(vcpu);+dec=mfspr(SPRN_DEC);if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */dec=(s32)dec;*tb=mftb();vcpu->arch.dec_expires=dec+(*tb+vc->tb_offset);+timer_rearm_host_dec(*tb);++restore_p9_host_os_sprs(vcpu,&host_os_sprs);++switch_pmu_to_host(vcpu,&host_os_sprs);+returntrap;}
@@ -3908,33 +3941,6 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vcpu->arch.ceded=0;-save_p9_host_os_sprs(&host_os_sprs);--/*-*ThiscouldbecombinedwithMSR[RI]clearing,butthatexpands-*theunrecoverablewindow.Itwouldbebettertocoverunrecoverable-*withKVMbadinterrupthandlingratherthanuseMSR[RI]atall.-*-*MuchmoredifficultandlessworthwhiletocombinewithIR/DR-*disable.-*/-hard_irq_disable();-if(lazy_irq_pending())-return0;--/* MSR bits may have been cleared by context switch */-msr=0;-if(IS_ENABLED(CONFIG_PPC_FPU))-msr|=MSR_FP;-if(cpu_has_feature(CPU_FTR_ALTIVEC))-msr|=MSR_VEC;-if(cpu_has_feature(CPU_FTR_VSX))-msr|=MSR_VSX;-if(cpu_has_feature(CPU_FTR_TM)||-cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))-msr|=MSR_TM;-msr=msr_check_and_set(msr);-kvmppc_subcore_enter_guest();vc->entry_exit_map=1;
@@ -3942,11 +3948,6 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,vcpu_vpa_increment_dispatch(vcpu);-if(unlikely(load_vcpu_state(vcpu,&host_os_sprs)))-msr=mfmsr();/* MSR may have been updated */--switch_pmu_to_guest(vcpu,&host_os_sprs);-if(kvmhv_on_pseries()){trap=kvmhv_vcpu_entry_p9_nested(vcpu,time_limit,lpcr,tb);
@@ -567,9 +568,6 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvcpu->arch.ceded=0;-/* Could avoid mfmsr by passing around, but probably no big deal */-msr=mfmsr();-host_hfscr=mfspr(SPRN_HFSCR);host_ciabr=mfspr(SPRN_CIABR);host_dawr0=mfspr(SPRN_DAWR0);
@@ -584,6 +582,41 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpclocal_paca->kvm_hstate.host_purr=mfspr(SPRN_PURR);local_paca->kvm_hstate.host_spurr=mfspr(SPRN_SPURR);+switch_pmu_to_guest(vcpu,&host_os_sprs);++save_p9_host_os_sprs(&host_os_sprs);++/*+*ThiscouldbecombinedwithMSR[RI]clearing,butthatexpands+*theunrecoverablewindow.Itwouldbebettertocoverunrecoverable+*withKVMbadinterrupthandlingratherthanuseMSR[RI]atall.+*+*MuchmoredifficultandlessworthwhiletocombinewithIR/DR+*disable.+*/+hard_irq_disable();+if(lazy_irq_pending()){+trap=0;+gotoout;+}++/* MSR bits may have been cleared by context switch */+msr=0;+if(IS_ENABLED(CONFIG_PPC_FPU))+msr|=MSR_FP;+if(cpu_has_feature(CPU_FTR_ALTIVEC))+msr|=MSR_VEC;+if(cpu_has_feature(CPU_FTR_VSX))+msr|=MSR_VSX;+if(cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))+msr|=MSR_TM;+msr=msr_check_and_set(msr);+/* Save MSR for restore. This is after hard disable, so EE is clear. */++if(unlikely(load_vcpu_state(vcpu,&host_os_sprs)))+msr=mfmsr();/* MSR may have been updated */+if(vc->tb_offset){u64new_tb=*tb+vc->tb_offset;mtspr(SPRN_TBU40,new_tb);
@@ -642,6 +675,14 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcmtspr(SPRN_SPRG2,vcpu->arch.shregs.sprg2);mtspr(SPRN_SPRG3,vcpu->arch.shregs.sprg3);+/*+*Itmightbepreferabletoload_vcpu_statehere,inordertogetthe+*GPR/FPregisterloadsexecutinginparallelwiththepreviousmtSPR+*instructions,butfornowthatcan'tbedonebecausetheTMhandling+*inload_vcpu_statecanchangesomeSPRsandvcpustate(nip,msr).+*ButTMcouldbesplitoutifthiswouldbeasignificantbenefit.+*/+local_paca->kvm_hstate.in_guest=KVM_GUEST_MODE_HV_P9;/*
@@ -819,6 +860,20 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvc->dpdes=mfspr(SPRN_DPDES);vc->vtb=mfspr(SPRN_VTB);+save_clear_guest_mmu(kvm,vcpu);+switch_mmu_to_host(kvm,host_pidr);++/*+*Ifweareinrealmode,onlyswitchMMUonaftertheMMUis+*switchedtohost,toavoidtheP9_RADIX_PREFETCH_BUG.+*/+if(IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM)&&+vcpu->arch.shregs.msr&MSR_TS_MASK)+msr|=MSR_TS_S;+__mtmsrd(msr,0);++store_vcpu_state(vcpu);+dec=mfspr(SPRN_DEC);if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */dec=(s32)dec;
@@ -851,6 +906,19 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcmtspr(SPRN_DAWRX1,host_dawrx1);}+mtspr(SPRN_DPDES,0);+if(vc->pcr)+mtspr(SPRN_PCR,PCR_MASK);++/* HDEC must be at least as large as DEC, so decrementer_max fits */+mtspr(SPRN_HDEC,decrementer_max);++timer_rearm_host_dec(*tb);++restore_p9_host_os_sprs(vcpu,&host_os_sprs);++local_paca->kvm_hstate.in_guest=KVM_GUEST_MODE_NONE;+if(kvm_is_radix(kvm)){/**Sincethisisradix,doaeieio;tlbsync;ptesyncsequence
@@ -867,26 +935,8 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcif(cpu_has_feature(CPU_FTR_ARCH_31))asmvolatile(PPC_CP_ABORT);-mtspr(SPRN_DPDES,0);-if(vc->pcr)-mtspr(SPRN_PCR,PCR_MASK);--/* HDEC must be at least as large as DEC, so decrementer_max fits */-mtspr(SPRN_HDEC,decrementer_max);--save_clear_guest_mmu(kvm,vcpu);-switch_mmu_to_host(kvm,host_pidr);-local_paca->kvm_hstate.in_guest=KVM_GUEST_MODE_NONE;--/*-*Ifweareinrealmode,onlyswitchMMUonaftertheMMUis-*switchedtohost,toavoidtheP9_RADIX_PREFETCH_BUG.-*/-if(IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM)&&-vcpu->arch.shregs.msr&MSR_TS_MASK)-msr|=MSR_TS_S;--__mtmsrd(msr,0);+out:+switch_pmu_to_host(vcpu,&host_os_sprs);end_timing(vcpu);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:13:58
If TM is not active, only TM register state needs to be saved and
restored, avoiding several mfmsr/mtmsrd instructions and improving
performance.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv_p9_entry.c | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:14:37
This moves PMU switch to guest as late as possible in entry, and switch
back to host as early as possible at exit. This helps the host get the
most perf coverage of KVM entry/exit code as possible.
This is slightly suboptimal for SPR scheduling point of view when the
PMU is enabled, but when perf is disabled there is no real difference.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 6 ++----
arch/powerpc/kvm/book3s_hv_p9_entry.c | 6 ++----
2 files changed, 4 insertions(+), 8 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:15:21
Use CPU_FTR_P9_RADIX_PREFETCH_BUG to apply the workaround, to test for
DD2.1 and below processors. This saves a mtSPR in guest entry.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 3 ++-
arch/powerpc/kvm/book3s_hv_p9_entry.c | 6 ++++--
2 files changed, 6 insertions(+), 3 deletions(-)
@@ -1590,7 +1590,8 @@ static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,unsignedlongvsid;longerr;-if(vcpu->arch.fault_dsisr==HDSISR_CANARY){+if(cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG)&&+unlikely(vcpu->arch.fault_dsisr==HDSISR_CANARY)){r=RESUME_GUEST;/* Just retry if it's the canary */break;}
@@ -645,24 +645,29 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvc->tb_offset_applied=vc->tb_offset;}-if(vc->pcr)-mtspr(SPRN_PCR,vc->pcr|PCR_MASK);-mtspr(SPRN_DPDES,vc->dpdes);mtspr(SPRN_VTB,vc->vtb);-mtspr(SPRN_PURR,vcpu->arch.purr);mtspr(SPRN_SPURR,vcpu->arch.spurr);+if(vc->pcr)+mtspr(SPRN_PCR,vc->pcr|PCR_MASK);+if(vc->dpdes)+mtspr(SPRN_DPDES,vc->dpdes);+if(dawr_enabled()){-mtspr(SPRN_DAWR0,vcpu->arch.dawr0);-mtspr(SPRN_DAWRX0,vcpu->arch.dawrx0);+if(vcpu->arch.dawr0!=host_dawr0)+mtspr(SPRN_DAWR0,vcpu->arch.dawr0);+if(vcpu->arch.dawrx0!=host_dawrx0)+mtspr(SPRN_DAWRX0,vcpu->arch.dawrx0);if(cpu_has_feature(CPU_FTR_DAWR1)){-mtspr(SPRN_DAWR1,vcpu->arch.dawr1);-mtspr(SPRN_DAWRX1,vcpu->arch.dawrx1);+if(vcpu->arch.dawr1!=host_dawr1)+mtspr(SPRN_DAWR1,vcpu->arch.dawr1);+if(vcpu->arch.dawrx1!=host_dawrx1)+mtspr(SPRN_DAWRX1,vcpu->arch.dawrx1);}}-mtspr(SPRN_CIABR,vcpu->arch.ciabr);-mtspr(SPRN_IC,vcpu->arch.ic);+if(vcpu->arch.ciabr!=host_ciabr)+mtspr(SPRN_CIABR,vcpu->arch.ciabr);mtspr(SPRN_PSSCR,vcpu->arch.psscr|PSSCR_EC|(local_paca->kvm_hstate.fake_suspend<<PSSCR_FAKE_SUSPEND_LG));
@@ -881,20 +886,6 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvc->dpdes=mfspr(SPRN_DPDES);vc->vtb=mfspr(SPRN_VTB);-save_clear_guest_mmu(kvm,vcpu);-switch_mmu_to_host(kvm,host_pidr);--/*-*Ifweareinrealmode,onlyswitchMMUonaftertheMMUis-*switchedtohost,toavoidtheP9_RADIX_PREFETCH_BUG.-*/-if(IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM)&&-vcpu->arch.shregs.msr&MSR_TS_MASK)-msr|=MSR_TS_S;-__mtmsrd(msr,0);--store_vcpu_state(vcpu);-dec=mfspr(SPRN_DEC);if(!(lpcr&LPCR_LD))/* Sign extend if not using large decrementer */dec=(s32)dec;
@@ -912,6 +903,22 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvc->tb_offset_applied=0;}+save_clear_guest_mmu(kvm,vcpu);+switch_mmu_to_host(kvm,host_pidr);++/*+*EnableMSRhereinordertohavefacilitiesenabledtosave+*guestregisters.ThisenablesMMU(ifwewereinrealmode),so+*onlyswitchMMUonaftertheMMUisswitchedtohost,toavoid+*theP9_RADIX_PREFETCH_BUGorhashguestcontext.+*/+if(IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM)&&+vcpu->arch.shregs.msr&MSR_TS_MASK)+msr|=MSR_TS_S;+__mtmsrd(msr,0);++store_vcpu_state(vcpu);+mtspr(SPRN_PURR,local_paca->kvm_hstate.host_purr);mtspr(SPRN_SPURR,local_paca->kvm_hstate.host_spurr);
@@ -919,15 +926,21 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcmtspr(SPRN_PSSCR,host_psscr|(local_paca->kvm_hstate.fake_suspend<<PSSCR_FAKE_SUSPEND_LG));mtspr(SPRN_HFSCR,host_hfscr);-mtspr(SPRN_CIABR,host_ciabr);-mtspr(SPRN_DAWR0,host_dawr0);-mtspr(SPRN_DAWRX0,host_dawrx0);+if(vcpu->arch.ciabr!=host_ciabr)+mtspr(SPRN_CIABR,host_ciabr);+if(vcpu->arch.dawr0!=host_dawr0)+mtspr(SPRN_DAWR0,host_dawr0);+if(vcpu->arch.dawrx0!=host_dawrx0)+mtspr(SPRN_DAWRX0,host_dawrx0);if(cpu_has_feature(CPU_FTR_DAWR1)){-mtspr(SPRN_DAWR1,host_dawr1);-mtspr(SPRN_DAWRX1,host_dawrx1);+if(vcpu->arch.dawr1!=host_dawr1)+mtspr(SPRN_DAWR1,host_dawr1);+if(vcpu->arch.dawrx1!=host_dawrx1)+mtspr(SPRN_DAWRX1,host_dawrx1);}-mtspr(SPRN_DPDES,0);+if(vc->dpdes)+mtspr(SPRN_DPDES,0);if(vc->pcr)mtspr(SPRN_PCR,PCR_MASK);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:16:37
Use HFSCR facility disabling to implement demand faulting for EBB, with
a hysteresis counter similar to the load_fp etc counters in context
switching that implement the equivalent demand faulting for userspace
facilities.
This speeds up guest entry/exit by avoiding the register save/restore
when a guest is not frequently using them. When a guest does use them
often, there will be some additional demand fault overhead, but these
are not commonly used facilities.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/kvm_host.h | 1 +
arch/powerpc/kvm/book3s_hv.c | 16 +++++++++++++--
arch/powerpc/kvm/book3s_hv_p9_entry.c | 28 +++++++++++++++++++++------
3 files changed, 37 insertions(+), 8 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:17:16
Use HFSCR facility disabling to implement demand faulting for TM, with
a hysteresis counter similar to the load_fp etc counters in context
switching that implement the equivalent demand faulting for userspace
facilities.
This speeds up guest entry/exit by avoiding the register save/restore
when a guest is not frequently using them. When a guest does use them
often, there will be some additional demand fault overhead, but these
are not commonly used facilities.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/kvm_host.h | 3 +++
arch/powerpc/kvm/book3s_hv.c | 26 ++++++++++++++++++++------
arch/powerpc/kvm/book3s_hv_p9_entry.c | 15 +++++++++++----
3 files changed, 34 insertions(+), 10 deletions(-)
@@ -641,8 +647,9 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcmsr|=MSR_VEC;if(cpu_has_feature(CPU_FTR_VSX))msr|=MSR_VSX;-if(cpu_has_feature(CPU_FTR_TM)||-cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))+if((cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))&&+(vcpu->arch.hfscr&HFSCR_TM))msr|=MSR_TM;msr=msr_check_and_set(msr);/* Save MSR for restore. This is after hard disable, so EE is clear. */
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:18:03
Linux implements SPR save/restore including storage space for registers
in the task struct for process context switching. Make use of this
similarly to the way we make use of the context switching fp/vec save
restore.
This improves code reuse, allows some stack space to be saved, and helps
with avoiding VRSAVE updates if they are not required.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/switch_to.h | 1 +
arch/powerpc/kernel/process.c | 6 ++
arch/powerpc/kvm/book3s_hv.c | 21 +-----
arch/powerpc/kvm/book3s_hv.h | 3 -
arch/powerpc/kvm/book3s_hv_p9_entry.c | 93 +++++++++++++++++++--------
5 files changed, 73 insertions(+), 51 deletions(-)
@@ -4566,9 +4566,6 @@ static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)structkvm_run*run=vcpu->run;intr;intsrcu_idx;-unsignedlongebb_regs[3]={};/* shut up GCC */-unsignedlonguser_tar=0;-unsignedintuser_vrsave;structkvm*kvm;unsignedlongmsr;
@@ -4629,14 +4626,7 @@ static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)kvmppc_save_user_regs();-/* Save userspace EBB and other register values */-if(cpu_has_feature(CPU_FTR_ARCH_207S)){-ebb_regs[0]=mfspr(SPRN_EBBHR);-ebb_regs[1]=mfspr(SPRN_EBBRR);-ebb_regs[2]=mfspr(SPRN_BESCR);-user_tar=mfspr(SPRN_TAR);-}-user_vrsave=mfspr(SPRN_VRSAVE);+kvmppc_save_current_sprs();vcpu->arch.waitp=&vcpu->arch.vcore->wait;vcpu->arch.pgdir=kvm->mm->pgd;
@@ -4677,15 +4667,6 @@ static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)}}while(is_kvmppc_resume_guest(r));-/* Restore userspace EBB and other register values */-if(cpu_has_feature(CPU_FTR_ARCH_207S)){-mtspr(SPRN_EBBHR,ebb_regs[0]);-mtspr(SPRN_EBBRR,ebb_regs[1]);-mtspr(SPRN_BESCR,ebb_regs[2]);-mtspr(SPRN_TAR,user_tar);-}-mtspr(SPRN_VRSAVE,user_vrsave);-vcpu->arch.state=KVMPPC_VCPU_NOTREADY;atomic_dec(&kvm->arch.vcpus_running);
@@ -231,15 +231,26 @@ EXPORT_SYMBOL_GPL(switch_pmu_to_host);staticvoidload_spr_state(structkvm_vcpu*vcpu,structp9_host_os_sprs*host_os_sprs){+/* TAR is very fast */mtspr(SPRN_TAR,vcpu->arch.tar);+#ifdef CONFIG_ALTIVEC+if(cpu_has_feature(CPU_FTR_ALTIVEC)&&+current->thread.vrsave!=vcpu->arch.vrsave)+mtspr(SPRN_VRSAVE,vcpu->arch.vrsave);+#endif+if(vcpu->arch.hfscr&HFSCR_EBB){-mtspr(SPRN_EBBHR,vcpu->arch.ebbhr);-mtspr(SPRN_EBBRR,vcpu->arch.ebbrr);-mtspr(SPRN_BESCR,vcpu->arch.bescr);+if(current->thread.ebbhr!=vcpu->arch.ebbhr)+mtspr(SPRN_EBBHR,vcpu->arch.ebbhr);+if(current->thread.ebbrr!=vcpu->arch.ebbrr)+mtspr(SPRN_EBBRR,vcpu->arch.ebbrr);+if(current->thread.bescr!=vcpu->arch.bescr)+mtspr(SPRN_BESCR,vcpu->arch.bescr);}-if(cpu_has_feature(CPU_FTR_P9_TIDR))+if(cpu_has_feature(CPU_FTR_P9_TIDR)&&+current->thread.tidr!=vcpu->arch.tid)mtspr(SPRN_TIDR,vcpu->arch.tid);if(host_os_sprs->iamr!=vcpu->arch.iamr)mtspr(SPRN_IAMR,vcpu->arch.iamr);
@@ -377,26 +377,63 @@ EXPORT_SYMBOL_GPL(save_p9_host_os_sprs);voidrestore_p9_host_os_sprs(structkvm_vcpu*vcpu,structp9_host_os_sprs*host_os_sprs){+/*+*current->thread.xxxregistersmustallberestoredtohost+*valuesbeforeapotentialcontextswitch,othrewisethecontext+*switchitselfwilloverwritecurrent->thread.xxxwiththevalues+*fromtheguestSPRs.+*/+mtspr(SPRN_SPRG_VDSO_WRITE,local_paca->sprg_vdso);-if(cpu_has_feature(CPU_FTR_P9_TIDR))-mtspr(SPRN_TIDR,host_os_sprs->tidr);+if(cpu_has_feature(CPU_FTR_P9_TIDR)&&+current->thread.tidr!=vcpu->arch.tid)+mtspr(SPRN_TIDR,current->thread.tidr);if(host_os_sprs->iamr!=vcpu->arch.iamr)mtspr(SPRN_IAMR,host_os_sprs->iamr);if(vcpu->arch.uamor!=0)mtspr(SPRN_UAMOR,0);if(host_os_sprs->amr!=vcpu->arch.amr)mtspr(SPRN_AMR,host_os_sprs->amr);-if(host_os_sprs->fscr!=vcpu->arch.fscr)-mtspr(SPRN_FSCR,host_os_sprs->fscr);-if(host_os_sprs->dscr!=vcpu->arch.dscr)-mtspr(SPRN_DSCR,host_os_sprs->dscr);+if(current->thread.fscr!=vcpu->arch.fscr)+mtspr(SPRN_FSCR,current->thread.fscr);+if(current->thread.dscr!=vcpu->arch.dscr)+mtspr(SPRN_DSCR,current->thread.dscr);if(vcpu->arch.pspb!=0)mtspr(SPRN_PSPB,0);/* Save guest CTRL register, set runlatch to 1 */if(!(vcpu->arch.ctrl&1))mtspr(SPRN_CTRLT,1);++#ifdef CONFIG_ALTIVEC+if(cpu_has_feature(CPU_FTR_ALTIVEC)&&+vcpu->arch.vrsave!=current->thread.vrsave)+mtspr(SPRN_VRSAVE,current->thread.vrsave);+#endif+if(vcpu->arch.hfscr&HFSCR_EBB){+if(vcpu->arch.bescr!=current->thread.bescr)+mtspr(SPRN_BESCR,current->thread.bescr);+if(vcpu->arch.ebbhr!=current->thread.ebbhr)+mtspr(SPRN_EBBHR,current->thread.ebbhr);+if(vcpu->arch.ebbrr!=current->thread.ebbrr)+mtspr(SPRN_EBBRR,current->thread.ebbrr);++if(!vcpu->arch.nested){+/*+*Thisislikeload_fpincontextswitching,turnoff+*thefacilityafteritwrapstheu8totryavoiding+*savingandrestoringtheregisterseachpartition+*switch.+*/+vcpu->arch.load_ebb++;+if(!vcpu->arch.load_ebb)+vcpu->arch.hfscr&=~HFSCR_EBB;+}+}++if(vcpu->arch.tar!=current->thread.tar)+mtspr(SPRN_TAR,current->thread.tar);}EXPORT_SYMBOL_GPL(restore_p9_host_os_sprs);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:18:41
Tighten up partition switching code synchronisation and comments.
In particular, hwsync ; isync is required after the last access that is
performed in the context of a partition, before the partition is
switched away from.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_64_entry.S | 11 +++++--
arch/powerpc/kvm/book3s_64_mmu_radix.c | 4 +++
arch/powerpc/kvm/book3s_hv_p9_entry.c | 40 +++++++++++++++++++-------
3 files changed, 42 insertions(+), 13 deletions(-)
@@ -374,11 +374,16 @@ END_MMU_FTR_SECTION_IFCLR(MMU_FTR_TYPE_RADIX)BEGIN_FTR_SECTIONmtsprSPRN_DAWRX1,r10END_FTR_SECTION_IFSET(CPU_FTR_DAWR1)-mtsprSPRN_PID,r10/*-*SwitchtohostMMUmode+*SwitchtohostMMUmode (don't have the real host PID but we aren't+*goingbacktouserspace).*/+hwsync+isync++mtsprSPRN_PID,r10+ldr10,HSTATE_KVM_VCPU(r13)ldr10,VCPU_KVM(r10)lwzr10,KVM_HOST_LPID(r10)
@@ -389,6 +394,8 @@ END_FTR_SECTION_IFSET(CPU_FTR_DAWR1)ldr10,KVM_HOST_LPCR(r10)mtsprSPRN_LPCR,r10+isync+/**SetGUEST_MODE_NONEsothehandlerwon't branch to KVM, and clear*MSR_RIinr12 ([H]SRR1)sothehandlerwon't try to return.
@@ -57,6 +57,8 @@ unsigned long __kvmhv_copy_tofrom_guest_radix(int lpid, int pid,preempt_disable();+asmvolatile("hwsync":::"memory");+isync();/* switch the lpid first to avoid running host with unallocated pid */old_lpid=mfspr(SPRN_LPID);if(old_lpid!=lpid)
@@ -75,6 +77,8 @@ unsigned long __kvmhv_copy_tofrom_guest_radix(int lpid, int pid,ret=__copy_to_user_inatomic((void__user*)to,from,n);pagefault_enable();+asmvolatile("hwsync":::"memory");+isync();/* switch the pid first to avoid running host with unallocated pid */if(quadrant==1&&pid!=old_pid)mtspr(SPRN_PID,old_pid);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:19:19
Some of the DAWR SPR access is already predicated on dawr_enabled(),
apply this to the remainder of the accesses.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv_p9_entry.c | 34 ++++++++++++++++-----------
1 file changed, 20 insertions(+), 14 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:19:58
This also moves the PSSCR update in nested entry to avoid a SPR
scoreboard stall.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 7 +++++--
arch/powerpc/kvm/book3s_hv_p9_entry.c | 26 +++++++++++++++++++-------
2 files changed, 24 insertions(+), 9 deletions(-)
@@ -649,6 +649,7 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcunsignedlonghost_dawr0;unsignedlonghost_dawrx0;unsignedlonghost_psscr;+unsignedlonghost_hpsscr;unsignedlonghost_pidr;unsignedlonghost_dawr1;unsignedlonghost_dawrx1;
@@ -666,7 +667,9 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpchost_hfscr=mfspr(SPRN_HFSCR);host_ciabr=mfspr(SPRN_CIABR);-host_psscr=mfspr(SPRN_PSSCR);+host_psscr=mfspr(SPRN_PSSCR_PR);+if(cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))+host_hpsscr=mfspr(SPRN_PSSCR);host_pidr=mfspr(SPRN_PID);if(dawr_enabled()){
@@ -750,8 +753,14 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcif(vcpu->arch.ciabr!=host_ciabr)mtspr(SPRN_CIABR,vcpu->arch.ciabr);-mtspr(SPRN_PSSCR,vcpu->arch.psscr|PSSCR_EC|-(local_paca->kvm_hstate.fake_suspend<<PSSCR_FAKE_SUSPEND_LG));++if(cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)){+mtspr(SPRN_PSSCR,vcpu->arch.psscr|PSSCR_EC|+(local_paca->kvm_hstate.fake_suspend<<PSSCR_FAKE_SUSPEND_LG));+}else{+if(vcpu->arch.psscr!=host_psscr)+mtspr(SPRN_PSSCR_PR,vcpu->arch.psscr);+}mtspr(SPRN_HFSCR,vcpu->arch.hfscr);
@@ -957,7 +966,7 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvcpu->arch.ic=mfspr(SPRN_IC);vcpu->arch.pid=mfspr(SPRN_PID);-vcpu->arch.psscr=mfspr(SPRN_PSSCR)&PSSCR_GUEST_VIS;+vcpu->arch.psscr=mfspr(SPRN_PSSCR_PR);vcpu->arch.shregs.sprg0=mfspr(SPRN_SPRG0);vcpu->arch.shregs.sprg1=mfspr(SPRN_SPRG1);
@@ -1003,9 +1012,12 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcmtspr(SPRN_PURR,local_paca->kvm_hstate.host_purr);mtspr(SPRN_SPURR,local_paca->kvm_hstate.host_spurr);-/* Preserve PSSCR[FAKE_SUSPEND] until we've called kvmppc_save_tm_hv */-mtspr(SPRN_PSSCR,host_psscr|-(local_paca->kvm_hstate.fake_suspend<<PSSCR_FAKE_SUSPEND_LG));+if(cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)){+/* Preserve PSSCR[FAKE_SUSPEND] until we've called kvmppc_save_tm_hv */+mtspr(SPRN_PSSCR,host_hpsscr|+(local_paca->kvm_hstate.fake_suspend<<PSSCR_FAKE_SUSPEND_LG));+}+mtspr(SPRN_HFSCR,host_hfscr);if(vcpu->arch.ciabr!=host_ciabr)mtspr(SPRN_CIABR,host_ciabr);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:20:42
This creates separate functions for old and new paths for vCPU TLB
flushing, which will reduce complexity of the next change.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/kvm_ppc.h | 3 +-
arch/powerpc/kvm/book3s_hv_builtin.c | 53 ++++-------------------
arch/powerpc/kvm/book3s_hv_p9_entry.c | 62 ++++++++++++++++++++++++++-
3 files changed, 70 insertions(+), 48 deletions(-)
@@ -682,60 +682,23 @@ static void flush_guest_tlb(struct kvm *kvm)unsignedlongrb,set;rb=PPC_BIT(52);/* IS = 2 */-if(kvm_is_radix(kvm)){-/* R=1 PRS=1 RIC=2 */+for(set=0;set<kvm->arch.tlb_sets;++set){+/* R=0 PRS=0 RIC=0 */asmvolatile(PPC_TLBIEL(%0,%4,%3,%2,%1)-::"r"(rb),"i"(1),"i"(1),"i"(2),+::"r"(rb),"i"(0),"i"(0),"i"(0),"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 */-asmvolatile(PPC_TLBIEL(%0,%4,%3,%2,%1)-::"r"(rb),"i"(1),"i"(1),"i"(0),-"r"(0):"memory");-}-asmvolatile("ptesync":::"memory");-// POWER9 congruence-class TLBIEL leaves ERAT. Flush it now.-asmvolatile(PPC_RADIX_INVALIDATE_ERAT_GUEST:::"memory");-}else{-for(set=0;set<kvm->arch.tlb_sets;++set){-/* R=0 PRS=0 RIC=0 */-asmvolatile(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 */-}-asmvolatile("ptesync":::"memory");-// POWER9 congruence-class TLBIEL leaves ERAT. Flush it now.-if(cpu_has_feature(CPU_FTR_ARCH_300))-asmvolatile(PPC_ISA_3_0_INVALIDATE_ERAT:::"memory");+rb+=PPC_BIT(51);/* increment set number */}+asmvolatile("ptesync":::"memory");}-voidkvmppc_check_need_tlb_flush(structkvm*kvm,intpcpu,-structkvm_nested_guest*nested)+voidkvmppc_check_need_tlb_flush(structkvm*kvm,intpcpu){-cpumask_t*need_tlb_flush;--/*-*OnPOWER9,individualthreadscancomeinhere,butthe-*TLBissharedbetweenthe4threadsinacore,hence-*invalidatingononethreadinvalidatesforall.-*Thuswemakeall4threadsusethesamebit.-*/-if(cpu_has_feature(CPU_FTR_ARCH_300))-pcpu=cpu_first_tlb_thread_sibling(pcpu);--if(nested)-need_tlb_flush=&nested->need_tlb_flush;-else-need_tlb_flush=&kvm->arch.need_tlb_flush;--if(cpumask_test_cpu(pcpu,need_tlb_flush)){+if(cpumask_test_cpu(pcpu,&kvm->arch.need_tlb_flush)){flush_guest_tlb(kvm);/* Clear the bit after the TLB flush */-cpumask_clear_cpu(pcpu,need_tlb_flush);+cpumask_clear_cpu(pcpu,&kvm->arch.need_tlb_flush);}}EXPORT_SYMBOL_GPL(kvmppc_check_need_tlb_flush);
@@ -632,6 +632,66 @@ static void save_clear_guest_mmu(struct kvm *kvm, struct kvm_vcpu *vcpu)}}+staticvoidflush_guest_tlb(structkvm*kvm)+{+unsignedlongrb,set;++rb=PPC_BIT(52);/* IS = 2 */+if(kvm_is_radix(kvm)){+/* R=1 PRS=1 RIC=2 */+asmvolatile(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 */+asmvolatile(PPC_TLBIEL(%0,%4,%3,%2,%1)+::"r"(rb),"i"(1),"i"(1),"i"(0),+"r"(0):"memory");+}+asmvolatile("ptesync":::"memory");+// POWER9 congruence-class TLBIEL leaves ERAT. Flush it now.+asmvolatile(PPC_RADIX_INVALIDATE_ERAT_GUEST:::"memory");+}else{+for(set=0;set<kvm->arch.tlb_sets;++set){+/* R=0 PRS=0 RIC=0 */+asmvolatile(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 */+}+asmvolatile("ptesync":::"memory");+// POWER9 congruence-class TLBIEL leaves ERAT. Flush it now.+asmvolatile(PPC_ISA_3_0_INVALIDATE_ERAT:::"memory");+}+}++staticvoidcheck_need_tlb_flush(structkvm*kvm,intpcpu,+structkvm_nested_guest*nested)+{+cpumask_t*need_tlb_flush;++/*+*OnPOWER9,individualthreadscancomeinhere,butthe+*TLBissharedbetweenthe4threadsinacore,hence+*invalidatingononethreadinvalidatesforall.+*Thuswemakeall4threadsusethesamebit.+*/+pcpu=cpu_first_tlb_thread_sibling(pcpu);++if(nested)+need_tlb_flush=&nested->need_tlb_flush;+else+need_tlb_flush=&kvm->arch.need_tlb_flush;++if(cpumask_test_cpu(pcpu,need_tlb_flush)){+flush_guest_tlb(kvm);++/* Clear the bit after the TLB flush */+cpumask_clear_cpu(pcpu,need_tlb_flush);+}+}+intkvmhv_vcpu_entry_p9(structkvm_vcpu*vcpu,u64time_limit,unsignedlonglpcr,u64*tb){structp9_host_os_sprshost_os_sprs;
@@ -819,7 +879,7 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpc}/* TLBIEL uses LPID=LPIDR, so run this after setting guest LPID */-kvmppc_check_need_tlb_flush(kvm,vc->pcpu,nested);+check_need_tlb_flush(kvm,vc->pcpu,nested);/**P9suppressestheHDECexceptionwhenLPCR[HDICE]=0,
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:21:21
Use the existing TLB flushing logic to IPI the previous CPU and run the
necessary barriers before running a guest vCPU on a new physical CPU,
to do the necessary radix GTSE barriers for handling the case of an
interrupted guest tlbie sequence.
This requires the vCPU TLB flush sequence that is currently just done
on one thread, to be expanded to ensure the other threads execute a
ptesync, because causing them to exit the guest will no longer cause a
ptesync by itself.
This results in more IPIs than the TLB flush logic requires, but it's
a significant win for common case scheduling when the vCPU remains on
the same physical CPU.
This saves about 520 cycles (nearly 10%) on a guest entry+exit micro
benchmark on a POWER9.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 48 +++++++++++++++++++++------
arch/powerpc/kvm/book3s_hv_p9_entry.c | 48 +++++++++++++++------------
arch/powerpc/kvm/book3s_hv_rm_mmu.c | 6 ----
3 files changed, 65 insertions(+), 37 deletions(-)
@@ -670,26 +670,41 @@ static void check_need_tlb_flush(struct kvm *kvm, int pcpu,structkvm_nested_guest*nested){cpumask_t*need_tlb_flush;--/*-*OnPOWER9,individualthreadscancomeinhere,butthe-*TLBissharedbetweenthe4threadsinacore,hence-*invalidatingononethreadinvalidatesforall.-*Thuswemakeall4threadsusethesamebit.-*/-pcpu=cpu_first_tlb_thread_sibling(pcpu);+boolall_set=true;+inti;if(nested)need_tlb_flush=&nested->need_tlb_flush;elseneed_tlb_flush=&kvm->arch.need_tlb_flush;-if(cpumask_test_cpu(pcpu,need_tlb_flush)){-flush_guest_tlb(kvm);+if(likely(!cpumask_test_cpu(pcpu,need_tlb_flush)))+return;-/* Clear the bit after the TLB flush */-cpumask_clear_cpu(pcpu,need_tlb_flush);+/*+*Individualthreadscancomeinhere,buttheTLBissharedbetween+*the4threadsinacore,henceinvalidatingononethread+*invalidatesforall,soonlyinvalidatethefirsttime(ifallbits+*wereset.Theothersmuststillexecuteaptesync.+*+*IfaraceoccursandtwothreadsdotheTLBflush,thatisnota+*problem,justsub-optimal.+*/+for(i=cpu_first_tlb_thread_sibling(pcpu);+i<=cpu_last_tlb_thread_sibling(pcpu);+i+=cpu_tlb_thread_sibling_step()){+if(!cpumask_test_cpu(i,need_tlb_flush)){+all_set=false;+break;+}}+if(all_set)+flush_guest_tlb(kvm);+else+asmvolatile("ptesync":::"memory");++/* Clear the bit after the TLB flush */+cpumask_clear_cpu(pcpu,need_tlb_flush);}intkvmhv_vcpu_entry_p9(structkvm_vcpu*vcpu,u64time_limit,unsignedlonglpcr,u64*tb)
@@ -1109,15 +1124,6 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpclocal_paca->kvm_hstate.in_guest=KVM_GUEST_MODE_NONE;-if(kvm_is_radix(kvm)){-/*-*Sincethisisradix,doaeieio;tlbsync;ptesyncsequence-*incaseweinterruptedtheguestbetweenatlbieanda-*ptesync.-*/-asmvolatile("eieio; tlbsync; ptesync");-}-/**cp_abortisrequirediftheprocessorsupportslocalcopy-paste*toclearthecopybufferthatwasundercontroloftheguest.
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:22:00
mftb() is expensive and one can be avoided on nested guest dispatch.
If the time checking code distinguishes between the L0 timer and the
nested HV timer, then both can be tested in the same place with the
same mftb() value.
This also nicely illustrates the relationship between the L0 and nested
HV timers.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/kvm_asm.h | 1 +
arch/powerpc/kvm/book3s_hv.c | 12 ++++++++++++
arch/powerpc/kvm/book3s_hv_nested.c | 5 -----
3 files changed, 13 insertions(+), 5 deletions(-)
@@ -1486,6 +1486,10 @@ static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,run->ready_for_interrupt_injection=1;switch(vcpu->arch.trap){/* We're good on these - the host merely wanted to get our attention */+caseBOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:+WARN_ON_ONCE(1);/* Should never happen */+vcpu->arch.trap=BOOK3S_INTERRUPT_HV_DECREMENTER;+fallthrough;caseBOOK3S_INTERRUPT_HV_DECREMENTER:vcpu->stat.dec_exits++;r=RESUME_GUEST;
@@ -1814,6 +1818,12 @@ static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)vcpu->stat.ext_intr_exits++;r=RESUME_GUEST;break;+/* These need to go to the nested HV */+caseBOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:+vcpu->arch.trap=BOOK3S_INTERRUPT_HV_DECREMENTER;+vcpu->stat.dec_exits++;+r=RESUME_HOST;+break;/* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/caseBOOK3S_INTERRUPT_HMI:caseBOOK3S_INTERRUPT_PERFMON:
@@ -3993,6 +4003,8 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,returnBOOK3S_INTERRUPT_HV_DECREMENTER;if(next_timer<time_limit)time_limit=next_timer;+elseif(*tb>=time_limit)/* nested time limit */+returnBOOK3S_INTERRUPT_NESTED_HV_DECREMENTER;vcpu->arch.ceded=0;
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:22:39
Rearrange the MSR saving on entry so it does not follow the mtmsrd to
disable interrupts, avoiding a possible RAW scoreboard stall.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/kvm_book3s_64.h | 2 +
arch/powerpc/kvm/book3s_hv.c | 18 ++-----
arch/powerpc/kvm/book3s_hv_p9_entry.c | 66 +++++++++++++++---------
3 files changed, 47 insertions(+), 39 deletions(-)
@@ -3906,24 +3908,10 @@ static int kvmhv_vcpu_entry_p9_nested(struct kvm_vcpu *vcpu, u64 time_limit, uns*/host_psscr=mfspr(SPRN_PSSCR_PR);-hard_irq_disable();+kvmppc_msr_hard_disable_set_facilities(vcpu,msr);if(lazy_irq_pending())return0;-/* MSR bits may have been cleared by context switch */-msr=0;-if(IS_ENABLED(CONFIG_PPC_FPU))-msr|=MSR_FP;-if(cpu_has_feature(CPU_FTR_ALTIVEC))-msr|=MSR_VEC;-if(cpu_has_feature(CPU_FTR_VSX))-msr|=MSR_VSX;-if((cpu_has_feature(CPU_FTR_TM)||-cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))&&-(vcpu->arch.hfscr&HFSCR_TM))-msr|=MSR_TM;-msr=msr_check_and_set(msr);-if(unlikely(load_vcpu_state(vcpu,&host_os_sprs)))msr=mfmsr();/* TM restore can update msr */
@@ -707,6 +707,44 @@ static void check_need_tlb_flush(struct kvm *kvm, int pcpu,cpumask_clear_cpu(pcpu,need_tlb_flush);}+unsignedlongkvmppc_msr_hard_disable_set_facilities(structkvm_vcpu*vcpu,unsignedlongmsr)+{+unsignedlongmsr_needed=0;++msr&=~MSR_EE;++/* MSR bits may have been cleared by context switch so must recheck */+if(IS_ENABLED(CONFIG_PPC_FPU))+msr_needed|=MSR_FP;+if(cpu_has_feature(CPU_FTR_ALTIVEC))+msr_needed|=MSR_VEC;+if(cpu_has_feature(CPU_FTR_VSX))+msr_needed|=MSR_VSX;+if((cpu_has_feature(CPU_FTR_TM)||+cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))&&+(vcpu->arch.hfscr&HFSCR_TM))+msr_needed|=MSR_TM;++/*+*ThiscouldbecombinedwithMSR[RI]clearing,butthatexpands+*theunrecoverablewindow.Itwouldbebettertocoverunrecoverable+*withKVMbadinterrupthandlingratherthanuseMSR[RI]atall.+*+*MuchmoredifficultandlessworthwhiletocombinewithIR/DR+*disable.+*/+if((msr&msr_needed)!=msr_needed){+msr|=msr_needed;+__mtmsrd(msr,0);+}else{+__hard_irq_disable();+}+local_paca->irq_happened|=PACA_IRQ_HARD_DIS;++returnmsr;+}+EXPORT_SYMBOL_GPL(kvmppc_msr_hard_disable_set_facilities);+intkvmhv_vcpu_entry_p9(structkvm_vcpu*vcpu,u64time_limit,unsignedlonglpcr,u64*tb){structp9_host_os_sprshost_os_sprs;
@@ -740,6 +778,9 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvcpu->arch.ceded=0;+/* Save MSR for restore, with EE clear. */+msr=mfmsr()&~MSR_EE;+host_hfscr=mfspr(SPRN_HFSCR);host_ciabr=mfspr(SPRN_CIABR);host_psscr=mfspr(SPRN_PSSCR_PR);
@@ -761,35 +802,12 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcsave_p9_host_os_sprs(&host_os_sprs);-/*-*ThiscouldbecombinedwithMSR[RI]clearing,butthatexpands-*theunrecoverablewindow.Itwouldbebettertocoverunrecoverable-*withKVMbadinterrupthandlingratherthanuseMSR[RI]atall.-*-*MuchmoredifficultandlessworthwhiletocombinewithIR/DR-*disable.-*/-hard_irq_disable();+msr=kvmppc_msr_hard_disable_set_facilities(vcpu,msr);if(lazy_irq_pending()){trap=0;gotoout;}-/* MSR bits may have been cleared by context switch */-msr=0;-if(IS_ENABLED(CONFIG_PPC_FPU))-msr|=MSR_FP;-if(cpu_has_feature(CPU_FTR_ALTIVEC))-msr|=MSR_VEC;-if(cpu_has_feature(CPU_FTR_VSX))-msr|=MSR_VSX;-if((cpu_has_feature(CPU_FTR_TM)||-cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))&&-(vcpu->arch.hfscr&HFSCR_TM))-msr|=MSR_TM;-msr=msr_check_and_set(msr);-/* Save MSR for restore. This is after hard disable, so EE is clear. */-if(unlikely(load_vcpu_state(vcpu,&host_os_sprs)))msr=mfmsr();/* MSR may have been updated */
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:23:25
slbmfee/slbmfev instructions are very expensive, moreso than a regular
mfspr instruction, so minimising them significantly improves hash guest
exit performance. The slbmfev is only required if slbmfee found a valid
SLB entry.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv_p9_entry.c | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:24:04
kvm_hstate.in_guest provides the equivalent of MSR[RI]=0 protection,
and it covers the existing MSR[RI]=0 section in late entry and early
exit, so clearing and setting MSR[RI] in those cases does not
actually do anything useful.
Remove the RI manipulation and replace it with comments. Make the
in_guest memory accesses a bit closer to a proper critical section
pattern. This speeds up guest entry/exit performance.
This also removes the MSR[RI] warnings which aren't very interesting
and would cause crashes if they hit due to causing an interrupt in
non-recoverable code.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv_p9_entry.c | 50 ++++++++++++---------------
1 file changed, 23 insertions(+), 27 deletions(-)
@@ -904,7 +904,15 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpc*ButTMcouldbesplitoutifthiswouldbeasignificantbenefit.*/-local_paca->kvm_hstate.in_guest=KVM_GUEST_MODE_HV_P9;+/*+*MSR[RI]doesnotneedtobecleared(andisnot,forradixguests+*withnoprefetchbug),becausein_guestisset.IfwetakeaSRESET+*orMCEwithin_guestsetbutstillinHVmode,then+*kvmppc_p9_bad_interrupthandlestheinterrupt,whicheffectively+*clearsMSR[RI]anddoesn'treturn.+*/+WRITE_ONCE(local_paca->kvm_hstate.in_guest,KVM_GUEST_MODE_HV_P9);+barrier();/* Open in_guest critical section *//**Hashhost,hashguest,orradixguestwithprefetchbug,allhave
@@ -916,14 +924,10 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcsave_clear_host_mmu(kvm);-if(kvm_is_radix(kvm)){+if(kvm_is_radix(kvm))switch_mmu_to_guest_radix(kvm,vcpu,lpcr);-if(!cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG))-__mtmsrd(0,1);/* clear RI */--}else{+elseswitch_mmu_to_guest_hpt(kvm,vcpu,lpcr);-}/* TLBIEL uses LPID=LPIDR, so run this after setting guest LPID */check_need_tlb_flush(kvm,vc->pcpu,nested);
@@ -978,19 +982,16 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcvcpu->arch.regs.gpr[3]=local_paca->kvm_hstate.scratch2;/*-*OnlysetRIafterreadingmachinecheckregs(DAR,DSISR,SRR0/1)-*andhstatescratch(whichweneedtomoveintoexsavetomake-*re-entrantvsSRESET/MCE)+*Afterreadingmachinecheckregs(DAR,DSISR,SRR0/1)andhstate+*scratch(whichweneedtomoveintoexsavetomakere-entrantvs+*SRESET/MCE),registerstateisprotectedfromreentrancy.However+*timebase,MMU,amongotherstateisstillsettoguest,sodon't+*enableMSR[RI]here.Itgetsenabledattheend,afterin_guest+*iscleared.+*+*ItispossibleanNMIcouldcomeinhere,whichiswhyitis+*importanttosavetheabovestateearlysoitcanbedebugged.*/-if(ri_set){-if(unlikely(!(mfmsr()&MSR_RI))){-__mtmsrd(MSR_RI,1);-WARN_ON_ONCE(1);-}-}else{-WARN_ON_ONCE(mfmsr()&MSR_RI);-__mtmsrd(MSR_RI,1);-}vcpu->arch.regs.gpr[9]=exsave[EX_R9/sizeof(u64)];vcpu->arch.regs.gpr[10]=exsave[EX_R10/sizeof(u64)];
@@ -1048,13 +1049,6 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpc*/mtspr(SPRN_HSRR0,vcpu->arch.regs.nip);mtspr(SPRN_HSRR1,vcpu->arch.shregs.msr);--/*-*tm_return_to_guestre-loadsSRR0/1,DAR,-*DSISRafterRIiscleared,incasetheyhad-*beenclobberedbyaMCE.-*/-__mtmsrd(0,1);/* clear RI */gototm_return_to_guest;}}
@@ -1154,7 +1148,9 @@ int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcrestore_p9_host_os_sprs(vcpu,&host_os_sprs);-local_paca->kvm_hstate.in_guest=KVM_GUEST_MODE_NONE;+barrier();/* Close in_guest critical section */+WRITE_ONCE(local_paca->kvm_hstate.in_guest,KVM_GUEST_MODE_NONE);+/* Interrupts are recoverable at this point *//**cp_abortisrequirediftheprocessorsupportslocalcopy-paste
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:24:42
The mmu will almost always be ready.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
@@ -4426,7 +4426,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,vc->runner=vcpu;/* See if the MMU is ready to go */-if(!kvm->arch.mmu_ready){+if(unlikely(!kvm->arch.mmu_ready)){r=kvmhv_setup_mmu(vcpu);if(r){run->exit_reason=KVM_EXIT_FAIL_ENTRY;
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:25:20
cpu_in_guest is set to determine if a CPU needs to be IPI'ed to exit
the guest and notice the need_tlb_flush bit.
This can be implemented as a global per-CPU pointer to the currently
running guest instead of per-guest cpumasks, saving 2 atomics per
entry/exit. P7/8 doesn't require cpu_in_guest, nor does a nested HV
(only the L0 does), so move it to the P9 HV path.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/kvm_book3s_64.h | 1 -
arch/powerpc/include/asm/kvm_host.h | 1 -
arch/powerpc/kvm/book3s_hv.c | 39 +++++++++++++-----------
3 files changed, 22 insertions(+), 19 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:25:58
The P9 path always uses one vcpu per vcore, so none of the vcore, locks,
stolen time, blocking logic, shared waitq, etc., is required.
Remove most of it.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 147 ++++++++++++++++++++---------------
1 file changed, 85 insertions(+), 62 deletions(-)
@@ -898,13 +920,14 @@ static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)*modehandlerisnotcalledbutnootherthreadsareinthe*sourcevcore.*/--spin_lock(&vcore->lock);-if(target->arch.state==KVMPPC_VCPU_RUNNABLE&&-vcore->vcore_state!=VCORE_INACTIVE&&-vcore->runner)-target=vcore->runner;-spin_unlock(&vcore->lock);+if(!cpu_has_feature(CPU_FTR_ARCH_300)){+spin_lock(&vcore->lock);+if(target->arch.state==KVMPPC_VCPU_RUNNABLE&&+vcore->vcore_state!=VCORE_INACTIVE&&+vcore->runner)+target=vcore->runner;+spin_unlock(&vcore->lock);+}returnkvm_vcpu_yield_to(target);}
@@ -3131,13 +3154,6 @@ static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)kvmppc_ipi_thread(cpu);}-/* Old path does this in asm */-staticvoidkvmppc_stop_thread(structkvm_vcpu*vcpu)-{-vcpu->cpu=-1;-vcpu->arch.thread_cpu=-1;-}-staticvoidkvmppc_wait_for_nap(intn_threads){intcpu=smp_processor_id();
@@ -4132,7 +4152,7 @@ static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)inti;for_each_runnable_thread(i,vcpu,vc){-if(!vcpu->arch.ceded||kvmppc_vcpu_woken(vcpu))+if(kvmppc_vcpu_check_block(vcpu))return1;}
@@ -4149,6 +4169,8 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)intdo_sleep=1;u64block_ns;+WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));+/* Poll for pending exceptions and ceded state */cur=start_poll=ktime_get();if(vc->halt_poll_ns){
@@ -4426,11 +4448,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,vcpu->arch.ceded=0;vcpu->arch.run_task=current;vcpu->arch.state=KVMPPC_VCPU_RUNNABLE;-vcpu->arch.busy_preempt=TB_NIL;vcpu->arch.last_inst=KVM_INST_FETCH_FAILED;-vc->runnable_threads[0]=vcpu;-vc->n_runnable=1;-vc->runner=vcpu;/* See if the MMU is ready to go */if(unlikely(!kvm->arch.mmu_ready)){
@@ -4448,11 +4466,8 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,kvmppc_update_vpas(vcpu);-init_vcore_to_run(vc);-preempt_disable();pcpu=smp_processor_id();-vc->pcpu=pcpu;if(kvm_is_radix(kvm))kvmppc_prepare_radix_vcpu(vcpu,pcpu);
@@ -4481,21 +4496,23 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,gotoout;}-tb=mftb();+if(vcpu->arch.timer_running){+hrtimer_try_to_cancel(&vcpu->arch.dec_timer);+vcpu->arch.timer_running=0;+}-vcpu->arch.stolen_logged=vcore_stolen_time(vc,tb);-vc->preempt_tb=TB_NIL;+tb=mftb();-kvmppc_clear_host_core(pcpu);+vcpu->cpu=pcpu;+vcpu->arch.thread_cpu=pcpu;+vc->pcpu=pcpu;+local_paca->kvm_hstate.kvm_vcpu=vcpu;+local_paca->kvm_hstate.ptid=0;+local_paca->kvm_hstate.fake_suspend=0;-local_paca->kvm_hstate.napping=0;-local_paca->kvm_hstate.kvm_split_mode=NULL;-kvmppc_start_thread(vcpu,vc);kvmppc_create_dtl_entry(vcpu,vc,tb);-trace_kvm_guest_enter(vcpu);-vc->vcore_state=VCORE_RUNNING;-trace_kvmppc_run_core(vc,0);+trace_kvm_guest_enter(vcpu);guest_enter_irqoff();
@@ -4517,8 +4534,6 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,set_irq_happened(trap);-kvmppc_set_host_core(pcpu);-context_tracking_guest_exit();if(!vtime_accounting_enabled_this_cpu()){local_irq_enable();
@@ -4534,7 +4549,8 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,}vtime_account_guest_exit();-kvmppc_stop_thread(vcpu);+vcpu->cpu=-1;+vcpu->arch.thread_cpu=-1;powerpc_local_irq_pmu_restore(flags);
@@ -4561,28 +4577,31 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,}vcpu->arch.ret=r;-if(is_kvmppc_resume_guest(r)&&vcpu->arch.ceded&&-!kvmppc_vcpu_woken(vcpu)){+if(is_kvmppc_resume_guest(r)&&!kvmppc_vcpu_check_block(vcpu)){kvmppc_set_timer(vcpu);-while(vcpu->arch.ceded&&!kvmppc_vcpu_woken(vcpu)){++prepare_to_rcuwait(&vcpu->wait);+for(;;){+set_current_state(TASK_INTERRUPTIBLE);if(signal_pending(current)){vcpu->stat.signal_exits++;run->exit_reason=KVM_EXIT_INTR;vcpu->arch.ret=-EINTR;break;}-spin_lock(&vc->lock);-kvmppc_vcore_blocked(vc);-spin_unlock(&vc->lock);++if(kvmppc_vcpu_check_block(vcpu))+break;++trace_kvmppc_vcore_blocked(vc,0);+schedule();+trace_kvmppc_vcore_blocked(vc,1);}+finish_rcuwait(&vcpu->wait);}vcpu->arch.ceded=0;-vc->vcore_state=VCORE_INACTIVE;-trace_kvmppc_run_core(vc,1);-done:-kvmppc_remove_runnable(vc,vcpu,tb);trace_kvmppc_run_vcpu_exit(vcpu);returnvcpu->arch.ret;
@@ -4664,7 +4683,8 @@ static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)kvmppc_save_current_sprs();-vcpu->arch.waitp=&vcpu->arch.vcore->wait;+if(!cpu_has_feature(CPU_FTR_ARCH_300))+vcpu->arch.waitp=&vcpu->arch.vcore->wait;vcpu->arch.pgdir=kvm->mm->pgd;vcpu->arch.state=KVMPPC_VCPU_BUSY_IN_HOST;
@@ -5126,6 +5146,9 @@ void kvmppc_alloc_host_rm_ops(void)intcpu,core;intsize;+if(cpu_has_feature(CPU_FTR_ARCH_300))+return;+/* Not the first time here ? */if(kvmppc_host_rm_ops_hv!=NULL)return;
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:26:45
This goes further to removing vcores from the P9 path. Also avoid the
memset in favour of explicitly initialising all fields.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 60 +++++++++++++++++++++---------------
1 file changed, 35 insertions(+), 25 deletions(-)
@@ -743,6 +732,27 @@ static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,vcpu->arch.dtl.dirty=true;}+staticvoidkvmppc_create_dtl_entry(structkvm_vcpu*vcpu,+structkvmppc_vcore*vc)+{+unsignedlongstolen;+unsignedlongcore_stolen;+u64now;+unsignedlongflags;++now=mftb();++core_stolen=vcore_stolen_time(vc,now);+stolen=core_stolen-vcpu->arch.stolen_logged;+vcpu->arch.stolen_logged=core_stolen;+spin_lock_irqsave(&vcpu->arch.tbacct_lock,flags);+stolen+=vcpu->arch.busy_stolen;+vcpu->arch.busy_stolen=0;+spin_unlock_irqrestore(&vcpu->arch.tbacct_lock,flags);++__kvmppc_create_dtl_entry(vcpu,vc->pcpu,now+vc->tb_offset,stolen);+}+/* See if there is a doorbell interrupt pending for a vcpu */staticboolkvmppc_doorbell_pending(structkvm_vcpu*vcpu){
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:27:23
The P9 path uses vc->dpdes only for msgsndp / SMT emulation. This adds
an ordering requirement between vcpu->doorbell_request and vc->dpdes for
no real benefit. Use vcpu->doorbell_request directly.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 18 ++++++++++--------
arch/powerpc/kvm/book3s_hv_builtin.c | 2 ++
arch/powerpc/kvm/book3s_hv_p9_entry.c | 14 ++++++++++----
3 files changed, 22 insertions(+), 12 deletions(-)
@@ -649,6 +649,8 @@ void kvmppc_guest_entry_inject_int(struct kvm_vcpu *vcpu)intext;unsignedlonglpcr;+WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));+/* Insert EXTERNAL bit into LPCR at the MER bit position */ext=(vcpu->arch.pending_exceptions>>BOOK3S_IRQPRIO_EXTERNAL)&1;lpcr=mfspr(SPRN_LPCR);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-11-23 10:28:01
On POWER9 and newer, rather than the complex HMI synchronisation and
subcore state, have each thread un-apply the guest TB offset before
calling into the early HMI handler.
This allows the subcore state to be avoided, including subcore enter
/ exit guest, which includes an expensive divide that shows up
slightly in profiles.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/kvm_ppc.h | 1 +
arch/powerpc/kvm/book3s_hv.c | 12 +++---
arch/powerpc/kvm/book3s_hv_hmi.c | 7 +++-
arch/powerpc/kvm/book3s_hv_p9_entry.c | 2 +-
arch/powerpc/kvm/book3s_hv_ras.c | 54 +++++++++++++++++++++++++++
5 files changed, 67 insertions(+), 9 deletions(-)
@@ -136,6 +136,60 @@ void kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu)vcpu->arch.mce_evt=mce_evt;}++longkvmppc_p9_realmode_hmi_handler(structkvm_vcpu*vcpu)+{+structkvmppc_vcore*vc=vcpu->arch.vcore;+longret=0;++/*+*Unapplyandcleartheoffsetfirst.Thatway,iftheTBwasnot+*resyncedthenitwillremaininhost-offset,andifitwasresynced+*thenitisbroughtintohost-offset.Thenthetboffsetis+*re-appliedbeforecontinuingwiththeKVMexit.+*+*Thisway,wedon'tneedtoactuallyknowwhethernotOPALresynced+*thetimebaseordoanyofthecomplicateddancethattheP7/8+*pathrequires.+*/+if(vc->tb_offset_applied){+u64new_tb=mftb()-vc->tb_offset_applied;+mtspr(SPRN_TBU40,new_tb);+if((mftb()&0xffffff)<(new_tb&0xffffff)){+new_tb+=0x1000000;+mtspr(SPRN_TBU40,new_tb);+}+vc->tb_offset_applied=0;+}++local_paca->hmi_irqs++;++if(hmi_handle_debugtrig(NULL)>=0){+ret=1;+gotoout;+}++if(ppc_md.hmi_exception_early)+ppc_md.hmi_exception_early(NULL);++out:+if(vc->tb_offset){+u64new_tb=mftb()+vc->tb_offset;+mtspr(SPRN_TBU40,new_tb);+if((mftb()&0xffffff)<(new_tb&0xffffff)){+new_tb+=0x1000000;+mtspr(SPRN_TBU40,new_tb);+}+vc->tb_offset_applied=vc->tb_offset;+}++returnret;+}++/*+*ThefollowingsubcoreHMIhandlingisallonlyforpre-POWER9CPUs.+*/+/* Check if dynamic split is in force and return subcore size accordingly. */staticinlineintkvmppc_cur_subcore_size(void){
From: Michael Ellerman <hidden> Date: 2021-11-25 09:52:18
On Tue, 23 Nov 2021 19:51:38 +1000, Nicholas Piggin wrote:
This reduces radix guest full entry/exit latency on POWER9 and POWER10
by 2x.
Nested HV guests should see smaller improvements in their L1 entry/exit,
but this is also combined with most L0 speedups also applying to nested
entry. nginx localhost throughput test in a SMP nested guest is improved
about 10% (in a direct guest it doesn't change much because it uses XIVE
for IPIs) when L0 and L1 are patched.
[...]