From a98f3f24173accf68f75e8b91b61bf6d43a86e7f Mon Sep 17 00:00:00 2001 From: wangmingrong1 Date: Wed, 19 Feb 2025 16:25:09 +0800 Subject: [PATCH] mm: Support CONFIG_MM_NODE_GUARDSIZE configuration After it is not zero, the preceding member of the next node will no longer belong to the valid area of the previous alloc node. Due to the existence of precedence, the memory block size of the node can only be aligned with sizeof(mmsize_t). This configuration will be applied in the following scenarios when set 8: ARM64 MTE hardware tag KASan, which requires the tag's memory address to be 16-byte aligned and the memory size must also be 16-byte aligned Signed-off-by: wangmingrong1 --- mm/Kconfig | 7 +++++++ mm/mm_heap/mm.h | 3 ++- mm/mm_heap/mm_realloc.c | 3 ++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mm/Kconfig b/mm/Kconfig index 2966d9f3008..7c029878b17 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -69,6 +69,13 @@ 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_GUARDSIZE + int "Memory node guard size" + default 0 + ---help--- + After it is enabled, the front and rear nodes will maintain a safety + distance of at least CONFIG_MM_NODE_GUARDSIZE. + config MM_SMALL bool "Small memory model" default n diff --git a/mm/mm_heap/mm.h b/mm/mm_heap/mm.h index 58d432716f1..0a818b06dd9 100644 --- a/mm/mm_heap/mm.h +++ b/mm/mm_heap/mm.h @@ -143,7 +143,8 @@ * previous freenode */ -#define MM_ALLOCNODE_OVERHEAD (MM_SIZEOF_ALLOCNODE - sizeof(mmsize_t)) +#define MM_ALLOCNODE_OVERHEAD (CONFIG_MM_NODE_GUARDSIZE + \ + MM_SIZEOF_ALLOCNODE - sizeof(mmsize_t)) /* Get the node size */ diff --git a/mm/mm_heap/mm_realloc.c b/mm/mm_heap/mm_realloc.c index d41619a66ad..6600290f585 100644 --- a/mm/mm_heap/mm_realloc.c +++ b/mm/mm_heap/mm_realloc.c @@ -153,7 +153,8 @@ 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)); + sizeof(mmsize_t) - CONFIG_MM_NODE_GUARDSIZE, + oldsize - MM_SIZEOF_NODE(oldnode)); } /* Then return the original address */