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