Received: from eggs.gnu.org ([2001:4830:134:3::10]:53779) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEbx4-00062q-Fz for qemu-devel@nongnu.org; Mon, 13 Jul 2015 07:31:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZEbx2-0000Eo-W1 for qemu-devel@nongnu.org; Mon, 13 Jul 2015 07:31:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38386) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEbx2-0000Ee-Q4 for qemu-devel@nongnu.org; Mon, 13 Jul 2015 07:31:12 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 77170B7A80 for ; Mon, 13 Jul 2015 11:31:12 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-64.ams2.redhat.com [10.36.112.64]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t6DBV5MN015701 for ; Mon, 13 Jul 2015 07:31:11 -0400 From: Paolo Bonzini Date: Mon, 13 Jul 2015 13:31:05 +0200 Message-Id: <1436787065-25095-4-git-send-email-pbonzini@redhat.com> In-Reply-To: <1436787065-25095-1-git-send-email-pbonzini@redhat.com> References: <1436787065-25095-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH 3/3] rcu: detect missing rcu_register_thread() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Use an "impossible" value for the .depth field in order to quickly detect threads that have not registered themselves with the RCU subsystem. Signed-off-by: Paolo Bonzini --- include/qemu/rcu.h | 4 +++- util/rcu.c | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 3 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 8830295..435cdbe 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; @@ -277,7 +280,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); @@ -288,6 +296,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); -- 2.4.3