Skip to contentSkip to conversation

coroutine: Assertions and debugging aids

PATCH Applied Kevin Wolf 3 participants 6 messages Aug 11, 2016 – Aug 15, 2016

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover coroutine: Assertions and debugging aids

A while ago we were debugging a hang where coroutines were waiting for a mutex to be unlocked, but we couldn't find out who held the lock. This series adds some information to Coroutine and CoMutex that both allows to add a few assertions to check locking behaviour and can be used to find the culprit when analysing a core dump.

Kevin Wolf (2):
coroutine: Let CoMutex remember who holds it
coroutine: Assert that no locks are held on termination

 include/qemu/coroutine.h     |  1 +
 include/qemu/coroutine_int.h |  1 +
 util/qemu-coroutine-lock.c   | 14 ++++++++++++++
 util/qemu-coroutine.c        |  1 +
 4 files changed, 17 insertions(+)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/2 coroutine: Let CoMutex remember who holds it

In cases of deadlocks, knowing who holds a given CoMutex is really helpful for debugging. Keeping the information around doesn't cost much and allows us to add another assertion to keep the code correct, so let's just add it.

---
 include/qemu/coroutine.h   | 1 +
 util/qemu-coroutine-lock.c | 3 +++
 2 files changed, 4 insertions(+)
include/qemu/coroutine.h +1 -0
index ac8d4c9..29a2078 100644
@@ -143,6 +143,7 @@ bool qemu_co_queue_empty(CoQueue *queue);
  */
 typedef struct CoMutex {
     bool locked;
+    Coroutine *holder;
     CoQueue queue;
 } CoMutex;
 
util/qemu-coroutine-lock.c +3 -0
index 22aa9ab..f30ee81 100644
@@ -129,6 +129,7 @@ void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
     }
 
     mutex->locked = true;
+    mutex->holder = self;
 
     trace_qemu_co_mutex_lock_return(mutex, self);
 }
@@ -140,9 +141,11 @@ void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
     trace_qemu_co_mutex_unlock_entry(mutex, self);
 
     assert(mutex->locked == true);
+    assert(mutex->holder == self);
     assert(qemu_in_coroutine());
 
     mutex->locked = false;
+    mutex->holder = NULL;
     qemu_co_queue_next(&mutex->queue);
 
     trace_qemu_co_mutex_unlock_return(mutex, self);
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH 2/2 coroutine: Assert that no locks are held on termination

In reply to Kevin Wolf

A coroutine that takes a lock must also release it again. If the coroutine terminates without having released all its locks, it's buggy and we'll probably run into a deadlock sooner or later. Make sure that we don't get such cases.

---
 include/qemu/coroutine_int.h |  1 +
 util/qemu-coroutine-lock.c   | 11 +++++++++++
 util/qemu-coroutine.c        |  1 +
 3 files changed, 13 insertions(+)
include/qemu/coroutine_int.h +1 -0
index 581a7f5..6df9d33 100644
@@ -39,6 +39,7 @@ struct Coroutine {
     void *entry_arg;
     Coroutine *caller;
     QSLIST_ENTRY(Coroutine) pool_next;
+    size_t locks_held;
 
     /* Coroutines that should be woken up when we yield or terminate */
     QSIMPLEQ_HEAD(, Coroutine) co_queue_wakeup;
util/qemu-coroutine-lock.c +11 -0
index f30ee81..14cf9ce 100644
@@ -130,6 +130,7 @@ void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
 
     mutex->locked = true;
     mutex->holder = self;
+    self->locks_held++;
 
     trace_qemu_co_mutex_lock_return(mutex, self);
 }
@@ -146,6 +147,7 @@ void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
 
     mutex->locked = false;
     mutex->holder = NULL;
+    self->locks_held--;
     qemu_co_queue_next(&mutex->queue);
 
     trace_qemu_co_mutex_unlock_return(mutex, self);
@@ -159,14 +161,19 @@ void qemu_co_rwlock_init(CoRwlock *lock)
 
 void qemu_co_rwlock_rdlock(CoRwlock *lock)
 {
+    Coroutine *self = qemu_coroutine_self();
+
     while (lock->writer) {
         qemu_co_queue_wait(&lock->queue);
     }
     lock->reader++;
+    self->locks_held++;
 }
 
 void qemu_co_rwlock_unlock(CoRwlock *lock)
 {
+    Coroutine *self = qemu_coroutine_self();
+
     assert(qemu_in_coroutine());
     if (lock->writer) {
         lock->writer = false;
@@ -179,12 +186,16 @@ void qemu_co_rwlock_unlock(CoRwlock *lock)
             qemu_co_queue_next(&lock->queue);
         }
     }
+    self->locks_held--;
 }
 
 void qemu_co_rwlock_wrlock(CoRwlock *lock)
 {
+    Coroutine *self = qemu_coroutine_self();
+
     while (lock->writer || lock->reader) {
         qemu_co_queue_wait(&lock->queue);
     }
     lock->writer = true;
+    self->locks_held++;
 }
util/qemu-coroutine.c +1 -0
index 89f21a9..3cbf225 100644
@@ -122,6 +122,7 @@ void qemu_coroutine_enter(Coroutine *co)
     case COROUTINE_YIELD:
         return;
     case COROUTINE_TERMINATE:
+        assert(!co->locks_held);
         trace_qemu_coroutine_terminate(co);
         coroutine_delete(co);
         return;
Signature · 1 line
1.8.3.1
Reviewed-by on the v1 series
#3 lore raw
In reply to Kevin Wolf
Quoted 11 lines

A while ago we were debugging a hang where coroutines were waiting for a mutex
to be unlocked, but we couldn't find out who held the lock. This series adds some information to Coroutine and CoMutex that both allows to add a few assertions to check locking behaviour and can be used to find the culprit when analysing a core dump.

Kevin Wolf (2):
coroutine: Let CoMutex remember who holds it
coroutine: Assert that no locks are held on termination

Quoted 5 lines
 include/qemu/coroutine.h     |  1 +
 include/qemu/coroutine_int.h |  1 +
 util/qemu-coroutine-lock.c   | 14 ++++++++++++++
 util/qemu-coroutine.c        |  1 +
 4 files changed, 17 insertions(+)

Thanks for finally getting round to this.

Reviewed-by on the v1 series
#4 lore raw
In reply to Kevin Wolf
Quoted 10 lines · Kevin Wolf

A while ago we were debugging a hang where coroutines were waiting for a mutex to be unlocked, but we couldn't find out who held the lock. This series adds some information to Coroutine and CoMutex that both allows to add a few assertions to check locking behaviour and can be used to find the culprit when analysing a core dump.

Kevin Wolf (2):
coroutine: Let CoMutex remember who holds it
coroutine: Assert that no locks are held on termination

Quoted 10 lines
 include/qemu/coroutine.h     |  1 +
 include/qemu/coroutine_int.h |  1 +
 util/qemu-coroutine-lock.c   | 14 ++++++++++++++
 util/qemu-coroutine.c        |  1 +
 4 files changed, 17 insertions(+)
Signature · 1 line
1.8.3.1
author Applied
#5 lore raw
In reply to Kevin Wolf

Am 11.08.2016 um 18:22 hat Kevin Wolf geschrieben:

Quoted 5 lines

A while ago we were debugging a hang where coroutines were waiting for a mutex to be unlocked, but we couldn't find out who held the lock. This series adds some information to Coroutine and CoMutex that both allows to add a few assertions to check locking behaviour and can be used to find the culprit when analysing a core dump.

Applied to block-next.

Kevin