From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:02:17
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 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
Fabiano Rosas (3):
KVM: PPC: Book3S HV Nested: Sanitise vcpu registers
KVM: PPC: Book3S HV Nested: Stop forwarding all HFUs to L1
KVM: PPC: Book3S HV Nested: save_hv_return_state does not require trap
argument
Nicholas Piggin (57):
KVM: PPC: Book3S HV: Initialise vcpu MSR with MSR_ME
KVM: PPC: Book3S HV: Remove TM emulation from POWER7/8 path
KVM: PPC: Book3S HV P9: Fixes for TM softpatch interrupt NIP
KVM: PPC: Book3S HV Nested: Fix TM softpatch HFAC interrupt emulation
KVM: PPC: Book3S HV Nested: Make nested HFSCR state accessible
KVM: PPC: Book3S HV Nested: Reflect guest PMU in-use to L0 when guest
SPRs are live
powerpc/64s: Remove WORT SPR from POWER9/10
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 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 | 6 +-
arch/powerpc/include/asm/kvm_host.h | 7 +-
arch/powerpc/include/asm/kvm_ppc.h | 1 +
arch/powerpc/include/asm/pmc.h | 7 +
arch/powerpc/include/asm/reg.h | 3 +-
arch/powerpc/include/asm/switch_to.h | 2 +
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 | 32 +
arch/powerpc/kernel/time.c | 54 +-
arch/powerpc/kvm/Kconfig | 15 +
arch/powerpc/kvm/book3s_64_mmu_radix.c | 4 +
arch/powerpc/kvm/book3s_hv.c | 890 ++++++++++--------
arch/powerpc/kvm/book3s_hv.h | 41 +
arch/powerpc/kvm/book3s_hv_builtin.c | 2 +
arch/powerpc/kvm/book3s_hv_hmi.c | 7 +-
arch/powerpc/kvm/book3s_hv_interrupts.S | 13 +-
arch/powerpc/kvm/book3s_hv_nested.c | 109 ++-
arch/powerpc/kvm/book3s_hv_p9_entry.c | 817 +++++++++++++---
arch/powerpc/kvm/book3s_hv_ras.c | 54 ++
arch/powerpc/kvm/book3s_hv_rmhandlers.S | 115 +--
arch/powerpc/kvm/book3s_hv_tm.c | 61 +-
arch/powerpc/mm/book3s64/radix_pgtable.c | 15 -
arch/powerpc/perf/core-book3s.c | 35 +
arch/powerpc/platforms/powernv/idle.c | 10 +-
30 files changed, 1589 insertions(+), 770 deletions(-)
create mode 100644 arch/powerpc/kvm/book3s_hv.h
--
2.23.0
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:02:41
It is possible to create a VCPU without setting the MSR before running
it, which results in a warning in kvmhv_vcpu_entry_p9() that MSR_ME is
not set. This is pretty harmless because the MSR_ME bit is added to
HSRR1 before HRFID to guest, and a normal qemu guest doesn't hit it.
Initialise the vcpu MSR with MSR_ME set.
Reported-by: Alexey Kardashevskiy <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 1 +
1 file changed, 1 insertion(+)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:03:14
TM fake-suspend emulation is only used by POWER9. Remove it from the old
code path.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv_rmhandlers.S | 42 -------------------------
1 file changed, 42 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:03:45
The softpatch interrupt sets HSRR0 to the faulting instruction +4, so
it should subtract 4 for the faulting instruction address in the case
it is a TM softpatch interrupt (the instruction was not executed) and
it was not emulated.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv_tm.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
@@ -46,6 +46,15 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)u64newmsr,bescr;intra,rs;+/*+*TheTMsoftpatchinterruptsetsNIPtotheinstructionfollowing+*thefaultinginstruction,whichisnotexecuted.Rewindniptothe+*faultinginstructionsoitlookslikeanormalsynchronous+*interrupt,thenupdatenipintheplaceswheretheinstructionis+*emulated.+*/+vcpu->arch.regs.nip-=4;+/**rfid,rfebb,andmtmsrdencodebit31=0sinceit'sareservedbit*intheseinstructions,somaskingbit31outdoesn'tchangethese
@@ -67,7 +76,7 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)(newmsr&MSR_TM)));newmsr=sanitize_msr(newmsr);vcpu->arch.shregs.msr=newmsr;-vcpu->arch.cfar=vcpu->arch.regs.nip-4;+vcpu->arch.cfar=vcpu->arch.regs.nip;vcpu->arch.regs.nip=vcpu->arch.shregs.srr0;returnRESUME_GUEST;
@@ -100,7 +109,7 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)vcpu->arch.bescr=bescr;msr=(msr&~MSR_TS_MASK)|MSR_TS_T;vcpu->arch.shregs.msr=msr;-vcpu->arch.cfar=vcpu->arch.regs.nip-4;+vcpu->arch.cfar=vcpu->arch.regs.nip;vcpu->arch.regs.nip=vcpu->arch.ebbrr;returnRESUME_GUEST;
@@ -116,6 +125,7 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)newmsr=(newmsr&~MSR_LE)|(msr&MSR_LE);newmsr=sanitize_msr(newmsr);vcpu->arch.shregs.msr=newmsr;+vcpu->arch.regs.nip+=4;returnRESUME_GUEST;/* ignore bit 31, see comment above */
@@ -152,6 +162,7 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)msr=(msr&~MSR_TS_MASK)|MSR_TS_S;}vcpu->arch.shregs.msr=msr;+vcpu->arch.regs.nip+=4;returnRESUME_GUEST;/* ignore bit 31, see comment above */
@@ -189,6 +200,7 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)vcpu->arch.regs.ccr=(vcpu->arch.regs.ccr&0x0fffffff)|(((msr&MSR_TS_MASK)>>MSR_TS_S_LG)<<29);vcpu->arch.shregs.msr&=~MSR_TS_MASK;+vcpu->arch.regs.nip+=4;returnRESUME_GUEST;/* ignore bit 31, see comment above */
@@ -220,6 +232,7 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)vcpu->arch.regs.ccr=(vcpu->arch.regs.ccr&0x0fffffff)|(((msr&MSR_TS_MASK)>>MSR_TS_S_LG)<<29);vcpu->arch.shregs.msr=msr|MSR_TS_S;+vcpu->arch.regs.nip+=4;returnRESUME_GUEST;}
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:04:19
Have the TM softpatch emulation code set up the HFAC interrupt and
return -1 in case an instruction was executed with HFSCR bits clear,
and have the interrupt exit handler fall through to the HFAC handler.
When the L0 is running a nested guest, this ensures the HFAC interrupt
is correctly passed up to the L1.
The "direct guest" exit handler will turn these into PROGILL program
interrupts so functionality in practice will be unchanged. But it's
possible an L1 would want to handle these in a different way.
Also rearrange the FAC interrupt emulation code to match the HFAC format
while here (mainly, adding the FSCR_INTR_CAUSE mask).
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/reg.h | 3 ++-
arch/powerpc/kvm/book3s_hv.c | 35 ++++++++++++++++----------
arch/powerpc/kvm/book3s_hv_tm.c | 44 ++++++++++++++++++---------------
3 files changed, 48 insertions(+), 34 deletions(-)
@@ -1814,9 +1817,15 @@ static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)*modeandwastransitioningtotransactionalstate.*/r=kvmhv_p9_tm_emulation(vcpu);-break;+if(r!=-1)+break;+fallthrough;/* go to facility unavailable handler */#endif+caseBOOK3S_INTERRUPT_H_FAC_UNAVAIL:+r=RESUME_HOST;+break;+caseBOOK3S_INTERRUPT_HV_RM_HARD:vcpu->arch.trap=0;r=RESUME_GUEST;
@@ -88,14 +88,15 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)}/* check EBB facility is available */if(!(vcpu->arch.hfscr&HFSCR_EBB)){-/* generate an illegal instruction interrupt */-kvmppc_core_queue_program(vcpu,SRR1_PROGILL);-returnRESUME_GUEST;+vcpu->arch.hfscr&=~HFSCR_INTR_CAUSE;+vcpu->arch.hfscr|=(u64)FSCR_EBB_LG<<56;+vcpu->arch.trap=BOOK3S_INTERRUPT_H_FAC_UNAVAIL;+return-1;/* rerun host interrupt handler */}if((msr&MSR_PR)&&!(vcpu->arch.fscr&FSCR_EBB)){/* generate a facility unavailable interrupt */-vcpu->arch.fscr=(vcpu->arch.fscr&~(0xffull<<56))|-((u64)FSCR_EBB_LG<<56);+vcpu->arch.fscr&=~FSCR_INTR_CAUSE;+vcpu->arch.fscr|=(u64)FSCR_EBB_LG<<56;kvmppc_book3s_queue_irqprio(vcpu,BOOK3S_INTERRUPT_FAC_UNAVAIL);returnRESUME_GUEST;}
@@ -138,14 +139,15 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)}/* check for TM disabled in the HFSCR or MSR */if(!(vcpu->arch.hfscr&HFSCR_TM)){-/* generate an illegal instruction interrupt */-kvmppc_core_queue_program(vcpu,SRR1_PROGILL);-returnRESUME_GUEST;+vcpu->arch.hfscr&=~HFSCR_INTR_CAUSE;+vcpu->arch.hfscr|=(u64)FSCR_TM_LG<<56;+vcpu->arch.trap=BOOK3S_INTERRUPT_H_FAC_UNAVAIL;+return-1;/* rerun host interrupt handler */}if(!(msr&MSR_TM)){/* generate a facility unavailable interrupt */-vcpu->arch.fscr=(vcpu->arch.fscr&~(0xffull<<56))|-((u64)FSCR_TM_LG<<56);+vcpu->arch.fscr&=~FSCR_INTR_CAUSE;+vcpu->arch.fscr|=(u64)FSCR_TM_LG<<56;kvmppc_book3s_queue_irqprio(vcpu,BOOK3S_INTERRUPT_FAC_UNAVAIL);returnRESUME_GUEST;
@@ -169,14 +171,15 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)case(PPC_INST_TRECLAIM&PO_XOP_OPCODE_MASK):/* check for TM disabled in the HFSCR or MSR */if(!(vcpu->arch.hfscr&HFSCR_TM)){-/* generate an illegal instruction interrupt */-kvmppc_core_queue_program(vcpu,SRR1_PROGILL);-returnRESUME_GUEST;+vcpu->arch.hfscr&=~HFSCR_INTR_CAUSE;+vcpu->arch.hfscr|=(u64)FSCR_TM_LG<<56;+vcpu->arch.trap=BOOK3S_INTERRUPT_H_FAC_UNAVAIL;+return-1;/* rerun host interrupt handler */}if(!(msr&MSR_TM)){/* generate a facility unavailable interrupt */-vcpu->arch.fscr=(vcpu->arch.fscr&~(0xffull<<56))|-((u64)FSCR_TM_LG<<56);+vcpu->arch.fscr&=~FSCR_INTR_CAUSE;+vcpu->arch.fscr|=(u64)FSCR_TM_LG<<56;kvmppc_book3s_queue_irqprio(vcpu,BOOK3S_INTERRUPT_FAC_UNAVAIL);returnRESUME_GUEST;
@@ -208,14 +211,15 @@ int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)/* XXX do we need to check for PR=0 here? *//* check for TM disabled in the HFSCR or MSR */if(!(vcpu->arch.hfscr&HFSCR_TM)){-/* generate an illegal instruction interrupt */-kvmppc_core_queue_program(vcpu,SRR1_PROGILL);-returnRESUME_GUEST;+vcpu->arch.hfscr&=~HFSCR_INTR_CAUSE;+vcpu->arch.hfscr|=(u64)FSCR_TM_LG<<56;+vcpu->arch.trap=BOOK3S_INTERRUPT_H_FAC_UNAVAIL;+return-1;/* rerun host interrupt handler */}if(!(msr&MSR_TM)){/* generate a facility unavailable interrupt */-vcpu->arch.fscr=(vcpu->arch.fscr&~(0xffull<<56))|-((u64)FSCR_TM_LG<<56);+vcpu->arch.fscr&=~FSCR_INTR_CAUSE;+vcpu->arch.fscr|=(u64)FSCR_TM_LG<<56;kvmppc_book3s_queue_irqprio(vcpu,BOOK3S_INTERRUPT_FAC_UNAVAIL);returnRESUME_GUEST;
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:05:05
From: Fabiano Rosas <redacted>
As one of the arguments of the H_ENTER_NESTED hypercall, the nested
hypervisor (L1) prepares a structure containing the values of various
hypervisor-privileged registers with which it wants the nested guest
(L2) to run. Since the nested HV runs in supervisor mode it needs the
host to write to these registers.
To stop a nested HV manipulating this mechanism and using a nested
guest as a proxy to access a facility that has been made unavailable
to it, we have a routine that sanitises the values of the HV registers
before copying them into the nested guest's vcpu struct.
However, when coming out of the guest the values are copied as they
were back into L1 memory, which means that any sanitisation we did
during guest entry will be exposed to L1 after H_ENTER_NESTED returns.
This patch alters this sanitisation to have effect on the vcpu->arch
registers directly before entering and after exiting the guest,
leaving the structure that is copied back into L1 unchanged (except
when we really want L1 to access the value, e.g the Cause bits of
HFSCR).
Signed-off-by: Fabiano Rosas <redacted>
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv_nested.c | 94 ++++++++++++++---------------
1 file changed, 46 insertions(+), 48 deletions(-)
@@ -128,55 +127,17 @@ static void save_hv_return_state(struct kvm_vcpu *vcpu, int trap,caseBOOK3S_INTERRUPT_H_INST_STORAGE:hr->asdr=vcpu->arch.fault_gpa;break;+caseBOOK3S_INTERRUPT_H_FAC_UNAVAIL:+hr->hfscr=((~HFSCR_INTR_CAUSE&hr->hfscr)|+(HFSCR_INTR_CAUSE&vcpu->arch.hfscr));+break;caseBOOK3S_INTERRUPT_H_EMUL_ASSIST:hr->heir=vcpu->arch.emul_inst;break;}}-/*-*ThiscanresultinsomeL0HVregisterstatebeingleakedtoanL1-*hypervisorwhenthehv_guest_stateiscopiedbacktotheguestafter-*beingmodifiedhere.-*-*Thereisnoknownproblemwithsuchaleak,andinmanycasesthese-*registersettingscouldbederivedbytheguestbyobservingbehaviour-*andtiming,interrupts,etc.,butitisanissuetoconsider.-*/-staticvoidsanitise_hv_regs(structkvm_vcpu*vcpu,structhv_guest_state*hr)-{-structkvmppc_vcore*vc=vcpu->arch.vcore;-u64mask;--/*-*Don'tletL1changeLPCRbitsfortheL2exceptthese:-*/-mask=LPCR_DPFD|LPCR_ILE|LPCR_TC|LPCR_AIL|LPCR_LD|-LPCR_LPES|LPCR_MER;--/*-*Additionalfilteringisrequireddependingonhardware-*andconfiguration.-*/-hr->lpcr=kvmppc_filter_lpcr_hv(vcpu->kvm,-(vc->lpcr&~mask)|(hr->lpcr&mask));--/*-*Don'tletL1enablefeaturesforL2whichwe'vedisabledforL1,-*butpreservetheinterruptcausefield.-*/-hr->hfscr&=(HFSCR_INTR_CAUSE|vcpu->arch.hfscr);--/* Don't let data address watchpoint match in hypervisor state */-hr->dawrx0&=~DAWRX_HYP;-hr->dawrx1&=~DAWRX_HYP;--/* Don't let completed instruction address breakpt match in HV state */-if((hr->ciabr&CIABR_PRIV)==CIABR_PRIV_HYPER)-hr->ciabr&=~CIABR_PRIV;-}--staticvoidrestore_hv_regs(structkvm_vcpu*vcpu,structhv_guest_state*hr)+staticvoidrestore_hv_regs(structkvm_vcpu*vcpu,conststructhv_guest_state*hr){structkvmppc_vcore*vc=vcpu->arch.vcore;
@@ -288,6 +249,43 @@ static int kvmhv_write_guest_state_and_regs(struct kvm_vcpu *vcpu,sizeof(structpt_regs));}+staticvoidload_l2_hv_regs(structkvm_vcpu*vcpu,+conststructhv_guest_state*l2_hv,+conststructhv_guest_state*l1_hv,u64*lpcr)+{+structkvmppc_vcore*vc=vcpu->arch.vcore;+u64mask;++restore_hv_regs(vcpu,l2_hv);++/*+*Don'tletL1changeLPCRbitsfortheL2exceptthese:+*/+mask=LPCR_DPFD|LPCR_ILE|LPCR_TC|LPCR_AIL|LPCR_LD|+LPCR_LPES|LPCR_MER;++/*+*Additionalfilteringisrequireddependingonhardware+*andconfiguration.+*/+*lpcr=kvmppc_filter_lpcr_hv(vcpu->kvm,+(vc->lpcr&~mask)|(*lpcr&mask));++/*+*Don'tletL1enablefeaturesforL2whichwe'vedisabledforL1,+*butpreservetheinterruptcausefield.+*/+vcpu->arch.hfscr=l2_hv->hfscr&(HFSCR_INTR_CAUSE|vcpu->arch.hfscr);++/* Don't let data address watchpoint match in hypervisor state */+vcpu->arch.dawrx0=l2_hv->dawrx0&~DAWRX_HYP;+vcpu->arch.dawrx1=l2_hv->dawrx1&~DAWRX_HYP;++/* Don't let completed instruction address breakpt match in HV state */+if((l2_hv->ciabr&CIABR_PRIV)==CIABR_PRIV_HYPER)+vcpu->arch.ciabr=l2_hv->ciabr&~CIABR_PRIV;+}+longkvmhv_enter_nested_guest(structkvm_vcpu*vcpu){longinterr,r;
@@ -296,7 +294,7 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)structhv_guest_statel2_hv={0},saved_l1_hv;structkvmppc_vcore*vc=vcpu->arch.vcore;u64hv_ptr,regs_ptr;-u64hdec_exp;+u64hdec_exp,lpcr;s64delta_purr,delta_spurr,delta_ic,delta_vtb;if(vcpu->kvm->arch.l1_ptcr==0)
@@ -369,8 +367,8 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)/* Guest must always run with ME enabled, HV disabled. */vcpu->arch.shregs.msr=(vcpu->arch.regs.msr|MSR_ME)&~MSR_HV;-sanitise_hv_regs(vcpu,&l2_hv);-restore_hv_regs(vcpu,&l2_hv);+lpcr=l2_hv.lpcr;+load_l2_hv_regs(vcpu,&l2_hv,&saved_l1_hv,&lpcr);vcpu->arch.ret=RESUME_GUEST;vcpu->arch.trap=0;
@@ -380,7 +378,7 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)r=RESUME_HOST;break;}-r=kvmhv_run_single_vcpu(vcpu,hdec_exp,l2_hv.lpcr);+r=kvmhv_run_single_vcpu(vcpu,hdec_exp,lpcr);}while(is_kvmppc_resume_guest(r));/* save L2 state for return */
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:05:43
When the L0 runs a nested L2, there are several permutations of HFSCR
that can be relevant. The HFSCR that the L1 vcpu L1 requested, the
HFSCR that the L1 vcpu may use, and the HFSCR that is actually being
used to run the L2.
The L1 requested HFSCR is not accessible outside the nested hcall
handler, so copy that into a new kvm_nested_guest.hfscr field.
The permitted HFSCR is taken from the HFSCR that the L1 runs with,
which is also not accessible while the hcall is being made. Move
this into a new kvm_vcpu_arch.hfscr_permitted field.
These will be used by the next patch to improve facility handling
for nested guests, and later by facility demand faulting patches.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/kvm_book3s_64.h | 1 +
arch/powerpc/include/asm/kvm_host.h | 2 ++
arch/powerpc/kvm/book3s_hv.c | 2 ++
arch/powerpc/kvm/book3s_hv_nested.c | 5 +++--
4 files changed, 8 insertions(+), 2 deletions(-)
@@ -39,6 +39,7 @@ struct kvm_nested_guest {pgd_t*shadow_pgtable;/* our page table for this guest */u64l1_gr_to_hr;/* L1's addr of part'n-scoped table */u64process_table;/* process table entry for this guest */+u64hfscr;/* HFSCR that the L1 requested for this nested guest */longrefcnt;/* number of pointers to this struct */structmutextlb_lock;/* serialize page faults and tlbies */structkvm_nested_guest*next;
@@ -811,6 +811,8 @@ struct kvm_vcpu_arch {u32online;+u64hfscr_permitted;/* A mask of permitted HFSCR facilities */+/* For support of nested guests */structkvm_nested_guest*nested;u32nested_vcpu_id;
@@ -272,10 +272,10 @@ static void load_l2_hv_regs(struct kvm_vcpu *vcpu,(vc->lpcr&~mask)|(*lpcr&mask));/*-*Don'tletL1enablefeaturesforL2whichwe'vedisabledforL1,+*Don'tletL1enablefeaturesforL2whichwedon'tallowforL1,*butpreservetheinterruptcausefield.*/-vcpu->arch.hfscr=l2_hv->hfscr&(HFSCR_INTR_CAUSE|vcpu->arch.hfscr);+vcpu->arch.hfscr=l2_hv->hfscr&(HFSCR_INTR_CAUSE|vcpu->arch.hfscr_permitted);/* Don't let data address watchpoint match in hypervisor state */vcpu->arch.dawrx0=l2_hv->dawrx0&~DAWRX_HYP;
@@ -362,6 +362,7 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)/* set L1 state to L2 state */vcpu->arch.nested=l2;vcpu->arch.nested_vcpu_id=l2_hv.vcpu_token;+l2->hfscr=l2_hv.hfscr;vcpu->arch.regs=l2_regs;/* Guest must always run with ME enabled, HV disabled. */
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:06:21
From: Fabiano Rosas <redacted>
If the nested hypervisor has no access to a facility because it has
been disabled by the host, it should also not be able to see the
Hypervisor Facility Unavailable that arises from one of its guests
trying to access the facility.
This patch turns a HFU that happened in L2 into a Hypervisor Emulation
Assistance interrupt and forwards it to L1 for handling. The ones that
happened because L1 explicitly disabled the facility for L2 are still
let through, along with the corresponding Cause bits in the HFSCR.
Signed-off-by: Fabiano Rosas <redacted>
[np: move handling into kvmppc_handle_nested_exit]
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 31 +++++++++++++++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)
@@ -1822,9 +1823,35 @@ static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)fallthrough;/* go to facility unavailable handler */#endif-caseBOOK3S_INTERRUPT_H_FAC_UNAVAIL:-r=RESUME_HOST;+caseBOOK3S_INTERRUPT_H_FAC_UNAVAIL:{+u64cause=vcpu->arch.hfscr>>56;++/*+*OnlypassHFUinterruptstotheL1ifthefacilityis+*permittedbutdisabledbytheL1'sHFSCR,otherwise+*theinterruptdoesnotmakesensetotheL1soturn+*itintoaHEAI.+*/+if(!(vcpu->arch.hfscr_permitted&(1UL<<cause))||+(nested->hfscr&(1UL<<cause))){+vcpu->arch.trap=BOOK3S_INTERRUPT_H_EMUL_ASSIST;++/*+*Ifthefetchfailed,returntoguestand+*tryexecutingitagain.+*/+r=kvmppc_get_last_inst(vcpu,INST_GENERIC,+&vcpu->arch.emul_inst);+if(r!=EMULATE_DONE)+r=RESUME_GUEST;+else+r=RESUME_HOST;+}else{+r=RESUME_HOST;+}+break;+}caseBOOK3S_INTERRUPT_HV_RM_HARD:vcpu->arch.trap=0;
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:07:38
After the L1 saves its PMU SPRs but before loading the L2's PMU SPRs,
switch the pmcregs_in_use field in the L1 lppaca to the value advertised
by the L2 in its VPA. On the way out of the L2, set it back after saving
the L2 PMU registers (if they were in-use).
This transfers the PMU liveness indication between the L1 and L2 at the
points where the registers are not live.
This fixes the nested HV bug for which a workaround was added to the L0
HV by commit 63279eeb7f93a ("KVM: PPC: Book3S HV: Always save guest pmu
for guest capable of nesting"), which explains the problem in detail.
That workaround is no longer required for guests that include this bug
fix.
Fixes: 360cae313702 ("KVM: PPC: Book3S HV: Nested guest entry via hypercall")
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/include/asm/pmc.h | 7 +++++++
arch/powerpc/kvm/book3s_hv.c | 20 ++++++++++++++++++++
2 files changed, 27 insertions(+)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:08:16
This register is not architected and not implemented in POWER9 or 10,
it just reads back zeroes for compatibility.
Reviewed-by: Fabiano Rosas <redacted>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv.c | 3 ---
arch/powerpc/platforms/powernv/idle.c | 2 --
2 files changed, 5 deletions(-)
@@ -667,7 +667,6 @@ static unsigned long power9_idle_stop(unsigned long psscr)sprs.purr=mfspr(SPRN_PURR);sprs.spurr=mfspr(SPRN_SPURR);sprs.dscr=mfspr(SPRN_DSCR);-sprs.wort=mfspr(SPRN_WORT);sprs.ciabr=mfspr(SPRN_CIABR);sprs.mmcra=mfspr(SPRN_MMCRA);
@@ -785,7 +784,6 @@ static unsigned long power9_idle_stop(unsigned long psscr)mtspr(SPRN_PURR,sprs.purr);mtspr(SPRN_SPURR,sprs.spurr);mtspr(SPRN_DSCR,sprs.dscr);-mtspr(SPRN_WORT,sprs.wort);mtspr(SPRN_CIABR,sprs.ciabr);mtspr(SPRN_MMCRA,sprs.mmcra);
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:09:03
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(-)
@@ -4049,7 +4049,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-08-11 16:09:40
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(-)
@@ -4049,7 +4048,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-08-11 16:10:17
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-08-11 16:10:56
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(-)
@@ -4049,7 +4049,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-08-11 16:11:34
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(-)
@@ -542,13 +552,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.
@@ -609,10 +650,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);}
@@ -854,11 +892,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;}
@@ -4048,11 +4048,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-08-11 16:12:22
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-08-11 16:13:01
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);}}
@@ -687,7 +686,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 */
@@ -708,7 +706,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-08-11 16:13:39
Provide a config option that controls the the workaround added by commit
63279eeb7f93a ("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 ("KVM: PPC: Book3S
HV Nested: Indicate guest PMU in-use in VPA") 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.
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(-)
@@ -4033,8 +4033,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-08-11 16:14:18
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: Athira Jajeev <redacted>
Cc: Madhavan Srinivasan <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(-)
@@ -2718,6 +2718,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-08-11 16:15:04
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: Athira Jajeev <redacted>
Cc: Madhavan Srinivasan <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(+)
@@ -4089,6 +4089,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.
@@ -2428,8 +2428,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){+printk(KERN_WARNING"perf: 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;
@@ -2451,4 +2467,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
@@ -3765,6 +3765,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-08-11 16:16:22
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.
Cc: 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-08-11 16:17:09
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.
Cc: 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(-)
@@ -2753,6 +2776,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;
@@ -3823,6 +3851,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
@@ -3857,41 +3893,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,
@@ -3935,9 +3977,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-08-11 16:18:26
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.
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-08-11 16:19:03
Move the SPR update into its relevant helper function. This will
help with SPR scheduling improvements in later changes.
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-08-11 16:19:41
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.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kernel/process.c | 26 ++++++++++++
arch/powerpc/kvm/book3s_hv.c | 61 ++++++++++++++++++---------
arch/powerpc/kvm/book3s_hv_p9_entry.c | 1 +
3 files changed, 69 insertions(+), 19 deletions(-)
@@ -4150,8 +4151,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;
@@ -4160,12 +4176,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);
@@ -4803,6 +4819,8 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,gotodone;}+voidsave_user_regs_kvm(void);+staticintkvmppc_vcpu_run_hv(structkvm_vcpu*vcpu){structkvm_run*run=vcpu->run;
@@ -4812,19 +4830,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)){
@@ -4832,12 +4855,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
@@ -4852,18 +4869,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);++save_user_regs_kvm();/* 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-08-11 16:20:26
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(-)
@@ -4155,6 +4155,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))
@@ -4654,6 +4666,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);
@@ -4697,11 +4710,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){
@@ -4756,7 +4769,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,guest_exit_irqoff();-local_irq_enable();+powerpc_local_irq_pmu_restore(flags);cpumask_clear_cpu(pcpu,&kvm->arch.cpu_in_guest);
@@ -4814,7 +4827,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-08-11 16:21:04
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(-)
@@ -3073,6 +3073,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);
@@ -3383,7 +3381,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);
@@ -4260,6 +4261,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){
@@ -4298,12 +4306,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);
@@ -4788,7 +4790,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-08-11 16:22:21
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-08-11 16:23:09
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(-)
@@ -4264,8 +4263,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&&
@@ -4570,7 +4569,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){
@@ -4605,7 +4604,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;
@@ -4646,7 +4645,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;
@@ -4674,6 +4673,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);
@@ -4684,7 +4684,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;
@@ -4709,7 +4708,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();
@@ -4719,6 +4717,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)
@@ -4741,12 +4740,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;
@@ -4761,7 +4765,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();
@@ -4790,7 +4794,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);
@@ -4826,7 +4830,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-08-11 16:24:27
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 | 64 ++++++++++++++++++++++--------------
1 file changed, 39 insertions(+), 25 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:25:12
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-08-11 16:25:50
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(-)
@@ -4098,6 +4098,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){if(!cpu_has_feature(CPU_FTR_ARCH_31))
@@ -4205,19 +4243,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-08-11 16:26:31
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 | 378 +-------------------------
arch/powerpc/kvm/book3s_hv.h | 44 +++
arch/powerpc/kvm/book3s_hv_p9_entry.c | 353 ++++++++++++++++++++++++
3 files changed, 398 insertions(+), 377 deletions(-)
create mode 100644 arch/powerpc/kvm/book3s_hv.h
@@ -3800,378 +3796,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_ARCH_31))-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_ARCH_31))-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)-{-if(!cpu_has_feature(CPU_FTR_ARCH_31))-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_ARCH_31))-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_ARCH_31))+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_ARCH_31))+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_ARCH_31))+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_ARCH_31))+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-08-11 16:27:18
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(-)
@@ -3812,6 +3812,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.*/
@@ -3873,63 +3938,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&&
@@ -3828,6 +3834,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;
@@ -3869,12 +3896,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;}
@@ -3898,33 +3931,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;
@@ -3932,11 +3938,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-08-11 16:28:37
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 | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:29:15
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-08-11 16:30:00
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(-)
@@ -1593,7 +1593,8 @@ XXX benchmark guest exitsunsignedlongvsid;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;}
@@ -641,24 +641,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));
@@ -877,20 +882,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;
@@ -908,6 +899,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);
@@ -915,15 +922,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-08-11 16:31:17
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-08-11 16:31:56
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 | 1 +
arch/powerpc/kvm/book3s_hv.c | 26 ++++++++++++++++++++------
arch/powerpc/kvm/book3s_hv_p9_entry.c | 15 +++++++++++----
3 files changed, 32 insertions(+), 10 deletions(-)
@@ -637,8 +643,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-08-11 16:32:43
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 | 2 +
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, 74 insertions(+), 51 deletions(-)
@@ -4533,9 +4533,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;
@@ -4596,14 +4593,7 @@ static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)save_user_regs_kvm();-/* 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;
@@ -4644,15 +4634,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_ARCH_31))+if(!cpu_has_feature(CPU_FTR_ARCH_31)&&+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);
@@ -373,26 +373,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_ARCH_31))-mtspr(SPRN_TIDR,host_os_sprs->tidr);+if(!cpu_has_feature(CPU_FTR_ARCH_31)&&+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-08-11 16:33:21
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_mmu_radix.c | 4 +++
arch/powerpc/kvm/book3s_hv_p9_entry.c | 40 +++++++++++++++++++-------
2 files changed, 33 insertions(+), 11 deletions(-)
@@ -54,6 +54,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)
@@ -70,6 +72,8 @@ unsigned long __kvmhv_copy_tofrom_guest_radix(int lpid, int pid,elseret=copy_to_user_nofault((void__user*)to,from,n);+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-08-11 16:33:59
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-08-11 16:34:39
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(-)
@@ -645,6 +645,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;
@@ -662,7 +663,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()){
@@ -746,8 +749,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);
@@ -953,7 +962,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);
@@ -999,9 +1008,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-08-11 16:35:15
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 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 | 31 +++++++++++++++++++++++----
arch/powerpc/kvm/book3s_hv_p9_entry.c | 9 --------
2 files changed, 27 insertions(+), 13 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:35:54
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;
@@ -1817,6 +1821,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:
@@ -3978,6 +3988,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-08-11 16:36:41
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(-)
@@ -3891,24 +3893,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 */
@@ -628,6 +628,44 @@ static void save_clear_guest_mmu(struct kvm *kvm, struct kvm_vcpu *vcpu)}}+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;
@@ -661,6 +699,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);
@@ -682,35 +723,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-08-11 16:37:19
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-08-11 16:37:58
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.
From: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/kvm/book3s_hv_p9_entry.c | 50 ++++++++++++---------------
1 file changed, 23 insertions(+), 27 deletions(-)
@@ -825,7 +825,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
@@ -837,14 +845,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 */kvmppc_check_need_tlb_flush(kvm,vc->pcpu,nested);
@@ -899,19 +903,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)];
@@ -969,13 +970,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;}}
@@ -1075,7 +1069,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-08-11 16:38:45
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(-)
@@ -4399,7 +4399,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-08-11 16:39:24
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 | 38 +++++++++++++-----------
3 files changed, 21 insertions(+), 19 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2021-08-11 16:40:04
The P9 path always uses one vcpu per vcore, so none of the 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);}
@@ -3128,13 +3151,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();
@@ -4116,7 +4136,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;}
@@ -4133,6 +4153,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){
@@ -4398,11 +4420,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)){
@@ -4420,11 +4438,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);
@@ -4453,21 +4468,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;+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);+vc->pcpu=pcpu;// for kvmppc_create_dtl_entrykvmppc_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();
@@ -4489,11 +4506,10 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,set_irq_happened(trap);-kvmppc_set_host_core(pcpu);-guest_exit_irqoff();-kvmppc_stop_thread(vcpu);+vcpu->cpu=-1;+vcpu->arch.thread_cpu=-1;powerpc_local_irq_pmu_restore(flags);
@@ -4520,28 +4536,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;
@@ -4625,7 +4644,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;
@@ -5087,6 +5107,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-08-11 16:40:41
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 | 61 +++++++++++++++++++++---------------
1 file changed, 35 insertions(+), 26 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-08-11 16:41:27
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(-)
@@ -660,6 +660,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-08-11 16:42:07
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'tneedtoactualyknowwhethernotOPALresynced+*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: kernel test robot <hidden> Date: 2021-08-11 20:42:37
Hi Nicholas,
I love your patch! Yet something to improve:
[auto build test ERROR on powerpc/next]
[also build test ERROR on linus/master v5.14-rc5 next-20210811]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Nicholas-Piggin/KVM-PPC-Book3S-HV-P9-entry-exit-optimisations/20210812-000748
base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: powerpc64-randconfig-r024-20210811 (attached as .config)
compiler: powerpc-linux-gcc (GCC) 10.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/30a3a9ae99f124a863c41f268c68b647d7116b65
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Nicholas-Piggin/KVM-PPC-Book3S-HV-P9-entry-exit-optimisations/20210812-000748
git checkout 30a3a9ae99f124a863c41f268c68b647d7116b65
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-10.3.0 make.cross O=build_dir ARCH=powerpc SHELL=/bin/bash arch/powerpc/kvm/
If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <redacted>
All errors (new ones prefixed by >>):
In file included from arch/powerpc/include/asm/processor.h:11,
from arch/powerpc/include/asm/thread_info.h:40,
from include/linux/thread_info.h:60,
from include/asm-generic/preempt.h:5,
from ./arch/powerpc/include/generated/asm/preempt.h:1,
from include/linux/preempt.h:78,
from include/linux/percpu.h:6,
from include/linux/context_tracking_state.h:5,
from include/linux/hardirq.h:5,
from include/linux/kvm_host.h:7,
from arch/powerpc/kvm/book3s_hv_p9_entry.c:3:
arch/powerpc/kvm/book3s_hv_p9_entry.c: In function 'load_vcpu_state':
quoted
arch/powerpc/kvm/book3s_hv_p9_entry.c:297:33: error: 'struct kvm_vcpu_arch' has no member named 'texasr'
On 11-Aug-2021, at 9:30 PM, Nicholas Piggin [off-list ref] 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.
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 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.
Hi Nick,
For the PMU related changes,
Reviewed-by: Athira Rajeev <redacted>
Thanks
Athira
- 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
Fabiano Rosas (3):
KVM: PPC: Book3S HV Nested: Sanitise vcpu registers
KVM: PPC: Book3S HV Nested: Stop forwarding all HFUs to L1
KVM: PPC: Book3S HV Nested: save_hv_return_state does not require trap
argument
Nicholas Piggin (57):
KVM: PPC: Book3S HV: Initialise vcpu MSR with MSR_ME
KVM: PPC: Book3S HV: Remove TM emulation from POWER7/8 path
KVM: PPC: Book3S HV P9: Fixes for TM softpatch interrupt NIP
KVM: PPC: Book3S HV Nested: Fix TM softpatch HFAC interrupt emulation
KVM: PPC: Book3S HV Nested: Make nested HFSCR state accessible
KVM: PPC: Book3S HV Nested: Reflect guest PMU in-use to L0 when guest
SPRs are live
powerpc/64s: Remove WORT SPR from POWER9/10
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 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 | 6 +-
arch/powerpc/include/asm/kvm_host.h | 7 +-
arch/powerpc/include/asm/kvm_ppc.h | 1 +
arch/powerpc/include/asm/pmc.h | 7 +
arch/powerpc/include/asm/reg.h | 3 +-
arch/powerpc/include/asm/switch_to.h | 2 +
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 | 32 +
arch/powerpc/kernel/time.c | 54 +-
arch/powerpc/kvm/Kconfig | 15 +
arch/powerpc/kvm/book3s_64_mmu_radix.c | 4 +
arch/powerpc/kvm/book3s_hv.c | 890 ++++++++++--------
arch/powerpc/kvm/book3s_hv.h | 41 +
arch/powerpc/kvm/book3s_hv_builtin.c | 2 +
arch/powerpc/kvm/book3s_hv_hmi.c | 7 +-
arch/powerpc/kvm/book3s_hv_interrupts.S | 13 +-
arch/powerpc/kvm/book3s_hv_nested.c | 109 ++-
arch/powerpc/kvm/book3s_hv_p9_entry.c | 817 +++++++++++++---
arch/powerpc/kvm/book3s_hv_ras.c | 54 ++
arch/powerpc/kvm/book3s_hv_rmhandlers.S | 115 +--
arch/powerpc/kvm/book3s_hv_tm.c | 61 +-
arch/powerpc/mm/book3s64/radix_pgtable.c | 15 -
arch/powerpc/perf/core-book3s.c | 35 +
arch/powerpc/platforms/powernv/idle.c | 10 +-
30 files changed, 1589 insertions(+), 770 deletions(-)
create mode 100644 arch/powerpc/kvm/book3s_hv.h
--
2.23.0
From: Michael Ellerman <hidden> Date: 2021-08-27 13:31:35
On Thu, 12 Aug 2021 02:00:34 +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.
[...]