Skip to contentSkip to conversation

softmmu/physmem: Fix address of FlatView access in address_space_(read|write)_cached_slow()

PATCH Superseded Alberto Faria 3 participants 7 messages Aug 26, 2022 – Sep 4, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 softmmu/physmem: Fix address of FlatView access in address_space_(read|write)_cached_slow()

Apply cache->xlat to addr before passing it to flatview_(read|write)_continue(), to convert it from the MemoryRegionCache's address space to the FlatView's.

Fixes 48564041a7 ("exec: reintroduce MemoryRegion caching")
---
 softmmu/physmem.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
softmmu/physmem.c +6 -6
index dc3c3e5f2e..95d4c77cc3 100644
@@ -3450,9 +3450,9 @@ address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr,
     l = len;
     mr = address_space_translate_cached(cache, addr, &addr1, &l, false,
                                         MEMTXATTRS_UNSPECIFIED);
-    return flatview_read_continue(cache->fv,
-                                  addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                  addr1, l, mr);
+    return flatview_read_continue(cache->fv, cache->xlat + addr,
+                                  MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                  mr);
 }
 
 /* Called from RCU critical section. address_space_write_cached uses this
@@ -3468,9 +3468,9 @@ address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr,
     l = len;
     mr = address_space_translate_cached(cache, addr, &addr1, &l, true,
                                         MEMTXATTRS_UNSPECIFIED);
-    return flatview_write_continue(cache->fv,
-                                   addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                   addr1, l, mr);
+    return flatview_write_continue(cache->fv, cache->xlat + addr,
+                                   MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                   mr);
 }
 
 #define ARG1_DECL                MemoryRegionCache *cache
Signature · 1 line
2.37.2
#1 lore raw

Hi, Alberto,

Quoted 3 lines · Alberto Faria

Apply cache->xlat to addr before passing it to flatview_(read|write)_continue(), to convert it from the MemoryRegionCache's address space to the FlatView's.

Any bug encountered? It'll be great to add more information into the commit message if there is. We could also mention the issue was observed by code review or so.

Quoted 27 lines
Fixes 48564041a7 ("exec: reintroduce MemoryRegion caching")
---
 softmmu/physmem.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
softmmu/physmem.c +3 -3
index dc3c3e5f2e..95d4c77cc3 100644
@@ -3450,9 +3450,9 @@ address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr,
     l = len;
     mr = address_space_translate_cached(cache, addr, &addr1, &l, false,
                                         MEMTXATTRS_UNSPECIFIED);
-    return flatview_read_continue(cache->fv,
-                                  addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                  addr1, l, mr);
+    return flatview_read_continue(cache->fv, cache->xlat + addr,
+                                  MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                  mr);
 }
 
 /* Called from RCU critical section. address_space_write_cached uses this

softmmu/physmem.c address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr, · line 3474

Quoted 10 lines
softmmu/physmem.c +3 -3
@@ -3468,9 +3468,9 @@ address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr,
     l = len;
     mr = address_space_translate_cached(cache, addr, &addr1, &l, true,
                                         MEMTXATTRS_UNSPECIFIED);
-    return flatview_write_continue(cache->fv,
-                                   addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                   addr1, l, mr);
+    return flatview_write_continue(cache->fv, cache->xlat + addr,
+                                   MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                   mr);
 }

The issue looks correct, but I hesitate on the fix.. since afaict cache->xlat is per memory region not flat view, so the new calculation is offset within memory region but not flat view too.

So I'm wondering whether it should become:

cache->xlat + addr - cache.mrs.offset_within_region +
cache.mrs.offset_within_address_space

If the issue happens on vIOMMU+virtio on x86, then offset_within_region and offset_within_address_space should be all zeros for vt-d emulation since vt-d only has one huge vIOMMU window, then the outcome could be the same. However maybe there might be a difference with other vIOMMUs.

Thanks,

Signature · 1 line
Peter Xu
#2 lore raw
Quoted 10 lines · Peter Xu

Hi, Alberto,

Quoted 3 lines · Alberto Faria

Apply cache->xlat to addr before passing it to flatview_(read|write)_continue(), to convert it from the MemoryRegionCache's address space to the FlatView's.

Any bug encountered? It'll be great to add more information into the commit message if there is. We could also mention the issue was observed by code review or so.

Agreed.

Quoted 9 lines
Quoted 9 lines
Fixes 48564041a7 ("exec: reintroduce MemoryRegion caching")
---
  softmmu/physmem.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
Quoted 12 lines

softmmu/physmem.c address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr, · line 3453

Quoted 12 lines
softmmu/physmem.c +1 -3
index dc3c3e5f2e..95d4c77cc3 100644
@@ -3450,9 +3450,9 @@ address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr,
      l = len;
      mr = address_space_translate_cached(cache, addr, &addr1, &l, false,
                                          MEMTXATTRS_UNSPECIFIED);
-    return flatview_read_continue(cache->fv,
-                                  addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                  addr1, l, mr);
+    return flatview_read_continue(cache->fv, cache->xlat + addr,

So this is just addr1...

Quoted 21 lines
Quoted 6 lines
softmmu/physmem.c +2 -0
+                                  MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                  mr);
  }
 
  /* Called from RCU critical section. address_space_write_cached uses this

softmmu/physmem.c address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr, · line 3474

Quoted 10 lines
softmmu/physmem.c +3 -3
@@ -3468,9 +3468,9 @@ address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr,
      l = len;
      mr = address_space_translate_cached(cache, addr, &addr1, &l, true,
                                          MEMTXATTRS_UNSPECIFIED);
-    return flatview_write_continue(cache->fv,
-                                   addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                   addr1, l, mr);
+    return flatview_write_continue(cache->fv, cache->xlat + addr,
+                                   MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                   mr);
  }

The issue looks correct, but I hesitate on the fix.. since afaict cache->xlat is per memory region not flat view, so the new calculation is offset within memory region but not flat view too.

Quoted 4 lines

So I'm wondering whether it should become:

   cache->xlat + addr - cache.mrs.offset_within_region +
     cache.mrs.offset_within_address_space

If so, shouldn't this be calculated [*] within address_space_translate_cached() instead of the caller?

[*] Maybe passed as another pointer to hwaddr

Quoted 7 lines

If the issue happens on vIOMMU+virtio on x86, then offset_within_region and offset_within_address_space should be all zeros for vt-d emulation since vt-d only has one huge vIOMMU window, then the outcome could be the same. However maybe there might be a difference with other vIOMMUs.

Thanks,

#3 lore raw
Quoted 23 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Peter Xu

Hi, Alberto,

Quoted 3 lines · Alberto Faria

Apply cache->xlat to addr before passing it to flatview_(read|write)_continue(), to convert it from the MemoryRegionCache's address space to the FlatView's.

Any bug encountered? It'll be great to add more information into the commit message if there is. We could also mention the issue was observed by code review or so.

Agreed.

Quoted 9 lines
Quoted 9 lines
Fixes 48564041a7 ("exec: reintroduce MemoryRegion caching")
---
  softmmu/physmem.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
softmmu/physmem.c +0 -0
index dc3c3e5f2e..95d4c77cc3 100644
Quoted 8 lines

softmmu/physmem.c address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr, · line 3453

Quoted 8 lines
softmmu/physmem.c +1 -3
@@ -3450,9 +3450,9 @@ address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr,
      l = len;
      mr = address_space_translate_cached(cache, addr, &addr1, &l, false,
                                          MEMTXATTRS_UNSPECIFIED);
-    return flatview_read_continue(cache->fv,
-                                  addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                  addr1, l, mr);
+    return flatview_read_continue(cache->fv, cache->xlat + addr,

So this is just addr1...

It may not; note that in address_space_translate_cached() the xlat pointer will also be passed over to address_space_translate_iommu(), so it can be further modified to point to the real MR address behind the IOMMU.

Quoted 23 lines
Quoted 23 lines
Quoted 6 lines
softmmu/physmem.c +2 -0
+                                  MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                  mr);
  }
  /* Called from RCU critical section. address_space_write_cached uses this

softmmu/physmem.c address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr, · line 3474

Quoted 10 lines
softmmu/physmem.c +3 -3
@@ -3468,9 +3468,9 @@ address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr,
      l = len;
      mr = address_space_translate_cached(cache, addr, &addr1, &l, true,
                                          MEMTXATTRS_UNSPECIFIED);
-    return flatview_write_continue(cache->fv,
-                                   addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                   addr1, l, mr);
+    return flatview_write_continue(cache->fv, cache->xlat + addr,
+                                   MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                   mr);
  }

The issue looks correct, but I hesitate on the fix.. since afaict cache->xlat is per memory region not flat view, so the new calculation is offset within memory region but not flat view too.

So I'm wondering whether it should become:

Quoted 7 lines
Quoted 2 lines
   cache->xlat + addr - cache.mrs.offset_within_region +
     cache.mrs.offset_within_address_space

If so, shouldn't this be calculated [*] within address_space_translate_cached() instead of the caller?

[*] Maybe passed as another pointer to hwaddr

No strong opinion, but that looks like a duplication of information we have. Maybe we can also have a helper to convert the cache address space to global address space.

Signature · 1 line
Peter Xu
#4 lore raw
Quoted 39 lines · Peter Xu
Quoted 23 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Peter Xu

Hi, Alberto,

Quoted 3 lines · Alberto Faria

Apply cache->xlat to addr before passing it to flatview_(read|write)_continue(), to convert it from the MemoryRegionCache's address space to the FlatView's.

Any bug encountered? It'll be great to add more information into the commit message if there is. We could also mention the issue was observed by code review or so.

Agreed.

Quoted 9 lines
Quoted 9 lines
Fixes 48564041a7 ("exec: reintroduce MemoryRegion caching")
---
   softmmu/physmem.c | 12 ++++++------
   1 file changed, 6 insertions(+), 6 deletions(-)
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
softmmu/physmem.c +0 -0
index dc3c3e5f2e..95d4c77cc3 100644
Quoted 8 lines

softmmu/physmem.c address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr, · line 3453

Quoted 8 lines
softmmu/physmem.c +1 -3
@@ -3450,9 +3450,9 @@ address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr,
       l = len;
       mr = address_space_translate_cached(cache, addr, &addr1, &l, false,
                                           MEMTXATTRS_UNSPECIFIED);
-    return flatview_read_continue(cache->fv,
-                                  addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                  addr1, l, mr);
+    return flatview_read_continue(cache->fv, cache->xlat + addr,

So this is just addr1...

Quoted 3 lines

It may not; note that in address_space_translate_cached() the xlat pointer will also be passed over to address_space_translate_iommu(), so it can be further modified to point to the real MR address behind the IOMMU.

Oh, I missed that call.

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 6 lines
softmmu/physmem.c +2 -0
+                                  MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                  mr);
   }
   /* Called from RCU critical section. address_space_write_cached uses this

softmmu/physmem.c address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr, · line 3474

Quoted 10 lines
softmmu/physmem.c +3 -3
@@ -3468,9 +3468,9 @@ address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr,
       l = len;
       mr = address_space_translate_cached(cache, addr, &addr1, &l, true,
                                           MEMTXATTRS_UNSPECIFIED);
-    return flatview_write_continue(cache->fv,
-                                   addr, MEMTXATTRS_UNSPECIFIED, buf, len,
-                                   addr1, l, mr);
+    return flatview_write_continue(cache->fv, cache->xlat + addr,
+                                   MEMTXATTRS_UNSPECIFIED, buf, len, addr1, l,
+                                   mr);
   }

The issue looks correct, but I hesitate on the fix.. since afaict cache->xlat is per memory region not flat view, so the new calculation is offset within memory region but not flat view too.

So I'm wondering whether it should become:

    cache->xlat + addr - cache.mrs.offset_within_region +
      cache.mrs.offset_within_address_space
Quoted 8 lines
Quoted 4 lines

If so, shouldn't this be calculated [*] within address_space_translate_cached() instead of the caller?

[*] Maybe passed as another pointer to hwaddr

No strong opinion, but that looks like a duplication of information we have. Maybe we can also have a helper to convert the cache address space to global address space.

Ah yes, a helper is clever.

#5 lore raw
In reply to Peter Xu

Hi Peter,

Quoted 3 lines · Peter Xu

Any bug encountered? It'll be great to add more information into the commit message if there is. We could also mention the issue was observed by code review or so.

I came across this when performing unaligned 8-byte writes on x86_64-softmmu. The write was aligned to 4 bytes but not 8, so the first 4 bytes were written just fine by address_space_translate_cached(), but flatview_write_continue() failed to write the following 4 bytes as it was translating the wrong address. I'll mention this in the commit description.

Thanks,
Alberto

#6 lore raw
Quoted 7 lines · Alberto Campinho Faria

Hi Peter,

Quoted 3 lines · Peter Xu

Any bug encountered? It'll be great to add more information into the commit message if there is. We could also mention the issue was observed by code review or so.

Quoted 6 lines

I came across this when performing unaligned 8-byte writes on x86_64-softmmu. The write was aligned to 4 bytes but not 8, so the first 4 bytes were written just fine by address_space_translate_cached(), but flatview_write_continue() failed to write the following 4 bytes as it was translating the wrong address. I'll mention this in the commit description.

Oops, I meant that the first 4 bytes were written just fine by flatview_write_continue(), but since it couldn't write the 8 bytes in one go, it then called flatview_translate(), which translated the wrong address.