9ae3a8
From f69bd1c99d908b9de5ba4f335a55597a3e8fd947 Mon Sep 17 00:00:00 2001
9ae3a8
Message-Id: <f69bd1c99d908b9de5ba4f335a55597a3e8fd947.1387276076.git.minovotn@redhat.com>
9ae3a8
In-Reply-To: <e610718166120379517e80d1a7aa12d60294209b.1387276076.git.minovotn@redhat.com>
9ae3a8
References: <e610718166120379517e80d1a7aa12d60294209b.1387276076.git.minovotn@redhat.com>
9ae3a8
From: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
Date: Mon, 9 Dec 2013 17:52:56 +0100
9ae3a8
Subject: [PATCH 07/16] kvm: Query KVM for available memory slots
9ae3a8
9ae3a8
RH-Author: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
Message-id: <20131209175223.22125.66085.stgit@bling.home>
9ae3a8
Patchwork-id: 56092
9ae3a8
O-Subject: [RHEL7 qemu-kvm PATCH] kvm: Query KVM for available memory slots
9ae3a8
Bugzilla: 921490
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
RH-Acked-by: Bandan Das <bsd@redhat.com>
9ae3a8
RH-Acked-by: Amos Kong <akong@redhat.com>
9ae3a8
9ae3a8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=921490
9ae3a8
Brew: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=6696277
9ae3a8
Upstream: fb541ca59c8b55911821c9f794c3dbe5de9ba9d8
9ae3a8
9ae3a8
KVM reports the number of available memory slots (KVM_CAP_NR_MEMSLOTS)
9ae3a8
using the extension interface.  Both x86 and s390 implement this, ARM
9ae3a8
and powerpc do not yet enable it.  Convert the static slots array to
9ae3a8
be dynamically allocated, supporting more slots when available.
9ae3a8
Default to 32 when KVM_CAP_NR_MEMSLOTS is not implemented.  The
9ae3a8
motivation for this change is to support more assigned devices, where
9ae3a8
memory mapped PCI MMIO BARs typically take one slot each.
9ae3a8
9ae3a8
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
Reviewed-by: Thomas Huth <thuth@linux.vnet.ibm.com>
9ae3a8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
---
9ae3a8
 kvm-all.c |   30 +++++++++++++++++++++---------
9ae3a8
 1 file changed, 21 insertions(+), 9 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Michal Novotny <minovotn@redhat.com>
9ae3a8
---
9ae3a8
 kvm-all.c | 30 +++++++++++++++++++++---------
9ae3a8
 1 file changed, 21 insertions(+), 9 deletions(-)
9ae3a8
9ae3a8
diff --git a/kvm-all.c b/kvm-all.c
9ae3a8
index f2f68d6..0e21494 100644
9ae3a8
--- a/kvm-all.c
9ae3a8
+++ b/kvm-all.c
9ae3a8
@@ -72,7 +72,8 @@ typedef struct kvm_dirty_log KVMDirtyLog;
9ae3a8
 
9ae3a8
 struct KVMState
9ae3a8
 {
9ae3a8
-    KVMSlot slots[32];
9ae3a8
+    KVMSlot *slots;
9ae3a8
+    int nr_slots;
9ae3a8
     int fd;
9ae3a8
     int vmfd;
9ae3a8
     int coalesced_mmio;
9ae3a8
@@ -122,7 +123,7 @@ static KVMSlot *kvm_alloc_slot(KVMState *s)
9ae3a8
 {
9ae3a8
     int i;
9ae3a8
 
9ae3a8
-    for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
9ae3a8
+    for (i = 0; i < s->nr_slots; i++) {
9ae3a8
         if (s->slots[i].memory_size == 0) {
9ae3a8
             return &s->slots[i];
9ae3a8
         }
9ae3a8
@@ -138,7 +139,7 @@ static KVMSlot *kvm_lookup_matching_slot(KVMState *s,
9ae3a8
 {
9ae3a8
     int i;
9ae3a8
 
9ae3a8
-    for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
9ae3a8
+    for (i = 0; i < s->nr_slots; i++) {
9ae3a8
         KVMSlot *mem = &s->slots[i];
9ae3a8
 
9ae3a8
         if (start_addr == mem->start_addr &&
9ae3a8
@@ -160,7 +161,7 @@ static KVMSlot *kvm_lookup_overlapping_slot(KVMState *s,
9ae3a8
     KVMSlot *found = NULL;
9ae3a8
     int i;
9ae3a8
 
9ae3a8
-    for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
9ae3a8
+    for (i = 0; i < s->nr_slots; i++) {
9ae3a8
         KVMSlot *mem = &s->slots[i];
9ae3a8
 
9ae3a8
         if (mem->memory_size == 0 ||
9ae3a8
@@ -182,7 +183,7 @@ int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
9ae3a8
 {
9ae3a8
     int i;
9ae3a8
 
9ae3a8
-    for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
9ae3a8
+    for (i = 0; i < s->nr_slots; i++) {
9ae3a8
         KVMSlot *mem = &s->slots[i];
9ae3a8
 
9ae3a8
         if (ram >= mem->ram && ram < mem->ram + mem->memory_size) {
9ae3a8
@@ -342,7 +343,7 @@ static int kvm_set_migration_log(int enable)
9ae3a8
 
9ae3a8
     s->migration_log = enable;
9ae3a8
 
9ae3a8
-    for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
9ae3a8
+    for (i = 0; i < s->nr_slots; i++) {
9ae3a8
         mem = &s->slots[i];
9ae3a8
 
9ae3a8
         if (!mem->memory_size) {
9ae3a8
@@ -1328,9 +1329,6 @@ int kvm_init(void)
9ae3a8
 #ifdef KVM_CAP_SET_GUEST_DEBUG
9ae3a8
     QTAILQ_INIT(&s->kvm_sw_breakpoints);
9ae3a8
 #endif
9ae3a8
-    for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
9ae3a8
-        s->slots[i].slot = i;
9ae3a8
-    }
9ae3a8
     s->vmfd = -1;
9ae3a8
     s->fd = qemu_open("/dev/kvm", O_RDWR);
9ae3a8
     if (s->fd == -1) {
9ae3a8
@@ -1354,6 +1352,19 @@ int kvm_init(void)
9ae3a8
         goto err;
9ae3a8
     }
9ae3a8
 
9ae3a8
+    s->nr_slots = kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
9ae3a8
+
9ae3a8
+    /* If unspecified, use the default value */
9ae3a8
+    if (!s->nr_slots) {
9ae3a8
+        s->nr_slots = 32;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    s->slots = g_malloc0(s->nr_slots * sizeof(KVMSlot));
9ae3a8
+
9ae3a8
+    for (i = 0; i < s->nr_slots; i++) {
9ae3a8
+        s->slots[i].slot = i;
9ae3a8
+    }
9ae3a8
+
9ae3a8
     /* check the vcpu limits */
9ae3a8
     soft_vcpus_limit = kvm_recommended_vcpus(s);
9ae3a8
     hard_vcpus_limit = kvm_max_vcpus(s);
9ae3a8
@@ -1467,6 +1478,7 @@ err:
9ae3a8
     if (s->fd != -1) {
9ae3a8
         close(s->fd);
9ae3a8
     }
9ae3a8
+    g_free(s->slots);
9ae3a8
     g_free(s);
9ae3a8
 
9ae3a8
     return ret;
9ae3a8
-- 
9ae3a8
1.7.11.7
9ae3a8