Fix MemoryRegionSection alignment and comparison
cover Fix MemoryRegionSection alignment and comparison
From: "Dr. David Alan Gilbert" <[email protected]>
This fixes a symptom I've seen on vhost-user on aarch64 where the daemon would be falsely notified of memory region changes that didn't exist.
The underlying problem was me memcmp'ing MemoryRegionSections even though they had padding in.
(Discovered while getting virtiofs working on aarch)
Dave
Dr. David Alan Gilbert (2):
memory: Align and add helper for comparing MemoryRegionSections
vhost: Fix memory region section comparison
hw/virtio/vhost.c | 9 +++++++-- include/exec/memory.h | 14 +++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.0
PATCH 1/2 memory: Align and add helper for comparing MemoryRegionSections
From: "Dr. David Alan Gilbert" <[email protected]>
MemoryRegionSection includes an Int128 'size' field; on some platforms the compiler causes an alignment of this to a 128bit boundary, leaving 8 bytes of dead space. This deadspace can be filled with junk.
Move the size field to the top avoiding unnecsssary alignment and provide an 'eq' routine to safely compare MRS's.
--- include/exec/memory.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
include/exec/memory.h +13 -1
@@ -487,15 +487,27 @@ static inline FlatView *address_space_to_flatview(AddressSpace *as)* @nonvolatile: this section is non-volatile */ struct MemoryRegionSection { +Int128 size;MemoryRegion *mr;FlatView *fv;hwaddr offset_within_region; -Int128 size;hwaddr offset_within_address_space;bool readonly;bool nonvolatile; }; +static inline bool MemoryRegionSection_eq (MemoryRegionSection *a, +MemoryRegionSection *b) +{ +return a->mr == b->mr && + a->fv == b->fv && + a->offset_within_region == b->offset_within_region && + a->offset_within_address_space == b->offset_within_address_space && +int128_eq (a->size, b->size) && + a->readonly == b->readonly && + a->nonvolatile == b->nonvolatile; +} +/** * memory_region_init: Initialize a memory region *
Signature · 1 line
2.21.0
Re: [Qemu-devel] [PATCH 1/2] memory: Align and add helper for comparing MemoryRegionSections
Hi David,
Quoted 8 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
MemoryRegionSection includes an Int128 'size' field; on some platforms the compiler causes an alignment of this to a 128bit boundary, leaving 8 bytes of dead space. This deadspace can be filled with junk.
Move the size field to the top avoiding unnecsssary alignment
"unnecessary"
This is enough change to be in its own commit.
---
Quoted 1 line
and provide an 'eq' routine to safely compare MRS's.
This is another change, and should be squashed in the next patch IMO. Doesn't Clang warn about unused 'static inline' btw?
Quoted 40 lines
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/exec/memory.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)include/exec/memory.h +13
-1@@ -487,15 +487,27 @@ static inline FlatView *address_space_to_flatview(AddressSpace *as)* @nonvolatile: this section is non-volatile */ struct MemoryRegionSection { +Int128 size;MemoryRegion *mr;FlatView *fv;hwaddr offset_within_region; -Int128 size;hwaddr offset_within_address_space;bool readonly;bool nonvolatile; }; +static inline bool MemoryRegionSection_eq (MemoryRegionSection *a, +MemoryRegionSection *b) +{ +return a->mr == b->mr && + a->fv == b->fv && + a->offset_within_region == b->offset_within_region && + a->offset_within_address_space == b->offset_within_address_space && +int128_eq (a->size, b->size) && + a->readonly == b->readonly && + a->nonvolatile == b->nonvolatile; +} +/** * memory_region_init: Initialize a memory region *
Re: [Qemu-devel] [PATCH 1/2] memory: Align and add helper for comparing MemoryRegionSections
Quoted 5 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
Hi David,
Quoted 2 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Quoted 8 lines
Quoted 6 lines
MemoryRegionSection includes an Int128 'size' field; on some platforms the compiler causes an alignment of this to a 128bit boundary, leaving 8 bytes of dead space. This deadspace can be filled with junk.
Move the size field to the top avoiding unnecsssary alignment
"unnecessary"
Oops thanks.
Quoted 7 lines
This is enough change to be in its own commit.
---
Quoted 1 line
and provide an 'eq' routine to safely compare MRS's.
This is another change, and should be squashed in the next patch IMO.
OK, what I'll do is I'll split this one into two ; I feel better having the extra function here separate from the next commit.
Quoted 1 line
Doesn't Clang warn about unused 'static inline' btw?
I was using gcc; but we seem to have loads of static inline's - what would make this one different?
Dave
Quoted 24 lines
Quoted 24 lines
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/exec/memory.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)include/exec/memory.h +1
-1@@ -487,15 +487,27 @@ static inline FlatView *address_space_to_flatview(AddressSpace *as)* @nonvolatile: this section is non-volatile */ struct MemoryRegionSection { +Int128 size;MemoryRegion *mr;FlatView *fv;hwaddr offset_within_region; -Int128 size;hwaddr offset_within_address_space;bool readonly;bool nonvolatile; };
Quoted 16 lines
Quoted 6 lines
include/exec/memory.h +5
-0+static inline bool MemoryRegionSection_eq (MemoryRegionSection *a, +MemoryRegionSection *b) +{ +return a->mr == b->mr && + a->fv == b->fv &&include/exec/memory.h static inline FlatView *address_space_to_flatview(AddressSpace *as) · line 513
Quoted 10 lines
include/exec/memory.h +7
-0+ a->offset_within_region == b->offset_within_region && + a->offset_within_address_space == b->offset_within_address_space && +int128_eq (a->size, b->size) && + a->readonly == b->readonly && + a->nonvolatile == b->nonvolatile; +} +/** * memory_region_init: Initialize a memory region *
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Philippe Mathieu-Daudé
Reviewed-byRe: [Qemu-devel] [PATCH 1/2] memory: Align and add helper for comparing MemoryRegionSections
Quoted 19 lines · Dr. David Alan Gilbert
Quoted 5 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
Hi David,
Quoted 2 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Quoted 8 lines
Quoted 6 lines
MemoryRegionSection includes an Int128 'size' field; on some platforms the compiler causes an alignment of this to a 128bit boundary, leaving 8 bytes of dead space. This deadspace can be filled with junk.
Move the size field to the top avoiding unnecsssary alignment
"unnecessary"
Oops thanks.
Quoted 2 lines
This is enough change to be in its own commit.
Quoted 8 lines
Quoted 5 lines
---
Quoted 1 line
and provide an 'eq' routine to safely compare MRS's.
This is another change, and should be squashed in the next patch IMO.
OK, what I'll do is I'll split this one into two ; I feel better having the extra function here separate from the next commit.
Thanks, feel free to add to both:
Quoted 4 lines
Quoted 1 line
Doesn't Clang warn about unused 'static inline' btw?
I was using gcc; but we seem to have loads of static inline's - what would make this one different?
I guess I was confused with 'static (no-inline)' :)
Quoted 43 lines
Quoted 24 lines
Quoted 24 lines
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/exec/memory.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)include/exec/memory.h +1
-1@@ -487,15 +487,27 @@ static inline FlatView *address_space_to_flatview(AddressSpace *as)* @nonvolatile: this section is non-volatile */ struct MemoryRegionSection { +Int128 size;MemoryRegion *mr;FlatView *fv;hwaddr offset_within_region; -Int128 size;hwaddr offset_within_address_space;bool readonly;bool nonvolatile; };Quoted 16 lines
Quoted 6 lines
include/exec/memory.h +5
-0+static inline bool MemoryRegionSection_eq (MemoryRegionSection *a, +MemoryRegionSection *b) +{ +return a->mr == b->mr && + a->fv == b->fv &&include/exec/memory.h static inline FlatView *address_space_to_flatview(AddressSpace *as) · line 513
Quoted 10 lines
include/exec/memory.h +7
-0+ a->offset_within_region == b->offset_within_region && + a->offset_within_address_space == b->offset_within_address_space && +int128_eq (a->size, b->size) && + a->readonly == b->readonly && + a->nonvolatile == b->nonvolatile; +} +/** * memory_region_init: Initialize a memory region * --
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 1/2] memory: Align and add helper for comparing MemoryRegionSections
Quoted 24 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
Quoted 23 lines · Dr. David Alan Gilbert
Quoted 5 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
Hi David,
Quoted 2 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Quoted 8 lines
Quoted 6 lines
MemoryRegionSection includes an Int128 'size' field; on some platforms the compiler causes an alignment of this to a 128bit boundary, leaving 8 bytes of dead space. This deadspace can be filled with junk.
Move the size field to the top avoiding unnecsssary alignment
"unnecessary"
Oops thanks.
Quoted 6 lines
This is enough change to be in its own commit.
---
Quoted 1 line
and provide an 'eq' routine to safely compare MRS's.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
This is another change, and should be squashed in the next patch IMO.
OK, what I'll do is I'll split this one into two ; I feel better having the extra function here separate from the next commit.
Thanks, feel free to add to both:
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Thanks!
Quoted 50 lines
Quoted 4 lines
Quoted 1 line
Doesn't Clang warn about unused 'static inline' btw?
I was using gcc; but we seem to have loads of static inline's - what would make this one different?
I guess I was confused with 'static (no-inline)' :)
Quoted 43 lines
Quoted 24 lines
Quoted 24 lines
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/exec/memory.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)include/exec/memory.h +1
-1@@ -487,15 +487,27 @@ static inline FlatView *address_space_to_flatview(AddressSpace *as)* @nonvolatile: this section is non-volatile */ struct MemoryRegionSection { +Int128 size;MemoryRegion *mr;FlatView *fv;hwaddr offset_within_region; -Int128 size;hwaddr offset_within_address_space;bool readonly;bool nonvolatile; };Quoted 16 lines
Quoted 6 lines
include/exec/memory.h +5
-0+static inline bool MemoryRegionSection_eq (MemoryRegionSection *a, +MemoryRegionSection *b) +{ +return a->mr == b->mr && + a->fv == b->fv &&include/exec/memory.h static inline FlatView *address_space_to_flatview(AddressSpace *as) · line 513
Quoted 10 lines
include/exec/memory.h +7
-0+ a->offset_within_region == b->offset_within_region && + a->offset_within_address_space == b->offset_within_address_space && +int128_eq (a->size, b->size) && + a->readonly == b->readonly && + a->nonvolatile == b->nonvolatile; +} +/** * memory_region_init: Initialize a memory region * --
Dr. David Alan Gilbert / [email protected] / Manchester, UK
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH 2/2 vhost: Fix memory region section comparison
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Using memcmp to compare structures wasn't safe,
as I found out on ARM when I was getting false miscompares.
Use the helper function for comparing the MRSs.
--- hw/virtio/vhost.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
hw/virtio/vhost.c +7 -2
@@ -451,8 +451,13 @@ static void vhost_commit(MemoryListener *listener) changed = true; }else {/* Same size, lets check the contents */ - changed = n_old_sections &&memcmp (dev->mem_sections, old_sections, - n_old_sections *sizeof (old_sections[0 ])) !=0 ; +for (int i =0 ; i < n_old_sections; i++) { +if (!MemoryRegionSection_eq (&old_sections[i], + &dev->mem_sections[i])) { + changed = true; +break ; + } + } }trace_vhost_commit (dev->started, changed);
Signature · 1 line
2.21.0
Quoted 13 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
This fixes a symptom I've seen on vhost-user on aarch64 where the daemon would be falsely notified of memory region changes that didn't exist.
The underlying problem was me memcmp'ing MemoryRegionSections even though they had padding in.(Discovered while getting virtiofs working on aarch)
Dave
Quoted 8 lines
Dr. David Alan Gilbert (2):
memory: Align and add helper for comparing MemoryRegionSections
vhost: Fix memory region section comparisonhw/virtio/vhost.c | 9 +++++++-- include/exec/memory.h | 14 +++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-)
I think it can be merged through the vhost tree.
Paolo