src/share/vm/memory/gcLocker.hpp

Fri, 20 Sep 2013 10:53:28 +0200

author
stefank
date
Fri, 20 Sep 2013 10:53:28 +0200
changeset 5769
2c022e432e10
parent 4299
f34d701e952e
child 6198
55fb97c4c58d
permissions
-rw-r--r--

8024974: Incorrect use of GC_locker::is_active()
Summary: SymbolTable and StringTable can make calls to GC_locker::is_active() outside a safepoint. This isn't safe because the GC_locker active state (lock count) is only updated at a safepoint and only remains valid as long as _needs_gc is true. However, outside a safepoint_needs_gc can change to false at any time, which makes it impossible to do a correct call to is_active() in that context. In this case these calls can just be removed since the input argument to basic_add() should never be on the heap and so there's no need to check the GC_locker state. This change also adjusts the assert() in is_active() to makes sure all calls to this function are always done under a safepoint.
Reviewed-by: brutisso, dcubed
Contributed-by: per.liden@oracle.com

duke@435 1 /*
never@3494 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_MEMORY_GCLOCKER_HPP
stefank@2314 26 #define SHARE_VM_MEMORY_GCLOCKER_HPP
stefank@2314 27
stefank@2314 28 #include "gc_interface/collectedHeap.hpp"
stefank@2314 29 #include "memory/genCollectedHeap.hpp"
stefank@2314 30 #include "memory/universe.hpp"
stefank@2314 31 #include "oops/oop.hpp"
stefank@4299 32 #include "runtime/thread.inline.hpp"
stefank@2314 33 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 34 # include "os_linux.inline.hpp"
stefank@2314 35 #endif
stefank@2314 36 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 37 # include "os_solaris.inline.hpp"
stefank@2314 38 #endif
stefank@2314 39 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 40 # include "os_windows.inline.hpp"
stefank@2314 41 #endif
never@3156 42 #ifdef TARGET_OS_FAMILY_bsd
never@3156 43 # include "os_bsd.inline.hpp"
never@3156 44 #endif
stefank@2314 45
duke@435 46 // The direct lock/unlock calls do not force a collection if an unlock
duke@435 47 // decrements the count to zero. Avoid calling these if at all possible.
duke@435 48
duke@435 49 class GC_locker: public AllStatic {
duke@435 50 private:
never@3494 51 // The _jni_lock_count keeps track of the number of threads that are
never@3494 52 // currently in a critical region. It's only kept up to date when
never@3494 53 // _needs_gc is true. The current value is computed during
never@3494 54 // safepointing and decremented during the slow path of GC_locker
never@3494 55 // unlocking.
never@3494 56 static volatile jint _jni_lock_count; // number of jni active instances.
never@3494 57
duke@435 58 static volatile jint _lock_count; // number of other active instances
duke@435 59 static volatile bool _needs_gc; // heap is filling, we need a GC
duke@435 60 // note: bool is typedef'd as jint
duke@435 61 static volatile bool _doing_gc; // unlock_critical() is doing a GC
duke@435 62
never@3494 63 #ifdef ASSERT
never@3494 64 // This lock count is updated for all operations and is used to
never@3494 65 // validate the jni_lock_count that is computed during safepoints.
never@3494 66 static volatile jint _debug_jni_lock_count;
never@3494 67 #endif
never@3494 68
duke@435 69 // Accessors
duke@435 70 static bool is_jni_active() {
never@3494 71 assert(_needs_gc, "only valid when _needs_gc is set");
duke@435 72 return _jni_lock_count > 0;
duke@435 73 }
duke@435 74
never@3494 75 // At a safepoint, visit all threads and count the number of active
never@3494 76 // critical sections. This is used to ensure that all active
never@3494 77 // critical sections are exited before a new one is started.
never@3494 78 static void verify_critical_count() NOT_DEBUG_RETURN;
duke@435 79
never@3494 80 static void jni_lock(JavaThread* thread);
never@3494 81 static void jni_unlock(JavaThread* thread);
duke@435 82
never@3576 83 static bool is_active_internal() {
never@3576 84 verify_critical_count();
never@3576 85 return _lock_count > 0 || _jni_lock_count > 0;
never@3576 86 }
never@3576 87
duke@435 88 public:
duke@435 89 // Accessors
never@3576 90 static bool is_active() {
stefank@5769 91 assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint");
never@3576 92 return is_active_internal();
never@3576 93 }
duke@435 94 static bool needs_gc() { return _needs_gc; }
never@3494 95
duke@435 96 // Shorthand
never@3576 97 static bool is_active_and_needs_gc() {
never@3576 98 // Use is_active_internal since _needs_gc can change from true to
never@3576 99 // false outside of a safepoint, triggering the assert in
never@3576 100 // is_active.
never@3576 101 return needs_gc() && is_active_internal();
never@3576 102 }
duke@435 103
never@3494 104 // In debug mode track the locking state at all times
never@3494 105 static void increment_debug_jni_lock_count() {
never@3494 106 #ifdef ASSERT
never@3494 107 assert(_debug_jni_lock_count >= 0, "bad value");
never@3494 108 Atomic::inc(&_debug_jni_lock_count);
never@3494 109 #endif
never@3494 110 }
never@3494 111 static void decrement_debug_jni_lock_count() {
never@3494 112 #ifdef ASSERT
never@3494 113 assert(_debug_jni_lock_count > 0, "bad value");
never@3494 114 Atomic::dec(&_debug_jni_lock_count);
never@3494 115 #endif
never@3494 116 }
never@3494 117
never@3494 118 // Set the current lock count
never@3494 119 static void set_jni_lock_count(int count) {
never@3494 120 _jni_lock_count = count;
never@3494 121 verify_critical_count();
never@3494 122 }
never@3494 123
never@3494 124 // Sets _needs_gc if is_active() is true. Returns is_active().
duke@435 125 static bool check_active_before_gc();
duke@435 126
duke@435 127 // Stalls the caller (who should not be in a jni critical section)
duke@435 128 // until needs_gc() clears. Note however that needs_gc() may be
duke@435 129 // set at a subsequent safepoint and/or cleared under the
duke@435 130 // JNICritical_lock, so the caller may not safely assert upon
duke@435 131 // return from this method that "!needs_gc()" since that is
duke@435 132 // not a stable predicate.
duke@435 133 static void stall_until_clear();
duke@435 134
duke@435 135 // Non-structured GC locking: currently needed for JNI. Use with care!
duke@435 136 static void lock();
duke@435 137 static void unlock();
duke@435 138
duke@435 139 // The following two methods are used for JNI critical regions.
duke@435 140 // If we find that we failed to perform a GC because the GC_locker
duke@435 141 // was active, arrange for one as soon as possible by allowing
duke@435 142 // all threads in critical regions to complete, but not allowing
duke@435 143 // other critical regions to be entered. The reasons for that are:
duke@435 144 // 1) a GC request won't be starved by overlapping JNI critical
duke@435 145 // region activities, which can cause unnecessary OutOfMemory errors.
duke@435 146 // 2) even if allocation requests can still be satisfied before GC locker
duke@435 147 // becomes inactive, for example, in tenured generation possibly with
duke@435 148 // heap expansion, those allocations can trigger lots of safepointing
duke@435 149 // attempts (ineffective GC attempts) and require Heap_lock which
duke@435 150 // slow down allocations tremendously.
duke@435 151 //
duke@435 152 // Note that critical regions can be nested in a single thread, so
duke@435 153 // we must allow threads already in critical regions to continue.
duke@435 154 //
duke@435 155 // JNI critical regions are the only participants in this scheme
duke@435 156 // because they are, by spec, well bounded while in a critical region.
duke@435 157 //
never@3494 158 // Each of the following two method is split into a fast path and a
never@3494 159 // slow path. JNICritical_lock is only grabbed in the slow path.
duke@435 160 // _needs_gc is initially false and every java thread will go
never@3494 161 // through the fast path, which simply increments or decrements the
never@3494 162 // current thread's critical count. When GC happens at a safepoint,
never@3494 163 // GC_locker::is_active() is checked. Since there is no safepoint in
never@3494 164 // the fast path of lock_critical() and unlock_critical(), there is
never@3494 165 // no race condition between the fast path and GC. After _needs_gc
never@3494 166 // is set at a safepoint, every thread will go through the slow path
never@3494 167 // after the safepoint. Since after a safepoint, each of the
never@3494 168 // following two methods is either entered from the method entry and
never@3494 169 // falls into the slow path, or is resumed from the safepoints in
never@3494 170 // the method, which only exist in the slow path. So when _needs_gc
never@3494 171 // is set, the slow path is always taken, till _needs_gc is cleared.
duke@435 172 static void lock_critical(JavaThread* thread);
duke@435 173 static void unlock_critical(JavaThread* thread);
never@3500 174
never@3500 175 static address needs_gc_address() { return (address) &_needs_gc; }
duke@435 176 };
duke@435 177
duke@435 178
duke@435 179 // A No_GC_Verifier object can be placed in methods where one assumes that
duke@435 180 // no garbage collection will occur. The destructor will verify this property
duke@435 181 // unless the constructor is called with argument false (not verifygc).
duke@435 182 //
duke@435 183 // The check will only be done in debug mode and if verifygc true.
duke@435 184
duke@435 185 class No_GC_Verifier: public StackObj {
duke@435 186 friend class Pause_No_GC_Verifier;
duke@435 187
duke@435 188 protected:
duke@435 189 bool _verifygc;
duke@435 190 unsigned int _old_invocations;
duke@435 191
duke@435 192 public:
duke@435 193 #ifdef ASSERT
duke@435 194 No_GC_Verifier(bool verifygc = true);
duke@435 195 ~No_GC_Verifier();
duke@435 196 #else
duke@435 197 No_GC_Verifier(bool verifygc = true) {}
duke@435 198 ~No_GC_Verifier() {}
duke@435 199 #endif
duke@435 200 };
duke@435 201
duke@435 202 // A Pause_No_GC_Verifier is used to temporarily pause the behavior
duke@435 203 // of a No_GC_Verifier object. If we are not in debug mode or if the
duke@435 204 // No_GC_Verifier object has a _verifygc value of false, then there
duke@435 205 // is nothing to do.
duke@435 206
duke@435 207 class Pause_No_GC_Verifier: public StackObj {
duke@435 208 private:
duke@435 209 No_GC_Verifier * _ngcv;
duke@435 210
duke@435 211 public:
duke@435 212 #ifdef ASSERT
duke@435 213 Pause_No_GC_Verifier(No_GC_Verifier * ngcv);
duke@435 214 ~Pause_No_GC_Verifier();
duke@435 215 #else
duke@435 216 Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {}
duke@435 217 ~Pause_No_GC_Verifier() {}
duke@435 218 #endif
duke@435 219 };
duke@435 220
duke@435 221
duke@435 222 // A No_Safepoint_Verifier object will throw an assertion failure if
duke@435 223 // the current thread passes a possible safepoint while this object is
duke@435 224 // instantiated. A safepoint, will either be: an oop allocation, blocking
duke@435 225 // on a Mutex or JavaLock, or executing a VM operation.
duke@435 226 //
duke@435 227 // If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier
duke@435 228 //
duke@435 229 class No_Safepoint_Verifier : public No_GC_Verifier {
duke@435 230 friend class Pause_No_Safepoint_Verifier;
duke@435 231
duke@435 232 private:
duke@435 233 bool _activated;
duke@435 234 Thread *_thread;
duke@435 235 public:
duke@435 236 #ifdef ASSERT
never@533 237 No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) :
never@533 238 No_GC_Verifier(verifygc),
never@533 239 _activated(activated) {
duke@435 240 _thread = Thread::current();
duke@435 241 if (_activated) {
duke@435 242 _thread->_allow_allocation_count++;
duke@435 243 _thread->_allow_safepoint_count++;
duke@435 244 }
duke@435 245 }
duke@435 246
duke@435 247 ~No_Safepoint_Verifier() {
duke@435 248 if (_activated) {
duke@435 249 _thread->_allow_allocation_count--;
duke@435 250 _thread->_allow_safepoint_count--;
duke@435 251 }
duke@435 252 }
duke@435 253 #else
duke@435 254 No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){}
duke@435 255 ~No_Safepoint_Verifier() {}
duke@435 256 #endif
duke@435 257 };
duke@435 258
duke@435 259 // A Pause_No_Safepoint_Verifier is used to temporarily pause the
duke@435 260 // behavior of a No_Safepoint_Verifier object. If we are not in debug
duke@435 261 // mode then there is nothing to do. If the No_Safepoint_Verifier
duke@435 262 // object has an _activated value of false, then there is nothing to
duke@435 263 // do for safepoint and allocation checking, but there may still be
duke@435 264 // something to do for the underlying No_GC_Verifier object.
duke@435 265
duke@435 266 class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier {
duke@435 267 private:
duke@435 268 No_Safepoint_Verifier * _nsv;
duke@435 269
duke@435 270 public:
duke@435 271 #ifdef ASSERT
duke@435 272 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@435 273 : Pause_No_GC_Verifier(nsv) {
duke@435 274
duke@435 275 _nsv = nsv;
duke@435 276 if (_nsv->_activated) {
duke@435 277 _nsv->_thread->_allow_allocation_count--;
duke@435 278 _nsv->_thread->_allow_safepoint_count--;
duke@435 279 }
duke@435 280 }
duke@435 281
duke@435 282 ~Pause_No_Safepoint_Verifier() {
duke@435 283 if (_nsv->_activated) {
duke@435 284 _nsv->_thread->_allow_allocation_count++;
duke@435 285 _nsv->_thread->_allow_safepoint_count++;
duke@435 286 }
duke@435 287 }
duke@435 288 #else
duke@435 289 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@435 290 : Pause_No_GC_Verifier(nsv) {}
duke@435 291 ~Pause_No_Safepoint_Verifier() {}
duke@435 292 #endif
duke@435 293 };
duke@435 294
ysr@1241 295 // A SkipGCALot object is used to elide the usual effect of gc-a-lot
ysr@1241 296 // over a section of execution by a thread. Currently, it's used only to
ysr@1241 297 // prevent re-entrant calls to GC.
ysr@1241 298 class SkipGCALot : public StackObj {
ysr@1241 299 private:
ysr@1241 300 bool _saved;
ysr@1241 301 Thread* _t;
ysr@1241 302
ysr@1241 303 public:
ysr@1241 304 #ifdef ASSERT
ysr@1241 305 SkipGCALot(Thread* t) : _t(t) {
ysr@1241 306 _saved = _t->skip_gcalot();
ysr@1241 307 _t->set_skip_gcalot(true);
ysr@1241 308 }
ysr@1241 309
ysr@1241 310 ~SkipGCALot() {
ysr@1241 311 assert(_t->skip_gcalot(), "Save-restore protocol invariant");
ysr@1241 312 _t->set_skip_gcalot(_saved);
ysr@1241 313 }
ysr@1241 314 #else
ysr@1241 315 SkipGCALot(Thread* t) { }
ysr@1241 316 ~SkipGCALot() { }
ysr@1241 317 #endif
ysr@1241 318 };
ysr@1241 319
duke@435 320 // JRT_LEAF currently can be called from either _thread_in_Java or
duke@435 321 // _thread_in_native mode. In _thread_in_native, it is ok
duke@435 322 // for another thread to trigger GC. The rest of the JRT_LEAF
duke@435 323 // rules apply.
duke@435 324 class JRT_Leaf_Verifier : public No_Safepoint_Verifier {
duke@435 325 static bool should_verify_GC();
duke@435 326 public:
duke@435 327 #ifdef ASSERT
duke@435 328 JRT_Leaf_Verifier();
duke@435 329 ~JRT_Leaf_Verifier();
duke@435 330 #else
duke@435 331 JRT_Leaf_Verifier() {}
duke@435 332 ~JRT_Leaf_Verifier() {}
duke@435 333 #endif
duke@435 334 };
duke@435 335
duke@435 336 // A No_Alloc_Verifier object can be placed in methods where one assumes that
duke@435 337 // no allocation will occur. The destructor will verify this property
duke@435 338 // unless the constructor is called with argument false (not activated).
duke@435 339 //
duke@435 340 // The check will only be done in debug mode and if activated.
duke@435 341 // Note: this only makes sense at safepoints (otherwise, other threads may
duke@435 342 // allocate concurrently.)
duke@435 343
duke@435 344 class No_Alloc_Verifier : public StackObj {
duke@435 345 private:
duke@435 346 bool _activated;
duke@435 347
duke@435 348 public:
duke@435 349 #ifdef ASSERT
duke@435 350 No_Alloc_Verifier(bool activated = true) {
duke@435 351 _activated = activated;
duke@435 352 if (_activated) Thread::current()->_allow_allocation_count++;
duke@435 353 }
duke@435 354
duke@435 355 ~No_Alloc_Verifier() {
duke@435 356 if (_activated) Thread::current()->_allow_allocation_count--;
duke@435 357 }
duke@435 358 #else
duke@435 359 No_Alloc_Verifier(bool activated = true) {}
duke@435 360 ~No_Alloc_Verifier() {}
duke@435 361 #endif
duke@435 362 };
stefank@2314 363
stefank@2314 364 #endif // SHARE_VM_MEMORY_GCLOCKER_HPP

mercurial