qemu-e2k/include/qemu
Paolo Bonzini ff00bed189 qatomic: add smp_mb__before/after_rmw()
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>
2023-03-07 12:38:40 +01:00
..
accel.h typedefs: Forward-declare AccelState 2023-01-06 00:50:32 +01:00
async-teardown.h Don't include headers already included by qemu/osdep.h 2023-02-08 07:28:05 +01:00
atomic.h qatomic: add smp_mb__before/after_rmw() 2023-03-07 12:38:40 +01:00
atomic128.h include/qemu/int128: Use Int128 structure for TCI 2023-02-04 06:19:42 -10:00
base64.h
bcd.h
bitmap.h migration: Use non-atomic ops for clear log bitmap 2022-11-21 11:58:10 +01:00
bitops.h bitops.h: revert db1ffc32dd ("qemu/bitops.h: add bitrev8 implementation") 2021-07-26 06:56:41 -10:00
bswap.h remove unnecessary extern "C" blocks 2023-02-10 14:12:06 +01:00
buffer.h Replace GCC_FMT_ATTR with G_GNUC_PRINTF 2022-03-22 14:40:51 +04:00
cacheflush.h
cacheinfo.h include: Move qemu_[id]cache_* declarations to new qemu/cacheinfo.h 2022-02-21 13:30:20 +00:00
clang-tsa.h clang-tsa: Add macros for shared locks 2022-12-15 16:07:43 +01:00
co-shared-resource.h co-shared-resource: protect with a mutex 2021-06-25 14:24:24 +03:00
compiler.h compiler.h: add QEMU_SANITIZE_{ADDRESS,THREAD} 2022-04-21 17:08:52 +04:00
config-file.h qemu-config: Make config_parse_qdict() return bool 2022-12-14 16:19:35 +01:00
coroutine-core.h coroutine: Split qemu/coroutine-core.h off qemu/coroutine.h 2023-01-20 07:21:46 +01:00
coroutine-tls.h tls: add macros for coroutine-safe TLS variables 2022-03-04 18:14:40 +01:00
coroutine.h coroutine: Split qemu/coroutine-core.h off qemu/coroutine.h 2023-01-20 07:21:46 +01:00
coroutine_int.h
cpu-float.h Clean up ill-advised or unusual header guards 2022-05-11 16:50:01 +02:00
cpuid.h tcg/i386: Detect AVX512 2022-03-04 08:50:41 -10:00
crc-ccitt.h Clean up ill-advised or unusual header guards 2022-05-11 16:50:01 +02:00
crc32c.h
ctype.h
cutils.h cutils: Introduce bundle mechanism 2022-07-13 16:58:57 +02:00
datadir.h
dbus.h Don't include headers already included by qemu/osdep.h 2023-02-08 07:28:05 +01:00
drm.h
envlist.h remove unnecessary extern "C" blocks 2023-02-10 14:12:06 +01:00
error-report.h Move error_printf_unless_qmp() with monitor unit 2022-04-21 17:09:09 +04:00
event_notifier.h event_notifier: add event_notifier_get_wfd() 2022-03-06 06:19:47 -05:00
fifo8.h
fifo32.h
filemonitor.h
futex.h
guest-random.h
hbitmap.h hbitmap: fix hbitmap_status() return value for first dirty bit case 2023-02-17 14:34:24 +01:00
help-texts.h Clean up header guards that don't match their file name 2022-05-11 16:49:06 +02:00
help_option.h
host-utils.h Don't include headers already included by qemu/osdep.h 2023-02-08 07:28:05 +01:00
hw-version.h include: Move hardware version declarations to new qemu/hw-version.h 2022-02-21 13:30:20 +00:00
id.h net: Use id_generate() in the network subsystem, too 2021-03-09 21:47:45 +01:00
int128.h include/qemu/int128: Use Int128 structure for TCI 2023-02-04 06:19:42 -10:00
interval-tree.h util: Add interval-tree.c 2022-12-20 17:09:41 -08:00
iov.h
iova-tree.h util: accept iova_tree_remove_parameter by value 2022-09-02 10:22:39 +08:00
jhash.h
job.h job: remove unused functions 2022-10-07 12:11:41 +02:00
keyval.h Clean up ill-advised or unusual header guards 2022-05-11 16:50:01 +02:00
lockable.h coroutine: Split qemu/coroutine-core.h off qemu/coroutine.h 2023-01-20 07:21:46 +01:00
log-for-trace.h util/log: Drop return value from qemu_log 2022-04-20 10:51:11 -07:00
log.h util/log: Support per-thread log files 2022-04-20 10:51:11 -07:00
madvise.h include: Move qemu_madvise() and related #defines to new qemu/madvise.h 2022-02-21 13:30:20 +00:00
main-loop.h qemu/main-loop: Introduce QEMU_IOTHREAD_LOCK_GUARD 2023-01-04 16:20:01 -08:00
memalign.h osdep: Move memalign-related functions to their own header 2022-03-07 13:16:49 +00:00
memfd.h
mmap-alloc.h util/mmap-alloc: Remove qemu_mempath_getpagesize() 2022-08-26 13:34:21 +02:00
module.h module: add Error arguments to module_load and module_load_qom 2022-11-06 09:48:50 +01:00
mprotect.h include: Move qemu_mprotect_*() to new qemu/mprotect.h 2022-02-21 13:30:20 +00:00
notify.h
nvdimm-utils.h
option.h include: add qemu/keyval.h 2022-04-21 17:03:51 +04:00
option_int.h
osdep.h block: Add no_coroutine_fn and coroutine_mixed_fn marker 2023-01-24 18:26:41 +01:00
path.h
plugin-memory.h include: Include headers where needed 2023-01-08 01:54:22 -05:00
plugin.h cpu-exec: assert that plugin_mem_cbs is NULL after execution 2023-02-02 11:48:20 +00:00
pmem.h
processor.h
progress_meter.h coroutine: Clean up superfluous inclusion of qemu/lockable.h 2023-01-19 10:18:28 +01:00
qdist.h
qemu-options.h remove qemu-options* from root directory 2021-05-26 14:49:46 +02:00
qemu-plugin.h Clean up header guards that don't match their file name 2022-05-11 16:49:06 +02:00
qemu-print.h Replace GCC_FMT_ATTR with G_GNUC_PRINTF 2022-03-22 14:40:51 +04:00
qemu-progress.h include: move progress API to qemu-progress.h 2022-04-06 14:31:43 +02:00
qht.h
qsp.h
queue.h
range.h compiler.h: replace QEMU_WARN_UNUSED_RESULT with G_GNUC_WARN_UNUSED_RESULT 2022-03-22 14:40:51 +04:00
ratelimit.h ratelimit: treat zero speed as unlimited 2021-06-25 14:22:21 +03:00
rcu.h remove unnecessary extern "C" blocks 2023-02-10 14:12:06 +01:00
rcu_queue.h remove unnecessary extern "C" blocks 2023-02-10 14:12:06 +01:00
readline.h readline: Extract readline_add_completion_of() from monitor 2023-02-04 07:56:54 +01:00
selfmap.h Clean up decorations and whitespace around header guards 2022-05-11 16:50:32 +02:00
seqlock.h
sockets.h * Fix and test the VISTR instruction on s390x 2022-10-31 06:19:54 -04:00
stats64.h qemu/atomic: Add aligned_{int64,uint64}_t types 2021-07-21 07:45:38 -10:00
sys_membarrier.h
systemd.h
thread-context.h util: Introduce ThreadContext user-creatable object 2022-10-27 11:00:43 +02:00
thread-posix.h thread-posix: implement Semaphore with QemuCond and QemuMutex 2022-04-06 14:31:56 +02:00
thread-win32.h util: Use real functions for thread-posix QemuRecMutex 2021-06-16 15:03:26 +02:00
thread.h util/qemu-thread-posix: use TSA_NO_TSA to suppress clang TSA warnings in FreeBSD 2023-02-17 11:22:19 +01:00
throttle-options.h
throttle.h
timed-average.h
timer.h Use g_new() & friends where that makes obvious sense 2022-03-21 15:44:44 +01:00
transactions.h transactions: Invoke clean() after everything else 2021-11-16 09:43:44 +01:00
tsan.h
typedefs.h qemu/typedefs: Sort in case-insensitive alphabetical order (again) 2023-02-27 22:29:01 +01:00
unicode.h
units.h
uri.h Updated the FSF address to <https://www.gnu.org/licenses/> 2023-02-27 09:15:39 +01:00
userfaultfd.h migration: Clean up includes 2023-02-08 07:27:20 +01:00
uuid.h
vfio-helpers.h util/vfio-helpers: Pass Error handle to qemu_vfio_dma_map() 2021-09-07 09:08:24 +01:00
vhost-user-server.h Do not include "qemu/error-report.h" in headers that do not need it 2023-02-14 09:11:27 +01:00
win_dump_defs.h include/qemu: add 32-bit Windows dump structures 2022-04-22 13:36:04 +04:00
xattr.h qemu/xattr.h: Exclude <sys/xattr.h> for Windows 2022-12-23 11:48:13 +01:00
xxhash.h
yank.h yank: Remove dependency on qiochannel 2021-04-01 15:27:44 +04:00