qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

membarrier: introduce qemu/sys_membarrier.h

This new header file provides heavy-weight "global" memory barriers that
enforce memory ordering on each running thread belonging to the current
process. For now, use a dummy implementation that issues memory barriers
on both sides (matching what QEMU has been doing so far).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

+26 -7
+4 -3
include/qemu/rcu.h
··· 27 27 #include "qemu/thread.h" 28 28 #include "qemu/queue.h" 29 29 #include "qemu/atomic.h" 30 + #include "qemu/sys_membarrier.h" 30 31 31 32 #ifdef __cplusplus 32 33 extern "C" { ··· 82 83 atomic_set(&p_rcu_reader->ctr, ctr); 83 84 84 85 /* Write p_rcu_reader->ctr before reading RCU-protected pointers. */ 85 - smp_mb(); 86 + smp_mb_placeholder(); 86 87 } 87 88 88 89 static inline void rcu_read_unlock(void) ··· 96 97 97 98 /* Ensure that the critical section is seen to precede the 98 99 * store to p_rcu_reader->ctr. Together with the following 99 - * smp_mb(), this ensures writes to p_rcu_reader->ctr 100 + * smp_mb_placeholder(), this ensures writes to p_rcu_reader->ctr 100 101 * are sequentially consistent. 101 102 */ 102 103 atomic_store_release(&p_rcu_reader->ctr, 0); 103 104 104 105 /* Write p_rcu_reader->ctr before reading p_rcu_reader->waiting. */ 105 - smp_mb(); 106 + smp_mb_placeholder(); 106 107 if (unlikely(atomic_read(&p_rcu_reader->waiting))) { 107 108 atomic_set(&p_rcu_reader->waiting, false); 108 109 qemu_event_set(&rcu_gp_event);
+17
include/qemu/sys_membarrier.h
··· 1 + /* 2 + * Process-global memory barriers 3 + * 4 + * Copyright (c) 2018 Red Hat, Inc. 5 + * 6 + * Author: Paolo Bonzini <pbonzini@redhat.com> 7 + */ 8 + 9 + #ifndef QEMU_SYS_MEMBARRIER_H 10 + #define QEMU_SYS_MEMBARRIER_H 1 11 + 12 + /* Keep it simple, execute a real memory barrier on both sides. */ 13 + static inline void smp_mb_global_init(void) {} 14 + #define smp_mb_global() smp_mb() 15 + #define smp_mb_placeholder() smp_mb() 16 + 17 + #endif
+5 -4
util/rcu.c
··· 93 93 } 94 94 95 95 /* Here, order the stores to index->waiting before the loads of 96 - * index->ctr. Pairs with smp_mb() in rcu_read_unlock(), 96 + * index->ctr. Pairs with smp_mb_placeholder() in rcu_read_unlock(), 97 97 * ensuring that the loads of index->ctr are sequentially consistent. 98 98 */ 99 - smp_mb(); 99 + smp_mb_global(); 100 100 101 101 QLIST_FOREACH_SAFE(index, &registry, node, tmp) { 102 102 if (!rcu_gp_ongoing(&index->ctr)) { ··· 145 145 qemu_mutex_lock(&rcu_sync_lock); 146 146 147 147 /* Write RCU-protected pointers before reading p_rcu_reader->ctr. 148 - * Pairs with smp_mb() in rcu_read_lock(). 148 + * Pairs with smp_mb_placeholder() in rcu_read_lock(). 149 149 */ 150 - smp_mb(); 150 + smp_mb_global(); 151 151 152 152 qemu_mutex_lock(&rcu_registry_lock); 153 153 if (!QLIST_EMPTY(&registry)) { ··· 376 376 377 377 static void __attribute__((__constructor__)) rcu_init(void) 378 378 { 379 + smp_mb_global_init(); 379 380 #ifdef CONFIG_POSIX 380 381 pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_child); 381 382 #endif