Skip to content

Commit a1fd5f4

Browse files
committedJan 27, 2025
8348554: Enhance Linux kernel version checks
Reviewed-by: shade, fyang
1 parent 002679a commit a1fd5f4

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed
 

‎src/hotspot/os/linux/os_linux.cpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -378,22 +378,40 @@ static void next_line(FILE *f) {
378378
} while (c != '\n' && c != EOF);
379379
}
380380

381-
void os::Linux::kernel_version(long* major, long* minor) {
382-
*major = -1;
383-
*minor = -1;
381+
void os::Linux::kernel_version(long* major, long* minor, long* patch) {
382+
*major = 0;
383+
*minor = 0;
384+
*patch = 0;
384385

385386
struct utsname buffer;
386387
int ret = uname(&buffer);
387388
if (ret != 0) {
388389
log_warning(os)("uname(2) failed to get kernel version: %s", os::errno_name(ret));
389390
return;
390391
}
391-
int nr_matched = sscanf(buffer.release, "%ld.%ld", major, minor);
392-
if (nr_matched != 2) {
393-
log_warning(os)("Parsing kernel version failed, expected 2 version numbers, only matched %d", nr_matched);
392+
int nr_matched = sscanf(buffer.release, "%ld.%ld.%ld", major, minor, patch);
393+
if (nr_matched != 3) {
394+
log_warning(os)("Parsing kernel version failed, expected 3 version numbers, only matched %d", nr_matched);
394395
}
395396
}
396397

398+
int os::Linux::kernel_version_compare(long major1, long minor1, long patch1,
399+
long major2, long minor2, long patch2) {
400+
// Compare major versions
401+
if (major1 > major2) return 1;
402+
if (major1 < major2) return -1;
403+
404+
// Compare minor versions
405+
if (minor1 > minor2) return 1;
406+
if (minor1 < minor2) return -1;
407+
408+
// Compare patchlevel versions
409+
if (patch1 > patch2) return 1;
410+
if (patch1 < patch2) return -1;
411+
412+
return 0;
413+
}
414+
397415
bool os::Linux::get_tick_information(CPUPerfTicks* pticks, int which_logical_cpu) {
398416
FILE* fh;
399417
uint64_t userTicks, niceTicks, systemTicks, idleTicks;

‎src/hotspot/os/linux/os_linux.hpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ class os::Linux {
9191
};
9292

9393
static int active_processor_count();
94-
static void kernel_version(long* major, long* minor);
94+
static void kernel_version(long* major, long* minor, long* patch);
95+
96+
// If kernel1 > kernel2 return 1
97+
// If kernel1 < kernel2 return -1
98+
// If kernel1 = kernel2 return 0
99+
static int kernel_version_compare(long major1, long minor1, long patch1,
100+
long major2, long minor2, long patch2);
95101

96102
// which_logical_cpu=-1 returns accumulated ticks for all cpus.
97103
static bool get_tick_information(CPUPerfTicks* pticks, int which_logical_cpu);

‎src/hotspot/os/linux/systemMemoryBarrier_linux.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ bool LinuxSystemMemoryBarrier::initialize() {
6666
// RISCV port was introduced in kernel 4.4.
6767
// 4.4 also made membar private expedited mandatory.
6868
// But RISCV actually don't support it until 6.9.
69-
long major, minor;
70-
os::Linux::kernel_version(&major, &minor);
71-
if (!(major > 6 || (major == 6 && minor >= 9))) {
72-
log_info(os)("Linux kernel %ld.%ld does not support MEMBARRIER PRIVATE_EXPEDITED on RISC-V.",
73-
major, minor);
69+
long major, minor, patch;
70+
os::Linux::kernel_version(&major, &minor, &patch);
71+
if (os::Linux::kernel_version_compare(major, minor, patch, 6, 9, 0) == -1) {
72+
log_info(os)("Linux kernel %ld.%ld.%ld does not support MEMBARRIER PRIVATE_EXPEDITED on RISC-V.",
73+
major, minor, patch);
7474
return false;
7575
}
7676
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.