Skip to content

Commit 3c31352

Browse files
committed
KVM: PPC: Book3S HV: Allow userspace to set the desired SMT mode
This 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. Signed-off-by: Paul Mackerras <[email protected]>
1 parent 769377f commit 3c31352

File tree

5 files changed

+90
-12
lines changed

5 files changed

+90
-12
lines changed

Documentation/virtual/kvm/api.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3996,6 +3996,21 @@ Parameters: none
39963996
Allow use of adapter-interruption suppression.
39973997
Returns: 0 on success; -EBUSY if a VCPU has already been created.
39983998

3999+
7.11 KVM_CAP_PPC_SMT
4000+
4001+
Architectures: ppc
4002+
Parameters: vsmt_mode, flags
4003+
4004+
Enabling this capability on a VM provides userspace with a way to set
4005+
the desired virtual SMT mode (i.e. the number of virtual CPUs per
4006+
virtual core). The virtual SMT mode, vsmt_mode, must be a power of 2
4007+
between 1 and 8. On POWER8, vsmt_mode must also be no greater than
4008+
the number of threads per subcore for the host. Currently flags must
4009+
be 0. A successful call to enable this capability will result in
4010+
vsmt_mode being returned when the KVM_CAP_PPC_SMT capability is
4011+
subsequently queried for the VM. This capability is only supported by
4012+
HV KVM, and can only be set before any VCPUs have been created.
4013+
39994014
8. Other capabilities.
40004015
----------------------
40014016

arch/powerpc/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ struct kvm_resize_hpt;
267267

268268
struct kvm_arch {
269269
unsigned int lpid;
270+
unsigned int smt_mode; /* # vcpus per virtual core */
270271
#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
271272
unsigned int tlb_sets;
272273
struct kvm_hpt_info hpt;

arch/powerpc/include/asm/kvm_ppc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ struct kvmppc_ops {
315315
struct irq_bypass_producer *);
316316
int (*configure_mmu)(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg);
317317
int (*get_rmmu_info)(struct kvm *kvm, struct kvm_ppc_rmmu_info *info);
318+
int (*set_smt_mode)(struct kvm *kvm, unsigned long mode,
319+
unsigned long flags);
318320
};
319321

320322
extern struct kvmppc_ops *kvmppc_hv_ops;

arch/powerpc/kvm/book3s_hv.c

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
16281628
init_swait_queue_head(&vcore->wq);
16291629
vcore->preempt_tb = TB_NIL;
16301630
vcore->lpcr = kvm->arch.lpcr;
1631-
vcore->first_vcpuid = core * threads_per_vcore();
1631+
vcore->first_vcpuid = core * kvm->arch.smt_mode;
16321632
vcore->kvm = kvm;
16331633
INIT_LIST_HEAD(&vcore->preempt_list);
16341634

