All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matt Evans <matt@ozlabs.org>
To: Pekka Enberg <penberg@kernel.org>
Cc: KVM list <kvm@vger.kernel.org>,
	Sasha Levin <levinsasha928@gmail.com>,
	Cyrill Gorcunov <gorcunov@gmail.com>,
	Asias He <asias.hejun@gmail.com>, Alexander Graf <agraf@suse.de>
Subject: [PATCH] kvm tools: Remove KVM_NR_CPUS
Date: Thu, 15 Dec 2011 16:06:57 +1100	[thread overview]
Message-ID: <4EE98071.9020009@ozlabs.org> (raw)

The KVM_NR_CPUS define is only really used to statically size the global
kvm_cpus array, which can just as easily be allocated on startup.  There is
some checking of the -c <nr cpus> value given against NR_CPUs but this is
later again checked against a dynamically-determined limit from
KVM_CAP_MAX_VCPUS anyway.  The hardwired limit is arbitrary and not strictly
necessary.

This patch removes the #define, replacing the statically-sized array with
a malloc; the array is kvm->nrcpus+1 in size so that any iterator can halt
at the end (this is done in kvm_cpu__reboot, which doesn't have access to
a struct kvm* and therefore kvm->nrcpus).

An unused #define in x86/mptable.c is also removed.

Signed-off-by: Matt Evans <matt@ozlabs.org>
---
 tools/kvm/builtin-run.c                  |    9 ++++++---
 tools/kvm/kvm-cpu.c                      |    8 ++++++--
 tools/kvm/kvm.c                          |    2 +-
 tools/kvm/powerpc/include/kvm/kvm-arch.h |    2 --
 tools/kvm/x86/include/kvm/kvm-arch.h     |    2 --
 tools/kvm/x86/mptable.c                  |    8 --------
 6 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c
index 47e4ea8..0879ab9 100644
--- a/tools/kvm/builtin-run.c
+++ b/tools/kvm/builtin-run.c
@@ -64,7 +64,7 @@ const char *DEFAULT_SANDBOX_FILENAME = "guest/sandbox.sh";
 #define MIN_RAM_SIZE_BYTE	(MIN_RAM_SIZE_MB << MB_SHIFT)
 
 struct kvm *kvm;
-struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
+struct kvm_cpu **kvm_cpus;
 __thread struct kvm_cpu *current_kvm_cpu;
 
 static u64 ram_size;
@@ -875,8 +875,6 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 
 	if (nrcpus == 0)
 		nrcpus = nr_online_cpus;
-	else if (nrcpus < 1 || nrcpus > KVM_NR_CPUS)
-		die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS);
 
 	if (!ram_size)
 		ram_size	= get_ram_size(nrcpus);
@@ -947,6 +945,11 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 
 	kvm->nrcpus = nrcpus;
 
+	/* Alloc one pointer too many, so array ends up 0-terminated */
+	kvm_cpus = calloc(nrcpus + 1, sizeof(void *));
+	if (!kvm_cpus)
+		die("Couldn't allocate array for %d CPUs", nrcpus);
+
 	irq__init(kvm);
 
 	pci__init();
diff --git a/tools/kvm/kvm-cpu.c b/tools/kvm/kvm-cpu.c
index a0f330f..52db84a 100644
--- a/tools/kvm/kvm-cpu.c
+++ b/tools/kvm/kvm-cpu.c
@@ -12,7 +12,7 @@
 #include <errno.h>
 #include <stdio.h>
 
-extern struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
+extern struct kvm_cpu **kvm_cpus;
 extern __thread struct kvm_cpu *current_kvm_cpu;
 
 void kvm_cpu__enable_singlestep(struct kvm_cpu *vcpu)
@@ -66,9 +66,13 @@ void kvm_cpu__reboot(void)
 {
 	int i;
 
-	for (i = 0; i < KVM_NR_CPUS; i++)
+	/* The kvm_cpus array contains a null pointer in the last location */
+	for (i = 0; ; i++) {
 		if (kvm_cpus[i])
 			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
+		else
+			break;
+	}
 }
 
 int kvm_cpu__start(struct kvm_cpu *cpu)
diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index 25f1419..6b220c6 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -55,7 +55,7 @@ const char *kvm_exit_reasons[] = {
 };
 
 extern struct kvm *kvm;
-extern struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
+extern struct kvm_cpu **kvm_cpus;
 static int pause_event;
 static DEFINE_MUTEX(pause_lock);
 extern struct kvm_ext kvm_req_ext[];
diff --git a/tools/kvm/powerpc/include/kvm/kvm-arch.h b/tools/kvm/powerpc/include/kvm/kvm-arch.h
index da61774..10aa2d9 100644
--- a/tools/kvm/powerpc/include/kvm/kvm-arch.h
+++ b/tools/kvm/powerpc/include/kvm/kvm-arch.h
@@ -15,8 +15,6 @@
 #include <linux/types.h>
 #include <time.h>
 
-#define KVM_NR_CPUS			(255)
-
 /*
  * MMIO lives after RAM, but it'd be nice if it didn't constantly move.
  * Choose a suitably high address, e.g. 63T...  This limits RAM size.
diff --git a/tools/kvm/x86/include/kvm/kvm-arch.h b/tools/kvm/x86/include/kvm/kvm-arch.h
index 686b1b8..1ce3d31 100644
--- a/tools/kvm/x86/include/kvm/kvm-arch.h
+++ b/tools/kvm/x86/include/kvm/kvm-arch.h
@@ -8,8 +8,6 @@
 #include <linux/types.h>
 #include <time.h>
 
-#define KVM_NR_CPUS		(255)
-
 /*
  * The hole includes VESA framebuffer and PCI memory.
  */
diff --git a/tools/kvm/x86/mptable.c b/tools/kvm/x86/mptable.c
index cfc7d79..701605a 100644
--- a/tools/kvm/x86/mptable.c
+++ b/tools/kvm/x86/mptable.c
@@ -8,14 +8,6 @@
 #include <linux/kernel.h>
 #include <string.h>
 
-/*
- * If kernel is not configured yet this macro
- * might not be defined, fix it by own definition
- */
-#ifndef NR_CPUS
-#define NR_CPUS KVM_NR_CPUS
-#endif
-
 #include <asm/mpspec_def.h>
 #include <linux/types.h>
 
-- 
1.7.0.4


                 reply	other threads:[~2011-12-15  5:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4EE98071.9020009@ozlabs.org \
    --to=matt@ozlabs.org \
    --cc=agraf@suse.de \
    --cc=asias.hejun@gmail.com \
    --cc=gorcunov@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=levinsasha928@gmail.com \
    --cc=penberg@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.