Skip to contentSkip to conversation

Memory API mutators

PATCH Superseded Avi Kivity 3 participants 11 messages Sep 14, 2011

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
author
#3 lore raw

PATCH 3/3 memory: optimize empty transactions due to mutators

In reply to Avi Kivity

The mutating memory APIs can easily cause empty transactions, where the mutators don't actually change anything, or perhaps only modify disabled regions. Detect these conditions and avoid regenerating the memory topology.

---
 memory.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
memory.c +7 -1
index 3b0cc25..1370fac 100644
@@ -19,6 +19,7 @@
 #include <assert.h>
 
 unsigned memory_region_transaction_depth = 0;
+static bool memory_region_update_pending = false;
 
 typedef struct AddrRange AddrRange;
 
@@ -717,6 +718,7 @@ static void address_space_update_topology(AddressSpace *as)
 static void memory_region_update_topology(MemoryRegion *mr)
 {
     if (memory_region_transaction_depth) {
+        memory_region_update_pending |= !mr || mr->enabled;
         return;
     }
 
@@ -730,6 +732,8 @@ static void memory_region_update_topology(MemoryRegion *mr)
     if (address_space_io.root) {
         address_space_update_topology(&address_space_io);
     }
+
+    memory_region_update_pending = false;
 }
 
 void memory_region_transaction_begin(void)
@@ -741,7 +745,9 @@ void memory_region_transaction_commit(void)
 {
     assert(memory_region_transaction_depth);
     --memory_region_transaction_depth;
-    memory_region_update_topology(NULL);
+    if (!memory_region_transaction_depth && memory_region_update_pending) {
+        memory_region_update_topology(NULL);
+    }
 }
 
 static void memory_region_destructor_none(MemoryRegion *mr)
Signature · 1 line
1.7.6.3
author
#2 lore raw

PATCH 2/3 memory: introduce memory_region_set_address()

In reply to Avi Kivity

Allow changing the address of a memory region while it is in the memory hierarchy.

---
 memory.c |   20 ++++++++++++++++++++
 memory.h |   11 +++++++++++
 2 files changed, 31 insertions(+), 0 deletions(-)
memory.c +20 -0
index ce0f3fd..3b0cc25 100644
@@ -1260,6 +1260,26 @@ void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
     memory_region_update_topology(NULL);
 }
 
