From 63fa74a95e3228a5273190b4f77ad7c836eb7a84 Mon Sep 17 00:00:00 2001 From: Poonam Parhar Date: Tue, 23 May 2023 17:42:59 +0000 Subject: [PATCH 1/5] 8303215: Make thread stacks not use huge pages --- src/hotspot/os/linux/os_linux.cpp | 12 ++++++++++-- .../os_cpu/linux_aarch64/globals_linux_aarch64.hpp | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index fae9143a2aefd..ed07673298c73 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -927,6 +927,14 @@ bool os::create_thread(Thread* thread, ThreadType thr_type, } assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned"); + size_t default_large_page_size = os::Linux::scan_default_large_page_size(); + + // Add an additional page to the stack size to reduce its chances of getting large page aligned + // so that the stack does not get backed by a transparent huge page. + if (stack_size >= default_large_page_size && is_aligned(stack_size, default_large_page_size)) { + stack_size += os::vm_page_size(); + } + int status = pthread_attr_setstacksize(&attr, stack_size); if (status != 0) { // pthread_attr_setstacksize() function can fail @@ -3619,7 +3627,7 @@ static void set_coredump_filter(CoredumpFilterBit bit) { static size_t _large_page_size = 0; -static size_t scan_default_large_page_size() { +size_t os::Linux::scan_default_large_page_size() { size_t default_large_page_size = 0; // large_page_size on Linux is used to round up heap size. x86 uses either @@ -3765,7 +3773,7 @@ void os::large_page_init() { } // 2) Scan OS info - size_t default_large_page_size = scan_default_large_page_size(); + size_t default_large_page_size = os::Linux::scan_default_large_page_size(); os::Linux::_default_large_page_size = default_large_page_size; if (default_large_page_size == 0) { // No large pages configured, return. diff --git a/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp b/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp index 6f33872bc2345..44b931501e118 100644 --- a/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp +++ b/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp @@ -30,10 +30,10 @@ // (see globals.hpp) define_pd_global(bool, DontYieldALot, false); -define_pd_global(intx, ThreadStackSize, 2048); // 0 => use system default -define_pd_global(intx, VMThreadStackSize, 2048); +define_pd_global(intx, ThreadStackSize, 2040); // 0 => use system default +define_pd_global(intx, VMThreadStackSize, 2040); -define_pd_global(intx, CompilerThreadStackSize, 2048); +define_pd_global(intx, CompilerThreadStackSize, 2040); define_pd_global(uintx,JVMInvokeMethodSlack, 8192); From c1353af77275279bf0baaef86cf14a87618ce558 Mon Sep 17 00:00:00 2001 From: Poonam Parhar Date: Thu, 25 May 2023 16:18:33 +0000 Subject: [PATCH 2/5] call scan_default_large_page_size() in the beginning of large_page_init() --- src/hotspot/os/linux/os_linux.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index ed07673298c73..c747b2d237600 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -927,11 +927,9 @@ bool os::create_thread(Thread* thread, ThreadType thr_type, } assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned"); - size_t default_large_page_size = os::Linux::scan_default_large_page_size(); - // Add an additional page to the stack size to reduce its chances of getting large page aligned // so that the stack does not get backed by a transparent huge page. - if (stack_size >= default_large_page_size && is_aligned(stack_size, default_large_page_size)) { + if (stack_size >= os::Linux::_default_large_page_size && is_aligned(stack_size, os::Linux::_default_large_page_size)) { stack_size += os::vm_page_size(); } @@ -3753,6 +3751,10 @@ bool os::Linux::setup_large_page_type(size_t page_size) { } void os::large_page_init() { + // Scan default large page size + size_t default_large_page_size = os::Linux::scan_default_large_page_size(); + os::Linux::_default_large_page_size = default_large_page_size; + // 1) Handle the case where we do not want to use huge pages and hence // there is no need to scan the OS for related info if (!UseLargePages && @@ -3772,9 +3774,7 @@ void os::large_page_init() { return; } - // 2) Scan OS info - size_t default_large_page_size = os::Linux::scan_default_large_page_size(); - os::Linux::_default_large_page_size = default_large_page_size; + // 2) check if large pages are configured if (default_large_page_size == 0) { // No large pages configured, return. warn_no_large_pages_configured(); From a46982470c7c3c4f7e3d12aaa2a0b9d6dfb7e8a5 Mon Sep 17 00:00:00 2001 From: Poonam Parhar Date: Fri, 26 May 2023 20:12:39 +0000 Subject: [PATCH 3/5] addressed review comments --- src/hotspot/os/linux/os_linux.cpp | 6 +++--- src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index c747b2d237600..5f6bfdc3d060f 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -929,7 +929,7 @@ bool os::create_thread(Thread* thread, ThreadType thr_type, // Add an additional page to the stack size to reduce its chances of getting large page aligned // so that the stack does not get backed by a transparent huge page. - if (stack_size >= os::Linux::_default_large_page_size && is_aligned(stack_size, os::Linux::_default_large_page_size)) { + if (stack_size >= os::Linux::default_large_page_size() && is_aligned(stack_size, os::Linux::default_large_page_size())) { stack_size += os::vm_page_size(); } @@ -3625,7 +3625,7 @@ static void set_coredump_filter(CoredumpFilterBit bit) { static size_t _large_page_size = 0; -size_t os::Linux::scan_default_large_page_size() { +static size_t scan_default_large_page_size() { size_t default_large_page_size = 0; // large_page_size on Linux is used to round up heap size. x86 uses either @@ -3752,7 +3752,7 @@ bool os::Linux::setup_large_page_type(size_t page_size) { void os::large_page_init() { // Scan default large page size - size_t default_large_page_size = os::Linux::scan_default_large_page_size(); + size_t default_large_page_size = scan_default_large_page_size(); os::Linux::_default_large_page_size = default_large_page_size; // 1) Handle the case where we do not want to use huge pages and hence diff --git a/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp b/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp index 44b931501e118..cff297efc1d5a 100644 --- a/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp +++ b/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp @@ -30,6 +30,12 @@ // (see globals.hpp) define_pd_global(bool, DontYieldALot, false); + +// set default stack sizes < 2MB so as to prevent stacks from getting +// large-page aligned and backed by THPs on systems where 2MB is the +// default huge page size. For non-JavaThreads, glibc adds an additional +// guard page to the total stack size, so to keep the default sizes same +// for all the following flags, we set them to 2 pages less than 2MB. define_pd_global(intx, ThreadStackSize, 2040); // 0 => use system default define_pd_global(intx, VMThreadStackSize, 2040); From 7643e8d17dc47e2ba555ab30e1c454675f1535a0 Mon Sep 17 00:00:00 2001 From: Poonam Parhar Date: Tue, 30 May 2023 20:25:39 +0000 Subject: [PATCH 4/5] addressed second set of review comments --- src/hotspot/os/linux/os_linux.cpp | 12 +++++++----- .../os_cpu/linux_aarch64/globals_linux_aarch64.hpp | 8 +++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index 5f6bfdc3d060f..f6f98bd4dd259 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -929,8 +929,11 @@ bool os::create_thread(Thread* thread, ThreadType thr_type, // Add an additional page to the stack size to reduce its chances of getting large page aligned // so that the stack does not get backed by a transparent huge page. - if (stack_size >= os::Linux::default_large_page_size() && is_aligned(stack_size, os::Linux::default_large_page_size())) { - stack_size += os::vm_page_size(); + size_t default_large_page_size = os::Linux::default_large_page_size(); + if (default_large_page_size != 0) { + if (stack_size >= default_large_page_size && is_aligned(stack_size, default_large_page_size)) { + stack_size += os::vm_page_size(); + } } int status = pthread_attr_setstacksize(&attr, stack_size); @@ -3751,12 +3754,11 @@ bool os::Linux::setup_large_page_type(size_t page_size) { } void os::large_page_init() { - // Scan default large page size + // Always initialize the default large page size even if large pages are not being used. size_t default_large_page_size = scan_default_large_page_size(); os::Linux::_default_large_page_size = default_large_page_size; - // 1) Handle the case where we do not want to use huge pages and hence - // there is no need to scan the OS for related info + // 1) Handle the case where we do not want to use huge pages if (!UseLargePages && !UseTransparentHugePages && !UseHugeTLBFS && diff --git a/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp b/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp index cff297efc1d5a..c4219c2bac3d8 100644 --- a/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp +++ b/src/hotspot/os_cpu/linux_aarch64/globals_linux_aarch64.hpp @@ -31,11 +31,13 @@ define_pd_global(bool, DontYieldALot, false); -// set default stack sizes < 2MB so as to prevent stacks from getting +// Set default stack sizes < 2MB so as to prevent stacks from getting // large-page aligned and backed by THPs on systems where 2MB is the -// default huge page size. For non-JavaThreads, glibc adds an additional +// default huge page size. For non-JavaThreads, glibc may add an additional // guard page to the total stack size, so to keep the default sizes same -// for all the following flags, we set them to 2 pages less than 2MB. +// for all the following flags, we set them to 2 pages less than 2MB. On +// systems where 2MB is the default large page size, 4KB is most commonly +// the regular page size. define_pd_global(intx, ThreadStackSize, 2040); // 0 => use system default define_pd_global(intx, VMThreadStackSize, 2040); From 1ef71431b7b4870ab2b4a39819a9e73b174bf395 Mon Sep 17 00:00:00 2001 From: Poonam Parhar Date: Wed, 31 May 2023 16:11:26 +0000 Subject: [PATCH 5/5] combined the if-clause statements --- src/hotspot/os/linux/os_linux.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index f6f98bd4dd259..6c659e6c7cc9d 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -930,10 +930,10 @@ bool os::create_thread(Thread* thread, ThreadType thr_type, // Add an additional page to the stack size to reduce its chances of getting large page aligned // so that the stack does not get backed by a transparent huge page. size_t default_large_page_size = os::Linux::default_large_page_size(); - if (default_large_page_size != 0) { - if (stack_size >= default_large_page_size && is_aligned(stack_size, default_large_page_size)) { - stack_size += os::vm_page_size(); - } + if (default_large_page_size != 0 && + stack_size >= default_large_page_size && + is_aligned(stack_size, default_large_page_size)) { + stack_size += os::vm_page_size(); } int status = pthread_attr_setstacksize(&attr, stack_size);