Skip to contentSkip to conversation

memory: Add memory_region_msync() & make NVMe emulated device generic

PATCH Applied Philippe Mathieu-Daudé 5 participants 18 messages May 8, 2020 – May 27, 2020

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover memory: Add memory_region_msync() & make NVMe emulated device generic

Let the NVMe emulated device be target-agnostic.

It is not clear if dccvap_writefn() really needs memory_region_writeback() or could use memory_region_msync().

Philippe Mathieu-Daudé (4):
memory: Rename memory_region_do_writeback -> memory_region_writeback
memory: Extract memory_region_msync() from memory_region_writeback() hw/block: Let the NVMe emulated device be target-agnostic
exec: Rename qemu_ram_writeback() as qemu_ram_msync()

 include/exec/memory.h   | 15 +++++++++++++--
 include/exec/ram_addr.h |  4 ++--
 exec.c                  |  2 +-
 hw/block/nvme.c         |  6 ++----
 memory.c                | 12 +++++++++---
 target/arm/helper.c     |  2 +-
 hw/block/Makefile.objs  |  2 +-
 7 files changed, 29 insertions(+), 14 deletions(-)
Signature · 1 line
2.21.3
#1 lore raw

PATCH 1/4 memory: Rename memory_region_do_writeback -> memory_region_writeback

We usually use '_do_' for internal functions. Rename memory_region_do_writeback() as memory_region_writeback().

---
 include/exec/memory.h | 4 ++--
 memory.c              | 2 +-
 target/arm/helper.c   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
include/exec/memory.h +2 -2
index e000bd2f97..1b7cfdd5b6 100644
@@ -1474,14 +1474,14 @@ void *memory_region_get_ram_ptr(MemoryRegion *mr);
 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
                               Error **errp);
 /**
- * memory_region_do_writeback: Trigger cache writeback or msync for
+ * memory_region_writeback: Trigger cache writeback or msync for
  * selected address range
  *
  * @mr: the memory region to be updated
  * @addr: the initial address of the range to be written back
  * @size: the size of the range to be written back
  */
-void memory_region_do_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size);
+void memory_region_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size);
 
 /**
  * memory_region_set_log: Turn dirty logging on or off for a region.
memory.c +1 -1
index 601b749906..aa1f807392 100644
@@ -2198,7 +2198,7 @@ void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp
 }
 
 
-void memory_region_do_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size)
+void memory_region_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size)
 {
     /*
      * Might be extended case needed to cover
target/arm/helper.c +1 -1
index a94f650795..c4710b7371 100644
@@ -6829,7 +6829,7 @@ static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
         mr = memory_region_from_host(haddr, &offset);
 
         if (mr) {
-            memory_region_do_writeback(mr, offset, dline_size);
+            memory_region_writeback(mr, offset, dline_size);
         }
     }
 }
Signature · 1 line
2.21.3
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] memory: Rename memory_region_do_writeback -> memory_region_writeback

Quoted 9 lines · Philippe Mathieu-Daudé

We usually use '_do_' for internal functions. Rename memory_region_do_writeback() as memory_region_writeback().

---
 include/exec/memory.h | 4 ++--
 memory.c              | 2 +-
 target/arm/helper.c   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

r~

#3 lore raw

PATCH 2/4 memory: Extract memory_region_msync() from memory_region_writeback()

In reply to Philippe Mathieu-Daudé
---
 include/exec/memory.h | 13 ++++++++++++-
 memory.c              | 10 ++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)
include/exec/memory.h +12 -1
index 1b7cfdd5b6..3e00cdbbfa 100644
@@ -1473,8 +1473,19 @@ void *memory_region_get_ram_ptr(MemoryRegion *mr);
  */
 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
                               Error **errp);
+
 /**
- * memory_region_writeback: Trigger cache writeback or msync for
+ * memory_region_msync: Synchronize selected address range of
+ * a memory mapped region
+ *
+ * @mr: the memory region to be msync
+ * @addr: the initial address of the range to be sync
+ * @size: the size of the range to be sync
+ */
+void memory_region_msync(MemoryRegion *mr, hwaddr addr, hwaddr size);
+
+/**
+ * memory_region_writeback: Trigger cache writeback for
  * selected address range
  *
  * @mr: the memory region to be updated
memory.c +8 -2
index aa1f807392..ed26514c89 100644
@@ -2197,6 +2197,12 @@ void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp
     qemu_ram_resize(mr->ram_block, newsize, errp);
 }
 
+void memory_region_msync(MemoryRegion *mr, hwaddr addr, hwaddr size)
+{
+    if (mr->ram_block) {
+        qemu_ram_writeback(mr->ram_block, addr, size);
+    }
+}
 
 void memory_region_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size)
 {
@@ -2204,8 +2210,8 @@ void memory_region_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size)
      * Might be extended case needed to cover
      * different types of memory regions
      */
