Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8275704: Metaspace::contains() should be threadsafe
Reviewed-by: coleenp, dholmes
  • Loading branch information
tstuefe committed Oct 28, 2021
1 parent 9a3e954 commit d9b0138
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/hotspot/share/memory/metaspace/virtualSpaceList.cpp
Expand Up @@ -34,6 +34,7 @@
#include "memory/metaspace/metaspaceCommon.hpp"
#include "memory/metaspace/virtualSpaceList.hpp"
#include "memory/metaspace/virtualSpaceNode.hpp"
#include "runtime/atomic.hpp"
#include "runtime/mutexLocker.hpp"

namespace metaspace {
Expand Down Expand Up @@ -74,8 +75,10 @@ VirtualSpaceList::VirtualSpaceList(const char* name, ReservedSpace rs, CommitLim

VirtualSpaceList::~VirtualSpaceList() {
assert_lock_strong(Metaspace_lock);
// Note: normally, there is no reason ever to delete a vslist since they are
// global objects, but for gtests it makes sense to allow this.
// Delete every single mapping in this list.
// Please note that this only gets executed during gtests under controlled
// circumstances, so we do not have any concurrency issues here. The "real"
// lists in metaspace are immortal.
VirtualSpaceNode* vsn = _first_node;
VirtualSpaceNode* vsn2 = vsn;
while (vsn != NULL) {
Expand All @@ -96,7 +99,7 @@ void VirtualSpaceList::create_new_node() {
_commit_limiter,
&_reserved_words_counter, &_committed_words_counter);
vsn->set_next(_first_node);
_first_node = vsn;
Atomic::release_store(&_first_node, vsn);
_nodes_counter.increment();
}

Expand Down Expand Up @@ -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);
while (vsn != NULL) {
if (vsn->contains(p)) {
return true;
Expand Down
18 changes: 14 additions & 4 deletions src/hotspot/share/memory/metaspace/virtualSpaceList.hpp
Expand Up @@ -40,22 +40,32 @@ namespace metaspace {
class Metachunk;
class FreeChunkListVector;

// VirtualSpaceList manages a single (if its non-expandable) or
// a series of (if its expandable) virtual memory regions used
// VirtualSpaceList manages a series of virtual memory regions used
// for metaspace.
//
// Internally it holds a list of nodes (VirtualSpaceNode) each
// managing a single contiguous memory region. The first node of
// this list is the current node and used for allocation of new
// root chunks.
//
// The list will only ever grow, never shrink. It will be immortal,
// never to be destroyed.
//
// The list will only be modified under lock protection, but may be
// read concurrently without lock.
//
// The list may be prevented from expanding beyond a single node -
// in that case it degenerates to a one-node-list (used for
// class space).
//

class VirtualSpaceList : public CHeapObj<mtClass> {

// Name
const char* const _name;

// Head of the list.
VirtualSpaceNode* _first_node;
// Head of the list (last added).
VirtualSpaceNode* volatile _first_node;

// Number of nodes (kept for statistics only).
IntCounter _nodes_counter;
Expand Down

5 comments on commit d9b0138

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@tstuefe
Copy link
Member Author

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d9b0138 Jan 10, 2023

Choose a reason for hiding this comment

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

@tstuefe Could not automatically backport d9b0138d to openjdk/jdk17u-dev due to conflicts in the following files:

  • src/hotspot/share/memory/metaspace/virtualSpaceList.hpp

Please fetch the appropriate branch/commit and manually resolve these conflicts by using the following commands in your personal fork of openjdk/jdk17u-dev. Note: these commands are just some suggestions and you can use other equivalent commands you know.

# Fetch the up-to-date version of the target branch
$ git fetch --no-tags https://git.openjdk.org/jdk17u-dev master:master

# Check out the target branch and create your own branch to backport
$ git checkout master
$ git checkout -b tstuefe-backport-d9b0138d

# Fetch the commit you want to backport
$ git fetch --no-tags https://git.openjdk.org/jdk d9b0138d7d02ceddc5d9c73908177f0b0d2e7c54

# Backport the commit
$ git cherry-pick --no-commit d9b0138d7d02ceddc5d9c73908177f0b0d2e7c54
# Resolve conflicts now

# Commit the files you have modified
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport d9b0138d7d02ceddc5d9c73908177f0b0d2e7c54'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk17u-dev with the title Backport d9b0138d7d02ceddc5d9c73908177f0b0d2e7c54.

@tstuefe
Copy link
Member Author

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d9b0138 Jan 27, 2023

Choose a reason for hiding this comment

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

@tstuefe the backport was successfully created on the branch tstuefe-backport-d9b0138d in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d9b0138d from the openjdk/jdk repository.

The commit being backported was authored by Thomas Stuefe on 28 Oct 2021 and was reviewed by Coleen Phillimore and David Holmes.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev tstuefe-backport-d9b0138d:tstuefe-backport-d9b0138d
$ git checkout tstuefe-backport-d9b0138d
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev tstuefe-backport-d9b0138d

Please sign in to comment.