kvm_create_max_vcpus.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kvm_create_max_vcpus
  4. *
  5. * Copyright (C) 2019, Google LLC.
  6. *
  7. * Test for KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID.
  8. */
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "test_util.h"
  14. #include "kvm_util.h"
  15. #include "asm/kvm.h"
  16. #include "linux/kvm.h"
  17. void test_vcpu_creation(int first_vcpu_id, int num_vcpus)
  18. {
  19. struct kvm_vm *vm;
  20. int i;
  21. pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n",
  22. num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1);
  23. vm = vm_create_barebones();
  24. for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++)
  25. /* This asserts that the vCPU was created. */
  26. __vm_vcpu_add(vm, i);
  27. kvm_vm_free(vm);
  28. }
  29. int main(int argc, char *argv[])
  30. {
  31. int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
  32. int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
  33. pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
  34. pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);
  35. kvm_set_files_rlimit(kvm_max_vcpus);
  36. /*
  37. * Upstream KVM prior to 4.8 does not support KVM_CAP_MAX_VCPU_ID.
  38. * Userspace is supposed to use KVM_CAP_MAX_VCPUS as the maximum ID
  39. * in this case.
  40. */
  41. if (!kvm_max_vcpu_id)
  42. kvm_max_vcpu_id = kvm_max_vcpus;
  43. TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus,
  44. "KVM_MAX_VCPU_IDS (%d) must be at least as large as KVM_MAX_VCPUS (%d).",
  45. kvm_max_vcpu_id, kvm_max_vcpus);
  46. test_vcpu_creation(0, kvm_max_vcpus);
  47. if (kvm_max_vcpu_id > kvm_max_vcpus)
  48. test_vcpu_creation(
  49. kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus);
  50. return 0;
  51. }