[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250515133519.2779639-3-usamaarif642@gmail.com>
Date: Thu, 15 May 2025 14:33:31 +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 2/6] prctl: introduce PR_THP_POLICY_DEFAULT_NOHUGE for the process
This is set via the new PR_SET_THP_POLICY prctl.
This will set the MMF2_THP_VMA_DEFAULT_NOHUGE process flag
which changes the default of new VMAs to be VM_NOHUGEPAGE. The
call also modifies all existing VMAs that are not VM_HUGEPAGE
to be VM_NOHUGEPAGE. The policy is inherited during fork+exec.
This allows systems where the global policy is set to "always"
to effectively have THPs on madvise only for the process. In an
environment where different types of workloads are stacked on the
same machine,this will allow workloads that benefit from having
hugepages on an madvise basis only to do so, without regressing those
that benefit from having hugepages always.
Signed-off-by: Usama Arif <usamaarif642@...il.com>
---
include/linux/huge_mm.h | 1 +
include/linux/mm_types.h | 5 +++-
include/uapi/linux/prctl.h | 1 +
kernel/sys.c | 8 +++++++
mm/huge_memory.c | 24 +++++++++++++++++++
tools/include/uapi/linux/prctl.h | 1 +
.../trace/beauty/include/uapi/linux/prctl.h | 1 +
7 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index e652ad9ddbbd..d46bba282701 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -262,6 +262,7 @@ static inline unsigned long thp_vma_suitable_orders(struct vm_area_struct *vma,
void vma_set_thp_policy(struct vm_area_struct *vma);
void process_vmas_thp_default_huge(struct mm_struct *mm);
+void process_vmas_thp_default_nohuge(struct mm_struct *mm);
unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
unsigned long vm_flags,
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 2fe93965e761..5e770411d8d1 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -1747,8 +1747,11 @@ enum {
#define MMF2_THP_VMA_DEFAULT_HUGE 0
#define MMF2_THP_VMA_DEFAULT_HUGE_MASK (1 << MMF2_THP_VMA_DEFAULT_HUGE)
+#define MMF2_THP_VMA_DEFAULT_NOHUGE 1
+#define MMF2_THP_VMA_DEFAULT_NOHUGE_MASK (1 << MMF2_THP_VMA_DEFAULT_NOHUGE)
-#define MMF2_INIT_MASK (MMF2_THP_VMA_DEFAULT_HUGE_MASK)
+#define MMF2_INIT_MASK (MMF2_THP_VMA_DEFAULT_HUGE_MASK |\
+ MMF2_THP_VMA_DEFAULT_NOHUGE_MASK)
static inline unsigned long mmf_init_flags(unsigned long flags)
{
diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
index 325c72f40a93..d25458f4db9e 100644
--- a/include/uapi/linux/prctl.h
+++ b/include/uapi/linux/prctl.h
@@ -367,5 +367,6 @@ struct prctl_mm_map {
#define PR_SET_THP_POLICY 78
#define PR_GET_THP_POLICY 79
#define PR_THP_POLICY_DEFAULT_HUGE 0
+#define PR_THP_POLICY_DEFAULT_NOHUGE 1
#endif /* _LINUX_PRCTL_H */
diff --git a/kernel/sys.c b/kernel/sys.c
index 1115f258f253..d91203e6dd0d 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -2663,6 +2663,8 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
return -EINVAL;
if (!!test_bit(MMF2_THP_VMA_DEFAULT_HUGE, &me->mm->flags2))
error = PR_THP_POLICY_DEFAULT_HUGE;
+ else if (!!test_bit(MMF2_THP_VMA_DEFAULT_NOHUGE, &me->mm->flags2))
+ error = PR_THP_POLICY_DEFAULT_NOHUGE;
break;
case PR_SET_THP_POLICY:
if (arg3 || arg4 || arg5)
@@ -2672,8 +2674,14 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
switch (arg2) {
case PR_THP_POLICY_DEFAULT_HUGE:
set_bit(MMF2_THP_VMA_DEFAULT_HUGE, &me->mm->flags2);
+ clear_bit(MMF2_THP_VMA_DEFAULT_NOHUGE, &me->mm->flags2);
process_vmas_thp_default_huge(me->mm);
break;
+ case PR_THP_POLICY_DEFAULT_NOHUGE:
+ clear_bit(MMF2_THP_VMA_DEFAULT_HUGE, &me->mm->flags2);
+ set_bit(MMF2_THP_VMA_DEFAULT_NOHUGE, &me->mm->flags2);
+ process_vmas_thp_default_nohuge(me->mm);
+ break;
default:
return -EINVAL;
}
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 64f66d5295e8..9d70a365ced3 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -104,6 +104,8 @@ void vma_set_thp_policy(struct vm_area_struct *vma)
if (test_bit(MMF2_THP_VMA_DEFAULT_HUGE, &mm->flags2))
vm_flags_set(vma, VM_HUGEPAGE);
+ else if (test_bit(MMF2_THP_VMA_DEFAULT_NOHUGE, &mm->flags2))
+ vm_flags_set(vma, VM_NOHUGEPAGE);
}
static void vmas_thp_default_huge(struct mm_struct *mm)
@@ -129,6 +131,28 @@ void process_vmas_thp_default_huge(struct mm_struct *mm)
vmas_thp_default_huge(mm);
}
+static void vmas_thp_default_nohuge(struct mm_struct *mm)
+{
+ struct vm_area_struct *vma;
+ unsigned long vm_flags;
+
+ VMA_ITERATOR(vmi, mm, 0);
+ for_each_vma(vmi, vma) {
+ vm_flags = vma->vm_flags;
+ if (vm_flags & VM_HUGEPAGE)
+ continue;
+ vm_flags_set(vma, VM_NOHUGEPAGE);
+ }
+}
+
+void process_vmas_thp_default_nohuge(struct mm_struct *mm)
+{
+ if (test_bit(MMF2_THP_VMA_DEFAULT_NOHUGE, &mm->flags2))
+ return;
+
+ set_bit(MMF2_THP_VMA_DEFAULT_NOHUGE, &mm->flags2);
+ vmas_thp_default_nohuge(mm);
+}
unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
unsigned long vm_flags,
diff --git a/tools/include/uapi/linux/prctl.h b/tools/include/uapi/linux/prctl.h
index f5945ebfe3f2..e03d0ed890c5 100644
--- a/tools/include/uapi/linux/prctl.h
+++ b/tools/include/uapi/linux/prctl.h
@@ -331,5 +331,6 @@ struct prctl_mm_map {
#define PR_SET_THP_POLICY 78
#define PR_GET_THP_POLICY 79
#define PR_THP_POLICY_DEFAULT_HUGE 0
+#define PR_THP_POLICY_DEFAULT_NOHUGE 1
#endif /* _LINUX_PRCTL_H */
diff --git a/tools/perf/trace/beauty/include/uapi/linux/prctl.h b/tools/perf/trace/beauty/include/uapi/linux/prctl.h
index 325c72f40a93..d25458f4db9e 100644
--- a/tools/perf/trace/beauty/include/uapi/linux/prctl.h
+++ b/tools/perf/trace/beauty/include/uapi/linux/prctl.h
@@ -367,5 +367,6 @@ struct prctl_mm_map {
#define PR_SET_THP_POLICY 78
#define PR_GET_THP_POLICY 79
#define PR_THP_POLICY_DEFAULT_HUGE 0
+#define PR_THP_POLICY_DEFAULT_NOHUGE 1
#endif /* _LINUX_PRCTL_H */
--
2.47.1
Powered by blists - more mailing lists