From d34351d9945435fd29ff5b2ecdf4805cf784f779 Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Fri, 22 Mar 2024 14:11:49 -0700 Subject: [PATCH] kernel: align thread stack size declaration When thread stack is defined as an array, K_THREAD_STACK_LEN() is used to calculate the size for each stack in the array. However, standalone thread stack has its size calculated by Z_THREAD_STACK_SIZE_ADJUST() instead. Depending on the arch alignment requirement, they may not be the same... which could cause some confusions. So align them both to use K_THREAD_STACK_LEN(). Signed-off-by: Daniel Leung --- arch/x86/core/userspace.c | 2 +- include/zephyr/kernel/thread_stack.h | 4 ++-- kernel/thread.c | 2 +- kernel/userspace.c | 6 +++--- tests/kernel/fatal/exception/src/main.c | 4 ++-- tests/kernel/threads/dynamic_thread_stack/src/main.c | 2 +- tests/kernel/threads/thread_stack/src/main.c | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/arch/x86/core/userspace.c b/arch/x86/core/userspace.c index 750c977594..09e7945303 100644 --- a/arch/x86/core/userspace.c +++ b/arch/x86/core/userspace.c @@ -132,7 +132,7 @@ FUNC_NORETURN void arch_user_mode_enter(k_thread_entry_t user_entry, size_t stack_aligned_size; stack_start = POINTER_TO_UINT(_current->stack_obj); - stack_size = Z_THREAD_STACK_SIZE_ADJUST(_current->stack_info.size); + stack_size = K_THREAD_STACK_LEN(_current->stack_info.size); #if defined(CONFIG_HW_STACK_PROTECTION) /* With hardware stack protection, the first page of stack diff --git a/include/zephyr/kernel/thread_stack.h b/include/zephyr/kernel/thread_stack.h index 3598742a24..a8151cce9d 100644 --- a/include/zephyr/kernel/thread_stack.h +++ b/include/zephyr/kernel/thread_stack.h @@ -404,7 +404,7 @@ static inline char *K_KERNEL_STACK_BUFFER(k_thread_stack_t *sym) */ #define K_THREAD_STACK_DECLARE(sym, size) \ extern struct z_thread_stack_element \ - sym[Z_THREAD_STACK_SIZE_ADJUST(size)] + sym[K_THREAD_STACK_LEN(size)] /** * @brief Declare a reference to a thread stack array @@ -467,7 +467,7 @@ static inline char *K_KERNEL_STACK_BUFFER(k_thread_stack_t *sym) #define Z_THREAD_STACK_DEFINE_IN(sym, size, lsect) \ struct z_thread_stack_element lsect \ __aligned(Z_THREAD_STACK_OBJ_ALIGN(size)) \ - sym[Z_THREAD_STACK_SIZE_ADJUST(size)] + sym[K_THREAD_STACK_LEN(size)] /** * @brief Define a toplevel array of thread stack memory regions in specified region diff --git a/kernel/thread.c b/kernel/thread.c index 5d705e114b..115bf34a3b 100644 --- a/kernel/thread.c +++ b/kernel/thread.c @@ -384,7 +384,7 @@ static char *setup_thread_stack(struct k_thread *new_thread, #ifdef CONFIG_USERSPACE if (z_stack_is_user_capable(stack)) { - stack_obj_size = Z_THREAD_STACK_SIZE_ADJUST(stack_size); + stack_obj_size = K_THREAD_STACK_LEN(stack_size); stack_buf_start = K_THREAD_STACK_BUFFER(stack); stack_buf_size = stack_obj_size - K_THREAD_STACK_RESERVED; } else diff --git a/kernel/userspace.c b/kernel/userspace.c index c87421d072..0d9a437570 100644 --- a/kernel/userspace.c +++ b/kernel/userspace.c @@ -59,13 +59,13 @@ static struct k_spinlock objfree_lock; /* k_object_free */ #if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU) #define STACK_ELEMENT_DATA_SIZE(size) \ (sizeof(struct z_stack_data) + CONFIG_PRIVILEGED_STACK_SIZE + \ - Z_THREAD_STACK_OBJ_ALIGN(size) + Z_THREAD_STACK_SIZE_ADJUST(size)) + Z_THREAD_STACK_OBJ_ALIGN(size) + K_THREAD_STACK_LEN(size)) #else #define STACK_ELEMENT_DATA_SIZE(size) (sizeof(struct z_stack_data) + \ - Z_THREAD_STACK_SIZE_ADJUST(size)) + K_THREAD_STACK_LEN(size)) #endif /* CONFIG_ARM_MPU || CONFIG_ARC_MPU */ #else -#define STACK_ELEMENT_DATA_SIZE(size) Z_THREAD_STACK_SIZE_ADJUST(size) +#define STACK_ELEMENT_DATA_SIZE(size) K_THREAD_STACK_LEN(size) #endif /* CONFIG_GEN_PRIV_STACKS */ #endif /* CONFIG_DYNAMIC_OBJECTS */ diff --git a/tests/kernel/fatal/exception/src/main.c b/tests/kernel/fatal/exception/src/main.c index 5a44b215b4..30050d3ca3 100644 --- a/tests/kernel/fatal/exception/src/main.c +++ b/tests/kernel/fatal/exception/src/main.c @@ -465,7 +465,7 @@ static void *fatal_setup(void) obj_size = K_THREAD_STACK_SIZEOF(overflow_stack); #if defined(CONFIG_USERSPACE) - obj_size = Z_THREAD_STACK_SIZE_ADJUST(obj_size); + obj_size = K_THREAD_STACK_LEN(obj_size); #endif k_mem_region_align(&pin_addr, &pin_size, @@ -477,7 +477,7 @@ static void *fatal_setup(void) obj_size = K_THREAD_STACK_SIZEOF(alt_stack); #if defined(CONFIG_USERSPACE) - obj_size = Z_THREAD_STACK_SIZE_ADJUST(obj_size); + obj_size = K_THREAD_STACK_LEN(obj_size); #endif k_mem_region_align(&pin_addr, &pin_size, diff --git a/tests/kernel/threads/dynamic_thread_stack/src/main.c b/tests/kernel/threads/dynamic_thread_stack/src/main.c index c2446e04d6..bb15d824d8 100644 --- a/tests/kernel/threads/dynamic_thread_stack/src/main.c +++ b/tests/kernel/threads/dynamic_thread_stack/src/main.c @@ -12,7 +12,7 @@ #define POOL_SIZE 20480 #ifdef CONFIG_USERSPACE -#define STACK_OBJ_SIZE Z_THREAD_STACK_SIZE_ADJUST(CONFIG_DYNAMIC_THREAD_STACK_SIZE) +#define STACK_OBJ_SIZE K_THREAD_STACK_LEN(CONFIG_DYNAMIC_THREAD_STACK_SIZE) #else #define STACK_OBJ_SIZE K_KERNEL_STACK_LEN(CONFIG_DYNAMIC_THREAD_STACK_SIZE) #endif diff --git a/tests/kernel/threads/thread_stack/src/main.c b/tests/kernel/threads/thread_stack/src/main.c index df68c06032..cf9b5d8c3a 100644 --- a/tests/kernel/threads/thread_stack/src/main.c +++ b/tests/kernel/threads/thread_stack/src/main.c @@ -231,7 +231,7 @@ void stack_buffer_scenarios(void) * For some stack declared with: * * K_THREAD_STACK_DEFINE(my_stack, X); - * Z_THREAD_STACK_SIZE_ADJUST(X) - K_THREAD_STACK_RESERVED == + * K_THREAD_STACK_LEN(X) - K_THREAD_STACK_RESERVED == * K_THREAD_STACK_SIZEOF(my_stack) * * K_KERNEL_STACK_DEFINE(my_kern_stack, Y): @@ -241,7 +241,7 @@ void stack_buffer_scenarios(void) #ifdef CONFIG_USERSPACE /* Not defined if user mode disabled, all stacks are kernel stacks */ if (scenario_data.is_user) { - adjusted = Z_THREAD_STACK_SIZE_ADJUST(scenario_data.declared_size); + adjusted = K_THREAD_STACK_LEN(scenario_data.declared_size); } else #endif {