Skip to content

Commit

Permalink
kernel/userspace: Fix dynamic thread stack allocation at userspace
Browse files Browse the repository at this point in the history
It wasn't saving adjusted stack size at either the private stack or the
k_object, thus failing subsequent checks.

Test added to check for this case and prevent regressions.

Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
  • Loading branch information
edersondisouza committed Feb 28, 2024
1 parent 7bd2804 commit 8604f2f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kernel/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ static struct k_object *dynamic_object_create(enum k_objects otype, size_t align
struct z_stack_data *stack_data = (struct z_stack_data *)
((uint8_t *)dyn->data + adjusted_size - sizeof(*stack_data));
stack_data->priv = (uint8_t *)dyn->data;
stack_data->size = adjusted_size;
dyn->kobj.data.stack_data = stack_data;
#if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU)
dyn->kobj.name = (void *)ROUND_UP(
Expand All @@ -351,6 +352,7 @@ static struct k_object *dynamic_object_create(enum k_objects otype, size_t align
#endif
#else
dyn->kobj.name = dyn->data;
dyn->kobj.data.stack_size = adjusted_size;
#endif
} else {
dyn->data = z_thread_aligned_alloc(align, obj_size_get(otype) + size);
Expand Down
36 changes: 36 additions & 0 deletions tests/kernel/threads/dynamic_thread_stack/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ static void func(void *arg1, void *arg2, void *arg3)
*flag = true;
}

/** @brief Check we can create a thread from userspace, using dynamic objects */
ZTEST_USER(dynamic_thread_stack, test_dynamic_thread_stack_userspace_dyn_obj)
{
k_tid_t tid;
struct k_thread *th;
k_thread_stack_t *stack;

if (!IS_ENABLED(CONFIG_USERSPACE)) {
ztest_test_skip();
}

if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD_PREFER_ALLOC)) {
ztest_test_skip();
}

if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD_ALLOC)) {
ztest_test_skip();
}

stack = k_thread_stack_alloc(CONFIG_DYNAMIC_THREAD_STACK_SIZE, K_USER);
zassert_not_null(stack);

th = k_object_alloc(K_OBJ_THREAD);
zassert_not_null(th);

tid = k_thread_create(th, stack, CONFIG_DYNAMIC_THREAD_STACK_SIZE, func,
&tflag[0], NULL, NULL, 0,
K_USER | K_INHERIT_PERMS, K_NO_WAIT);

zassert_not_null(tid);

zassert_ok(k_thread_join(tid, K_MSEC(TIMEOUT_MS)));
zassert_true(tflag[0]);
zassert_ok(k_thread_stack_free(stack));
}

/** @brief Exercise the pool-based thread stack allocator */
ZTEST(dynamic_thread_stack, test_dynamic_thread_stack_pool)
{
Expand Down
10 changes: 10 additions & 0 deletions tests/kernel/threads/dynamic_thread_stack/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ tests:
- CONFIG_DYNAMIC_THREAD_POOL_SIZE=2
- CONFIG_DYNAMIC_THREAD_ALLOC=y
- CONFIG_USERSPACE=y
kernel.threads.dynamic_thread.stack.no_pool.alloc.user.dynamic_object:
tags: userspace
filter: CONFIG_ARCH_HAS_USERSPACE
extra_configs:
# 011
- CONFIG_DYNAMIC_THREAD_PREFER_ALLOC=y
- CONFIG_DYNAMIC_THREAD_POOL_SIZE=0
- CONFIG_DYNAMIC_THREAD_ALLOC=y
- CONFIG_USERSPACE=y
- CONFIG_DYNAMIC_OBJECTS=y

0 comments on commit 8604f2f

Please sign in to comment.