-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JDK-8317661: [REDO] store/load order not preserved when handling memory pool due to weakly ordered memory architecture of aarch64 #16143
Conversation
…ry pool due to weakly ordered memory architecture of aarch64
👋 Welcome back dfenacci! A progress list of the required criteria for merging this PR into |
Co-authored-by: Andrew Haley <aph-open@littlepinkcloud.com>
Webrevs
|
What testing has been done on this [REDO]? |
src/hotspot/share/memory/heap.cpp
Outdated
@@ -303,7 +303,7 @@ void* CodeHeap::allocate(size_t instance_size) { | |||
mark_segmap_as_used(_next_segment, _next_segment + number_of_segments, false); | |||
block = block_at(_next_segment); | |||
block->initialize(number_of_segments); | |||
_next_segment += number_of_segments; | |||
Atomic::release_store(&_next_segment, _next_segment + number_of_segments); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we do the release here, what previous stores are we trying to order with the reading code below? It is hard to see the complete picture with this code just from the PR changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dholmes-ora you’re right, it is quite difficult to get the whole picture: let me try to make it a bit clearer (I hope it makes sense).
The issue originates here:
jdk/src/hotspot/share/services/memoryPool.cpp
Lines 181 to 182 in 138542d
size_t used = used_in_bytes(); | |
size_t committed = _codeHeap->capacity(); |
We want to ensure that the values used to calculate
committed
are actually saved before the values used to calculate used
are used.
The problem starts in CodeCache::allocate
. First CodeHeap::allocate
is called and if there is not enough space we call CodeHeap::expand_by
and retry CodeHeap::allocate
(while loop):
jdk/src/hotspot/share/code/codeCache.cpp
Lines 535 to 537 in e510dee
cb = (CodeBlob*)heap->allocate(size); | |
if (cb != nullptr) break; | |
if (!heap->expand_by(CodeCacheExpansionSize)) { |
During the second call of
CodeHeap::allocate
we update the values used to calculate the used
variable (_next_segment
in particular since it is the last to be updated) and we want to make sure that when we read it, all earlier values have been written.
As for the link between used
and _next_segment
, used
is calculated here
jdk/src/hotspot/share/memory/heap.cpp
Line 556 in e510dee
return segments_to_size(_next_segment - _freelist_segments); |
_next_segment
and _freelist_segments
. _next_segment
is the last to be written and it is updated herejdk/src/hotspot/share/memory/heap.cpp
Line 306 in e510dee
_next_segment += number_of_segments; |
CodeCache::allocate
jdk/src/hotspot/share/code/codeCache.cpp
Line 535 in e510dee
cb = (CodeBlob*)heap->allocate(size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I'm still missing part of this (and I know we discussed it a few weeks ago). What is the variable(s) that is written before _next_segment
that we need to see updated when we call _codeHeap->capacity()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically it would be the field VirtualSpace::_high
. _codeHeap->capacity()
calls VirtualSpace::committed_size
which reads the _high
and _low
fields:
jdk/src/hotspot/share/memory/virtualspace.cpp
Lines 768 to 770 in e510dee
size_t VirtualSpace::committed_size() const { | |
return pointer_delta(high(), low(), sizeof(char)); | |
} |
_low
doesn't really get updated but _high
is increased in VirtualSpace::expand_by
, which is called by CodeHeap::expand_by
jdk/src/hotspot/share/memory/heap.cpp
Line 259 in e510dee
if (!_memory.expand_by(dm)) return false; |
CodeCache::allocate
: jdk/src/hotspot/share/code/codeCache.cpp
Line 537 in e510dee
if (!heap->expand_by(CodeCacheExpansionSize)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dholmes-ora is there possibly a way to restrict the update that we need to see to that _high
variable only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So ... as we discussed offline a few weeks ago, it is very hard to clearly express the ordering constraints in this code as the two variables are so far apart in the code. So to reiterate, on the writer side we have:
while (true) {
cb = (CodeBlob*)heap->allocate(size); // updates _next_segment
if (cb != nullptr) break;
if (!heap->expand_by(CodeCacheExpansionSize)) { // updates _high
where the writes to _high
in expand_by
have to be ordered with the write to _next_segment
in allocate
that happen on the next loop iteration.
And on the reader side we have:
size_t used = used_in_bytes(); // reads _next_segment
size_t committed = _codeHeap->capacity(); // reads _high
where the reads must also be ordered.
I can't help but think that the clearest fix here is to use explicit barriers as discussed before the locking solution was proposed. The problem using release_store
and load_acquire
in the bottom level code is that those semantics are only needed in this higher-level code for the codeHeap
, not for every kind of heap
the code applies to.
I wonder if perhaps the following is clearer:
while (true) {
cb = (CodeBlob*)heap->allocate(size);
if (cb != nullptr) break;
if (!heap->expand_by(CodeCacheExpansionSize)) {
// ...
} else {
OrderAccess::release(); // ensure heap expansion is visible before we allocate on next iteration
}
coupled with:
size_t used = used_in_bytes();
OrderAccess:acquire();
size_t committed = _codeHeap->capacity();
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't help but think that the clearest fix here is to use explicit barriers as discussed before the locking solution was proposed. The problem using
release_store
andload_acquire
in the bottom level code is that those semantics are only needed in this higher-level code for thecodeHeap
, not for every kind ofheap
the code applies to.I wonder if perhaps the following is clearer:
while (true) { cb = (CodeBlob*)heap->allocate(size); if (cb != nullptr) break; if (!heap->expand_by(CodeCacheExpansionSize)) { // ... } else { OrderAccess::release(); // ensure heap expansion is visible
"visible to an asynchronous observer (e.g. CodeHeapPool::get_memory_usage())"
before we allocate on next iteration
}coupled with:
size_t used = used_in_bytes();
OrderAccess:acquire();
size_t committed = _codeHeap->capacity();Thoughts?
As long as it's more thoroughly commented, i guess so.
A floating release is, however, a pretty bad code smell. The only reason we're aren't solving this problem (IMHO) properly is that the code cache lock isn't re-entrant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem using
release_store
andload_acquire
in the bottom level code is that those semantics are only needed in this higher-level code for thecodeHeap
, not for every kind ofheap
the code applies to.I wonder if perhaps the following is clearer:
while (true) { cb = (CodeBlob*)heap->allocate(size); if (cb != nullptr) break; if (!heap->expand_by(CodeCacheExpansionSize)) { // ... } else { OrderAccess::release(); // ensure heap expansion is visible before we allocate on next iteration }
This would definitely make sense.
A floating release is, however, a pretty bad code smell. The only reason we're aren't solving this problem (IMHO) properly is that the code cache lock isn't re-entrant.
Indeed, it still looks a bit "messy". I was also wondering if there could be a performance penalty by using explicit barriers (although at least OrderAccess::release()
won't be called very often with this solution).
In any case I've modified the code to make use of explicit barriers following @dholmes-ora suggestion.
Thanks a lot!
@dcubed-ojdk I ran Tier1-5 + hs-precheckin-comp,hs-comp-stress tests and checked that it fixes the original test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the best of a bunch of bad options.
Thanks.
@dafedafe This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 430 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
@dholmes-ora @theRealAph thank you very much for your reviews! |
/integrate |
Going to push as commit ddd0716.
Your commit was automatically rebased without conflicts. |
Issue
An intermittent Memory Pool not found error has been noticed when running a few tests (vmTestbase/vm/mlvm/meth/stress/compiler/deoptimize/Test.java, vmTestbase/vm/mlvm/meth/stress/compiler/sequences/Test.java) on macosx_aarch64 (production build) with non-segmented code cache.
Origin
The issue originates from the fact that aarch64 architecture is a weakly ordered memory architecture, i.e. it permits the observation and completion of memory accesses in a different order from the program order.
More precisely: while calling
CodeHeapPool::get_memory_usage
, theused
andcommitted
variables are retrievedjdk/src/hotspot/share/services/memoryPool.cpp
Lines 181 to 182 in 138542d
and these are computed based on different variables saved in memory in
CodeCache::allocate
(duringheap->allocate
andheap->expand_by
to be precise) .jdk/src/hotspot/share/code/codeCache.cpp
Lines 535 to 537 in 138542d
The problem happens when first
heap->expand_by
gets called (which increasescommitted
) and thenheap->allocate
gets called in a second loop pass (which increasesused
). Although stores inCodeCache::allocate
happen in the this order, when reading from memory inCodeHeapPool::get_memory_usage
it can happen thatused
has the newly computed value, whilecommitted
is still "old" (because of ARM’s weak memory order). This is a problem, sincecommitted
must be > thanused
.Solution
To avoid this situation we must assure that values used to calculate
committed
are actually saved before the values used to calculateused
and that the opposite be true for reading. To enforce this explicit barriers are introduced:OrderAccess::release
inCodeCache::allocate
if we need to expand the heap, to ensure heap expansion is visible before we allocate, andOrderAccess::acquire
before calculatingcommitted
inCodeHeapPool::get_memory_usage
to make sure that the cache expansion is seen.Acquiring a
CodeCache_lock
has been attempted in #15819 but resulted in deadlocks (that seem to be unavoidable).Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/16143/head:pull/16143
$ git checkout pull/16143
Update a local copy of the PR:
$ git checkout pull/16143
$ git pull https://git.openjdk.org/jdk.git pull/16143/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 16143
View PR using the GUI difftool:
$ git pr show -t 16143
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/16143.diff
Webrev
Link to Webrev Comment