From 3470119370ad8cad4691b0c701a41c14f72fd975 Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Thu, 14 Dec 2023 11:40:03 +1100 Subject: [PATCH] v1.16: Fix - Uses fetch_add() to accumulate usage counters in LoadedPrograms (backport of #34319) (#34431) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix tx_usage_counter use fetch_add. * loaded_programs: fix tx_usage_counter for tombstone entries --------- Co-authored-by: Alexander Meißner --- program-runtime/src/loaded_programs.rs | 30 +++++++++++--------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/program-runtime/src/loaded_programs.rs b/program-runtime/src/loaded_programs.rs index d0a130d859b788..7e4c37daac0b28 100644 --- a/program-runtime/src/loaded_programs.rs +++ b/program-runtime/src/loaded_programs.rs @@ -288,7 +288,7 @@ impl LoadedProgram { effective_slot: self.effective_slot, maybe_expiration_slot: self.maybe_expiration_slot, tx_usage_counter: AtomicU64::new(self.tx_usage_counter.load(Ordering::Relaxed)), - ix_usage_counter: AtomicU64::new(self.tx_usage_counter.load(Ordering::Relaxed)), + ix_usage_counter: AtomicU64::new(self.ix_usage_counter.load(Ordering::Relaxed)), }) } @@ -447,13 +447,11 @@ impl LoadedPrograms { if matches!(existing.program, LoadedProgramType::Unloaded(_)) { // The unloaded program is getting reloaded // Copy over the usage counter to the new entry - let mut usage_count = existing.tx_usage_counter.load(Ordering::Relaxed); - saturating_add_assign!( - usage_count, - entry.tx_usage_counter.load(Ordering::Relaxed) + entry.tx_usage_counter.fetch_add( + existing.tx_usage_counter.load(Ordering::Relaxed), + Ordering::Relaxed, ); - entry.tx_usage_counter.store(usage_count, Ordering::Relaxed); - entry.ix_usage_counter.store( + entry.ix_usage_counter.fetch_add( existing.ix_usage_counter.load(Ordering::Relaxed), Ordering::Relaxed, ); @@ -622,22 +620,20 @@ impl LoadedPrograms { } if current_slot >= entry.effective_slot { - let mut usage_count = - entry.tx_usage_counter.load(Ordering::Relaxed); - saturating_add_assign!(usage_count, count); - entry.tx_usage_counter.store(usage_count, Ordering::Relaxed); + entry.tx_usage_counter.fetch_add(count, Ordering::Relaxed); return Some((key, entry.clone())); } else if entry.is_implicit_delay_visibility_tombstone(current_slot) { // Found a program entry on the current fork, but it's not effective // yet. It indicates that the program has delayed visibility. Return // the tombstone to reflect that. - return Some(( - key, - Arc::new(LoadedProgram::new_tombstone( - entry.deployment_slot, - LoadedProgramType::DelayVisibility, - )), + let entry_to_return = Arc::new(LoadedProgram::new_tombstone( + entry.deployment_slot, + LoadedProgramType::DelayVisibility, )); + entry_to_return + .tx_usage_counter + .fetch_add(count, Ordering::Relaxed); + return Some((key, entry_to_return)); } } }