[PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive

From: Dmytro Maluka

Date: Fri Jul 31 2026 - 11:22:33 EST


KVM_CAP_MAX_VCPU_ID is (confusingly) exclusive, i.e. the maximum allowed
vcpu_id value is KVM_CAP_MAX_VCPU_ID minus one. So in particular,
setting bsp_vcpu_id to the value _equal_ to KVM_CAP_MAX_VCPU_ID is
invalid, it must be strictly below KVM_CAP_MAX_VCPU_ID. Whereas the
corresponding test in x86/max_vcpuid_cap_test mistakenly assumes that
it is valid and expects the ioctl to return success.

Fix this, by changing the "semantics" of the selftest's internal
MAX_VCPU_ID constant: let it represent the actual maximum vcpu_id, i.e.
the KVM_CAP_MAX_VCPU_ID value minus one.

Fixes: 4b451a57809c ("KVM: selftests: Test max vCPU IDs corner cases")
Signed-off-by: Dmytro Maluka <dmaluka@xxxxxxxxxxxx>
---
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
index 7e2bfb3c3f3b..47ee14967873 100644
--- a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
+++ b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
@@ -36,16 +36,19 @@ int main(int argc, char *argv[])
"Setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID should fail");
}

- /* Set KVM_CAP_MAX_VCPU_ID */
- vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID);
+ /*
+ * Set KVM_CAP_MAX_VCPU_ID. Note: KVM_CAP_MAX_VCPU_ID is a misnomer,
+ * it actually represents maximum vcpu_id plus one.
+ */
+ vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID + 1);

/* Try to set KVM_CAP_MAX_VCPU_ID again */
- ret = __vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID + 1);
+ ret = __vm_enable_cap(vm, KVM_CAP_MAX_VCPU_ID, MAX_VCPU_ID + 2);
TEST_ASSERT(ret < 0,
"Setting KVM_CAP_MAX_VCPU_ID multiple times should fail");

/* Create vCPU with id beyond KVM_CAP_MAX_VCPU_ID cap */
- ret = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)MAX_VCPU_ID);
+ ret = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)(MAX_VCPU_ID + 1));
TEST_ASSERT(ret < 0, "Creating vCPU with ID > MAX_VCPU_ID should fail");

/* Create vCPU with bits 63:32 != 0, but an otherwise valid id */
--
2.55.0.508.g3f0d502094-goog