Skip to content

Commit 062cfab

Browse files
gkurzpaulusmack
authored andcommitted
KVM: PPC: Book3S HV: XIVE: Make VP block size configurable
The XIVE VP is an internal structure which allow the XIVE interrupt controller to maintain the interrupt context state of vCPUs non dispatched on HW threads. When a guest is started, the XIVE KVM device allocates a block of XIVE VPs in OPAL, enough to accommodate the highest possible vCPU id KVM_MAX_VCPU_ID (16384) packed down to KVM_MAX_VCPUS (2048). With a guest's core stride of 8 and a threading mode of 1 (QEMU's default), a VM must run at least 256 vCPUs to actually need such a range of VPs. A POWER9 system has a limited XIVE VP space : 512k and KVM is currently wasting this HW resource with large VP allocations, especially since a typical VM likely runs with a lot less vCPUs. Make the size of the VP block configurable. Add an nr_servers field to the XIVE structure and a function to set it for this purpose. Split VP allocation out of the device create function. Since the VP block isn't used before the first vCPU connects to the XIVE KVM device, allocation is now performed by kvmppc_xive_connect_vcpu(). This gives the opportunity to set nr_servers in between: kvmppc_xive_create() / kvmppc_xive_native_create() . . kvmppc_xive_set_nr_servers() . . kvmppc_xive_connect_vcpu() / kvmppc_xive_native_connect_vcpu() The connect_vcpu() functions check that the vCPU id is below nr_servers and if it is the first vCPU they allocate the VP block. This is protected against a concurrent update of nr_servers by kvmppc_xive_set_nr_servers() with the xive->lock mutex. Also, the block is allocated once for the device lifetime: nr_servers should stay constant otherwise connect_vcpu() could generate a boggus VP id and likely crash OPAL. It is thus forbidden to update nr_servers once the block is allocated. If the VP allocation fail, return ENOSPC which seems more appropriate to report the depletion of system wide HW resource than ENOMEM or ENXIO. A VM using a stride of 8 and 1 thread per core with 32 vCPUs would hence only need 256 VPs instead of 2048. If the stride is set to match the number of threads per core, this goes further down to 32. This will be exposed to userspace by a subsequent patch. Signed-off-by: Greg Kurz <groug@kaod.org> Reviewed-by: Cédric Le Goater <clg@kaod.org> Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
1 parent 8db29ea commit 062cfab

3 files changed

Lines changed: 62 additions & 25 deletions

File tree

‎arch/powerpc/kvm/book3s_xive.c‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,13 +1213,13 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu)
12131213

12141214
static bool kvmppc_xive_vcpu_id_valid(struct kvmppc_xive *xive, u32 cpu)
12151215
{
1216-
/* We have a block of KVM_MAX_VCPUS VPs. We just need to check
1216+
/* We have a block of xive->nr_servers VPs. We just need to check
12171217
* raw vCPU ids are below the expected limit for this guest's
12181218
* core stride ; kvmppc_pack_vcpu_id() will pack them down to an
12191219
* index that can be safely used to compute a VP id that belongs
12201220
* to the VP block.
12211221
*/
1222-
return cpu < KVM_MAX_VCPUS * xive->kvm->arch.emul_smt_mode;
1222+
return cpu < xive->nr_servers * xive->kvm->arch.emul_smt_mode;
12231223
}
12241224

12251225
int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp)
@@ -1231,6 +1231,14 @@ int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp)
12311231
return -EINVAL;
12321232
}
12331233

1234+
if (xive->vp_base == XIVE_INVALID_VP) {
1235+
xive->vp_base = xive_native_alloc_vp_block(xive->nr_servers);
1236+
pr_devel("VP_Base=%x nr_servers=%d\n", xive->vp_base, xive->nr_servers);
1237+
1238+
if (xive->vp_base == XIVE_INVALID_VP)
1239+
return -ENOSPC;
1240+
}
1241+
12341242
vp_id = kvmppc_xive_vp(xive, cpu);
12351243
if (kvmppc_xive_vp_in_use(xive->kvm, vp_id)) {
12361244
pr_devel("Duplicate !\n");
@@ -1858,6 +1866,43 @@ int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
18581866
return 0;
18591867
}
18601868

