Skip to contentSkip to conversation

virtio: Add aligned ld/st accessors for vring

PATCH In review BillXiang 4 participants 10 messages Aug 21 – Oct 16

lore
Share & tools
lore

BillXiang

author
#0 lore raw

PATCH v2 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                          | 20 +++++
 include/system/memory_cached.h                | 16 ++++
 .../system/memory_ldst_cached_aligned.h.inc   | 77 +++++++++++++++++++
 4 files changed, 117 insertions(+), 4 deletions(-)
 create mode 100644 include/system/memory_ldst_cached_aligned.h.inc
hw/virtio/virtio.c +4 -4
index daa5607..f796bdd 100644
@@ -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 +20 -0
index 387d65c..be9913c 100644
@@ -301,6 +301,11 @@ 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 le16_to_cpu(*(uint16_t *)ptr);
+}
+
 static inline int ldsw_le_p(const void *ptr)
 {
     return (int16_t)le_bswap(lduw_he_p(ptr), 16);
@@ -321,6 +326,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)
+{
+    *(uint16_t *)ptr = cpu_to_le16(v);
+}
+
 static inline void st24_le_p(void *ptr, uint32_t v)
 {
     st24_he_p(ptr, le_bswap24(v));
@@ -341,6 +351,11 @@ 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 be16_to_cpu(*(uint16_t *)ptr);
+}
+
 static inline int ldsw_be_p(const void *ptr)
 {
     return (int16_t)be_bswap(lduw_he_p(ptr), 16);
@@ -361,6 +376,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)
+{
+    *(uint16_t *)ptr = cpu_to_be16(v);
+}
+
 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
index 09d4682..6884f77 100644
@@ -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 +77 -0

77 lines not on this page · raw message

Signature · 1 line
2.53.0
#1 lore raw
Quoted 5 lines · BillXiang

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.

It is very unfortunate that your host doesn't have working unaligned accesses. This puts you into the same bucket as SPARC (i.e. a rare and not very well tested corner case) and you're likely to find you have a lot of annoying cases you need to track down to get things working.

Quoted 27 lines

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.

include/qemu/bswap.h +0 -0
index 387d65c..be9913c 100644

include/qemu/bswap.h static inline int lduw_le_p(const void *ptr) · line 306

Quoted 6 lines
include/qemu/bswap.h +3 -0
@@ -301,6 +301,11 @@ 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 le16_to_cpu(*(uint16_t *)ptr);

If the pointer passed in must be a validly aligned one for a uint16_t, we can make the argument be 'uint16_t*', not void*. Then the compiler can give us some assistance about not passing the wrong type.

