Skip to content
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

cpu/stm32/gpio: fix IRQ handler #16272

Merged
merged 1 commit into from
Apr 2, 2021
Merged

Conversation

jue89
Copy link
Contributor

@jue89 jue89 commented Apr 1, 2021

Contribution description

I've found a race condition in the GPIO IRQ handling. Without this patch, there is a small probability to get caught in an endless loop since the related Pending Request bit is never cleared.

What happend:

  1. The GPIO's related IMR bit is enabled
  2. The condition of the interrupt is met (like the GPIO went from HIGH to LOW) and the corresponding PR bit is set
  3. The IMR bit is cleared by the program
  4. The program gets interrupted and isr_exti() is called
  5. isr_exti() reads the PR bits and masks out the bit that caused its execution. Thus, the PR bit is never cleared and isr_exti() is called again after cortexm_isr_end().

Testing procedure

The DOSE driver uses the GPIO IRQ to detect start bits of Ethernet frames. This IRQ is then disabled during transmission and enabled after the whole frame has been received. Without this patch I was able to trigger the race condition within a few hours by creating some heavy bus traffic. After applying this patch, I was able to run this driver for days (tests are still ongoing).

To verify the mechanics (i.e. the PR bit must be cleared even if the corresponding IMR bit is unset) please see RM0360 Page 173 Figure 21

Issues/PRs references

none

@jue89 jue89 added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Area: cpu Area: CPU/MCU ports labels Apr 1, 2021
@jue89 jue89 force-pushed the fix/stm32_gpio_irq branch from 34d1bc0 to 43f83a5 Compare April 1, 2021 18:31
@jue89 jue89 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 1, 2021
Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

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

Make sense.

@fjmolinas
Copy link
Contributor

@jue89 was this recently introduced or has this bug been forever present?

@fjmolinas
Copy link
Contributor

@jue89 was this recently introduced or has this bug been forever present?

In any case I think this should be backported no?

@jue89
Copy link
Contributor Author

jue89 commented Apr 2, 2021

Thanks for your review and responses!

@jue89 was this recently introduced or has this bug been forever present?

In any case I think this should be backported no?

I've used the STM32F103RCT (which doesn't seem to be affected by this race condition even though the same pattern is used in the gpio_f1.c implementation) before I switched to the STM32F030CCT. So this seems to be a bug that has been introduced in 2016: df99604

So which releases should receive a backport?

Nevertheless - can this PR be merged? Is one ACK enough?

@fjmolinas
Copy link
Contributor

Nevertheless - can this PR be merged? Is one ACK enough?

Yes one ACK is enough.

@fjmolinas fjmolinas merged commit e04dd4d into RIOT-OS:master Apr 2, 2021
@fjmolinas
Copy link
Contributor

I've used the STM32F103RCT (which doesn't seem to be affected by this race condition even though the same pattern is used in the gpio_f1.c implementation) before I switched to the STM32F030CCT. So this seems to be a bug that has been introduced in 2016: df99604

Hmm @jia200x what do you think?

@jue89
Copy link
Contributor Author

jue89 commented Apr 2, 2021

Thanks for merging!

I came to the conclusion that we should port this patch to the gpio_f1.c implementation as well as the implementation for the STM32G0, STM32L5 and STM32MP1. The datasheet implies that the race condition could happen there as well. Or what do you think?

@jue89 jue89 deleted the fix/stm32_gpio_irq branch April 2, 2021 12:04
@kaspar030 kaspar030 added this to the Release 2021.04 milestone Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: cpu Area: CPU/MCU ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants