-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
System workqueue: Prevent blocking API calls #87522
Conversation
Add warning to workqueue docs, explaining that using the system work queue for blocking work can not be done safely. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
d5d410b
to
e5e0e38
Compare
e5e0e38
to
989a8a5
Compare
989a8a5
to
6536974
Compare
Very good catch, have found myself tracking down issues because of this multiple times :^) |
System workqueue items must not use blocking APIs, like k_msleep(). Replace k_msleep() with k_busy_wait() to both adhere to this rule, and to better emulate more realistic work being done by system workqueue. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
The system work queue is being misused in-tree in some tests, drivers, and subsystems, I'm helping by mandating that they, and user applications, not do that. I truly fail to understand why you are so opposed to this.
No work should be done in callbacks by default, definitely not blocking work, just signal/delegate work to threads which you have control of. I thought that was commonly understood, given in most cases, the user does not know the context the callback is called from. There are obviously cases where the callback context is known, in which cases it is fine, but a callback from a device driver for example, definitely don't do work from there, it is likely from ISR.
It does not solve them, it prevents implementing a design pattern which can cause them.
Because, it is fundamentally unsafe to do so. See PR description regarding deadlocking. |
Because a much more limited version of this was merged for Bluetooth and it triggered months of issues and PRs before finally being reverted. I just don't want to see the same thing repeated here. I am perfectly okay with this happening if the consequences have been acknowledged. IMO this should only be merged AFTER existing users have been transitioned to individual work queues, doing it before is just asking for problems. |
kernel/sched.c
Outdated
@@ -523,6 +523,9 @@ static inline void z_vrfy_k_thread_resume(k_tid_t thread) | |||
|
|||
static void unready_thread(struct k_thread *thread) | |||
{ | |||
#ifndef CONFIG_SYSTEM_WORKQUEUE_BLOCKING | |||
__ASSERT_NO_MSG(!k_is_in_sys_work()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use k_oops()
instead. Assertions are for expressions that we know will be true because we have control over all code that could affect it. Application code is unknown to us, so we cannot make assertions about it.
__ASSERT_NO_MSG(!k_is_in_sys_work()); | |
if (k_is_in_sys_work()) { | |
k_oops(); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed :)
I agree. We should consider that Zephyr has dynamic threads. That looks like cheap threads that are perfect for short-lived tasks that need their own stack but don't want the static allocation. |
95b554b
to
0668826
Compare
Question: If some high level call is done in the system work queue, that would cause a mutex/semaphore/... wait in some lower level (for example in an SPI driver), will this result in a kernel oops if the kconfig symbol isn't set? IMO this will be confusing for new users. Also it's perfectly valid to wait in other threads, why should we make the system work queue special? EDIT: It's default off, which is better IMO. |
If the symbol is not selected, no kernel oops :) The issue with "blocks" here is that they have to be un-blocked by another context. If that context is another thread, and the block is very short, no problem. However, if the un-block needs to be performed by a later work item passed to the same work queue, there is a deadlock.
The system workqueue is already special, in that it has an "unlimited"/uncontrolled scope, compared to RTIO for example where every item passed to it is known and accounted for. Any module, driver, subsystem etc. on either side on an API call, can delegate work to the system workqueue, so there is no guarantee that the system workqueue is not both the calling context of a blocking call, and the context supposed to unblock it. The deadlock which inspired me create this PR is users using a system workqueue item to call pm_device_suspend() on a modem which uses the |
Add k_is_in_sys_work() API which returns true if the thread context is the system workqueue thread and the thread is currently servicing a work item. Useful for checking if blocking is safe or required in the case of calling an API which uses the system work queue internally. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
The system workqueue should never be unreadied from a work item handler (while busy). This commit implements an optional check which will invoke a kernel oops is a blocking operation is attempted from a work item passed to the system workqueue. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
0668826
to
f0d9a3b
Compare
I agree. This is a major problem. I want to expand on your point: Consider an operation that is extremely fast, but requires a large amount of memory. It would make sense to implement it using static memory protected by a mutex. This operation would never block for any significant amount of time, and would never participate in a deadlock. I think it's reasonable to treat the mutex as an implementation detail. It's completely safe to invoke this operation from the system work queue. Then consider a library that previously implemented the above operation by allocating on the stack, but transitions to doing it the way described above. I think it would be reasonable for a library author to change it without considering it to be a breaking change. But, with this PR, it would start failing on the system work queue. This is very unfortunate for stability guarantees. I think the terms 'blocking' and 'non-blocking' don't describe this situation well. This PR actually enforces that all work items on the system work queue are ISR-safe, a stronger requirement, right? |
In this case, use a spinlock. Accessing shared data is allowed from the system workqueue :) Mutexes and semaphores only become relevant once you start calling other APIs which you don't have control over.
Should have been using a spinlock :)
That is one way to put it : ) They need to be unblocking, which is also a requirement of ISR-safe code. The difference is you get a large stack at the cost of higher latency using the system work queue compared to actually calling something from ISRs :) |
Architecture WG meeting:
|
@@ -103,6 +103,15 @@ operations that are potentially blocking (e.g. taking a semaphore) must be | |||
used with care, since the workqueue cannot process subsequent work items in | |||
its queue until the handler function finishes executing. | |||
|
|||
.. warning:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did not see this warning, it lacks the warning regarding deadlocking which is the crucial one :) I can move it to this section if we decide on continuing with this PR :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the deadlocking issue true of any work queue though? There's nothing particularly special about the system work queue other than it tends to get used by default a lot?
I also think its worth pointing out a simple scenario where this occurs as well.
E.g. one work item is taking a semaphore a subsequent work item is giving. Work queue is now dead locked.
Blocking calls aren't inherently the issue here either I'd note, its a possible symptom but not the cause of the deadlock.
A call to i2c_transfer() for example in a work queue item is a blocking call, and may cause the work queue thread to pend. Just because it blocks doesn't inherently mean there will be a deadlock!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the deadlocking issue true of any work queue though? There's nothing particularly special about the system work queue other than it tends to get used by default a lot?
I also think its worth pointing out a simple scenario where this occurs as well.
E.g. one work item is taking a semaphore a subsequent work item is giving. Work queue is now dead locked.
Blocking calls aren't inherently the issue here either I'd note, its a possible symptom but not the cause of the deadlock.
A call to i2c_transfer() for example in a work queue item is a blocking call, and may cause the work queue thread to pend. Just because it blocks doesn't inherently mean there will be a deadlock!
The "which is available to any application or kernel code" part it what makes it true especially for the sys workq, given an owner of the queue would know all work passed to the queue, so can prevent deadlocks and manage latencies :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems reasonable to me--particularly since it provides a means to allow a project to choose whether or not to allow blocking operations in the system work queue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure the test is in the wrong spot. Agree with the feature on the whole though.
Personally I'm neutral on whether this should apply to all work queues or just the system work queue. Doesn't seem like there's much value to an app trying to block in its own queue, but at the same time it has clear and definable semantics and I guess there's no reason to disallow it.
@@ -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 |
There was a problem hiding this comment.
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
@@ -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(); |
There was a problem hiding this comment.
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).
I think it's worth noting that one category of system workqueue "misuse" that this would not prevent, is when a work item doesn't block on any kernel object but still ends up blocking the workqueue itself due to spending lots of time doing "something", e.g. crypto operations lasting for multiple seconds, like we've seen in #84216. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems reasonable to me--particularly since it provides a means to allow a project to choose whether or not to allow blocking operations in the system work queue.
I don't believe this leads to a choice at all. If there is an option to disable all blocking on the system workqueue, all in-tree system workqueue users need to avoid blocking. Otherwise you can't turn on the feature without the application breaking immediately.
IMO the only reliable way the choice can work is if it defaults to non-blocking in-tree, and downstream users can enable blocking only if their application needs it for whatever reason.
The problem is that you cannot turn this on by default, because there are too many in-tree users that rely on the blocking behavior. If I am wrong and you can enable this by default, I'm happy to give a +1. My 2 cents.
Are there any such users? I don't think it's unreasonable at all that we enforce "No Blocking The System Work Queue" rule for in-tree code even if we are more flexible for apps, in which case this PR would still have value. |
if I understand this correctly, wouldnt this require a tree wide effort to change anything that blocks? For example, these GPIO drivers would need to be updated zephyr/drivers/gpio/gpio_tca6424a.c Lines 180 to 229 in 1446416
zephyr/drivers/gpio/gpio_sx1509b.c Lines 189 to 227 in 1446416
zephyr/drivers/gpio/gpio_pcf857x.c Lines 85 to 118 in 1446416
zephyr/drivers/gpio/gpio_pcal64xxa.c Lines 198 to 275 in 1446416
zephyr/drivers/gpio/gpio_mcp23xxx.c Lines 405 to 465 in 1446416
|
Any fetch+get sensor with trigger handling optionally uses the "GLOBAL_THREAD" which is the system workqueue. Probably 50+ drivers. Mostly fixable by find/replacing k_work_submit with k_work_submit_to_queue with a sensor specific thread perhaps... This will add a stack which is quite possibly non-negligible for people that were using this method instead of the per sensor thread. |
I have created an alternative to this PR which monitors for work taking too long rather than if the work blocks, see #88345 |
It is inherently unsafe to call blocking APIs from the system work queue, as it is shared opaquely between modules which may use the system work queue internally to unblock the blocking API call (deadlock).
This particular deadlock is extraordinarily hard to hunt down, given there is no hard fault, and it affects seemingly random parts of the system. Drivers and subsystems just "stop responding" from a seemingly arbitrary, unrelated API call.
Subsystems which rely heavily on the system workqueue already adhere to the fact that blocking API calls are unsafe, and have implemented their own workarounds to address this.
This PR addresses the issue by extending the documentation to state that it is inherently unsafe to use the system work queue for blocking API calls, and introduces an optional check similar to a stack sentinal or spinlock verify, which invokes a kernel oops if a work item passed to the system work queue attempts a blocking call. Lastly, the new kernel API
k_is_in_sys_work()
for checking if a calling context is within a work item handler passed to the system workqueue is added.Note the new API is
k_is_in_sys_work()
, notk_is_in_sys_workqueue_thread()
. The thread is allowed to unready when it is not busy, between items for example, it is only while executing a work item handler it is not allowed to unready :)