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