1 /*
  2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "gc/shared/gc_globals.hpp"
 26 #include "logging/log.hpp"
 27 #include "logging/logStream.hpp"
 28 #include "memory/resourceArea.hpp"
 29 #include "memory/universe.hpp"
 30 #include "runtime/javaThread.hpp"
 31 #include "runtime/mutexLocker.hpp"
 32 #include "runtime/safepoint.hpp"
 33 #include "runtime/vmThread.hpp"
 34 #include "utilities/vmError.hpp"
 35 
 36 // Mutexes used in the VM (see comment in mutexLocker.hpp):
 37 
 38 Mutex*   NMethodState_lock            = nullptr;
 39 Monitor* SystemDictionary_lock        = nullptr;
 40 Mutex*   InvokeMethodTypeTable_lock   = nullptr;
 41 Monitor* InvokeMethodIntrinsicTable_lock = nullptr;
 42 Mutex*   SharedDictionary_lock        = nullptr;
 43 Monitor* ClassInitError_lock          = nullptr;
 44 Mutex*   Module_lock                  = nullptr;
 45 Mutex*   CompiledIC_lock              = nullptr;
 46 Mutex*   VMStatistic_lock             = nullptr;
 47 Mutex*   JmethodIdCreation_lock       = nullptr;
 48 Mutex*   JfieldIdCreation_lock        = nullptr;
 49 Monitor* JNICritical_lock             = nullptr;
 50 Mutex*   JvmtiThreadState_lock        = nullptr;
 51 Monitor* EscapeBarrier_lock           = nullptr;
 52 Monitor* JvmtiVTMSTransition_lock     = nullptr;
 53 Mutex*   JvmtiVThreadSuspend_lock     = nullptr;
 54 Monitor* Heap_lock                    = nullptr;
 55 #if INCLUDE_PARALLELGC
 56 Mutex*   PSOldGenExpand_lock      = nullptr;
 57 #endif
 58 Mutex*   AdapterHandlerLibrary_lock   = nullptr;
 59 Mutex*   SignatureHandlerLibrary_lock = nullptr;
 60 Mutex*   VtableStubs_lock             = nullptr;
 61 Mutex*   SymbolArena_lock             = nullptr;
 62 Monitor* StringDedup_lock             = nullptr;
 63 Mutex*   StringDedupIntern_lock       = nullptr;
 64 Monitor* CodeCache_lock               = nullptr;
 65 Mutex*   TouchedMethodLog_lock        = nullptr;
 66 Mutex*   RetData_lock                 = nullptr;
 67 Monitor* VMOperation_lock             = nullptr;
 68 Monitor* ThreadsLockThrottle_lock     = nullptr;
 69 Monitor* Threads_lock                 = nullptr;
 70 Mutex*   NonJavaThreadsList_lock      = nullptr;
 71 Mutex*   NonJavaThreadsListSync_lock  = nullptr;
 72 Monitor* STS_lock                     = nullptr;
 73 Mutex*   MonitoringSupport_lock       = nullptr;
 74 Monitor* ConcurrentGCBreakpoints_lock = nullptr;
 75 Mutex*   Compile_lock                 = nullptr;
 76 Monitor* CompileTaskWait_lock         = nullptr;
 77 Monitor* MethodCompileQueue_lock      = nullptr;
 78 Monitor* CompileThread_lock           = nullptr;
 79 Monitor* Compilation_lock             = nullptr;
 80 Mutex*   CompileStatistics_lock       = nullptr;
 81 Mutex*   DirectivesStack_lock         = nullptr;
 82 Monitor* Terminator_lock              = nullptr;
 83 Monitor* InitCompleted_lock           = nullptr;
 84 Monitor* BeforeExit_lock              = nullptr;
 85 Monitor* Notify_lock                  = nullptr;
 86 Mutex*   ExceptionCache_lock          = nullptr;
 87 Mutex*   TrainingData_lock            = nullptr;
 88 Monitor* TrainingReplayQueue_lock     = nullptr;
 89 #ifndef PRODUCT
 90 Mutex*   FullGCALot_lock              = nullptr;
 91 #endif
 92 
 93 Mutex*   tty_lock                     = nullptr;
 94 
 95 Mutex*   RawMonitor_lock              = nullptr;
 96 Mutex*   PerfDataMemAlloc_lock        = nullptr;
 97 Mutex*   PerfDataManager_lock         = nullptr;
 98 
 99 #if INCLUDE_G1GC
100 Monitor* G1CGC_lock                   = nullptr;
101 Mutex*   G1FreeList_lock              = nullptr;
102 Mutex*   G1MarkStackChunkList_lock    = nullptr;
103 Mutex*   G1MarkStackFreeList_lock     = nullptr;
104 Monitor* G1OldGCCount_lock            = nullptr;
105 Mutex*   G1OldSets_lock               = nullptr;
106 Mutex*   G1ReviseYoungLength_lock     = nullptr;
107 Monitor* G1RootRegionScan_lock        = nullptr;
108 Mutex*   G1RareEvent_lock             = nullptr;
109 Mutex*   G1Uncommit_lock              = nullptr;
110 #endif
111 
112 Mutex*   Management_lock              = nullptr;
113 Monitor* MonitorDeflation_lock        = nullptr;
114 Monitor* Service_lock                 = nullptr;
115 Monitor* Notification_lock            = nullptr;
116 Monitor* PeriodicTask_lock            = nullptr;
117 Monitor* RedefineClasses_lock         = nullptr;
118 Mutex*   Verify_lock                  = nullptr;
119 
120 #if INCLUDE_JFR
121 Mutex*   JfrStacktrace_lock           = nullptr;
122 Monitor* JfrMsg_lock                  = nullptr;
123 Mutex*   JfrBuffer_lock               = nullptr;
124 #endif
125 
126 Mutex*   CodeHeapStateAnalytics_lock  = nullptr;
127 
128 Mutex*   ExternalsRecorder_lock       = nullptr;
129 
130 Mutex*   AOTCodeCStrings_lock         = nullptr;
131 
132 Monitor* ContinuationRelativize_lock  = nullptr;
133 
134 Mutex*   Metaspace_lock               = nullptr;
135 Monitor* MetaspaceCritical_lock       = nullptr;
136 Mutex*   ClassLoaderDataGraph_lock    = nullptr;
137 Monitor* ThreadsSMRDelete_lock        = nullptr;
138 Mutex*   ThreadIdTableCreate_lock     = nullptr;
139 Mutex*   SharedDecoder_lock           = nullptr;
140 Mutex*   DCmdFactory_lock             = nullptr;
141 Mutex*   NMTQuery_lock                = nullptr;
142 Mutex*   NMTCompilationCostHistory_lock = nullptr;
143 Mutex*   NmtVirtualMemory_lock          = nullptr;
144 
145 #if INCLUDE_CDS
146 #if INCLUDE_JVMTI
147 Mutex*   CDSClassFileStream_lock      = nullptr;
148 #endif
149 Mutex*   DumpTimeTable_lock           = nullptr;
150 Mutex*   CDSLambda_lock               = nullptr;
151 Mutex*   DumpRegion_lock              = nullptr;
152 Mutex*   ClassListFile_lock           = nullptr;
153 Mutex*   UnregisteredClassesTable_lock= nullptr;
154 Mutex*   LambdaFormInvokers_lock      = nullptr;
155 Mutex*   ScratchObjects_lock          = nullptr;
156 Mutex*   FinalImageRecipes_lock       = nullptr;
157 #endif // INCLUDE_CDS
158 Mutex*   Bootclasspath_lock           = nullptr;
159 
160 #if INCLUDE_JVMCI
161 Monitor* JVMCI_lock                   = nullptr;
162 Monitor* JVMCIRuntime_lock            = nullptr;
163 #endif
164 
165 // Only one RecursiveMutex
166 RecursiveMutex* MultiArray_lock       = nullptr;
167 
168 #ifdef ASSERT
169 void assert_locked_or_safepoint(const Mutex* lock) {
170   if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
171   // check if this thread owns the lock (common case)
172   assert(lock != nullptr, "Need non-null lock");
173   if (lock->owned_by_self()) return;
174   if (SafepointSynchronize::is_at_safepoint()) return;
175   if (!Universe::is_fully_initialized()) return;
176   fatal("must own lock %s", lock->name());
177 }
178 
179 // a stronger assertion than the above
180 void assert_lock_strong(const Mutex* lock) {
181   if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
182   assert(lock != nullptr, "Need non-null lock");
183   if (lock->owned_by_self()) return;
184   fatal("must own lock %s", lock->name());
185 }
186 #endif
187 
188 #define MUTEX_STORAGE_NAME(name) name##_storage
189 #define MUTEX_STORAGE(name, type) alignas(type) static uint8_t MUTEX_STORAGE_NAME(name)[sizeof(type)]
190 #define MUTEX_DEF(name, type, pri, ...) {                                                       \
191   assert(name == nullptr, "Mutex/Monitor initialized twice");                                   \
192   MUTEX_STORAGE(name, type);                                                                    \
193   name = ::new(static_cast<void*>(MUTEX_STORAGE_NAME(name))) type((pri), #name, ##__VA_ARGS__); \
194   Mutex::add_mutex(name);                                                                       \
195 }
196 #define MUTEX_DEFN(name, type, pri, ...) MUTEX_DEF(name, type, Mutex::pri, ##__VA_ARGS__)
197 
198 // Specify relative ranked lock
199 #ifdef ASSERT
200 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEF(name, type, (held_lock)->rank() - 1, ##__VA_ARGS__)
201 #else
202 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEFN(name, type, safepoint, ##__VA_ARGS__)
203 #endif
204 
205 // Using Padded subclasses to prevent false sharing of these global monitors and mutexes.
206 void mutex_init() {
207   MUTEX_DEFN(tty_lock                        , PaddedMutex  , tty);      // allow to lock in VM
208 
209   MUTEX_DEFN(STS_lock                        , PaddedMonitor, nosafepoint);
210 
211 #if INCLUDE_G1GC
212   if (UseG1GC) {
213     MUTEX_DEFN(G1CGC_lock                    , PaddedMonitor, nosafepoint);
214     MUTEX_DEFN(G1FreeList_lock               , PaddedMutex  , service-1);
215     MUTEX_DEFN(G1MarkStackChunkList_lock     , PaddedMutex  , nosafepoint);
216     MUTEX_DEFN(G1MarkStackFreeList_lock      , PaddedMutex  , nosafepoint);
217     MUTEX_DEFN(G1OldSets_lock                , PaddedMutex  , nosafepoint);
218     MUTEX_DEFN(G1RootRegionScan_lock         , PaddedMonitor, nosafepoint-1);
219     MUTEX_DEFN(G1Uncommit_lock               , PaddedMutex  , service-2);
220   }
221 #endif
222 
223   MUTEX_DEFN(MonitoringSupport_lock          , PaddedMutex  , service-1);        // used for serviceability monitoring support
224 
225   MUTEX_DEFN(StringDedup_lock                , PaddedMonitor, nosafepoint);
226   MUTEX_DEFN(StringDedupIntern_lock          , PaddedMutex  , nosafepoint);
227   MUTEX_DEFN(RawMonitor_lock                 , PaddedMutex  , nosafepoint-1);
228 
229   MUTEX_DEFN(Metaspace_lock                  , PaddedMutex  , nosafepoint-3);
230   MUTEX_DEFN(MetaspaceCritical_lock          , PaddedMonitor, nosafepoint-1);
231 
232   MUTEX_DEFN(MonitorDeflation_lock           , PaddedMonitor, nosafepoint);      // used for monitor deflation thread operations
233   MUTEX_DEFN(Service_lock                    , PaddedMonitor, service);          // used for service thread operations
234   MUTEX_DEFN(Notification_lock               , PaddedMonitor, service);          // used for notification thread operations
235 
236   MUTEX_DEFN(JmethodIdCreation_lock          , PaddedMutex  , nosafepoint-1);    // used for creating jmethodIDs can also lock HandshakeState_lock
237   MUTEX_DEFN(InvokeMethodTypeTable_lock      , PaddedMutex  , safepoint);
238   MUTEX_DEFN(InvokeMethodIntrinsicTable_lock , PaddedMonitor, safepoint);
239   MUTEX_DEFN(AdapterHandlerLibrary_lock      , PaddedMutex  , safepoint);
240   MUTEX_DEFN(SharedDictionary_lock           , PaddedMutex  , safepoint);
241   MUTEX_DEFN(VMStatistic_lock                , PaddedMutex  , safepoint);
242   MUTEX_DEFN(SignatureHandlerLibrary_lock    , PaddedMutex  , safepoint);
243   MUTEX_DEFN(SymbolArena_lock                , PaddedMutex  , nosafepoint);
244   MUTEX_DEFN(ExceptionCache_lock             , PaddedMutex  , safepoint);
245 #ifndef PRODUCT
246   MUTEX_DEFN(FullGCALot_lock                 , PaddedMutex  , safepoint); // a lock to make FullGCALot MT safe
247 #endif
248   MUTEX_DEFN(BeforeExit_lock                 , PaddedMonitor, safepoint);
249 
250   MUTEX_DEFN(NonJavaThreadsList_lock         , PaddedMutex  , nosafepoint-1);
251   MUTEX_DEFN(NonJavaThreadsListSync_lock     , PaddedMutex  , nosafepoint);
252 
253   MUTEX_DEFN(RetData_lock                    , PaddedMutex  , safepoint);
254   MUTEX_DEFN(Terminator_lock                 , PaddedMonitor, safepoint, true);
255   MUTEX_DEFN(InitCompleted_lock              , PaddedMonitor, nosafepoint);
256   MUTEX_DEFN(Notify_lock                     , PaddedMonitor, safepoint, true);
257 
258   MUTEX_DEFN(JfieldIdCreation_lock           , PaddedMutex  , safepoint);
259 
260   MUTEX_DEFN(CompiledIC_lock                 , PaddedMutex  , nosafepoint);  // locks VtableStubs_lock
261   MUTEX_DEFN(MethodCompileQueue_lock         , PaddedMonitor, safepoint);
262   MUTEX_DEFN(TrainingData_lock               , PaddedMutex  , nosafepoint);
263   MUTEX_DEFN(TrainingReplayQueue_lock        , PaddedMonitor, safepoint);
264   MUTEX_DEFN(CompileStatistics_lock          , PaddedMutex  , safepoint);
265   MUTEX_DEFN(DirectivesStack_lock            , PaddedMutex  , nosafepoint);
266 
267   MUTEX_DEFN(JvmtiVTMSTransition_lock        , PaddedMonitor, safepoint);   // used for Virtual Thread Mount State transition management
268   MUTEX_DEFN(JvmtiVThreadSuspend_lock        , PaddedMutex,   nosafepoint-1);
269   MUTEX_DEFN(EscapeBarrier_lock              , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI
270   MUTEX_DEFN(Management_lock                 , PaddedMutex  , safepoint);   // used for JVM management
271 
272   MUTEX_DEFN(ConcurrentGCBreakpoints_lock    , PaddedMonitor, safepoint, true);
273   MUTEX_DEFN(TouchedMethodLog_lock           , PaddedMutex  , safepoint);
274 
275   MUTEX_DEFN(CompileThread_lock              , PaddedMonitor, safepoint);
276   MUTEX_DEFN(PeriodicTask_lock               , PaddedMonitor, safepoint, true);
277   MUTEX_DEFN(RedefineClasses_lock            , PaddedMonitor, safepoint);
278   MUTEX_DEFN(Verify_lock                     , PaddedMutex  , safepoint);
279   MUTEX_DEFN(ClassLoaderDataGraph_lock       , PaddedMutex  , safepoint);
280 
281   if (WhiteBoxAPI) {
282     MUTEX_DEFN(Compilation_lock              , PaddedMonitor, nosafepoint);
283   }
284 
285 #if INCLUDE_JFR
286   MUTEX_DEFN(JfrBuffer_lock                  , PaddedMutex  , event);
287   MUTEX_DEFN(JfrMsg_lock                     , PaddedMonitor, event);
288   MUTEX_DEFN(JfrStacktrace_lock              , PaddedMutex  , event);
289 #endif
290 
291   MUTEX_DEFN(ContinuationRelativize_lock     , PaddedMonitor, nosafepoint-3);
292   MUTEX_DEFN(CodeHeapStateAnalytics_lock     , PaddedMutex  , safepoint);
293   MUTEX_DEFN(ThreadsSMRDelete_lock           , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock
294   MUTEX_DEFN(ThreadIdTableCreate_lock        , PaddedMutex  , safepoint);
295   MUTEX_DEFN(DCmdFactory_lock                , PaddedMutex  , nosafepoint);
296   MUTEX_DEFN(NMTQuery_lock                   , PaddedMutex  , safepoint);
297   MUTEX_DEFN(NMTCompilationCostHistory_lock  , PaddedMutex  , nosafepoint);
298   MUTEX_DEFN(NmtVirtualMemory_lock           , PaddedMutex  , service-4); // Must be lower than G1Mapper_lock used from G1RegionsSmallerThanCommitSizeMapper::commit_regions
299 #if INCLUDE_CDS
300 #if INCLUDE_JVMTI
301   MUTEX_DEFN(CDSClassFileStream_lock         , PaddedMutex  , safepoint);
302 #endif
303   MUTEX_DEFN(DumpTimeTable_lock              , PaddedMutex  , nosafepoint);
304   MUTEX_DEFN(CDSLambda_lock                  , PaddedMutex  , nosafepoint);
305   MUTEX_DEFL(DumpRegion_lock                 , PaddedMutex  , DumpTimeTable_lock);
306   MUTEX_DEFN(ClassListFile_lock              , PaddedMutex  , nosafepoint);
307   MUTEX_DEFN(UnregisteredClassesTable_lock   , PaddedMutex  , nosafepoint-1);
308   MUTEX_DEFN(LambdaFormInvokers_lock         , PaddedMutex  , safepoint);
309   MUTEX_DEFL(ScratchObjects_lock             , PaddedMutex  , DumpTimeTable_lock);
310   MUTEX_DEFN(FinalImageRecipes_lock          , PaddedMutex  , nosafepoint);
311 #endif // INCLUDE_CDS
312   MUTEX_DEFN(Bootclasspath_lock              , PaddedMutex  , nosafepoint);
313 
314 #if INCLUDE_JVMCI
315   // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock
316   MUTEX_DEFN(JVMCIRuntime_lock               , PaddedMonitor, safepoint, true);
317 #endif
318 
319   MUTEX_DEFN(ThreadsLockThrottle_lock        , PaddedMonitor, safepoint);
320 
321   // These locks have relative rankings, and inherit safepoint checking attributes from that rank.
322   MUTEX_DEFL(VtableStubs_lock               , PaddedMutex  , CompiledIC_lock);  // Also holds DumpTimeTable_lock
323   MUTEX_DEFL(CodeCache_lock                 , PaddedMonitor, VtableStubs_lock);
324   MUTEX_DEFL(NMethodState_lock              , PaddedMutex  , CodeCache_lock);
325 
326   // tty_lock is held when printing nmethod and its relocations which use this lock.
327   MUTEX_DEFL(ExternalsRecorder_lock         , PaddedMutex  , tty_lock);
328 
329   MUTEX_DEFL(AOTCodeCStrings_lock           , PaddedMutex  , tty_lock);
330 
331   MUTEX_DEFL(Threads_lock                   , PaddedMonitor, CompileThread_lock, true);
332   MUTEX_DEFL(Compile_lock                   , PaddedMutex  , MethodCompileQueue_lock);
333   MUTEX_DEFL(JNICritical_lock               , PaddedMonitor, AdapterHandlerLibrary_lock); // used for JNI critical regions
334   MUTEX_DEFL(Heap_lock                      , PaddedMonitor, JNICritical_lock);
335 
336   MUTEX_DEFL(PerfDataMemAlloc_lock          , PaddedMutex  , Heap_lock);
337   MUTEX_DEFL(PerfDataManager_lock           , PaddedMutex  , Heap_lock);
338   MUTEX_DEFL(VMOperation_lock               , PaddedMonitor, Heap_lock, true);
339   MUTEX_DEFL(ClassInitError_lock            , PaddedMonitor, Threads_lock);
340 
341 #if INCLUDE_G1GC
342   if (UseG1GC) {
343     MUTEX_DEFL(G1OldGCCount_lock            , PaddedMonitor, Threads_lock, true);
344     MUTEX_DEFL(G1RareEvent_lock             , PaddedMutex  , Threads_lock, true);
345     MUTEX_DEFL(G1ReviseYoungLength_lock     , PaddedMutex  , Threads_lock, true);
346   }
347 #endif
348 
349   MUTEX_DEFL(CompileTaskWait_lock           , PaddedMonitor, MethodCompileQueue_lock);
350 
351 #if INCLUDE_PARALLELGC
352   if (UseParallelGC) {
353     MUTEX_DEFL(PSOldGenExpand_lock          , PaddedMutex  , Heap_lock, true);
354   }
355 #endif
356   MUTEX_DEFL(Module_lock                    , PaddedMutex  ,  ClassLoaderDataGraph_lock);
357   MUTEX_DEFL(SystemDictionary_lock          , PaddedMonitor, Module_lock);
358 #if INCLUDE_JVMCI
359   // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock
360   MUTEX_DEFL(JVMCI_lock                     , PaddedMonitor, JVMCIRuntime_lock);
361 #endif
362   MUTEX_DEFL(JvmtiThreadState_lock          , PaddedMutex  , JvmtiVTMSTransition_lock);   // Used by JvmtiThreadState/JvmtiEventController
363   MUTEX_DEFL(SharedDecoder_lock             , PaddedMutex  , NmtVirtualMemory_lock); // Must be lower than NmtVirtualMemory_lock due to MemTracker::print_containing_region
364 
365   // Allocate RecursiveMutex
366   MultiArray_lock = new RecursiveMutex();
367 }
368 
369 #undef MUTEX_DEFL
370 #undef MUTEX_DEFN
371 #undef MUTEX_DEF
372 #undef MUTEX_STORAGE
373 #undef MUTEX_STORAGE_NAME
374 
375 void MutexLockerImpl::post_initialize() {
376   // Print mutex ranks if requested.
377   LogTarget(Info, vmmutex) lt;
378   if (lt.is_enabled()) {
379     ResourceMark rm;
380     LogStream ls(lt);
381     Mutex::print_lock_ranks(&ls);
382   }
383 }
384 
385 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
386   if (SafepointSynchronize::is_at_safepoint()) {
387     _locked = false;
388   } else {
389     _mutex = mutex;
390     _locked = true;
391     _mutex->lock();
392   }
393 }