Skip to content

Commit 8db29ea

Browse files
gkurzpaulusmack
authored andcommitted
KVM: PPC: Book3S HV: XIVE: Compute the VP id in a common helper
Reduce code duplication by consolidating the checking of vCPU ids and VP ids to a common helper used by both legacy and native XIVE KVM devices. And explain the magic with a comment. Signed-off-by: Greg Kurz <groug@kaod.org> Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
1 parent 8a4e759 commit 8db29ea

3 files changed

Lines changed: 36 additions & 18 deletions

File tree

‎arch/powerpc/kvm/book3s_xive.c‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,37 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu)
12111211
vcpu->arch.xive_vcpu = NULL;
12121212
}
12131213

1214+
static bool kvmppc_xive_vcpu_id_valid(struct kvmppc_xive *xive, u32 cpu)
1215+
{
1216+
/* We have a block of KVM_MAX_VCPUS VPs. We just need to check
1217+
* raw vCPU ids are below the expected limit for this guest's
1218+
* core stride ; kvmppc_pack_vcpu_id() will pack them down to an
1219+
* index that can be safely used to compute a VP id that belongs
1220+
* to the VP block.
1221+
*/
1222+
return cpu < KVM_MAX_VCPUS * xive->kvm->arch.emul_smt_mode;
1223+
}
1224+
1225+
int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp)
1226+
{
1227+
u32 vp_id;
1228+
1229+
if (!kvmppc_xive_vcpu_id_valid(xive, cpu)) {
1230+
pr_devel("Out of bounds !\n");
1231+
return -EINVAL;
1232+
}
1233+
1234+
vp_id = kvmppc_xive_vp(xive, cpu);
1235+
if (kvmppc_xive_vp_in_use(xive->kvm, vp_id)) {
1236+
pr_devel("Duplicate !\n");
1237+
return -EEXIST;
1238+
}
1239+
1240+
*vp = vp_id;
1241+
1242+
return 0;
1243+
}
1244+
12141245
int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
12151246
struct kvm_vcpu *vcpu, u32 cpu)
12161247
{
@@ -1229,20 +1260,13 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
12291260
return -EPERM;
12301261
if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
12311262
return -EBUSY;
1232-
if (cpu >= (KVM_MAX_VCPUS * vcpu->kvm->arch.emul_smt_mode)) {
1233-
pr_devel("Out of bounds !\n");
1234-
return -EINVAL;
1235-
}
12361263

12371264
/* We need to synchronize with queue provisioning */
12381265
mutex_lock(&xive->lock);
12391266

1240-
vp_id = kvmppc_xive_vp(xive, cpu);
1241-
if (kvmppc_xive_vp_in_use(xive->kvm, vp_id)) {
1242-
pr_devel("Duplicate !\n");
1243-
r = -EEXIST;
1267+
r = kvmppc_xive_compute_vp_id(xive, cpu, &vp_id);
1268+
if (r)
12441269
goto bail;
1245-
}
12461270

12471271
xc = kzalloc(sizeof(*xc), GFP_KERNEL);
12481272
if (!xc) {

‎arch/powerpc/kvm/book3s_xive.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ int kvmppc_xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio,
296296
struct kvmppc_xive *kvmppc_xive_get_device(struct kvm *kvm, u32 type);
297297
void xive_cleanup_single_escalation(struct kvm_vcpu *vcpu,
298298
struct kvmppc_xive_vcpu *xc, int irq);
299+
int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp);
299300

300301
#endif /* CONFIG_KVM_XICS */
301302
#endif /* _KVM_PPC_BOOK3S_XICS_H */

‎arch/powerpc/kvm/book3s_xive_native.c‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,12 @@ int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
118118
return -EPERM;
119119
if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
120120
return -EBUSY;
121-
if (server_num >= (KVM_MAX_VCPUS * vcpu->kvm->arch.emul_smt_mode)) {
122-
pr_devel("Out of bounds !\n");
123-
return -EINVAL;
124-
}
125121

126122
mutex_lock(&xive->lock);
127123

128-
vp_id = kvmppc_xive_vp(xive, server_num);
129-
if (kvmppc_xive_vp_in_use(xive->kvm, vp_id)) {
130-
pr_devel("Duplicate !\n");
131-
rc = -EEXIST;
124+
rc = kvmppc_xive_compute_vp_id(xive, server_num, &vp_id);
125+
if (rc)
132126
goto bail;
133-
}
134127

135128
xc = kzalloc(sizeof(*xc), GFP_KERNEL);
136129
if (!xc) {

0 commit comments

Comments
 (0)