Skip to contentSkip to conversation

coroutine rwlock downgrade fix, minor VDI changes

PATCH Superseded Paolo Bonzini 4 participants 15 messages Mar 17, 2021 – Mar 24, 2021 Block layer

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover coroutine rwlock downgrade fix, minor VDI changes

This is a resubmit of David Edmondson's series at
https://patchew.org/QEMU/[email protected]/.
After closer analysis on IRC, the CoRwlock's attempt to ensure fairness turned out to be flawed. Therefore, this series reimplements CoRwlock without using a CoQueue. Tracking whether each queued coroutine is a reader/writer makes it possible to never wake a writer when only readers should be allowed and vice versa.

v2->v3: new CoRwlock implementation

v3->v4: fix upgrade and add a test for that, too

v4->v5: typo

David Edmondson (4):
block/vdi: When writing new bmap entry fails, don't leak the buffer block/vdi: Don't assume that blocks are larger than VdiHeader coroutine/mutex: Store the coroutine in the CoWaitRecord only once
test-coroutine: Add rwlock downgrade test

Paolo Bonzini (2):
coroutine-lock: reimplement CoRwlock to fix downgrade bug
test-coroutine: add rwlock upgrade test

 block/vdi.c                 |  11 ++-
 include/qemu/coroutine.h    |  17 ++--
 tests/unit/test-coroutine.c | 161 ++++++++++++++++++++++++++++++++++++
 util/qemu-coroutine-lock.c  | 149 +++++++++++++++++++++------------
 4 files changed, 274 insertions(+), 64 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH v5 1/6 block/vdi: When writing new bmap entry fails, don't leak the buffer

From: David Edmondson <[email protected]>

If a new bitmap entry is allocated, requiring the entire block to be written, avoiding leaking the buffer allocated for the block should the write fail.

Message-Id: <[email protected]>

---
 block/vdi.c | 1 +
 1 file changed, 1 insertion(+)
block/vdi.c +1 -0
index 5627e7d764..2a6dc26124 100644
@@ -690,6 +690,7 @@ nonallocating_write:
 
     logout("finished data write\n");
     if (ret < 0) {
+        g_free(block);
         return ret;
     }
 
Signature · 1 line
2.29.2
Reviewed-by on v5 1/6
#2 lore raw

Re: [PATCH 1/6] block/vdi: When writing new bmap entry fails, don't leak the buffer

Quoted 6 lines · Paolo Bonzini

From: David Edmondson <[email protected]>

If a new bitmap entry is allocated, requiring the entire block to be written, avoiding leaking the buffer allocated for the block should the write fail.

Quoted 7 lines

Message-Id: <[email protected]>

---
  block/vdi.c | 1 +
  1 file changed, 1 insertion(+)
#3 lore raw

PATCH v5 2/6 block/vdi: Don't assume that blocks are larger than VdiHeader

In reply to Paolo Bonzini

From: David Edmondson <[email protected]>

Given that the block size is read from the header of the VDI file, a wide variety of sizes might be seen. Rather than re-using a block sized memory region when writing the VDI header, allocate an appropriately sized buffer.

Message-Id: <[email protected]>

---
 block/vdi.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