1869+
int kvmppc_xive_set_nr_servers(struct kvmppc_xive *xive, u64 addr)
1870+
{
1871+
u32 __user *ubufp = (u32 __user *) addr;
1872+
u32 nr_servers;
1873+
int rc = 0;
1874+
1875+
if (get_user(nr_servers, ubufp))
1876+
return -EFAULT;
1877+
1878+
pr_devel("%s nr_servers=%u\n", __func__, nr_servers);
1879+
1880+
if (!nr_servers || nr_servers > KVM_MAX_VCPU_ID)
1881+
return -EINVAL;
1882+
1883+
mutex_lock(&xive->lock);
1884+
if (xive->vp_base != XIVE_INVALID_VP)
1885+
/* The VP block is allocated once and freed when the device
1886+
* is released. Better not allow to change its size since its
1887+
* used by connect_vcpu to validate vCPU ids are valid (eg,
1888+
* setting it back to a higher value could allow connect_vcpu
1889+
* to come up with a VP id that goes beyond the VP block, which
1890+
* is likely to cause a crash in OPAL).
1891+
*/
1892+
rc = -EBUSY;
1893+
else if (nr_servers > KVM_MAX_VCPUS)
1894+
/* We don't need more servers. Higher vCPU ids get packed
1895+
* down below KVM_MAX_VCPUS by kvmppc_pack_vcpu_id().
1896+
*/
1897+
xive->nr_servers = KVM_MAX_VCPUS;
1898+
else
1899+
xive->nr_servers = nr_servers;
1900+
1901+
mutex_unlock(&xive->lock);
1902+
1903+
return rc;
1904+
}
1905+
18611906
static int xive_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
18621907
{
18631908
struct kvmppc_xive *xive = dev->private;
@@ -2025,7 +2070,6 @@ static int kvmppc_xive_create(struct kvm_device *dev, u32 type)
20252070
{
20262071
struct kvmppc_xive *xive;
20272072
struct kvm *kvm = dev->kvm;
2028-
int ret = 0;
20292073

20302074
pr_devel("Creating xive for partition\n");
20312075

@@ -2049,18 +2093,15 @@ static int kvmppc_xive_create(struct kvm_device *dev, u32 type)
20492093
else
20502094
xive->q_page_order = xive->q_order - PAGE_SHIFT;
20512095

2052-
/* Allocate a bunch of VPs */
2053-
xive->vp_base = xive_native_alloc_vp_block(KVM_MAX_VCPUS);
2054-
pr_devel("VP_Base=%x\n", xive->vp_base);
2055-
2056-
if (xive->vp_base == XIVE_INVALID_VP)
2057-
ret = -ENOMEM;
2096+
/* VP allocation is delayed to the first call to connect_vcpu */
2097+
xive->vp_base = XIVE_INVALID_VP;
2098+
/* KVM_MAX_VCPUS limits the number of VMs to roughly 64 per sockets
2099+
* on a POWER9 system.
2100+
*/
2101+
xive->nr_servers = KVM_MAX_VCPUS;
20582102

20592103
xive->single_escalation = xive_native_has_single_escalation();
20602104

2061-
if (ret)
2062-
return ret;
2063-
20642105
kvm->arch.xive = xive;
20652106
return 0;
20662107
}

‎arch/powerpc/kvm/book3s_xive.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ struct kvmppc_xive {
135135
/* Flags */
136136
u8 single_escalation;
137137

138+
/* Number of entries in the VP block */
139+
u32 nr_servers;
140+
138141
struct kvmppc_xive_ops *ops;
139142
struct address_space *mapping;
140143
struct mutex mapping_lock;
@@ -297,6 +300,7 @@ struct kvmppc_xive *kvmppc_xive_get_device(struct kvm *kvm, u32 type);
297300
void xive_cleanup_single_escalation(struct kvm_vcpu *vcpu,
298301
struct kvmppc_xive_vcpu *xc, int irq);
299302
int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp);
303+
int kvmppc_xive_set_nr_servers(struct kvmppc_xive *xive, u64 addr);
300304

301305
#endif /* CONFIG_KVM_XICS */
302306
#endif /* _KVM_PPC_BOOK3S_XICS_H */

‎arch/powerpc/kvm/book3s_xive_native.c‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,6 @@ static int kvmppc_xive_native_create(struct kvm_device *dev, u32 type)
10601060
{
10611061
struct kvmppc_xive *xive;
10621062
struct kvm *kvm = dev->kvm;
1063-
int ret = 0;
10641063

10651064
pr_devel("Creating xive native device\n");
10661065

@@ -1077,23 +1076,16 @@ static int kvmppc_xive_native_create(struct kvm_device *dev, u32 type)
10771076
mutex_init(&xive->mapping_lock);
10781077
mutex_init(&xive->lock);
10791078

1080-
/*
1081-
* Allocate a bunch of VPs. KVM_MAX_VCPUS is a large value for
1082-
* a default. Getting the max number of CPUs the VM was
1083-
* configured with would improve our usage of the XIVE VP space.
1079+
/* VP allocation is delayed to the first call to connect_vcpu */
1080+
xive->vp_base = XIVE_INVALID_VP;
1081+
/* KVM_MAX_VCPUS limits the number of VMs to roughly 64 per sockets
1082+
* on a POWER9 system.
10841083
*/
1085-
xive->vp_base = xive_native_alloc_vp_block(KVM_MAX_VCPUS);
1086-
pr_devel("VP_Base=%x\n", xive->vp_base);
1087-
1088-
if (xive->vp_base == XIVE_INVALID_VP)
1089-
ret = -ENXIO;
1084+
xive->nr_servers = KVM_MAX_VCPUS;
10901085

10911086
xive->single_escalation = xive_native_has_single_escalation();
10921087
xive->ops = &kvmppc_xive_native_ops;
10931088

1094-
if (ret)
1095-
return ret;
1096-
10971089
kvm->arch.xive = xive;
10981090
return 0;
10991091
}

0 commit comments

Comments
 (0)