ff00bed189
On ARM, seqcst loads and stores (which QEMU does not use) are compiled respectively as LDAR and STLR instructions. Even though LDAR is also used for load-acquire operations, it also waits for all STLRs to leave the store buffer. Thus, LDAR and STLR alone are load-acquire and store-release operations, but LDAR also provides store-against-load ordering as long as the previous store is a STLR. Compare this to ARMv7, where store-release is DMB+STR and load-acquire is LDR+DMB, but an additional DMB is needed between store-seqcst and load-seqcst (e.g. DMB+STR+DMB+LDR+DMB); or with x86, where MOV provides load-acquire and store-release semantics and the two can be reordered. Likewise, on ARM sequentially consistent read-modify-write operations only need to use LDAXR and STLXR respectively for the load and the store, while on x86 they need to use the stronger LOCK prefix. In a strange twist of events, however, the _stronger_ semantics of the ARM instructions can end up causing bugs on ARM, not on x86. The problems occur when seqcst atomics are mixed with relaxed atomics. QEMU's atomics try to bridge the Linux API (that most of the developers are familiar with) and the C11 API, and the two have a substantial difference: - in Linux, strongly-ordered atomics such as atomic_add_return() affect the global ordering of _all_ memory operations, including for example READ_ONCE()/WRITE_ONCE() - in C11, sequentially consistent atomics (except for seq-cst fences) only affect the ordering of sequentially consistent operations. In particular, since relaxed loads are done with LDR on ARM, they are not ordered against seqcst stores (which are done with STLR). QEMU implements high-level synchronization primitives with the idea that the primitives contain the necessary memory barriers, and the callers can use relaxed atomics (qatomic_read/qatomic_set) or even regular accesses. This is very much incompatible with the C11 view that seqcst accesses are only ordered against other seqcst accesses, and requires using seqcst fences as in the following example: qatomic_set(&y, 1); qatomic_set(&x, 1); smp_mb(); smp_mb(); ... qatomic_read(&x) ... ... qatomic_read(&y) ... When a qatomic_*() read-modify write operation is used instead of one or both stores, developers that are more familiar with the Linux API may be tempted to omit the smp_mb(), which will work on x86 but not on ARM. This nasty difference between Linux and C11 read-modify-write operations has already caused issues in util/async.c and more are being found. Provide something similar to Linux smp_mb__before/after_atomic(); this has the double function of documenting clearly why there is a memory barrier, and avoiding a double barrier on x86 and s390x systems. The new macro can already be put to use in qatomic_mb_set(). Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: David Hildenbrand <david@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> |
||
---|---|---|
.. | ||
acpi-bits.rst | ||
atomics.rst | ||
bitops.rst | ||
blkdebug.txt | ||
blkverify.txt | ||
block-coroutine-wrapper.rst | ||
build-system.rst | ||
ci-definitions.rst.inc | ||
ci-jobs.rst.inc | ||
ci-runners.rst.inc | ||
ci.rst | ||
clocks.rst | ||
code-of-conduct.rst | ||
conflict-resolution.rst | ||
control-flow-integrity.rst | ||
decodetree.rst | ||
ebpf_rss.rst | ||
fuzzing.rst | ||
index-api.rst | ||
index-build.rst | ||
index-internals.rst | ||
index-process.rst | ||
index-tcg.rst | ||
index.rst | ||
kconfig.rst | ||
loads-stores.rst | ||
lockcnt.txt | ||
maintainers.rst | ||
memory.rst | ||
migration.rst | ||
modules.rst | ||
multi-process.rst | ||
multi-thread-tcg.rst | ||
multiple-iothreads.txt | ||
qapi-code-gen.rst | ||
qgraph.rst | ||
qom.rst | ||
qtest.rst | ||
rcu.txt | ||
replay.rst | ||
reset.rst | ||
s390-dasd-ipl.rst | ||
secure-coding-practices.rst | ||
stable-process.rst | ||
style.rst | ||
submitting-a-patch.rst | ||
submitting-a-pull-request.rst | ||
tcg-icount.rst | ||
tcg-ops.rst | ||
tcg-plugins.rst | ||
tcg.rst | ||
testing.rst | ||
tracing.rst | ||
trivial-patches.rst | ||
ui.rst | ||
vfio-migration.rst | ||
virtio-backends.rst | ||
virtio-migration.txt | ||
writing-monitor-commands.rst |