From 4de24cdbe65289bd99eace30399f20694441f0aa Mon Sep 17 00:00:00 2001 From: Martin Doerr Date: Mon, 3 Apr 2023 09:37:16 +0000 Subject: [PATCH] 8303210: [linux, Windows] Make UseSystemMemoryBarrier available as product flag Reviewed-by: dholmes, rehn --- src/hotspot/cpu/arm/sharedRuntime_arm.cpp | 4 +++- .../arm/templateInterpreterGenerator_arm.cpp | 6 ++++-- src/hotspot/cpu/riscv/downcallLinker_riscv.cpp | 4 +++- src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp | 5 ++++- .../templateInterpreterGenerator_riscv.cpp | 5 ++++- src/hotspot/cpu/s390/sharedRuntime_s390.cpp | 4 +++- .../s390/templateInterpreterGenerator_s390.cpp | 4 +++- src/hotspot/cpu/x86/sharedRuntime_x86_32.cpp | 8 +++++--- .../os/linux/systemMemoryBarrier_linux.cpp | 6 +++--- src/hotspot/share/runtime/arguments.cpp | 3 +++ src/hotspot/share/runtime/globals.hpp | 4 ++-- src/hotspot/share/runtime/threads.cpp | 9 --------- .../share/utilities/systemMemoryBarrier.hpp | 17 ++++++++++++++--- 13 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/hotspot/cpu/arm/sharedRuntime_arm.cpp b/src/hotspot/cpu/arm/sharedRuntime_arm.cpp index fdb8f8250a3..15e597444c0 100644 --- a/src/hotspot/cpu/arm/sharedRuntime_arm.cpp +++ b/src/hotspot/cpu/arm/sharedRuntime_arm.cpp @@ -1211,7 +1211,9 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm, __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset())); // make sure the store is observed before reading the SafepointSynchronize state and further mem refs - __ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreLoad | MacroAssembler::StoreStore), Rtemp); + if (!UseSystemMemoryBarrier) { + __ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreLoad | MacroAssembler::StoreStore), Rtemp); + } __ safepoint_poll(R2, call_safepoint_runtime); __ ldr_u32(R3, Address(Rthread, JavaThread::suspend_flags_offset())); diff --git a/src/hotspot/cpu/arm/templateInterpreterGenerator_arm.cpp b/src/hotspot/cpu/arm/templateInterpreterGenerator_arm.cpp index 36c3de48668..f42c53d0f5b 100644 --- a/src/hotspot/cpu/arm/templateInterpreterGenerator_arm.cpp +++ b/src/hotspot/cpu/arm/templateInterpreterGenerator_arm.cpp @@ -1007,8 +1007,10 @@ address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) { __ mov(Rtemp, _thread_in_native_trans); __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset())); - // Force this write out before the read below - __ membar(MacroAssembler::StoreLoad, Rtemp); + // Force this write out before the read below + if (!UseSystemMemoryBarrier) { + __ membar(MacroAssembler::StoreLoad, Rtemp); + } // Protect the return value in the interleaved code: save it to callee-save registers. __ mov(Rsaved_result_lo, R0); diff --git a/src/hotspot/cpu/riscv/downcallLinker_riscv.cpp b/src/hotspot/cpu/riscv/downcallLinker_riscv.cpp index a702f248515..e5e28d77962 100644 --- a/src/hotspot/cpu/riscv/downcallLinker_riscv.cpp +++ b/src/hotspot/cpu/riscv/downcallLinker_riscv.cpp @@ -265,7 +265,9 @@ void DowncallStubGenerator::generate() { __ sw(t0, Address(xthread, JavaThread::thread_state_offset())); // Force this write out before the read below - __ membar(MacroAssembler::AnyAny); + if (!UseSystemMemoryBarrier) { + __ membar(MacroAssembler::AnyAny); + } Label L_after_safepoint_poll; Label L_safepoint_poll_slow_path; diff --git a/src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp b/src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp index 5e7ebcc31f6..f06f6eae625 100644 --- a/src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp +++ b/src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp @@ -44,6 +44,7 @@ #include "prims/methodHandles.hpp" #include "runtime/continuation.hpp" #include "runtime/continuationEntry.inline.hpp" +#include "runtime/globals.hpp" #include "runtime/jniHandles.hpp" #include "runtime/safepointMechanism.hpp" #include "runtime/sharedRuntime.hpp" @@ -1746,7 +1747,9 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm, __ sw(t0, Address(xthread, JavaThread::thread_state_offset())); // Force this write out before the read below - __ membar(MacroAssembler::AnyAny); + if (!UseSystemMemoryBarrier) { + __ membar(MacroAssembler::AnyAny); + } // check for safepoint operation in progress and/or pending suspend requests { diff --git a/src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp b/src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp index 0e4fff2327b..eee1bf2cfa8 100644 --- a/src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp +++ b/src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp @@ -45,6 +45,7 @@ #include "runtime/arguments.hpp" #include "runtime/deoptimization.hpp" #include "runtime/frame.inline.hpp" +#include "runtime/globals.hpp" #include "runtime/jniHandles.hpp" #include "runtime/sharedRuntime.hpp" #include "runtime/stubRoutines.hpp" @@ -1159,7 +1160,9 @@ address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) { __ sw(t0, Address(xthread, JavaThread::thread_state_offset())); // Force this write out before the read below - __ membar(MacroAssembler::AnyAny); + if (!UseSystemMemoryBarrier) { + __ membar(MacroAssembler::AnyAny); + } // check for safepoint operation in progress and/or pending suspend requests { diff --git a/src/hotspot/cpu/s390/sharedRuntime_s390.cpp b/src/hotspot/cpu/s390/sharedRuntime_s390.cpp index 9e0ce8a0f17..c0ef3bb8c78 100644 --- a/src/hotspot/cpu/s390/sharedRuntime_s390.cpp +++ b/src/hotspot/cpu/s390/sharedRuntime_s390.cpp @@ -1838,7 +1838,9 @@ nmethod *SharedRuntime::generate_native_wrapper(MacroAssembler *masm, save_native_result(masm, ret_type, workspace_slot_offset); // Make Z_R2 available as work reg. // Force this write out before the read below. - __ z_fence(); + if (!UseSystemMemoryBarrier) { + __ z_fence(); + } __ safepoint_poll(sync, Z_R1); diff --git a/src/hotspot/cpu/s390/templateInterpreterGenerator_s390.cpp b/src/hotspot/cpu/s390/templateInterpreterGenerator_s390.cpp index 87e23aacd69..60ca0020907 100644 --- a/src/hotspot/cpu/s390/templateInterpreterGenerator_s390.cpp +++ b/src/hotspot/cpu/s390/templateInterpreterGenerator_s390.cpp @@ -1525,7 +1525,9 @@ address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) { // synchronization is progress, and escapes. __ set_thread_state(_thread_in_native_trans); - __ z_fence(); + if (!UseSystemMemoryBarrier) { + __ z_fence(); + } // Now before we return to java we must look for a current safepoint // (a new safepoint can not start since we entered native_trans). diff --git a/src/hotspot/cpu/x86/sharedRuntime_x86_32.cpp b/src/hotspot/cpu/x86/sharedRuntime_x86_32.cpp index 1f07065540d..99464fb1884 100644 --- a/src/hotspot/cpu/x86/sharedRuntime_x86_32.cpp +++ b/src/hotspot/cpu/x86/sharedRuntime_x86_32.cpp @@ -1772,9 +1772,11 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm, __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native_trans); // Force this write out before the read below - __ membar(Assembler::Membar_mask_bits( - Assembler::LoadLoad | Assembler::LoadStore | - Assembler::StoreLoad | Assembler::StoreStore)); + if (!UseSystemMemoryBarrier) { + __ membar(Assembler::Membar_mask_bits( + Assembler::LoadLoad | Assembler::LoadStore | + Assembler::StoreLoad | Assembler::StoreStore)); + } if (AlwaysRestoreFPU) { // Make sure the control word is correct. diff --git a/src/hotspot/os/linux/systemMemoryBarrier_linux.cpp b/src/hotspot/os/linux/systemMemoryBarrier_linux.cpp index ede94436389..609967128fb 100644 --- a/src/hotspot/os/linux/systemMemoryBarrier_linux.cpp +++ b/src/hotspot/os/linux/systemMemoryBarrier_linux.cpp @@ -72,16 +72,17 @@ static int membarrier(int cmd, unsigned int flags, int cpu_id) { bool LinuxSystemMemoryBarrier::initialize() { int ret = membarrier(MEMBARRIER_CMD_QUERY, 0, 0); if (ret < 0) { - log_error(os)("MEMBARRIER_CMD_QUERY unsupported"); + log_info(os)("MEMBARRIER_CMD_QUERY unsupported"); return false; } if (!(ret & MEMBARRIER_CMD_PRIVATE_EXPEDITED) || !(ret & MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED)) { - log_error(os)("MEMBARRIER PRIVATE_EXPEDITED unsupported"); + log_info(os)("MEMBARRIER PRIVATE_EXPEDITED unsupported"); return false; } ret = membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0, 0); guarantee_with_errno(ret == 0, "MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED failed"); + log_info(os)("Using MEMBARRIER PRIVATE_EXPEDITED"); return true; } @@ -89,4 +90,3 @@ void LinuxSystemMemoryBarrier::emit() { int s = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0); guarantee_with_errno(s >= 0, "MEMBARRIER_CMD_PRIVATE_EXPEDITED failed"); } - diff --git a/src/hotspot/share/runtime/arguments.cpp b/src/hotspot/share/runtime/arguments.cpp index a38452ed10d..9222d3e3f7b 100644 --- a/src/hotspot/share/runtime/arguments.cpp +++ b/src/hotspot/share/runtime/arguments.cpp @@ -64,6 +64,7 @@ #include "utilities/parseInteger.hpp" #include "utilities/powerOfTwo.hpp" #include "utilities/stringUtils.hpp" +#include "utilities/systemMemoryBarrier.hpp" #if INCLUDE_JFR #include "jfr/jfr.hpp" #endif @@ -2153,6 +2154,8 @@ jint Arguments::parse_vm_init_args(const JavaVMInitArgs *vm_options_args, os::init_container_support(); + SystemMemoryBarrier::initialize(); + // Do final processing now that all arguments have been parsed result = finalize_vm_init_args(patch_mod_javabase); if (result != JNI_OK) { diff --git a/src/hotspot/share/runtime/globals.hpp b/src/hotspot/share/runtime/globals.hpp index 483287e6262..c67b17ae3fb 100644 --- a/src/hotspot/share/runtime/globals.hpp +++ b/src/hotspot/share/runtime/globals.hpp @@ -1293,8 +1293,8 @@ const int ObjectAlignmentInBytes = 8; "Delay in milliseconds for option SafepointTimeout") \ range(0, max_intx LP64_ONLY(/MICROUNITS)) \ \ - product(bool, UseSystemMemoryBarrier, false, EXPERIMENTAL, \ - "Try to enable system memory barrier") \ + product(bool, UseSystemMemoryBarrier, false, \ + "Try to enable system memory barrier if supported by OS") \ \ product(intx, NmethodSweepActivity, 4, \ "Removes cold nmethods from code cache if > 0. Higher values " \ diff --git a/src/hotspot/share/runtime/threads.cpp b/src/hotspot/share/runtime/threads.cpp index 2ade1575250..7384c125daf 100644 --- a/src/hotspot/share/runtime/threads.cpp +++ b/src/hotspot/share/runtime/threads.cpp @@ -95,7 +95,6 @@ #include "utilities/dtrace.hpp" #include "utilities/events.hpp" #include "utilities/macros.hpp" -#include "utilities/systemMemoryBarrier.hpp" #include "utilities/vmError.hpp" #if INCLUDE_JVMCI #include "jvmci/jvmci.hpp" @@ -552,14 +551,6 @@ jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) { // crash Linux VM, see notes in os_linux.cpp. main_thread->stack_overflow_state()->create_stack_guard_pages(); - if (UseSystemMemoryBarrier) { - if (!SystemMemoryBarrier::initialize()) { - vm_shutdown_during_initialization("Failed to initialize the requested system memory barrier synchronization."); - return JNI_EINVAL; - } - log_debug(os)("Using experimental system memory barrier synchronization"); - } - // Initialize Java-Level synchronization subsystem ObjectMonitor::Initialize(); ObjectSynchronizer::initialize(); diff --git a/src/hotspot/share/utilities/systemMemoryBarrier.hpp b/src/hotspot/share/utilities/systemMemoryBarrier.hpp index 7143b39d56b..5a70340f62d 100644 --- a/src/hotspot/share/utilities/systemMemoryBarrier.hpp +++ b/src/hotspot/share/utilities/systemMemoryBarrier.hpp @@ -26,6 +26,8 @@ #define SHARE_UTILITIES_SYSTEMMEMORYBARRIER_HPP #include "logging/log.hpp" +#include "runtime/globals.hpp" +#include "runtime/globals_extension.hpp" #include "utilities/globalDefinitions.hpp" #if defined(LINUX) @@ -38,7 +40,7 @@ typedef WindowsSystemMemoryBarrier SystemMemoryBarrierDefault; class NoSystemMemoryBarrier { public: static bool initialize() { - log_error(os)("SystemMemoryBarrier not supported on this platform"); + log_info(os)("SystemMemoryBarrier not supported on this platform"); return false; } static void emit() { @@ -51,8 +53,17 @@ typedef NoSystemMemoryBarrier SystemMemoryBarrierDefault; template class SystemMemoryBarrierType : public AllStatic { public: - static bool initialize() { return SystemMemoryBarrierImpl::initialize(); } - static void emit() { SystemMemoryBarrierImpl::emit(); } + static void initialize() { + if (UseSystemMemoryBarrier) { + if (!SystemMemoryBarrierImpl::initialize()) { + if (!FLAG_IS_DEFAULT(UseSystemMemoryBarrier)) { + warning("UseSystemMemoryBarrier specified, but not supported on this OS version. Use -Xlog:os=info for details."); + } + FLAG_SET_ERGO(UseSystemMemoryBarrier, false); + } + } + } + static void emit() { SystemMemoryBarrierImpl::emit(); } }; typedef SystemMemoryBarrierType SystemMemoryBarrier;