[libvirt] [PATCH] qemu: Fix retrieval of maximum number of vCPUs on KVM hosts

The detection of the maximum number of cpus used incorrect ioctl argument value. This flaw caused that on kvm hosts this returns always "160" as the maximum. This is just a recommended maximum value. The real value is higher than that. This patch tweaks the detection function to behave as described by the kernel docs: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documen... --- src/qemu/qemu_driver.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index f4bbd74..7ca3e4c 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1103,23 +1103,32 @@ static int qemuIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED) } -static int kvmGetMaxVCPUs(void) { - int maxvcpus = 1; - - int r, fd; +static int +kvmGetMaxVCPUs(void) { + int fd; + int ret; - fd = open(KVM_DEVICE, O_RDONLY); - if (fd < 0) { + if ((fd = open(KVM_DEVICE, O_RDONLY)) < 0) { virReportSystemError(errno, _("Unable to open %s"), KVM_DEVICE); return -1; } - r = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS); - if (r > 0) - maxvcpus = r; + /* at first try KVM_CAP_MAX_VCPUS to determine the maximum count */ + if ((ret = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_MAX_VCPUS)) > 0) + goto cleanup; + + /* as a fallback get KVM_CAP_NR_VCPUS (the recommended maximum number of + * vcpus). Note that on most machines this is set to 160. */ + if ((ret = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS)) > 0) + goto cleanup; + + /* if KVM_CAP_NR_VCPUS doesn't exist either, kernel documentation states + * that 4 should be used as the maximum number of cpus */ + ret = 4; +cleanup: VIR_FORCE_CLOSE(fd); - return maxvcpus; + return ret; } -- 1.8.1.5

On 03/11/2013 07:59 AM, Peter Krempa wrote:
The detection of the maximum number of cpus used incorrect ioctl argument value. This flaw caused that on kvm hosts this returns always "160" as the maximum. This is just a recommended maximum value. The real value is higher than that.
This patch tweaks the detection function to behave as described by the kernel docs:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documen... --- src/qemu/qemu_driver.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-)
ACK. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 03/11/13 17:56, Eric Blake wrote:
On 03/11/2013 07:59 AM, Peter Krempa wrote:
The detection of the maximum number of cpus used incorrect ioctl argument value. This flaw caused that on kvm hosts this returns always "160" as the maximum. This is just a recommended maximum value. The real value is higher than that.
This patch tweaks the detection function to behave as described by the kernel docs:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documen... --- src/qemu/qemu_driver.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-)
ACK.
Pushed. Thanks. Peter
participants (2)
-
Eric Blake
-
Peter Krempa