Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Documentation/os/memory/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ per-task allocations, and with any of the ``CONFIG_MM_RECORD_*`` options
These diagnostics cover static initial storage, dynamically allocated
initial storage, expansion chunks and interrupt-reserved storage. The
initial and expansion chunks may have different sizes. Stack traces
additionally require ``CONFIG_MM_RECORD_STACK > 0`` and recording enabled
for the pool.
additionally require ``CONFIG_MM_RECORD_STACK`` (the depth is
``CONFIG_LIBC_BACKTRACE_DEPTH``) and recording enabled for the pool.

Granule Allocator
-----------------
Expand Down
6 changes: 3 additions & 3 deletions fs/procfs/fs_procfsmeminfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer,
#if CONFIG_MM_HEAP_BIGGEST_COUNT > 0
"/biggest"
#endif
#if CONFIG_MM_RECORD_STACK > 0
#ifdef CONFIG_MM_RECORD_STACK
"/on/off"
#endif
#ifdef CONFIG_MM_RECORD_PID
Expand All @@ -462,7 +462,7 @@ static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer,
#if CONFIG_MM_HEAP_BIGGEST_COUNT > 0
"biggest: dump allocated top n node\n"
#endif
#if CONFIG_MM_RECORD_STACK > 0
#ifdef CONFIG_MM_RECORD_STACK
"on/off: set backtrace enabled state\n"
#endif
#ifdef CONFIG_MM_RECORD_PID
Expand Down Expand Up @@ -512,7 +512,7 @@ static ssize_t memdump_write(FAR struct file *filep, FAR const char *buffer,
procfile = filep->f_priv;
DEBUGASSERT(procfile);

#if CONFIG_MM_RECORD_STACK > 0
#ifdef CONFIG_MM_RECORD_STACK
if (strcmp(buffer, "on") == 0)
{
for (entry = g_procfs_meminfo; entry != NULL; entry = entry->next)
Expand Down
2 changes: 1 addition & 1 deletion include/execinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void backtrace_dump(void);
#else
# define backtrace_record(skip) (NULL)
# define backtrace_remove(entry)
# define backtrace_get(entry, size) (*(size)=0)
# define backtrace_get(entry, size) (*(size) = 0, NULL)
# define backtrace_dump()
#endif

Expand Down
2 changes: 1 addition & 1 deletion include/nuttx/fs/procfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct procfs_meminfo_entry_s
struct mallinfo (*mallinfo)(FAR struct mm_heap_s *);
void (*memdump)(FAR struct mm_heap_s *,
FAR const struct mm_memdump_s *);
#if CONFIG_MM_RECORD_STACK > 0
#ifdef CONFIG_MM_RECORD_STACK

/* This is dynamic control flag whether to turn on backtrace in the heap,
* you can set it by /proc/memdump.
Expand Down
8 changes: 5 additions & 3 deletions include/nuttx/mm/mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct mempool_procfs_entry_s
{
FAR const char *name;
FAR struct mempool_procfs_entry_s *next;
#if CONFIG_MM_RECORD_STACK > 0
#ifdef CONFIG_MM_RECORD_STACK

/* This is dynamic control flag whether to turn on backtrace in the heap,
* you can set it by /proc/mempool.
Expand Down Expand Up @@ -162,12 +162,14 @@ struct mempool_record_s
*/
# ifdef CONFIG_MM_RECORD_PID
pid_t pid;
pid_t freepid;
# endif
# ifdef CONFIG_MM_RECORD_SEQNO
unsigned long seqno; /* The sequence of memory malloc */
# endif
# if CONFIG_MM_RECORD_STACK > 0
FAR void *backtrace[CONFIG_MM_RECORD_STACK];
# ifdef CONFIG_MM_RECORD_STACK
FAR void *stack;
FAR void *freestack;
# endif
};
#endif
Expand Down
2 changes: 1 addition & 1 deletion include/nuttx/mm/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ typedef CODE void (*mm_delaycb_t)(FAR void *mem);
#endif

#if defined(CONFIG_MM_RECORD_PID) || defined(CONFIG_MM_RECORD_SEQNO) || \
CONFIG_MM_RECORD_STACK > 0
defined(CONFIG_MM_RECORD_STACK)
# define CONFIG_MM_RECORD
#endif

Expand Down
5 changes: 3 additions & 2 deletions mm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ config MM_RECORD_SEQNO
Enable the sequence number record in memory block.

config MM_RECORD_STACK
int "Record backtrace depth for memory block"
default 0
bool "Record backtrace for memory block"
default n
depends on LIBC_BACKTRACE_DEPTH > 0
---help---
Config the depth of backtrace stack in memory block by specified
this config.
Expand Down
85 changes: 58 additions & 27 deletions mm/mempool/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ static inline void mempool_add_queue(FAR struct mempool_s *pool,
/* Usr mem layout after record payload, keep record when overlap. */

node = (FAR void *)mempool_get_block_from_record(record);
# ifdef CONFIG_MM_RECORD_STACK
record->stack = NULL;
record->freestack = NULL;
# endif
record->magic = MEMPOOL_MAGIC_FREE;
#endif
sq_addlast(node, queue);
Expand All @@ -141,36 +145,41 @@ static inline void mempool_add_queue(FAR struct mempool_s *pool,

#ifdef CONFIG_MM_RECORD
static inline void mempool_record(FAR struct mempool_s *pool,
FAR struct mempool_record_s *record)
FAR struct mempool_record_s *record,
unsigned int magic)
{
# if defined(CONFIG_MM_RECORD_STACK) || defined(CONFIG_MM_RECORD_PID)
pid_t pid = _SCHED_GETTID();
# ifdef CONFIG_MM_RECORD_STACK
FAR void **stack;
# endif
DEBUGASSERT(record->magic == MEMPOOL_MAGIC_FREE);
record->magic = MEMPOOL_MAGIC_ALLOC;

record->magic = magic;
# ifdef CONFIG_MM_RECORD_PID
record->pid = pid;
if (magic == MEMPOOL_MAGIC_ALLOC)
{
record->pid = _SCHED_GETTID();
}
else
{
record->freepid = _SCHED_GETTID();
}
# endif

MM_INCSEQNO(record);

# if CONFIG_MM_RECORD_STACK > 0
# ifdef CONFIG_MM_RECORD_STACK
stack = (magic == MEMPOOL_MAGIC_ALLOC ?
&record->stack : &record->freestack);
backtrace_remove(*stack);
# if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
if (pool->procfs.backtrace)
{
int result = sched_backtrace(pid, record->backtrace,
CONFIG_MM_RECORD_STACK,
CONFIG_MM_HEAP_MEMPOOL_RECORD_STACK_SKIP);

if (result < CONFIG_MM_RECORD_STACK)
{
record->backtrace[result] = NULL;
}
*stack = backtrace_record(
CONFIG_MM_HEAP_MEMPOOL_RECORD_STACK_SKIP);
}
else
# endif
{
record->backtrace[0] = NULL;
*stack = NULL;
}
# endif
}
Expand All @@ -183,7 +192,7 @@ static void mempool_foreach(FAR struct mempool_s *pool,
size_t chunksize = pool->expandsize;
FAR struct mempool_record_s *record;
FAR sq_entry_t *entry;
FAR char *base ;
FAR char *base;
size_t nblks;

if (pool->ibase != NULL)
Expand Down Expand Up @@ -279,11 +288,16 @@ static void mempool_memdump_callback(FAR struct mempool_s *pool,
if ((MM_DUMP_ASSIGN(dump, record) || MM_DUMP_ALLOC(dump, record) ||
MM_DUMP_LEAK(dump, record)) && MM_DUMP_SEQNO(dump, record))
{
# if CONFIG_MM_RECORD_STACK > 0
char tmp[BACKTRACE_BUFFER_SIZE(CONFIG_MM_RECORD_STACK)];
# ifdef CONFIG_MM_RECORD_STACK
char tmp[BACKTRACE_BUFFER_SIZE(CONFIG_LIBC_BACKTRACE_DEPTH)] = "";
FAR void **stack;
int stacksize;

backtrace_format(tmp, sizeof(tmp), record->backtrace,
CONFIG_MM_RECORD_STACK);
stack = backtrace_get(record->stack, &stacksize);
if (stack && stacksize)
{
backtrace_format(tmp, sizeof(tmp), stack, stacksize);
}
# else
FAR const char *tmp = "";
# endif
Expand All @@ -309,9 +323,23 @@ mempool_memdump_free_callback(FAR struct mempool_s *pool,

if (record->magic == MEMPOOL_MAGIC_FREE)
{
syslog(LOG_INFO, "%12zu%9zu%*p\n",
#ifdef CONFIG_MM_RECORD_STACK
char tmp[BACKTRACE_BUFFER_SIZE(CONFIG_LIBC_BACKTRACE_DEPTH)] = "";
FAR void **stack;
int stacksize;

stack = backtrace_get(record->freestack, &stacksize);
if (stack && stacksize)
{
backtrace_format(tmp, sizeof(tmp), stack, stacksize);
}
#else
FAR const char *tmp = "";
#endif
syslog(LOG_INFO, MEMDUMP_PID_FORMAT "%12zu%9zu%*p %s\n",
MEMDUMP_PID_ARG(record->freepid)
blocksize, overhead, BACKTRACE_PTR_FMT_WIDTH,
mempool_get_block_from_record(record));
mempool_get_block_from_record(record), tmp);
}
}
#endif
Expand Down Expand Up @@ -484,7 +512,7 @@ int mempool_init(FAR struct mempool_s *pool)
mempool_procfs_register(&pool->procfs, pool->name);
# ifdef CONFIG_MM_RECORD_STACK_DEFAULT
pool->procfs.backtrace = true;
# elif CONFIG_MM_RECORD_STACK > 0
# elif defined(CONFIG_MM_RECORD_STACK)
pool->procfs.backtrace = false;
# endif
#endif
Expand Down Expand Up @@ -602,7 +630,8 @@ FAR void *mempool_allocate(FAR struct mempool_s *pool, unsigned int timeout)

#ifdef CONFIG_MM_RECORD
record = mempool_get_record_from_block(blk);
mempool_record(pool, record);
DEBUGASSERT(record->magic == MEMPOOL_MAGIC_FREE);
mempool_record(pool, record, MEMPOOL_MAGIC_ALLOC);
#endif

blk = kasan_unpoison(blk, pool->blocksize);
Expand Down Expand Up @@ -658,7 +687,7 @@ FAR void *mempool_zallocate(FAR struct mempool_s *pool, unsigned int timeout)

void mempool_release(FAR struct mempool_s *pool, FAR void *blk)
{
irqstate_t flags = spin_lock_irqsave(&pool->lock);
irqstate_t flags;
#ifdef CONFIG_MM_RECORD
FAR struct mempool_record_s *record;

Expand All @@ -669,10 +698,12 @@ void mempool_release(FAR struct mempool_s *pool, FAR void *blk)
/* Check double free or out of out of bounds */

DEBUGASSERT(record->magic == MEMPOOL_MAGIC_ALLOC);
record->magic = MEMPOOL_MAGIC_FREE;

mempool_record(pool, record, MEMPOOL_MAGIC_FREE);
#endif

flags = spin_lock_irqsave(&pool->lock);

pool->nalloc--;

#ifdef CONFIG_MM_FILL_ALLOCATIONS
Expand Down
23 changes: 9 additions & 14 deletions mm/mm_heap/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <nuttx/mm/mm.h>

#include <assert.h>
#include <execinfo.h>
#include <sys/types.h>
#include <stdbool.h>
#include <string.h>
Expand Down Expand Up @@ -78,23 +79,17 @@
# define MM_RECORD_PID(node, tid)
#endif

#if CONFIG_MM_RECORD_STACK > 0
#ifdef CONFIG_MM_RECORD_STACK
# define MM_RECORD_STACK(node, tid) \
do \
{ \
if ((heap)->mm_procfs.backtrace) \
{ \
int n = sched_backtrace(tid, (node)->backtrace, \
CONFIG_MM_RECORD_STACK, \
CONFIG_MM_RECORD_STACK_SKIP); \
if (n < CONFIG_MM_RECORD_STACK) \
{ \
(node)->backtrace[n] = NULL; \
} \
(node)->stack = backtrace_record(CONFIG_MM_RECORD_STACK_SKIP); \
} \
else \
{ \
(node)->backtrace[0] = NULL; \
(node)->stack = NULL; \
} \
} \
while (0)
Expand Down Expand Up @@ -191,9 +186,9 @@ struct mm_allocnode_s
#ifdef CONFIG_MM_RECORD_SEQNO
unsigned long seqno; /* The sequence of memory malloc */
#endif
#if CONFIG_MM_RECORD_STACK > 0
FAR void *backtrace[CONFIG_MM_RECORD_STACK]; /* The backtrace buffer for caller */
#endif
#ifdef CONFIG_MM_RECORD_STACK
FAR void *stack; /* The backtrace buffer for caller */
# endif
};

/* This describes a free chunk */
Expand All @@ -208,8 +203,8 @@ struct mm_freenode_s
#ifdef CONFIG_MM_RECORD_SEQNO
unsigned long seqno; /* The sequence of memory malloc */
#endif
#if CONFIG_MM_RECORD_STACK > 0
FAR void *backtrace[CONFIG_MM_RECORD_STACK]; /* The backtrace buffer for caller */
#ifdef CONFIG_MM_RECORD_STACK
FAR void *stack; /* The backtrace buffer for caller */
#endif
FAR struct mm_freenode_s *flink; /* Supports a doubly linked list */
FAR struct mm_freenode_s *blink;
Expand Down
5 changes: 5 additions & 0 deletions mm/mm_heap/mm_free.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <assert.h>
#include <nuttx/debug.h>
#include <execinfo.h>

#include <nuttx/arch.h>
#include <nuttx/sched.h>
Expand Down Expand Up @@ -148,6 +149,10 @@ void mm_forcefree(FAR struct mm_heap_s *heap, FAR void *mem)
((FAR char *)kasan_clear_tag(mem) - MM_SIZEOF_ALLOCNODE);
nodesize = MM_SIZEOF_NODE(node);

#ifdef CONFIG_MM_RECORD_STACK
backtrace_remove(node->stack);
#endif

/* Sanity check against double-frees */

DEBUGASSERT(MM_NODE_IS_ALLOC(node));
Expand Down
5 changes: 2 additions & 3 deletions mm/mm_heap/mm_initialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,9 @@ void mm_uninitialize(FAR struct mm_heap_s *heap)
heap->mm_curused);
}

#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO)
# if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
#if defined(CONFIG_FS_PROCFS) && (defined(CONFIG_BUILD_FLAT) || \
defined(__KERNEL__)) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO)
procfs_unregister_meminfo(&heap->mm_procfs);
# endif
#endif
nxmutex_destroy(&heap->mm_lock);
}
2 changes: 1 addition & 1 deletion mm/mm_heap/mm_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int mm_lock(FAR struct mm_heap_s *heap)

if (up_interrupt_context())
{
# if !defined(CONFIG_SMP)
# ifndef CONFIG_SMP
/* Check the mutex value, if held by someone, then return false.
* Or, touch the heap internal data directly.
*/
Expand Down
Loading
Loading