Skip to content

Commit

Permalink
regions_mm: Invalidate cache when freeing memory
Browse files Browse the repository at this point in the history
A memory region may be allocated to a different core than before, and
performing a writeback by the cache may corrupt the current data.

Signed-off-by: Adrian Warecki <[email protected]>
  • Loading branch information
softwarecki committed Jul 5, 2024
1 parent 6f12b80 commit 36132d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions zephyr/lib/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ static void virtual_heap_free(void *ptr)
ptr = (__sparse_force void *)sys_cache_cached_ptr_get(ptr);

K_SPINLOCK(&vmh_lock) {
virtual_buffers_heap->core_id = cpu_get_id();
ret = vmh_free(virtual_buffers_heap, ptr);
}

Expand Down
6 changes: 6 additions & 0 deletions zephyr/lib/regions_mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,14 @@ int vmh_free(struct vmh_heap *heap, void *ptr)
if (retval)
return retval;

/* Platforms based on xtensa have a non-coherent cache between cores. Before releasing
* a memory block, it is necessary to invalidate the cache. This memory block can be
* allocated to another core and performing cache writeback by the previous owner will
* destroy current content of the main memory. */
sys_cache_data_invd_range(ptr, size_to_free);
return vmh_unmap_region(heap->physical_blocks_allocators[mem_block_iter], ptr,
size_to_free);

}

/**
Expand Down

0 comments on commit 36132d4

Please sign in to comment.