14f8ab
From 11b64d494c52004002f900888694d20ef8af6df6 Mon Sep 17 00:00:00 2001
14f8ab
From: Mohammed Rafi KC <rkavunga@redhat.com>
14f8ab
Date: Sat, 11 May 2019 22:40:22 +0530
14f8ab
Subject: [PATCH 158/169] glusterfsd/cleanup: Protect graph object under a lock
14f8ab
14f8ab
While processing a cleanup_and_exit function, we are
14f8ab
accessing a graph object. But this has not been protected
14f8ab
under a lock. Because a parallel cleanup of a graph is quite
14f8ab
possible which might lead to an invalid memory access
14f8ab
14f8ab
Upstream patch:https://review.gluster.org/#/c/glusterfs/+/22709/
14f8ab
14f8ab
>Change-Id: Id05ca70d5b57e172b0401d07b6a1f5386c044e79
14f8ab
>fixes: bz#1708926
14f8ab
>Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
14f8ab
14f8ab
Change-Id: I55ab0525c79baa99a3bd929ee979c5519be5ab21
14f8ab
BUG: 1716626
14f8ab
Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
14f8ab
Reviewed-on: https://code.engineering.redhat.com/gerrit/172283
14f8ab
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
14f8ab
Tested-by: RHGS Build Bot <nigelb@redhat.com>
14f8ab
---
14f8ab
 libglusterfs/src/graph.c                        | 58 +++++++++++++++----------
14f8ab
 libglusterfs/src/statedump.c                    | 16 +++++--
14f8ab
 tests/bugs/glusterd/optimized-basic-testcases.t |  4 +-
14f8ab
 3 files changed, 50 insertions(+), 28 deletions(-)
14f8ab
14f8ab
diff --git a/libglusterfs/src/graph.c b/libglusterfs/src/graph.c
14f8ab
index 4c8b02d..18fb2d9 100644
14f8ab
--- a/libglusterfs/src/graph.c
14f8ab
+++ b/libglusterfs/src/graph.c
14f8ab
@@ -1392,8 +1392,12 @@ glusterfs_graph_cleanup(void *arg)
14f8ab
     }
14f8ab
     pthread_mutex_unlock(&ctx->notify_lock);
14f8ab
 
14f8ab
-    glusterfs_graph_fini(graph);
14f8ab
-    glusterfs_graph_destroy(graph);
14f8ab
+    pthread_mutex_lock(&ctx->cleanup_lock);
14f8ab
+    {
14f8ab
+        glusterfs_graph_fini(graph);
14f8ab
+        glusterfs_graph_destroy(graph);
14f8ab
+    }
14f8ab
+    pthread_mutex_unlock(&ctx->cleanup_lock);
14f8ab
 out:
14f8ab
     return NULL;
14f8ab
 }
14f8ab
@@ -1468,31 +1472,37 @@ glusterfs_process_svc_detach(glusterfs_ctx_t *ctx, gf_volfile_t *volfile_obj)
14f8ab
 
14f8ab
     if (!ctx || !ctx->active || !volfile_obj)
14f8ab
         goto out;
14f8ab
-    parent_graph = ctx->active;
14f8ab
-    graph = volfile_obj->graph;
14f8ab
-    if (!graph)
14f8ab
-        goto out;
14f8ab
-    if (graph->first)
14f8ab
-        xl = graph->first;
14f8ab
 
14f8ab
-    last_xl = graph->last_xl;
14f8ab
-    if (last_xl)
14f8ab
-        last_xl->next = NULL;
14f8ab
-    if (!xl || xl->cleanup_starting)
14f8ab
-        goto out;
14f8ab
+    pthread_mutex_lock(&ctx->cleanup_lock);
14f8ab
+    {
14f8ab
+        parent_graph = ctx->active;
14f8ab
+        graph = volfile_obj->graph;
14f8ab
+        if (!graph)
14f8ab
+            goto unlock;
14f8ab
+        if (graph->first)
14f8ab
+            xl = graph->first;
14f8ab
+
14f8ab
+        last_xl = graph->last_xl;
14f8ab
+        if (last_xl)
14f8ab
+            last_xl->next = NULL;
14f8ab
+        if (!xl || xl->cleanup_starting)
14f8ab
+            goto unlock;
14f8ab
 
14f8ab
-    xl->cleanup_starting = 1;
14f8ab
-    gf_msg("mgmt", GF_LOG_INFO, 0, LG_MSG_GRAPH_DETACH_STARTED,
14f8ab
-           "detaching child %s", volfile_obj->vol_id);
14f8ab
+        xl->cleanup_starting = 1;
14f8ab
+        gf_msg("mgmt", GF_LOG_INFO, 0, LG_MSG_GRAPH_DETACH_STARTED,
14f8ab
+               "detaching child %s", volfile_obj->vol_id);
14f8ab
 
14f8ab
-    list_del_init(&volfile_obj->volfile_list);
14f8ab
-    glusterfs_mux_xlator_unlink(parent_graph->top, xl);
14f8ab
-    parent_graph->last_xl = glusterfs_get_last_xlator(parent_graph);
14f8ab
-    parent_graph->xl_count -= graph->xl_count;
14f8ab
-    parent_graph->leaf_count -= graph->leaf_count;
14f8ab
-    default_notify(xl, GF_EVENT_PARENT_DOWN, xl);
14f8ab
-    parent_graph->id++;
14f8ab
-    ret = 0;
14f8ab
+        list_del_init(&volfile_obj->volfile_list);
14f8ab
+        glusterfs_mux_xlator_unlink(parent_graph->top, xl);
14f8ab
+        parent_graph->last_xl = glusterfs_get_last_xlator(parent_graph);
14f8ab
+        parent_graph->xl_count -= graph->xl_count;
14f8ab
+        parent_graph->leaf_count -= graph->leaf_count;
14f8ab
+        default_notify(xl, GF_EVENT_PARENT_DOWN, xl);
14f8ab
+        parent_graph->id++;
14f8ab
+        ret = 0;
14f8ab
+    }
14f8ab
+unlock:
14f8ab
+    pthread_mutex_unlock(&ctx->cleanup_lock);
14f8ab
 out:
14f8ab
     if (!ret) {
14f8ab
         list_del_init(&volfile_obj->volfile_list);
14f8ab
diff --git a/libglusterfs/src/statedump.c b/libglusterfs/src/statedump.c
14f8ab
index 0cf80c0..0d58f8f 100644
14f8ab
--- a/libglusterfs/src/statedump.c
14f8ab
+++ b/libglusterfs/src/statedump.c
14f8ab
@@ -805,11 +805,17 @@ gf_proc_dump_info(int signum, glusterfs_ctx_t *ctx)
14f8ab
     int brick_count = 0;
14f8ab
     int len = 0;
14f8ab
 
14f8ab
-    gf_proc_dump_lock();
14f8ab
-
14f8ab
     if (!ctx)
14f8ab
         goto out;
14f8ab
 
14f8ab
+    /*
14f8ab
+     * Multiplexed daemons can change the active graph when attach/detach
14f8ab
+     * is called. So this has to be protected with the cleanup lock.
14f8ab
+     */
14f8ab
+    if (mgmt_is_multiplexed_daemon(ctx->cmd_args.process_name))
14f8ab
+        pthread_mutex_lock(&ctx->cleanup_lock);
14f8ab
+    gf_proc_dump_lock();
14f8ab
+
14f8ab
     if (!mgmt_is_multiplexed_daemon(ctx->cmd_args.process_name) &&
14f8ab
         (ctx && ctx->active)) {
14f8ab
         top = ctx->active->first;
14f8ab
@@ -923,7 +929,11 @@ gf_proc_dump_info(int signum, glusterfs_ctx_t *ctx)
14f8ab
 out:
14f8ab
     GF_FREE(dump_options.dump_path);
14f8ab
     dump_options.dump_path = NULL;
14f8ab
-    gf_proc_dump_unlock();
14f8ab
+    if (ctx) {
14f8ab
+        gf_proc_dump_unlock();
14f8ab
+        if (mgmt_is_multiplexed_daemon(ctx->cmd_args.process_name))
14f8ab
+            pthread_mutex_unlock(&ctx->cleanup_lock);
14f8ab
+    }
14f8ab
 
14f8ab
     return;
14f8ab
 }
14f8ab
diff --git a/tests/bugs/glusterd/optimized-basic-testcases.t b/tests/bugs/glusterd/optimized-basic-testcases.t
14f8ab
index d700b5e..110f1b9 100644
14f8ab
--- a/tests/bugs/glusterd/optimized-basic-testcases.t
14f8ab
+++ b/tests/bugs/glusterd/optimized-basic-testcases.t
14f8ab
@@ -289,7 +289,9 @@ mkdir -p /xyz/var/lib/glusterd/abc
14f8ab
 TEST  $CLI volume create "test" $H0:/xyz/var/lib/glusterd/abc
14f8ab
 EXPECT 'Created' volinfo_field "test" 'Status';
14f8ab
 
14f8ab
-EXPECT "1" generate_statedump_and_check_for_glusterd_info
14f8ab
+#While taking a statedump, there is a TRY_LOCK on call_frame, which might may cause
14f8ab
+#failure. So Adding a EXPECT_WITHIN
14f8ab
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "^1$" generate_statedump_and_check_for_glusterd_info
14f8ab
 
14f8ab
 cleanup_statedump `pidof glusterd`
14f8ab
 cleanup
14f8ab
-- 
14f8ab
1.8.3.1
14f8ab