From 3575acc44a9eb2217735b2bc8acdb0fed45e8938 Mon Sep 17 00:00:00 2001
From: Dean Nelson <dnelson@redhat.com>
Date: Thu, 16 Jun 2011 03:07:58 -0300
Subject: [RHEL6 qemu-kvm PATCH 2/6] exec: remove code duplication in qemu_ram_alloc() and qemu_ram_alloc_from_ptr()

RH-Author: Dean Nelson <dnelson@redhat.com>
Message-id: <20110616030757.4846.80445.email-sent-by-dnelson@localhost6.localdomain6>
Patchwork-id: 27204
O-Subject: [RHEL6.2 qemu-kvm PATCH 2/6] exec: remove code duplication in qemu_ram_alloc() and qemu_ram_alloc_from_ptr()
Bugzilla: 696102
RH-Acked-by: Jes Sorensen <Jes.Sorensen@redhat.com>
RH-Acked-by: Alex Williamson <alex.williamson@redhat.com>
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>

Resolves RHBZ 696102

Backport of:

commit 6977dfe6af975d72a8140dbc91effe8b8f2a58f8
Author: Yoshiaki Tamura <tamura.yoshiaki@lab.ntt.co.jp>
Date:   Wed Aug 18 15:41:49 2010 +0900

    exec: remove code duplication in qemu_ram_alloc() and qemu_ram_alloc_from_ptr()

    Since most of the code in qemu_ram_alloc() and
    qemu_ram_alloc_from_ptr() are duplicated, let
    qemu_ram_alloc_from_ptr() to switch by checking void *host, and change
    qemu_ram_alloc() to a wrapper.

    Signed-off-by: Yoshiaki Tamura <tamura.yoshiaki@lab.ntt.co.jp>
    Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

---
 exec.c |   82 +++++++++++++++++++--------------------------------------------
 1 files changed, 25 insertions(+), 57 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 exec.c |   82 +++++++++++++++++++--------------------------------------------
 1 files changed, 25 insertions(+), 57 deletions(-)

diff --git a/exec.c b/exec.c
index ef44f06..035bce5 100644
--- a/exec.c
+++ b/exec.c
@@ -2689,49 +2689,7 @@ static ram_addr_t last_ram_offset(void)
 }
 
 ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
-                        ram_addr_t size, void *host)
-{
-    RAMBlock *new_block, *block;
-
-    size = TARGET_PAGE_ALIGN(size);
-    new_block = qemu_mallocz(sizeof(*new_block));
-
-    if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
-        char *id = dev->parent_bus->info->get_dev_path(dev);
-        if (id) {
-            snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
-            qemu_free(id);
-        }
-    }
-    pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
-
-    QLIST_FOREACH(block, &ram_list.blocks, next) {
-        if (!strcmp(block->idstr, new_block->idstr)) {
-            fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
-                    new_block->idstr);
-            abort();
-        }
-    }
-
-    new_block->host = host;
-
-    new_block->offset = find_ram_offset(size);
-    new_block->length = size;
-
-    QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
-
-    ram_list.phys_dirty = qemu_realloc(ram_list.phys_dirty,
-                                       last_ram_offset() >> TARGET_PAGE_BITS);
-    memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
-           0xff, size >> TARGET_PAGE_BITS);
-
-    if (kvm_enabled())
-        kvm_setup_guest_memory(new_block->host, size);
-
-    return new_block->offset;
-}
-
-ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
+                                   ram_addr_t size, void *host)
 {
     RAMBlock *new_block, *block;
     extern int disable_KSM;
@@ -2756,24 +2714,27 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
         }
     }
 
-    new_block->host = file_ram_alloc(new_block, size, mem_path);
-    if (!new_block->host) {
+    if (host) {
+        new_block->host = host;
+    } else {
+        new_block->host = file_ram_alloc(new_block, size, mem_path);
+        if (!new_block->host) {
 #if defined(TARGET_S390X) && defined(CONFIG_KVM)
-    /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
-        new_block->host = mmap((void*)0x1000000, size,
-                               PROT_EXEC|PROT_READ|PROT_WRITE,
-                               MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+            /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
+            new_block->host = mmap((void*)0x1000000, size,
+                                   PROT_EXEC|PROT_READ|PROT_WRITE,
+                                   MAP_SHARED | MAP_ANONYMOUS, -1, 0);
 #else
 #ifdef PREFERRED_RAM_ALIGN
-	if (size >= PREFERRED_RAM_ALIGN)
-		new_block->host = qemu_memalign(PREFERRED_RAM_ALIGN, size);
-	else
+	    if (size >= PREFERRED_RAM_ALIGN)
+		    new_block->host = qemu_memalign(PREFERRED_RAM_ALIGN, size);
+	    else
 #endif 
-		new_block->host = qemu_vmalloc(size);
+		    new_block->host = qemu_vmalloc(size);
 #endif
 #ifdef MADV_MERGEABLE
-        if (!disable_KSM)
-            madvise(new_block->host, size, MADV_MERGEABLE);
+            if (!disable_KSM)
+                madvise(new_block->host, size, MADV_MERGEABLE);
 #else
 #if defined(__linux__) 
 #error "MADV_MERGEABLE missing"
@@ -2785,12 +2746,14 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
 #endif
 #endif
 #ifdef MADV_DONTFORK
-        madvise(new_block->host, size, MADV_DONTFORK);
+            madvise(new_block->host, size, MADV_DONTFORK);
 #endif
 #ifdef MADV_HUGEPAGE
-        madvise(new_block->host, size, MADV_HUGEPAGE);
+            madvise(new_block->host, size, MADV_HUGEPAGE);
 #endif
+        }
     }
+
     new_block->offset = find_ram_offset(size);
     new_block->length = size;
 
@@ -2809,6 +2772,11 @@ ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
     return new_block->offset;
 }
 
+ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
+{
+    return qemu_ram_alloc_from_ptr(dev, name, size, NULL);
+}
+
 void qemu_ram_free(ram_addr_t addr)
 {
     RAMBlock *block;
-- 
1.7.3.2

