From f0eefbef3e67090a9eb8f74c24e484e960013e52 Mon Sep 17 00:00:00 2001 From: Dmitry Ilvokhin Date: Tue, 18 Feb 2025 10:59:10 -0800 Subject: [PATCH] Make hugify system call only when thp=madvise When `/sys/kernel/mm/transparent_hugepage/enabled` is always or never it doesn't make much sense to call `madvise(..., MADV_HUGEPAGE)` and `madvise(..., MADV_NOHUGEPAGE)`, because it will not have desired effect anyway, but we'll waste CPU cycles on system call. Call `madvise(..., MADV_HUGEPAGE)` and `madvise(..., MADV_NOHUGEPAGE)`, only, when `/sys/kernel/mm/transparent_hugepage/enabled` is set to `madvise` --- src/hpa_hooks.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/hpa_hooks.c b/src/hpa_hooks.c index 4628c14f..2a6d30be 100644 --- a/src/hpa_hooks.c +++ b/src/hpa_hooks.c @@ -53,7 +53,15 @@ hpa_hooks_hugify(void *ptr, size_t size, bool sync) { * hints, when EINVAL is returned it is likely that khugepaged won't be * able to collapse memory range into hugepage either). */ - bool err = pages_huge(ptr, size); + bool err = false; + /* + * It doesn't make much sense to call madvise(..., MADV_HUGEPAGE), when + * transparent_hugepage option is never or always, we'll just waste CPU + * cycles on system call without any effect. + */ + if (init_system_thp_mode == thp_mode_default) { + err = pages_huge(ptr, size); + } if (sync) { err = pages_collapse(ptr, size); } @@ -62,6 +70,9 @@ hpa_hooks_hugify(void *ptr, size_t size, bool sync) { static void hpa_hooks_dehugify(void *ptr, size_t size) { + if (init_system_thp_mode != thp_mode_default) { + return; + } bool err = pages_nohuge(ptr, size); (void)err; }