1 /*
  2  * Copyright (c) 1997, 2021, 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 "precompiled.hpp"
 26 #include "gc/shared/gc_globals.hpp"
 27 #include "memory/universe.hpp"
 28 #include "runtime/mutexLocker.hpp"
 29 #include "runtime/os.inline.hpp"
 30 #include "runtime/safepoint.hpp"
 31 #include "runtime/thread.inline.hpp"
 32 #include "runtime/vmThread.hpp"
 33 
 34 // Mutexes used in the VM (see comment in mutexLocker.hpp):
 35 //
 36 // Note that the following pointers are effectively final -- after having been
 37 // set at JVM startup-time, they should never be subsequently mutated.
 38 // Instead of using pointers to malloc()ed monitors and mutexes we should consider
 39 // eliminating the indirection and using instances instead.
 40 // Consider using GCC's __read_mostly.
 41 
 42 Mutex*   Patching_lock                = NULL;
 43 Mutex*   CompiledMethod_lock          = NULL;
 44 Monitor* SystemDictionary_lock        = NULL;
 45 Mutex*   SharedDictionary_lock        = NULL;
 46 Monitor* ClassInitError_lock          = NULL;
 47 Mutex*   Module_lock                  = NULL;
 48 Mutex*   CompiledIC_lock              = NULL;
 49 Mutex*   InlineCacheBuffer_lock       = NULL;
 50 Mutex*   VMStatistic_lock             = NULL;
 51 Mutex*   JNIHandleBlockFreeList_lock  = NULL;
 52 Mutex*   JmethodIdCreation_lock       = NULL;
 53 Mutex*   JfieldIdCreation_lock        = NULL;
 54 Monitor* JNICritical_lock             = NULL;
 55 Mutex*   JvmtiThreadState_lock        = NULL;
 56 Monitor* EscapeBarrier_lock           = NULL;
 57 Monitor* JvmtiVTMT_lock               = NULL;
 58 Monitor* Heap_lock                    = NULL;
 59 Mutex*   ExpandHeap_lock              = NULL;
 60 Mutex*   AdapterHandlerLibrary_lock   = NULL;
 61 Mutex*   SignatureHandlerLibrary_lock = NULL;
 62 Mutex*   VtableStubs_lock             = NULL;
 63 Mutex*   SymbolArena_lock             = NULL;
 64 Monitor* StringDedup_lock             = NULL;
 65 Mutex*   StringDedupIntern_lock       = NULL;
 66 Monitor* CodeCache_lock               = NULL;
 67 Monitor* CodeSweeper_lock             = NULL;
 68 Mutex*   MethodData_lock              = NULL;
 69 Mutex*   TouchedMethodLog_lock        = NULL;
 70 Mutex*   RetData_lock                 = NULL;
 71 Monitor* VMOperation_lock             = NULL;
 72 Monitor* Threads_lock                 = NULL;
 73 Mutex*   NonJavaThreadsList_lock      = NULL;
 74 Mutex*   NonJavaThreadsListSync_lock  = NULL;
 75 Monitor* CGC_lock                     = NULL;
 76 Monitor* STS_lock                     = NULL;
 77 Monitor* G1OldGCCount_lock            = NULL;
 78 Mutex*   G1DetachedRefinementStats_lock = NULL;
 79 Mutex*   MarkStackFreeList_lock       = NULL;
 80 Mutex*   MarkStackChunkList_lock      = NULL;
 81 Mutex*   MonitoringSupport_lock       = NULL;
 82 Mutex*   ParGCRareEvent_lock          = NULL;
 83 Monitor* ConcurrentGCBreakpoints_lock = NULL;
 84 Mutex*   Compile_lock                 = NULL;
 85 Monitor* MethodCompileQueue_lock      = NULL;
 86 Monitor* CompileThread_lock           = NULL;
 87 Monitor* Compilation_lock             = NULL;
 88 Mutex*   CompileTaskAlloc_lock        = NULL;
 89 Mutex*   CompileStatistics_lock       = NULL;
 90 Mutex*   DirectivesStack_lock         = NULL;
 91 Mutex*   MultiArray_lock              = NULL;
 92 Monitor* Terminator_lock              = NULL;
 93 Monitor* InitCompleted_lock           = NULL;
 94 Monitor* BeforeExit_lock              = NULL;
 95 Monitor* Notify_lock                  = NULL;
 96 Mutex*   ExceptionCache_lock          = NULL;
 97 Mutex*   NMethodSweeperStats_lock     = NULL;
 98 #ifndef PRODUCT
 99 Mutex*   FullGCALot_lock              = NULL;
100 #endif
101 
102 Mutex*   tty_lock                     = NULL;
103 
104 Mutex*   RawMonitor_lock              = NULL;
105 Mutex*   PerfDataMemAlloc_lock        = NULL;
106 Mutex*   PerfDataManager_lock         = NULL;
107 Mutex*   OopMapCacheAlloc_lock        = NULL;
108 
109 Mutex*   FreeList_lock                = NULL;
110 Mutex*   OldSets_lock                 = NULL;
111 Mutex*   Uncommit_lock                = NULL;
112 Monitor* RootRegionScan_lock          = NULL;
113 
114 Mutex*   Management_lock              = NULL;
115 Monitor* MonitorDeflation_lock        = NULL;
116 Monitor* Service_lock                 = NULL;
117 Monitor* Notification_lock            = NULL;
118 Monitor* PeriodicTask_lock            = NULL;
119 Monitor* RedefineClasses_lock         = NULL;
120 Mutex*   Verify_lock                  = NULL;
121 Monitor* Zip_lock                     = NULL;
122 
123 #if INCLUDE_JFR
124 Mutex*   JfrStacktrace_lock           = NULL;
125 Monitor* JfrMsg_lock                  = NULL;
126 Mutex*   JfrBuffer_lock               = NULL;
127 Monitor* JfrThreadSampler_lock        = NULL;
128 #endif
129 
130 #ifndef SUPPORTS_NATIVE_CX8
131 Mutex*   UnsafeJlong_lock             = NULL;
132 #endif
133 Mutex*   CodeHeapStateAnalytics_lock  = NULL;
134 
135 Mutex*   Metaspace_lock               = NULL;
136 Mutex*   ClassLoaderDataGraph_lock    = NULL;
137 Monitor* ThreadsSMRDelete_lock        = NULL;
138 Mutex*   ThreadIdTableCreate_lock     = NULL;
139 Mutex*   SharedDecoder_lock           = NULL;
140 Mutex*   DCmdFactory_lock             = NULL;
141 #if INCLUDE_NMT
142 Mutex*   NMTQuery_lock                = NULL;
143 #endif
144 #if INCLUDE_CDS
145 #if INCLUDE_JVMTI
146 Mutex*   CDSClassFileStream_lock      = NULL;
147 #endif
148 Mutex*   DumpTimeTable_lock           = NULL;
149 Mutex*   CDSLambda_lock               = NULL;
150 Mutex*   DumpRegion_lock              = NULL;
151 Mutex*   ClassListFile_lock           = NULL;
152 Mutex*   UnregisteredClassesTable_lock= NULL;
153 Mutex*   LambdaFormInvokers_lock      = NULL;
154 #endif // INCLUDE_CDS
155 Mutex*   Bootclasspath_lock           = NULL;
156 
157 #if INCLUDE_JVMCI
158 Monitor* JVMCI_lock                   = NULL;
159 #endif
160 
161 
162 #define MAX_NUM_MUTEX 128
163 static Mutex* _mutex_array[MAX_NUM_MUTEX];
164 static int _num_mutex;
165 
166 #ifdef ASSERT
167 void assert_locked_or_safepoint(const Mutex* lock) {
168   // check if this thread owns the lock (common case)
169   assert(lock != NULL, "Need non-NULL lock");
170   if (lock->owned_by_self()) return;
171   if (SafepointSynchronize::is_at_safepoint()) return;
172   if (!Universe::is_fully_initialized()) return;
173   fatal("must own lock %s", lock->name());
174 }
175 
176 // a weaker assertion than the above
177 void assert_locked_or_safepoint_weak(const Mutex* lock) {
178   assert(lock != NULL, "Need non-NULL lock");
179   if (lock->is_locked()) return;
180   if (SafepointSynchronize::is_at_safepoint()) return;
181   if (!Universe::is_fully_initialized()) return;
182   fatal("must own lock %s", lock->name());
183 }
184 
185 // a stronger assertion than the above
186 void assert_lock_strong(const Mutex* lock) {
187   assert(lock != NULL, "Need non-NULL lock");
188   if (lock->owned_by_self()) return;
189   fatal("must own lock %s", lock->name());
190 }
191 
192 void assert_locked_or_safepoint_or_handshake(const Mutex* lock, const JavaThread* thread) {
193   if (thread->is_handshake_safe_for(Thread::current())) return;
194   assert_locked_or_safepoint(lock);
195 }
196 #endif
197 
198 static void add_mutex(Mutex* var) {
199   assert(_num_mutex < MAX_NUM_MUTEX, "increase MAX_NUM_MUTEX");
200   _mutex_array[_num_mutex++] = var;
201 }
202 
203 #define def(var, type, pri, vm_block) {       \
204   var = new type(Mutex::pri, #var, vm_block); \
205   add_mutex(var);                             \
206 }
207 
208 // Specify relative ranked lock
209 #ifdef ASSERT
210 #define defl(var, type, held_lock, vm_block) {         \
211   var = new type(held_lock->rank()-1, #var, vm_block); \
212   add_mutex(var);                                      \
213 }
214 #else
215 #define defl(var, type, held_lock, vm_block) {         \
216   var = new type(Mutex::safepoint, #var, vm_block);    \
217   add_mutex(var);                                      \
218 }
219 #endif
220 
221 // Using Padded subclasses to prevent false sharing of these global monitors and mutexes.
222 void mutex_init() {
223   def(tty_lock                     , PaddedMutex  , tty,            true);      // allow to lock in VM
224 
225   def(STS_lock                     , PaddedMonitor, nosafepoint,    true);
226 
227   if (UseG1GC) {
228     def(CGC_lock                   , PaddedMonitor, nosafepoint,    true);
229 
230     def(G1DetachedRefinementStats_lock, PaddedMutex, nosafepoint-2, true);
231 
232     def(FreeList_lock              , PaddedMutex  , service-1,      true);
233     def(OldSets_lock               , PaddedMutex  , nosafepoint,    true);
234     def(Uncommit_lock              , PaddedMutex  , service-2,      true);
235     def(RootRegionScan_lock        , PaddedMonitor, nosafepoint-1,  true);
236 
237     def(MarkStackFreeList_lock     , PaddedMutex  , nosafepoint,    true);
238     def(MarkStackChunkList_lock    , PaddedMutex  , nosafepoint,    true);
239 
240     def(MonitoringSupport_lock     , PaddedMutex  , service-1,      true);      // used for serviceability monitoring support
241   }
242   def(StringDedup_lock             , PaddedMonitor, nosafepoint,    true);
243   def(StringDedupIntern_lock       , PaddedMutex  , nosafepoint,    true);
244   def(ParGCRareEvent_lock          , PaddedMutex  , safepoint,      true);
245   def(RawMonitor_lock              , PaddedMutex  , nosafepoint-1,  true);
246 
247   def(Metaspace_lock               , PaddedMutex  , nosafepoint-3,  true);
248 
249   def(Patching_lock                , PaddedMutex  , nosafepoint,    true);      // used for safepointing and code patching.
250   def(MonitorDeflation_lock        , PaddedMonitor, nosafepoint,    true);      // used for monitor deflation thread operations
251   def(Service_lock                 , PaddedMonitor, service,        true);      // used for service thread operations
252 
253   if (UseNotificationThread) {
254     def(Notification_lock          , PaddedMonitor, service,        true);  // used for notification thread operations
255   } else {
256     Notification_lock = Service_lock;
257   }
258 
259   def(JmethodIdCreation_lock       , PaddedMutex  , nosafepoint-2,  true); // used for creating jmethodIDs.
260 
261   def(SharedDictionary_lock        , PaddedMutex  , safepoint,      true);
262   def(VMStatistic_lock             , PaddedMutex  , safepoint,      false);
263   def(JNIHandleBlockFreeList_lock  , PaddedMutex  , nosafepoint-1,  true);      // handles are used by VM thread
264   def(SignatureHandlerLibrary_lock , PaddedMutex  , safepoint,      false);
265   def(SymbolArena_lock             , PaddedMutex  , nosafepoint,    true);
266   def(ExceptionCache_lock          , PaddedMutex  , safepoint,      false);
267 #ifndef PRODUCT
268   def(FullGCALot_lock              , PaddedMutex  , safepoint,      false); // a lock to make FullGCALot MT safe
269 #endif
270   def(BeforeExit_lock              , PaddedMonitor, safepoint,      true);
271 
272   def(NonJavaThreadsList_lock      , PaddedMutex,   nosafepoint-1,  true);
273   def(NonJavaThreadsListSync_lock  , PaddedMutex,   nosafepoint,    true);
274 
275   def(RetData_lock                 , PaddedMutex  , safepoint,      false);
276   def(Terminator_lock              , PaddedMonitor, safepoint,      true);
277   def(InitCompleted_lock           , PaddedMonitor, nosafepoint,    true);
278   def(Notify_lock                  , PaddedMonitor, safepoint,      true);
279   def(AdapterHandlerLibrary_lock   , PaddedMutex  , safepoint,      true);
280 
281   def(Heap_lock                    , PaddedMonitor, safepoint,      false); // Doesn't safepoint check during termination.
282   def(JfieldIdCreation_lock        , PaddedMutex  , safepoint,      true);  // jfieldID, Used in VM_Operation
283 
284   def(CompiledIC_lock              , PaddedMutex  , nosafepoint,    true);  // locks VtableStubs_lock, InlineCacheBuffer_lock
285   def(MethodCompileQueue_lock      , PaddedMonitor, safepoint,      false);
286   def(CompileStatistics_lock       , PaddedMutex  , safepoint,      false);
287   def(DirectivesStack_lock         , PaddedMutex  , nosafepoint,    true);
288   def(MultiArray_lock              , PaddedMutex  , safepoint,      false);
289 
290   def(JvmtiThreadState_lock        , PaddedMutex  , safepoint,      false); // Used by JvmtiThreadState/JvmtiEventController
291   def(EscapeBarrier_lock           , PaddedMonitor, nosafepoint,    true);  // Used to synchronize object reallocation/relocking triggered by JVMTI
292   def(JvmtiVTMT_lock               , PaddedMonitor, nosafepoint,    true);  // used for Virtual Thread Mount Transition management
293   def(Management_lock              , PaddedMutex  , safepoint,      false); // used for JVM management
294 
295   def(ConcurrentGCBreakpoints_lock , PaddedMonitor, safepoint,      true);
296   def(MethodData_lock              , PaddedMutex  , safepoint,      false);
297   def(TouchedMethodLog_lock        , PaddedMutex  , safepoint,      false);
298 
299   def(CompileThread_lock           , PaddedMonitor, safepoint,      false);
300   def(PeriodicTask_lock            , PaddedMonitor, safepoint,      true);
301   def(RedefineClasses_lock         , PaddedMonitor, safepoint,      true);
302   def(Verify_lock                  , PaddedMutex,   safepoint,      true);
303 
304   if (WhiteBoxAPI) {
305     def(Compilation_lock           , PaddedMonitor, nosafepoint,    true);
306   }
307 
308 #if INCLUDE_JFR
309   def(JfrBuffer_lock               , PaddedMutex  , nosafepoint,       true);
310   def(JfrStacktrace_lock           , PaddedMutex  , stackwatermark-1,  true);
311   def(JfrThreadSampler_lock        , PaddedMonitor, nosafepoint,       true);
312 #endif
313 
314 #ifndef SUPPORTS_NATIVE_CX8
315   def(UnsafeJlong_lock             , PaddedMutex  , nosafepoint,    true);
316 #endif
317 
318   def(CodeHeapStateAnalytics_lock  , PaddedMutex  , safepoint,      false);
319   def(NMethodSweeperStats_lock     , PaddedMutex  , nosafepoint,    true);
320   def(ThreadsSMRDelete_lock        , PaddedMonitor, nosafepoint-3,  true); // Holds ConcurrentHashTableResize_lock
321   def(ThreadIdTableCreate_lock     , PaddedMutex  , safepoint,      false);
322   def(SharedDecoder_lock           , PaddedMutex  , tty-1,          true);
323   def(DCmdFactory_lock             , PaddedMutex  , nosafepoint,    true);
324 #if INCLUDE_NMT
325   def(NMTQuery_lock                , PaddedMutex  , safepoint,      false);
326 #endif
327 #if INCLUDE_CDS
328 #if INCLUDE_JVMTI
329   def(CDSClassFileStream_lock      , PaddedMutex  , safepoint,      false);
330 #endif
331   def(DumpTimeTable_lock           , PaddedMutex  , nosafepoint,    true);
332   def(CDSLambda_lock               , PaddedMutex  , nosafepoint,    true);
333   def(DumpRegion_lock              , PaddedMutex  , nosafepoint,    true);
334   def(ClassListFile_lock           , PaddedMutex  , nosafepoint,    true);
335   def(LambdaFormInvokers_lock      , PaddedMutex  , safepoint,      false);
336 #endif // INCLUDE_CDS
337   def(Bootclasspath_lock           , PaddedMutex  , nosafepoint,    true);
338   def(Zip_lock                     , PaddedMonitor, nosafepoint-1,  true); // Holds DumpTimeTable_lock
339 
340 #if INCLUDE_JVMCI
341   def(JVMCI_lock                   , PaddedMonitor, safepoint,      true);
342 #endif
343 
344   // These locks have relative rankings, and inherit safepoint checking attributes from that rank.
345   defl(InlineCacheBuffer_lock      , PaddedMutex  , CompiledIC_lock,  true);
346   defl(VtableStubs_lock            , PaddedMutex  , CompiledIC_lock,  true);  // Also holds DumpTimeTable_lock
347   defl(CodeCache_lock              , PaddedMonitor, VtableStubs_lock, true);
348   defl(CompiledMethod_lock         , PaddedMutex  , CodeCache_lock,   true);
349   defl(CodeSweeper_lock            , PaddedMonitor, CompiledMethod_lock, true);
350 
351   defl(Threads_lock                , PaddedMonitor, CompileThread_lock, true);
352   defl(Heap_lock                   , PaddedMonitor, MultiArray_lock,    false);
353   defl(Compile_lock                , PaddedMutex ,  MethodCompileQueue_lock, false);
354 
355   defl(PerfDataMemAlloc_lock       , PaddedMutex  , Heap_lock,         true);
356   defl(PerfDataManager_lock        , PaddedMutex  , Heap_lock,         true);
357   defl(ClassLoaderDataGraph_lock   , PaddedMutex  , MultiArray_lock,   false);
358   defl(VMOperation_lock            , PaddedMonitor, Compile_lock,      true);
359   defl(ClassInitError_lock         , PaddedMonitor, Threads_lock,      true);
360 
361   if (UseG1GC) {
362     defl(G1OldGCCount_lock         , PaddedMonitor, Threads_lock,      true);
363   }
364   defl(CompileTaskAlloc_lock       , PaddedMutex ,  MethodCompileQueue_lock,   true);
365   defl(ExpandHeap_lock             , PaddedMutex ,  Heap_lock,                 true);
366   defl(OopMapCacheAlloc_lock       , PaddedMutex ,  Threads_lock,              true);
367   defl(Module_lock                 , PaddedMutex ,  ClassLoaderDataGraph_lock, false);
368   defl(SystemDictionary_lock       , PaddedMonitor, Module_lock,               true);
369   defl(JNICritical_lock            , PaddedMonitor, MultiArray_lock,           true); // used for JNI critical regions
370 
371 #if INCLUDE_JFR
372   defl(JfrMsg_lock                 , PaddedMonitor, Module_lock,               true);
373 #endif
374 }
375 
376 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
377   if (SafepointSynchronize::is_at_safepoint()) {
378     _locked = false;
379   } else {
380     _mutex = mutex;
381     _locked = true;
382     _mutex->lock();
383   }
384 }
385 
386 // Print all mutexes/monitors that are currently owned by a thread; called
387 // by fatal error handler.
388 void print_owned_locks_on_error(outputStream* st) {
389   st->print("VM Mutex/Monitor currently owned by a thread: ");
390   bool none = true;
391   for (int i = 0; i < _num_mutex; i++) {
392      // see if it has an owner
393      if (_mutex_array[i]->owner() != NULL) {
394        if (none) {
395           // print format used by Mutex::print_on_error()
396           st->print_cr(" ([mutex/lock_event])");
397           none = false;
398        }
399        _mutex_array[i]->print_on_error(st);
400        st->cr();
401      }
402   }
403   if (none) st->print_cr("None");
404 }