[PATCH v3 5/9] selftests/mm: check that FORCE_READ() succeeded

Kevin Brodsky posted 9 patches 2 weeks, 3 days ago
[PATCH v3 5/9] selftests/mm: check that FORCE_READ() succeeded
Posted by Kevin Brodsky 2 weeks, 3 days ago
Many cow tests rely on FORCE_READ() to populate pages. Introduce a
helper to make sure that the pages are actually populated, and fail
otherwise.

Suggested-by: David Hildenbrand (Red Hat) <david@kernel.org>
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
---
 tools/testing/selftests/mm/cow.c | 43 ++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 10 deletions(-)

diff --git a/tools/testing/selftests/mm/cow.c b/tools/testing/selftests/mm/cow.c
index 83b3563be26b..d9c69c04b67d 100644
--- a/tools/testing/selftests/mm/cow.c
+++ b/tools/testing/selftests/mm/cow.c
@@ -75,6 +75,18 @@ static bool range_is_swapped(void *addr, size_t size)
 	return true;
 }
 
+static bool populate_page_checked(char *addr)
+{
+	bool ret;
+
+	FORCE_READ(*addr);
+	ret = pagemap_is_populated(pagemap_fd, addr);
+	if (!ret)
+		ksft_print_msg("Failed to populate page\n");
+
+	return ret;
+}
+
 struct comm_pipes {
 	int child_ready[2];
 	int parent_ready[2];
@@ -1549,8 +1561,10 @@ static void run_with_zeropage(non_anon_test_fn fn, const char *desc)
 	}
 
 	/* Read from the page to populate the shared zeropage. */
-	FORCE_READ(*mem);
-	FORCE_READ(*smem);
+	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
+		log_test_result(KSFT_FAIL);
+		goto munmap;
+	}
 
 	fn(mem, smem, pagesize);
 munmap:
@@ -1612,8 +1626,11 @@ static void run_with_huge_zeropage(non_anon_test_fn fn, const char *desc)
 	 * the first sub-page and test if we get another sub-page populated
 	 * automatically.
 	 */
-	FORCE_READ(*mem);
-	FORCE_READ(*smem);
+	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
+		log_test_result(KSFT_FAIL);
+		goto munmap;
+	}
+
 	if (!pagemap_is_populated(pagemap_fd, mem + pagesize) ||
 	    !pagemap_is_populated(pagemap_fd, smem + pagesize)) {
 		ksft_test_result_skip("Did not get THPs populated\n");
@@ -1663,8 +1680,10 @@ static void run_with_memfd(non_anon_test_fn fn, const char *desc)
 	}
 
 	/* Fault the page in. */
-	FORCE_READ(*mem);
-	FORCE_READ(*smem);
+	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
+		log_test_result(KSFT_FAIL);
+		goto munmap;
+	}
 
 	fn(mem, smem, pagesize);
 munmap:
@@ -1719,8 +1738,10 @@ static void run_with_tmpfile(non_anon_test_fn fn, const char *desc)
 	}
 
 	/* Fault the page in. */
-	FORCE_READ(*mem);
-	FORCE_READ(*smem);
+	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
+		log_test_result(KSFT_FAIL);
+		goto munmap;
+	}
 
 	fn(mem, smem, pagesize);
 munmap:
@@ -1773,8 +1794,10 @@ static void run_with_memfd_hugetlb(non_anon_test_fn fn, const char *desc,
 	}
 
 	/* Fault the page in. */
-	FORCE_READ(*mem);
-	FORCE_READ(*smem);
+	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
+		log_test_result(KSFT_FAIL);
+		goto munmap;
+	}
 
 	fn(mem, smem, hugetlbsize);
 munmap:
