All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 27/35] ramblock-notifier: new
Date: Fri, 20 Jan 2017 14:31:31 +0100	[thread overview]
Message-ID: <20170120133139.31080-28-pbonzini@redhat.com> (raw)
In-Reply-To: <20170120133139.31080-1-pbonzini@redhat.com>

This adds a notify interface of ram block additions and removals.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 exec.c                  |  5 ++++
 include/exec/memory.h   |  6 +----
 include/exec/ram_addr.h | 46 ++-----------------------------
 include/exec/ramlist.h  | 72 +++++++++++++++++++++++++++++++++++++++++++++++++
 numa.c                  | 29 ++++++++++++++++++++
 xen-mapcache.c          |  3 +++
 6 files changed, 112 insertions(+), 49 deletions(-)
 create mode 100644 include/exec/ramlist.h

diff --git a/exec.c b/exec.c
index c95ae33..067c51b 100644
--- a/exec.c
+++ b/exec.c
@@ -1687,6 +1687,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
         qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_HUGEPAGE);
         /* MADV_DONTFORK is also needed by KVM in absence of synchronous MMU */
         qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_DONTFORK);
+        ram_block_notify_add(new_block->host, new_block->max_length);
     }
 }
 
@@ -1817,6 +1818,10 @@ void qemu_ram_free(RAMBlock *block)
         return;
     }
 
+    if (block->host) {
+        ram_block_notify_remove(block->host, block->max_length);
+    }
+
     qemu_mutex_lock_ramlist();
     QLIST_REMOVE_RCU(block, next);
     ram_list.mru_block = NULL;
diff --git a/include/exec/memory.h b/include/exec/memory.h
index bec9756..a10044f 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -16,16 +16,12 @@
 
 #ifndef CONFIG_USER_ONLY
 
-#define DIRTY_MEMORY_VGA       0
-#define DIRTY_MEMORY_CODE      1
-#define DIRTY_MEMORY_MIGRATION 2
-#define DIRTY_MEMORY_NUM       3        /* num of dirty bits */
-
 #include "exec/cpu-common.h"
 #ifndef CONFIG_USER_ONLY
 #include "exec/hwaddr.h"
 #endif
 #include "exec/memattrs.h"
+#include "exec/ramlist.h"
 #include "qemu/queue.h"
 #include "qemu/int128.h"
 #include "qemu/notify.h"
diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h
index 54d7108..3e79466 100644
--- a/include/exec/ram_addr.h
+++ b/include/exec/ram_addr.h
@@ -21,6 +21,7 @@
 
 #ifndef CONFIG_USER_ONLY
 #include "hw/xen/xen.h"
+#include "exec/ramlist.h"
 
 struct RAMBlock {
     struct rcu_head rcu;
@@ -35,6 +36,7 @@ struct RAMBlock {
     char idstr[256];
     /* RCU-enabled, writes protected by the ramlist lock */
     QLIST_ENTRY(RAMBlock) next;
+    QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers;
     int fd;
     size_t page_size;
 };
@@ -50,51 +52,7 @@ static inline void *ramblock_ptr(RAMBlock *block, ram_addr_t offset)
     return (char *)block->host + offset;
 }
 
-/* The dirty memory bitmap is split into fixed-size blocks to allow growth
- * under RCU.  The bitmap for a block can be accessed as follows:
- *
- *   rcu_read_lock();
- *
- *   DirtyMemoryBlocks *blocks =
- *       atomic_rcu_read(&ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION]);
- *
- *   ram_addr_t idx = (addr >> TARGET_PAGE_BITS) / DIRTY_MEMORY_BLOCK_SIZE;
- *   unsigned long *block = blocks.blocks[idx];
- *   ...access block bitmap...
- *
- *   rcu_read_unlock();
- *
- * Remember to check for the end of the block when accessing a range of
- * addresses.  Move on to the next block if you reach the end.
- *
- * Organization into blocks allows dirty memory to grow (but not shrink) under
- * RCU.  When adding new RAMBlocks requires the dirty memory to grow, a new
- * DirtyMemoryBlocks array is allocated with pointers to existing blocks kept
- * the same.  Other threads can safely access existing blocks while dirty
- * memory is being grown.  When no threads are using the old DirtyMemoryBlocks
- * anymore it is freed by RCU (but the underlying blocks stay because they are
- * pointed to from the new DirtyMemoryBlocks).
- */
-#define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
-typedef struct {
-    struct rcu_head rcu;
-    unsigned long *blocks[];
-} DirtyMemoryBlocks;
-
-typedef struct RAMList {
-    QemuMutex mutex;
-    RAMBlock *mru_block;
-    /* RCU-enabled, writes protected by the ramlist lock. */
-    QLIST_HEAD(, RAMBlock) blocks;
-    DirtyMemoryBlocks *dirty_memory[DIRTY_MEMORY_NUM];
-    uint32_t version;
-} RAMList;
-extern RAMList ram_list;
-
 ram_addr_t last_ram_offset(void);
