233933
From ea7f11b989896d76b8d091d26bc0241bce9413f8 Mon Sep 17 00:00:00 2001
233933
From: Xavi Hernandez <xhernandez@redhat.com>
233933
Date: Thu, 4 Jul 2019 13:21:33 +0200
233933
Subject: [PATCH 253/255] core: fix deadlock between statedump and
233933
 fd_anonymous()
233933
233933
There exists a deadlock between statedump generation and fd_anonymous()
233933
function because they are acquiring inode table lock and inode lock in
233933
reverse order.
233933
233933
This patch modifies fd_anonymous() so that it takes inode lock only when
233933
it's really necessary, avoiding the deadlock.
233933
233933
Upstream patch:
233933
> Change-Id: I24355447f0ea1b39e2546782ad07f0512cc381e7
233933
> Upstream patch link: https://review.gluster.org/c/glusterfs/+/22995
233933
> BUG: 1727068
233933
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
233933
233933
Change-Id: I24355447f0ea1b39e2546782ad07f0512cc381e7
233933
Fixes: bz#1722209
233933
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
233933
Reviewed-on: https://code.engineering.redhat.com/gerrit/176096
233933
Tested-by: RHGS Build Bot <nigelb@redhat.com>
233933
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
233933
---
233933
 libglusterfs/src/fd.c | 137 ++++++++++++++++++++++----------------------------
233933
 1 file changed, 61 insertions(+), 76 deletions(-)
233933
233933
diff --git a/libglusterfs/src/fd.c b/libglusterfs/src/fd.c
233933
index b8aac72..314546a 100644
233933
--- a/libglusterfs/src/fd.c
233933
+++ b/libglusterfs/src/fd.c
233933
@@ -532,7 +532,7 @@ fd_unref(fd_t *fd)
233933
     return;
233933
 }
233933
 
233933
-fd_t *
233933
+static fd_t *
233933
 __fd_bind(fd_t *fd)
233933
 {
233933
     list_del_init(&fd->inode_list);
233933
@@ -562,9 +562,9 @@ fd_bind(fd_t *fd)
233933
 }
233933
 
233933
 static fd_t *
233933
-__fd_create(inode_t *inode, uint64_t pid)
233933
+fd_allocate(inode_t *inode, uint64_t pid)
233933
 {
233933
-    fd_t *fd = NULL;
233933
+    fd_t *fd;
233933
 
233933
     if (inode == NULL) {
233933
         gf_msg_callingfn("fd", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG,
233933
@@ -573,64 +573,67 @@ __fd_create(inode_t *inode, uint64_t pid)
233933
     }
233933
 
233933
     fd = mem_get0(inode->table->fd_mem_pool);
233933
-    if (!fd)
233933
-        goto out;
233933
+    if (fd == NULL) {
233933
+        return NULL;
233933
+    }
233933
 
233933
     fd->xl_count = inode->table->xl->graph->xl_count + 1;
233933
 
233933
     fd->_ctx = GF_CALLOC(1, (sizeof(struct _fd_ctx) * fd->xl_count),
233933
                          gf_common_mt_fd_ctx);
233933
-    if (!fd->_ctx)
233933
-        goto free_fd;
233933
+    if (fd->_ctx == NULL) {
233933
+        goto failed;
233933
+    }
233933
 
233933
     fd->lk_ctx = fd_lk_ctx_create();
233933
-    if (!fd->lk_ctx)
233933
-        goto free_fd_ctx;
233933
-
233933
-    fd->inode = inode_ref(inode);
233933
-    fd->pid = pid;
233933
-    INIT_LIST_HEAD(&fd->inode_list);
233933
-
233933
-    LOCK_INIT(&fd->lock);
233933
-out:
233933
-    return fd;
233933
+    if (fd->lk_ctx != NULL) {
233933
+        /* We need to take a reference from the inode, but we cannot do it
233933
+         * here because this function can be called with the inode lock taken
233933
+         * and inode_ref() takes the inode's table lock. This is the reverse
233933
+         * of the logical lock acquisition order and can cause a deadlock. So
233933
+         * we simply assign the inode here and we delefate the inode reference
233933
+         * responsibility to the caller (when this function succeeds and the
233933
+         * inode lock is released). This is safe because the caller must hold
233933
+         * a reference of the inode to use it, so it's guaranteed that the
233933
+         * number of references won't reach 0 before the caller finishes.
233933
+         *
233933
+         * TODO: minimize use of locks in favor of atomic operations to avoid
233933
+         *       these dependencies. */
233933
+        fd->inode = inode;
233933
+        fd->pid = pid;
233933
+        INIT_LIST_HEAD(&fd->inode_list);
233933
+        LOCK_INIT(&fd->lock);
233933
+        GF_ATOMIC_INIT(fd->refcount, 1);
233933
+        return fd;
233933
+    }
233933
 
233933
-free_fd_ctx:
233933
     GF_FREE(fd->_ctx);
233933
-free_fd:
233933
+
233933
+failed:
233933
     mem_put(fd);
233933
 
233933
     return NULL;
233933
 }
233933
 
233933
 fd_t *
233933
-fd_create(inode_t *inode, pid_t pid)
233933
+fd_create_uint64(inode_t *inode, uint64_t pid)
233933
 {
233933
-    fd_t *fd = NULL;
233933
-
233933
-    fd = __fd_create(inode, (uint64_t)pid);
233933
-    if (!fd)
233933
-        goto out;
233933
+    fd_t *fd;
233933
 
233933
-    fd = fd_ref(fd);
233933
+    fd = fd_allocate(inode, pid);
233933
+    if (fd != NULL) {
233933
+        /* fd_allocate() doesn't get a reference from the inode. We need to
233933
+         * take it here in case of success. */
233933
+        inode_ref(inode);
233933
+    }
233933
 
233933
-out:
233933
     return fd;
233933
 }
233933
 
233933
 fd_t *
233933
-fd_create_uint64(inode_t *inode, uint64_t pid)
233933
+fd_create(inode_t *inode, pid_t pid)
233933
 {
233933
-    fd_t *fd = NULL;
233933
-
233933
-    fd = __fd_create(inode, pid);
233933
-    if (!fd)
233933
-        goto out;
233933
-
233933
-    fd = fd_ref(fd);
233933
-
233933
-out:
233933
-    return fd;
233933
+    return fd_create_uint64(inode, (uint64_t)pid);
233933
 }
233933
 
233933
 static fd_t *
233933
@@ -719,10 +722,13 @@ __fd_lookup_anonymous(inode_t *inode, int32_t flags)
233933
     return fd;
233933
 }