Quoted 10 lines
include/qemu/bswap.h +10 -0
+#define ADDRESS_SPACE_LD_CACHED_ALIGNED(size) \
+    glue(glue(address_space_ld, size), glue(ENDIANNESS, _cached_aligned))
+#define ADDRESS_SPACE_LD_CACHED_SLOW(size) \
+    glue(glue(address_space_ld, size), glue(ENDIANNESS, _cached_slow))
+#define LD_P_ALIGNED(size) \
+    glue(glue(ld, size), glue(ENDIANNESS, _p_aligned))
+#define LD_PHYS_CACHED_ALIGNED(size) \
+    glue(glue(ld, size), glue(ENDIANNESS, glue(_phys, _cached_aligned)))
+
+static inline uint16_t ADDRESS_SPACE_LD_CACHED_ALIGNED(uw)(MemoryRegionCache *cache,

include/qemu/bswap.h static inline int lduw_le_p(const void *ptr)

Quoted 10 lines
include/qemu/bswap.h +10 -0
+    hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
+{
+    assert(addr < cache->len && 2 <= cache->len - addr);
+    fuzz_dma_read_cb(cache->xlat + addr, 2, cache->mrs.mr);
+    if (likely(cache->ptr)) {
+        return LD_P_ALIGNED(uw)(cache->ptr + addr);
+    } else {
+        return ADDRESS_SPACE_LD_CACHED_SLOW(uw)(cache, addr, attrs, result);
+    }
+}

I'm tempted to suggest some kind of "if pointer is aligned take aligned path, otherwise take slow path" either here or actually in lduw_le_p(), but maybe that's a bad idea. Richard ?

(I have a suspicion that other places than this one will assume that an aligned ldl_he_p() is not going to tear.)

-- PMM

#2 lore raw
Quoted 6 lines · Peter Maydell

I'm tempted to suggest some kind of "if pointer is aligned take aligned path, otherwise take slow path" either here or actually in lduw_le_p(), but maybe that's a bad idea. Richard ?

(I have a suspicion that other places than this one will assume that an aligned ldl_he_p() is not going to tear.)

I agree -- I expect most everything assumes ldl_he_p won't tear for aligned accesses.

This kinda begs the question of what atomicity the caller expects. It's not implausible that an x86 path expects even unaligned accesses not crossing a cacheline to be atomic, since that's been a thing since 1995. I expect both IBM architectures similarly expect atomicity by alignment, since that's been a thing for s390 since yonks and Power has the same language.

We have a bunch of code in accel/tcg/ldst_atomicity.c.inc that can handle this, we'd just need to provide it with the correct inputs. And I assume we'd still like to inline the single access on appropriate hosts.

r~

author
#3 lore raw
Quoted 9 lines · Richard Henderson
Quoted 6 lines · Peter Maydell

I'm tempted to suggest some kind of "if pointer is aligned take aligned path, otherwise take slow path" either here or actually in lduw_le_p(), but maybe that's a bad idea. Richard ?

(I have a suspicion that other places than this one will assume that an aligned ldl_he_p() is not going to tear.)

I agree -- I expect most everything assumes ldl_he_p won't tear for aligned accesses.

Hi Peter, I noticed that in your commit [1], you have pointed out that ld*_he_p() and st*_he_p() is not atomic especially for vring_avail_idx. This suggests it’s time to finally implement the atomic functions. And I think we should provide explicit atomic operations, similar to those in CPU instruction sets, rather than a single all‑purpose function cluttered with conditional branches — and it should be the caller’s responsibility to decide whether to use them.

Quoted 8 lines

This kinda begs the question of what atomicity the caller expects.  It's not implausible that an x86 path expects even unaligned accesses not crossing a cacheline to be atomic, since that's been a thing since 1995.  I expect both IBM architectures similarly expect atomicity by alignment, since that's been a thing for s390 since yonks and Power has the same language.

Quoted 6 lines

We have a bunch of code in accel/tcg/ldst_atomicity.c.inc that can handle this, we'd just need to provide it with the correct inputs.  And I assume we'd still like to inline the single access on appropriate hosts.

r~

Hi Richard, I've read your code in accel/tcg/ldst_atomicity.c.inc. Do you think it would be better to make the load/store_atomic* public?

--
Bill Xiang

[1]
https://lore.kernel.org/qemu-devel/[email protected]/#r

#4 lore raw
Quoted 2 lines · BillXiang

Hi Richard, I've read your code in accel/tcg/ldst_atomicity.c.inc. Do you think it would be better to make the load/store_atomic* public?

They do not fit by default, as we need to still process unaligned cases?

I wished we can use qemu_mem_move() directly that just got introduced.. but it does slightly more than wanted. Maybe something like this? Below diff dropped ldsw_he_p() alone the way as it's never used.

Thanks,

===8<===

include/qemu/bswap.h +40 -49
index 387d65c0b0..04a0f63612 100644
@@ -1,6 +1,7 @@
 #ifndef BSWAP_H
 #define BSWAP_H
 
+#include "qemu/atomic.h"
 #include "qemu/target-info.h"
 #include "exec/memop.h"
 
@@ -238,62 +239,52 @@ static inline void stb_p(void *ptr, uint8_t v)
     *(uint8_t *)ptr = v;
 }
 
