cb8e9e
From 92952a767ffe126e1dfa548be003ce2b0461065e Mon Sep 17 00:00:00 2001
cb8e9e
From: Pranith Kumar K <pkarampu@redhat.com>
cb8e9e
Date: Sat, 18 Jul 2015 10:04:13 +0530
cb8e9e
Subject: [PATCH 245/245] Revert "timer: fix race between gf_timer_call_cancel() and gf_timer_proc()"
cb8e9e
cb8e9e
This reverts commit 9b237463a8f5c75cff66364d07278217f8e4e586.
cb8e9e
cb8e9e
BUG: 1244187
cb8e9e
Change-Id: Ibb6c04ab6167071e62c7a33e114b3dd5931825c8
cb8e9e
Signed-off-by: Pranith Kumar K <pkarampu@redhat.com>
cb8e9e
Reviewed-on: https://code.engineering.redhat.com/gerrit/53266
cb8e9e
Reviewed-by: Krishnan Parthasarathi <kparthas@redhat.com>
cb8e9e
Reviewed-by: Kaushal Madappa <kaushal@redhat.com>
cb8e9e
---
cb8e9e
 libglusterfs/src/timer.c |   48 +++++++++++++++++++++++++++++++--------------
cb8e9e
 libglusterfs/src/timer.h |    2 +-
cb8e9e
 2 files changed, 34 insertions(+), 16 deletions(-)
cb8e9e
cb8e9e
diff --git a/libglusterfs/src/timer.c b/libglusterfs/src/timer.c
cb8e9e
index a4d1890..e103f05 100644
cb8e9e
--- a/libglusterfs/src/timer.c
cb8e9e
+++ b/libglusterfs/src/timer.c
cb8e9e
@@ -85,11 +85,30 @@ gf_timer_call_after (glusterfs_ctx_t *ctx,
cb8e9e
 }
cb8e9e
 
cb8e9e
 int32_t
cb8e9e
+gf_timer_call_stale (gf_timer_registry_t *reg,
cb8e9e
+                     gf_timer_t *event)
cb8e9e
+{
cb8e9e
+        if (reg == NULL || event == NULL) {
cb8e9e
+                gf_msg_callingfn ("timer", GF_LOG_ERROR, EINVAL,
cb8e9e
+                                  LG_MSG_INVALID_ARG, "invalid argument");
cb8e9e
+                return 0;
cb8e9e
+        }
cb8e9e
+
cb8e9e
+        event->next->prev = event->prev;
cb8e9e
+        event->prev->next = event->next;
cb8e9e
+        event->next = &reg->stale;
cb8e9e
+        event->prev = event->next->prev;
cb8e9e
+        event->next->prev = event;
cb8e9e
+        event->prev->next = event;
cb8e9e
+
cb8e9e
+        return 0;
cb8e9e
+}
cb8e9e
+
cb8e9e
+int32_t
cb8e9e
 gf_timer_call_cancel (glusterfs_ctx_t *ctx,
cb8e9e
                       gf_timer_t *event)