-    if (mr->ram_block && mr->dirty_log_mask) {
-        qemu_ram_writeback(mr->ram_block, addr, size);
+    if (mr->dirty_log_mask) {
+        memory_region_msync(mr, addr, size);
     }
 }
 
Signature · 1 line
2.21.3
#4 lore raw

PATCH 3/4 hw/block: Let the NVMe emulated device be target-agnostic

In reply to Philippe Mathieu-Daudé

Now than the non-target specific memory_region_msync() function is available, use it to make this device target-agnostic.

---
 hw/block/nvme.c        | 6 ++----
 hw/block/Makefile.objs | 2 +-
 2 files changed, 3 insertions(+), 5 deletions(-)
hw/block/nvme.c +2 -4
index 9b453423cf..8a72dce0e6 100644
@@ -46,8 +46,7 @@
 #include "qapi/visitor.h"
 #include "sysemu/hostmem.h"
 #include "sysemu/block-backend.h"
-#include "exec/ram_addr.h"
-
+#include "exec/memory.h"
 #include "qemu/log.h"
 #include "qemu/module.h"
 #include "qemu/cutils.h"
@@ -1207,8 +1206,7 @@ static uint64_t nvme_mmio_read(void *opaque, hwaddr addr, unsigned size)
          */
         if (addr == 0xE08 &&
             (NVME_PMRCAP_PMRWBM(n->bar.pmrcap) & 0x02)) {
-            qemu_ram_writeback(n->pmrdev->mr.ram_block,
-                               0, n->pmrdev->size);
+            memory_region_msync(&n->pmrdev->mr, 0, n->pmrdev->size);
         }
         memcpy(&val, ptr + addr, size);
     } else {
hw/block/Makefile.objs +1 -1
index 47960b5f0d..8855c22656 100644
@@ -13,6 +13,6 @@ common-obj-$(CONFIG_SH4) += tc58128.o
 
 obj-$(CONFIG_VIRTIO_BLK) += virtio-blk.o
 obj-$(CONFIG_VHOST_USER_BLK) += vhost-user-blk.o
-obj-$(CONFIG_NVME_PCI) += nvme.o
+common-obj-$(CONFIG_NVME_PCI) += nvme.o
 
 obj-y += dataplane/
Signature · 1 line
2.21.3
Reviewed-by on v1 3/4
#5 lore raw

Re: [PATCH 3/4] hw/block: Let the NVMe emulated device be target-agnostic

Quoted 8 lines · Philippe Mathieu-Daudé

Now than the non-target specific memory_region_msync() function is available, use it to make this device target-agnostic.

---
 hw/block/nvme.c        | 6 ++----
 hw/block/Makefile.objs | 2 +-
 2 files changed, 3 insertions(+), 5 deletions(-)

r~

#6 lore raw

PATCH 4/4 exec: Rename qemu_ram_writeback() as qemu_ram_msync()

In reply to Philippe Mathieu-Daudé

Rename qemu_ram_writeback() as qemu_ram_msync() to better match what it does.

---
 include/exec/ram_addr.h | 4 ++--
 exec.c                  | 2 +-
 memory.c                | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
include/exec/ram_addr.h +2 -2
index a94809f89b..84817e19fa 100644
@@ -136,12 +136,12 @@ void qemu_ram_free(RAMBlock *block);
 
 int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp);
 
-void qemu_ram_writeback(RAMBlock *block, ram_addr_t start, ram_addr_t length);
+void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length);
 
 /* Clear whole block of mem */
 static inline void qemu_ram_block_writeback(RAMBlock *block)
 {
-    qemu_ram_writeback(block, 0, block->used_length);
+    qemu_ram_msync(block, 0, block->used_length);
 }
 
 #define DIRTY_CLIENTS_ALL     ((1 << DIRTY_MEMORY_NUM) - 1)
exec.c +1 -1
index 2874bb5088..f5a8cdf370 100644
@@ -2127,7 +2127,7 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp)
  * Otherwise no-op.
  * @Note: this is supposed to be a synchronous op.
  */