233933
 
233933
-static fd_t *
233933
-__fd_anonymous(inode_t *inode, int32_t flags)
233933
+fd_t *
233933
+fd_anonymous_with_flags(inode_t *inode, int32_t flags)
233933
 {
233933
     fd_t *fd = NULL;
233933
+    bool ref = false;
233933
+
233933
+    LOCK(&inode->lock);
233933
 
233933
     fd = __fd_lookup_anonymous(inode, flags);
233933
 
233933
@@ -730,54 +736,33 @@ __fd_anonymous(inode_t *inode, int32_t flags)
233933
        __fd_lookup_anonymous(), so no need of one more fd_ref().
233933
        if (!fd); then both create and bind won't bump up the ref
233933
        count, so we have to call fd_ref() after bind. */
233933
-    if (!fd) {
233933
-        fd = __fd_create(inode, 0);
233933
-
233933
-        if (!fd)
233933
-            return NULL;
233933
-
233933
-        fd->anonymous = _gf_true;
233933
-        fd->flags = GF_ANON_FD_FLAGS | flags;
233933
+    if (fd == NULL) {
233933
+        fd = fd_allocate(inode, 0);
233933
+        if (fd != NULL) {
233933
+            fd->anonymous = _gf_true;
233933
+            fd->flags = GF_ANON_FD_FLAGS | (flags & O_DIRECT);
233933
 
233933
-        __fd_bind(fd);
233933
+            __fd_bind(fd);
233933
 
233933
-        __fd_ref(fd);
233933
+            ref = true;
233933
+        }
233933
     }
233933
 
233933
-    return fd;
233933
-}
233933
-
233933
-fd_t *
233933
-fd_anonymous(inode_t *inode)
233933
-{
233933
-    fd_t *fd = NULL;
233933
+    UNLOCK(&inode->lock);
233933
 
233933
-    LOCK(&inode->lock);
233933
-    {
233933
-        fd = __fd_anonymous(inode, GF_ANON_FD_FLAGS);
233933
+    if (ref) {
233933
+        /* fd_allocate() doesn't get a reference from the inode. We need to
233933
+         * take it here in case of success. */
233933
+        inode_ref(inode);
233933
     }
233933
-    UNLOCK(&inode->lock);
233933
 
233933
     return fd;
233933
 }
233933
 
233933
 fd_t *
233933
-fd_anonymous_with_flags(inode_t *inode, int32_t flags)
233933
+fd_anonymous(inode_t *inode)
233933
 {
233933
-    fd_t *fd = NULL;
233933
-
233933
-    if (flags & O_DIRECT)
233933
-        flags = GF_ANON_FD_FLAGS | O_DIRECT;
233933
-    else
233933
-        flags = GF_ANON_FD_FLAGS;
233933
-
233933
-    LOCK(&inode->lock);
233933
-    {
233933
-        fd = __fd_anonymous(inode, flags);
233933
-    }
233933
-    UNLOCK(&inode->lock);
233933
-
233933
-    return fd;
233933
+    return fd_anonymous_with_flags(inode, 0);
233933
 }
233933
 
233933
 fd_t *
233933
-- 
233933
1.8.3.1
233933