bare-metal: Mutex is not safe on multi-core systems
On a multi-core system, disabling interrupts does not prevent the other core from operating, and so values protected by a bare_metal::Mutex will be incorrectly marked Sync.
Since the overwhelming majority of embedded use cases are single-core, I propose putting a prominent warning in the Mutex docstring for now, and working to develop a safe multi-core extension to the Mutex which can be enabled with a feature gate. Probably something using an atomic to implement a spinlock on top of requiring a CriticalSection.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 24 (22 by maintainers)
Only a small fraction of people actually chimed in here, so it’s a bit early to make such statements.
Indeed I don’t have any issues with the
Mutexbeing here but I don’t have any problems with moving it either. I’m just pointing out the obvious that any change to a foundational crate like this needs to be planned and executed with extreme care.We still don’t have the ability to do something like a crater run to ensure that we’re not accidentally causing major damage to the ecosystem, so I’d rather we treat with extreme caution.
Indeed.
This issue was effectively closed by https://github.com/rust-embedded/wg/pull/419; the Mutex in bare-metal is only considered sound on single-core systems and some other abstraction will be required for multi-core systems.
There seems to be some confusion about what
CriticalSectionandMutexprovide here.CriticalSectionis just a token that guarantees, for the duration of its existence, that the current core is in a critical section (ie. has any interrupts that could preempt execution disabled). The contract of this type means that any safe code that constructs aCriticalSectionwithout disabling interrupts is unsound.Mutexthen takes this no-interrupts guarantee to provide mutual exclusion. This is sound if we adopt https://github.com/rust-embedded/wg/pull/419, but not at the current state. It’s sound in any case if the data is only accessible from a single core.I’ll improve the docs of
CriticalSectionto make its contract clearer.@therealprof I was looking into this again this morning. It is not
Mutexthat is unsound for multiple cores. It’s usingcortex_m::interrupt::freeto provide theCriticalSectionthat is unsound on multi-core.It is possible to implement a different mechanism to provide the
CritcialSectionthat would be sound.https://gist.github.com/rubberduck203/20415cb0bdc0726b2ebf0903e7193665