kernel: dynamic: update storage size for pool of dynamic thread stacks

Commit 5c5e17f introduced a subtle regression when userspace was
configured on architectures requiring guard pages.

Prior to 5c5e17f, the assumption was that guard pages would be included in
`CONFIG_DYNAMIC_THREAD_STACK_SIZE`, and that was something that the caller
of `k_thread_stack_alloc()` would need to be aware of, although it was not
documented at all, unfortunately.

It seems that 5c5e17f intended to remove the need for that assumption, but
the necessary conditions for doing so had not been met.

Update pool storage size to account for guard pages, which ensures that
users can access every byte of `CONFIG_DYNAMIC_THREAD_STACK_SIZE` rather
than needing to be aware that guard pages would be included in the
requested size.

The compromise is a more intuitive API at the cost of more storage space
for the pool of thread stacks when userspace is enabled.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
This commit is contained in:
Chris Friedt
2025-10-01 16:37:14 -04:00
committed by Johan Hedberg
parent 4c221ac3cf
commit 6c01157fef
2 changed files with 4 additions and 8 deletions

View File

@@ -28,7 +28,7 @@ struct dyn_cb_data {
};
static K_THREAD_STACK_ARRAY_DEFINE(dynamic_stack, CONFIG_DYNAMIC_THREAD_POOL_SIZE,
CONFIG_DYNAMIC_THREAD_STACK_SIZE);
K_THREAD_STACK_LEN(CONFIG_DYNAMIC_THREAD_STACK_SIZE));
SYS_BITARRAY_DEFINE_STATIC(dynamic_ba, BA_SIZE);
static k_thread_stack_t *z_thread_stack_alloc_pool(size_t size, int flags)
@@ -36,11 +36,10 @@ static k_thread_stack_t *z_thread_stack_alloc_pool(size_t size, int flags)
int rv;
size_t offset;
k_thread_stack_t *stack;
const size_t max_size = ((flags & K_USER) != 0) ? K_THREAD_STACK_SIZEOF(dynamic_stack[0]) :
K_KERNEL_STACK_SIZEOF(dynamic_stack[0]);
if (size > max_size) {
LOG_DBG("stack size %zu is > pool stack size %zu", size, max_size);
if (size > CONFIG_DYNAMIC_THREAD_STACK_SIZE) {
LOG_DBG("stack size %zu is > pool stack size %zu", size,
(size_t)CONFIG_DYNAMIC_THREAD_STACK_SIZE);
return NULL;
}

View File

@@ -70,9 +70,6 @@ tests:
- CONFIG_THREAD_STACK_INFO=n
portability.posix.common.userspace:
filter: CONFIG_ARCH_HAS_USERSPACE
# Excluded while #96602 is in review
platform_exclude:
- mps2/an385
tags:
- userspace
extra_configs: