From 59d9d9fcb93c26dd8931d70934b889245b050acc Mon Sep 17 00:00:00 2001 From: Poonam Bajaj Date: Fri, 2 Jun 2023 13:32:22 +0000 Subject: [PATCH] 8303215: Make thread stacks not use huge pages Reviewed-by: stuefe, dholmes --- src/hotspot/os/linux/os_linux.cpp | 20 ++++++++++++++----- .../linux_aarch64/globals_linux_aarch64.hpp | 14 ++++++++++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index fae9143a2ae..6c659e6c7cc 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -927,6 +927,15 @@ bool os::create_thread(Thread* thread, ThreadType thr_type, } assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned"); + // 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 && + 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 @@ -3745,8 +3754,11 @@ bool os::Linux::setup_large_page_type(size_t page_size) { } void os::large_page_init() { - // 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 + // 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 if (!UseLargePages && !UseTransparentHugePages && !UseHugeTLBFS && @@ -3764,9 +3776,7 @@ void os::large_page_init() { return; } - // 2) Scan OS info - size_t default_large_page_size = 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(); 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 6f33872bc23..c4219c2bac3 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,18 @@ // (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, CompilerThreadStackSize, 2048); +// 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 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. 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); + +define_pd_global(intx, CompilerThreadStackSize, 2040); define_pd_global(uintx,JVMInvokeMethodSlack, 8192);