Skip to contentSkip to conversation

docs/devel: memory: Document MemoryRegionOps requirement

PATCH In review Bin Meng 4 participants 6 messages Sep 6, 2021 – Oct 2, 2021

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/1 docs/devel: memory: Document MemoryRegionOps requirement

It's been a requirement that at least one function pointer for read and one for write are provided ever since the MemoryRegion APIs were introduced in 2012.

---

 docs/devel/memory.rst | 5 +++++
 1 file changed, 5 insertions(+)
docs/devel/memory.rst +5 -0
index 5dc8a12682..7b589b21d2 100644
@@ -344,6 +344,11 @@ based on the attributes used for the memory transaction, or need
 to be able to respond that the access should provoke a bus error
 rather than completing successfully; those devices can use the
 ->read_with_attrs() and ->write_with_attrs() callbacks instead.
+The requirement for a device's MemoryRegionOps is that at least
+one callback for read and one for write are provided. If both
+->read() and ->read_with_attrs() are provided, the plain ->read()
+version takes precedence over the with_attrs() version. So does
+the write callback.
 
 In addition various constraints can be supplied to control how these
 callbacks are called:
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Bin Meng

It's been a requirement that at least one function pointer for read and one for write are provided ever since the MemoryRegion APIs were introduced in 2012.

---

  docs/devel/memory.rst | 5 +++++
  1 file changed, 5 insertions(+)
docs/devel/memory.rst +0 -0
index 5dc8a12682..7b589b21d2 100644

docs/devel/memory.rst based on the attributes used for the memory transaction, or need · line 351

Quoted 8 lines
docs/devel/memory.rst +5 -0
@@ -344,6 +344,11 @@ based on the attributes used for the memory transaction, or need
  to be able to respond that the access should provoke a bus error
  rather than completing successfully; those devices can use the
  ->read_with_attrs() and ->write_with_attrs() callbacks instead.
+The requirement for a device's MemoryRegionOps is that at least
+one callback for read and one for write are provided. If both
+->read() and ->read_with_attrs() are provided, the plain ->read()
+version takes precedence over the with_attrs() version. So does
+the write callback.
Signature · 3 lines
Thanks,

David / dhildenb
#2 lore raw
In reply to Bin Meng
Quoted 15 lines · Bin Meng

It's been a requirement that at least one function pointer for read and one for write are provided ever since the MemoryRegion APIs were introduced in 2012.

---

 docs/devel/memory.rst | 5 +++++
 1 file changed, 5 insertions(+)
docs/devel/memory.rst +0 -0
index 5dc8a12682..7b589b21d2 100644

docs/devel/memory.rst based on the attributes used for the memory transaction, or need · line 351

Quoted 8 lines
docs/devel/memory.rst +5 -0
@@ -344,6 +344,11 @@ based on the attributes used for the memory transaction, or need
 to be able to respond that the access should provoke a bus error
 rather than completing successfully; those devices can use the
 ->read_with_attrs() and ->write_with_attrs() callbacks instead.
+The requirement for a device's MemoryRegionOps is that at least
+one callback for read and one for write are provided. If both
+->read() and ->read_with_attrs() are provided, the plain ->read()
+version takes precedence over the with_attrs() version. So does
+the write callback.

What about also adding a runtime check?

-- >8 --

softmmu/memory.c +0 -0
index bfedaf9c4df..8ab602d3379 100644
@@ -1516,6 +1516,17 @@ MemTxResult

memory_region_dispatch_write(MemoryRegion *mr,

     }

}

