[PATCH v3 6/7] selftests: prctl: introduce tests for PR_THP_POLICY_DEFAULT_HUGE
From: Usama Arif
Date: Mon May 19 2025 - 18:34:46 EST
The test is limited to 2M PMD THPs. It does not modify the system
settings in order to not disturb other process running in the
system.
It runs if the PMD size is 2M, if the 2M policy is set to inherit
and if the system global THP policy is set to "madvise", so that
the change in behaviour due to PR_THP_POLICY_DEFAULT_HUGE can
be seen.
This tests if:
- the process can successfully set the policy
- carry it over to the new process with fork
- if hugepage is gotten both with and without madvise
- the process can successfully reset the policy to
PR_DEFAULT_SYSTEM
- if hugepage is gotten after the policy reset only with MADV_HUGEPAGE
Signed-off-by: Usama Arif <usamaarif642@xxxxxxxxx>
---
tools/testing/selftests/prctl/thp_policy.c | 74 +++++++++++++++++++++-
1 file changed, 73 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/prctl/thp_policy.c b/tools/testing/selftests/prctl/thp_policy.c
index 7791d282f7c8..62cf1fa6fd28 100644
--- a/tools/testing/selftests/prctl/thp_policy.c
+++ b/tools/testing/selftests/prctl/thp_policy.c
@@ -203,6 +203,77 @@ static int test_global_always_process_nohuge(void)
return -1;
}
+/* Global policy is madvise, process is changed to HUGE (process becomes always) */
+static int test_global_madvise_process_huge(void)
+{
+ int is_anonhuge = 0, res = 0, status = 0;
+ pid_t pid;
+
+ if (prctl(PR_SET_THP_POLICY, PR_DEFAULT_MADV_HUGEPAGE, NULL, NULL, NULL) != 0) {
+ perror("prctl failed to set process policy to always");
+ return -1;
+ }
+
+ /* Make sure prctl changes are carried across fork */
+ pid = fork();
+ if (pid < 0) {
+ perror("fork");
+ exit(EXIT_FAILURE);
+ }
+
+ res = prctl(PR_GET_THP_POLICY, NULL, NULL, NULL, NULL);
+ if (res != PR_DEFAULT_MADV_HUGEPAGE) {
+ printf("prctl PR_GET_THP_POLICY returned %d pid %d\n", res, pid);
+ goto err_out;
+ }
+
+ /* global = madvise, process = always, we should get HPs irrespective of MADV_HUGEPAGE */
+ is_anonhuge = test_mmap_thp(0);
+ if (!is_anonhuge) {
+ printf("PR_DEFAULT_MADV_HUGEPAGE set but didn't get hugepages\n");
+ goto err_out;
+ }
+
+ is_anonhuge = test_mmap_thp(1);
+ if (!is_anonhuge) {
+ printf("PR_DEFAULT_MADV_HUGEPAGE set but did't get hugepages\n");
+ goto err_out;
+ }
+
+ /* Reset to system policy */
+ if (prctl(PR_SET_THP_POLICY, PR_DEFAULT_SYSTEM, NULL, NULL, NULL) != 0) {
+ perror("prctl failed to set policy to system");
+ goto err_out;
+ }
+
+ is_anonhuge = test_mmap_thp(0);
+ if (is_anonhuge) {
+ printf("global policy is madvise\n");
+ goto err_out;
+ }
+
+ is_anonhuge = test_mmap_thp(1);
+ if (!is_anonhuge) {
+ printf("global policy is madvise\n");
+ goto err_out;
+ }
+
+ if (pid == 0) {
+ exit(EXIT_SUCCESS);
+ } else {
+ wait(&status);
+ if (WIFEXITED(status))
+ return 0;
+ else
+ return -1;
+ }
+err_out:
+ if (pid == 0)
+ exit(EXIT_FAILURE);
+ else
+ return -1;
+}
+
int main(void)
{
if (sysfs_check())
@@ -210,5 +281,6 @@ int main(void)
if (system_thp_policy == SYSTEM_POLICY_ALWAYS)
return test_global_always_process_nohuge();
-
+ else
+ return test_global_madvise_process_huge();
}
--
2.47.1