From 62dd4edaaf859b60f74a51f2a526d4d3d85d0248 Mon Sep 17 00:00:00 2001 From: Greg Kurz Date: Thu, 21 Sep 2017 18:00:53 +0200 Subject: kvm: check KVM_CAP_SYNC_MMU with kvm_vm_check_extension() On a server-class ppc host, this capability depends on the KVM type, ie, HV or PR. If both KVM are present in the kernel, we will always get the HV specific value, even if we explicitely requested PR on the command line. This can have an impact if we're using hugepages or a balloon device. Since we've already created the VM at the time any user calls kvm_has_sync_mmu(), switching to kvm_vm_check_extension() is enough to fix any potential issue. It is okay for the other archs that also implement KVM_CAP_SYNC_MMU, ie, mips, s390, x86 and arm, because they don't depend on the VM being created or not. While here, let's cache the state of this extension in a bool variable, since it has several users in the code, as suggested by Thomas Huth. Signed-off-by: Greg Kurz Message-Id: <150600965332.30533.14702405809647835716.stgit@bahia.lan> Reviewed-by: David Gibson Signed-off-by: Paolo Bonzini --- accel/kvm/kvm-all.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'accel/kvm/kvm-all.c') diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 4f1997deec..f54a337c4d 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -87,6 +87,7 @@ struct KVMState #endif int many_ioeventfds; int intx_set_mask; + bool sync_mmu; /* The man page (and posix) say ioctl numbers are signed int, but * they're not. Linux, glibc and *BSD all treat ioctl numbers as * unsigned, and treating them as signed here can break things */ @@ -1664,6 +1665,8 @@ static int kvm_init(MachineState *ms) s->many_ioeventfds = kvm_check_many_ioeventfds(); + s->sync_mmu = !!kvm_vm_check_extension(kvm_state, KVM_CAP_SYNC_MMU); + return 0; err: @@ -2130,10 +2133,9 @@ int kvm_device_access(int fd, int group, uint64_t attr, return err; } -/* Return 1 on success, 0 on failure */ -int kvm_has_sync_mmu(void) +bool kvm_has_sync_mmu(void) { - return kvm_check_extension(kvm_state, KVM_CAP_SYNC_MMU); + return kvm_state->sync_mmu; } int kvm_has_vcpu_events(void) -- cgit 1.4.1 From 11748ba72ea4fc03e975aa5f5d876b5b0902e356 Mon Sep 17 00:00:00 2001 From: Greg Kurz Date: Thu, 21 Sep 2017 18:01:02 +0200 Subject: kvm: check KVM_CAP_NR_VCPUS with kvm_vm_check_extension() On a modern server-class ppc host with the following CPU topology: Architecture: ppc64le Byte Order: Little Endian CPU(s): 32 On-line CPU(s) list: 0,8,16,24 Off-line CPU(s) list: 1-7,9-15,17-23,25-31 Thread(s) per core: 1 If both KVM PR and KVM HV loaded and we pass: -machine pseries,accel=kvm,kvm-type=PR -smp 8 We expect QEMU to warn that this exceeds the number of online CPUs: Warning: Number of SMP cpus requested (8) exceeds the recommended cpus supported by KVM (4) Warning: Number of hotpluggable cpus requested (8) exceeds the recommended cpus supported by KVM (4) but nothing is printed... This happens because on ppc the KVM_CAP_NR_VCPUS capability is VM specific ndreally depends on the KVM type, but we currently use it as a global capability. And KVM returns a fallback value based on KVM HV being present. Maybe KVM on POWER shouldn't presume anything as long as it doesn't have a VM, but in all cases, we should call KVM_CREATE_VM first and use KVM_CAP_NR_VCPUS as a VM capability. This patch hence changes kvm_recommended_vcpus() accordingly and moves the sanity checking of smp_cpus after the VM creation. It is okay for the other archs that also implement KVM_CAP_NR_VCPUS, ie, mips, s390, x86 and arm, because they don't depend on the VM being created or not. Signed-off-by: Greg Kurz Reviewed-by: David Gibson Reviewed-by: Thomas Huth Message-Id: <150600966286.30533.10909862523552370889.stgit@bahia.lan> Signed-off-by: Paolo Bonzini --- accel/kvm/kvm-all.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'accel/kvm/kvm-all.c') diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index f54a337c4d..90c88b517d 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -1440,7 +1440,7 @@ static void kvm_irqchip_create(MachineState *machine, KVMState *s) */ static int kvm_recommended_vcpus(KVMState *s) { - int ret = kvm_check_extension(s, KVM_CAP_NR_VCPUS); + int ret = kvm_vm_check_extension(s, KVM_CAP_NR_VCPUS); return (ret) ? ret : 4; } @@ -1530,26 +1530,6 @@ static int kvm_init(MachineState *ms) s->nr_slots = 32; } - /* check the vcpu limits */ - soft_vcpus_limit = kvm_recommended_vcpus(s); - hard_vcpus_limit = kvm_max_vcpus(s); - - while (nc->name) { - if (nc->num > soft_vcpus_limit) { - warn_report("Number of %s cpus requested (%d) exceeds " - "the recommended cpus supported by KVM (%d)", - nc->name, nc->num, soft_vcpus_limit); - - if (nc->num > hard_vcpus_limit) { - fprintf(stderr, "Number of %s cpus requested (%d) exceeds " - "the maximum cpus supported by KVM (%d)\n", - nc->name, nc->num, hard_vcpus_limit); - exit(1); - } - } - nc++; - } - kvm_type = qemu_opt_get(qemu_get_machine_opts(), "kvm-type"); if (mc->kvm_type) { type = mc->kvm_type(kvm_type); @@ -1584,6 +1564,27 @@ static int kvm_init(MachineState *ms) } s->vmfd = ret; + + /* check the vcpu limits */ + soft_vcpus_limit = kvm_recommended_vcpus(s); + hard_vcpus_limit = kvm_max_vcpus(s); + + while (nc->name) { + if (nc->num > soft_vcpus_limit) { + warn_report("Number of %s cpus requested (%d) exceeds " + "the recommended cpus supported by KVM (%d)", + nc->name, nc->num, soft_vcpus_limit); + + if (nc->num > hard_vcpus_limit) { + fprintf(stderr, "Number of %s cpus requested (%d) exceeds " + "the maximum cpus supported by KVM (%d)\n", + nc->name, nc->num, hard_vcpus_limit); + exit(1); + } + } + nc++; + } + missing_cap = kvm_check_extension_list(s, kvm_required_capabilites); if (!missing_cap) { missing_cap = -- cgit 1.4.1