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 <xuxingliang@xiaomi.com>
This commit is contained in:
parent
22bcb88687
commit
9af5fc5d09
|
@ -27,6 +27,7 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <nuttx/arch.h>
|
#include <nuttx/arch.h>
|
||||||
|
#include <nuttx/cache.h>
|
||||||
#include <nuttx/kmalloc.h>
|
#include <nuttx/kmalloc.h>
|
||||||
#include <nuttx/pgalloc.h>
|
#include <nuttx/pgalloc.h>
|
||||||
|
|
||||||
|
@ -47,20 +48,24 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
|
||||||
* chunk in kernel heap
|
* chunk in kernel heap
|
||||||
*/
|
*/
|
||||||
|
|
||||||
size_t alloc_size = sizeof(struct shmfs_object_s) + length;
|
object = fs_heap_zalloc(sizeof(struct shmfs_object_s));
|
||||||
if (alloc_size < length)
|
|
||||||
{
|
|
||||||
/* There must have been an integer overflow */
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
object = fs_heap_zalloc(alloc_size);
|
|
||||||
if (object)
|
if (object)
|
||||||
{
|
{
|
||||||
object->paddr = (FAR char *)(object + 1);
|
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;
|
allocated = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#elif defined(CONFIG_BUILD_PROTECTED)
|
#elif defined(CONFIG_BUILD_PROTECTED)
|
||||||
/* in PROTECTED build, allocate the shm object in kernel heap, and shared
|
/* in PROTECTED build, allocate the shm object in kernel heap, and shared
|
||||||
|
@ -70,7 +75,15 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
|
||||||
object = fs_heap_zalloc(sizeof(struct shmfs_object_s));
|
object = fs_heap_zalloc(sizeof(struct shmfs_object_s));
|
||||||
if (object)
|
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)
|
if (object->paddr)
|
||||||
{
|
{
|
||||||
|
@ -140,7 +153,9 @@ void shmfs_free_object(FAR struct shmfs_object_s *object)
|
||||||
|
|
||||||
if (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);
|
kumm_free(object->paddr);
|
||||||
#elif defined(CONFIG_BUILD_KERNEL)
|
#elif defined(CONFIG_BUILD_KERNEL)
|
||||||
pages = &object->paddr;
|
pages = &object->paddr;
|
||||||
|
|
Loading…
Reference in New Issue