KVM: PPC: Book3S HV: Support virtual SMT modes on POWER9
Subsystems (4)
cover KVM: PPC: Book3S HV: Support virtual SMT modes on POWER9
This patch series makes it possible to run guests on POWER9 with 2, 4 or 8 threads (vCPUs) per virtual core, for example by specifying -smp 8,threads=8 on the QEMU command line. Previously it was only possible to use 1 thread per virtual core. This was mostly because the CPU threads being independent on POWER9 has the side-effect that the doorbell facility (i.e. the msgsndp instruction) can only be used to send an interrupt from a CPU to itself. With this patch series, the doorbell facility is emulated in software, and thus the guest can now send doorbell interrupts to other vCPUs in the same virtual core as it expects to be able to do when there is more than one thread per core.
This patch series also has the effect that userspace on POWER8 can specify the virtual SMT mode that it wants for the guest and then use consecutive CPU numbering, rather than having to space the vCPU numbers out to get the desired SMT mode (for example by using vCPU numbers 0, 1, 8, 9 to get 4 vCPUs with 2 threads per virtual core).
The interface for userspace to set the vSMT mode uses the existing KVM_CAP_PPC_SMT capability. It can now be enabled on a VM, and the parameters specify the desired vSMT mode. Old kernels will give an error on attempts to enable the capability, which should be sufficient indication that this feature is not present; I don't think it is necessary to provide another capability to say that this capability can be enabled.
These patches are against v4.12-rc2 plus my patch series "KVM: PPC:
Book3S HV: Support POWER9's large decrementer mode", although the two patch series don't interact.
Paul.
--- Documentation/virtual/kvm/api.txt | 15 ++ arch/powerpc/include/asm/kvm_host.h | 4 + arch/powerpc/include/asm/kvm_ppc.h | 2 + arch/powerpc/include/asm/ppc-opcode.h | 2 + arch/powerpc/kernel/asm-offsets.c | 2 + arch/powerpc/kvm/book3s_hv.c | 239 +++++++++++++++++++++++++++++--- arch/powerpc/kvm/book3s_hv_rmhandlers.S | 32 +++++ arch/powerpc/kvm/powerpc.c | 16 ++- 8 files changed, 289 insertions(+), 23 deletions(-)
PATCH 1/4 KVM: PPC: Book3S HV: Don't let VCPU sleep if it has a doorbell pending
It is possible, through a narrow race condition, for a VCPU to exit the guest with a H_CEDE hypercall while it has a doorbell interrupt pending. In this case, the H_CEDE should return immediately, but in fact it puts the VCPU to sleep until some other interrupt becomes pending or a prod is received (via another VCPU doing H_PROD).
This fixes it by checking the DPDES (Directed Privileged Doorbell Exception Status) bit for the thread along with the other interrupt pending bits.
--- arch/powerpc/kvm/book3s_hv.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)
arch/powerpc/kvm/book3s_hv.c +22 -3
@@ -675,6 +675,17 @@ static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, vcpu->arch.dtl.dirty = true; } +/* See if there is a doorbell interrupt pending for a vcpu */ +static bool kvmppc_doorbell_pending (struct kvm_vcpu *vcpu) +{ +int thr; +struct kvmppc_vcore *vc; + + vc = vcpu->arch.vcore; + thr = vcpu->vcpu_id - vc->first_vcpuid; +return !!(vc->dpdes & (1 << thr)); +} +static bool kvmppc_power8_compatible (struct kvm_vcpu *vcpu) {if (vcpu->arch.vcore->arch_compat >=PVR_ARCH_207 )
@@ -2660,6 +2671,15 @@ static void shrink_halt_poll_ns(struct kvmppc_vcore *vc) vc->halt_poll_ns /= halt_poll_ns_shrink; } +static bool kvmppc_vcpu_woken (struct kvm_vcpu *vcpu) +{ +if (vcpu->arch.pending_exceptions || vcpu->arch.prodded || +kvmppc_doorbell_pending (vcpu)) +return true; + +return false; +} +/* * Check to see if any of the runnable vcpus on the vcore have pending * exceptions or are no longer ceded
@@ -2670,8 +2690,7 @@ static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)int i;for_each_runnable_thread (i, vcpu, vc) { -if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded || - vcpu->arch.prodded) +if (!vcpu->arch.ceded ||kvmppc_vcpu_woken (vcpu))return 1 ; }
@@ -2857,7 +2876,7 @@ static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)break ; n_ceded =0 ;for_each_runnable_thread (i, v, vc) { -if (!v->arch.pending_exceptions && !v->arch.prodded) +if (!kvmppc_vcpu_woken (v)) n_ceded += v->arch.ceded;else v->arch.ceded =0 ;
Signature · 1 line
2.7.4
PATCH 2/4 KVM: PPC: Book3S HV: Context-switch HFSCR between host and guest on POWER9
In reply to Paul MackerrasThis adds code to allow us to use a different value for the HFSCR (Hypervisor Facilities Status and Control Register) when running the guest from that which applies in the host. The reason for doing this is to allow us to trap the msgsndp instruction and related operations in future so that they can be virtualized. We also save the value of HFSCR when a hypervisor facility unavailable interrupt occurs, because the high byte of HFSCR indicates which facility the guest attempted to access.
We save and restore the host value on guest entry/exit because some bits of it affect host userspace execution.
We only do all this on POWER9, not on POWER8, because we are not intending to virtualize any of the facilities controlled by HFSCR on POWER8. In particular, the HFSCR bit that controls execution of msgsndp and related operations does not exist on POWER8. The HFSCR doesn't exist at all on POWER7.
--- arch/powerpc/include/asm/kvm_host.h | 1 + arch/powerpc/kernel/asm-offsets.c | 1 + arch/powerpc/kvm/book3s_hv.c | 10 ++++++++++ arch/powerpc/kvm/book3s_hv_rmhandlers.S | 15 +++++++++++++++ 4 files changed, 27 insertions(+)
arch/powerpc/include/asm/kvm_host.h +1 -0
@@ -566,6 +566,7 @@ struct kvm_vcpu_arch {ulong wort;ulong tid;ulong psscr; +ulong hfscr;ulong shadow_srr1;#endif u32 vrsave;/* also USPRG0 */
arch/powerpc/kernel/asm-offsets.c +1 -0
@@ -542,6 +542,7 @@ int main(void)OFFSET (VCPU_WORT , kvm_vcpu, arch.wort);OFFSET (VCPU_TID , kvm_vcpu, arch.tid);OFFSET (VCPU_PSSCR , kvm_vcpu, arch.psscr); +OFFSET (VCPU_HFSCR , kvm_vcpu, arch.hfscr);OFFSET (VCORE_ENTRY_EXIT , kvmppc_vcore, entry_exit_map);OFFSET (VCORE_IN_GUEST , kvmppc_vcore, in_guest);OFFSET (VCORE_NAPPING_THREADS , kvmppc_vcore, napping_threads);
arch/powerpc/kvm/book3s_hv.c +10 -0
@@ -1813,6 +1813,16 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm, vcpu->arch.busy_preempt =TB_NIL ; vcpu->arch.intr_msr =MSR_SF |MSR_ME ; +/* +* Set the default HFSCR for the guest from the host value. +* This value is only used on POWER9. +* On POWER9 DD1, TM doesn't work, so we make sure to +* prevent the guest from using it. +*/ + vcpu->arch.hfscr =mfspr (SPRN_HFSCR ); +if (!cpu_has_feature (CPU_FTR_TM )) + vcpu->arch.hfscr &= ~HFSCR_TM ; +kvmppc_mmu_book3s_hv_init (vcpu); vcpu->arch.state =KVMPPC_VCPU_NOTREADY ;
arch/powerpc/kvm/book3s_hv_rmhandlers.S +15 -0
@@ -554,6 +554,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) #define STACK_SLOT_TID (112-16) #define STACK_SLOT_PSSCR (112-24) #define STACK_SLOT_PID (112-32) +#define STACK_SLOT_HFSCR (112-40) .global kvmppc_hv_entry kvmppc_hv_entry:
@@ -754,9 +755,11 @@ BEGIN_FTR_SECTION mfspr r5, SPRN_TIDR mfspr r6, SPRN_PSSCR mfspr r7, SPRN_PID + mfspr r8, SPRN_HFSCR std r5, STACK_SLOT_TID(r1) std r6, STACK_SLOT_PSSCR(r1) std r7, STACK_SLOT_PID(r1) + std r8, STACK_SLOT_HFSCR(r1) END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300) BEGIN_FTR_SECTION
@@ -900,8 +903,10 @@ FTR_SECTION_ELSE ld r5, VCPU_TID(r4) ld r6, VCPU_PSSCR(r4) oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */ + ld r7, VCPU_HFSCR(r4) mtspr SPRN_TIDR, r5 mtspr SPRN_PSSCR, r6 + mtspr SPRN_HFSCR, r7 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300) 8:
@@ -1276,6 +1281,13 @@ END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) beq 4f b guest_exit_cont 3: + /* If it's a hypervisor facility unavailable interrupt, save HFSCR */ + cmpwi r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL + bne 14f + mfspr r3, SPRN_HFSCR + std r3, VCPU_HFSCR(r9) + b guest_exit_cont +14: /* External interrupt ? */ cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL bne+ guest_exit_cont
@@ -1519,6 +1531,9 @@ FTR_SECTION_ELSE rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */ rotldi r6, r6, 60 std r6, VCPU_PSSCR(r9) + /* Restore host HFSCR value */ + ld r7, STACK_SLOT_HFSCR(r1) + mtspr SPRN_HFSCR, r7 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300) /* * Restore various registers to 0, where non-zero values
Signature · 1 line
2.7.4
PATCH 3/4 KVM: PPC: Book3S HV: Allow userspace to set the desired SMT mode
In reply to Paul MackerrasThis allows userspace to set the desired virtual SMT (simultaneous multithreading) mode for a VM, that is, the number of VCPUs that get assigned to each virtual core. Previously, the virtual SMT mode was fixed to the number of threads per subcore, and if userspace wanted to have fewer vcpus per vcore, then it would achieve that by using a sparse CPU numbering. This had the disadvantage that the vcpu numbers can get quite large, particularly for SMT1 guests on a POWER8 with 8 threads per core. With this patch, userspace can set its desired virtual SMT mode and then use contiguous vcpu numbering.
On POWER8, where the threading mode is "strict", the virtual SMT mode must be less than or equal to the number of threads per subcore. On POWER9, which implements a "loose" threading mode, the virtual SMT mode can be any power of 2 between 1 and 8, even though there is effectively one thread per subcore, since the threads are independent and can all be in different partitions.
--- Documentation/virtual/kvm/api.txt | 15 ++++++++ arch/powerpc/include/asm/kvm_host.h | 1 + arch/powerpc/include/asm/kvm_ppc.h | 2 ++ arch/powerpc/kvm/book3s_hv.c | 71 +++++++++++++++++++++++++++++++------ arch/powerpc/kvm/powerpc.c | 13 ++++++- 5 files changed, 90 insertions(+), 12 deletions(-)
Documentation/virtual/kvm/api.txt +15 -0
@@ -3996,6 +3996,21 @@ Parameters: none Allow use of adapter-interruption suppression. Returns: 0 on success; -EBUSY if a VCPU has already been created. +7.11 KVM_CAP_PPC_SMT + +Architectures: ppc +Parameters: vsmt_mode, flags + +Enabling this capability on a VM provides userspace with a way to set +the desired virtual SMT mode (i.e. the number of virtual CPUs per +virtual core). The virtual SMT mode, vsmt_mode, must be a power of 2 +between 1 and 8. On POWER8, vsmt_mode must also be no greater than +the number of threads per subcore for the host. Currently flags must +be 0. A successful call to enable this capability will result in +vsmt_mode being returned when the KVM_CAP_PPC_SMT capability is +subsequently queried for the VM. This capability is only supported by +HV KVM, and can only be set before any VCPUs have been created. + 8. Other capabilities. ----------------------
arch/powerpc/include/asm/kvm_host.h +1 -0
@@ -267,6 +267,7 @@ struct kvm_resize_hpt;struct kvm_arch {unsigned int lpid; +unsigned int smt_mode;/* # vcpus per virtual core */ #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE unsigned int tlb_sets;struct kvm_hpt_info hpt;
arch/powerpc/include/asm/kvm_ppc.h +2 -0
@@ -315,6 +315,8 @@ struct kvmppc_ops { struct irq_bypass_producer *);int (*configure_mmu)(struct kvm *kvm,struct kvm_ppc_mmuv3_cfg *cfg);int (*get_rmmu_info)(struct kvm *kvm,struct kvm_ppc_rmmu_info *info); +int (*set_smt_mode)(struct kvm *kvm,unsigned long mode, +unsigned long flags); };extern struct kvmppc_ops *kvmppc_hv_ops;
arch/powerpc/kvm/book3s_hv.c +60 -11
110 lines not on this page · raw message
arch/powerpc/kvm/powerpc.c +12 -1
@@ -554,7 +554,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE case KVM_CAP_PPC_SMT : r =0 ; -if (hv_enabled) { +if (kvm) + r = kvm->arch.smt_mode; +else if (hv_enabled) {if (cpu_has_feature (CPU_FTR_ARCH_300 )) r =1 ;else
@@ -1712,6 +1714,15 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r =0 ; break; } +case KVM_CAP_PPC_SMT : { +unsigned long mode = cap->args[0 ]; +unsigned long flags = cap->args[1 ]; + + r = -EINVAL ; +if (kvm->arch.kvm_ops->set_smt_mode) + r = kvm->arch.kvm_ops->set_smt_mode (kvm, mode, flags); +break ; + }#endif default : r = -EINVAL ;
Signature · 1 line
2.7.4
PATCH 4/4 KVM: PPC: Book3S HV: Virtualize doorbell facility on POWER9
In reply to Paul MackerrasOn POWER9, we no longer have the restriction that we had on POWER8 where all threads in a core have to be in the same partition, so the CPU threads are now independent. However, we still want to be able to run guests with a virtual SMT topology, if only to allow migration of guests from POWER8 systems to POWER9.
A guest that has a virtual SMT mode greater than 1 will expect to be able to use the doorbell facility; it will expect the msgsndp and msgclrp instructions to work appropriately and to be able to read sensible values from the TIR (thread identification register) and DPDES (directed privileged doorbell exception status) special-purpose registers. However, since each CPU thread is a separate sub-processor in POWER9, these instructions and registers can only be used within a single CPU thread.
In order for these instructions to appear to act correctly according to the guest's virtual SMT mode, we have to trap and emulate them. We cause them to trap by clearing the HFSCR_MSGP bit in the HFSCR register. The emulation is triggered by the hypervisor facility unavailable interrupt that occurs when the guest uses them.
To cause a doorbell interrupt to occur within the guest, we set the DPDES register to 1. If the guest has interrupts enabled, the CPU will generate a doorbell interrupt and clear the DPDES register in hardware. The DPDES hardware register for the guest is saved in the vcpu->arch.vcore->dpdes field. Since this gets written by the guest exit code, other VCPUs wishing to cause a doorbell interrupt don't write that field directly, but instead set a vcpu->arch.doorbell_request flag. This is consumed and set to 0 by the guest entry code, which then sets DPDES to 1.
Emulating reads of the DPDES register is somewhat involved, because it requires reading the doorbell pending interrupt status of all of the VCPU threads in the virtual core, and if any of those VCPUs are running, their doorbell status is only up-to-date in the hardware DPDES registers of the CPUs where they are running. In order to get a reasonable approximation of the current doorbell status, we send those CPUs an IPI, causing an exit from the guest which will update the vcpu->arch.vcore->dpdes field. We then use that value in constructing the emulated DPDES register value.
--- arch/powerpc/include/asm/kvm_host.h | 2 + arch/powerpc/include/asm/ppc-opcode.h | 2 + arch/powerpc/kernel/asm-offsets.c | 1 + arch/powerpc/kvm/book3s_hv.c | 133 ++++++++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_hv_rmhandlers.S | 17 ++++ arch/powerpc/kvm/powerpc.c | 9 ++- 6 files changed, 153 insertions(+), 11 deletions(-)
arch/powerpc/include/asm/kvm_host.h +2 -0
@@ -268,6 +268,7 @@ struct kvm_resize_hpt;struct kvm_arch {unsigned int lpid;unsigned int smt_mode;/* # vcpus per virtual core */ +unsigned int emul_smt_mode;/* emualted SMT mode, on P9 */ #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE unsigned int tlb_sets;struct kvm_hpt_info hpt;
@@ -712,6 +713,7 @@ struct kvm_vcpu_arch {unsigned long pending_exceptions;u8 ceded;u8 prodded; +u8 doorbell_request;u32 last_inst;struct swait_queue_head *wqp;
arch/powerpc/include/asm/ppc-opcode.h +2 -0
@@ -103,6 +103,8 @@#define OP_31_XOP_STBUX 247#define OP_31_XOP_LHZX 279#define OP_31_XOP_LHZUX 311 +#define OP_31_XOP_MSGSNDP 142 +#define OP_31_XOP_MSGCLRP 174#define OP_31_XOP_MFSPR 339#define OP_31_XOP_LWAX 341#define OP_31_XOP_LHAX 343
arch/powerpc/kernel/asm-offsets.c +1 -0
@@ -513,6 +513,7 @@ int main(void)OFFSET (VCPU_PENDING_EXC , kvm_vcpu, arch.pending_exceptions);OFFSET (VCPU_CEDED , kvm_vcpu, arch.ceded);OFFSET (VCPU_PRODDED , kvm_vcpu, arch.prodded); +OFFSET (VCPU_DBELL_REQ , kvm_vcpu, arch.doorbell_request);OFFSET (VCPU_MMCR , kvm_vcpu, arch.mmcr);OFFSET (VCPU_PMC , kvm_vcpu, arch.pmc);OFFSET (VCPU_SPMC , kvm_vcpu, arch.spmc);
arch/powerpc/kvm/book3s_hv.c +125 -8
199 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_rmhandlers.S +17 -0
@@ -1051,6 +1051,23 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300) mr r9, r4 bl kvmppc_msr_interrupt 5: +BEGIN_FTR_SECTION + b fast_guest_return +END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300) + /* On POWER9, check for pending doorbell requests */ + lbz r0, VCPU_DBELL_REQ(r4) + cmpwi r0, 0 + beq fast_guest_return + ld r5, HSTATE_KVM_VCORE(r13) + /* Set DPDES register so the CPU will take a doorbell interrupt */ + li r0, 1 + mtspr SPRN_DPDES, r0 + std r0, VCORE_DPDES(r5) + /* Make sure other cpus see vcore->dpdes set before dbell req clear */ + lwsync + /* Clear the pending doorbell request */ + li r0, 0 + stb r0, VCPU_DBELL_REQ(r4) /* * Required state:
arch/powerpc/kvm/powerpc.c +6 -3
@@ -554,9 +554,12 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE case KVM_CAP_PPC_SMT : r =0 ; -if (kvm) - r = kvm->arch.smt_mode; -else if (hv_enabled) { +if (kvm) { +if (kvm->arch.emul_smt_mode >1 ) + r = kvm->arch.emul_smt_mode; +else + r = kvm->arch.smt_mode; + }else if (hv_enabled) {if (cpu_has_feature (CPU_FTR_ARCH_300 )) r =1 ;else
Signature · 1 line
2.7.4