Skip to contentSkip to conversation

xen: mapcache: Fix unmapping of first the entry in a bucket

PATCH In review Edgar E. Iglesias 5 participants 13 messages Jul 1, 2024 – Jul 8, 2024

lore
Share & tools
lore
#0 lore raw

cover xen: mapcache: Fix unmapping of first the entry in a bucket

From: "Edgar E. Iglesias" <[email protected]>

This fixes the unmapping of the first mapping in a bucket of a mapcache.

We also add error handling to qemu_ram_block_from_host() to bail out when xen_ram_addr_from_mapcache() doesn't find an existing mapping.

Cheers,
Edgar

Edgar E. Iglesias (2):
physmem: Bail out qemu_ram_block_from_host() for invalid ram addrs
xen: mapcache: Fix unmapping of first entries in buckets

 hw/xen/xen-mapcache.c | 12 +++++++++++-
 system/physmem.c      |  4 ++++
 2 files changed, 15 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 physmem: Bail out qemu_ram_block_from_host() for invalid ram addrs

From: "Edgar E. Iglesias" <[email protected]>

Bail out in qemu_ram_block_from_host() when xen_ram_addr_from_mapcache() does not find an existing mapping.

---
 system/physmem.c | 4 ++++
 1 file changed, 4 insertions(+)
system/physmem.c +4 -0
index 33d09f7571..59d1576c2b 100644
@@ -2277,6 +2277,10 @@ RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
         ram_addr_t ram_addr;
         RCU_READ_LOCK_GUARD();
         ram_addr = xen_ram_addr_from_mapcache(ptr);
+        if (ram_addr == RAM_ADDR_INVALID) {
+            return NULL;
+        }
+
         block = qemu_get_ram_block(ram_addr);
         if (block) {
             *offset = ram_addr - block->offset;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v1 1/2] physmem: Bail out qemu_ram_block_from_host() for invalid ram addrs

Quoted 16 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

Bail out in qemu_ram_block_from_host() when xen_ram_addr_from_mapcache() does not find an existing mapping.

---
 system/physmem.c | 4 ++++
 1 file changed, 4 insertions(+)
system/physmem.c +0 -0
index 33d09f7571..59d1576c2b 100644

system/physmem.c RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset, · line 2283

Quoted 7 lines
system/physmem.c +4 -0
@@ -2277,6 +2277,10 @@ RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
         ram_addr_t ram_addr;
         RCU_READ_LOCK_GUARD();
         ram_addr = xen_ram_addr_from_mapcache(ptr);
+        if (ram_addr == RAM_ADDR_INVALID) {
+            return NULL;
+        }
+

Isn't this indicative of a failure? Should there at least be a trace point for failed mappings?

