All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hu Tao <hutao@cn.fujitsu.com>
To: qemu-devel@nongnu.org
Cc: Eduardo Habkost <ehabkost@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Igor Mammedov <imammedo@redhat.com>,
	Yasunori Goto <y-goto@jp.fujitsu.com>
Subject: [Qemu-devel] [PATCH v5 10/16] hostmem: allow preallocation of any memory region
Date: Tue, 10 Jun 2014 19:15:23 +0800	[thread overview]
Message-ID: <104cccbc0d22be3ddb883930eadb25adc5b04654.1402397894.git.hutao@cn.fujitsu.com> (raw)
In-Reply-To: <cover.1402397894.git.hutao@cn.fujitsu.com>

From: Paolo Bonzini <pbonzini@redhat.com>

And allow preallocation of file-based memory even without -mem-prealloc.
Some care is necessary because -mem-prealloc does not allow disabling
preallocation for hostmem-file.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
---
 backends/hostmem-file.c  |  3 +++
 backends/hostmem.c       | 42 ++++++++++++++++++++++++++++++++++++++++++
 exec.c                   |  7 +++++++
 include/exec/memory.h    | 10 ++++++++++
 include/exec/ram_addr.h  |  1 +
 include/sysemu/hostmem.h |  1 +
 memory.c                 | 11 +++++++++++
 7 files changed, 75 insertions(+)

diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index 9b75314..35e32d1 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -9,7 +9,9 @@
  * This work is licensed under the terms of the GNU GPL, version 2 or later.
  * See the COPYING file in the top-level directory.
  */
+#include "qemu-common.h"
 #include "sysemu/hostmem.h"
+#include "sysemu/sysemu.h"
 #include "qom/object_interfaces.h"
 
 /* hostmem-file.c */
@@ -46,6 +48,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     error_setg(errp, "-mem-path not supported on this host");
 #else
     if (!memory_region_size(&backend->mr)) {
+        backend->force_prealloc = mem_prealloc;
         memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
                                  object_get_canonical_path(OBJECT(backend)),
                                  backend->size,
diff --git a/backends/hostmem.c b/backends/hostmem.c
index a2550fe..ebef620 100644
--- a/backends/hostmem.c
+++ b/backends/hostmem.c
@@ -105,6 +105,41 @@ static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
     }
 }
 
+static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
+{
+    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+
+    return backend->prealloc || backend->force_prealloc;
+}
+
+static void host_memory_backend_set_prealloc(Object *obj, bool value,
+                                             Error **errp)
+{
+    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+
+    if (backend->force_prealloc) {
+        if (value) {
+            error_setg(errp,
+                       "remove -mem-prealloc to use the prealloc property");
+            return;
+        }
+    }
+
+    if (!memory_region_size(&backend->mr)) {
+        backend->prealloc = value;
+        return;
+    }
+
+    if (value && !backend->prealloc) {
+        int fd = memory_region_get_fd(&backend->mr);
+        void *ptr = memory_region_get_ram_ptr(&backend->mr);
+        uint64_t sz = memory_region_size(&backend->mr);
+
+        os_mem_prealloc(fd, ptr, sz);
+        backend->prealloc = true;
+    }
+}
+
 static void host_memory_backend_init(Object *obj)
 {
     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
@@ -113,6 +148,7 @@ static void host_memory_backend_init(Object *obj)
                                        "mem-merge", true);
     backend->dump = qemu_opt_get_bool(qemu_get_machine_opts(),
                                       "dump-guest-core", true);
+    backend->prealloc = mem_prealloc;
 
     object_property_add_bool(obj, "merge",
                         host_memory_backend_get_merge,
@@ -120,6 +156,9 @@ static void host_memory_backend_init(Object *obj)
     object_property_add_bool(obj, "dump",
                         host_memory_backend_get_dump,
                         host_memory_backend_set_dump, NULL);
+    object_property_add_bool(obj, "prealloc",
+                        host_memory_backend_get_prealloc,
+                        host_memory_backend_set_prealloc, NULL);
     object_property_add(obj, "size", "int",
                         host_memory_backend_get_size,
                         host_memory_backend_set_size, NULL, NULL, NULL);
@@ -165,6 +204,9 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
         if (!backend->dump) {
             qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
         }
+        if (backend->prealloc) {
+            os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz);
+        }
     }
 }
 
diff --git a/exec.c b/exec.c
index 739f0cf..520d673 100644
--- a/exec.c
+++ b/exec.c
@@ -1432,6 +1432,13 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
 }
 #endif /* !_WIN32 */
 
+int qemu_get_ram_fd(ram_addr_t addr)
+{
+    RAMBlock *block = qemu_get_ram_block(addr);
+
+    return block->fd;
+}
+
 /* Return a host pointer to ram allocated with qemu_ram_alloc.
    With the exception of the softmmu code in this file, this should
    only be used for local memory (e.g. video ram) that the device owns,
diff --git a/include/exec/memory.h b/include/exec/memory.h
index 82d7781..36226f7 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -534,6 +534,16 @@ bool memory_region_is_logging(MemoryRegion *mr);
 bool memory_region_is_rom(MemoryRegion *mr);
 
 /**
+ * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
+ *
+ * Returns a file descriptor backing a file-based RAM memory region,
+ * or -1 if the region is not a file-based RAM memory region.
+ *
+ * @mr: the RAM or alias memory region being queried.
+ */
+int memory_region_get_fd(MemoryRegion *mr);
+
+/**
  * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
  *
  * Returns a host pointer to a RAM memory region (created with
diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h
index f9518a6..d352f60 100644
--- a/include/exec/ram_addr.h
+++ b/include/exec/ram_addr.h
@@ -27,6 +27,7 @@ ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
 ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                    MemoryRegion *mr);
 ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr);
+int qemu_get_ram_fd(ram_addr_t addr);
 void *qemu_get_ram_ptr(ram_addr_t addr);
 void qemu_ram_free(ram_addr_t addr);
 void qemu_ram_free_from_ptr(ram_addr_t addr);
diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h
index ede5ec9..4cae673 100644
--- a/include/sysemu/hostmem.h
+++ b/include/sysemu/hostmem.h
@@ -53,6 +53,7 @@ struct HostMemoryBackend {
     /* protected */
     uint64_t size;
     bool merge, dump;
