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