Skip to content
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-8275704: Metaspace::contains() should be threadsafe #6060

Conversation

tstuefe
Copy link
Member

@tstuefe tstuefe commented Oct 21, 2021

Metaspace::contains() is used in many places. It is not threadsafe since it walks the list of metaspace mappings, which can be altered concurrently. This is suspected to be the cause of JDK-8271124.

Currently, it does not lock, and adding a lock is not realistic either. It should work lockless.

This patch builds atop of https://bugs.openjdk.java.net/browse/JDK-8275582, which removed the old (pre JEP 387) technique of uncommitting metaspace memory. As a side effect, that patch changed the mapping list to an add-only structure. The only remaining place where it gets modified is in VirtualSpaceList::create_node(). Modifications are synchronized via lock. The only place where we walk the list locklessly is in Metaspace::contains(). This patch adds the appropriate memory barriers to those two places.

Tests:

  • GHAs
  • SAP nightlies (queued)

Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8275704: Metaspace::contains() should be threadsafe

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/6060/head:pull/6060
$ git checkout pull/6060

Update a local copy of the PR:
$ git checkout pull/6060
$ git pull https://git.openjdk.java.net/jdk pull/6060/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 6060

View PR using the GUI difftool:
$ git pr show -t 6060

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/6060.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 21, 2021

👋 Welcome back stuefe! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Oct 21, 2021

@tstuefe The following label will be automatically applied to this pull request:

  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot-runtime hotspot-runtime-dev@openjdk.org label Oct 21, 2021
@tstuefe tstuefe changed the title JDK-8275704: Metaspace::contains() should the threadsafe JDK-8275704: Metaspace::contains() should be threadsafe Oct 21, 2021
@tstuefe tstuefe force-pushed the JDK-8275704-metaspace-contains-should-be-threadsafe branch from 7ddf71e to bfe54c1 Compare October 27, 2021 05:53
@tstuefe tstuefe marked this pull request as ready for review October 27, 2021 09:07
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 27, 2021
@mlbridge
Copy link

mlbridge bot commented Oct 27, 2021

Webrevs

Copy link
Contributor

@coleenp coleenp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! Thank you for fixing this.

@openjdk
Copy link

openjdk bot commented Oct 27, 2021

@tstuefe 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:

8275704: Metaspace::contains() should be threadsafe

Reviewed-by: coleenp, dholmes

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 12 new commits pushed to the master branch:

  • 9a3e954: 8274879: Replace uses of StringBuffer with StringBuilder within java.base classes
  • e6fa5fa: 8276063: ProblemList gtest dll_address_to_function_and_library_name on macosx-generic
  • 809488b: 8276046: codestrings.validate_vm gtest fails on ppc64, s390
  • 93be099: 4718400: Many quantities are held as signed that should be unsigned
  • 168081e: 8270490: Charset.forName() taking fallback default value
  • a292733: 8275975: Remove dead code in ciInstanceKlass
  • 4060602: 8275800: Redefinition leaks MethodData::_extra_data_lock
  • 485d658: 8275851: Deproblemlist open/test/jdk/javax/swing/JComponent/6683775/bug6683775.java
  • b3f45f8: 8275689: [TESTBUG] Use color tolerance only for XRender in BlitRotateClippedArea test
  • 6c05cc9: 8275863: Use encodeASCII for ASCII-compatible DoubleByte encodings
  • ... and 2 more: https://git.openjdk.java.net/jdk/compare/9f75d5ce500886b32175cc541939b7f0eee190ca...master

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 master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 27, 2021
@tstuefe
Copy link
Member Author

tstuefe commented Oct 27, 2021

This looks good! Thank you for fixing this.

Thank you Coleen!

@@ -186,7 +189,8 @@ void VirtualSpaceList::verify() const {

// Returns true if this pointer is contained in one of our nodes.
bool VirtualSpaceList::contains(const MetaWord* p) const {
const VirtualSpaceNode* vsn = _first_node;
// Note: needs to work without locks.
const VirtualSpaceNode* vsn = Atomic::load_acquire(&_first_node);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe vsn->next() now also requires load_acquire semantics.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The internal nodes are already protected by the acquire/release semantics used for accessing _first_node, which is the only mutable field.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I figured too.

Also, if that were wrong we would have the same problem in CLDG. Where, in contrast to this list nodes get deleted too (see ClassLoaderDataGraph::clear_claimed_marks()).

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Thomas,

This seems fine to me.

Thanks,
David

@@ -186,7 +189,8 @@ void VirtualSpaceList::verify() const {

// Returns true if this pointer is contained in one of our nodes.
bool VirtualSpaceList::contains(const MetaWord* p) const {
const VirtualSpaceNode* vsn = _first_node;
// Note: needs to work without locks.
const VirtualSpaceNode* vsn = Atomic::load_acquire(&_first_node);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The internal nodes are already protected by the acquire/release semantics used for accessing _first_node, which is the only mutable field.

// never to be destroyed.
//
// The list will only be modified under lock protection, but may be
// read asynchronously without lock.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asynchronously? Did you mean concurrently?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I'll correct it before pushing.

@tstuefe
Copy link
Member Author

tstuefe commented Oct 28, 2021

Thanks @coleenp, @zhengyu123, @dholmes-ora!

Note that one of the next changes will be an increase in size of the individual mappings. Today they are two root chunks sized (8MB) and by making them bigger we will have fewer of them, which will make traversing the list in contains() faster.

/integrate

@openjdk
Copy link

openjdk bot commented Oct 28, 2021

Going to push as commit d9b0138.
Since your change was applied there have been 12 commits pushed to the master branch:

  • 9a3e954: 8274879: Replace uses of StringBuffer with StringBuilder within java.base classes
  • e6fa5fa: 8276063: ProblemList gtest dll_address_to_function_and_library_name on macosx-generic
  • 809488b: 8276046: codestrings.validate_vm gtest fails on ppc64, s390
  • 93be099: 4718400: Many quantities are held as signed that should be unsigned
  • 168081e: 8270490: Charset.forName() taking fallback default value
  • a292733: 8275975: Remove dead code in ciInstanceKlass
  • 4060602: 8275800: Redefinition leaks MethodData::_extra_data_lock
  • 485d658: 8275851: Deproblemlist open/test/jdk/javax/swing/JComponent/6683775/bug6683775.java
  • b3f45f8: 8275689: [TESTBUG] Use color tolerance only for XRender in BlitRotateClippedArea test
  • 6c05cc9: 8275863: Use encodeASCII for ASCII-compatible DoubleByte encodings
  • ... and 2 more: https://git.openjdk.java.net/jdk/compare/9f75d5ce500886b32175cc541939b7f0eee190ca...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot closed this Oct 28, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 28, 2021
@openjdk
Copy link

openjdk bot commented Oct 28, 2021

@tstuefe Pushed as commit d9b0138.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@tstuefe tstuefe deleted the JDK-8275704-metaspace-contains-should-be-threadsafe branch October 28, 2021 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated
4 participants