scheduling: functions that might reschedule assume interrupts enabled#20941
Draft
derMihai wants to merge 5 commits intoRIOT-OS:masterfrom
Draft
scheduling: functions that might reschedule assume interrupts enabled#20941derMihai wants to merge 5 commits intoRIOT-OS:masterfrom
derMihai wants to merge 5 commits intoRIOT-OS:masterfrom
Conversation
Contributor
Author
|
I do realize that for some of the functions, it does make sense to store and restore interrupt state. These are the ones that may be called interrupt context (e.g. |
Contributor
Author
|
@kaspar030 what is the scope of this test? |
Contributor
Author
|
|
Contributor
Author
|
Run the native tests on native and samd20-xpro. tests/core/thread_priority_inversion and tests/sys/event_source failed as expected. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contribution description
Currently, most functions that might reschedule contain logic that enables them to be called with interrupts disabled. This is not only going against the scope disabling interrupts in the first place, but also doesn't work. All these functions end up calling
thread_yield_higher(), which will fail to suspend the current thread.Depending on the use-case, this can be more or less catastrophic. If the tread doesn't go to sleep,
thread_yield_higher()will just trigger the switch once the interrupts are enabled(although on some architectures this might be bad due to unflushed instruction caches - seethe cortex m code). But if the thread is set to go to sleep, the current thread will continue execution although it shouldn't. For example, the following assertion will be triggered:This breaks both program logic and leaves the scheduler in a messed up state.
thread_yieldand removed any IRQ state saving logic whereverthread_yield*()was found and the code is clearly not allowed to be executed from IRQ context, so don't expect it to be comprehensive. There might still be places that I missed and still feature useless IRQ state saving logic.I also added an assertion in
thread_yield_higher()s.t. if it's executed in thread context, the interrupts must be enabled.The case of the mutex
mutex_lock()really shouldn't be called with interrupts disabled, as it reschedules. However, it's fine to do it if we know there is no thread blocking on that mutex. This is the case ofvfs_bind_stdio(), which is called in early init, with interrupts disabled.I therefore left the mutex the way it is. The assertion in
thread_yield_higher()will still catch a reschedule.Testing procedure
I run the automatic tests on native, and I get some failures (see comments).
Issues/PRs references
#20940