* [PATCH v5 01/19] selftests/mm: raise the khugepaged test-case cap
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-09 7:42 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 02/19] selftests/mm: skip collapse_compound_extreme() where the PMD is too large Kiryl Shutsemau
` (18 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
TEST() ends the run with "MAX_TEST_CASES is too small" when the table
fills, and the table holds 64. A full invocation already registers 63, so
the next case added anywhere aborts the whole suite before a single test
runs.
Raise the cap to 256. The table is a static array of small structs, so the
room costs nothing worth counting.
Assisted-by: LLM
Acked-by: Usama Arif <usama.arif@linux.dev>
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index f82673f5f6b4..e57016bd96fb 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -1285,7 +1285,7 @@ struct test_case {
test_fn fn;
};
-#define MAX_TEST_CASES 64
+#define MAX_TEST_CASES 256
static struct test_case test_cases[MAX_TEST_CASES];
static int nr_test_cases;
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 01/19] selftests/mm: raise the khugepaged test-case cap
2026-09-08 12:50 ` [PATCH v5 01/19] selftests/mm: raise the khugepaged test-case cap Kiryl Shutsemau
@ 2026-09-09 7:42 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-09 7:42 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> TEST() ends the run with "MAX_TEST_CASES is too small" when the table
> fills, and the table holds 64. A full invocation already registers 63, so
> the next case added anywhere aborts the whole suite before a single test
> runs.
>
> Raise the cap to 256. The table is a static array of small structs, so the
> room costs nothing worth counting.
>
> Assisted-by: LLM
> Acked-by: Usama Arif <usama.arif@linux.dev>
> Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 02/19] selftests/mm: skip collapse_compound_extreme() where the PMD is too large
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
2026-09-08 12:50 ` [PATCH v5 01/19] selftests/mm: raise the khugepaged test-case cap Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-09 7:51 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 03/19] selftests/mm: scale khugepaged's collapse wait with the PMD size Kiryl Shutsemau
` (17 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
collapse_compound_extreme() builds a PTE table full of distinct PTE-mapped
compound pages by cycling hpage_pmd_nr fault-time THPs through mremap. It
therefore needs hpage_pmd_nr PMD-order allocations in a row. That is fine
at a 2M PMD (4K base pages) or a 32M one (16K). A 512M PMD -- arm64 with
64K base pages -- makes each of those an order-13 allocation, which the
allocator cannot reliably hand out even once, let alone 8192 times.
The failure is not a quiet one: the case calls ksft_exit_fail_msg(), so the
whole binary stops and every case after it is lost.
Skip the case where the PMD is larger than 32M. The MADV_COLLAPSE cases
still cover PMD-order collapse on those configurations, and 4K and 16K
PMDs are unaffected.
Assisted-by: LLM
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index e57016bd96fb..1ca7c6978571 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -935,6 +935,16 @@ static void collapse_compound_extreme(struct collapse_context *c, struct mem_ops
void *p;
int i;
+ /*
+ * This needs hpage_pmd_nr PMD-order allocations in a row, which the
+ * allocator will not supply if the PMD is very large.
+ */
+ if (hpage_pmd_size > (32UL << 20)) {
+ ksft_test_result_skip("%s: PMD too large for fault-time THP construction\n",
+ __func__);
+ return;
+ }
+
p = ops->setup_area(1);
ksft_print_msg("Construct PTE page table full of different PTE-mapped compound pages\n");
for (i = 0; i < hpage_pmd_nr; i++) {
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 02/19] selftests/mm: skip collapse_compound_extreme() where the PMD is too large
2026-09-08 12:50 ` [PATCH v5 02/19] selftests/mm: skip collapse_compound_extreme() where the PMD is too large Kiryl Shutsemau
@ 2026-09-09 7:51 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-09 7:51 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> collapse_compound_extreme() builds a PTE table full of distinct PTE-mapped
> compound pages by cycling hpage_pmd_nr fault-time THPs through mremap. It
> therefore needs hpage_pmd_nr PMD-order allocations in a row. That is fine
> at a 2M PMD (4K base pages) or a 32M one (16K). A 512M PMD -- arm64 with
> 64K base pages -- makes each of those an order-13 allocation, which the
> allocator cannot reliably hand out even once, let alone 8192 times.
>
> The failure is not a quiet one: the case calls ksft_exit_fail_msg(), so the
> whole binary stops and every case after it is lost.
>
> Skip the case where the PMD is larger than 32M. The MADV_COLLAPSE cases
> still cover PMD-order collapse on those configurations, and 4K and 16K
> PMDs are unaffected.
>
> Assisted-by: LLM
> Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 03/19] selftests/mm: scale khugepaged's collapse wait with the PMD size
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
2026-09-08 12:50 ` [PATCH v5 01/19] selftests/mm: raise the khugepaged test-case cap Kiryl Shutsemau
2026-09-08 12:50 ` [PATCH v5 02/19] selftests/mm: skip collapse_compound_extreme() where the PMD is too large Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-09 7:59 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 04/19] selftests/mm: skip khugepaged page cache cases without a PMD folio Kiryl Shutsemau
` (16 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
wait_for_scan() gives every case the same three seconds, whatever the huge
page costs to build. collapse_full() asks for four of them: 8M at a 2M
PMD, but 2G at a 512M PMD -- arm64 with 64K base pages. Three seconds is
thin at that size, and the case has reported a failure for a collapse that
was still going.
The timeout is a ceiling on a poll loop, not a sleep: the loop stops as
soon as ops->check_huge() sees the collapse, or as soon as full_scans has
advanced by two. Raising it costs a passing case nothing. Across 80 runs
of collapse_full() on arm64 with 64K pages the wait was half a second in
73 of them, with a tail to two seconds.
Keep three seconds as the floor and add a second per 128M collapsed. A 2M
PMD is unchanged, so x86-64 is too; a 512M PMD gets 19 seconds.
On arm64 with 64K pages a passing ./khugepaged all:anon takes 49 seconds
under TCG before and after this change.
Assisted-by: LLM
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 1ca7c6978571..48e0040d53b4 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -556,8 +556,11 @@ static bool wait_for_scan(const char *msg, char *p, size_t len,
int nr_hpages, int collap_order, struct mem_ops *ops)
{
unsigned long hpage_size = page_size << collap_order;
- int full_scans;
- int timeout = 6; /* 3 seconds */
+ unsigned long bytes = (unsigned long)nr_hpages * hpage_size;
+ int timeout, full_scans;
+
+ /* Half-second ticks: three seconds floor, plus a second per 128M */
+ timeout = 6 + 2 * (bytes / (128UL << 20));
/* Sanity check */
if (!ops->check_huge(p, len, 0, hpage_size))
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 03/19] selftests/mm: scale khugepaged's collapse wait with the PMD size
2026-09-08 12:50 ` [PATCH v5 03/19] selftests/mm: scale khugepaged's collapse wait with the PMD size Kiryl Shutsemau
@ 2026-09-09 7:59 ` Baolin Wang
2026-09-09 10:09 ` Kiryl Shutsemau
0 siblings, 1 reply; 48+ messages in thread
From: Baolin Wang @ 2026-09-09 7:59 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> wait_for_scan() gives every case the same three seconds, whatever the huge
> page costs to build. collapse_full() asks for four of them: 8M at a 2M
> PMD, but 2G at a 512M PMD -- arm64 with 64K base pages. Three seconds is
> thin at that size, and the case has reported a failure for a collapse that
> was still going.
>
> The timeout is a ceiling on a poll loop, not a sleep: the loop stops as
> soon as ops->check_huge() sees the collapse, or as soon as full_scans has
> advanced by two. Raising it costs a passing case nothing. Across 80 runs
> of collapse_full() on arm64 with 64K pages the wait was half a second in
> 73 of them, with a tail to two seconds.
>
> Keep three seconds as the floor and add a second per 128M collapsed. A 2M
> PMD is unchanged, so x86-64 is too; a 512M PMD gets 19 seconds.
>
> On arm64 with 64K pages a passing ./khugepaged all:anon takes 49 seconds
> under TCG before and after this change.
>
> Assisted-by: LLM
> Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
> tools/testing/selftests/mm/khugepaged.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> index 1ca7c6978571..48e0040d53b4 100644
> --- a/tools/testing/selftests/mm/khugepaged.c
> +++ b/tools/testing/selftests/mm/khugepaged.c
> @@ -556,8 +556,11 @@ static bool wait_for_scan(const char *msg, char *p, size_t len,
> int nr_hpages, int collap_order, struct mem_ops *ops)
> {
> unsigned long hpage_size = page_size << collap_order;
> - int full_scans;
> - int timeout = 6; /* 3 seconds */
> + unsigned long bytes = (unsigned long)nr_hpages * hpage_size;
We already pass in the 'len' parameter, and its size is also 'nr_hpages
* hpage_size", so you can drop the 'bytes' variable. With that,
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
> + int timeout, full_scans;
> +
> + /* Half-second ticks: three seconds floor, plus a second per 128M */
> + timeout = 6 + 2 * (bytes / (128UL << 20));
>
> /* Sanity check */
> if (!ops->check_huge(p, len, 0, hpage_size))
^ permalink raw reply [flat|nested] 48+ messages in thread* Re: [PATCH v5 03/19] selftests/mm: scale khugepaged's collapse wait with the PMD size
2026-09-09 7:59 ` Baolin Wang
@ 2026-09-09 10:09 ` Kiryl Shutsemau
2026-09-09 10:17 ` Baolin Wang
0 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-09 10:09 UTC (permalink / raw)
To: Baolin Wang
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On Wed, Sep 09, 2026 at 03:59:37PM +0800, Baolin Wang wrote:
>
>
> On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> > From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
> >
> > wait_for_scan() gives every case the same three seconds, whatever the huge
> > page costs to build. collapse_full() asks for four of them: 8M at a 2M
> > PMD, but 2G at a 512M PMD -- arm64 with 64K base pages. Three seconds is
> > thin at that size, and the case has reported a failure for a collapse that
> > was still going.
> >
> > The timeout is a ceiling on a poll loop, not a sleep: the loop stops as
> > soon as ops->check_huge() sees the collapse, or as soon as full_scans has
> > advanced by two. Raising it costs a passing case nothing. Across 80 runs
> > of collapse_full() on arm64 with 64K pages the wait was half a second in
> > 73 of them, with a tail to two seconds.
> >
> > Keep three seconds as the floor and add a second per 128M collapsed. A 2M
> > PMD is unchanged, so x86-64 is too; a 512M PMD gets 19 seconds.
> >
> > On arm64 with 64K pages a passing ./khugepaged all:anon takes 49 seconds
> > under TCG before and after this change.
> >
> > Assisted-by: LLM
> > Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> > Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> > Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> > Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> > ---
> > tools/testing/selftests/mm/khugepaged.c | 7 +++++--
> > 1 file changed, 5 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> > index 1ca7c6978571..48e0040d53b4 100644
> > --- a/tools/testing/selftests/mm/khugepaged.c
> > +++ b/tools/testing/selftests/mm/khugepaged.c
> > @@ -556,8 +556,11 @@ static bool wait_for_scan(const char *msg, char *p, size_t len,
> > int nr_hpages, int collap_order, struct mem_ops *ops)
> > {
> > unsigned long hpage_size = page_size << collap_order;
> > - int full_scans;
> > - int timeout = 6; /* 3 seconds */
> > + unsigned long bytes = (unsigned long)nr_hpages * hpage_size;
>
> We already pass in the 'len' parameter, and its size is also 'nr_hpages *
> hpage_size", so you can drop the 'bytes' variable. With that,
They are the same for the PMD contexts, but not for mthp_khugepaged:
mthp_khugepaged_collapse() passes len = hpage_pmd_size, the range scanned,
while nr_hpages is the number of folios asked for. collapse_single_mthp()
asks for one order-N folio in a whole PMD.
That matters on arm64 with 64K pages, where the PMD is 512M: with len the
single-mTHP case would wait up to 7 seconds for one folio, with
nr_hpages * hpage_size it gets the 3 second floor. The budget should
follow what gets built, not what gets scanned, so I would keep it.
Does the Reviewed-by stand with that?
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 48+ messages in thread* Re: [PATCH v5 03/19] selftests/mm: scale khugepaged's collapse wait with the PMD size
2026-09-09 10:09 ` Kiryl Shutsemau
@ 2026-09-09 10:17 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-09 10:17 UTC (permalink / raw)
To: Kiryl Shutsemau
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On 9/9/26 6:09 PM, Kiryl Shutsemau wrote:
> On Wed, Sep 09, 2026 at 03:59:37PM +0800, Baolin Wang wrote:
>>
>>
>> On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
>>> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>>>
>>> wait_for_scan() gives every case the same three seconds, whatever the huge
>>> page costs to build. collapse_full() asks for four of them: 8M at a 2M
>>> PMD, but 2G at a 512M PMD -- arm64 with 64K base pages. Three seconds is
>>> thin at that size, and the case has reported a failure for a collapse that
>>> was still going.
>>>
>>> The timeout is a ceiling on a poll loop, not a sleep: the loop stops as
>>> soon as ops->check_huge() sees the collapse, or as soon as full_scans has
>>> advanced by two. Raising it costs a passing case nothing. Across 80 runs
>>> of collapse_full() on arm64 with 64K pages the wait was half a second in
>>> 73 of them, with a tail to two seconds.
>>>
>>> Keep three seconds as the floor and add a second per 128M collapsed. A 2M
>>> PMD is unchanged, so x86-64 is too; a 512M PMD gets 19 seconds.
>>>
>>> On arm64 with 64K pages a passing ./khugepaged all:anon takes 49 seconds
>>> under TCG before and after this change.
>>>
>>> Assisted-by: LLM
>>> Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
>>> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
>>> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
>>> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
>>> ---
>>> tools/testing/selftests/mm/khugepaged.c | 7 +++++--
>>> 1 file changed, 5 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
>>> index 1ca7c6978571..48e0040d53b4 100644
>>> --- a/tools/testing/selftests/mm/khugepaged.c
>>> +++ b/tools/testing/selftests/mm/khugepaged.c
>>> @@ -556,8 +556,11 @@ static bool wait_for_scan(const char *msg, char *p, size_t len,
>>> int nr_hpages, int collap_order, struct mem_ops *ops)
>>> {
>>> unsigned long hpage_size = page_size << collap_order;
>>> - int full_scans;
>>> - int timeout = 6; /* 3 seconds */
>>> + unsigned long bytes = (unsigned long)nr_hpages * hpage_size;
>>
>> We already pass in the 'len' parameter, and its size is also 'nr_hpages *
>> hpage_size", so you can drop the 'bytes' variable. With that,
>
> They are the same for the PMD contexts, but not for mthp_khugepaged:
> mthp_khugepaged_collapse() passes len = hpage_pmd_size, the range scanned,
> while nr_hpages is the number of folios asked for. collapse_single_mthp()
> asks for one order-N folio in a whole PMD.
>
> That matters on arm64 with 64K pages, where the PMD is 512M: with len the
> single-mTHP case would wait up to 7 seconds for one folio, with
> nr_hpages * hpage_size it gets the 3 second floor. The budget should
> follow what gets built, not what gets scanned, so I would keep it.
OK. Got it. Thanks.
> Does the Reviewed-by stand with that?
Yes. Please keep my reviewed tag.
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 04/19] selftests/mm: skip khugepaged page cache cases without a PMD folio
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (2 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 03/19] selftests/mm: scale khugepaged's collapse wait with the PMD size Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-09 8:21 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 05/19] selftests/mm: make the swap cases' swapout reliable Kiryl Shutsemau
` (15 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
The page cache caps folio order at MAX_PAGECACHE_ORDER, which is below the
PMD order on arm64 with 64K pages, where a PMD is 512M. A PMD-sized page
cache folio is impossible there, so the kernel refuses these collapses:
MADV_COLLAPSE answers -EINVAL and khugepaged passes over the range. Four
shmem cases ask for a PMD-sized folio anyway, fail, and the run bails out
in the middle.
Skip the shmem and file mem types where the cap is below the PMD order.
The cap is not shmem-specific: it applies to every file folio. Add
thp_file_supported_orders() to read the orders the page cache allows.
Anonymous collapse is unaffected: its orders are not capped this way.
Assisted-by: LLM
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
.../testing/selftests/mm/hugepage_settings.h | 9 +++++++++
tools/testing/selftests/mm/khugepaged.c | 19 +++++++++++++++++++
2 files changed, 28 insertions(+)
diff --git a/tools/testing/selftests/mm/hugepage_settings.h b/tools/testing/selftests/mm/hugepage_settings.h
index 726c73c43c05..a1d12e2ffd62 100644
--- a/tools/testing/selftests/mm/hugepage_settings.h
+++ b/tools/testing/selftests/mm/hugepage_settings.h
@@ -87,6 +87,15 @@ void thp_set_read_ahead_path(char *path);
unsigned long thp_supported_orders(void);
unsigned long thp_shmem_supported_orders(void);
+/*
+ * The per-order shmem_enabled attribute is created for the orders the page
+ * cache can hold, not just for shmem, so it answers for regular files too.
+ */
+static inline unsigned long thp_file_supported_orders(void)
+{
+ return thp_shmem_supported_orders();
+}
+
bool thp_available(void);
bool thp_is_enabled(void);
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 48e0040d53b4..73bf07c39145 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -1353,6 +1353,25 @@ int main(int argc, char **argv)
setbuf(stdout, NULL);
+ /*
+ * Without a PMD-order page cache folio the kernel refuses these
+ * collapses, so there is nothing to test.
+ */
+ if (!(thp_file_supported_orders() & (1UL << hpage_pmd_order))) {
+ if (shmem_ops) {
+ ksft_print_msg("no PMD-order page cache folio: skipping shmem\n");
+ shmem_ops = NULL;
+ }
+ if (read_only_file_ops) {
+ ksft_print_msg("no PMD-order page cache folio: skipping file\n");
+ read_only_file_ops = NULL;
+ read_write_file_read_ops = NULL;
+ read_write_file_write_ops = NULL;
+ }
+ if (!anon_ops && !shmem_ops && !read_only_file_ops)
+ ksft_exit_skip("No mem_type left to run\n");
+ }
+
default_settings.khugepaged.max_ptes_none = hpage_pmd_nr - 1;
default_settings.khugepaged.max_ptes_swap = hpage_pmd_nr / 8;
default_settings.khugepaged.max_ptes_shared = hpage_pmd_nr / 2;
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 04/19] selftests/mm: skip khugepaged page cache cases without a PMD folio
2026-09-08 12:50 ` [PATCH v5 04/19] selftests/mm: skip khugepaged page cache cases without a PMD folio Kiryl Shutsemau
@ 2026-09-09 8:21 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-09 8:21 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> The page cache caps folio order at MAX_PAGECACHE_ORDER, which is below the
> PMD order on arm64 with 64K pages, where a PMD is 512M. A PMD-sized page
> cache folio is impossible there, so the kernel refuses these collapses:
> MADV_COLLAPSE answers -EINVAL and khugepaged passes over the range. Four
> shmem cases ask for a PMD-sized folio anyway, fail, and the run bails out
> in the middle.
>
> Skip the shmem and file mem types where the cap is below the PMD order.
>
> The cap is not shmem-specific: it applies to every file folio. Add
> thp_file_supported_orders() to read the orders the page cache allows.
>
> Anonymous collapse is unaffected: its orders are not capped this way.
>
> Assisted-by: LLM
> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 05/19] selftests/mm: make the swap cases' swapout reliable
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (3 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 04/19] selftests/mm: skip khugepaged page cache cases without a PMD folio Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-09 8:59 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 06/19] selftests/mm: stop khugepaged during the MADV_COLLAPSE cases Kiryl Shutsemau
` (14 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
collapse_swapin_single_pte() and collapse_max_ptes_swap() swap a range out
and then require smaps to report exactly the count they asked for. Two
things keep that count from arriving.
MADV_PAGEOUT is best effort, so the count often turns up a moment late.
And wait_for_scan() leaves the range eligible for collapsing, so
khugepaged is still working on it. Collapsing reads the swapped-out pages
back in, so the daemon empties the swap as fast as the case fills it. On
arm64 with 64K pages max_ptes_swap is 1024 pages, which is 64M a step, and
the case loses the race:
# Swapout 1024 of 8192 pages... Fail
not ok 10 collapse_max_ptes_swap
Retry for up to two seconds, holding the range out of khugepaged's reach
meanwhile. The collapse each case runs next restores MADV_HUGEPAGE, so
only the setup is affected.
If the pages still won't swap out, skip: no swap, swap too small or full,
a memcg cap or busy writeback. None of that is a kernel bug.
Assisted-by: LLM
Reviewed-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged.c | 41 +++++++++++++++++--------
1 file changed, 29 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 73bf07c39145..398430c33872 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -220,6 +220,29 @@ static bool check_swap(void *addr, unsigned long size)
return swap;
}
+static bool swapout_range(void *p, unsigned long size)
+{
+ int i;
+
+ /* keep khugepaged from collapsing the range and swapping it back in */
+ if (madvise(p, size, MADV_NOHUGEPAGE))
+ ksft_exit_fail_perror("madvise(MADV_NOHUGEPAGE)");
+
+ /*
+ * Retry several times because MADV_PAGEOUT is best effort. Sleep
+ * between the retries to give outstanding writeback a chance to
+ * finish.
+ */
+ for (i = 0; i < 40; i++) {
+ if (madvise(p, size, MADV_PAGEOUT))
+ ksft_exit_fail_perror("madvise(MADV_PAGEOUT)");
+ if (check_swap(p, size))
+ return true;
+ usleep(50 * 1000);
+ }
+ return false;
+}
+
static void *alloc_mapping(int nr)
{
void *p;
@@ -823,12 +846,10 @@ static void collapse_swapin_single_pte(struct collapse_context *c, struct mem_op
ops->fault(p, 0, hpage_pmd_size);
ksft_print_msg("Swapout one page...");
- if (madvise(p, page_size, MADV_PAGEOUT))
- ksft_exit_fail_perror("madvise(MADV_PAGEOUT)");
- if (check_swap(p, page_size)) {
+ if (swapout_range(p, page_size)) {
success("OK");
} else {
- fail("Fail");
+ skip("Could not swap out");
goto out;
}
@@ -849,12 +870,10 @@ static void collapse_max_ptes_swap(struct collapse_context *c, struct mem_ops *o
ops->fault(p, 0, hpage_pmd_size);
ksft_print_msg("Swapout %d of %d pages...", max_ptes_swap + 1, hpage_pmd_nr);
- if (madvise(p, (max_ptes_swap + 1) * page_size, MADV_PAGEOUT))
- ksft_exit_fail_perror("madvise(MADV_PAGEOUT)");
- if (check_swap(p, (max_ptes_swap + 1) * page_size)) {
+ if (swapout_range(p, (max_ptes_swap + 1) * page_size)) {
success("OK");
} else {
- fail("Fail");
+ skip("Could not swap out");
goto out;
}
@@ -866,12 +885,10 @@ static void collapse_max_ptes_swap(struct collapse_context *c, struct mem_ops *o
ops->fault(p, 0, hpage_pmd_size);
ksft_print_msg("Swapout %d of %d pages...", max_ptes_swap,
hpage_pmd_nr);
- if (madvise(p, max_ptes_swap * page_size, MADV_PAGEOUT))
- ksft_exit_fail_perror("madvise(MADV_PAGEOUT)");
- if (check_swap(p, max_ptes_swap * page_size)) {
+ if (swapout_range(p, max_ptes_swap * page_size)) {
success("OK");
} else {
- fail("Fail");
+ skip("Could not swap out");
goto out;
}
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 05/19] selftests/mm: make the swap cases' swapout reliable
2026-09-08 12:50 ` [PATCH v5 05/19] selftests/mm: make the swap cases' swapout reliable Kiryl Shutsemau
@ 2026-09-09 8:59 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-09 8:59 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> collapse_swapin_single_pte() and collapse_max_ptes_swap() swap a range out
> and then require smaps to report exactly the count they asked for. Two
> things keep that count from arriving.
>
> MADV_PAGEOUT is best effort, so the count often turns up a moment late.
>
> And wait_for_scan() leaves the range eligible for collapsing, so
> khugepaged is still working on it. Collapsing reads the swapped-out pages
> back in, so the daemon empties the swap as fast as the case fills it. On
> arm64 with 64K pages max_ptes_swap is 1024 pages, which is 64M a step, and
> the case loses the race:
>
> # Swapout 1024 of 8192 pages... Fail
> not ok 10 collapse_max_ptes_swap
>
> Retry for up to two seconds, holding the range out of khugepaged's reach
> meanwhile. The collapse each case runs next restores MADV_HUGEPAGE, so
> only the setup is affected.
>
> If the pages still won't swap out, skip: no swap, swap too small or full,
> a memcg cap or busy writeback. None of that is a kernel bug.
>
> Assisted-by: LLM
> Reviewed-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
Make sense to me. So
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 06/19] selftests/mm: stop khugepaged during the MADV_COLLAPSE cases
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (4 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 05/19] selftests/mm: make the swap cases' swapout reliable Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-09 9:55 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 07/19] selftests/mm: move is_backed_by_folio() into vm_util Kiryl Shutsemau
` (13 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
__madvise_collapse() turns THP off before each MADV_COLLAPSE, both to keep
khugepaged out of the range and to prove MADV_COLLAPSE ignores the setting.
It clears the global controls only, which is no longer enough. A per-order
control overrides them, and -s, which makes the cases fault in folios of
one order, leaves that order's control at "always". khugepaged then
collapses the very range the case is working on, and the case fails on a
collapse that was interfered with rather than refused.
Clear the per-order controls too, setting them to "inherit" rather than
"never": khugepaged honours the global never and stays out, while
MADV_COLLAPSE on shmem still finds an order to build.
Fixes: 9f0704eae8a4 ("selftests/mm/khugepaged: enlighten for multi-size THP")
Assisted-by: LLM
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 398430c33872..e9bc8fe8a1f8 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -533,8 +533,8 @@ static bool is_anon(struct mem_ops *ops)
static void __madvise_collapse(const char *msg, char *p, int nr_hpages,
struct mem_ops *ops, bool expect)
{
- int ret;
struct thp_settings settings = *thp_current_settings();
+ int ret, i;
ksft_print_msg("%s...", msg);
@@ -547,9 +547,16 @@ static void __madvise_collapse(const char *msg, char *p, int nr_hpages,
/*
* Prevent khugepaged interference and tests that MADV_COLLAPSE
* ignores /sys/kernel/mm/transparent_hugepage/enabled
+ *
+ * "inherit" rather than "never" so that MADV_COLLAPSE on shmem still
+ * finds an order to build.
*/
settings.thp_enabled = THP_NEVER;
settings.shmem_enabled = SHMEM_NEVER;
+ for (i = 0; i < NR_ORDERS; i++) {
+ settings.hugepages[i].enabled = THP_INHERIT;
+ settings.shmem_hugepages[i].enabled = SHMEM_INHERIT;
+ }
thp_push_settings(&settings);
/* Clear VM_NOHUGEPAGE */
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 06/19] selftests/mm: stop khugepaged during the MADV_COLLAPSE cases
2026-09-08 12:50 ` [PATCH v5 06/19] selftests/mm: stop khugepaged during the MADV_COLLAPSE cases Kiryl Shutsemau
@ 2026-09-09 9:55 ` Baolin Wang
2026-09-09 10:41 ` Kiryl Shutsemau
0 siblings, 1 reply; 48+ messages in thread
From: Baolin Wang @ 2026-09-09 9:55 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> __madvise_collapse() turns THP off before each MADV_COLLAPSE, both to keep
> khugepaged out of the range and to prove MADV_COLLAPSE ignores the setting.
> It clears the global controls only, which is no longer enough. A per-order
> control overrides them, and -s, which makes the cases fault in folios of
> one order, leaves that order's control at "always". khugepaged then
> collapses the very range the case is working on, and the case fails on a
> collapse that was interfered with rather than refused.
Right. So I think the correct fix tag is b7f16963efe7 ("mm/khugepaged:
run khugepaged for all orders"), because before this commit, khugepaged
would not try to collapse this range since it only checked whether the
PMD order was suitable for collapse.
> Clear the per-order controls too, setting them to "inherit" rather than
> "never": khugepaged honours the global never and stays out, while
> MADV_COLLAPSE on shmem still finds an order to build.
>
> Fixes: 9f0704eae8a4 ("selftests/mm/khugepaged: enlighten for multi-size THP")
> Assisted-by: LLM
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
> tools/testing/selftests/mm/khugepaged.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> index 398430c33872..e9bc8fe8a1f8 100644
> --- a/tools/testing/selftests/mm/khugepaged.c
> +++ b/tools/testing/selftests/mm/khugepaged.c
> @@ -533,8 +533,8 @@ static bool is_anon(struct mem_ops *ops)
> static void __madvise_collapse(const char *msg, char *p, int nr_hpages,
> struct mem_ops *ops, bool expect)
> {
> - int ret;
> struct thp_settings settings = *thp_current_settings();
> + int ret, i;
>
> ksft_print_msg("%s...", msg);
>
> @@ -547,9 +547,16 @@ static void __madvise_collapse(const char *msg, char *p, int nr_hpages,
> /*
> * Prevent khugepaged interference and tests that MADV_COLLAPSE
> * ignores /sys/kernel/mm/transparent_hugepage/enabled
> + *
> + * "inherit" rather than "never" so that MADV_COLLAPSE on shmem still
> + * finds an order to build.
> */
> settings.thp_enabled = THP_NEVER;
> settings.shmem_enabled = SHMEM_NEVER;
> + for (i = 0; i < NR_ORDERS; i++) {
> + settings.hugepages[i].enabled = THP_INHERIT;
> + settings.shmem_hugepages[i].enabled = SHMEM_INHERIT;
> + }
This looks like a workaround to me. Shouldn't we fix this in shmem
instead? Since MADV_COLLAPSE is supposed to ignore the THP setting, the
fix should be something like this?
diff --git a/mm/shmem.c b/mm/shmem.c
index c92ed17dbc4a..e0c0901c34a4 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2050,7 +2050,7 @@ unsigned long shmem_allowable_huge_orders(struct
inode *inode,
global_orders = shmem_huge_global_enabled(inode, index, write_end,
shmem_huge_force,
vma, vm_flags);
/* Tmpfs huge pages allocation */
- if (!vma || !vma_is_anon_shmem(vma))
+ if (!vma || !vma_is_anon_shmem(vma) || shmem_huge_force)
return global_orders;
/*
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 06/19] selftests/mm: stop khugepaged during the MADV_COLLAPSE cases
2026-09-09 9:55 ` Baolin Wang
@ 2026-09-09 10:41 ` Kiryl Shutsemau
2026-09-10 6:27 ` Baolin Wang
0 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-09 10:41 UTC (permalink / raw)
To: Baolin Wang
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On Wed, Sep 09, 2026 at 05:55:01PM +0800, Baolin Wang wrote:
>
>
> On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> > From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
> >
> > __madvise_collapse() turns THP off before each MADV_COLLAPSE, both to keep
> > khugepaged out of the range and to prove MADV_COLLAPSE ignores the setting.
> > It clears the global controls only, which is no longer enough. A per-order
> > control overrides them, and -s, which makes the cases fault in folios of
> > one order, leaves that order's control at "always". khugepaged then
> > collapses the very range the case is working on, and the case fails on a
> > collapse that was interfered with rather than refused.
>
> Right. So I think the correct fix tag is b7f16963efe7 ("mm/khugepaged: run
> khugepaged for all orders"), because before this commit, khugepaged would
> not try to collapse this range since it only checked whether the PMD order
> was suitable for collapse.
Agreed. The series is in mm-new already; if a respin is needed I will use
that tag.
> > @@ -547,9 +547,16 @@ static void __madvise_collapse(const char *msg, char *p, int nr_hpages,
> > /*
> > * Prevent khugepaged interference and tests that MADV_COLLAPSE
> > * ignores /sys/kernel/mm/transparent_hugepage/enabled
> > + *
> > + * "inherit" rather than "never" so that MADV_COLLAPSE on shmem still
> > + * finds an order to build.
> > */
> > settings.thp_enabled = THP_NEVER;
> > settings.shmem_enabled = SHMEM_NEVER;
> > + for (i = 0; i < NR_ORDERS; i++) {
> > + settings.hugepages[i].enabled = THP_INHERIT;
> > + settings.shmem_hugepages[i].enabled = SHMEM_INHERIT;
> > + }
>
> This looks like a workaround to me. Shouldn't we fix this in shmem instead?
Good point.
It can be a follow-up patch. Do you want to make a proper shmem.c fix
and update the selftest along with it?
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 48+ messages in thread* Re: [PATCH v5 06/19] selftests/mm: stop khugepaged during the MADV_COLLAPSE cases
2026-09-09 10:41 ` Kiryl Shutsemau
@ 2026-09-10 6:27 ` Baolin Wang
2026-09-10 10:59 ` Kiryl Shutsemau
0 siblings, 1 reply; 48+ messages in thread
From: Baolin Wang @ 2026-09-10 6:27 UTC (permalink / raw)
To: Kiryl Shutsemau
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On 9/9/26 6:41 PM, Kiryl Shutsemau wrote:
> On Wed, Sep 09, 2026 at 05:55:01PM +0800, Baolin Wang wrote:
>>
>>
>> On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
>>> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>>>
>>> __madvise_collapse() turns THP off before each MADV_COLLAPSE, both to keep
>>> khugepaged out of the range and to prove MADV_COLLAPSE ignores the setting.
>>> It clears the global controls only, which is no longer enough. A per-order
>>> control overrides them, and -s, which makes the cases fault in folios of
>>> one order, leaves that order's control at "always". khugepaged then
>>> collapses the very range the case is working on, and the case fails on a
>>> collapse that was interfered with rather than refused.
>>
>> Right. So I think the correct fix tag is b7f16963efe7 ("mm/khugepaged: run
>> khugepaged for all orders"), because before this commit, khugepaged would
>> not try to collapse this range since it only checked whether the PMD order
>> was suitable for collapse.
>
> Agreed. The series is in mm-new already; if a respin is needed I will use
> that tag.
>
>>> @@ -547,9 +547,16 @@ static void __madvise_collapse(const char *msg, char *p, int nr_hpages,
>>> /*
>>> * Prevent khugepaged interference and tests that MADV_COLLAPSE
>>> * ignores /sys/kernel/mm/transparent_hugepage/enabled
>>> + *
>>> + * "inherit" rather than "never" so that MADV_COLLAPSE on shmem still
>>> + * finds an order to build.
>>> */
>>> settings.thp_enabled = THP_NEVER;
>>> settings.shmem_enabled = SHMEM_NEVER;
>>> + for (i = 0; i < NR_ORDERS; i++) {
>>> + settings.hugepages[i].enabled = THP_INHERIT;
>>> + settings.shmem_hugepages[i].enabled = SHMEM_INHERIT;
>>> + }
>>
>> This looks like a workaround to me. Shouldn't we fix this in shmem instead?
>
> Good point.
>
> It can be a follow-up patch. Do you want to make a proper shmem.c fix
> and update the selftest along with it?
Either way works for me.
^ permalink raw reply [flat|nested] 48+ messages in thread* Re: [PATCH v5 06/19] selftests/mm: stop khugepaged during the MADV_COLLAPSE cases
2026-09-10 6:27 ` Baolin Wang
@ 2026-09-10 10:59 ` Kiryl Shutsemau
2026-09-10 11:06 ` Baolin Wang
0 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-10 10:59 UTC (permalink / raw)
To: Baolin Wang
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On Thu, Sep 10, 2026 at 02:27:28PM +0800, Baolin Wang wrote:
>
>
> On 9/9/26 6:41 PM, Kiryl Shutsemau wrote:
> > On Wed, Sep 09, 2026 at 05:55:01PM +0800, Baolin Wang wrote:
> > >
> > >
> > > On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> > > > From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
> > > >
> > > > __madvise_collapse() turns THP off before each MADV_COLLAPSE, both to keep
> > > > khugepaged out of the range and to prove MADV_COLLAPSE ignores the setting.
> > > > It clears the global controls only, which is no longer enough. A per-order
> > > > control overrides them, and -s, which makes the cases fault in folios of
> > > > one order, leaves that order's control at "always". khugepaged then
> > > > collapses the very range the case is working on, and the case fails on a
> > > > collapse that was interfered with rather than refused.
> > >
> > > Right. So I think the correct fix tag is b7f16963efe7 ("mm/khugepaged: run
> > > khugepaged for all orders"), because before this commit, khugepaged would
> > > not try to collapse this range since it only checked whether the PMD order
> > > was suitable for collapse.
> >
> > Agreed. The series is in mm-new already; if a respin is needed I will use
> > that tag.
> >
> > > > @@ -547,9 +547,16 @@ static void __madvise_collapse(const char *msg, char *p, int nr_hpages,
> > > > /*
> > > > * Prevent khugepaged interference and tests that MADV_COLLAPSE
> > > > * ignores /sys/kernel/mm/transparent_hugepage/enabled
> > > > + *
> > > > + * "inherit" rather than "never" so that MADV_COLLAPSE on shmem still
> > > > + * finds an order to build.
> > > > */
> > > > settings.thp_enabled = THP_NEVER;
> > > > settings.shmem_enabled = SHMEM_NEVER;
> > > > + for (i = 0; i < NR_ORDERS; i++) {
> > > > + settings.hugepages[i].enabled = THP_INHERIT;
> > > > + settings.shmem_hugepages[i].enabled = SHMEM_INHERIT;
> > > > + }
> > >
> > > This looks like a workaround to me. Shouldn't we fix this in shmem instead?
> >
> > Good point.
> >
> > It can be a follow-up patch. Do you want to make a proper shmem.c fix
> > and update the selftest along with it?
>
> Either way works for me.
Please, do it as a standlone shmem fix, plus selftest update to reflect
the change.
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 48+ messages in thread* Re: [PATCH v5 06/19] selftests/mm: stop khugepaged during the MADV_COLLAPSE cases
2026-09-10 10:59 ` Kiryl Shutsemau
@ 2026-09-10 11:06 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-10 11:06 UTC (permalink / raw)
To: Kiryl Shutsemau
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On 9/10/26 6:59 PM, Kiryl Shutsemau wrote:
> On Thu, Sep 10, 2026 at 02:27:28PM +0800, Baolin Wang wrote:
>>
>>
>> On 9/9/26 6:41 PM, Kiryl Shutsemau wrote:
>>> On Wed, Sep 09, 2026 at 05:55:01PM +0800, Baolin Wang wrote:
>>>>
>>>>
>>>> On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
>>>>> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>>>>>
>>>>> __madvise_collapse() turns THP off before each MADV_COLLAPSE, both to keep
>>>>> khugepaged out of the range and to prove MADV_COLLAPSE ignores the setting.
>>>>> It clears the global controls only, which is no longer enough. A per-order
>>>>> control overrides them, and -s, which makes the cases fault in folios of
>>>>> one order, leaves that order's control at "always". khugepaged then
>>>>> collapses the very range the case is working on, and the case fails on a
>>>>> collapse that was interfered with rather than refused.
>>>>
>>>> Right. So I think the correct fix tag is b7f16963efe7 ("mm/khugepaged: run
>>>> khugepaged for all orders"), because before this commit, khugepaged would
>>>> not try to collapse this range since it only checked whether the PMD order
>>>> was suitable for collapse.
>>>
>>> Agreed. The series is in mm-new already; if a respin is needed I will use
>>> that tag.
>>>
>>>>> @@ -547,9 +547,16 @@ static void __madvise_collapse(const char *msg, char *p, int nr_hpages,
>>>>> /*
>>>>> * Prevent khugepaged interference and tests that MADV_COLLAPSE
>>>>> * ignores /sys/kernel/mm/transparent_hugepage/enabled
>>>>> + *
>>>>> + * "inherit" rather than "never" so that MADV_COLLAPSE on shmem still
>>>>> + * finds an order to build.
>>>>> */
>>>>> settings.thp_enabled = THP_NEVER;
>>>>> settings.shmem_enabled = SHMEM_NEVER;
>>>>> + for (i = 0; i < NR_ORDERS; i++) {
>>>>> + settings.hugepages[i].enabled = THP_INHERIT;
>>>>> + settings.shmem_hugepages[i].enabled = SHMEM_INHERIT;
>>>>> + }
>>>>
>>>> This looks like a workaround to me. Shouldn't we fix this in shmem instead?
>>>
>>> Good point.
>>>
>>> It can be a follow-up patch. Do you want to make a proper shmem.c fix
>>> and update the selftest along with it?
>>
>> Either way works for me.
>
> Please, do it as a standlone shmem fix, plus selftest update to reflect
> the change.
Ah, sorry, I misunderstood your points. I'll send out a fix patch tomorrow.
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 07/19] selftests/mm: move is_backed_by_folio() into vm_util
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (5 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 06/19] selftests/mm: stop khugepaged during the MADV_COLLAPSE cases Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-09 9:16 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 08/19] selftests/mm: add folio-order check for address ranges Kiryl Shutsemau
` (12 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
Checking that an address range is backed by a folio of a given order is
useful to any test that builds or collapses large folios. mTHP collapse
coverage in the khugepaged selftest needs exactly that.
split_huge_page_test.c already has the building block:
is_backed_by_folio() reads the compound head and tail flags from
/proc/kpageflags to classify the folio behind a page.
Move it into vm_util so other tests can use it. No functional change.
Assisted-by: LLM
Acked-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
.../selftests/mm/split_huge_page_test.c | 62 -------------------
tools/testing/selftests/mm/vm_util.c | 62 +++++++++++++++++++
tools/testing/selftests/mm/vm_util.h | 2 +
3 files changed, 64 insertions(+), 62 deletions(-)
diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
index 86a603692826..0adfe7dde7e5 100644
--- a/tools/testing/selftests/mm/split_huge_page_test.c
+++ b/tools/testing/selftests/mm/split_huge_page_test.c
@@ -42,68 +42,6 @@ const char *kpageflags_proc = "/proc/kpageflags";
int pagemap_fd;
int kpageflags_fd;
-static bool is_backed_by_folio(char *vaddr, int order, int pagemap_fd,
- int kpageflags_fd)
-{
- const uint64_t folio_head_flags = KPF_THP | KPF_COMPOUND_HEAD;
- const uint64_t folio_tail_flags = KPF_THP | KPF_COMPOUND_TAIL;
- const unsigned long nr_pages = 1UL << order;
- unsigned long pfn_head;
- uint64_t pfn_flags;
- unsigned long pfn;
- unsigned long i;
-
- pfn = pagemap_get_pfn(pagemap_fd, vaddr);
-
- /* non present page */
- if (pfn == -1UL)
- return false;
-
- if (pageflags_get(pfn, kpageflags_fd, &pfn_flags))
- goto fail;
-
- /* check for order-0 pages */
- if (!order) {
- if (pfn_flags & (folio_head_flags | folio_tail_flags))
- return false;
- return true;
- }
-
- /* non THP folio */
- if (!(pfn_flags & KPF_THP))
- return false;
-
- pfn_head = pfn & ~(nr_pages - 1);
-
- if (pageflags_get(pfn_head, kpageflags_fd, &pfn_flags))
- goto fail;
-
- /* head PFN has no compound_head flag set */
- if ((pfn_flags & folio_head_flags) != folio_head_flags)
- return false;
-
- /* check all tail PFN flags */
- for (i = 1; i < nr_pages; i++) {
- if (pageflags_get(pfn_head + i, kpageflags_fd, &pfn_flags))
- goto fail;
- if ((pfn_flags & folio_tail_flags) != folio_tail_flags)
- return false;
- }
-
- /*
- * check the PFN after this folio, but if its flags cannot be obtained,
- * assume this folio has the expected order
- */
- if (pageflags_get(pfn_head + nr_pages, kpageflags_fd, &pfn_flags))
- return true;
-
- /* If we find another tail page, then the folio is larger. */
- return (pfn_flags & folio_tail_flags) != folio_tail_flags;
-fail:
- ksft_exit_fail_msg("Failed to get folio info\n");
- return false;
-}
-
static int check_after_split_folio_orders(char *vaddr_start, size_t len,
int pagemap_fd, int kpageflags_fd, int orders[], int nr_orders)
{
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 4821a3563036..3ea42a7a2a3e 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -490,6 +490,68 @@ int pageflags_get(unsigned long pfn, int kpageflags_fd, uint64_t *flags)
return 0;
}
+bool is_backed_by_folio(char *vaddr, int order, int pagemap_fd,
+ int kpageflags_fd)
+{
+ const uint64_t folio_head_flags = KPF_THP | KPF_COMPOUND_HEAD;
+ const uint64_t folio_tail_flags = KPF_THP | KPF_COMPOUND_TAIL;
+ const unsigned long nr_pages = 1UL << order;
+ unsigned long pfn_head;
+ uint64_t pfn_flags;
+ unsigned long pfn;
+ unsigned long i;
+
+ pfn = pagemap_get_pfn(pagemap_fd, vaddr);
+
+ /* non present page */
+ if (pfn == -1UL)
+ return false;
+
+ if (pageflags_get(pfn, kpageflags_fd, &pfn_flags))
+ goto fail;
+
+ /* check for order-0 pages */
+ if (!order) {
+ if (pfn_flags & (folio_head_flags | folio_tail_flags))
+ return false;
+ return true;
+ }
+
+ /* non THP folio */
+ if (!(pfn_flags & KPF_THP))
+ return false;
+
+ pfn_head = pfn & ~(nr_pages - 1);
+
+ if (pageflags_get(pfn_head, kpageflags_fd, &pfn_flags))
+ goto fail;
+
+ /* head PFN has no compound_head flag set */
+ if ((pfn_flags & folio_head_flags) != folio_head_flags)
+ return false;
+
+ /* check all tail PFN flags */
+ for (i = 1; i < nr_pages; i++) {
+ if (pageflags_get(pfn_head + i, kpageflags_fd, &pfn_flags))
+ goto fail;
+ if ((pfn_flags & folio_tail_flags) != folio_tail_flags)
+ return false;
+ }
+
+ /*
+ * check the PFN after this folio, but if its flags cannot be obtained,
+ * assume this folio has the expected order
+ */
+ if (pageflags_get(pfn_head + nr_pages, kpageflags_fd, &pfn_flags))
+ return true;
+
+ /* If we find another tail page, then the folio is larger. */
+ return (pfn_flags & folio_tail_flags) != folio_tail_flags;
+fail:
+ ksft_exit_fail_msg("Failed to get folio info\n");
+ return false;
+}
+
/* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */
int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
bool miss, bool wp, bool minor, uint64_t *ioctls)
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index 9a49af88702e..56a28ce7d029 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -97,6 +97,8 @@ int64_t allocate_transhuge(void *ptr, int pagemap_fd);
int pageflags_get(unsigned long pfn, int kpageflags_fd, uint64_t *flags);
int gather_folio_orders(char *vaddr_start, size_t len,
int pagemap_fd, int kpageflags_fd, int orders[], int nr_orders);
+bool is_backed_by_folio(char *vaddr, int order, int pagemap_fd,
+ int kpageflags_fd);
int uffd_register(int uffd, void *addr, uint64_t len,
bool miss, bool wp, bool minor);
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 07/19] selftests/mm: move is_backed_by_folio() into vm_util
2026-09-08 12:50 ` [PATCH v5 07/19] selftests/mm: move is_backed_by_folio() into vm_util Kiryl Shutsemau
@ 2026-09-09 9:16 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-09 9:16 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> Checking that an address range is backed by a folio of a given order is
> useful to any test that builds or collapses large folios. mTHP collapse
> coverage in the khugepaged selftest needs exactly that.
>
> split_huge_page_test.c already has the building block:
> is_backed_by_folio() reads the compound head and tail flags from
> /proc/kpageflags to classify the folio behind a page.
>
> Move it into vm_util so other tests can use it. No functional change.
>
> Assisted-by: LLM
> Acked-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 08/19] selftests/mm: add folio-order check for address ranges
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (6 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 07/19] selftests/mm: move is_backed_by_folio() into vm_util Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-09 10:01 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 09/19] selftests/mm: add folio-order detection self-check Kiryl Shutsemau
` (11 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
An mTHP collapse test needs to know that a range is backed by folios of the
target order, and that they sit where a collapse would put them. Nothing
answers that today: is_backed_by_folio() classifies the folio behind a
single page, and check_huge_anon() reads smaps AnonHugePages, which only
accounts PMD mappings.
Add is_range_backed_by_order(). It requires every folio-sized, folio-
aligned part of the range to map one folio of that order, head to tail,
with the head at the start of the part.
A part backed by two smaller folios fails, and so does a folio mapped off
its natural alignment. The mTHP cases need both to tell a collapsed range
from the one beside it.
Assisted-by: LLM
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/vm_util.c | 47 ++++++++++++++++++++++++++++
tools/testing/selftests/mm/vm_util.h | 2 ++
2 files changed, 49 insertions(+)
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 3ea42a7a2a3e..4947612e8b3d 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -552,6 +552,53 @@ bool is_backed_by_folio(char *vaddr, int order, int pagemap_fd,
return false;
}
+/**
+ * is_range_backed_by_order() - check that a range is backed by @order folios
+ * @start: start of the range, a multiple of the folio size
+ * @len: length of the range in bytes, a multiple of the folio size
+ * @order: the folio order to check for
+ * @pagemap_fd: open /proc/<pid>/pagemap of the range's owner
+ * @kpageflags_fd: open /proc/kpageflags
+ *
+ * Every folio-sized, folio-aligned part of the range must map one folio of
+ * @order, head to tail, with the head at the start of the part. A part
+ * backed by several smaller folios fails, and so does a folio mapped off
+ * its natural alignment.
+ *
+ * Returns: true if the whole range is backed that way, false otherwise.
+ */
+bool is_range_backed_by_order(char *start, size_t len, int order,
+ int pagemap_fd, int kpageflags_fd)
+{
+ const unsigned long nr_pages = 1UL << order;
+ const size_t folio_size = nr_pages * psize();
+ char *vaddr;
+
+ if ((uintptr_t)start % folio_size || len % folio_size)
+ return false;
+
+ for (vaddr = start; vaddr < start + len; vaddr += folio_size) {
+ const unsigned long pfn = pagemap_get_pfn(pagemap_fd, vaddr);
+ unsigned long i;
+
+ /* Not present, or a tail page */
+ if (pfn == -1UL || pfn % nr_pages)
+ return false;
+
+ for (i = 1; i < nr_pages; i++) {
+ char *page = vaddr + i * psize();
+
+ if (pagemap_get_pfn(pagemap_fd, page) != pfn + i)
+ return false;
+ }
+
+ if (!is_backed_by_folio(vaddr, order, pagemap_fd, kpageflags_fd))
+ return false;
+ }
+
+ return true;
+}
+
/* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */
int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
bool miss, bool wp, bool minor, uint64_t *ioctls)
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index 56a28ce7d029..e509fc4012a5 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -99,6 +99,8 @@ int gather_folio_orders(char *vaddr_start, size_t len,
int pagemap_fd, int kpageflags_fd, int orders[], int nr_orders);
bool is_backed_by_folio(char *vaddr, int order, int pagemap_fd,
int kpageflags_fd);
+bool is_range_backed_by_order(char *start, size_t len, int order,
+ int pagemap_fd, int kpageflags_fd);
int uffd_register(int uffd, void *addr, uint64_t len,
bool miss, bool wp, bool minor);
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 08/19] selftests/mm: add folio-order check for address ranges
2026-09-08 12:50 ` [PATCH v5 08/19] selftests/mm: add folio-order check for address ranges Kiryl Shutsemau
@ 2026-09-09 10:01 ` Baolin Wang
2026-09-10 10:45 ` Kiryl Shutsemau
0 siblings, 1 reply; 48+ messages in thread
From: Baolin Wang @ 2026-09-09 10:01 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> An mTHP collapse test needs to know that a range is backed by folios of the
> target order, and that they sit where a collapse would put them. Nothing
> answers that today: is_backed_by_folio() classifies the folio behind a
> single page, and check_huge_anon() reads smaps AnonHugePages, which only
> accounts PMD mappings.
Have you checked check_large_folios() in vm_util.c? It seems to meet
your requirements, or am I missing something?
> Add is_range_backed_by_order(). It requires every folio-sized, folio-
> aligned part of the range to map one folio of that order, head to tail,
> with the head at the start of the part.
>
> A part backed by two smaller folios fails, and so does a folio mapped off
> its natural alignment. The mTHP cases need both to tell a collapsed range
> from the one beside it.
>
> Assisted-by: LLM
> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
> tools/testing/selftests/mm/vm_util.c | 47 ++++++++++++++++++++++++++++
> tools/testing/selftests/mm/vm_util.h | 2 ++
> 2 files changed, 49 insertions(+)
>
> diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
> index 3ea42a7a2a3e..4947612e8b3d 100644
> --- a/tools/testing/selftests/mm/vm_util.c
> +++ b/tools/testing/selftests/mm/vm_util.c
> @@ -552,6 +552,53 @@ bool is_backed_by_folio(char *vaddr, int order, int pagemap_fd,
> return false;
> }
>
> +/**
> + * is_range_backed_by_order() - check that a range is backed by @order folios
> + * @start: start of the range, a multiple of the folio size
> + * @len: length of the range in bytes, a multiple of the folio size
> + * @order: the folio order to check for
> + * @pagemap_fd: open /proc/<pid>/pagemap of the range's owner
> + * @kpageflags_fd: open /proc/kpageflags
> + *
> + * Every folio-sized, folio-aligned part of the range must map one folio of
> + * @order, head to tail, with the head at the start of the part. A part
> + * backed by several smaller folios fails, and so does a folio mapped off
> + * its natural alignment.
> + *
> + * Returns: true if the whole range is backed that way, false otherwise.
> + */
> +bool is_range_backed_by_order(char *start, size_t len, int order,
> + int pagemap_fd, int kpageflags_fd)
> +{
> + const unsigned long nr_pages = 1UL << order;
> + const size_t folio_size = nr_pages * psize();
> + char *vaddr;
> +
> + if ((uintptr_t)start % folio_size || len % folio_size)
> + return false;
> +
> + for (vaddr = start; vaddr < start + len; vaddr += folio_size) {
> + const unsigned long pfn = pagemap_get_pfn(pagemap_fd, vaddr);
> + unsigned long i;
> +
> + /* Not present, or a tail page */
> + if (pfn == -1UL || pfn % nr_pages)
> + return false;
> +
> + for (i = 1; i < nr_pages; i++) {
> + char *page = vaddr + i * psize();
> +
> + if (pagemap_get_pfn(pagemap_fd, page) != pfn + i)
> + return false;
> + }
> +
> + if (!is_backed_by_folio(vaddr, order, pagemap_fd, kpageflags_fd))
> + return false;
> + }
> +
> + return true;
> +}
> +
> /* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */
> int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
> bool miss, bool wp, bool minor, uint64_t *ioctls)
> diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
> index 56a28ce7d029..e509fc4012a5 100644
> --- a/tools/testing/selftests/mm/vm_util.h
> +++ b/tools/testing/selftests/mm/vm_util.h
> @@ -99,6 +99,8 @@ int gather_folio_orders(char *vaddr_start, size_t len,
> int pagemap_fd, int kpageflags_fd, int orders[], int nr_orders);
> bool is_backed_by_folio(char *vaddr, int order, int pagemap_fd,
> int kpageflags_fd);
> +bool is_range_backed_by_order(char *start, size_t len, int order,
> + int pagemap_fd, int kpageflags_fd);
>
> int uffd_register(int uffd, void *addr, uint64_t len,
> bool miss, bool wp, bool minor);
^ permalink raw reply [flat|nested] 48+ messages in thread* Re: [PATCH v5 08/19] selftests/mm: add folio-order check for address ranges
2026-09-09 10:01 ` Baolin Wang
@ 2026-09-10 10:45 ` Kiryl Shutsemau
2026-09-10 11:14 ` Baolin Wang
0 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-10 10:45 UTC (permalink / raw)
To: Baolin Wang
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On Wed, Sep 09, 2026 at 06:01:13PM +0800, Baolin Wang wrote:
>
>
> On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> > From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
> >
> > An mTHP collapse test needs to know that a range is backed by folios of the
> > target order, and that they sit where a collapse would put them. Nothing
> > answers that today: is_backed_by_folio() classifies the folio behind a
> > single page, and check_huge_anon() reads smaps AnonHugePages, which only
> > accounts PMD mappings.
>
> Have you checked check_large_folios() in vm_util.c? It seems to meet your
> requirements, or am I missing something?
The changelog is out of date, sorry: since 6dedaf0d46a9 check_huge_anon()
counts mTHP folios through check_large_folios() rather than reading
smaps, and it is what check_huge() already uses for mthp_khugepaged.
It could do this job too, called once per window.
What is_range_backed_by_order() adds is alignment check: the folio has
to sit at the window start. And check_large_folios() reopens two fds per
per call which kinda wasteful.
The collapse_order_* cases check three windows per case, so I wanted a
helper whose contract is "this window is one folio of this order".
I will fix the changelog if the series gets respun.
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 48+ messages in thread
* Re: [PATCH v5 08/19] selftests/mm: add folio-order check for address ranges
2026-09-10 10:45 ` Kiryl Shutsemau
@ 2026-09-10 11:14 ` Baolin Wang
2026-09-10 13:04 ` Kiryl Shutsemau
0 siblings, 1 reply; 48+ messages in thread
From: Baolin Wang @ 2026-09-10 11:14 UTC (permalink / raw)
To: Kiryl Shutsemau
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On 9/10/26 6:45 PM, Kiryl Shutsemau wrote:
> On Wed, Sep 09, 2026 at 06:01:13PM +0800, Baolin Wang wrote:
>>
>>
>> On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
>>> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>>>
>>> An mTHP collapse test needs to know that a range is backed by folios of the
>>> target order, and that they sit where a collapse would put them. Nothing
>>> answers that today: is_backed_by_folio() classifies the folio behind a
>>> single page, and check_huge_anon() reads smaps AnonHugePages, which only
>>> accounts PMD mappings.
>>
>> Have you checked check_large_folios() in vm_util.c? It seems to meet your
>> requirements, or am I missing something?
>
> The changelog is out of date, sorry: since 6dedaf0d46a9 check_huge_anon()
> counts mTHP folios through check_large_folios() rather than reading
> smaps, and it is what check_huge() already uses for mthp_khugepaged.
>
> It could do this job too, called once per window.
>
> What is_range_backed_by_order() adds is alignment check: the folio has
> to sit at the window start. And check_large_folios() reopens two fds per
> per call which kinda wasteful.
OK. But can we extend the check_huge_xxx() functions to meet your
requirements, for example by adding a check_aligned_huge_xxx() that
wraps the underlying implementation of check_large_folios()?
I still find it confusing that khugepaged.c has two separate sets of
functions for checking large folios.
Also, Yeoreum has also extended check_large_folios() further[1].
[1]
https://lore.kernel.org/all/20260907-fix_split-v5-2-822b810458bc@arm.com/
^ permalink raw reply [flat|nested] 48+ messages in thread
* Re: [PATCH v5 08/19] selftests/mm: add folio-order check for address ranges
2026-09-10 11:14 ` Baolin Wang
@ 2026-09-10 13:04 ` Kiryl Shutsemau
2026-09-11 2:50 ` Baolin Wang
0 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-10 13:04 UTC (permalink / raw)
To: Baolin Wang
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On Thu, Sep 10, 2026 at 07:14:42PM +0800, Baolin Wang wrote:
>
>
> On 9/10/26 6:45 PM, Kiryl Shutsemau wrote:
> > On Wed, Sep 09, 2026 at 06:01:13PM +0800, Baolin Wang wrote:
> > >
> > >
> > > On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> > > > From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
> > > >
> > > > An mTHP collapse test needs to know that a range is backed by folios of the
> > > > target order, and that they sit where a collapse would put them. Nothing
> > > > answers that today: is_backed_by_folio() classifies the folio behind a
> > > > single page, and check_huge_anon() reads smaps AnonHugePages, which only
> > > > accounts PMD mappings.
> > >
> > > Have you checked check_large_folios() in vm_util.c? It seems to meet your
> > > requirements, or am I missing something?
> >
> > The changelog is out of date, sorry: since 6dedaf0d46a9 check_huge_anon()
> > counts mTHP folios through check_large_folios() rather than reading
> > smaps, and it is what check_huge() already uses for mthp_khugepaged.
> >
> > It could do this job too, called once per window.
> >
> > What is_range_backed_by_order() adds is alignment check: the folio has
> > to sit at the window start. And check_large_folios() reopens two fds per
> > per call which kinda wasteful.
>
> OK. But can we extend the check_huge_xxx() functions to meet your
> requirements, for example by adding a check_aligned_huge_xxx() that wraps
> the underlying implementation of check_large_folios()?
>
> I still find it confusing that khugepaged.c has two separate sets of
> functions for checking large folios.
Agreed that two sets is one too many. Yeoreum's series rewrites
check_huge_xxx() on top of pagemap and kpageflags, which is what
is_backed_by_folio() already walks, so once that lands the two can meet:
the mTHP branch of check_huge() checks each window for a folio of the
order at its start, and is_range_backed_by_order() goes away or becomes
its internal. That also makes the generic mTHP cases stricter for free.
I would rather do that as a follow-up than fold it into either series
now, since both touch the same functions.
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 48+ messages in thread
* Re: [PATCH v5 08/19] selftests/mm: add folio-order check for address ranges
2026-09-10 13:04 ` Kiryl Shutsemau
@ 2026-09-11 2:50 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-11 2:50 UTC (permalink / raw)
To: Kiryl Shutsemau
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On 9/10/26 9:04 PM, Kiryl Shutsemau wrote:
> On Thu, Sep 10, 2026 at 07:14:42PM +0800, Baolin Wang wrote:
>>
>>
>> On 9/10/26 6:45 PM, Kiryl Shutsemau wrote:
>>> On Wed, Sep 09, 2026 at 06:01:13PM +0800, Baolin Wang wrote:
>>>>
>>>>
>>>> On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
>>>>> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>>>>>
>>>>> An mTHP collapse test needs to know that a range is backed by folios of the
>>>>> target order, and that they sit where a collapse would put them. Nothing
>>>>> answers that today: is_backed_by_folio() classifies the folio behind a
>>>>> single page, and check_huge_anon() reads smaps AnonHugePages, which only
>>>>> accounts PMD mappings.
>>>>
>>>> Have you checked check_large_folios() in vm_util.c? It seems to meet your
>>>> requirements, or am I missing something?
>>>
>>> The changelog is out of date, sorry: since 6dedaf0d46a9 check_huge_anon()
>>> counts mTHP folios through check_large_folios() rather than reading
>>> smaps, and it is what check_huge() already uses for mthp_khugepaged.
>>>
>>> It could do this job too, called once per window.
>>>
>>> What is_range_backed_by_order() adds is alignment check: the folio has
>>> to sit at the window start. And check_large_folios() reopens two fds per
>>> per call which kinda wasteful.
>>
>> OK. But can we extend the check_huge_xxx() functions to meet your
>> requirements, for example by adding a check_aligned_huge_xxx() that wraps
>> the underlying implementation of check_large_folios()?
>>
>> I still find it confusing that khugepaged.c has two separate sets of
>> functions for checking large folios.
>
> Agreed that two sets is one too many. Yeoreum's series rewrites
> check_huge_xxx() on top of pagemap and kpageflags, which is what
> is_backed_by_folio() already walks, so once that lands the two can meet:
> the mTHP branch of check_huge() checks each window for a folio of the
> order at its start, and is_range_backed_by_order() goes away or becomes
> its internal. That also makes the generic mTHP cases stricter for free.
Look forward to seeing this happen:)
> I would rather do that as a follow-up than fold it into either series
> now, since both touch the same functions.
Sure.
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 09/19] selftests/mm: add folio-order detection self-check
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (7 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 08/19] selftests/mm: add folio-order check for address ranges Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-08 12:50 ` [PATCH v5 10/19] selftests/mm: add khugepaged completion barrier helper Kiryl Shutsemau
` (10 subsequent siblings)
19 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
The khugepaged mTHP tests detect collapse results with the vm_util
folio-order helpers rather than smaps AnonHugePages, which only sees PMD
mappings. If those helpers are wrong, every case built on them is wrong
the same way, and nothing says so.
Check them directly. For every anon THP order the kernel supports, fault
memory in with only that order enabled. Require the helpers to classify
the backing as exactly that order: not the order below it, and base-page
memory as order 0.
Run it in the thp category, ahead of ./khugepaged, so a broken helper is
reported as itself rather than as a collapse failure. Verified on x86-64
4K (orders 0, 2-9) and arm64 64K (orders 0, 2-13).
The test needs ALIGN(), which hmm-tests.c and migration.c each defined
privately. Move it to vm_util.h and drop both copies.
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/Makefile | 1 +
.../testing/selftests/mm/folio_order_check.c | 122 ++++++++++++++++++
tools/testing/selftests/mm/hmm-tests.c | 1 -
tools/testing/selftests/mm/migration.c | 1 -
tools/testing/selftests/mm/run_vmtests.sh | 2 +
tools/testing/selftests/mm/vm_util.h | 2 +
6 files changed, 127 insertions(+), 2 deletions(-)
create mode 100644 tools/testing/selftests/mm/folio_order_check.c
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 2d5366196e30..2093fcf6e915 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -104,6 +104,7 @@ TEST_GEN_FILES += guard-regions
TEST_GEN_FILES += merge
TEST_GEN_FILES += rmap
TEST_GEN_FILES += folio_split_race_test
+TEST_GEN_FILES += folio_order_check
ifneq ($(ARCH),arm64)
TEST_GEN_FILES += soft-dirty
diff --git a/tools/testing/selftests/mm/folio_order_check.c b/tools/testing/selftests/mm/folio_order_check.c
new file mode 100644
index 000000000000..fa736c9f701a
--- /dev/null
+++ b/tools/testing/selftests/mm/folio_order_check.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Self-check for the vm_util folio-order helpers, is_backed_by_folio() and
+ * is_range_backed_by_order(), which the khugepaged mTHP cases use to detect
+ * collapse results. For every anon THP order the kernel supports, fault
+ * memory in with only that order enabled and require the helpers to report
+ * exactly that order.
+ */
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include "kselftest.h"
+#include "vm_util.h"
+#include "hugepage_settings.h"
+
+static int pagemap_fd;
+static int kpageflags_fd;
+
+static char *alloc_aligned(size_t size)
+{
+ size_t len = size * 2;
+ char *p, *aligned;
+
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+ if (p == MAP_FAILED)
+ ksft_exit_fail_perror("mmap()");
+
+ aligned = (char *)ALIGN((uintptr_t)p, size);
+ if (aligned != p)
+ munmap(p, aligned - p);
+ if (aligned + size != p + len)
+ munmap(aligned + size, p + len - aligned - size);
+
+ return aligned;
+}
+
+static void check_order(int order)
+{
+ struct thp_settings settings = *thp_current_settings();
+ size_t size = psize() << order;
+ bool ok = true;
+ char *p;
+ int i;
+
+ for (i = 0; i < NR_ORDERS; i++)
+ settings.hugepages[i].enabled = THP_NEVER;
+ if (order)
+ settings.hugepages[order].enabled = THP_ALWAYS;
+ thp_push_settings(&settings);
+
+ p = alloc_aligned(size);
+ *p = 1;
+
+ if (!is_range_backed_by_order(p, size, order, pagemap_fd, kpageflags_fd)) {
+ ksft_print_msg("order %d not detected after fault\n", order);
+ ok = false;
+ }
+
+ /* A lower order must be rejected: the folio is larger */
+ if (order && is_range_backed_by_order(p, size, order - 1,
+ pagemap_fd, kpageflags_fd)) {
+ ksft_print_msg("order %d also reported as order %d\n",
+ order, order - 1);
+ ok = false;
+ }
+
+ /* A large folio must not pass as order 0 */
+ if (order && is_range_backed_by_order(p, size, 0,
+ pagemap_fd, kpageflags_fd)) {
+ ksft_print_msg("order %d also reported as order 0\n", order);
+ ok = false;
+ }
+
+ munmap(p, size);
+ thp_pop_settings();
+
+ ksft_test_result(ok, "order %d classified\n", order);
+}
+
+int main(void)
+{
+ struct thp_settings settings;
+ unsigned long orders;
+ int order;
+
+ ksft_print_header();
+
+ if (!thp_available())
+ ksft_exit_skip("Transparent Hugepages not available\n");
+
+ pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
+ if (pagemap_fd < 0)
+ ksft_exit_fail_perror("open(/proc/self/pagemap)");
+ kpageflags_fd = open("/proc/kpageflags", O_RDONLY);
+ if (kpageflags_fd < 0)
+ ksft_exit_skip("open(/proc/kpageflags) requires root\n");
+
+ orders = thp_supported_orders();
+ if (!orders)
+ ksft_exit_skip("No supported THP orders\n");
+
+ ksft_set_plan(__builtin_popcountl(orders) + 1);
+
+ thp_save_settings();
+ thp_read_settings(&settings);
+ /* Base of the settings stack; the bottom entry is never popped */
+ thp_push_settings(&settings);
+
+ check_order(0);
+ for (order = 1; order < NR_ORDERS; order++) {
+ if (!(orders & (1UL << order)))
+ continue;
+ check_order(order);
+ }
+
+ ksft_finished();
+}
diff --git a/tools/testing/selftests/mm/hmm-tests.c b/tools/testing/selftests/mm/hmm-tests.c
index e2642eca0d02..df426f9218e7 100644
--- a/tools/testing/selftests/mm/hmm-tests.c
+++ b/tools/testing/selftests/mm/hmm-tests.c
@@ -65,7 +65,6 @@ enum {
#define HMM_PATH_MAX 64
#define NTIMES 10
-#define ALIGN(x, a) (((x) + (a - 1)) & (~((a) - 1)))
/* Just the flags we need, copied from mm.h: */
#ifndef FOLL_WRITE
diff --git a/tools/testing/selftests/mm/migration.c b/tools/testing/selftests/mm/migration.c
index f19d53c69576..fd35f8a7b5b8 100644
--- a/tools/testing/selftests/mm/migration.c
+++ b/tools/testing/selftests/mm/migration.c
@@ -20,7 +20,6 @@
#define TWOMEG (2<<20)
#define RUNTIME (20)
-#define ALIGN(x, a) (((x) + (a - 1)) & (~((a) - 1)))
HUGETLB_SETUP_DEFAULT_PAGES(1)
diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh
index d09f9f6a384e..2652a7920b80 100755
--- a/tools/testing/selftests/mm/run_vmtests.sh
+++ b/tools/testing/selftests/mm/run_vmtests.sh
@@ -402,6 +402,8 @@ CATEGORY="pfnmap" run_test ./pfnmap
# COW tests
CATEGORY="cow" run_test ./cow
+CATEGORY="thp" run_test ./folio_order_check
+
CATEGORY="thp" run_test ./khugepaged
CATEGORY="thp" run_test ./khugepaged -s 2
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index e509fc4012a5..3be430e01901 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -10,6 +10,8 @@
#include <linux/fs.h>
#define BIT_ULL(nr) (1ULL << (nr))
+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
+
#define PM_SOFT_DIRTY BIT_ULL(55)
#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
#define PM_UFFD_WP BIT_ULL(57)
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* [PATCH v5 10/19] selftests/mm: add khugepaged completion barrier helper
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (8 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 09/19] selftests/mm: add folio-order detection self-check Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-10 1:17 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 11/19] selftests/mm: add order-parameterized khugepaged collapse cases Kiryl Shutsemau
` (9 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
A khugepaged test has to tell "not collapsed" from "not scanned yet", and
nothing in the selftests can. wait_for_scan() in khugepaged.c comes
closest: it polls full_scans until the counter has advanced by two, since
the pass in progress may already have passed the test's mm. But it only
returns in time if scan_sleep_millisecs happens to be short, and it is
private to that one test.
Add khugepaged_full_pass() to hugepage_settings, built on the same
advance-by-two wait but driven through sysfs: a store to
scan_sleep_millisecs wakes the daemon, so the barrier completes whatever
the scan cadence. A store made while the daemon is scanning rather than
sleeping is lost, so the helper keeps storing until the pass lands.
One wake completes one pass only if pages_to_scan covers every mm on the
list, so callers need it large.
Settings pushes must not start passes of their own. A store to either
sleep knob wakes the daemon, so thp_write_settings() now writes a
khugepaged knob only when its value changes.
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
.../testing/selftests/mm/hugepage_settings.c | 60 ++++++++++++++++---
.../testing/selftests/mm/hugepage_settings.h | 2 +
2 files changed, 53 insertions(+), 9 deletions(-)
diff --git a/tools/testing/selftests/mm/hugepage_settings.c b/tools/testing/selftests/mm/hugepage_settings.c
index d7917dce3aba..ca73f9ac8e9b 100644
--- a/tools/testing/selftests/mm/hugepage_settings.c
+++ b/tools/testing/selftests/mm/hugepage_settings.c
@@ -183,6 +183,13 @@ void thp_read_settings(struct thp_settings *settings)
}
}
+/* A store to either sleep knob wakes khugepaged, so write only on change */
+static void thp_update_num(const char *name, unsigned long num)
+{
+ if (thp_read_num(name) != num)
+ thp_write_num(name, num);
+}
+
void thp_write_settings(struct thp_settings *settings)
{
struct khugepaged_settings *khugepaged = &settings->khugepaged;
@@ -198,15 +205,15 @@ void thp_write_settings(struct thp_settings *settings)
shmem_enabled_strings[settings->shmem_enabled]);
thp_write_num("use_zero_page", settings->use_zero_page);
- thp_write_num("khugepaged/defrag", khugepaged->defrag);
- thp_write_num("khugepaged/alloc_sleep_millisecs",
- khugepaged->alloc_sleep_millisecs);
- thp_write_num("khugepaged/scan_sleep_millisecs",
- khugepaged->scan_sleep_millisecs);
- thp_write_num("khugepaged/max_ptes_none", khugepaged->max_ptes_none);
- thp_write_num("khugepaged/max_ptes_swap", khugepaged->max_ptes_swap);
- thp_write_num("khugepaged/max_ptes_shared", khugepaged->max_ptes_shared);
- thp_write_num("khugepaged/pages_to_scan", khugepaged->pages_to_scan);
+ thp_update_num("khugepaged/defrag", khugepaged->defrag);
+ thp_update_num("khugepaged/alloc_sleep_millisecs",
+ khugepaged->alloc_sleep_millisecs);
+ thp_update_num("khugepaged/scan_sleep_millisecs",
+ khugepaged->scan_sleep_millisecs);
+ thp_update_num("khugepaged/max_ptes_none", khugepaged->max_ptes_none);
+ thp_update_num("khugepaged/max_ptes_swap", khugepaged->max_ptes_swap);
+ thp_update_num("khugepaged/max_ptes_shared", khugepaged->max_ptes_shared);
+ thp_update_num("khugepaged/pages_to_scan", khugepaged->pages_to_scan);
if (dev_queue_read_ahead_path[0])
write_num(dev_queue_read_ahead_path, settings->read_ahead_kb);
@@ -230,6 +237,41 @@ void thp_write_settings(struct thp_settings *settings)
}
}
+/*
+ * Wait for a full khugepaged scan pass that started after this call: the
+ * pass in progress may already have passed this mm, so full_scans has to
+ * advance twice.
+ *
+ * A store to scan_sleep_millisecs wakes the daemon, but one made while it
+ * is scanning rather than sleeping is lost, so keep storing until the pass
+ * lands.
+ *
+ * One wake is one pass only if pages_to_scan covers every mm on the list.
+ */
+bool khugepaged_full_pass(unsigned int timeout_s)
+{
+ unsigned long deadline_ms = timeout_s * 1000UL;
+ unsigned long elapsed_ms = 0, poll_ms = 10;
+ unsigned long sleep_ms;
+ int pass;
+
+ sleep_ms = thp_read_num("khugepaged/scan_sleep_millisecs");
+ for (pass = 0; pass < 2; pass++) {
+ unsigned long target =
+ thp_read_num("khugepaged/full_scans") + 1;
+
+ while (thp_read_num("khugepaged/full_scans") < target) {
+ if (elapsed_ms >= deadline_ms)
+ return false;
+ thp_write_num("khugepaged/scan_sleep_millisecs",
+ sleep_ms);
+ usleep(poll_ms * 1000);
+ elapsed_ms += poll_ms;
+ }
+ }
+ return true;
+}
+
struct thp_settings *thp_current_settings(void)
{
if (!settings_index) {
diff --git a/tools/testing/selftests/mm/hugepage_settings.h b/tools/testing/selftests/mm/hugepage_settings.h
index a1d12e2ffd62..2ea169d11796 100644
--- a/tools/testing/selftests/mm/hugepage_settings.h
+++ b/tools/testing/selftests/mm/hugepage_settings.h
@@ -83,6 +83,8 @@ static inline void thp_save_settings(void)
hugepage_save_settings(/* thp = */ true, /* hugetlb = */ false);
}
+bool khugepaged_full_pass(unsigned int timeout_s);
+
void thp_set_read_ahead_path(char *path);
unsigned long thp_supported_orders(void);
unsigned long thp_shmem_supported_orders(void);
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 10/19] selftests/mm: add khugepaged completion barrier helper
2026-09-08 12:50 ` [PATCH v5 10/19] selftests/mm: add khugepaged completion barrier helper Kiryl Shutsemau
@ 2026-09-10 1:17 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-10 1:17 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> A khugepaged test has to tell "not collapsed" from "not scanned yet", and
> nothing in the selftests can. wait_for_scan() in khugepaged.c comes
> closest: it polls full_scans until the counter has advanced by two, since
> the pass in progress may already have passed the test's mm. But it only
> returns in time if scan_sleep_millisecs happens to be short, and it is
> private to that one test.
>
> Add khugepaged_full_pass() to hugepage_settings, built on the same
> advance-by-two wait but driven through sysfs: a store to
> scan_sleep_millisecs wakes the daemon, so the barrier completes whatever
> the scan cadence. A store made while the daemon is scanning rather than
> sleeping is lost, so the helper keeps storing until the pass lands.
>
> One wake completes one pass only if pages_to_scan covers every mm on the
> list, so callers need it large.
>
> Settings pushes must not start passes of their own. A store to either
> sleep knob wakes the daemon, so thp_write_settings() now writes a
> khugepaged knob only when its value changes.
>
> Assisted-by: LLM
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
Make sense to me.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 11/19] selftests/mm: add order-parameterized khugepaged collapse cases
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (9 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 10/19] selftests/mm: add khugepaged completion barrier helper Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-10 4:59 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 12/19] selftests/mm: parameterize the mixed-source collapse case by source order Kiryl Shutsemau
` (8 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
The mthp_khugepaged context runs the generic cases at a sub-PMD order,
which answers how many folios of that order a range ends up with. It
cannot say which order-sized window they landed in, so "the populated
window collapsed" and "the empty window next to it collapsed instead" look
alike.
Add four cases that check each window on its own, with the folio-order
helpers in vm_util:
- collapse_order_single_window(): only the populated window collapses;
- collapse_order_partial_window(): the default max_ptes_none lets a window
with one present PTE collapse;
- collapse_order_max_ptes_none(): with max_ptes_none=0 a full window
collapses and one missing a page does not;
- collapse_order_mixed_sources(): sources that are already large folios of
a smaller order collapse to the target.
Each case faults its region before MADV_HUGEPAGE with only the target
order enabled, so the sources are order 0 and the result can only come
from khugepaged. They wait for a full pass rather than for the result to
appear: without a completed pass, "not collapsed" and "not scanned yet"
are the same thing.
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged.c | 212 ++++++++++++++++++++++++
1 file changed, 212 insertions(+)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index e9bc8fe8a1f8..fb4efaf67c40 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -31,6 +31,8 @@ static unsigned long page_size;
static int hpage_pmd_nr;
static int anon_order;
static int collapse_order;
+static int pagemap_fd = -1;
+static int kpageflags_fd = -1;
#define PID_SMAPS "/proc/self/smaps"
#define TEST_FILE "collapse_test_file"
@@ -1207,6 +1209,198 @@ static void madvise_retracted_page_tables(struct collapse_context *c,
ksft_test_result_report(exit_status, "%s\n", __func__);
}
+/* Smallest order khugepaged will consider for mTHP collapse */
+#define MIN_MTHP_ORDER 2
+
+/* Time budget for one khugepaged pass in the collapse_order_* cases */
+#define MTHP_PASS_TIMEOUT_S 30
+
+static size_t mthp_window_size(void)
+{
+ return page_size << collapse_order;
+}
+
+static void mthp_push_target_order(void)
+{
+ struct thp_settings settings = *thp_current_settings();
+ int i;
+
+ /*
+ * Only the target order, and only for madvise: the cases fault their
+ * region first, so the sources stay order 0 whatever -s asked for.
+ */
+ settings.thp_enabled = THP_NEVER;
+ for (i = 0; i < NR_ORDERS; i++)
+ settings.hugepages[i].enabled = THP_NEVER;
+ settings.hugepages[collapse_order].enabled = THP_MADVISE;
+ thp_push_settings(&settings);
+}
+
+static bool all_windows_at_order(void *p, size_t len)
+{
+ return is_range_backed_by_order(p, len, collapse_order,
+ pagemap_fd, kpageflags_fd);
+}
+
+static bool any_window_at_order(void *p, size_t len)
+{
+ size_t window = mthp_window_size();
+ char *addr = p;
+
+ for (; len >= window; addr += window, len -= window) {
+ if (all_windows_at_order(addr, window))
+ return true;
+ }
+ return false;
+}
+
+static void collapse_order_single_window(struct collapse_context *c,
+ struct mem_ops *ops)
+{
+ size_t window = mthp_window_size();
+ void *p;
+
+ mthp_push_target_order();
+
+ p = ops->setup_area(1);
+ ops->fault(p, window, 2 * window);
+ if (any_window_at_order(p, hpage_pmd_size))
+ ksft_exit_fail_msg("Unexpected large folio after fault\n");
+
+ if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
+ ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
+ ksft_print_msg("Collapse one fully populated window...");
+ if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
+ fail("Timeout");
+ else if (all_windows_at_order(p + window, window) &&
+ !any_window_at_order(p, window) &&
+ !any_window_at_order(p + 2 * window,
+ hpage_pmd_size - 2 * window))
+ success("OK");
+ else
+ fail("Fail");
+
+ validate_memory(p, window, 2 * window);
+ ops->cleanup_area(p, hpage_pmd_size);
+ thp_pop_settings();
+ ksft_test_result_report(exit_status, "%s\n", __func__);
+}
+
+static void collapse_order_partial_window(struct collapse_context *c,
+ struct mem_ops *ops)
+{
+ void *p;
+
+ mthp_push_target_order();
+
+ p = ops->setup_area(1);
+ ops->fault(p, 0, page_size);
+ if (any_window_at_order(p, hpage_pmd_size))
+ ksft_exit_fail_msg("Unexpected large folio after fault\n");
+
+ if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
+ ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
+ ksft_print_msg("Collapse window with single PTE entry present...");
+ if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
+ fail("Timeout");
+ else if (all_windows_at_order(p, mthp_window_size()))
+ success("OK");
+ else
+ fail("Fail");
+
+ validate_memory(p, 0, page_size);
+ ops->cleanup_area(p, hpage_pmd_size);
+ thp_pop_settings();
+ ksft_test_result_report(exit_status, "%s\n", __func__);
+}
+
+static void collapse_order_max_ptes_none(struct collapse_context *c,
+ struct mem_ops *ops)
+{
+ struct thp_settings settings;
+ size_t window = mthp_window_size();
+ void *p;
+
+ mthp_push_target_order();
+ settings = *thp_current_settings();
+ settings.khugepaged.max_ptes_none = 0;
+ thp_push_settings(&settings);
+
+ p = ops->setup_area(1);
+ ops->fault(p, 0, 2 * window - page_size);
+ if (any_window_at_order(p, hpage_pmd_size))
+ ksft_exit_fail_msg("Unexpected large folio after fault\n");
+
+ if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
+ ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
+ ksft_print_msg("Collapse full window, not the one missing a page...");
+ if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
+ fail("Timeout");
+ else if (all_windows_at_order(p, window) &&
+ !any_window_at_order(p + window, window))
+ success("OK");
+ else
+ fail("Fail");
+
+ validate_memory(p, 0, 2 * window - page_size);
+ ops->cleanup_area(p, hpage_pmd_size);
+ thp_pop_settings();
+ thp_pop_settings();
+ ksft_test_result_report(exit_status, "%s\n", __func__);
+}
+
+static void collapse_order_mixed_sources(struct collapse_context *c,
+ struct mem_ops *ops)
+{
+ struct thp_settings settings;
+ void *p;
+
+ if (collapse_order <= MIN_MTHP_ORDER) {
+ ksft_test_result_skip("%s: no source order below target\n",
+ __func__);
+ return;
+ }
+
+ mthp_push_target_order();
+
+ settings = *thp_current_settings();
+ settings.hugepages[MIN_MTHP_ORDER].enabled = THP_ALWAYS;
+ thp_push_settings(&settings);
+ p = ops->setup_area(1);
+ ops->fault(p, 0, hpage_pmd_size);
+ thp_pop_settings();
+
+ /*
+ * The allocator can fall back to smaller folios under fragmentation;
+ * having nothing to collapse from is not a failure.
+ */
+ if (!is_range_backed_by_order(p, hpage_pmd_size, MIN_MTHP_ORDER,
+ pagemap_fd, kpageflags_fd)) {
+ ksft_print_msg("No order-%d sources to collapse...",
+ MIN_MTHP_ORDER);
+ skip("Skip");
+ ops->cleanup_area(p, hpage_pmd_size);
+ thp_pop_settings();
+ ksft_test_result_report(exit_status, "%s\n", __func__);
+ return;
+ }
+
+ if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
+ ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
+ ksft_print_msg("Collapse region backed by smaller large folios...");
+ if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
+ fail("Timeout");
+ else if (all_windows_at_order(p, hpage_pmd_size))
+ success("OK");
+ else
+ fail("Fail");
+
+ validate_memory(p, 0, hpage_pmd_size);
+ ops->cleanup_area(p, hpage_pmd_size);
+ thp_pop_settings();
+ ksft_test_result_report(exit_status, "%s\n", __func__);
+}
+
static void usage(void)
{
fprintf(stderr, "\nUsage: ./khugepaged [OPTIONS] <test type> [dir]\n\n");
@@ -1375,6 +1569,20 @@ int main(int argc, char **argv)
parse_test_type(argc, argv);
+ if (mthp_khugepaged_context &&
+ !(thp_supported_orders() & (1UL << collapse_order)))
+ ksft_exit_skip("Order %d is not a supported anon THP order\n",
+ collapse_order);
+
+ if (mthp_khugepaged_context) {
+ pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
+ if (pagemap_fd < 0)
+ ksft_exit_fail_perror("open(/proc/self/pagemap)");
+ kpageflags_fd = open("/proc/kpageflags", O_RDONLY);
+ if (kpageflags_fd < 0)
+ ksft_exit_fail_perror("open(/proc/kpageflags)");
+ }
+
setbuf(stdout, NULL);
/*
@@ -1425,6 +1633,10 @@ int main(int argc, char **argv)
TEST(collapse_empty, madvise_context, anon_ops);
TEST(collapse_single_mthp, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_order_single_window, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_order_partial_window, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_order_max_ptes_none, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_order_mixed_sources, mthp_khugepaged_context, anon_ops);
TEST(collapse_single_pte_entry, khugepaged_context, anon_ops);
TEST(collapse_single_pte_entry, khugepaged_context, read_only_file_ops);
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 11/19] selftests/mm: add order-parameterized khugepaged collapse cases
2026-09-08 12:50 ` [PATCH v5 11/19] selftests/mm: add order-parameterized khugepaged collapse cases Kiryl Shutsemau
@ 2026-09-10 4:59 ` Baolin Wang
2026-09-10 10:53 ` Kiryl Shutsemau
0 siblings, 1 reply; 48+ messages in thread
From: Baolin Wang @ 2026-09-10 4:59 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> The mthp_khugepaged context runs the generic cases at a sub-PMD order,
> which answers how many folios of that order a range ends up with. It
> cannot say which order-sized window they landed in, so "the populated
> window collapsed" and "the empty window next to it collapsed instead" look
> alike.
>
> Add four cases that check each window on its own, with the folio-order
> helpers in vm_util:
>
> - collapse_order_single_window(): only the populated window collapses;
> - collapse_order_partial_window(): the default max_ptes_none lets a window
> with one present PTE collapse;
> - collapse_order_max_ptes_none(): with max_ptes_none=0 a full window
> collapses and one missing a page does not;
> - collapse_order_mixed_sources(): sources that are already large folios of
> a smaller order collapse to the target.
>
> Each case faults its region before MADV_HUGEPAGE with only the target
> order enabled, so the sources are order 0 and the result can only come
> from khugepaged. They wait for a full pass rather than for the result to
> appear: without a completed pass, "not collapsed" and "not scanned yet"
> are the same thing.
>
> Assisted-by: LLM
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
> tools/testing/selftests/mm/khugepaged.c | 212 ++++++++++++++++++++++++
> 1 file changed, 212 insertions(+)
>
> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> index e9bc8fe8a1f8..fb4efaf67c40 100644
> --- a/tools/testing/selftests/mm/khugepaged.c
> +++ b/tools/testing/selftests/mm/khugepaged.c
> @@ -31,6 +31,8 @@ static unsigned long page_size;
> static int hpage_pmd_nr;
> static int anon_order;
> static int collapse_order;
> +static int pagemap_fd = -1;
> +static int kpageflags_fd = -1;
>
> #define PID_SMAPS "/proc/self/smaps"
> #define TEST_FILE "collapse_test_file"
> @@ -1207,6 +1209,198 @@ static void madvise_retracted_page_tables(struct collapse_context *c,
> ksft_test_result_report(exit_status, "%s\n", __func__);
> }
>
> +/* Smallest order khugepaged will consider for mTHP collapse */
> +#define MIN_MTHP_ORDER 2
> +
> +/* Time budget for one khugepaged pass in the collapse_order_* cases */
> +#define MTHP_PASS_TIMEOUT_S 30
> +
> +static size_t mthp_window_size(void)
> +{
> + return page_size << collapse_order;
> +}
> +
> +static void mthp_push_target_order(void)
> +{
> + struct thp_settings settings = *thp_current_settings();
> + int i;
> +
> + /*
> + * Only the target order, and only for madvise: the cases fault their
> + * region first, so the sources stay order 0 whatever -s asked for.
> + */
> + settings.thp_enabled = THP_NEVER;
> + for (i = 0; i < NR_ORDERS; i++)
> + settings.hugepages[i].enabled = THP_NEVER;
> + settings.hugepages[collapse_order].enabled = THP_MADVISE;
> + thp_push_settings(&settings);
> +}
> +
> +static bool all_windows_at_order(void *p, size_t len)
> +{
> + return is_range_backed_by_order(p, len, collapse_order,
> + pagemap_fd, kpageflags_fd);
Like I mentioned in patch 8, you can implement these helpers using
check_large_folios() in vm_util.c. Then you do not need to add new
'pagemap_fd' and 'kpageflags_fd' variables.
> +}
> +
> +static bool any_window_at_order(void *p, size_t len)
> +{
> + size_t window = mthp_window_size();
> + char *addr = p;
> +
> + for (; len >= window; addr += window, len -= window) {
> + if (all_windows_at_order(addr, window))
> + return true;
> + }
> + return false;
> +}
> +
> +static void collapse_order_single_window(struct collapse_context *c,
> + struct mem_ops *ops)
> +{
> + size_t window = mthp_window_size();
> + void *p;
> +
> + mthp_push_target_order();
> +
> + p = ops->setup_area(1);
> + ops->fault(p, window, 2 * window);
> + if (any_window_at_order(p, hpage_pmd_size))
> + ksft_exit_fail_msg("Unexpected large folio after fault\n");
> +
> + if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
> + ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
> + ksft_print_msg("Collapse one fully populated window...");
> + if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
> + fail("Timeout");
> + else if (all_windows_at_order(p + window, window) &&
> + !any_window_at_order(p, window) &&
> + !any_window_at_order(p + 2 * window,
> + hpage_pmd_size - 2 * window))
> + success("OK");
> + else
> + fail("Fail");
> +
> + validate_memory(p, window, 2 * window);
> + ops->cleanup_area(p, hpage_pmd_size);
> + thp_pop_settings();
> + ksft_test_result_report(exit_status, "%s\n", __func__);
> +}
> +
> +static void collapse_order_partial_window(struct collapse_context *c,
> + struct mem_ops *ops)
> +{
> + void *p;
> +
> + mthp_push_target_order();
> +
> + p = ops->setup_area(1);
> + ops->fault(p, 0, page_size);
> + if (any_window_at_order(p, hpage_pmd_size))
> + ksft_exit_fail_msg("Unexpected large folio after fault\n");
> +
> + if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
> + ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
> + ksft_print_msg("Collapse window with single PTE entry present...");
> + if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
> + fail("Timeout");
> + else if (all_windows_at_order(p, mthp_window_size()))
> + success("OK");
> + else
> + fail("Fail");
> +
> + validate_memory(p, 0, page_size);
> + ops->cleanup_area(p, hpage_pmd_size);
> + thp_pop_settings();
> + ksft_test_result_report(exit_status, "%s\n", __func__);
> +}
> +
> +static void collapse_order_max_ptes_none(struct collapse_context *c,
> + struct mem_ops *ops)
> +{
> + struct thp_settings settings;
> + size_t window = mthp_window_size();
> + void *p;
> +
> + mthp_push_target_order();
> + settings = *thp_current_settings();
> + settings.khugepaged.max_ptes_none = 0;
> + thp_push_settings(&settings);
> +
> + p = ops->setup_area(1);
> + ops->fault(p, 0, 2 * window - page_size);
> + if (any_window_at_order(p, hpage_pmd_size))
> + ksft_exit_fail_msg("Unexpected large folio after fault\n");
> +
> + if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
> + ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
> + ksft_print_msg("Collapse full window, not the one missing a page...");
> + if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
> + fail("Timeout");
> + else if (all_windows_at_order(p, window) &&
> + !any_window_at_order(p + window, window))
> + success("OK");
> + else
> + fail("Fail");
> +
> + validate_memory(p, 0, 2 * window - page_size);
> + ops->cleanup_area(p, hpage_pmd_size);
> + thp_pop_settings();
> + thp_pop_settings();
> + ksft_test_result_report(exit_status, "%s\n", __func__);
> +}
> +
> +static void collapse_order_mixed_sources(struct collapse_context *c,
> + struct mem_ops *ops)
> +{
> + struct thp_settings settings;
> + void *p;
> +
> + if (collapse_order <= MIN_MTHP_ORDER) {
> + ksft_test_result_skip("%s: no source order below target\n",
> + __func__);
> + return;
> + }
> +
> + mthp_push_target_order();
> +
> + settings = *thp_current_settings();
> + settings.hugepages[MIN_MTHP_ORDER].enabled = THP_ALWAYS;
> + thp_push_settings(&settings);
> + p = ops->setup_area(1);
> + ops->fault(p, 0, hpage_pmd_size);
> + thp_pop_settings();
> +
> + /*
> + * The allocator can fall back to smaller folios under fragmentation;
> + * having nothing to collapse from is not a failure.
> + */
> + if (!is_range_backed_by_order(p, hpage_pmd_size, MIN_MTHP_ORDER,
> + pagemap_fd, kpageflags_fd)) {
> + ksft_print_msg("No order-%d sources to collapse...",
> + MIN_MTHP_ORDER);
> + skip("Skip");
> + ops->cleanup_area(p, hpage_pmd_size);
> + thp_pop_settings();
> + ksft_test_result_report(exit_status, "%s\n", __func__);
> + return;
> + }
> +
> + if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
> + ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
> + ksft_print_msg("Collapse region backed by smaller large folios...");
> + if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
> + fail("Timeout");
> + else if (all_windows_at_order(p, hpage_pmd_size))
> + success("OK");
> + else
> + fail("Fail");
> +
> + validate_memory(p, 0, hpage_pmd_size);
> + ops->cleanup_area(p, hpage_pmd_size);
> + thp_pop_settings();
> + ksft_test_result_report(exit_status, "%s\n", __func__);
> +}
> +
> static void usage(void)
> {
> fprintf(stderr, "\nUsage: ./khugepaged [OPTIONS] <test type> [dir]\n\n");
> @@ -1375,6 +1569,20 @@ int main(int argc, char **argv)
>
> parse_test_type(argc, argv);
>
> + if (mthp_khugepaged_context &&
> + !(thp_supported_orders() & (1UL << collapse_order)))
> + ksft_exit_skip("Order %d is not a supported anon THP order\n",
> + collapse_order);
This check can be moved into parse_test_type(), where the
'mthp_khugepaged' parameter is parsed.
> +
> + if (mthp_khugepaged_context) {
> + pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
> + if (pagemap_fd < 0)
> + ksft_exit_fail_perror("open(/proc/self/pagemap)");
> + kpageflags_fd = open("/proc/kpageflags", O_RDONLY);
> + if (kpageflags_fd < 0)
> + ksft_exit_fail_perror("open(/proc/kpageflags)");
When you change to use check_large_folios(), these fds can be removed
from this file.
> + }
> +
> setbuf(stdout, NULL);
>
> /*
> @@ -1425,6 +1633,10 @@ int main(int argc, char **argv)
> TEST(collapse_empty, madvise_context, anon_ops);
>
> TEST(collapse_single_mthp, mthp_khugepaged_context, anon_ops);
> + TEST(collapse_order_single_window, mthp_khugepaged_context, anon_ops);
> + TEST(collapse_order_partial_window, mthp_khugepaged_context, anon_ops);
> + TEST(collapse_order_max_ptes_none, mthp_khugepaged_context, anon_ops);
> + TEST(collapse_order_mixed_sources, mthp_khugepaged_context, anon_ops);
These test cases look good to me. Thanks.
^ permalink raw reply [flat|nested] 48+ messages in thread* Re: [PATCH v5 11/19] selftests/mm: add order-parameterized khugepaged collapse cases
2026-09-10 4:59 ` Baolin Wang
@ 2026-09-10 10:53 ` Kiryl Shutsemau
0 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-10 10:53 UTC (permalink / raw)
To: Baolin Wang
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On Thu, Sep 10, 2026 at 12:59:14PM +0800, Baolin Wang wrote:
> > +static bool all_windows_at_order(void *p, size_t len)
> > +{
> > + return is_range_backed_by_order(p, len, collapse_order,
> > + pagemap_fd, kpageflags_fd);
>
> Like I mentioned in patch 8, you can implement these helpers using
> check_large_folios() in vm_util.c. Then you do not need to add new
> 'pagemap_fd' and 'kpageflags_fd' variables.
As I mentioned in reply to patch 8, I don't like that
check_large_folios() keeps re-opening file descriptors per call.
> > @@ -1375,6 +1569,20 @@ int main(int argc, char **argv)
> > parse_test_type(argc, argv);
> > + if (mthp_khugepaged_context &&
> > + !(thp_supported_orders() & (1UL << collapse_order)))
> > + ksft_exit_skip("Order %d is not a supported anon THP order\n",
> > + collapse_order);
>
> This check can be moved into parse_test_type(), where the 'mthp_khugepaged'
> parameter is parsed.
Patch 14 makes -c optional and turns this into the code that builds the
list of orders to run, next to the "-c has to be above -s" check. That
list wants thp_supported_orders() and belongs after parsing rather than in
it, so I left parse_test_type() to the range check.
> > @@ -1425,6 +1633,10 @@ int main(int argc, char **argv)
> > TEST(collapse_empty, madvise_context, anon_ops);
> > TEST(collapse_single_mthp, mthp_khugepaged_context, anon_ops);
> > + TEST(collapse_order_single_window, mthp_khugepaged_context, anon_ops);
> > + TEST(collapse_order_partial_window, mthp_khugepaged_context, anon_ops);
> > + TEST(collapse_order_max_ptes_none, mthp_khugepaged_context, anon_ops);
> > + TEST(collapse_order_mixed_sources, mthp_khugepaged_context, anon_ops);
>
> These test cases look good to me. Thanks.
Thanks for going through them!
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 12/19] selftests/mm: parameterize the mixed-source collapse case by source order
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (10 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 11/19] selftests/mm: add order-parameterized khugepaged collapse cases Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-10 5:09 ` Baolin Wang
2026-09-08 12:50 ` [PATCH v5 13/19] selftests/mm: cover a shared-source collapse write race Kiryl Shutsemau
` (7 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
collapse_order_mixed_sources() faults its region as order-2 folios and
collapses them to the -c target. Order 2 is below the contpte size on
every arm64 page size, so nothing in this suite collapses a contpte-mapped
source on purpose.
Let -s name the source order alongside -c. The case then faults at that
order, keeping order 2 when -s is absent, and the source order has to be a
supported mTHP order below the target. The other mTHP cases are
unaffected: mthp_push_target_order() enables only the target order.
"-s 5 -c 7" on arm64/64K then collapses contpte-mapped sources into a
larger mTHP.
Assisted-by: LLM
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged.c | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index fb4efaf67c40..b15cd07fc0b3 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -1352,11 +1352,13 @@ static void collapse_order_max_ptes_none(struct collapse_context *c,
static void collapse_order_mixed_sources(struct collapse_context *c,
struct mem_ops *ops)
{
+ int source_order = anon_order ? anon_order : MIN_MTHP_ORDER;
struct thp_settings settings;
void *p;
- if (collapse_order <= MIN_MTHP_ORDER) {
- ksft_test_result_skip("%s: no source order below target\n",
+ if (source_order >= collapse_order ||
+ !(thp_supported_orders() & (1UL << source_order))) {
+ ksft_test_result_skip("%s: no supported source order below target\n",
__func__);
return;
}
@@ -1364,7 +1366,7 @@ static void collapse_order_mixed_sources(struct collapse_context *c,
mthp_push_target_order();
settings = *thp_current_settings();
- settings.hugepages[MIN_MTHP_ORDER].enabled = THP_ALWAYS;
+ settings.hugepages[source_order].enabled = THP_ALWAYS;
thp_push_settings(&settings);
p = ops->setup_area(1);
ops->fault(p, 0, hpage_pmd_size);
@@ -1374,10 +1376,9 @@ static void collapse_order_mixed_sources(struct collapse_context *c,
* The allocator can fall back to smaller folios under fragmentation;
* having nothing to collapse from is not a failure.
*/
- if (!is_range_backed_by_order(p, hpage_pmd_size, MIN_MTHP_ORDER,
+ if (!is_range_backed_by_order(p, hpage_pmd_size, source_order,
pagemap_fd, kpageflags_fd)) {
- ksft_print_msg("No order-%d sources to collapse...",
- MIN_MTHP_ORDER);
+ ksft_print_msg("No order-%d sources to collapse...", source_order);
skip("Skip");
ops->cleanup_area(p, hpage_pmd_size);
thp_pop_settings();
@@ -1387,7 +1388,8 @@ static void collapse_order_mixed_sources(struct collapse_context *c,
if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
- ksft_print_msg("Collapse region backed by smaller large folios...");
+ ksft_print_msg("Collapse region backed by order-%d sources...",
+ source_order);
if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
fail("Timeout");
else if (all_windows_at_order(p, hpage_pmd_size))
@@ -1418,6 +1420,7 @@ static void usage(void)
fprintf(stderr, "\t\t-s: mTHP size, expressed as page order.\n");
fprintf(stderr, "\t\t Defaults to 0. Use this size for anon or shmem allocations.\n");
fprintf(stderr, "\t\t-c: collapse order for mTHP collapse, expressed as page order.\n");
+ fprintf(stderr, "\t\t -s, if set, is the source order for the mixed-source case.\n");
exit(1);
}
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 12/19] selftests/mm: parameterize the mixed-source collapse case by source order
2026-09-08 12:50 ` [PATCH v5 12/19] selftests/mm: parameterize the mixed-source collapse case by source order Kiryl Shutsemau
@ 2026-09-10 5:09 ` Baolin Wang
2026-09-10 10:58 ` Kiryl Shutsemau
0 siblings, 1 reply; 48+ messages in thread
From: Baolin Wang @ 2026-09-10 5:09 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> collapse_order_mixed_sources() faults its region as order-2 folios and
> collapses them to the -c target. Order 2 is below the contpte size on
> every arm64 page size, so nothing in this suite collapses a contpte-mapped
> source on purpose.
>
> Let -s name the source order alongside -c. The case then faults at that
> order, keeping order 2 when -s is absent, and the source order has to be a
> supported mTHP order below the target. The other mTHP cases are
> unaffected: mthp_push_target_order() enables only the target order.
>
> "-s 5 -c 7" on arm64/64K then collapses contpte-mapped sources into a
> larger mTHP.
Make sense. But we should validate invalid parameter combinations for
'-s' and '-c'. For example, when I set the following unreasonable
parameters, some test cases fail, so such invalid configurations should
be rejected.
[root@]# ./khugepaged -s 7 -c 6 mthp_khugepaged:anon
TAP version 13
# Save THP and khugepaged settings... OK
1..8
# Allocate huge page on fault... OK
# Split huge PMD on MADV_DONTNEED... OK
ok 1 allocate on fault and split
#
# Run test: collapse_full (mthp_khugepaged:anon)
# Collapse multiple fully populated PTE table.... Fail
not ok 2 collapse_full
#
# Run test: collapse_empty (mthp_khugepaged:anon)
# Do not collapse empty PTE table.... OK
ok 3 collapse_empty
#
# Run test: collapse_single_mthp (mthp_khugepaged:anon)
# Collapse PTE table with half PTE entries present.... Fail
not ok 4 collapse_single_mthp
#
# Run test: collapse_order_single_window (mthp_khugepaged:anon)
# Collapse one fully populated window... OK
ok 5 collapse_order_single_window
#
# Run test: collapse_order_partial_window (mthp_khugepaged:anon)
# Collapse window with single PTE entry present... OK
ok 6 collapse_order_partial_window
#
# Run test: collapse_order_max_ptes_none (mthp_khugepaged:anon)
# Collapse full window, not the one missing a page... OK
ok 7 collapse_order_max_ptes_none
#
# Run test: collapse_order_mixed_sources (mthp_khugepaged:anon)
ok 8 # SKIP collapse_order_mixed_sources: no supported source order
below target
# 1 skipped test(s) detected. Consider enabling relevant config options
to improve coverage.
# Totals: pass:5 fail:2 xfail:0 xpass:0 skip:1 error:0
> Assisted-by: LLM
> Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
> tools/testing/selftests/mm/khugepaged.c | 17 ++++++++++-------
> 1 file changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> index fb4efaf67c40..b15cd07fc0b3 100644
> --- a/tools/testing/selftests/mm/khugepaged.c
> +++ b/tools/testing/selftests/mm/khugepaged.c
> @@ -1352,11 +1352,13 @@ static void collapse_order_max_ptes_none(struct collapse_context *c,
> static void collapse_order_mixed_sources(struct collapse_context *c,
> struct mem_ops *ops)
> {
> + int source_order = anon_order ? anon_order : MIN_MTHP_ORDER;
> struct thp_settings settings;
> void *p;
>
> - if (collapse_order <= MIN_MTHP_ORDER) {
> - ksft_test_result_skip("%s: no source order below target\n",
> + if (source_order >= collapse_order ||
> + !(thp_supported_orders() & (1UL << source_order))) {
> + ksft_test_result_skip("%s: no supported source order below target\n",
> __func__);
> return;
> }
> @@ -1364,7 +1366,7 @@ static void collapse_order_mixed_sources(struct collapse_context *c,
> mthp_push_target_order();
>
> settings = *thp_current_settings();
> - settings.hugepages[MIN_MTHP_ORDER].enabled = THP_ALWAYS;
> + settings.hugepages[source_order].enabled = THP_ALWAYS;
> thp_push_settings(&settings);
> p = ops->setup_area(1);
> ops->fault(p, 0, hpage_pmd_size);
> @@ -1374,10 +1376,9 @@ static void collapse_order_mixed_sources(struct collapse_context *c,
> * The allocator can fall back to smaller folios under fragmentation;
> * having nothing to collapse from is not a failure.
> */
> - if (!is_range_backed_by_order(p, hpage_pmd_size, MIN_MTHP_ORDER,
> + if (!is_range_backed_by_order(p, hpage_pmd_size, source_order,
> pagemap_fd, kpageflags_fd)) {
> - ksft_print_msg("No order-%d sources to collapse...",
> - MIN_MTHP_ORDER);
> + ksft_print_msg("No order-%d sources to collapse...", source_order);
> skip("Skip");
> ops->cleanup_area(p, hpage_pmd_size);
> thp_pop_settings();
> @@ -1387,7 +1388,8 @@ static void collapse_order_mixed_sources(struct collapse_context *c,
>
> if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
> ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
> - ksft_print_msg("Collapse region backed by smaller large folios...");
> + ksft_print_msg("Collapse region backed by order-%d sources...",
> + source_order);
> if (!khugepaged_full_pass(MTHP_PASS_TIMEOUT_S))
> fail("Timeout");
> else if (all_windows_at_order(p, hpage_pmd_size))
> @@ -1418,6 +1420,7 @@ static void usage(void)
> fprintf(stderr, "\t\t-s: mTHP size, expressed as page order.\n");
> fprintf(stderr, "\t\t Defaults to 0. Use this size for anon or shmem allocations.\n");
> fprintf(stderr, "\t\t-c: collapse order for mTHP collapse, expressed as page order.\n");
> + fprintf(stderr, "\t\t -s, if set, is the source order for the mixed-source case.\n");
> exit(1);
> }
>
^ permalink raw reply [flat|nested] 48+ messages in thread* Re: [PATCH v5 12/19] selftests/mm: parameterize the mixed-source collapse case by source order
2026-09-10 5:09 ` Baolin Wang
@ 2026-09-10 10:58 ` Kiryl Shutsemau
0 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-10 10:58 UTC (permalink / raw)
To: Baolin Wang
Cc: akpm, david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, nico.pache, ziy, baohua, dev.jain, hughd,
lance.yang, liam, mhocko, ryan.roberts, shuah, surenb, vbabka,
agordeev, jgg, leon, kernel-team
On Thu, Sep 10, 2026 at 01:09:00PM +0800, Baolin Wang wrote:
>
>
> On 9/8/26 8:50 PM, Kiryl Shutsemau wrote:
> > From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
> >
> > collapse_order_mixed_sources() faults its region as order-2 folios and
> > collapses them to the -c target. Order 2 is below the contpte size on
> > every arm64 page size, so nothing in this suite collapses a contpte-mapped
> > source on purpose.
> >
> > Let -s name the source order alongside -c. The case then faults at that
> > order, keeping order 2 when -s is absent, and the source order has to be a
> > supported mTHP order below the target. The other mTHP cases are
> > unaffected: mthp_push_target_order() enables only the target order.
> >
> > "-s 5 -c 7" on arm64/64K then collapses contpte-mapped sources into a
> > larger mTHP.
>
> Make sense. But we should validate invalid parameter combinations for '-s'
> and '-c'. For example, when I set the following unreasonable parameters,
> some test cases fail, so such invalid configurations should be rejected.
Patch 14 adds that check; with the whole series applied,
"-s 7 -c 6 mthp_khugepaged:anon" stops before running anything:
1..0 # SKIP -c 6 needs a source order below it, -s says 7
So this is a minor bisectability issue: the check should have come with
this patch rather than two later. I will move it here if the series gets
respun.
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 13/19] selftests/mm: cover a shared-source collapse write race
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (11 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 12/19] selftests/mm: parameterize the mixed-source collapse case by source order Kiryl Shutsemau
@ 2026-09-08 12:50 ` Kiryl Shutsemau
2026-09-08 21:02 ` Kiryl Shutsemau
2026-09-08 12:51 ` [PATCH v5 14/19] selftests/mm: run every supported collapse order by default Kiryl Shutsemau
` (6 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:50 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
collapse_fork() checks that a fork-shared range collapses in the child
while the parent keeps its own pages, but the parent sits still while
that happens. Nothing checks that CoW isolation survives a collapse
racing with writes to the shared source.
Add a case where the parent writes to the shared range throughout the
child's collapse. CoW has to keep the two apart: the child must see the
content from before the fork, and the parent only its own writes.
The parent unshares one page every 10ms, starting only once the child
says it is about to collapse. Writing the range in a burst would break
CoW on all of it before the collapse begins, leaving the child to collapse
pages that are already exclusive to it.
Preparation for changing how collapse handles fork-shared sources.
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged.c | 100 ++++++++++++++++++++++++
1 file changed, 100 insertions(+)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index b15cd07fc0b3..f5d4847c50cd 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -1164,6 +1164,103 @@ static void collapse_max_ptes_shared(struct collapse_context *c, struct mem_ops
ksft_test_result_report(exit_status, "%s\n", __func__);
}
+/*
+ * The parent writes to the fork-shared range throughout the child's
+ * collapse. CoW must keep the two apart: the child sees the pre-fork
+ * content, the parent only its own writes.
+ */
+static void collapse_fork_cow_race(struct collapse_context *c, struct mem_ops *ops)
+{
+ const int stride = page_size / sizeof(int);
+ int wstatus, child_status, i, n;
+ unsigned long shared;
+ volatile int *ip;
+ pid_t child;
+ int sync[2];
+ char go = 1;
+ void *p;
+
+ /* At a page per 10 ms, 64 pages spread the writes across the collapse */
+ n = 64;
+ shared = n * page_size;
+
+ p = ops->setup_area(1);
+ /* Shared prefix, with the pre-fork pattern */
+ ops->fault(p, 0, shared);
+ if (pipe(sync))
+ ksft_exit_fail_perror("pipe()");
+
+ /* A volatile pointer so the stores are not merged or dropped */
+ ip = p;
+
+ ksft_print_msg("Fork, collapse in the child while the parent rewrites...");
+ child = fork();
+ if (!child) {
+ int collapse_status;
+
+ close(sync[0]);
+ /* Private remainder */
+ ops->fault(p, shared, hpage_pmd_size);
+ /* Start the parent unsharing, and give it a head start */
+ if (write(sync[1], &go, 1) != 1)
+ _exit(KSFT_FAIL);
+ usleep(5000);
+ c->collapse("Collapse a range the parent is writing to",
+ p, 1, ops, true);
+ collapse_status = exit_status;
+ for (i = 0; i < n; i++)
+ if (ip[i * stride] != i + 0xdead0000)
+ break;
+ if (i == n)
+ success("OK");
+ else
+ fail("Fail: child content");
+ /* The content check must not bury a failed collapse */
+ if (exit_status != KSFT_FAIL)
+ exit_status = collapse_status;
+ ops->cleanup_area(p, hpage_pmd_size);
+ _exit(exit_status);
+ }
+
+ close(sync[1]);
+ if (read(sync[0], &go, 1) != 1)
+ ksft_exit_fail_msg("child never reached the collapse\n");
+
+ /*
+ * Unshare one page at a time: a burst would break CoW on the whole
+ * range before the collapse starts, leaving nothing shared to collapse.
+ */
+ i = 0;
+ for (;;) {
+ if (i < n)
+ ip[i * stride] = i + 0xbeef0000;
+ i++;
+ usleep(10 * 1000);
+ if (waitpid(child, &wstatus, WNOHANG))
+ break;
+ }
+
+ /* Finish whatever the paced sweep did not reach */
+ for (; i < n; i++)
+ ip[i * stride] = i + 0xbeef0000;
+ /* A child that died reading the racing pages is a failure, not a zero */
+ child_status = WIFEXITED(wstatus) ? WEXITSTATUS(wstatus) : KSFT_FAIL;
+
+ ksft_print_msg("Check the parent sees only its own writes...");
+ for (i = 0; i < n; i++)
+ if (ip[i * stride] != i + 0xbeef0000)
+ break;
+ if (i == n)
+ success("OK");
+ else
+ fail("Fail: parent content");
+ ops->cleanup_area(p, hpage_pmd_size);
+ /* The parent's check must not bury the child's verdict */
+ if (exit_status != KSFT_FAIL)
+ exit_status = child_status;
+ ksft_test_result_report(exit_status, "%s\n", __func__);
+}
+
static void madvise_collapse_existing_thps(struct collapse_context *c,
struct mem_ops *ops)
{
@@ -1695,6 +1792,9 @@ int main(int argc, char **argv)
TEST(collapse_max_ptes_shared, khugepaged_context, anon_ops);
TEST(collapse_max_ptes_shared, madvise_context, anon_ops);
+ TEST(collapse_fork_cow_race, khugepaged_context, anon_ops);
+ TEST(collapse_fork_cow_race, madvise_context, anon_ops);
+
TEST(madvise_collapse_existing_thps, madvise_context, anon_ops);
TEST(madvise_collapse_existing_thps, madvise_context, read_only_file_ops);
TEST(madvise_collapse_existing_thps, madvise_context, read_write_file_read_ops);
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 13/19] selftests/mm: cover a shared-source collapse write race
2026-09-08 12:50 ` [PATCH v5 13/19] selftests/mm: cover a shared-source collapse write race Kiryl Shutsemau
@ 2026-09-08 21:02 ` Kiryl Shutsemau
0 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 21:02 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team
On Tue, Sep 08, 2026 at 01:50:59PM +0100, Kiryl Shutsemau wrote:
> + close(sync[1]);
> + if (read(sync[0], &go, 1) != 1)
> + ksft_exit_fail_msg("child never reached the collapse\n");
> +
> + /*
> + * Unshare one page at a time: a burst would break CoW on the whole
> + * range before the collapse starts, leaving nothing shared to collapse.
> + */
> + i = 0;
> + for (;;) {
> + if (i < n)
> + ip[i * stride] = i + 0xbeef0000;
> + i++;
> + usleep(10 * 1000);
> + if (waitpid(child, &wstatus, WNOHANG))
> + break;
> + }
Sashiko flagged two issues here:
- sync[0] never closed. It is fd leak;
- wstatus is garbage if waitpid() fails, returning -1;
Here's a fixup:
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 335f946eca61..c0a07d75a306 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -1228,6 +1228,7 @@ static void collapse_fork_cow_race(struct collapse_context *c, struct mem_ops *o
close(sync[1]);
if (read(sync[0], &go, 1) != 1)
ksft_exit_fail_msg("child never reached the collapse\n");
+ close(sync[0]);
/*
* Unshare one page at a time: a burst would break CoW on the whole
@@ -1235,12 +1236,17 @@ static void collapse_fork_cow_race(struct collapse_context *c, struct mem_ops *o
*/
i = 0;
for (;;) {
+ pid_t ret;
+
if (i < n)
ip[i * stride] = i + 0xbeef0000;
i++;
usleep(10 * 1000);
- if (waitpid(child, &wstatus, WNOHANG))
+ ret = waitpid(child, &wstatus, WNOHANG);
+ if (ret == child)
break;
+ if (ret < 0)
+ ksft_exit_fail_perror("waitpid()");
}
/* Finish whatever the paced sweep did not reach */
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply related [flat|nested] 48+ messages in thread
* [PATCH v5 14/19] selftests/mm: run every supported collapse order by default
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (12 preceding siblings ...)
2026-09-08 12:50 ` [PATCH v5 13/19] selftests/mm: cover a shared-source collapse write race Kiryl Shutsemau
@ 2026-09-08 12:51 ` Kiryl Shutsemau
2026-09-10 6:07 ` Baolin Wang
2026-09-08 12:51 ` [PATCH v5 15/19] selftests/mm: check that one khugepaged pass collapses one window Kiryl Shutsemau
` (5 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:51 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
The mTHP collapse cases only run when the caller names both the context
and an order, so a plain ./khugepaged covers the PMD contexts on anon and
nothing else. run_vmtests.sh pinned order 4 and covered no other.
Run the mTHP cases once per supported anon THP order below the PMD when
-c is absent, and pull that context into both the no-argument invocation
and "all". Also:
- -c still pins one order, and now says what is wrong instead of
printing the usage text. An order at or below the -s source order is
skipped: the sources would already be the size being asked for.
- Both orders end up as array indices and shift counts, so -s and -c
are range-checked before they get there.
- The mTHP context has only anon cases, so a run that names a different
mem_type -- "all:shmem", say -- drops it again rather than refusing
to start. Naming both explicitly still refuses.
- A case carries the order it was registered at, so a result names it:
# Run test: collapse_single_mthp (mthp_khugepaged:anon, order 6)
On x86-64 with 4K pages that is orders 2 through 8, and ./khugepaged goes
from 29 results in 17 seconds to 77 in 29, so run_vmtests.sh can drop its
pinned order-4 line.
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged.c | 98 ++++++++++++++++++-----
tools/testing/selftests/mm/run_vmtests.sh | 2 -
2 files changed, 78 insertions(+), 22 deletions(-)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index f5d4847c50cd..335f946eca61 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -31,6 +31,9 @@ static unsigned long page_size;
static int hpage_pmd_nr;
static int anon_order;
static int collapse_order;
+static bool collapse_order_set;
+static int collapse_orders[NR_ORDERS];
+static int nr_collapse_orders;
static int pagemap_fd = -1;
static int kpageflags_fd = -1;
@@ -1517,12 +1520,14 @@ static void usage(void)
fprintf(stderr, "\t\t-s: mTHP size, expressed as page order.\n");
fprintf(stderr, "\t\t Defaults to 0. Use this size for anon or shmem allocations.\n");
fprintf(stderr, "\t\t-c: collapse order for mTHP collapse, expressed as page order.\n");
+ fprintf(stderr, "\t\t Defaults to every supported order below the PMD.\n");
fprintf(stderr, "\t\t -s, if set, is the source order for the mixed-source case.\n");
exit(1);
}
static void parse_test_type(int argc, char **argv)
{
+ bool mthp_context_implied = false;
int opt;
char *buf;
const char *token;
@@ -1534,6 +1539,7 @@ static void parse_test_type(int argc, char **argv)
break;
case 'c':
collapse_order = atoi(optarg);
+ collapse_order_set = true;
break;
case 'h':
default:
@@ -1541,12 +1547,25 @@ static void parse_test_type(int argc, char **argv)
}
}
+ /*
+ * Both orders end up as array indices and shift counts, so neither
+ * can be negative, and a zero collapse order asks for base pages.
+ */
+ if (anon_order < 0 || anon_order > hpage_pmd_order)
+ ksft_exit_fail_msg("-s takes an order in 0..%d, not %d\n",
+ hpage_pmd_order, anon_order);
+ if (collapse_order_set &&
+ (collapse_order <= 0 || collapse_order >= hpage_pmd_order))
+ ksft_exit_fail_msg("-c takes an order in 1..%d, not %d\n",
+ hpage_pmd_order - 1, collapse_order);
+
argv += optind;
argc -= optind;
if (argc == 0) {
- /* Backwards compatibility */
+ /* No arguments: anon under every context */
khugepaged_context = &__khugepaged_context;
+ mthp_khugepaged_context = &__mthp_khugepaged_context;
madvise_context = &__madvise_context;
anon_ops = &__anon_ops;
return;
@@ -1557,13 +1576,14 @@ static void parse_test_type(int argc, char **argv)
if (!strcmp(token, "all")) {
khugepaged_context = &__khugepaged_context;
+ mthp_khugepaged_context = &__mthp_khugepaged_context;
madvise_context = &__madvise_context;
+ /* The mTHP context has only anon cases; let other mem_types drop it */
+ mthp_context_implied = true;
} else if (!strcmp(token, "khugepaged")) {
khugepaged_context = &__khugepaged_context;
} else if (!strcmp(token, "mthp_khugepaged")) {
mthp_khugepaged_context = &__mthp_khugepaged_context;
- if (collapse_order <= 0 || collapse_order >= hpage_pmd_order)
- usage();
} else if (!strcmp(token, "madvise")) {
madvise_context = &__madvise_context;
} else {
@@ -1579,20 +1599,20 @@ static void parse_test_type(int argc, char **argv)
read_write_file_write_ops = &__read_write_file_write_ops;
anon_ops = &__anon_ops;
shmem_ops = &__shmem_ops;
- if (mthp_khugepaged_context)
- usage();
} else if (!strcmp(buf, "anon")) {
anon_ops = &__anon_ops;
} else if (!strcmp(buf, "file")) {
read_only_file_ops = &__read_only_file_ops;
read_write_file_read_ops = &__read_write_file_read_ops;
read_write_file_write_ops = &__read_write_file_write_ops;
- if (mthp_khugepaged_context)
+ if (mthp_khugepaged_context && !mthp_context_implied)
usage();
+ mthp_khugepaged_context = NULL;
} else if (!strcmp(buf, "shmem")) {
shmem_ops = &__shmem_ops;
- if (mthp_khugepaged_context)
+ if (mthp_khugepaged_context && !mthp_context_implied)
usage();
+ mthp_khugepaged_context = NULL;
} else {
usage();
}
@@ -1614,6 +1634,7 @@ struct test_case {
struct mem_ops *ops;
const char *desc;
test_fn fn;
+ int order; /* mTHP contexts: the collapse order */
};
#define MAX_TEST_CASES 256
@@ -1629,6 +1650,7 @@ static int nr_test_cases;
.ops = o, \
.desc = #t, \
.fn = t, \
+ .order = collapse_order, \
}; \
} \
} while (0)
@@ -1669,10 +1691,35 @@ int main(int argc, char **argv)
parse_test_type(argc, argv);
- if (mthp_khugepaged_context &&
- !(thp_supported_orders() & (1UL << collapse_order)))
- ksft_exit_skip("Order %d is not a supported anon THP order\n",
- collapse_order);
+ if (mthp_khugepaged_context) {
+ unsigned long orders = thp_supported_orders();
+
+ if (collapse_order_set) {
+ if (!(orders & (1UL << collapse_order)))
+ ksft_exit_skip("Order %d is not a supported anon THP order\n",
+ collapse_order);
+ if (collapse_order <= anon_order)
+ ksft_exit_skip("-c %d needs a source order below it, -s says %d\n",
+ collapse_order, anon_order);
+ collapse_orders[nr_collapse_orders++] = collapse_order;
+ } else {
+ /*
+ * Every supported order above the source: -s makes the
+ * fault path hand out folios of that order, so a target
+ * at or below it has nothing to collapse.
+ */
+ int first = anon_order + 1;
+
+ if (first < MIN_MTHP_ORDER)
+ first = MIN_MTHP_ORDER;
+ for (int i = first; i < hpage_pmd_order; i++) {
+ if (orders & (1UL << i))
+ collapse_orders[nr_collapse_orders++] = i;
+ }
+ if (!nr_collapse_orders)
+ ksft_print_msg("mTHP cases skipped: no order above the source\n");
+ }
+ }
if (mthp_khugepaged_context) {
pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
@@ -1721,7 +1768,17 @@ int main(int argc, char **argv)
TEST(collapse_full, khugepaged_context, read_write_file_read_ops);
TEST(collapse_full, khugepaged_context, read_write_file_write_ops);
TEST(collapse_full, khugepaged_context, shmem_ops);
- TEST(collapse_full, mthp_khugepaged_context, anon_ops);
+ for (int i = 0; i < nr_collapse_orders; i++) {
+ collapse_order = collapse_orders[i];
+ TEST(collapse_full, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_empty, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_single_mthp, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_order_single_window, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_order_partial_window, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_order_max_ptes_none, mthp_khugepaged_context, anon_ops);
+ TEST(collapse_order_mixed_sources, mthp_khugepaged_context, anon_ops);
+ }
+
TEST(collapse_full, madvise_context, anon_ops);
TEST(collapse_full, madvise_context, read_only_file_ops);
TEST(collapse_full, madvise_context, read_write_file_read_ops);
@@ -1729,15 +1786,8 @@ int main(int argc, char **argv)
TEST(collapse_full, madvise_context, shmem_ops);
TEST(collapse_empty, khugepaged_context, anon_ops);
- TEST(collapse_empty, mthp_khugepaged_context, anon_ops);
TEST(collapse_empty, madvise_context, anon_ops);
- TEST(collapse_single_mthp, mthp_khugepaged_context, anon_ops);
- TEST(collapse_order_single_window, mthp_khugepaged_context, anon_ops);
- TEST(collapse_order_partial_window, mthp_khugepaged_context, anon_ops);
- TEST(collapse_order_max_ptes_none, mthp_khugepaged_context, anon_ops);
- TEST(collapse_order_mixed_sources, mthp_khugepaged_context, anon_ops);
-
TEST(collapse_single_pte_entry, khugepaged_context, anon_ops);
TEST(collapse_single_pte_entry, khugepaged_context, read_only_file_ops);
TEST(collapse_single_pte_entry, khugepaged_context, read_write_file_read_ops);
@@ -1810,7 +1860,15 @@ int main(int argc, char **argv)
for (int i = 0; i < nr_test_cases; i++) {
struct test_case *t = &test_cases[i];
- ksft_print_msg("\n# Run test: %s (%s:%s)\n", t->desc, t->ctx->name, t->ops->name);
+ if (t->ctx == &__mthp_khugepaged_context) {
+ collapse_order = t->order;
+ ksft_print_msg("\n# Run test: %s (%s:%s, order %d)\n",
+ t->desc, t->ctx->name, t->ops->name,
+ t->order);
+ } else {
+ ksft_print_msg("\n# Run test: %s (%s:%s)\n", t->desc,
+ t->ctx->name, t->ops->name);
+ }
t->fn(t->ctx, t->ops);
}
diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh
index 2652a7920b80..8bf898b71350 100755
--- a/tools/testing/selftests/mm/run_vmtests.sh
+++ b/tools/testing/selftests/mm/run_vmtests.sh
@@ -412,8 +412,6 @@ CATEGORY="thp" run_test ./khugepaged all:shmem
CATEGORY="thp" run_test ./khugepaged -s 4 all:shmem
-CATEGORY="thp" run_test ./khugepaged -c 4 mthp_khugepaged:anon
-
# Try to create XFS if not provided
if [ -z "${SPLIT_HUGE_PAGE_TEST_XFS_PATH}" ]; then
if test_selected "thp"; then
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 14/19] selftests/mm: run every supported collapse order by default
2026-09-08 12:51 ` [PATCH v5 14/19] selftests/mm: run every supported collapse order by default Kiryl Shutsemau
@ 2026-09-10 6:07 ` Baolin Wang
0 siblings, 0 replies; 48+ messages in thread
From: Baolin Wang @ 2026-09-10 6:07 UTC (permalink / raw)
To: Kiryl Shutsemau, akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
nico.pache, ziy, baohua, dev.jain, hughd, lance.yang, liam,
mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg, leon,
kernel-team, Kiryl Shutsemau (Meta)
On 9/8/26 8:51 PM, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
>
> The mTHP collapse cases only run when the caller names both the context
> and an order, so a plain ./khugepaged covers the PMD contexts on anon and
> nothing else. run_vmtests.sh pinned order 4 and covered no other.
>
> Run the mTHP cases once per supported anon THP order below the PMD when
> -c is absent, and pull that context into both the no-argument invocation
> and "all". Also:
>
> - -c still pins one order, and now says what is wrong instead of
> printing the usage text. An order at or below the -s source order is
> skipped: the sources would already be the size being asked for.
>
> - Both orders end up as array indices and shift counts, so -s and -c
> are range-checked before they get there.
>
> - The mTHP context has only anon cases, so a run that names a different
> mem_type -- "all:shmem", say -- drops it again rather than refusing
> to start. Naming both explicitly still refuses.
>
> - A case carries the order it was registered at, so a result names it:
>
> # Run test: collapse_single_mthp (mthp_khugepaged:anon, order 6)
>
> On x86-64 with 4K pages that is orders 2 through 8, and ./khugepaged goes
> from 29 results in 17 seconds to 77 in 29, so run_vmtests.sh can drop its
> pinned order-4 line.
>
> Assisted-by: LLM
> Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Tested-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 48+ messages in thread
* [PATCH v5 15/19] selftests/mm: check that one khugepaged pass collapses one window
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (13 preceding siblings ...)
2026-09-08 12:51 ` [PATCH v5 14/19] selftests/mm: run every supported collapse order by default Kiryl Shutsemau
@ 2026-09-08 12:51 ` Kiryl Shutsemau
2026-09-08 12:51 ` [PATCH v5 16/19] selftests/mm: add khugepaged race harness Kiryl Shutsemau
` (4 subsequent siblings)
19 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:51 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
khugepaged_full_pass() drives the daemon through sysfs: a store to
scan_sleep_millisecs wakes it, and full_scans advancing by two marks one
pass that started after setup. Every mTHP collapse result in the suite
rests on that pair, and nothing checks it.
Add khugepaged_sync_check. Each step:
- prepare one aligned window
- record its source PFNs from pagemap
- run one khugepaged_full_pass() barrier
- require the window came out collapsed, with exactly one collapse
attempt attributed to it
The anon events carry no virtual address, so an attempt is matched by the
source folio PFN and order that the mm_collapse_huge_page_isolate
tracepoint reports.
Reading the trace buffer takes four small helpers in vm_util: open an
event subsystem's enable file, flip it, clear the buffer, and open it for
reading.
scan_sleep_millisecs is set to a minute, so a step that took a sleep
instead of a wake would blow the budget.
Passes 5/5 on x86-64 4K and arm64 64K.
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/Makefile | 1 +
.../selftests/mm/khugepaged_sync_check.c | 179 ++++++++++++++++++
tools/testing/selftests/mm/run_vmtests.sh | 2 +
tools/testing/selftests/mm/vm_util.c | 38 ++++
tools/testing/selftests/mm/vm_util.h | 4 +
5 files changed, 224 insertions(+)
create mode 100644 tools/testing/selftests/mm/khugepaged_sync_check.c
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 2093fcf6e915..b2d6e5c12934 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -105,6 +105,7 @@ TEST_GEN_FILES += merge
TEST_GEN_FILES += rmap
TEST_GEN_FILES += folio_split_race_test
TEST_GEN_FILES += folio_order_check
+TEST_GEN_FILES += khugepaged_sync_check
ifneq ($(ARCH),arm64)
TEST_GEN_FILES += soft-dirty
diff --git a/tools/testing/selftests/mm/khugepaged_sync_check.c b/tools/testing/selftests/mm/khugepaged_sync_check.c
new file mode 100644
index 000000000000..1c1b942ac325
--- /dev/null
+++ b/tools/testing/selftests/mm/khugepaged_sync_check.c
@@ -0,0 +1,179 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Check that khugepaged_full_pass() drives khugepaged in step: one barrier
+ * over one prepared window must collapse it with exactly one collapse
+ * attempt attributed to its source pages, step after step.
+ *
+ * scan_sleep_millisecs is a minute so that a step which slept instead of
+ * being woken blows the budget.
+ */
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include "kselftest.h"
+#include "vm_util.h"
+#include "hugepage_settings.h"
+
+#define BASE_ADDR ((void *)(1UL << 30))
+/* Smallest order khugepaged considers */
+#define TARGET_ORDER 2
+#define NR_ITERATIONS 5
+#define PASS_TIMEOUT_S 30
+
+static int pagemap_fd;
+static int kpageflags_fd;
+static int trace_events_fd = -1;
+static unsigned long hpage_pmd_size;
+
+/*
+ * The events are system-wide: switch them off however the test ends,
+ * including from inside a helper that gives up.
+ */
+static void trace_events_off(void)
+{
+ if (trace_events_fd >= 0)
+ tracing_events_enable(trace_events_fd, false);
+}
+
+/* Count the isolate events whose scan_pfn is one of the window's source PFNs */
+static int count_attributed(unsigned long *pfns, int nr_pfns,
+ unsigned int order)
+{
+ char line[1024];
+ int count = 0;
+ FILE *fp;
+
+ fp = tracing_open_trace();
+ if (!fp)
+ ksft_exit_fail_msg("Cannot open trace buffer\n");
+
+ while (fgets(line, sizeof(line), fp)) {
+ unsigned long val;
+ unsigned int ord;
+ char *s, *o;
+ int i;
+
+ s = strstr(line, "mm_collapse_huge_page_isolate:");
+ if (!s)
+ continue;
+ if (sscanf(s, "mm_collapse_huge_page_isolate: scan_pfn=0x%lx",
+ &val) != 1)
+ continue;
+ o = strstr(s, "order=");
+ if (!o || sscanf(o, "order=%u", &ord) != 1 || ord != order)
+ continue;
+ for (i = 0; i < nr_pfns; i++) {
+ if (val == pfns[i]) {
+ count++;
+ break;
+ }
+ }
+ }
+ fclose(fp);
+ return count;
+}
+
+static void one_step(int iteration)
+{
+ const size_t window = getpagesize() << TARGET_ORDER;
+ const int nr_pages = 1 << TARGET_ORDER;
+ unsigned long pfns[1 << TARGET_ORDER];
+ bool collapsed, passed;
+ int attributed;
+ char *p;
+ int i;
+
+ p = mmap(BASE_ADDR, hpage_pmd_size, PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED_NOREPLACE, -1, 0);
+ if (p != BASE_ADDR)
+ ksft_exit_fail_perror("mmap() window");
+
+ for (i = 0; i < nr_pages; i++) {
+ p[i * getpagesize()] = i + 1;
+ pfns[i] = pagemap_get_pfn(pagemap_fd, p + i * getpagesize());
+ if (pfns[i] == -1UL)
+ ksft_exit_fail_msg("Source page not present\n");
+ }
+
+ /* Clear before enabling so the buffer holds only this step's events */
+ if (tracing_clear_trace())
+ ksft_exit_fail_msg("Cannot clear the trace buffer\n");
+ if (tracing_events_enable(trace_events_fd, true))
+ ksft_exit_fail_msg("Cannot enable huge_memory events\n");
+
+ if (madvise(p, hpage_pmd_size, MADV_HUGEPAGE))
+ ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
+ passed = khugepaged_full_pass(PASS_TIMEOUT_S);
+
+ /* Off before anything that can give up: the events are system-wide */
+ if (tracing_events_enable(trace_events_fd, false))
+ ksft_exit_fail_msg("Cannot disable huge_memory events\n");
+ if (!passed)
+ ksft_exit_fail_msg("khugepaged did not complete a full pass\n");
+
+ collapsed = is_range_backed_by_order(p, window, TARGET_ORDER,
+ pagemap_fd, kpageflags_fd);
+ attributed = count_attributed(pfns, nr_pages, TARGET_ORDER);
+
+ ksft_test_result(collapsed && attributed == 1,
+ "step %d: window collapsed, %d attributed result(s)\n",
+ iteration, attributed);
+
+ munmap(p, hpage_pmd_size);
+}
+
+int main(void)
+{
+ struct thp_settings settings;
+ int i;
+
+ ksft_print_header();
+
+ if (!thp_available())
+ ksft_exit_skip("Transparent Hugepages not available\n");
+ if (!(thp_supported_orders() & (1UL << TARGET_ORDER)))
+ ksft_exit_skip("Order %d is not a supported anon THP order\n",
+ TARGET_ORDER);
+
+ hpage_pmd_size = read_pmd_pagesize();
+ if (!hpage_pmd_size)
+ ksft_exit_fail_msg("Reading PMD pagesize failed\n");
+ pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
+ if (pagemap_fd < 0)
+ ksft_exit_fail_perror("open(/proc/self/pagemap)");
+ kpageflags_fd = open("/proc/kpageflags", O_RDONLY);
+ if (kpageflags_fd < 0)
+ ksft_exit_skip("open(/proc/kpageflags) requires root\n");
+ trace_events_fd = tracing_events_open("huge_memory");
+ if (trace_events_fd < 0)
+ ksft_exit_skip("huge_memory events require tracefs and root\n");
+ atexit(trace_events_off);
+
+ ksft_set_plan(NR_ITERATIONS);
+
+ thp_save_settings();
+ thp_read_settings(&settings);
+ settings.thp_enabled = THP_MADVISE;
+ settings.thp_defrag = THP_DEFRAG_ALWAYS;
+ settings.khugepaged.defrag = 1;
+ settings.khugepaged.scan_sleep_millisecs = 60 * 1000;
+ settings.khugepaged.alloc_sleep_millisecs = 60 * 1000;
+ settings.khugepaged.max_ptes_none = (hpage_pmd_size / getpagesize()) - 1;
+ /* One wake must complete one full pass; see khugepaged_full_pass() */
+ settings.khugepaged.pages_to_scan = 1UL << 24;
+ for (i = 0; i < NR_ORDERS; i++)
+ settings.hugepages[i].enabled = THP_NEVER;
+ settings.hugepages[TARGET_ORDER].enabled = THP_INHERIT;
+ /* Base of the settings stack; the bottom entry is never popped */
+ thp_push_settings(&settings);
+
+ for (i = 0; i < NR_ITERATIONS; i++)
+ one_step(i);
+
+ ksft_finished();
+}
diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh
index 8bf898b71350..c0f69da3fd3b 100755
--- a/tools/testing/selftests/mm/run_vmtests.sh
+++ b/tools/testing/selftests/mm/run_vmtests.sh
@@ -404,6 +404,8 @@ CATEGORY="cow" run_test ./cow
CATEGORY="thp" run_test ./folio_order_check
+CATEGORY="thp" run_test ./khugepaged_sync_check
+
CATEGORY="thp" run_test ./khugepaged
CATEGORY="thp" run_test ./khugepaged -s 2
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 4947612e8b3d..af8324e1e8f2 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -599,6 +599,44 @@ bool is_range_backed_by_order(char *start, size_t len, int order,
return true;
}
+#define TRACEFS_ROOT "/sys/kernel/tracing"
+
+/*
+ * Returns -1 without tracefs or the subsystem. The events are system-wide:
+ * whoever switches them on has to switch them off again, on every exit path.
+ */
+int tracing_events_open(const char *subsys)
+{
+ char path[256];
+
+ snprintf(path, sizeof(path), TRACEFS_ROOT "/events/%s/enable",
+ subsys);
+ return open(path, O_WRONLY);
+}
+
+int tracing_events_enable(int fd, bool enable)
+{
+ if (pwrite(fd, enable ? "1" : "0", 1, 0) != 1)
+ return -1;
+ return 0;
+}
+
+/* Drop what the trace buffer holds so far */
+int tracing_clear_trace(void)
+{
+ int fd = open(TRACEFS_ROOT "/trace", O_WRONLY | O_TRUNC);
+
+ if (fd < 0)
+ return -1;
+ close(fd);
+ return 0;
+}
+
+FILE *tracing_open_trace(void)
+{
+ return fopen(TRACEFS_ROOT "/trace", "r");
+}
+
/* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */
int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
bool miss, bool wp, bool minor, uint64_t *ioctls)
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index 3be430e01901..5a91b9676ec5 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -119,6 +119,10 @@ int close_procmap(struct procmap_fd *procmap);
int write_sysfs(const char *file_path, unsigned long val);
int read_sysfs(const char *file_path, unsigned long *val);
bool softdirty_supported(void);
+int tracing_events_open(const char *subsys);
+int tracing_events_enable(int fd, bool enable);
+int tracing_clear_trace(void);
+FILE *tracing_open_trace(void);
static inline int open_self_procmap(struct procmap_fd *procmap_out)
{
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* [PATCH v5 16/19] selftests/mm: add khugepaged race harness
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (14 preceding siblings ...)
2026-09-08 12:51 ` [PATCH v5 15/19] selftests/mm: check that one khugepaged pass collapses one window Kiryl Shutsemau
@ 2026-09-08 12:51 ` Kiryl Shutsemau
2026-09-08 21:34 ` Kiryl Shutsemau
2026-09-08 12:51 ` [PATCH v5 17/19] selftests/mm: race the collapse of windows with holes Kiryl Shutsemau
` (3 subsequent siblings)
19 siblings, 1 reply; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:51 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
Collapse serialises against faults, GUP, fork, mremap and zapping through
a protocol of locks, TLB flushes and refcount checks. No khugepaged
selftest exercises any of it under contention.
Add khugepaged_race. Six racing threads work the same address space:
- two faulters
- an MADV_DONTNEED thread
- a transient FOLL_PIN thread (gup_test)
- a forker
- an mremap thread
One of three drivers collapses under them:
stepped khugepaged, one full pass at a time via
khugepaged_full_pass(), so each step covers a known extent;
free khugepaged left to run (scan_sleep_millisecs=0), for soak;
madvise an MADV_COLLAPSE and MADV_DONTNEED loop.
Every mode runs in turn unless -m names one, five seconds each. Every
supported anon THP order is set to inherit and max_ptes_none is 0, so a
window collapses only once fully populated and the racing MADV_DONTNEED
steers selection across orders.
The rule is that a racing page reads as its pattern or as zero, never
anything else. The faulters and fork children check it throughout, and a
final sweep checks it again. The other half of the check is the kernel's
own assertions, so read dmesg too.
The pin thread goes through gup_test, so the harness skips without
CONFIG_GUP_TEST or root. The default playground is three shared PMD-sized
areas plus the mremap thread's, over two gigabytes at a 512M PMD; -a
shrinks it.
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/Makefile | 1 +
tools/testing/selftests/mm/khugepaged_race.c | 410 +++++++++++++++++++
tools/testing/selftests/mm/run_vmtests.sh | 2 +
3 files changed, 413 insertions(+)
create mode 100644 tools/testing/selftests/mm/khugepaged_race.c
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index b2d6e5c12934..308bbad73c11 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -106,6 +106,7 @@ TEST_GEN_FILES += rmap
TEST_GEN_FILES += folio_split_race_test
TEST_GEN_FILES += folio_order_check
TEST_GEN_FILES += khugepaged_sync_check
+TEST_GEN_FILES += khugepaged_race
ifneq ($(ARCH),arm64)
TEST_GEN_FILES += soft-dirty
diff --git a/tools/testing/selftests/mm/khugepaged_race.c b/tools/testing/selftests/mm/khugepaged_race.c
new file mode 100644
index 000000000000..448256704ef4
--- /dev/null
+++ b/tools/testing/selftests/mm/khugepaged_race.c
@@ -0,0 +1,410 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Race collapse against faults, GUP pins, fork, mremap and MADV_DONTNEED
+ * over the same ranges. A racing page must read as its pattern or as
+ * zero, never anything else; the kernel's own assertions in dmesg are the
+ * other half of the check.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "kselftest.h"
+#include "vm_util.h"
+#include "hugepage_settings.h"
+#include "../../../../mm/gup_test.h"
+
+#ifndef FOLL_WRITE
+#define FOLL_WRITE 0x01
+#endif
+
+#define BASE_ADDR ((void *)(1UL << 30))
+#define PASS_TIMEOUT_S 30
+
+/*
+ * PMD-sized areas the racing threads share, plus one for the mremap
+ * thread. -a shrinks it where a PMD is 512M.
+ */
+#define DEFAULT_SHARED_AREAS 3
+static int nr_shared_areas;
+static int nr_areas;
+
+static unsigned long hpage_pmd_size;
+static unsigned long page_size;
+/* nr_areas PMD-sized areas; the last one belongs to the mremap thread */
+static char *region;
+static char *mremap_area;
+static char *mremap_scratch;
+static int gup_fd = -1;
+static volatile int stop;
+static volatile int corrupted;
+
+static unsigned int pattern(unsigned long page_idx)
+{
+ unsigned int val = (unsigned int)page_idx * 2654435761U;
+
+ return val ? val : 1; /* never collides with the zero-fill */
+}
+
+/* Zero means never written; anything else must be this page's pattern */
+static bool page_is_corrupt(unsigned long page_idx, unsigned int *val)
+{
+ *val = *(unsigned int *)(region + page_idx * page_size);
+
+ return *val && *val != pattern(page_idx);
+}
+
+static void check_page(unsigned long page_idx)
+{
+ unsigned int val;
+
+ if (page_is_corrupt(page_idx, &val)) {
+ corrupted = 1;
+ ksft_print_msg("Corruption at page %lu: %#x != %#x\n",
+ page_idx, val, pattern(page_idx));
+ }
+}
+
+static unsigned long shared_pages(void)
+{
+ return nr_shared_areas * hpage_pmd_size / page_size;
+}
+
+static unsigned long rand_page(unsigned int *seed)
+{
+ return (unsigned long)rand_r(seed) % shared_pages();
+}
+
+/* Clamp so a range never reaches the mremap thread's area */
+static unsigned long room_from(unsigned long page_idx, unsigned long want)
+{
+ unsigned long left = shared_pages() - page_idx;
+
+ return want < left ? want : left;
+}
+
+static void *faulter_fn(void *arg)
+{
+ unsigned int seed = (unsigned long)arg;
+
+ while (!stop) {
+ unsigned long page_idx = rand_page(&seed);
+
+ if (rand_r(&seed) & 1)
+ *(unsigned int *)(region + page_idx * page_size) =
+ pattern(page_idx);
+ else
+ check_page(page_idx);
+ }
+ return NULL;
+}
+
+static void *dontneed_fn(void *arg)
+{
+ unsigned int seed = (unsigned long)arg;
+
+ while (!stop) {
+ unsigned long page_idx = rand_page(&seed);
+ unsigned long nr = 1UL << (rand_r(&seed) % 6); /* 1..32 pages */
+
+ madvise(region + page_idx * page_size,
+ room_from(page_idx, nr) * page_size, MADV_DONTNEED);
+ usleep(rand_r(&seed) % 500);
+ }
+ return NULL;
+}
+
+static void *pinner_fn(void *arg)
+{
+ unsigned int seed = (unsigned long)arg;
+
+ while (!stop) {
+ struct gup_test gup = {};
+ unsigned long page_idx = rand_page(&seed);
+ unsigned long nr = room_from(page_idx, 16);
+
+ gup.addr = (unsigned long)(region + page_idx * page_size);
+ gup.size = nr * page_size;
+ gup.nr_pages_per_call = nr;
+ gup.gup_flags = FOLL_WRITE;
+ /* Racing MADV_DONTNEED makes transient failures expected */
+ ioctl(gup_fd, PIN_FAST_BENCHMARK, &gup);
+ usleep(rand_r(&seed) % 200);
+ }
+ return NULL;
+}
+
+static void *forker_fn(void *arg)
+{
+ unsigned int seed = (unsigned long)arg;
+
+ while (!stop) {
+ pid_t pid = fork();
+
+ if (pid == 0) {
+ unsigned int val;
+ int bad = 0;
+
+ /*
+ * No stdio in the child: a thread may hold stdout's
+ * lock across the fork, and printing under it hangs.
+ */
+ for (int i = 0; i < 16; i++)
+ bad |= page_is_corrupt(rand_page(&seed), &val);
+ _exit(bad);
+ }
+ if (pid > 0) {
+ int wstatus;
+
+ if (waitpid(pid, &wstatus, 0) < 0)
+ ksft_exit_fail_perror("waitpid()");
+ /* A child killed on the read counts too */
+ if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus))
+ corrupted = 1;
+ }
+ usleep(rand_r(&seed) % 2000);
+ }
+ return NULL;
+}
+
+static void *mremapper_fn(void *arg)
+{
+ unsigned int seed = (unsigned long)arg;
+
+ while (!stop) {
+ void *p;
+
+ p = mremap(mremap_area, hpage_pmd_size, hpage_pmd_size,
+ MREMAP_MAYMOVE | MREMAP_FIXED, mremap_scratch);
+ if (p == MAP_FAILED)
+ ksft_exit_fail_perror("mremap() away");
+ for (int i = 0; i < 8; i++)
+ mremap_scratch[(rand_r(&seed) %
+ (hpage_pmd_size / page_size)) * page_size] = 1;
+ p = mremap(mremap_scratch, hpage_pmd_size, hpage_pmd_size,
+ MREMAP_MAYMOVE | MREMAP_FIXED, mremap_area);
+ if (p == MAP_FAILED)
+ ksft_exit_fail_perror("mremap() back");
+ usleep(rand_r(&seed) % 2000);
+ }
+ return NULL;
+}
+
+static unsigned long now_ms(void)
+{
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+ return tv.tv_sec * 1000UL + tv.tv_usec / 1000;
+}
+
+static void usage(void)
+{
+ fprintf(stderr,
+ "Usage: khugepaged_race [-d seconds] [-m stepped|free|madvise] [-a areas] [-t mask]\n"
+ "\tWithout -m, every mode runs in turn.\n"
+ "\t-d: seconds per mode (default 5)\n"
+ "\t-a: number of shared PMD-sized playground areas (default 3)\n"
+ "\t-t: bitmask of racing threads to start, for bisecting a failure\n");
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ static const char * const thread_names[] = {
+ "faulter", "faulter2", "dontneed", "pinner", "forker",
+ "mremapper",
+ };
+ void *(*const thread_fns[])(void *) = {
+ faulter_fn, faulter_fn, dontneed_fn, pinner_fn, forker_fn,
+ mremapper_fn,
+ };
+ const int nr_threads = ARRAY_SIZE(thread_names);
+ pthread_t threads[ARRAY_SIZE(thread_names)];
+ static const char * const all_modes[] = { "stepped", "free", "madvise" };
+ const char *one_mode[1];
+ const char * const *modes = all_modes;
+ int nr_modes = ARRAY_SIZE(all_modes);
+ const char *mode_arg = NULL;
+ struct thp_settings settings;
+ unsigned long end_ms;
+ int duration_s = 5;
+ unsigned long thread_mask = ~0UL;
+ int nr_areas_arg = 0;
+ unsigned long i;
+ int steps = 0;
+ int opt;
+
+ while ((opt = getopt(argc, argv, "a:d:m:t:h")) != -1) {
+ switch (opt) {
+ case 'a':
+ nr_areas_arg = atoi(optarg);
+ break;
+ case 'd':
+ duration_s = atoi(optarg);
+ break;
+ case 'm':
+ mode_arg = optarg;
+ break;
+ case 't':
+ thread_mask = strtoul(optarg, NULL, 0);
+ break;
+ default:
+ usage();
+ }
+ }
+
+ if (mode_arg) {
+ if (strcmp(mode_arg, "stepped") && strcmp(mode_arg, "free") &&
+ strcmp(mode_arg, "madvise"))
+ usage();
+ one_mode[0] = mode_arg;
+ modes = one_mode;
+ nr_modes = 1;
+ }
+
+ ksft_print_header();
+ if (!thp_available())
+ ksft_exit_skip("Transparent Hugepages not available\n");
+
+ page_size = getpagesize();
+ hpage_pmd_size = read_pmd_pagesize();
+ if (!hpage_pmd_size)
+ ksft_exit_fail_msg("Reading PMD pagesize failed\n");
+
+ gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR);
+ if (gup_fd < 0)
+ ksft_exit_skip("/sys/kernel/debug/gup_test requires CONFIG_GUP_TEST and root\n");
+
+ nr_shared_areas = nr_areas_arg > 0 ? nr_areas_arg : DEFAULT_SHARED_AREAS;
+ nr_areas = nr_shared_areas + 1;
+
+ /*
+ * MREMAP_FIXED unmaps whatever is in the way without saying so, so
+ * claim the mremap thread's scratch address up front.
+ */
+ mremap_scratch = (char *)BASE_ADDR + 2 * nr_areas * hpage_pmd_size;
+ if (mmap(mremap_scratch, hpage_pmd_size, PROT_NONE,
+ MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED_NOREPLACE,
+ -1, 0) != (void *)mremap_scratch)
+ ksft_exit_fail_perror("mmap() mremap scratch");
+
+ ksft_set_plan(nr_modes);
+
+ thp_save_settings();
+ thp_read_settings(&settings);
+
+ /* Base of the settings stack; the bottom entry is never popped */
+ thp_push_settings(&settings);
+
+ for (int m = 0; m < nr_modes; m++) {
+ const char *mode = modes[m];
+
+ thp_read_settings(&settings);
+ settings.thp_enabled = THP_MADVISE;
+ settings.thp_defrag = THP_DEFRAG_ALWAYS;
+ settings.shmem_enabled = SHMEM_NEVER;
+ settings.khugepaged.defrag = 1;
+ settings.khugepaged.scan_sleep_millisecs =
+ strcmp(mode, "free") ? 1000 : 0;
+ settings.khugepaged.alloc_sleep_millisecs = 10;
+ /*
+ * mTHP collapse honours only 0 or HPAGE_PMD_NR - 1 here, and 0
+ * keeps a step from being spent on PMD allocations that racing
+ * MADV_DONTNEED will not let succeed.
+ */
+ settings.khugepaged.max_ptes_none = 0;
+ /* One wake, one pass: the playground plus the forked children's copies */
+ settings.khugepaged.pages_to_scan =
+ nr_areas * (hpage_pmd_size / page_size) * 8;
+ for (i = 0; i < NR_ORDERS; i++) {
+ if (thp_supported_orders() & (1UL << i))
+ settings.hugepages[i].enabled = THP_INHERIT;
+ }
+ thp_push_settings(&settings);
+
+ region = mmap(BASE_ADDR, nr_areas * hpage_pmd_size,
+ PROT_READ | PROT_WRITE, MAP_ANONYMOUS |
+ MAP_PRIVATE | MAP_FIXED_NOREPLACE, -1, 0);
+ if (region != BASE_ADDR)
+ ksft_exit_fail_perror("mmap() playground");
+ mremap_area = region + nr_shared_areas * hpage_pmd_size;
+
+ /* Populate so the first pass has something to collapse */
+ for (i = 0; i < nr_shared_areas * hpage_pmd_size / page_size; i++)
+ *(unsigned int *)(region + i * page_size) = pattern(i);
+ memset(mremap_area, 1, hpage_pmd_size);
+ if (madvise(region, nr_areas * hpage_pmd_size, MADV_HUGEPAGE))
+ ksft_exit_fail_perror("madvise(MADV_HUGEPAGE)");
+
+ for (i = 0; i < nr_threads; i++) {
+ if (!(thread_mask & (1UL << i))) {
+ threads[i] = 0;
+ continue;
+ }
+ if (pthread_create(&threads[i], NULL, thread_fns[i],
+ (void *)(i + 1)))
+ ksft_exit_fail_perror(thread_names[i]);
+ }
+
+ end_ms = now_ms() + duration_s * 1000UL;
+ if (!strcmp(mode, "stepped")) {
+ while (now_ms() < end_ms && !corrupted) {
+ if (!khugepaged_full_pass(PASS_TIMEOUT_S))
+ ksft_exit_fail_msg("khugepaged pass timed out\n");
+ steps++;
+ }
+ } else if (!strcmp(mode, "free")) {
+ while (now_ms() < end_ms && !corrupted)
+ usleep(100 * 1000);
+ } else { /* madvise */
+ while (now_ms() < end_ms && !corrupted) {
+ for (i = 0; i < nr_shared_areas; i++) {
+ madvise(region + i * hpage_pmd_size,
+ hpage_pmd_size, MADV_COLLAPSE);
+ }
+ madvise(region, nr_shared_areas * hpage_pmd_size,
+ MADV_DONTNEED);
+ steps++;
+ }
+ }
+
+ stop = 1;
+ for (i = 0; i < nr_threads; i++) {
+ if (threads[i])
+ pthread_join(threads[i], NULL);
+ }
+
+ for (i = 0; i < nr_shared_areas * hpage_pmd_size / page_size; i++)
+ check_page(i);
+
+ ksft_test_result(!corrupted,
+ "%s: %ds, %d steps, no corruption\n",
+ mode, duration_s, steps);
+
+ /* The next mode maps the same fixed address with its own settings */
+ munmap(region, nr_areas * hpage_pmd_size);
+ thp_pop_settings();
+ stop = 0;
+ steps = 0;
+
+ if (corrupted) {
+ /* Memory is suspect; the rest would prove nothing */
+ while (++m < nr_modes)
+ ksft_test_result_skip("%s: skipped after corruption\n",
+ modes[m]);
+ break;
+ }
+ }
+
+ ksft_finished();
+}
diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh
index c0f69da3fd3b..fc61907aa3b2 100755
--- a/tools/testing/selftests/mm/run_vmtests.sh
+++ b/tools/testing/selftests/mm/run_vmtests.sh
@@ -406,6 +406,8 @@ CATEGORY="thp" run_test ./folio_order_check
CATEGORY="thp" run_test ./khugepaged_sync_check
+CATEGORY="thp" run_test ./khugepaged_race
+
CATEGORY="thp" run_test ./khugepaged
CATEGORY="thp" run_test ./khugepaged -s 2
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 16/19] selftests/mm: add khugepaged race harness
2026-09-08 12:51 ` [PATCH v5 16/19] selftests/mm: add khugepaged race harness Kiryl Shutsemau
@ 2026-09-08 21:34 ` Kiryl Shutsemau
0 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 21:34 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team
On Tue, Sep 08, 2026 at 01:51:02PM +0100, Kiryl Shutsemau wrote:
> +static void *mremapper_fn(void *arg)
> +{
> + unsigned int seed = (unsigned long)arg;
> +
> + while (!stop) {
> + void *p;
> +
> + p = mremap(mremap_area, hpage_pmd_size, hpage_pmd_size,
> + MREMAP_MAYMOVE | MREMAP_FIXED, mremap_scratch);
> + if (p == MAP_FAILED)
> + ksft_exit_fail_perror("mremap() away");
> + for (int i = 0; i < 8; i++)
> + mremap_scratch[(rand_r(&seed) %
> + (hpage_pmd_size / page_size)) * page_size] = 1;
> + p = mremap(mremap_scratch, hpage_pmd_size, hpage_pmd_size,
> + MREMAP_MAYMOVE | MREMAP_FIXED, mremap_area);
> + if (p == MAP_FAILED)
> + ksft_exit_fail_perror("mremap() back");
> + usleep(rand_r(&seed) % 2000);
> + }
> + return NULL;
> +}
Sashiko pointed out that mremap() freed scratch area and it has to be
re-reserved for next cycle.
Here's fixup:
diff --git a/tools/testing/selftests/mm/khugepaged_race.c b/tools/testing/selftests/mm/khugepaged_race.c
index c3478b5123e2..ccfe655b3ddd 100644
--- a/tools/testing/selftests/mm/khugepaged_race.c
+++ b/tools/testing/selftests/mm/khugepaged_race.c
@@ -209,6 +209,11 @@ static void *mremapper_fn(void *arg)
MREMAP_MAYMOVE | MREMAP_FIXED, mremap_area);
if (p == MAP_FAILED)
ksft_exit_fail_perror("mremap() back");
+ /* The move back unmapped the scratch address: claim it again */
+ if (mmap(mremap_scratch, hpage_pmd_size, PROT_NONE,
+ MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED_NOREPLACE,
+ -1, 0) != (void *)mremap_scratch)
+ ksft_exit_fail_perror("mmap() mremap scratch");
usleep(rand_r(&seed) % 2000);
}
return NULL;
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply related [flat|nested] 48+ messages in thread
* [PATCH v5 17/19] selftests/mm: race the collapse of windows with holes
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (15 preceding siblings ...)
2026-09-08 12:51 ` [PATCH v5 16/19] selftests/mm: add khugepaged race harness Kiryl Shutsemau
@ 2026-09-08 12:51 ` Kiryl Shutsemau
2026-09-08 12:51 ` [PATCH v5 18/19] selftests/mm: add memory-pressure threads to the khugepaged race harness Kiryl Shutsemau
` (2 subsequent siblings)
19 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:51 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
The harness pins max_ptes_none to 0, so khugepaged only collapses a window
once every PTE in it is present. A window with holes takes a different
route, and never gets raced. A hole is zero-filled in the new folio
rather than copied. Which slots count as holes keeps moving under the
racing MADV_DONTNEED, right up to the moment the PMD is detached.
Run both ends of the occupancy scale for every driver mode, one after the
other. mTHP collapse supports only those two, 0 and HPAGE_PMD_NR - 1, and
coerces anything between them to 0. Each result says which end it ran:
ok 1 stepped/strict: 5s, 231 steps, no corruption
ok 2 stepped/holes: 5s, 194 steps, no corruption
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged_race.c | 32 ++++++++++++--------
1 file changed, 20 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/mm/khugepaged_race.c b/tools/testing/selftests/mm/khugepaged_race.c
index 448256704ef4..f31f12fc0390 100644
--- a/tools/testing/selftests/mm/khugepaged_race.c
+++ b/tools/testing/selftests/mm/khugepaged_race.c
@@ -231,6 +231,8 @@ int main(int argc, char **argv)
const int nr_threads = ARRAY_SIZE(thread_names);
pthread_t threads[ARRAY_SIZE(thread_names)];
static const char * const all_modes[] = { "stepped", "free", "madvise" };
+ static const bool occupancies[] = { false, true }; /* strict, holes */
+ const int nr_occupancies = ARRAY_SIZE(occupancies);
const char *one_mode[1];
const char * const *modes = all_modes;
int nr_modes = ARRAY_SIZE(all_modes);
@@ -298,7 +300,7 @@ int main(int argc, char **argv)
-1, 0) != (void *)mremap_scratch)
ksft_exit_fail_perror("mmap() mremap scratch");
- ksft_set_plan(nr_modes);
+ ksft_set_plan(nr_modes * nr_occupancies);
thp_save_settings();
thp_read_settings(&settings);
@@ -306,8 +308,9 @@ int main(int argc, char **argv)
/* Base of the settings stack; the bottom entry is never popped */
thp_push_settings(&settings);
- for (int m = 0; m < nr_modes; m++) {
- const char *mode = modes[m];
+ for (int run = 0; run < nr_modes * nr_occupancies; run++) {
+ const char *mode = modes[run / nr_occupancies];
+ bool holes = occupancies[run % nr_occupancies];
thp_read_settings(&settings);
settings.thp_enabled = THP_MADVISE;
@@ -317,12 +320,14 @@ int main(int argc, char **argv)
settings.khugepaged.scan_sleep_millisecs =
strcmp(mode, "free") ? 1000 : 0;
settings.khugepaged.alloc_sleep_millisecs = 10;
+
/*
- * mTHP collapse honours only 0 or HPAGE_PMD_NR - 1 here, and 0
- * keeps a step from being spent on PMD allocations that racing
- * MADV_DONTNEED will not let succeed.
+ * mTHP collapse honours only 0 or HPAGE_PMD_NR - 1 here. The two
+ * ends race different paths: a strict window has every PTE
+ * present, a hole-heavy one is mostly zero-filled.
*/
- settings.khugepaged.max_ptes_none = 0;
+ settings.khugepaged.max_ptes_none = holes ?
+ (hpage_pmd_size / page_size) - 1 : 0;
/* One wake, one pass: the playground plus the forked children's copies */
settings.khugepaged.pages_to_scan =
nr_areas * (hpage_pmd_size / page_size) * 8;
@@ -388,8 +393,9 @@ int main(int argc, char **argv)
check_page(i);
ksft_test_result(!corrupted,
- "%s: %ds, %d steps, no corruption\n",
- mode, duration_s, steps);
+ "%s/%s: %ds, %d steps, no corruption\n",
+ mode, holes ? "holes" : "strict",
+ duration_s, steps);
/* The next mode maps the same fixed address with its own settings */
munmap(region, nr_areas * hpage_pmd_size);
@@ -399,9 +405,11 @@ int main(int argc, char **argv)
if (corrupted) {
/* Memory is suspect; the rest would prove nothing */
- while (++m < nr_modes)
- ksft_test_result_skip("%s: skipped after corruption\n",
- modes[m]);
+ while (++run < nr_modes * nr_occupancies)
+ ksft_test_result_skip("%s/%s: skipped after corruption\n",
+ modes[run / nr_occupancies],
+ occupancies[run % nr_occupancies] ?
+ "holes" : "strict");
break;
}
}
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* [PATCH v5 18/19] selftests/mm: add memory-pressure threads to the khugepaged race harness
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (16 preceding siblings ...)
2026-09-08 12:51 ` [PATCH v5 17/19] selftests/mm: race the collapse of windows with holes Kiryl Shutsemau
@ 2026-09-08 12:51 ` Kiryl Shutsemau
2026-09-08 12:51 ` [PATCH v5 19/19] selftests/mm: zap whole PTE tables in " Kiryl Shutsemau
2026-09-08 19:41 ` [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Andrew Morton
19 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:51 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
The harness races collapse against faults, pins, fork, mremap and
MADV_DONTNEED, but nothing in it runs reclaim or compaction against the
collapse.
Add two more threads, and run every mode and occupancy limit both with and
without them:
- pageout: cycles MADV_PAGEOUT over a region of its own, faults it back
in and checks the content each round, since a page's pattern must
survive the trip through swap. Left out when the host has no swap,
because then there is no anon reclaim to drive.
- compactor: writes /proc/sys/vm/compact_memory in a loop. Compaction
isolates and migrates folios, so it competes with a collapse for the
pages it is gathering, with refcount elevations and migration entries
of its own.
Each result says whether it ran under pressure:
ok 2 stepped/strict/pressure: 5s, 88 steps, no corruption
A full run is now twelve combinations; -m picks one mode, -d shortens
each run.
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged_race.c | 142 ++++++++++++++++---
1 file changed, 122 insertions(+), 20 deletions(-)
diff --git a/tools/testing/selftests/mm/khugepaged_race.c b/tools/testing/selftests/mm/khugepaged_race.c
index f31f12fc0390..1f4aa23834db 100644
--- a/tools/testing/selftests/mm/khugepaged_race.c
+++ b/tools/testing/selftests/mm/khugepaged_race.c
@@ -44,6 +44,8 @@ static unsigned long page_size;
static char *region;
static char *mremap_area;
static char *mremap_scratch;
+static char *pageout_area;
+static size_t pageout_size;
static int gup_fd = -1;
static volatile int stop;
static volatile int corrupted;
@@ -199,6 +201,69 @@ static void *mremapper_fn(void *arg)
return NULL;
}
+/*
+ * Swap traffic and LRU churn on a region nothing else writes, so a page's
+ * pattern must survive the trip through swap exactly.
+ */
+static void *pageout_fn(void *arg)
+{
+ unsigned int seed = (unsigned long)arg;
+ unsigned long nr = pageout_size / page_size;
+ unsigned long i;
+
+ for (i = 0; i < nr; i++)
+ *(unsigned int *)(pageout_area + i * page_size) = pattern(i);
+
+ while (!stop) {
+ madvise(pageout_area, pageout_size, MADV_PAGEOUT);
+ for (i = 0; i < nr && !stop; i++) {
+ unsigned int val = *(unsigned int *)(pageout_area +
+ i * page_size);
+
+ if (val != pattern(i)) {
+ corrupted = 1;
+ ksft_print_msg("Pageout corruption at page %lu: %#x != %#x\n",
+ i, val, pattern(i));
+ }
+ }
+ usleep(rand_r(&seed) % 2000);
+ }
+ return NULL;
+}
+
+/* Compaction migrates the collapse sources while they are being gathered */
+static void *compactor_fn(void *arg)
+{
+ unsigned int seed = (unsigned long)arg;
+ int fd = open("/proc/sys/vm/compact_memory", O_WRONLY);
+
+ if (fd < 0) {
+ ksft_print_msg("No compact_memory; compactor idle\n");
+ return NULL;
+ }
+ while (!stop) {
+ if (write(fd, "1", 1) < 0)
+ break;
+ usleep(10000 + rand_r(&seed) % 100000);
+ }
+ close(fd);
+ return NULL;
+}
+
+static bool swap_available(void)
+{
+ char line[256];
+ int lines = 0;
+ FILE *fp = fopen("/proc/swaps", "r");
+
+ if (!fp)
+ return false;
+ while (fgets(line, sizeof(line), fp))
+ lines++;
+ fclose(fp);
+ return lines > 1;
+}
+
static unsigned long now_ms(void)
{
struct timeval tv;
@@ -222,17 +287,23 @@ int main(int argc, char **argv)
{
static const char * const thread_names[] = {
"faulter", "faulter2", "dontneed", "pinner", "forker",
- "mremapper",
+ "mremapper", "pageout", "compactor",
};
void *(*const thread_fns[])(void *) = {
faulter_fn, faulter_fn, dontneed_fn, pinner_fn, forker_fn,
- mremapper_fn,
+ mremapper_fn, pageout_fn, compactor_fn,
};
+ enum { T_FAULTER, T_FAULTER2, T_DONTNEED, T_PINNER, T_FORKER,
+ T_MREMAPPER, T_PAGEOUT, T_COMPACTOR };
+ const unsigned long pageout_bit = 1UL << T_PAGEOUT;
+ const unsigned long compactor_bit = 1UL << T_COMPACTOR;
const int nr_threads = ARRAY_SIZE(thread_names);
pthread_t threads[ARRAY_SIZE(thread_names)];
static const char * const all_modes[] = { "stepped", "free", "madvise" };
static const bool occupancies[] = { false, true }; /* strict, holes */
+ static const bool pressures[] = { false, true }; /* quiet, under pressure */
const int nr_occupancies = ARRAY_SIZE(occupancies);
+ const int nr_pressures = ARRAY_SIZE(pressures);
const char *one_mode[1];
const char * const *modes = all_modes;
int nr_modes = ARRAY_SIZE(all_modes);
@@ -241,6 +312,9 @@ int main(int argc, char **argv)
unsigned long end_ms;
int duration_s = 5;
unsigned long thread_mask = ~0UL;
+ unsigned long base_mask;
+ bool have_swap;
+ char label[64];
int nr_areas_arg = 0;
unsigned long i;
int steps = 0;
@@ -300,7 +374,13 @@ int main(int argc, char **argv)
-1, 0) != (void *)mremap_scratch)
ksft_exit_fail_perror("mmap() mremap scratch");
- ksft_set_plan(nr_modes * nr_occupancies);
+ base_mask = thread_mask;
+ have_swap = swap_available();
+ if (!have_swap)
+ /* No swap, no anon reclaim: compaction-only pressure */
+ ksft_print_msg("no swap: the pageout thread is not started\n");
+
+ ksft_set_plan(nr_modes * nr_occupancies * nr_pressures);
thp_save_settings();
thp_read_settings(&settings);
@@ -308,9 +388,25 @@ int main(int argc, char **argv)
/* Base of the settings stack; the bottom entry is never popped */
thp_push_settings(&settings);
- for (int run = 0; run < nr_modes * nr_occupancies; run++) {
- const char *mode = modes[run / nr_occupancies];
- bool holes = occupancies[run % nr_occupancies];
+ for (int run = 0; run < nr_modes * nr_occupancies * nr_pressures; run++) {
+ int rem = run % (nr_occupancies * nr_pressures);
+ const char *mode = modes[run / (nr_occupancies * nr_pressures)];
+ bool holes = occupancies[rem / nr_pressures];
+ bool pressure = pressures[rem % nr_pressures];
+
+ snprintf(label, sizeof(label), "%s/%s%s", mode,
+ holes ? "holes" : "strict", pressure ? "/pressure" : "");
+ if (corrupted) {
+ /* Memory is suspect; the rest would prove nothing */
+ ksft_test_result_skip("%s: skipped after corruption\n", label);
+ continue;
+ }
+
+ thread_mask = base_mask;
+ if (!pressure)
+ thread_mask &= ~(pageout_bit | compactor_bit);
+ else if (!have_swap)
+ thread_mask &= ~pageout_bit;
thp_read_settings(&settings);
settings.thp_enabled = THP_MADVISE;
@@ -344,6 +440,20 @@ int main(int argc, char **argv)
ksft_exit_fail_perror("mmap() playground");
mremap_area = region + nr_shared_areas * hpage_pmd_size;
+ if (thread_mask & pageout_bit) {
+ /* Enough to drive real reclaim without swamping a small guest */
+ pageout_size = 4 * hpage_pmd_size;
+ if (pageout_size < 16UL << 20)
+ pageout_size = 16UL << 20;
+ if (pageout_size > 64UL << 20)
+ pageout_size = 64UL << 20;
+ pageout_area = mmap(NULL, pageout_size,
+ PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+ if (pageout_area == MAP_FAILED)
+ ksft_exit_fail_perror("mmap() pageout area");
+ }
+
/* Populate so the first pass has something to collapse */
for (i = 0; i < nr_shared_areas * hpage_pmd_size / page_size; i++)
*(unsigned int *)(region + i * page_size) = pattern(i);
@@ -392,26 +502,18 @@ int main(int argc, char **argv)
for (i = 0; i < nr_shared_areas * hpage_pmd_size / page_size; i++)
check_page(i);
- ksft_test_result(!corrupted,
- "%s/%s: %ds, %d steps, no corruption\n",
- mode, holes ? "holes" : "strict",
- duration_s, steps);
+ ksft_test_result(!corrupted, "%s: %ds, %d steps, no corruption\n",
+ label, duration_s, steps);
/* The next mode maps the same fixed address with its own settings */
munmap(region, nr_areas * hpage_pmd_size);
+ if (pageout_area) {
+ munmap(pageout_area, pageout_size);
+ pageout_area = NULL;
+ }
thp_pop_settings();
stop = 0;
steps = 0;
-
- if (corrupted) {
- /* Memory is suspect; the rest would prove nothing */
- while (++run < nr_modes * nr_occupancies)
- ksft_test_result_skip("%s/%s: skipped after corruption\n",
- modes[run / nr_occupancies],
- occupancies[run % nr_occupancies] ?
- "holes" : "strict");
- break;
- }
}
ksft_finished();
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* [PATCH v5 19/19] selftests/mm: zap whole PTE tables in the khugepaged race harness
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (17 preceding siblings ...)
2026-09-08 12:51 ` [PATCH v5 18/19] selftests/mm: add memory-pressure threads to the khugepaged race harness Kiryl Shutsemau
@ 2026-09-08 12:51 ` Kiryl Shutsemau
2026-09-08 19:41 ` [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Andrew Morton
19 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 12:51 UTC (permalink / raw)
To: akpm, david, ljs, rppt
Cc: linux-mm, linux-kselftest, linux-kernel, usama.anjum, usama.arif,
baolin.wang, nico.pache, ziy, baohua, dev.jain, hughd, lance.yang,
liam, mhocko, ryan.roberts, shuah, surenb, vbabka, agordeev, jgg,
leon, kernel-team, Kiryl Shutsemau (Meta)
From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
The harness's MADV_DONTNEED thread zaps 1 to 32 pages at a time, never a
whole PMD-aligned area, and only a zap that covers a full table frees the
table itself (CONFIG_PT_RECLAIM).
Make the thread zap a whole PMD-aligned area about one iteration in 64,
and keep the fine-grained zaps as the common case. The new case frees
page tables, racing that against a collapse walking the same table.
Assisted-by: LLM
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
tools/testing/selftests/mm/khugepaged_race.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/mm/khugepaged_race.c b/tools/testing/selftests/mm/khugepaged_race.c
index 1f4aa23834db..c3478b5123e2 100644
--- a/tools/testing/selftests/mm/khugepaged_race.c
+++ b/tools/testing/selftests/mm/khugepaged_race.c
@@ -118,8 +118,21 @@ static void *dontneed_fn(void *arg)
unsigned long page_idx = rand_page(&seed);
unsigned long nr = 1UL << (rand_r(&seed) % 6); /* 1..32 pages */
- madvise(region + page_idx * page_size,
- room_from(page_idx, nr) * page_size, MADV_DONTNEED);
+ /*
+ * Now and then zap a whole PMD-aligned area: only a zap that
+ * covers the full table frees the table itself (CONFIG_PT_RECLAIM).
+ */
+ if (!(rand_r(&seed) % 64)) {
+ unsigned long area = page_idx /
+ (hpage_pmd_size / page_size);
+
+ madvise(region + area * hpage_pmd_size,
+ hpage_pmd_size, MADV_DONTNEED);
+ } else {
+ madvise(region + page_idx * page_size,
+ room_from(page_idx, nr) * page_size,
+ MADV_DONTNEED);
+ }
usleep(rand_r(&seed) % 500);
}
return NULL;
--
2.54.0
^ permalink raw reply related [flat|nested] 48+ messages in thread* Re: [PATCH v5 00/19] selftests/mm: improve khugepaged coverage
2026-09-08 12:50 [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Kiryl Shutsemau
` (18 preceding siblings ...)
2026-09-08 12:51 ` [PATCH v5 19/19] selftests/mm: zap whole PTE tables in " Kiryl Shutsemau
@ 2026-09-08 19:41 ` Andrew Morton
2026-09-08 21:36 ` Kiryl Shutsemau
19 siblings, 1 reply; 48+ messages in thread
From: Andrew Morton @ 2026-09-08 19:41 UTC (permalink / raw)
To: Kiryl Shutsemau
Cc: david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, baolin.wang, nico.pache, ziy, baohua,
dev.jain, hughd, lance.yang, liam, mhocko, ryan.roberts, shuah,
surenb, vbabka, agordeev, jgg, leon, kernel-team,
Kiryl Shutsemau (Meta)
On Tue, 8 Sep 2026 13:50:46 +0100 Kiryl Shutsemau <kirill@shutemov.name> wrote:
> khugepaged collapses to mTHP orders since 7.2, and 7.3 added three
> selftest cases for it: the generic collapse cases run at one order named
> by -c, with the result detected by counting folios of that order.
>
> That leaves the collapse path largely untested. The suite does not run
> where a PMD is 512M. A folio count cannot say where a collapse landed.
> Fixed sleeps cannot tell "not collapsed" from "not scanned yet". And
> nothing exercises collapse under contention.
>
> Close those gaps in order:
Added to mm-new, thanks.
Sashiko asked a few things:
https://sashiko.dev/#/patchset/20260908125105.1510704-1-kirill@shutemov.name
^ permalink raw reply [flat|nested] 48+ messages in thread* Re: [PATCH v5 00/19] selftests/mm: improve khugepaged coverage
2026-09-08 19:41 ` [PATCH v5 00/19] selftests/mm: improve khugepaged coverage Andrew Morton
@ 2026-09-08 21:36 ` Kiryl Shutsemau
0 siblings, 0 replies; 48+ messages in thread
From: Kiryl Shutsemau @ 2026-09-08 21:36 UTC (permalink / raw)
To: Andrew Morton
Cc: david, ljs, rppt, linux-mm, linux-kselftest, linux-kernel,
usama.anjum, usama.arif, baolin.wang, nico.pache, ziy, baohua,
dev.jain, hughd, lance.yang, liam, mhocko, ryan.roberts, shuah,
surenb, vbabka, agordeev, jgg, leon, kernel-team
On Tue, Sep 08, 2026 at 12:41:02PM -0700, Andrew Morton wrote:
> On Tue, 8 Sep 2026 13:50:46 +0100 Kiryl Shutsemau <kirill@shutemov.name> wrote:
>
> > khugepaged collapses to mTHP orders since 7.2, and 7.3 added three
> > selftest cases for it: the generic collapse cases run at one order named
> > by -c, with the result detected by counting folios of that order.
> >
> > That leaves the collapse path largely untested. The suite does not run
> > where a PMD is 512M. A folio count cannot say where a collapse landed.
> > Fixed sleeps cannot tell "not collapsed" from "not scanned yet". And
> > nothing exercises collapse under contention.
> >
> > Close those gaps in order:
>
> Added to mm-new, thanks.
Thanks!
> Sashiko asked a few things:
>
> https://sashiko.dev/#/patchset/20260908125105.1510704-1-kirill@shutemov.name
I've replied with fixups to the affected patches.
Let me know if you would like a respin or the fixes in any other form.
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 48+ messages in thread