Skip to contentSkip to conversation

bugfix: Decrease dirty bitmap blocks after we remove ramblock

RFC PATCH Superseded Keqian Zhu 1 participant 3 messages Nov 20, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Keqian Zhu

author
#0 lore raw

cover bugfix: Decrease dirty bitmap blocks after we remove ramblock

Hi all,

I failed to find where we free dirty bitmap blocks of ramlist. If this is a memory leakage problem, I hope this patch series can fix it properly :-).

Thanks,
Keqian.

Keqian Zhu (2):
ramlist: Make dirty bitmap blocks of ramlist resizable
ramlist: Resize dirty bitmap blocks after remove ramblock

 softmmu/physmem.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)
Signature · 1 line
2.23.0
author
#1 lore raw

RFC PATCH 1/2 ramlist: Make dirty bitmap blocks of ramlist resizable

When we remove a ramblock, we should decrease the dirty bitmap blocks of ramlist to avoid memory leakage. This patch rebuilds dirty_memory_ extend to support both "extend" and "decrease".

---
 softmmu/physmem.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
softmmu/physmem.c +20 -8
index 3027747c03..f6ff78378e 100644
@@ -1816,17 +1816,19 @@ void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length)
 }
 
 /* Called with ram_list.mutex held */
-static void dirty_memory_extend(ram_addr_t old_ram_size,
+static void dirty_bitmap_resize(ram_addr_t old_ram_size,
                                 ram_addr_t new_ram_size)
 {
     ram_addr_t old_num_blocks = DIV_ROUND_UP(old_ram_size,
                                              DIRTY_MEMORY_BLOCK_SIZE);
     ram_addr_t new_num_blocks = DIV_ROUND_UP(new_ram_size,
                                              DIRTY_MEMORY_BLOCK_SIZE);
+    ram_addr_t cpy_num_blocks = MIN(old_num_blocks, new_num_blocks);
+    bool extend = new_num_blocks > old_num_blocks;
     int i;
 
-    /* Only need to extend if block count increased */
-    if (new_num_blocks <= old_num_blocks) {
+    /* Only need to resize if block count changed */
+    if (new_num_blocks == old_num_blocks) {
         return;
     }
 
@@ -1839,13 +1841,23 @@ static void dirty_memory_extend(ram_addr_t old_ram_size,
         new_blocks = g_malloc(sizeof(*new_blocks) +
                               sizeof(new_blocks->blocks[0]) * new_num_blocks);
 
-        if (old_num_blocks) {
+        if (cpy_num_blocks) {
             memcpy(new_blocks->blocks, old_blocks->blocks,
-                   old_num_blocks * sizeof(old_blocks->blocks[0]));
+                   cpy_num_blocks * sizeof(old_blocks->blocks[0]));
         }
 
-        for (j = old_num_blocks; j < new_num_blocks; j++) {
-            new_blocks->blocks[j] = bitmap_new(DIRTY_MEMORY_BLOCK_SIZE);
+        if (extend) {
+            for (j = old_num_blocks; j < new_num_blocks; j++) {
+                new_blocks->blocks[j] = bitmap_new(DIRTY_MEMORY_BLOCK_SIZE);
+            }
+        } else {
+            for (j = cpy_num_blocks; j < old_num_blocks; j++) {
+                /*
+                 * We are safe to free it here, for that its RAMblock
+                 * is out-of-use.
+                 */
+                g_free(old_blocks->blocks[j]);
+            }
         }
 
         qatomic_rcu_set(&ram_list.dirty_memory[i], new_blocks);
@@ -1894,7 +1906,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared)
     new_ram_size = MAX(old_ram_size,
               (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS);
     if (new_ram_size > old_ram_size) {
-        dirty_memory_extend(old_ram_size, new_ram_size);
+        dirty_bitmap_resize(old_ram_size, new_ram_size);
     }
     /* Keep the list sorted from biggest to smallest block.  Unlike QTAILQ,
      * QLIST (which has an RCU-friendly variant) does not have insertion at
Signature · 1 line
2.23.0
author
#2 lore raw

RFC PATCH 2/2 ramlist: Resize dirty bitmap blocks after remove ramblock

In reply to Keqian Zhu

Use the new "dirty_bitmap_resize" interface to reduce dirty bitmap blocks after we remove a ramblock from ramlist.

---
 softmmu/physmem.c | 8 ++++++++
 1 file changed, 8 insertions(+)
softmmu/physmem.c +8 -0
index f6ff78378e..2a17e0a89a 100644
@@ -2131,6 +2131,8 @@ static void reclaim_ramblock(RAMBlock *block)
 
 void qemu_ram_free(RAMBlock *block)
 {
+    ram_addr_t old_ram_size, new_ram_size;
+
     if (!block) {
         return;
     }
@@ -2140,12 +2142,18 @@ void qemu_ram_free(RAMBlock *block)
     }
 
     qemu_mutex_lock_ramlist();
+
+    old_ram_size = last_ram_page();
     QLIST_REMOVE_RCU(block, next);
+    new_ram_size = last_ram_page();
+    dirty_bitmap_resize(old_ram_size, new_ram_size);
+
     ram_list.mru_block = NULL;
     /* Write list before version */
     smp_wmb();
     ram_list.version++;
     call_rcu(block, reclaim_ramblock, rcu);
+
     qemu_mutex_unlock_ramlist();
 }
 
Signature · 1 line
2.23.0