Blame SOURCES/0043-Ticket-49495-Fix-memory-management-is-vattr.patch

058656
From 2c56e7dc08a41fc1dfa6a79213e93686f553847c Mon Sep 17 00:00:00 2001
058656
From: William Brown <firstyear@redhat.com>
058656
Date: Mon, 11 Dec 2017 15:48:24 +0100
058656
Subject: [PATCH] Ticket 49495 - Fix memory management is vattr.
058656
058656
Bug Description:  During the fix for
058656
https://pagure.io/389-ds-base/issue/49436 a issue was exposed
058656
in how registration of attributes to cos work. With the change
058656
to handle -> attr link, this exposed that cos treats each attribute
058656
and template pair as a new type for the handle. As  aresult, this
058656
caused the sp_list to create a long linked list of M*N entries
058656
for each attr - template value. Obviously, this is extremely
058656
slow to traverse during a search!
058656
058656
Fix Description:  Undo part of the SLL next change and convert
058656
to reference counting. The issue remains that there is a defect
058656
in how cos handles attribute registration, but this can not be
058656
resolved without a significant rearchitecture of the code
058656
related to virtual attributes.
058656
058656
https://pagure.io/389-ds-base/issue/49495
058656
058656
Author: wibrown
058656
058656
Review by: tbordaz, lkrispen (Thanks!)
058656
---
058656
 ldap/servers/plugins/cos/cos_cache.c | 28 +++++++++++-----------------
058656
 ldap/servers/slapd/vattr.c           | 23 +++++++++++++++++++++--
058656
 2 files changed, 32 insertions(+), 19 deletions(-)
058656
058656
diff --git a/ldap/servers/plugins/cos/cos_cache.c b/ldap/servers/plugins/cos/cos_cache.c
058656
index 662dace35..3b3c05783 100644
058656
--- a/ldap/servers/plugins/cos/cos_cache.c
058656
+++ b/ldap/servers/plugins/cos/cos_cache.c
058656
@@ -275,7 +275,7 @@ static Slapi_Mutex *start_lock;
058656
 static Slapi_Mutex *stop_lock;
058656
 static Slapi_CondVar *something_changed = NULL;
058656
 static Slapi_CondVar *start_cond = NULL;
058656
-
058656
+static vattr_sp_handle *vattr_handle = NULL;
058656
 
058656
 /*
058656
     cos_cache_init
058656
@@ -314,6 +314,15 @@ cos_cache_init(void)
058656
         goto out;
058656
     }
058656
 
058656
+    if (slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
058656
+                                cos_cache_vattr_get,
058656
+                                cos_cache_vattr_compare,
058656
+                                cos_cache_vattr_types) != 0) {
058656
+        slapi_log_err(SLAPI_LOG_ERR, COS_PLUGIN_SUBSYSTEM, "cos_cache_init - Cannot register as service provider\n");
058656
+        ret = -1;
058656
+        goto out;
058656
+    }
058656
+
058656
     /* grab the views interface */
058656
     if (slapi_apib_get_interface(Views_v1_0_GUID, &views_api)) {
058656
         /* lets be tolerant if views is disabled */
058656
@@ -847,22 +856,7 @@ cos_dn_defs_cb(Slapi_Entry *e, void *callback_data)
058656
                                           dnVals[valIndex]->bv_val);
058656
                 }
058656
 
058656
-                /*
058656
-                 * Each SP_handle is associated to one and only one vattr.
058656
-                 * We could consider making this a single function rather
058656
-                 * than the double-call.
058656
-                 */
058656
-
058656
-                vattr_sp_handle *vattr_handle = NULL;
058656
-
058656
-                if (slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
058656
-                                            cos_cache_vattr_get,
058656
-                                            cos_cache_vattr_compare,
058656
-                                            cos_cache_vattr_types) != 0) {
058656
-                    slapi_log_err(SLAPI_LOG_ERR, COS_PLUGIN_SUBSYSTEM, "cos_cache_init - Cannot register as service provider for %s\n", dnVals[valIndex]->bv_val);
058656
-                } else {
058656
-                    slapi_vattrspi_regattr((vattr_sp_handle *)vattr_handle, dnVals[valIndex]->bv_val, NULL, NULL);
058656
-                }
058656
+                slapi_vattrspi_regattr((vattr_sp_handle *)vattr_handle, dnVals[valIndex]->bv_val, NULL, NULL);
058656
 
