memory: memory_region_is_mapped() cleanups
PATCH v2 1/3 machine: Use host_memory_backend_is_mapped() in machine_consume_memdev()
In reply to David Hildenbrandmemory_region_is_mapped() is the wrong check, we actually want to check whether the backend is already marked mapped.
For example, memory regions mapped via an alias, such as NVDIMMs, currently don't make memory_region_is_mapped() return "true". As the machine is initialized before any memory devices (and thereby before NVDIMMs are initialized), this isn't a fix but merely a cleanup.
--- hw/core/machine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/core/machine.c +1 -1
@@ -1260,7 +1260,7 @@ MemoryRegion *machine_consume_memdev(MachineState *machine, {MemoryRegion *ret =host_memory_backend_get_memory (backend); -if (memory_region_is_mapped (ret)) { +if (host_memory_backend_is_mapped (backend)) {error_report ("memory backend %s can't be used multiple times." ,object_get_canonical_path_component (OBJECT (backend)));exit (EXIT_FAILURE );
Signature · 1 line
2.31.1
v2 cover memory: memory_region_is_mapped() cleanups
This is the follow-up of [1].
Playing with memory_region_is_mapped(), I realized that memory regions mapped via an alias behave a little bit "differently", as they don't have their ->container set.
* memory_region_is_mapped() will never succeed for memory regions mapped via an alias
* memory_region_to_address_space(), memory_region_find(), memory_region_find_rcu(), memory_region_present() won't work, which seems okay, because we don't expect such memory regions getting passed to these functions.
* memory_region_to_absolute_addr() will result in a wrong address. As the result is only used for tracing, that is tolerable.
Let's cleanup/fix the code and documentation of memory_region_is_mapped() and change one user that really should be checking something else.
[1] https://lkml.kernel.org/r/[email protected]
David Hildenbrand (3):
machine: Use host_memory_backend_is_mapped() in
machine_consume_memdev()
memory: Make memory_region_is_mapped() succeed when mapped via an
alias
memory: Update description of memory_region_is_mapped()
hw/core/machine.c | 2 +- include/exec/memory.h | 4 +++- softmmu/memory.c | 12 +++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 2/3 memory: Make memory_region_is_mapped() succeed when mapped via an alias
memory_region_is_mapped() currently does not return "true" when a memory region is mapped via an alias.
Assuming we have:
alias (A0) -> alias (A1) -> region (R0)
Mapping A0 would currently only make memory_region_is_mapped() succeed on A0, but not on A1 and R0.
Let's fix that by adding a "mapped_via_alias" counter to memory regions and updating it accordingly when an alias gets (un)mapped.
I am not aware of actual issues, this is rather a cleanup to make it consistent.
--- include/exec/memory.h | 1 + softmmu/memory.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-)
include/exec/memory.h +1 -0
@@ -707,6 +707,7 @@ struct MemoryRegion {const MemoryRegionOps *ops;void *opaque;MemoryRegion *container; +int mapped_via_alias;/* Mapped via an alias, container might be NULL */ Int128 size;hwaddr addr;void (*destructor)(MemoryRegion *mr);
softmmu/memory.c +11 -1
@@ -2524,8 +2524,13 @@ static void memory_region_add_subregion_common(MemoryRegion *mr,hwaddr offset, MemoryRegion *subregion) { + MemoryRegion *alias; +assert (!subregion->container); subregion->container = mr; +for (alias = subregion->alias; alias; alias = alias->alias) { + alias->mapped_via_alias++; + } subregion->addr = offset;memory_region_update_container_subregions (subregion); }
@@ -2550,9 +2555,14 @@ void memory_region_add_subregion_overlap(MemoryRegion *mr,void memory_region_del_subregion (MemoryRegion *mr,MemoryRegion *subregion) { +MemoryRegion *alias; +memory_region_transaction_begin ();assert (subregion->container == mr); subregion->container =NULL ; +for (alias = subregion->alias; alias; alias = alias->alias) { + alias->mapped_via_alias--; + }QTAILQ_REMOVE (&mr->subregions, subregion, subregions_link);memory_region_unref (subregion); memory_region_update_pending |= mr->enabled && subregion->enabled;
@@ -2649,7 +2659,7 @@ static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)bool memory_region_is_mapped (MemoryRegion *mr) { -return mr->container ? true : false; +return !!mr->container || mr->mapped_via_alias; }/* Same as memory_region_find, but it does not add a reference to the
Signature · 1 line
2.31.1
PATCH v2 3/3 memory: Update description of memory_region_is_mapped()
In reply to David HildenbrandLet's update the documentation, making it clearer what the semantics of memory_region_is_mapped() actually are.
--- include/exec/memory.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/exec/memory.h +2 -1
@@ -2266,7 +2266,8 @@ bool memory_region_present(MemoryRegion *container, hwaddr addr);/** * memory_region_is_mapped: returns true if #MemoryRegion is mapped -* into any address space. +* into another memory region, which does not necessarily imply that it is +* mapped into an address space. * * @mr: a #MemoryRegion which should be checked if it's mapped */
Signature · 1 line
2.31.1
Re: [PATCH v2 1/3] machine: Use host_memory_backend_is_mapped() in machine_consume_memdev()
In reply to David HildenbrandQuoted 8 lines · David Hildenbrand
memory_region_is_mapped()is the wrong check, we actually want to check whether the backend is already marked mapped.For example, memory regions mapped via an alias, such as NVDIMMs, currently don't make
memory_region_is_mapped()return "true". As the machine is initialized before any memory devices (and thereby before NVDIMMs are initialized), this isn't a fix but merely a cleanup.
Quoted 6 lines
Reviewed-by Richard Henderson <[email protected]>Reviewed-by Igor Mammedov <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- hw/core/machine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Re: [PATCH v2 2/3] memory: Make memory_region_is_mapped() succeed when mapped via an alias
In reply to David HildenbrandQuoted 51 lines · David Hildenbrand
memory_region_is_mapped()currently does not return "true" when a memory region is mapped via an alias.Assuming we have:
alias (A0) -> alias (A1) -> region (R0)Mapping A0 would currently only make
memory_region_is_mapped()succeed on A0, but not on A1 and R0.Let's fix that by adding a "
mapped_via_alias" counter to memory regions and updating it accordingly when an alias gets (un)mapped.I am not aware of actual issues, this is rather a cleanup to make it consistent.
Signed-off-by David Hildenbrand <[email protected]>--- include/exec/memory.h | 1 + softmmu/memory.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-)include/exec/memory.h +1
-0@@ -707,6 +707,7 @@ struct MemoryRegion {const MemoryRegionOps *ops;void *opaque;MemoryRegion *container; +int mapped_via_alias;/* Mapped via an alias, container might be NULL */ Int128 size;hwaddr addr;void (*destructor)(MemoryRegion *mr);softmmu/memory.c +5
-0@@ -2524,8 +2524,13 @@ static void memory_region_add_subregion_common(MemoryRegion *mr,hwaddr offset, MemoryRegion *subregion) { + MemoryRegion *alias; +assert (!subregion->container); subregion->container = mr; +for (alias = subregion->alias; alias; alias = alias->alias) { + alias->mapped_via_alias++; + } subregion->addr = offset;memory_region_update_container_subregions (subregion); }
softmmu/memory.c void memory_region_add_subregion_overlap(MemoryRegion *mr, · line 2564
Quoted 10 lines
softmmu/memory.c +4
-0@@ -2550,9 +2555,14 @@ void memory_region_add_subregion_overlap(MemoryRegion *mr,void memory_region_del_subregion (MemoryRegion *mr,MemoryRegion *subregion) { +MemoryRegion *alias; +memory_region_transaction_begin ();assert (subregion->container == mr); subregion->container =NULL ; +for (alias = subregion->alias; alias; alias = alias->alias) { + alias->mapped_via_alias--;
assert(alias->mapped_via_alias >= 0);
Quoted 6 lines
softmmu/memory.c +1
-0+ }QTAILQ_REMOVE (&mr->subregions, subregion, subregions_link);memory_region_unref (subregion); memory_region_update_pending |= mr->enabled && subregion->enabled;
softmmu/memory.c static FlatRange *flatview_lookup(FlatView *view, AddrRange addr) · line 2665
Quoted 8 lines
softmmu/memory.c +1
-1@@ -2649,7 +2659,7 @@ static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)bool memory_region_is_mapped (MemoryRegion *mr) { -return mr->container ? true : false; +return !!mr->container || mr->mapped_via_alias; }/* Same as memory_region_find, but it does not add a reference to the
Re: [PATCH v2 3/3] memory: Update description of memory_region_is_mapped()
In reply to David HildenbrandQuoted 7 lines · David Hildenbrand
Let's update the documentation, making it clearer what the semantics of
memory_region_is_mapped()actually are.Signed-off-by David Hildenbrand <[email protected]>--- include/exec/memory.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 5 lines · David Hildenbrand
This is the follow-up of [1].
Playing with
memory_region_is_mapped(), I realized that memory regions mapped via an alias behave a little bit "differently", as they don't have their ->container set.
The patches look ok to me, though I have a few pure questions to ask..
Quoted 2 lines
*
memory_region_is_mapped()will never succeed for memory regions mapped via an alias
I think you mentioned that in commit message of patch 2 that it fixes no real problem so far, so I'm also wondering in which case it'll help. Say, normally when there's an alias of another MR and we want to know whether the MR is mapped, we simply call memory_region_is_mapped() upon the alias .
To verify my thoughts, I did look up a few memory_region_is_mapped() random callers that used with alias and that's what they did:
Here'sthe dino.c example:
*** hw/hppa/dino.c:
gsc_to_pci_forwarding[151] if (!memory_region_is_mapped(mem)) {
gsc_to_pci_forwarding[155] } else if (memory_region_is_mapped(mem)) {
The "mem" points to:
MemoryRegion *mem = &s->pci_mem_alias[i];
Which is the alias.
Another one:
*** hw/pci-host/pnv_phb3.c:
pnv_phb3_check_m32[121] if (memory_region_is_mapped(&phb->mr_m32)) {
pnv_phb3_update_regions[1076] if (memory_region_is_mapped(&phb->mr_m32)) {
Andmr_m32 is the alias MR itself:
memory_region_init_alias(&phb->mr_m32, OBJECT(phb), "phb3-m32",
&phb->pci_mmio, start, size);I mean, if it should always be very straightforward to fetch the alias mr, then I'm just afraid patch 2 won't really help in any real use case but pure overhead.
And I hope we won't trigger problem with any use case where memory_region_is_mapped() returned false previously but then it'll return true after patch 2, because logically with the old code one can detect explicitly on "whether this original MR is mapped somewhere, irrelevant of other alias mappings upon this mr". Patch 2 blurrs it from that pov.
Quoted 4 lines
*
memory_region_to_address_space(),memory_region_find(),memory_region_find_rcu(),memory_region_present()won't work, which seems okay, because we don't expect such memory regions getting passed to these functions.
Looks right.
Quoted 2 lines
*
memory_region_to_absolute_addr()will result in a wrong address. As the result is only used for tracing, that is tolerable.
memory_region_{read|write}_accessor() seem to be only called from the address space layer, so it looks fine even for tracing as it'll always fetch the alias, afaiu. Phil's patch may change that fact, though, it seems.
Thanks,
Signature · 1 line
Peter Xu
Quoted 7 lines · Peter Xu
Quoted 5 lines · David Hildenbrand
This is the follow-up of [1].
Playing with
memory_region_is_mapped(), I realized that memory regions mapped via an alias behave a little bit "differently", as they don't have their ->container set.
Hi Peter,
thanks for your review!
Quoted 9 lines
The patches look ok to me, though I have a few pure questions to ask..
Quoted 2 lines
*
memory_region_is_mapped()will never succeed for memory regions mapped via an aliasI think you mentioned that in commit message of patch 2 that it fixes no real problem so far, so I'm also wondering in which case it'll help. Say, normally when there's an alias of another MR and we want to know whether the MR is mapped, we simply call
memory_region_is_mapped()upon the alias .
Just to recap: in v1 I proposed to just document that it doesn't work on aliases, and folks weren't too happy to see regions mapped via aliases being special-cased where it might just be avoided.
Quoted 22 lines
To verify my thoughts, I did look up a few
memory_region_is_mapped()random callers that used with alias and that's what they did:Here'sthe
dino.cexample:***
hw/hppa/dino.c:
gsc_to_pci_forwarding[151] if (!memory_region_is_mapped(mem)) {
gsc_to_pci_forwarding[155] } else if (memory_region_is_mapped(mem)) {The "mem" points to:
MemoryRegion *mem = &s->pci_mem_alias[i];Which is the alias.
Another one:
***
hw/pci-host/pnv_phb3.c:
pnv_phb3_check_m32[121] if (memory_region_is_mapped(&phb->mr_m32)) {
pnv_phb3_update_regions[1076] if (memory_region_is_mapped(&phb->mr_m32)) {
Quoted 7 lines
Andmr_m32is the alias MR itself:memory_region_init_alias(&phb->mr_m32, OBJECT(phb), "phb3-m32", &phb->pci_mmio, start, size);I mean, if it should always be very straightforward to fetch the alias mr, then I'm just afraid patch 2 won't really help in any real use case but pure overhead.
That is true as long as it's not a mapping check in generic code. Say, we have a RAMBlock and use ->mr. Checking memory_region_is_mapped(rb->mr) is misleading if the region is mapped via aliases.
The reason I stumbled over this at all is a sanity check I added in
void memory_region_set_ram_discard_manager(MemoryRegion *mr,
RamDiscardManager *rdm)
{
g_assert(memory_region_is_ram(mr) && !memory_region_is_mapped(mr));
g_assert(!rdm || !mr->rdm);
mr->rdm = rdm;
}If mr is only mapped via aliases (see the virtio-mem memslot series), this check is of no value, because even if the mr would be mapped via aliases, we would not be able to catch it.
Having that said, the check is not 100% correct, because memory_region_is_mapped() does not indicate that we're actually mapped into an address space. But at least for memory devices (-> target use case of RamDiscardManager) with an underlying RAMBlock, it's pretty reliable -- and there is no easy way to check if we're mapped into an address space when aliases are involved.
Note that there is also a similar check in memory_region_is_mapped(), but I'm removing that as part of the virtio-mem memslot series, because it's not actually helpful in the context of RAMBlock migration (nothing might be mapped, but migration code would still try migrating such a RAMBlock and has to consider the RamDiscardManager).
Another example of a generic code check is patch #1: the only reason it works right now is because NVDIMMs cannot exist before initial memory is created. But yes, there is a better way of doing it when we have a memdev at hand.
Quoted 7 lines
And I hope we won't trigger problem with any use case where
memory_region_is_mapped()returned false previously but then it'll return true after patch 2, because logically with the old code one can detect explicitly on "whether this original MR is mapped somewhere, irrelevant of other alias mappings upon this mr". Patch 2 blurrs it from that pov.
I hope tests will catch that early. I ran some without surprises.
Quoted 5 lines
Quoted 4 lines
*
memory_region_to_address_space(),memory_region_find(),memory_region_find_rcu(),memory_region_present()won't work, which seems okay, because we don't expect such memory regions getting passed to these functions.
Quoted 8 lines
Looks right.
Quoted 2 lines
*
memory_region_to_absolute_addr()will result in a wrong address. As the result is only used for tracing, that is tolerable.memory_region_{read|write}_accessor() seem to be only called from the address space layer, so it looks fine even for tracing as it'll always fetch the alias, afaiu. Phil's patch may change that fact, though, it seems.
Unfortunately, not much we can do: a memory region might theoretically be mapped via aliases into different address spaces and into different
locations: there is no right answer to memory_region_to_absolute_addr() when only having the memory region at hand without an address space.
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Peter Xu
Reviewed-byQuoted 72 lines · David Hildenbrand
Quoted 7 lines · Peter Xu
Quoted 5 lines · David Hildenbrand
This is the follow-up of [1].
Playing with
memory_region_is_mapped(), I realized that memory regions mapped via an alias behave a little bit "differently", as they don't have their ->container set.Hi Peter,
thanks for your review!
Quoted 9 lines
The patches look ok to me, though I have a few pure questions to ask..
Quoted 2 lines
*
memory_region_is_mapped()will never succeed for memory regions mapped via an aliasI think you mentioned that in commit message of patch 2 that it fixes no real problem so far, so I'm also wondering in which case it'll help. Say, normally when there's an alias of another MR and we want to know whether the MR is mapped, we simply call
memory_region_is_mapped()upon the alias .Just to recap: in v1 I proposed to just document that it doesn't work on aliases, and folks weren't too happy to see regions mapped via aliases being special-cased where it might just be avoided.
Quoted 22 lines
To verify my thoughts, I did look up a few
memory_region_is_mapped()random callers that used with alias and that's what they did:Here'sthe
dino.cexample:***
hw/hppa/dino.c:
gsc_to_pci_forwarding[151] if (!memory_region_is_mapped(mem)) {
gsc_to_pci_forwarding[155] } else if (memory_region_is_mapped(mem)) {The "mem" points to:
MemoryRegion *mem = &s->pci_mem_alias[i];Which is the alias.
Another one:
***
hw/pci-host/pnv_phb3.c:
pnv_phb3_check_m32[121] if (memory_region_is_mapped(&phb->mr_m32)) {
pnv_phb3_update_regions[1076] if (memory_region_is_mapped(&phb->mr_m32)) {Quoted 7 lines
Andmr_m32is the alias MR itself:memory_region_init_alias(&phb->mr_m32, OBJECT(phb), "phb3-m32", &phb->pci_mmio, start, size);I mean, if it should always be very straightforward to fetch the alias mr, then I'm just afraid patch 2 won't really help in any real use case but pure overhead.
That is true as long as it's not a mapping check in generic code. Say, we have a RAMBlock and use ->mr. Checking
memory_region_is_mapped(rb->mr)is misleading if the region is mapped via aliases.The reason I stumbled over this at all is a sanity check I added in
void memory_region_set_ram_discard_manager(MemoryRegion *mr,
RamDiscardManager *rdm){ g_assert(memory_region_is_ram(mr) && !memory_region_is_mapped(mr)); g_assert(!rdm || !mr->rdm); mr->rdm = rdm; }
Quoted 3 lines
If mr is only mapped via aliases (see the virtio-mem memslot series), this check is of no value, because even if the mr would be mapped via aliases, we would not be able to catch it.
Yeah, this is a sane check to ask for.
Quoted 15 lines
Having that said, the check is not 100% correct, because
memory_region_is_mapped()does not indicate that we're actually mapped into an address space. But at least for memory devices (-> target use case of RamDiscardManager) with an underlying RAMBlock, it's pretty reliable -- and there is no easy way to check if we're mapped into an address space when aliases are involved.Note that there is also a similar check in
memory_region_is_mapped(), but I'm removing that as part of the virtio-mem memslot series, because it's not actually helpful in the context of RAMBlock migration (nothing might be mapped, but migration code would still try migrating such a RAMBlock and has to consider the RamDiscardManager).
Quoted 4 lines
Another example of a generic code check is patch #1: the only reason it works right now is because NVDIMMs cannot exist before initial memory is created. But yes, there is a better way of doing it when we have a memdev at hand.
IMHO patch 1 is actually an example showing that when we want that explicit meaning we can simply introduce another boolean in parent struct. :)
Quoted 21 lines
Quoted 8 lines
And I hope we won't trigger problem with any use case where
memory_region_is_mapped()returned false previously but then it'll return true after patch 2, because logically with the old code one can detect explicitly on "whether this original MR is mapped somewhere, irrelevant of other alias mappings upon this mr". Patch 2 blurrs it from that pov.I hope tests will catch that early. I ran some without surprises.
Quoted 10 lines
Quoted 4 lines
*
memory_region_to_address_space(),memory_region_find(),memory_region_find_rcu(),memory_region_present()won't work, which seems okay, because we don't expect such memory regions getting passed to these functions.Looks right.
Quoted 2 lines
*
memory_region_to_absolute_addr()will result in a wrong address. As the result is only used for tracing, that is tolerable.
Quoted 8 lines
Quoted 3 lines
memory_region_{read|write}_accessor() seem to be only called from the address space layer, so it looks fine even for tracing as it'll always fetch the alias, afaiu. Phil's patch may change that fact, though, it seems.
Unfortunately, not much we can do: a memory region might theoretically be mapped via aliases into different address spaces and into different
locations: there is no right answer tomemory_region_to_absolute_addr()when only having the memory region at hand without an address space.
Yes.
Now I think patch 2 is fine too, it'll be nicer imho to be a new API like memory_region_is_mapped_any() with comment showing it checks the aliases, but no strong opinion. If nothing fails with the change, it'll be the same indeed.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v2 2/3] memory: Make memory_region_is_mapped() succeed when mapped via an alias
In reply to Philippe Mathieu-DaudéQuoted 56 lines · Philippe Mathieu-Daudé
Quoted 55 lines · David Hildenbrand
memory_region_is_mapped()currently does not return "true" when a memory region is mapped via an alias.Assuming we have:
alias (A0) -> alias (A1) -> region (R0)Mapping A0 would currently only make
memory_region_is_mapped()succeed on A0, but not on A1 and R0.Let's fix that by adding a "
mapped_via_alias" counter to memory regions and updating it accordingly when an alias gets (un)mapped.I am not aware of actual issues, this is rather a cleanup to make it consistent.
Signed-off-by David Hildenbrand <[email protected]>--- include/exec/memory.h | 1 + softmmu/memory.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-)include/exec/memory.h +1
-0@@ -707,6 +707,7 @@ struct MemoryRegion {const MemoryRegionOps *ops;void *opaque;MemoryRegion *container; +int mapped_via_alias;/* Mapped via an alias, container might be NULL */ Int128 size;hwaddr addr;void (*destructor)(MemoryRegion *mr);softmmu/memory.c +6
-0@@ -2524,8 +2524,13 @@ static void memory_region_add_subregion_common(MemoryRegion *mr,hwaddr offset, MemoryRegion *subregion) { + MemoryRegion *alias; +assert (!subregion->container); subregion->container = mr; +for (alias = subregion->alias; alias; alias = alias->alias) { + alias->mapped_via_alias++; + } subregion->addr = offset;memory_region_update_container_subregions (subregion); }@@ -2550,9 +2555,14 @@ void memory_region_add_subregion_overlap(MemoryRegion *mr,void memory_region_del_subregion (MemoryRegion *mr,MemoryRegion *subregion) { +MemoryRegion *alias;
Quoted 8 lines
softmmu/memory.c void memory_region_add_subregion_overlap(MemoryRegion *mr, · line 2564
Quoted 6 lines
softmmu/memory.c +3
-0+memory_region_transaction_begin ();assert (subregion->container == mr); subregion->container =NULL ; +for (alias = subregion->alias; alias; alias = alias->alias) { + alias->mapped_via_alias--;assert(alias->mapped_via_alias >= 0);
Makes sense, I'll respin with that -- thanks!
Signature · 3 lines
Thanks, David / dhildenb