14f8ab
From 5946a6ec18976c0f52162fe0f47e9b5171af87ec Mon Sep 17 00:00:00 2001
14f8ab
From: Soumya Koduri <skoduri@redhat.com>
14f8ab
Date: Mon, 6 Apr 2020 12:36:44 +0530
14f8ab
Subject: [PATCH 503/511] gfapi: Suspend synctasks instead of blocking them
14f8ab
14f8ab
There are certain conditions which blocks the current
14f8ab
execution thread (like waiting on mutex lock or condition
14f8ab
variable or I/O response). In such cases, if it is a
14f8ab
synctask thread, we should suspend the task instead
14f8ab
of blocking it (like done in SYNCOP using synctask_yield)
14f8ab
14f8ab
This is to avoid deadlock like the one mentioned below -
14f8ab
14f8ab
1) synctaskA sets fs->migration_in_progress to 1 and
14f8ab
   does I/O (LOOKUP)
14f8ab
2) Other synctask threads wait for fs->migration_in_progress
14f8ab
  to be reset to 0 by synctaskA and hence blocked
14f8ab
3) but synctaskA cannot resume as all synctask threads are blocked
14f8ab
   on (2).
14f8ab
14f8ab
Note: this same approach is already used by few other components
14f8ab
like syncbarrier etc.
14f8ab
14f8ab
>Change-Id: If90f870d663bb242c702a5b86ac52eeda67c6f0d
14f8ab
>Fixes: #1146
14f8ab
>Signed-off-by: Soumya Koduri <skoduri@redhat.com>
14f8ab
Upstream patch: https://review.gluster.org/c/glusterfs/+/24276
14f8ab
14f8ab
BUG: 1779238
14f8ab
Change-Id: If90f870d663bb242c702a5b86ac52eeda67c6f0d
14f8ab
Signed-off-by: nik-redhat <nladha@redhat.com>
14f8ab
Reviewed-on: https://code.engineering.redhat.com/gerrit/221081
14f8ab
Tested-by: RHGS Build Bot <nigelb@redhat.com>
14f8ab
Reviewed-by: Soumya Koduri <skoduri@redhat.com>
14f8ab
---
14f8ab
 api/src/glfs-internal.h | 34 ++++++++++++++++++++++++++++++++--
14f8ab
 api/src/glfs-resolve.c  |  9 +++++++++
14f8ab
 api/src/glfs.c          |  9 +++++++++
14f8ab
 3 files changed, 50 insertions(+), 2 deletions(-)
14f8ab
14f8ab
diff --git a/api/src/glfs-internal.h b/api/src/glfs-internal.h
14f8ab
index 55401b2..15cf0ee 100644
14f8ab
--- a/api/src/glfs-internal.h
14f8ab
+++ b/api/src/glfs-internal.h
14f8ab
@@ -16,6 +16,7 @@
14f8ab
 #include <glusterfs/upcall-utils.h>
14f8ab
 #include "glfs-handles.h"
14f8ab
 #include <glusterfs/refcount.h>
14f8ab
+#include <glusterfs/syncop.h>
14f8ab
 
14f8ab
 #define GLFS_SYMLINK_MAX_FOLLOW 2048
14f8ab
 
14f8ab
@@ -207,6 +208,7 @@ struct glfs {
14f8ab
     glfs_upcall_cbk up_cbk; /* upcall cbk function to be registered */
14f8ab
     void *up_data;          /* Opaque data provided by application
14f8ab
                              * during upcall registration */
14f8ab
+    struct list_head waitq; /* waiting synctasks */
14f8ab
 };
14f8ab
 
