233933
From f305ee93ec9dbbd679e1eb58c7c0bf8d9b5659d5 Mon Sep 17 00:00:00 2001
233933
From: Xavi Hernandez <xhernandez@redhat.com>
233933
Date: Fri, 12 Apr 2019 13:40:59 +0200
233933
Subject: [PATCH 129/141] core: handle memory accounting correctly
233933
233933
When a translator stops, memory accounting for that translator is not
233933
destroyed (because there could remain memory allocated that references
233933
it), but mutexes that coordinate updates of memory accounting were
233933
destroyed. This caused incorrect memory accounting and even crashes in
233933
debug mode.
233933
233933
This patch also fixes some other things:
233933
233933
* Reduce the number of atomic operations needed to manage memory
233933
  accounting.
233933
* Correctly account memory when realloc() is used.
233933
* Merge two critical sections into one.
233933
* Cleaned the code a bit.
233933
233933
Upstream patch:
233933
> Change-Id: Id5eaee7338729b9bc52c931815ca3ff1e5a7dcc8
233933
> Upstream patch link : https://review.gluster.org/#/c/glusterfs/+/22554/
233933
> BUG: 1659334
233933
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
233933
233933
Change-Id: Id5eaee7338729b9bc52c931815ca3ff1e5a7dcc8
233933
Fixes: bz#1702270
233933
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
233933
Reviewed-on: https://code.engineering.redhat.com/gerrit/169325
233933
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
233933
Tested-by: RHGS Build Bot <nigelb@redhat.com>
233933
---
233933
 libglusterfs/src/glusterfs/xlator.h |   2 +
233933
 libglusterfs/src/libglusterfs.sym   |   1 +
233933
 libglusterfs/src/mem-pool.c         | 193 ++++++++++++++++--------------------
233933
 libglusterfs/src/xlator.c           |  23 +++--
233933
 4 files changed, 105 insertions(+), 114 deletions(-)
233933
233933
diff --git a/libglusterfs/src/glusterfs/xlator.h b/libglusterfs/src/glusterfs/xlator.h
233933
index 06152ec..8998976 100644
233933
--- a/libglusterfs/src/glusterfs/xlator.h
233933
+++ b/libglusterfs/src/glusterfs/xlator.h
233933
@@ -1035,6 +1035,8 @@ gf_boolean_t
233933
 loc_is_nameless(loc_t *loc);
233933
 int
233933
 xlator_mem_acct_init(xlator_t *xl, int num_types);
233933
+void
233933
+xlator_mem_acct_unref(struct mem_acct *mem_acct);
233933
 int
233933
 is_gf_log_command(xlator_t *trans, const char *name, char *value);
233933
 int
233933
diff --git a/libglusterfs/src/libglusterfs.sym b/libglusterfs/src/libglusterfs.sym
233933
index fa2025e..cf5757c 100644
233933
--- a/libglusterfs/src/libglusterfs.sym
233933
+++ b/libglusterfs/src/libglusterfs.sym
233933
@@ -1093,6 +1093,7 @@ xlator_foreach
233933
 xlator_foreach_depth_first
233933
 xlator_init
233933
 xlator_mem_acct_init
233933
+xlator_mem_acct_unref
233933
 xlator_notify
233933
 xlator_option_info_list
233933
 xlator_option_init_bool
233933
diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c
233933
index 34cb87a..3934a78 100644
233933
--- a/libglusterfs/src/mem-pool.c
233933
+++ b/libglusterfs/src/mem-pool.c
233933
@@ -35,61 +35,92 @@ gf_mem_acct_enable_set(void *data)
233933
     return;
233933
 }
233933
 
233933
-int
233933
-gf_mem_set_acct_info(xlator_t *xl, char **alloc_ptr, size_t size, uint32_t type,
233933
-                     const char *typestr)
233933
+static void *
233933
+gf_mem_header_prepare(struct mem_header *header, size_t size)
233933
 {
233933
-    void *ptr = NULL;
233933
-    struct mem_header *header = NULL;
233933
+    void *ptr;
233933
 
233933
-    if (!alloc_ptr)
233933
-        return -1;
233933
+    header->size = size;
233933
 
233933
-    ptr = *alloc_ptr;
233933
+    ptr = header + 1;
233933
 
233933
-    GF_ASSERT(xl != NULL);
233933
+    /* data follows in this gap of 'size' bytes */
233933
+    *(uint32_t *)(ptr + size) = GF_MEM_TRAILER_MAGIC;
233933
 
233933
-    GF_ASSERT(xl->mem_acct != NULL);
233933
+    return ptr;
233933
+}
233933
 
233933
-    GF_ASSERT(type <= xl->mem_acct->num_types);
233933
+static void *
233933
+gf_mem_set_acct_info(struct mem_acct *mem_acct, struct mem_header *header,
233933
+                     size_t size, uint32_t type, const char *typestr)
233933
+{
233933
+    struct mem_acct_rec *rec = NULL;
233933
+    bool new_ref = false;
233933
 
233933
-    LOCK(&xl->mem_acct->rec[type].lock);
233933
-    {
233933
-        if (!xl->mem_acct->rec[type].typestr)
233933
-            xl->mem_acct->rec[type].typestr = typestr;
233933
-        xl->mem_acct->rec[type].size += size;
233933
-        xl->mem_acct->rec[type].num_allocs++;
233933
-        xl->mem_acct->rec[type].total_allocs++;
233933
-        xl->mem_acct->rec[type].max_size = max(xl->mem_acct->rec[type].max_size,
233933
-                                               xl->mem_acct->rec[type].size);
233933
-        xl->mem_acct->rec[type].max_num_allocs = max(
233933
-            xl->mem_acct->rec[type].max_num_allocs,
233933
-            xl->mem_acct->rec[type].num_allocs);
233933
-    }
233933
-    UNLOCK(&xl->mem_acct->rec[type].lock);
233933
+    if (mem_acct != NULL) {
233933
+        GF_ASSERT(type <= mem_acct->num_types);
233933
 
233933
-    GF_ATOMIC_INC(xl->mem_acct->refcnt);
233933
+        rec = &mem_acct->rec[type];
233933
+        LOCK(&rec->lock);
233933
+        {
233933
+            if (!rec->typestr) {
233933
+                rec->typestr = typestr;
233933
+            }
233933
+            rec->size += size;
233933
+            new_ref = (rec->num_allocs == 0);
233933
+            rec->num_allocs++;
233933
+            rec->total_allocs++;
233933
+            rec->max_size = max(rec->max_size, rec->size);
233933
+            rec->max_num_allocs = max(rec->max_num_allocs, rec->num_allocs);
233933
+
233933
+#ifdef DEBUG
233933
+            list_add(&header->acct_list, &rec->obj_list);
233933
+#endif
233933
+        }
233933
+        UNLOCK(&rec->lock);
233933
+
233933
+        /* We only take a reference for each memory type used, not for each
233933
+         * allocation. This minimizes the use of atomic operations. */
233933
+        if (new_ref) {
233933
+            GF_ATOMIC_INC(mem_acct->refcnt);
233933
+        }
233933
+    }
233933
 
233933
-    header = (struct mem_header *)ptr;
233933
     header->type = type;
233933
-    header->size = size;
233933
-    header->mem_acct = xl->mem_acct;
233933
+    header->mem_acct = mem_acct;
233933
     header->magic = GF_MEM_HEADER_MAGIC;
233933
 
233933
+    return gf_mem_header_prepare(header, size);
233933
+}
233933
+
233933
+static void *
233933
+gf_mem_update_acct_info(struct mem_acct *mem_acct, struct mem_header *header,
233933
+                        size_t size)
233933
+{
233933
+    struct mem_acct_rec *rec = NULL;
233933
+
233933
+    if (mem_acct != NULL) {
233933
+        rec = &mem_acct->rec[header->type];
233933
+        LOCK(&rec->lock);
233933
+        {
233933
+            rec->size += size - header->size;
233933
+            rec->total_allocs++;
233933
+            rec->max_size = max(rec->max_size, rec->size);
233933
+
233933
 #ifdef DEBUG
233933
-    INIT_LIST_HEAD(&header->acct_list);
233933
-    LOCK(&xl->mem_acct->rec[type].lock);
233933
-    {
233933
-        list_add(&header->acct_list, &(xl->mem_acct->rec[type].obj_list));
233933
-    }
233933
-    UNLOCK(&xl->mem_acct->rec[type].lock);
233933
+            /* The old 'header' already was present in 'obj_list', but
233933
+             * realloc() could have changed its address. We need to remove
233933
+             * the old item from the list and add the new one. This can be
233933
+             * done this way because list_move() doesn't use the pointers
233933
+             * to the old location (which are not valid anymore) already
233933
+             * present in the list, it simply overwrites them. */
233933
+            list_move(&header->acct_list, &rec->obj_list);
233933
 #endif
233933
-    ptr += sizeof(struct mem_header);
233933
-    /* data follows in this gap of 'size' bytes */
233933
-    *(uint32_t *)(ptr + size) = GF_MEM_TRAILER_MAGIC;
233933
+        }
233933
+        UNLOCK(&rec->lock);
233933
+    }
233933
 
233933
-    *alloc_ptr = ptr;
233933
-    return 0;
233933
+    return gf_mem_header_prepare(header, size);
233933
 }
