Skip to content

System workqueue: Prevent blocking API calls #87522

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions doc/kernel/services/threads/workqueue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ its queue until the handler function finishes executing.
potentially blocking, as there is no guarantee that work items submitted to
it do not depend on subsequent work items in the queue to unblock them.

:kconfig:option:`CONFIG_SYSTEM_WORKQUEUE_NO_BLOCK` enforces that no work
items submitted to the system workqueue perform any blocking operations.

The single argument that is passed to a handler function can be ignored if it
is not required. If the handler function requires additional information about
the work it is to perform, the work item can be embedded in a larger data
Expand Down
8 changes: 8 additions & 0 deletions kernel/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,14 @@ config SYSTEM_WORKQUEUE_NO_YIELD
cooperative and a sequence of work items is expected to complete
without yielding.

config SYSTEM_WORKQUEUE_NO_BLOCK
bool "Select whether system work queue enforces non-blocking work items"
help
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe default y if ASSERT or something similar? This is a cheap check with clear value, probably wants to be on any time CONFIG_ASSERT=y

By default, the system work queue does not enforce work items
passed to it to not perform blocking operations. Selecting this
enforces that blocking operations are not performed by invoking
a kernel oops if such operations are attempted.

endmenu

menu "Barrier Operations"
Expand Down
4 changes: 4 additions & 0 deletions kernel/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ static inline void z_vrfy_k_thread_resume(k_tid_t thread)

static void unready_thread(struct k_thread *thread)
{
if (IS_ENABLED(CONFIG_SYSTEM_WORKQUEUE_NO_BLOCK) && k_is_in_sys_work()) {
k_oops();
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks wrong to me. "Ready" and "running" aren't the same thing. A thread can be ready but lower priority than _current. Basically: my guess is that this code will oops if you try to k_thread_suspend() a runnable thread out of a work queue item, which would be expected to be legal and work.

You need to add a test for thread == _current at least, but it would probably be better to move this test to reschedule() instead.

Also: probably want a panic here and not an oops. An oops in userspace will kill only the current thread, but a misuse of the system workqueue (which obviously is a kernel thread anyway) is a global failure.

And finally: neither oops nor panic give any feedback to the poor user whose code blew up. Probably wants a printk() here (or to be expressed as an __ASSERT() when available).

}

if (z_is_thread_queued(thread)) {
dequeue_thread(thread);
}
Expand Down
Loading