You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by je...@apache.org on 2023/01/18 08:02:39 UTC

[nuttx] branch master updated (01c96a211f -> b362f18d6a)

This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


    from 01c96a211f touchscreen.h: move the #ifdef CONFIG_INPUT position
     new 288725a5f8 mempool:fix a bug when use smp on mempool backtrace
     new d846004533 mempool:use two-dimensional array avoid competition
     new b362f18d6a mempool:Calibration total memory statistics

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 include/nuttx/mm/mempool.h    |  33 ++++++++-----
 mm/Kconfig                    |   7 +++
 mm/mempool/mempool.c          |  19 ++++++--
 mm/mempool/mempool_multiple.c | 110 +++++++++++++++++++++++++++---------------
 mm/mm_heap/mm_initialize.c    |   8 +--
 mm/tlsf/mm_tlsf.c             |   8 +--
 6 files changed, 125 insertions(+), 60 deletions(-)


[nuttx] 02/03: mempool:use two-dimensional array avoid competition

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit d846004533308f6b43fc0059f81e35a2fc5e32ea
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Thu Dec 22 21:18:09 2022 +0800

    mempool:use two-dimensional array avoid competition
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 include/nuttx/mm/mempool.h    |  18 +++----
 mm/Kconfig                    |   7 +++
 mm/mempool/mempool_multiple.c | 108 +++++++++++++++++++++++++++---------------
 mm/mm_heap/mm_initialize.c    |   7 +--
 mm/tlsf/mm_tlsf.c             |   7 +--
 5 files changed, 95 insertions(+), 52 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 7af6558728..4c7aeaeb5c 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -281,13 +281,14 @@ void mempool_procfs_unregister(FAR struct mempool_procfs_entry_s *entry);
  *   relationship between the each block size of mempool in multiple mempool.
  *
  * Input Parameters:
- *   name        - The name of memory pool.
- *   poolsize    - The block size array for pools in multiples pool.
- *   npools      - How many pools in multiples pool.
- *   alloc       - The alloc memory function for multiples pool.
- *   free        - The free memory function for multiples pool.
- *   arg         - The alloc & free memory fuctions used arg.
- *   expandsize  - The expend mempry for all pools in multiples pool.
+ *   name            - The name of memory pool.
+ *   poolsize        - The block size array for pools in multiples pool.
+ *   npools          - How many pools in multiples pool.
+ *   alloc           - The alloc memory function for multiples pool.
+ *   free            - The free memory function for multiples pool.
+ *   arg             - The alloc & free memory fuctions used arg.
+ *   expandsize      - The expend mempry for all pools in multiples pool.
+ *   dict_expendsize - The expend number for multiple dictnoary
  *
  * Returned Value:
  *   Return an initialized multiple pool pointer on success,
@@ -302,7 +303,8 @@ mempool_multiple_init(FAR const char *name,
                       FAR size_t *poolsize, size_t npools,
                       mempool_multiple_alloc_t alloc,
                       mempool_multiple_free_t free,
-                      FAR void *arg, size_t expandsize);
+                      FAR void *arg, size_t expandsize,
+                      size_t dict_expendsize);
 
 /****************************************************************************
  * Name: mempool_multiple_alloc
diff --git a/mm/Kconfig b/mm/Kconfig
index 8b572f142b..5dc5c9f85d 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -196,6 +196,13 @@ config MM_HEAP_MEMPOOL_EXPAND
 		This size describes the size of each expansion of each memory
 		pool with insufficient memory in the multi-level memory pool.
 
+config MM_HEAP_MEMPOOL_DICTIONARY_EXPAND
+	int "The expand size for multiple mempool's dictonary"
+	default MM_HEAP_MEMPOOL_EXPAND
+	depends on MM_HEAP_MEMPOOL_THRESHOLD != 0
+	---help---
+		This size describes the multiple mempool dictonary expend.
+
 config FS_PROCFS_EXCLUDE_MEMPOOL
 	bool "Exclude mempool"
 	default DEFAULT_SMALL
diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c
index d03b541f25..c8516ae79f 100644
--- a/mm/mempool/mempool_multiple.c
+++ b/mm/mempool/mempool_multiple.c
@@ -22,6 +22,8 @@
  * Included Files
  ****************************************************************************/
 
+#include <strings.h>
+#include <nuttx/mutex.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/mm/mempool.h>
 
@@ -73,9 +75,11 @@ struct mempool_multiple_s
    * expansion, and find the mempool by adding an index
    */
 
