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