+void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr)
+{
+    MemoryRegion *parent = mr->parent;
+    unsigned priority = mr->priority;
+    bool may_overlap = mr->may_overlap;
+
+    if (addr == mr->addr || !parent) {
+        return;
+    }
+
+    memory_region_transaction_begin();
+    memory_region_del_subregion(parent, mr);
+    if (may_overlap) {
+        memory_region_add_subregion_overlap(parent, addr, mr, priority);
+    } else {
+        memory_region_add_subregion(parent, addr, mr);
+    }
+    memory_region_transaction_commit();
+}
+
 void set_system_memory_map(MemoryRegion *mr)
 {
     address_space_memory.root = mr;
memory.h +11 -0
index 60b1449..468970b 100644
@@ -509,6 +509,17 @@ void memory_region_del_subregion(MemoryRegion *mr,
  */
 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
 
+/*
+ * memory_region_set_address: dynamically update the address of a region
+ *
+ * Dynamically updates the address of a region, relative to its parent.
+ * May be used on regions are currently part of a memory hierarchy.
+ *
+ * @mr: the region to be updated
+ * @addr: new address, relative to parent region
+ */
+void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr);
+
 /* Start a transaction; changes will be accumulated and made visible only
  * when the transaction ends.
  */
Signature · 1 line
1.7.6.3

Avi Kivity

author
#0 lore raw

cover Memory API mutators

This patchset introduces memory_region_set_enabled() and memory_region_set_address() to avoid the requirement on memory routers to track the internal state of the memory API (so they know whether they need to add or remove a region). Instead, they can simply copy the state of the region from the guest-exposed register to the memory core, via the new mutator functions.

Please review. Do we need a memory_region_set_size() as well? Do we want

  memory_region_set_attributes(mr,
                               MR_ATTR_ENABLED | MR_ATTR_SIZE,
                               (MemoryRegionAttributes) {
                                   .enabled = s->enabled,
                                   .address = s->addr,
                               });

?

Avi Kivity (3):
memory: introduce memory_region_set_enabled()
memory: introduce memory_region_set_address()
memory: optimize empty transactions due to mutators

 memory.c |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 memory.h |   28 +++++++++++++++++++++++++++
 2 files changed, 82 insertions(+), 10 deletions(-)
Signature · 1 line
1.7.6.3
author
#1 lore raw

PATCH 1/3 memory: introduce memory_region_set_enabled()

This allows users to disable a memory region without removing it from the hierarchy, simplifying the implementation of memory routers.

---
 memory.c |   38 ++++++++++++++++++++++++++++----------
 memory.h |   17 +++++++++++++++++
 2 files changed, 45 insertions(+), 10 deletions(-)
memory.c +28 -10
index 101b67c..ce0f3fd 100644
@@ -494,6 +494,10 @@ static void render_memory_region(FlatView *view,
     FlatRange fr;
     AddrRange tmp;
 
+    if (!mr->enabled) {
+        return;
+    }
+
     base += mr->addr;
 
     tmp = addrrange_make(base, mr->size);
@@ -710,12 +714,16 @@ static void address_space_update_topology(AddressSpace *as)
     address_space_update_ioeventfds(as);
 }
 
-static void memory_region_update_topology(void)
+static void memory_region_update_topology(MemoryRegion *mr)
 {
     if (memory_region_transaction_depth) {
         return;
     }
 
+    if (mr && !mr->enabled) {
+        return;
+    }
+
     if (address_space_memory.root) {
         address_space_update_topology(&address_space_memory);
     }
@@ -733,7 +741,7 @@ void memory_region_transaction_commit(void)
 {
     assert(memory_region_transaction_depth);
     --memory_region_transaction_depth;
-    memory_region_update_topology();
+    memory_region_update_topology(NULL);
 }
 
 static void memory_region_destructor_none(MemoryRegion *mr)
@@ -770,6 +778,7 @@ void memory_region_init(MemoryRegion *mr,
     mr->size = size;
     mr->addr = 0;
     mr->offset = 0;
+    mr->enabled = true;
     mr->terminates = false;
     mr->readable = true;
     mr->destructor = memory_region_destructor_none;
@@ -1005,7 +1014,7 @@ void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
     uint8_t mask = 1 << client;
 
     mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
-    memory_region_update_topology();
+    memory_region_update_topology(mr);
 }
 
 bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
@@ -1042,7 +1051,7 @@ void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable)
 {
     if (mr->readable != readable) {
         mr->readable = readable;
-        memory_region_update_topology();
+        memory_region_update_topology(mr);
     }
 }
 
@@ -1144,7 +1153,7 @@ void memory_region_add_eventfd(MemoryRegion *mr,
     memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
             sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
     mr->ioeventfds[i] = mrfd;
-    memory_region_update_topology();
+    memory_region_update_topology(mr);
 }
 
 void memory_region_del_eventfd(MemoryRegion *mr,
@@ -1174,7 +1183,7 @@ void memory_region_del_eventfd(MemoryRegion *mr,
     --mr->ioeventfd_nb;
     mr->ioeventfds = g_realloc(mr->ioeventfds,
                                   sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
-    memory_region_update_topology();
+    memory_region_update_topology(mr);
 }
 
 static void memory_region_add_subregion_common(MemoryRegion *mr,
@@ -1210,7 +1219,7 @@ static void memory_region_add_subregion_common(MemoryRegion *mr,
     }
     QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
 done:
-    memory_region_update_topology();
+    memory_region_update_topology(mr);
 }
 
 
@@ -1239,17 +1248,26 @@ void memory_region_del_subregion(MemoryRegion *mr,
     assert(subregion->parent == mr);
     subregion->parent = NULL;
     QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
-    memory_region_update_topology();
+    memory_region_update_topology(mr);
+}
+
+void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
+{
+    if (enabled == mr->enabled) {
+        return;
+    }
+    mr->enabled = enabled;
+    memory_region_update_topology(NULL);
 }
 
 void set_system_memory_map(MemoryRegion *mr)
 {
     address_space_memory.root = mr;
-    memory_region_update_topology();
+    memory_region_update_topology(NULL);
 }
 
 void set_system_io_map(MemoryRegion *mr)
 {
     address_space_io.root = mr;
-    memory_region_update_topology();
+    memory_region_update_topology(NULL);
 }
memory.h +17 -0
index 06b83ae..60b1449 100644
@@ -114,6 +114,7 @@ struct MemoryRegion {
     IORange iorange;
     bool terminates;
     bool readable;
+    bool enabled;
     MemoryRegion *alias;
     target_phys_addr_t alias_offset;
     unsigned priority;
@@ -492,6 +493,22 @@ void memory_region_add_subregion_overlap(MemoryRegion *mr,
 void memory_region_del_subregion(MemoryRegion *mr,
                                  MemoryRegion *subregion);
 
+
+/*
+ * memory_region_set_enabled: dynamically enable or disable a region
+ *
+ * Enables or disables a memory region.  A disabled memory region
+ * ignores all accesses to itself and its subregions.  It does not
+ * obscure sibling subregions with lower priority - it simply behaves as
+ * if it was removed from the hierarchy.
+ *
+ * Regions default to being enabled.
+ *
+ * @mr: the region to be updated
+ * @enabled: whether to enable or disable the region
+ */
+void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
+
 /* Start a transaction; changes will be accumulated and made visible only
  * when the transaction ends.
  */
Signature · 1 line
1.7.6.3
author
#4 lore raw
In reply to Avi Kivity

Jan, too, was interested in this.

Quoted 27 lines · Avi Kivity

This patchset introduces memory_region_set_enabled() and memory_region_set_address() to avoid the requirement on memory routers to track the internal state of the memory API (so they know whether they need to add or remove a region). Instead, they can simply copy the state of the region from the guest-exposed register to the memory core, via the new mutator functions.

Please review. Do we need a memory_region_set_size() as well? Do we want

   memory_region_set_attributes(mr,
                                MR_ATTR_ENABLED | MR_ATTR_SIZE,
                                (MemoryRegionAttributes) {
                                    .enabled = s->enabled,
                                    .address = s->addr,
                                });

?

Avi Kivity (3):
memory: introduce memory_region_set_enabled()
memory: introduce memory_region_set_address()
memory: optimize empty transactions due to mutators

  memory.c |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------
  memory.h |   28 +++++++++++++++++++++++++++
  2 files changed, 82 insertions(+), 10 deletions(-)
Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw
In reply to Avi Kivity
Quoted 8 lines · Avi Kivity

This patchset introduces memory_region_set_enabled() and memory_region_set_address() to avoid the requirement on memory routers to track the internal state of the memory API (so they know whether they need to add or remove a region).  Instead, they can simply copy the state of the region from the guest-exposed register to the memory core, via the new mutator functions.

Please review.  Do we need a memory_region_set_size() as well?

Would set_size() allow things like omap_gpmc() to avoid the need to create an intermediate container subregion to enforce size clipping on the child region it's trying to map?

(Strictly speaking what omap_gpmc() wants is not merely clipping to a guest-specified size but also wrapping, so you can take a 16MB child region and map the bottom 4MB of it repeating into a 32MB chunk of address space, say. But that would require a lot of playing games with aliases to implement a bizarre corner case that nobody uses in practice.)

-- PMM

author
#8 lore raw
Quoted 8 lines · Peter Maydell
Quoted 7 lines · Avi Kivity

This patchset introduces memory_region_set_enabled() and memory_region_set_address() to avoid the requirement on memory routers to track the internal state of the memory API (so they know whether they need to add or remove a region). Instead, they can simply copy the state of the region from the guest-exposed register to the memory core, via the new mutator functions.

Quoted 5 lines
Quoted 1 line

Please review. Do we need a memory_region_set_size() as well?

Would set_size() allow things like omap_gpmc() to avoid the need to create an intermediate container subregion to enforce size clipping on the child region it's trying to map?

I'd recommend not calling _set_size() on somebody else's region - this quickly leads to confusion. Only call set_size() if you also called _init() and will call _destroy().

Can you point me at the code in question?

_set_size() may be useful for dynamic bridge windows and the like.

Quoted 7 lines

(Strictly speaking what omap_gpmc() wants is not merely clipping to a guest-specified size but also wrapping, so you can take a 16MB child region and map the bottom 4MB of it repeating into a 32MB chunk of address space, say. But that would require a lot of playing games with aliases to implement a bizarre corner case that nobody uses in practice.)

That's best done in the memory core, the rendering loop can be adjusted to do this replication.

Signature · 1 line
error compiling committee.c: too many arguments to function
#9 lore raw
Quoted 16 lines · Avi Kivity
Quoted 9 lines · Peter Maydell
Quoted 7 lines · Avi Kivity

 This patchset introduces memory_region_set_enabled() and memory_region_set_address() to avoid the requirement on memory routers to track the internal state of the memory API (so they know whether they need to add or remove a region).  Instead, they can simply copy the state of the region from the guest-exposed register to the memory core, via the new mutator functions.

Quoted 5 lines
Quoted 1 line

 Please review.  Do we need a memory_region_set_size() as well?

Would set_size() allow things like omap_gpmc() to avoid the need to create an intermediate container subregion to enforce size clipping on the child region it's trying to map?

Quoted 5 lines

I'd recommend not calling _set_size() on somebody else's region - this quickly leads to confusion.  Only call set_size() if you also called _init() and will call _destroy().

Can you point me at the code in question?

hw/omap_gpmc.c:omap_gpmc_cs_map(). For each of the 8 children you can connect to it, the GPMC has a base and mask register. The hardware logic is effectively
if ((address & mask) == base) { send transaction to this child }

(complicated only slightly by the register for base only having bits [29:24] with the others implied-zero, and the register for mask only having bits [27:24].) The effect is that you can use the mask value to set the size of the area the child is mapped in. (Silly mask settings with "holes" are discouraged by the TRM, and the current code doesn't handle them.)

The repeated-in-the-space effect happens if the child is smaller than the space it's in: the child hardware just ignores the higher bits of the address so appears multiple times.

Quoted 9 lines
Quoted 6 lines

(Strictly speaking what omap_gpmc() wants is not merely clipping to a guest-specified size but also wrapping, so you can take a 16MB child region and map the bottom 4MB of it repeating into a 32MB chunk of address space, say. But that would require a lot of playing games with aliases to implement a bizarre corner case that nobody uses in practice.)

That's best done in the memory core, the rendering loop can be adjusted to do this replication.

That would be nice, although as I say nobody is actually relying on it so probably not worth the effort unless there's another user for it.

-- PMM

#5 lore raw
In reply to Avi Kivity
Quoted 30 lines · Avi Kivity

Jan, too, was interested in this.

Quoted 27 lines · Avi Kivity

This patchset introduces memory_region_set_enabled() and memory_region_set_address() to avoid the requirement on memory routers to track the internal state of the memory API (so they know whether they need to add or remove a region). Instead, they can simply copy the state of the region from the guest-exposed register to the memory core, via the new mutator functions.

Please review. Do we need a memory_region_set_size() as well? Do we want

   memory_region_set_attributes(mr,
                                MR_ATTR_ENABLED | MR_ATTR_SIZE,
                                (MemoryRegionAttributes) {
                                    .enabled = s->enabled,
                                    .address = s->addr,
                                });

?

Avi Kivity (3):
memory: introduce memory_region_set_enabled()
memory: introduce memory_region_set_address()
memory: optimize empty transactions due to mutators

  memory.c |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------
  memory.h |   28 +++++++++++++++++++++++++++
  2 files changed, 82 insertions(+), 10 deletions(-)

Whatever the outcome is (tons of memory_region_set/get_X functions or huge attribute structures + set/get_attributes), it should be consistent for all attributes of a memory region. And there should be only one way of doing this.

I think the decision multiple set/get vs. attribute struct depends on some (estimated) usage stats: How many call sites will access multiple attributes in one run and how may will only manipulate a single?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#6 lore raw
Quoted 11 lines · Jan Kiszka
Quoted 10 lines
Quoted 10 lines

Avi Kivity (3):
memory: introduce memory_region_set_enabled()
memory: introduce memory_region_set_address()
memory: optimize empty transactions due to mutators

   memory.c |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------
   memory.h |   28 +++++++++++++++++++++++++++
   2 files changed, 82 insertions(+), 10 deletions(-)
Quoted 4 lines

Whatever the outcome is (tons of memory_region_set/get_X functions or huge attribute structures + set/get_attributes), it should be consistent for all attributes of a memory region. And there should be only one way of doing this.

Why just one way? Different users may have different patterns. Of course internally one will be implemented on top of the other.

Quoted 4 lines

I think the decision multiple set/get vs. attribute struct depends on some (estimated) usage stats: How many call sites will access multiple attributes in one run and how may will only manipulate a single?

There won't be that many call sites to have real statistics.

Let's just go with the individual accessors and see. When the entire tree is converted (including the already-converted sites that need revisiting to use this) we can see how it looks and update the API.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#10 lore raw
In reply to Peter Maydell
Quoted 28 lines · Peter Maydell
Quoted 16 lines · Avi Kivity
Quoted 9 lines · Peter Maydell
Quoted 7 lines · Avi Kivity

This patchset introduces memory_region_set_enabled() and memory_region_set_address() to avoid the requirement on memory routers to track the internal state of the memory API (so they know whether they need to add or remove a region). Instead, they can simply copy the state of the region from the guest-exposed register to the memory core, via the new mutator functions.

Quoted 5 lines
Quoted 1 line

Please review. Do we need a memory_region_set_size() as well?

Would set_size() allow things like omap_gpmc() to avoid the need to create an intermediate container subregion to enforce size clipping on the child region it's trying to map?

Quoted 5 lines

I'd recommend not calling _set_size() on somebody else's region - this quickly leads to confusion. Only call set_size() if you also called _init() and will call _destroy().

Can you point me at the code in question?

hw/omap_gpmc.c:omap_gpmc_cs_map(). For each of the 8 children you can connect to it, the GPMC has a base and mask register. The hardware logic is effectively
if ((address& mask) == base) { send transaction to this child }

Quoted 6 lines

(complicated only slightly by the register for base only having bits [29:24] with the others implied-zero, and the register for mask only having bits [27:24].) The effect is that you can use the mask value to set the size of the area the child is mapped in. (Silly mask settings with "holes" are discouraged by the TRM, and the current code doesn't handle them.)

Thanks; and I think that in this case the omap code should avoid touching the child region (who knows, its owner may call memory_region_size() one day) and use a container (or alias - seems a better fit?) instead.

btw, what does a truncating _set_size() do to a RAM region? Discard the excess state? Or remember it and maintain two size, one internal and one for show?

Quoted 11 lines

The repeated-in-the-space effect happens if the child is smaller than the space it's in: the child hardware just ignores the higher bits of the address so appears multiple times.

Quoted 6 lines
Quoted 6 lines

(Strictly speaking what omap_gpmc() wants is not merely clipping to a guest-specified size but also wrapping, so you can take a 16MB child region and map the bottom 4MB of it repeating into a 32MB chunk of address space, say. But that would require a lot of playing games with aliases to implement a bizarre corner case that nobody uses in practice.)

Quoted 6 lines
Quoted 2 lines

That's best done in the memory core, the rendering loop can be adjusted to do this replication.

That would be nice, although as I say nobody is actually relying on it so probably not worth the effort unless there's another user for it.

There's another user in the infamous pflash_cfi02 - see pflash_setup_mappings().

Signature · 1 line
error compiling committee.c: too many arguments to function