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

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