Skip to content

Commit 371fefd

Browse files
paulusmackavikivity
authored andcommitted
KVM: PPC: Allow book3s_hv guests to use SMT processor modes
This lifts the restriction that book3s_hv guests can only run one hardware thread per core, and allows them to use up to 4 threads per core on POWER7. The host still has to run single-threaded. This capability is advertised to qemu through a new KVM_CAP_PPC_SMT capability. The return value of the ioctl querying this capability is the number of vcpus per virtual CPU core (vcore), currently 4. To use this, the host kernel should be booted with all threads active, and then all the secondary threads should be offlined. This will put the secondary threads into nap mode. KVM will then wake them from nap mode and use them for running guest code (while they are still offline). To wake the secondary threads, we send them an IPI using a new xics_wake_cpu() function, implemented in arch/powerpc/sysdev/xics/icp-native.c. In other words, at this stage we assume that the platform has a XICS interrupt controller and we are using icp-native.c to drive it. Since the woken thread will need to acknowledge and clear the IPI, we also export the base physical address of the XICS registers using kvmppc_set_xics_phys() for use in the low-level KVM book3s code. When a vcpu is created, it is assigned to a virtual CPU core. The vcore number is obtained by dividing the vcpu number by the number of threads per core in the host. This number is exported to userspace via the KVM_CAP_PPC_SMT capability. If qemu wishes to run the guest in single-threaded mode, it should make all vcpu numbers be multiples of the number of threads per core. We distinguish three states of a vcpu: runnable (i.e., ready to execute the guest), blocked (that is, idle), and busy in host. We currently implement a policy that the vcore can run only when all its threads are runnable or blocked. This way, if a vcpu needs to execute elsewhere in the kernel or in qemu, it can do so without being starved of CPU by the other vcpus. When a vcore starts to run, it executes in the context of one of the vcpu threads. The other vcpu threads all go to sleep and stay asleep until something happens requiring the vcpu thread to return to qemu, or to wake up to run the vcore (this can happen when another vcpu thread goes from busy in host state to blocked). It can happen that a vcpu goes from blocked to runnable state (e.g. because of an interrupt), and the vcore it belongs to is already running. In that case it can start to run immediately as long as the none of the vcpus in the vcore have started to exit the guest. We send the next free thread in the vcore an IPI to get it to start to execute the guest. It synchronizes with the other threads via the vcore->entry_exit_count field to make sure that it doesn't go into the guest if the other vcpus are exiting by the time that it is ready to actually enter the guest. Note that there is no fixed relationship between the hardware thread number and the vcpu number. Hardware threads are assigned to vcpus as they become runnable, so we will always use the lower-numbered hardware threads in preference to higher-numbered threads if not all the vcpus in the vcore are runnable, regardless of which vcpus are runnable. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Alexander Graf <agraf@suse.de>
1 parent 54738c0 commit 371fefd

13 files changed

Lines changed: 567 additions & 45 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ KVM_CHECK_EXTENSION ioctl() to determine the value for max_vcpus at run-time.
180180
If the KVM_CAP_NR_VCPUS does not exist, you should assume that max_vcpus is 4
181181
cpus max.
182182

183+
On powerpc using book3s_hv mode, the vcpus are mapped onto virtual
184+
threads in one or more virtual CPU cores. (This is because the
185+
hardware requires all the hardware threads in a CPU core to be in the
186+
same partition.) The KVM_CAP_PPC_SMT capability indicates the number
187+
of vcpus per virtual core (vcore). The vcore id is obtained by
188+
dividing the vcpu id by the number of vcpus per vcore. The vcpus in a
189+
given vcore will always be in the same physical core as each other
190+
(though that might be a different physical core from time to time).
191+
Userspace can control the threading (SMT) mode of the guest by its
192+
allocation of vcpu ids. For example, if userspace wants
193+
single-threaded guest vcpus, it should make all vcpu ids be a multiple
194+
of the number of vcpus per vcore.
195+
183196
4.8 KVM_GET_DIRTY_LOG (vm ioctl)
184197

185198
Capability: basic

‎arch/powerpc/include/asm/kvm.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/* Select powerpc specific features in <linux/kvm.h> */
2626
#define __KVM_HAVE_SPAPR_TCE
27+
#define __KVM_HAVE_PPC_SMT
2728

