17b94a
From f199094cb61341a47c98a8ed91b293446182b5a9 Mon Sep 17 00:00:00 2001
17b94a
From: Mohit Agrawal <moagrawal@redhat.com>
17b94a
Date: Thu, 3 Oct 2019 14:06:52 +0530
17b94a
Subject: [PATCH 333/335] rpc: Synchronize slot allocation code
17b94a
17b94a
Problem: Current slot allocation/deallocation code path is not
17b94a
         synchronized.There are scenario when due to race condition
17b94a
         in slot allocation/deallocation code path brick is crashed.
17b94a
17b94a
Solution: Synchronize slot allocation/deallocation code path to
17b94a
          avoid the issue
17b94a
17b94a
> Change-Id: I4fb659a75234218ffa0e5e0bf9308f669f75fc25
17b94a
> Fixes: bz#1763036
17b94a
> Signed-off-by: Mohit Agrawal <moagrawal@redhat.com>
17b94a
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/23508/)
17b94a
> (Cherry pick from commit faf5ac13c4ee00a05e9451bf8da3be2a9043bbf2)
17b94a
17b94a
Change-Id: I4fb659a75234218ffa0e5e0bf9308f669f75fc25
17b94a
BUG: 1741193
17b94a
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
17b94a
Reviewed-on: https://code.engineering.redhat.com/gerrit/185827
17b94a
Tested-by: RHGS Build Bot <nigelb@redhat.com>
17b94a
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
17b94a
---
17b94a
 libglusterfs/src/event-epoll.c | 74 +++++++++++++++++++++++-------------------
17b94a
 1 file changed, 41 insertions(+), 33 deletions(-)
17b94a
17b94a
diff --git a/libglusterfs/src/event-epoll.c b/libglusterfs/src/event-epoll.c
17b94a
index 0cec47e..65f5efd 100644
17b94a
--- a/libglusterfs/src/event-epoll.c
17b94a
+++ b/libglusterfs/src/event-epoll.c
17b94a
@@ -69,15 +69,27 @@ __event_newtable(struct event_pool *event_pool, int table_idx)
17b94a
 }
17b94a
 
17b94a
 static int
