Blame SOURCES/0136-KCM-Fix-the-per-client-serialization-queue.patch

ecf709
From b31f75f44a9e1dc0521ec73176f89e05db4973ba Mon Sep 17 00:00:00 2001
ecf709
From: Jakub Hrozek <jhrozek@redhat.com>
ecf709
Date: Thu, 11 May 2017 16:24:24 +0200
ecf709
Subject: [PATCH 136/138] KCM: Fix the per-client serialization queue
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
Resolves:
ecf709
    https://pagure.io/SSSD/sssd/issue/3372
ecf709
ecf709
Fixes a race condition between one client request adding an operation to
ecf709
the hash table value, which was previously a linked list of operations,
ecf709
while another concurrent operation would remove the last remaining
ecf709
linked list element through its callback.
ecf709
ecf709
Instead, the hash table value is now a separate 'queue head' structure
ecf709
which is only changed in a tevent request to make sure is is not
ecf709
processes concurrently with adding to the queue (which is also a tevent
ecf709
request).
ecf709
ecf709
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
ecf709
(cherry picked from commit fb51bb68e62de7bb8542f5d224994eb7143040a6)
ecf709
---
ecf709
 src/responder/kcm/kcmsrv_op_queue.c | 182 ++++++++++++++++++++++++------------
ecf709
 1 file changed, 122 insertions(+), 60 deletions(-)
ecf709
ecf709
diff --git a/src/responder/kcm/kcmsrv_op_queue.c b/src/responder/kcm/kcmsrv_op_queue.c
ecf709
index f6c425dd5b64877c8b7401e488dd6565157fc9b5..55c8b65d94f70979fe56fcc4d8747547a9cc9d33 100644
ecf709
--- a/src/responder/kcm/kcmsrv_op_queue.c
ecf709
+++ b/src/responder/kcm/kcmsrv_op_queue.c
ecf709
@@ -27,17 +27,23 @@
ecf709
 
ecf709
 struct kcm_ops_queue_entry {
ecf709
     struct tevent_req *req;
ecf709
-    uid_t uid;
ecf709
 
ecf709
-    hash_table_t *wait_queue_hash;
ecf709
+    struct kcm_ops_queue *queue;
ecf709
 
ecf709
-    struct kcm_ops_queue_entry *head;
ecf709
     struct kcm_ops_queue_entry *next;
ecf709
     struct kcm_ops_queue_entry *prev;
ecf709
 };
ecf709
 
ecf709
+struct kcm_ops_queue {
ecf709
+    uid_t uid;
ecf709
+    struct tevent_context *ev;
ecf709
+    struct kcm_ops_queue_ctx *qctx;
ecf709
+
ecf709
+    struct kcm_ops_queue_entry *head;
ecf709
+};
ecf709
+
ecf709
 struct kcm_ops_queue_ctx {
ecf709
-    /* UID: dlist of kcm_ops_queue_entry */
ecf709
+    /* UID:kcm_ops_queue */
ecf709
     hash_table_t *wait_queue_hash;
ecf709
 };
ecf709
 