@@ -1787,14 +1787,10 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
17871787
unsigned int id)
17881788
{
17891789
struct kvm_vcpu *vcpu;
1790-
int err = -EINVAL;
1790+
int err;
17911791
int core;
17921792
struct kvmppc_vcore *vcore;
17931793

1794-
core = id / threads_per_vcore();
1795-
if (core >= KVM_MAX_VCORES)
1796-
goto out;
1797-
17981794
err = -ENOMEM;
17991795
vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
18001796
if (!vcpu)
@@ -1842,11 +1838,17 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
18421838
init_waitqueue_head(&vcpu->arch.cpu_run);
18431839

18441840
mutex_lock(&kvm->lock);
1845-
vcore = kvm->arch.vcores[core];
1846-
if (!vcore) {
1847-
vcore = kvmppc_vcore_create(kvm, core);
1848-
kvm->arch.vcores[core] = vcore;
1849-
kvm->arch.online_vcores++;
1841+
vcore = NULL;
1842+
err = -EINVAL;
1843+
core = id / kvm->arch.smt_mode;
1844+
if (core < KVM_MAX_VCORES) {
1845+
vcore = kvm->arch.vcores[core];
1846+
if (!vcore) {
1847+
err = -ENOMEM;
1848+
vcore = kvmppc_vcore_create(kvm, core);
1849+
kvm->arch.vcores[core] = vcore;
1850+
kvm->arch.online_vcores++;
1851+
}
18501852
}
18511853
mutex_unlock(&kvm->lock);
18521854

@@ -1874,6 +1876,40 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
18741876
return ERR_PTR(err);
18751877
}
18761878

1879+
static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
1880+
unsigned long flags)
1881+
{
1882+
int err;
1883+
1884+
if (flags)
1885+
return -EINVAL;
1886+
if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
1887+
return -EINVAL;
1888+
if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
1889+
/*
1890+
* On POWER8 (or POWER7), the threading mode is "strict",
1891+
* so we pack smt_mode vcpus per vcore.
1892+
*/
1893+
if (smt_mode > threads_per_subcore)
1894+
return -EINVAL;
1895+
} else {
1896+
/*
1897+
* On POWER9, the threading mode is "loose",
1898+
* so each vcpu gets its own vcore.
1899+
*/
1900+
smt_mode = 1;
1901+
}
1902+
mutex_lock(&kvm->lock);
1903+
err = -EBUSY;
1904+
if (!kvm->arch.online_vcores) {
1905+
kvm->arch.smt_mode = smt_mode;
1906+
err = 0;
1907+
}
1908+
mutex_unlock(&kvm->lock);
1909+
1910+
return err;
1911+
}
1912+
18771913
static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
18781914
{
18791915
if (vpa->pinned_addr)
@@ -3553,6 +3589,18 @@ static int kvmppc_core_init_vm_hv(struct kvm *kvm)
35533589
if (!cpu_has_feature(CPU_FTR_ARCH_300))
35543590
kvm_hv_vm_activated();
35553591

3592+
/*
3593+
* Initialize smt_mode depending on processor.
3594+
* POWER8 and earlier have to use "strict" threading, where
3595+
* all vCPUs in a vcore have to run on the same (sub)core,
3596+
* whereas on POWER9 the threads can each run a different
3597+
* guest.
3598+
*/
3599+
if (!cpu_has_feature(CPU_FTR_ARCH_300))
3600+
kvm->arch.smt_mode = threads_per_subcore;
3601+
else
3602+
kvm->arch.smt_mode = 1;
3603+
35563604
/*
35573605
* Create a debugfs directory for the VM
35583606
*/
@@ -3982,6 +4030,7 @@ static struct kvmppc_ops kvm_ops_hv = {
39824030
#endif
39834031
.configure_mmu = kvmhv_configure_mmu,
39844032
.get_rmmu_info = kvmhv_get_rmmu_info,
4033+
.set_smt_mode = kvmhv_set_smt_mode,
39854034
};
39864035

39874036
static int kvm_init_subcore_bitmap(void)

arch/powerpc/kvm/powerpc.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
554554
#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
555555
case KVM_CAP_PPC_SMT:
556556
r = 0;
557-
if (hv_enabled) {
557+
if (kvm)
558+
r = kvm->arch.smt_mode;
559+
else if (hv_enabled) {
558560
if (cpu_has_feature(CPU_FTR_ARCH_300))
559561
r = 1;
560562
else
@@ -1712,6 +1714,15 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
17121714
r = 0;
17131715
break;
17141716
}
1717+
case KVM_CAP_PPC_SMT: {
1718+
unsigned long mode = cap->args[0];
1719+
unsigned long flags = cap->args[1];
1720+
1721+
r = -EINVAL;
1722+
if (kvm->arch.kvm_ops->set_smt_mode)
1723+
r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
1724+
break;
1725+
}
17151726
#endif
17161727
default:
17171728
r = -EINVAL;

0 commit comments

Comments
 (0)