-void qemu_mutex_lock_ramlist(void);
-void qemu_mutex_unlock_ramlist(void);
-
 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
                                    bool share, const char *mem_path,
                                    Error **errp);
diff --git a/include/exec/ramlist.h b/include/exec/ramlist.h
new file mode 100644
index 0000000..c59880d
--- /dev/null
+++ b/include/exec/ramlist.h
@@ -0,0 +1,72 @@
+#ifndef RAMLIST_H
+#define RAMLIST_H
+
+#include "qemu/queue.h"
+#include "qemu/thread.h"
+#include "qemu/rcu.h"
+
+typedef struct RAMBlockNotifier RAMBlockNotifier;
+
+#define DIRTY_MEMORY_VGA       0
+#define DIRTY_MEMORY_CODE      1
+#define DIRTY_MEMORY_MIGRATION 2
+#define DIRTY_MEMORY_NUM       3        /* num of dirty bits */
+
+/* The dirty memory bitmap is split into fixed-size blocks to allow growth
+ * under RCU.  The bitmap for a block can be accessed as follows:
+ *
+ *   rcu_read_lock();
+ *
+ *   DirtyMemoryBlocks *blocks =
+ *       atomic_rcu_read(&ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION]);
+ *
+ *   ram_addr_t idx = (addr >> TARGET_PAGE_BITS) / DIRTY_MEMORY_BLOCK_SIZE;
+ *   unsigned long *block = blocks.blocks[idx];
+ *   ...access block bitmap...
+ *
+ *   rcu_read_unlock();
+ *
+ * Remember to check for the end of the block when accessing a range of
+ * addresses.  Move on to the next block if you reach the end.
+ *
+ * Organization into blocks allows dirty memory to grow (but not shrink) under
+ * RCU.  When adding new RAMBlocks requires the dirty memory to grow, a new
+ * DirtyMemoryBlocks array is allocated with pointers to existing blocks kept
+ * the same.  Other threads can safely access existing blocks while dirty
+ * memory is being grown.  When no threads are using the old DirtyMemoryBlocks
+ * anymore it is freed by RCU (but the underlying blocks stay because they are
+ * pointed to from the new DirtyMemoryBlocks).
+ */
+#define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
+typedef struct {
+    struct rcu_head rcu;
+    unsigned long *blocks[];
+} DirtyMemoryBlocks;
+
+typedef struct RAMList {
+    QemuMutex mutex;
+    RAMBlock *mru_block;
+    /* RCU-enabled, writes protected by the ramlist lock. */
+    QLIST_HEAD(, RAMBlock) blocks;
+    DirtyMemoryBlocks *dirty_memory[DIRTY_MEMORY_NUM];
+    uint32_t version;
+    QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers;
+} RAMList;
+extern RAMList ram_list;
+
+void qemu_mutex_lock_ramlist(void);
+void qemu_mutex_unlock_ramlist(void);
+
+struct RAMBlockNotifier {
+    void (*ram_block_added)(RAMBlockNotifier *n, void *host, size_t size);
+    void (*ram_block_removed)(RAMBlockNotifier *n, void *host, size_t size);
+    QLIST_ENTRY(RAMBlockNotifier) next;
+};
+
+void ram_block_notifier_add(RAMBlockNotifier *n);
+void ram_block_notifier_remove(RAMBlockNotifier *n);
+void ram_block_notify_add(void *host, size_t size);
+void ram_block_notify_remove(void *host, size_t size);
+
+
+#endif /* RAMLIST_H */
diff --git a/numa.c b/numa.c
index 379bc8a..9f56be9 100644
--- a/numa.c
+++ b/numa.c
@@ -25,6 +25,7 @@
 #include "qemu/osdep.h"
 #include "sysemu/numa.h"
 #include "exec/cpu-common.h"
+#include "exec/ramlist.h"
 #include "qemu/bitmap.h"
 #include "qom/cpu.h"
 #include "qemu/error-report.h"
@@ -572,3 +573,31 @@ int numa_get_node_for_cpu(int idx)
     }
     return i;
 }
