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