Skip to contentSkip to conversation

softmmu/physmem: fix memory leak in dirty_memory_extend()

PATCH Superseded David Hildenbrand 4 participants 11 messages Aug 27, 2024 – Aug 28, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 softmmu/physmem: fix memory leak in dirty_memory_extend()

As reported by Peter, we might be leaking memory when removing the highest RAMBlock (in the weird ram_addr_t space), and adding a new one.

We will fail to realize that we already allocated bitmaps for more dirty memory blocks, and effectively discard the pointers to them.

Fix it by getting rid of last_ram_page() and simply storing the number of dirty memory blocks that have been allocated. We'll store the number of blocks along with the actual pointer to keep it simple.

Looks like this leak was introduced as we switched from using a single bitmap_zero_extend() to allocating multiple bitmaps:
bitmap_zero_extend() relies on g_renew() which should have taken care of this.

Resolves: https://lkml.kernel.org/r/CAFEAcA-k7a+VObGAfCFNygQNfCKL=AfX6A4kScq=VSSK0peqPg@mail.gmail.com

Fixes 5b82b703b69a ("memory: RCU ram_list.dirty_memory[] for safe RAM hotplug")
Cc Stefan Hajnoczi <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Xu <[email protected]>
Cc "Philippe Mathieu-Daudé" <[email protected]>
---
 include/exec/ramlist.h |  1 +
 system/physmem.c       | 44 ++++++++++++++----------------------------
 2 files changed, 16 insertions(+), 29 deletions(-)
include/exec/ramlist.h +1 -0
index 2ad2a81acc..f2a965f293 100644
@@ -41,6 +41,7 @@ typedef struct RAMBlockNotifier RAMBlockNotifier;
 #define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
 typedef struct {
     struct rcu_head rcu;
+    unsigned int num_blocks;
     unsigned long *blocks[];
 } DirtyMemoryBlocks;
 
system/physmem.c +15 -29

83 lines not on this page · raw message

Signature · 1 line
2.46.0
Tested-by on v1 1/1
#1 lore raw
Quoted 17 lines · David Hildenbrand

As reported by Peter, we might be leaking memory when removing the highest RAMBlock (in the weird ram_addr_t space), and adding a new one.

We will fail to realize that we already allocated bitmaps for more dirty memory blocks, and effectively discard the pointers to them.

Fix it by getting rid of last_ram_page() and simply storing the number of dirty memory blocks that have been allocated. We'll store the number of blocks along with the actual pointer to keep it simple.

Looks like this leak was introduced as we switched from using a single bitmap_zero_extend() to allocating multiple bitmaps:
bitmap_zero_extend() relies on g_renew() which should have taken care of this.

Resolves: https://lkml.kernel.org/r/CAFEAcA-k7a+VObGAfCFNygQNfCKL=AfX6A4kScq=VSSK0peqPg@mail.gmail.com

Quoted 8 lines
Fixes 5b82b703b69a ("memory: RCU ram_list.dirty_memory[] for safe RAM hotplug")
Cc Stefan Hajnoczi <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Xu <[email protected]>
Cc "Philippe Mathieu-Daudé" <[email protected]>

