]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: Limit CPU count by KVM_CAP_NR_VCPUS
authorPekka Enberg <penberg@kernel.org>
Sat, 7 May 2011 14:37:28 +0000 (17:37 +0300)
committerPekka Enberg <penberg@kernel.org>
Sat, 7 May 2011 14:39:37 +0000 (17:39 +0300)
This patch limits the number of CPUs to KVM_CAP_NR_VCPUS when user specifies
more CPUs with the "--cpus=N" command line option than what the in-kernel KVM
is able to handle.

Cc: Asias He <asias.hejun@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Prasad Joshi <prasadjoshi124@gmail.com>
Cc: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/include/kvm/kvm.h
tools/kvm/kvm-run.c
tools/kvm/kvm.c

index f6975794985d8bd98b542684fb852488246db498..3dab78de4faec385a36af50fa1679d7b1f0b1583 100644 (file)
@@ -29,6 +29,7 @@ struct kvm {
 };
 
 struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size);
+int kvm__max_cpus(struct kvm *self);
 void kvm__init_ram(struct kvm *self);
 void kvm__delete(struct kvm *self);
 bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,
index 2504ab02a85e54f906d6ce8e79b4910a52871b82..ec1e4621649e3b853b39a658a13c5828b1224240 100644 (file)
@@ -311,11 +311,12 @@ static char *host_image(char *cmd_line, size_t size)
 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 {
        static char real_cmdline[2048];
+       unsigned int nr_online_cpus;
+       int max_cpus;
        int exit_code = 0;
        int i;
        struct virtio_net_parameters net_params;
        char *hi;
-       unsigned int nr_online_cpus;
 
        signal(SIGALRM, handle_sigalrm);
        signal(SIGQUIT, handle_sigquit);
@@ -383,6 +384,13 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 
        kvm = kvm__init(kvm_dev, ram_size);
 
+       max_cpus = kvm__max_cpus(kvm);
+
+       if (nrcpus > max_cpus) {
+               printf("  # Limit the number of CPUs to %d\n", max_cpus);
+               kvm->nrcpus     = max_cpus;
+       }
+
        kvm->nrcpus = nrcpus;
 
        memset(real_cmdline, 0, sizeof(real_cmdline));
index b3adb6b2ce3e0d964572f1a9422534fdcf37a863..65793f24df7f8300f1da82ca14e95939bdcd34e2 100644 (file)
@@ -170,6 +170,17 @@ void kvm__init_ram(struct kvm *self)
                die_perror("KVM_SET_USER_MEMORY_REGION ioctl");
 }
 
+int kvm__max_cpus(struct kvm *self)
+{
+       int ret;
+
+       ret = ioctl(self->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS);
+       if (ret < 0)
+               die_perror("KVM_CAP_NR_VCPUS");
+
+       return ret;
+}
+
 struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size)
 {
        struct kvm_pit_config pit_config = { .flags = 0, };