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