amoralej / rpms / 389-ds-base

Forked from rpms/389-ds-base 5 years ago
Clone

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

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