Skip to content

Conversation

maksfb
Copy link
Contributor

@maksfb maksfb commented Sep 10, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

@llvm/pr-subscribers-bolt

Author: Maksim Panchenko (maksfb)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/157774.diff

5 Files Affected:

  • (modified) bolt/include/bolt/Rewrite/MetadataRewriters.h (+4-2)
  • (modified) bolt/lib/Rewrite/CMakeLists.txt (+1)
  • (added) bolt/lib/Rewrite/RSeqRewriter.cpp (+71)
  • (modified) bolt/lib/Rewrite/RewriteInstance.cpp (+2)
  • (added) bolt/test/X86/rseq.s (+32)
diff --git a/bolt/include/bolt/Rewrite/MetadataRewriters.h b/bolt/include/bolt/Rewrite/MetadataRewriters.h
index b71bd6cad2505..429c2d7712979 100644
--- a/bolt/include/bolt/Rewrite/MetadataRewriters.h
+++ b/bolt/include/bolt/Rewrite/MetadataRewriters.h
@@ -19,12 +19,14 @@ class BinaryContext;
 
 // The list of rewriter build functions.
 
-std::unique_ptr<MetadataRewriter> createLinuxKernelRewriter(BinaryContext &);
-
 std::unique_ptr<MetadataRewriter> createBuildIDRewriter(BinaryContext &);
 
+std::unique_ptr<MetadataRewriter> createLinuxKernelRewriter(BinaryContext &);
+
 std::unique_ptr<MetadataRewriter> createPseudoProbeRewriter(BinaryContext &);
 
+std::unique_ptr<MetadataRewriter> createRSeqRewriter(BinaryContext &);
+
 std::unique_ptr<MetadataRewriter> createSDTRewriter(BinaryContext &);
 
 } // namespace bolt
diff --git a/bolt/lib/Rewrite/CMakeLists.txt b/bolt/lib/Rewrite/CMakeLists.txt
index 775036063dd5a..12914bfe64301 100644
--- a/bolt/lib/Rewrite/CMakeLists.txt
+++ b/bolt/lib/Rewrite/CMakeLists.txt
@@ -24,6 +24,7 @@ add_llvm_library(LLVMBOLTRewrite
   BuildIDRewriter.cpp
   PseudoProbeRewriter.cpp
   RewriteInstance.cpp
+  RSeqRewriter.cpp
   SDTRewriter.cpp
 
   NO_EXPORT
diff --git a/bolt/lib/Rewrite/RSeqRewriter.cpp b/bolt/lib/Rewrite/RSeqRewriter.cpp
new file mode 100644
index 0000000000000..7cbe9ac17da52
--- /dev/null
+++ b/bolt/lib/Rewrite/RSeqRewriter.cpp
@@ -0,0 +1,71 @@
+//===- bolt/Rewrite/RSeqRewriter.cpp --------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Basic support for restartable sequences used by tcmalloc. Prevent critical
+// section overrides by ignoring optimizations in containing functions.
+//
+// References:
+//   * https://google.github.io/tcmalloc/rseq.html
+//   * tcmalloc/internal/percpu_rseq_x86_64.S
+//
+//===----------------------------------------------------------------------===//
+
+#include "bolt/Core/BinaryFunction.h"
+#include "bolt/Rewrite/MetadataRewriter.h"
+#include "bolt/Rewrite/MetadataRewriters.h"
+#include "llvm/Support/Errc.h"
+
+using namespace llvm;
+using namespace bolt;
+
+namespace {
+
+class RSeqRewriter final : public MetadataRewriter {
+public:
+  RSeqRewriter(StringRef Name, BinaryContext &BC)
+      : MetadataRewriter(Name, BC) {}
+
+  Error postCFGInitializer() override {
+    for (const BinarySection &Section : BC.allocatableSections()) {
+      if (Section.getName() != "__rseq_cs")
+        continue;
+
+      auto handleRelocation = [&](const Relocation &Rel) {
+        BinaryFunction *BF = nullptr;
+        if (Rel.Symbol)
+          BF = BC.getFunctionForSymbol(Rel.Symbol);
+        else if (Relocation::isRelative(Rel.Type))
+          BF = BC.getBinaryFunctionContainingAddress(Rel.Addend);
+
+        if (BF) {
+          BC.outs() << "BOLT-INFO: restartable sequence detected in " << *BF
+                    << ". Function will not be optimized\n";
+          BF->setSimple(false);
+        } else {
+          BC.errs() << "BOLT-WARNING: no function found matching dynamic "
+                       "relocation in __rseq_cs\n";
+        }
+      };
+
+      for (const Relocation &Rel : Section.dynamicRelocations())
+        handleRelocation(Rel);
+
+      for (const Relocation &Rel : Section.relocations())
+        handleRelocation(Rel);
+    }
+
+    return Error::success();
+  }
+};
+
+} // namespace
+
+std::unique_ptr<MetadataRewriter>
+llvm::bolt::createRSeqRewriter(BinaryContext &BC) {
+  return std::make_unique<RSeqRewriter>("rseq-cs-rewriter", BC);
+}
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index a6e4dbc9c192f..6e07d2ea8d975 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -3338,6 +3338,8 @@ void RewriteInstance::initializeMetadataManager() {
 
   MetadataManager.registerRewriter(createPseudoProbeRewriter(*BC));
 
+  MetadataManager.registerRewriter(createRSeqRewriter(*BC));
+
   MetadataManager.registerRewriter(createSDTRewriter(*BC));
 }
 
diff --git a/bolt/test/X86/rseq.s b/bolt/test/X86/rseq.s
new file mode 100644
index 0000000000000..7ebc57e1128f4
--- /dev/null
+++ b/bolt/test/X86/rseq.s
@@ -0,0 +1,32 @@
+## Check that llvm-bolt avoids optimization of functions referenced from
+## __rseq_cs section, i.e. containing critical sections used by restartable
+## sequences in tcmalloc.
+
+# RUN: %clang %cflags %s -o %t -nostdlib -no-pie -Wl,-q
+# RUN: llvm-bolt %t -o %t.bolt --print-cfg 2>&1 \
+# RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIE
+# RUN: %clang %cflags %s -o %t.pie -nostdlib -pie -Wl,-q
+# RUN: llvm-bolt %t.pie -o %t.pie.bolt 2>&1 \
+# RUN:   | FileCheck %s --check-prefix=CHECK-PIE
+
+# CHECK-NO-PIE: Binary Function "_start"
+# CHECK-NO-PIE: IsSimple
+# CHECK-NO-PIE-SAME: 0
+
+# CHECK-PIE: restartable sequence detected in _start
+
+.global _start
+  .type _start, %function
+_start:
+        pushq %rbp
+        mov %rsp, %rbp
+.L1:
+        pop %rbp
+        retq
+.size _start, .-_start
+
+.reloc 0, R_X86_64_NONE
+
+.section __rseq_cs, "aw"
+.balign 32
+  .quad  .L1

@Bradshawz
Copy link

Bradshawz commented Sep 11, 2025

Tried out the patch, it seems to successfully print out all the functions in info but the final binary still crashes.

Test:
Moved the skip function

TcmallocSlab_Internal_Pop.*/Push.* 

back to just being TcmallocSlab_Internal_PopBatch/PushBatch and used this new version of bolt.

Prints out:

BOLT-WARNING: 1566 collisions detected while hashing binary objects. Use -v=1 to see the list.
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs
BOLT-WARNING: failed to patch entries in _ZNSt18condition_variableD1Ev(*2). The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZNSt9exceptionD2Ev(*2). The function will not be optimized
BOLT-WARNING: failed to patch entries in __wt_session_release_dhandle. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZNK5mongo17CollectionCatalog8getEpochEv. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZN5mongo21bsonExtractTypedFieldERKNS_7BSONObjENS_10StringDataENS_8BSONTypeEPNS_11BSONElementE. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZN5mongo23bsonExtractIntegerFieldERKNS_7BSONObjENS_10StringDataEPx. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZNSt18condition_variable10notify_allEv. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZNSt18condition_variable10notify_oneEv. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZNSt18condition_variable4waitERSt11unique_lockISt5mutexE. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZN5mongo3str13escapeForJSONERN3fmt3v1119basic_memory_bufferIcLm500ENS2_6detail9allocatorIcEEEENS_10StringDataEmPm. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZN5mongo10timeseries23CollectionPreConditions29setIsTimeseriesLogicalRequestEb. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZNK5mongo4impl16ParsedUpdateBase10getRequestEv. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZN5mongo4impl16ParsedUpdateBase9getDriverEv. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZN5mongo23waitable_atomic_details9notifyAllEPKv. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZNK5mongo16IDLParserContext23getSerializationContextEv. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZN5mongo5logv210LogManager15getGlobalDomainEv. The function will not be optimized
BOLT-WARNING: failed to patch entries in __wt_txn_parse_timestamp_raw. The function will not be optimized
BOLT-WARNING: failed to patch entries in _ZN5mongo7express27releaseShardFilterResourcesERNS_18write_stage_common14PreWriteFilterE. The function will not be optimized
BOLT-INFO: shared object or position-independent executable detected
BOLT-INFO: Target architecture: aarch64
BOLT-INFO: BOLT version: dd11305a2b57b0da0d1529164ebcbb3a4b1a0343
BOLT-INFO: first alloc address is 0x0
BOLT-INFO: creating new program header table at address 0x7c00000, offset 0x7c00000
BOLT-INFO: enabling relocation mode
BOLT-INFO: enabling lite mode
BOLT-INFO: pre-processing profile using branch profile reader
BOLT-INFO: operating with basic samples profiling data (no LBR).
BOLT-INFO: restartable sequence detected in malloc(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_0/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZdaPv(*6). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_1/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in realloc(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_2/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in realloc(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_3/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in realloc(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_4/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in realloc(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_5/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in realloc(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_6/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in calloc(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_7/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in __libc_memalign(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_8/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in __libc_valloc(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_9/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in __libc_pvalloc(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_10/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in posix_memalign. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_11/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _Znwm(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_12/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZdlPvm(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_13/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZnwmRKSt9nothrow_t(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_14/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZnwmSt11align_val_t(*2). Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_15/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZdlPvmSt11align_val_t. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_16/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in reallocarray. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_17/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in reallocarray. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_18/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in reallocarray. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_19/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in reallocarray. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_20/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in reallocarray. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_21/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in aligned_alloc. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_22/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL13FreeSmallSlowEPvm/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_23/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL13FreeSmallSlowEPvm/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_24/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL16slow_alloc_smallINS0_14TCMallocPolicyINS0_13NullOomPolicyENS0_17MallocAlignPolicyENS0_25AllocationAccessHotPolicyENS0_13NoHooksPolicyENS0_21IsSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmjSA_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_27/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL38alloc_small_sampled_hooks_or_perthreadINS0_14TCMallocPolicyINS0_13NullOomPolicyENS0_17MallocAlignPolicyENS0_25AllocationAccessHotPolicyENS0_13NoHooksPolicyENS0_21IsSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmmSA_m/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_28/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internal18cpu_cache_internal8CpuCacheINS1_15StaticForwarderEE19AllocateSlowNoHooksEm/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_29/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internal18cpu_cache_internal8CpuCacheINS1_15StaticForwarderEE6RefillEim/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_30/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL16slow_alloc_smallINS0_14TCMallocPolicyINS0_15MallocOomPolicyENS0_17MallocAlignPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmjSA_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_31/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL16slow_alloc_smallINS0_14TCMallocPolicyINS0_15MallocOomPolicyENS0_17MallocAlignPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmjSA_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_32/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL38alloc_small_sampled_hooks_or_perthreadINS0_14TCMallocPolicyINS0_15MallocOomPolicyENS0_17MallocAlignPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmmSA_m/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_33/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL16slow_alloc_smallINS0_14TCMallocPolicyINS0_12CppOomPolicyENS0_18DefaultAlignPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmjSA_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_34/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL16slow_alloc_smallINS0_14TCMallocPolicyINS0_12CppOomPolicyENS0_18DefaultAlignPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmjSA_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_35/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL38alloc_small_sampled_hooks_or_perthreadINS0_14TCMallocPolicyINS0_12CppOomPolicyENS0_18DefaultAlignPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmmSA_m/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_36/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL16slow_alloc_smallINS0_14TCMallocPolicyINS0_13NullOomPolicyENS0_18DefaultAlignPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmjSA_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_37/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL38alloc_small_sampled_hooks_or_perthreadINS0_14TCMallocPolicyINS0_13NullOomPolicyENS0_18DefaultAlignPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmmSA_m/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_38/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL16slow_alloc_smallINS0_14TCMallocPolicyINS0_15MallocOomPolicyENS0_13AlignAsPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmjSA_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_39/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL38alloc_small_sampled_hooks_or_perthreadINS0_14TCMallocPolicyINS0_15MallocOomPolicyENS0_13AlignAsPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmmSA_m/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_40/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL16slow_alloc_smallINS0_14TCMallocPolicyINS0_12CppOomPolicyENS0_13AlignAsPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmjSA_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_41/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL16slow_alloc_smallINS0_14TCMallocPolicyINS0_12CppOomPolicyENS0_13AlignAsPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmjSA_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_42/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL38alloc_small_sampled_hooks_or_perthreadINS0_14TCMallocPolicyINS0_12CppOomPolicyENS0_13AlignAsPolicyENS0_25AllocationAccessHotPolicyENS0_17InvokeHooksPolicyENS0_22NonSizeReturningPolicyENS0_24LocalNumaPartitionPolicyEEEEENT_12pointer_typeEmmSA_m/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_43/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internal12_GLOBAL__N_115free_non_normalINS0_13AlignAsPolicyEEEvPvmT_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_44/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internal12_GLOBAL__N_115free_non_normalINS0_18DefaultAlignPolicyEEEvPvmT_/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_45/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internal18cpu_cache_internal8CpuCacheINS1_15StaticForwarderEE21DeallocateSlowNoHooksEPvm/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_25/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internal18cpu_cache_internal8CpuCacheINS1_15StaticForwarderEE14UpdateCapacityEimb/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_26/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internalL15FreeProxyObjectINS0_6StaticEEEvRT_Pvm/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_0/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in _ZN8tcmalloc17tcmalloc_internal6subtle6percpu12TcmallocSlab16CacheCpuSlabSlowEv. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_0/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_PushBatch. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_PushBatch_trampoline. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_PopBatch. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_PopBatch_trampoline. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_0/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_1/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_2/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_3/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_4/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_5/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_6/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_7/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_8/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_9/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_10/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_11/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_12/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_13/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_14/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_15/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_16/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_17/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_18/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_19/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_20/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_21/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_22/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_23/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_24/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_27/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_28/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_29/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_30/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_31/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_32/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_33/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_34/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_35/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_36/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_37/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_38/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_39/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_40/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_41/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_42/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Pop_trampoline_43/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_44/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_45/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_25/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_26/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_Push_trampoline_0/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_StoreCurrentCpu_trampoline_0/1. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_PushBatch_trampoline. Function will not be optimized
BOLT-INFO: restartable sequence detected in TcmallocSlab_Internal_PopBatch_trampoline. Function will not be optimized
BOLT-INFO: number of removed linker-inserted veneers: 0
BOLT-INFO: 2800 out of 183884 functions in the binary (1.5%) have non-empty execution profile
BOLT-INFO: 32 functions with profile could not be optimized
BOLT-INFO: profile quality metrics for the hottest 1000 functions (reporting top 5% values): function CFG discontinuity 64.75%; call graph flow conservation gap 100.00%; CFG flow conservation gap 95.26% (weighted) 100.00% (worst); exception handling usage 0.00% (of total BBEC) 0.00% (of total InvokeEC)
BOLT-INFO: removed 4 empty blocks
BOLT-INFO: basic block reordering modified layout of 1720 functions (61.43% of profiled, 0.93% of total)
BOLT-INFO: 24 Functions were reordered by LoopInversionPass
BOLT-INFO: program-wide dynostats after all optimizations before SCTC and FOP:

          7006598000 : executed forward branches
           788560657 : taken forward branches
           453275000 : executed backward branches
           461244787 : taken backward branches
           342341126 : executed unconditional branches
          2866428000 : all function calls
          1332493000 : indirect calls
           268913000 : PLT calls
        145599083000 : executed instructions
         50856386000 : executed load instructions
         24530291000 : executed store instructions
                   0 : taken jump table branches
                   0 : taken unknown indirect branches
          7802214126 : total branches
          1592146570 : taken branches
          6210067556 : non-taken conditional branches
          1249805444 : taken conditional branches
          7459873000 : all conditional branches
                   0 : linker-inserted veneer calls

          6747164000 : executed forward branches (-3.7%)
           151112377 : taken forward branches (-80.8%)
           712709000 : executed backward branches (+57.2%)
           109079063 : taken backward branches (-76.4%)
           345795572 : executed unconditional branches (+1.0%)
          2866428000 : all function calls (=)
          1332493000 : indirect calls (=)
           268913000 : PLT calls (=)
        146063795000 : executed instructions (+0.3%)
         50856386000 : executed load instructions (=)
         24530291000 : executed store instructions (=)
                   0 : taken jump table branches (=)
                   0 : taken unknown indirect branches (=)
          7805668572 : total branches (+0.0%)
           605987012 : taken branches (-61.9%)
          7199681560 : non-taken conditional branches (+15.9%)
           260191440 : taken conditional branches (-79.2%)
          7459873000 : all conditional branches (=)
                   0 : linker-inserted veneer calls (=)

BOLT-INFO: Starting stub-insertion pass
BOLT-INFO: Inserted 12000 stubs in the hot area and 4736 stubs in the cold area. Shared 0 times, iterated 3 times.
BOLT-INFO: patched build-id (flipped last bit)
BOLT-INFO: setting __hot_start to 0x7e00000
BOLT-INFO: setting __hot_end to 0x7f0a158

Moved the skip func from TcmallocSlab_Internal_PopBatch/PushBatch back to

TcmallocSlab_Internal_Pop.*/Push.* 

and it started working again.

@maksfb
Copy link
Contributor Author

maksfb commented Sep 11, 2025

Thanks for trying the patch out. I'm going to look more into it and specifically at the warnings. Meanwhile, I've updated the patch to ignore the __rseq-cs-referenced functions completely, you can try it and it should work without the need for --skip-funcs=....

@Bradshawz
Copy link

Thanks that seems to work! It also turns out almost all the functions on our skip list seem to have been there due to rseq related reasons.

Copy link
Member

@paschalis-mpeis paschalis-mpeis left a comment

Choose a reason for hiding this comment

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

Overall, the patch looks good, and it was verified that ignoring the function does the trick.

One thing it's not clear to me: is fixed code (non-pie) safe, given the heuristic won't detect which functions to ignore?

For the logs, would it be better to condense the BOLT-INFO messages into a warning summary like:

~ N functionsN functions were found in restartable sequences section and were ignored ..

possibly, including a hint to view full details at higher verbosity? Not a strong opinion.

I believe you will also suppress these messages?

BOLT-WARNING: no function found matching dynamic relocation in __rseq_cs

@maksfb
Copy link
Contributor Author

maksfb commented Sep 23, 2025

One thing it's not clear to me: are static binaries safe, given the heuristic won't detect which functions to ignore?

Hi Paschalis, what do you mean by static binaries? Note that we check both types of relocations.

@paschalis-mpeis
Copy link
Member

Hey Maksim, sorry I meant fixed code, ie the non-pie case listed in the test.

@maksfb
Copy link
Contributor Author

maksfb commented Sep 25, 2025

For non-pie, we rely on linker's --emit-relocs to detect code pointers.

Let me take another look at the warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants