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