2829
struct kvm_regs {
2930
__u64 pc;

‎arch/powerpc/include/asm/kvm_book3s_asm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ struct kvmppc_host_state {
7878

7979
#ifdef CONFIG_KVM_BOOK3S_64_HV
8080
struct kvm_vcpu *kvm_vcpu;
81+
struct kvmppc_vcore *kvm_vcore;
82+
unsigned long xics_phys;
8183
u64 dabr;
8284
u64 host_mmcr[3];
8385
u32 host_pmc[6];

‎arch/powerpc/include/asm/kvm_host.h‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@
2525
#include <linux/interrupt.h>
2626
#include <linux/types.h>
2727
#include <linux/kvm_types.h>
28+
#include <linux/threads.h>
29+
#include <linux/spinlock.h>
2830
#include <linux/kvm_para.h>
2931
#include <asm/kvm_asm.h>
32+
#include <asm/processor.h>
3033

31-
#define KVM_MAX_VCPUS 1
34+
#define KVM_MAX_VCPUS NR_CPUS
35+
#define KVM_MAX_VCORES NR_CPUS
3236
#define KVM_MEMORY_SLOTS 32
3337
/* memory slots that does not exposed to userspace */
3438
#define KVM_PRIVATE_MEM_SLOTS 4
@@ -167,9 +171,34 @@ struct kvm_arch {
167171
int tlbie_lock;
168172
struct list_head spapr_tce_tables;
169173
unsigned short last_vcpu[NR_CPUS];
174+
struct kvmppc_vcore *vcores[KVM_MAX_VCORES];
170175
#endif /* CONFIG_KVM_BOOK3S_64_HV */
171176
};
172177

178+
/*
179+
* Struct for a virtual core.
180+
* Note: entry_exit_count combines an entry count in the bottom 8 bits
181+
* and an exit count in the next 8 bits. This is so that we can
182+
* atomically increment the entry count iff the exit count is 0
183+
* without taking the lock.
184+
*/
185+
struct kvmppc_vcore {
186+
int n_runnable;
187+
int n_blocked;
188+
int num_threads;
189+
int entry_exit_count;
190+
int n_woken;
191+
int nap_count;
192+
u16 pcpu;
193+
u8 vcore_running;
194+
u8 in_guest;
195+
struct list_head runnable_threads;
196+
spinlock_t lock;
197+
};
198+
199+
#define VCORE_ENTRY_COUNT(vc) ((vc)->entry_exit_count & 0xff)
200+
#define VCORE_EXIT_COUNT(vc) ((vc)->entry_exit_count >> 8)
201+
173202
struct kvmppc_pte {
174203
ulong eaddr;
175204
u64 vpage;
@@ -365,14 +394,29 @@ struct kvm_vcpu_arch {
365394
struct slb_shadow *slb_shadow;
366395
struct dtl *dtl;
367396
struct dtl *dtl_end;
397+
398+
struct kvmppc_vcore *vcore;
399+
int ret;
368400
int trap;
401+
int state;
402+
int ptid;
403+
wait_queue_head_t cpu_run;
404+
369405
struct kvm_vcpu_arch_shared *shared;
370406
unsigned long magic_page_pa; /* phys addr to map the magic page to */
371407
unsigned long magic_page_ea; /* effect. addr to map the magic page to */
372408

373409
#ifdef CONFIG_KVM_BOOK3S_64_HV
374410
struct kvm_vcpu_arch_shared shregs;
411+
412+
struct list_head run_list;
413+
struct task_struct *run_task;
414+
struct kvm_run *kvm_run;
375415
#endif
376416
};
377417

418+
#define KVMPPC_VCPU_BUSY_IN_HOST 0
419+
#define KVMPPC_VCPU_BLOCKED 1
420+
#define KVMPPC_VCPU_RUNNABLE 2
421+
378422
#endif /* __POWERPC_KVM_HOST_H__ */

‎arch/powerpc/include/asm/kvm_ppc.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#else
3434
#include <asm/kvm_booke.h>
3535
#endif
36+
#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
37+
#include <asm/paca.h>
38+
#endif
3639

3740
enum emulation_result {
3841
EMULATE_DONE, /* no further processing */
@@ -169,4 +172,14 @@ int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
169172

170173
void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
171174

175+
#ifdef CONFIG_KVM_BOOK3S_64_HV
176+
static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
177+
{
178+
paca[cpu].kvm_hstate.xics_phys = addr;
179+
}
180+
#else
181+
static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
182+
{}
183+
#endif
184+
172185
#endif /* __POWERPC_KVM_PPC_H__ */

‎arch/powerpc/kernel/asm-offsets.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ int main(void)
471471
DEFINE(VCPU_FAULT_DAR, offsetof(struct kvm_vcpu, arch.fault_dar));
472472
DEFINE(VCPU_LAST_INST, offsetof(struct kvm_vcpu, arch.last_inst));
473473
DEFINE(VCPU_TRAP, offsetof(struct kvm_vcpu, arch.trap));
474+
DEFINE(VCPU_PTID, offsetof(struct kvm_vcpu, arch.ptid));
475+
DEFINE(VCORE_ENTRY_EXIT, offsetof(struct kvmppc_vcore, entry_exit_count));
476+
DEFINE(VCORE_NAP_COUNT, offsetof(struct kvmppc_vcore, nap_count));
477+
DEFINE(VCORE_IN_GUEST, offsetof(struct kvmppc_vcore, in_guest));
474478
DEFINE(VCPU_SVCPU, offsetof(struct kvmppc_vcpu_book3s, shadow_vcpu) -
475479
offsetof(struct kvmppc_vcpu_book3s, vcpu));
476480
DEFINE(VCPU_SLB_E, offsetof(struct kvmppc_slb, orige));
@@ -530,6 +534,8 @@ int main(void)
530534

531535
#ifdef CONFIG_KVM_BOOK3S_64_HV
532536
HSTATE_FIELD(HSTATE_KVM_VCPU, kvm_vcpu);
537+
HSTATE_FIELD(HSTATE_KVM_VCORE, kvm_vcore);
538+
HSTATE_FIELD(HSTATE_XICS_PHYS, xics_phys);
533539
HSTATE_FIELD(HSTATE_MMCR, host_mmcr);
534540
HSTATE_FIELD(HSTATE_PMC, host_pmc);
535541
HSTATE_FIELD(HSTATE_PURR, host_purr);

‎arch/powerpc/kernel/exceptions-64s.S‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,32 @@ BEGIN_FTR_SECTION
4949
* state loss at this time.
5050
*/
5151
mfspr r13,SPRN_SRR1
52-
rlwinm r13,r13,47-31,30,31
53-
cmpwi cr0,r13,1
54-
bne 1f
55-
b .power7_wakeup_noloss
56-
1: cmpwi cr0,r13,2
57-
bne 1f
58-
b .power7_wakeup_loss
52+
rlwinm. r13,r13,47-31,30,31
53+
beq 9f
54+
55+
/* waking up from powersave (nap) state */
56+
cmpwi cr1,r13,2
5957
/* Total loss of HV state is fatal, we could try to use the
6058
* PIR to locate a PACA, then use an emergency stack etc...
6159
* but for now, let's just stay stuck here
6260
*/
63-
1: cmpwi cr0,r13,3
64-
beq .
61+
bgt cr1,.
62+
GET_PACA(r13)
63+
64+
#ifdef CONFIG_KVM_BOOK3S_64_HV
65+
lbz r0,PACAPROCSTART(r13)
66+
cmpwi r0,0x80
67+
bne 1f
68+
li r0,0
69+
stb r0,PACAPROCSTART(r13)
70+
b kvm_start_guest
71+
1:
72+
#endif
73+
74+
beq cr1,2f
75+
b .power7_wakeup_noloss
76+
2: b .power7_wakeup_loss
77+
9:
6578
END_FTR_SECTION_IFSET(CPU_FTR_HVMODE_206)
6679
#endif /* CONFIG_PPC_P7_NAP */
6780
EXCEPTION_PROLOG_PSERIES(PACA_EXGEN, system_reset_common, EXC_STD,

‎arch/powerpc/kernel/idle_power7.S‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ _GLOBAL(power7_idle)
7373
b .
7474

7575
_GLOBAL(power7_wakeup_loss)
76-
GET_PACA(r13)
7776
ld r1,PACAR1(r13)
7877
REST_NVGPRS(r1)
7978
REST_GPR(2, r1)
@@ -87,7 +86,6 @@ _GLOBAL(power7_wakeup_loss)
8786
rfid
8887

8988
_GLOBAL(power7_wakeup_noloss)
90-
GET_PACA(r13)
9189
ld r1,PACAR1(r13)
9290
ld r4,_MSR(r1)
9391
ld r5,_NIP(r1)

0 commit comments

Comments
 (0)