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

61f723
From d1236b0d3917b024a252b4d6acf823a975182d3b Mon Sep 17 00:00:00 2001
61f723
From: William Brown <firstyear@redhat.com>
61f723
Date: Mon, 11 Dec 2017 15:48:24 +0100
61f723
Subject: [PATCH] Ticket 49495 - Fix memory management is vattr.
61f723
61f723
Bug Description:  During the fix for
61f723
https://pagure.io/389-ds-base/issue/49436 a issue was exposed
61f723
in how registration of attributes to cos work. With the change
61f723
to handle -> attr link, this exposed that cos treats each attribute
61f723
and template pair as a new type for the handle. As  aresult, this
61f723
caused the sp_list to create a long linked list of M*N entries
61f723
for each attr - template value. Obviously, this is extremely
61f723
slow to traverse during a search!
61f723
61f723
Fix Description:  Undo part of the SLL next change and convert
61f723
to reference counting. The issue remains that there is a defect
61f723
in how cos handles attribute registration, but this can not be
61f723
resolved without a significant rearchitecture of the code
61f723
related to virtual attributes.
61f723
61f723
https://pagure.io/389-ds-base/issue/49495
61f723
61f723
Author: wibrown
61f723
61f723
Review by: tbordaz, lkrispen (Thanks!)
61f723
61f723
Note: includes backport of incr and decr for rc
61f723
---
61f723
 ldap/servers/plugins/cos/cos_cache.c |  28 ++++------
61f723
 ldap/servers/slapd/slapi-plugin.h    |  21 ++++++++
61f723
 ldap/servers/slapd/slapi_counter.c   |  30 +++++++++++
61f723
 ldap/servers/slapd/vattr.c           | 100 +++++++++++++++++++++--------------
61f723
 4 files changed, 122 insertions(+), 57 deletions(-)
61f723
61f723
diff --git a/ldap/servers/plugins/cos/cos_cache.c b/ldap/servers/plugins/cos/cos_cache.c
61f723
index 0e93183d2..74261af87 100644
61f723
--- a/ldap/servers/plugins/cos/cos_cache.c
61f723
+++ b/ldap/servers/plugins/cos/cos_cache.c
61f723
@@ -275,7 +275,7 @@ static Slapi_Mutex *start_lock;
61f723
 static Slapi_Mutex *stop_lock;
61f723
 static Slapi_CondVar *something_changed = NULL;
61f723
 static Slapi_CondVar *start_cond = NULL;
61f723
-
61f723
+static vattr_sp_handle *vattr_handle = NULL;
61f723
 
61f723
 /*
61f723
 	cos_cache_init
61f723
@@ -313,6 +313,15 @@ int cos_cache_init(void)
61f723
         goto out;
61f723
     }
61f723
 
61f723
+    if (slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
61f723
+                                cos_cache_vattr_get,
61f723
+                                cos_cache_vattr_compare,
61f723
+                                cos_cache_vattr_types) != 0) {
61f723
+        slapi_log_err(SLAPI_LOG_ERR, COS_PLUGIN_SUBSYSTEM, "cos_cache_init - Cannot register as service provider\n");
61f723
+        ret = -1;
61f723
+        goto out;
61f723
+    }
61f723
+
61f723
     /* grab the views interface */
61f723
     if (slapi_apib_get_interface(Views_v1_0_GUID, &views_api)) {
61f723
         /* lets be tolerant if views is disabled */
61f723
@@ -872,22 +881,7 @@ cos_dn_defs_cb (Slapi_Entry* e, void *callback_data)
61f723
                                           dnVals[valIndex]->bv_val);
61f723
                 }
61f723
 
61f723
-                /*
61f723
-                 * Each SP_handle is associated to one and only one vattr.
61f723
-                 * We could consider making this a single function rather
61f723
-                 * than the double-call.
61f723
-                 */
61f723
-
61f723
-                vattr_sp_handle *vattr_handle = NULL;
61f723
-
61f723
-                if (slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
61f723
-                                            cos_cache_vattr_get,
61f723
-                                            cos_cache_vattr_compare,
61f723
-                                            cos_cache_vattr_types) != 0) {
61f723
-                    slapi_log_err(SLAPI_LOG_ERR, COS_PLUGIN_SUBSYSTEM, "cos_cache_init - Cannot register as service provider for %s\n", dnVals[valIndex]->bv_val);
61f723
-                } else {
61f723
-                    slapi_vattrspi_regattr((vattr_sp_handle *)vattr_handle, dnVals[valIndex]->bv_val, NULL, NULL);
61f723
-                }
61f723
+                slapi_vattrspi_regattr((vattr_sp_handle *)vattr_handle, dnVals[valIndex]->bv_val, NULL, NULL);
61f723
 
61f723
             } /* if(attrType is cosAttribute) */
61f723
 
61f723
diff --git a/ldap/servers/slapd/slapi-plugin.h b/ldap/servers/slapd/slapi-plugin.h
61f723
index 4084945f4..16aa1b711 100644
61f723
--- a/ldap/servers/slapd/slapi-plugin.h
61f723
+++ b/ldap/servers/slapd/slapi-plugin.h
61f723
@@ -8063,6 +8063,27 @@ int slapi_is_special_rdn(const char *rdn, int flag);
61f723
  */
