Sean Christopherson <[email protected]> writes:

> On Wed, Jun 10, 2026, Ritesh Harjani (IBM) wrote:
>> From: Nicholas Piggin <[email protected]>
>> 
>> powerpc's maximum permitted vCPU ID depends on the VM's SMT mode, and
>> the maximum reported by KVM_CAP_MAX_VCPU_ID exceeds a simple non-SMT
>> VM's limit.
>> 
>> The powerpc KVM selftest port uses non-SMT VMs, so add a workaround
>> to the kvm_create_max_vcpus test case to limit vCPU IDs to
>> KVM_CAP_MAX_VCPUS on powerpc.
>
> How is this not a KVM bug?  Literally the reason this test exists is to 
> validate
> KVM's advertised KVM_CAP_MAX_VCPU_ID and KVM_CAP_MAX_VCPUS.

It's not a KVM bug, it's expected on PowerPC. On PowerPC, vCPU ID encodes SMT 
topology, e.g. on P9,
vcpu id = core * stride + thread,
  .. where the stride is same as kvm->arch.emul_smt_mode (VM's emulated SMT 
mode)

So the vcpu ID space can be sparse, however KVM_CAP_MAX_VCPU_ID is the
absolute ceil value (MAX_SMT_THREADS * KVM_MAX_VCORES) i.e. the value
with the maximum stride / SMT value.

Since default selftest VM uses stride 1, so it rejects IDS >= max_vcpus.

e.g.
static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu)
...
                if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) {
                        pr_devel("KVM: VCPU ID too high\n");
                        core = KVM_MAX_VCORES;              /* rejected case */
                } else {

So, it's expected on PowerPC. vcpus with higher IDs can be created but
for that we need to set KVM_CAP_PPC_SMT and use strided (sparse) IDs.
But since the test as of now is not doing that - that's the reason why
the patch only allows to test max vcpu IDs upto max vcpus.



But I guess you must be hating the #ifdef __powerpc__ there. I agree I
don't like it either.. maybe we can do it this way?


-#ifdef __powerpc64__
        /*
-        * powerpc has a particular format for the vcpu ID that depends on
-        * the guest SMT mode, and the max ID cap is too large for non-SMT
-        * modes, where the maximum ID is the same as the maximum vCPUs.
+        * Some architectures (e.g. powerpc) encode topology into the vCPU ID,
+        * so a default VM can't necessarily use the full advertised ID range.
+        * Let the arch limit the highest ID this test will create.
         */
-       kvm_max_vcpu_id = kvm_max_vcpus;
-#endif
+       kvm_max_vcpu_id = kvm_arch_vcpu_id_limit(kvm_max_vcpus, 
kvm_max_vcpu_id);


And then in kvm_util.c -
+
+__weak int kvm_arch_vcpu_id_limit(int nr_vcpus, int vcpu_id_max)
+{
+       return vcpu_id_max;
+}

and lib/powerpc/processor.c can define -

+
+int kvm_arch_vcpu_id_limit(int nr_vcpus, int vcpu_id_max)
+{
+       /*
+        * The stride is the default SMT mode from KVM_CAP_PPC_SMT (1 on
+        * POWER9+, the host's threads-per-subcore on POWER8) and is always <=
+        * MAX_SMT_THREADS, so the result never exceeds KVM_CAP_MAX_VCPU_ID.
+        *
+        * TODO: exercising the higher (sparse) ID range would require setting
+        * KVM_CAP_PPC_SMT and creating strided vCPU IDs.
+        */
+
+       int stride = kvm_check_cap(KVM_CAP_PPC_SMT);
+
+       return nr_vcpus * (stride > 0 ? stride : 1);
+}


If this looks good, then I can re-spin a newer version with the
following changes:

1. Use above logic in Patch-4 for kvm_create_max_vcpus_test instead of
   hardcoded #ifdef __powerpc__ logic.

2. Drop patch-5 (print vcpu_id) debug patch

3. Squash all the type specifier changes i.e. Patch 6-10 in the main
   Patch-3, the patch which adds kvm selftests support for powerpc.


-ritesh

Reply via email to