+
+void ram_block_notifier_add(RAMBlockNotifier *n)
+{
+    QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
+}
+
+void ram_block_notifier_remove(RAMBlockNotifier *n)
+{
+    QLIST_REMOVE(n, next);
+}
+
+void ram_block_notify_add(void *host, size_t size)
+{
+    RAMBlockNotifier *notifier;
+
+    QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
+        notifier->ram_block_added(notifier, host, size);
+    }
+}
+
+void ram_block_notify_remove(void *host, size_t size)
+{
+    RAMBlockNotifier *notifier;
+
+    QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
+        notifier->ram_block_removed(notifier, host, size);
+    }
+}
diff --git a/xen-mapcache.c b/xen-mapcache.c
index 8f3a592..31debdf 100644
--- a/xen-mapcache.c
+++ b/xen-mapcache.c
@@ -163,6 +163,7 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     err = g_malloc0(nb_pfn * sizeof (int));
 
     if (entry->vaddr_base != NULL) {
+        ram_block_notify_remove(entry->vaddr_base, entry->size);
         if (munmap(entry->vaddr_base, entry->size) != 0) {
             perror("unmap fails");
             exit(-1);
@@ -188,6 +189,7 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
             BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
 
+    ram_block_notify_add(entry->vaddr_base, entry->size);
     bitmap_zero(entry->valid_mapping, nb_pfn);
     for (i = 0; i < nb_pfn; i++) {
         if (!err[i]) {
@@ -397,6 +399,7 @@ static void xen_invalidate_map_cache_entry_unlocked(uint8_t *buffer)
     }
 
     pentry->next = entry->next;
+    ram_block_notify_remove(entry->vaddr_base, entry->size);
     if (munmap(entry->vaddr_base, entry->size) != 0) {
         perror("unmap fails");
         exit(-1);
-- 
2.9.3

  parent reply	other threads:[~2017-01-20 13:32 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-20 13:31 [Qemu-devel] [PULL v3 00/32] Misc patches for 2017-01-11 Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 01/35] bugfix: vm halt when in reset looping Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 02/35] megasas: fix guest-triggered memory leak Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 03/35] qom: Make all interface types abstract Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 04/35] smbios: filter based on CONFIG_SMBIOS rather than TARGET Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 05/35] stubs: merge all monitor stubs in one file, remove monitor_cur_is_qmp stub Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 06/35] stubs: move smbios stubs to hw/smbios Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 07/35] stubs: move acpi stubs to hw/acpi Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 08/35] stubs: remove unused stub for serial_hd Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 09/35] hw: move reset handlers from vl.c to hw/core Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 10/35] stubs: group stubs for user-mode emulation Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 11/35] stubs: group all monitor_fdset_* functions in a single file Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 12/35] stubs: move vhost stubs to stubs/vhost.o Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 13/35] event_notifier: cleanups around event_notifier_set_handler Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 14/35] build: remove --enable-colo/--disable-colo Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 15/35] stubs: remove stubs/kvm.c Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 16/35] acpi: filter based on CONFIG_ACPI_X86 rather than TARGET Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 17/35] pc: fix crash in rtc_set_memory() if initial cpu is marked as hotplugged Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 18/35] scsi-block: fix direction of BYTCHK test for VERIFY commands Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 19/35] serial: fix memory leak in serial exit Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 20/35] qemu-thread: fix qemu_thread_set_name() race in qemu_thread_create() Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 21/35] hxtool: emit Texinfo headings as @subsection Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 22/35] x86: ioapic: add traces for ioapic Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 23/35] x86: ioapic: dump version for "info ioapic" Paolo Bonzini
2017-01-30 14:07   ` Peter Maydell
2017-01-30 19:33     ` Paolo Bonzini
2017-02-03  7:12       ` Peter Xu
2017-01-20 13:31 ` [Qemu-devel] [PULL 24/35] x86: ioapic: fix fail migration when irqchip=split Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 25/35] exec: Add missing rcu_read_unlock Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 26/35] char: fix ctrl-a b not working Paolo Bonzini
2017-01-20 13:31 ` Paolo Bonzini [this message]
2017-01-20 13:31 ` [Qemu-devel] [PULL 28/35] KVM: PPC: eliminate unnecessary duplicate constants Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 29/35] kvm: move cpu synchronization code Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 30/35] target/i386: Add Intel HAX files Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 31/35] Plumb the HAXM-based hardware acceleration support Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 32/35] hax: add Darwin support Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 33/35] Revert "win32: don't run subprocess tests on Mingw32 platform" Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 34/35] bitmap: assert that start and nr are non negative Paolo Bonzini
2017-01-20 13:31 ` [Qemu-devel] [PULL 35/35] pc.h: move x-mach-use-reliable-get-clock compat entry to PC_COMPAT_2_8 Paolo Bonzini
2017-01-20 15:16 ` [Qemu-devel] [PULL v3 00/32] Misc patches for 2017-01-11 no-reply
2017-01-20 17:16 ` Paolo Bonzini
2017-01-23 10:07 ` Peter Maydell
2017-01-23 14:02   ` Paolo Bonzini

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=20170120133139.31080-28-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.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.