61f723
 void    DS_Sleep(PRIntervalTime ticks);
61f723
 
61f723
+/**
61f723
+ * Increment a 64bitintegral atomicly
61f723
+ *
61f723
+ * \param ptr - pointer to integral to increment
61f723
+ * \param memorder - __ATOMIC_RELAXED, __ATOMIC_CONSUME, __ATOMIC_ACQUIRE,
61f723
+ * __ATOMIC_RELEASE, __ATOMIC_ACQ_REL, __ATOMIC_SEQ_CST
61f723
+ * \return - new value of ptr
61f723
+ */
61f723
+uint64_t slapi_atomic_incr_64(uint64_t *ptr, int memorder);
61f723
+
61f723
+/**
61f723
+ * Decrement a 64bitintegral atomicly
61f723
+ *
61f723
+ * \param ptr - pointer to integral to decrement
61f723
+ * \param memorder - __ATOMIC_RELAXED, __ATOMIC_CONSUME, __ATOMIC_ACQUIRE,
61f723
+ * __ATOMIC_RELEASE, __ATOMIC_ACQ_REL, __ATOMIC_SEQ_CST
61f723
+ * \return - new value of ptr
61f723
+ */
61f723
+uint64_t slapi_atomic_decr_64(uint64_t *ptr, int memorder);
61f723
+
61f723
+
61f723
 #ifdef __cplusplus
61f723
 }
61f723
 #endif
61f723
diff --git a/ldap/servers/slapd/slapi_counter.c b/ldap/servers/slapd/slapi_counter.c
61f723
index 9904fe964..59e5223ad 100644
61f723
--- a/ldap/servers/slapd/slapi_counter.c
61f723
+++ b/ldap/servers/slapd/slapi_counter.c
61f723
@@ -269,3 +269,33 @@ uint64_t slapi_counter_get_value(Slapi_Counter *counter)
61f723
     return value;
61f723
 }
61f723
 
61f723
+/*
61f723
+ * atomic increment functions (64bit)
61f723
+ */
61f723
+uint64_t
61f723
+slapi_atomic_incr_64(uint64_t *ptr, int memorder)
61f723
+{
61f723
+#ifdef ATOMIC_64BIT_OPERATIONS
61f723
+    return __atomic_add_fetch_8(ptr, 1, memorder);
61f723
+#else
61f723
+    PRInt32 *pr_ptr = (PRInt32 *)ptr;
61f723
+    return PR_AtomicIncrement(pr_ptr);
61f723
+#endif
61f723
+}
61f723
+
61f723
+/*
61f723
+ * atomic decrement functions (64bit)
61f723
+ */
61f723
+
61f723
+uint64_t
61f723
+slapi_atomic_decr_64(uint64_t *ptr, int memorder)
61f723
+{
61f723
+#ifdef ATOMIC_64BIT_OPERATIONS
61f723
+    return __atomic_sub_fetch_8(ptr, 1, memorder);
61f723
+#else
61f723
+    PRInt32 *pr_ptr = (PRInt32 *)ptr;
61f723
+    return PR_AtomicDecrement(pr_ptr);
61f723
+#endif
61f723
+}
61f723
+
61f723
+
61f723
diff --git a/ldap/servers/slapd/vattr.c b/ldap/servers/slapd/vattr.c
61f723
index 84e01cd62..adf44b0b6 100644
61f723
--- a/ldap/servers/slapd/vattr.c
61f723
+++ b/ldap/servers/slapd/vattr.c
61f723
@@ -1529,10 +1529,12 @@ struct _vattr_sp {
61f723
 typedef struct _vattr_sp vattr_sp;
61f723
 
61f723
 /* Service provider handle */
61f723
-struct _vattr_sp_handle {
61f723
-	vattr_sp *sp;
61f723
-	struct _vattr_sp_handle *next; /* So we can link them together in the map */
61f723
-	void *hint; /* Hint to the SP */
61f723
+struct _vattr_sp_handle
61f723
+{
61f723
+    vattr_sp *sp;
61f723
+    struct _vattr_sp_handle *next; /* So we can link them together in the map */
61f723
+    void *hint;                    /* Hint to the SP */
61f723
+    uint64_t rc;
61f723
 };
61f723
 
61f723
 /* Calls made by Service Providers */
61f723
@@ -1758,7 +1760,7 @@ is a separate thing in the insterests of stability.
61f723
 
61f723
  */
61f723
 
61f723
-#define VARRT_MAP_HASHTABLE_SIZE 10
61f723
+#define VARRT_MAP_HASHTABLE_SIZE 32
61f723
 
61f723
 /* Attribute map oject */
61f723
 /* Needs to contain: a linked list of pointers to provider handles handles,
61f723
@@ -1849,7 +1851,10 @@ vattr_map_entry_free(vattr_map_entry *vae)
61f723
     vattr_sp_handle *list_entry = vae->sp_list;
61f723
     while (list_entry != NULL) {
61f723
         vattr_sp_handle *next_entry = list_entry->next;
61f723
-        slapi_ch_free((void **)&list_entry);
61f723
+        if (slapi_atomic_decr_64(&(list_entry->rc), __ATOMIC_RELAXED) == 0) {
61f723
+            /* Only free on RC 0 */
61f723
+            slapi_ch_free((void **)&list_entry);
61f723
+        }
61f723
         list_entry = next_entry;
61f723
     }