-- 
2.51.2
Re: [PATCH v3 5/9] selftests/mm: check that FORCE_READ() succeeded
Posted by David Hildenbrand (Red Hat) 2 weeks, 3 days ago
On 1/22/26 18:02, Kevin Brodsky wrote:
> Many cow tests rely on FORCE_READ() to populate pages. Introduce a
> helper to make sure that the pages are actually populated, and fail
> otherwise.
> 
> Suggested-by: David Hildenbrand (Red Hat) <david@kernel.org>
> Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
> ---
>   tools/testing/selftests/mm/cow.c | 43 ++++++++++++++++++++++++--------
>   1 file changed, 33 insertions(+), 10 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/cow.c b/tools/testing/selftests/mm/cow.c
> index 83b3563be26b..d9c69c04b67d 100644
> --- a/tools/testing/selftests/mm/cow.c
> +++ b/tools/testing/selftests/mm/cow.c
> @@ -75,6 +75,18 @@ static bool range_is_swapped(void *addr, size_t size)
>   	return true;
>   }
>   
> +static bool populate_page_checked(char *addr)
> +{
> +	bool ret;
> +
> +	FORCE_READ(*addr);
> +	ret = pagemap_is_populated(pagemap_fd, addr);
> +	if (!ret)
> +		ksft_print_msg("Failed to populate page\n");
> +
> +	return ret;
> +}

LGTM.

On second thought, this primarily catches if someone breaks FORCE_READ() 
... which hopefully doesn't happen that often? ;)

Because if the read succeeded, surely there must be something in the 
page tables.

-- 
Cheers

David
Re: [PATCH v3 5/9] selftests/mm: check that FORCE_READ() succeeded
Posted by Kevin Brodsky 2 weeks, 3 days ago
On 22/01/2026 23:18, David Hildenbrand (Red Hat) wrote:
> On 1/22/26 18:02, Kevin Brodsky wrote:
>> Many cow tests rely on FORCE_READ() to populate pages. Introduce a
>> helper to make sure that the pages are actually populated, and fail
>> otherwise.
>>
>> Suggested-by: David Hildenbrand (Red Hat) <david@kernel.org>
>> Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
>> ---
>>   tools/testing/selftests/mm/cow.c | 43 ++++++++++++++++++++++++--------
>>   1 file changed, 33 insertions(+), 10 deletions(-)
>>
>> diff --git a/tools/testing/selftests/mm/cow.c
>> b/tools/testing/selftests/mm/cow.c
>> index 83b3563be26b..d9c69c04b67d 100644
>> --- a/tools/testing/selftests/mm/cow.c
>> +++ b/tools/testing/selftests/mm/cow.c
>> @@ -75,6 +75,18 @@ static bool range_is_swapped(void *addr, size_t size)
>>       return true;
>>   }
>>   +static bool populate_page_checked(char *addr)
>> +{
>> +    bool ret;
>> +
>> +    FORCE_READ(*addr);
>> +    ret = pagemap_is_populated(pagemap_fd, addr);
>> +    if (!ret)
>> +        ksft_print_msg("Failed to populate page\n");
>> +
>> +    return ret;
>> +}
>
> LGTM.
>
> On second thought, this primarily catches if someone breaks
> FORCE_READ() ... which hopefully doesn't happen that often? ;)
>
> Because if the read succeeded, surely there must be something in the
> page tables. 

Indeed, one would hope so! The check doesn't hurt either though
(especially since we already have the pagemap fd around).

- Kevin
Re: [PATCH v3 5/9] selftests/mm: check that FORCE_READ() succeeded
Posted by Usama Anjum 2 weeks, 3 days ago
On 22/01/2026 5:02 pm, Kevin Brodsky wrote:
> Many cow tests rely on FORCE_READ() to populate pages. Introduce a
> helper to make sure that the pages are actually populated, and fail
> otherwise.
> 
> Suggested-by: David Hildenbrand (Red Hat) <david@kernel.org>
> Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
> ---
>   tools/testing/selftests/mm/cow.c | 43 ++++++++++++++++++++++++--------
>   1 file changed, 33 insertions(+), 10 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/cow.c b/tools/testing/selftests/mm/cow.c
> index 83b3563be26b..d9c69c04b67d 100644
> --- a/tools/testing/selftests/mm/cow.c
> +++ b/tools/testing/selftests/mm/cow.c
> @@ -75,6 +75,18 @@ static bool range_is_swapped(void *addr, size_t size)
>   	return true;
>   }
>   
> +static bool populate_page_checked(char *addr)
> +{
> +	bool ret;
> +
> +	FORCE_READ(*addr);
> +	ret = pagemap_is_populated(pagemap_fd, addr);
Very useful. Maybe extending FORCE_READ can help the entire suite?

> +	if (!ret)
> +		ksft_print_msg("Failed to populate page\n");
> +
> +	return ret;
> +}
> +
>   struct comm_pipes {
>   	int child_ready[2];
>   	int parent_ready[2];
> @@ -1549,8 +1561,10 @@ static void run_with_zeropage(non_anon_test_fn fn, const char *desc)
>   	}
>   
>   	/* Read from the page to populate the shared zeropage. */
> -	FORCE_READ(*mem);
> -	FORCE_READ(*smem);
> +	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
> +		log_test_result(KSFT_FAIL);
> +		goto munmap;
> +	}
>   
>   	fn(mem, smem, pagesize);
>   munmap:
> @@ -1612,8 +1626,11 @@ static void run_with_huge_zeropage(non_anon_test_fn fn, const char *desc)
>   	 * the first sub-page and test if we get another sub-page populated
>   	 * automatically.
>   	 */
> -	FORCE_READ(*mem);
> -	FORCE_READ(*smem);
> +	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
> +		log_test_result(KSFT_FAIL);
> +		goto munmap;
> +	}
> +
>   	if (!pagemap_is_populated(pagemap_fd, mem + pagesize) ||
>   	    !pagemap_is_populated(pagemap_fd, smem + pagesize)) {
>   		ksft_test_result_skip("Did not get THPs populated\n");
> @@ -1663,8 +1680,10 @@ static void run_with_memfd(non_anon_test_fn fn, const char *desc)
>   	}
>   
>   	/* Fault the page in. */
> -	FORCE_READ(*mem);
> -	FORCE_READ(*smem);
> +	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
> +		log_test_result(KSFT_FAIL);
> +		goto munmap;
> +	}
>   
>   	fn(mem, smem, pagesize);
>   munmap:
> @@ -1719,8 +1738,10 @@ static void run_with_tmpfile(non_anon_test_fn fn, const char *desc)
>   	}
>   
>   	/* Fault the page in. */
> -	FORCE_READ(*mem);
> -	FORCE_READ(*smem);
> +	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
> +		log_test_result(KSFT_FAIL);
> +		goto munmap;
> +	}
>   
>   	fn(mem, smem, pagesize);
>   munmap:
> @@ -1773,8 +1794,10 @@ static void run_with_memfd_hugetlb(non_anon_test_fn fn, const char *desc,
>   	}
>   
>   	/* Fault the page in. */
> -	FORCE_READ(*mem);
> -	FORCE_READ(*smem);
> +	if (!populate_page_checked(mem) || !populate_page_checked(smem)) {
> +		log_test_result(KSFT_FAIL);
> +		goto munmap;
> +	}
>   
>   	fn(mem, smem, hugetlbsize);
>   munmap:
Re: [PATCH v3 5/9] selftests/mm: check that FORCE_READ() succeeded
Posted by Kevin Brodsky 2 weeks, 3 days ago
On 22/01/2026 18:36, Usama Anjum wrote:
>> +static bool populate_page_checked(char *addr)
>> +{
>> +    bool ret;
>> +
>> +    FORCE_READ(*addr);
>> +    ret = pagemap_is_populated(pagemap_fd, addr);
> Very useful. Maybe extending FORCE_READ can help the entire suite?

I considered it but one needs to have a pagemap fd around, so we'd have
to open it for every test that uses the helper. Maybe such check is not
so essential either (see David's reply).

- Kevin