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