Skip to content

Fixed the RP2040 interrupt problem #8505

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

Merged
merged 2 commits into from
Oct 24, 2023

Conversation

Snipeye
Copy link

@Snipeye Snipeye commented Oct 23, 2023

I noticed a bug in the pulseio module for the RP2040 when using it:

Under specific circumstances, an item getting pushed into the queue during the PIO interrupt handler can be overwritten.

This happens specifically when the "len--" of popleft() is being executed: len is loaded into a register, then the register is decremented, then written back to memory. If the interrupt happens after len is loaded from memory into a register, or after it's decremented - but before it's written back to memory - then the value added in that interrupt will be overwritten (since the register state is restored when the interrupt ends, and the old decremented value of len is written back in, even though len was incremented in the handler).

The fix is not particularly elegant, but it requires no spinlocks or interrupt disabling, and in testing has not failed.

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

Thanks for coming up with an elegant fix. But it also perfectly ok to disable interrupts, and that's typically what we'd do in this kind of case, with common_hal_mcu_disable_interrupts() and common_hal_mcu_enable_interrupts(). That is straightforward, and fast to execute. It would be more robust against future code changes.

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

Thank you for noticing and fixing!

@dhalbert dhalbert merged commit 2c25e82 into adafruit:8.2.x Oct 24, 2023
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.

3 participants