From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49763) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YQNS7-0003KQ-VU for qemu-devel@nongnu.org; Tue, 24 Feb 2015 16:55:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YQNRv-0008Qs-Ix for qemu-devel@nongnu.org; Tue, 24 Feb 2015 16:55:39 -0500 Received: from e33.co.us.ibm.com ([32.97.110.151]:41519) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YQNRv-0008QZ-D7 for qemu-devel@nongnu.org; Tue, 24 Feb 2015 16:55:27 -0500 Received: from /spool/local by e33.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 24 Feb 2015 14:55:26 -0700 From: Michael Roth Date: Tue, 24 Feb 2015 15:48:13 -0600 Message-Id: <1424814498-6993-39-git-send-email-mdroth@linux.vnet.ibm.com> In-Reply-To: <1424814498-6993-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1424814498-6993-1-git-send-email-mdroth@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 38/43] qemu-thread: fix qemu_event without futexes List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Paolo Bonzini , qemu-stable@nongnu.org From: Paolo Bonzini This had a possible deadlock that was visible with rcutorture. qemu_event_set qemu_event_wait ---------------------------------------------------------------- cmpxchg reads FREE, writes BUSY futex_wait: pthread_mutex_lock futex_wait: value == BUSY xchg reads BUSY, writes SET futex_wake: pthread_cond_broadcast futex_wait: pthread_cond_wait The fix is simply to avoid condvar tricks and do the obvious locking around pthread_cond_broadcast: qemu_event_set qemu_event_wait ---------------------------------------------------------------- cmpxchg reads FREE, writes BUSY futex_wait: pthread_mutex_lock futex_wait: value == BUSY xchg reads BUSY, writes SET futex_wake: pthread_mutex_lock (blocks) futex_wait: pthread_cond_wait (mutex unlocked) futex_wake: pthread_cond_broadcast futex_wake: pthread_mutex_unlock futex_wait: pthread_mutex_unlock Cc: qemu-stable@nongnu.org Signed-off-by: Paolo Bonzini (cherry picked from commit 158ef8cbb7e0fe8bb430310924b8bebe5f186e6e) Signed-off-by: Michael Roth --- util/qemu-thread-posix.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index d05a649..bb14ad4 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -306,11 +306,13 @@ static inline void futex_wait(QemuEvent *ev, unsigned val) #else static inline void futex_wake(QemuEvent *ev, int n) { + pthread_mutex_lock(&ev->lock); if (n == 1) { pthread_cond_signal(&ev->cond); } else { pthread_cond_broadcast(&ev->cond); } + pthread_mutex_unlock(&ev->lock); } static inline void futex_wait(QemuEvent *ev, unsigned val) -- 1.9.1