Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit b318535

Browse files
author
Markus Grönlund
committed
8267579: Thread::cooked_allocated_bytes() hits assert(left >= right) failed: avoid underflow
Backport-of: c420735
1 parent fe48ea9 commit b318535

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "memory/resourceArea.hpp"
3232
#include "memory/universe.hpp"
3333
#include "oops/oop.inline.hpp"
34+
#include "runtime/atomic.hpp"
3435
#include "runtime/perfData.hpp"
3536
#include "runtime/thread.inline.hpp"
3637
#include "runtime/threadSMR.hpp"
@@ -473,3 +474,11 @@ size_t ThreadLocalAllocBuffer::end_reserve() {
473474
size_t reserve_size = Universe::heap()->tlab_alloc_reserve();
474475
return MAX2(reserve_size, (size_t)_reserve_for_allocation_prefetch);
475476
}
477+
478+
const HeapWord* ThreadLocalAllocBuffer::start_relaxed() const {
479+
return Atomic::load(&_start);
480+
}
481+
482+
const HeapWord* ThreadLocalAllocBuffer::top_relaxed() const {
483+
return Atomic::load(&_top);
484+
}

src/hotspot/share/gc/shared/threadLocalAllocBuffer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ class ThreadLocalAllocBuffer: public CHeapObj<mtThread> {
129129
size_t refill_waste_limit() const { return _refill_waste_limit; }
130130
size_t bytes_since_last_sample_point() const { return _bytes_since_last_sample_point; }
131131

132+
// For external inspection.
133+
const HeapWord* start_relaxed() const;
134+
const HeapWord* top_relaxed() const;
135+
132136
// Allocate size HeapWords. The memory is NOT initialized to zero.
133137
inline HeapWord* allocate(size_t size);
134138

src/hotspot/share/runtime/thread.inline.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@
4141
inline jlong Thread::cooked_allocated_bytes() {
4242
jlong allocated_bytes = Atomic::load_acquire(&_allocated_bytes);
4343
if (UseTLAB) {
44-
size_t used_bytes = tlab().used_bytes();
44+
// These reads are unsynchronized and unordered with the thread updating its tlab pointers.
45+
// Use only if top > start && used_bytes <= max_tlab_size_bytes.
46+
const HeapWord* const top = tlab().top_relaxed();
47+
const HeapWord* const start = tlab().start_relaxed();
48+
if (top <= start) {
49+
return allocated_bytes;
50+
}
51+
const size_t used_bytes = pointer_delta(top, start, 1);
4552
if (used_bytes <= ThreadLocalAllocBuffer::max_size_in_bytes()) {
4653
// Comparing used_bytes with the maximum allowed size will ensure
4754
// that we don't add the used bytes from a semi-initialized TLAB

0 commit comments

Comments
 (0)