61f723
     slapi_ch_free_string(&(vae->type_name));
61f723
@@ -2268,41 +2273,56 @@ to handle the calls on it, but return nothing */
61f723
  *
61f723
  * Better idea, is that regattr should just take the fn pointers
61f723
  * and callers never *see* the sp_handle structure at all.
61f723
+ *
61f723
+ * This leaves us with some quirks today. First: if you have plugin A
61f723
+ * and B, A registers attr 1 and B 1 and 2, it's possible that if you
61f723
+ * register A1 first, then B1, you have B->A in next. Then when you
61f723
+ * register B2, because we take 0==result from map_lookup, we add sp
61f723
+ * "as is" to the map. This means that B2 now has the same next to A1
61f723
+ * handle. This won't add a bug, because A1 won't be able to service the
61f723
+ * attr, but it could cause some head scratching ...
61f723
+ *
61f723
+ * Again, to fix this, the whole vattr external interface needs a
61f723
+ * redesign ... :(
61f723
  */
61f723
-
61f723
-int vattr_map_sp_insert(char *type_to_add, vattr_sp_handle *sp, void *hint)
61f723
-{
61f723
-	int result = 0;
61f723
-	vattr_map_entry *map_entry = NULL;
61f723
-	/* Is this type already there ? */
61f723
-	result = vattr_map_lookup(type_to_add,&map_entry);
61f723
-	/* If it is, add this SP to the list, safely even if readers are traversing the list at the same time */
61f723
-	if (0 == result) {
61f723
-		int found = 0;
61f723
-		vattr_sp_handle *list_entry = NULL;
61f723
-		/* Walk the list checking that the daft SP isn't already here */
61f723
-		for (list_entry = map_entry->sp_list ; list_entry; list_entry = list_entry->next) {
61f723
-			if (list_entry == sp) {
61f723
-				found = 1;
61f723
-				break;
61f723
-			}
61f723
-		}
61f723
-		/* If it is, we do nothing */
61f723
-		if(found) {
61f723
-			return 0;
61f723
-		}
61f723
-		/* We insert the SP handle into the linked list at the head */
61f723
-		sp->next = map_entry->sp_list;
61f723
-		map_entry->sp_list = sp;
61f723
-	} else {
61f723
-	/* If not, add it */
61f723
-		map_entry = vattr_map_entry_new(type_to_add,sp,hint);
61f723
-		if (NULL == map_entry) {
61f723
-			return ENOMEM;
61f723
-		}
61f723
-		return vattr_map_insert(map_entry);
61f723
-	}
61f723
-	return 0;
61f723
+int
61f723
+vattr_map_sp_insert(char *type_to_add, vattr_sp_handle *sp, void *hint)
61f723
+{
61f723
+    int result = 0;
61f723
+    vattr_map_entry *map_entry = NULL;
61f723
+    /* Is this type already there ? */
61f723
+    result = vattr_map_lookup(type_to_add, &map_entry);
61f723
+    /* If it is, add this SP to the list, safely even if readers are traversing the list at the same time */
61f723
+    if (0 == result) {
61f723
+        int found = 0;
61f723
+        vattr_sp_handle *list_entry = NULL;
61f723
+        /* Walk the list checking that the daft SP isn't already here */
61f723
+        for (list_entry = map_entry->sp_list; list_entry; list_entry = list_entry->next) {
61f723
+            if (list_entry == sp) {
61f723
+                found = 1;
61f723
+                break;
61f723
+            }
61f723
+        }
61f723
+        /* If it is, we do nothing */
61f723
+        if (found) {
61f723
+            return 0;
61f723
+        }
61f723
+        /* Increase the ref count of the sphandle */
61f723
+        slapi_atomic_incr_64(&(sp->rc), __ATOMIC_RELAXED);
61f723
+        /* We insert the SP handle into the linked list at the head */
61f723
+        sp->next = map_entry->sp_list;
61f723
+        map_entry->sp_list = sp;
61f723
+    } else {
61f723
+        /* If not, add it */
61f723
+        /* Claim a reference on the sp ... */
61f723
+        slapi_atomic_incr_64(&(sp->rc), __ATOMIC_RELAXED);
61f723
+        map_entry = vattr_map_entry_new(type_to_add, sp, hint);
61f723
+        if (NULL == map_entry) {
61f723
+            return ENOMEM;
61f723
+        }
61f723
+        return vattr_map_insert(map_entry);
61f723
+    }
61f723
+    return 0;
61f723
 }
61f723
 
61f723
 /*
61f723
-- 
61f723
2.13.6
61f723