Skip to content

Commit

Permalink
Guard more madv numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
limingliu-ampere committed Jun 7, 2024
1 parent fe98ec0 commit cb2adb8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
60 changes: 55 additions & 5 deletions src/hotspot/os/linux/os_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#include "utilities/growableArray.hpp"
#include "utilities/macros.hpp"
#include "utilities/powerOfTwo.hpp"
#include "utilities/tuple.hpp"
#include "utilities/vmError.hpp"
#if INCLUDE_JFR
#include "jfr/jfrEvents.hpp"
Expand Down Expand Up @@ -176,7 +177,8 @@ pthread_t os::Linux::_main_thread;
bool os::Linux::_supports_fast_thread_cpu_time = false;
const char * os::Linux::_libc_version = nullptr;
const char * os::Linux::_libpthread_version = nullptr;

long os::Linux::kernel_version_major = -1;
long os::Linux::kernel_version_minor = -1;
bool os::Linux::_thp_requested{false};

#ifdef __GLIBC__
Expand Down Expand Up @@ -2966,6 +2968,15 @@ void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,
#define MADV_HUGEPAGE 14
#endif

// Define MADV_POPULATE_READ here so we can build HotSpot on old systems.
#define MADV_POPULATE_READ_value 22
#ifndef MADV_POPULATE_READ
#define MADV_POPULATE_READ MADV_POPULATE_READ_value
#else
// Sanity-check our assumed default value if we build with a new enough libc.
STATIC_ASSERT(MADV_POPULATE_READ == MADV_POPULATE_READ_value);
#endif

// Define MADV_POPULATE_WRITE here so we can build HotSpot on old systems.
#define MADV_POPULATE_WRITE_value 23
#ifndef MADV_POPULATE_WRITE
Expand All @@ -2975,6 +2986,24 @@ void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,
STATIC_ASSERT(MADV_POPULATE_WRITE == MADV_POPULATE_WRITE_value);
#endif

// Define MADV_DONTNEED_LOCKED here so we can build HotSpot on old systems.
#define MADV_DONTNEED_LOCKED_value 24
#ifndef MADV_DONTNEED_LOCKED
#define MADV_DONTNEED_LOCKED MADV_DONTNEED_LOCKED_value
#else
// Sanity-check our assumed default value if we build with a new enough libc.
STATIC_ASSERT(MADV_DONTNEED_LOCKED == MADV_DONTNEED_LOCKED_value);
#endif

// Define MADV_COLLAPSE here so we can build HotSpot on old systems.
#define MADV_COLLAPSE_value 25
#ifndef MADV_COLLAPSE
#define MADV_COLLAPSE MADV_COLLAPSE_value
#else
// Sanity-check our assumed default value if we build with a new enough libc.
STATIC_ASSERT(MADV_COLLAPSE == MADV_COLLAPSE_value);
#endif

// Note that the value for MAP_FIXED_NOREPLACE differs between architectures, but all architectures
// supported by OpenJDK share the same flag value.
#define MAP_FIXED_NOREPLACE_value 0x100000
Expand All @@ -2985,6 +3014,27 @@ void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,
STATIC_ASSERT(MAP_FIXED_NOREPLACE == MAP_FIXED_NOREPLACE_value);
#endif

// Guard newer madv numbers by checking kernel versions, as downstream kernels
// might use the numbers as unexpected behaviors.
bool os::Linux::can_use_madvise_flag(int advice) {
static constexpr Tuple<int, long, long> adviceVersions[] = {
{ MADV_POPULATE_READ, 5, 14 },
{ MADV_POPULATE_WRITE, 5, 14 },
{ MADV_DONTNEED_LOCKED, 5, 18 },
{ MADV_COLLAPSE, 6, 1 }
};

for (auto &t : adviceVersions) {
if (advice == t.get<0>()) {
return kernel_version_major > t.get<1>() ||
(kernel_version_major == t.get<1>() &&
kernel_version_minor >= t.get<2>());
}
}

return true;
}

int os::Linux::commit_memory_impl(char* addr, size_t size,
size_t alignment_hint, bool exec) {
int err = os::Linux::commit_memory_impl(addr, size, exec);
Expand Down Expand Up @@ -4522,6 +4572,8 @@ void os::init(void) {

check_pax();

Linux::kernel_version(&Linux::kernel_version_major, &Linux::kernel_version_minor);

os::Posix::init();
}

Expand Down Expand Up @@ -4803,17 +4855,15 @@ jint os::init_2(void) {
// Some downstream kernels recognize MADV_POPULATE_WRITE (23) as another
// advice, so the check of versions is required here.
// See https://github.com/oracle/linux-uek/issues/23
long major, minor;
Linux::kernel_version(&major, &minor);
bool supportMadvPopulateWrite =
((major > 5 || (major == 5 && minor >= 14)) &&
(Linux::can_use_madvise_flag(MADV_POPULATE_WRITE) &&
(::madvise(0, 0, MADV_POPULATE_WRITE) == 0));
if (!supportMadvPopulateWrite) {
if (!FLAG_IS_DEFAULT(UseMadvPopulateWrite)) {
warning("Platform does not support MADV_POPULATE_WRITE, "
"disabling using it to pretouch (-XX:-UseMadvPopulateWrite)");
}
UseMadvPopulateWrite = false;
FLAG_SET_ERGO(UseMadvPopulateWrite, false);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/hotspot/os/linux/os_linux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class os::Linux {
static const char *_libc_version;
static const char *_libpthread_version;

static long kernel_version_major;
static long kernel_version_minor;

static bool _supports_fast_thread_cpu_time;

static GrowableArray<int>* _cpu_to_node;
Expand Down Expand Up @@ -193,6 +196,8 @@ class os::Linux {
// none present

private:
static bool can_use_madvise_flag(int advice);

static void numa_init();

typedef int (*sched_getcpu_func_t)(void);
Expand Down

0 comments on commit cb2adb8

Please sign in to comment.