+static void memory_region_set_ops(MemoryRegion *mr, const MemoryRegionOps *ops)
+{
+ if (ops) {

+        assert(ops->valid.accepts || (ops->read || ops->read_with_attrs));
+        assert(ops->valid.accepts || (ops->write ||

ops->write_with_attrs));
+ mr->ops = ops;
+ } else {
+ mr->ops = &unassigned_mem_ops;
+ }
+}
+
void memory_region_init_io(MemoryRegion *mr,
Object *owner,
const MemoryRegionOps *ops,
@@ -1524,7 +1535,7 @@ void memory_region_init_io(MemoryRegion *mr,

                            uint64_t size)
 {
     memory_region_init(mr, owner, name, size);
-    mr->ops = ops ? ops : &unassigned_mem_ops;
+    memory_region_set_ops(mr, ops);
     mr->opaque = opaque;
     mr->terminates = true;
 }
@@ -1701,7 +1712,7 @@ void
memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
     Error *err = NULL;
     assert(ops);
     memory_region_init(mr, owner, name, size);
-    mr->ops = ops;
+    memory_region_set_ops(mr, ops);
     mr->opaque = opaque;
     mr->terminates = true;
     mr->rom_device = true;

---

#3 lore raw
Quoted 34 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Bin Meng

It's been a requirement that at least one function pointer for read and one for write are provided ever since the MemoryRegion APIs were introduced in 2012.

---

 docs/devel/memory.rst | 5 +++++
 1 file changed, 5 insertions(+)
docs/devel/memory.rst +0 -0
index 5dc8a12682..7b589b21d2 100644

docs/devel/memory.rst based on the attributes used for the memory transaction, or need · line 351

Quoted 8 lines
docs/devel/memory.rst +5 -0
@@ -344,6 +344,11 @@ based on the attributes used for the memory transaction, or need
 to be able to respond that the access should provoke a bus error
 rather than completing successfully; those devices can use the
 ->read_with_attrs() and ->write_with_attrs() callbacks instead.
+The requirement for a device's MemoryRegionOps is that at least
+one callback for read and one for write are provided. If both
+->read() and ->read_with_attrs() are provided, the plain ->read()
+version takes precedence over the with_attrs() version. So does
+the write callback.

What about also adding a runtime check?

-- >8 --

softmmu/memory.c +0 -0
index bfedaf9c4df..8ab602d3379 100644
@@ -1516,6 +1516,17 @@ MemTxResult
 memory_region_dispatch_write(MemoryRegion *mr,

softmmu/memory.c MemTxResult · line 1526

Quoted 10 lines
softmmu/memory.c +5 -0
     }
 }
 
+static void memory_region_set_ops(MemoryRegion *mr, const
 MemoryRegionOps *ops)
