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