+    bool prealloc, force_prealloc;
 
     MemoryRegion mr;
 };
diff --git a/memory.c b/memory.c
index 310729a..bcef72b 100644
--- a/memory.c
+++ b/memory.c
@@ -1258,6 +1258,17 @@ void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
     cpu_physical_memory_reset_dirty(mr->ram_addr + addr, size, client);
 }
 
+int memory_region_get_fd(MemoryRegion *mr)
+{
+    if (mr->alias) {
+        return memory_region_get_fd(mr->alias);
+    }
+
+    assert(mr->terminates);
+
+    return qemu_get_ram_fd(mr->ram_addr & TARGET_PAGE_MASK);
+}
+
 void *memory_region_get_ram_ptr(MemoryRegion *mr)
 {
     if (mr->alias) {
-- 
1.9.3

  parent reply	other threads:[~2014-06-10 11:17 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-10 11:15 [Qemu-devel] [PATCH v5 00/16] NUMA series v5 Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 01/16] fixup! NUMA: check if the total numa memory size is equal to ram_size Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 02/16] vl: redo -object parsing Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 03/16] fixup! qmp: improve error reporting for -object and object-add Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 04/16] pc: pass MachineState to pc_memory_init Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 05/16] backend:hostmem: replace hostmemory with host_memory Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 06/16] hostmem: separate allocation from UserCreatable complete method Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 07/16] fixup! numa: add -numa node, memdev= option Hu Tao
2014-06-10 11:27   ` Michael S. Tsirkin
2014-06-10 11:30   ` Michael S. Tsirkin
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 08/16] hostmem: add file-based HostMemoryBackend Hu Tao
2014-06-11  8:03   ` Michael S. Tsirkin
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 09/16] hostmem: add merge and dump properties Hu Tao
2014-06-10 11:15 ` Hu Tao [this message]
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 11/16] hostmem: add property to map memory with MAP_SHARED Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 12/16] hostmem: add properties for NUMA memory policy Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 13/16] tests: fix memory leak in test of string input visitor Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 14/16] qapi: make string input visitor parse int list Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 15/16] qapi: make string output " Hu Tao
2014-06-10 11:15 ` [Qemu-devel] [PATCH v5 16/16] qmp: add query-memdev Hu Tao
2014-06-12  7:41 ` [Qemu-devel] [PATCH v5 00/16] NUMA series v5 Michael S. Tsirkin
2014-06-12  7:53   ` Hu Tao
2014-06-13  8:03     ` Michael S. Tsirkin
2014-06-13  8:18       ` Paolo Bonzini
2014-06-13  8:46         ` Michael S. Tsirkin
2014-06-13  8:49         ` Hu Tao
2014-06-13  8:54           ` Michael S. Tsirkin
2014-06-14  4:48             ` [Qemu-devel] [PATCH RFC 0/4] fixes for pci tree Hu Tao
2014-06-14  4:48               ` [Qemu-devel] [PATCH RFC 1/4] get rid of signed range Hu Tao
2014-06-15  9:00                 ` Michael S. Tsirkin
2014-06-16  9:47                   ` Hu Tao
2014-06-16 15:06                 ` Michael S. Tsirkin
2014-06-14  4:48               ` [Qemu-devel] [PATCH RFC 2/4] check if we have space left for hotplugged memory Hu Tao
2014-06-15  8:53                 ` Michael S. Tsirkin
2014-06-16  9:47                   ` Hu Tao
2014-06-14  4:48               ` [Qemu-devel] [PATCH RFC 3/4] exec: don't exit unconditionally if failed to allocate memory Hu Tao
2014-06-14 17:07                 ` Paolo Bonzini
2014-06-15  9:58                   ` Michael S. Tsirkin
2014-06-16  9:54                     ` Hu Tao
2014-06-16 10:07                       ` Paolo Bonzini
2014-06-14  4:48               ` [Qemu-devel] [PATCH RFC 4/4] memory-backend-file: error out " Hu Tao
2014-06-14 17:09                 ` Paolo Bonzini
2014-06-16  6:30                   ` Hu Tao
2014-06-15 10:00               ` [Qemu-devel] [PATCH RFC 0/4] fixes for pci tree Michael S. Tsirkin
2014-06-16  6:29                 ` Hu Tao
2014-06-16  7:04                   ` Michael S. Tsirkin
2014-06-16  8:28                     ` Hu Tao

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=104cccbc0d22be3ddb883930eadb25adc5b04654.1402397894.git.hutao@cn.fujitsu.com \
    --to=hutao@cn.fujitsu.com \
    --cc=ehabkost@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=y-goto@jp.fujitsu.com \
    /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.