linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised
@ 2025-08-04 15:40 Usama Arif
  2025-08-04 15:40 ` [PATCH v3 1/6] prctl: extend PR_SET_THP_DISABLE to optionally exclude VM_HUGEPAGE Usama Arif
                   ` (7 more replies)
  0 siblings, 8 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-04 15:40 UTC (permalink / raw)
  To: Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Usama Arif

(Resending this as forgot to include PATCH v2 in subject prefix)

This will allow individual processes to opt-out of THP = "always"
into THP = "madvise", without affecting other workloads on the system.
This has been extensively discussed on the mailing list and has been
summarized very well by David in the first patch which also includes
the links to alternatives, please refer to the first patch commit message
for the motivation for this series.

Patch 1 adds the PR_THP_DISABLE_EXCEPT_ADVISED flag to implement this, along
with the MMF changes.
Patch 2 is a cleanup patch for tva_flags that will allow the forced collapse
case to be transmitted to vma_thp_disabled (which is done in patch 3).
Patch 4 adds documentation for PR_SET_THP_DISABLE/PR_GET_THP_DISABLE.
Patches 5-6 implement the selftests for PR_SET_THP_DISABLE for completely
disabling THPs (old behaviour) and only enabling it at advise
(PR_THP_DISABLE_EXCEPT_ADVISED).

The patches are tested on top of 4ad831303eca6ae518c3b3d86838a2a04b90ec41
from mm-new.

v2 -> v3: https://lore.kernel.org/all/20250731122825.2102184-1-usamaarif642@gmail.com/
- Fix sign off and added ack for patch 1 (Lorenzo and Zi Yan)
- Fix up commit message, comments and variable names in patch 2 and 3 (Lorenzo)
- Added documentation for PR_SET_THP_DISABLE/PR_GET_THP_DISABLE (Lorenzo)
- remove struct test_results and enum thp_policy for prctl tests (David)

v1 -> v2: https://lore.kernel.org/all/20250725162258.1043176-1-usamaarif642@gmail.com/
- Change thp_push_settings to thp_write_settings (David)
- Add tests for all the system policies for the prctl call (David)
- Small fixes and cleanups

David Hildenbrand (3):
  prctl: extend PR_SET_THP_DISABLE to optionally exclude VM_HUGEPAGE
  mm/huge_memory: convert "tva_flags" to "enum tva_type"
  mm/huge_memory: respect MADV_COLLAPSE with
    PR_THP_DISABLE_EXCEPT_ADVISED

Usama Arif (3):
  docs: transhuge: document process level THP controls
  selftests: prctl: introduce tests for disabling THPs completely
  selftests: prctl: introduce tests for disabling THPs except for
    madvise

 Documentation/admin-guide/mm/transhuge.rst    |  38 +++
 Documentation/filesystems/proc.rst            |   5 +-
 fs/proc/array.c                               |   2 +-
 fs/proc/task_mmu.c                            |   4 +-
 include/linux/huge_mm.h                       |  60 ++--
 include/linux/mm_types.h                      |  13 +-
 include/uapi/linux/prctl.h                    |  10 +
 kernel/sys.c                                  |  59 +++-
 mm/huge_memory.c                              |  11 +-
 mm/khugepaged.c                               |  19 +-
 mm/memory.c                                   |  20 +-
 mm/shmem.c                                    |   2 +-
 tools/testing/selftests/mm/.gitignore         |   1 +
 tools/testing/selftests/mm/Makefile           |   1 +
 .../testing/selftests/mm/prctl_thp_disable.c  | 280 ++++++++++++++++++
 tools/testing/selftests/mm/thp_settings.c     |   9 +-
 tools/testing/selftests/mm/thp_settings.h     |   1 +
 17 files changed, 464 insertions(+), 71 deletions(-)
 create mode 100644 tools/testing/selftests/mm/prctl_thp_disable.c

-- 
2.47.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH v3 1/6] prctl: extend PR_SET_THP_DISABLE to optionally exclude VM_HUGEPAGE
  2025-08-04 15:40 [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
@ 2025-08-04 15:40 ` Usama Arif
  2025-08-04 15:40 ` [PATCH v3 2/6] mm/huge_memory: convert "tva_flags" to "enum tva_type" Usama Arif
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-04 15:40 UTC (permalink / raw)
  To: Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Usama Arif

From: David Hildenbrand <david@redhat.com>

People want to make use of more THPs, for example, moving from
the "never" system policy to "madvise", or from "madvise" to "always".

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: individual processes may need to
opt-out from this behavior for various reasons, and this should be
permitted without needing to make all other workloads on the system
similarly opt-out.

The following scenarios are imaginable:

(1) Switch from "none" system policy to "madvise"/"always", but keep THPs
    disabled for selected workloads.

(2) Stay at "none" system policy, but enable THPs for selected
    workloads, making only these workloads use the "madvise" or "always"
    policy.

(3) Switch from "madvise" system policy to "always", but keep the
    "madvise" policy for selected workloads: allocate THPs only when
    advised.

(4) Stay at "madvise" system policy, but enable THPs even when not advised
    for selected workloads -- "always" policy.

Once can emulate (2) through (1), by setting the system policy to
"madvise"/"always" 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 process,
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 is a step backwards, because these
processes can no longer allocate THPs in regions where THPs were
explicitly advised: 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), add 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).

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

    Previously, it would return 1 if THPs were disabled completely. Now
    it returns the set flags as well: 3 if PR_THP_DISABLE_EXCEPT_ADVISED
    was set.

(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
    disabled completely

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

    For now, we don't add another interface to obtained whether THPs
    are disabled partially (PR_THP_DISABLE_EXCEPT_ADVISED was set). If
    ever required, we could add a new entry.

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()).

For now, MADV_COLLAPSE will *fail* in regions without VM_HUGEPAGE and
VM_NOHUGEPAGE. As MADV_COLLAPSE is a clear advise that user space
thinks a THP is a good idea, we'll enable that separately next
(requiring a bit of cleanup first).

There is currently not way to prevent that a process will not issue
PR_SET_THP_DISABLE itself to re-enable THP. There are not really known
users for re-enabling it, and it's against the purpose of the original
interface. So if ever required, we could investigate just forbidding to
re-enable them, or make this somehow configurable.

Acked-by: Usama Arif <usamaarif642@gmail.com>
Tested-by: Usama Arif <usamaarif642@gmail.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Signed-off-by: Usama Arif <usamaarif642@gmail.com>
Acked-by: Zi Yan <ziy@nvidia.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         | 10 +++++
 kernel/sys.c                       | 59 ++++++++++++++++++++++++------
 mm/khugepaged.c                    |  2 +-
 7 files changed, 82 insertions(+), 29 deletions(-)

diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst
index 2971551b7235..915a3e44bc12 100644
--- a/Documentation/filesystems/proc.rst
+++ b/Documentation/filesystems/proc.rst
@@ -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
diff --git a/fs/proc/array.c b/fs/proc/array.c
index d6a0369caa93..c4f91a784104 100644
--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -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);
 }
 
diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index 7748489fde1b..71db243a002e 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -318,16 +318,26 @@ struct thpsize {
 	(transparent_hugepage_flags &					\
 	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
 
+/*
+ * 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)
 {
+	/* 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)
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 1ec273b06691..123fefaa4b98 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -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 when advised (e.g., 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 */
 /*
diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
index 43dec6eed559..9c1d6e49b8a9 100644
--- a/include/uapi/linux/prctl.h
+++ b/include/uapi/linux/prctl.h
@@ -177,7 +177,17 @@ 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 | 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 (e.g., MADV_HUGEPAGE /
+ * VM_HUGEPAGE).
+ */
+# define PR_THP_DISABLE_EXCEPT_ADVISED	(1 << 1)
 #define PR_GET_THP_DISABLE	42
 
 /*
diff --git a/kernel/sys.c b/kernel/sys.c
index b153fb345ada..5b6c80eafff9 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -2423,6 +2423,51 @@ 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 disabled, we return "1 | flags", otherwise 0. */
+	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(bool 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) || (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 +2641,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:
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 1ff0c7dd2be4..2c9008246785 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -410,7 +410,7 @@ static inline int hpage_collapse_test_exit(struct mm_struct *mm)
 static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm)
 {
 	return hpage_collapse_test_exit(mm) ||
-	       test_bit(MMF_DISABLE_THP, &mm->flags);
+	       test_bit(MMF_DISABLE_THP_COMPLETELY, &mm->flags);
 }
 
 static bool hugepage_pmd_enabled(void)
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v3 2/6] mm/huge_memory: convert "tva_flags" to "enum tva_type"
  2025-08-04 15:40 [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
  2025-08-04 15:40 ` [PATCH v3 1/6] prctl: extend PR_SET_THP_DISABLE to optionally exclude VM_HUGEPAGE Usama Arif
@ 2025-08-04 15:40 ` Usama Arif
  2025-08-05  1:41   ` Baolin Wang
  2025-08-05 14:35   ` Lorenzo Stoakes
  2025-08-04 15:40 ` [PATCH v3 3/6] mm/huge_memory: respect MADV_COLLAPSE with PR_THP_DISABLE_EXCEPT_ADVISED Usama Arif
                   ` (5 subsequent siblings)
  7 siblings, 2 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-04 15:40 UTC (permalink / raw)
  To: Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Usama Arif

From: David Hildenbrand <david@redhat.com>

When determining which THP orders are eligible for a VMA mapping,
we have previously specified tva_flags, however it turns out it is
really not necessary to treat these as flags.

Rather, we distinguish between distinct modes.

The only case where we previously combined flags was with
TVA_ENFORCE_SYSFS, but we can avoid this by observing that this
is the default, except for MADV_COLLAPSE or an edge cases in
collapse_pte_mapped_thp() and hugepage_vma_revalidate(), and
adding a mode specifically for this case - TVA_FORCED_COLLAPSE.

We have:
* smaps handling for showing "THPeligible"
* Pagefault handling
* khugepaged handling
* Forced collapse handling: primarily MADV_COLLAPSE, but also for
  an edge case in collapse_pte_mapped_thp()

Disregarding the edge cases, we only want to ignore sysfs settings only
when we are forcing a collapse through MADV_COLLAPSE, otherwise we
want to enforce it, hence this patch does the following flag to enum
conversions:

* TVA_SMAPS | TVA_ENFORCE_SYSFS -> TVA_SMAPS
* TVA_IN_PF | TVA_ENFORCE_SYSFS -> TVA_PAGEFAULT
* TVA_ENFORCE_SYSFS             -> TVA_KHUGEPAGED
* 0                             -> TVA_FORCED_COLLAPSE

With this change, we immediately know if we are in the forced collapse
case, which will be valuable next.

Signed-off-by: David Hildenbrand <david@redhat.com>
Acked-by: Usama Arif <usamaarif642@gmail.com>
Signed-off-by: Usama Arif <usamaarif642@gmail.com>
---
 fs/proc/task_mmu.c      |  4 ++--
 include/linux/huge_mm.h | 30 ++++++++++++++++++------------
 mm/huge_memory.c        |  8 ++++----
 mm/khugepaged.c         | 17 ++++++++---------
 mm/memory.c             | 14 ++++++--------
 5 files changed, 38 insertions(+), 35 deletions(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 3d6d8a9f13fc..d440df7b3d59 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1293,8 +1293,8 @@ static int show_smap(struct seq_file *m, void *v)
 	__show_smap(m, &mss, false);
 
 	seq_printf(m, "THPeligible:    %8u\n",
-		   !!thp_vma_allowable_orders(vma, vma->vm_flags,
-			   TVA_SMAPS | TVA_ENFORCE_SYSFS, THP_ORDERS_ALL));
+		   !!thp_vma_allowable_orders(vma, vma->vm_flags, TVA_SMAPS,
+					      THP_ORDERS_ALL));
 
 	if (arch_pkeys_enabled())
 		seq_printf(m, "ProtectionKey:  %8u\n", vma_pkey(vma));
diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index 71db243a002e..bd4f9e6327e0 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -94,12 +94,15 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr;
 #define THP_ORDERS_ALL	\
 	(THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_SPECIAL | THP_ORDERS_ALL_FILE_DEFAULT)
 
-#define TVA_SMAPS		(1 << 0)	/* Will be used for procfs */
-#define TVA_IN_PF		(1 << 1)	/* Page fault handler */
-#define TVA_ENFORCE_SYSFS	(1 << 2)	/* Obey sysfs configuration */
+enum tva_type {
+	TVA_SMAPS,		/* Exposing "THPeligible:" in smaps. */
+	TVA_PAGEFAULT,		/* Serving a page fault. */
+	TVA_KHUGEPAGED,		/* Khugepaged collapse. */
+	TVA_FORCED_COLLAPSE,	/* Forced collapse (e.g. MADV_COLLAPSE). */
+};
 
-#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, type, order) \
+	(!!thp_vma_allowable_orders(vma, vm_flags, type, BIT(order)))
 
 #define split_folio(f) split_folio_to_list(f, NULL)
 
@@ -264,14 +267,14 @@ 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,
+					 enum tva_type type,
 					 unsigned long orders);
 
 /**
  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
  * @vma:  the vm area to check
  * @vm_flags: use these vm_flags instead of vma->vm_flags
- * @tva_flags: Which TVA flags to honour
+ * @type: TVA type
  * @orders: bitfield of all orders to consider
  *
  * Calculates the intersection of the requested hugepage orders and the allowed
@@ -285,11 +288,14 @@ unsigned long __thp_vma_allowable_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,
+				       enum tva_type type,
 				       unsigned long orders)
 {
-	/* Optimization to check if required orders are enabled early. */
-	if ((tva_flags & TVA_ENFORCE_SYSFS) && vma_is_anonymous(vma)) {
+	/*
+	 * Optimization to check if required orders are enabled early. Only
+	 * forced collapse ignores sysfs configs.
+	 */
+	if (type != TVA_FORCED_COLLAPSE && vma_is_anonymous(vma)) {
 		unsigned long mask = READ_ONCE(huge_anon_orders_always);
 
 		if (vm_flags & VM_HUGEPAGE)
@@ -303,7 +309,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, type, orders);
 }
 
 struct thpsize {
@@ -536,7 +542,7 @@ 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,
+					enum tva_type type,
 					unsigned long orders)
 {
 	return 0;
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 2b4ea5a2ce7d..85252b468f80 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -99,12 +99,12 @@ 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,
+					 enum tva_type type,
 					 unsigned long orders)
 {
-	bool smaps = tva_flags & TVA_SMAPS;
-	bool in_pf = tva_flags & TVA_IN_PF;
-	bool enforce_sysfs = tva_flags & TVA_ENFORCE_SYSFS;
+	const bool smaps = type == TVA_SMAPS;
+	const bool in_pf = type == TVA_PAGEFAULT;
+	const bool enforce_sysfs = type != TVA_FORCED_COLLAPSE;
 	unsigned long supported_orders;
 
 	/* Check the intersection of requested and supported orders. */
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 2c9008246785..88cb6339e910 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -474,8 +474,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))
+		if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
 			__khugepaged_enter(vma->vm_mm);
 	}
 }
@@ -921,7 +920,8 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
 				   struct collapse_control *cc)
 {
 	struct vm_area_struct *vma;
-	unsigned long tva_flags = cc->is_khugepaged ? TVA_ENFORCE_SYSFS : 0;
+	enum tva_type type = cc->is_khugepaged ? TVA_KHUGEPAGED :
+				 TVA_FORCED_COLLAPSE;
 
 	if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
 		return SCAN_ANY_PROCESS;
@@ -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, type, PMD_ORDER))
 		return SCAN_VMA_CHECK;
 	/*
 	 * Anon VMA expected, the address may be unmapped then
@@ -1532,9 +1532,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	 * in the page cache with a single hugepage. If a mm were to fault-in
 	 * this memory (mapped by a suitably aligned VMA), we'd get the hugepage
 	 * and map it by a PMD, regardless of sysfs THP settings. As such, let's
-	 * analogously elide sysfs THP settings here.
+	 * analogously elide sysfs THP settings here and force collapse.
 	 */
-	if (!thp_vma_allowable_order(vma, vma->vm_flags, 0, PMD_ORDER))
+	if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
 		return SCAN_VMA_CHECK;
 
 	/* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
@@ -2431,8 +2431,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 			progress++;
 			break;
 		}
-		if (!thp_vma_allowable_order(vma, vma->vm_flags,
-					TVA_ENFORCE_SYSFS, PMD_ORDER)) {
+		if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER)) {
 skip:
 			progress++;
 			continue;
@@ -2766,7 +2765,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, TVA_FORCED_COLLAPSE, PMD_ORDER))
 		return -EINVAL;
 
 	cc = kmalloc(sizeof(*cc), GFP_KERNEL);
diff --git a/mm/memory.c b/mm/memory.c
index 92fd18a5d8d1..be761753f240 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4369,8 +4369,8 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 	 * Get a list of all the (large) orders below PMD_ORDER that are enabled
 	 * and suitable for swapping THP.
 	 */
-	orders = thp_vma_allowable_orders(vma, vma->vm_flags,
-			TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1);
+	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_PAGEFAULT,
+					  BIT(PMD_ORDER) - 1);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 	orders = thp_swap_suitable_orders(swp_offset(entry),
 					  vmf->address, orders);
@@ -4917,8 +4917,8 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 	 * for this vma. Then filter out the orders that can't be allocated over
 	 * 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);
+	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_PAGEFAULT,
+					  BIT(PMD_ORDER) - 1);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 
 	if (!orders)
@@ -6108,8 +6108,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
 		return VM_FAULT_OOM;
 retry_pud:
 	if (pud_none(*vmf.pud) &&
-	    thp_vma_allowable_order(vma, vm_flags,
-				TVA_IN_PF | TVA_ENFORCE_SYSFS, PUD_ORDER)) {
+	    thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PUD_ORDER)) {
 		ret = create_huge_pud(&vmf);
 		if (!(ret & VM_FAULT_FALLBACK))
 			return ret;
@@ -6143,8 +6142,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
 		goto retry_pud;
 
 	if (pmd_none(*vmf.pmd) &&
-	    thp_vma_allowable_order(vma, vm_flags,
-				TVA_IN_PF | TVA_ENFORCE_SYSFS, PMD_ORDER)) {
+	    thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
 		ret = create_huge_pmd(&vmf);
 		if (!(ret & VM_FAULT_FALLBACK))
 			return ret;
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v3 3/6] mm/huge_memory: respect MADV_COLLAPSE with PR_THP_DISABLE_EXCEPT_ADVISED
  2025-08-04 15:40 [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
  2025-08-04 15:40 ` [PATCH v3 1/6] prctl: extend PR_SET_THP_DISABLE to optionally exclude VM_HUGEPAGE Usama Arif
  2025-08-04 15:40 ` [PATCH v3 2/6] mm/huge_memory: convert "tva_flags" to "enum tva_type" Usama Arif
@ 2025-08-04 15:40 ` Usama Arif
  2025-08-05  1:49   ` Baolin Wang
  2025-08-05 14:43   ` Lorenzo Stoakes
  2025-08-04 15:40 ` [PATCH v3 4/6] docs: transhuge: document process level THP controls Usama Arif
                   ` (4 subsequent siblings)
  7 siblings, 2 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-04 15:40 UTC (permalink / raw)
  To: Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Usama Arif

From: David Hildenbrand <david@redhat.com>

Let's allow for making MADV_COLLAPSE succeed on areas that neither have
VM_HUGEPAGE nor VM_NOHUGEPAGE when we have THP disabled
unless explicitly advised (PR_THP_DISABLE_EXCEPT_ADVISED).

MADV_COLLAPSE is a clear advice that we want to collapse.

Note that we still respect the VM_NOHUGEPAGE flag, just like
MADV_COLLAPSE always does. So consequently, MADV_COLLAPSE is now only
refused on VM_NOHUGEPAGE with PR_THP_DISABLE_EXCEPT_ADVISED,
including for shmem.

Co-developed-by: Usama Arif <usamaarif642@gmail.com>
Signed-off-by: Usama Arif <usamaarif642@gmail.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
---
 include/linux/huge_mm.h    | 8 +++++++-
 include/uapi/linux/prctl.h | 2 +-
 mm/huge_memory.c           | 5 +++--
 mm/memory.c                | 6 ++++--
 mm/shmem.c                 | 2 +-
 5 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index bd4f9e6327e0..1fd06ecbde72 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -329,7 +329,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 forced_collapse)
 {
 	/* Are THPs disabled for this VMA? */
 	if (vm_flags & VM_NOHUGEPAGE)
@@ -343,6 +343,12 @@ static inline bool vma_thp_disabled(struct vm_area_struct *vma,
 	 */
 	if (vm_flags & VM_HUGEPAGE)
 		return false;
+	/*
+	 * Forcing a collapse (e.g., madv_collapse), is a clear advice to
+	 * use THPs.
+	 */
+	if (forced_collapse)
+		return false;
 	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
 }
 
diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
index 9c1d6e49b8a9..cdda963a039a 100644
--- a/include/uapi/linux/prctl.h
+++ b/include/uapi/linux/prctl.h
@@ -185,7 +185,7 @@ struct prctl_mm_map {
 #define PR_SET_THP_DISABLE	41
 /*
  * Don't disable THPs when explicitly advised (e.g., MADV_HUGEPAGE /
- * VM_HUGEPAGE).
+ * VM_HUGEPAGE, MADV_COLLAPSE).
  */
 # define PR_THP_DISABLE_EXCEPT_ADVISED	(1 << 1)
 #define PR_GET_THP_DISABLE	42
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 85252b468f80..ef5ccb0ec5d5 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -104,7 +104,8 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 {
 	const bool smaps = type == TVA_SMAPS;
 	const bool in_pf = type == TVA_PAGEFAULT;
-	const bool enforce_sysfs = type != TVA_FORCED_COLLAPSE;
+	const bool forced_collapse = type == TVA_FORCED_COLLAPSE;
+	const bool enforce_sysfs = !forced_collapse;
 	unsigned long supported_orders;
 
 	/* Check the intersection of requested and supported orders. */
@@ -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, forced_collapse))
 		return 0;
 
 	/* khugepaged doesn't collapse DAX vma, but page fault is fine. */
diff --git a/mm/memory.c b/mm/memory.c
index be761753f240..bd04212d6f79 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5186,9 +5186,11 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	 * It is too late to allocate a small folio, we already have a large
 	 * folio in the pagecache: especially s390 KVM cannot tolerate any
 	 * PMD mappings, but PTE-mapped THP are fine. So let's simply refuse any
-	 * PMD mappings if THPs are disabled.
+	 * PMD mappings if THPs are disabled. As we already have a THP ...
+	 * behave as if we are forcing a collapse.
 	 */
-	if (thp_disabled_by_hw() || vma_thp_disabled(vma, vma->vm_flags))
+	if (thp_disabled_by_hw() || vma_thp_disabled(vma, vma->vm_flags,
+						     /* forced_collapse=*/ true))
 		return ret;
 
 	if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER))
diff --git a/mm/shmem.c b/mm/shmem.c
index e6cdfda08aed..30609197a266 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -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, shmem_huge_force)))
 		return 0;
 
 	global_orders = shmem_huge_global_enabled(inode, index, write_end,
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v3 4/6] docs: transhuge: document process level THP controls
  2025-08-04 15:40 [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
                   ` (2 preceding siblings ...)
  2025-08-04 15:40 ` [PATCH v3 3/6] mm/huge_memory: respect MADV_COLLAPSE with PR_THP_DISABLE_EXCEPT_ADVISED Usama Arif
@ 2025-08-04 15:40 ` Usama Arif
  2025-08-05 10:24   ` David Hildenbrand
  2025-08-05 15:08   ` Lorenzo Stoakes
  2025-08-04 15:40 ` [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely Usama Arif
                   ` (3 subsequent siblings)
  7 siblings, 2 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-04 15:40 UTC (permalink / raw)
  To: Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Usama Arif

This includes the PR_SET_THP_DISABLE/PR_GET_THP_DISABLE pair of
prctl calls as well the newly introduced PR_THP_DISABLE_EXCEPT_ADVISED
flag for the PR_SET_THP_DISABLE prctl call.

Signed-off-by: Usama Arif <usamaarif642@gmail.com>
---
 Documentation/admin-guide/mm/transhuge.rst | 38 ++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/admin-guide/mm/transhuge.rst
index 370fba113460..a36a04394ff5 100644
--- a/Documentation/admin-guide/mm/transhuge.rst
+++ b/Documentation/admin-guide/mm/transhuge.rst
@@ -225,6 +225,44 @@ to "always" or "madvise"), and it'll be automatically shutdown when
 PMD-sized THP is disabled (when both the per-size anon control and the
 top-level control are "never")
 
+process THP controls
+--------------------
+
+A process can control its own THP behaviour using the ``PR_SET_THP_DISABLE``
+and ``PR_GET_THP_DISABLE`` pair of prctl(2) calls. These calls support the
+following arguments::
+
+	prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0):
+		This will set the MMF_DISABLE_THP_COMPLETELY mm flag which will
+		result in no THPs being faulted in or collapsed, irrespective
+		of global THP controls. This flag and hence the behaviour is
+		inherited across fork(2) and execve(2).
+
+	prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, 0, 0):
+		This will set the MMF_DISABLE_THP_EXCEPT_ADVISED mm flag which
+		will result in THPs being faulted in or collapsed only for
+		the following cases:
+		- Global THP controls are set to "always" or "madvise" and
+		  the process has madvised the region with either MADV_HUGEPAGE
+		  or MADV_COLLAPSE.
+		- Global THP controls is set to "never" and the process has
+		  madvised the region with MADV_COLLAPSE.
+		This flag and hence the behaviour is inherited across fork(2)
+		and execve(2).
+
+	prctl(PR_SET_THP_DISABLE, 0, 0, 0, 0):
+		This will clear the MMF_DISABLE_THP_COMPLETELY and
+		MMF_DISABLE_THP_EXCEPT_ADVISED mm flags. The process will
+		behave according to the global THP controls. This behaviour
+		will be inherited across fork(2) and execve(2).
+
+	prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0):
+		This will return the THP disable mm flag status of the process
+		that was set by prctl(PR_SET_THP_DISABLE, ...). i.e.
+		- 1 if MMF_DISABLE_THP_COMPLETELY flag is set
+		- 3 if MMF_DISABLE_THP_EXCEPT_ADVISED flag is set
+		- 0 otherwise.
+
 Khugepaged controls
 -------------------
 
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely
  2025-08-04 15:40 [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
                   ` (3 preceding siblings ...)
  2025-08-04 15:40 ` [PATCH v3 4/6] docs: transhuge: document process level THP controls Usama Arif
@ 2025-08-04 15:40 ` Usama Arif
  2025-08-05 10:30   ` David Hildenbrand
  2025-08-05 12:39   ` David Hildenbrand
  2025-08-04 15:40 ` [PATCH v3 6/6] selftests: prctl: introduce tests for disabling THPs except for madvise Usama Arif
                   ` (2 subsequent siblings)
  7 siblings, 2 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-04 15:40 UTC (permalink / raw)
  To: Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Usama Arif

The test will set the global system THP setting to never, madvise
or always depending on the fixture variant and the 2M setting to
inherit before it starts (and reset to original at teardown).

This tests if the process can:
- successfully set and get the policy to disable THPs completely.
- never get a hugepage when the THPs are completely disabled
  with the prctl, including with MADV_HUGE and MADV_COLLAPSE.
- successfully reset the policy of the process.
- after reset, only get hugepages with:
  - MADV_COLLAPSE when policy is set to never.
  - MADV_HUGE and MADV_COLLAPSE when policy is set to madvise.
  - always when policy is set to "always".
- repeat the above tests in a forked process to make sure
  the policy is carried across forks.

Signed-off-by: Usama Arif <usamaarif642@gmail.com>
---
 tools/testing/selftests/mm/.gitignore         |   1 +
 tools/testing/selftests/mm/Makefile           |   1 +
 .../testing/selftests/mm/prctl_thp_disable.c  | 173 ++++++++++++++++++
 tools/testing/selftests/mm/thp_settings.c     |   9 +-
 tools/testing/selftests/mm/thp_settings.h     |   1 +
 5 files changed, 184 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/mm/prctl_thp_disable.c

diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore
index e7b23a8a05fe..eb023ea857b3 100644
--- a/tools/testing/selftests/mm/.gitignore
+++ b/tools/testing/selftests/mm/.gitignore
@@ -58,3 +58,4 @@ pkey_sighandler_tests_32
 pkey_sighandler_tests_64
 guard-regions
 merge
+prctl_thp_disable
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index d13b3cef2a2b..2bb8d3ebc17c 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -86,6 +86,7 @@ TEST_GEN_FILES += on-fault-limit
 TEST_GEN_FILES += pagemap_ioctl
 TEST_GEN_FILES += pfnmap
 TEST_GEN_FILES += process_madv
+TEST_GEN_FILES += prctl_thp_disable
 TEST_GEN_FILES += thuge-gen
 TEST_GEN_FILES += transhuge-stress
 TEST_GEN_FILES += uffd-stress
diff --git a/tools/testing/selftests/mm/prctl_thp_disable.c b/tools/testing/selftests/mm/prctl_thp_disable.c
new file mode 100644
index 000000000000..ef150180daf4
--- /dev/null
+++ b/tools/testing/selftests/mm/prctl_thp_disable.c
@@ -0,0 +1,173 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Basic tests for PR_GET/SET_THP_DISABLE prctl calls
+ *
+ * Author(s): Usama Arif <usamaarif642@gmail.com>
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/wait.h>
+
+#include "../kselftest_harness.h"
+#include "thp_settings.h"
+#include "vm_util.h"
+
+static int sz2ord(size_t size, size_t pagesize)
+{
+	return __builtin_ctzll(size / pagesize);
+}
+
+enum thp_collapse_type {
+	THP_COLLAPSE_NONE,
+	THP_COLLAPSE_MADV_HUGEPAGE,	/* MADV_HUGEPAGE before access */
+	THP_COLLAPSE_MADV_COLLAPSE,	/* MADV_COLLAPSE after access */
+};
+
+/*
+ * Function to mmap a buffer, fault it in, madvise it appropriately (before
+ * page fault for MADV_HUGE, and after for MADV_COLLAPSE), and check if the
+ * mmap region is huge.
+ * Returns:
+ * 0 if test doesn't give hugepage
+ * 1 if test gives a hugepage
+ * -errno if mmap fails
+ */
+static int test_mmap_thp(enum thp_collapse_type madvise_buf, size_t pmdsize)
+{
+	char *mem, *mmap_mem;
+	size_t mmap_size;
+	int ret;
+
+	/* For alignment purposes, we need twice the THP size. */
+	mmap_size = 2 * pmdsize;
+	mmap_mem = (char *)mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
+				    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (mmap_mem == MAP_FAILED)
+		return -errno;
+
+	/* We need a THP-aligned memory area. */
+	mem = (char *)(((uintptr_t)mmap_mem + pmdsize) & ~(pmdsize - 1));
+
+	if (madvise_buf == THP_COLLAPSE_MADV_HUGEPAGE)
+		madvise(mem, pmdsize, MADV_HUGEPAGE);
+
+	/* Ensure memory is allocated */
+	memset(mem, 1, pmdsize);
+
+	if (madvise_buf == THP_COLLAPSE_MADV_COLLAPSE)
+		madvise(mem, pmdsize, MADV_COLLAPSE);
+
+	/* HACK: make sure we have a separate VMA that we can check reliably. */
+	mprotect(mem, pmdsize, PROT_READ);
+
+	ret = check_huge_anon(mem, 1, pmdsize);
+	munmap(mmap_mem, mmap_size);
+	return ret;
+}
+
+static void prctl_thp_disable_completely_test(struct __test_metadata *const _metadata,
+					      size_t pmdsize,
+					      enum thp_enabled thp_policy)
+{
+	ASSERT_EQ(prctl(PR_GET_THP_DISABLE, NULL, NULL, NULL, NULL), 1);
+
+	/* tests after prctl overrides global policy */
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_NONE, pmdsize), 0);
+
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_MADV_HUGEPAGE, pmdsize), 0);
+
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_MADV_COLLAPSE, pmdsize), 0);
+
+	/* Reset to global policy */
+	ASSERT_EQ(prctl(PR_SET_THP_DISABLE, 0, NULL, NULL, NULL), 0);
+
+	/* tests after prctl is cleared, and only global policy is effective */
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_NONE, pmdsize),
+		  thp_policy == THP_ALWAYS ? 1 : 0);
+
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_MADV_HUGEPAGE, pmdsize),
+		  thp_policy == THP_NEVER ? 0 : 1);
+
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_MADV_COLLAPSE, pmdsize), 1);
+}
+
+FIXTURE(prctl_thp_disable_completely)
+{
+	struct thp_settings settings;
+	size_t pmdsize;
+};
+
+FIXTURE_VARIANT(prctl_thp_disable_completely)
+{
+	enum thp_enabled thp_policy;
+};
+
+FIXTURE_VARIANT_ADD(prctl_thp_disable_completely, never)
+{
+	.thp_policy = THP_NEVER,
+};
+
+FIXTURE_VARIANT_ADD(prctl_thp_disable_completely, madvise)
+{
+	.thp_policy = THP_MADVISE,
+};
+
+FIXTURE_VARIANT_ADD(prctl_thp_disable_completely, always)
+{
+	.thp_policy = THP_ALWAYS,
+};
+
+FIXTURE_SETUP(prctl_thp_disable_completely)
+{
+	if (!thp_available())
+		SKIP(return, "Transparent Hugepages not available\n");
+
+	self->pmdsize = read_pmd_pagesize();
+	if (!self->pmdsize)
+		SKIP(return, "Unable to read PMD size\n");
+
+	thp_save_settings();
+	thp_read_settings(&self->settings);
+	self->settings.thp_enabled = variant->thp_policy;
+	self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;
+	thp_write_settings(&self->settings);
+}
+
+FIXTURE_TEARDOWN(prctl_thp_disable_completely)
+{
+	thp_restore_settings();
+}
+
+TEST_F(prctl_thp_disable_completely, nofork)
+{
+	ASSERT_EQ(prctl(PR_SET_THP_DISABLE, 1, NULL, NULL, NULL), 0);
+	prctl_thp_disable_completely_test(_metadata, self->pmdsize, variant->thp_policy);
+}
+
+TEST_F(prctl_thp_disable_completely, fork)
+{
+	int ret = 0;
+	pid_t pid;
+
+	ASSERT_EQ(prctl(PR_SET_THP_DISABLE, 1, NULL, NULL, NULL), 0);
+
+	/* Make sure prctl changes are carried across fork */
+	pid = fork();
+	ASSERT_GE(pid, 0);
+
+	if (!pid)
+		prctl_thp_disable_completely_test(_metadata, self->pmdsize, variant->thp_policy);
+
+	wait(&ret);
+	if (WIFEXITED(ret))
+		ret = WEXITSTATUS(ret);
+	else
+		ret = -EINVAL;
+	ASSERT_EQ(ret, 0);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/mm/thp_settings.c b/tools/testing/selftests/mm/thp_settings.c
index bad60ac52874..574bd0f8ae48 100644
--- a/tools/testing/selftests/mm/thp_settings.c
+++ b/tools/testing/selftests/mm/thp_settings.c
@@ -382,10 +382,17 @@ unsigned long thp_shmem_supported_orders(void)
 	return __thp_supported_orders(true);
 }
 
-bool thp_is_enabled(void)
+bool thp_available(void)
 {
 	if (access(THP_SYSFS, F_OK) != 0)
 		return false;
+	return true;
+}
+
+bool thp_is_enabled(void)
+{
+	if (!thp_available())
+		return false;
 
 	int mode = thp_read_string("enabled", thp_enabled_strings);
 
diff --git a/tools/testing/selftests/mm/thp_settings.h b/tools/testing/selftests/mm/thp_settings.h
index 6c07f70beee9..76eeb712e5f1 100644
--- a/tools/testing/selftests/mm/thp_settings.h
+++ b/tools/testing/selftests/mm/thp_settings.h
@@ -84,6 +84,7 @@ void thp_set_read_ahead_path(char *path);
 unsigned long thp_supported_orders(void);
 unsigned long thp_shmem_supported_orders(void);
 
+bool thp_available(void);
 bool thp_is_enabled(void);
 
 #endif /* __THP_SETTINGS_H__ */
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v3 6/6] selftests: prctl: introduce tests for disabling THPs except for madvise
  2025-08-04 15:40 [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
                   ` (4 preceding siblings ...)
  2025-08-04 15:40 ` [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely Usama Arif
@ 2025-08-04 15:40 ` Usama Arif
  2025-08-05 10:36   ` David Hildenbrand
  2025-08-04 15:46 ` [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
  2025-08-13  6:06 ` Lorenzo Stoakes
  7 siblings, 1 reply; 30+ messages in thread
From: Usama Arif @ 2025-08-04 15:40 UTC (permalink / raw)
  To: Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Usama Arif

The test will set the global system THP setting to never, madvise
or always depending on the fixture variant and the 2M setting to
inherit before it starts (and reset to original at teardown)

This tests if the process can:
- successfully set and get the policy to disable THPs expect for madvise.
- get hugepages only on MADV_HUGE and MADV_COLLAPSE if the global policy
  is madvise/always and only with MADV_COLLAPSE if the global policy is
  never.
- successfully reset the policy of the process.
- after reset, only get hugepages with:
  - MADV_COLLAPSE when policy is set to never.
  - MADV_HUGE and MADV_COLLAPSE when policy is set to madvise.
  - always when policy is set to "always".
- repeat the above tests in a forked process to make sure  the policy is
  carried across forks.

Signed-off-by: Usama Arif <usamaarif642@gmail.com>
---
 .../testing/selftests/mm/prctl_thp_disable.c  | 107 ++++++++++++++++++
 1 file changed, 107 insertions(+)

diff --git a/tools/testing/selftests/mm/prctl_thp_disable.c b/tools/testing/selftests/mm/prctl_thp_disable.c
index ef150180daf4..93cedaa59854 100644
--- a/tools/testing/selftests/mm/prctl_thp_disable.c
+++ b/tools/testing/selftests/mm/prctl_thp_disable.c
@@ -16,6 +16,10 @@
 #include "thp_settings.h"
 #include "vm_util.h"
 
+#ifndef PR_THP_DISABLE_EXCEPT_ADVISED
+#define PR_THP_DISABLE_EXCEPT_ADVISED (1 << 1)
+#endif
+
 static int sz2ord(size_t size, size_t pagesize)
 {
 	return __builtin_ctzll(size / pagesize);
@@ -170,4 +174,107 @@ TEST_F(prctl_thp_disable_completely, fork)
 	ASSERT_EQ(ret, 0);
 }
 
+static void prctl_thp_disable_except_madvise_test(struct __test_metadata *const _metadata,
+						  size_t pmdsize,
+						  enum thp_enabled thp_policy)
+{
+	ASSERT_EQ(prctl(PR_GET_THP_DISABLE, NULL, NULL, NULL, NULL), 3);
+
+	/* tests after prctl overrides global policy */
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_NONE, pmdsize), 0);
+
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_MADV_HUGEPAGE, pmdsize),
+		  thp_policy == THP_NEVER ? 0 : 1);
+
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_MADV_COLLAPSE, pmdsize), 1);
+
+	/* Reset to global policy */
+	ASSERT_EQ(prctl(PR_SET_THP_DISABLE, 0, NULL, NULL, NULL), 0);
+
+	/* tests after prctl is cleared, and only global policy is effective */
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_NONE, pmdsize),
+		  thp_policy == THP_ALWAYS ? 1 : 0);
+
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_MADV_HUGEPAGE, pmdsize),
+		  thp_policy == THP_NEVER ? 0 : 1);
+
+	ASSERT_EQ(test_mmap_thp(THP_COLLAPSE_MADV_COLLAPSE, pmdsize), 1);
+}
+
+FIXTURE(prctl_thp_disable_except_madvise)
+{
+	struct thp_settings settings;
+	size_t pmdsize;
+};
+
+FIXTURE_VARIANT(prctl_thp_disable_except_madvise)
+{
+	enum thp_enabled thp_policy;
+};
+
+FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, never)
+{
+	.thp_policy = THP_NEVER,
+};
+
+FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, madvise)
+{
+	.thp_policy = THP_MADVISE,
+};
+
+FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, always)
+{
+	.thp_policy = THP_ALWAYS,
+};
+
+FIXTURE_SETUP(prctl_thp_disable_except_madvise)
+{
+	if (!thp_available())
+		SKIP(return, "Transparent Hugepages not available\n");
+
+	self->pmdsize = read_pmd_pagesize();
+	if (!self->pmdsize)
+		SKIP(return, "Unable to read PMD size\n");
+
+	thp_save_settings();
+	thp_read_settings(&self->settings);
+	self->settings.thp_enabled = variant->thp_policy;
+	self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;
+	thp_write_settings(&self->settings);
+}
+
+FIXTURE_TEARDOWN(prctl_thp_disable_except_madvise)
+{
+	thp_restore_settings();
+}
+
+TEST_F(prctl_thp_disable_except_madvise, nofork)
+{
+	ASSERT_EQ(prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, NULL, NULL), 0);
+	prctl_thp_disable_except_madvise_test(_metadata, self->pmdsize, variant->thp_policy);
+}
+
+TEST_F(prctl_thp_disable_except_madvise, fork)
+{
+	int ret = 0;
+	pid_t pid;
+
+	ASSERT_EQ(prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, NULL, NULL), 0);
+
+	/* Make sure prctl changes are carried across fork */
+	pid = fork();
+	ASSERT_GE(pid, 0);
+
+	if (!pid)
+		prctl_thp_disable_except_madvise_test(_metadata, self->pmdsize,
+						      variant->thp_policy);
+
+	wait(&ret);
+	if (WIFEXITED(ret))
+		ret = WEXITSTATUS(ret);
+	else
+		ret = -EINVAL;
+	ASSERT_EQ(ret, 0);
+}
+
 TEST_HARNESS_MAIN
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised
  2025-08-04 15:40 [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
                   ` (5 preceding siblings ...)
  2025-08-04 15:40 ` [PATCH v3 6/6] selftests: prctl: introduce tests for disabling THPs except for madvise Usama Arif
@ 2025-08-04 15:46 ` Usama Arif
  2025-08-13  6:06 ` Lorenzo Stoakes
  7 siblings, 0 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-04 15:46 UTC (permalink / raw)
  To: Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team



On 04/08/2025 16:40, Usama Arif wrote:
> (Resending this as forgot to include PATCH v2 in subject prefix)

Not a resend, this is v3, just forgot to remove the above line from the
previous cover letter :) 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 2/6] mm/huge_memory: convert "tva_flags" to "enum tva_type"
  2025-08-04 15:40 ` [PATCH v3 2/6] mm/huge_memory: convert "tva_flags" to "enum tva_type" Usama Arif
@ 2025-08-05  1:41   ` Baolin Wang
  2025-08-05 14:35   ` Lorenzo Stoakes
  1 sibling, 0 replies; 30+ messages in thread
From: Baolin Wang @ 2025-08-05  1:41 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, npache,
	lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka, jannh,
	Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team



On 2025/8/4 23:40, Usama Arif wrote:
> From: David Hildenbrand <david@redhat.com>
> 
> When determining which THP orders are eligible for a VMA mapping,
> we have previously specified tva_flags, however it turns out it is
> really not necessary to treat these as flags.
> 
> Rather, we distinguish between distinct modes.
> 
> The only case where we previously combined flags was with
> TVA_ENFORCE_SYSFS, but we can avoid this by observing that this
> is the default, except for MADV_COLLAPSE or an edge cases in
> collapse_pte_mapped_thp() and hugepage_vma_revalidate(), and
> adding a mode specifically for this case - TVA_FORCED_COLLAPSE.
> 
> We have:
> * smaps handling for showing "THPeligible"
> * Pagefault handling
> * khugepaged handling
> * Forced collapse handling: primarily MADV_COLLAPSE, but also for
>    an edge case in collapse_pte_mapped_thp()
> 
> Disregarding the edge cases, we only want to ignore sysfs settings only
> when we are forcing a collapse through MADV_COLLAPSE, otherwise we
> want to enforce it, hence this patch does the following flag to enum
> conversions:
> 
> * TVA_SMAPS | TVA_ENFORCE_SYSFS -> TVA_SMAPS
> * TVA_IN_PF | TVA_ENFORCE_SYSFS -> TVA_PAGEFAULT
> * TVA_ENFORCE_SYSFS             -> TVA_KHUGEPAGED
> * 0                             -> TVA_FORCED_COLLAPSE
> 
> With this change, we immediately know if we are in the forced collapse
> case, which will be valuable next.
> 
> Signed-off-by: David Hildenbrand <david@redhat.com>
> Acked-by: Usama Arif <usamaarif642@gmail.com>
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> ---

Looks really nice. Thanks.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 3/6] mm/huge_memory: respect MADV_COLLAPSE with PR_THP_DISABLE_EXCEPT_ADVISED
  2025-08-04 15:40 ` [PATCH v3 3/6] mm/huge_memory: respect MADV_COLLAPSE with PR_THP_DISABLE_EXCEPT_ADVISED Usama Arif
@ 2025-08-05  1:49   ` Baolin Wang
  2025-08-05 14:43   ` Lorenzo Stoakes
  1 sibling, 0 replies; 30+ messages in thread
From: Baolin Wang @ 2025-08-05  1:49 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, david, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, npache,
	lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka, jannh,
	Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team



On 2025/8/4 23:40, Usama Arif wrote:
> From: David Hildenbrand <david@redhat.com>
> 
> Let's allow for making MADV_COLLAPSE succeed on areas that neither have
> VM_HUGEPAGE nor VM_NOHUGEPAGE when we have THP disabled
> unless explicitly advised (PR_THP_DISABLE_EXCEPT_ADVISED).
> 
> MADV_COLLAPSE is a clear advice that we want to collapse.
> 
> Note that we still respect the VM_NOHUGEPAGE flag, just like
> MADV_COLLAPSE always does. So consequently, MADV_COLLAPSE is now only
> refused on VM_NOHUGEPAGE with PR_THP_DISABLE_EXCEPT_ADVISED,
> including for shmem.
> 
> Co-developed-by: Usama Arif <usamaarif642@gmail.com>
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> Signed-off-by: David Hildenbrand <david@redhat.com>
> ---

LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 4/6] docs: transhuge: document process level THP controls
  2025-08-04 15:40 ` [PATCH v3 4/6] docs: transhuge: document process level THP controls Usama Arif
@ 2025-08-05 10:24   ` David Hildenbrand
  2025-08-05 12:09     ` Usama Arif
  2025-08-05 15:08   ` Lorenzo Stoakes
  1 sibling, 1 reply; 30+ messages in thread
From: David Hildenbrand @ 2025-08-05 10:24 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team

On 04.08.25 17:40, Usama Arif wrote:
> This includes the PR_SET_THP_DISABLE/PR_GET_THP_DISABLE pair of
> prctl calls as well the newly introduced PR_THP_DISABLE_EXCEPT_ADVISED
> flag for the PR_SET_THP_DISABLE prctl call.
> 
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> ---
>   Documentation/admin-guide/mm/transhuge.rst | 38 ++++++++++++++++++++++
>   1 file changed, 38 insertions(+)
> 
> diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/admin-guide/mm/transhuge.rst
> index 370fba113460..a36a04394ff5 100644
> --- a/Documentation/admin-guide/mm/transhuge.rst
> +++ b/Documentation/admin-guide/mm/transhuge.rst
> @@ -225,6 +225,44 @@ to "always" or "madvise"), and it'll be automatically shutdown when
>   PMD-sized THP is disabled (when both the per-size anon control and the
>   top-level control are "never")
>   
> +process THP controls
> +--------------------
> +
> +A process can control its own THP behaviour using the ``PR_SET_THP_DISABLE``
> +and ``PR_GET_THP_DISABLE`` pair of prctl(2) calls. These calls support the
> +following arguments::
> +

Not sure if we really want to talk about MMF_ internals.

> +	prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0):
> +		This will set the MMF_DISABLE_THP_COMPLETELY mm flag which will
> +		result in no THPs being faulted in or collapsed, irrespective
> +		of global THP controls. This flag and hence the behaviour is
> +		inherited across fork(2) and execve(2).

"
This will disable THPs completely for the process, irrespective
of global THP controls or MADV_COLLAPSE.
"

?

> +
> +	prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, 0, 0):
> +		This will set the MMF_DISABLE_THP_EXCEPT_ADVISED mm flag which
> +		will result in THPs being faulted in or collapsed only for
> +		the following cases:

Maybe:

This will disable THPs for the process except when the usage of THPs is 
advised. Consequently, THPs will only be used when:
- Global THP controls are set to "always" or "madvise" and
   the area either has VM_HUGEPAGE set (e.g., due do MADV_HUGEPAGE) or
   MADV_COLLAPSE is used.
- Global THP controls are set to "never" and MADV_COLLAPSE is used. This
   is the same behavior as if THPs would not be disabled on a process
   level.
Note that MADV_COLLAPSE is currently always rejected if VM_NOHUGEPAGE is 
set on an area.
...

 > +		  or MADV_COLLAPSE.
> +		- Global THP controls are set to "always" or "madvise" and
> +		  the process has madvised the region with either MADV_HUGEPAGE
> +		  or MADV_COLLAPSE.
> +		- Global THP controls is set to "never" and the process has
> +		  madvised the region with MADV_COLLAPSE.
> +		This flag and hence the behaviour is inherited across fork(2)
> +		and execve(2).
> +
> +	prctl(PR_SET_THP_DISABLE, 0, 0, 0, 0):
> +		This will clear the MMF_DISABLE_THP_COMPLETELY and
> +		MMF_DISABLE_THP_EXCEPT_ADVISED mm flags. The process will
> +		behave according to the global THP controls. This behaviour
> +		will be inherited across fork(2) and execve(2).

"This will re-enabled THPs for the process, as if they would never have 
been disabled. Whether THPs will actually be used depends on global THP 
controls."

?

> +
> +	prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0):
> +		This will return the THP disable mm flag status of the process
> +		that was set by prctl(PR_SET_THP_DISABLE, ...). i.e.
> +		- 1 if MMF_DISABLE_THP_COMPLETELY flag is set

if PR_SET_THP_DISABLE was called without any flags

> +		- 3 if MMF_DISABLE_THP_EXCEPT_ADVISED flag is set

if PR_SET_THP_DISABLE was called with PR_THP_DISABLE_EXCEPT_ADVISED

> +		- 0 otherwise.

> +
>   Khugepaged controls
>   -------------------
>   


-- 
Cheers,

David / dhildenb


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely
  2025-08-04 15:40 ` [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely Usama Arif
@ 2025-08-05 10:30   ` David Hildenbrand
  2025-08-05 12:13     ` Usama Arif
  2025-08-05 12:39   ` David Hildenbrand
  1 sibling, 1 reply; 30+ messages in thread
From: David Hildenbrand @ 2025-08-05 10:30 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Donet Tom

On 04.08.25 17:40, Usama Arif wrote:
> The test will set the global system THP setting to never, madvise
> or always depending on the fixture variant and the 2M setting to
> inherit before it starts (and reset to original at teardown).
> 
> This tests if the process can:
> - successfully set and get the policy to disable THPs completely.
> - never get a hugepage when the THPs are completely disabled
>    with the prctl, including with MADV_HUGE and MADV_COLLAPSE.
> - successfully reset the policy of the process.
> - after reset, only get hugepages with:
>    - MADV_COLLAPSE when policy is set to never.
>    - MADV_HUGE and MADV_COLLAPSE when policy is set to madvise.
>    - always when policy is set to "always".
> - repeat the above tests in a forked process to make sure
>    the policy is carried across forks.
> 
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> ---
>   tools/testing/selftests/mm/.gitignore         |   1 +
>   tools/testing/selftests/mm/Makefile           |   1 +
>   .../testing/selftests/mm/prctl_thp_disable.c  | 173 ++++++++++++++++++
>   tools/testing/selftests/mm/thp_settings.c     |   9 +-
>   tools/testing/selftests/mm/thp_settings.h     |   1 +
>   5 files changed, 184 insertions(+), 1 deletion(-)
>   create mode 100644 tools/testing/selftests/mm/prctl_thp_disable.c
> 
> diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore
> index e7b23a8a05fe..eb023ea857b3 100644
> --- a/tools/testing/selftests/mm/.gitignore
> +++ b/tools/testing/selftests/mm/.gitignore
> @@ -58,3 +58,4 @@ pkey_sighandler_tests_32
>   pkey_sighandler_tests_64
>   guard-regions
>   merge
> +prctl_thp_disable
> diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
> index d13b3cef2a2b..2bb8d3ebc17c 100644
> --- a/tools/testing/selftests/mm/Makefile
> +++ b/tools/testing/selftests/mm/Makefile
> @@ -86,6 +86,7 @@ TEST_GEN_FILES += on-fault-limit
>   TEST_GEN_FILES += pagemap_ioctl
>   TEST_GEN_FILES += pfnmap
>   TEST_GEN_FILES += process_madv
> +TEST_GEN_FILES += prctl_thp_disable
>   TEST_GEN_FILES += thuge-gen
>   TEST_GEN_FILES += transhuge-stress
>   TEST_GEN_FILES += uffd-stress
> diff --git a/tools/testing/selftests/mm/prctl_thp_disable.c b/tools/testing/selftests/mm/prctl_thp_disable.c
> new file mode 100644
> index 000000000000..ef150180daf4
> --- /dev/null
> +++ b/tools/testing/selftests/mm/prctl_thp_disable.c
> @@ -0,0 +1,173 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Basic tests for PR_GET/SET_THP_DISABLE prctl calls
> + *
> + * Author(s): Usama Arif <usamaarif642@gmail.com>
> + */
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <sys/mman.h>
> +#include <sys/prctl.h>
> +#include <sys/wait.h>
> +
> +#include "../kselftest_harness.h"
> +#include "thp_settings.h"
> +#include "vm_util.h"
> +
> +static int sz2ord(size_t size, size_t pagesize)
> +{
> +	return __builtin_ctzll(size / pagesize);
> +}

Note: We have this helper duplicated elsewhere (e.g., cow.c).

See [1] how we're going to clean it up.

Not sure how to resolve this. Probably, which series lands first in 
mm-unstable should clean it up.

I would assume that Donet's series would go in first, such that you can 
just reuse the helper from vm_utils.h

[1] https://lkml.kernel.org/r/20250804090410.of5xwrlker665bdp@master


Nothing else jumped at me

Acked-by: David Hildenbrand <david@redhat.com>

-- 
Cheers,

David / dhildenb


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 6/6] selftests: prctl: introduce tests for disabling THPs except for madvise
  2025-08-04 15:40 ` [PATCH v3 6/6] selftests: prctl: introduce tests for disabling THPs except for madvise Usama Arif
@ 2025-08-05 10:36   ` David Hildenbrand
  2025-08-05 12:19     ` Usama Arif
  0 siblings, 1 reply; 30+ messages in thread
From: David Hildenbrand @ 2025-08-05 10:36 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team

On 04.08.25 17:40, Usama Arif wrote:
> The test will set the global system THP setting to never, madvise
> or always depending on the fixture variant and the 2M setting to
> inherit before it starts (and reset to original at teardown)
> 
> This tests if the process can:
> - successfully set and get the policy to disable THPs expect for madvise.
> - get hugepages only on MADV_HUGE and MADV_COLLAPSE if the global policy
>    is madvise/always and only with MADV_COLLAPSE if the global policy is
>    never.
> - successfully reset the policy of the process.
> - after reset, only get hugepages with:
>    - MADV_COLLAPSE when policy is set to never.
>    - MADV_HUGE and MADV_COLLAPSE when policy is set to madvise.
>    - always when policy is set to "always".
> - repeat the above tests in a forked process to make sure  the policy is
>    carried across forks.
> 
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> ---

[...]

> +FIXTURE_VARIANT(prctl_thp_disable_except_madvise)
> +{
> +	enum thp_enabled thp_policy;
> +};
> +
> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, never)
> +{
> +	.thp_policy = THP_NEVER,
> +};
> +
> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, madvise)
> +{
> +	.thp_policy = THP_MADVISE,
> +};
> +
> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, always)
> +{
> +	.thp_policy = THP_ALWAYS,
> +};
> +
> +FIXTURE_SETUP(prctl_thp_disable_except_madvise)
> +{
> +	if (!thp_available())
> +		SKIP(return, "Transparent Hugepages not available\n");
> +
> +	self->pmdsize = read_pmd_pagesize();
> +	if (!self->pmdsize)
> +		SKIP(return, "Unable to read PMD size\n");

Should we test here if the kernel knows PR_THP_DISABLE_EXCEPT_ADVISED, 
and if not, skip?

Might be as simple as trying issuing two prctl, and making sure the 
first disabling attempt doesn't fail. If so, SKIP.

Nothing else jumped at me. Can you include a test run result in the 
patch description?

-- 
Cheers,

David / dhildenb


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 4/6] docs: transhuge: document process level THP controls
  2025-08-05 10:24   ` David Hildenbrand
@ 2025-08-05 12:09     ` Usama Arif
  0 siblings, 0 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-05 12:09 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team



On 05/08/2025 11:24, David Hildenbrand wrote:
> On 04.08.25 17:40, Usama Arif wrote:
>> This includes the PR_SET_THP_DISABLE/PR_GET_THP_DISABLE pair of
>> prctl calls as well the newly introduced PR_THP_DISABLE_EXCEPT_ADVISED
>> flag for the PR_SET_THP_DISABLE prctl call.
>>
>> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
>> ---
>>   Documentation/admin-guide/mm/transhuge.rst | 38 ++++++++++++++++++++++
>>   1 file changed, 38 insertions(+)
>>
>> diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/admin-guide/mm/transhuge.rst
>> index 370fba113460..a36a04394ff5 100644
>> --- a/Documentation/admin-guide/mm/transhuge.rst
>> +++ b/Documentation/admin-guide/mm/transhuge.rst
>> @@ -225,6 +225,44 @@ to "always" or "madvise"), and it'll be automatically shutdown when
>>   PMD-sized THP is disabled (when both the per-size anon control and the
>>   top-level control are "never")
>>   +process THP controls
>> +--------------------
>> +
>> +A process can control its own THP behaviour using the ``PR_SET_THP_DISABLE``
>> +and ``PR_GET_THP_DISABLE`` pair of prctl(2) calls. These calls support the
>> +following arguments::
>> +
> 
> Not sure if we really want to talk about MMF_ internals.
> 

Thanks! have changed it for the next revision according to your suggestions.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely
  2025-08-05 10:30   ` David Hildenbrand
@ 2025-08-05 12:13     ` Usama Arif
  2025-08-05 12:16       ` David Hildenbrand
  0 siblings, 1 reply; 30+ messages in thread
From: Usama Arif @ 2025-08-05 12:13 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Donet Tom



On 05/08/2025 11:30, David Hildenbrand wrote:
> On 04.08.25 17:40, Usama Arif wrote:
>> The test will set the global system THP setting to never, madvise
>> or always depending on the fixture variant and the 2M setting to
>> inherit before it starts (and reset to original at teardown).
>>
>> This tests if the process can:
>> - successfully set and get the policy to disable THPs completely.
>> - never get a hugepage when the THPs are completely disabled
>>    with the prctl, including with MADV_HUGE and MADV_COLLAPSE.
>> - successfully reset the policy of the process.
>> - after reset, only get hugepages with:
>>    - MADV_COLLAPSE when policy is set to never.
>>    - MADV_HUGE and MADV_COLLAPSE when policy is set to madvise.
>>    - always when policy is set to "always".
>> - repeat the above tests in a forked process to make sure
>>    the policy is carried across forks.
>>
>> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
>> ---
>>   tools/testing/selftests/mm/.gitignore         |   1 +
>>   tools/testing/selftests/mm/Makefile           |   1 +
>>   .../testing/selftests/mm/prctl_thp_disable.c  | 173 ++++++++++++++++++
>>   tools/testing/selftests/mm/thp_settings.c     |   9 +-
>>   tools/testing/selftests/mm/thp_settings.h     |   1 +
>>   5 files changed, 184 insertions(+), 1 deletion(-)
>>   create mode 100644 tools/testing/selftests/mm/prctl_thp_disable.c
>>
>> diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore
>> index e7b23a8a05fe..eb023ea857b3 100644
>> --- a/tools/testing/selftests/mm/.gitignore
>> +++ b/tools/testing/selftests/mm/.gitignore
>> @@ -58,3 +58,4 @@ pkey_sighandler_tests_32
>>   pkey_sighandler_tests_64
>>   guard-regions
>>   merge
>> +prctl_thp_disable
>> diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
>> index d13b3cef2a2b..2bb8d3ebc17c 100644
>> --- a/tools/testing/selftests/mm/Makefile
>> +++ b/tools/testing/selftests/mm/Makefile
>> @@ -86,6 +86,7 @@ TEST_GEN_FILES += on-fault-limit
>>   TEST_GEN_FILES += pagemap_ioctl
>>   TEST_GEN_FILES += pfnmap
>>   TEST_GEN_FILES += process_madv
>> +TEST_GEN_FILES += prctl_thp_disable
>>   TEST_GEN_FILES += thuge-gen
>>   TEST_GEN_FILES += transhuge-stress
>>   TEST_GEN_FILES += uffd-stress
>> diff --git a/tools/testing/selftests/mm/prctl_thp_disable.c b/tools/testing/selftests/mm/prctl_thp_disable.c
>> new file mode 100644
>> index 000000000000..ef150180daf4
>> --- /dev/null
>> +++ b/tools/testing/selftests/mm/prctl_thp_disable.c
>> @@ -0,0 +1,173 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * Basic tests for PR_GET/SET_THP_DISABLE prctl calls
>> + *
>> + * Author(s): Usama Arif <usamaarif642@gmail.com>
>> + */
>> +#include <stdio.h>
>> +#include <stdlib.h>
>> +#include <string.h>
>> +#include <unistd.h>
>> +#include <sys/mman.h>
>> +#include <sys/prctl.h>
>> +#include <sys/wait.h>
>> +
>> +#include "../kselftest_harness.h"
>> +#include "thp_settings.h"
>> +#include "vm_util.h"
>> +
>> +static int sz2ord(size_t size, size_t pagesize)
>> +{
>> +    return __builtin_ctzll(size / pagesize);
>> +}
> 
> Note: We have this helper duplicated elsewhere (e.g., cow.c).
> 
> See [1] how we're going to clean it up.
> 
> Not sure how to resolve this. Probably, which series lands first in mm-unstable should clean it up.
> 
> I would assume that Donet's series would go in first, such that you can just reuse the helper from vm_utils.h
> 
> [1] https://lkml.kernel.org/r/20250804090410.of5xwrlker665bdp@master
> 
> 
> Nothing else jumped at me
> 
> Acked-by: David Hildenbrand <david@redhat.com>
> 

Thanks!

I will wait a few days and see if it has made it into mm-new and rebase and send the next revision.


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely
  2025-08-05 12:13     ` Usama Arif
@ 2025-08-05 12:16       ` David Hildenbrand
  0 siblings, 0 replies; 30+ messages in thread
From: David Hildenbrand @ 2025-08-05 12:16 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team,
	Donet Tom

>>
>> Nothing else jumped at me
>>
>> Acked-by: David Hildenbrand <david@redhat.com>
>>
> 
> Thanks!
> 
> I will wait a few days and see if it has made it into mm-new and rebase and send the next revision.

Will only happen next week, after the merge windows is closed. @Donet, 
are you planning on re-submitting you series?

-- 
Cheers,

David / dhildenb


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 6/6] selftests: prctl: introduce tests for disabling THPs except for madvise
  2025-08-05 10:36   ` David Hildenbrand
@ 2025-08-05 12:19     ` Usama Arif
  2025-08-05 12:29       ` David Hildenbrand
  0 siblings, 1 reply; 30+ messages in thread
From: Usama Arif @ 2025-08-05 12:19 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team



On 05/08/2025 11:36, David Hildenbrand wrote:
> On 04.08.25 17:40, Usama Arif wrote:
>> The test will set the global system THP setting to never, madvise
>> or always depending on the fixture variant and the 2M setting to
>> inherit before it starts (and reset to original at teardown)
>>
>> This tests if the process can:
>> - successfully set and get the policy to disable THPs expect for madvise.
>> - get hugepages only on MADV_HUGE and MADV_COLLAPSE if the global policy
>>    is madvise/always and only with MADV_COLLAPSE if the global policy is
>>    never.
>> - successfully reset the policy of the process.
>> - after reset, only get hugepages with:
>>    - MADV_COLLAPSE when policy is set to never.
>>    - MADV_HUGE and MADV_COLLAPSE when policy is set to madvise.
>>    - always when policy is set to "always".
>> - repeat the above tests in a forked process to make sure  the policy is
>>    carried across forks.
>>
>> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
>> ---
> 
> [...]
> 
>> +FIXTURE_VARIANT(prctl_thp_disable_except_madvise)
>> +{
>> +    enum thp_enabled thp_policy;
>> +};
>> +
>> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, never)
>> +{
>> +    .thp_policy = THP_NEVER,
>> +};
>> +
>> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, madvise)
>> +{
>> +    .thp_policy = THP_MADVISE,
>> +};
>> +
>> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, always)
>> +{
>> +    .thp_policy = THP_ALWAYS,
>> +};
>> +
>> +FIXTURE_SETUP(prctl_thp_disable_except_madvise)
>> +{
>> +    if (!thp_available())
>> +        SKIP(return, "Transparent Hugepages not available\n");
>> +
>> +    self->pmdsize = read_pmd_pagesize();
>> +    if (!self->pmdsize)
>> +        SKIP(return, "Unable to read PMD size\n");
> 
> Should we test here if the kernel knows PR_THP_DISABLE_EXCEPT_ADVISED, and if not, skip?
> 
> Might be as simple as trying issuing two prctl, and making sure the first disabling attempt doesn't fail. If so, SKIP.
> 
> Nothing else jumped at me. Can you include a test run result in the patch description?
> 

Instead of 2 prctls, I think doing just the below should be enough:

diff --git a/tools/testing/selftests/mm/prctl_thp_disable.c b/tools/testing/selftests/mm/prctl_thp_disable.c
index 93cedaa59854..da28bc4441ed 100644
--- a/tools/testing/selftests/mm/prctl_thp_disable.c
+++ b/tools/testing/selftests/mm/prctl_thp_disable.c
@@ -236,6 +236,9 @@ FIXTURE_SETUP(prctl_thp_disable_except_madvise)
        if (!self->pmdsize)
                SKIP(return, "Unable to read PMD size\n");
 
+       if (prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, NULL, NULL))
+               SKIP(return, "Unable to set PR_THP_DISABLE_EXCEPT_ADVISED\n");
+
        thp_save_settings();
        thp_read_settings(&self->settings);
        self->settings.thp_enabled = variant->thp_policy;



Will include the test run result in the last patch description. Just adding it here as well:

./prctl_thp_disable
TAP version 13
1..12
# Starting 12 tests from 6 test cases.
#  RUN           prctl_thp_disable_completely.never.nofork ...
#            OK  prctl_thp_disable_completely.never.nofork
ok 1 prctl_thp_disable_completely.never.nofork
#  RUN           prctl_thp_disable_completely.never.fork ...
#            OK  prctl_thp_disable_completely.never.fork
ok 2 prctl_thp_disable_completely.never.fork
#  RUN           prctl_thp_disable_completely.madvise.nofork ...
#            OK  prctl_thp_disable_completely.madvise.nofork
ok 3 prctl_thp_disable_completely.madvise.nofork
#  RUN           prctl_thp_disable_completely.madvise.fork ...
#            OK  prctl_thp_disable_completely.madvise.fork
ok 4 prctl_thp_disable_completely.madvise.fork
#  RUN           prctl_thp_disable_completely.always.nofork ...
#            OK  prctl_thp_disable_completely.always.nofork
ok 5 prctl_thp_disable_completely.always.nofork
#  RUN           prctl_thp_disable_completely.always.fork ...
#            OK  prctl_thp_disable_completely.always.fork
ok 6 prctl_thp_disable_completely.always.fork
#  RUN           prctl_thp_disable_except_madvise.never.nofork ...
#            OK  prctl_thp_disable_except_madvise.never.nofork
ok 7 prctl_thp_disable_except_madvise.never.nofork
#  RUN           prctl_thp_disable_except_madvise.never.fork ...
#            OK  prctl_thp_disable_except_madvise.never.fork
ok 8 prctl_thp_disable_except_madvise.never.fork
#  RUN           prctl_thp_disable_except_madvise.madvise.nofork ...
#            OK  prctl_thp_disable_except_madvise.madvise.nofork
ok 9 prctl_thp_disable_except_madvise.madvise.nofork
#  RUN           prctl_thp_disable_except_madvise.madvise.fork ...
#            OK  prctl_thp_disable_except_madvise.madvise.fork
ok 10 prctl_thp_disable_except_madvise.madvise.fork
#  RUN           prctl_thp_disable_except_madvise.always.nofork ...
#            OK  prctl_thp_disable_except_madvise.always.nofork
ok 11 prctl_thp_disable_except_madvise.always.nofork
#  RUN           prctl_thp_disable_except_madvise.always.fork ...
#            OK  prctl_thp_disable_except_madvise.always.fork
ok 12 prctl_thp_disable_except_madvise.always.fork
# PASSED: 12 / 12 tests passed.
# Totals: pass:12 fail:0 xfail:0 xpass:0 skip:0 error:0

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 6/6] selftests: prctl: introduce tests for disabling THPs except for madvise
  2025-08-05 12:19     ` Usama Arif
@ 2025-08-05 12:29       ` David Hildenbrand
  2025-08-05 12:31         ` Usama Arif
  0 siblings, 1 reply; 30+ messages in thread
From: David Hildenbrand @ 2025-08-05 12:29 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team

On 05.08.25 14:19, Usama Arif wrote:
> 
> 
> On 05/08/2025 11:36, David Hildenbrand wrote:
>> On 04.08.25 17:40, Usama Arif wrote:
>>> The test will set the global system THP setting to never, madvise
>>> or always depending on the fixture variant and the 2M setting to
>>> inherit before it starts (and reset to original at teardown)
>>>
>>> This tests if the process can:
>>> - successfully set and get the policy to disable THPs expect for madvise.
>>> - get hugepages only on MADV_HUGE and MADV_COLLAPSE if the global policy
>>>     is madvise/always and only with MADV_COLLAPSE if the global policy is
>>>     never.
>>> - successfully reset the policy of the process.
>>> - after reset, only get hugepages with:
>>>     - MADV_COLLAPSE when policy is set to never.
>>>     - MADV_HUGE and MADV_COLLAPSE when policy is set to madvise.
>>>     - always when policy is set to "always".
>>> - repeat the above tests in a forked process to make sure  the policy is
>>>     carried across forks.
>>>
>>> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
>>> ---
>>
>> [...]
>>
>>> +FIXTURE_VARIANT(prctl_thp_disable_except_madvise)
>>> +{
>>> +    enum thp_enabled thp_policy;
>>> +};
>>> +
>>> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, never)
>>> +{
>>> +    .thp_policy = THP_NEVER,
>>> +};
>>> +
>>> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, madvise)
>>> +{
>>> +    .thp_policy = THP_MADVISE,
>>> +};
>>> +
>>> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, always)
>>> +{
>>> +    .thp_policy = THP_ALWAYS,
>>> +};
>>> +
>>> +FIXTURE_SETUP(prctl_thp_disable_except_madvise)
>>> +{
>>> +    if (!thp_available())
>>> +        SKIP(return, "Transparent Hugepages not available\n");
>>> +
>>> +    self->pmdsize = read_pmd_pagesize();
>>> +    if (!self->pmdsize)
>>> +        SKIP(return, "Unable to read PMD size\n");
>>
>> Should we test here if the kernel knows PR_THP_DISABLE_EXCEPT_ADVISED, and if not, skip?
>>
>> Might be as simple as trying issuing two prctl, and making sure the first disabling attempt doesn't fail. If so, SKIP.
>>
>> Nothing else jumped at me. Can you include a test run result in the patch description?
>>
> 
> Instead of 2 prctls, I think doing just the below should be enough:
> 
> diff --git a/tools/testing/selftests/mm/prctl_thp_disable.c b/tools/testing/selftests/mm/prctl_thp_disable.c
> index 93cedaa59854..da28bc4441ed 100644
> --- a/tools/testing/selftests/mm/prctl_thp_disable.c
> +++ b/tools/testing/selftests/mm/prctl_thp_disable.c
> @@ -236,6 +236,9 @@ FIXTURE_SETUP(prctl_thp_disable_except_madvise)
>          if (!self->pmdsize)
>                  SKIP(return, "Unable to read PMD size\n");
>   
> +       if (prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, NULL, NULL))
> +               SKIP(return, "Unable to set PR_THP_DISABLE_EXCEPT_ADVISED\n");
> +
>          thp_save_settings();
>          thp_read_settings(&self->settings);
>          self->settings.thp_enabled = variant->thp_policy;

Then probably best to remove the

ASSERT_EQ(prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, 
NULL, NULL), 0);

 From both test functions?

You can consider doing the same in patch #5.

-- 
Cheers,

David / dhildenb


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 6/6] selftests: prctl: introduce tests for disabling THPs except for madvise
  2025-08-05 12:29       ` David Hildenbrand
@ 2025-08-05 12:31         ` Usama Arif
  0 siblings, 0 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-05 12:31 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team



On 05/08/2025 13:29, David Hildenbrand wrote:
> On 05.08.25 14:19, Usama Arif wrote:
>>
>>
>> On 05/08/2025 11:36, David Hildenbrand wrote:
>>> On 04.08.25 17:40, Usama Arif wrote:
>>>> The test will set the global system THP setting to never, madvise
>>>> or always depending on the fixture variant and the 2M setting to
>>>> inherit before it starts (and reset to original at teardown)
>>>>
>>>> This tests if the process can:
>>>> - successfully set and get the policy to disable THPs expect for madvise.
>>>> - get hugepages only on MADV_HUGE and MADV_COLLAPSE if the global policy
>>>>     is madvise/always and only with MADV_COLLAPSE if the global policy is
>>>>     never.
>>>> - successfully reset the policy of the process.
>>>> - after reset, only get hugepages with:
>>>>     - MADV_COLLAPSE when policy is set to never.
>>>>     - MADV_HUGE and MADV_COLLAPSE when policy is set to madvise.
>>>>     - always when policy is set to "always".
>>>> - repeat the above tests in a forked process to make sure  the policy is
>>>>     carried across forks.
>>>>
>>>> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
>>>> ---
>>>
>>> [...]
>>>
>>>> +FIXTURE_VARIANT(prctl_thp_disable_except_madvise)
>>>> +{
>>>> +    enum thp_enabled thp_policy;
>>>> +};
>>>> +
>>>> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, never)
>>>> +{
>>>> +    .thp_policy = THP_NEVER,
>>>> +};
>>>> +
>>>> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, madvise)
>>>> +{
>>>> +    .thp_policy = THP_MADVISE,
>>>> +};
>>>> +
>>>> +FIXTURE_VARIANT_ADD(prctl_thp_disable_except_madvise, always)
>>>> +{
>>>> +    .thp_policy = THP_ALWAYS,
>>>> +};
>>>> +
>>>> +FIXTURE_SETUP(prctl_thp_disable_except_madvise)
>>>> +{
>>>> +    if (!thp_available())
>>>> +        SKIP(return, "Transparent Hugepages not available\n");
>>>> +
>>>> +    self->pmdsize = read_pmd_pagesize();
>>>> +    if (!self->pmdsize)
>>>> +        SKIP(return, "Unable to read PMD size\n");
>>>
>>> Should we test here if the kernel knows PR_THP_DISABLE_EXCEPT_ADVISED, and if not, skip?
>>>
>>> Might be as simple as trying issuing two prctl, and making sure the first disabling attempt doesn't fail. If so, SKIP.
>>>
>>> Nothing else jumped at me. Can you include a test run result in the patch description?
>>>
>>
>> Instead of 2 prctls, I think doing just the below should be enough:
>>
>> diff --git a/tools/testing/selftests/mm/prctl_thp_disable.c b/tools/testing/selftests/mm/prctl_thp_disable.c
>> index 93cedaa59854..da28bc4441ed 100644
>> --- a/tools/testing/selftests/mm/prctl_thp_disable.c
>> +++ b/tools/testing/selftests/mm/prctl_thp_disable.c
>> @@ -236,6 +236,9 @@ FIXTURE_SETUP(prctl_thp_disable_except_madvise)
>>          if (!self->pmdsize)
>>                  SKIP(return, "Unable to read PMD size\n");
>>   +       if (prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, NULL, NULL))
>> +               SKIP(return, "Unable to set PR_THP_DISABLE_EXCEPT_ADVISED\n");
>> +
>>          thp_save_settings();
>>          thp_read_settings(&self->settings);
>>          self->settings.thp_enabled = variant->thp_policy;
> 
> Then probably best to remove the
> 
> ASSERT_EQ(prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, NULL, NULL), 0);
> 
> From both test functions?
> 
> You can consider doing the same in patch #5.
> 

Yes makes sense, Thanks!

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely
  2025-08-04 15:40 ` [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely Usama Arif
  2025-08-05 10:30   ` David Hildenbrand
@ 2025-08-05 12:39   ` David Hildenbrand
  2025-08-05 12:46     ` Usama Arif
  1 sibling, 1 reply; 30+ messages in thread
From: David Hildenbrand @ 2025-08-05 12:39 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team

> +FIXTURE_SETUP(prctl_thp_disable_completely)
> +{
> +	if (!thp_available())
> +		SKIP(return, "Transparent Hugepages not available\n");
> +
> +	self->pmdsize = read_pmd_pagesize();
> +	if (!self->pmdsize)
> +		SKIP(return, "Unable to read PMD size\n");
> +
> +	thp_save_settings();
> +	thp_read_settings(&self->settings);
> +	self->settings.thp_enabled = variant->thp_policy;
> +	self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;

Oh, one more thing: should we set all other sizes also to THP_INHERIT or 
(for simplicity) THP_NEVER?

-- 
Cheers,

David / dhildenb


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely
  2025-08-05 12:39   ` David Hildenbrand
@ 2025-08-05 12:46     ` Usama Arif
  2025-08-05 12:55       ` David Hildenbrand
  0 siblings, 1 reply; 30+ messages in thread
From: Usama Arif @ 2025-08-05 12:46 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team



On 05/08/2025 13:39, David Hildenbrand wrote:
>> +FIXTURE_SETUP(prctl_thp_disable_completely)
>> +{
>> +    if (!thp_available())
>> +        SKIP(return, "Transparent Hugepages not available\n");
>> +
>> +    self->pmdsize = read_pmd_pagesize();
>> +    if (!self->pmdsize)
>> +        SKIP(return, "Unable to read PMD size\n");
>> +
>> +    thp_save_settings();
>> +    thp_read_settings(&self->settings);
>> +    self->settings.thp_enabled = variant->thp_policy;
>> +    self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;
> 
> Oh, one more thing: should we set all other sizes also to THP_INHERIT or (for simplicity) THP_NEVER?
> 

hmm do we need to? I am hoping that we should always get the PMD size THP no matter what the policy
for others is in the scenario we have?


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely
  2025-08-05 12:46     ` Usama Arif
@ 2025-08-05 12:55       ` David Hildenbrand
  2025-08-05 14:26         ` Usama Arif
  0 siblings, 1 reply; 30+ messages in thread
From: David Hildenbrand @ 2025-08-05 12:55 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team

On 05.08.25 14:46, Usama Arif wrote:
> 
> 
> On 05/08/2025 13:39, David Hildenbrand wrote:
>>> +FIXTURE_SETUP(prctl_thp_disable_completely)
>>> +{
>>> +    if (!thp_available())
>>> +        SKIP(return, "Transparent Hugepages not available\n");
>>> +
>>> +    self->pmdsize = read_pmd_pagesize();
>>> +    if (!self->pmdsize)
>>> +        SKIP(return, "Unable to read PMD size\n");
>>> +
>>> +    thp_save_settings();
>>> +    thp_read_settings(&self->settings);
>>> +    self->settings.thp_enabled = variant->thp_policy;
>>> +    self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;
>>
>> Oh, one more thing: should we set all other sizes also to THP_INHERIT or (for simplicity) THP_NEVER?
>>
> 
> hmm do we need to? I am hoping that we should always get the PMD size THP no matter what the policy
> for others is in the scenario we have?

Assuming 64K is set to "always", couldn't it mislead us in the 
"madvise"/"never" test cases in some scenarios?

-- 
Cheers,

David / dhildenb


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely
  2025-08-05 12:55       ` David Hildenbrand
@ 2025-08-05 14:26         ` Usama Arif
  2025-08-05 14:32           ` David Hildenbrand
  0 siblings, 1 reply; 30+ messages in thread
From: Usama Arif @ 2025-08-05 14:26 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team



On 05/08/2025 13:55, David Hildenbrand wrote:
> On 05.08.25 14:46, Usama Arif wrote:
>>
>>
>> On 05/08/2025 13:39, David Hildenbrand wrote:
>>>> +FIXTURE_SETUP(prctl_thp_disable_completely)
>>>> +{
>>>> +    if (!thp_available())
>>>> +        SKIP(return, "Transparent Hugepages not available\n");
>>>> +
>>>> +    self->pmdsize = read_pmd_pagesize();
>>>> +    if (!self->pmdsize)
>>>> +        SKIP(return, "Unable to read PMD size\n");
>>>> +
>>>> +    thp_save_settings();
>>>> +    thp_read_settings(&self->settings);
>>>> +    self->settings.thp_enabled = variant->thp_policy;
>>>> +    self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;
>>>
>>> Oh, one more thing: should we set all other sizes also to THP_INHERIT or (for simplicity) THP_NEVER?
>>>
>>
>> hmm do we need to? I am hoping that we should always get the PMD size THP no matter what the policy
>> for others is in the scenario we have?
> 
> Assuming 64K is set to "always", couldn't it mislead us in the "madvise"/"never" test cases in some scenarios?
> 

I tried it with 64K to always and seems to be ok. check_huge_anon checks AnonHugepages from smaps which
only indicates the pmd mapped THPs only. So I think should be ok?
Happy to set them to never and the change is probably something simple like below (untested), but
just trying to understand better the need for it.

diff --git a/tools/testing/selftests/mm/prctl_thp_disable.c b/tools/testing/selftests/mm/prctl_thp_disable.c
index 4b072d4ecc30..ee692c397835 100644
--- a/tools/testing/selftests/mm/prctl_thp_disable.c
+++ b/tools/testing/selftests/mm/prctl_thp_disable.c
@@ -142,6 +142,8 @@ FIXTURE_SETUP(prctl_thp_disable_completely)
        thp_save_settings();
        thp_read_settings(&self->settings);
        self->settings.thp_enabled = variant->thp_policy;
+       for (int i = 1; i < sz2ord(self->pmdsize, getpagesize()); i++)
+               self->settings.hugepages[i].enabled = THP_INHERIT;
        self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;
        thp_write_settings(&self->settings);
 }
@@ -244,6 +246,8 @@ FIXTURE_SETUP(prctl_thp_disable_except_madvise)
        thp_save_settings();
        thp_read_settings(&self->settings);
        self->settings.thp_enabled = variant->thp_policy;
+       for (int i = 1; i < sz2ord(self->pmdsize, getpagesize()); i++)
+               self->settings.hugepages[i].enabled = THP_INHERIT;
        self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;
        thp_write_settings(&self->settings);
 }
 


[root@vm7 vmuser]# echo always >  /sys/kernel/mm/transparent_hugepage/hugepages-64kB/enabled                                                                                                                                                                                                                        
[root@vm7 vmuser]# ./prctl_thp_disable                                                                                                                                                                                                                                                                              
TAP version 13                                                                                                                                                                                                                                                                                                      
1..12                                                                                                                                                                                                                                                                                                               
# Starting 12 tests from 6 test cases.                                                                                                                                                                                                                                                                              
#  RUN           prctl_thp_disable_completely.never.nofork ...
#            OK  prctl_thp_disable_completely.never.nofork
ok 1 prctl_thp_disable_completely.never.nofork
#  RUN           prctl_thp_disable_completely.never.fork ...
#            OK  prctl_thp_disable_completely.never.fork
ok 2 prctl_thp_disable_completely.never.fork
#  RUN           prctl_thp_disable_completely.madvise.nofork ...
#            OK  prctl_thp_disable_completely.madvise.nofork
ok 3 prctl_thp_disable_completely.madvise.nofork
#  RUN           prctl_thp_disable_completely.madvise.fork ...
#            OK  prctl_thp_disable_completely.madvise.fork
ok 4 prctl_thp_disable_completely.madvise.fork
#  RUN           prctl_thp_disable_completely.always.nofork ...
#            OK  prctl_thp_disable_completely.always.nofork
ok 5 prctl_thp_disable_completely.always.nofork
#  RUN           prctl_thp_disable_completely.always.fork ...
#            OK  prctl_thp_disable_completely.always.fork
ok 6 prctl_thp_disable_completely.always.fork
#  RUN           prctl_thp_disable_except_madvise.never.nofork ...
#            OK  prctl_thp_disable_except_madvise.never.nofork
ok 7 prctl_thp_disable_except_madvise.never.nofork
#  RUN           prctl_thp_disable_except_madvise.never.fork ...
#            OK  prctl_thp_disable_except_madvise.never.fork
ok 8 prctl_thp_disable_except_madvise.never.fork
#  RUN           prctl_thp_disable_except_madvise.madvise.nofork ...
#            OK  prctl_thp_disable_except_madvise.madvise.nofork
ok 9 prctl_thp_disable_except_madvise.madvise.nofork
#  RUN           prctl_thp_disable_except_madvise.madvise.fork ...
#            OK  prctl_thp_disable_except_madvise.madvise.fork
ok 10 prctl_thp_disable_except_madvise.madvise.fork
#  RUN           prctl_thp_disable_except_madvise.always.nofork ...
#            OK  prctl_thp_disable_except_madvise.always.nofork
ok 11 prctl_thp_disable_except_madvise.always.nofork
#  RUN           prctl_thp_disable_except_madvise.always.fork ...
#            OK  prctl_thp_disable_except_madvise.always.fork
ok 12 prctl_thp_disable_except_madvise.always.fork
# PASSED: 12 / 12 tests passed.
# Totals: pass:12 fail:0 xfail:0 xpass:0 skip:0 error:0

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely
  2025-08-05 14:26         ` Usama Arif
@ 2025-08-05 14:32           ` David Hildenbrand
  0 siblings, 0 replies; 30+ messages in thread
From: David Hildenbrand @ 2025-08-05 14:32 UTC (permalink / raw)
  To: Usama Arif, Andrew Morton, linux-mm
  Cc: linux-fsdevel, corbet, rppt, surenb, mhocko, hannes, baohua,
	shakeel.butt, riel, ziy, laoar.shao, dev.jain, baolin.wang,
	npache, lorenzo.stoakes, Liam.Howlett, ryan.roberts, vbabka,
	jannh, Arnd Bergmann, sj, linux-kernel, linux-doc, kernel-team

On 05.08.25 16:26, Usama Arif wrote:
> 
> 
> On 05/08/2025 13:55, David Hildenbrand wrote:
>> On 05.08.25 14:46, Usama Arif wrote:
>>>
>>>
>>> On 05/08/2025 13:39, David Hildenbrand wrote:
>>>>> +FIXTURE_SETUP(prctl_thp_disable_completely)
>>>>> +{
>>>>> +    if (!thp_available())
>>>>> +        SKIP(return, "Transparent Hugepages not available\n");
>>>>> +
>>>>> +    self->pmdsize = read_pmd_pagesize();
>>>>> +    if (!self->pmdsize)
>>>>> +        SKIP(return, "Unable to read PMD size\n");
>>>>> +
>>>>> +    thp_save_settings();
>>>>> +    thp_read_settings(&self->settings);
>>>>> +    self->settings.thp_enabled = variant->thp_policy;
>>>>> +    self->settings.hugepages[sz2ord(self->pmdsize, getpagesize())].enabled = THP_INHERIT;
>>>>
>>>> Oh, one more thing: should we set all other sizes also to THP_INHERIT or (for simplicity) THP_NEVER?
>>>>
>>>
>>> hmm do we need to? I am hoping that we should always get the PMD size THP no matter what the policy
>>> for others is in the scenario we have?
>>
>> Assuming 64K is set to "always", couldn't it mislead us in the "madvise"/"never" test cases in some scenarios?
>>
> 
> I tried it with 64K to always and seems to be ok. check_huge_anon checks AnonHugepages from smaps which
> only indicates the pmd mapped THPs only. So I think should be ok?

Ah, yes. If that's the case, then we can focus on PMD only and not care 
about how the others are configured.

-- 
Cheers,

David / dhildenb


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 2/6] mm/huge_memory: convert "tva_flags" to "enum tva_type"
  2025-08-04 15:40 ` [PATCH v3 2/6] mm/huge_memory: convert "tva_flags" to "enum tva_type" Usama Arif
  2025-08-05  1:41   ` Baolin Wang
@ 2025-08-05 14:35   ` Lorenzo Stoakes
  1 sibling, 0 replies; 30+ messages in thread
From: Lorenzo Stoakes @ 2025-08-05 14:35 UTC (permalink / raw)
  To: Usama Arif
  Cc: Andrew Morton, david, linux-mm, linux-fsdevel, corbet, rppt,
	surenb, mhocko, hannes, baohua, shakeel.butt, riel, ziy,
	laoar.shao, dev.jain, baolin.wang, npache, Liam.Howlett,
	ryan.roberts, vbabka, jannh, Arnd Bergmann, sj, linux-kernel,
	linux-doc, kernel-team

On Mon, Aug 04, 2025 at 04:40:45PM +0100, Usama Arif wrote:
> From: David Hildenbrand <david@redhat.com>
>
> When determining which THP orders are eligible for a VMA mapping,
> we have previously specified tva_flags, however it turns out it is
> really not necessary to treat these as flags.
>
> Rather, we distinguish between distinct modes.
>
> The only case where we previously combined flags was with
> TVA_ENFORCE_SYSFS, but we can avoid this by observing that this
> is the default, except for MADV_COLLAPSE or an edge cases in
> collapse_pte_mapped_thp() and hugepage_vma_revalidate(), and
> adding a mode specifically for this case - TVA_FORCED_COLLAPSE.
>
> We have:
> * smaps handling for showing "THPeligible"
> * Pagefault handling
> * khugepaged handling
> * Forced collapse handling: primarily MADV_COLLAPSE, but also for
>   an edge case in collapse_pte_mapped_thp()
>
> Disregarding the edge cases, we only want to ignore sysfs settings only
> when we are forcing a collapse through MADV_COLLAPSE, otherwise we
> want to enforce it, hence this patch does the following flag to enum
> conversions:
>
> * TVA_SMAPS | TVA_ENFORCE_SYSFS -> TVA_SMAPS
> * TVA_IN_PF | TVA_ENFORCE_SYSFS -> TVA_PAGEFAULT
> * TVA_ENFORCE_SYSFS             -> TVA_KHUGEPAGED
> * 0                             -> TVA_FORCED_COLLAPSE
>
> With this change, we immediately know if we are in the forced collapse
> case, which will be valuable next.

Thanks for the changes to the commit message, looking good!

>
> Signed-off-by: David Hildenbrand <david@redhat.com>
> Acked-by: Usama Arif <usamaarif642@gmail.com>
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>

LGTM, so:

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> ---
>  fs/proc/task_mmu.c      |  4 ++--
>  include/linux/huge_mm.h | 30 ++++++++++++++++++------------
>  mm/huge_memory.c        |  8 ++++----
>  mm/khugepaged.c         | 17 ++++++++---------
>  mm/memory.c             | 14 ++++++--------
>  5 files changed, 38 insertions(+), 35 deletions(-)
>
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 3d6d8a9f13fc..d440df7b3d59 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -1293,8 +1293,8 @@ static int show_smap(struct seq_file *m, void *v)
>  	__show_smap(m, &mss, false);
>
>  	seq_printf(m, "THPeligible:    %8u\n",
> -		   !!thp_vma_allowable_orders(vma, vma->vm_flags,
> -			   TVA_SMAPS | TVA_ENFORCE_SYSFS, THP_ORDERS_ALL));
> +		   !!thp_vma_allowable_orders(vma, vma->vm_flags, TVA_SMAPS,
> +					      THP_ORDERS_ALL));
>
>  	if (arch_pkeys_enabled())
>  		seq_printf(m, "ProtectionKey:  %8u\n", vma_pkey(vma));
> diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
> index 71db243a002e..bd4f9e6327e0 100644
> --- a/include/linux/huge_mm.h
> +++ b/include/linux/huge_mm.h
> @@ -94,12 +94,15 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr;
>  #define THP_ORDERS_ALL	\
>  	(THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_SPECIAL | THP_ORDERS_ALL_FILE_DEFAULT)
>
> -#define TVA_SMAPS		(1 << 0)	/* Will be used for procfs */
> -#define TVA_IN_PF		(1 << 1)	/* Page fault handler */
> -#define TVA_ENFORCE_SYSFS	(1 << 2)	/* Obey sysfs configuration */
> +enum tva_type {
> +	TVA_SMAPS,		/* Exposing "THPeligible:" in smaps. */
> +	TVA_PAGEFAULT,		/* Serving a page fault. */
> +	TVA_KHUGEPAGED,		/* Khugepaged collapse. */
> +	TVA_FORCED_COLLAPSE,	/* Forced collapse (e.g. MADV_COLLAPSE). */
> +};
>
> -#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, type, order) \
> +	(!!thp_vma_allowable_orders(vma, vm_flags, type, BIT(order)))
>
>  #define split_folio(f) split_folio_to_list(f, NULL)
>
> @@ -264,14 +267,14 @@ 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,
> +					 enum tva_type type,
>  					 unsigned long orders);
>
>  /**
>   * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
>   * @vma:  the vm area to check
>   * @vm_flags: use these vm_flags instead of vma->vm_flags
> - * @tva_flags: Which TVA flags to honour
> + * @type: TVA type
>   * @orders: bitfield of all orders to consider
>   *
>   * Calculates the intersection of the requested hugepage orders and the allowed
> @@ -285,11 +288,14 @@ unsigned long __thp_vma_allowable_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,
> +				       enum tva_type type,
>  				       unsigned long orders)
>  {
> -	/* Optimization to check if required orders are enabled early. */
> -	if ((tva_flags & TVA_ENFORCE_SYSFS) && vma_is_anonymous(vma)) {
> +	/*
> +	 * Optimization to check if required orders are enabled early. Only
> +	 * forced collapse ignores sysfs configs.
> +	 */
> +	if (type != TVA_FORCED_COLLAPSE && vma_is_anonymous(vma)) {
>  		unsigned long mask = READ_ONCE(huge_anon_orders_always);
>
>  		if (vm_flags & VM_HUGEPAGE)
> @@ -303,7 +309,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, type, orders);
>  }
>
>  struct thpsize {
> @@ -536,7 +542,7 @@ 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,
> +					enum tva_type type,
>  					unsigned long orders)
>  {
>  	return 0;
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 2b4ea5a2ce7d..85252b468f80 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -99,12 +99,12 @@ 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,
> +					 enum tva_type type,
>  					 unsigned long orders)
>  {
> -	bool smaps = tva_flags & TVA_SMAPS;
> -	bool in_pf = tva_flags & TVA_IN_PF;
> -	bool enforce_sysfs = tva_flags & TVA_ENFORCE_SYSFS;
> +	const bool smaps = type == TVA_SMAPS;
> +	const bool in_pf = type == TVA_PAGEFAULT;
> +	const bool enforce_sysfs = type != TVA_FORCED_COLLAPSE;
>  	unsigned long supported_orders;
>
>  	/* Check the intersection of requested and supported orders. */
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 2c9008246785..88cb6339e910 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -474,8 +474,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))
> +		if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
>  			__khugepaged_enter(vma->vm_mm);
>  	}
>  }
> @@ -921,7 +920,8 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
>  				   struct collapse_control *cc)
>  {
>  	struct vm_area_struct *vma;
> -	unsigned long tva_flags = cc->is_khugepaged ? TVA_ENFORCE_SYSFS : 0;
> +	enum tva_type type = cc->is_khugepaged ? TVA_KHUGEPAGED :
> +				 TVA_FORCED_COLLAPSE;
>
>  	if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
>  		return SCAN_ANY_PROCESS;
> @@ -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, type, PMD_ORDER))
>  		return SCAN_VMA_CHECK;
>  	/*
>  	 * Anon VMA expected, the address may be unmapped then
> @@ -1532,9 +1532,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
>  	 * in the page cache with a single hugepage. If a mm were to fault-in
>  	 * this memory (mapped by a suitably aligned VMA), we'd get the hugepage
>  	 * and map it by a PMD, regardless of sysfs THP settings. As such, let's
> -	 * analogously elide sysfs THP settings here.
> +	 * analogously elide sysfs THP settings here and force collapse.
>  	 */
> -	if (!thp_vma_allowable_order(vma, vma->vm_flags, 0, PMD_ORDER))
> +	if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
>  		return SCAN_VMA_CHECK;
>
>  	/* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
> @@ -2431,8 +2431,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
>  			progress++;
>  			break;
>  		}
> -		if (!thp_vma_allowable_order(vma, vma->vm_flags,
> -					TVA_ENFORCE_SYSFS, PMD_ORDER)) {
> +		if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER)) {
>  skip:
>  			progress++;
>  			continue;
> @@ -2766,7 +2765,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, TVA_FORCED_COLLAPSE, PMD_ORDER))
>  		return -EINVAL;
>
>  	cc = kmalloc(sizeof(*cc), GFP_KERNEL);
> diff --git a/mm/memory.c b/mm/memory.c
> index 92fd18a5d8d1..be761753f240 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4369,8 +4369,8 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
>  	 * Get a list of all the (large) orders below PMD_ORDER that are enabled
>  	 * and suitable for swapping THP.
>  	 */
> -	orders = thp_vma_allowable_orders(vma, vma->vm_flags,
> -			TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1);
> +	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_PAGEFAULT,
> +					  BIT(PMD_ORDER) - 1);
>  	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
>  	orders = thp_swap_suitable_orders(swp_offset(entry),
>  					  vmf->address, orders);
> @@ -4917,8 +4917,8 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
>  	 * for this vma. Then filter out the orders that can't be allocated over
>  	 * 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);
> +	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_PAGEFAULT,
> +					  BIT(PMD_ORDER) - 1);
>  	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
>
>  	if (!orders)
> @@ -6108,8 +6108,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
>  		return VM_FAULT_OOM;
>  retry_pud:
>  	if (pud_none(*vmf.pud) &&
> -	    thp_vma_allowable_order(vma, vm_flags,
> -				TVA_IN_PF | TVA_ENFORCE_SYSFS, PUD_ORDER)) {
> +	    thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PUD_ORDER)) {
>  		ret = create_huge_pud(&vmf);
>  		if (!(ret & VM_FAULT_FALLBACK))
>  			return ret;
> @@ -6143,8 +6142,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
>  		goto retry_pud;
>
>  	if (pmd_none(*vmf.pmd) &&
> -	    thp_vma_allowable_order(vma, vm_flags,
> -				TVA_IN_PF | TVA_ENFORCE_SYSFS, PMD_ORDER)) {
> +	    thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
>  		ret = create_huge_pmd(&vmf);
>  		if (!(ret & VM_FAULT_FALLBACK))
>  			return ret;
> --
> 2.47.3
>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 3/6] mm/huge_memory: respect MADV_COLLAPSE with PR_THP_DISABLE_EXCEPT_ADVISED
  2025-08-04 15:40 ` [PATCH v3 3/6] mm/huge_memory: respect MADV_COLLAPSE with PR_THP_DISABLE_EXCEPT_ADVISED Usama Arif
  2025-08-05  1:49   ` Baolin Wang
@ 2025-08-05 14:43   ` Lorenzo Stoakes
  2025-08-05 15:08     ` Usama Arif
  1 sibling, 1 reply; 30+ messages in thread
From: Lorenzo Stoakes @ 2025-08-05 14:43 UTC (permalink / raw)
  To: Usama Arif
  Cc: Andrew Morton, david, linux-mm, linux-fsdevel, corbet, rppt,
	surenb, mhocko, hannes, baohua, shakeel.butt, riel, ziy,
	laoar.shao, dev.jain, baolin.wang, npache, Liam.Howlett,
	ryan.roberts, vbabka, jannh, Arnd Bergmann, sj, linux-kernel,
	linux-doc, kernel-team

On Mon, Aug 04, 2025 at 04:40:46PM +0100, Usama Arif wrote:
> From: David Hildenbrand <david@redhat.com>
>
> Let's allow for making MADV_COLLAPSE succeed on areas that neither have
> VM_HUGEPAGE nor VM_NOHUGEPAGE when we have THP disabled
> unless explicitly advised (PR_THP_DISABLE_EXCEPT_ADVISED).
>
> MADV_COLLAPSE is a clear advice that we want to collapse.
>
> Note that we still respect the VM_NOHUGEPAGE flag, just like
> MADV_COLLAPSE always does. So consequently, MADV_COLLAPSE is now only
> refused on VM_NOHUGEPAGE with PR_THP_DISABLE_EXCEPT_ADVISED,
> including for shmem.

Hm feels like 'including for shmem' is a bit brief here :)

But fine probably ok.

>
> Co-developed-by: Usama Arif <usamaarif642@gmail.com>
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> Signed-off-by: David Hildenbrand <david@redhat.com>

LGTM, so:

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> ---
>  include/linux/huge_mm.h    | 8 +++++++-
>  include/uapi/linux/prctl.h | 2 +-
>  mm/huge_memory.c           | 5 +++--
>  mm/memory.c                | 6 ++++--
>  mm/shmem.c                 | 2 +-
>  5 files changed, 16 insertions(+), 7 deletions(-)
>
> diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
> index bd4f9e6327e0..1fd06ecbde72 100644
> --- a/include/linux/huge_mm.h
> +++ b/include/linux/huge_mm.h
> @@ -329,7 +329,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 forced_collapse)
>  {
>  	/* Are THPs disabled for this VMA? */
>  	if (vm_flags & VM_NOHUGEPAGE)
> @@ -343,6 +343,12 @@ static inline bool vma_thp_disabled(struct vm_area_struct *vma,
>  	 */
>  	if (vm_flags & VM_HUGEPAGE)
>  		return false;
> +	/*
> +	 * Forcing a collapse (e.g., madv_collapse), is a clear advice to
> +	 * use THPs.
> +	 */
> +	if (forced_collapse)
> +		return false;
>  	return test_bit(MMF_DISABLE_THP_EXCEPT_ADVISED, &vma->vm_mm->flags);
>  }
>
> diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
> index 9c1d6e49b8a9..cdda963a039a 100644
> --- a/include/uapi/linux/prctl.h
> +++ b/include/uapi/linux/prctl.h
> @@ -185,7 +185,7 @@ struct prctl_mm_map {
>  #define PR_SET_THP_DISABLE	41
>  /*
>   * Don't disable THPs when explicitly advised (e.g., MADV_HUGEPAGE /
> - * VM_HUGEPAGE).
> + * VM_HUGEPAGE, MADV_COLLAPSE).
>   */
>  # define PR_THP_DISABLE_EXCEPT_ADVISED	(1 << 1)
>  #define PR_GET_THP_DISABLE	42
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 85252b468f80..ef5ccb0ec5d5 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -104,7 +104,8 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
>  {
>  	const bool smaps = type == TVA_SMAPS;
>  	const bool in_pf = type == TVA_PAGEFAULT;
> -	const bool enforce_sysfs = type != TVA_FORCED_COLLAPSE;
> +	const bool forced_collapse = type == TVA_FORCED_COLLAPSE;
> +	const bool enforce_sysfs = !forced_collapse;

I guess as discussed we'll return to this.

>  	unsigned long supported_orders;
>
>  	/* Check the intersection of requested and supported orders. */
> @@ -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, forced_collapse))
>  		return 0;
>
>  	/* khugepaged doesn't collapse DAX vma, but page fault is fine. */
> diff --git a/mm/memory.c b/mm/memory.c
> index be761753f240..bd04212d6f79 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -5186,9 +5186,11 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
>  	 * It is too late to allocate a small folio, we already have a large
>  	 * folio in the pagecache: especially s390 KVM cannot tolerate any
>  	 * PMD mappings, but PTE-mapped THP are fine. So let's simply refuse any
> -	 * PMD mappings if THPs are disabled.
> +	 * PMD mappings if THPs are disabled. As we already have a THP ...
> +	 * behave as if we are forcing a collapse.
>  	 */
> -	if (thp_disabled_by_hw() || vma_thp_disabled(vma, vma->vm_flags))
> +	if (thp_disabled_by_hw() || vma_thp_disabled(vma, vma->vm_flags,
> +						     /* forced_collapse=*/ true))
>  		return ret;
>
>  	if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER))
> diff --git a/mm/shmem.c b/mm/shmem.c
> index e6cdfda08aed..30609197a266 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -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, shmem_huge_force)))
>  		return 0;
>
>  	global_orders = shmem_huge_global_enabled(inode, index, write_end,
> --
> 2.47.3
>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 4/6] docs: transhuge: document process level THP controls
  2025-08-04 15:40 ` [PATCH v3 4/6] docs: transhuge: document process level THP controls Usama Arif
  2025-08-05 10:24   ` David Hildenbrand
@ 2025-08-05 15:08   ` Lorenzo Stoakes
  1 sibling, 0 replies; 30+ messages in thread
From: Lorenzo Stoakes @ 2025-08-05 15:08 UTC (permalink / raw)
  To: Usama Arif
  Cc: Andrew Morton, david, linux-mm, linux-fsdevel, corbet, rppt,
	surenb, mhocko, hannes, baohua, shakeel.butt, riel, ziy,
	laoar.shao, dev.jain, baolin.wang, npache, Liam.Howlett,
	ryan.roberts, vbabka, jannh, Arnd Bergmann, sj, linux-kernel,
	linux-doc, kernel-team

On Mon, Aug 04, 2025 at 04:40:47PM +0100, Usama Arif wrote:
> This includes the PR_SET_THP_DISABLE/PR_GET_THP_DISABLE pair of
> prctl calls as well the newly introduced PR_THP_DISABLE_EXCEPT_ADVISED
> flag for the PR_SET_THP_DISABLE prctl call.
>
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> ---
>  Documentation/admin-guide/mm/transhuge.rst | 38 ++++++++++++++++++++++
>  1 file changed, 38 insertions(+)
>
> diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/admin-guide/mm/transhuge.rst
> index 370fba113460..a36a04394ff5 100644
> --- a/Documentation/admin-guide/mm/transhuge.rst
> +++ b/Documentation/admin-guide/mm/transhuge.rst
> @@ -225,6 +225,44 @@ to "always" or "madvise"), and it'll be automatically shutdown when
>  PMD-sized THP is disabled (when both the per-size anon control and the
>  top-level control are "never")
>
> +process THP controls
> +--------------------
> +
> +A process can control its own THP behaviour using the ``PR_SET_THP_DISABLE``
> +and ``PR_GET_THP_DISABLE`` pair of prctl(2) calls. These calls support the
> +following arguments::
> +
> +	prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0):
> +		This will set the MMF_DISABLE_THP_COMPLETELY mm flag which will

I'm not sure these impl details are necessary, this is an admin guide doc.

> +		result in no THPs being faulted in or collapsed, irrespective
> +		of global THP controls. This flag and hence the behaviour is

Also irrespective of MADV_COLLAPSE right?

Important to highlight that, as it's the only way to disable that.

> +		inherited across fork(2) and execve(2).

I'd remove the 'this flag' bit here.

> +
> +	prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, 0, 0):
> +		This will set the MMF_DISABLE_THP_EXCEPT_ADVISED mm flag which

No need to refer to implementation detail of mm flag.

> +		will result in THPs being faulted in or collapsed only for
> +		the following cases:
> +		- Global THP controls are set to "always" or "madvise" and
> +		  the process has madvised the region with either MADV_HUGEPAGE
> +		  or MADV_COLLAPSE.
> +		- Global THP controls is set to "never" and the process has
> +		  madvised the region with MADV_COLLAPSE.

Nit, but prefer madvise()'d, or really 'used madvise() with the MADV_COLLAPSE
flag over the region'.

> +		This flag and hence the behaviour is inherited across fork(2)
> +		and execve(2).

Again drop 'this flag'.

> +
> +	prctl(PR_SET_THP_DISABLE, 0, 0, 0, 0):
> +		This will clear the MMF_DISABLE_THP_COMPLETELY and
> +		MMF_DISABLE_THP_EXCEPT_ADVISED mm flags. The process will

Remove impl details.

> +		behave according to the global THP controls. This behaviour
> +		will be inherited across fork(2) and execve(2).

Something like 'this will revoke any specified THP disable behaviour and the
process will behave normally with respect to THP. As with the other THP disable
flags, this change is inherited across fork/exec.'

> +
> +	prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0):
> +		This will return the THP disable mm flag status of the process
> +		that was set by prctl(PR_SET_THP_DISABLE, ...). i.e.
> +		- 1 if MMF_DISABLE_THP_COMPLETELY flag is set

This is incorrect, this is set if either flag is set.

Again, remove impl details.

> +		- 3 if MMF_DISABLE_THP_EXCEPT_ADVISED flag is set
> +		- 0 otherwise.

This is really not clear, people are going to wonder why it's randomly 3.

Maybe something like:

This returns a value whose bit indicate how THP-disable is configured:

    Bits
     1 0  Value  Description
    |0|0|   0    No THP-disable behaviour specified.
    |0|1|   1    THP is entirely disabled for this process.
    |1|1|   3    THP-except-advised mode is set for this process.

> +
>  Khugepaged controls
>  -------------------
>
> --
> 2.47.3
>

Cheers, Lorenzo

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 3/6] mm/huge_memory: respect MADV_COLLAPSE with PR_THP_DISABLE_EXCEPT_ADVISED
  2025-08-05 14:43   ` Lorenzo Stoakes
@ 2025-08-05 15:08     ` Usama Arif
  0 siblings, 0 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-05 15:08 UTC (permalink / raw)
  To: Lorenzo Stoakes
  Cc: Andrew Morton, david, linux-mm, linux-fsdevel, corbet, rppt,
	surenb, mhocko, hannes, baohua, shakeel.butt, riel, ziy,
	laoar.shao, dev.jain, baolin.wang, npache, Liam.Howlett,
	ryan.roberts, vbabka, jannh, Arnd Bergmann, sj, linux-kernel,
	linux-doc, kernel-team


>> diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
>> index 9c1d6e49b8a9..cdda963a039a 100644
>> --- a/include/uapi/linux/prctl.h
>> +++ b/include/uapi/linux/prctl.h
>> @@ -185,7 +185,7 @@ struct prctl_mm_map {
>>  #define PR_SET_THP_DISABLE	41
>>  /*
>>   * Don't disable THPs when explicitly advised (e.g., MADV_HUGEPAGE /
>> - * VM_HUGEPAGE).
>> + * VM_HUGEPAGE, MADV_COLLAPSE).
>>   */
>>  # define PR_THP_DISABLE_EXCEPT_ADVISED	(1 << 1)
>>  #define PR_GET_THP_DISABLE	42
>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
>> index 85252b468f80..ef5ccb0ec5d5 100644
>> --- a/mm/huge_memory.c
>> +++ b/mm/huge_memory.c
>> @@ -104,7 +104,8 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
>>  {
>>  	const bool smaps = type == TVA_SMAPS;
>>  	const bool in_pf = type == TVA_PAGEFAULT;
>> -	const bool enforce_sysfs = type != TVA_FORCED_COLLAPSE;
>> +	const bool forced_collapse = type == TVA_FORCED_COLLAPSE;
>> +	const bool enforce_sysfs = !forced_collapse;
> 
> I guess as discussed we'll return to this.
> 

Thanks for the review!

Yes I have a follow up patch ready, will send that when things stabalize and this series
makes into mm-new.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised
  2025-08-04 15:40 [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
                   ` (6 preceding siblings ...)
  2025-08-04 15:46 ` [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
@ 2025-08-13  6:06 ` Lorenzo Stoakes
  2025-08-13  8:07   ` Usama Arif
  7 siblings, 1 reply; 30+ messages in thread
From: Lorenzo Stoakes @ 2025-08-13  6:06 UTC (permalink / raw)
  To: Usama Arif
  Cc: Andrew Morton, david, linux-mm, linux-fsdevel, corbet, rppt,
	surenb, mhocko, hannes, baohua, shakeel.butt, riel, ziy,
	laoar.shao, dev.jain, baolin.wang, npache, Liam.Howlett,
	ryan.roberts, vbabka, jannh, Arnd Bergmann, sj, linux-kernel,
	linux-doc, kernel-team

Usama - did we plan another respin here? I ask as not in mm-new.

Also heads up, my mm flags series will break this one, so if you're
respinning, please make sure to use the mm flag helpers described in [0].

It's really simple, you just do:

mm_flags_test(MMF_xxx, mm) instead of test_bit(MMF_xxx, &mm->flags)
mm_flags_set(MMF_xxx, mm) instead of set_bit(MMF_xxx, &mm->flags)
mm_flags_clear(MMF_xxx, mm) instead of clear_bit(MMF_xxx, &mm->flags)

So should be very quick to fixup.

Sorry about that, but should be super simple to sort out.

Cheers, Lorenzo

[0]: https://lore.kernel.org/linux-mm/cover.1755012943.git.lorenzo.stoakes@oracle.com/

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised
  2025-08-13  6:06 ` Lorenzo Stoakes
@ 2025-08-13  8:07   ` Usama Arif
  0 siblings, 0 replies; 30+ messages in thread
From: Usama Arif @ 2025-08-13  8:07 UTC (permalink / raw)
  To: Lorenzo Stoakes
  Cc: Andrew Morton, david, linux-mm, linux-fsdevel, corbet, rppt,
	surenb, mhocko, hannes, baohua, shakeel.butt, riel, ziy,
	laoar.shao, dev.jain, baolin.wang, npache, Liam.Howlett,
	ryan.roberts, vbabka, jannh, Arnd Bergmann, sj, linux-kernel,
	linux-doc, kernel-team



On 13/08/2025 07:06, Lorenzo Stoakes wrote:
> Usama - did we plan another respin here? I ask as not in mm-new.
> 

Yes, I have the changes ready since last week, was just waiting for a respin of
the selftest cleanup series that David mentioned in [1], but I dont see it
in the mailing list. I will just do the cleanup in my series and send it.


> Also heads up, my mm flags series will break this one, so if you're
> respinning, please make sure to use the mm flag helpers described in [0].

Sounds good, Thanks for the heads up!

I do see this in mm-new, so will just send the next revision today tested
on latest mm-new.


> 
> It's really simple, you just do:
> 
> mm_flags_test(MMF_xxx, mm) instead of test_bit(MMF_xxx, &mm->flags)
> mm_flags_set(MMF_xxx, mm) instead of set_bit(MMF_xxx, &mm->flags)
> mm_flags_clear(MMF_xxx, mm) instead of clear_bit(MMF_xxx, &mm->flags)
> 
> So should be very quick to fixup.
> 
> Sorry about that, but should be super simple to sort out.
> 
> Cheers, Lorenzo
> 
> [0]: https://lore.kernel.org/linux-mm/cover.1755012943.git.lorenzo.stoakes@oracle.com/
[1] https://lore.kernel.org/all/eec7e868-a61f-41ed-a8ef-7ff80548089f@redhat.com/



^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2025-08-13  8:07 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-04 15:40 [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
2025-08-04 15:40 ` [PATCH v3 1/6] prctl: extend PR_SET_THP_DISABLE to optionally exclude VM_HUGEPAGE Usama Arif
2025-08-04 15:40 ` [PATCH v3 2/6] mm/huge_memory: convert "tva_flags" to "enum tva_type" Usama Arif
2025-08-05  1:41   ` Baolin Wang
2025-08-05 14:35   ` Lorenzo Stoakes
2025-08-04 15:40 ` [PATCH v3 3/6] mm/huge_memory: respect MADV_COLLAPSE with PR_THP_DISABLE_EXCEPT_ADVISED Usama Arif
2025-08-05  1:49   ` Baolin Wang
2025-08-05 14:43   ` Lorenzo Stoakes
2025-08-05 15:08     ` Usama Arif
2025-08-04 15:40 ` [PATCH v3 4/6] docs: transhuge: document process level THP controls Usama Arif
2025-08-05 10:24   ` David Hildenbrand
2025-08-05 12:09     ` Usama Arif
2025-08-05 15:08   ` Lorenzo Stoakes
2025-08-04 15:40 ` [PATCH v3 5/6] selftests: prctl: introduce tests for disabling THPs completely Usama Arif
2025-08-05 10:30   ` David Hildenbrand
2025-08-05 12:13     ` Usama Arif
2025-08-05 12:16       ` David Hildenbrand
2025-08-05 12:39   ` David Hildenbrand
2025-08-05 12:46     ` Usama Arif
2025-08-05 12:55       ` David Hildenbrand
2025-08-05 14:26         ` Usama Arif
2025-08-05 14:32           ` David Hildenbrand
2025-08-04 15:40 ` [PATCH v3 6/6] selftests: prctl: introduce tests for disabling THPs except for madvise Usama Arif
2025-08-05 10:36   ` David Hildenbrand
2025-08-05 12:19     ` Usama Arif
2025-08-05 12:29       ` David Hildenbrand
2025-08-05 12:31         ` Usama Arif
2025-08-04 15:46 ` [PATCH v3 0/6] prctl: extend PR_SET_THP_DISABLE to only provide THPs when advised Usama Arif
2025-08-13  6:06 ` Lorenzo Stoakes
2025-08-13  8:07   ` Usama Arif

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).