058656
             } /* if(attrType is cosAttribute) */
058656
 
058656
diff --git a/ldap/servers/slapd/vattr.c b/ldap/servers/slapd/vattr.c
058656
index 432946c79..13e527188 100644
058656
--- a/ldap/servers/slapd/vattr.c
058656
+++ b/ldap/servers/slapd/vattr.c
058656
@@ -1544,6 +1544,7 @@ struct _vattr_sp_handle
058656
     vattr_sp *sp;
058656
     struct _vattr_sp_handle *next; /* So we can link them together in the map */
058656
     void *hint;                    /* Hint to the SP */
058656
+    uint64_t rc;
058656
 };
058656
 
058656
 /* Calls made by Service Providers */
058656
@@ -1770,7 +1771,7 @@ is a separate thing in the insterests of stability.
058656
 
058656
  */
058656
 
058656
-#define VARRT_MAP_HASHTABLE_SIZE 10
058656
+#define VARRT_MAP_HASHTABLE_SIZE 32
058656
 
058656
 /* Attribute map oject */
058656
 /* Needs to contain: a linked list of pointers to provider handles handles,
058656
@@ -1867,7 +1868,10 @@ vattr_map_entry_free(vattr_map_entry *vae)
058656
     vattr_sp_handle *list_entry = vae->sp_list;
058656
     while (list_entry != NULL) {
058656
         vattr_sp_handle *next_entry = list_entry->next;
058656
-        slapi_ch_free((void **)&list_entry);
058656
+        if (slapi_atomic_decr_64(&(list_entry->rc), __ATOMIC_RELAXED) == 0) {
058656
+            /* Only free on RC 0 */
058656
+            slapi_ch_free((void **)&list_entry);
058656
+        }
058656
         list_entry = next_entry;
058656
     }
058656
     slapi_ch_free_string(&(vae->type_name));
058656
@@ -2280,6 +2284,17 @@ to handle the calls on it, but return nothing */
058656
  *
058656
  * Better idea, is that regattr should just take the fn pointers
058656
  * and callers never *see* the sp_handle structure at all.
058656
+ *
058656
+ * This leaves us with some quirks today. First: if you have plugin A
058656
+ * and B, A registers attr 1 and B 1 and 2, it's possible that if you
058656
+ * register A1 first, then B1, you have B->A in next. Then when you
058656
+ * register B2, because we take 0==result from map_lookup, we add sp
058656
+ * "as is" to the map. This means that B2 now has the same next to A1
058656
+ * handle. This won't add a bug, because A1 won't be able to service the
058656
+ * attr, but it could cause some head scratching ...
058656
+ *
058656
+ * Again, to fix this, the whole vattr external interface needs a
058656
+ * redesign ... :(
058656
  */
058656
 
058656
 int
058656
@@ -2304,11 +2319,15 @@ vattr_map_sp_insert(char *type_to_add, vattr_sp_handle *sp, void *hint)
058656
         if (found) {
058656
             return 0;
058656
         }
058656
+        /* Increase the ref count of the sphandle */
058656
+        slapi_atomic_incr_64(&(sp->rc), __ATOMIC_RELAXED);
058656
         /* We insert the SP handle into the linked list at the head */
058656
         sp->next = map_entry->sp_list;
058656
         map_entry->sp_list = sp;
058656
     } else {
058656
         /* If not, add it */
058656
+        /* Claim a reference on the sp ... */
058656
+        slapi_atomic_incr_64(&(sp->rc), __ATOMIC_RELAXED);
058656
         map_entry = vattr_map_entry_new(type_to_add, sp, hint);
058656
         if (NULL == map_entry) {
058656
             return ENOMEM;
058656
-- 
058656
2.13.6
058656