From 9af5fc5d09724a5d300bc07ce85b9ba5c01ffadd Mon Sep 17 00:00:00 2001 From: xuxingliang Date: Wed, 16 Oct 2024 10:19:30 +0800 Subject: [PATCH] fs/shm: alloc aligned memory if CPU has cache For kernel builds, shared memory is automatically aligned to page size. For flat and protected builds, we align the memory size to the CPU cache line size. Failure to align memory properly could result in partial data read/write by the CPU and peripherals, potentially causing data corruption during cache flushes or invalidations. Signed-off-by: xuxingliang --- fs/shm/shmfs_alloc.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/fs/shm/shmfs_alloc.c b/fs/shm/shmfs_alloc.c index a3add18a6d..a6bd4affad 100644 --- a/fs/shm/shmfs_alloc.c +++ b/fs/shm/shmfs_alloc.c @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -47,19 +48,23 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length) * chunk in kernel heap */ - size_t alloc_size = sizeof(struct shmfs_object_s) + length; - if (alloc_size < length) - { - /* There must have been an integer overflow */ - - return NULL; - } - - object = fs_heap_zalloc(alloc_size); + object = fs_heap_zalloc(sizeof(struct shmfs_object_s)); if (object) { - object->paddr = (FAR char *)(object + 1); - allocated = true; + size_t cachesize = up_get_dcache_linesize(); + if (cachesize > 0) + { + object->paddr = fs_heap_memalign(cachesize, length); + } + else + { + object->paddr = fs_heap_malloc(length); + } + + if (object->paddr) + { + allocated = true; + } } #elif defined(CONFIG_BUILD_PROTECTED) @@ -70,7 +75,15 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length) object = fs_heap_zalloc(sizeof(struct shmfs_object_s)); if (object) { - object->paddr = kumm_zalloc(length); + size_t cachesize = up_get_dcache_linesize(); + if (cachesize > 0) + { + object->paddr = kumm_memalign(cachesize, length); + } + else + { + object->paddr = kumm_malloc(length); + } if (object->paddr) { @@ -140,7 +153,9 @@ void shmfs_free_object(FAR struct shmfs_object_s *object) if (object) { -#if defined (CONFIG_BUILD_PROTECTED) +#if defined(CONFIG_BUILD_FLAT) + fs_heap_free(object->paddr); +#elif defined(CONFIG_BUILD_PROTECTED) kumm_free(object->paddr); #elif defined(CONFIG_BUILD_KERNEL) pages = &object->paddr;