From 332563cc1ef269a2550bf92629f5cd15ff1ae8f8 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 10 Apr 2026 04:06:34 +0000 Subject: [PATCH] Fix data race on `pmull_runtime_flag` in crc32c The global `bool pmull_runtime_flag` is written from `IsFastCrc32Supported` and `Choose_Extend` without synchronization, causing a TSan report when multiple RocksDB instances are opened concurrently. Make it `std::atomic` with relaxed ordering, since it is always set to the same value. Co-Authored-By: Claude Opus 4.6 (1M context) (cherry picked from commit dabb7ba1e714369d2a7d3f1a7482b98324f60554) --- util/crc32c.cc | 7 ++++--- util/crc32c_arm64.cc | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/util/crc32c.cc b/util/crc32c.cc index c00276d765ce..107509271144 100644 --- a/util/crc32c.cc +++ b/util/crc32c.cc @@ -12,6 +12,7 @@ #include "util/crc32c.h" #include +#include #include #include @@ -53,7 +54,7 @@ ASSERT_FEATURE_COMPAT_HEADER(); #endif #if defined(HAVE_ARM64_CRC) -bool pmull_runtime_flag = false; +std::atomic pmull_runtime_flag{false}; #endif namespace ROCKSDB_NAMESPACE::crc32c { @@ -377,7 +378,7 @@ std::string IsFastCrc32Supported() { if (crc32c_runtime_check()) { has_fast_crc = true; arch = "Arm64"; - pmull_runtime_flag = crc32c_pmull_runtime_check(); + pmull_runtime_flag.store(crc32c_pmull_runtime_check(), std::memory_order_relaxed); } else { has_fast_crc = false; arch = "Arm64"; @@ -1108,7 +1109,7 @@ static inline Function Choose_Extend() { return isAltiVec() ? ExtendPPCImpl : ExtendImpl; #elif defined(HAVE_ARM64_CRC) if(crc32c_runtime_check()) { - pmull_runtime_flag = crc32c_pmull_runtime_check(); + pmull_runtime_flag.store(crc32c_pmull_runtime_check(), std::memory_order_relaxed); return ExtendARMImpl; } else { return ExtendImpl; diff --git a/util/crc32c_arm64.cc b/util/crc32c_arm64.cc index 47c22c030fc5..2e90eb7a6cab 100644 --- a/util/crc32c_arm64.cc +++ b/util/crc32c_arm64.cc @@ -5,6 +5,8 @@ #include "util/crc32c_arm64.h" +#include + #if defined(HAVE_ARM64_CRC) #if defined(__linux__) @@ -49,7 +51,7 @@ } while (0) #endif -extern bool pmull_runtime_flag; +extern std::atomic pmull_runtime_flag; uint32_t crc32c_runtime_check(void) { #if defined(ROCKSDB_AUXV_GETAUXVAL_PRESENT) || defined(__FreeBSD__) @@ -124,7 +126,7 @@ uint32_t crc32c_arm64(uint32_t crc, unsigned char const* data, size_t len) { * Raspberry Pi supports crc32 but doesn't support pmull. * Skip Crc32c Parallel computation if no crypto extension available. */ - if (pmull_runtime_flag) { + if (pmull_runtime_flag.load(std::memory_order_relaxed)) { /* Macro (HAVE_ARM64_CRYPTO) is used for compiling check */ #ifdef HAVE_ARM64_CRYPTO /* Crc32c Parallel computation