ecf709
@@ -45,8 +51,9 @@ struct kcm_ops_queue_ctx {
ecf709
  * Per-UID wait queue
ecf709
  *
ecf709
  * They key in the hash table is the UID of the peer. The value of each
ecf709
- * hash table entry is a linked list of kcm_ops_queue_entry structures
ecf709
- * which primarily hold the tevent request being queued.
ecf709
+ * hash table entry is kcm_ops_queue structure which in turn contains a
ecf709
+ * linked list of kcm_ops_queue_entry structures * which primarily hold the
ecf709
+ * tevent request being queued.
ecf709
  */
ecf709
 struct kcm_ops_queue_ctx *kcm_ops_queue_create(TALLOC_CTX *mem_ctx)
ecf709
 {
ecf709
@@ -71,11 +78,45 @@ struct kcm_ops_queue_ctx *kcm_ops_queue_create(TALLOC_CTX *mem_ctx)
ecf709
     return queue_ctx;
ecf709
 }
ecf709
 
ecf709
-static int kcm_op_queue_entry_destructor(struct kcm_ops_queue_entry *entry)
ecf709
+void queue_removal_cb(struct tevent_context *ctx,
ecf709
+                      struct tevent_immediate *imm,
ecf709
+                      void *private_data)
ecf709
 {
ecf709
+    struct kcm_ops_queue *kq = talloc_get_type(private_data,
ecf709
+                                               struct kcm_ops_queue);
ecf709
     int ret;
ecf709
+    hash_key_t key;
ecf709
+
ecf709
+    talloc_free(imm);
ecf709
+
ecf709
+    if (kq->head != NULL) {
ecf709
+        DEBUG(SSSDBG_TRACE_LIBS, "The queue is no longer empty\n");
ecf709
+        return;
ecf709
+    }
ecf709
+
ecf709
+    key.type = HASH_KEY_ULONG;
ecf709
+    key.ul = kq->uid;
ecf709
+
ecf709
+    /* If this was the last entry, remove the key (the UID) from the
ecf709
+     * hash table to signal the queue is empty
ecf709
+     */
ecf709
+    ret = hash_delete(kq->qctx->wait_queue_hash, &key);
ecf709
+    if (ret != HASH_SUCCESS) {
ecf709
+        DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
+              "Failed to remove wait queue for user %"SPRIuid"\n",
ecf709
+              kq->uid);
ecf709
+        return;
ecf709
+    }
ecf709
+
ecf709
+    DEBUG(SSSDBG_FUNC_DATA,
ecf709
+          "Removed queue for %"SPRIuid" \n", kq->uid);
ecf709
+    talloc_free(kq);
ecf709
+}
ecf709
+
ecf709
+static int kcm_op_queue_entry_destructor(struct kcm_ops_queue_entry *entry)
ecf709
+{
ecf709
     struct kcm_ops_queue_entry *next_entry;
ecf709
-    hash_key_t key;
ecf709
+    struct tevent_immediate *imm;
ecf709
 
ecf709
     if (entry == NULL) {
ecf709
         return 1;
ecf709
@@ -85,22 +126,19 @@ static int kcm_op_queue_entry_destructor(struct kcm_ops_queue_entry *entry)
ecf709
     next_entry = entry->next;
ecf709
 
ecf709
     /* Remove the current entry from the queue */
ecf709
-    DLIST_REMOVE(entry->head, entry);
ecf709
+    DLIST_REMOVE(entry->queue->head, entry);
ecf709
 
ecf709
     if (next_entry == NULL) {
ecf709
-        key.type = HASH_KEY_ULONG;
ecf709
-        key.ul = entry->uid;
ecf709
-
ecf709
-        /* If this was the last entry, remove the key (the UID) from the
ecf709
-         * hash table to signal the queue is empty
ecf709
+        /* If there was no other entry, schedule removal of the queue. Do it
ecf709
+         * in another tevent tick to avoid issues with callbacks invoking
ecf709
+         * the descructor while another request is touching the queue
ecf709
          */
ecf709
-        ret = hash_delete(entry->wait_queue_hash, &key);
ecf709
-        if (ret != HASH_SUCCESS) {
ecf709
-            DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
-                  "Failed to remove wait queue for user %"SPRIuid"\n",
ecf709
-                  entry->uid);
ecf709
+        imm = tevent_create_immediate(entry->queue);
ecf709
+        if (imm == NULL) {
ecf709
             return 1;
ecf709
         }
ecf709
+
ecf709
+        tevent_schedule_immediate(imm, entry->queue->ev, queue_removal_cb, entry->queue);
ecf709
         return 0;
ecf709
     }
ecf709
 
ecf709
@@ -109,41 +147,33 @@ static int kcm_op_queue_entry_destructor(struct kcm_ops_queue_entry *entry)
ecf709
     return 0;
ecf709
 }
ecf709
 
ecf709
-static errno_t kcm_op_queue_add(hash_table_t *wait_queue_hash,
ecf709
-                                struct kcm_ops_queue_entry *entry,
ecf709
-                                uid_t uid)
ecf709
+static struct kcm_ops_queue *kcm_op_queue_get(struct kcm_ops_queue_ctx *qctx,
ecf709
+                                              struct tevent_context *ev,
ecf709
+                                              uid_t uid)
ecf709
 {
ecf709
     errno_t ret;
ecf709
     hash_key_t key;
ecf709
     hash_value_t value;
ecf709
-    struct kcm_ops_queue_entry *head = NULL;
ecf709
+    struct kcm_ops_queue *kq;
ecf709
 
ecf709
     key.type = HASH_KEY_ULONG;
ecf709
     key.ul = uid;
ecf709
 
ecf709
-    ret = hash_lookup(wait_queue_hash, &key, &value);
ecf709
+    ret = hash_lookup(qctx->wait_queue_hash, &key, &value);
ecf709
     switch (ret) {
ecf709
     case HASH_SUCCESS:
ecf709
-        /* The key with this UID already exists. Its value is request queue
ecf709
-         * for the UID, so let's just add the current request to the end
ecf709
-         * of the queue and wait for the previous requests to finish
ecf709
-         */
ecf709
         if (value.type != HASH_VALUE_PTR) {
ecf709
             DEBUG(SSSDBG_CRIT_FAILURE, "Unexpected hash value type.\n");
ecf709
-            return EINVAL;
ecf709
+            return NULL;
ecf709
         }
ecf709
 
ecf709
-        head = talloc_get_type(value.ptr, struct kcm_ops_queue_entry);
ecf709
-        if (head == NULL) {
ecf709
+        kq = talloc_get_type(value.ptr, struct kcm_ops_queue);
ecf709
+        if (kq == NULL) {
ecf709
             DEBUG(SSSDBG_CRIT_FAILURE, "Invalid queue pointer\n");
ecf709
-            return EINVAL;
ecf709
+            return NULL;
ecf709
         }
ecf709
 
ecf709
-        entry->head = head;
ecf709
-        DLIST_ADD_END(head, entry, struct kcm_ops_queue_entry *);
ecf709
-
ecf709
-        DEBUG(SSSDBG_TRACE_LIBS, "Waiting in queue\n");
ecf709
-        ret = EAGAIN;
ecf709
+        DEBUG(SSSDBG_TRACE_LIBS, "Found existing queue for this ID\n");
ecf709
         break;
ecf709
 
ecf709
     case HASH_ERROR_KEY_NOT_FOUND:
ecf709
@@ -151,36 +181,41 @@ static errno_t kcm_op_queue_add(hash_table_t *wait_queue_hash,
ecf709
          * another one comes in and return EOK to run the current request
ecf709
          * immediatelly
ecf709
          */
ecf709
-        entry->head = entry;
ecf709
+        DEBUG(SSSDBG_TRACE_LIBS, "No existing queue for this ID\n");
ecf709
+
ecf709
+        kq = talloc_zero(qctx->wait_queue_hash, struct kcm_ops_queue);
ecf709
+        if (kq == NULL) {
ecf709
+            return NULL;
ecf709
+        }
ecf709
+        kq->uid = uid;
ecf709
+        kq->qctx = qctx;
ecf709
+        kq->ev = ev;
ecf709
 
ecf709
         value.type = HASH_VALUE_PTR;
ecf709
-        value.ptr = entry;
ecf709
+        value.ptr = kq;
ecf709
 
ecf709
-        ret = hash_enter(wait_queue_hash, &key, &value);
ecf709
+        ret = hash_enter(qctx->wait_queue_hash, &key, &value);
ecf709
         if (ret != HASH_SUCCESS) {
ecf709
             DEBUG(SSSDBG_CRIT_FAILURE, "hash_enter failed.\n");
ecf709
-            return EIO;
ecf709
+            return NULL;
ecf709
         }
ecf709
-
ecf709
-        DEBUG(SSSDBG_TRACE_LIBS,
ecf709
-              "Added a first request to the queue, running immediately\n");
ecf709
-        ret = EOK;
ecf709
         break;
ecf709
 
ecf709
     default:
ecf709
         DEBUG(SSSDBG_CRIT_FAILURE, "hash_lookup failed.\n");
ecf709
-        return EIO;
ecf709
+        return NULL;
ecf709
     }
ecf709
 
ecf709
-    talloc_steal(wait_queue_hash, entry);
ecf709
-    talloc_set_destructor(entry, kcm_op_queue_entry_destructor);
ecf709
-    return ret;
ecf709
+    return kq;
ecf709
 }
ecf709
 
ecf709
 struct kcm_op_queue_state {
ecf709
     struct kcm_ops_queue_entry *entry;
ecf709
 };
ecf709
 
ecf709
+static errno_t kcm_op_queue_add_req(struct kcm_ops_queue *kq,
ecf709
+                                    struct tevent_req *req);
ecf709
+
ecf709
 /*
ecf709
  * Enqueue a request.
ecf709
  *
ecf709
@@ -198,6 +233,7 @@ struct tevent_req *kcm_op_queue_send(TALLOC_CTX *mem_ctx,
ecf709
 {
ecf709
     errno_t ret;
ecf709
     struct tevent_req *req;
ecf709
+    struct kcm_ops_queue *kq;
ecf709
     struct kcm_op_queue_state *state;
ecf709
     uid_t uid;
ecf709
 
ecf709
@@ -208,22 +244,21 @@ struct tevent_req *kcm_op_queue_send(TALLOC_CTX *mem_ctx,
ecf709
         return NULL;
ecf709
     }
ecf709
 
ecf709
-    state->entry = talloc_zero(state, struct kcm_ops_queue_entry);
ecf709
-    if (state->entry == NULL) {
ecf709
-        ret = ENOMEM;
ecf709
-        goto immediate;
ecf709
-    }
ecf709
-    state->entry->req = req;
ecf709
-    state->entry->uid = uid;
ecf709
-    state->entry->wait_queue_hash = qctx->wait_queue_hash;
ecf709
-
ecf709
     DEBUG(SSSDBG_FUNC_DATA,
ecf709
           "Adding request by %"SPRIuid" to the wait queue\n", uid);
ecf709
 
ecf709
-    ret = kcm_op_queue_add(qctx->wait_queue_hash, state->entry, uid);
ecf709
+    kq = kcm_op_queue_get(qctx, ev, uid);
ecf709
+    if (kq == NULL) {
ecf709
+        ret = EIO;
ecf709
+        DEBUG(SSSDBG_OP_FAILURE,
ecf709
+              "Cannot get queue [%d]: %s\n", ret, sss_strerror(ret));
ecf709
+        goto immediate;
ecf709
+    }
ecf709
+
ecf709
+    ret = kcm_op_queue_add_req(kq, req);
ecf709
     if (ret == EOK) {
ecf709
         DEBUG(SSSDBG_TRACE_LIBS,
ecf709
-              "Wait queue was empty, running immediately\n");
ecf709
+              "Queue was empty, running the request immediately\n");
ecf709
         goto immediate;
ecf709
     } else if (ret != EAGAIN) {
ecf709
         DEBUG(SSSDBG_OP_FAILURE,
ecf709
@@ -244,6 +279,33 @@ immediate:
ecf709
     return req;
ecf709
 }
ecf709
 
ecf709
+static errno_t kcm_op_queue_add_req(struct kcm_ops_queue *kq,
ecf709
+                                    struct tevent_req *req)
ecf709
+{
ecf709
+    errno_t ret;
ecf709
+    struct kcm_op_queue_state *state = tevent_req_data(req,
ecf709
+                                                struct kcm_op_queue_state);
ecf709
+
ecf709
+    state->entry = talloc_zero(kq->qctx->wait_queue_hash, struct kcm_ops_queue_entry);
ecf709
+    if (state->entry == NULL) {
ecf709
+        return ENOMEM;
ecf709
+    }
ecf709
+    state->entry->req = req;
ecf709
+    state->entry->queue = kq;
ecf709
+    talloc_set_destructor(state->entry, kcm_op_queue_entry_destructor);
ecf709
+
ecf709
+    if (kq->head == NULL) {
ecf709
+        /* First entry, will run callback at once */
ecf709
+        ret = EOK;
ecf709
+    } else {
ecf709
+        /* Will wait for the previous callbacks to finish */
ecf709
+        ret = EAGAIN;
ecf709
+    }
ecf709
+
ecf709
+    DLIST_ADD_END(kq->head, state->entry, struct kcm_ops_queue_entry *);
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
 /*
ecf709
  * The queue recv function is called when this request is 'activated'. The queue
ecf709
  * entry should be allocated on the same memory context as the enqueued request
ecf709
-- 
ecf709
2.9.4
ecf709