samd21: don't change priority of interrupts, this is really evil#4081
Merged
haukepetersen merged 1 commit intoRIOT-OS:masterfrom Nov 10, 2015
Merged
Conversation
Member
|
See also the discussion in #3450 |
Contributor
Completely right, +1 for the change. Other RTOS put functions in different classes, like "interrupt safe", so it is visible where functions can be called. Until we got that classifying, nested interrupts are a source of painful bugs. |
Member
Author
|
@kaspar030 is this an ACK? |
Contributor
|
ACK and go |
haukepetersen
added a commit
that referenced
this pull request
Nov 10, 2015
samd21: don't change priority of interrupts, this is really evil
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.
Changing interrupt priorities on cortex_m platforms has really bad consequences. RIOT assumes that there is no interrupt nesting (i.e. all IRQs have the same priority) which holds true until you change any IRQ priority.
Having low(er) priority interrupts is fine (they don't preempt e.g. the scheduler), but they can be preempted indeed (e.g. by a timer). When sending messages from within an interrupt callback with
msg_send(), thenmsg_send_int()is used internally.Inside msg_send_int() there's a call to sched_set_status() which assumes not to be preempted. That's where all the fun begins.
I was getting hardfaults inside
sched_run()for some time now, but never really identified the cause. When a call tosched_set_status()gets preempted here the runqueue will beNULLfor that process priority whilerunqueue_bitcachestill indicates a non-empty runqueue.Back in
sched_run()this leads tonext_threadpointing nowhere (yes, on samd21 you can dereference NULL pointer!). Boom.Long story short: don't ever change interrupt priorities unless you really know what you're doing. It's okay to have such a limitation to not call
msg_send()inside an interrupt that has a lower priority, but I wasn't aware of this yet.$ cd cpu/ $ ack-grep NVIC_SetPriorityYields a lot hits that may also lead to bugs like this one.