233933
 
233933
 void *
233933
@@ -97,7 +128,7 @@ __gf_calloc(size_t nmemb, size_t size, uint32_t type, const char *typestr)
233933
 {
233933
     size_t tot_size = 0;
233933
     size_t req_size = 0;
233933
-    char *ptr = NULL;
233933
+    void *ptr = NULL;
233933
     xlator_t *xl = NULL;
233933
 
233933
     if (!THIS->ctx->mem_acct_enable)
233933
@@ -114,16 +145,15 @@ __gf_calloc(size_t nmemb, size_t size, uint32_t type, const char *typestr)
233933
         gf_msg_nomem("", GF_LOG_ALERT, tot_size);
233933
         return NULL;
233933
     }
233933
-    gf_mem_set_acct_info(xl, &ptr, req_size, type, typestr);
233933
 
233933
-    return (void *)ptr;
233933
+    return gf_mem_set_acct_info(xl->mem_acct, ptr, req_size, type, typestr);
233933
 }
233933
 
233933
 void *
233933
 __gf_malloc(size_t size, uint32_t type, const char *typestr)
233933
 {
233933
     size_t tot_size = 0;
233933
-    char *ptr = NULL;
233933
+    void *ptr = NULL;
233933
     xlator_t *xl = NULL;
233933
 
233933
     if (!THIS->ctx->mem_acct_enable)
233933
@@ -138,84 +168,32 @@ __gf_malloc(size_t size, uint32_t type, const char *typestr)
233933
         gf_msg_nomem("", GF_LOG_ALERT, tot_size);
233933
         return NULL;
233933
     }
233933
-    gf_mem_set_acct_info(xl, &ptr, size, type, typestr);
233933
 
233933
-    return (void *)ptr;
233933
+    return gf_mem_set_acct_info(xl->mem_acct, ptr, size, type, typestr);
233933
 }
233933
 
233933
 void *
233933
 __gf_realloc(void *ptr, size_t size)