block/vdi.c +6 -4
index 2a6dc26124..548f8a057b 100644
@@ -696,18 +696,20 @@ nonallocating_write:
 
     if (block) {
         /* One or more new blocks were allocated. */
-        VdiHeader *header = (VdiHeader *) block;
+        VdiHeader *header;
         uint8_t *base;
         uint64_t offset;
         uint32_t n_sectors;
 
+        g_free(block);
+        header = g_malloc(sizeof(*header));
+
         logout("now writing modified header\n");
         assert(VDI_IS_ALLOCATED(bmap_first));
         *header = s->header;
         vdi_header_to_le(header);
-        ret = bdrv_pwrite(bs->file, 0, block, sizeof(VdiHeader));
-        g_free(block);
-        block = NULL;
+        ret = bdrv_pwrite(bs->file, 0, header, sizeof(*header));
+        g_free(header);
 
         if (ret < 0) {
             return ret;
Signature · 1 line
2.29.2
Reviewed-by on v5 2/6
#4 lore raw

Re: [PATCH 2/6] block/vdi: Don't assume that blocks are larger than VdiHeader

Quoted 19 lines · Paolo Bonzini

From: David Edmondson <[email protected]>

Given that the block size is read from the header of the VDI file, a wide variety of sizes might be seen. Rather than re-using a block sized memory region when writing the VDI header, allocate an appropriately sized buffer.

Message-Id: <[email protected]>

---
  block/vdi.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
block/vdi.c +0 -0
index 2a6dc26124..548f8a057b 100644

block/vdi.c nonallocating_write: · line 704

Quoted 10 lines
block/vdi.c +2 -1
@@ -696,18 +696,20 @@ nonallocating_write:
 
      if (block) {
          /* One or more new blocks were allocated. */
-        VdiHeader *header = (VdiHeader *) block;
+        VdiHeader *header;
          uint8_t *base;
          uint64_t offset;
          uint32_t n_sectors;
 
+        g_free(block);

This logic (checking @block != NULL) was a bit weird before and this just seems to accomodate it. We probably shouldn’t check @block, but some boolean, and *block should be freed after the while () loop.

Considering the “if (block)” was there already and it’d be weird to keep that check if *block were freed by that point, I suppose this isn’t making it worse, though, so:

Quoted 15 lines
block/vdi.c +4 -3
+        header = g_malloc(sizeof(*header));
+
          logout("now writing modified header\n");
          assert(VDI_IS_ALLOCATED(bmap_first));
          *header = s->header;
          vdi_header_to_le(header);
-        ret = bdrv_pwrite(bs->file, 0, block, sizeof(VdiHeader));
-        g_free(block);
-        block = NULL;
+        ret = bdrv_pwrite(bs->file, 0, header, sizeof(*header));
+        g_free(header);
 
          if (ret < 0) {
              return ret;
#5 lore raw

PATCH v5 3/6 coroutine/mutex: Store the coroutine in the CoWaitRecord only once

In reply to Paolo Bonzini

From: David Edmondson <[email protected]>

When taking the slow path for mutex acquisition, set the coroutine value in the CoWaitRecord in push_waiter(), rather than both there and in the caller.

Message-Id: <[email protected]>

---
 util/qemu-coroutine-lock.c | 1 -
 1 file changed, 1 deletion(-)
util/qemu-coroutine-lock.c +0 -1
index 5816bf8900..eb73cf11dc 100644
@@ -204,7 +204,6 @@ static void coroutine_fn qemu_co_mutex_lock_slowpath(AioContext *ctx,
     unsigned old_handoff;
 
     trace_qemu_co_mutex_lock_entry(mutex, self);
-    w.co = self;
     push_waiter(mutex, &w);
 
     /* This is the "Responsibility Hand-Off" protocol; a lock() picks from
Signature · 1 line
2.29.2
#6 lore raw

PATCH v5 4/6 coroutine-lock: reimplement CoRwlock to fix downgrade bug

In reply to Paolo Bonzini

An invariant of the current rwlock is that if multiple coroutines hold a reader lock, all must be runnable. The unlock implementation relies on this, choosing to wake a single coroutine when the final read lock holder exits the critical section, assuming that it will wake a coroutine attempting to acquire a write lock.

The downgrade implementation violates this assumption by creating a read lock owning coroutine that is exclusively runnable - any other coroutines that are waiting to acquire a read lock are *not* made runnable when the write lock holder converts its ownership to read only.

More in general, the old implementation had lots of other fairness bugs. The root cause of the bugs was that CoQueue would wake up readers even if there were pending writers, and would wake up writers even if there were readers. In that case, the coroutine would go back to sleep *at the end* of the CoQueue, losing its place at the head of the line.

To fix this, keep the queue of waiters explicitly in the CoRwlock instead of using CoQueue, and store for each whether it is a potential reader or a writer. This way, downgrade can look at the first queued coroutines and wake it only if it is a reader, causing all other readers in line to be released in turn.

---
v3->v4: clean up the code and fix upgrade logic. Fix upgrade comment too.

 include/qemu/coroutine.h   |  17 +++--
 util/qemu-coroutine-lock.c | 148 ++++++++++++++++++++++++-------------
 2 files changed, 106 insertions(+), 59 deletions(-)
include/qemu/coroutine.h +10 -7
index 84eab6e3bf..7919d3bb62 100644
@@ -237,11 +237,15 @@ bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock);
 bool qemu_co_queue_empty(CoQueue *queue);
 
 
+typedef struct CoRwTicket CoRwTicket;
 typedef struct CoRwlock {
-    int pending_writer;
-    int reader;
     CoMutex mutex;
-    CoQueue queue;
+
+    /* Number of readers, or -1 if owned for writing.  */
+    int owners;
+
+    /* Waiting coroutines.  */
+    QSIMPLEQ_HEAD(, CoRwTicket) tickets;
 } CoRwlock;
 
 /**
@@ -260,10 +264,9 @@ void qemu_co_rwlock_rdlock(CoRwlock *lock);
 /**
  * Write Locks the CoRwlock from a reader.  This is a bit more efficient than
  * @qemu_co_rwlock_unlock followed by a separate @qemu_co_rwlock_wrlock.
- * However, if the lock cannot be upgraded immediately, control is transferred
- * to the caller of the current coroutine.  Also, @qemu_co_rwlock_upgrade
- * only overrides CoRwlock fairness if there are no concurrent readers, so
- * another writer might run while @qemu_co_rwlock_upgrade blocks.
+ * Note that if the lock cannot be upgraded immediately, control is transferred
+ * to the caller of the current coroutine; another writer might run while
+ * @qemu_co_rwlock_upgrade blocks.
  */
 void qemu_co_rwlock_upgrade(CoRwlock *lock);
 
util/qemu-coroutine-lock.c +96 -52

189 lines not on this page · raw message

Signature · 1 line
2.29.2
#7 lore raw

Re: [PATCH 4/6] coroutine-lock: reimplement CoRwlock to fix downgrade bug

Quoted 18 lines · Paolo Bonzini
util/qemu-coroutine-lock.c +18 -0
+static void qemu_co_rwlock_maybe_wake_one(CoRwlock *lock)
+{
+    CoRwTicket *tkt = QSIMPLEQ_FIRST(&lock->tickets);
+    Coroutine *co = NULL;
+
+    /*
+     * Setting lock->owners here prevents rdlock and wrlock from
+     * sneaking in between unlock and wake.
+     */
+
+    if (tkt) {
+        if (tkt->read) {
+            if (lock->owners >= 0) {
+                lock->owners++;
+                co = tkt->co;
+            }
+        } else {
+            if (lock->owners == 0) {

util/qemu-coroutine-lock.c void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex) · line 371

Quoted 10 lines
util/qemu-coroutine-lock.c +10 -0
+                lock->owners = -1;
+                co = tkt->co;
+            }
+        }
+    }
+
+    if (co) {
+        QSIMPLEQ_REMOVE_HEAD(&lock->tickets, next);
+        qemu_co_mutex_unlock(&lock->mutex);
+        aio_co_wake(co);

I find it hard to reason about QSIMPLEQ_EMPTY(&lock->tickets) callers that execute before co is entered. They see an empty queue even though a coroutine is about to run. Updating owners above ensures that the code correctly tracks the state of the rwlock, but I'm not 100% confident about this aspect of the code.

Quoted 20 lines

void qemu_co_rwlock_upgrade(CoRwlock *lock)
{

util/qemu-coroutine-lock.c +7 -9
-    Coroutine *self = qemu_coroutine_self();
-
     qemu_co_mutex_lock(&lock->mutex);
-    assert(lock->reader > 0);
-    lock->reader--;
-    lock->pending_writer++;
-    while (lock->reader) {
-        qemu_co_queue_wait(&lock->queue, &lock->mutex);
-    }
-    lock->pending_writer--;
+    assert(lock->owners > 0);
+    /* For fairness, wait if a writer is in line.  */
+    if (lock->owners == 1 && QSIMPLEQ_EMPTY(&lock->tickets)) {
+        lock->owners = -1;
+        qemu_co_mutex_unlock(&lock->mutex);
+    } else {
+        CoRwTicket my_ticket = { false, qemu_coroutine_self() };
 

util/qemu-coroutine-lock.c void qemu_co_rwlock_unlock(CoRwlock *lock) · line 465

Quoted 10 lines
util/qemu-coroutine-lock.c +5 -5
-    /* The rest of the write-side critical section is run with
-     * the mutex taken, similar to qemu_co_rwlock_wrlock.  Do
-     * not account for the lock twice in self->locks_held.
-     */
-    self->locks_held--;
+        lock->owners--;
+        QSIMPLEQ_INSERT_TAIL(&lock->tickets, &my_ticket, next);
+        qemu_co_rwlock_maybe_wake_one(lock);
+        qemu_coroutine_yield();
+        assert(lock->owners == -1);

lock->owners is read outside lock->mutex here. Not sure if this can cause problems.

Quoted 2 lines

+ }
}

locks_held is kept unchanged across qemu_coroutine_yield() even though the read lock has been released. rdlock() and wrlock() only increment locks_held after acquiring the rwlock.

In practice I don't think it matters, but it seems inconsistent. If locks_held is supposed to track tickets (not just coroutines currently holding a lock), then rdlock() and wrlock() should increment before yielding.

#8 lore raw

Re: [PATCH 4/6] coroutine-lock: reimplement CoRwlock to fix downgrade bug

Quoted 27 lines · Stefan Hajnoczi
Quoted 26 lines · Paolo Bonzini
util/qemu-coroutine-lock.c +26 -0
+static void qemu_co_rwlock_maybe_wake_one(CoRwlock *lock)
+{
+    CoRwTicket *tkt = QSIMPLEQ_FIRST(&lock->tickets);
+    Coroutine *co = NULL;
+
+    /*
+     * Setting lock->owners here prevents rdlock and wrlock from
+     * sneaking in between unlock and wake.
+     */
+
+    if (tkt) {
+        if (tkt->read) {
+            if (lock->owners >= 0) {
+                lock->owners++;
+                co = tkt->co;
+            }
+        } else {
+            if (lock->owners == 0) {
+                lock->owners = -1;
+                co = tkt->co;
+            }
+        }
+    }
+
+    if (co) {
+        QSIMPLEQ_REMOVE_HEAD(&lock->tickets, next);
Quoted 8 lines

util/qemu-coroutine-lock.c void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex) · line 371

Quoted 2 lines
util/qemu-coroutine-lock.c +2 -0
+        qemu_co_mutex_unlock(&lock->mutex);
+        aio_co_wake(co);

I find it hard to reason about QSIMPLEQ_EMPTY(&lock->tickets) callers that execute before co is entered. They see an empty queue even though a coroutine is about to run. Updating owners above ensures that the code correctly tracks the state of the rwlock, but I'm not 100% confident about this aspect of the code.

Good point. The invariant when lock->mutex is released should be clarified; a better way to phrase the comment above "if (tkt)" is:

The invariant when lock->mutex is released is that every ticket is tracked in either lock->owners or lock->tickets. By updating lock->owners here, rdlock/wrlock/upgrade will block even if they execute between qemu_co_mutex_unlock and aio_co_wake.

Quoted 9 lines

util/qemu-coroutine-lock.c void qemu_co_rwlock_unlock(CoRwlock *lock) · line 465

Quoted 6 lines
util/qemu-coroutine-lock.c +5 -1
-    self->locks_held--;
+        lock->owners--;
+        QSIMPLEQ_INSERT_TAIL(&lock->tickets, &my_ticket, next);
+        qemu_co_rwlock_maybe_wake_one(lock);
+        qemu_coroutine_yield();
+        assert(lock->owners == -1);

lock->owners is read outside lock->mutex here. Not sure if this can cause problems.

True. It is okay though because lock->owners cannot change until this coroutine unlocks. A worse occurrence of the issue is in rdlock:

        assert(lock->owners >= 1);

        /* Possibly wake another reader, which will wake the next in 
line.  */
        qemu_co_mutex_lock(&lock->mutex);

where the assert should be moved after taking the lock, or possibly changed to use qatomic_read. (I prefer the former).

Quoted 8 lines

locks_held is kept unchanged across qemu_coroutine_yield() even though the read lock has been released. rdlock() and wrlock() only increment locks_held after acquiring the rwlock.

In practice I don't think it matters, but it seems inconsistent. If locks_held is supposed to track tickets (not just coroutines currently holding a lock), then rdlock() and wrlock() should increment before yielding.

locks_held (unlike owners) is not part of the lock, it's part of the Coroutine and only used for debugging (asserting that terminating coroutines are not holding any lock).

Paolo

#9 lore raw

PATCH v5 5/6 test-coroutine: add rwlock upgrade test

In reply to Paolo Bonzini

Test that rwlock upgrade is fair, and readers go back to sleep if a writer is in line.

---
 tests/unit/test-coroutine.c | 62 +++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
tests/unit/test-coroutine.c +62 -0
index e946d93a65..6e6f51d480 100644
@@ -264,6 +264,67 @@ static void test_co_mutex_lockable(void)
     g_assert(QEMU_MAKE_LOCKABLE(null_pointer) == NULL);
 }
 
+static CoRwlock rwlock;
+
+/* Test that readers are properly sent back to the queue when upgrading,
+ * even if they are the sole readers.  The test scenario is as follows:
+ *
+ *
+ * | c1           | c2         |
+ * |--------------+------------+
+ * | rdlock       |            |
+ * | yield        |            |
+ * |              | wrlock     |
+ * |              | <queued>   |
+ * | upgrade      |            |
+ * | <queued>     | <dequeued> |
+ * |              | unlock     |
+ * | <dequeued>   |            |
+ * | unlock       |            |
+ */
+
+static void coroutine_fn rwlock_yield_upgrade(void *opaque)
+{
+    qemu_co_rwlock_rdlock(&rwlock);
+    qemu_coroutine_yield();
+
+    qemu_co_rwlock_upgrade(&rwlock);
+    qemu_co_rwlock_unlock(&rwlock);
+
+    *(bool *)opaque = true;
+}
+
+static void coroutine_fn rwlock_wrlock_yield(void *opaque)
+{
+    qemu_co_rwlock_wrlock(&rwlock);
+    qemu_coroutine_yield();
+
+    qemu_co_rwlock_unlock(&rwlock);
+    *(bool *)opaque = true;
+}
+
+static void test_co_rwlock_upgrade(void)
+{
+    bool c1_done = false;
+    bool c2_done = false;
+    Coroutine *c1, *c2;
+
+    qemu_co_rwlock_init(&rwlock);
+    c1 = qemu_coroutine_create(rwlock_yield_upgrade, &c1_done);
+    c2 = qemu_coroutine_create(rwlock_wrlock_yield, &c2_done);
+
+    qemu_coroutine_enter(c1);
+    qemu_coroutine_enter(c2);
+
+    /* c1 now should go to sleep.  */
+    qemu_coroutine_enter(c1);
+    g_assert(!c1_done);
+
+    qemu_coroutine_enter(c2);
+    g_assert(c1_done);
+    g_assert(c2_done);
+}
+
 /*
  * Check that creation, enter, and return work
  */
@@ -501,6 +562,7 @@ int main(int argc, char **argv)
     g_test_add_func("/basic/order", test_order);
     g_test_add_func("/locking/co-mutex", test_co_mutex);
     g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable);
+    g_test_add_func("/locking/co-rwlock/upgrade", test_co_rwlock_upgrade);
     if (g_test_perf()) {
         g_test_add_func("/perf/lifecycle", perf_lifecycle);
         g_test_add_func("/perf/nesting", perf_nesting);
Signature · 1 line
2.29.2
Reviewed-by on v5 5/6
#10 lore raw

Re: [PATCH 5/6] test-coroutine: add rwlock upgrade test

Quoted 4 lines · Paolo Bonzini

Test that rwlock upgrade is fair, and readers go back to sleep if a writer is in line.

Quoted 86 lines
---
 tests/unit/test-coroutine.c | 62 +++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
tests/unit/test-coroutine.c +62 -0
index e946d93a65..6e6f51d480 100644
@@ -264,6 +264,67 @@ static void test_co_mutex_lockable(void)
     g_assert(QEMU_MAKE_LOCKABLE(null_pointer) == NULL);
 }
 
+static CoRwlock rwlock;
+
+/* Test that readers are properly sent back to the queue when upgrading,
+ * even if they are the sole readers.  The test scenario is as follows:
+ *
+ *
+ * | c1           | c2         |
+ * |--------------+------------+
+ * | rdlock       |            |
+ * | yield        |            |
+ * |              | wrlock     |
+ * |              | <queued>   |
+ * | upgrade      |            |
+ * | <queued>     | <dequeued> |
+ * |              | unlock     |
+ * | <dequeued>   |            |
+ * | unlock       |            |
+ */
+
+static void coroutine_fn rwlock_yield_upgrade(void *opaque)
+{
+    qemu_co_rwlock_rdlock(&rwlock);
+    qemu_coroutine_yield();
+
+    qemu_co_rwlock_upgrade(&rwlock);
+    qemu_co_rwlock_unlock(&rwlock);
+
+    *(bool *)opaque = true;
+}
+
+static void coroutine_fn rwlock_wrlock_yield(void *opaque)
+{
+    qemu_co_rwlock_wrlock(&rwlock);
+    qemu_coroutine_yield();
+
+    qemu_co_rwlock_unlock(&rwlock);
+    *(bool *)opaque = true;
+}
+
+static void test_co_rwlock_upgrade(void)
+{
+    bool c1_done = false;
+    bool c2_done = false;
+    Coroutine *c1, *c2;
+
+    qemu_co_rwlock_init(&rwlock);
+    c1 = qemu_coroutine_create(rwlock_yield_upgrade, &c1_done);
+    c2 = qemu_coroutine_create(rwlock_wrlock_yield, &c2_done);
+
+    qemu_coroutine_enter(c1);
+    qemu_coroutine_enter(c2);
+
+    /* c1 now should go to sleep.  */
+    qemu_coroutine_enter(c1);
+    g_assert(!c1_done);
+
+    qemu_coroutine_enter(c2);
+    g_assert(c1_done);
+    g_assert(c2_done);
+}
+
 /*
  * Check that creation, enter, and return work
  */
@@ -501,6 +562,7 @@ int main(int argc, char **argv)
     g_test_add_func("/basic/order", test_order);
     g_test_add_func("/locking/co-mutex", test_co_mutex);
     g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable);
+    g_test_add_func("/locking/co-rwlock/upgrade", test_co_rwlock_upgrade);
     if (g_test_perf()) {
         g_test_add_func("/perf/lifecycle", perf_lifecycle);
         g_test_add_func("/perf/nesting", perf_nesting);
Signature · 1 line
2.29.2

dme.

Signature · 1 line
Don't you know you're never going to get to France.
#11 lore raw

PATCH v5 6/6 test-coroutine: Add rwlock downgrade test

In reply to Paolo Bonzini

From: David Edmondson <[email protected]>

Test that downgrading an rwlock does not result in a failure to schedule coroutines queued on the rwlock.

The diagram associated with test_co_rwlock_downgrade() describes the intended behaviour, but what was observed previously corresponds to:

| c1     | c2         | c3         | c4       |
|--------+------------+------------+----------|
| rdlock |            |            |          |
| yield  |            |            |          |
|        | wrlock     |            |          |
|        | <queued>   |            |          |
|        |            | rdlock     |          |
|        |            | <queued>   |          |
|        |            |            | wrlock   |
|        |            |            | <queued> |
| unlock |            |            |          |
| yield  |            |            |          |
|        | <dequeued> |            |          |
|        | downgrade  |            |          |
|        | ...        |            |          |
|        | unlock     |            |          |
|        |            | <dequeued> |          |
|        |            | <queued>   |          |

This results in a failure...

ERROR:../tests/test-coroutine.c:369:test_co_rwlock_downgrade: assertion failed: (c3_done) Bail out! ERROR:../tests/test-coroutine.c:369:test_co_rwlock_downgrade: assertion failed: (c3_done)

...as a result of the c3 coroutine failing to run to completion.

Message-Id: <[email protected]>

---
 tests/unit/test-coroutine.c | 99 +++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
tests/unit/test-coroutine.c +99 -0
index 6e6f51d480..aa77a3bcb3 100644
@@ -325,6 +325,104 @@ static void test_co_rwlock_upgrade(void)
     g_assert(c2_done);
 }
 
+static void coroutine_fn rwlock_rdlock_yield(void *opaque)
+{
+    qemu_co_rwlock_rdlock(&rwlock);
+    qemu_coroutine_yield();
+
+    qemu_co_rwlock_unlock(&rwlock);
+    qemu_coroutine_yield();
+
+    *(bool *)opaque = true;
+}
+
+static void coroutine_fn rwlock_wrlock_downgrade(void *opaque)
+{
+    qemu_co_rwlock_wrlock(&rwlock);
+
+    qemu_co_rwlock_downgrade(&rwlock);
+    qemu_co_rwlock_unlock(&rwlock);
+    *(bool *)opaque = true;
+}
+
+static void coroutine_fn rwlock_rdlock(void *opaque)
+{
+    qemu_co_rwlock_rdlock(&rwlock);
+
+    qemu_co_rwlock_unlock(&rwlock);
+    *(bool *)opaque = true;
+}
+
+static void coroutine_fn rwlock_wrlock(void *opaque)
+{
+    qemu_co_rwlock_wrlock(&rwlock);
+
+    qemu_co_rwlock_unlock(&rwlock);
+    *(bool *)opaque = true;
+}
+
+/*
+ * Check that downgrading a reader-writer lock does not cause a hang.
+ *
+ * Four coroutines are used to produce a situation where there are
+ * both reader and writer hopefuls waiting to acquire an rwlock that
+ * is held by a reader.
+ *
+ * The correct sequence of operations we aim to provoke can be
+ * represented as:
+ *
+ * | c1     | c2         | c3         | c4         |
+ * |--------+------------+------------+------------|
+ * | rdlock |            |            |            |
+ * | yield  |            |            |            |
+ * |        | wrlock     |            |            |
+ * |        | <queued>   |            |            |
+ * |        |            | rdlock     |            |
+ * |        |            | <queued>   |            |
+ * |        |            |            | wrlock     |
+ * |        |            |            | <queued>   |
+ * | unlock |            |            |            |
+ * | yield  |            |            |            |
+ * |        | <dequeued> |            |            |
+ * |        | downgrade  |            |            |
+ * |        |            | <dequeued> |            |
+ * |        |            | unlock     |            |
+ * |        | ...        |            |            |
+ * |        | unlock     |            |            |
+ * |        |            |            | <dequeued> |
+ * |        |            |            | unlock     |
+ */
+static void test_co_rwlock_downgrade(void)
+{
+    bool c1_done = false;
+    bool c2_done = false;
+    bool c3_done = false;
+    bool c4_done = false;
+    Coroutine *c1, *c2, *c3, *c4;
+
+    qemu_co_rwlock_init(&rwlock);
+
+    c1 = qemu_coroutine_create(rwlock_rdlock_yield, &c1_done);
+    c2 = qemu_coroutine_create(rwlock_wrlock_downgrade, &c2_done);
+    c3 = qemu_coroutine_create(rwlock_rdlock, &c3_done);
+    c4 = qemu_coroutine_create(rwlock_wrlock, &c4_done);
+
+    qemu_coroutine_enter(c1);
+    qemu_coroutine_enter(c2);
+    qemu_coroutine_enter(c3);
+    qemu_coroutine_enter(c4);
+
+    qemu_coroutine_enter(c1);
+
+    g_assert(c2_done);
+    g_assert(c3_done);
+    g_assert(c4_done);
+
+    qemu_coroutine_enter(c1);
+
+    g_assert(c1_done);
+}
+
 /*
  * Check that creation, enter, and return work
  */
@@ -563,6 +661,7 @@ int main(int argc, char **argv)
     g_test_add_func("/locking/co-mutex", test_co_mutex);
     g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable);
     g_test_add_func("/locking/co-rwlock/upgrade", test_co_rwlock_upgrade);
+    g_test_add_func("/locking/co-rwlock/downgrade", test_co_rwlock_downgrade);
     if (g_test_perf()) {
         g_test_add_func("/perf/lifecycle", perf_lifecycle);
         g_test_add_func("/perf/nesting", perf_nesting);
Signature · 1 line
2.29.2
#12 lore raw
In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

This is a resubmit of David Edmondson's series at
https://patchew.org/QEMU/[email protected]/.
After closer analysis on IRC, the CoRwlock's attempt to ensure fairness turned out to be flawed. Therefore, this series reimplements CoRwlock without using a CoQueue. Tracking whether each queued coroutine is a reader/writer makes it possible to never wake a writer when only readers should be allowed and vice versa.

I’m not a maintainer for patches 3 through 6. Stefan is, though. So Stefan, can you take this series with an R-b from me on 1 and 2?

Max

#13 lore raw
In reply to Paolo Bonzini
Quoted 25 lines · Paolo Bonzini

This is a resubmit of David Edmondson's series at
https://patchew.org/QEMU/[email protected]/.
After closer analysis on IRC, the CoRwlock's attempt to ensure fairness turned out to be flawed. Therefore, this series reimplements CoRwlock without using a CoQueue. Tracking whether each queued coroutine is a reader/writer makes it possible to never wake a writer when only readers should be allowed and vice versa.

v2->v3: new CoRwlock implementation

v3->v4: fix upgrade and add a test for that, too

v4->v5: typo

David Edmondson (4):
block/vdi: When writing new bmap entry fails, don't leak the buffer block/vdi: Don't assume that blocks are larger than VdiHeader coroutine/mutex: Store the coroutine in the CoWaitRecord only once
test-coroutine: Add rwlock downgrade test

Paolo Bonzini (2):
coroutine-lock: reimplement CoRwlock to fix downgrade bug
test-coroutine: add rwlock upgrade test

Quoted 5 lines
 block/vdi.c                 |  11 ++-
 include/qemu/coroutine.h    |  17 ++--
 tests/unit/test-coroutine.c | 161 ++++++++++++++++++++++++++++++++++++
 util/qemu-coroutine-lock.c  | 149 +++++++++++++++++++++------------
 4 files changed, 274 insertions(+), 64 deletions(-)

I had questions about the rwlock implementation. The other patches look ready to go.

Stefan

#14 lore raw
Quoted 26 lines · Stefan Hajnoczi
Quoted 25 lines · Paolo Bonzini

This is a resubmit of David Edmondson's series at
https://patchew.org/QEMU/[email protected]/.
After closer analysis on IRC, the CoRwlock's attempt to ensure fairness turned out to be flawed. Therefore, this series reimplements CoRwlock without using a CoQueue. Tracking whether each queued coroutine is a reader/writer makes it possible to never wake a writer when only readers should be allowed and vice versa.

v2->v3: new CoRwlock implementation

v3->v4: fix upgrade and add a test for that, too

v4->v5: typo

David Edmondson (4):
block/vdi: When writing new bmap entry fails, don't leak the buffer block/vdi: Don't assume that blocks are larger than VdiHeader coroutine/mutex: Store the coroutine in the CoWaitRecord only once
test-coroutine: Add rwlock downgrade test

Paolo Bonzini (2):
coroutine-lock: reimplement CoRwlock to fix downgrade bug
test-coroutine: add rwlock upgrade test

Quoted 8 lines
Quoted 5 lines
  block/vdi.c                 |  11 ++-
  include/qemu/coroutine.h    |  17 ++--
  tests/unit/test-coroutine.c | 161 ++++++++++++++++++++++++++++++++++++
  util/qemu-coroutine-lock.c  | 149 +++++++++++++++++++++------------
  4 files changed, 274 insertions(+), 64 deletions(-)

I had questions about the rwlock implementation. The other patches look ready to go.

Cool, none of them seem to be blockers but you raised good points. I'll send v6 tomorrow.

Paolo