14f8ab
 /* This enum is used to maintain the state of glfd. In case of async fops
14f8ab
@@ -442,6 +444,34 @@ glfs_process_upcall_event(struct glfs *fs, void *data)
14f8ab
         THIS = glfd->fd->inode->table->xl->ctx->master;                        \
14f8ab
     } while (0)
14f8ab
 
14f8ab
+#define __GLFS_LOCK_WAIT(fs)                                                   \
14f8ab
+    do {                                                                       \
14f8ab
+        struct synctask *task = NULL;                                          \
14f8ab
+                                                                               \
14f8ab
+        task = synctask_get();                                                 \
14f8ab
+                                                                               \
14f8ab
+        if (task) {                                                            \
14f8ab
+            list_add_tail(&task->waitq, &fs->waitq);                           \
14f8ab
+            pthread_mutex_unlock(&fs->mutex);                                  \
14f8ab
+            synctask_yield(task, NULL);                                              \
14f8ab
+            pthread_mutex_lock(&fs->mutex);                                    \
14f8ab
+        } else {                                                               \
14f8ab
+            /* non-synctask */                                                 \
14f8ab
+            pthread_cond_wait(&fs->cond, &fs->mutex);                          \
14f8ab
+        }                                                                      \
14f8ab
+    } while (0)
14f8ab
+
14f8ab
+#define __GLFS_SYNCTASK_WAKE(fs)                                               \
14f8ab
+    do {                                                                       \
14f8ab
+        struct synctask *waittask = NULL;                                      \
14f8ab
+                                                                               \
14f8ab
+        while (!list_empty(&fs->waitq)) {                                      \
14f8ab
+            waittask = list_entry(fs->waitq.next, struct synctask, waitq);     \
14f8ab
+            list_del_init(&waittask->waitq);                                   \
14f8ab
+            synctask_wake(waittask);                                           \
14f8ab
+        }                                                                      \
14f8ab
+    } while (0)
14f8ab
+
14f8ab
 /*
14f8ab
   By default all lock attempts from user context must
14f8ab
   use glfs_lock() and glfs_unlock(). This allows
14f8ab
@@ -466,10 +496,10 @@ glfs_lock(struct glfs *fs, gf_boolean_t wait_for_migration)
14f8ab
     pthread_mutex_lock(&fs->mutex);
14f8ab
 
14f8ab
     while (!fs->init)
14f8ab
-        pthread_cond_wait(&fs->cond, &fs->mutex);
14f8ab
+        __GLFS_LOCK_WAIT(fs);
14f8ab
 
14f8ab
     while (wait_for_migration && fs->migration_in_progress)
14f8ab
-        pthread_cond_wait(&fs->cond, &fs->mutex);
14f8ab
+        __GLFS_LOCK_WAIT(fs);
14f8ab
 
14f8ab
     return 0;
14f8ab
 }
14f8ab
diff --git a/api/src/glfs-resolve.c b/api/src/glfs-resolve.c
14f8ab
index 062b7dc..58b6ace 100644
14f8ab
--- a/api/src/glfs-resolve.c
14f8ab
+++ b/api/src/glfs-resolve.c
14f8ab
@@ -65,6 +65,9 @@ __glfs_first_lookup(struct glfs *fs, xlator_t *subvol)
14f8ab
     fs->migration_in_progress = 0;
14f8ab
     pthread_cond_broadcast(&fs->cond);
14f8ab
 
14f8ab
+    /* wake up other waiting tasks */
14f8ab
+    __GLFS_SYNCTASK_WAKE(fs);
14f8ab
+
14f8ab
     return ret;
14f8ab
 }
14f8ab
 
14f8ab
@@ -154,6 +157,9 @@ __glfs_refresh_inode(struct glfs *fs, xlator_t *subvol, inode_t *inode,
14f8ab
     fs->migration_in_progress = 0;
14f8ab
     pthread_cond_broadcast(&fs->cond);
14f8ab
 
14f8ab
+    /* wake up other waiting tasks */
14f8ab
+    __GLFS_SYNCTASK_WAKE(fs);
14f8ab
+
14f8ab
     return newinode;
14f8ab
 }
14f8ab
 
14f8ab
@@ -841,6 +847,9 @@ __glfs_migrate_fd(struct glfs *fs, xlator_t *newsubvol, struct glfs_fd *glfd)
14f8ab
     fs->migration_in_progress = 0;
14f8ab
     pthread_cond_broadcast(&fs->cond);
14f8ab
 
14f8ab
+    /* wake up other waiting tasks */
14f8ab
+    __GLFS_SYNCTASK_WAKE(fs);
14f8ab
+
14f8ab
     return newfd;
14f8ab
 }
14f8ab
 
14f8ab
diff --git a/api/src/glfs.c b/api/src/glfs.c
14f8ab
index f36616d..ae994fa 100644
14f8ab
--- a/api/src/glfs.c
14f8ab
+++ b/api/src/glfs.c
14f8ab
@@ -740,6 +740,7 @@ glfs_new_fs(const char *volname)
14f8ab
 
14f8ab
     INIT_LIST_HEAD(&fs->openfds);
14f8ab
     INIT_LIST_HEAD(&fs->upcall_list);
14f8ab
+    INIT_LIST_HEAD(&fs->waitq);
14f8ab
 
14f8ab
     PTHREAD_MUTEX_INIT(&fs->mutex, NULL, fs->pthread_flags, GLFS_INIT_MUTEX,
14f8ab
                        err);
14f8ab
@@ -1228,6 +1229,7 @@ pub_glfs_fini(struct glfs *fs)
14f8ab
     call_pool_t *call_pool = NULL;
14f8ab
     int fs_init = 0;
14f8ab
     int err = -1;
14f8ab
+    struct synctask *waittask = NULL;
14f8ab
 
14f8ab
     DECLARE_OLD_THIS;
14f8ab
 
14f8ab
@@ -1249,6 +1251,13 @@ pub_glfs_fini(struct glfs *fs)
14f8ab
 
14f8ab
     call_pool = fs->ctx->pool;
14f8ab
 
14f8ab
+    /* Wake up any suspended synctasks */
14f8ab
+    while (!list_empty(&fs->waitq)) {
14f8ab
+        waittask = list_entry(fs->waitq.next, struct synctask, waitq);
14f8ab
+        list_del_init(&waittask->waitq);
14f8ab
+        synctask_wake(waittask);
14f8ab
+    }
14f8ab
+
14f8ab
     while (countdown--) {
14f8ab
         /* give some time for background frames to finish */
14f8ab
         pthread_mutex_lock(&fs->mutex);
14f8ab
-- 
14f8ab
1.8.3.1
14f8ab