From 8f29e9c5ec26a5f726ddd0557e046e66c1c94e10 Mon Sep 17 00:00:00 2001
Date: Wed, 8 Dec 2021 16:23:26 +0800
Subject: [PATCH] [Backport] 8272138: ZGC: Adopt relaxed ordering for self-healing

Reference: https://bugs.openjdk.java.net/browse/JDK-8272138
---
 src/hotspot/share/gc/z/zBarrier.inline.hpp    | 2 +-
 src/hotspot/share/gc/z/zForwarding.inline.hpp | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/hotspot/share/gc/z/zBarrier.inline.hpp b/src/hotspot/share/gc/z/zBarrier.inline.hpp
index 0fa611005..2eaebfcfd 100644
--- a/src/hotspot/share/gc/z/zBarrier.inline.hpp
+++ b/src/hotspot/share/gc/z/zBarrier.inline.hpp
@@ -117,7 +117,7 @@ inline void ZBarrier::self_heal(volatile oop* p, uintptr_t addr, uintptr_t heal_
 
   for (;;) {
     // Heal
-    const uintptr_t prev_addr = Atomic::cmpxchg((volatile uintptr_t*)p, addr, heal_addr);
+    const uintptr_t prev_addr = Atomic::cmpxchg((volatile uintptr_t*)p, addr, heal_addr, memory_order_relaxed);
     if (prev_addr == addr) {
       // Success
       return;
diff --git a/src/hotspot/share/gc/z/zForwarding.inline.hpp b/src/hotspot/share/gc/z/zForwarding.inline.hpp
index 31bf72929..cff6d7a90 100644
--- a/src/hotspot/share/gc/z/zForwarding.inline.hpp
+++ b/src/hotspot/share/gc/z/zForwarding.inline.hpp
@@ -136,8 +136,12 @@ inline uintptr_t ZForwarding::insert(uintptr_t from_index, uintptr_t to_offset,
   const ZForwardingEntry new_entry(from_index, to_offset);
   const ZForwardingEntry old_entry; // Empty
 
+  // Make sure that object copy is finished
+  // before forwarding table installation
+  OrderAccess::release();
+
   for (;;) {
-    const ZForwardingEntry prev_entry = Atomic::cmpxchg(entries() + *cursor, old_entry, new_entry);
+    const ZForwardingEntry prev_entry = Atomic::cmpxchg(entries() + *cursor, old_entry, new_entry, memory_order_relaxed);
     if (!prev_entry.populated()) {
       // Success
       return to_offset;
-- 
2.19.0

