virtio: Add aligned ld/st accessors for vring
PATCH 1/1 virtio: Add aligned ld/st accessors for vring
The generic ld/st*_p() pointer helpers lower to __builtin_memcpy, which on RISC-V will be expanded to multiple byte-access instructions rather than a single aligned access by the compiler because it cannot prove alignment at the call site.
Each cached 16-bit access of a vring field therefore performs several distinct byte ld/st, which is a memory-tearing hazard for fields that the guest may access concurrently — most notably avail->idx, where we find the guest can write a new value between the individual byte loads and produce a torn read that never existed in memory, as seen in logs like:
"Guest moved used index from 49417 to 49919"
Here, 49919 (binary 1100 0010-1111 1111) is incorrectly assembled from the lower byte of 49663 (1100 0001-1111 1111) and the upper byte of 49664 (1100 0010-0000 0000).
Add a parallel set of _aligned cached accessors so the fast (RAM) path emits a single aligned load instruction, eliminating the tearing window.
Callers MUST ensure @addr is naturally aligned to the access size before invoking the _aligned helpers; the virtio vring layout guarantees this for avail->idx and other naturally-aligned fields.
This patch fixes the memory-tearing hazard while also improves performance.
--- hw/virtio/virtio.c | 8 +- include/qemu/bswap.h | 41 ++++++++++ include/system/memory_cached.h | 16 ++++ .../system/memory_ldst_cached_aligned.h.inc | 76 +++++++++++++++++++ 4 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 include/system/memory_ldst_cached_aligned.h.inc
hw/virtio/virtio.c +4 -4
@@ -223,9 +223,9 @@ static inline uint16_t virtio_lduw_phys_cached(VirtIODevice *vdev, hwaddr pa) {if (virtio_vdev_is_big_endian (vdev)) { -return lduw_be_phys_cached (cache, pa); +return lduw_be_phys_cached_aligned (cache, pa); } -return lduw_le_phys_cached (cache, pa); +return lduw_le_phys_cached_aligned (cache, pa); } static inline void virtio_stw_phys_cached(VirtIODevice *vdev,
@@ -233,9 +233,9 @@ static inline void virtio_stw_phys_cached(VirtIODevice *vdev, hwaddr pa, uint16_t value) { if (virtio_vdev_is_big_endian(vdev)) { -stw_be_phys_cached (cache ,pa ,value ); +stw_be_phys_cached_aligned (cache ,pa ,value ); } else { -stw_le_phys_cached (cache, pa, value); +stw_le_phys_cached_aligned (cache, pa, value); } }
include/qemu/bswap.h +32 -0
@@ -255,6 +255,20 @@ static inline int lduw_he_p(const void *ptr)return r; } +static inline int lduw_he_p_aligned (const void *ptr) +{ +uint16_t r; +__builtin_memcpy (&r,__builtin_assume_aligned (ptr,sizeof (r)),sizeof (r)); +return r; +} +static inline int ldsw_he_p (const void *ptr) {int16_t r;
@@ -267,6 +281,11 @@ static inline void stw_he_p(void *ptr, uint16_t v)__builtin_memcpy (ptr, &v,sizeof (v)); } +static inline void stw_he_p_aligned (void *ptr,uint16_t v) +{ +__builtin_memcpy (__builtin_assume_aligned (ptr,sizeof (v)), &v,sizeof (v)); +} +static inline void st24_he_p (void *ptr,uint32_t v) {__builtin_memcpy (ptr, &v,3 );
@@ -301,6 +320,12 @@ static inline int lduw_le_p(const void *ptr)return (uint16_t )le_bswap (lduw_he_p (ptr),16 ); } +static inline int lduw_le_p_aligned (const void *ptr) +{ +return (uint16_t )le_bswap (lduw_he_p_aligned (ptr),16 ); +} +static inline int ldsw_le_p (const void *ptr) {return (int16_t )le_bswap (lduw_he_p (ptr),16 );
@@ -321,6 +346,11 @@ static inline void stw_le_p(void *ptr, uint16_t v)stw_he_p (ptr,le_bswap (v,16 )); } +static inline void stw_le_p_aligned (void *ptr,uint16_t v) +{ +stw_he_p_aligned (ptr,le_bswap (v,16 )); +} +static inline void st24_le_p (void *ptr,uint32_t v) {st24_he_p (ptr,le_bswap24 (v));
@@ -341,6 +371,12 @@ static inline int lduw_be_p(const void *ptr)return (uint16_t )be_bswap (lduw_he_p (ptr),16 ); } +static inline int lduw_be_p_aligned (const void *ptr) +{ +return (uint16_t )be_bswap (lduw_he_p_aligned (ptr),16 ); +} +static inline int ldsw_be_p (const void *ptr) {return (int16_t )be_bswap (lduw_he_p (ptr),16 );
@@ -361,6 +397,11 @@ static inline void stw_be_p(void *ptr, uint16_t v)stw_he_p (ptr,be_bswap (v,16 )); } +static inline void stw_be_p_aligned (void *ptr,uint16_t v) +{ +stw_he_p_aligned (ptr,be_bswap (v,16 )); +} +static inline void st24_be_p (void *ptr,uint32_t v) {st24_he_p (ptr,be_bswap24 (v));
include/system/memory_cached.h +16 -0
@@ -96,6 +96,22 @@ void address_space_stb_cached(const MemoryRegionCache *cache,#define ARG1_DECL const MemoryRegionCache *cache#include "system/memory_ldst_phys.h.inc" +/* +* Aligned counterparts of the cached load accessors. +* +* The fast path (direct RAM access) uses the ld*_p_aligned() pointer helpers, +* which assume the host pointer is naturally aligned to the access size and +* therefore let the compiler emit an aligned load instruction. +* +* Callers MUST ensure @addr is aligned to the access size before invoking +* these helpers; otherwise the behavior is undefined. +*/ +#define ENDIANNESS _le +#include "system/memory_ldst_cached_aligned.h.inc" + +#define ENDIANNESS _be +#include "system/memory_ldst_cached_aligned.h.inc" +/** * address_space_cache_init: prepare for repeated access to a physical * memory region
include/system/memory_ldst_cached_aligned.h.inc new file +76 -0
76 lines not on this page · raw message
+#undef ENDIANNESS
Signature · 1 line
2.53.0
Quoted 5 lines · BillXiang
include/qemu/bswap.h +0
-0
include/qemu/bswap.h static inline int lduw_he_p(const void *ptr) · line 263
Quoted 9 lines
include/qemu/bswap.h +6
-0@@ -255,6 +255,20 @@ static inline int lduw_he_p(const void *ptr)return r; } +static inline int lduw_he_p_aligned (const void *ptr) +{ +uint16_t r; +__builtin_memcpy (&r,__builtin_assume_aligned (ptr,sizeof (r)),sizeof (r)); +return r; +}
If you know it's aligned, then you don't need anything special: just a normal C memory access. E.g.
*(uint16_t *)ptr
and then of course no need for a wrapper function.
include/qemu/bswap.h static inline int lduw_le_p(const void *ptr) · line 326
Quoted 4 lines
include/qemu/bswap.h +4
-0+static inline int lduw_le_p_aligned (const void *ptr) +{ +return (uint16_t )le_bswap (lduw_he_p_aligned (ptr),16 ); +}
And this: le16_to_cpu(*(uint16_t *)ptr)
r~
Quoted 19 lines · Richard Henderson
Quoted 5 lines · BillXiang
include/qemu/bswap.h +0
-0include/qemu/bswap.h static inline int lduw_he_p(const void *ptr) · line 263
Quoted 9 lines
include/qemu/bswap.h +6
-0@@ -255,6 +255,20 @@ static inline int lduw_he_p(const void *ptr) return r; } +static inline int lduw_he_p_aligned (const void *ptr) +{ + uint16_t r; + __builtin_memcpy(&r, __builtin_assume_aligned(ptr, sizeof(r)),sizeof (r)); + return r; +}If you know it's aligned, then you don't need anything special: just a normal C memory access. E.g.
Quoted 3 lines
*(uint16_t *)ptr
and then of course no need for a wrapper function.
Noted, thanks! I’ll resend it with the changes.
Quoted 10 lines
include/qemu/bswap.h static inline int lduw_le_p(const void *ptr) · line 326
Quoted 5 lines
include/qemu/bswap.h +4
-0+static inline int lduw_le_p_aligned (const void *ptr) +{ + return (uint16_t)le_bswap(lduw_he_p_aligned(ptr), 16); +}And this:
le16_to_cpu(*(uint16_t *)ptr)r~