[PATCH] kvm tools: Limit CPU count by KVM_CAP_NR_VCPUS

2011-05-07 Thread Pekka Enberg
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.he...@gmail.com
Cc: Cyrill Gorcunov gorcu...@gmail.com
Cc: Ingo Molnar mi...@elte.hu
Cc: Prasad Joshi prasadjoshi...@gmail.com
Cc: Sasha Levin levinsasha...@gmail.com
Signed-off-by: Pekka Enberg penb...@kernel.org
---
 tools/kvm/include/kvm/kvm.h |1 +
 tools/kvm/kvm-run.c |   10 +-
 tools/kvm/kvm.c |   11 +++
 3 files changed, 21 insertions(+), 1 deletions(-)

diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h
index f697579..3dab78d 100644
--- a/tools/kvm/include/kvm/kvm.h
+++ b/tools/kvm/include/kvm/kvm.h
@@ -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,
diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c
index 2504ab0..ec1e462 100644
--- a/tools/kvm/kvm-run.c
+++ b/tools/kvm/kvm-run.c
@@ -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));
diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index b3adb6b..65793f2 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -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, };
-- 
1.7.0.4

--
To unsubscribe from this list: send the line unsubscribe kvm in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] kvm tools: Limit CPU count by KVM_CAP_NR_VCPUS

2011-05-07 Thread Ingo Molnar

* Pekka Enberg penb...@kernel.org wrote:

 +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;
 +}

I double checked, and KVM_CAP_NR_VCPUS was available even with the very first 
KVM version, so this should work on all KVM versions, old and new alike.

Thanks,

Ingo
--
To unsubscribe from this list: send the line unsubscribe kvm in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html