[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PULL 08/11] rcu: detect missing rcu_register_thread()
From: |
Wen Congyang |
Subject: |
Re: [Qemu-devel] [PULL 08/11] rcu: detect missing rcu_register_thread() |
Date: |
Fri, 24 Jul 2015 09:06:21 +0800 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 |
On 07/17/2015 12:55 AM, Paolo Bonzini wrote:
> Use an "impossible" value for the .depth field in order to quickly
> detect threads that have not registered themselves with the RCU
> subsystem.
>
> Avoid a false positive around forking by unregistering and registering
> the forking thread explicitly. Previously, it was enough to re-register
> the thread.
>
> Signed-off-by: Paolo Bonzini <address@hidden>
> ---
> include/qemu/rcu.h | 4 +++-
> util/rcu.c | 35 ++++++++++++++++++++++++++++++-----
> 2 files changed, 33 insertions(+), 6 deletions(-)
>
> diff --git a/include/qemu/rcu.h b/include/qemu/rcu.h
> index 7df1e86..4facb35 100644
> --- a/include/qemu/rcu.h
> +++ b/include/qemu/rcu.h
> @@ -82,7 +82,9 @@ static inline void rcu_read_lock(void)
> struct rcu_reader_data *p_rcu_reader = &rcu_reader;
> unsigned ctr;
>
> - if (p_rcu_reader->depth++ > 0) {
> + p_rcu_reader->depth++;
> + assert(p_rcu_reader->depth >= 1);
> + if (p_rcu_reader->depth > 1) {
> return;
> }
>
> diff --git a/util/rcu.c b/util/rcu.c
> index e21bb46..2490273 100644
> --- a/util/rcu.c
> +++ b/util/rcu.c
> @@ -63,8 +63,11 @@ static inline int rcu_gp_ongoing(unsigned long *ctr)
>
> /* Written to only by each individual reader. Read by both the reader and the
> * writers.
> + *
> + * Initializing the depth to -1 causes an assertion failure on the first
> + * call to rcu_read_lock() if the thread does not call rcu_register_thread().
> */
> -__thread struct rcu_reader_data rcu_reader;
> +__thread struct rcu_reader_data rcu_reader = { .depth = -1 };
>
> /* Protected by rcu_gp_lock. */
> typedef QLIST_HEAD(, rcu_reader_data) ThreadList;
> @@ -279,7 +282,12 @@ static void rcu_unregister_thread_notify(Notifier *n,
> void *data)
>
> void rcu_register_thread(void)
> {
> - assert(rcu_reader.ctr == 0);
> + /* rcu_reader.depth is also used to detect whether the thread is
> + * registered.
> + */
> + assert(rcu_reader.depth == -1);
> + rcu_reader.depth = 0;
> +
> qemu_mutex_lock(&rcu_gp_lock);
> QLIST_INSERT_HEAD(®istry, &rcu_reader, node);
> qemu_mutex_unlock(&rcu_gp_lock);
> @@ -290,6 +298,12 @@ void rcu_register_thread(void)
>
> void rcu_unregister_thread(void)
> {
> + /* Resetting the depth to -1 causes an assertion failure on the next
> + * call to rcu_read_lock().
> + */
> + assert(rcu_reader.depth == 0);
> + rcu_reader.depth = -1;
> +
> qemu_mutex_lock(&rcu_gp_lock);
> QLIST_REMOVE(&rcu_reader, node);
> qemu_mutex_unlock(&rcu_gp_lock);
> @@ -301,7 +315,6 @@ static void rcu_init_complete(void)
> {
> QemuThread thread;
>
> - qemu_mutex_init(&rcu_gp_lock);
> qemu_event_init(&rcu_gp_event, true);
>
> qemu_event_init(&rcu_call_ready_event, false);
> @@ -311,8 +324,6 @@ static void rcu_init_complete(void)
> */
> qemu_thread_create(&thread, "call_rcu", call_rcu_thread,
> NULL, QEMU_THREAD_DETACHED);
> -
> - rcu_register_thread();
> }
>
> #ifdef CONFIG_POSIX
> @@ -329,14 +340,28 @@ static void rcu_init_unlock(void)
>
> void rcu_after_fork(void)
> {
> + int save_depth = rcu_reader.depth;
> + if (save_depth != -1) {
If we allow call fork() in RCU read-side critical section,
rcu_reader.depth should be set to 0 before calling
rcu_unregister_thread().
Thanks
Wen Congyang
> + rcu_unregister_thread();
> + }
> +
> memset(®istry, 0, sizeof(registry));
> +
> rcu_init_complete();
> +
> + if (save_depth != -1) {
> + rcu_register_thread();
> + rcu_reader.depth = save_depth;
> + }
> }
>
> static void __attribute__((__constructor__)) rcu_init(void)
> {
> + qemu_mutex_init(&rcu_gp_lock);
> #ifdef CONFIG_POSIX
> pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_unlock);
> #endif
> rcu_init_complete();
> +
> + rcu_register_thread();
> }
>
- [Qemu-devel] [PULL 00/11] RCU, KVM, memory API, crypto, Coverity fixes for 2.4.0-rc1, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 01/11] mips/kvm: Fix Big endian 32-bit register access, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 02/11] mips/kvm: Sign extend registers written to KVM, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 03/11] ppc/spapr_drc: fix memory leak, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 04/11] arm/xlnx-zynqmp: fix memory leak, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 05/11] RDMA: Fix error exits, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 07/11] rcu: actually register threads that have RCU read-side critical sections, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 06/11] rcu: automatically unregister threads when they exit, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 08/11] rcu: detect missing rcu_register_thread(), Paolo Bonzini, 2015/07/16
- Re: [Qemu-devel] [PULL 08/11] rcu: detect missing rcu_register_thread(),
Wen Congyang <=
- [Qemu-devel] [PULL 10/11] crypto: fix build with nettle >= 3.0.0, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 09/11] memory: fix refcount leak in memory_region_present, Paolo Bonzini, 2015/07/16
- [Qemu-devel] [PULL 11/11] crypto: avoid undefined behavior in nettle calls, Paolo Bonzini, 2015/07/16
- Re: [Qemu-devel] [PULL 00/11] RCU, KVM, memory API, crypto, Coverity fixes for 2.4.0-rc1, Peter Maydell, 2015/07/16