-void qemu_ram_writeback(RAMBlock *block, ram_addr_t start, ram_addr_t length)
+void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length)
 {
     /* The requested range should fit in within the block range */
     g_assert((start + length) <= block->used_length);
memory.c +1 -1
index ed26514c89..352b0a3aa8 100644
@@ -2200,7 +2200,7 @@ void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp
 void memory_region_msync(MemoryRegion *mr, hwaddr addr, hwaddr size)
 {
     if (mr->ram_block) {
-        qemu_ram_writeback(mr->ram_block, addr, size);
+        qemu_ram_msync(mr->ram_block, addr, size);
     }
 }
 
Signature · 1 line
2.21.3
Reviewed-by on v1 4/4
#7 lore raw

Re: [PATCH 4/4] exec: Rename qemu_ram_writeback() as qemu_ram_msync()

Quoted 10 lines · Philippe Mathieu-Daudé

Rename qemu_ram_writeback() as qemu_ram_msync() to better match what it does.

---
 include/exec/ram_addr.h | 4 ++--
 exec.c                  | 2 +-
 memory.c                | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

r~

#8 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 2 lines · Philippe Mathieu-Daudé

It is not clear if dccvap_writefn() really needs memory_region_writeback() or could use memory_region_msync().

Indeed, I don't understand the code and why it matters that mr->dirty_log_mask is nonzero.

mr->dirty_log_mask tells if dirty tracking has been enabled, not if the page is dirty. It would always be true during live migration and when running on TCG, but otherwise it would always be false.

Beata, can you explain what you had in mind? :)

Paolo

#9 lore raw
Quoted 5 lines · Paolo Bonzini
Quoted 2 lines · Philippe Mathieu-Daudé

It is not clear if dccvap_writefn() really needs memory_region_writeback() or could use memory_region_msync().

Quoted 9 lines

Indeed, I don't understand the code and why it matters that mr->dirty_log_mask is nonzero.

mr->dirty_log_mask tells if dirty tracking has been enabled, not if the page is dirty. It would always be true during live migration and when running on TCG, but otherwise it would always be false.

Beata, can you explain what you had in mind? :)

It has been a while ... , but the intention there was to skip the sync if there is nothing to be synced in the first place - so for performance reasons. I honestly do not recall why I went for the dirty_log_mask, as that seems not to be the right choice .

BR
Beata

Quoted 2 lines

Paolo

#10 lore raw
Quoted 11 lines · Beata Michalska
Quoted 7 lines

mr->dirty_log_mask tells if dirty tracking has been enabled, not if the page is dirty. It would always be true during live migration and when running on TCG, but otherwise it would always be false.

Beata, can you explain what you had in mind? :)

It has been a while ... , but the intention there was to skip the sync if there is nothing to be synced in the first place - so for performance reasons. I honestly do not recall why I went for the dirty_log_mask, as that seems not to be the right choice .

You probably wanted to look at the dirty bitmap, but you would have to define a new bitmap rather than looking at dirty_log_mask.

But that's cool, because it means that we can just remove it! Thanks,

Paolo

#11 lore raw
Quoted 13 lines · Paolo Bonzini
Quoted 11 lines · Beata Michalska
Quoted 7 lines

mr->dirty_log_mask tells if dirty tracking has been enabled, not if the page is dirty. It would always be true during live migration and when running on TCG, but otherwise it would always be false.

Beata, can you explain what you had in mind? :)

It has been a while ... , but the intention there was to skip the sync if there is nothing to be synced in the first place - so for performance reasons. I honestly do not recall why I went for the dirty_log_mask, as that seems not to be the right choice .

Quoted 4 lines

You probably wanted to look at the dirty bitmap, but you would have to define a new bitmap rather than looking at dirty_log_mask.

But that's cool, because it means that we can just remove it! Thanks,

So I understand I can simply rename memory_region_writeback -> memory_region_msync in patch #2.

#12 lore raw
Quoted 15 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Paolo Bonzini
Quoted 11 lines · Beata Michalska
Quoted 7 lines

mr->dirty_log_mask tells if dirty tracking has been enabled, not if the page is dirty. It would always be true during live migration and when running on TCG, but otherwise it would always be false.

Beata, can you explain what you had in mind? :)

It has been a while ... , but the intention there was to skip the sync if there is nothing to be synced in the first place - so for performance reasons. I honestly do not recall why I went for the dirty_log_mask, as that seems not to be the right choice .

Quoted 7 lines
Quoted 4 lines

You probably wanted to look at the dirty bitmap, but you would have to define a new bitmap rather than looking at dirty_log_mask.

But that's cool, because it means that we can just remove it! Thanks,

So I understand I can simply rename memory_region_writeback -> memory_region_msync in patch #2.

(removing the mr->dirty_log_mask).