+{
+    if (ops) {
+        assert(ops->valid.accepts || (ops->read || ops->read_with_attrs));
+        assert(ops->valid.accepts || (ops->write ||
 ops->write_with_attrs));

Curious why accepts() matters.. Say, if there's only accepts() provided and it returned true, then I think we still can't avoid the coredump when read/write?

I'm also curious what's the issue that Paolo mentioned here:

https://lore.kernel.org/qemu-devel/[email protected]/

I believe Paolo was referring to this series from Prasad:

https://lore.kernel.org/qemu-devel/[email protected]/

We may need to solve that issue then maybe we can consider revive Prasad's patchset?

Signature · 1 line
Peter Xu
#4 lore raw
Quoted 38 lines · Peter Xu
Quoted 37 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Bin Meng

It's been a requirement that at least one function pointer for read and one for write are provided ever since the MemoryRegion APIs were introduced in 2012.

---

 docs/devel/memory.rst | 5 +++++
 1 file changed, 5 insertions(+)
docs/devel/memory.rst +0 -0
index 5dc8a12682..7b589b21d2 100644

docs/devel/memory.rst based on the attributes used for the memory transaction, or need · line 351

Quoted 8 lines
docs/devel/memory.rst +5 -0
@@ -344,6 +344,11 @@ based on the attributes used for the memory transaction, or need
 to be able to respond that the access should provoke a bus error
 rather than completing successfully; those devices can use the
 ->read_with_attrs() and ->write_with_attrs() callbacks instead.
+The requirement for a device's MemoryRegionOps is that at least
+one callback for read and one for write are provided. If both
+->read() and ->read_with_attrs() are provided, the plain ->read()
+version takes precedence over the with_attrs() version. So does
+the write callback.

What about also adding a runtime check?

-- >8 --

softmmu/memory.c +0 -0
index bfedaf9c4df..8ab602d3379 100644
@@ -1516,6 +1516,17 @@ MemTxResult
 memory_region_dispatch_write(MemoryRegion *mr,
     }
 }
 
Quoted 10 lines

softmmu/memory.c MemTxResult · line 1526

Quoted 7 lines
softmmu/memory.c +5 -0
+static void memory_region_set_ops(MemoryRegion *mr, const
 MemoryRegionOps *ops)
+{
+    if (ops) {
+        assert(ops->valid.accepts || (ops->read || ops->read_with_attrs));
+        assert(ops->valid.accepts || (ops->write ||
 ops->write_with_attrs));

Curious why accepts() matters.. Say, if there's only accepts() provided and it returned true, then I think we still can't avoid the coredump when read/write?

Good point :(

Quoted 11 lines

I'm also curious what's the issue that Paolo mentioned here:

https://lore.kernel.org/qemu-devel/[email protected]/

I believe Paolo was referring to this series from Prasad:

https://lore.kernel.org/qemu-devel/[email protected]/

We may need to solve that issue then maybe we can consider revive Prasad's patchset?

author
#5 lore raw
Quoted 51 lines · Philippe Mathieu-Daudé
Quoted 38 lines · Peter Xu
Quoted 37 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Bin Meng

It's been a requirement that at least one function pointer for read and one for write are provided ever since the MemoryRegion APIs were introduced in 2012.

---

 docs/devel/memory.rst | 5 +++++
 1 file changed, 5 insertions(+)
docs/devel/memory.rst +0 -0
index 5dc8a12682..7b589b21d2 100644

docs/devel/memory.rst based on the attributes used for the memory transaction, or need · line 351

Quoted 8 lines
docs/devel/memory.rst +5 -0
@@ -344,6 +344,11 @@ based on the attributes used for the memory transaction, or need
 to be able to respond that the access should provoke a bus error
 rather than completing successfully; those devices can use the
 ->read_with_attrs() and ->write_with_attrs() callbacks instead.
+The requirement for a device's MemoryRegionOps is that at least
+one callback for read and one for write are provided. If both
+->read() and ->read_with_attrs() are provided, the plain ->read()
+version takes precedence over the with_attrs() version. So does
+the write callback.

What about also adding a runtime check?

-- >8 --

softmmu/memory.c +0 -0
index bfedaf9c4df..8ab602d3379 100644
@@ -1516,6 +1516,17 @@ MemTxResult
 memory_region_dispatch_write(MemoryRegion *mr,
     }
 }
 
Quoted 10 lines

softmmu/memory.c MemTxResult · line 1526

Quoted 7 lines
softmmu/memory.c +5 -0
+static void memory_region_set_ops(MemoryRegion *mr, const
 MemoryRegionOps *ops)
+{
+    if (ops) {
+        assert(ops->valid.accepts || (ops->read || ops->read_with_attrs));
+        assert(ops->valid.accepts || (ops->write ||
 ops->write_with_attrs));

Curious why accepts() matters.. Say, if there's only accepts() provided and it returned true, then I think we still can't avoid the coredump when read/write?

Quoted 12 lines

Good point :(

Quoted 10 lines

I'm also curious what's the issue that Paolo mentioned here:

https://lore.kernel.org/qemu-devel/[email protected]/

I believe Paolo was referring to this series from Prasad:

https://lore.kernel.org/qemu-devel/[email protected]/

We may need to solve that issue then maybe we can consider revive Prasad's patchset?

It looks this patch is not applied. Given it's a doc improvement for current implementation, I think we should apply this, and future enhancement should be done in separate series?

Regards,
Bin