-  size_t                   dict_alloc;
-  size_t                   dict_used;
-  FAR struct mpool_dict_s *dict;
+  mutex_t                   dict_lock;
+  size_t                    dict_used;
+  size_t                    dict_col_num_log2;
+  size_t                    dict_row_num;
+  FAR struct mpool_dict_s **dict;
 };
 
 /****************************************************************************
@@ -133,23 +137,8 @@ static FAR void *mempool_multiple_alloc_callback(FAR struct mempool_s *pool,
 {
   FAR struct mempool_multiple_s *mpool = pool->priv;
   FAR void *ret;
-
-  if (mpool->dict_used >= mpool->dict_alloc)
-    {
-      ret = mpool->alloc(mpool->arg, sizeof(uintptr_t),
-                         mpool->dict_alloc *
-                         sizeof(struct mpool_dict_s) * 2);
-      if (ret == NULL)
-        {
-          return NULL;
-        }
-
-      memcpy(ret, mpool->dict,
-             mpool->dict_alloc * sizeof(struct mpool_dict_s));
-      mpool->free(mpool->arg, mpool->dict);
-      mpool->dict = ret;
-      mpool->dict_alloc *= 2;
-    }
+  size_t row;
+  size_t col;
 
   ret = mpool->alloc(mpool->arg, mpool->expandsize,
                      mpool->minpoolsize + size);
@@ -158,10 +147,27 @@ static FAR void *mempool_multiple_alloc_callback(FAR struct mempool_s *pool,
       return NULL;
     }
 
-  mpool->dict[mpool->dict_used].pool = pool;
-  mpool->dict[mpool->dict_used].addr = ret;
-  mpool->dict[mpool->dict_used].size = mpool->minpoolsize + size;
+  nxmutex_lock(&mpool->dict_lock);
+  row = mpool->dict_used >> mpool->dict_col_num_log2;
+
+  /* There is no new pointer address to store the dictionarys */
+
+  DEBUGASSERT(mpool->dict_row_num > row);
+
+  col = mpool->dict_used - (row << mpool->dict_col_num_log2);
+
+  if (mpool->dict[row] == NULL)
+    {
+      mpool->dict[row] = mpool->alloc(mpool->arg, sizeof(uintptr_t),
+                                      (1 << mpool->dict_col_num_log2) *
+                                      sizeof(struct mpool_dict_s));
+    }
+
+  mpool->dict[row][col].pool = pool;
+  mpool->dict[row][col].addr = ret;
+  mpool->dict[row][col].size = mpool->minpoolsize + size;
   *(FAR size_t *)ret = mpool->dict_used++;
+  nxmutex_unlock(&mpool->dict_lock);
   return (FAR char *)ret + mpool->minpoolsize;
 }
 
