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