softmmu/physmem: Fix address of FlatView access in address_space_(read|write)_cached_slow()
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.
--- softmmu/physmem.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
softmmu/physmem.c +6 -6
@@ -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
Hi, Alberto,
Quoted 3 lines · Alberto Faria
Apply
cache->xlatto 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")Co-Developed-by Stefan Hajnoczi <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Alberto Faria <[email protected]>--- softmmu/physmem.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)softmmu/physmem.c +3
-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, +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
Quoted 10 lines · Peter Xu
Hi, Alberto,
Quoted 3 lines · Alberto Faria
Apply
cache->xlatto 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")Co-Developed-by Stefan Hajnoczi <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Alberto Faria <[email protected]>--- 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@@ -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->xlatis 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_regionandoffset_within_address_spaceshould 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,
Quoted 23 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Peter Xu
Hi, Alberto,
Quoted 3 lines · Alberto Faria
Apply
cache->xlatto 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")Co-Developed-by Stefan Hajnoczi <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Alberto Faria <[email protected]>--- softmmu/physmem.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
softmmu/physmem.c +0
-0Quoted 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->xlatis 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_spaceIf 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
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->xlatto 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")Co-Developed-by Stefan Hajnoczi <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Alberto Faria <[email protected]>--- softmmu/physmem.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
softmmu/physmem.c +0
-0Quoted 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 toaddress_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->xlatis 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.
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
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(), butflatview_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.