Skip to contentSkip to conversation

prctl: extend PR_SET_THP_DISABLE to optionally exclude VM_HUGEPAGE

PATCH In review David Hildenbrand 4 participants 18 messages Jul 21, 2025 – Jul 25, 2025

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 prctl: extend PR_SET_THP_DISABLE to optionally exclude VM_HUGEPAGE

People want to make use of more THPs, for example, moving from THP=never to THP=madvise, or from THP=madvise to THP=never.

While this is great news for every THP desperately waiting to get allocated out there, apparently there are some workloads that require a bit of care during that transition: once problems are detected, these workloads should be started with the old behavior, without making all other workloads on the system go back to the old behavior as well.

In essence, the following scenarios are imaginable:

(1) Switch from THP=none to THP=madvise or THP=always, but keep the old behavior (no THP) for selected workloads.

(2) Stay at THP=none, but have "madvise" or "always" behavior for selected workloads.

(3) Switch from THP=madvise to THP=always, but keep the old behavior (THP only when advised) for selected workloads.

(4) Stay at THP=madvise, but have "always" behavior for selected workloads.

In essence, (2) can be emulated through (1), by setting THP!=none while disabling THPs for all processes that don't want THPs. It requires configuring all workloads, but that is a user-space problem to sort out.

(4) can be emulated through (3) in a similar way.

Back when (1) was relevant in the past, as people started enabling THPs, we added PR_SET_THP_DISABLE, so relevant workloads that were not ready yet (i.e., used by Redis) were able to just disable THPs completely. Redis still implements the option to use this interface to disable THPs completely.

With PR_SET_THP_DISABLE, we added a way to force-disable THPs for a workload -- a process, including fork+exec'ed process hierarchy. That essentially made us support (1): simply disable THPs for all workloads that are not ready for THPs yet, while still enabling THPs system-wide.

The quest for handling (3) and (4) started, but current approaches (completely new prctl, options to set other policies per processm,
alternatives to prctl -- mctrl, cgroup handling) don't look particularly
promising. Likely, the future will use bpf or something similar to implement better policies, in particular to also make better decisions about THP sizes to use, but this will certainly take a while as that work just started.

Long story short: a simple enable/disable is not really suitable for the future, so we're not willing to add completely new toggles.

While we could emulate (3)+(4) through (1)+(2) by simply disabling THPs completely for these processes, this scares many THPs in our system because they could no longer get allocated where they used to be allocated
for: regions flagged as VM_HUGEPAGE. Apparently, that imposes a problem for relevant workloads, because "not THPs" is certainly worse than "THPs only when advised".

Could we simply relax PR_SET_THP_DISABLE, to "disable THPs unless not explicitly advised by the app through MAD_HUGEPAGE"? *maybe*, but this would change the documented semantics quite a bit, and the versatility to use it for debugging purposes, so I am not 100% sure that is what we want -- although it would certainly be much easier.

So instead, as an easy way forward for (3) and (4), an option to make PR_SET_THP_DISABLE disable *less* THPs for a process.

In essence, this patch:

(A) Adds PR_THP_DISABLE_EXCEPT_ADVISED, to be used as a flag in arg3 of prctl(PR_SET_THP_DISABLE) when disabling THPs (arg2 != 0).

For now, arg3 was not allowed to be set (-EINVAL). Now it holds flags.

(B) Makes prctl(PR_GET_THP_DISABLE) return 3 if PR_THP_DISABLE_EXCEPT_ADVISED was set while disabling.

For now, it would return 1 if THPs were disabled completely. Now it essentially returns the set flags as well.

(C) Renames MMF_DISABLE_THP to MMF_DISABLE_THP_COMPLETELY, to express the semantics clearly.

Fortunately, there are only two instances outside of prctl() code.

(D) Adds MMF_DISABLE_THP_EXCEPT_ADVISED to express "no THP except for VMAs with VM_HUGEPAGE" -- essentially "thp=madvise" behavior

Fortunately, we only have to extend vma_thp_disabled().

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

The documented semantics in the man page for PR_SET_THP_DISABLE "is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

Cc Jonathan Corbet <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Usama Arif <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Jann Horn <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Yafang Shao <[email protected]>
Cc Matthew Wilcox <[email protected]>

---

At first, I thought of "why not simply relax PR_SET_THP_DISABLE", but I think there might be real use cases where we want to disable any THPs -- in particular also around debugging THP-related problems, and "THP=never" not meaning ... "never" anymore. PR_SET_THP_DISABLE will also block MADV_COLLAPSE, which can be very helpful. Of course, I thought of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

"prctl: allow overriding system THP policy to always"[1] proposed "overriding policies to always", which is just the wrong way around: we should not add mechanisms to "enable more" when we already have an interface/mechanism to "disable" them (PR_SET_THP_DISABLE). It all gets weird otherwise.

"[PATCH 0/6] prctl: introduce PR_SET/GET_THP_POLICY"[2] proposed setting the default of the VM_HUGEPAGE, which is similarly the wrong way around I think now.

The proposals by Lorenzo to extend process_madvise()[3] and mctrl()[4] similarly were around the "default for VM_HUGEPAGE" idea, but after the discussion, I think we should better leave VM_HUGEPAGE untouched.

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

I think this here (modifying an existing toggle) is the only prctl() extension that we might be willing to accept. In general, I agree like most others, that prctl() is a very bad interface for that -- but PR_SET_THP_DISABLE is already there and is getting used.

Long-term, I think the answer will be something based on bpf[5]. Maybe in that context, I there could still be value in easily disabling THPs for selected workloads (esp. debugging purposes).

Jann raised valid concerns[6] about new flags that are persistent across exec[6]. As this here is a relaxation to existing PR_SET_THP_DISABLE I consider it having a similar security risk as our existing PR_SET_THP_DISABLE, but devil is in the detail.

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

---
 Documentation/filesystems/proc.rst |  5 +--
 fs/proc/array.c                    |  2 +-
 include/linux/huge_mm.h            | 20 ++++++++---
 include/linux/mm_types.h           | 13 +++----
 include/uapi/linux/prctl.h         |  7 ++++
 kernel/sys.c                       | 58 +++++++++++++++++++++++-------
 mm/khugepaged.c                    |  2 +-
 7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/filesystems/proc.rst +3 -2
index 2971551b72353..915a3e44bc120 100644
@@ -291,8 +291,9 @@ It's slow but very precise.
  HugetlbPages                size of hugetlb memory portions
  CoreDumping                 process's memory is currently being dumped
                              (killing the process may lead to a corrupted core)
- THP_enabled		     process is allowed to use THP (returns 0 when
-			     PR_SET_THP_DISABLE is set on the process
+ THP_enabled                 process is allowed to use THP (returns 0 when
+                             PR_SET_THP_DISABLE is set on the process to disable
+                             THP completely, not just partially)
  Threads                     number of threads
  SigQ                        number of signals queued/max. number for queue
  SigPnd                      bitmap of pending signals for the thread
fs/proc/array.c +1 -1
index d6a0369caa931..c4f91a784104f 100644
@@ -422,7 +422,7 @@ static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
 	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
 
 	if (thp_enabled)
-		thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
+		thp_enabled = !test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
 	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
 }
 
include/linux/huge_mm.h +15 -5
index e0a27f80f390d..c4127104d9bc3 100644
@@ -323,16 +323,26 @@ struct thpsize {
 	(transparent_hugepage_flags &					\
 	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
 
+/*
+ * Check whether THPs are explicitly disabled through madvise or prctl, or some
+ * architectures may disable THP for some mappings, for example, s390 kvm.
+ */
 static inline bool vma_thp_disabled(struct vm_area_struct *vma,
 		vm_flags_t vm_flags)
 {
+	/* Are THPs disabled for this VMA? */
+	if (vm_flags & VM_NOHUGEPAGE)
+		return true;
+	/* Are THPs disabled for all VMAs in the whole process? */
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, &vma->vm_mm->flags))
+		return true;
 	/*
-	 * Explicitly disabled through madvise or prctl, or some
-	 * architectures may disable THP for some mappings, for
-	 * example, s390 kvm.
+	 * Are THPs disabled only for VMAs where we didn't get an explicit
+	 * advise to use them?
 	 */
-	return (vm_flags & VM_NOHUGEPAGE) ||
-	       test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags);
+	if (vm_flags & VM_HUGEPAGE)
+		return false;
+	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
 }
 
 static inline bool thp_disabled_by_hw(void)
include/linux/mm_types.h +5 -8
index 1ec273b066915..a999f2d352648 100644
@@ -1743,19 +1743,16 @@ enum {
 #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
 #define MMF_VM_HUGEPAGE		17	/* set when mm is available for khugepaged */
 
-/*
- * This one-shot flag is dropped due to necessity of changing exe once again
- * on NFS restore
- */
-//#define MMF_EXE_FILE_CHANGED	18	/* see prctl_set_mm_exe_file() */
+#define MMF_HUGE_ZERO_PAGE	18      /* mm has ever used the global huge zero page */
 
 #define MMF_HAS_UPROBES		19	/* has uprobes */
 #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
 #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
 #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
-#define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
-#define MMF_DISABLE_THP		24	/* disable THP for all VMAs */
-#define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
+#define MMF_DISABLE_THP_EXCEPT_ADVISED	23	/* no THP except for VMAs with VM_HUGEPAGE */
+#define MMF_DISABLE_THP_COMPLETELY	24	/* no THP for all VMAs */
+#define MMF_DISABLE_THP_MASK	((1 << MMF_DISABLE_THP_COMPLETELY) |\
+				 (1 << MMF_DISABLE_THP_EXCEPT_ADVISED))
 #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
 #define MMF_MULTIPROCESS	26	/* mm is shared between processes */
 /*
include/uapi/linux/prctl.h +7 -0
index 43dec6eed559a..1949bb9270d48 100644
@@ -177,7 +177,14 @@ struct prctl_mm_map {
 
 #define PR_GET_TID_ADDRESS	40
 
+/*
+ * Flags for PR_SET_THP_DISABLE are only applicable when disabling. Bit 0
+ * is reserved, so PR_GET_THP_DISABLE can return 1 when no other flags were
+ * specified for PR_SET_THP_DISABLE.
+ */
 #define PR_SET_THP_DISABLE	41
+/* Don't disable THPs when explicitly advised (MADV_HUGEPAGE / VM_HUGEPAGE). */
+# define PR_THP_DISABLE_EXCEPT_ADVISED	(1 << 1)
 #define PR_GET_THP_DISABLE	42
 
 /*
kernel/sys.c +46 -12

72 lines not on this page · raw message

mm/khugepaged.c +1 -1
index 8a5873d0a23a7..a685077644b4e 100644
@@ -427,7 +427,7 @@ static inline int collapse_test_exit(struct mm_struct *mm)
 static inline int collapse_test_exit_or_disable(struct mm_struct *mm)
 {
 	return collapse_test_exit(mm) ||
-	       test_bit(MMF_DISABLE_THP, &mm->flags);
+	       test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
 }
 
 static bool hugepage_enabled(void)

base-commit: 760b462b3921c5dc8bfa151d2d27a944e4e96081

Signature · 1 line
2.50.1
#1 lore raw

Two additions:

Quoted 3 lines

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

As raised off-list, this should be "only if THPs are disabled completely"

Quoted 9 lines

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

Quoted 1 line

The documented semantics in the man page for PR_SET_THP_DISABLE

"is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

Quoted 5 lines

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

I don't think there is any user that would try re-enabling THPs through that interface. It's kind-of against the original purpose (man page): "Setting this flag provides a method for disabling transparent huge pages for jobs where the code cannot be modified ..."

So if ever really required, one could investigate forbidding re-enabling once disabled. But that obviously needs more investigation.

(also, if a workload ever enables THPs through that mechanism, probably it is to blame)

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
In reply to David Hildenbrand

Overall, while I HATE this interface (as y'know, everyone knows :P), since it _already_ exists and fulfils a real need (and we _have_ to keep supporting that need) I'm open to us solving the issue this way.

So this might be a way for us to achieve what Usama + others need without having to splice in horridness.

This as a proof-of-concept is obviously not for 6.17 (and late in the day anyway :P), so we have at least 6.18 cycle to discuss.

Quoted 2 lines · David Hildenbrand

People want to make use of more THPs, for example, moving from THP=never to THP=madvise, or from THP=madvise to THP=never.

Nitty, but sort of vague as to what THP= means here, I'd just say 'from never to madvise, or from madvise to never' - it's pretty clear what you mean and keeps enough 'flexibility' of interpretation to cover off the various ways you can do this in the sysfs interfaces.

Same comment for simlar below.

Quoted 6 lines

While this is great news for every THP desperately waiting to get allocated out there, apparently there are some workloads that require a bit of care during that transition: once problems are detected, these workloads should be started with the old behavior, without making all other workloads on the system go back to the old behavior as well.

I'm confused about what 'old behavior' is here. Also it's not always necessarily due to problems, there can be a desire to treat THPs as a resource to be distributed as desired.

So I'd say something like '... transition: individual processes may need to opt-out from this behaviour for various reasons, and this should be permitted without needing to make all other workloads on the system similarly opt-out'.

Quoted 5 lines

In essence, the following scenarios are imaginable:

(1) Switch from THP=none to THP=madvise or THP=always, but keep the old behavior (no THP) for selected workloads.

I'd remove 'old behavior' here as it's confusing, and simply refer to THP being disabled for selected workloads.

Quoted 7 lines

(2) Stay at THP=none, but have "madvise" or "always" behavior for selected workloads.

(3) Switch from THP=madvise to THP=always, but keep the old behavior (THP only when advised) for selected workloads.

Quoted 6 lines

(4) Stay at THP=madvise, but have "always" behavior for selected workloads.

In essence, (2) can be emulated through (1), by setting THP!=none while disabling THPs for all processes that don't want THPs. It requires configuring all workloads, but that is a user-space problem to sort out.

NIT: Delete 'In essence' here.

Quoted 14 lines

(4) can be emulated through (3) in a similar way.

Back when (1) was relevant in the past, as people started enabling THPs, we added PR_SET_THP_DISABLE, so relevant workloads that were not ready yet (i.e., used by Redis) were able to just disable THPs completely. Redis still implements the option to use this interface to disable THPs completely.

With PR_SET_THP_DISABLE, we added a way to force-disable THPs for a workload -- a process, including fork+exec'ed process hierarchy. That essentially made us support (1): simply disable THPs for all workloads that are not ready for THPs yet, while still enabling THPs system-wide.

Quoted 7 lines

The quest for handling (3) and (4) started, but current approaches (completely new prctl, options to set other policies per processm,
alternatives to prctl -- mctrl, cgroup handling) don't look particularly
promising. Likely, the future will use bpf or something similar to implement better policies, in particular to also make better decisions about THP sizes to use, but this will certainly take a while as that work just started.

Ack.

Quoted 3 lines

Long story short: a simple enable/disable is not really suitable for the future, so we're not willing to add completely new toggles.

Yes this is the crux of the problem.

Quoted 7 lines

While we could emulate (3)+(4) through (1)+(2) by simply disabling THPs completely for these processes, this scares many THPs in our system because they could no longer get allocated where they used to be allocated
for: regions flagged as VM_HUGEPAGE. Apparently, that imposes a problem for relevant workloads, because "not THPs" is certainly worse than "THPs only when advised".

I don't know what you mean by 'scares' many THPs? :P

Quoted 3 lines

Could we simply relax PR_SET_THP_DISABLE, to "disable THPs unless not explicitly advised by the app through MAD_HUGEPAGE"? *maybe*, but this

MAD_HUGEPAGE -> MADV_HUGEPAGE

I'm confused by 'unless not explicitly advised' do you mean 'disable THPs unless explicitly advised by the app through MADV_HUGEPAGE'?

Quoted 11 lines

would change the documented semantics quite a bit, and the versatility to use it for debugging purposes, so I am not 100% sure that is what we want -- although it would certainly be much easier.

So instead, as an easy way forward for (3) and (4), an option to make PR_SET_THP_DISABLE disable *less* THPs for a process.

In essence, this patch:

(A) Adds PR_THP_DISABLE_EXCEPT_ADVISED, to be used as a flag in arg3

    of prctl(PR_SET_THP_DISABLE) when disabling THPs (arg2 != 0).

prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED)?

Quoted 3 lines

For now, arg3 was not allowed to be set (-EINVAL). Now it holds flags.

This sentence is redundant.

Quoted 6 lines

(B) Makes prctl(PR_GET_THP_DISABLE) return 3 if PR_THP_DISABLE_EXCEPT_ADVISED was set while disabling.

For now, it would return 1 if THPs were disabled completely. Now it essentially returns the set flags as well.

For now as in 'previously'. I guess right now it's just used as a boolean, so this seems fine.

Quoted 6 lines

(C) Renames MMF_DISABLE_THP to MMF_DISABLE_THP_COMPLETELY, to express the semantics clearly.

Fortunately, there are only two instances outside of prctl() code.

Quoted 7 lines

(D) Adds MMF_DISABLE_THP_EXCEPT_ADVISED to express "no THP except for VMAs with VM_HUGEPAGE" -- essentially "thp=madvise" behavior

Fortunately, we only have to extend vma_thp_disabled().

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

You mean 'are disabled completely' but this has been covered already :P

Quoted 3 lines

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

So the really interesting part in the above is the small delta this change represents... which makes it a lot more compelling as a solution.

Quoted 6 lines

The documented semantics in the man page for PR_SET_THP_DISABLE "is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

Yeah, this is something I REALLY don't want us to perpetuate, as it's adding a now policy method by the 'back door'.

I had actually come to the conclusion that we absolutely should NOT implement anything like this, as discussed in the THP cabal meeting.

HOWEVER, since this mechanism ALREADY EXISTS for this case, I am much more ok with this.

We already perpetuate state for this across fork/exec.

Quoted 6 lines

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

Yeah not a fan of the seal idea, that will add a bunch of complexity and state that I would rather not have.

I'd far prefer just disallowing re-enabling THP. We could allow re-disabling with different flags though.

Be good to get some examples of the old + new prctl() invocations in the commit message too.

Quoted 23 lines
Cc Jonathan Corbet <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Usama Arif <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Jann Horn <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Yafang Shao <[email protected]>
Cc Matthew Wilcox <[email protected]>
Quoted 6 lines

---

At first, I thought of "why not simply relax PR_SET_THP_DISABLE", but I think there might be real use cases where we want to disable any THPs -- in particular also around debugging THP-related problems, and "THP=never" not meaning ... "never" anymore. PR_SET_THP_DISABLE will

Well, not quite anymore :) it's been this way for a while right? Even since MADV_COLLAPSE was introduced.

Quoted 1 line

also block MADV_COLLAPSE, which can be very helpful. Of course, I thought

Yes.

I mean I hate, hate, HATE this interface. But it exists and we have to support it anyway.

Quoted 2 lines

of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

What do you mean?

Quoted 6 lines

"prctl: allow overriding system THP policy to always"[1] proposed "overriding policies to always", which is just the wrong way around: we should not add mechanisms to "enable more" when we already have an interface/mechanism to "disable" them (PR_SET_THP_DISABLE). It all gets weird otherwise.

Yes. A 'disable but' is more logical.

Quoted 4 lines

"[PATCH 0/6] prctl: introduce PR_SET/GET_THP_POLICY"[2] proposed setting the default of the VM_HUGEPAGE, which is similarly the wrong way around I think now.

Yes.

Quoted 4 lines

The proposals by Lorenzo to extend process_madvise()[3] and mctrl()[4] similarly were around the "default for VM_HUGEPAGE" idea, but after the discussion, I think we should better leave VM_HUGEPAGE untouched.

Well, to be clear, these were more 'if we HAVE to do this, what is the least awful way of doing this?' rather than proposals per se :P and mctrl() was really taking existing discussed ideas and -simply seeing how it looked in code- though in the end we decided better to spell out in words how it would look.

At least now I'm not in favour of us implementing policy this way (but again, am open to us extending an _existing_ abomination :)

Quoted 4 lines

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

Seems OK to me. I guess the one point of confusion could be people being confused between the THP toggle 'madvise' vs. actually having MADV_HUGEPAGE set, but that's moot, because 'madvise' mode only enables THP if the region has had MADV_HUGEPAGE set.

Quoted 4 lines

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

Yes my exact thoughts. But I will be adding a series to change this for VMA flags soon enough, and can potentially do mm flags at the same time...

So this shouldn't in the end be as much of a problem.

Maybe it's worth holding off on this until I've done that? But at any rate I intend to do those changes next cycle, and this will be a next cycle thing at the earliest anyway.

Quoted 5 lines

I think this here (modifying an existing toggle) is the only prctl() extension that we might be willing to accept. In general, I agree like most others, that prctl() is a very bad interface for that -- but PR_SET_THP_DISABLE is already there and is getting used.

Yes.

Quoted 9 lines

Long-term, I think the answer will be something based on bpf[5]. Maybe in that context, I there could still be value in easily disabling THPs for selected workloads (esp. debugging purposes).

Jann raised valid concerns[6] about new flags that are persistent across exec[6]. As this here is a relaxation to existing PR_SET_THP_DISABLE I consider it having a similar security risk as our existing PR_SET_THP_DISABLE, but devil is in the detail.

Yes...

Quoted 4 lines

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

Well :) I mean we should definitely try this out in anger and it _MUST_ have self tests and put under some pressure.

Usama, can you attack this and see?

Quoted 23 lines

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

---
 Documentation/filesystems/proc.rst |  5 +--
 fs/proc/array.c                    |  2 +-
 include/linux/huge_mm.h            | 20 ++++++++---
 include/linux/mm_types.h           | 13 +++----
 include/uapi/linux/prctl.h         |  7 ++++
 kernel/sys.c                       | 58 +++++++++++++++++++++++-------
 mm/khugepaged.c                    |  2 +-
 7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/filesystems/proc.rst +0 -0
index 2971551b72353..915a3e44bc120 100644

Documentation/filesystems/proc.rst It's slow but very precise. · line 296

Quoted 8 lines
Documentation/filesystems/proc.rst +3 -2
@@ -291,8 +291,9 @@ It's slow but very precise.
  HugetlbPages                size of hugetlb memory portions
  CoreDumping                 process's memory is currently being dumped
                              (killing the process may lead to a corrupted core)
- THP_enabled		     process is allowed to use THP (returns 0 when
-			     PR_SET_THP_DISABLE is set on the process
+ THP_enabled                 process is allowed to use THP (returns 0 when
+                             PR_SET_THP_DISABLE is set on the process to disable
+                             THP completely, not just partially)

Hmm but this means we have no way of knowing if it's set for partial

Quoted 21 lines
Documentation/filesystems/proc.rst +0 -0
  Threads                     number of threads
  SigQ                        number of signals queued/max. number for queue
  SigPnd                      bitmap of pending signals for the thread
fs/proc/array.c +1 -1
index d6a0369caa931..c4f91a784104f 100644
@@ -422,7 +422,7 @@ static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
 	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
 
 	if (thp_enabled)
-		thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
+		thp_enabled = !test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
 	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
 }
 
include/linux/huge_mm.h +0 -0
index e0a27f80f390d..c4127104d9bc3 100644

include/linux/huge_mm.h struct thpsize { · line 331

Quoted 9 lines
include/linux/huge_mm.h +4 -0
@@ -323,16 +323,26 @@ struct thpsize {
 	(transparent_hugepage_flags &					\
 	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
 
+/*
+ * Check whether THPs are explicitly disabled through madvise or prctl, or some
+ * architectures may disable THP for some mappings, for example, s390 kvm.
+ */
 static inline bool vma_thp_disabled(struct vm_area_struct *vma,
 		vm_flags_t vm_flags)

This _should_ work as we set/clear VM_HUGEPAGE, VM_NOHUGEPAGE in madvise() unconditionally without bothering to check available THP orders first so no chicken-and-egg here.

include/linux/huge_mm.h struct thpsize { · line 341

Quoted 13 lines
include/linux/huge_mm.h +8 -3
 {
+	/* Are THPs disabled for this VMA? */
+	if (vm_flags & VM_NOHUGEPAGE)
+		return true;
+	/* Are THPs disabled for all VMAs in the whole process? */
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, &vma->vm_mm->flags))
+		return true;
 	/*
-	 * Explicitly disabled through madvise or prctl, or some
-	 * architectures may disable THP for some mappings, for
-	 * example, s390 kvm.
+	 * Are THPs disabled only for VMAs where we didn't get an explicit
+	 * advise to use them?

Probably fine to drop the rather pernickety reference to s390 kvm here, I mean we don't need to spell out massively specific details in a general handler.

Quoted 25 lines
include/linux/huge_mm.h +3 -2
 	 */
-	return (vm_flags & VM_NOHUGEPAGE) ||
-	       test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags);
+	if (vm_flags & VM_HUGEPAGE)
+		return false;
+	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
 }
 
 static inline bool thp_disabled_by_hw(void)
include/linux/mm_types.h +1 -5
index 1ec273b066915..a999f2d352648 100644
@@ -1743,19 +1743,16 @@ enum {
 #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
 #define MMF_VM_HUGEPAGE		17	/* set when mm is available for khugepaged */
 
-/*
- * This one-shot flag is dropped due to necessity of changing exe once again
- * on NFS restore
- */
-//#define MMF_EXE_FILE_CHANGED	18	/* see prctl_set_mm_exe_file() */
+#define MMF_HUGE_ZERO_PAGE	18      /* mm has ever used the global huge zero page */
 
 #define MMF_HAS_UPROBES		19	/* has uprobes */

include/linux/mm_types.h enum { · line 1755

Quoted 10 lines
include/linux/mm_types.h +4 -3
 #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
 #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
 #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
-#define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
-#define MMF_DISABLE_THP		24	/* disable THP for all VMAs */
-#define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
+#define MMF_DISABLE_THP_EXCEPT_ADVISED	23	/* no THP except for VMAs with VM_HUGEPAGE */
+#define MMF_DISABLE_THP_COMPLETELY	24	/* no THP for all VMAs */
+#define MMF_DISABLE_THP_MASK	((1 << MMF_DISABLE_THP_COMPLETELY) |\
+				 (1 << MMF_DISABLE_THP_EXCEPT_ADVISED))

It feels a bit sigh to have to use up low-supply mm flags for this. But again, I should be attacking this shortage soon enough.

Are we not going ahead with Barry's series that was going to use one of these in the end?

Quoted 8 lines
include/linux/mm_types.h +0 -0
 #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
 #define MMF_MULTIPROCESS	26	/* mm is shared between processes */
 /*
include/uapi/linux/prctl.h +0 -0
index 43dec6eed559a..1949bb9270d48 100644

include/uapi/linux/prctl.h struct prctl_mm_map { · line 184

Quoted 8 lines
include/uapi/linux/prctl.h +5 -0
@@ -177,7 +177,14 @@ struct prctl_mm_map {
 
 #define PR_GET_TID_ADDRESS	40
 
+/*
+ * Flags for PR_SET_THP_DISABLE are only applicable when disabling. Bit 0
+ * is reserved, so PR_GET_THP_DISABLE can return 1 when no other flags were
+ * specified for PR_SET_THP_DISABLE.
+ */

Probably worth specifying that you're just returning the flags here.

Quoted 70 lines
include/uapi/linux/prctl.h +2 -0
 #define PR_SET_THP_DISABLE	41
+/* Don't disable THPs when explicitly advised (MADV_HUGEPAGE / VM_HUGEPAGE). */
+# define PR_THP_DISABLE_EXCEPT_ADVISED	(1 << 1)
 #define PR_GET_THP_DISABLE	42
 
 /*
kernel/sys.c +45 -3
index b153fb345ada2..2a34b2f708900 100644
@@ -2423,6 +2423,50 @@ static int prctl_get_auxv(void __user *addr, unsigned long len)
 	return sizeof(mm->saved_auxv);
 }
 
+static int prctl_get_thp_disable(unsigned long arg2, unsigned long arg3,
+				 unsigned long arg4, unsigned long arg5)
+{
+	unsigned long *mm_flags = &current->mm->flags;
+
+	if (arg2 || arg3 || arg4 || arg5)
+		return -EINVAL;
+
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, mm_flags))
+		return 1;
+	else if (test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, mm_flags))
+		return 1 | PR_THP_DISABLE_EXCEPT_ADVISED;
+	return 0;
+}
+
+static int prctl_set_thp_disable(unsigned long thp_disable, unsigned long flags,
+				 unsigned long arg4, unsigned long arg5)
+{
+	unsigned long *mm_flags = &current->mm->flags;
+
+	if (arg4 || arg5)
+		return -EINVAL;
+
+	/* Flags are only allowed when disabling. */
+	if (!thp_disable || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
+		return -EINVAL;
+	if (mmap_write_lock_killable(current->mm))
+		return -EINTR;
+	if (thp_disable) {
+		if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
+			clear_bit(MMF_DISABLE_THP_COMPLETELY, mm_flags);
+			set_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, mm_flags);
+		} else {
+			set_bit(MMF_DISABLE_THP_COMPLETELY, mm_flags);
+			clear_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, mm_flags);
+		}
+	} else {
+		clear_bit(MMF_DISABLE_THP_COMPLETELY, mm_flags);
+		clear_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, mm_flags);
+	}
+	mmap_write_unlock(current->mm);
+	return 0;
+}
+
 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 		unsigned long, arg4, unsigned long, arg5)
 {
@@ -2596,20 +2640,10 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 			return -EINVAL;
 		return task_no_new_privs(current) ? 1 : 0;
 	case PR_GET_THP_DISABLE:
-		if (arg2 || arg3 || arg4 || arg5)
-			return -EINVAL;
-		error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
+		error = prctl_get_thp_disable(arg2, arg3, arg4, arg5);
 		break;

kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2612

Quoted 10 lines
kernel/sys.c +0 -9
 	case PR_SET_THP_DISABLE:
-		if (arg3 || arg4 || arg5)
-			return -EINVAL;
-		if (mmap_write_lock_killable(me->mm))
-			return -EINTR;
-		if (arg2)
-			set_bit(MMF_DISABLE_THP, &me->mm->flags);
-		else
-			clear_bit(MMF_DISABLE_THP, &me->mm->flags);
-		mmap_write_unlock(me->mm);

I HATE that these were implemented here before.

Quoted 22 lines
kernel/sys.c +1 -0
+		error = prctl_set_thp_disable(arg2, arg3, arg4, arg5);
 		break;
 	case PR_MPX_ENABLE_MANAGEMENT:
 	case PR_MPX_DISABLE_MANAGEMENT:
mm/khugepaged.c +1 -1
index 8a5873d0a23a7..a685077644b4e 100644
@@ -427,7 +427,7 @@ static inline int collapse_test_exit(struct mm_struct *mm)
 static inline int collapse_test_exit_or_disable(struct mm_struct *mm)
 {
 	return collapse_test_exit(mm) ||
-	       test_bit(MMF_DISABLE_THP, &mm->flags);
+	       test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
 }
 
 static bool hugepage_enabled(void)

base-commit: 760b462b3921c5dc8bfa151d2d27a944e4e96081 --
2.50.1

#3 lore raw
Quoted 27 lines · Lorenzo Stoakes

Overall, while I HATE this interface (as y'know, everyone knows :P), since it _already_ exists and fulfils a real need (and we _have_ to keep supporting that need) I'm open to us solving the issue this way.

So this might be a way for us to achieve what Usama + others need without having to splice in horridness.

This as a proof-of-concept is obviously not for 6.17 (and late in the day anyway :P), so we have at least 6.18 cycle to discuss.

Quoted 2 lines · David Hildenbrand

People want to make use of more THPs, for example, moving from THP=never to THP=madvise, or from THP=madvise to THP=never.

Nitty, but sort of vague as to what THP= means here, I'd just say 'from never to madvise, or from madvise to never' - it's pretty clear what you mean and keeps enough 'flexibility' of interpretation to cover off the various ways you can do this in the sysfs interfaces.

Quoted 8 lines

Same comment for simlar below.

While this is great news for every THP desperately waiting to get allocated out there, apparently there are some workloads that require a bit of care during that transition: once problems are detected, these workloads should be started with the old behavior, without making all other workloads on the system go back to the old behavior as well.

Quoted 8 lines

I'm confused about what 'old behavior' is here. Also it's not always necessarily due to problems, there can be a desire to treat THPs as a resource to be distributed as desired.

So I'd say something like '... transition: individual processes may need to opt-out from this behaviour for various reasons, and this should be permitted without needing to make all other workloads on the system similarly opt-out'.

No strong opinion.

Quoted 9 lines
Quoted 6 lines

In essence, the following scenarios are imaginable:

(1) Switch from THP=none to THP=madvise or THP=always, but keep the old behavior (no THP) for selected workloads.

I'd remove 'old behavior' here as it's confusing, and simply refer to THP being disabled for selected workloads.

Yes.

Quoted 8 lines
Quoted 8 lines

(2) Stay at THP=none, but have "madvise" or "always" behavior for selected workloads.

(3) Switch from THP=madvise to THP=always, but keep the old behavior (THP only when advised) for selected workloads.

Quoted 8 lines
Quoted 6 lines

(4) Stay at THP=madvise, but have "always" behavior for selected workloads.

In essence, (2) can be emulated through (1), by setting THP!=none while disabling THPs for all processes that don't want THPs. It requires configuring all workloads, but that is a user-space problem to sort out.

NIT: Delete 'In essence' here.

I wanted "something" there to not make it look like the list keeps going on in a weird way ;)

Quoted 32 lines
Quoted 15 lines

(4) can be emulated through (3) in a similar way.

Back when (1) was relevant in the past, as people started enabling THPs, we added PR_SET_THP_DISABLE, so relevant workloads that were not ready yet (i.e., used by Redis) were able to just disable THPs completely. Redis still implements the option to use this interface to disable THPs completely.

With PR_SET_THP_DISABLE, we added a way to force-disable THPs for a workload -- a process, including fork+exec'ed process hierarchy. That essentially made us support (1): simply disable THPs for all workloads that are not ready for THPs yet, while still enabling THPs system-wide.

Quoted 7 lines

The quest for handling (3) and (4) started, but current approaches (completely new prctl, options to set other policies per processm,
alternatives to prctl -- mctrl, cgroup handling) don't look particularly
promising. Likely, the future will use bpf or something similar to implement better policies, in particular to also make better decisions about THP sizes to use, but this will certainly take a while as that work just started.

Ack.

Quoted 3 lines

Long story short: a simple enable/disable is not really suitable for the future, so we're not willing to add completely new toggles.

Yes this is the crux of the problem.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

While we could emulate (3)+(4) through (1)+(2) by simply disabling THPs completely for these processes, this scares many THPs in our system because they could no longer get allocated where they used to be allocated
for: regions flagged as VM_HUGEPAGE. Apparently, that imposes a problem for relevant workloads, because "not THPs" is certainly worse than "THPs only when advised".

I don't know what you mean by 'scares' many THPs? :P

They are very afraid of not getting allocated :)

Quoted 9 lines
Quoted 4 lines

Could we simply relax PR_SET_THP_DISABLE, to "disable THPs unless not explicitly advised by the app through MAD_HUGEPAGE"? *maybe*, but this

MAD_HUGEPAGE -> MADV_HUGEPAGE

I'm confused by 'unless not explicitly advised' do you mean 'disable THPs unless explicitly advised by the app through MADV_HUGEPAGE'?

Yes.

Quoted 40 lines
Quoted 5 lines

would change the documented semantics quite a bit, and the versatility to use it for debugging purposes, so I am not 100% sure that is what we want -- although it would certainly be much easier.

Quoted 7 lines

So instead, as an easy way forward for (3) and (4), an option to make PR_SET_THP_DISABLE disable *less* THPs for a process.

In essence, this patch:

(A) Adds PR_THP_DISABLE_EXCEPT_ADVISED, to be used as a flag in arg3

     of prctl(PR_SET_THP_DISABLE) when disabling THPs (arg2 != 0).

prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED)?

Quoted 3 lines

For now, arg3 was not allowed to be set (-EINVAL). Now it holds flags.

This sentence is redundant.

Quoted 6 lines

(B) Makes prctl(PR_GET_THP_DISABLE) return 3 if PR_THP_DISABLE_EXCEPT_ADVISED was set while disabling.

For now, it would return 1 if THPs were disabled completely. Now it essentially returns the set flags as well.

For now as in 'previously'. I guess right now it's just used as a boolean, so this seems fine.

Quoted 9 lines

(C) Renames MMF_DISABLE_THP to MMF_DISABLE_THP_COMPLETELY, to express the semantics clearly.

Fortunately, there are only two instances outside of prctl() code.

(D) Adds MMF_DISABLE_THP_EXCEPT_ADVISED to express "no THP except for VMAs with VM_HUGEPAGE" -- essentially "thp=madvise" behavior

Quoted 6 lines
Quoted 4 lines

Fortunately, we only have to extend vma_thp_disabled().

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

You mean 'are disabled completely' but this has been covered already :P

Yeah, see my self-reply.

Quoted 68 lines
Quoted 4 lines

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

So the really interesting part in the above is the small delta this change represents... which makes it a lot more compelling as a solution.

Quoted 6 lines

The documented semantics in the man page for PR_SET_THP_DISABLE "is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

Yeah, this is something I REALLY don't want us to perpetuate, as it's adding a now policy method by the 'back door'.

I had actually come to the conclusion that we absolutely should NOT implement anything like this, as discussed in the THP cabal meeting.

HOWEVER, since this mechanism ALREADY EXISTS for this case, I am much more ok with this.

We already perpetuate state for this across fork/exec.

Quoted 6 lines

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

Yeah not a fan of the seal idea, that will add a bunch of complexity and state that I would rather not have.

I'd far prefer just disallowing re-enabling THP. We could allow re-disabling with different flags though.

Be good to get some examples of the old + new prctl() invocations in the commit message too.

Quoted 25 lines
Cc Jonathan Corbet <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Usama Arif <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Jann Horn <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Yafang Shao <[email protected]>
Cc Matthew Wilcox <[email protected]>

---

Quoted 7 lines
Quoted 4 lines

At first, I thought of "why not simply relax PR_SET_THP_DISABLE", but I think there might be real use cases where we want to disable any THPs -- in particular also around debugging THP-related problems, and "THP=never" not meaning ... "never" anymore. PR_SET_THP_DISABLE will

Well, not quite anymore :) it's been this way for a while right? Even since MADV_COLLAPSE was introduced.

It goes back to 3.15, yes ...

Quoted 5 lines
Quoted 2 lines

also block MADV_COLLAPSE, which can be very helpful. Of course, I thought

Yes.

Quoted 7 lines

I mean I hate, hate, HATE this interface. But it exists and we have to support it anyway.

Quoted 2 lines

of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

What do you mean?

Kconfig option to change the behavior etc. In summary, I don't want to go down that path, it all gets weird.

Quoted 32 lines
Quoted 7 lines

"prctl: allow overriding system THP policy to always"[1] proposed "overriding policies to always", which is just the wrong way around: we should not add mechanisms to "enable more" when we already have an interface/mechanism to "disable" them (PR_SET_THP_DISABLE). It all gets weird otherwise.

Yes. A 'disable but' is more logical.

Quoted 4 lines

"[PATCH 0/6] prctl: introduce PR_SET/GET_THP_POLICY"[2] proposed setting the default of the VM_HUGEPAGE, which is similarly the wrong way around I think now.

Yes.

Quoted 4 lines

The proposals by Lorenzo to extend process_madvise()[3] and mctrl()[4] similarly were around the "default for VM_HUGEPAGE" idea, but after the discussion, I think we should better leave VM_HUGEPAGE untouched.

Well, to be clear, these were more 'if we HAVE to do this, what is the least awful way of doing this?' rather than proposals per se :P and mctrl() was really taking existing discussed ideas and -simply seeing how it looked in code- though in the end we decided better to spell out in words how it would look.

At least now I'm not in favour of us implementing policy this way (but again, am open to us extending an _existing_ abomination :)

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

Seems OK to me. I guess the one point of confusion could be people being confused between the THP toggle 'madvise' vs. actually having MADV_HUGEPAGE set, but that's moot, because 'madvise' mode only enables THP if the region has had MADV_HUGEPAGE set.

Right, whatever ends up setting VM_HUGEPAGE.

Quoted 6 lines
Quoted 5 lines

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

Quoted 8 lines

Yes my exact thoughts. But I will be adding a series to change this for VMA flags soon enough, and can potentially do mm flags at the same time...

So this shouldn't in the end be as much of a problem.

Maybe it's worth holding off on this until I've done that? But at any rate I intend to do those changes next cycle, and this will be a next cycle thing at the earliest anyway.

I don't think this series must be blocked by that. Using a bitmap instead of a single "unsigned long" should be fairly easy later -- I did not identify any big blockers.

Quoted 22 lines
Quoted 6 lines

I think this here (modifying an existing toggle) is the only prctl() extension that we might be willing to accept. In general, I agree like most others, that prctl() is a very bad interface for that -- but PR_SET_THP_DISABLE is already there and is getting used.

Yes.

Quoted 9 lines

Long-term, I think the answer will be something based on bpf[5]. Maybe in that context, I there could still be value in easily disabling THPs for selected workloads (esp. debugging purposes).

Jann raised valid concerns[6] about new flags that are persistent across exec[6]. As this here is a relaxation to existing PR_SET_THP_DISABLE I consider it having a similar security risk as our existing PR_SET_THP_DISABLE, but devil is in the detail.

Yes...

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

Well :) I mean we should definitely try this out in anger and it _MUST_ have self tests and put under some pressure.

Usama, can you attack this and see?

Yes, that's what I am hoping for.

Quoted 19 lines
Quoted 19 lines

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

---
  Documentation/filesystems/proc.rst |  5 +--
  fs/proc/array.c                    |  2 +-
  include/linux/huge_mm.h            | 20 ++++++++---
  include/linux/mm_types.h           | 13 +++----
  include/uapi/linux/prctl.h         |  7 ++++
  kernel/sys.c                       | 58 +++++++++++++++++++++++-------
  mm/khugepaged.c                    |  2 +-
  7 files changed, 78 insertions(+), 29 deletions(-)
Quoted 15 lines

Documentation/filesystems/proc.rst It's slow but very precise. · line 296

Quoted 13 lines
Documentation/filesystems/proc.rst +3 -2
index 2971551b72353..915a3e44bc120 100644
@@ -291,8 +291,9 @@ It's slow but very precise.
   HugetlbPages                size of hugetlb memory portions
   CoreDumping                 process's memory is currently being dumped
                               (killing the process may lead to a corrupted core)
- THP_enabled		     process is allowed to use THP (returns 0 when
-			     PR_SET_THP_DISABLE is set on the process
+ THP_enabled                 process is allowed to use THP (returns 0 when
+                             PR_SET_THP_DISABLE is set on the process to disable
+                             THP completely, not just partially)

Hmm but this means we have no way of knowing if it's set for partial

Yes. I briefly thought about indicating another member, but then I thought (a) it's ugly and (b) "who cares".

I also thought about just printing "partial" instead of "1", but not sure if that would break any parser.

Quoted 45 lines
Quoted 22 lines
Documentation/filesystems/proc.rst +0 -0
   Threads                     number of threads
   SigQ                        number of signals queued/max. number for queue
   SigPnd                      bitmap of pending signals for the thread
fs/proc/array.c +1 -1
index d6a0369caa931..c4f91a784104f 100644
@@ -422,7 +422,7 @@ static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
  	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
 
  	if (thp_enabled)
-		thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
+		thp_enabled = !test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
  	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
  }
 
include/linux/huge_mm.h +0 -0
index e0a27f80f390d..c4127104d9bc3 100644

include/linux/huge_mm.h struct thpsize { · line 331

Quoted 9 lines
include/linux/huge_mm.h +4 -0
@@ -323,16 +323,26 @@ struct thpsize {
  	(transparent_hugepage_flags &					\
  	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
 
+/*
+ * Check whether THPs are explicitly disabled through madvise or prctl, or some
+ * architectures may disable THP for some mappings, for example, s390 kvm.
+ */
  static inline bool vma_thp_disabled(struct vm_area_struct *vma,
  		vm_flags_t vm_flags)

This _should_ work as we set/clear VM_HUGEPAGE, VM_NOHUGEPAGE in madvise() unconditionally without bothering to check available THP orders first so no chicken-and-egg here.

Quoted 9 lines
include/linux/huge_mm.h +6 -1
  {
+	/* Are THPs disabled for this VMA? */
+	if (vm_flags & VM_NOHUGEPAGE)
+		return true;
+	/* Are THPs disabled for all VMAs in the whole process? */
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, &vma->vm_mm->flags))
+		return true;
  	/*
-	 * Explicitly disabled through madvise or prctl, or some
Quoted 8 lines

include/linux/huge_mm.h struct thpsize { · line 341

Quoted 4 lines
include/linux/huge_mm.h +2 -2
-	 * architectures may disable THP for some mappings, for
-	 * example, s390 kvm.
+	 * Are THPs disabled only for VMAs where we didn't get an explicit
+	 * advise to use them?

Probably fine to drop the rather pernickety reference to s390 kvm here, I mean we don't need to spell out massively specific details in a general handler.

No strong opinion.

Quoted 40 lines
Quoted 26 lines
include/linux/huge_mm.h +3 -2
  	 */
-	return (vm_flags & VM_NOHUGEPAGE) ||
-	       test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags);
+	if (vm_flags & VM_HUGEPAGE)
+		return false;
+	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
  }
 
  static inline bool thp_disabled_by_hw(void)
include/linux/mm_types.h +1 -5
index 1ec273b066915..a999f2d352648 100644
@@ -1743,19 +1743,16 @@ enum {
  #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
  #define MMF_VM_HUGEPAGE		17	/* set when mm is available for khugepaged */
 
-/*
- * This one-shot flag is dropped due to necessity of changing exe once again
- * on NFS restore
- */
-//#define MMF_EXE_FILE_CHANGED	18	/* see prctl_set_mm_exe_file() */
+#define MMF_HUGE_ZERO_PAGE	18      /* mm has ever used the global huge zero page */
 
  #define MMF_HAS_UPROBES		19	/* has uprobes */

include/linux/mm_types.h enum { · line 1755

Quoted 10 lines
include/linux/mm_types.h +4 -3
  #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
  #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
  #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
-#define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
-#define MMF_DISABLE_THP		24	/* disable THP for all VMAs */
-#define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
+#define MMF_DISABLE_THP_EXCEPT_ADVISED	23	/* no THP except for VMAs with VM_HUGEPAGE */
+#define MMF_DISABLE_THP_COMPLETELY	24	/* no THP for all VMAs */
+#define MMF_DISABLE_THP_MASK	((1 << MMF_DISABLE_THP_COMPLETELY) |\
+				 (1 << MMF_DISABLE_THP_EXCEPT_ADVISED))

It feels a bit sigh to have to use up low-supply mm flags for this. But again, I should be attacking this shortage soon enough.

Quoted 2 lines

Are we not going ahead with Barry's series that was going to use one of these in the end?

Whoever gets acked first ;)

Quoted 12 lines
Quoted 12 lines
include/linux/mm_types.h +0 -0
  #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
  #define MMF_MULTIPROCESS	26	/* mm is shared between processes */
  /*
include/uapi/linux/prctl.h +0 -0
index 43dec6eed559a..1949bb9270d48 100644
@@ -177,7 +177,14 @@ struct prctl_mm_map {
 
  #define PR_GET_TID_ADDRESS	40
 
Quoted 7 lines

include/uapi/linux/prctl.h struct prctl_mm_map { · line 184

Quoted 5 lines
include/uapi/linux/prctl.h +5 -0
+/*
+ * Flags for PR_SET_THP_DISABLE are only applicable when disabling. Bit 0
+ * is reserved, so PR_GET_THP_DISABLE can return 1 when no other flags were
+ * specified for PR_SET_THP_DISABLE.
+ */

Probably worth specifying that you're just returning the flags here.

Yes.

Thanks!

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw
Quoted 10 lines · David Hildenbrand
Quoted 10 lines
Quoted 10 lines

(2) Stay at THP=none, but have "madvise" or "always" behavior for selected workloads.

(3) Switch from THP=madvise to THP=always, but keep the old behavior (THP only when advised) for selected workloads.

(4) Stay at THP=madvise, but have "always" behavior for selected workloads.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

In essence, (2) can be emulated through (1), by setting THP!=none while disabling THPs for all processes that don't want THPs. It requires configuring all workloads, but that is a user-space problem to sort out.

NIT: Delete 'In essence' here.

I wanted "something" there to not make it look like the list keeps going on in a weird way ;)

I mean it's not a big deal :P just have memories of English teachers telling me off for repetition of such phrases...

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

While we could emulate (3)+(4) through (1)+(2) by simply disabling THPs completely for these processes, this scares many THPs in our system because they could no longer get allocated where they used to be allocated
for: regions flagged as VM_HUGEPAGE. Apparently, that imposes a problem for relevant workloads, because "not THPs" is certainly worse than "THPs only when advised".

I don't know what you mean by 'scares' many THPs? :P

They are very afraid of not getting allocated :)

Haha they sound cute!

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

What do you mean?

Kconfig option to change the behavior etc. In summary, I don't want to go down that path, it all gets weird.

Yeah please don't! :>)

Quoted 10 lines
Quoted 8 lines
Quoted 3 lines

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

Seems OK to me. I guess the one point of confusion could be people being confused between the THP toggle 'madvise' vs. actually having MADV_HUGEPAGE set, but that's moot, because 'madvise' mode only enables THP if the region has had MADV_HUGEPAGE set.

Right, whatever ends up setting VM_HUGEPAGE.

Yeah this naming is fine iMO.

Quoted 12 lines
Quoted 12 lines
Quoted 6 lines

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

Yes my exact thoughts. But I will be adding a series to change this for VMA flags soon enough, and can potentially do mm flags at the same time...

So this shouldn't in the end be as much of a problem.

Quoted 7 lines
Quoted 3 lines

Maybe it's worth holding off on this until I've done that? But at any rate I intend to do those changes next cycle, and this will be a next cycle thing at the earliest anyway.

I don't think this series must be blocked by that. Using a bitmap instead of a single "unsigned long" should be fairly easy later -- I did not identify any big blockers.

Yeah that's fine. And I don't know when I will get the bitmap changes done, so let's not block this with that!

Quoted 10 lines
Quoted 8 lines
Quoted 3 lines

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

Well :) I mean we should definitely try this out in anger and it _MUST_ have self tests and put under some pressure.

Usama, can you attack this and see?

Yes, that's what I am hoping for.

Cool. And of course Usama is best placed to experiment with this approach, as he can experiment with workloads relevant to this requirement.

Quoted 36 lines
Quoted 20 lines
Quoted 20 lines

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

---
  Documentation/filesystems/proc.rst |  5 +--
  fs/proc/array.c                    |  2 +-
  include/linux/huge_mm.h            | 20 ++++++++---
  include/linux/mm_types.h           | 13 +++----
  include/uapi/linux/prctl.h         |  7 ++++
  kernel/sys.c                       | 58 +++++++++++++++++++++++-------
  mm/khugepaged.c                    |  2 +-
  7 files changed, 78 insertions(+), 29 deletions(-)
Quoted 15 lines

Documentation/filesystems/proc.rst It's slow but very precise. · line 296

Quoted 13 lines
Documentation/filesystems/proc.rst +3 -2
index 2971551b72353..915a3e44bc120 100644
@@ -291,8 +291,9 @@ It's slow but very precise.
   HugetlbPages                size of hugetlb memory portions
   CoreDumping                 process's memory is currently being dumped
                               (killing the process may lead to a corrupted core)
- THP_enabled		     process is allowed to use THP (returns 0 when
-			     PR_SET_THP_DISABLE is set on the process
+ THP_enabled                 process is allowed to use THP (returns 0 when
+                             PR_SET_THP_DISABLE is set on the process to disable
+                             THP completely, not just partially)

Hmm but this means we have no way of knowing if it's set for partial

Quoted 5 lines

Yes. I briefly thought about indicating another member, but then I thought
(a) it's ugly and (b) "who cares".

I also thought about just printing "partial" instead of "1", but not sure if that would break any parser.

Hm and >1 could break a user who expects this to be 0, 1. We can always add a new entry if needed.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Documentation/filesystems/proc.rst +6 -1
  {
+	/* Are THPs disabled for this VMA? */
+	if (vm_flags & VM_NOHUGEPAGE)
+		return true;
+	/* Are THPs disabled for all VMAs in the whole process? */
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, &vma->vm_mm->flags))
+		return true;
  	/*
-	 * Explicitly disabled through madvise or prctl, or some
Quoted 8 lines

Documentation/filesystems/proc.rst

Quoted 4 lines
Documentation/filesystems/proc.rst +2 -2
-	 * architectures may disable THP for some mappings, for
-	 * example, s390 kvm.
+	 * Are THPs disabled only for VMAs where we didn't get an explicit
+	 * advise to use them?

Probably fine to drop the rather pernickety reference to s390 kvm here, I mean we don't need to spell out massively specific details in a general handler.

Quoted 1 line

No strong opinion.

I mean what I'm saying is this is fine :P Got no problem wtih removing this bit of the comment.

Quoted 41 lines
Quoted 41 lines
Quoted 27 lines

*/

Documentation/filesystems/proc.rst +3 -2
-	return (vm_flags & VM_NOHUGEPAGE) ||
-	       test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags);
+	if (vm_flags & VM_HUGEPAGE)
+		return false;
+	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
  }
 
  static inline bool thp_disabled_by_hw(void)
include/linux/mm_types.h +1 -5
index 1ec273b066915..a999f2d352648 100644
@@ -1743,19 +1743,16 @@ enum {
  #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
  #define MMF_VM_HUGEPAGE		17	/* set when mm is available for khugepaged */
 
-/*
- * This one-shot flag is dropped due to necessity of changing exe once again
- * on NFS restore
- */
-//#define MMF_EXE_FILE_CHANGED	18	/* see prctl_set_mm_exe_file() */
+#define MMF_HUGE_ZERO_PAGE	18      /* mm has ever used the global huge zero page */
 
  #define MMF_HAS_UPROBES		19	/* has uprobes */

include/linux/mm_types.h enum { · line 1755

Quoted 10 lines
include/linux/mm_types.h +4 -3
  #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
  #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
  #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
-#define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
-#define MMF_DISABLE_THP		24	/* disable THP for all VMAs */
-#define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
+#define MMF_DISABLE_THP_EXCEPT_ADVISED	23	/* no THP except for VMAs with VM_HUGEPAGE */
+#define MMF_DISABLE_THP_COMPLETELY	24	/* no THP for all VMAs */
+#define MMF_DISABLE_THP_MASK	((1 << MMF_DISABLE_THP_COMPLETELY) |\
+				 (1 << MMF_DISABLE_THP_EXCEPT_ADVISED))

It feels a bit sigh to have to use up low-supply mm flags for this. But again, I should be attacking this shortage soon enough.

Quoted 4 lines
Quoted 2 lines

Are we not going ahead with Barry's series that was going to use one of these in the end?

Whoever gets acked first ;)

;)

Quoted 21 lines
Quoted 13 lines
Quoted 13 lines
include/linux/mm_types.h +0 -0
  #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
  #define MMF_MULTIPROCESS	26	/* mm is shared between processes */
  /*
include/uapi/linux/prctl.h +0 -0
index 43dec6eed559a..1949bb9270d48 100644
@@ -177,7 +177,14 @@ struct prctl_mm_map {
 
  #define PR_GET_TID_ADDRESS	40
 
Quoted 7 lines

include/uapi/linux/prctl.h struct prctl_mm_map { · line 184

Quoted 5 lines
include/uapi/linux/prctl.h +5 -0
+/*
+ * Flags for PR_SET_THP_DISABLE are only applicable when disabling. Bit 0
+ * is reserved, so PR_GET_THP_DISABLE can return 1 when no other flags were
+ * specified for PR_SET_THP_DISABLE.
+ */

Probably worth specifying that you're just returning the flags here.

Quoted 3 lines

Yes.

Thanks!

Cheers!

Quoted 6 lines

--
Cheers,

David / dhildenb

Cheers, Lorenzo

#5 lore raw
In reply to David Hildenbrand
Quoted 72 lines · David Hildenbrand

People want to make use of more THPs, for example, moving from THP=never to THP=madvise, or from THP=madvise to THP=never.

While this is great news for every THP desperately waiting to get allocated out there, apparently there are some workloads that require a bit of care during that transition: once problems are detected, these workloads should be started with the old behavior, without making all other workloads on the system go back to the old behavior as well.

In essence, the following scenarios are imaginable:

(1) Switch from THP=none to THP=madvise or THP=always, but keep the old behavior (no THP) for selected workloads.

(2) Stay at THP=none, but have "madvise" or "always" behavior for selected workloads.

(3) Switch from THP=madvise to THP=always, but keep the old behavior (THP only when advised) for selected workloads.

(4) Stay at THP=madvise, but have "always" behavior for selected workloads.

In essence, (2) can be emulated through (1), by setting THP!=none while disabling THPs for all processes that don't want THPs. It requires configuring all workloads, but that is a user-space problem to sort out.

(4) can be emulated through (3) in a similar way.

Back when (1) was relevant in the past, as people started enabling THPs, we added PR_SET_THP_DISABLE, so relevant workloads that were not ready yet (i.e., used by Redis) were able to just disable THPs completely. Redis still implements the option to use this interface to disable THPs completely.

With PR_SET_THP_DISABLE, we added a way to force-disable THPs for a workload -- a process, including fork+exec'ed process hierarchy. That essentially made us support (1): simply disable THPs for all workloads that are not ready for THPs yet, while still enabling THPs system-wide.

The quest for handling (3) and (4) started, but current approaches (completely new prctl, options to set other policies per processm,
alternatives to prctl -- mctrl, cgroup handling) don't look particularly
promising. Likely, the future will use bpf or something similar to implement better policies, in particular to also make better decisions about THP sizes to use, but this will certainly take a while as that work just started.

Long story short: a simple enable/disable is not really suitable for the future, so we're not willing to add completely new toggles.

While we could emulate (3)+(4) through (1)+(2) by simply disabling THPs completely for these processes, this scares many THPs in our system because they could no longer get allocated where they used to be allocated
for: regions flagged as VM_HUGEPAGE. Apparently, that imposes a problem for relevant workloads, because "not THPs" is certainly worse than "THPs only when advised".

Could we simply relax PR_SET_THP_DISABLE, to "disable THPs unless not explicitly advised by the app through MAD_HUGEPAGE"? *maybe*, but this would change the documented semantics quite a bit, and the versatility to use it for debugging purposes, so I am not 100% sure that is what we want -- although it would certainly be much easier.

So instead, as an easy way forward for (3) and (4), an option to make PR_SET_THP_DISABLE disable *less* THPs for a process.

In essence, this patch:

(A) Adds PR_THP_DISABLE_EXCEPT_ADVISED, to be used as a flag in arg3 of prctl(PR_SET_THP_DISABLE) when disabling THPs (arg2 != 0).

Quoted 9 lines

For now, arg3 was not allowed to be set (-EINVAL). Now it holds flags.

(B) Makes prctl(PR_GET_THP_DISABLE) return 3 if PR_THP_DISABLE_EXCEPT_ADVISED was set while disabling.

For now, it would return 1 if THPs were disabled completely. Now it essentially returns the set flags as well.

No strong opinion, but maybe we have it return 2 (i.e. bit 1 set)?

I know that you are returning bit 1 set to indicate the flag, and I know that everyone dislikes prctl so its likely no more flags will be added :), but in the off chance there are extra flags, than it can make the return value weird?
If instead we return a value with only a single bit set, might be better?

Again, no strong opinion here.

Quoted 253 lines

(C) Renames MMF_DISABLE_THP to MMF_DISABLE_THP_COMPLETELY, to express the semantics clearly.

Fortunately, there are only two instances outside of prctl() code.

(D) Adds MMF_DISABLE_THP_EXCEPT_ADVISED to express "no THP except for VMAs with VM_HUGEPAGE" -- essentially "thp=madvise" behavior

Fortunately, we only have to extend vma_thp_disabled().

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

The documented semantics in the man page for PR_SET_THP_DISABLE "is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

Cc Jonathan Corbet <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Usama Arif <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Jann Horn <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Yafang Shao <[email protected]>
Cc Matthew Wilcox <[email protected]>

---

At first, I thought of "why not simply relax PR_SET_THP_DISABLE", but I think there might be real use cases where we want to disable any THPs -- in particular also around debugging THP-related problems, and "THP=never" not meaning ... "never" anymore. PR_SET_THP_DISABLE will also block MADV_COLLAPSE, which can be very helpful. Of course, I thought of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

"prctl: allow overriding system THP policy to always"[1] proposed "overriding policies to always", which is just the wrong way around: we should not add mechanisms to "enable more" when we already have an interface/mechanism to "disable" them (PR_SET_THP_DISABLE). It all gets weird otherwise.

"[PATCH 0/6] prctl: introduce PR_SET/GET_THP_POLICY"[2] proposed setting the default of the VM_HUGEPAGE, which is similarly the wrong way around I think now.

The proposals by Lorenzo to extend process_madvise()[3] and mctrl()[4] similarly were around the "default for VM_HUGEPAGE" idea, but after the discussion, I think we should better leave VM_HUGEPAGE untouched.

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

I think this here (modifying an existing toggle) is the only prctl() extension that we might be willing to accept. In general, I agree like most others, that prctl() is a very bad interface for that -- but PR_SET_THP_DISABLE is already there and is getting used.

Long-term, I think the answer will be something based on bpf[5]. Maybe in that context, I there could still be value in easily disabling THPs for selected workloads (esp. debugging purposes).

Jann raised valid concerns[6] about new flags that are persistent across exec[6]. As this here is a relaxation to existing PR_SET_THP_DISABLE I consider it having a similar security risk as our existing PR_SET_THP_DISABLE, but devil is in the detail.

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

---
 Documentation/filesystems/proc.rst |  5 +--
 fs/proc/array.c                    |  2 +-
 include/linux/huge_mm.h            | 20 ++++++++---
 include/linux/mm_types.h           | 13 +++----
 include/uapi/linux/prctl.h         |  7 ++++
 kernel/sys.c                       | 58 +++++++++++++++++++++++-------
 mm/khugepaged.c                    |  2 +-
 7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/filesystems/proc.rst +3 -2
index 2971551b72353..915a3e44bc120 100644
@@ -291,8 +291,9 @@ It's slow but very precise.
  HugetlbPages                size of hugetlb memory portions
  CoreDumping                 process's memory is currently being dumped
                              (killing the process may lead to a corrupted core)
- THP_enabled		     process is allowed to use THP (returns 0 when
-			     PR_SET_THP_DISABLE is set on the process
+ THP_enabled                 process is allowed to use THP (returns 0 when
+                             PR_SET_THP_DISABLE is set on the process to disable
+                             THP completely, not just partially)
  Threads                     number of threads
  SigQ                        number of signals queued/max. number for queue
  SigPnd                      bitmap of pending signals for the thread
fs/proc/array.c +1 -1
index d6a0369caa931..c4f91a784104f 100644
@@ -422,7 +422,7 @@ static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
 	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
 
 	if (thp_enabled)
-		thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
+		thp_enabled = !test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
 	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
 }
 
include/linux/huge_mm.h +15 -5
index e0a27f80f390d..c4127104d9bc3 100644
@@ -323,16 +323,26 @@ struct thpsize {
 	(transparent_hugepage_flags &					\
 	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
 
+/*
+ * Check whether THPs are explicitly disabled through madvise or prctl, or some
+ * architectures may disable THP for some mappings, for example, s390 kvm.
+ */
 static inline bool vma_thp_disabled(struct vm_area_struct *vma,
 		vm_flags_t vm_flags)
 {
+	/* Are THPs disabled for this VMA? */
+	if (vm_flags & VM_NOHUGEPAGE)
+		return true;
+	/* Are THPs disabled for all VMAs in the whole process? */
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, &vma->vm_mm->flags))
+		return true;
 	/*
-	 * Explicitly disabled through madvise or prctl, or some
-	 * architectures may disable THP for some mappings, for
-	 * example, s390 kvm.
+	 * Are THPs disabled only for VMAs where we didn't get an explicit
+	 * advise to use them?
 	 */
-	return (vm_flags & VM_NOHUGEPAGE) ||
-	       test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags);
+	if (vm_flags & VM_HUGEPAGE)
+		return false;
+	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
 }
 
 static inline bool thp_disabled_by_hw(void)
include/linux/mm_types.h +5 -8
index 1ec273b066915..a999f2d352648 100644
@@ -1743,19 +1743,16 @@ enum {
 #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
 #define MMF_VM_HUGEPAGE		17	/* set when mm is available for khugepaged */
 
-/*
- * This one-shot flag is dropped due to necessity of changing exe once again
- * on NFS restore
- */
-//#define MMF_EXE_FILE_CHANGED	18	/* see prctl_set_mm_exe_file() */
+#define MMF_HUGE_ZERO_PAGE	18      /* mm has ever used the global huge zero page */
 
 #define MMF_HAS_UPROBES		19	/* has uprobes */
 #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
 #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
 #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
-#define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
-#define MMF_DISABLE_THP		24	/* disable THP for all VMAs */
-#define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
+#define MMF_DISABLE_THP_EXCEPT_ADVISED	23	/* no THP except for VMAs with VM_HUGEPAGE */
+#define MMF_DISABLE_THP_COMPLETELY	24	/* no THP for all VMAs */
+#define MMF_DISABLE_THP_MASK	((1 << MMF_DISABLE_THP_COMPLETELY) |\
+				 (1 << MMF_DISABLE_THP_EXCEPT_ADVISED))
 #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
 #define MMF_MULTIPROCESS	26	/* mm is shared between processes */
 /*
include/uapi/linux/prctl.h +7 -0
index 43dec6eed559a..1949bb9270d48 100644
@@ -177,7 +177,14 @@ struct prctl_mm_map {
 
 #define PR_GET_TID_ADDRESS	40
 
+/*
+ * Flags for PR_SET_THP_DISABLE are only applicable when disabling. Bit 0
+ * is reserved, so PR_GET_THP_DISABLE can return 1 when no other flags were
+ * specified for PR_SET_THP_DISABLE.
+ */
 #define PR_SET_THP_DISABLE	41
+/* Don't disable THPs when explicitly advised (MADV_HUGEPAGE / VM_HUGEPAGE). */
+# define PR_THP_DISABLE_EXCEPT_ADVISED	(1 << 1)
 #define PR_GET_THP_DISABLE	42
 
 /*
kernel/sys.c +15 -0
index b153fb345ada2..2a34b2f708900 100644
@@ -2423,6 +2423,50 @@ static int prctl_get_auxv(void __user *addr, unsigned long len)
 	return sizeof(mm->saved_auxv);
 }
 
+static int prctl_get_thp_disable(unsigned long arg2, unsigned long arg3,
+				 unsigned long arg4, unsigned long arg5)
+{
+	unsigned long *mm_flags = &current->mm->flags;
+
+	if (arg2 || arg3 || arg4 || arg5)
+		return -EINVAL;
+
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, mm_flags))
+		return 1;
+	else if (test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, mm_flags))
+		return 1 | PR_THP_DISABLE_EXCEPT_ADVISED;
+	return 0;
+}
+

kernel/sys.c static int prctl_get_auxv(void __user *addr, unsigned long len) · line 2450

Quoted 10 lines
kernel/sys.c +10 -0
+static int prctl_set_thp_disable(unsigned long thp_disable, unsigned long flags,
+				 unsigned long arg4, unsigned long arg5)
+{
+	unsigned long *mm_flags = &current->mm->flags;
+
+	if (arg4 || arg5)
+		return -EINVAL;
+
+	/* Flags are only allowed when disabling. */
+	if (!thp_disable || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))

I think you meant over here?

	if (!thp_disable && (flags & PR_THP_DISABLE_EXCEPT_ADVISED))
Quoted 60 lines
kernel/sys.c +21 -12
+		return -EINVAL;
+	if (mmap_write_lock_killable(current->mm))
+		return -EINTR;
+	if (thp_disable) {
+		if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
+			clear_bit(MMF_DISABLE_THP_COMPLETELY, mm_flags);
+			set_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, mm_flags);
+		} else {
+			set_bit(MMF_DISABLE_THP_COMPLETELY, mm_flags);
+			clear_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, mm_flags);
+		}
+	} else {
+		clear_bit(MMF_DISABLE_THP_COMPLETELY, mm_flags);
+		clear_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, mm_flags);
+	}
+	mmap_write_unlock(current->mm);
+	return 0;
+}
+
 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 		unsigned long, arg4, unsigned long, arg5)
 {
@@ -2596,20 +2640,10 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 			return -EINVAL;
 		return task_no_new_privs(current) ? 1 : 0;
 	case PR_GET_THP_DISABLE:
-		if (arg2 || arg3 || arg4 || arg5)
-			return -EINVAL;
-		error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
+		error = prctl_get_thp_disable(arg2, arg3, arg4, arg5);
 		break;
 	case PR_SET_THP_DISABLE:
-		if (arg3 || arg4 || arg5)
-			return -EINVAL;
-		if (mmap_write_lock_killable(me->mm))
-			return -EINTR;
-		if (arg2)
-			set_bit(MMF_DISABLE_THP, &me->mm->flags);
-		else
-			clear_bit(MMF_DISABLE_THP, &me->mm->flags);
-		mmap_write_unlock(me->mm);
+		error = prctl_set_thp_disable(arg2, arg3, arg4, arg5);
 		break;
 	case PR_MPX_ENABLE_MANAGEMENT:
 	case PR_MPX_DISABLE_MANAGEMENT:
mm/khugepaged.c +1 -1
index 8a5873d0a23a7..a685077644b4e 100644
@@ -427,7 +427,7 @@ static inline int collapse_test_exit(struct mm_struct *mm)
 static inline int collapse_test_exit_or_disable(struct mm_struct *mm)
 {
 	return collapse_test_exit(mm) ||
-	       test_bit(MMF_DISABLE_THP, &mm->flags);
+	       test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
 }
 
 static bool hugepage_enabled(void)

base-commit: 760b462b3921c5dc8bfa151d2d27a944e4e96081

#6 lore raw
Quoted 8 lines
Quoted 7 lines

(B) Makes prctl(PR_GET_THP_DISABLE) return 3 if PR_THP_DISABLE_EXCEPT_ADVISED was set while disabling.

For now, it would return 1 if THPs were disabled completely. Now it essentially returns the set flags as well.

Quoted 6 lines

No strong opinion, but maybe we have it return 2 (i.e. bit 1 set)?

I know that you are returning bit 1 set to indicate the flag, and I know that everyone dislikes prctl so its likely no more flags will be added :), but in the off chance there are extra flags, than it can make the return value weird?

Well, never say never, so I decided to just return the set flags :)

Quoted 3 lines

If instead we return a value with only a single bit set, might be better?

Again, no strong opinion here.

I prefer the current approach, until there is good reason not do do it.

IOW, set bit-0 if something is disabled, and specify using flag what exectly (provided flags).

Quoted 268 lines
Quoted 254 lines

(C) Renames MMF_DISABLE_THP to MMF_DISABLE_THP_COMPLETELY, to express the semantics clearly.

Fortunately, there are only two instances outside of prctl() code.

(D) Adds MMF_DISABLE_THP_EXCEPT_ADVISED to express "no THP except for VMAs with VM_HUGEPAGE" -- essentially "thp=madvise" behavior

Fortunately, we only have to extend vma_thp_disabled().

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

The documented semantics in the man page for PR_SET_THP_DISABLE "is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

Cc Jonathan Corbet <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Usama Arif <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Jann Horn <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Yafang Shao <[email protected]>
Cc Matthew Wilcox <[email protected]>

---

At first, I thought of "why not simply relax PR_SET_THP_DISABLE", but I think there might be real use cases where we want to disable any THPs -- in particular also around debugging THP-related problems, and "THP=never" not meaning ... "never" anymore. PR_SET_THP_DISABLE will also block MADV_COLLAPSE, which can be very helpful. Of course, I thought of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

"prctl: allow overriding system THP policy to always"[1] proposed "overriding policies to always", which is just the wrong way around: we should not add mechanisms to "enable more" when we already have an interface/mechanism to "disable" them (PR_SET_THP_DISABLE). It all gets weird otherwise.

"[PATCH 0/6] prctl: introduce PR_SET/GET_THP_POLICY"[2] proposed setting the default of the VM_HUGEPAGE, which is similarly the wrong way around I think now.

The proposals by Lorenzo to extend process_madvise()[3] and mctrl()[4] similarly were around the "default for VM_HUGEPAGE" idea, but after the discussion, I think we should better leave VM_HUGEPAGE untouched.

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

I think this here (modifying an existing toggle) is the only prctl() extension that we might be willing to accept. In general, I agree like most others, that prctl() is a very bad interface for that -- but PR_SET_THP_DISABLE is already there and is getting used.

Long-term, I think the answer will be something based on bpf[5]. Maybe in that context, I there could still be value in easily disabling THPs for selected workloads (esp. debugging purposes).

Jann raised valid concerns[6] about new flags that are persistent across exec[6]. As this here is a relaxation to existing PR_SET_THP_DISABLE I consider it having a similar security risk as our existing PR_SET_THP_DISABLE, but devil is in the detail.

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

---
  Documentation/filesystems/proc.rst |  5 +--
  fs/proc/array.c                    |  2 +-
  include/linux/huge_mm.h            | 20 ++++++++---
  include/linux/mm_types.h           | 13 +++----
  include/uapi/linux/prctl.h         |  7 ++++
  kernel/sys.c                       | 58 +++++++++++++++++++++++-------
  mm/khugepaged.c                    |  2 +-
  7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/filesystems/proc.rst +3 -2
index 2971551b72353..915a3e44bc120 100644
@@ -291,8 +291,9 @@ It's slow but very precise.
   HugetlbPages                size of hugetlb memory portions
   CoreDumping                 process's memory is currently being dumped
                               (killing the process may lead to a corrupted core)
- THP_enabled		     process is allowed to use THP (returns 0 when
-			     PR_SET_THP_DISABLE is set on the process
+ THP_enabled                 process is allowed to use THP (returns 0 when
+                             PR_SET_THP_DISABLE is set on the process to disable
+                             THP completely, not just partially)
   Threads                     number of threads
   SigQ                        number of signals queued/max. number for queue
   SigPnd                      bitmap of pending signals for the thread
fs/proc/array.c +1 -1
index d6a0369caa931..c4f91a784104f 100644
@@ -422,7 +422,7 @@ static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
  	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
 
  	if (thp_enabled)
-		thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
+		thp_enabled = !test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
  	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
  }
 
include/linux/huge_mm.h +15 -5
index e0a27f80f390d..c4127104d9bc3 100644
@@ -323,16 +323,26 @@ struct thpsize {
  	(transparent_hugepage_flags &					\
  	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
 
+/*
+ * Check whether THPs are explicitly disabled through madvise or prctl, or some
+ * architectures may disable THP for some mappings, for example, s390 kvm.
+ */
  static inline bool vma_thp_disabled(struct vm_area_struct *vma,
  		vm_flags_t vm_flags)
  {
+	/* Are THPs disabled for this VMA? */
+	if (vm_flags & VM_NOHUGEPAGE)
+		return true;
+	/* Are THPs disabled for all VMAs in the whole process? */
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, &vma->vm_mm->flags))
+		return true;
  	/*
-	 * Explicitly disabled through madvise or prctl, or some
-	 * architectures may disable THP for some mappings, for
-	 * example, s390 kvm.
+	 * Are THPs disabled only for VMAs where we didn't get an explicit
+	 * advise to use them?
  	 */
-	return (vm_flags & VM_NOHUGEPAGE) ||
-	       test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags);
+	if (vm_flags & VM_HUGEPAGE)
+		return false;
+	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
  }
 
  static inline bool thp_disabled_by_hw(void)
include/linux/mm_types.h +5 -8
index 1ec273b066915..a999f2d352648 100644
@@ -1743,19 +1743,16 @@ enum {
  #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
  #define MMF_VM_HUGEPAGE		17	/* set when mm is available for khugepaged */
 
-/*
- * This one-shot flag is dropped due to necessity of changing exe once again
- * on NFS restore
- */
-//#define MMF_EXE_FILE_CHANGED	18	/* see prctl_set_mm_exe_file() */
+#define MMF_HUGE_ZERO_PAGE	18      /* mm has ever used the global huge zero page */
 
  #define MMF_HAS_UPROBES		19	/* has uprobes */
  #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
  #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
  #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
-#define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
-#define MMF_DISABLE_THP		24	/* disable THP for all VMAs */
-#define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
+#define MMF_DISABLE_THP_EXCEPT_ADVISED	23	/* no THP except for VMAs with VM_HUGEPAGE */
+#define MMF_DISABLE_THP_COMPLETELY	24	/* no THP for all VMAs */
+#define MMF_DISABLE_THP_MASK	((1 << MMF_DISABLE_THP_COMPLETELY) |\
+				 (1 << MMF_DISABLE_THP_EXCEPT_ADVISED))
  #define MMF_OOM_REAP_QUEUED	25	/* mm was queued for oom_reaper */
  #define MMF_MULTIPROCESS	26	/* mm is shared between processes */
  /*
include/uapi/linux/prctl.h +7 -0
index 43dec6eed559a..1949bb9270d48 100644
@@ -177,7 +177,14 @@ struct prctl_mm_map {
 
  #define PR_GET_TID_ADDRESS	40
 
+/*
+ * Flags for PR_SET_THP_DISABLE are only applicable when disabling. Bit 0
+ * is reserved, so PR_GET_THP_DISABLE can return 1 when no other flags were
+ * specified for PR_SET_THP_DISABLE.
+ */
  #define PR_SET_THP_DISABLE	41
+/* Don't disable THPs when explicitly advised (MADV_HUGEPAGE / VM_HUGEPAGE). */
+# define PR_THP_DISABLE_EXCEPT_ADVISED	(1 << 1)
  #define PR_GET_THP_DISABLE	42
 
  /*
kernel/sys.c +15 -0
index b153fb345ada2..2a34b2f708900 100644
@@ -2423,6 +2423,50 @@ static int prctl_get_auxv(void __user *addr, unsigned long len)
  	return sizeof(mm->saved_auxv);
  }
 
+static int prctl_get_thp_disable(unsigned long arg2, unsigned long arg3,
+				 unsigned long arg4, unsigned long arg5)
+{
+	unsigned long *mm_flags = &current->mm->flags;
+
+	if (arg2 || arg3 || arg4 || arg5)
+		return -EINVAL;
+
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, mm_flags))
+		return 1;
+	else if (test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, mm_flags))
+		return 1 | PR_THP_DISABLE_EXCEPT_ADVISED;
+	return 0;
+}
+

kernel/sys.c static int prctl_get_auxv(void __user *addr, unsigned long len) · line 2450

Quoted 10 lines
kernel/sys.c +10 -0
+static int prctl_set_thp_disable(unsigned long thp_disable, unsigned long flags,
+				 unsigned long arg4, unsigned long arg5)
+{
+	unsigned long *mm_flags = &current->mm->flags;
+
+	if (arg4 || arg5)
+		return -EINVAL;
+
+	/* Flags are only allowed when disabling. */
+	if (!thp_disable || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))

I think you meant over here?

Quoted 2 lines
	if (!thp_disable && (flags & PR_THP_DISABLE_EXCEPT_ADVISED))

When re-enabling, we don't allow flags, otherwise we only allow the supported (PR_THP_DISABLE_EXCEPT_ADVISED) flag.

So I think it should probably be something like

if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))

Signature · 3 lines
Cheers,

David / dhildenb
Acked-by Tested-by on v1 1/1
#7 lore raw
In reply to David Hildenbrand
Quoted 188 lines · David Hildenbrand

People want to make use of more THPs, for example, moving from THP=never to THP=madvise, or from THP=madvise to THP=never.

While this is great news for every THP desperately waiting to get allocated out there, apparently there are some workloads that require a bit of care during that transition: once problems are detected, these workloads should be started with the old behavior, without making all other workloads on the system go back to the old behavior as well.

In essence, the following scenarios are imaginable:

(1) Switch from THP=none to THP=madvise or THP=always, but keep the old behavior (no THP) for selected workloads.

(2) Stay at THP=none, but have "madvise" or "always" behavior for selected workloads.

(3) Switch from THP=madvise to THP=always, but keep the old behavior (THP only when advised) for selected workloads.

(4) Stay at THP=madvise, but have "always" behavior for selected workloads.

In essence, (2) can be emulated through (1), by setting THP!=none while disabling THPs for all processes that don't want THPs. It requires configuring all workloads, but that is a user-space problem to sort out.

(4) can be emulated through (3) in a similar way.

Back when (1) was relevant in the past, as people started enabling THPs, we added PR_SET_THP_DISABLE, so relevant workloads that were not ready yet (i.e., used by Redis) were able to just disable THPs completely. Redis still implements the option to use this interface to disable THPs completely.

With PR_SET_THP_DISABLE, we added a way to force-disable THPs for a workload -- a process, including fork+exec'ed process hierarchy. That essentially made us support (1): simply disable THPs for all workloads that are not ready for THPs yet, while still enabling THPs system-wide.

The quest for handling (3) and (4) started, but current approaches (completely new prctl, options to set other policies per processm,
alternatives to prctl -- mctrl, cgroup handling) don't look particularly
promising. Likely, the future will use bpf or something similar to implement better policies, in particular to also make better decisions about THP sizes to use, but this will certainly take a while as that work just started.

Long story short: a simple enable/disable is not really suitable for the future, so we're not willing to add completely new toggles.

While we could emulate (3)+(4) through (1)+(2) by simply disabling THPs completely for these processes, this scares many THPs in our system because they could no longer get allocated where they used to be allocated
for: regions flagged as VM_HUGEPAGE. Apparently, that imposes a problem for relevant workloads, because "not THPs" is certainly worse than "THPs only when advised".

Could we simply relax PR_SET_THP_DISABLE, to "disable THPs unless not explicitly advised by the app through MAD_HUGEPAGE"? *maybe*, but this would change the documented semantics quite a bit, and the versatility to use it for debugging purposes, so I am not 100% sure that is what we want -- although it would certainly be much easier.

So instead, as an easy way forward for (3) and (4), an option to make PR_SET_THP_DISABLE disable *less* THPs for a process.

In essence, this patch:

(A) Adds PR_THP_DISABLE_EXCEPT_ADVISED, to be used as a flag in arg3 of prctl(PR_SET_THP_DISABLE) when disabling THPs (arg2 != 0).

For now, arg3 was not allowed to be set (-EINVAL). Now it holds flags.

(B) Makes prctl(PR_GET_THP_DISABLE) return 3 if PR_THP_DISABLE_EXCEPT_ADVISED was set while disabling.

For now, it would return 1 if THPs were disabled completely. Now it essentially returns the set flags as well.

(C) Renames MMF_DISABLE_THP to MMF_DISABLE_THP_COMPLETELY, to express the semantics clearly.

Fortunately, there are only two instances outside of prctl() code.

(D) Adds MMF_DISABLE_THP_EXCEPT_ADVISED to express "no THP except for VMAs with VM_HUGEPAGE" -- essentially "thp=madvise" behavior

Fortunately, we only have to extend vma_thp_disabled().

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

The documented semantics in the man page for PR_SET_THP_DISABLE "is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

Cc Jonathan Corbet <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Usama Arif <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Jann Horn <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Yafang Shao <[email protected]>
Cc Matthew Wilcox <[email protected]>

---

At first, I thought of "why not simply relax PR_SET_THP_DISABLE", but I think there might be real use cases where we want to disable any THPs -- in particular also around debugging THP-related problems, and "THP=never" not meaning ... "never" anymore. PR_SET_THP_DISABLE will also block MADV_COLLAPSE, which can be very helpful. Of course, I thought of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

"prctl: allow overriding system THP policy to always"[1] proposed "overriding policies to always", which is just the wrong way around: we should not add mechanisms to "enable more" when we already have an interface/mechanism to "disable" them (PR_SET_THP_DISABLE). It all gets weird otherwise.

"[PATCH 0/6] prctl: introduce PR_SET/GET_THP_POLICY"[2] proposed setting the default of the VM_HUGEPAGE, which is similarly the wrong way around I think now.

The proposals by Lorenzo to extend process_madvise()[3] and mctrl()[4] similarly were around the "default for VM_HUGEPAGE" idea, but after the discussion, I think we should better leave VM_HUGEPAGE untouched.

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

I think this here (modifying an existing toggle) is the only prctl() extension that we might be willing to accept. In general, I agree like most others, that prctl() is a very bad interface for that -- but PR_SET_THP_DISABLE is already there and is getting used.

Long-term, I think the answer will be something based on bpf[5]. Maybe in that context, I there could still be value in easily disabling THPs for selected workloads (esp. debugging purposes).

Jann raised valid concerns[6] about new flags that are persistent across exec[6]. As this here is a relaxation to existing PR_SET_THP_DISABLE I consider it having a similar security risk as our existing PR_SET_THP_DISABLE, but devil is in the detail.

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

Quoted 9 lines
---
 Documentation/filesystems/proc.rst |  5 +--
 fs/proc/array.c                    |  2 +-
 include/linux/huge_mm.h            | 20 ++++++++---
 include/linux/mm_types.h           | 13 +++----
 include/uapi/linux/prctl.h         |  7 ++++
 kernel/sys.c                       | 58 +++++++++++++++++++++++-------
 mm/khugepaged.c                    |  2 +-
 7 files changed, 78 insertions(+), 29 deletions(-)

Thanks for the patch David!

As discussed in the other thread, with the below diff

kernel/sys.c +1 -1
index 2a34b2f70890..3912f5b6a02d 100644
@@ -2447,7 +2447,7 @@ static int prctl_set_thp_disable(unsigned long thp_disable, unsigned long flags,
                return -EINVAL;
 
        /* Flags are only allowed when disabling. */
-       if (!thp_disable || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
+       if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
                return -EINVAL;
        if (mmap_write_lock_killable(current->mm))
                return -EINTR;

I tested with the below selftest, and it works. It hopefully covers majority of the cases including fork and re-enabling THPs. Let me know if it looks ok and please feel free to add this in the next revision you send.

Once the above diff is included, please feel free to add

Thanks!

From ee9004e7d34511a79726ee1314aec0503e6351d4 Mon Sep 17 00:00:00 2001
From: Usama Arif <[email protected]>
Date: Thu, 15 May 2025 14:33:33 +0100
Subject: [PATCH] selftests: prctl: introduce tests for
PR_THP_DISABLE_EXCEPT_ADVISED

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 checks if the PMD size is 2M, if the 2M policy is set to inherit and if the system global THP policy is set to "always", so that the change in behaviour due to PR_THP_DISABLE_EXCEPT_ADVISED can be seen.

This tests if:
- the process can successfully set the policy
- carry it over to the new process with fork
- if no hugepage is gotten when the process doesn't MADV_HUGEPAGE
- if hugepage is gotten when the process does MADV_HUGEPAGE
- the process can successfully reset the policy to PR_THP_POLICY_SYSTEM
- if hugepage is gotten after the policy reset

---
 tools/testing/selftests/prctl/Makefile      |   2 +-
 tools/testing/selftests/prctl/thp_disable.c | 207 ++++++++++++++++++++
 2 files changed, 208 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/prctl/thp_disable.c
tools/testing/selftests/prctl/Makefile +1 -1
index 01dc90fbb509..a3cf76585c48 100644
@@ -5,7 +5,7 @@ ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
 
 ifeq ($(ARCH),x86)
 TEST_PROGS := disable-tsc-ctxt-sw-stress-test disable-tsc-on-off-stress-test \
-		disable-tsc-test set-anon-vma-name-test set-process-name
+		disable-tsc-test set-anon-vma-name-test set-process-name thp_disable
 all: $(TEST_PROGS)
 
 include ../lib.mk
tools/testing/selftests/prctl/thp_disable.c new file +207 -0

207 lines not on this page · raw message

Signature · 1 line
2.47.1
Acked-by
#8 lore raw

On Mon, 21 Jul 2025 18:27:38 +0100 Usama Arif <[email protected]> wrote:

[...]

Quoted 13 lines
Quoted 1 line

From ee9004e7d34511a79726ee1314aec0503e6351d4 Mon Sep 17 00:00:00 2001

From: Usama Arif <[email protected]>
Date: Thu, 15 May 2025 14:33:33 +0100
Subject: [PATCH] selftests: prctl: introduce tests for
PR_THP_DISABLE_EXCEPT_ADVISED

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 checks if the PMD size is 2M, if the 2M policy is set to inherit and if the system global THP policy is set to "always", so that the change in behaviour due to PR_THP_DISABLE_EXCEPT_ADVISED can be seen.

Quoted 7 lines

This tests if:
- the process can successfully set the policy
- carry it over to the new process with fork
- if no hugepage is gotten when the process doesn't MADV_HUGEPAGE
- if hugepage is gotten when the process does MADV_HUGEPAGE
- the process can successfully reset the policy to PR_THP_POLICY_SYSTEM
- if hugepage is gotten after the policy reset

Nice! I added a few trivial comments below, though.

Quoted 2 lines
Quoted 3 lines
---
 tools/testing/selftests/prctl/Makefile      |   2 +-
 tools/testing/selftests/prctl/thp_disable.c | 207 ++++++++++++++++++++

I once thought this might better fit on selftests/mm/, but I found we already have selftests/prctl/set-anon-vma-name-tests.c, no no strong opinion from my side.

Quoted 57 lines
 2 files changed, 208 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/prctl/thp_disable.c
tools/testing/selftests/prctl/Makefile +1 -1
index 01dc90fbb509..a3cf76585c48 100644
@@ -5,7 +5,7 @@ ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
 
 ifeq ($(ARCH),x86)
 TEST_PROGS := disable-tsc-ctxt-sw-stress-test disable-tsc-on-off-stress-test \
-		disable-tsc-test set-anon-vma-name-test set-process-name
+		disable-tsc-test set-anon-vma-name-test set-process-name thp_disable
 all: $(TEST_PROGS)
 
 include ../lib.mk
tools/testing/selftests/prctl/thp_disable.c new file +35 -0
new file mode 100644
index 000000000000..e524723b3313
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This test covers the PR_GET/SET_THP_DISABLE functionality of prctl calls
+ * for PR_THP_DISABLE_EXCEPT_ADVISED
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/wait.h>
+
+#ifndef PR_THP_DISABLE_EXCEPT_ADVISED
+#define PR_THP_DISABLE_EXCEPT_ADVISED (1 << 1)
+#endif
+
+#define CONTENT_SIZE 256
+#define BUF_SIZE (12 * 2 * 1024 * 1024) // 12 x 2MB pages
+
+enum system_policy {
+	SYSTEM_POLICY_ALWAYS,
+	SYSTEM_POLICY_MADVISE,
+	SYSTEM_POLICY_NEVER,
+};
+
+int system_thp_policy;
+
+/* check if the sysfs file contains the expected substring */
+static int check_file_content(const char *file_path, const char *expected_substring)
+{
+	FILE *file = fopen(file_path, "r");
+	char buffer[CONTENT_SIZE];
+
+	if (!file) {

tools/testing/selftests/prctl/thp_disable.c line 45

Quoted 10 lines
tools/testing/selftests/prctl/thp_disable.c new file +10 -0
+		perror("Failed to open file");
+		return -1;
+	}
+	if (fgets(buffer, CONTENT_SIZE, file) == NULL) {
+		perror("Failed to read file");
+		fclose(file);
+		return -1;
+	}
+	fclose(file);
+	// Remove newline character from the buffer

Nit. I'd suggest using "/* */" consisetntly.

Quoted 10 lines
tools/testing/selftests/prctl/thp_disable.c new file +10 -0
+	buffer[strcspn(buffer, "\n")] = '\0';
+	if (strstr(buffer, expected_substring))
+		return 0;
+	else
+		return 1;
+}
+
+/*
+ * The test is designed for 2M hugepages only.
+ * Check if hugepage size is 2M, if 2M size inherits from global

tools/testing/selftests/prctl/thp_disable.c line 65

Quoted 10 lines
tools/testing/selftests/prctl/thp_disable.c new file +10 -0
+ * setting, and if the global setting is always.
+ */
+static int sysfs_check(void)
+{
+	int res = 0;
+
+	res = check_file_content("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "2097152");
+	if (res) {
+		printf("hpage_pmd_size is not set to 2MB. Skipping test.\n");
+		return -1;

Nit. Skipping is done by the caller, right? I think it is more natural to say "Skipping test" from the caller.

tools/testing/selftests/prctl/thp_disable.c line 68

Quoted 3 lines
tools/testing/selftests/prctl/thp_disable.c new file +3 -0
+	}
+	res |= check_file_content("/sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled",
+				  "[inherit]");

Nit. I think we can drop '|' and just do 'res = '.

tools/testing/selftests/prctl/thp_disable.c line 75

Quoted 7 lines
tools/testing/selftests/prctl/thp_disable.c new file +7 -0
+	if (res) {
+		printf("hugepages-2048kB does not inherit global setting. Skipping test.\n");
+		return -1;
+	}
+
+	res = check_file_content("/sys/kernel/mm/transparent_hugepage/enabled", "[always]");
+	if (!res) {

Seems 'res' is being used for only checking whether it is zero. Maybe doing 'if (check_file_content(...))' and removing 'res' can make code simpler?

tools/testing/selftests/prctl/thp_disable.c line 78

Quoted 3 lines
tools/testing/selftests/prctl/thp_disable.c new file +3 -0
+		system_thp_policy = SYSTEM_POLICY_ALWAYS;
+		return 0;
+	}

Also, system_thp_policy is set only here, so we know 'system_thp_policy == SYSTEM_POLICY_ALWAYS' if sysfs_check() returned zero. Maybe system_thp_policy is not really required?

Quoted 29 lines
tools/testing/selftests/prctl/thp_disable.c new file +29 -0
+	printf("Global THP policy not set to always. Skipping test.\n");
+	return -1;
+}
+
+static int check_smaps_for_huge(void)
+{
+	FILE *file = fopen("/proc/self/smaps", "r");
+	int is_anonhuge = 0;
+	char line[256];
+
+	if (!file) {
+		perror("fopen");
+		return -1;
+	}
+
+	while (fgets(line, sizeof(line), file)) {
+		if (strstr(line, "AnonHugePages:") && strstr(line, "24576 kB")) {
+			is_anonhuge = 1;
+			break;
+		}
+	}
+	fclose(file);
+	return is_anonhuge;
+}
+
+static int test_mmap_thp(int madvise_buffer)
+{
+	int is_anonhuge;
+

tools/testing/selftests/prctl/thp_disable.c line 117

Quoted 10 lines
tools/testing/selftests/prctl/thp_disable.c new file +10 -0
+	char *buffer = (char *)mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE,
+				    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (buffer == MAP_FAILED) {
+		perror("mmap");
+		return -1;
+	}
+	if (madvise_buffer)
+		madvise(buffer, BUF_SIZE, MADV_HUGEPAGE);
+
+	// set memory to ensure it's allocated

'/* */' for consistency?

tools/testing/selftests/prctl/thp_disable.c line 130

Quoted 13 lines
tools/testing/selftests/prctl/thp_disable.c new file +13 -0
+	memset(buffer, 0, BUF_SIZE);
+	is_anonhuge = check_smaps_for_huge();
+	munmap(buffer, BUF_SIZE);
+	return is_anonhuge;
+}
+
+/* Global policy is always, process is changed to "madvise only" */
+static int test_global_always_process_madvise(void)
+{
+	int is_anonhuge = 0, res = 0, status = 0;
+	pid_t pid;
+
+	if (prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, NULL, NULL) != 0) {

Nit. '!= 0' can be dropped.

Quoted 39 lines
tools/testing/selftests/prctl/thp_disable.c new file +39 -0
+		perror("prctl failed to set policy to madvise");
+		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_DISABLE, NULL, NULL, NULL, NULL);
+	if (res != 3) {
+		printf("prctl PR_GET_THP_POLICY returned %d pid %d\n", res, pid);
+		goto err_out;
+	}
+
+	/* global = always, process = madvise, we shouldn't get HPs without madvise */
+	is_anonhuge = test_mmap_thp(0);
+	if (is_anonhuge) {
+		printf(
+		"PR_THP_POLICY_DEFAULT_NOHUGE set but still got hugepages without MADV_HUGEPAGE\n");
+		goto err_out;
+	}
+
+	is_anonhuge = test_mmap_thp(1);
+	if (!is_anonhuge) {
+		printf(
+		"PR_THP_POLICY_DEFAULT_NOHUGE set but did't get hugepages with MADV_HUGEPAGE\n");
+		goto err_out;
+	}
+
+	/* Reset to system policy */
+	if (prctl(PR_SET_THP_DISABLE, 0, NULL, NULL, NULL) != 0) {
+		perror("prctl failed to set policy to system");
+		goto err_out;
+	}
+
+	is_anonhuge = test_mmap_thp(0);

tools/testing/selftests/prctl/thp_disable.c line 179

Quoted 10 lines
tools/testing/selftests/prctl/thp_disable.c new file +10 -0
+	if (!is_anonhuge) {
+		printf("global policy is always but we still didn't get hugepages\n");
+		goto err_out;
+	}
+
+	is_anonhuge = test_mmap_thp(1);
+	if (!is_anonhuge) {
+		printf("global policy is always but we still didn't get hugepages\n");
+		goto err_out;
+	}

Seems is_anonhugepage is used for only whether it is zero or not, just after being assigned from test_mmap_thp(). How about removing the variable?

Quoted 13 lines
tools/testing/selftests/prctl/thp_disable.c new file +13 -0
+	printf("PASS\n");
+
+	if (pid == 0) {
+		exit(EXIT_SUCCESS);
+	} else {
+		wait(&status);
+		if (WIFEXITED(status))
+			return 0;
+		else
+			return -1;
+	}
+
+err_out:

tools/testing/selftests/prctl/thp_disable.c line 202

Quoted 10 lines
tools/testing/selftests/prctl/thp_disable.c new file +10 -0
+	if (pid == 0)
+		exit(EXIT_FAILURE);
+	else
+		return -1;
+}
+
+int main(void)
+{
+	if (sysfs_check())
+		return 0;

May better to return KSFT_SKIP insted of 0?

tools/testing/selftests/prctl/thp_disable.c line 204

Quoted 2 lines
tools/testing/selftests/prctl/thp_disable.c new file +2 -0
+
+	if (system_thp_policy == SYSTEM_POLICY_ALWAYS)

This should be always true, since sysfs_check() returned zero, right? I think we can remove this check.

tools/testing/selftests/prctl/thp_disable.c line 206

Quoted 2 lines
tools/testing/selftests/prctl/thp_disable.c new file +2 -0
+		return test_global_always_process_madvise();
+

Nit. Unnecessary blank line.

Quoted 6 lines
tools/testing/selftests/prctl/thp_disable.c new file +1 -0
+}
Signature · 1 line
2.47.1

Thanks,
SJ

#9 lore raw
In reply to Usama Arif
Quoted 227 lines · Usama Arif
Quoted 188 lines · David Hildenbrand

People want to make use of more THPs, for example, moving from THP=never to THP=madvise, or from THP=madvise to THP=never.

While this is great news for every THP desperately waiting to get allocated out there, apparently there are some workloads that require a bit of care during that transition: once problems are detected, these workloads should be started with the old behavior, without making all other workloads on the system go back to the old behavior as well.

In essence, the following scenarios are imaginable:

(1) Switch from THP=none to THP=madvise or THP=always, but keep the old behavior (no THP) for selected workloads.

(2) Stay at THP=none, but have "madvise" or "always" behavior for selected workloads.

(3) Switch from THP=madvise to THP=always, but keep the old behavior (THP only when advised) for selected workloads.

(4) Stay at THP=madvise, but have "always" behavior for selected workloads.

In essence, (2) can be emulated through (1), by setting THP!=none while disabling THPs for all processes that don't want THPs. It requires configuring all workloads, but that is a user-space problem to sort out.

(4) can be emulated through (3) in a similar way.

Back when (1) was relevant in the past, as people started enabling THPs, we added PR_SET_THP_DISABLE, so relevant workloads that were not ready yet (i.e., used by Redis) were able to just disable THPs completely. Redis still implements the option to use this interface to disable THPs completely.

With PR_SET_THP_DISABLE, we added a way to force-disable THPs for a workload -- a process, including fork+exec'ed process hierarchy. That essentially made us support (1): simply disable THPs for all workloads that are not ready for THPs yet, while still enabling THPs system-wide.

The quest for handling (3) and (4) started, but current approaches (completely new prctl, options to set other policies per processm,
alternatives to prctl -- mctrl, cgroup handling) don't look particularly
promising. Likely, the future will use bpf or something similar to implement better policies, in particular to also make better decisions about THP sizes to use, but this will certainly take a while as that work just started.

Long story short: a simple enable/disable is not really suitable for the future, so we're not willing to add completely new toggles.

While we could emulate (3)+(4) through (1)+(2) by simply disabling THPs completely for these processes, this scares many THPs in our system because they could no longer get allocated where they used to be allocated
for: regions flagged as VM_HUGEPAGE. Apparently, that imposes a problem for relevant workloads, because "not THPs" is certainly worse than "THPs only when advised".

Could we simply relax PR_SET_THP_DISABLE, to "disable THPs unless not explicitly advised by the app through MAD_HUGEPAGE"? *maybe*, but this would change the documented semantics quite a bit, and the versatility to use it for debugging purposes, so I am not 100% sure that is what we want -- although it would certainly be much easier.

So instead, as an easy way forward for (3) and (4), an option to make PR_SET_THP_DISABLE disable *less* THPs for a process.

In essence, this patch:

(A) Adds PR_THP_DISABLE_EXCEPT_ADVISED, to be used as a flag in arg3 of prctl(PR_SET_THP_DISABLE) when disabling THPs (arg2 != 0).

For now, arg3 was not allowed to be set (-EINVAL). Now it holds flags.

(B) Makes prctl(PR_GET_THP_DISABLE) return 3 if PR_THP_DISABLE_EXCEPT_ADVISED was set while disabling.

For now, it would return 1 if THPs were disabled completely. Now it essentially returns the set flags as well.

(C) Renames MMF_DISABLE_THP to MMF_DISABLE_THP_COMPLETELY, to express the semantics clearly.

Fortunately, there are only two instances outside of prctl() code.

(D) Adds MMF_DISABLE_THP_EXCEPT_ADVISED to express "no THP except for VMAs with VM_HUGEPAGE" -- essentially "thp=madvise" behavior

Fortunately, we only have to extend vma_thp_disabled().

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

The documented semantics in the man page for PR_SET_THP_DISABLE "is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

Cc Jonathan Corbet <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Usama Arif <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Jann Horn <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Yafang Shao <[email protected]>
Cc Matthew Wilcox <[email protected]>

---

At first, I thought of "why not simply relax PR_SET_THP_DISABLE", but I think there might be real use cases where we want to disable any THPs -- in particular also around debugging THP-related problems, and "THP=never" not meaning ... "never" anymore. PR_SET_THP_DISABLE will also block MADV_COLLAPSE, which can be very helpful. Of course, I thought of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

"prctl: allow overriding system THP policy to always"[1] proposed "overriding policies to always", which is just the wrong way around: we should not add mechanisms to "enable more" when we already have an interface/mechanism to "disable" them (PR_SET_THP_DISABLE). It all gets weird otherwise.

"[PATCH 0/6] prctl: introduce PR_SET/GET_THP_POLICY"[2] proposed setting the default of the VM_HUGEPAGE, which is similarly the wrong way around I think now.

The proposals by Lorenzo to extend process_madvise()[3] and mctrl()[4] similarly were around the "default for VM_HUGEPAGE" idea, but after the discussion, I think we should better leave VM_HUGEPAGE untouched.

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

I think this here (modifying an existing toggle) is the only prctl() extension that we might be willing to accept. In general, I agree like most others, that prctl() is a very bad interface for that -- but PR_SET_THP_DISABLE is already there and is getting used.

Long-term, I think the answer will be something based on bpf[5]. Maybe in that context, I there could still be value in easily disabling THPs for selected workloads (esp. debugging purposes).

Jann raised valid concerns[6] about new flags that are persistent across exec[6]. As this here is a relaxation to existing PR_SET_THP_DISABLE I consider it having a similar security risk as our existing PR_SET_THP_DISABLE, but devil is in the detail.

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

Quoted 9 lines
---
  Documentation/filesystems/proc.rst |  5 +--
  fs/proc/array.c                    |  2 +-
  include/linux/huge_mm.h            | 20 ++++++++---
  include/linux/mm_types.h           | 13 +++----
  include/uapi/linux/prctl.h         |  7 ++++
  kernel/sys.c                       | 58 +++++++++++++++++++++++-------
  mm/khugepaged.c                    |  2 +-
  7 files changed, 78 insertions(+), 29 deletions(-)

Thanks for the patch David!

As discussed in the other thread, with the below diff

kernel/sys.c +1 -1
index 2a34b2f70890..3912f5b6a02d 100644
@@ -2447,7 +2447,7 @@ static int prctl_set_thp_disable(unsigned long thp_disable, unsigned long flags,
                 return -EINVAL;
 
         /* Flags are only allowed when disabling. */
-       if (!thp_disable || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
+       if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
                 return -EINVAL;
         if (mmap_write_lock_killable(current->mm))
                 return -EINTR;

I tested with the below selftest, and it works. It hopefully covers majority of the cases including fork and re-enabling THPs. Let me know if it looks ok and please feel free to add this in the next revision you send.

Quoted 4 lines

Once the above diff is included, please feel free to add

Thanks!

The latest version lives at

https://github.com/davidhildenbrand/linux/tree/PR_SET_THP_DISABLE

With all current review feedback addressed (primarily around description+comments) + that one fix.

Quoted 25 lines

Thanks!

From ee9004e7d34511a79726ee1314aec0503e6351d4 Mon Sep 17 00:00:00 2001
From: Usama Arif <[email protected]>
Date: Thu, 15 May 2025 14:33:33 +0100
Subject: [PATCH] selftests: prctl: introduce tests for
PR_THP_DISABLE_EXCEPT_ADVISED

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 checks if the PMD size is 2M, if the 2M policy is set to inherit and if the system global THP policy is set to "always", so that the change in behaviour due to PR_THP_DISABLE_EXCEPT_ADVISED can be seen.

This tests if:
- the process can successfully set the policy
- carry it over to the new process with fork
- if no hugepage is gotten when the process doesn't MADV_HUGEPAGE
- if hugepage is gotten when the process does MADV_HUGEPAGE
- the process can successfully reset the policy to PR_THP_POLICY_SYSTEM
- if hugepage is gotten after the policy reset

Quoted 4 lines
---
  tools/testing/selftests/prctl/Makefile      |   2 +-
  tools/testing/selftests/prctl/thp_disable.c | 207 ++++++++++++++++++++

Like SJ says, this should better live under mm, then we can also make use of check_huge_anon() and vm_utils.c and probably also THP helpers from thp_settings.h. Most of the helpers you use should be available in some form there already.

With THP helpers in thp_settings.h, you can explicitly set the system policy, to then reset to eh previous version IIRC.

Further, can you make sure to use kselftest infrastructure for the test, preferrably kselftest_harness.h? (see pfnmap.c on one of my latest selftests)

I also wonder if we want to test old behavior, without the flag set. We could also test that MADV_COLLAPSE doesn't succeed in either case.

Ideally, you'd send my patch (see above) along with the selftest, as I suspect there will be more review+changes to the selftest (and only smaller changes to my patch).

Thanks!

Signature · 3 lines
Cheers,

David / dhildenb
#10 lore raw
Quoted 8 lines · David Hildenbrand
Quoted 2 lines · Usama Arif
  tools/testing/selftests/prctl/Makefile      |   2 +-
  tools/testing/selftests/prctl/thp_disable.c | 207 ++++++++++++++++++++

Like SJ says, this should better live under mm, then we can also make use of check_huge_anon() and vm_utils.c and probably also THP helpers from thp_settings.h. Most of the helpers you use should be available in some form there already.

*A wild Lorenzo appears*

I mean everyone's saying the same thing, but you'd almost be disappointed if I didn't say here 'pleeeease keep as much of this in mm as possible' also :P

Thanks for taking this on and writing the test Usama! :)

Cheers, Lorenzo

#11 lore raw
In reply to David Hildenbrand
Quoted 31 lines
Quoted 24 lines

Thanks for the patch David!

As discussed in the other thread, with the below diff

kernel/sys.c +1 -1
index 2a34b2f70890..3912f5b6a02d 100644
@@ -2447,7 +2447,7 @@ static int prctl_set_thp_disable(unsigned long thp_disable, unsigned long flags,
                  return -EINVAL;
            /* Flags are only allowed when disabling. */
-       if (!thp_disable || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
+       if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
                  return -EINVAL;
          if (mmap_write_lock_killable(current->mm))
                  return -EINTR;
 

I tested with the below selftest, and it works. It hopefully covers majority of the cases including fork and re-enabling THPs. Let me know if it looks ok and please feel free to add this in the next revision you send.

Quoted 4 lines

Once the above diff is included, please feel free to add

Thanks!

Quoted 7 lines

The latest version lives at

  https://github.com/davidhildenbrand/linux/tree/PR_SET_THP_DISABLE

With all current review feedback addressed (primarily around description+comments) + that one fix.

Hi David,

Just wanted to check if the above branch is up to date?

I didn't check the description/comments, but it still has [1]:

if (!thp_disable || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))

and not

if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))

in prctl_set_thp_disable, which is causing the reset to system policy case in my selftest to fail.

[1] https://github.com/davidhildenbrand/linux/commit/5711cdf5dfe65ca28dac2a57d62e18f1475dac57#diff-dc9985831020a20a54baf023fec641593d0d4e75a78988c3b35a176aff1c0321R2450

Thanks,
Usama

#12 lore raw
Quoted 35 lines · Usama Arif
Quoted 35 lines
Quoted 24 lines

Thanks for the patch David!

As discussed in the other thread, with the below diff

kernel/sys.c +1 -1
index 2a34b2f70890..3912f5b6a02d 100644
@@ -2447,7 +2447,7 @@ static int prctl_set_thp_disable(unsigned long thp_disable, unsigned long flags,
                  return -EINVAL;
            /* Flags are only allowed when disabling. */
-       if (!thp_disable || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
+       if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
                  return -EINVAL;
          if (mmap_write_lock_killable(current->mm))
                  return -EINTR;
 

I tested with the below selftest, and it works. It hopefully covers majority of the cases including fork and re-enabling THPs. Let me know if it looks ok and please feel free to add this in the next revision you send.

Quoted 4 lines

Once the above diff is included, please feel free to add

Thanks!

The latest version lives at

  https://github.com/davidhildenbrand/linux/tree/PR_SET_THP_DISABLE

Quoted 8 lines
Quoted 3 lines

With all current review feedback addressed (primarily around description+comments) + that one fix.

Hi David,

Just wanted to check if the above branch is up to date?

No, I forgot to push. Now pushed.

This is the diff (excluding description changes):

include/linux/huge_mm.h +2 -2
index c4127104d9bc3..527aa4c9645fd 100644
@@ -324,8 +324,8 @@ struct thpsize {
         (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
 
 /*
- * Check whether THPs are explicitly disabled through madvise or prctl, or some
- * architectures may disable THP for some mappings, for example, s390 kvm.
+ * Check whether THPs are explicitly disabled for this VMA, for example,
+ * through madvise or prctl.
  */
 static inline bool vma_thp_disabled(struct vm_area_struct *vma,
                vm_flags_t vm_flags)
include/uapi/linux/prctl.h +2 -2
index 1949bb9270d48..60e496ecabe04 100644
@@ -179,8 +179,8 @@ struct prctl_mm_map {
 
 /*
  * Flags for PR_SET_THP_DISABLE are only applicable when disabling. Bit 0
- * is reserved, so PR_GET_THP_DISABLE can return 1 when no other flags were
- * specified for PR_SET_THP_DISABLE.
+ * is reserved, so PR_GET_THP_DISABLE can return "1 | flags", to effectively
+ * return "1" when no flags were specified for PR_SET_THP_DISABLE.
  */
 #define PR_SET_THP_DISABLE     41
 /* Don't disable THPs when explicitly advised (MADV_HUGEPAGE / VM_HUGEPAGE). */
kernel/sys.c +2 -2
index 2a34b2f708900..b87d0acaab0be 100644
@@ -2438,7 +2438,7 @@ static int prctl_get_thp_disable(unsigned long arg2, unsigned long arg3,
        return 0;
 }
 
-static int prctl_set_thp_disable(unsigned long thp_disable, unsigned long flags,
+static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
                                 unsigned long arg4, unsigned long arg5)
 {
        unsigned long *mm_flags = &current->mm->flags;
@@ -2447,7 +2447,7 @@ static int prctl_set_thp_disable(unsigned long thp_disable, unsigned long flags,
                return -EINVAL;
 
        /* Flags are only allowed when disabling. */
-       if (!thp_disable || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
+       if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
                return -EINVAL;
        if (mmap_write_lock_killable(current->mm))
                return -EINTR;
Signature · 3 lines
Cheers,

David / dhildenb
#13 lore raw
In reply to David Hildenbrand
Quoted 251 lines · David Hildenbrand

People want to make use of more THPs, for example, moving from THP=never to THP=madvise, or from THP=madvise to THP=never.

While this is great news for every THP desperately waiting to get allocated out there, apparently there are some workloads that require a bit of care during that transition: once problems are detected, these workloads should be started with the old behavior, without making all other workloads on the system go back to the old behavior as well.

In essence, the following scenarios are imaginable:

(1) Switch from THP=none to THP=madvise or THP=always, but keep the old behavior (no THP) for selected workloads.

(2) Stay at THP=none, but have "madvise" or "always" behavior for selected workloads.

(3) Switch from THP=madvise to THP=always, but keep the old behavior (THP only when advised) for selected workloads.

(4) Stay at THP=madvise, but have "always" behavior for selected workloads.

In essence, (2) can be emulated through (1), by setting THP!=none while disabling THPs for all processes that don't want THPs. It requires configuring all workloads, but that is a user-space problem to sort out.

(4) can be emulated through (3) in a similar way.

Back when (1) was relevant in the past, as people started enabling THPs, we added PR_SET_THP_DISABLE, so relevant workloads that were not ready yet (i.e., used by Redis) were able to just disable THPs completely. Redis still implements the option to use this interface to disable THPs completely.

With PR_SET_THP_DISABLE, we added a way to force-disable THPs for a workload -- a process, including fork+exec'ed process hierarchy. That essentially made us support (1): simply disable THPs for all workloads that are not ready for THPs yet, while still enabling THPs system-wide.

The quest for handling (3) and (4) started, but current approaches (completely new prctl, options to set other policies per processm,
alternatives to prctl -- mctrl, cgroup handling) don't look particularly
promising. Likely, the future will use bpf or something similar to implement better policies, in particular to also make better decisions about THP sizes to use, but this will certainly take a while as that work just started.

Long story short: a simple enable/disable is not really suitable for the future, so we're not willing to add completely new toggles.

While we could emulate (3)+(4) through (1)+(2) by simply disabling THPs completely for these processes, this scares many THPs in our system because they could no longer get allocated where they used to be allocated
for: regions flagged as VM_HUGEPAGE. Apparently, that imposes a problem for relevant workloads, because "not THPs" is certainly worse than "THPs only when advised".

Could we simply relax PR_SET_THP_DISABLE, to "disable THPs unless not explicitly advised by the app through MAD_HUGEPAGE"? *maybe*, but this would change the documented semantics quite a bit, and the versatility to use it for debugging purposes, so I am not 100% sure that is what we want -- although it would certainly be much easier.

So instead, as an easy way forward for (3) and (4), an option to make PR_SET_THP_DISABLE disable *less* THPs for a process.

In essence, this patch:

(A) Adds PR_THP_DISABLE_EXCEPT_ADVISED, to be used as a flag in arg3 of prctl(PR_SET_THP_DISABLE) when disabling THPs (arg2 != 0).

For now, arg3 was not allowed to be set (-EINVAL). Now it holds flags.

(B) Makes prctl(PR_GET_THP_DISABLE) return 3 if PR_THP_DISABLE_EXCEPT_ADVISED was set while disabling.

For now, it would return 1 if THPs were disabled completely. Now it essentially returns the set flags as well.

(C) Renames MMF_DISABLE_THP to MMF_DISABLE_THP_COMPLETELY, to express the semantics clearly.

Fortunately, there are only two instances outside of prctl() code.

(D) Adds MMF_DISABLE_THP_EXCEPT_ADVISED to express "no THP except for VMAs with VM_HUGEPAGE" -- essentially "thp=madvise" behavior

Fortunately, we only have to extend vma_thp_disabled().

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

The documented semantics in the man page for PR_SET_THP_DISABLE "is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

Cc Jonathan Corbet <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Usama Arif <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Jann Horn <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Yafang Shao <[email protected]>
Cc Matthew Wilcox <[email protected]>

---

At first, I thought of "why not simply relax PR_SET_THP_DISABLE", but I think there might be real use cases where we want to disable any THPs -- in particular also around debugging THP-related problems, and "THP=never" not meaning ... "never" anymore. PR_SET_THP_DISABLE will also block MADV_COLLAPSE, which can be very helpful. Of course, I thought of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

"prctl: allow overriding system THP policy to always"[1] proposed "overriding policies to always", which is just the wrong way around: we should not add mechanisms to "enable more" when we already have an interface/mechanism to "disable" them (PR_SET_THP_DISABLE). It all gets weird otherwise.

"[PATCH 0/6] prctl: introduce PR_SET/GET_THP_POLICY"[2] proposed setting the default of the VM_HUGEPAGE, which is similarly the wrong way around I think now.

The proposals by Lorenzo to extend process_madvise()[3] and mctrl()[4] similarly were around the "default for VM_HUGEPAGE" idea, but after the discussion, I think we should better leave VM_HUGEPAGE untouched.

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

I think this here (modifying an existing toggle) is the only prctl() extension that we might be willing to accept. In general, I agree like most others, that prctl() is a very bad interface for that -- but PR_SET_THP_DISABLE is already there and is getting used.

Long-term, I think the answer will be something based on bpf[5]. Maybe in that context, I there could still be value in easily disabling THPs for selected workloads (esp. debugging purposes).

Jann raised valid concerns[6] about new flags that are persistent across exec[6]. As this here is a relaxation to existing PR_SET_THP_DISABLE I consider it having a similar security risk as our existing PR_SET_THP_DISABLE, but devil is in the detail.

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

---
 Documentation/filesystems/proc.rst |  5 +--
 fs/proc/array.c                    |  2 +-
 include/linux/huge_mm.h            | 20 ++++++++---
 include/linux/mm_types.h           | 13 +++----
 include/uapi/linux/prctl.h         |  7 ++++
 kernel/sys.c                       | 58 +++++++++++++++++++++++-------
 mm/khugepaged.c                    |  2 +-
 7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/filesystems/proc.rst +3 -2
index 2971551b72353..915a3e44bc120 100644
@@ -291,8 +291,9 @@ It's slow but very precise.
  HugetlbPages                size of hugetlb memory portions
  CoreDumping                 process's memory is currently being dumped
                              (killing the process may lead to a corrupted core)
- THP_enabled		     process is allowed to use THP (returns 0 when
-			     PR_SET_THP_DISABLE is set on the process
+ THP_enabled                 process is allowed to use THP (returns 0 when
+                             PR_SET_THP_DISABLE is set on the process to disable
+                             THP completely, not just partially)
  Threads                     number of threads
  SigQ                        number of signals queued/max. number for queue
  SigPnd                      bitmap of pending signals for the thread
fs/proc/array.c +1 -1
index d6a0369caa931..c4f91a784104f 100644
@@ -422,7 +422,7 @@ static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
 	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
 
 	if (thp_enabled)
-		thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
+		thp_enabled = !test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
 	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
 }
 
include/linux/huge_mm.h +10 -2
index e0a27f80f390d..c4127104d9bc3 100644
@@ -323,16 +323,26 @@ struct thpsize {
 	(transparent_hugepage_flags &					\
 	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
 
+/*
+ * Check whether THPs are explicitly disabled through madvise or prctl, or some
+ * architectures may disable THP for some mappings, for example, s390 kvm.
+ */
 static inline bool vma_thp_disabled(struct vm_area_struct *vma,
 		vm_flags_t vm_flags)
 {
+	/* Are THPs disabled for this VMA? */
+	if (vm_flags & VM_NOHUGEPAGE)
+		return true;
+	/* Are THPs disabled for all VMAs in the whole process? */
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, &vma->vm_mm->flags))
+		return true;
 	/*
-	 * Explicitly disabled through madvise or prctl, or some
-	 * architectures may disable THP for some mappings, for

include/linux/huge_mm.h struct thpsize { · line 346

Quoted 10 lines
include/linux/huge_mm.h +5 -3
-	 * example, s390 kvm.
+	 * Are THPs disabled only for VMAs where we didn't get an explicit
+	 * advise to use them?
 	 */
-	return (vm_flags & VM_NOHUGEPAGE) ||
-	       test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags);
+	if (vm_flags & VM_HUGEPAGE)
+		return false;
+	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
 }

Hi David,

Over here, with MMF_DISABLE_THP_EXCEPT_ADVISED, MADV_HUGEPAGE will succeed as vm_flags has VM_HUGEPAGE set, but MADV_COLLAPSE will fail to give a hugepage (as VM_HUGEPAGE is not set and MMF_DISABLE_THP_EXCEPT_ADVISED is set) which I feel might not be the right behaviour as MADV_COLLAPSE is "advise" and the prctl flag is PR_THP_DISABLE_EXCEPT_ADVISED?

This will be checked in multiple places in madvise_collapse: thp_vma_allowable_order, hugepage_vma_revalidate which calls thp_vma_allowable_order and hpage_collapse_scan_pmd which also ends up calling hugepage_vma_revalidate.

A hacky way would be to save and overwrite vma->vm_flags with VM_HUGEPAGE at the start of madvise_collapse if VM_NOHUGEPAGE is not set, and reset vma->vm_flags to its original value at the end of madvise_collapse (Not something I am recommending, just throwing it out there).

Another possibility is to pass the fact that you are in madvise_collapse to these functions as an argument, this might look ugly, although maybe not as ugly as hugepage_vma_revalidate already has collapse control arg, so just need to take care of thp_vma_allowable_orders.

Any preference or better suggestions?

Thanks!
Usama

#14 lore raw
Quoted 259 lines · Usama Arif
Quoted 256 lines · David Hildenbrand

People want to make use of more THPs, for example, moving from THP=never to THP=madvise, or from THP=madvise to THP=never.

While this is great news for every THP desperately waiting to get allocated out there, apparently there are some workloads that require a bit of care during that transition: once problems are detected, these workloads should be started with the old behavior, without making all other workloads on the system go back to the old behavior as well.

In essence, the following scenarios are imaginable:

(1) Switch from THP=none to THP=madvise or THP=always, but keep the old behavior (no THP) for selected workloads.

(2) Stay at THP=none, but have "madvise" or "always" behavior for selected workloads.

(3) Switch from THP=madvise to THP=always, but keep the old behavior (THP only when advised) for selected workloads.

(4) Stay at THP=madvise, but have "always" behavior for selected workloads.

In essence, (2) can be emulated through (1), by setting THP!=none while disabling THPs for all processes that don't want THPs. It requires configuring all workloads, but that is a user-space problem to sort out.

(4) can be emulated through (3) in a similar way.

Back when (1) was relevant in the past, as people started enabling THPs, we added PR_SET_THP_DISABLE, so relevant workloads that were not ready yet (i.e., used by Redis) were able to just disable THPs completely. Redis still implements the option to use this interface to disable THPs completely.

With PR_SET_THP_DISABLE, we added a way to force-disable THPs for a workload -- a process, including fork+exec'ed process hierarchy. That essentially made us support (1): simply disable THPs for all workloads that are not ready for THPs yet, while still enabling THPs system-wide.

The quest for handling (3) and (4) started, but current approaches (completely new prctl, options to set other policies per processm,
alternatives to prctl -- mctrl, cgroup handling) don't look particularly
promising. Likely, the future will use bpf or something similar to implement better policies, in particular to also make better decisions about THP sizes to use, but this will certainly take a while as that work just started.

Long story short: a simple enable/disable is not really suitable for the future, so we're not willing to add completely new toggles.

While we could emulate (3)+(4) through (1)+(2) by simply disabling THPs completely for these processes, this scares many THPs in our system because they could no longer get allocated where they used to be allocated
for: regions flagged as VM_HUGEPAGE. Apparently, that imposes a problem for relevant workloads, because "not THPs" is certainly worse than "THPs only when advised".

Could we simply relax PR_SET_THP_DISABLE, to "disable THPs unless not explicitly advised by the app through MAD_HUGEPAGE"? *maybe*, but this would change the documented semantics quite a bit, and the versatility to use it for debugging purposes, so I am not 100% sure that is what we want -- although it would certainly be much easier.

So instead, as an easy way forward for (3) and (4), an option to make PR_SET_THP_DISABLE disable *less* THPs for a process.

In essence, this patch:

(A) Adds PR_THP_DISABLE_EXCEPT_ADVISED, to be used as a flag in arg3 of prctl(PR_SET_THP_DISABLE) when disabling THPs (arg2 != 0).

For now, arg3 was not allowed to be set (-EINVAL). Now it holds flags.

(B) Makes prctl(PR_GET_THP_DISABLE) return 3 if PR_THP_DISABLE_EXCEPT_ADVISED was set while disabling.

For now, it would return 1 if THPs were disabled completely. Now it essentially returns the set flags as well.

(C) Renames MMF_DISABLE_THP to MMF_DISABLE_THP_COMPLETELY, to express the semantics clearly.

Fortunately, there are only two instances outside of prctl() code.

(D) Adds MMF_DISABLE_THP_EXCEPT_ADVISED to express "no THP except for VMAs with VM_HUGEPAGE" -- essentially "thp=madvise" behavior

Fortunately, we only have to extend vma_thp_disabled().

(E) Indicates "THP_enabled: 0" in /proc/pid/status only if THPs are not disabled completely

Only indicating that THPs are disabled when they are really disabled completely, not only partially.

The documented semantics in the man page for PR_SET_THP_DISABLE "is inherited by a child created via fork(2) and is preserved across execve(2)" is maintained. This behavior, for example, allows for disabling THPs for a workload through the launching process (e.g., systemd where we fork() a helper process to then exec()).

There is currently not way to prevent that a process will not issue PR_SET_THP_DISABLE itself to re-enable THP. We could add a "seal" option to PR_SET_THP_DISABLE through another flag if ever required. The known users (such as redis) really use PR_SET_THP_DISABLE to disable THPs, so that is not added for now.

Cc Jonathan Corbet <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Usama Arif <[email protected]>
Cc SeongJae Park <[email protected]>
Cc Jann Horn <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Yafang Shao <[email protected]>
Cc Matthew Wilcox <[email protected]>

---

At first, I thought of "why not simply relax PR_SET_THP_DISABLE", but I think there might be real use cases where we want to disable any THPs -- in particular also around debugging THP-related problems, and "THP=never" not meaning ... "never" anymore. PR_SET_THP_DISABLE will also block MADV_COLLAPSE, which can be very helpful. Of course, I thought of having a system-wide config to change PR_SET_THP_DISABLE behavior, but I just don't like the semantics.

"prctl: allow overriding system THP policy to always"[1] proposed "overriding policies to always", which is just the wrong way around: we should not add mechanisms to "enable more" when we already have an interface/mechanism to "disable" them (PR_SET_THP_DISABLE). It all gets weird otherwise.

"[PATCH 0/6] prctl: introduce PR_SET/GET_THP_POLICY"[2] proposed setting the default of the VM_HUGEPAGE, which is similarly the wrong way around I think now.

The proposals by Lorenzo to extend process_madvise()[3] and mctrl()[4] similarly were around the "default for VM_HUGEPAGE" idea, but after the discussion, I think we should better leave VM_HUGEPAGE untouched.

Happy to hear naming suggestions for "PR_THP_DISABLE_EXCEPT_ADVISED" where we essentially want to say "leave advised regions alone" -- "keep THP enabled for advised regions",

The only thing I really dislike about this is using another MMF_* flag, but well, no way around it -- and seems like we could easily support more than 32 if we want to, or storing this thp information elsewhere.

I think this here (modifying an existing toggle) is the only prctl() extension that we might be willing to accept. In general, I agree like most others, that prctl() is a very bad interface for that -- but PR_SET_THP_DISABLE is already there and is getting used.

Long-term, I think the answer will be something based on bpf[5]. Maybe in that context, I there could still be value in easily disabling THPs for selected workloads (esp. debugging purposes).

Jann raised valid concerns[6] about new flags that are persistent across exec[6]. As this here is a relaxation to existing PR_SET_THP_DISABLE I consider it having a similar security risk as our existing PR_SET_THP_DISABLE, but devil is in the detail.

This is *completely* untested and might be utterly broken. It merely serves as a PoC of what I think could be done. If this ever goes upstream, we need some kselftests for it, and extensive tests.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/CAG48ez3-7EnBVEjpdoW7z5K0hX41nLQN5Wb65Vg-1p8DdXRnjg@mail.gmail.com

---
  Documentation/filesystems/proc.rst |  5 +--
  fs/proc/array.c                    |  2 +-
  include/linux/huge_mm.h            | 20 ++++++++---
  include/linux/mm_types.h           | 13 +++----
  include/uapi/linux/prctl.h         |  7 ++++
  kernel/sys.c                       | 58 +++++++++++++++++++++++-------
  mm/khugepaged.c                    |  2 +-
  7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/filesystems/proc.rst +3 -2
index 2971551b72353..915a3e44bc120 100644
@@ -291,8 +291,9 @@ It's slow but very precise.
   HugetlbPages                size of hugetlb memory portions
   CoreDumping                 process's memory is currently being dumped
                               (killing the process may lead to a corrupted core)
- THP_enabled		     process is allowed to use THP (returns 0 when
-			     PR_SET_THP_DISABLE is set on the process
+ THP_enabled                 process is allowed to use THP (returns 0 when
+                             PR_SET_THP_DISABLE is set on the process to disable
+                             THP completely, not just partially)
   Threads                     number of threads
   SigQ                        number of signals queued/max. number for queue
   SigPnd                      bitmap of pending signals for the thread
fs/proc/array.c +1 -1
index d6a0369caa931..c4f91a784104f 100644
@@ -422,7 +422,7 @@ static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
  	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
 
  	if (thp_enabled)
-		thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
+		thp_enabled = !test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
  	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
  }
 
include/linux/huge_mm.h +12 -4
index e0a27f80f390d..c4127104d9bc3 100644
@@ -323,16 +323,26 @@ struct thpsize {
  	(transparent_hugepage_flags &					\
  	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
 
+/*
+ * Check whether THPs are explicitly disabled through madvise or prctl, or some
+ * architectures may disable THP for some mappings, for example, s390 kvm.
+ */
  static inline bool vma_thp_disabled(struct vm_area_struct *vma,
  		vm_flags_t vm_flags)
  {
+	/* Are THPs disabled for this VMA? */
+	if (vm_flags & VM_NOHUGEPAGE)
+		return true;
+	/* Are THPs disabled for all VMAs in the whole process? */
+	if (test_bit(MMF_DISABLE_THP_COMPLETELY, &vma->vm_mm->flags))
+		return true;
  	/*
-	 * Explicitly disabled through madvise or prctl, or some
-	 * architectures may disable THP for some mappings, for
-	 * example, s390 kvm.
+	 * Are THPs disabled only for VMAs where we didn't get an explicit
+	 * advise to use them?
  	 */
-	return (vm_flags & VM_NOHUGEPAGE) ||
Quoted 8 lines

include/linux/huge_mm.h struct thpsize { · line 346

Quoted 5 lines
include/linux/huge_mm.h +3 -1
-	       test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags);
+	if (vm_flags & VM_HUGEPAGE)
+		return false;
+	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
  }

Hi David,

Hi!

Quoted 5 lines

Over here, with MMF_DISABLE_THP_EXCEPT_ADVISED, MADV_HUGEPAGE will succeed as vm_flags has VM_HUGEPAGE set, but MADV_COLLAPSE will fail to give a hugepage (as VM_HUGEPAGE is not set and MMF_DISABLE_THP_EXCEPT_ADVISED is set) which I feel might not be the right behaviour as MADV_COLLAPSE is "advise" and the prctl flag is PR_THP_DISABLE_EXCEPT_ADVISED?

THPs are disabled for these regions, so it's at least consistent with the "disable all", but ...

Quoted 7 lines

This will be checked in multiple places in madvise_collapse: thp_vma_allowable_order, hugepage_vma_revalidate which calls thp_vma_allowable_order and hpage_collapse_scan_pmd which also ends up calling hugepage_vma_revalidate.

Quoted 1 line

A hacky way would be to save and overwrite vma->vm_flags with VM_HUGEPAGE at the start of madvise_collapse

if VM_NOHUGEPAGE is not set, and reset vma->vm_flags to its original value at the end of madvise_collapse (Not something I am recommending, just throwing it out there).

Gah.

Quoted 4 lines

Another possibility is to pass the fact that you are in madvise_collapse to these functions as an argument, this might look ugly, although maybe not as ugly as hugepage_vma_revalidate already has collapse control arg, so just need to take care of thp_vma_allowable_orders.

Likely this.

Quoted 2 lines

Any preference or better suggestions?

What you are asking for is not MMF_DISABLE_THP_EXCEPT_ADVISED as I planned it, but MMF_DISABLE_THP_EXCEPT_ADVISED_OR_MADV_COLLAPSE.

Now, one could consider MADV_COLLAPSE an "advise". (I am not opposed to that change)

Indeed, the right way might be telling vma_thp_disabled() whether we are in collapse.

Can you try implementing that on top of my patch to see how it looks?

Signature · 3 lines
Cheers,

David / dhildenb
#15 lore raw
Quoted 26 lines

Hi!

Quoted 5 lines

Over here, with MMF_DISABLE_THP_EXCEPT_ADVISED, MADV_HUGEPAGE will succeed as vm_flags has VM_HUGEPAGE set, but MADV_COLLAPSE will fail to give a hugepage (as VM_HUGEPAGE is not set and MMF_DISABLE_THP_EXCEPT_ADVISED is set) which I feel might not be the right behaviour as MADV_COLLAPSE is "advise" and the prctl flag is PR_THP_DISABLE_EXCEPT_ADVISED?

THPs are disabled for these regions, so it's at least consistent with the "disable all", but ...

Quoted 7 lines

This will be checked in multiple places in madvise_collapse: thp_vma_allowable_order, hugepage_vma_revalidate which calls thp_vma_allowable_order and hpage_collapse_scan_pmd which also ends up calling hugepage_vma_revalidate.

Quoted 1 line

A hacky way would be to save and overwrite vma->vm_flags with VM_HUGEPAGE at the start of madvise_collapse

if VM_NOHUGEPAGE is not set, and reset vma->vm_flags to its original value at the end of madvise_collapse (Not something I am recommending, just throwing it out there).

Gah.

Quoted 4 lines

Another possibility is to pass the fact that you are in madvise_collapse to these functions as an argument, this might look ugly, although maybe not as ugly as hugepage_vma_revalidate already has collapse control arg, so just need to take care of thp_vma_allowable_orders.

Quoted 9 lines

Likely this.

Quoted 2 lines

Any preference or better suggestions?

What you are asking for is not MMF_DISABLE_THP_EXCEPT_ADVISED as I planned it, but MMF_DISABLE_THP_EXCEPT_ADVISED_OR_MADV_COLLAPSE.

Now, one could consider MADV_COLLAPSE an "advise". (I am not opposed to that change)

lol yeah I always think of MADV_COLLAPSE as an extreme version of MADV_HUGE (more of a demand than an advice :)), eventhough its not persistant.
Which is why I think might be unexpected if MADV_HUGE gives hugepages but MADV_COLLAPSE doesn't (But could just be my opinion).

Quoted 4 lines

Indeed, the right way might be telling vma_thp_disabled() whether we are in collapse.

Can you try implementing that on top of my patch to see how it looks?

My reasoning is that a process that is running with system policy always but with PR_THP_DISABLE_EXCEPT_ADVISED gets THPs in exactly the same behaviour as a process that is running with system policy madvise. This will help us achieve (3) that you mentioned in the commit message:
(3) Switch from THP=madvise to THP=always, but keep the old behavior
(THP only when advised) for selected workloads.

I have written quite a few selftests now for prctl SET_THP_DISABLE, both with and without PR_THP_DISABLE_EXCEPT_ADVISED set incorporating your feedback on it. I have all of them passing with the below diff. The diff is slightly ugly, but very simple and hopefully acceptable. If it looks good, I can send a series with everything. Probably make the below diff as a separate patch on top of this patch as its mostly adding an extra arg to functions and would keep the review easier? I can squash it with this patch as well if thats better.

Thanks!

fs/proc/task_mmu.c +1 -1
index 3d6d8a9f13fc..bb5f1dedbd2c 100644
@@ -1294,7 +1294,7 @@ static int show_smap(struct seq_file *m, void *v)
 
 	seq_printf(m, "THPeligible:    %8u\n",
 		   !!thp_vma_allowable_orders(vma, vma->vm_flags,
-			   TVA_SMAPS | TVA_ENFORCE_SYSFS, THP_ORDERS_ALL));
+			   TVA_SMAPS | TVA_ENFORCE_SYSFS, THP_ORDERS_ALL, 0));
 
 	if (arch_pkeys_enabled())
 		seq_printf(m, "ProtectionKey:  %8u\n", vma_pkey(vma));
include/linux/huge_mm.h +14 -7
index 71db243a002e..82066721b161 100644
@@ -98,8 +98,8 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr;
 #define TVA_IN_PF		(1 << 1)	/* Page fault handler */
 #define TVA_ENFORCE_SYSFS	(1 << 2)	/* Obey sysfs configuration */
 
-#define thp_vma_allowable_order(vma, vm_flags, tva_flags, order) \
-	(!!thp_vma_allowable_orders(vma, vm_flags, tva_flags, BIT(order)))
+#define thp_vma_allowable_order(vma, vm_flags, tva_flags, order, in_collapse) \
+	(!!thp_vma_allowable_orders(vma, vm_flags, tva_flags, BIT(order), in_collapse))
 
 #define split_folio(f) split_folio_to_list(f, NULL)
 
@@ -265,7 +265,8 @@ static inline unsigned long thp_vma_suitable_orders(struct vm_area_struct *vma,
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 vm_flags_t vm_flags,
 					 unsigned long tva_flags,
-					 unsigned long orders);
+					 unsigned long orders,
+					 bool in_collapse);
 
 /**
  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
@@ -273,6 +274,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
  * @vm_flags: use these vm_flags instead of vma->vm_flags
  * @tva_flags: Which TVA flags to honour
  * @orders: bitfield of all orders to consider
+ * @in_collapse: whether we are being called from MADV_COLLAPSE
  *
  * Calculates the intersection of the requested hugepage orders and the allowed
  * hugepage orders for the provided vma. Permitted orders are encoded as a set
@@ -286,7 +288,8 @@ static inline
 unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
 				       vm_flags_t vm_flags,
 				       unsigned long tva_flags,
-				       unsigned long orders)
+				       unsigned long orders,
+				       bool in_collapse)
 {
 	/* Optimization to check if required orders are enabled early. */
 	if ((tva_flags & TVA_ENFORCE_SYSFS) && vma_is_anonymous(vma)) {
@@ -303,7 +306,7 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
 			return 0;
 	}
 
-	return __thp_vma_allowable_orders(vma, vm_flags, tva_flags, orders);
+	return __thp_vma_allowable_orders(vma, vm_flags, tva_flags, orders, in_collapse);
 }
 
 struct thpsize {
@@ -323,7 +326,7 @@ struct thpsize {
  * through madvise or prctl.
  */
 static inline bool vma_thp_disabled(struct vm_area_struct *vma,
-		vm_flags_t vm_flags)
+		vm_flags_t vm_flags, bool in_collapse)
 {
 	/* Are THPs disabled for this VMA? */
 	if (vm_flags & VM_NOHUGEPAGE)
@@ -331,6 +334,9 @@ static inline bool vma_thp_disabled(struct vm_area_struct *vma,
 	/* Are THPs disabled for all VMAs in the whole process? */
 	if (test_bit(MMF_DISABLE_THP_COMPLETELY, &vma->vm_mm->flags))
 		return true;
+	/* Are we being called from madvise_collapse? */
+	if (in_collapse)
+		return false;
 	/*
 	 * Are THPs disabled only for VMAs where we didn't get an explicit
 	 * advise to use them?
@@ -537,7 +543,8 @@ static inline unsigned long thp_vma_suitable_orders(struct vm_area_struct *vma,
 static inline unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
 					vm_flags_t vm_flags,
 					unsigned long tva_flags,
-					unsigned long orders)
+					unsigned long orders,
+					bool in_collapse)
 {
 	return 0;
 }
mm/huge_memory.c +3 -2
index 2b4ea5a2ce7d..ecf48a922530 100644
@@ -100,7 +100,8 @@ static inline bool file_thp_enabled(struct vm_area_struct *vma)
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 vm_flags_t vm_flags,
 					 unsigned long tva_flags,
-					 unsigned long orders)
+					 unsigned long orders,
+					 bool in_collapse)
 {
 	bool smaps = tva_flags & TVA_SMAPS;
 	bool in_pf = tva_flags & TVA_IN_PF;
@@ -122,7 +123,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 	if (!vma->vm_mm)		/* vdso */
 		return 0;
 
-	if (thp_disabled_by_hw() || vma_thp_disabled(vma, vm_flags))
+	if (thp_disabled_by_hw() || vma_thp_disabled(vma, vm_flags, in_collapse))
 		return 0;
 
 	/* khugepaged doesn't collapse DAX vma, but page fault is fine. */
mm/khugepaged.c +5 -5
index 2c9008246785..ba707ce5a00a 100644
@@ -475,7 +475,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma,
 	if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) &&
 	    hugepage_pmd_enabled()) {
 		if (thp_vma_allowable_order(vma, vm_flags, TVA_ENFORCE_SYSFS,
-					    PMD_ORDER))
+					    PMD_ORDER, 0))
 			__khugepaged_enter(vma->vm_mm);
 	}
 }
@@ -932,7 +932,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
 
 	if (!thp_vma_suitable_order(vma, address, PMD_ORDER))
 		return SCAN_ADDRESS_RANGE;
-	if (!thp_vma_allowable_order(vma, vma->vm_flags, tva_flags, PMD_ORDER))
+	if (!thp_vma_allowable_order(vma, vma->vm_flags, tva_flags, PMD_ORDER, 1))
 		return SCAN_VMA_CHECK;
 	/*
 	 * Anon VMA expected, the address may be unmapped then
@@ -1534,7 +1534,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	 * and map it by a PMD, regardless of sysfs THP settings. As such, let's
 	 * analogously elide sysfs THP settings here.
 	 */
-	if (!thp_vma_allowable_order(vma, vma->vm_flags, 0, PMD_ORDER))
+	if (!thp_vma_allowable_order(vma, vma->vm_flags, 0, PMD_ORDER, 1))
 		return SCAN_VMA_CHECK;
 
 	/* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
@@ -2432,7 +2432,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 			break;
 		}
 		if (!thp_vma_allowable_order(vma, vma->vm_flags,
-					TVA_ENFORCE_SYSFS, PMD_ORDER)) {
+					TVA_ENFORCE_SYSFS, PMD_ORDER, 0)) {
 skip:
 			progress++;
 			continue;
@@ -2766,7 +2766,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 	BUG_ON(vma->vm_start > start);
 	BUG_ON(vma->vm_end < end);
 
-	if (!thp_vma_allowable_order(vma, vma->vm_flags, 0, PMD_ORDER))
+	if (!thp_vma_allowable_order(vma, vma->vm_flags, 0, PMD_ORDER, 1))
 		return -EINVAL;
 
 	cc = kmalloc(sizeof(*cc), GFP_KERNEL);
mm/memory.c +5 -5
index 92fd18a5d8d1..da5ab2dc1797 100644
@@ -4370,7 +4370,7 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 	 * and suitable for swapping THP.
 	 */
 	orders = thp_vma_allowable_orders(vma, vma->vm_flags,
-			TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1);
+			TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1, 0);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 	orders = thp_swap_suitable_orders(swp_offset(entry),
 					  vmf->address, orders);
@@ -4918,7 +4918,7 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 	 * the faulting address and still be fully contained in the vma.
 	 */
 	orders = thp_vma_allowable_orders(vma, vma->vm_flags,
-			TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1);
+			TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1, 0);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 
 	if (!orders)
@@ -5188,7 +5188,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	 * PMD mappings, but PTE-mapped THP are fine. So let's simply refuse any
 	 * PMD mappings if THPs are disabled.
 	 */
-	if (thp_disabled_by_hw() || vma_thp_disabled(vma, vma->vm_flags))
+	if (thp_disabled_by_hw() || vma_thp_disabled(vma, vma->vm_flags, 0))
 		return ret;
 
 	if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER))
@@ -6109,7 +6109,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
 retry_pud:
 	if (pud_none(*vmf.pud) &&
 	    thp_vma_allowable_order(vma, vm_flags,
-				TVA_IN_PF | TVA_ENFORCE_SYSFS, PUD_ORDER)) {
+				TVA_IN_PF | TVA_ENFORCE_SYSFS, PUD_ORDER, 0)) {
 		ret = create_huge_pud(&vmf);
 		if (!(ret & VM_FAULT_FALLBACK))
 			return ret;
@@ -6144,7 +6144,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
 
 	if (pmd_none(*vmf.pmd) &&
 	    thp_vma_allowable_order(vma, vm_flags,
-				TVA_IN_PF | TVA_ENFORCE_SYSFS, PMD_ORDER)) {
+				TVA_IN_PF | TVA_ENFORCE_SYSFS, PMD_ORDER, 0)) {
 		ret = create_huge_pmd(&vmf);
 		if (!(ret & VM_FAULT_FALLBACK))
 			return ret;
mm/shmem.c +1 -1
index e6cdfda08aed..1960cf87b077 100644
@@ -1816,7 +1816,7 @@ unsigned long shmem_allowable_huge_orders(struct inode *inode,
 	vm_flags_t vm_flags = vma ? vma->vm_flags : 0;
 	unsigned int global_orders;
 
-	if (thp_disabled_by_hw() || (vma && vma_thp_disabled(vma, vm_flags)))
+	if (thp_disabled_by_hw() || (vma && vma_thp_disabled(vma, vm_flags, 0)))
 		return 0;
 
 	global_orders = shmem_huge_global_enabled(inode, index, write_end,
#16 lore raw
Quoted 54 lines · Usama Arif
Quoted 26 lines

Hi!

Quoted 5 lines

Over here, with MMF_DISABLE_THP_EXCEPT_ADVISED, MADV_HUGEPAGE will succeed as vm_flags has VM_HUGEPAGE set, but MADV_COLLAPSE will fail to give a hugepage (as VM_HUGEPAGE is not set and MMF_DISABLE_THP_EXCEPT_ADVISED is set) which I feel might not be the right behaviour as MADV_COLLAPSE is "advise" and the prctl flag is PR_THP_DISABLE_EXCEPT_ADVISED?

THPs are disabled for these regions, so it's at least consistent with the "disable all", but ...

Quoted 7 lines

This will be checked in multiple places in madvise_collapse: thp_vma_allowable_order, hugepage_vma_revalidate which calls thp_vma_allowable_order and hpage_collapse_scan_pmd which also ends up calling hugepage_vma_revalidate.

Quoted 1 line

A hacky way would be to save and overwrite vma->vm_flags with VM_HUGEPAGE at the start of madvise_collapse

if VM_NOHUGEPAGE is not set, and reset vma->vm_flags to its original value at the end of madvise_collapse (Not something I am recommending, just throwing it out there).

Gah.

Quoted 4 lines

Another possibility is to pass the fact that you are in madvise_collapse to these functions as an argument, this might look ugly, although maybe not as ugly as hugepage_vma_revalidate already has collapse control arg, so just need to take care of thp_vma_allowable_orders.

Quoted 9 lines

Likely this.

Quoted 2 lines

Any preference or better suggestions?

What you are asking for is not MMF_DISABLE_THP_EXCEPT_ADVISED as I planned it, but MMF_DISABLE_THP_EXCEPT_ADVISED_OR_MADV_COLLAPSE.

Now, one could consider MADV_COLLAPSE an "advise". (I am not opposed to that change)

lol yeah I always think of MADV_COLLAPSE as an extreme version of MADV_HUGE (more of a demand than an advice :)), eventhough its not persistant.
Which is why I think might be unexpected if MADV_HUGE gives hugepages but MADV_COLLAPSE doesn't (But could just be my opinion).

Quoted 4 lines

Indeed, the right way might be telling vma_thp_disabled() whether we are in collapse.

Can you try implementing that on top of my patch to see how it looks?

My reasoning is that a process that is running with system policy always but with PR_THP_DISABLE_EXCEPT_ADVISED gets THPs in exactly the same behaviour as a process that is running with system policy madvise. This will help us achieve (3) that you mentioned in the commit message:
(3) Switch from THP=madvise to THP=always, but keep the old behavior
(THP only when advised) for selected workloads.

Quoted 5 lines

I have written quite a few selftests now for prctl SET_THP_DISABLE, both with and without PR_THP_DISABLE_EXCEPT_ADVISED set incorporating your feedback on it. I have all of them passing with the below diff. The diff is slightly ugly, but very simple and hopefully acceptable. If it looks good, I can send a series with everything. Probably make the below diff as a separate patch on top of this patch as its mostly adding an extra arg to functions and would keep the review easier?

Yes, we should do it as a separate patch, makes our life easier, because that requires more work.

We require a cleanup first, the boolean parameter for __thp_vma_allowable_orders() is no good.

I just pushed something untested to my branch (slightly adjusted patch#1 + 2 more patches), can you have a look at that? (untested ... :) )

Signature · 3 lines
Cheers,

David / dhildenb
#17 lore raw
Quoted 61 lines · David Hildenbrand
Quoted 54 lines · Usama Arif
Quoted 26 lines

Hi!

Quoted 5 lines

Over here, with MMF_DISABLE_THP_EXCEPT_ADVISED, MADV_HUGEPAGE will succeed as vm_flags has VM_HUGEPAGE set, but MADV_COLLAPSE will fail to give a hugepage (as VM_HUGEPAGE is not set and MMF_DISABLE_THP_EXCEPT_ADVISED is set) which I feel might not be the right behaviour as MADV_COLLAPSE is "advise" and the prctl flag is PR_THP_DISABLE_EXCEPT_ADVISED?

THPs are disabled for these regions, so it's at least consistent with the "disable all", but ...

Quoted 7 lines

This will be checked in multiple places in madvise_collapse: thp_vma_allowable_order, hugepage_vma_revalidate which calls thp_vma_allowable_order and hpage_collapse_scan_pmd which also ends up calling hugepage_vma_revalidate.

Quoted 1 line

A hacky way would be to save and overwrite vma->vm_flags with VM_HUGEPAGE at the start of madvise_collapse

if VM_NOHUGEPAGE is not set, and reset vma->vm_flags to its original value at the end of madvise_collapse (Not something I am recommending, just throwing it out there).

Gah.

Quoted 4 lines

Another possibility is to pass the fact that you are in madvise_collapse to these functions as an argument, this might look ugly, although maybe not as ugly as hugepage_vma_revalidate already has collapse control arg, so just need to take care of thp_vma_allowable_orders.

Quoted 9 lines

Likely this.

Quoted 2 lines

Any preference or better suggestions?

What you are asking for is not MMF_DISABLE_THP_EXCEPT_ADVISED as I planned it, but MMF_DISABLE_THP_EXCEPT_ADVISED_OR_MADV_COLLAPSE.

Now, one could consider MADV_COLLAPSE an "advise". (I am not opposed to that change)

lol yeah I always think of MADV_COLLAPSE as an extreme version of MADV_HUGE (more of a demand than an advice :)), eventhough its not persistant.
Which is why I think might be unexpected if MADV_HUGE gives hugepages but MADV_COLLAPSE doesn't (But could just be my opinion).

Quoted 4 lines

Indeed, the right way might be telling vma_thp_disabled() whether we are in collapse.

Can you try implementing that on top of my patch to see how it looks?

My reasoning is that a process that is running with system policy always but with PR_THP_DISABLE_EXCEPT_ADVISED gets THPs in exactly the same behaviour as a process that is running with system policy madvise. This will help us achieve (3) that you mentioned in the commit message:
(3) Switch from THP=madvise to THP=always, but keep the old behavior
      (THP only when advised) for selected workloads.

Quoted 5 lines

I have written quite a few selftests now for prctl SET_THP_DISABLE, both with and without PR_THP_DISABLE_EXCEPT_ADVISED set incorporating your feedback on it. I have all of them passing with the below diff. The diff is slightly ugly, but very simple and hopefully acceptable. If it looks good, I can send a series with everything. Probably make the below diff as a separate patch on top of this patch as its mostly adding an extra arg to functions and would keep the review easier?

Quoted 6 lines

Yes, we should do it as a separate patch, makes our life easier, because that requires more work.

We require a cleanup first, the boolean parameter for __thp_vma_allowable_orders() is no good.

I just pushed something untested to my branch (slightly adjusted patch#1 + 2 more patches), can you have a look at that? (untested ... :) )

Thanks for this!
I tested it and its good, have sent it for review.