From 0e0dd33fff9922a086968cfa6ccd27727f979c83 Mon Sep 17 00:00:00 2001
From: Kim Barrett <kbarrett@openjdk.org>
Date: Fri, 5 Nov 2021 21:20:51 +0000
Subject: [PATCH] 8276129: PretouchTask should page-align the chunk size

Reviewed-by: stefank, tschatzl
---
 src/hotspot/share/gc/shared/pretouchTask.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/hotspot/share/gc/shared/pretouchTask.cpp b/src/hotspot/share/gc/shared/pretouchTask.cpp
index c345a12806b..4c18cf3eb20 100644
--- a/src/hotspot/share/gc/shared/pretouchTask.cpp
+++ b/src/hotspot/share/gc/shared/pretouchTask.cpp
@@ -29,6 +29,7 @@
 #include "runtime/atomic.hpp"
 #include "runtime/globals.hpp"
 #include "runtime/os.hpp"
+#include "utilities/align.hpp"
 
 PretouchTask::PretouchTask(const char* task_name,
                            char* start_address,
@@ -64,9 +65,9 @@ void PretouchTask::work(uint worker_id) {
 
 void PretouchTask::pretouch(const char* task_name, char* start_address, char* end_address,
                             size_t page_size, WorkerThreads* pretouch_workers) {
-  // Chunk size should be at least (unmodified) page size as using multiple threads
-  // pretouch on a single page can decrease performance.
-  size_t chunk_size = MAX2(PretouchTask::chunk_size(), page_size);
+  // Page-align the chunk size, so if start_address is also page-aligned (as
+  // is common) then there won't be any pages shared by multiple chunks.
+  size_t chunk_size = align_down_bounded(PretouchTask::chunk_size(), page_size);
 #ifdef LINUX
   // When using THP we need to always pre-touch using small pages as the OS will
   // initially always use small pages.