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