@@ -194,6 +200,8 @@ mempool_multiple_get_dict(FAR struct mempool_multiple_s *mpool,
 {
   FAR void *addr;
   size_t index;
+  size_t row;
+  size_t col;
 
   if (mpool == NULL || blk == NULL)
     {
@@ -208,13 +216,16 @@ mempool_multiple_get_dict(FAR struct mempool_multiple_s *mpool,
       return NULL;
     }
 
-  if (mpool->dict[index].addr != addr ||
-      blk - addr >= mpool->dict[index].size)
+  row = index >> mpool->dict_col_num_log2;
+  col = index - (row << mpool->dict_col_num_log2);
+  if (mpool->dict[row] == NULL ||
+      mpool->dict[row][col].addr != addr ||
+      blk - addr >= mpool->dict[row][col].size)
     {
       return NULL;
     }
 
-  return &mpool->dict[index];
+  return &mpool->dict[row][col];
 }
 
 /****************************************************************************
@@ -235,14 +246,14 @@ mempool_multiple_get_dict(FAR struct mempool_multiple_s *mpool,
  *   relationship between the each block size of mempool in multiple mempool.
  *
  * Input Parameters:
- *   name        - The name of memory pool.
- *   poolsize    - The block size array for pools in multiples pool.
- *   npools      - How many pools in multiples pool.
- *   alloc       - The alloc memory function for multiples pool.
- *   free        - The free memory function for multiples pool.
- *   arg         - The alloc & free memory fuctions used arg.
- *   expandsize  - The expend mempry for all pools in multiples pool.
- *
+ *   name            - The name of memory pool.
+ *   poolsize        - The block size array for pools in multiples pool.
+ *   npools          - How many pools in multiples pool.
+ *   alloc           - The alloc memory function for multiples pool.
+ *   free            - The free memory function for multiples pool.
+ *   arg             - The alloc & free memory fuctions used arg.
+ *   expandsize      - The expend mempry for all pools in multiples pool.
+ *   dict_expendsize -  The expend size for multiple dictnoary
  * Returned Value:
  *   Return an initialized multiple pool pointer on success,
  *   otherwise NULL is returned.
@@ -254,7 +265,8 @@ mempool_multiple_init(FAR const char *name,
                       FAR size_t *poolsize, size_t npools,
                       mempool_multiple_alloc_t alloc,
                       mempool_multiple_free_t free,
-                      FAR void *arg, size_t expandsize)
+                      FAR void *arg, size_t expandsize,
+                      size_t dict_expendsize)
 {
   FAR struct mempool_multiple_s *mpool;
   FAR struct mempool_s *pools;
@@ -335,15 +347,22 @@ mempool_multiple_init(FAR const char *name,
         }
     }
 
-  mpool->dict_alloc = maxpoolszie / sizeof(struct mpool_dict_s) + 1;
   mpool->dict_used = 0;
-  mpool->dict = alloc(arg, sizeof(uintptr_t),
-                      mpool->dict_alloc * sizeof(struct mpool_dict_s));
+  mpool->dict_col_num_log2 = fls(dict_expendsize /
+                                 sizeof(struct mpool_dict_s));
+
+  mpool->dict_row_num = dict_expendsize / sizeof(struct mpool_dict_s *);
+  mpool->dict = alloc(arg, sizeof(struct mpool_dict_s *),
+                      sizeof(struct mpool_dict_s *) * mpool->dict_row_num);
   if (mpool->dict == NULL)
     {
       goto err_with_pools;
     }
 
+  memset(mpool->dict, 0,
+         mpool->dict_row_num * sizeof(struct mpool_dict_s *));
+  nxmutex_init(&mpool->dict_lock);
+
   return mpool;
 
 err_with_pools:
@@ -643,7 +662,20 @@ void mempool_multiple_deinit(FAR struct mempool_multiple_s *mpool)
       DEBUGVERIFY(mempool_deinit(mpool->pools + i));
     }
 
+  for (i = 0; i < mpool->dict_row_num; i++)
+    {
+      if (mpool->dict[i] != NULL)
+        {
+          mpool->free(mpool->arg, mpool->dict[i]);
+        }
+      else
+        {
+          break;
+        }
+    }
+
   mpool->free(mpool->arg, mpool->dict);
   mpool->free(mpool->arg, mpool->pools);
   mpool->free(mpool->arg, mpool);
+  nxmutex_destroy(&mpool->dict_lock);
 }
diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c
index 533b5d8231..763c9387a2 100644
--- a/mm/mm_heap/mm_initialize.c
+++ b/mm/mm_heap/mm_initialize.c
@@ -256,9 +256,10 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
     }
 
   heap->mm_mpool = mempool_multiple_init(name, poolsize, MEMPOOL_NPOOLS,
-                                    (mempool_multiple_alloc_t)mm_memalign,
-                                    (mempool_multiple_free_t)mm_free, heap,
-                                    CONFIG_MM_HEAP_MEMPOOL_EXPAND);
+                                  (mempool_multiple_alloc_t)mm_memalign,
+                                  (mempool_multiple_free_t)mm_free, heap,
+                                  CONFIG_MM_HEAP_MEMPOOL_EXPAND,
+                                  CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND);
 #endif
 
   return heap;
diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c
index 0bd36e20dc..308fc1ca2c 100644
--- a/mm/tlsf/mm_tlsf.c
+++ b/mm/tlsf/mm_tlsf.c
@@ -801,9 +801,10 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
     }
 
   heap->mm_mpool = mempool_multiple_init(name, poolsize, MEMPOOL_NPOOLS,
-                                    (mempool_multiple_alloc_t)mm_memalign,
-                                    (mempool_multiple_free_t)mm_free, heap,
-                                    CONFIG_MM_HEAP_MEMPOOL_EXPAND);
+                                  (mempool_multiple_alloc_t)mm_memalign,
+                                  (mempool_multiple_free_t)mm_free, heap,
+                                  CONFIG_MM_HEAP_MEMPOOL_EXPAND,
+                                  CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND);
 #endif
 
   return heap;


[nuttx] 03/03: mempool:Calibration total memory statistics

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit b362f18d6a67b7ba28130bed44ab8d264eded4b7
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Tue Jan 10 18:10:28 2023 +0800

    mempool:Calibration total memory statistics
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 include/nuttx/mm/mempool.h    | 19 ++++++++++++-------
 mm/mempool/mempool.c          | 15 +++++++++++++++
 mm/mempool/mempool_multiple.c |  6 ++++--
 mm/mm_heap/mm_initialize.c    |  3 ++-
 mm/tlsf/mm_tlsf.c             |  3 ++-
 5 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 4c7aeaeb5c..8e4236cedc 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -75,6 +75,9 @@ struct mempool_s
   size_t     expandsize;    /* The size of expand block every time for mempool */
   bool       wait;          /* The flag of need to wait when mempool is empty */
   FAR void  *priv;          /* This pointer is used to store the user's private data */
+  bool       calibrate;     /* The flag is use expend memory calibration
+                             * real memory usage
+                             */
   mempool_alloc_t alloc;    /* The alloc function for mempool */
   mempool_free_t  free;     /* The free function for mempool */
 
@@ -85,12 +88,14 @@ struct mempool_s
   sq_queue_t iqueue;  /* The free block queue in interrupt mempool */
   sq_queue_t equeue;  /* The expand block queue for normal mempool */
 #if CONFIG_MM_BACKTRACE >= 0
-  struct list_node alist;   /* The used block list in mempool */
+  struct list_node alist;     /* The used block list in mempool */
 #else
-  size_t           nalloc;  /* The number of used block in mempool */
+  size_t           nalloc;    /* The number of used block in mempool */
 #endif
-  spinlock_t       lock;    /* The protect lock to mempool */
-  sem_t            waitsem; /* The semaphore of waiter get free block */
+  spinlock_t       lock;      /* The protect lock to mempool */
+  sem_t            waitsem;   /* The semaphore of waiter get free block */
+  size_t           nexpend;   /* The number of expend memory for mempool */
+  size_t           totalsize; /* Total size of the expend for mempoll */
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
   struct mempool_procfs_entry_s procfs; /* The entry of procfs */
 #endif
@@ -288,8 +293,8 @@ void mempool_procfs_unregister(FAR struct mempool_procfs_entry_s *entry);
  *   free            - The free memory function for multiples pool.
  *   arg             - The alloc & free memory fuctions used arg.
  *   expandsize      - The expend mempry for all pools in multiples pool.
- *   dict_expendsize - The expend number for multiple dictnoary
- *
+ *   dict_expendsize - The expend size for multiple dictnoary.
+ *   calibrate       - Whether to calibrate when counting memory usage.
  * Returned Value:
  *   Return an initialized multiple pool pointer on success,
  *   otherwise NULL is returned.
@@ -304,7 +309,7 @@ mempool_multiple_init(FAR const char *name,
                       mempool_multiple_alloc_t alloc,
                       mempool_multiple_free_t free,
                       FAR void *arg, size_t expandsize,
-                      size_t dict_expendsize);
+                      size_t dict_expendsize, bool calibrate);
 
 /****************************************************************************
  * Name: mempool_multiple_alloc
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index 10186c2aa9..7a1ea154ff 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -154,6 +154,8 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
   sq_init(&pool->queue);
   sq_init(&pool->iqueue);
   sq_init(&pool->equeue);
+  pool->nexpend = 0;
+  pool->totalsize = 0;
 
 #if CONFIG_MM_BACKTRACE >= 0
   list_initialize(&pool->alist);
@@ -173,6 +175,8 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
           return -ENOMEM;
         }
 
+      pool->nexpend++;
+      pool->totalsize += size;
       mempool_add_queue(&pool->iqueue, pool->ibase, ninterrupt, blocksize);
       kasan_poison(pool->ibase, size);
     }
@@ -194,6 +198,8 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
           return -ENOMEM;
         }
 
+      pool->nexpend++;
+      pool->totalsize += size;
       mempool_add_queue(&pool->queue, base, ninitial, blocksize);
       sq_addlast((FAR sq_entry_t *)(base + ninitial * blocksize),
                   &pool->equeue);
@@ -273,6 +279,8 @@ retry:
                   return NULL;
                 }
 
+              pool->nexpend++;
+              pool->totalsize += size;
               kasan_poison(base, size);
               flags = spin_lock_irqsave(&pool->lock);
               mempool_add_queue(&pool->queue, base, nexpand, blocksize);
@@ -425,6 +433,11 @@ int mempool_info_task(FAR struct mempool_s *pool,
 
       info->aordblks += count;
       info->uordblks += count * pool->blocksize;
+      if (pool->calibrate)
+        {
+          info->aordblks -= pool->nexpend;
+          info->uordblks -= pool->totalsize;
+        }
     }
   else if (info->pid == -1)
     {
@@ -436,6 +449,8 @@ int mempool_info_task(FAR struct mempool_s *pool,
 
       info->aordblks += count;
       info->uordblks += count * pool->blocksize;
+      info->aordblks -= pool->nexpend;
+      info->uordblks -= pool->totalsize;
     }
 #if CONFIG_MM_BACKTRACE >= 0
   else
diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c
index c8516ae79f..0ca738be06 100644
--- a/mm/mempool/mempool_multiple.c
+++ b/mm/mempool/mempool_multiple.c
@@ -253,7 +253,8 @@ mempool_multiple_get_dict(FAR struct mempool_multiple_s *mpool,
  *   free            - The free memory function for multiples pool.
  *   arg             - The alloc & free memory fuctions used arg.
  *   expandsize      - The expend mempry for all pools in multiples pool.
- *   dict_expendsize -  The expend size for multiple dictnoary
+ *   dict_expendsize - The expend size for multiple dictnoary.
+ *   calibrate       - Whether to calibrate when counting memory usage.
  * Returned Value:
  *   Return an initialized multiple pool pointer on success,
  *   otherwise NULL is returned.
@@ -266,7 +267,7 @@ mempool_multiple_init(FAR const char *name,
                       mempool_multiple_alloc_t alloc,
                       mempool_multiple_free_t free,
                       FAR void *arg, size_t expandsize,
-                      size_t dict_expendsize)
+                      size_t dict_expendsize, bool calibrate)
 {
   FAR struct mempool_multiple_s *mpool;
   FAR struct mempool_s *pools;
@@ -325,6 +326,7 @@ mempool_multiple_init(FAR const char *name,
       pools[i].priv = mpool;
       pools[i].alloc = mempool_multiple_alloc_callback;
       pools[i].free = mempool_multiple_free_callback;
+      pools[i].calibrate = calibrate;
 
       ret = mempool_init(pools + i, name);
       if (ret < 0)
diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c
index 763c9387a2..830bf15389 100644
--- a/mm/mm_heap/mm_initialize.c
+++ b/mm/mm_heap/mm_initialize.c
@@ -259,7 +259,8 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
                                   (mempool_multiple_alloc_t)mm_memalign,
                                   (mempool_multiple_free_t)mm_free, heap,
                                   CONFIG_MM_HEAP_MEMPOOL_EXPAND,
-                                  CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND);
+                                  CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND,
+                                  true);
 #endif
 
   return heap;
diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c
index 308fc1ca2c..7e3ab3093b 100644
--- a/mm/tlsf/mm_tlsf.c
+++ b/mm/tlsf/mm_tlsf.c
@@ -804,7 +804,8 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
                                   (mempool_multiple_alloc_t)mm_memalign,
                                   (mempool_multiple_free_t)mm_free, heap,
                                   CONFIG_MM_HEAP_MEMPOOL_EXPAND,
-                                  CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND);
+                                  CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND,
+                                  true);
 #endif
 
   return heap;


[nuttx] 01/03: mempool:fix a bug when use smp on mempool backtrace

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 288725a5f8b50fa8f8c32fb963dafca0467a1913
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Thu Dec 1 00:35:25 2022 +0800

    mempool:fix a bug when use smp on mempool backtrace
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 mm/mempool/mempool.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index d1ee35caf2..10186c2aa9 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -317,7 +317,7 @@ out_with_lock:
 
 void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
 {
-  irqstate_t flags;
+  irqstate_t flags = spin_lock_irqsave(&pool->lock);
 #if CONFIG_MM_BACKTRACE >= 0
   size_t blocksize =  ALIGN_UP(pool->blocksize +
                                sizeof(struct mempool_backtrace_s),
@@ -332,8 +332,6 @@ void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
   pool->nalloc--;
 #endif
 
-  flags = spin_lock_irqsave(&pool->lock);
-
   if (pool->interruptsize > blocksize)
     {
       if ((FAR char *)blk >= pool->ibase &&