Skip to contentSkip to conversation

virtio-mem: Fix the bitmap index of the section offset

PATCH In review Chenyi Qiang 3 participants 6 messages Dec 16, 2022 – Dec 19, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-mem: Fix the bitmap index of the section offset

vmem->bitmap indexes the memory region of the virtio-mem backend at a granularity of block_size. To calculate the index of target section offset, the block_size should be divided instead of the bitmap_size.

Fixes 2044969f0b ("virtio-mem: Implement RamDiscardManager interface")
---
 hw/virtio/virtio-mem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/virtio/virtio-mem.c +2 -2
index ed170def48..e19ee817fe 100644
@@ -235,7 +235,7 @@ static int virtio_mem_for_each_plugged_section(const VirtIOMEM *vmem,
     uint64_t offset, size;
     int ret = 0;
 
-    first_bit = s->offset_within_region / vmem->bitmap_size;
+    first_bit = s->offset_within_region / vmem->block_size;
     first_bit = find_next_bit(vmem->bitmap, vmem->bitmap_size, first_bit);
     while (first_bit < vmem->bitmap_size) {
         MemoryRegionSection tmp = *s;
@@ -267,7 +267,7 @@ static int virtio_mem_for_each_unplugged_section(const VirtIOMEM *vmem,
     uint64_t offset, size;
     int ret = 0;
 
-    first_bit = s->offset_within_region / vmem->bitmap_size;
+    first_bit = s->offset_within_region / vmem->block_size;
     first_bit = find_next_zero_bit(vmem->bitmap, vmem->bitmap_size, first_bit);
     while (first_bit < vmem->bitmap_size) {
         MemoryRegionSection tmp = *s;
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Chenyi Qiang

vmem->bitmap indexes the memory region of the virtio-mem backend at a granularity of block_size. To calculate the index of target section offset, the block_size should be divided instead of the bitmap_size.

I'm curious, what's the user-visible effect and how did you identify this issue?

IIUC, we could end up our search for a plugged/unplugged block "too late", such that we miss to process blocks.

That would be the case if the bitmap_size < block_size, which should effectively always happen ...

unplug_all and migration would be affected, which is why a simple test case without a guest reboot/migration wouldn't run into it.

Quoted 21 lines
Fixes 2044969f0b ("virtio-mem: Implement RamDiscardManager interface")
---
  hw/virtio/virtio-mem.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/virtio/virtio-mem.c +1 -1
index ed170def48..e19ee817fe 100644
@@ -235,7 +235,7 @@ static int virtio_mem_for_each_plugged_section(const VirtIOMEM *vmem,
      uint64_t offset, size;
      int ret = 0;
 
-    first_bit = s->offset_within_region / vmem->bitmap_size;
+    first_bit = s->offset_within_region / vmem->block_size;
      first_bit = find_next_bit(vmem->bitmap, vmem->bitmap_size, first_bit);
      while (first_bit < vmem->bitmap_size) {
          MemoryRegionSection tmp = *s;

hw/virtio/virtio-mem.c static int virtio_mem_for_each_unplugged_section(const VirtIOMEM *vmem, · line 273

Quoted 8 lines
hw/virtio/virtio-mem.c +1 -1
@@ -267,7 +267,7 @@ static int virtio_mem_for_each_unplugged_section(const VirtIOMEM *vmem,
      uint64_t offset, size;
      int ret = 0;
 
-    first_bit = s->offset_within_region / vmem->bitmap_size;
+    first_bit = s->offset_within_region / vmem->block_size;
      first_bit = find_next_zero_bit(vmem->bitmap, vmem->bitmap_size, first_bit);
      while (first_bit < vmem->bitmap_size) {
          MemoryRegionSection tmp = *s;

Looks correct to me

Thanks!

Signature · 3 lines
Thanks,

David / dhildenb
#2 lore raw
Quoted 11 lines · David Hildenbrand
Quoted 3 lines · Chenyi Qiang

vmem->bitmap indexes the memory region of the virtio-mem backend at a granularity of block_size. To calculate the index of target section offset, the block_size should be divided instead of the bitmap_size.

I'm curious, what's the user-visible effect and how did you identify this issue?

IIUC, we could end up our search for a plugged/unplugged block "too late", such that we miss to process blocks.

Quoted 6 lines

That would be the case if the bitmap_size < block_size, which should effectively always happen ...

unplug_all and migration would be affected, which is why a simple test case without a guest reboot/migration wouldn't run into it.

I just realized that unplug_all is fine because only vfio implements the ram_discard_listener so far and always sets double_discard_supported=true. So migration should be the issue (and IIRC migration with VFIO is still shaky).

Signature · 3 lines
Thanks,

David / dhildenb
#3 lore raw
Quoted 17 lines · David Hildenbrand
Quoted 16 lines · David Hildenbrand
Quoted 4 lines · Chenyi Qiang

vmem->bitmap indexes the memory region of the virtio-mem backend at a granularity of block_size. To calculate the index of target section offset,
the block_size should be divided instead of the bitmap_size.

I'm curious, what's the user-visible effect and how did you identify this issue?

IIUC, we could end up our search for a plugged/unplugged block "too late", such that we miss to process blocks.

That would be the case if the bitmap_size < block_size, which should effectively always happen ...

Quoted 7 lines
Quoted 2 lines

unplug_all and migration would be affected, which is why a simple test case without a guest reboot/migration wouldn't run into it.

I just realized that unplug_all is fine because only vfio implements the ram_discard_listener so far and always sets double_discard_supported=true. So migration should be the issue (and IIRC migration with VFIO is still shaky).

Yes, actually, no obvious visible effect on my side. I was just learning the RamDiscardManager interface and found this issue. :)

Quoted 1 line
#4 lore raw
Quoted 23 lines · Chenyi Qiang
Quoted 20 lines · David Hildenbrand
Quoted 18 lines · David Hildenbrand
Quoted 4 lines · Chenyi Qiang

vmem->bitmap indexes the memory region of the virtio-mem backend at a granularity of block_size. To calculate the index of target section offset,
the block_size should be divided instead of the bitmap_size.

I'm curious, what's the user-visible effect and how did you identify this issue?

IIUC, we could end up our search for a plugged/unplugged block "too late", such that we miss to process blocks.

That would be the case if the bitmap_size < block_size, which should effectively always happen ...

unplug_all and migration would be affected, which is why a simple test case without a guest reboot/migration wouldn't run into it.

Quoted 7 lines
Quoted 4 lines

I just realized that unplug_all is fine because only vfio implements the ram_discard_listener so far and always sets double_discard_supported=true. So migration should be the issue (and IIRC migration with VFIO is still shaky).

Yes, actually, no obvious visible effect on my side. I was just learning the RamDiscardManager interface and found this issue. :)

Good, thanks.

Queuing this to

https://github.com/davidhildenbrand/qemu.git mem-next

Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by on v1 1/1
#5 lore raw
In reply to Chenyi Qiang
Quoted 6 lines · Chenyi Qiang

vmem->bitmap indexes the memory region of the virtio-mem backend at a granularity of block_size. To calculate the index of target section offset, the block_size should be divided instead of the bitmap_size.

Fixes 2044969f0b ("virtio-mem: Implement RamDiscardManager interface")

I see David's queueing this.

Quoted 28 lines
---
 hw/virtio/virtio-mem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/virtio/virtio-mem.c +2 -2
index ed170def48..e19ee817fe 100644
@@ -235,7 +235,7 @@ static int virtio_mem_for_each_plugged_section(const VirtIOMEM *vmem,
     uint64_t offset, size;
     int ret = 0;
 
-    first_bit = s->offset_within_region / vmem->bitmap_size;
+    first_bit = s->offset_within_region / vmem->block_size;
     first_bit = find_next_bit(vmem->bitmap, vmem->bitmap_size, first_bit);
     while (first_bit < vmem->bitmap_size) {
         MemoryRegionSection tmp = *s;
@@ -267,7 +267,7 @@ static int virtio_mem_for_each_unplugged_section(const VirtIOMEM *vmem,
     uint64_t offset, size;
     int ret = 0;
 
-    first_bit = s->offset_within_region / vmem->bitmap_size;
+    first_bit = s->offset_within_region / vmem->block_size;
     first_bit = find_next_zero_bit(vmem->bitmap, vmem->bitmap_size, first_bit);
     while (first_bit < vmem->bitmap_size) {
         MemoryRegionSection tmp = *s;
Signature · 1 line
2.17.1