#13 lore raw
Quoted 5 lines · Philippe Mathieu-Daudé
Quoted 2 lines

So I understand I can simply rename memory_region_writeback -> memory_region_msync in patch #2.

(removing the mr->dirty_log_mask).

Yes.

Paolo

Reviewed-by on the v1 series
#14 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé

Let the NVMe emulated device be target-agnostic.

It is not clear if dccvap_writefn() really needs memory_region_writeback() or could use memory_region_msync().

Philippe Mathieu-Daudé (4):
memory: Rename memory_region_do_writeback -> memory_region_writeback
memory: Extract memory_region_msync() from memory_region_writeback() hw/block: Let the NVMe emulated device be target-agnostic
exec: Rename qemu_ram_writeback() as qemu_ram_msync()

Quoted 13 lines
 include/exec/memory.h   | 15 +++++++++++++--
 include/exec/ram_addr.h |  4 ++--
 exec.c                  |  2 +-
 hw/block/nvme.c         |  6 ++----
 memory.c                | 12 +++++++++---
 target/arm/helper.c     |  2 +-
 hw/block/Makefile.objs  |  2 +-
 7 files changed, 29 insertions(+), 14 deletions(-)
Signature · 1 line
2.21.3
Acked-by
#15 lore raw
Quoted 21 lines · Stefan Hajnoczi
Quoted 20 lines · Philippe Mathieu-Daudé

Let the NVMe emulated device be target-agnostic.

It is not clear if dccvap_writefn() really needs memory_region_writeback() or could use memory_region_msync().

Philippe Mathieu-Daudé (4):
memory: Rename memory_region_do_writeback -> memory_region_writeback
memory: Extract memory_region_msync() from memory_region_writeback() hw/block: Let the NVMe emulated device be target-agnostic
exec: Rename qemu_ram_writeback() as qemu_ram_msync()

 include/exec/memory.h   | 15 +++++++++++++--
 include/exec/ram_addr.h |  4 ++--
 exec.c                  |  2 +-
 hw/block/nvme.c         |  6 ++----
 memory.c                | 12 +++++++++---
 target/arm/helper.c     |  2 +-
 hw/block/Makefile.objs  |  2 +-
 7 files changed, 29 insertions(+), 14 deletions(-)
Quoted 7 lines
Quoted 4 lines
Signature · 1 line
2.21.3
#16 lore raw
Quoted 27 lines · Paolo Bonzini
Quoted 26 lines · Stefan Hajnoczi
Quoted 24 lines · Philippe Mathieu-Daudé

Let the NVMe emulated device be target-agnostic.

It is not clear if dccvap_writefn() really needs memory_region_writeback() or could use memory_region_msync().

Philippe Mathieu-Daudé (4):
memory: Rename memory_region_do_writeback -> memory_region_writeback
memory: Extract memory_region_msync() from memory_region_writeback() hw/block: Let the NVMe emulated device be target-agnostic
exec: Rename qemu_ram_writeback() as qemu_ram_msync()

  include/exec/memory.h   | 15 +++++++++++++--
  include/exec/ram_addr.h |  4 ++--
  exec.c                  |  2 +-
  hw/block/nvme.c         |  6 ++----
  memory.c                | 12 +++++++++---
  target/arm/helper.c     |  2 +-
  hw/block/Makefile.objs  |  2 +-
  7 files changed, 29 insertions(+), 14 deletions(-)
Signature · 1 line
2.21.3
Quoted 5 lines
Quoted 2 lines

Thanks both.

Stefan, do you mind taking the series via your block tree?

Thanks,

Phil.

#17 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé

Let the NVMe emulated device be target-agnostic.

It is not clear if dccvap_writefn() really needs memory_region_writeback() or could use memory_region_msync().

Philippe Mathieu-Daudé (4):
memory: Rename memory_region_do_writeback -> memory_region_writeback
memory: Extract memory_region_msync() from memory_region_writeback() hw/block: Let the NVMe emulated device be target-agnostic
exec: Rename qemu_ram_writeback() as qemu_ram_msync()

Quoted 8 lines
 include/exec/memory.h   | 15 +++++++++++++--
 include/exec/ram_addr.h |  4 ++--
 exec.c                  |  2 +-
 hw/block/nvme.c         |  6 ++----
 memory.c                | 12 +++++++++---
 target/arm/helper.c     |  2 +-
 hw/block/Makefile.objs  |  2 +-
 7 files changed, 29 insertions(+), 14 deletions(-)

Thanks, applied to my block tree with Paolo's Acked-by:
https://github.com/stefanha/qemu/commits/block

Stefan