Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel/userspace: Fix dynamic thread stack allocation at userspace #69540

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions tests/kernel/threads/dynamic_thread_stack/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ CONFIG_MAIN_STACK_SIZE=2048

CONFIG_HW_STACK_PROTECTION=n
CONFIG_TEST_HW_STACK_PROTECTION=n

CONFIG_DYNAMIC_OBJECTS=y
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
Loading