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