I checked, and with this fix (plus various others I've sent out last week) we can run "make check" for the x86_64-softmmu target under asan/lsan without getting any leak reports. So (at least to that extent)

-- PMM

Reviewed-by on v1 1/1
#2 lore raw
In reply to David Hildenbrand
Quoted 35 lines · David Hildenbrand

As reported by Peter, we might be leaking memory when removing the highest RAMBlock (in the weird ram_addr_t space), and adding a new one.

We will fail to realize that we already allocated bitmaps for more dirty memory blocks, and effectively discard the pointers to them.

Fix it by getting rid of last_ram_page() and simply storing the number of dirty memory blocks that have been allocated. We'll store the number of blocks along with the actual pointer to keep it simple.

Looks like this leak was introduced as we switched from using a single bitmap_zero_extend() to allocating multiple bitmaps:
bitmap_zero_extend() relies on g_renew() which should have taken care of this.

Resolves: https://lkml.kernel.org/r/CAFEAcA-k7a+VObGAfCFNygQNfCKL=AfX6A4kScq=VSSK0peqPg@mail.gmail.com

Fixes 5b82b703b69a ("memory: RCU ram_list.dirty_memory[] for safe RAM hotplug")
Cc Stefan Hajnoczi <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Xu <[email protected]>
Cc "Philippe Mathieu-Daudé" <[email protected]>
---
 include/exec/ramlist.h |  1 +
 system/physmem.c       | 44 ++++++++++++++----------------------------
 2 files changed, 16 insertions(+), 29 deletions(-)
include/exec/ramlist.h +0 -0
index 2ad2a81acc..f2a965f293 100644

include/exec/ramlist.h typedef struct RAMBlockNotifier RAMBlockNotifier; · line 44

Quoted 4 lines
include/exec/ramlist.h +1 -0
@@ -41,6 +41,7 @@ typedef struct RAMBlockNotifier RAMBlockNotifier;
 #define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
 typedef struct {
     struct rcu_head rcu;
+    unsigned int num_blocks;

The maximum amount of memory supported by unsigned int is:
(2 ^ 32 - 1) * 4KB * DIRTY_MEMORY_BLOCK_SIZE
= ~32 exabytes

This should be fine. The maximum guest RAM sizes are in the TBs range (source: https://access.redhat.com/articles/rhel-kvm-limits).

Quoted 98 lines
include/exec/ramlist.h +0 -0
     unsigned long *blocks[];
 } DirtyMemoryBlocks;
 
system/physmem.c +15 -30
index 94600a33ec..fa48ff8333 100644
@@ -1534,18 +1534,6 @@ static ram_addr_t find_ram_offset(ram_addr_t size)
     return offset;
 }
 
-static unsigned long last_ram_page(void)
-{
-    RAMBlock *block;
-    ram_addr_t last = 0;
-
-    RCU_READ_LOCK_GUARD();
-    RAMBLOCK_FOREACH(block) {
-        last = MAX(last, block->offset + block->max_length);
-    }
-    return last >> TARGET_PAGE_BITS;
-}
-
 static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
 {
     int ret;
@@ -1799,28 +1787,31 @@ 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,
-                                ram_addr_t new_ram_size)
+static void dirty_memory_extend(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);
     int i;
 
-    /* Only need to extend if block count increased */
-    if (new_num_blocks <= old_num_blocks) {
-        return;
-    }
-
     for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
         DirtyMemoryBlocks *old_blocks;
         DirtyMemoryBlocks *new_blocks;
+        ram_addr_t old_num_blocks = 0;
         int j;
 
         old_blocks = qatomic_rcu_read(&ram_list.dirty_memory[i]);
+        if (old_blocks) {
+            old_num_blocks = old_blocks->num_blocks;
+
+            /* Only need to extend if block count increased */
+            if (new_num_blocks <= old_num_blocks) {
+                return;
+            }
+        }
+
         new_blocks = g_malloc(sizeof(*new_blocks) +
                               sizeof(new_blocks->blocks[0]) * new_num_blocks);
+        new_blocks->num_blocks = new_num_blocks;
 
         if (old_num_blocks) {
             memcpy(new_blocks->blocks, old_blocks->blocks,
@@ -1846,11 +1837,9 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
     RAMBlock *block;
     RAMBlock *last_block = NULL;
     bool free_on_error = false;
-    ram_addr_t old_ram_size, new_ram_size;
+    ram_addr_t ram_size;
     Error *err = NULL;
 
-    old_ram_size = last_ram_page();
-
     qemu_mutex_lock_ramlist();
     new_block->offset = find_ram_offset(new_block->max_length);
 
@@ -1901,11 +1890,8 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
         }
     }
 
-    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);
-    }
+    ram_size = (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS;
+    dirty_memory_extend(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
      * tail, so save the last element in last_block.
--

2.46.0

#3 lore raw
Quoted 31 lines · Stefan Hajnoczi
Quoted 30 lines · David Hildenbrand

As reported by Peter, we might be leaking memory when removing the highest RAMBlock (in the weird ram_addr_t space), and adding a new one.

We will fail to realize that we already allocated bitmaps for more dirty memory blocks, and effectively discard the pointers to them.

Fix it by getting rid of last_ram_page() and simply storing the number of dirty memory blocks that have been allocated. We'll store the number of blocks along with the actual pointer to keep it simple.

Looks like this leak was introduced as we switched from using a single bitmap_zero_extend() to allocating multiple bitmaps:
bitmap_zero_extend() relies on g_renew() which should have taken care of this.

Resolves: https://lkml.kernel.org/r/CAFEAcA-k7a+VObGAfCFNygQNfCKL=AfX6A4kScq=VSSK0peqPg@mail.gmail.com

Fixes 5b82b703b69a ("memory: RCU ram_list.dirty_memory[] for safe RAM hotplug")
Cc Stefan Hajnoczi <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Xu <[email protected]>
Cc "Philippe Mathieu-Daudé" <[email protected]>
---
  include/exec/ramlist.h |  1 +
  system/physmem.c       | 44 ++++++++++++++----------------------------
  2 files changed, 16 insertions(+), 29 deletions(-)
Quoted 14 lines

include/exec/ramlist.h typedef struct RAMBlockNotifier RAMBlockNotifier; · line 44

Quoted 9 lines
include/exec/ramlist.h +1 -0
index 2ad2a81acc..f2a965f293 100644
@@ -41,6 +41,7 @@ typedef struct RAMBlockNotifier RAMBlockNotifier;
  #define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
  typedef struct {
      struct rcu_head rcu;
+    unsigned int num_blocks;

The maximum amount of memory supported by unsigned int is:
(2 ^ 32 - 1) * 4KB * DIRTY_MEMORY_BLOCK_SIZE
= ~32 exabytes

True, should we simply use ram_addr_t ?

Quoted 4 lines

This should be fine. The maximum guest RAM sizes are in the TBs range (source: https://access.redhat.com/articles/rhel-kvm-limits).

Thanks

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw
Quoted 48 lines · David Hildenbrand
Quoted 31 lines · Stefan Hajnoczi
Quoted 30 lines · David Hildenbrand

As reported by Peter, we might be leaking memory when removing the highest RAMBlock (in the weird ram_addr_t space), and adding a new one.

We will fail to realize that we already allocated bitmaps for more dirty memory blocks, and effectively discard the pointers to them.

Fix it by getting rid of last_ram_page() and simply storing the number of dirty memory blocks that have been allocated. We'll store the number of blocks along with the actual pointer to keep it simple.

Looks like this leak was introduced as we switched from using a single bitmap_zero_extend() to allocating multiple bitmaps:
bitmap_zero_extend() relies on g_renew() which should have taken care of this.

Resolves: https://lkml.kernel.org/r/CAFEAcA-k7a+VObGAfCFNygQNfCKL=AfX6A4kScq=VSSK0peqPg@mail.gmail.com

Fixes 5b82b703b69a ("memory: RCU ram_list.dirty_memory[] for safe RAM hotplug")
Cc Stefan Hajnoczi <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Xu <[email protected]>
Cc "Philippe Mathieu-Daudé" <[email protected]>
---
  include/exec/ramlist.h |  1 +
  system/physmem.c       | 44 ++++++++++++++----------------------------
  2 files changed, 16 insertions(+), 29 deletions(-)
Quoted 14 lines

include/exec/ramlist.h typedef struct RAMBlockNotifier RAMBlockNotifier; · line 44

Quoted 9 lines
include/exec/ramlist.h +1 -0
index 2ad2a81acc..f2a965f293 100644
@@ -41,6 +41,7 @@ typedef struct RAMBlockNotifier RAMBlockNotifier;
  #define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
  typedef struct {
      struct rcu_head rcu;
+    unsigned int num_blocks;

The maximum amount of memory supported by unsigned int is:
(2 ^ 32 - 1) * 4KB * DIRTY_MEMORY_BLOCK_SIZE
= ~32 exabytes

Quoted 1 line

True, should we simply use ram_addr_t ?

Sounds good to me. In practice scalability bottlenecks are likely with those memory sizes and it will be necessary to change how guest memory is organized anyway. But it doesn't hurt to make this counter future-proof.

Stefan

Reviewed-by
#5 lore raw
Quoted 49 lines · Stefan Hajnoczi
Quoted 48 lines · David Hildenbrand
Quoted 45 lines · Stefan Hajnoczi
Quoted 35 lines · David Hildenbrand

As reported by Peter, we might be leaking memory when removing the highest RAMBlock (in the weird ram_addr_t space), and adding a new one.

We will fail to realize that we already allocated bitmaps for more dirty memory blocks, and effectively discard the pointers to them.

Fix it by getting rid of last_ram_page() and simply storing the number of dirty memory blocks that have been allocated. We'll store the number of blocks along with the actual pointer to keep it simple.

Looks like this leak was introduced as we switched from using a single bitmap_zero_extend() to allocating multiple bitmaps:
bitmap_zero_extend() relies on g_renew() which should have taken care of this.

Resolves: https://lkml.kernel.org/r/CAFEAcA-k7a+VObGAfCFNygQNfCKL=AfX6A4kScq=VSSK0peqPg@mail.gmail.com

Fixes 5b82b703b69a ("memory: RCU ram_list.dirty_memory[] for safe RAM hotplug")
Cc Stefan Hajnoczi <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Xu <[email protected]>
Cc "Philippe Mathieu-Daudé" <[email protected]>
---
  include/exec/ramlist.h |  1 +
  system/physmem.c       | 44 ++++++++++++++----------------------------
  2 files changed, 16 insertions(+), 29 deletions(-)
include/exec/ramlist.h +0 -0
index 2ad2a81acc..f2a965f293 100644

include/exec/ramlist.h typedef struct RAMBlockNotifier RAMBlockNotifier; · line 44

Quoted 4 lines
include/exec/ramlist.h +1 -0
@@ -41,6 +41,7 @@ typedef struct RAMBlockNotifier RAMBlockNotifier;
  #define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
  typedef struct {
      struct rcu_head rcu;
+    unsigned int num_blocks;

The maximum amount of memory supported by unsigned int is:
(2 ^ 32 - 1) * 4KB * DIRTY_MEMORY_BLOCK_SIZE
= ~32 exabytes

Quoted 6 lines
Quoted 1 line

True, should we simply use ram_addr_t ?

Sounds good to me. In practice scalability bottlenecks are likely with those memory sizes and it will be necessary to change how guest memory is organized anyway. But it doesn't hurt to make this counter future-proof.

IMHO it'll be nice to only use ram_addr_t when a variable is describing the ramblock address space (with an offset, or a length there). In this case it is a pure counter for how many bitmap chunks we allocated, so maybe "unsigned long" or "uint64_t" would suite more?

Though I'd think "unsigned int" is good enough per the calculation Stefan provided.

Thanks,

Signature · 1 line
Peter Xu
#6 lore raw
Quoted 57 lines · Peter Xu
Quoted 49 lines · Stefan Hajnoczi
Quoted 48 lines · David Hildenbrand
Quoted 45 lines · Stefan Hajnoczi
Quoted 35 lines · David Hildenbrand

As reported by Peter, we might be leaking memory when removing the highest RAMBlock (in the weird ram_addr_t space), and adding a new one.

We will fail to realize that we already allocated bitmaps for more dirty memory blocks, and effectively discard the pointers to them.

Fix it by getting rid of last_ram_page() and simply storing the number of dirty memory blocks that have been allocated. We'll store the number of blocks along with the actual pointer to keep it simple.

Looks like this leak was introduced as we switched from using a single bitmap_zero_extend() to allocating multiple bitmaps:
bitmap_zero_extend() relies on g_renew() which should have taken care of this.

Resolves: https://lkml.kernel.org/r/CAFEAcA-k7a+VObGAfCFNygQNfCKL=AfX6A4kScq=VSSK0peqPg@mail.gmail.com

Fixes 5b82b703b69a ("memory: RCU ram_list.dirty_memory[] for safe RAM hotplug")
Cc Stefan Hajnoczi <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Xu <[email protected]>
Cc "Philippe Mathieu-Daudé" <[email protected]>
---
   include/exec/ramlist.h |  1 +
   system/physmem.c       | 44 ++++++++++++++----------------------------
   2 files changed, 16 insertions(+), 29 deletions(-)
include/exec/ramlist.h +0 -0
index 2ad2a81acc..f2a965f293 100644

include/exec/ramlist.h typedef struct RAMBlockNotifier RAMBlockNotifier; · line 44

Quoted 4 lines
include/exec/ramlist.h +1 -0
@@ -41,6 +41,7 @@ typedef struct RAMBlockNotifier RAMBlockNotifier;
   #define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
   typedef struct {
       struct rcu_head rcu;
+    unsigned int num_blocks;

The maximum amount of memory supported by unsigned int is:
(2 ^ 32 - 1) * 4KB * DIRTY_MEMORY_BLOCK_SIZE
= ~32 exabytes

Quoted 6 lines
Quoted 1 line

True, should we simply use ram_addr_t ?

Sounds good to me. In practice scalability bottlenecks are likely with those memory sizes and it will be necessary to change how guest memory is organized anyway. But it doesn't hurt to make this counter future-proof.

Quoted 7 lines

IMHO it'll be nice to only use ram_addr_t when a variable is describing the ramblock address space (with an offset, or a length there). In this case it is a pure counter for how many bitmap chunks we allocated, so maybe "unsigned long" or "uint64_t" would suite more?

Though I'd think "unsigned int" is good enough per the calculation Stefan provided.

Likely best, "ram_addr_t" requires including "exec/cpu-common.h".

So let's stick to "unsigned int" for now. Likely best to also include for consistency:

system/physmem.c +3 -3
index fa48ff8333..e1391492fd 100644
@@ -1789,14 +1789,14 @@ 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 new_ram_size)
 {
-    ram_addr_t new_num_blocks = DIV_ROUND_UP(new_ram_size,
-                                             DIRTY_MEMORY_BLOCK_SIZE);
+    unsigned int new_num_blocks = DIV_ROUND_UP(new_ram_size,
+                                               DIRTY_MEMORY_BLOCK_SIZE);
     int i;
 
     for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
         DirtyMemoryBlocks *old_blocks;
         DirtyMemoryBlocks *new_blocks;
-        ram_addr_t old_num_blocks = 0;
+        unsigned int old_num_blocks = 0;
         int j;
 
         old_blocks = qatomic_rcu_read(&ram_list.dirty_memory[i]);
Quoted 2 lines

Thanks!

Signature · 3 lines
Cheers,

David / dhildenb
#7 lore raw
In reply to David Hildenbrand
Quoted 5 lines · David Hildenbrand

/* Called with ram_list.mutex held */

system/physmem.c +1 -2
-static void dirty_memory_extend(ram_addr_t old_ram_size,
-                                ram_addr_t new_ram_size)
+static void dirty_memory_extend(ram_addr_t new_ram_size)
 {

system/physmem.c void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length) · line 1814

Quoted 10 lines
system/physmem.c +0 -6
-    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);
     int i;
 
-    /* Only need to extend if block count increased */
-    if (new_num_blocks <= old_num_blocks) {
-        return;
-    }

One nitpick here: IMHO we could move the n_blocks cache in ram_list instead, then we keep the check here and avoid caching it three times with the same value.

Quoted 16 lines
system/physmem.c +9 -1
-
     for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
         DirtyMemoryBlocks *old_blocks;
         DirtyMemoryBlocks *new_blocks;
+        ram_addr_t old_num_blocks = 0;
         int j;
 
         old_blocks = qatomic_rcu_read(&ram_list.dirty_memory[i]);
+        if (old_blocks) {
+            old_num_blocks = old_blocks->num_blocks;
+
+            /* Only need to extend if block count increased */
+            if (new_num_blocks <= old_num_blocks) {
+                return;
+            }
+        }
Signature · 1 line
Peter Xu
#8 lore raw
Quoted 12 lines · Peter Xu
Quoted 11 lines · David Hildenbrand

/* Called with ram_list.mutex held */

system/physmem.c +1 -4
-static void dirty_memory_extend(ram_addr_t old_ram_size,
-                                ram_addr_t new_ram_size)
+static void dirty_memory_extend(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);
      int i;
Quoted 8 lines

system/physmem.c void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length) · line 1814

Quoted 4 lines
system/physmem.c +0 -4
-    /* Only need to extend if block count increased */
-    if (new_num_blocks <= old_num_blocks) {
-        return;
-    }

One nitpick here: IMHO we could move the n_blocks cache in ram_list instead, then we keep the check here and avoid caching it three times with the same value.

yes, as written in the patch description: "We'll store the number of blocks along with the actual pointer to keep it simple."

It's cleaner to me to store it along the RCU-freed data structure that has this size.

Signature · 3 lines
Cheers,

David / dhildenb
#9 lore raw
Quoted 21 lines · David Hildenbrand
Quoted 11 lines · Peter Xu
Quoted 10 lines · David Hildenbrand

/* Called with ram_list.mutex held */

system/physmem.c +1 -4
-static void dirty_memory_extend(ram_addr_t old_ram_size,
-                                ram_addr_t new_ram_size)
+static void dirty_memory_extend(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);
      int i;
Quoted 8 lines

system/physmem.c void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length) · line 1814

Quoted 4 lines
system/physmem.c +0 -4
-    /* Only need to extend if block count increased */
-    if (new_num_blocks <= old_num_blocks) {
-        return;
-    }

One nitpick here: IMHO we could move the n_blocks cache in ram_list instead, then we keep the check here and avoid caching it three times with the same value.

Quoted 5 lines

yes, as written in the patch description: "We'll store the number of blocks along with the actual pointer to keep it simple."

It's cleaner to me to store it along the RCU-freed data structure that has this size.

Yep, I can get that.

I think one reason I had my current preference is to avoid things like:

  for (...) {
    if (...)
       return;
  }

I'd at least want to sanity check before "return" to make sure all three bitmap chunks are having the same size. It gave me the feeling that we could process "blocks[]" differently but we actually couldn't - In our case it has the ram_list mutex when update, so it must be guaranteed. However due to the same reason, I see it cleaner to just keep the counter there too.

No strong feelings, though.

Signature · 1 line
Peter Xu
#10 lore raw
Quoted 37 lines · Peter Xu
Quoted 21 lines · David Hildenbrand
Quoted 11 lines · Peter Xu
Quoted 10 lines · David Hildenbrand
   /* Called with ram_list.mutex held */
system/physmem.c +1 -4
-static void dirty_memory_extend(ram_addr_t old_ram_size,
-                                ram_addr_t new_ram_size)
+static void dirty_memory_extend(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);
       int i;
Quoted 8 lines

system/physmem.c void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length) · line 1814

Quoted 4 lines
system/physmem.c +0 -4
-    /* Only need to extend if block count increased */
-    if (new_num_blocks <= old_num_blocks) {
-        return;
-    }

One nitpick here: IMHO we could move the n_blocks cache in ram_list instead, then we keep the check here and avoid caching it three times with the same value.

Quoted 5 lines

yes, as written in the patch description: "We'll store the number of blocks along with the actual pointer to keep it simple."

It's cleaner to me to store it along the RCU-freed data structure that has this size.

Yep, I can get that.

I think one reason I had my current preference is to avoid things like:

   for (...) {
     if (...)
        return;
   }
Quoted 6 lines

I'd at least want to sanity check before "return" to make sure all three bitmap chunks are having the same size. It gave me the feeling that we could process "blocks[]" differently but we actually couldn't - In our case it has the ram_list mutex when update, so it must be guaranteed. However due to the same reason, I see it cleaner to just keep the counter there too.

I'll move it to the higher level because I have more important stuff to work on and want to get this off my plate.

"num_blocks" does not quite make sense in RAMList (where we have a different "blocks" variable) so I'll call it "num_dirty_blocks" or sth like that.

Signature · 3 lines
Cheers,

David / dhildenb