i386 + x86_64 mttcg
cover i386 + x86_64 mttcg
I sent ~20 days ago a series that allowed me to boot x86_64 with mttcg:
https://patchwork.kernel.org/cover/10564977/
Thanks to Paolo's work (already merged), we don't need to hold the BQL when calling cpu_get_ticks, which makes the MTTCG conversion even simpler. [ I have a couple more patches related to some of that work, but I'll send them next week once Paolo is back ]
This series enables MTTCG for both x86_64 and i386. I tested both with a buildroot image, and looked for races with Valgrind's helgrind.
The first four patches are not really necessary for i386 MTTCG, but since they'll eventually go via Paolo's tree I included them here.
You can fetch this series from:
https://github.com/cota/qemu/tree/i386-mttcg
Thanks,
Emilio
PATCH 1/6 qsp: drop atomics when using the seqlock
Using atomics here is a mistake since they're not guaranteed to compile.
--- util/qsp.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
util/qsp.c +10 -6
@@ -351,6 +351,7 @@ static QSPEntry *qsp_entry_get(const void *obj, const char *file, int line,static void qsp_entry_aggregate (QSPEntry *to,const QSPEntry *from) {#ifdef CONFIG_ATOMIC64 +/* use __nocheck because sizeof(void *) might be < sizeof(u64) */ to->ns +=atomic_read__nocheck (&from->ns); to->n_acqs +=atomic_read__nocheck (&from->n_acqs);#else
@@ -359,8 +360,8 @@ static void qsp_entry_aggregate(QSPEntry *to, const QSPEntry *from)do { version =seqlock_read_begin (&from->sequence); - ns =atomic_read__nocheck (&from->ns); - n_acqs =atomic_read__nocheck (&from->n_acqs); + ns = from->ns; + n_acqs = from->n_acqs; }while (seqlock_read_retry (&from->sequence, version)); to->ns += ns;
@@ -375,14 +376,17 @@ static void qsp_entry_aggregate(QSPEntry *to, const QSPEntry *from)*/ static inline void do_qsp_entry_record (QSPEntry *e,int64_t delta,bool acq) { -#ifndef CONFIG_ATOMIC64 -seqlock_write_begin (&e->sequence); -#endif +#ifdef CONFIG_ATOMIC64 atomic_set__nocheck (&e->ns, e->ns + delta);if (acq) {atomic_set__nocheck (&e->n_acqs, e->n_acqs +1 ); } -#ifndef CONFIG_ATOMIC64 +#else +seqlock_write_begin (&e->sequence); + e->ns += delta; +if (acq) { + e->n_acqs++; + }seqlock_write_end (&e->sequence);#endif }
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 1/6] qsp: drop atomics when using the seqlock
Quoted 2 lines · Emilio G. Cota
Using atomics here is a mistake since they're not guaranteed to compile.
But isn't it technically a C11 data race if you don't use atomics? Could we make nocheck read/set degrade to just a volatile access when used on a variable that is bigger than pointers, or perhaps always except when using tsan?
Paolo
Quoted 5 lines
Signed-off-by Emilio G. Cota <[email protected]>--- util/qsp.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
Re: [Qemu-devel] [PATCH 1/6] qsp: drop atomics when using the seqlock
Quoted 5 lines · Paolo Bonzini
Quoted 2 lines · Emilio G. Cota
Using atomics here is a mistake since they're not guaranteed to compile.
But isn't it technically a C11 data race if you don't use atomics?
Yes, it's undefined behaviour.
Quoted 3 lines
Could we make nocheck read/set degrade to just a volatile access when used on a variable that is bigger than pointers, or perhaps always except when using tsan?
But volatile wouldn't save you from undefined behaviour, would it?
A simpler and definitely correct alternative is to just use a spinlock instead of the seqlock also for reads when !CONFIG_ATOMIC64. We don't care about scalability on those rare hosts anyway, so I'd go with that.
Thanks,
Emilio
Re: [Qemu-devel] [PATCH 1/6] qsp: drop atomics when using the seqlock
Quoted 7 lines · Emilio G. Cota
Quoted 5 lines · Paolo Bonzini
Quoted 2 lines · Emilio G. Cota
Using atomics here is a mistake since they're not guaranteed to compile.
But isn't it technically a C11 data race if you don't use atomics?
Quoted 7 lines
Yes, it's undefined behaviour.
Quoted 3 lines
Could we make nocheck read/set degrade to just a volatile access when used on a variable that is bigger than pointers, or perhaps always except when using tsan?
But volatile wouldn't save you from undefined behaviour, would it?
Yeah, but 1) only on those hosts that cannot do CONFIG_ATOMIC64 2) we pretty much already define what we expect from volatile.
Paolo
Quoted 9 lines
A simpler and definitely correct alternative is to just use a spinlock instead of the seqlock also for reads when !CONFIG_ATOMIC64. We don't care about scalability on those rare hosts anyway, so I'd go with that.
Thanks,
Emilio
PATCH 2/6 test-rcu-list: avoid torn accesses to n_reclaims and n_nodes_removed
In reply to Emilio G. Cota--- tests/test-rcu-list.c | 67 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 8 deletions(-)
tests/test-rcu-list.c +59 -8
@@ -25,6 +25,23 @@#include "qemu/rcu.h" #include "qemu/thread.h" #include "qemu/rcu_queue.h" +#include "qemu/seqlock.h" + +/* +* Abstraction to avoid torn accesses when there is a single thread updating +* the count. +* +* If CONFIG_ATOMIC64 is defined, we simply use atomic accesses. Otherwise, we +* use a seqlock without a lock, since only one thread can update the count. +*/ +struct Count { +long long val; +#ifndef CONFIG_ATOMIC64 +QemuSeqLock sequence; +#endif +}; + +typedef struct Count Count ;/* * Test variables.
@@ -33,8 +50,8 @@static QemuMutex counts_mutex;static long long n_reads =0LL ;static long long n_updates =0LL ; -static long long n_reclaims =0LL ; -static long long n_nodes_removed =0LL ; +static Count n_reclaims; +static Count n_nodes_removed;static long long n_nodes =0LL ;static int g_test_in_charge =0 ;
@@ -60,6 +77,38 @@ static int select_random_el(int max)return (rand () % max); } +static inline long long count_read (Count *c) +{ +#ifdef CONFIG_ATOMIC64 +/* use __nocheck because sizeof(void *) might be < sizeof(long long) */ +return atomic_read__nocheck (&c->val); +#else +unsigned int version; +long long val; + +do { + version =seqlock_read_begin (&c->sequence); + val = c->val; + }while (seqlock_read_retry (&c->sequence, version)); +return val; +#endif +} + +static inline void count_add (Count *c,long long val) +{ +#ifdef CONFIG_ATOMIC64 +atomic_set__nocheck (&c->val, c->val + val); +#else +seqlock_write_begin (&c->sequence); + c->val += val; +seqlock_write_end (&c->sequence); +#endif +} + +static inline void count_inc (Count *c) +{ +count_add (c,1 ); +}static void create_thread (void *(*func)(void *)) {
@@ -104,7 +153,7 @@ static void reclaim_list_el(struct rcu_head *prcu) struct list_element *el = container_of(prcu, struct list_element, rcu);g_free (el);/* Accessed only from call_rcu thread. */ - n_reclaims++; +count_inc (&n_reclaims); }#if TEST_LIST_TYPE ==1
@@ -232,7 +281,7 @@ static void *rcu_q_updater(void *arg)qemu_mutex_lock (&counts_mutex); n_nodes += n_nodes_local; n_updates += n_updates_local; - n_nodes_removed += n_removed_local; +count_add (&n_nodes_removed, n_removed_local);qemu_mutex_unlock (&counts_mutex);return NULL ; }
@@ -286,19 +335,21 @@ static void rcu_qtest(const char *test, int duration, int nreaders) n_removed_local++; }qemu_mutex_lock (&counts_mutex); - n_nodes_removed += n_removed_local; +count_add (&n_nodes_removed, n_removed_local);qemu_mutex_unlock (&counts_mutex);synchronize_rcu (); -while (n_nodes_removed > n_reclaims) { +while (count_read (&n_nodes_removed) >count_read (&n_reclaims)) {g_usleep (100 );synchronize_rcu (); }if (g_test_in_charge) { - g_assert_cmpint(n_nodes_removed, ==, n_reclaims); + g_assert_cmpint(count_read(&n_nodes_removed), ==, +count_read (&n_reclaims)); }else {printf ("%s: %d readers; 1 updater; nodes read: " \"%lld, nodes removed: %lld; nodes reclaimed: %lld\n" , - test, nthreadsrunning -1 , n_reads, n_nodes_removed, n_reclaims); + test, nthreadsrunning -1 , n_reads, +count_read (&n_nodes_removed),count_read (&n_reclaims));exit (0 ); } }
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 2/6] test-rcu-list: avoid torn accesses to n_reclaims and n_nodes_removed
Hi, Emilio.
Quoted 34 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- tests/test-rcu-list.c | 67 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 8 deletions(-)tests/test-rcu-list.c +17
-0@@ -25,6 +25,23 @@#include "qemu/rcu.h" #include "qemu/thread.h" #include "qemu/rcu_queue.h" +#include "qemu/seqlock.h" + +/* +* Abstraction to avoid torn accesses when there is a single thread updating +* the count. +* +* If CONFIG_ATOMIC64 is defined, we simply use atomic accesses. Otherwise, we +* use a seqlock without a lock, since only one thread can update the count. +*/ +struct Count { +long long val; +#ifndef CONFIG_ATOMIC64 +QemuSeqLock sequence; +#endif +}; + +typedef struct Count Count ;/* * Test variables.
tests/test-rcu-list.c line 54
Quoted 7 lines
tests/test-rcu-list.c +2
-2@@ -33,8 +50,8 @@static QemuMutex counts_mutex;static long long n_reads =0LL ;static long long n_updates =0LL ; -static long long n_reclaims =0LL ; -static long long n_nodes_removed =0LL ; +static Count n_reclaims; +static Count n_nodes_removed;
Don't we need to init the seqlocks?
seqlock_init(&n_reclaims.sequence); seqlock_init(&n_nodes_removed.sequence);
Don't we need to init ->val with 0LL as well?
Quoted 29 lines
tests/test-rcu-list.c +22
-0static long long n_nodes =0LL ;static int g_test_in_charge =0 ;@@ -60,6 +77,38 @@ static int select_random_el(int max)return (rand () % max); } +static inline long long count_read (Count *c) +{ +#ifdef CONFIG_ATOMIC64 +/* use __nocheck because sizeof(void *) might be < sizeof(long long) */ +return atomic_read__nocheck (&c->val); +#else +unsigned int version; +long long val; + +do { + version =seqlock_read_begin (&c->sequence); + val = c->val; + }while (seqlock_read_retry (&c->sequence, version)); +return val; +#endif +} + +static inline void count_add (Count *c,long long val) +{ +#ifdef CONFIG_ATOMIC64 +atomic_set__nocheck (&c->val, c->val + val); +#else
tests/test-rcu-list.c static int select_random_el(int max) · line 111
Quoted 10 lines
tests/test-rcu-list.c +10
-0+seqlock_write_begin (&c->sequence); + c->val += val; +seqlock_write_end (&c->sequence); +#endif +} + +static inline void count_inc (Count *c) +{ +count_add (c,1 ); +}
Are these #ifdef CONFIG_ATOMIC64 required?
The bodies of
seqlock_read_begin()
seqlock_read_retry()
seqlock_write_begin()
seqlock_write_end()
in include/qemu/seqlock.h make me think that they already use atomic operations. What am I missing?
Quoted 13 lines
tests/test-rcu-list.c +1
-1static void create_thread (void *(*func)(void *)) {@@ -104,7 +153,7 @@ static void reclaim_list_el(struct rcu_head *prcu) struct list_element *el = container_of(prcu, struct list_element, rcu);g_free (el);/* Accessed only from call_rcu thread. */ - n_reclaims++; +count_inc (&n_reclaims); }#if TEST_LIST_TYPE ==1
tests/test-rcu-list.c static void *rcu_q_updater(void *arg) · line 284
Quoted 5 lines
tests/test-rcu-list.c +1
-1@@ -232,7 +281,7 @@ static void *rcu_q_updater(void *arg)qemu_mutex_lock (&counts_mutex); n_nodes += n_nodes_local; n_updates += n_updates_local; - n_nodes_removed += n_removed_local; +count_add (&n_nodes_removed, n_removed_local);
I'm just curious why n_nodes and n_updates don't need seqlocks. Are n_nodes_removed and n_reclaims some kind of special that seqlocks are required?
tests/test-rcu-list.c static void rcu_qtest(const char *test, int duration, int nreaders) · line 339
Quoted 10 lines
tests/test-rcu-list.c +1
-1qemu_mutex_unlock (&counts_mutex);return NULL ; }@@ -286,19 +335,21 @@ static void rcu_qtest(const char *test, int duration, int nreaders) n_removed_local++; }qemu_mutex_lock (&counts_mutex); - n_nodes_removed += n_removed_local; +count_add (&n_nodes_removed, n_removed_local);qemu_mutex_unlock (&counts_mutex);
Does this count_add() need to be guarded by a mutex?
Quoted 20 lines
tests/test-rcu-list.c +5
-4synchronize_rcu (); -while (n_nodes_removed > n_reclaims) { +while (count_read (&n_nodes_removed) >count_read (&n_reclaims)) {g_usleep (100 );synchronize_rcu (); }if (g_test_in_charge) { - g_assert_cmpint(n_nodes_removed, ==, n_reclaims); + g_assert_cmpint(count_read(&n_nodes_removed), ==, +count_read (&n_reclaims)); }else {printf ("%s: %d readers; 1 updater; nodes read: " \"%lld, nodes removed: %lld; nodes reclaimed: %lld\n" , - test, nthreadsrunning -1 , n_reads, n_nodes_removed, n_reclaims); + test, nthreadsrunning -1 , n_reads, +count_read (&n_nodes_removed),count_read (&n_reclaims));exit (0 ); } } --
Quoted 3 lines
2.17.1
--
Murilo
Re: [Qemu-devel] [PATCH 2/6] test-rcu-list: avoid torn accesses to n_reclaims and n_nodes_removed
Quoted 47 lines · Murilo Opsfelder Araujo
Hi, Emilio.
Quoted 34 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- tests/test-rcu-list.c | 67 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 8 deletions(-)tests/test-rcu-list.c +17
-0@@ -25,6 +25,23 @@#include "qemu/rcu.h" #include "qemu/thread.h" #include "qemu/rcu_queue.h" +#include "qemu/seqlock.h" + +/* +* Abstraction to avoid torn accesses when there is a single thread updating +* the count. +* +* If CONFIG_ATOMIC64 is defined, we simply use atomic accesses. Otherwise, we +* use a seqlock without a lock, since only one thread can update the count. +*/ +struct Count { +long long val; +#ifndef CONFIG_ATOMIC64 +QemuSeqLock sequence; +#endif +}; + +typedef struct Count Count ;/* * Test variables. tests/test-rcu-list.c line 54
Quoted 7 lines
tests/test-rcu-list.c +2
-2@@ -33,8 +50,8 @@static QemuMutex counts_mutex;static long long n_reads =0LL ;static long long n_updates =0LL ; -static long long n_reclaims =0LL ; -static long long n_nodes_removed =0LL ; +static Count n_reclaims; +static Count n_nodes_removed;Don't we need to init the seqlocks?
Quoted 4 lines
seqlock_init(&n_reclaims.sequence); seqlock_init(&n_nodes_removed.sequence);Don't we need to init ->val with 0LL as well?
These are all zeroed out due to being static.
We could add seqlock_init calls just to be more clear, but seqlock_init would not have any actual effect (it just sets the sequence to 0).
Quoted 44 lines
Quoted 29 lines
tests/test-rcu-list.c +22
-0static long long n_nodes =0LL ;static int g_test_in_charge =0 ;@@ -60,6 +77,38 @@ static int select_random_el(int max)return (rand () % max); } +static inline long long count_read (Count *c) +{ +#ifdef CONFIG_ATOMIC64 +/* use __nocheck because sizeof(void *) might be < sizeof(long long) */ +return atomic_read__nocheck (&c->val); +#else +unsigned int version; +long long val; + +do { + version =seqlock_read_begin (&c->sequence); + val = c->val; + }while (seqlock_read_retry (&c->sequence, version)); +return val; +#endif +} + +static inline void count_add (Count *c,long long val) +{ +#ifdef CONFIG_ATOMIC64 +atomic_set__nocheck (&c->val, c->val + val); +#else tests/test-rcu-list.c static int select_random_el(int max) · line 111
Quoted 10 lines
tests/test-rcu-list.c +10
-0+seqlock_write_begin (&c->sequence); + c->val += val; +seqlock_write_end (&c->sequence); +#endif +} + +static inline void count_inc (Count *c) +{ +count_add (c,1 ); +}Are these
#ifdef CONFIG_ATOMIC64required?The bodies of
Quoted 7 lines
seqlock_read_begin()
seqlock_read_retry()
seqlock_write_begin()
seqlock_write_end()in
include/qemu/seqlock.hmake me think that they already use atomic operations. What am I missing?
atomic_read/set(*foo), as defined in qemu/atomic.h, are as wide as foo. The sequence number in a seqlock is an "unsigned", so those atomics won't be larger than 32 bits.
The counts we're dealing with here are 64-bits, so with #ifdef CONFIG_ATOMIC64 we ensure that the host can actually perform those 64-bit atomic accesses.
Quoted 11 lines
Quoted 11 lines
tests/test-rcu-list.c +1
-1static void create_thread (void *(*func)(void *)) {@@ -104,7 +153,7 @@ static void reclaim_list_el(struct rcu_head *prcu) struct list_element *el = container_of(prcu, struct list_element, rcu);g_free (el);/* Accessed only from call_rcu thread. */ - n_reclaims++; +count_inc (&n_reclaims); }
Quoted 10 lines
tests/test-rcu-list.c static void *rcu_q_updater(void *arg) · line 284
Quoted 7 lines
tests/test-rcu-list.c +1
-1#if TEST_LIST_TYPE ==1 @@ -232,7 +281,7 @@ static void *rcu_q_updater(void *arg)qemu_mutex_lock (&counts_mutex); n_nodes += n_nodes_local; n_updates += n_updates_local; - n_nodes_removed += n_removed_local; +count_add (&n_nodes_removed, n_removed_local);I'm just curious why
n_nodesandn_updatesdon't need seqlocks. Aren_nodes_removedandn_reclaimssome kind of special that seqlocks are required?
n_nodes and n_updates are serialized by counts_mutex.
n_nodes_removed and n_reclaims don't really need the lock (even though some accesses to it are protected by it; more on this below), since they're updated by a single thread at a time. This is why just using atomic_set is enough for these, and why we use seqlocks if the host cannot do 64-bit atomic accesses.
Note that these "atomics" are not "read-modify-write"; they are atomic in the sense that they prevent torn accesses, but otherwise imply no memory fences or synchronization.
Quoted 4 lines
Quoted 4 lines
tests/test-rcu-list.c +0
-0qemu_mutex_unlock (&counts_mutex);return NULL ; }@@ -286,19 +335,21 @@ static void rcu_qtest(const char *test, int duration, int nreaders)
Quoted 8 lines
tests/test-rcu-list.c static void rcu_qtest(const char *test, int duration, int nreaders) · line 339
Quoted 6 lines
tests/test-rcu-list.c +1
-1n_removed_local++; }qemu_mutex_lock (&counts_mutex); - n_nodes_removed += n_removed_local; +count_add (&n_nodes_removed, n_removed_local);qemu_mutex_unlock (&counts_mutex);Does this
count_add()need to be guarded by a mutex?
No. In fact, accesses to n_nodes_removed don't need the mutex at all, because only one thread writes to it at a time (first the updater thread, then the main thread joins the updater thread, and then the main thread updates it).
Performance-wise, this change wouldn't change much though, which is why I decided not to include it. The main purpose of this patch is to avoid undefined behaviour when different threads access the same variable with regular accesses without always holding the same lock, as is the case with the two variables converted to Count.
Cheers,
Emilio
Re: [Qemu-devel] [PATCH 2/6] test-rcu-list: avoid torn accesses to n_reclaims and n_nodes_removed
Hi, Emilio.
Quoted 177 lines · Emilio G. Cota
Quoted 47 lines · Murilo Opsfelder Araujo
Hi, Emilio.
Quoted 34 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- tests/test-rcu-list.c | 67 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 8 deletions(-)tests/test-rcu-list.c +17
-0@@ -25,6 +25,23 @@#include "qemu/rcu.h" #include "qemu/thread.h" #include "qemu/rcu_queue.h" +#include "qemu/seqlock.h" + +/* +* Abstraction to avoid torn accesses when there is a single thread updating +* the count. +* +* If CONFIG_ATOMIC64 is defined, we simply use atomic accesses. Otherwise, we +* use a seqlock without a lock, since only one thread can update the count. +*/ +struct Count { +long long val; +#ifndef CONFIG_ATOMIC64 +QemuSeqLock sequence; +#endif +}; + +typedef struct Count Count ;/* * Test variables. tests/test-rcu-list.c line 54
Quoted 7 lines
tests/test-rcu-list.c +2
-2@@ -33,8 +50,8 @@static QemuMutex counts_mutex;static long long n_reads =0LL ;static long long n_updates =0LL ; -static long long n_reclaims =0LL ; -static long long n_nodes_removed =0LL ; +static Count n_reclaims; +static Count n_nodes_removed;Don't we need to init the seqlocks?
Quoted 4 lines
seqlock_init(&n_reclaims.sequence); seqlock_init(&n_nodes_removed.sequence);Don't we need to init ->val with 0LL as well?
These are all zeroed out due to being static.
We could add
seqlock_initcalls just to be more clear, butseqlock_initwould not have any actual effect (it just sets the sequence to 0).Quoted 44 lines
Quoted 29 lines
tests/test-rcu-list.c +22
-0static long long n_nodes =0LL ;static int g_test_in_charge =0 ;@@ -60,6 +77,38 @@ static int select_random_el(int max)return (rand () % max); } +static inline long long count_read (Count *c) +{ +#ifdef CONFIG_ATOMIC64 +/* use __nocheck because sizeof(void *) might be < sizeof(long long) */ +return atomic_read__nocheck (&c->val); +#else +unsigned int version; +long long val; + +do { + version =seqlock_read_begin (&c->sequence); + val = c->val; + }while (seqlock_read_retry (&c->sequence, version)); +return val; +#endif +} + +static inline void count_add (Count *c,long long val) +{ +#ifdef CONFIG_ATOMIC64 +atomic_set__nocheck (&c->val, c->val + val); +#else tests/test-rcu-list.c static int select_random_el(int max) · line 111
Quoted 10 lines
tests/test-rcu-list.c +10
-0+seqlock_write_begin (&c->sequence); + c->val += val; +seqlock_write_end (&c->sequence); +#endif +} + +static inline void count_inc (Count *c) +{ +count_add (c,1 ); +}Are these
#ifdef CONFIG_ATOMIC64required?The bodies of
Quoted 7 lines
seqlock_read_begin()
seqlock_read_retry()
seqlock_write_begin()
seqlock_write_end()in
include/qemu/seqlock.hmake me think that they already use atomic operations. What am I missing?atomic_read/set(*foo), as defined in
qemu/atomic.h, are as wide as foo. The sequence number in a seqlock is an "unsigned", so those atomics won't be larger than 32 bits.The counts we're dealing with here are 64-bits, so with #ifdef
CONFIG_ATOMIC64we ensure that the host can actually perform those 64-bit atomic accesses.Quoted 11 lines
Quoted 11 lines
tests/test-rcu-list.c +1
-1static void create_thread (void *(*func)(void *)) {@@ -104,7 +153,7 @@ static void reclaim_list_el(struct rcu_head *prcu) struct list_element *el = container_of(prcu, struct list_element, rcu);g_free (el);/* Accessed only from call_rcu thread. */ - n_reclaims++; +count_inc (&n_reclaims); }Quoted 10 lines
tests/test-rcu-list.c static void *rcu_q_updater(void *arg) · line 284
Quoted 7 lines
tests/test-rcu-list.c +1
-1#if TEST_LIST_TYPE ==1 @@ -232,7 +281,7 @@ static void *rcu_q_updater(void *arg)qemu_mutex_lock (&counts_mutex); n_nodes += n_nodes_local; n_updates += n_updates_local; - n_nodes_removed += n_removed_local; +count_add (&n_nodes_removed, n_removed_local);I'm just curious why
n_nodesandn_updatesdon't need seqlocks. Aren_nodes_removedandn_reclaimssome kind of special that seqlocks are required?
n_nodesandn_updatesare serialized bycounts_mutex.
n_nodes_removedandn_reclaimsdon't really need the lock (even though some accesses to it are protected by it; more on this below), since they're updated by a single thread at a time. This is why just usingatomic_setis enough for these, and why we use seqlocks if the host cannot do 64-bit atomic accesses.Note that these "atomics" are not "read-modify-write"; they are atomic in the sense that they prevent torn accesses, but otherwise imply no memory fences or synchronization.
Quoted 4 lines
Quoted 4 lines
tests/test-rcu-list.c +0
-0qemu_mutex_unlock (&counts_mutex);return NULL ; }@@ -286,19 +335,21 @@ static void rcu_qtest(const char *test, int duration, int nreaders)Quoted 8 lines
tests/test-rcu-list.c static void rcu_qtest(const char *test, int duration, int nreaders) · line 339
Quoted 6 lines
tests/test-rcu-list.c +1
-1n_removed_local++; }qemu_mutex_lock (&counts_mutex); - n_nodes_removed += n_removed_local; +count_add (&n_nodes_removed, n_removed_local);qemu_mutex_unlock (&counts_mutex);Does this
count_add()need to be guarded by a mutex?No. In fact, accesses to
n_nodes_removeddon't need the mutex at all, because only one thread writes to it at a time (first the updater thread, then the main thread joins the updater thread, and then the main thread updates it).Performance-wise, this change wouldn't change much though, which is why I decided not to include it. The main purpose of this patch is to avoid undefined behaviour when different threads access the same variable with regular accesses without always holding the same lock, as is the case with the two variables converted to Count.
Quoted 4 lines
Cheers,
Emilio
The explanations you provided made a lot of difference on understanding the why of your patch. Thank you!
Is it possible to enhance commit message and add the explanations?
Signature · 1 line
Murilo
Re: [Qemu-devel] [PATCH 2/6] test-rcu-list: avoid torn accesses to n_reclaims and n_nodes_removed
Quoted 2 lines · Murilo Opsfelder Araujo
The explanations you provided made a lot of difference on understanding the why of your patch. Thank you!
Glad that helped!
Quoted 1 line
Is it possible to enhance commit message and add the explanations?
If I end up submitting a v2 of this series due to code-related changes, then I'll expand this patch's commit message.
Thanks,
E.
Re: [Qemu-devel] [PATCH 2/6] test-rcu-list: avoid torn accesses to n_reclaims and n_nodes_removed
In reply to Murilo Opsfelder AraujoQuoted 26 lines · Murilo Opsfelder Araujo
Quoted 19 lines
Quoted 5 lines
tests/test-rcu-list.c +5
-0+static inline void count_add (Count *c,long long val) +{ +#ifdef CONFIG_ATOMIC64 +atomic_set__nocheck (&c->val, c->val + val); +#else tests/test-rcu-list.c static int select_random_el(int max) · line 111
Quoted 10 lines
tests/test-rcu-list.c +10
-0+seqlock_write_begin (&c->sequence); + c->val += val; +seqlock_write_end (&c->sequence); +#endif +} + +static inline void count_inc (Count *c) +{ +count_add (c,1 ); +}Are these
#ifdef CONFIG_ATOMIC64required?The bodies of
Quoted 7 lines
seqlock_read_begin()
seqlock_read_retry()
seqlock_write_begin()
seqlock_write_end()in
include/qemu/seqlock.hmake me think that they already use atomic operations. What am I missing?
Quoted 7 lines
atomic_read/set(*foo), as defined in
qemu/atomic.h, are as wide as foo. The sequence number in a seqlock is an "unsigned", so those atomics won't be larger than 32 bits.The counts we're dealing with here are 64-bits, so with #ifdef
CONFIG_ATOMIC64we ensure that the host can actually perform those 64-bit atomic accesses.
Like for patch 1, I'm not sure I like introducing the data races... While reads inside the seqlock do not need atomics, I think the write should be atomic in both cases.
Paolo
PATCH 3/6 atomic: fix comment s/x64_64/x86_64/
In reply to Emilio G. Cota--- include/qemu/atomic.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu/atomic.h +1 -1
@@ -98,7 +98,7 @@* We'd prefer not want to pull in everything else TCG related, so handle * those few cases by hand. * -* Note that x32 is fully detected with __x64_64__ + _ILP32, and that for +* Note that x32 is fully detected with __x86_64__ + _ILP32, and that for * Sparc we always force the use of sparcv9 in configure. */ #if defined(__x86_64__) || defined(__sparc__)
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 3/6] atomic: fix comment s/x64_64/x86_64/
Quoted 10 lines · Emilio G. Cota
Signed-off-by Emilio G. Cota <[email protected]>--- include/qemu/atomic.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/qemu/atomic.h +0
-0
include/qemu/atomic.h line 104
Quoted 8 lines
include/qemu/atomic.h +1
-1@@ -98,7 +98,7 @@* We'd prefer not want to pull in everything else TCG related, so handle * those few cases by hand. * -* Note that x32 is fully detected with __x64_64__ + _ILP32, and that for +* Note that x32 is fully detected with __x86_64__ + _ILP32, and that for * Sparc we always force the use of sparcv9 in configure. */ #if defined(__x86_64__) || defined(__sparc__)
Signature · 1 line
Alex Bennée
PATCH 4/6 cpus: initialize timers_state.vm_clock_lock
In reply to Emilio G. CotaWe forgot to initialize the spinlock introduced in 94377115b2 ("cpus: protect TimerState writes with a spinlock", 2018-08-23). Fix it.
--- cpus.c | 1 + 1 file changed, 1 insertion(+)
cpus.c +1 -0
@@ -823,6 +823,7 @@ int cpu_throttle_get_percentage(void)void cpu_ticks_init (void ) {seqlock_init (&timers_state.vm_clock_seqlock); +qemu_spin_init (&timers_state.vm_clock_lock);vmstate_register (NULL ,0 , &vmstate_timers, &timers_state); throttle_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL_RT , cpu_throttle_timer_tick,NULL );
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 4/6] cpus: initialize timers_state.vm_clock_lock
Quoted 14 lines · Emilio G. Cota
We forgot to initialize the spinlock introduced in
94377115b2("cpus: protect TimerState writes with a spinlock", 2018-08-23). Fix it.Signed-off-by Emilio G. Cota <[email protected]>--- cpus.c | 1 + 1 file changed, 1 insertion(+)cpus.c +0
-0
cpus.c int cpu_throttle_get_percentage(void) · line 829
Quoted 7 lines
cpus.c +1
-0@@ -823,6 +823,7 @@ int cpu_throttle_get_percentage(void)void cpu_ticks_init (void ) {seqlock_init (&timers_state.vm_clock_seqlock); +qemu_spin_init (&timers_state.vm_clock_lock);vmstate_register (NULL ,0 , &vmstate_timers, &timers_state); throttle_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL_RT , cpu_throttle_timer_tick,NULL );
Signature · 1 line
Alex Bennée
PATCH 5/6 target/i386/translate: use thread-local storage in !user-mode
In reply to Emilio G. CotaNeeded for MTTCG.
--- target/i386/translate.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
target/i386/translate.c +16 -8
@@ -71,26 +71,34 @@//#define MACRO_TEST 1 +/* we need thread-local storage for mttcg */ +#ifdef CONFIG_USER_ONLY +#define I386_THREAD +#else +#define I386_THREAD __thread +#endif +/* global register indexes */ -static TCGv cpu_A0; -static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT; +static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2;static TCGv_i32 cpu_cc_op;static TCGv cpu_regs[CPU_NB_REGS ];static TCGv cpu_seg_base[6 ];static TCGv_i64 cpu_bndl[4 ];static TCGv_i64 cpu_bndu[4 ];/* local temps */ -static TCGv cpu_T0, cpu_T1; +static I386_THREAD TCGv cpu_cc_srcT; +static I386_THREAD TCGv cpu_A0; +static I386_THREAD TCGv cpu_T0, cpu_T1;/* local register indexes (only used inside old micro ops) */ -static TCGv cpu_tmp0, cpu_tmp4; -static TCGv_ptr cpu_ptr0, cpu_ptr1; -static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32; -static TCGv_i64 cpu_tmp1_i64; +static I386_THREAD TCGv cpu_tmp0, cpu_tmp4; +static I386_THREAD TCGv_ptr cpu_ptr0, cpu_ptr1; +static I386_THREAD TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32; +static I386_THREAD TCGv_i64 cpu_tmp1_i64;#include "exec/gen-icount.h" #ifdef TARGET_X86_64 -static int x86_64_hregs; +static I386_THREAD int x86_64_hregs;#endif typedef struct DisasContext {
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 5/6] target/i386/translate: use thread-local storage in !user-mode
Quoted 12 lines · Emilio G. Cota
Needed for MTTCG.
Signed-off-by Emilio G. Cota <[email protected]>--- target/i386/translate.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)target/i386/translate.c +0
-0
target/i386/translate.c line 80
Quoted 10 lines
target/i386/translate.c +7
-0@@ -71,26 +71,34 @@//#define MACRO_TEST 1 +/* we need thread-local storage for mttcg */ +#ifdef CONFIG_USER_ONLY +#define I386_THREAD +#else +#define I386_THREAD __thread +#endif +
I'm confused - as we can have multi-threaded user space don't the same requirements apply?
Quoted 32 lines
target/i386/translate.c +9
-8/* global register indexes */ -static TCGv cpu_A0; -static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT; +static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2;static TCGv_i32 cpu_cc_op;static TCGv cpu_regs[CPU_NB_REGS ];static TCGv cpu_seg_base[6 ];static TCGv_i64 cpu_bndl[4 ];static TCGv_i64 cpu_bndu[4 ];/* local temps */ -static TCGv cpu_T0, cpu_T1; +static I386_THREAD TCGv cpu_cc_srcT; +static I386_THREAD TCGv cpu_A0; +static I386_THREAD TCGv cpu_T0, cpu_T1;/* local register indexes (only used inside old micro ops) */ -static TCGv cpu_tmp0, cpu_tmp4; -static TCGv_ptr cpu_ptr0, cpu_ptr1; -static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32; -static TCGv_i64 cpu_tmp1_i64; +static I386_THREAD TCGv cpu_tmp0, cpu_tmp4; +static I386_THREAD TCGv_ptr cpu_ptr0, cpu_ptr1; +static I386_THREAD TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32; +static I386_THREAD TCGv_i64 cpu_tmp1_i64;#include "exec/gen-icount.h" #ifdef TARGET_X86_64 -static int x86_64_hregs; +static I386_THREAD int x86_64_hregs;#endif typedef struct DisasContext {
Signature · 1 line
Alex Bennée
Re: [Qemu-devel] [PATCH 5/6] target/i386/translate: use thread-local storage in !user-mode
Quoted 18 lines · Alex Bennée
Quoted 15 lines · Emilio G. Cota
Needed for MTTCG.
Signed-off-by Emilio G. Cota <[email protected]>--- target/i386/translate.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)target/i386/translate.c +0
-0@@ -71,26 +71,34 @@//#define MACRO_TEST 1
Quoted 10 lines
target/i386/translate.c line 80
Quoted 7 lines
target/i386/translate.c +7
-0+/* we need thread-local storage for mttcg */ +#ifdef CONFIG_USER_ONLY +#define I386_THREAD +#else +#define I386_THREAD __thread +#endif +I'm confused - as we can have multi-threaded user space don't the same requirements apply?
In user-mode, code generation is serialized by mmap_lock. Making these per-thread would just waste TLS space.
E.
Alex Bennée
Acked-byRe: [Qemu-devel] [PATCH 5/6] target/i386/translate: use thread-local storage in !user-mode
Quoted 30 lines · Emilio G. Cota
Quoted 18 lines · Alex Bennée
Quoted 15 lines · Emilio G. Cota
Needed for MTTCG.
Signed-off-by Emilio G. Cota <[email protected]>--- target/i386/translate.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)target/i386/translate.c +0
-0@@ -71,26 +71,34 @@//#define MACRO_TEST 1 Quoted 10 lines
target/i386/translate.c line 80
Quoted 7 lines
target/i386/translate.c +7
-0+/* we need thread-local storage for mttcg */ +#ifdef CONFIG_USER_ONLY +#define I386_THREAD +#else +#define I386_THREAD __thread +#endif +I'm confused - as we can have multi-threaded user space don't the same requirements apply?
Quoted 2 lines
In user-mode, code generation is serialized by
mmap_lock. Making these per-thread would just waste TLS space.
Ahh this is still the case - ok.
--
Alex Bennée
Re: [Qemu-devel] [PATCH 5/6] target/i386/translate: use thread-local storage in !user-mode
In reply to Emilio G. CotaQuoted 4 lines · Emilio G. Cota
Quoted 2 lines
I'm confused - as we can have multi-threaded user space don't the same requirements apply?
In user-mode, code generation is serialized by
mmap_lock. Making these per-thread would just waste TLS space.
It's stupid question time! How can the TLS work? tcg_x86_init is only called once, the first time cpu_exec_realizefn is called.
Either they can be kept in non-TLS, or you should move them to DisasContext.
Paolo
Re: [Qemu-devel] [PATCH 5/6] target/i386/translate: use thread-local storage in !user-mode
Quoted 10 lines · Paolo Bonzini
Quoted 4 lines · Emilio G. Cota
Quoted 2 lines
I'm confused - as we can have multi-threaded user space don't the same requirements apply?
In user-mode, code generation is serialized by
mmap_lock. Making these per-thread would just waste TLS space.It's stupid question time! How can the TLS work?
tcg_x86_initis only called once, the first timecpu_exec_realizefnis called.Either they can be kept in non-TLS, or you should move them to DisasContext.
Yes, the latter is the Right Thing (tm), as both you and Richard pointed out. I should have done that in the first place.
Will send a v3 with just this change + the configure patch.
Thanks,
Emilio
PATCH 6/6 configure: enable mttcg for i386 and x86_64
In reply to Emilio G. Cota--- configure | 2 ++ 1 file changed, 2 insertions(+)
configure +2 -0
@@ -7025,12 +7025,14 @@ TARGET_ABI_DIR="" case "$target_name" in i386) + mttcg="yes" gdb_xml_files="i386-32bit.xml i386-32bit-core.xml i386-32bit-sse.xml" target_compiler=$cross_cc_i386 target_compiler_cflags=$cross_cc_ccflags_i386 ;; x86_64) TARGET_BASE_ARCH=i386 + mttcg="yes" gdb_xml_files="i386-64bit.xml i386-64bit-core.xml i386-64bit-sse.xml" target_compiler=$cross_cc_x86_64 ;;
Signature · 1 line
2.17.1
Quoted 8 lines · Emilio G. Cota
I sent ~20 days ago a series that allowed me to boot
x86_64with mttcg:
https://patchwork.kernel.org/cover/10564977/Thanks to Paolo's work (already merged), we don't need to hold the BQL when calling
cpu_get_ticks, which makes the MTTCG conversion even simpler. [ I have a couple more patches related to some of that work, but I'll send them next week once Paolo is back ]
Quoted 5 lines
This series enables MTTCG for both
x86_64and i386. I tested both with a buildroot image, and looked for races with Valgrind's helgrind.The first four patches are not really necessary for i386 MTTCG, but since they'll eventually go via Paolo's tree I included them here.
Applied 3-4 for now.
Paolo