-/*
- * Any compiler worth its salt will turn these memcpy into native unaligned
- * operations.  Thus we don't need to play games with packed attributes, or
- * inline byte-by-byte stores.
- * Some compilation environments (eg some fortify-source implementations)
- * may intercept memcpy() in a way that defeats the compiler optimization,
- * though, so we use __builtin_memcpy() to give ourselves the best chance
- * of good performance.
- */
-
-static inline int lduw_he_p(const void *ptr)
-{
-    uint16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
-
-static inline int ldsw_he_p(const void *ptr)
-{
-    int16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#define  LD_HE_P(type, size)                                \
+    static inline type                                      \
+    glue(glue(ld, size), _he_p)(const void *ptr)            \
+    {                                                       \
+        type v;                                             \
+        if (unlikely((uintptr_t)ptr & (sizeof(v) - 1))) {   \
+            __builtin_memcpy(&v, ptr, sizeof(v));           \
+        } else {                                            \
+            v = qatomic_read((type *)ptr);                  \
+        }                                                   \
+        return v;                                           \
+    }
 
-static inline void stw_he_p(void *ptr, uint16_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
+#define  ST_HE_P(type, size)                                \
+    static inline void                                      \
+    glue(glue(st, size), _he_p)(void *ptr, type v)          \
+    {                                                       \
+        if (unlikely((uintptr_t)ptr & (sizeof(v) - 1))) {   \
+            __builtin_memcpy(ptr, &v, sizeof(v));           \
+        } else {                                            \
+            qatomic_set((type *)ptr, v);                    \
+        }                                                   \
+    }
 
-static inline void st24_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, 3);
-}
+LD_HE_P(uint16_t, 16)
+LD_HE_P(uint32_t, 32)
+LD_HE_P(uint64_t, 64)
+ST_HE_P(uint16_t, 16)
+ST_HE_P(uint32_t, 32)
+ST_HE_P(uint64_t, 64)
 
-static inline int ldl_he_p(const void *ptr)
-{
-    int32_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#undef LD_HE_P
+#undef ST_HE_P
+#undef ADDR_ALIGNED
 
-static inline void stl_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
+#define  lduw_he_p  ld16_he_p
+#define  ldl_he_p  ld32_he_p
+#define  ldq_he_p  ld64_he_p
 
-static inline uint64_t ldq_he_p(const void *ptr)
-{
-    uint64_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#define  stw_he_p  st16_he_p
+#define  stl_he_p  st32_he_p
+#define  stq_he_p  st64_he_p
 
-static inline void stq_he_p(void *ptr, uint64_t v)
+static inline void st24_he_p(void *ptr, uint32_t v)
 {
-    __builtin_memcpy(ptr, &v, sizeof(v));
+    __builtin_memcpy(ptr, &v, 3);
 }
 
 static inline int lduw_le_p(const void *ptr)
Signature · 4 lines
2.54.0

-- 
Peter Xu
author
#5 lore raw
Quoted 5 lines · Peter Xu
Quoted 2 lines · BillXiang

Hi Richard, I've read your code in accel/tcg/ldst_atomicity.c.inc. Do you think it would be better to make the load/store_atomic* public?

They do not fit by default, as we need to still process unaligned cases?

You mentioned the use of guest CPU context in [1]. However, what I meant by load/store_atomic* is code like the following:

static inline uint16_t load_atomic2(void *pv)

{
     uint16_t *p = __builtin_assume_aligned(pv, 2);
     return qatomic_read(p);
}

static inline void store_atomic2(void *pv, uint16_t val)

{
     uint16_t *p = __builtin_assume_aligned(pv, 2);
     qatomic_set(p, val);
}

I consider these to be generic atomic load/store primitives.

Quoted 113 lines

I wished we can use qemu_mem_move() directly that just got introduced.. but it does slightly more than wanted. Maybe something like this? Below diff dropped ldsw_he_p() alone the way as it's never used.

Thanks,

===8<===

include/qemu/bswap.h +37 -47
index 387d65c0b0..04a0f63612 100644
@@ -1,6 +1,7 @@
  #ifndef BSWAP_H
  #define BSWAP_H
 
+#include "qemu/atomic.h"
  #include "qemu/target-info.h"
  #include "exec/memop.h"
 
@@ -238,62 +239,52 @@ static inline void stb_p(void *ptr, uint8_t v)
      *(uint8_t *)ptr = v;
  }
 
-/*
- * Any compiler worth its salt will turn these memcpy into native unaligned
- * operations.  Thus we don't need to play games with packed attributes, or
- * inline byte-by-byte stores.
- * Some compilation environments (eg some fortify-source implementations)
- * may intercept memcpy() in a way that defeats the compiler optimization,
- * though, so we use __builtin_memcpy() to give ourselves the best chance
- * of good performance.
- */
-
-static inline int lduw_he_p(const void *ptr)
-{
-    uint16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
-
-static inline int ldsw_he_p(const void *ptr)
-{
-    int16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#define  LD_HE_P(type, size)                                \
+    static inline type                                      \
+    glue(glue(ld, size), _he_p)(const void *ptr)            \
+    {                                                       \
+        type v;                                             \
+        if (unlikely((uintptr_t)ptr & (sizeof(v) - 1))) {   \
+            __builtin_memcpy(&v, ptr, sizeof(v));           \
+        } else {                                            \
+            v = qatomic_read((type *)ptr);                  \
+        }                                                   \
+        return v;                                           \
+    }
 
-static inline void stw_he_p(void *ptr, uint16_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
+#define  ST_HE_P(type, size)                                \
+    static inline void                                      \
+    glue(glue(st, size), _he_p)(void *ptr, type v)          \
+    {                                                       \
+        if (unlikely((uintptr_t)ptr & (sizeof(v) - 1))) {   \
+            __builtin_memcpy(ptr, &v, sizeof(v));           \
+        } else {                                            \
+            qatomic_set((type *)ptr, v);                    \
+        }                                                   \
+    }
 
-static inline void st24_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, 3);
-}
+LD_HE_P(uint16_t, 16)
+LD_HE_P(uint32_t, 32)
+LD_HE_P(uint64_t, 64)
+ST_HE_P(uint16_t, 16)
+ST_HE_P(uint32_t, 32)
+ST_HE_P(uint64_t, 64)
 
-static inline int ldl_he_p(const void *ptr)
-{
-    int32_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#undef LD_HE_P
+#undef ST_HE_P
+#undef ADDR_ALIGNED
 
-static inline void stl_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
+#define  lduw_he_p  ld16_he_p
+#define  ldl_he_p  ld32_he_p
+#define  ldq_he_p  ld64_he_p
 
-static inline uint64_t ldq_he_p(const void *ptr)
-{
-    uint64_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#define  stw_he_p  st16_he_p
+#define  stl_he_p  st32_he_p

include/qemu/bswap.h static inline void stb_p(void *ptr, uint8_t v) · line 290

Quoted 10 lines
include/qemu/bswap.h +3 -2
+#define  stq_he_p  st64_he_p
 
-static inline void stq_he_p(void *ptr, uint64_t v)
+static inline void st24_he_p(void *ptr, uint32_t v)
  {
-    __builtin_memcpy(ptr, &v, sizeof(v));
+    __builtin_memcpy(ptr, &v, 3);
  }
 
  static inline int lduw_le_p(const void *ptr)

IMO, your change serves as a good optimization for the original load/store by taking advantage of alignment. However, the real issue I'm trying to address is the atomicity of these operations—for use cases like avail_idx in virtio, and this should not limited to virtio alone. I still believe we should provide explicit atomic load/store interfaces for callers, rather than relying solely on aligned load/store. And I admit it was my mistake to use the aligned versions here in the first place. I'll send another version later and look forward to more feedback.

Thanks,

--
BillXiang

[1] https://lore.kernel.org/qemu-devel/[email protected]/

#6 lore raw
Quoted 16 lines · BillXiang
Quoted 5 lines · Peter Xu
Quoted 2 lines · BillXiang

Hi Richard, I've read your code in accel/tcg/ldst_atomicity.c.inc. Do you think it would be better to make the load/store_atomic* public?

They do not fit by default, as we need to still process unaligned cases?

You mentioned the use of guest CPU context in [1]. However, what I meant by load/store_atomic* is code like the following:

static inline uint16_t load_atomic2(void *pv)

{
     uint16_t *p = __builtin_assume_aligned(pv, 2);
     return qatomic_read(p);
}
Quoted 7 lines

static inline void store_atomic2(void *pv, uint16_t val)

{
     uint16_t *p = __builtin_assume_aligned(pv, 2);
     qatomic_set(p, val);
}

I consider these to be generic atomic load/store primitives.

__builtin_assume_aligned() tells the compiler the address is aligned. What if it is not?

Quoted 123 lines
Quoted 123 lines

I wished we can use qemu_mem_move() directly that just got introduced.. but it does slightly more than wanted. Maybe something like this? Below diff dropped ldsw_he_p() alone the way as it's never used.

Thanks,

===8<===

include/qemu/bswap.h +40 -49
index 387d65c0b0..04a0f63612 100644
@@ -1,6 +1,7 @@
  #ifndef BSWAP_H
  #define BSWAP_H
 
+#include "qemu/atomic.h"
  #include "qemu/target-info.h"
  #include "exec/memop.h"
 
@@ -238,62 +239,52 @@ static inline void stb_p(void *ptr, uint8_t v)
      *(uint8_t *)ptr = v;
  }
 
-/*
- * Any compiler worth its salt will turn these memcpy into native unaligned
- * operations.  Thus we don't need to play games with packed attributes, or
- * inline byte-by-byte stores.
- * Some compilation environments (eg some fortify-source implementations)
- * may intercept memcpy() in a way that defeats the compiler optimization,
- * though, so we use __builtin_memcpy() to give ourselves the best chance
- * of good performance.
- */
-
-static inline int lduw_he_p(const void *ptr)
-{
-    uint16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
-
-static inline int ldsw_he_p(const void *ptr)
-{
-    int16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#define  LD_HE_P(type, size)                                \
+    static inline type                                      \
+    glue(glue(ld, size), _he_p)(const void *ptr)            \
+    {                                                       \
+        type v;                                             \
+        if (unlikely((uintptr_t)ptr & (sizeof(v) - 1))) {   \
+            __builtin_memcpy(&v, ptr, sizeof(v));           \
+        } else {                                            \
+            v = qatomic_read((type *)ptr);                  \
+        }                                                   \
+        return v;                                           \
+    }
 
-static inline void stw_he_p(void *ptr, uint16_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
+#define  ST_HE_P(type, size)                                \
+    static inline void                                      \
+    glue(glue(st, size), _he_p)(void *ptr, type v)          \
+    {                                                       \
+        if (unlikely((uintptr_t)ptr & (sizeof(v) - 1))) {   \
+            __builtin_memcpy(ptr, &v, sizeof(v));           \
+        } else {                                            \
+            qatomic_set((type *)ptr, v);                    \
+        }                                                   \
+    }
 
-static inline void st24_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, 3);
-}
+LD_HE_P(uint16_t, 16)
+LD_HE_P(uint32_t, 32)
+LD_HE_P(uint64_t, 64)
+ST_HE_P(uint16_t, 16)
+ST_HE_P(uint32_t, 32)
+ST_HE_P(uint64_t, 64)
 
-static inline int ldl_he_p(const void *ptr)
-{
-    int32_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#undef LD_HE_P
+#undef ST_HE_P
+#undef ADDR_ALIGNED
 
-static inline void stl_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
+#define  lduw_he_p  ld16_he_p
+#define  ldl_he_p  ld32_he_p
+#define  ldq_he_p  ld64_he_p
 
-static inline uint64_t ldq_he_p(const void *ptr)
-{
-    uint64_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#define  stw_he_p  st16_he_p
+#define  stl_he_p  st32_he_p
+#define  stq_he_p  st64_he_p
 
-static inline void stq_he_p(void *ptr, uint64_t v)
+static inline void st24_he_p(void *ptr, uint32_t v)
  {
-    __builtin_memcpy(ptr, &v, sizeof(v));
+    __builtin_memcpy(ptr, &v, 3);
  }
 
Quoted 10 lines

include/qemu/bswap.h static inline void stb_p(void *ptr, uint8_t v) · line 290

Quoted 1 line
include/qemu/bswap.h +0 -0
  static inline int lduw_le_p(const void *ptr)

IMO, your change serves as a good optimization for the original load/store by taking advantage of alignment. However, the real issue I'm trying to address is the atomicity of these operations—for use cases like avail_idx in virtio, and this should not limited to virtio alone. I still believe we should provide explicit atomic load/store interfaces for callers, rather than relying solely on aligned load/store. And I admit it was my mistake to use the aligned versions here in the first place. I'll send another version later and look forward to more feedback.

Explicit atomic load/store maybe still make sense somewhere as API, but I think Peter Maydell also raised that this exact failure may not be the only one.

IMHO we could directly switch to aligned access automatically when it can, the only concern is we add one more test instruction in this path, but I expect it always hit the aligned case. Also I expect callers of these host endian APIs to not loop over a range of addr. So irrelevant of whether above change would make sense, we want to study more on the impact of the extra if on the callers. The hope is it is minimum impact and in case there're corner cases we can switch to other API even if existed.

Thanks,

Quoted 8 lines

Thanks,

--
BillXiang

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Signature · 1 line
Peter Xu
author
#7 lore raw
Quoted 23 lines · Peter Xu
Quoted 22 lines · BillXiang
Quoted 5 lines · Peter Xu
Quoted 2 lines · BillXiang

Hi Richard, I've read your code in accel/tcg/ldst_atomicity.c.inc. Do you think it would be better to make the load/store_atomic* public?

They do not fit by default, as we need to still process unaligned cases?

You mentioned the use of guest CPU context in [1]. However, what I meant by load/store_atomic* is code like the following:

static inline uint16_t load_atomic2(void *pv)

{
      uint16_t *p = __builtin_assume_aligned(pv, 2);
      return qatomic_read(p);
}

static inline void store_atomic2(void *pv, uint16_t val)

{
      uint16_t *p = __builtin_assume_aligned(pv, 2);
      qatomic_set(p, val);
}
Quoted 4 lines
Quoted 1 line

I consider these to be generic atomic load/store primitives.

__builtin_assume_aligned() tells the compiler the address is aligned. What if it is not?

When using __builtin_assume_aligned, the compiler will generate unaligned load/store instructions if the data is not aligned, rather than emitting byte‑by‑byte code, which may cause errors on processors that do not support unaligned accesses. That's what I mean: callers must ensure @addr is naturally aligned to the access size.

Quoted 135 lines
Quoted 124 lines
Quoted 124 lines

I wished we can use qemu_mem_move() directly that just got introduced.. but it does slightly more than wanted. Maybe something like this? Below diff dropped ldsw_he_p() alone the way as it's never used.

Thanks,

===8<===

include/qemu/bswap.h +40 -49
index 387d65c0b0..04a0f63612 100644
@@ -1,6 +1,7 @@
   #ifndef BSWAP_H
   #define BSWAP_H
 
+#include "qemu/atomic.h"
   #include "qemu/target-info.h"
   #include "exec/memop.h"
 
@@ -238,62 +239,52 @@ static inline void stb_p(void *ptr, uint8_t v)
       *(uint8_t *)ptr = v;
   }
 
-/*
- * Any compiler worth its salt will turn these memcpy into native unaligned
- * operations.  Thus we don't need to play games with packed attributes, or
- * inline byte-by-byte stores.
- * Some compilation environments (eg some fortify-source implementations)
- * may intercept memcpy() in a way that defeats the compiler optimization,
- * though, so we use __builtin_memcpy() to give ourselves the best chance
- * of good performance.
- */
-
-static inline int lduw_he_p(const void *ptr)
-{
-    uint16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
-
-static inline int ldsw_he_p(const void *ptr)
-{
-    int16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#define  LD_HE_P(type, size)                                \
+    static inline type                                      \
+    glue(glue(ld, size), _he_p)(const void *ptr)            \
+    {                                                       \
+        type v;                                             \
+        if (unlikely((uintptr_t)ptr & (sizeof(v) - 1))) {   \
+            __builtin_memcpy(&v, ptr, sizeof(v));           \
+        } else {                                            \
+            v = qatomic_read((type *)ptr);                  \
+        }                                                   \
+        return v;                                           \
+    }
 
-static inline void stw_he_p(void *ptr, uint16_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
+#define  ST_HE_P(type, size)                                \
+    static inline void                                      \
+    glue(glue(st, size), _he_p)(void *ptr, type v)          \
+    {                                                       \
+        if (unlikely((uintptr_t)ptr & (sizeof(v) - 1))) {   \
+            __builtin_memcpy(ptr, &v, sizeof(v));           \
+        } else {                                            \
+            qatomic_set((type *)ptr, v);                    \
+        }                                                   \
+    }
 
-static inline void st24_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, 3);
-}
+LD_HE_P(uint16_t, 16)
+LD_HE_P(uint32_t, 32)
+LD_HE_P(uint64_t, 64)
+ST_HE_P(uint16_t, 16)
+ST_HE_P(uint32_t, 32)
+ST_HE_P(uint64_t, 64)
 
-static inline int ldl_he_p(const void *ptr)
-{
-    int32_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#undef LD_HE_P
+#undef ST_HE_P
+#undef ADDR_ALIGNED
 
-static inline void stl_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
+#define  lduw_he_p  ld16_he_p
+#define  ldl_he_p  ld32_he_p
+#define  ldq_he_p  ld64_he_p
 
-static inline uint64_t ldq_he_p(const void *ptr)
-{
-    uint64_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
+#define  stw_he_p  st16_he_p
+#define  stl_he_p  st32_he_p
+#define  stq_he_p  st64_he_p
 
-static inline void stq_he_p(void *ptr, uint64_t v)
+static inline void st24_he_p(void *ptr, uint32_t v)
   {
-    __builtin_memcpy(ptr, &v, sizeof(v));
+    __builtin_memcpy(ptr, &v, 3);
   }
 
Quoted 10 lines

include/qemu/bswap.h static inline void stb_p(void *ptr, uint8_t v) · line 290

Quoted 1 line
include/qemu/bswap.h +0 -0
   static inline int lduw_le_p(const void *ptr)

IMO, your change serves as a good optimization for the original load/store by taking advantage of alignment. However, the real issue I'm trying to address is the atomicity of these operations—for use cases like avail_idx in virtio, and this should not limited to virtio alone. I still believe we should provide explicit atomic load/store interfaces for callers, rather than relying solely on aligned load/store. And I admit it was my mistake to use the aligned versions here in the first place. I'll send another version later and look forward to more feedback.

Quoted 3 lines

Explicit atomic load/store maybe still make sense somewhere as API, but I think Peter Maydell also raised that this exact failure may not be the only one.

I agree with that. Perhaps we should systematically find all relevant places and switch to explicit atomic load/store in the future.

Quoted 8 lines

IMHO we could directly switch to aligned access automatically when it can, the only concern is we add one more test instruction in this path, but I expect it always hit the aligned case. Also I expect callers of these host endian APIs to not loop over a range of addr. So irrelevant of whether above change would make sense, we want to study more on the impact of the extra if on the callers. The hope is it is minimum impact and in case there're corner cases we can switch to other API even if existed.

I used your latest code [1] to test virtio‑net with “iperf3 -u -l 64” on my RISC‑V server, and found that the extra conditional check has negligible impact.

Quoted 12 lines

Thanks,

Quoted 8 lines

Thanks,

--
BillXiang

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Thanks,

--
BillXiang

[1] https://lore.kernel.org/qemu-devel/[email protected]/

#8 lore raw
Quoted 27 lines · BillXiang
Quoted 25 lines · Peter Xu
Quoted 23 lines · BillXiang
Quoted 5 lines · Peter Xu
Quoted 2 lines · BillXiang

Hi Richard, I've read your code in accel/tcg/ldst_atomicity.c.inc. Do you think it would be better to make the load/store_atomic* public?

They do not fit by default, as we need to still process unaligned cases?

You mentioned the use of guest CPU context in [1]. However, what I meant by load/store_atomic* is code like the following:

static inline uint16_t load_atomic2(void *pv)

{
      uint16_t *p = __builtin_assume_aligned(pv, 2);
      return qatomic_read(p);
}

static inline void store_atomic2(void *pv, uint16_t val)

{
      uint16_t *p = __builtin_assume_aligned(pv, 2);
      qatomic_set(p, val);
}

I consider these to be generic atomic load/store primitives.

Quoted 8 lines
Quoted 2 lines

__builtin_assume_aligned() tells the compiler the address is aligned. What if it is not?

When using __builtin_assume_aligned, the compiler will generate unaligned load/store instructions if the data is not aligned, rather than emitting byte‑by‑byte code, which may cause errors on processors that do not support unaligned accesses. That's what I mean: callers must ensure @addr is naturally aligned to the access size.

Indeed. That's why the ldst_atomicity.c.inc code only calls them when it can guarantee the alignment, with fallback cases for when it isn't. Those are the lowest level internal functions in that code, and the higher level ones are things like load_atom_2() (which might call load_atomic2() if the address is 2-aligned, or lduw_he_p() if it's not but the caller said it doesn't care about getting a 2-byte-atomic load for an unaligned pointer, or a function to do an atomic 8-byte access and extract the 2 bytes we care about, or if all else fails falling back to "stop all QEMU threads and do a non-atomic read".

The difficulty in generalizing that code for device use I suspect is whether the fallback cases remain the right thing to do.

-- PMM

author
#9 lore raw
Quoted 49 lines · Peter Maydell
Quoted 27 lines · BillXiang
Quoted 25 lines · Peter Xu
Quoted 23 lines · BillXiang
Quoted 5 lines · Peter Xu
Quoted 2 lines · BillXiang

Hi Richard, I've read your code in accel/tcg/ldst_atomicity.c.inc. Do you think it would be better to make the load/store_atomic* public?

They do not fit by default, as we need to still process unaligned cases?

You mentioned the use of guest CPU context in [1]. However, what I meant by load/store_atomic* is code like the following:

static inline uint16_t load_atomic2(void *pv)

{
       uint16_t *p = __builtin_assume_aligned(pv, 2);
       return qatomic_read(p);
}

static inline void store_atomic2(void *pv, uint16_t val)

{
       uint16_t *p = __builtin_assume_aligned(pv, 2);
       qatomic_set(p, val);
}

I consider these to be generic atomic load/store primitives.

Quoted 8 lines
Quoted 2 lines

__builtin_assume_aligned() tells the compiler the address is aligned. What if it is not?

When using __builtin_assume_aligned, the compiler will generate unaligned load/store instructions if the data is not aligned, rather than emitting byte‑by‑byte code, which may cause errors on processors that do not support unaligned accesses. That's what I mean: callers must ensure @addr is naturally aligned to the access size.

Indeed. That's why the ldst_atomicity.c.inc code only calls them when it can guarantee the alignment, with fallback cases for when it isn't. Those are the lowest level internal functions in that code, and the higher level ones are things like load_atom_2() (which might call load_atomic2() if the address is 2-aligned, or lduw_he_p() if it's not but the caller said it doesn't care about getting a 2-byte-atomic load for an unaligned pointer, or a function to do an atomic 8-byte access and extract the 2 bytes we care about, or if all else fails falling back to "stop all QEMU threads and do a non-atomic read".

Quoted 4 lines

The difficulty in generalizing that code for device use I suspect is whether the fallback cases remain the right thing to do.

-- PMM

Hi all,

Sorry for the long delay on this patch. I've been occupied with other matters.

As discussed above, it would be better to address all the same failures, but it is also difficult to cover all the fallback cases.

Does anyone have any further suggestions? Could we just focus on the specific virtio case where the address is aligned and needs to be accessed atomically? Or could we use Peter's patch[1] to automatically switch to aligned access?

Thanks,

--
BillXiang

[1] https://lore.kernel.org/qemu-devel/[email protected]/