233933
 {
233933
     size_t tot_size = 0;
233933
-    char *new_ptr;
233933
-    struct mem_header *old_header = NULL;
233933
-    struct mem_header *new_header = NULL;
233933
-    struct mem_header tmp_header;
233933
+    struct mem_header *header = NULL;
233933
 
233933
     if (!THIS->ctx->mem_acct_enable)
233933
         return REALLOC(ptr, size);
233933
 
233933
     REQUIRE(NULL != ptr);
233933
 
233933
-    old_header = (struct mem_header *)(ptr - GF_MEM_HEADER_SIZE);
233933
-    GF_ASSERT(old_header->magic == GF_MEM_HEADER_MAGIC);
233933
-    tmp_header = *old_header;
233933
-
233933
-#ifdef DEBUG
233933
-    int type = 0;
233933
-    size_t copy_size = 0;
233933
-
233933
-    /* Making these changes for realloc is not straightforward. So
233933
-     * I am simulating realloc using calloc and free
233933
-     */
233933
-
233933
-    type = tmp_header.type;
233933
-    new_ptr = __gf_calloc(1, size, type,
233933
-                          tmp_header.mem_acct->rec[type].typestr);
233933
-    if (new_ptr) {
233933
-        copy_size = (size > tmp_header.size) ? tmp_header.size : size;
233933
-        memcpy(new_ptr, ptr, copy_size);
233933
-        __gf_free(ptr);
233933
-    }
233933
-
233933
-    /* This is not quite what the man page says should happen */
233933
-    return new_ptr;
233933
-#endif
233933
+    header = (struct mem_header *)(ptr - GF_MEM_HEADER_SIZE);
233933
+    GF_ASSERT(header->magic == GF_MEM_HEADER_MAGIC);
233933
 
233933
     tot_size = size + GF_MEM_HEADER_SIZE + GF_MEM_TRAILER_SIZE;
233933
-    new_ptr = realloc(old_header, tot_size);
233933
-    if (!new_ptr) {
233933
+    header = realloc(header, tot_size);
233933
+    if (!header) {
233933
         gf_msg_nomem("", GF_LOG_ALERT, tot_size);
233933
         return NULL;
233933
     }
233933
 
233933
-    /*
233933
-     * We used to pass (char **)&ptr as the second
233933
-     * argument after the value of realloc was saved
233933
-     * in ptr, but the compiler warnings complained
233933
-     * about the casting to and forth from void ** to
233933
-     * char **.
233933
-     * TBD: it would be nice to adjust the memory accounting info here,
233933
-     * but calling gf_mem_set_acct_info here is wrong because it bumps
233933
-     * up counts as though this is a new allocation - which it's not.
233933
-     * The consequence of doing nothing here is only that the sizes will be
233933
-     * wrong, but at least the counts won't be.
233933
-    uint32_t           type = 0;
233933
-    xlator_t          *xl = NULL;
233933
-    type = header->type;
233933
-    xl = (xlator_t *) header->xlator;
233933
-    gf_mem_set_acct_info (xl, &new_ptr, size, type, NULL);
233933
-     */
233933
-
233933
-    new_header = (struct mem_header *)new_ptr;
233933
-    *new_header = tmp_header;
233933
-    new_header->size = size;
233933
-
233933
-    new_ptr += sizeof(struct mem_header);
233933
-    /* data follows in this gap of 'size' bytes */
233933
-    *(uint32_t *)(new_ptr + size) = GF_MEM_TRAILER_MAGIC;
233933
-
233933
-    return (void *)new_ptr;
233933
+    return gf_mem_update_acct_info(header->mem_acct, header, size);
233933
 }
233933
 
233933
 int
233933
@@ -321,6 +299,7 @@ __gf_free(void *free_ptr)
233933
     void *ptr = NULL;
233933
     struct mem_acct *mem_acct;
233933
     struct mem_header *header = NULL;
233933
+    bool last_ref = false;
233933
 
233933
     if (!THIS->ctx->mem_acct_enable) {
233933
         FREE(free_ptr);
233933
@@ -352,16 +331,18 @@ __gf_free(void *free_ptr)
233933
         mem_acct->rec[header->type].num_allocs--;
233933
         /* If all the instances are freed up then ensure typestr is set
233933
          * to NULL */
233933
-        if (!mem_acct->rec[header->type].num_allocs)
233933
+        if (!mem_acct->rec[header->type].num_allocs) {
233933
+            last_ref = true;
233933
             mem_acct->rec[header->type].typestr = NULL;
233933
+        }
233933
 #ifdef DEBUG
233933
         list_del(&header->acct_list);
233933
 #endif
233933
     }
233933
     UNLOCK(&mem_acct->rec[header->type].lock);
233933
 
233933
-    if (GF_ATOMIC_DEC(mem_acct->refcnt) == 0) {
233933
-        FREE(mem_acct);
233933
+    if (last_ref) {
233933
+        xlator_mem_acct_unref(mem_acct);
233933
     }
233933
 
233933
 free:
233933
diff --git a/libglusterfs/src/xlator.c b/libglusterfs/src/xlator.c
233933
index 5d6f8d2..022c3ed 100644
233933
--- a/libglusterfs/src/xlator.c
233933
+++ b/libglusterfs/src/xlator.c
233933
@@ -736,6 +736,19 @@ xlator_mem_acct_init(xlator_t *xl, int num_types)
233933
 }
233933
 
233933
 void
233933
+xlator_mem_acct_unref(struct mem_acct *mem_acct)
233933
+{
233933
+    uint32_t i;
233933
+
233933
+    if (GF_ATOMIC_DEC(mem_acct->refcnt) == 0) {
233933
+        for (i = 0; i < mem_acct->num_types; i++) {
233933
+            LOCK_DESTROY(&(mem_acct->rec[i].lock));
233933
+        }
233933
+        FREE(mem_acct);
233933
+    }
233933
+}
233933
+
233933
+void
233933
 xlator_tree_fini(xlator_t *xl)
233933
 {
233933
     xlator_t *top = NULL;
233933
@@ -766,7 +779,6 @@ xlator_list_destroy(xlator_list_t *list)
233933
 int
233933
 xlator_memrec_free(xlator_t *xl)
233933
 {
233933
-    uint32_t i = 0;
233933
     struct mem_acct *mem_acct = NULL;
233933
 
233933
     if (!xl) {
233933
@@ -775,13 +787,8 @@ xlator_memrec_free(xlator_t *xl)
233933
     mem_acct = xl->mem_acct;
233933
 
233933
     if (mem_acct) {
233933
-        for (i = 0; i < mem_acct->num_types; i++) {
233933
-            LOCK_DESTROY(&(mem_acct->rec[i].lock));
233933
-        }
233933
-        if (GF_ATOMIC_DEC(mem_acct->refcnt) == 0) {
233933
-            FREE(mem_acct);
233933
-            xl->mem_acct = NULL;
233933
-        }
233933
+        xlator_mem_acct_unref(mem_acct);
233933
+        xl->mem_acct = NULL;
233933
     }
233933
 
233933
     return 0;
233933
-- 
233933
1.8.3.1
233933