Skip to content

Conversation

kjbracey
Copy link
Contributor

@kjbracey kjbracey commented Jan 3, 2019

Description

Critical section count/state variables are synchronised by IRQ disabling and
critical section calls themselves, so do not need to be volatile.

This eliminates a couple of unnecessary reads of the counter variable.

Saves a few bytes of ROM, and a bit of time in speed-critical code.

Pull request type

[ ] Fix
[X] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

Reviewers

@scartmell-arm, @c1728p9

Critical section count/state variables are synchronised by IRQ disabling and
critical section calls themselves, so do not need to be volatile.

This eliminates a couple of unnecessary reads of the counter variable.
@ciarmcom ciarmcom requested review from c1728p9 and a user January 3, 2019 12:00
@ciarmcom
Copy link
Member

ciarmcom commented Jan 3, 2019

@kjbracey-arm, thank you for your changes.
@c1728p9 @scartmell-arm @ARMmbed/mbed-os-core @ARMmbed/mbed-os-hal @ARMmbed/mbed-os-maintainers please review.

Copy link
Member

@pan- pan- left a comment

Choose a reason for hiding this comment

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

Good change, it would be interesting to have a mention about volatile uses in the handbook at some point.

Funnily enough that change doesn't change code generated: https://godbolt.org/z/za4blY

@kjbracey
Copy link
Contributor Author

kjbracey commented Jan 3, 2019

hal_critical_section_exit loses 2 load instructions: https://godbolt.org/z/IOL0lW (although 1 load could have already been saved by proper use of -- operator, and other load should probably be an assert anyway).

The other file is just being done for consistency, and doesn't gain anything because it accesses each variable only once per function.

I'm looking through a few other bits of code - getting paranoid about LTO.

@pan-
Copy link
Member

pan- commented Jan 3, 2019

It would be interesting to add the -Wodr warning to LTO gcc's config. In my experience, most of the issues I have faced with LTO were related to ODR violation and these may be hard to find.

@kjbracey
Copy link
Contributor Author

kjbracey commented Jan 3, 2019

My latest bit of paranoia is implementations of hal_critical_section that don't use PRIMASK, eg nRF52. If they're just doing a memory-mapped write to NVIC, that does have a built-in CPU barrier, but not the compiler barrier you get from the PRIMASK intrinsic or assembler... Do our current compilers reorder normal memory accesses across volatile accesses?

(Without LTO, function calls to other compilation units are compiler barriers, which does a lot of lifting. LTO loses that).

@kjbracey
Copy link
Contributor Author

kjbracey commented Jan 3, 2019

Do our current compilers reorder normal memory accesses across volatile accesses?

Immediate answer from an old GCC manual:

Accesses to non-volatile objects are not ordered with respect to volatile accesses. You cannot use a volatile object as a memory barrier to order a sequence of writes to non-volatile memory.

@pan-
Copy link
Member

pan- commented Jan 3, 2019

@kjbracey
Copy link
Contributor Author

kjbracey commented Jan 3, 2019

Interesting link. asm volatile presumably should force ordering with respect to other volatiles, like C volatile accesses, and that's been fixed. It's not enough though - for a critical section, you still need a memory clobber to act as compiler barrier against the normal code inside, and I see current CMSIS has that.

Raised a CMSIS issue here about NVIC_EnableIRQ lacking a barrier: ARM-software/CMSIS_5#493

@0xc0170
Copy link
Contributor

0xc0170 commented Jan 4, 2019

CI started

@mbed-ci
Copy link

mbed-ci commented Jan 7, 2019

Test run: FAILED

Summary: 1 of 7 test jobs failed
Build number : 1
Build artifacts

Failed test jobs:

  • jenkins-ci/mbed-os-ci_build-GCC_ARM

@0xc0170
Copy link
Contributor

0xc0170 commented Jan 7, 2019

We are experiencing CI node failures, few jobs were aborted. We are investigating, will restart the jobs once fixed.

cc @ARMmbed/mbed-os-test

@NirSonnenschein
Copy link
Contributor

re-running to check if CI issue is resolved

@mbed-ci
Copy link

mbed-ci commented Jan 7, 2019

Test run: SUCCESS

Summary: 11 of 11 test jobs passed
Build number : 2
Build artifacts

@0xc0170 0xc0170 merged commit 965f15d into ARMmbed:master Jan 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants