From a8b3238437994259245f27c08fbddb8045eb0d54 Mon Sep 17 00:00:00 2001 From: Sylvain Chouleur Date: Wed, 14 May 2025 09:22:21 +0200 Subject: [PATCH] arm64: core: force data synchronization between context switch This patch fixes memory corruption that can happen when running in multi-thread and multi-core environment with heavy scheduling stress. In SMP configuration, we must ensure that all thread's context is stored before writing the switch_handle flag. Otherwise some of the thread context writes could be delayed after another CPU begins to schedule this thread which could lead to memory corruptions. Signed-off-by: Sylvain Chouleur --- arch/arm64/core/switch.S | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/arm64/core/switch.S b/arch/arm64/core/switch.S index 333b8f0b21e..06a95d37b7a 100644 --- a/arch/arm64/core/switch.S +++ b/arch/arm64/core/switch.S @@ -74,6 +74,13 @@ SECTION_FUNC(TEXT, z_arm64_context_switch) ldp x0, x1, [sp], #16 #endif +#ifdef CONFIG_SMP + /* Write barrier: ensure all preceding writes are executed + * before writing the switch handle + */ + dmb sy +#endif + /* save old thread into switch handle which is required by * z_sched_switch_spin() */