zbd: Fix potential deadlock on read operations
authorDamien Le Moal <damien.lemoal@wdc.com>
Fri, 8 May 2020 07:56:40 +0000 (16:56 +0900)
committerJens Axboe <axboe@kernel.dk>
Fri, 15 May 2020 13:41:34 +0000 (07:41 -0600)
For read-only workloads, zbd_find_zone() has a similar zone locking
behavior as for write IOs: zones to be read are locked when an IO is
prepared and unlocked when the IO completes. With an asynchronous IO
engine, this can create deadlocks if 2 threads are trying to read the
same 2 zones. For instance, if thread A already has a lock on zone 1
and is waiting for a lock on zone 2 while thread B already has a lock
on zone 2 and waiting for a lock on zone 1.

The fix is similar to previous fixes for this potential deadlock,
namely, use zone_lock() instead of directly calling pthread_mutex_lock()
to ensure that a thread issues the IOs it already has prepared if it
encounters a locked zone, doing so ensuring forward progress.

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
zbd.c

diff --git a/zbd.c b/zbd.c
index 8dc3c39792931803b07d714ec222b1807c353102..5aaf1e2cf19c7e9645c56f1585bd47fc34b1f147 100644 (file)
--- a/zbd.c
+++ b/zbd.c
@@ -1141,7 +1141,7 @@ zbd_find_zone(struct thread_data *td, struct io_u *io_u,
         */
        for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
                if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
-                       pthread_mutex_lock(&z1->mutex);
+                       zone_lock(td, z1);
                        if (z1->start + min_bs <= z1->wp)
                                return z1;
                        pthread_mutex_unlock(&z1->mutex);
@@ -1150,7 +1150,7 @@ zbd_find_zone(struct thread_data *td, struct io_u *io_u,
                }
                if (td_random(td) && z2 >= zf &&
                    z2->cond != ZBD_ZONE_COND_OFFLINE) {
-                       pthread_mutex_lock(&z2->mutex);
+                       zone_lock(td, z2);
                        if (z2->start + min_bs <= z2->wp)
                                return z2;
                        pthread_mutex_unlock(&z2->mutex);