[PATCH] kvm tools: Remove KVM_NR_CPUS

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 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

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


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux