[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250515133519.2779639-6-usamaarif642@gmail.com>
Date: Thu, 15 May 2025 14:33:34 +0100
From: Usama Arif <usamaarif642@...il.com>
To: Andrew Morton <akpm@...ux-foundation.org>,
david@...hat.com,
linux-mm@...ck.org
Cc: hannes@...xchg.org,
shakeel.butt@...ux.dev,
riel@...riel.com,
ziy@...dia.com,
laoar.shao@...il.com,
baolin.wang@...ux.alibaba.com,
lorenzo.stoakes@...cle.com,
Liam.Howlett@...cle.com,
npache@...hat.com,
ryan.roberts@....com,
linux-kernel@...r.kernel.org,
linux-doc@...r.kernel.org,
kernel-team@...a.com,
Usama Arif <usamaarif642@...il.com>
Subject: [PATCH 5/6] selftests: prctl: introduce tests for PR_THP_POLICY_DEFAULT_HUGE
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_THP_POLICY_SYSTEM
- if hugepage is gotten after the policy reset only with MADV_HUGEPAGE
Signed-off-by: Usama Arif <usamaarif642@...il.com>
---
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 e39872a6d429..65c87f99423e 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_THP_POLICY_DEFAULT_HUGE, 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_THP_POLICY_DEFAULT_HUGE) {
+ 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_THP_POLICY_DEFAULT_HUGE set but didn't get hugepages\n");
+ goto err_out;
+ }
+
+ is_anonhuge = test_mmap_thp(1);
+ if (!is_anonhuge) {
+ printf("PR_THP_POLICY_DEFAULT_HUGE set but did't get hugepages\n");
+ goto err_out;
+ }
+
+ /* Reset to system policy */
+ if (prctl(PR_SET_THP_POLICY, PR_THP_POLICY_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
Powered by blists - more mailing lists