Mercurial > hg > truffle
comparison src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.cpp @ 1716:be3f9c242c9d
6948538: CMS: BOT walkers can fall into object allocation and initialization cracks
Summary: GC workers now recognize an intermediate transient state of blocks which are allocated but have not yet completed initialization. blk_start() calls do not attempt to determine the size of a block in the transient state, rather waiting for the block to become initialized so that it is safe to query its size. Audited and ensured the order of initialization of object fields (klass, free bit and size) to respect block state transition protocol. Also included some new assertion checking code enabled in debug mode.
Reviewed-by: chrisphi, johnc, poonam
author | ysr |
---|---|
date | Mon, 16 Aug 2010 15:58:42 -0700 |
parents | 63f4675ac87d |
children | 5ed703250bff |
comparison
equal
deleted
inserted
replaced
1713:7fcd5f39bd7a | 1716:be3f9c242c9d |
---|---|
1017 Mutex::_no_safepoint_check_flag); | 1017 Mutex::_no_safepoint_check_flag); |
1018 return have_lock_and_allocate(size, tlab); | 1018 return have_lock_and_allocate(size, tlab); |
1019 } | 1019 } |
1020 | 1020 |
1021 HeapWord* ConcurrentMarkSweepGeneration::have_lock_and_allocate(size_t size, | 1021 HeapWord* ConcurrentMarkSweepGeneration::have_lock_and_allocate(size_t size, |
1022 bool tlab) { | 1022 bool tlab /* ignored */) { |
1023 assert_lock_strong(freelistLock()); | 1023 assert_lock_strong(freelistLock()); |
1024 size_t adjustedSize = CompactibleFreeListSpace::adjustObjectSize(size); | 1024 size_t adjustedSize = CompactibleFreeListSpace::adjustObjectSize(size); |
1025 HeapWord* res = cmsSpace()->allocate(adjustedSize); | 1025 HeapWord* res = cmsSpace()->allocate(adjustedSize); |
1026 // Allocate the object live (grey) if the background collector has | 1026 // Allocate the object live (grey) if the background collector has |
1027 // started marking. This is necessary because the marker may | 1027 // started marking. This is necessary because the marker may |
1030 // Note that if this object contains references, the writing | 1030 // Note that if this object contains references, the writing |
1031 // of those references will dirty the card containing this object | 1031 // of those references will dirty the card containing this object |
1032 // allowing the object to be blackened (and its references scanned) | 1032 // allowing the object to be blackened (and its references scanned) |
1033 // either during a preclean phase or at the final checkpoint. | 1033 // either during a preclean phase or at the final checkpoint. |
1034 if (res != NULL) { | 1034 if (res != NULL) { |
1035 // We may block here with an uninitialized object with | |
1036 // its mark-bit or P-bits not yet set. Such objects need | |
1037 // to be safely navigable by block_start(). | |
1038 assert(oop(res)->klass_or_null() == NULL, "Object should be uninitialized here."); | |
1039 assert(!((FreeChunk*)res)->isFree(), "Error, block will look free but show wrong size"); | |
1035 collector()->direct_allocated(res, adjustedSize); | 1040 collector()->direct_allocated(res, adjustedSize); |
1036 _direct_allocated_words += adjustedSize; | 1041 _direct_allocated_words += adjustedSize; |
1037 // allocation counters | 1042 // allocation counters |
1038 NOT_PRODUCT( | 1043 NOT_PRODUCT( |
1039 _numObjectsAllocated++; | 1044 _numObjectsAllocated++; |
1059 MutexLockerEx y(_markBitMap.lock(), | 1064 MutexLockerEx y(_markBitMap.lock(), |
1060 Mutex::_no_safepoint_check_flag); | 1065 Mutex::_no_safepoint_check_flag); |
1061 // [see comments preceding SweepClosure::do_blk() below for details] | 1066 // [see comments preceding SweepClosure::do_blk() below for details] |
1062 // 1. need to mark the object as live so it isn't collected | 1067 // 1. need to mark the object as live so it isn't collected |
1063 // 2. need to mark the 2nd bit to indicate the object may be uninitialized | 1068 // 2. need to mark the 2nd bit to indicate the object may be uninitialized |
1064 // 3. need to mark the end of the object so sweeper can skip over it | 1069 // 3. need to mark the end of the object so marking, precleaning or sweeping |
1065 // if it's uninitialized when the sweeper reaches it. | 1070 // can skip over uninitialized or unparsable objects. An allocated |
1071 // object is considered uninitialized for our purposes as long as | |
1072 // its klass word is NULL. (Unparsable objects are those which are | |
1073 // initialized in the sense just described, but whose sizes can still | |
1074 // not be correctly determined. Note that the class of unparsable objects | |
1075 // can only occur in the perm gen. All old gen objects are parsable | |
1076 // as soon as they are initialized.) | |
1066 _markBitMap.mark(start); // object is live | 1077 _markBitMap.mark(start); // object is live |
1067 _markBitMap.mark(start + 1); // object is potentially uninitialized? | 1078 _markBitMap.mark(start + 1); // object is potentially uninitialized? |
1068 _markBitMap.mark(start + size - 1); | 1079 _markBitMap.mark(start + size - 1); |
1069 // mark end of object | 1080 // mark end of object |
1070 } | 1081 } |
1086 _markBitMap.mark(start); | 1097 _markBitMap.mark(start); |
1087 } | 1098 } |
1088 // We don't need to mark the object as uninitialized (as | 1099 // We don't need to mark the object as uninitialized (as |
1089 // in direct_allocated above) because this is being done with the | 1100 // in direct_allocated above) because this is being done with the |
1090 // world stopped and the object will be initialized by the | 1101 // world stopped and the object will be initialized by the |
1091 // time the sweeper gets to look at it. | 1102 // time the marking, precleaning or sweeping get to look at it. |
1103 // But see the code for copying objects into the CMS generation, | |
1104 // where we need to ensure that concurrent readers of the | |
1105 // block offset table are able to safely navigate a block that | |
1106 // is in flux from being free to being allocated (and in | |
1107 // transition while being copied into) and subsequently | |
1108 // becoming a bona-fide object when the copy/promotion is complete. | |
1092 assert(SafepointSynchronize::is_at_safepoint(), | 1109 assert(SafepointSynchronize::is_at_safepoint(), |
1093 "expect promotion only at safepoints"); | 1110 "expect promotion only at safepoints"); |
1094 | 1111 |
1095 if (_collectorState < Sweeping) { | 1112 if (_collectorState < Sweeping) { |
1096 // Mark the appropriate cards in the modUnionTable, so that | 1113 // Mark the appropriate cards in the modUnionTable, so that |
1302 size_t word_sz) | 1319 size_t word_sz) |
1303 { | 1320 { |
1304 return collector()->allocation_limit_reached(space, top, word_sz); | 1321 return collector()->allocation_limit_reached(space, top, word_sz); |
1305 } | 1322 } |
1306 | 1323 |
1324 // IMPORTANT: Notes on object size recognition in CMS. | |
1325 // --------------------------------------------------- | |
1326 // A block of storage in the CMS generation is always in | |
1327 // one of three states. A free block (FREE), an allocated | |
1328 // object (OBJECT) whose size() method reports the correct size, | |
1329 // and an intermediate state (TRANSIENT) in which its size cannot | |
1330 // be accurately determined. | |
1331 // STATE IDENTIFICATION: (32 bit and 64 bit w/o COOPS) | |
1332 // ----------------------------------------------------- | |
1333 // FREE: klass_word & 1 == 1; mark_word holds block size | |
1334 // | |
1335 // OBJECT: klass_word installed; klass_word != 0 && klass_word & 0 == 0; | |
1336 // obj->size() computes correct size | |
1337 // [Perm Gen objects needs to be "parsable" before they can be navigated] | |
1338 // | |
1339 // TRANSIENT: klass_word == 0; size is indeterminate until we become an OBJECT | |
1340 // | |
1341 // STATE IDENTIFICATION: (64 bit+COOPS) | |
1342 // ------------------------------------ | |
1343 // FREE: mark_word & CMS_FREE_BIT == 1; mark_word & ~CMS_FREE_BIT gives block_size | |
1344 // | |
1345 // OBJECT: klass_word installed; klass_word != 0; | |
1346 // obj->size() computes correct size | |
1347 // [Perm Gen comment above continues to hold] | |
1348 // | |
1349 // TRANSIENT: klass_word == 0; size is indeterminate until we become an OBJECT | |
1350 // | |
1351 // | |
1352 // STATE TRANSITION DIAGRAM | |
1353 // | |
1354 // mut / parnew mut / parnew | |
1355 // FREE --------------------> TRANSIENT ---------------------> OBJECT --| | |
1356 // ^ | | |
1357 // |------------------------ DEAD <------------------------------------| | |
1358 // sweep mut | |
1359 // | |
1360 // While a block is in TRANSIENT state its size cannot be determined | |
1361 // so readers will either need to come back later or stall until | |
1362 // the size can be determined. Note that for the case of direct | |
1363 // allocation, P-bits, when available, may be used to determine the | |
1364 // size of an object that may not yet have been initialized. | |
1365 | |
1307 // Things to support parallel young-gen collection. | 1366 // Things to support parallel young-gen collection. |
1308 oop | 1367 oop |
1309 ConcurrentMarkSweepGeneration::par_promote(int thread_num, | 1368 ConcurrentMarkSweepGeneration::par_promote(int thread_num, |
1310 oop old, markOop m, | 1369 oop old, markOop m, |
1311 size_t word_sz) { | 1370 size_t word_sz) { |
1329 if (!expand_and_ensure_spooling_space(promoInfo)) { | 1388 if (!expand_and_ensure_spooling_space(promoInfo)) { |
1330 return NULL; | 1389 return NULL; |
1331 } | 1390 } |
1332 } | 1391 } |
1333 assert(promoInfo->has_spooling_space(), "Control point invariant"); | 1392 assert(promoInfo->has_spooling_space(), "Control point invariant"); |
1334 HeapWord* obj_ptr = ps->lab.alloc(word_sz); | 1393 const size_t alloc_sz = CompactibleFreeListSpace::adjustObjectSize(word_sz); |
1394 HeapWord* obj_ptr = ps->lab.alloc(alloc_sz); | |
1335 if (obj_ptr == NULL) { | 1395 if (obj_ptr == NULL) { |
1336 obj_ptr = expand_and_par_lab_allocate(ps, word_sz); | 1396 obj_ptr = expand_and_par_lab_allocate(ps, alloc_sz); |
1337 if (obj_ptr == NULL) { | 1397 if (obj_ptr == NULL) { |
1338 return NULL; | 1398 return NULL; |
1339 } | 1399 } |
1340 } | 1400 } |
1341 oop obj = oop(obj_ptr); | 1401 oop obj = oop(obj_ptr); |
1402 OrderAccess::storestore(); | |
1342 assert(obj->klass_or_null() == NULL, "Object should be uninitialized here."); | 1403 assert(obj->klass_or_null() == NULL, "Object should be uninitialized here."); |
1404 assert(!((FreeChunk*)obj_ptr)->isFree(), "Error, block will look free but show wrong size"); | |
1405 // IMPORTANT: See note on object initialization for CMS above. | |
1343 // Otherwise, copy the object. Here we must be careful to insert the | 1406 // Otherwise, copy the object. Here we must be careful to insert the |
1344 // klass pointer last, since this marks the block as an allocated object. | 1407 // klass pointer last, since this marks the block as an allocated object. |
1345 // Except with compressed oops it's the mark word. | 1408 // Except with compressed oops it's the mark word. |
1346 HeapWord* old_ptr = (HeapWord*)old; | 1409 HeapWord* old_ptr = (HeapWord*)old; |
1410 // Restore the mark word copied above. | |
1411 obj->set_mark(m); | |
1412 assert(obj->klass_or_null() == NULL, "Object should be uninitialized here."); | |
1413 assert(!((FreeChunk*)obj_ptr)->isFree(), "Error, block will look free but show wrong size"); | |
1414 OrderAccess::storestore(); | |
1415 | |
1416 if (UseCompressedOops) { | |
1417 // Copy gap missed by (aligned) header size calculation below | |
1418 obj->set_klass_gap(old->klass_gap()); | |
1419 } | |
1347 if (word_sz > (size_t)oopDesc::header_size()) { | 1420 if (word_sz > (size_t)oopDesc::header_size()) { |
1348 Copy::aligned_disjoint_words(old_ptr + oopDesc::header_size(), | 1421 Copy::aligned_disjoint_words(old_ptr + oopDesc::header_size(), |
1349 obj_ptr + oopDesc::header_size(), | 1422 obj_ptr + oopDesc::header_size(), |
1350 word_sz - oopDesc::header_size()); | 1423 word_sz - oopDesc::header_size()); |
1351 } | 1424 } |
1352 | |
1353 if (UseCompressedOops) { | |
1354 // Copy gap missed by (aligned) header size calculation above | |
1355 obj->set_klass_gap(old->klass_gap()); | |
1356 } | |
1357 | |
1358 // Restore the mark word copied above. | |
1359 obj->set_mark(m); | |
1360 | 1425 |
1361 // Now we can track the promoted object, if necessary. We take care | 1426 // Now we can track the promoted object, if necessary. We take care |
1362 // to delay the transition from uninitialized to full object | 1427 // to delay the transition from uninitialized to full object |
1363 // (i.e., insertion of klass pointer) until after, so that it | 1428 // (i.e., insertion of klass pointer) until after, so that it |
1364 // atomically becomes a promoted object. | 1429 // atomically becomes a promoted object. |
1365 if (promoInfo->tracking()) { | 1430 if (promoInfo->tracking()) { |
1366 promoInfo->track((PromotedObject*)obj, old->klass()); | 1431 promoInfo->track((PromotedObject*)obj, old->klass()); |
1367 } | 1432 } |
1433 assert(obj->klass_or_null() == NULL, "Object should be uninitialized here."); | |
1434 assert(!((FreeChunk*)obj_ptr)->isFree(), "Error, block will look free but show wrong size"); | |
1435 assert(old->is_oop(), "Will use and dereference old klass ptr below"); | |
1368 | 1436 |
1369 // Finally, install the klass pointer (this should be volatile). | 1437 // Finally, install the klass pointer (this should be volatile). |
1438 OrderAccess::storestore(); | |
1370 obj->set_klass(old->klass()); | 1439 obj->set_klass(old->klass()); |
1371 | 1440 // We should now be able to calculate the right size for this object |
1372 assert(old->is_oop(), "Will dereference klass ptr below"); | 1441 assert(obj->is_oop() && obj->size() == (int)word_sz, "Error, incorrect size computed for promoted object"); |
1442 | |
1373 collector()->promoted(true, // parallel | 1443 collector()->promoted(true, // parallel |
1374 obj_ptr, old->is_objArray(), word_sz); | 1444 obj_ptr, old->is_objArray(), word_sz); |
1375 | 1445 |
1376 NOT_PRODUCT( | 1446 NOT_PRODUCT( |
1377 Atomic::inc(&_numObjectsPromoted); | 1447 Atomic::inc_ptr(&_numObjectsPromoted); |
1378 Atomic::add((jint)CompactibleFreeListSpace::adjustObjectSize(obj->size()), | 1448 Atomic::add_ptr(alloc_sz, &_numWordsPromoted); |
1379 &_numWordsPromoted); | |
1380 ) | 1449 ) |
1381 | 1450 |
1382 return obj; | 1451 return obj; |
1383 } | 1452 } |
1384 | 1453 |