Quoted 3 lines
system/physmem.c +0 -0
         block = qemu_get_ram_block(ram_addr);
         if (block) {
             *offset = ram_addr - block->offset;
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#3 lore raw

Re: [PATCH v1 1/2] physmem: Bail out qemu_ram_block_from_host() for invalid ram addrs

Quoted 27 lines · Alex Bennée

system/physmem.c RAMBlock *qemu_ram_block_from_host(void *ptr,

Quoted 16 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

Bail out in qemu_ram_block_from_host() when xen_ram_addr_from_mapcache() does not find an existing mapping.

---
 system/physmem.c | 4 ++++
 1 file changed, 4 insertions(+)
system/physmem.c +0 -0
index 33d09f7571..59d1576c2b 100644
@@ -2277,6 +2277,10 @@ RAMBlock *qemu_ram_block_from_host(void *ptr,

bool round_offset,

system/physmem.c RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset, · line 2283

Quoted 7 lines
system/physmem.c +4 -0
         ram_addr_t ram_addr;
         RCU_READ_LOCK_GUARD();
         ram_addr = xen_ram_addr_from_mapcache(ptr);
+        if (ram_addr == RAM_ADDR_INVALID) {
+            return NULL;
+        }
+
Quoted 4 lines

Isn't this indicative of a failure? Should there at least be a trace point for failed mappings?

Yes but there are already trace points for the failure cases inside xen_ram_addr_from_mapcache().
Do those address your concerns or do you think we need additional trace points?

Cheers,
Edgar

Quoted 8 lines

system/physmem.c RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset, · line 2286

Quoted 3 lines
system/physmem.c +0 -0
         block = qemu_get_ram_block(ram_addr);
         if (block) {
             *offset = ram_addr - block->offset;

--
Alex Bennée
Virtualisation Tech Lead @ Linaro

Reviewed-by
#4 lore raw

Re: [PATCH v1 1/2] physmem: Bail out qemu_ram_block_from_host() for invalid ram addrs

Quoted 31 lines · Edgar E. Iglesias

On Thu, Jul 4, 2024 at 1:26 PM Alex Bennée <[email protected]> wrote:

Quoted 16 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

Bail out in qemu_ram_block_from_host() when xen_ram_addr_from_mapcache() does not find an existing mapping.

---
 system/physmem.c | 4 ++++
 1 file changed, 4 insertions(+)
system/physmem.c +0 -0
index 33d09f7571..59d1576c2b 100644

system/physmem.c RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset, · line 2283

Quoted 7 lines
system/physmem.c +4 -0
@@ -2277,6 +2277,10 @@ RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
         ram_addr_t ram_addr;
         RCU_READ_LOCK_GUARD();
         ram_addr = xen_ram_addr_from_mapcache(ptr);
+        if (ram_addr == RAM_ADDR_INVALID) {
+            return NULL;
+        }
+

Isn't this indicative of a failure? Should there at least be a trace point for failed mappings?

Quoted 3 lines

Yes but there are already trace points for the failure cases inside xen_ram_addr_from_mapcache(). Do those address your concerns or do you think we need additional
trace points?

Ahh that will do.

I am curious for the reasons why we might not have an entry in the mapcache. I guess the trace_xen_map_cache() covers all insertions into the cache although you need to check trace_xen_map_cache_return() to see if anything failed.

Anyway:

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on v1 1/2
#5 lore raw

Re: [PATCH v1 1/2] physmem: Bail out qemu_ram_block_from_host() for invalid ram addrs

In reply to Edgar E. Iglesias
Quoted 7 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

Bail out in qemu_ram_block_from_host() when xen_ram_addr_from_mapcache() does not find an existing mapping.

Quoted 22 lines
---
 system/physmem.c | 4 ++++
 1 file changed, 4 insertions(+)
system/physmem.c +4 -0
index 33d09f7571..59d1576c2b 100644
@@ -2277,6 +2277,10 @@ RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
         ram_addr_t ram_addr;
         RCU_READ_LOCK_GUARD();
         ram_addr = xen_ram_addr_from_mapcache(ptr);
+        if (ram_addr == RAM_ADDR_INVALID) {
+            return NULL;
+        }
+
         block = qemu_get_ram_block(ram_addr);
         if (block) {
             *offset = ram_addr - block->offset;
Signature · 1 line
2.43.0
#6 lore raw

PATCH 2/2 xen: mapcache: Fix unmapping of first entries in buckets

In reply to Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

This fixes the clobbering of the entry->next pointer when unmapping the first entry in a bucket of a mapcache.

Fixes 123acd816d ("xen: mapcache: Unmap first entries in buckets")
---
 hw/xen/xen-mapcache.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/xen/xen-mapcache.c +11 -1
index 5f23b0adbe..18ba7b1d8f 100644
@@ -597,7 +597,17 @@ static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc,
         pentry->next = entry->next;
         g_free(entry);
     } else {
-        memset(entry, 0, sizeof *entry);
+        /*
+         * Invalidate mapping but keep entry->next pointing to the rest
+         * of the list.
+         *
+         * Note that lock is already zero here, otherwise we don't unmap.
+         */
+        entry->paddr_index = 0;
+        entry->vaddr_base = NULL;
+        entry->valid_mapping = NULL;
+        entry->flags = 0;
+        entry->size = 0;
     }
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH v1 2/2] xen: mapcache: Fix unmapping of first entries in buckets

Quoted 22 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

This fixes the clobbering of the entry->next pointer when unmapping the first entry in a bucket of a mapcache.

Fixes 123acd816d ("xen: mapcache: Unmap first entries in buckets")
---
 hw/xen/xen-mapcache.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/xen/xen-mapcache.c +1 -1
index 5f23b0adbe..18ba7b1d8f 100644
@@ -597,7 +597,17 @@ static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc,
         pentry->next = entry->next;
         g_free(entry);
     } else {
-        memset(entry, 0, sizeof *entry);
+        /*

hw/xen/xen-mapcache.c static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc, · line 610

Quoted 10 lines
hw/xen/xen-mapcache.c +10 -0
+         * Invalidate mapping but keep entry->next pointing to the rest
+         * of the list.
+         *
+         * Note that lock is already zero here, otherwise we don't unmap.
+         */
+        entry->paddr_index = 0;
+        entry->vaddr_base = NULL;
+        entry->valid_mapping = NULL;
+        entry->flags = 0;
+        entry->size = 0;

This kind of feels like mc->entry should be an array of pointer rather than an array of MapCacheEntry but that seems to work well enough and not the first time entries are been cleared like that.

Thanks,

Signature · 5 lines
Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#8 lore raw

Re: [PATCH v1 2/2] xen: mapcache: Fix unmapping of first entries in buckets

Quoted 29 lines · Anthony PERARD
Quoted 28 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

This fixes the clobbering of the entry->next pointer when unmapping the first entry in a bucket of a mapcache.

Fixes 123acd816d ("xen: mapcache: Unmap first entries in buckets")
---
 hw/xen/xen-mapcache.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/xen/xen-mapcache.c +7 -1
index 5f23b0adbe..18ba7b1d8f 100644
@@ -597,7 +597,17 @@ static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc,
         pentry->next = entry->next;
         g_free(entry);
     } else {
-        memset(entry, 0, sizeof *entry);
+        /*
+         * Invalidate mapping but keep entry->next pointing to the rest
+         * of the list.
+         *
+         * Note that lock is already zero here, otherwise we don't unmap.
+         */
+        entry->paddr_index = 0;
Quoted 8 lines

hw/xen/xen-mapcache.c static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc, · line 610

Quoted 4 lines
hw/xen/xen-mapcache.c +4 -0
+        entry->vaddr_base = NULL;
+        entry->valid_mapping = NULL;
+        entry->flags = 0;
+        entry->size = 0;

This kind of feels like mc->entry should be an array of pointer rather than an array of MapCacheEntry but that seems to work well enough and not the first time entries are been cleared like that.

The use of a hand rolled list is a bit of a concern considering QEMU and Glib both provide various abstractions used around the rest of the code base. The original patch that introduces the mapcache doesn't tell me much about access patterns for the cache, just that it is trying to solve memory exhaustion issues with lots of dynamic small mappings.

Maybe a simpler structure is desirable?

We also have an interval tree implementation ("qemu/interval-tree.h") if what we really want is a sorted tree of memory that can be iterated locklessly.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#9 lore raw

Re: [PATCH v1 2/2] xen: mapcache: Fix unmapping of first entries in buckets

Quoted 46 lines · Alex Bennée
Quoted 29 lines · Anthony PERARD
Quoted 28 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

This fixes the clobbering of the entry->next pointer when unmapping the first entry in a bucket of a mapcache.

Fixes 123acd816d ("xen: mapcache: Unmap first entries in buckets")
---
 hw/xen/xen-mapcache.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/xen/xen-mapcache.c +7 -1
index 5f23b0adbe..18ba7b1d8f 100644
@@ -597,7 +597,17 @@ static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc,
         pentry->next = entry->next;
         g_free(entry);
     } else {
-        memset(entry, 0, sizeof *entry);
+        /*
+         * Invalidate mapping but keep entry->next pointing to the rest
+         * of the list.
+         *
+         * Note that lock is already zero here, otherwise we don't unmap.
+         */
+        entry->paddr_index = 0;
Quoted 8 lines

hw/xen/xen-mapcache.c static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc, · line 610

Quoted 4 lines
hw/xen/xen-mapcache.c +4 -0
+        entry->vaddr_base = NULL;
+        entry->valid_mapping = NULL;
+        entry->flags = 0;
+        entry->size = 0;

This kind of feels like mc->entry should be an array of pointer rather than an array of MapCacheEntry but that seems to work well enough and not the first time entries are been cleared like that.

The use of a hand rolled list is a bit of a concern considering QEMU and Glib both provide various abstractions used around the rest of the code base. The original patch that introduces the mapcache doesn't tell me much about access patterns for the cache, just that it is trying to solve memory exhaustion issues with lots of dynamic small mappings.

Quoted 6 lines

Maybe a simpler structure is desirable?

We also have an interval tree implementation ("qemu/interval-tree.h") if what we really want is a sorted tree of memory that can be iterated locklessly.

Yes, it would be interesting to benchmark other options. I agree that we should at minimum reuse existing lists/hash tables.

We've also had some discussions around removing it partially or alltogether but there are some concerns around that. We're going to need something to keep track of grants. For 32-bit hosts, it's a problem to exhaust virtual address-space if mapping all of the guest (are folks still using 32-bit hosts?). There may be other issues aswell.

Some benefits are that we'll remove some of the complexity and latency for mapping and unmapping stuff continously.

Cheers,
Edgar

#10 lore raw

Re: [PATCH v1 2/2] xen: mapcache: Fix unmapping of first entries in buckets

Quoted 68 lines · Edgar E. Iglesias
Quoted 15 lines · Alex Bennée
Quoted 13 lines · Anthony PERARD
Quoted 12 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

This fixes the clobbering of the entry->next pointer when unmapping the first entry in a bucket of a mapcache.

Fixes 123acd816d ("xen: mapcache: Unmap first entries in buckets")
---
 hw/xen/xen-mapcache.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
Quoted 5 lines
Quoted 5 lines

hw/xen/xen-mapcache.c static void

Quoted 5 lines
hw/xen/xen-mapcache.c +0 -0
index 5f23b0adbe..18ba7b1d8f 100644
@@ -597,7 +597,17 @@ static void

xen_invalidate_map_cache_entry_unlocked(MapCache *mc,

Quoted 6 lines
Quoted 6 lines

hw/xen/xen-mapcache.c static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc,

Quoted 6 lines
hw/xen/xen-mapcache.c +2 -1
         pentry->next = entry->next;
         g_free(entry);
     } else {
-        memset(entry, 0, sizeof *entry);
+        /*
+         * Invalidate mapping but keep entry->next pointing to the

rest

Quoted 3 lines
Quoted 3 lines

hw/xen/xen-mapcache.c static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc,

Quoted 3 lines
hw/xen/xen-mapcache.c +3 -0
+         * of the list.
+         *
+         * Note that lock is already zero here, otherwise we don't

unmap.

Quoted 17 lines
Quoted 10 lines

hw/xen/xen-mapcache.c static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc, · line 610

Quoted 6 lines
hw/xen/xen-mapcache.c +6 -0
+         */
+        entry->paddr_index = 0;
+        entry->vaddr_base = NULL;
+        entry->valid_mapping = NULL;
+        entry->flags = 0;
+        entry->size = 0;

This kind of feels like mc->entry should be an array of pointer rather than an array of MapCacheEntry but that seems to work well enough and not the first time entries are been cleared like that.

The use of a hand rolled list is a bit of a concern considering QEMU and Glib both provide various abstractions used around the rest of the code base. The original patch that introduces the mapcache doesn't tell me much about access patterns for the cache, just that it is trying to solve memory exhaustion issues with lots of dynamic small mappings.

Quoted 6 lines

Maybe a simpler structure is desirable?

We also have an interval tree implementation ("qemu/interval-tree.h") if what we really want is a sorted tree of memory that can be iterated locklessly.

Yes, it would be interesting to benchmark other options. I agree that we should at minimum reuse existing lists/hash tables.

We've also had some discussions around removing it partially or alltogether but
there are some concerns around that. We're going to need something to keep track of grants. For 32-bit hosts, it's a problem to exhaust virtual address-space if mapping all of the guest (are folks still using 32-bit hosts?).
There may be other issues aswell.

Quoted 5 lines

Some benefits are that we'll remove some of the complexity and latency for mapping
and unmapping stuff continously.

One more thing I forgot to add is that IMO, these larger longer term changes should not block this tiny bugfix...

Cheers,
Edgar

#11 lore raw

Re: [PATCH v1 2/2] xen: mapcache: Fix unmapping of first entries in buckets

Quoted 65 lines · Edgar E. Iglesias

On Thu, Jul 4, 2024 at 9:48 PM Edgar E. Iglesias <[email protected]> wrote:

Quoted 46 lines · Alex Bennée
Quoted 29 lines · Anthony PERARD
Quoted 28 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

This fixes the clobbering of the entry->next pointer when unmapping the first entry in a bucket of a mapcache.

Fixes 123acd816d ("xen: mapcache: Unmap first entries in buckets")
---
 hw/xen/xen-mapcache.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/xen/xen-mapcache.c +7 -1
index 5f23b0adbe..18ba7b1d8f 100644
@@ -597,7 +597,17 @@ static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc,
         pentry->next = entry->next;
         g_free(entry);
     } else {
-        memset(entry, 0, sizeof *entry);
+        /*
+         * Invalidate mapping but keep entry->next pointing to the rest
+         * of the list.
+         *
+         * Note that lock is already zero here, otherwise we don't unmap.
+         */
+        entry->paddr_index = 0;
Quoted 8 lines

hw/xen/xen-mapcache.c static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc, · line 610

Quoted 4 lines
hw/xen/xen-mapcache.c +4 -0
+        entry->vaddr_base = NULL;
+        entry->valid_mapping = NULL;
+        entry->flags = 0;
+        entry->size = 0;

This kind of feels like mc->entry should be an array of pointer rather than an array of MapCacheEntry but that seems to work well enough and not the first time entries are been cleared like that.

The use of a hand rolled list is a bit of a concern considering QEMU and Glib both provide various abstractions used around the rest of the code base. The original patch that introduces the mapcache doesn't tell me much about access patterns for the cache, just that it is trying to solve memory exhaustion issues with lots of dynamic small mappings.

Quoted 6 lines

Maybe a simpler structure is desirable?

We also have an interval tree implementation ("qemu/interval-tree.h") if what we really want is a sorted tree of memory that can be iterated locklessly.

Yes, it would be interesting to benchmark other options. I agree that we should at minimum reuse existing lists/hash tables.

We've also had some discussions around removing it partially or alltogether but there are some concerns around that. We're going to need something to keep track of grants. For 32-bit hosts, it's a problem to exhaust virtual address-space if mapping all of the guest (are folks still using 32-bit hosts?). There may be other issues aswell.

Quoted 5 lines

Some benefits are that we'll remove some of the complexity and latency for mapping and unmapping stuff continously.

One more thing I forgot to add is that IMO, these larger longer term changes should not block this tiny bugfix...

Agreed.

Quoted 3 lines

Cheers,
Edgar

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on v1 2/2
#12 lore raw

Re: [PATCH v1 2/2] xen: mapcache: Fix unmapping of first entries in buckets

In reply to Edgar E. Iglesias
Quoted 8 lines · Edgar E. Iglesias

From: "Edgar E. Iglesias" <[email protected]>

This fixes the clobbering of the entry->next pointer when unmapping the first entry in a bucket of a mapcache.

Fixes 123acd816d ("xen: mapcache: Unmap first entries in buckets")
Quoted 30 lines
---
 hw/xen/xen-mapcache.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/xen/xen-mapcache.c +11 -1
index 5f23b0adbe..18ba7b1d8f 100644
@@ -597,7 +597,17 @@ static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc,
         pentry->next = entry->next;
         g_free(entry);
     } else {
-        memset(entry, 0, sizeof *entry);
+        /*
+         * Invalidate mapping but keep entry->next pointing to the rest
+         * of the list.
+         *
+         * Note that lock is already zero here, otherwise we don't unmap.
+         */
+        entry->paddr_index = 0;
+        entry->vaddr_base = NULL;
+        entry->valid_mapping = NULL;
+        entry->flags = 0;
+        entry->size = 0;
     }
 }
 
Signature · 1 line
2.43.0