17b94a
+event_slot_ref(struct event_slot_epoll *slot)
17b94a
+{
17b94a
+    if (!slot)
17b94a
+        return -1;
17b94a
+
17b94a
+    return GF_ATOMIC_INC(slot->ref);
17b94a
+}
17b94a
+
17b94a
+static int
17b94a
 __event_slot_alloc(struct event_pool *event_pool, int fd,
17b94a
-                   char notify_poller_death)
17b94a
+                   char notify_poller_death, struct event_slot_epoll **slot)
17b94a
 {
17b94a
     int i = 0;
17b94a
+    int j = 0;
17b94a
     int table_idx = -1;
17b94a
     int gen = -1;
17b94a
     struct event_slot_epoll *table = NULL;
17b94a
 
17b94a
-    for (i = 0; i < EVENT_EPOLL_TABLES; i++) {
17b94a
+retry:
17b94a
+
17b94a
+    while (i < EVENT_EPOLL_TABLES) {
17b94a
         switch (event_pool->slots_used[i]) {
17b94a
             case EVENT_EPOLL_SLOTS:
17b94a
                 continue;
17b94a
@@ -98,6 +110,7 @@ __event_slot_alloc(struct event_pool *event_pool, int fd,
17b94a
         if (table)
17b94a
             /* break out of the loop */
17b94a
             break;
17b94a
+        i++;
17b94a
     }
17b94a
 
17b94a
     if (!table)
17b94a
@@ -105,20 +118,20 @@ __event_slot_alloc(struct event_pool *event_pool, int fd,
17b94a
 
17b94a
     table_idx = i;
17b94a
 
17b94a
-    for (i = 0; i < EVENT_EPOLL_SLOTS; i++) {
17b94a
-        if (table[i].fd == -1) {
17b94a
+    for (j = 0; j < EVENT_EPOLL_SLOTS; j++) {
17b94a
+        if (table[j].fd == -1) {
17b94a
             /* wipe everything except bump the generation */
17b94a
-            gen = table[i].gen;
17b94a
-            memset(&table[i], 0, sizeof(table[i]));
17b94a
-            table[i].gen = gen + 1;
17b94a
+            gen = table[j].gen;
17b94a
+            memset(&table[j], 0, sizeof(table[j]));
17b94a
+            table[j].gen = gen + 1;
17b94a
 
17b94a
-            LOCK_INIT(&table[i].lock);
17b94a
-            INIT_LIST_HEAD(&table[i].poller_death);
17b94a
+            LOCK_INIT(&table[j].lock);
17b94a
+            INIT_LIST_HEAD(&table[j].poller_death);
17b94a
 
17b94a
-            table[i].fd = fd;
17b94a
+            table[j].fd = fd;
17b94a
             if (notify_poller_death) {
17b94a
-                table[i].idx = table_idx * EVENT_EPOLL_SLOTS + i;
17b94a
-                list_add_tail(&table[i].poller_death,
17b94a
+                table[j].idx = table_idx * EVENT_EPOLL_SLOTS + j;
17b94a
+                list_add_tail(&table[j].poller_death,
17b94a
                               &event_pool->poller_death);
17b94a
             }
17b94a
 
17b94a
@@ -128,18 +141,26 @@ __event_slot_alloc(struct event_pool *event_pool, int fd,
17b94a
         }
17b94a
     }
17b94a
 
17b94a
-    return table_idx * EVENT_EPOLL_SLOTS + i;
17b94a
+    if (j == EVENT_EPOLL_SLOTS) {
17b94a
+        table = NULL;
17b94a
+        i++;
17b94a
+        goto retry;
17b94a
+    } else {
17b94a
+        (*slot) = &table[j];
17b94a
+        event_slot_ref(*slot);
17b94a
+        return table_idx * EVENT_EPOLL_SLOTS + j;
17b94a
+    }
17b94a
 }
17b94a
 
17b94a
 static int
17b94a
 event_slot_alloc(struct event_pool *event_pool, int fd,
17b94a
-                 char notify_poller_death)
17b94a
+                 char notify_poller_death, struct event_slot_epoll **slot)
17b94a
 {
17b94a
     int idx = -1;
17b94a
 
17b94a
     pthread_mutex_lock(&event_pool->mutex);
17b94a
     {
17b94a
-        idx = __event_slot_alloc(event_pool, fd, notify_poller_death);
17b94a
+        idx = __event_slot_alloc(event_pool, fd, notify_poller_death, slot);
17b94a
     }
17b94a
     pthread_mutex_unlock(&event_pool->mutex);
17b94a
 
17b94a
@@ -153,6 +174,7 @@ __event_slot_dealloc(struct event_pool *event_pool, int idx)
17b94a
     int offset = 0;
17b94a
     struct event_slot_epoll *table = NULL;
17b94a
     struct event_slot_epoll *slot = NULL;
17b94a
+    int fd = -1;
17b94a
 
17b94a
     table_idx = idx / EVENT_EPOLL_SLOTS;
17b94a
     offset = idx % EVENT_EPOLL_SLOTS;
17b94a
@@ -164,11 +186,13 @@ __event_slot_dealloc(struct event_pool *event_pool, int idx)
17b94a
     slot = &table[offset];
17b94a
     slot->gen++;
17b94a
 
17b94a
+    fd = slot->fd;
17b94a
     slot->fd = -1;
17b94a
     slot->handled_error = 0;
17b94a
     slot->in_handler = 0;
17b94a
     list_del_init(&slot->poller_death);
17b94a
-    event_pool->slots_used[table_idx]--;
17b94a
+    if (fd != -1)
17b94a
+        event_pool->slots_used[table_idx]--;
17b94a
 
17b94a
     return;
17b94a
 }
17b94a
@@ -185,15 +209,6 @@ event_slot_dealloc(struct event_pool *event_pool, int idx)
17b94a
     return;
17b94a
 }
17b94a
 
17b94a
-static int
17b94a
-event_slot_ref(struct event_slot_epoll *slot)
17b94a
-{
17b94a
-    if (!slot)
17b94a
-        return -1;
17b94a
-
17b94a
-    return GF_ATOMIC_INC(slot->ref);
17b94a
-}
17b94a
-
17b94a
 static struct event_slot_epoll *
17b94a
 event_slot_get(struct event_pool *event_pool, int idx)
17b94a
 {
17b94a
@@ -379,20 +394,13 @@ event_register_epoll(struct event_pool *event_pool, int fd,
17b94a
     if (destroy == 1)
17b94a
         goto out;
17b94a
 
17b94a
-    idx = event_slot_alloc(event_pool, fd, notify_poller_death);
17b94a
+    idx = event_slot_alloc(event_pool, fd, notify_poller_death, &slot);
17b94a
     if (idx == -1) {
17b94a
         gf_msg("epoll", GF_LOG_ERROR, 0, LG_MSG_SLOT_NOT_FOUND,
17b94a
                "could not find slot for fd=%d", fd);
17b94a
         return -1;
17b94a
     }
17b94a
 
17b94a
-    slot = event_slot_get(event_pool, idx);
17b94a
-    if (!slot) {
17b94a
-        gf_msg("epoll", GF_LOG_ERROR, 0, LG_MSG_SLOT_NOT_FOUND,
17b94a
-               "could not find slot for fd=%d idx=%d", fd, idx);
17b94a
-        return -1;
17b94a
-    }
17b94a
-
17b94a
     assert(slot->fd == fd);
17b94a
 
17b94a
     LOCK(&slot->lock);
17b94a
-- 
17b94a
1.8.3.1
17b94a