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

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