cb8e9e
 {
cb8e9e
         gf_timer_registry_t *reg = NULL;
cb8e9e
-        gf_boolean_t fired = _gf_false;
cb8e9e
 
cb8e9e
         if (ctx == NULL || event == NULL)
cb8e9e
         {
cb8e9e
@@ -108,21 +127,13 @@ gf_timer_call_cancel (glusterfs_ctx_t *ctx,
cb8e9e
 
cb8e9e
         pthread_mutex_lock (&reg->lock);
cb8e9e
         {
cb8e9e
-		fired = event->fired;
cb8e9e
-		if (fired)
cb8e9e
-			goto unlock;
cb8e9e
-
cb8e9e
                 event->next->prev = event->prev;
cb8e9e
                 event->prev->next = event->next;
cb8e9e
         }
cb8e9e
-unlock:
cb8e9e
         pthread_mutex_unlock (&reg->lock);
cb8e9e
 
cb8e9e
-	if (!fired) {
cb8e9e
-		GF_FREE (event);
cb8e9e
-		return 0;
cb8e9e
-        }
cb8e9e
-        return -1;
cb8e9e
+        GF_FREE (event);
cb8e9e
+        return 0;
cb8e9e
 }
cb8e9e
 
cb8e9e
 static inline void __delete_entry (gf_timer_t *event) {
cb8e9e
@@ -169,9 +180,7 @@ gf_timer_proc (void *ctx)
cb8e9e
                                 at = TS (event->at);
cb8e9e
                                 if (event != &reg->active && now >= at) {
cb8e9e
                                         need_cbk = 1;
cb8e9e
-                                        event->next->prev = event->prev;
cb8e9e
-                                        event->prev->next = event->next;
cb8e9e
-                                        event->fired = 1;
cb8e9e
+                                        gf_timer_call_stale (reg, event);
cb8e9e
                                 }
cb8e9e
                         }
cb8e9e
                         pthread_mutex_unlock (&reg->lock);
cb8e9e
@@ -182,7 +191,9 @@ gf_timer_proc (void *ctx)
cb8e9e
                                         THIS = event->xl;
cb8e9e
                                 }
cb8e9e
                                 event->callbk (event->data);
cb8e9e
-                                GF_FREE (event);
cb8e9e
+                                /*This callbk above would have freed the event
cb8e9e
+                                 * by calling timer_cancel, don't ever touch it
cb8e9e
+                                 * again*/
cb8e9e
                                 if (old_THIS) {
cb8e9e
                                         THIS = old_THIS;
cb8e9e
                                 }
cb8e9e
@@ -204,6 +215,11 @@ gf_timer_proc (void *ctx)
cb8e9e
                          * list_head*/
cb8e9e
                         __delete_entry (event);
cb8e9e
                 }
cb8e9e
+
cb8e9e
+                while (reg->stale.next != &reg->stale) {
cb8e9e
+                        event = reg->stale.next;
cb8e9e
+                        __delete_entry (event);
cb8e9e
+                }
cb8e9e
         }
cb8e9e
         pthread_mutex_unlock (&reg->lock);
cb8e9e
         pthread_mutex_destroy (&reg->lock);
cb8e9e
@@ -232,6 +248,8 @@ gf_timer_registry_init (glusterfs_ctx_t *ctx)
cb8e9e
                 pthread_mutex_init (&reg->lock, NULL);
cb8e9e
                 reg->active.next = &reg->active;
cb8e9e
                 reg->active.prev = &reg->active;
cb8e9e
+                reg->stale.next = &reg->stale;
cb8e9e
+                reg->stale.prev = &reg->stale;
cb8e9e
 
cb8e9e
                 ctx->timer = reg;
cb8e9e
                 gf_thread_create (&reg->th, NULL, gf_timer_proc, ctx);
cb8e9e
diff --git a/libglusterfs/src/timer.h b/libglusterfs/src/timer.h
cb8e9e
index 35d99be..e64b350 100644
cb8e9e
--- a/libglusterfs/src/timer.h
cb8e9e
+++ b/libglusterfs/src/timer.h
cb8e9e
@@ -29,12 +29,12 @@ struct _gf_timer {
cb8e9e
         gf_timer_cbk_t    callbk;
cb8e9e
         void             *data;
cb8e9e
         xlator_t         *xl;
cb8e9e
-	gf_boolean_t      fired;
cb8e9e
 };
cb8e9e
 
cb8e9e
 struct _gf_timer_registry {
cb8e9e
         pthread_t        th;
cb8e9e
         char             fin;
cb8e9e
+        struct _gf_timer stale;
cb8e9e
         struct _gf_timer active;
cb8e9e
         pthread_mutex_t  lock;
cb8e9e
 };
cb8e9e
-- 
cb8e9e
1.7.1
cb8e9e