Skip to content

Conversation

@lyakh
Copy link
Collaborator

@lyakh lyakh commented Jan 12, 2026

Add a sys_sem test between kernel- and user-space. @dcpleung any idea why this might not be working? Just adding a sys_sem_take() in the kernel thread with a timeout hangs the DSP, instead of returning with a timeout as expected.

@lyakh lyakh changed the title [SKIP SOF-TEST][DNM] test: userspace: add a sys_sem test [DNM] test: userspace: add a sys_sem test Jan 14, 2026
Add a sys_sem test between kernel- and user-space.

Signed-off-by: Guennadi Liakhovetski <[email protected]>
@lyakh
Copy link
Collaborator Author

lyakh commented Jan 14, 2026

SOFCI TEST

@lyakh lyakh marked this pull request as ready for review January 14, 2026 12:40
Copilot AI review requested due to automatic review settings January 14, 2026 12:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Comment on lines +62 to +63
#include <zephyr/sys/sem.h>
#include <zephyr/app_memory/mem_domain.h>
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The include directives should be placed at the top of the file with the other includes, not in the middle of the file after a ZTEST function. This violates standard C coding conventions and can make the code harder to maintain.

Copilot uses AI. Check for mistakes.
Comment on lines +98 to +104
k_thread_create(&user_thread, user_stack, USER_STACKSIZE,
sys_sem_function, NULL, NULL, NULL,
-1, K_USER, K_FOREVER);
k_mem_domain_add_partition(&dp_mdom, &mpart);
k_mem_domain_add_thread(&dp_mdom, &user_thread);

k_thread_start(&user_thread);
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a potential race condition here. The user thread is started with K_FOREVER delay, then immediately started on line 104. The thread could execute sys_sem_give on line 78 before the kernel thread reaches sys_sem_take on line 107, causing the semaphore signal to be lost. This is likely why the test hangs. Consider adding synchronization to ensure the kernel thread is waiting on sem1 before the user thread is started, similar to how test_user_thread_with_sem uses K_MSEC(10) delay.

Copilot uses AI. Check for mistakes.
__ASSERT(k_is_user_context(), "isn't user");
/* This is the goal, but it hangs with this disabled too */
sys_sem_give(&simple_sem.sem1);
int ret = sys_sem_take(&simple_sem.sem2, K_MSEC(20));
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable 'ret' is declared and assigned but never used or checked. This return value from sys_sem_take should be validated with an assertion to ensure the semaphore operation behaves as expected, especially since this is a test.

Suggested change
int ret = sys_sem_take(&simple_sem.sem2, K_MSEC(20));
int ret = sys_sem_take(&simple_sem.sem2, K_MSEC(20));
zassert_equal(ret, 0, "sys_sem_take failed (%d)", ret);

Copilot uses AI. Check for mistakes.
sys_sem_init(&simple_sem.sem1, 0, 1);
sys_sem_init(&simple_sem.sem2, 0, 1);

k_thread_create(&user_thread, user_stack, USER_STACKSIZE,
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global variable 'user_thread' is being reused from the earlier tests in this file. This can cause issues if multiple tests run in sequence, as the thread structure may contain state from previous test runs. Consider using a separate thread structure for this test, such as 'sys_sem_thread', to avoid potential conflicts.

Copilot uses AI. Check for mistakes.

k_thread_start(&user_thread);

/* This is what doesn't work: enabling this line crashes the DSP */
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment style is inconsistent. This file uses both single-line comments starting with '/*' and multi-line comment style. The comment should follow the established pattern in the file, which appears to use multi-line comment style for longer explanations.

Suggested change
/* This is what doesn't work: enabling this line crashes the DSP */
/*
* This is what doesn't work: enabling this line crashes the DSP
*/

Copilot uses AI. Check for mistakes.
};

ztest_test_pass();
k_mem_domain_init(&dp_mdom, 0, NULL);
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memory domain is initialized but never cleaned up after the test completes. The memory domain should be destroyed with k_mem_domain_remove_thread or appropriate cleanup to avoid resource leaks, especially in a test suite where multiple tests may run.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant