RootBankCache must use Acquire-Release semantics when accessing BankForks::root (#30936)

This commit is contained in:
Brooks 2023-03-28 14:35:02 -04:00 committed by GitHub
parent 359d9c7b9f
commit e7887cfb06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -30,7 +30,10 @@ pub struct ReadOnlyAtomicSlot {
impl ReadOnlyAtomicSlot {
pub fn get(&self) -> Slot {
self.slot.load(Ordering::Relaxed)
// The expectation is that an instance `ReadOnlyAtomicSlot` is on a different thread than
// BankForks *and* this instance is being accessed *without* locking BankForks first.
// Thus, to ensure atomic ordering correctness, we must use Acquire-Release semantics.
self.slot.load(Ordering::Acquire)
}
}
@ -235,7 +238,10 @@ impl BankForks {
highest_confirmed_root: Option<Slot>,
) -> (Vec<Arc<Bank>>, SetRootMetrics) {
let old_epoch = self.root_bank().epoch();
self.root.store(root, Ordering::Relaxed);
// To support `RootBankCache` (via `ReadOnlyAtomicSlot`) accessing `root` *without* locking
// BankForks first *and* from a different thread, this store *must* be at least Release to
// ensure atomic ordering correctness.
self.root.store(root, Ordering::Release);
let root_bank = self
.banks