Skip to contentSkip to conversation

selftests/mm: directly add pagesize instead of increase until page size

PATCH In review Wei Yang 4 participants 9 messages Aug 30, 2025 – Sep 1, 2025

Subsystems (4)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 selftests/mm: directly add pagesize instead of increase until page size

The check of is_backed_by_folio() is done on each page.

Directly move pointer to next page instead of increase one and check if it is page size aligned.

---
 tools/testing/selftests/mm/split_huge_page_test.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
index 10ae65ea032f..7f7016ba4054 100644
@@ -423,9 +423,8 @@ static void split_pte_mapped_thp(void)
 
 	/* smap does not show THPs after mremap, use kpageflags instead */
 	thp_size = 0;
-	for (i = 0; i < pagesize * 4; i++)
-		if (i % pagesize == 0 &&
-		    is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
+	for (i = 0; i < pagesize * 4; i += pagesize)
+		if (is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
 			thp_size++;
 
 	if (thp_size != 4)
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 4 lines · On Sat, 30 Aug 2025 02:31:02 +0000 Wei Yang <[email protected]> wrote:

The check of is_backed_by_folio() is done on each page.

Directly move pointer to next page instead of increase one and check if it is page size aligned.

Why?

tools/testing/selftests/mm/split_huge_page_test.c static void split_pte_mapped_thp(void) · line 428

Quoted 12 lines
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
@@ -423,9 +423,8 @@ static void split_pte_mapped_thp(void)
 
 	/* smap does not show THPs after mremap, use kpageflags instead */
 	thp_size = 0;
-	for (i = 0; i < pagesize * 4; i++)
-		if (i % pagesize == 0 &&
-		    is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
+	for (i = 0; i < pagesize * 4; i += pagesize)
+		if (is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
 			thp_size++;

Looks like we're doing more work. Is there something wrong with the existing code?

author
#2 lore raw
Quoted 13 lines · Andrew Morton
Quoted 4 lines · On Sat, 30 Aug 2025 02:31:02 +0000 Wei Yang <[email protected]> wrote:

The check of is_backed_by_folio() is done on each page.

Directly move pointer to next page instead of increase one and check if it is page size aligned.

Why?

Quoted 4 lines
tools/testing/selftests/mm/split_huge_page_test.c +0 -0
@@ -423,9 +423,8 @@ static void split_pte_mapped_thp(void)
 
Quoted 11 lines

tools/testing/selftests/mm/split_huge_page_test.c static void split_pte_mapped_thp(void) · line 428

Quoted 8 lines
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
 	/* smap does not show THPs after mremap, use kpageflags instead */
 	thp_size = 0;
-	for (i = 0; i < pagesize * 4; i++)
-		if (i % pagesize == 0 &&
-		    is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
+	for (i = 0; i < pagesize * 4; i += pagesize)
+		if (is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
 			thp_size++;

Looks like we're doing more work. Is there something wrong with the existing code?

Excuse me if I misunderstand the code.

Originally, i iterate from 0 to pagesize * 4 one by one and call is_backed_by_folio() when i is 0, 4096, 8192, 12288.

The change makes i just iterate on 0, 4096, 8192, 12288 and call is_backed_by_folio() respectively.

Current code is not wrong, but not necessary to iterate one by one.

Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#3 lore raw
In reply to Wei Yang
Quoted 16 lines · Wei Yang

The check of is_backed_by_folio() is done on each page.

Directly move pointer to next page instead of increase one and check if it is page size aligned.

---
 tools/testing/selftests/mm/split_huge_page_test.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +0 -0
index 10ae65ea032f..7f7016ba4054 100644
@@ -423,9 +423,8 @@ static void split_pte_mapped_thp(void)
 

tools/testing/selftests/mm/split_huge_page_test.c static void split_pte_mapped_thp(void) · line 430

Quoted 10 lines
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
 	/* smap does not show THPs after mremap, use kpageflags instead */
 	thp_size = 0;
-	for (i = 0; i < pagesize * 4; i++)
-		if (i % pagesize == 0 &&
-		    is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
+	for (i = 0; i < pagesize * 4; i += pagesize)
+		if (is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
 			thp_size++;
 
 	if (thp_size != 4)

It might be better to add

if (pte_mapped[i] != (char)i)

	ksft_exit_fail_msg("%ld byte corrupted\n", i);

instead to make sure mremap() does not change pte_mapped[] values.

--
Best Regards,
Yan, Zi

author
#4 lore raw
Quoted 27 lines · Zi Yan
Quoted 25 lines · Wei Yang

The check of is_backed_by_folio() is done on each page.

Directly move pointer to next page instead of increase one and check if it is page size aligned.

---
 tools/testing/selftests/mm/split_huge_page_test.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
index 10ae65ea032f..7f7016ba4054 100644
@@ -423,9 +423,8 @@ static void split_pte_mapped_thp(void)
 
 	/* smap does not show THPs after mremap, use kpageflags instead */
 	thp_size = 0;
-	for (i = 0; i < pagesize * 4; i++)
-		if (i % pagesize == 0 &&
-		    is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
+	for (i = 0; i < pagesize * 4; i += pagesize)
+		if (is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
 			thp_size++;
 
Quoted 9 lines

tools/testing/selftests/mm/split_huge_page_test.c static void split_pte_mapped_thp(void) · line 430

Quoted 1 line
tools/testing/selftests/mm/split_huge_page_test.c +0 -0
 	if (thp_size != 4)

It might be better to add

if (pte_mapped[i] != (char)i)

	ksft_exit_fail_msg("%ld byte corrupted\n", i);

instead to make sure mremap() does not change pte_mapped[] values.

Thanks, will use this.

Quoted 3 lines

--
Best Regards,
Yan, Zi

Signature · 2 lines
Wei Yang
Help you, Help me
#5 lore raw
In reply to Zi Yan
Quoted 27 lines · Zi Yan
Quoted 25 lines · Wei Yang

The check of is_backed_by_folio() is done on each page.

Directly move pointer to next page instead of increase one and check if it is page size aligned.

---
  tools/testing/selftests/mm/split_huge_page_test.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
index 10ae65ea032f..7f7016ba4054 100644
@@ -423,9 +423,8 @@ static void split_pte_mapped_thp(void)
 
  	/* smap does not show THPs after mremap, use kpageflags instead */
  	thp_size = 0;
-	for (i = 0; i < pagesize * 4; i++)
-		if (i % pagesize == 0 &&
-		    is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
+	for (i = 0; i < pagesize * 4; i += pagesize)
+		if (is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
  			thp_size++;
 
Quoted 8 lines

tools/testing/selftests/mm/split_huge_page_test.c static void split_pte_mapped_thp(void) · line 430

Quoted 1 line
tools/testing/selftests/mm/split_huge_page_test.c +0 -0
  	if (thp_size != 4)

It might be better to add

if (pte_mapped[i] != (char)i)

	ksft_exit_fail_msg("%ld byte corrupted\n", i);

instead to make sure mremap() does not change pte_mapped[] values.

We do have a corruption check later in that function, so I think we can just keep it simple here.

So this as is LGTM

As noted, we should just move from mremap() to mprotect() or sth like that which has clearer semantics.

Signature · 3 lines
Cheers

David / dhildenb
author
#6 lore raw
Quoted 40 lines · David Hildenbrand
Quoted 27 lines · Zi Yan
Quoted 25 lines · Wei Yang

The check of is_backed_by_folio() is done on each page.

Directly move pointer to next page instead of increase one and check if it is page size aligned.

---
  tools/testing/selftests/mm/split_huge_page_test.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
index 10ae65ea032f..7f7016ba4054 100644
@@ -423,9 +423,8 @@ static void split_pte_mapped_thp(void)
 
  	/* smap does not show THPs after mremap, use kpageflags instead */
  	thp_size = 0;
-	for (i = 0; i < pagesize * 4; i++)
-		if (i % pagesize == 0 &&
-		    is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
+	for (i = 0; i < pagesize * 4; i += pagesize)
+		if (is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
  			thp_size++;
 
Quoted 8 lines

tools/testing/selftests/mm/split_huge_page_test.c static void split_pte_mapped_thp(void) · line 430

Quoted 1 line
tools/testing/selftests/mm/split_huge_page_test.c +0 -0
  	if (thp_size != 4)

It might be better to add

if (pte_mapped[i] != (char)i)

	ksft_exit_fail_msg("%ld byte corrupted\n", i);

instead to make sure mremap() does not change pte_mapped[] values.

We do have a corruption check later in that function, so I think we can just keep it simple here.

Quoted 7 lines

So this as is LGTM

As noted, we should just move from mremap() to mprotect() or sth like that which has clearer semantics.

If my understanding is correct, we should

mmap 4 PMD_SIZE region with RW
madvise and fault in to allocate pmd-mapped thp
mprotect first page of each PMD to read-only to split to pte-mapped thp check whether the page is backed by pmd-order folio

Is this the correct way?

Signature · 2 lines
Wei Yang
Help you, Help me
#7 lore raw
Quoted 49 lines · Wei Yang
Quoted 40 lines · David Hildenbrand
Quoted 27 lines · Zi Yan
Quoted 25 lines · Wei Yang

The check of is_backed_by_folio() is done on each page.

Directly move pointer to next page instead of increase one and check if it is page size aligned.

---
   tools/testing/selftests/mm/split_huge_page_test.c | 5 ++---
   1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
index 10ae65ea032f..7f7016ba4054 100644
@@ -423,9 +423,8 @@ static void split_pte_mapped_thp(void)
 
   	/* smap does not show THPs after mremap, use kpageflags instead */
   	thp_size = 0;
-	for (i = 0; i < pagesize * 4; i++)
-		if (i % pagesize == 0 &&
-		    is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
+	for (i = 0; i < pagesize * 4; i += pagesize)
+		if (is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
   			thp_size++;
 
Quoted 8 lines

tools/testing/selftests/mm/split_huge_page_test.c static void split_pte_mapped_thp(void) · line 430

Quoted 1 line
tools/testing/selftests/mm/split_huge_page_test.c +0 -0
   	if (thp_size != 4)

It might be better to add

if (pte_mapped[i] != (char)i)

	ksft_exit_fail_msg("%ld byte corrupted\n", i);

instead to make sure mremap() does not change pte_mapped[] values.

We do have a corruption check later in that function, so I think we can just keep it simple here.

Quoted 7 lines

So this as is LGTM

As noted, we should just move from mremap() to mprotect() or sth like that which has clearer semantics.

Quoted 8 lines

If my understanding is correct, we should

mmap 4 PMD_SIZE region with RW
madvise and fault in to allocate pmd-mapped thp
mprotect first page of each PMD to read-only to split to pte-mapped thp check whether the page is backed by pmd-order folio

Is this the correct way?

Yeah, I would just mprotect(PROT_READ) the first page of each PMD. That will trigger a PTE-mapping of the THP reliably.

Signature · 3 lines
Cheers

David / dhildenb
#8 lore raw
Quoted 57 lines · David Hildenbrand
Quoted 56 lines · Wei Yang
Quoted 40 lines · David Hildenbrand
Quoted 27 lines · Zi Yan
Quoted 25 lines · Wei Yang

The check of is_backed_by_folio() is done on each page.

Directly move pointer to next page instead of increase one and check if it is page size aligned.

---
    tools/testing/selftests/mm/split_huge_page_test.c | 5 ++---
    1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
index 10ae65ea032f..7f7016ba4054 100644
@@ -423,9 +423,8 @@ static void split_pte_mapped_thp(void)
 
    	/* smap does not show THPs after mremap, use kpageflags instead */
    	thp_size = 0;
-	for (i = 0; i < pagesize * 4; i++)
-		if (i % pagesize == 0 &&
-		    is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
+	for (i = 0; i < pagesize * 4; i += pagesize)
+		if (is_backed_by_folio(&pte_mapped[i], pmd_order, pagemap_fd, kpageflags_fd))
    			thp_size++;
 
Quoted 8 lines

tools/testing/selftests/mm/split_huge_page_test.c static void split_pte_mapped_thp(void) · line 430

Quoted 1 line
tools/testing/selftests/mm/split_huge_page_test.c +0 -0
    	if (thp_size != 4)

It might be better to add

if (pte_mapped[i] != (char)i)

	ksft_exit_fail_msg("%ld byte corrupted\n", i);

instead to make sure mremap() does not change pte_mapped[] values.

We do have a corruption check later in that function, so I think we can just keep it simple here.

Quoted 7 lines

So this as is LGTM

As noted, we should just move from mremap() to mprotect() or sth like that which has clearer semantics.

If my understanding is correct, we should

mmap 4 PMD_SIZE region with RW
madvise and fault in to allocate pmd-mapped thp
mprotect first page of each PMD to read-only to split to pte-mapped thp check whether the page is backed by pmd-order folio

Quoted 4 lines
Quoted 1 line

Is this the correct way?

Yeah, I would just mprotect(PROT_READ) the first page of each PMD. That will trigger a PTE-mapping of the THP reliably.

Zi Yan mentions that there is a reason we are using mremap in the other thread. So likely best to just keep it as is unless we can understand that it definitely can done simpler.

Signature · 3 lines
Cheers

David / dhildenb