Skip to content

Commit

Permalink
mm: Add pending configuration for mm node struct and precding
Browse files Browse the repository at this point in the history
pending memory block node precding and the entire struct, after turning on this bit, the size of each memory block wil be aligned to MM_ALIGN

Signed-off-by: wangmingrong1 <[email protected]>
  • Loading branch information
W-M-R committed Jan 13, 2025
1 parent 5f4a15b commit b45cc31
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
8 changes: 8 additions & 0 deletions mm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ config MM_DEFAULT_ALIGNMENT
memory default alignment is equal to sizoef(uintptr), if this value
is not 0, this value must be 2^n and at least sizeof(uintptr).

config MM_NODE_PENDING
bool "Enable pending memory node"
default n
---help---
Pending memory block node precding and the entire struct,
After turning on this bit, the size of each memory block
will be aligned to MM_ALIGN

config MM_SMALL
bool "Small memory model"
default n
Expand Down
40 changes: 33 additions & 7 deletions mm/mm_heap/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,15 @@
* previous freenode
*/

#define MM_ALLOCNODE_OVERHEAD (MM_SIZEOF_ALLOCNODE - sizeof(mmsize_t))
#ifdef CONFIG_MM_NODE_PENDING
# define MM_NODE_PENDING aligned_data(MM_ALIGN)
# define MMSIZE_T_LEN MM_ALIGN
#else
# define MM_NODE_PENDING
# define MMSIZE_T_LEN sizeof(mmsize_t)
#endif

#define MM_ALLOCNODE_OVERHEAD (MM_SIZEOF_ALLOCNODE - MMSIZE_T_LEN)

/* Get the node size */

Expand Down Expand Up @@ -173,23 +181,41 @@ typedef size_t mmsize_t;

struct mm_allocnode_s
{
mmsize_t preceding; /* Physical preceding chunk size */
mmsize_t size; /* Size of this chunk */
union
{
mmsize_t preceding; /* Physical preceding chunk size */
uint8_t preceding_align[MMSIZE_T_LEN];
};
union
{
mmsize_t size; /* Physical preceding chunk size */
uint8_t size_align[MMSIZE_T_LEN];
};

#if CONFIG_MM_BACKTRACE >= 0
pid_t pid; /* The pid for caller */
unsigned long seqno; /* The sequence of memory malloc */
# if CONFIG_MM_BACKTRACE > 0
FAR void *backtrace[CONFIG_MM_BACKTRACE]; /* The backtrace buffer for caller */
# endif
#endif
};
}MM_NODE_PENDING;

/* This describes a free chunk */

struct mm_freenode_s
{
mmsize_t preceding; /* Physical preceding chunk size */
mmsize_t size; /* Size of this chunk */
union
{
mmsize_t preceding; /* Physical preceding chunk size */
uint8_t preceding_align[MMSIZE_T_LEN];
};
union
{
mmsize_t size; /* Physical preceding chunk size */
uint8_t size_align[MMSIZE_T_LEN];
};

#if CONFIG_MM_BACKTRACE >= 0
pid_t pid; /* The pid for caller */
unsigned long seqno; /* The sequence of memory malloc */
Expand All @@ -199,7 +225,7 @@ struct mm_freenode_s
#endif
FAR struct mm_freenode_s *flink; /* Supports a doubly linked list */
FAR struct mm_freenode_s *blink;
};
}MM_NODE_PENDING;

static_assert(MM_SIZEOF_ALLOCNODE <= MM_MIN_CHUNK,
"Error size for struct mm_allocnode_s\n");
Expand Down
5 changes: 4 additions & 1 deletion mm/mm_heap/mm_realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
heap->mm_curused += newsize - oldsize;
mm_shrinkchunk(heap, oldnode, newsize);
kasan_poison((FAR char *)oldnode + MM_SIZEOF_NODE(oldnode) +
sizeof(mmsize_t), oldsize - MM_SIZEOF_NODE(oldnode));
MMSIZE_T_LEN, oldsize - MM_SIZEOF_NODE(oldnode));
}

/* Then return the original address */
Expand Down Expand Up @@ -388,10 +388,12 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
sched_note_heap(NOTE_HEAP_ALLOC, heap, newmem, newsize,
heap->mm_curused);
mm_unlock(heap);
kasan_hw_close();
MM_ADD_BACKTRACE(heap, (FAR char *)newmem - MM_SIZEOF_ALLOCNODE);

newmem = kasan_unpoison(newmem, MM_SIZEOF_NODE(oldnode) -
MM_ALLOCNODE_OVERHEAD);

if (kasan_reset_tag(newmem) != kasan_reset_tag(oldmem))
{
/* Now we have to move the user contents 'down' in memory. memcpy
Expand All @@ -401,6 +403,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
memcpy(newmem, oldmem, oldsize - MM_ALLOCNODE_OVERHEAD);
}

kasan_hw_open();
return newmem;
}

Expand Down

0 comments on commit b45cc31

Please sign in to comment.