diff mbox series

[1/2] selftests: mincore: fix tmpfs mincore test failure

Message ID 99a3e190d38b08a2b96ede952a29893bffdb3432.1742960003.git.baolin.wang@linux.alibaba.com (mailing list archive)
State New
Headers show
Series Fix mincore() tmpfs test failure | expand

Commit Message

Baolin Wang March 26, 2025, 3:38 a.m. UTC
When running mincore test cases, I encountered the following failures:

"
mincore_selftest.c:359:check_tmpfs_mmap:Expected ra_pages (511) == 0 (0)
mincore_selftest.c:360:check_tmpfs_mmap:Read-ahead pages found in memory
check_tmpfs_mmap: Test terminated by assertion
          FAIL  global.check_tmpfs_mmap
not ok 5 global.check_tmpfs_mmap
FAILED: 4 / 5 tests passed
"

The reason for the test case failure is that my system automatically enabled
tmpfs large folio allocation by adding the 'transparent_hugepage_tmpfs=always'
cmdline. However, the test case still expects the tmpfs mounted on /dev/shm to
allocate small folios, which leads to assertion failures when verifying readahead
pages.

To fix this issue, remount tmpfs to a new test directory and set the 'huge=never'
parameter to avoid allocating large folios, which can pass the test.

Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
---
 .../selftests/mincore/mincore_selftest.c      | 25 +++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

Comments

Zi Yan March 27, 2025, 2:36 p.m. UTC | #1
On 25 Mar 2025, at 23:38, Baolin Wang wrote:

> When running mincore test cases, I encountered the following failures:
>
> "
> mincore_selftest.c:359:check_tmpfs_mmap:Expected ra_pages (511) == 0 (0)
> mincore_selftest.c:360:check_tmpfs_mmap:Read-ahead pages found in memory
> check_tmpfs_mmap: Test terminated by assertion
>           FAIL  global.check_tmpfs_mmap
> not ok 5 global.check_tmpfs_mmap
> FAILED: 4 / 5 tests passed
> "
>
> The reason for the test case failure is that my system automatically enabled
> tmpfs large folio allocation by adding the 'transparent_hugepage_tmpfs=always'
> cmdline. However, the test case still expects the tmpfs mounted on /dev/shm to
> allocate small folios, which leads to assertion failures when verifying readahead
> pages.
>
> To fix this issue, remount tmpfs to a new test directory and set the 'huge=never'
> parameter to avoid allocating large folios, which can pass the test.
>
> Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
> ---
>  .../selftests/mincore/mincore_selftest.c      | 25 +++++++++++++++++--
>  1 file changed, 23 insertions(+), 2 deletions(-)
>

<snip>

>
>  	errno = 0;
> -	fd = open("/dev/shm", O_TMPFILE | O_RDWR, 0600);
> +	/* Do not use large folios for tmpfs mincore testing */
> +	retval = mount("tmpfs", tmpfs_loc, "tmpfs", 0, "huge=never,size=4M");
> +	ASSERT_EQ(0, retval) {
> +		TH_LOG("Unable to mount tmpfs for testing\n");
> +	}
> +
> +	retval = snprintf(testfile, INPUT_MAX, "%s/test_file", tmpfs_loc);
> +	ASSERT_GE(INPUT_MAX, retval) {
> +		TH_LOG("Unable to create a tmpfs for testing\n");
> +	}
> +
> +	fd = open(testfile, O_CREAT|O_RDWR, 0664);

The fd permission is changed from 0600 to 0664, but it probably does not
matter.

>  	ASSERT_NE(-1, fd) {
>  		TH_LOG("Can't create temporary file: %s",
>  			strerror(errno));
> @@ -363,6 +382,8 @@ TEST(check_tmpfs_mmap)
>  	munmap(addr, FILE_SIZE);
>  	close(fd);
>  	free(vec);
> +	umount(tmpfs_loc);
> +	rmdir(tmpfs_loc);
>  }
>
>  TEST_HARNESS_MAIN
> -- 
> 2.43.5

Otherwise, LGTM. Reviewed-by: Zi Yan <ziy@nvidia.com>

--
Best Regards,
Yan, Zi
Baolin Wang March 30, 2025, 7:47 p.m. UTC | #2
On 2025/3/27 22:36, Zi Yan wrote:
> On 25 Mar 2025, at 23:38, Baolin Wang wrote:
> 
>> When running mincore test cases, I encountered the following failures:
>>
>> "
>> mincore_selftest.c:359:check_tmpfs_mmap:Expected ra_pages (511) == 0 (0)
>> mincore_selftest.c:360:check_tmpfs_mmap:Read-ahead pages found in memory
>> check_tmpfs_mmap: Test terminated by assertion
>>            FAIL  global.check_tmpfs_mmap
>> not ok 5 global.check_tmpfs_mmap
>> FAILED: 4 / 5 tests passed
>> "
>>
>> The reason for the test case failure is that my system automatically enabled
>> tmpfs large folio allocation by adding the 'transparent_hugepage_tmpfs=always'
>> cmdline. However, the test case still expects the tmpfs mounted on /dev/shm to
>> allocate small folios, which leads to assertion failures when verifying readahead
>> pages.
>>
>> To fix this issue, remount tmpfs to a new test directory and set the 'huge=never'
>> parameter to avoid allocating large folios, which can pass the test.
>>
>> Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>> ---
>>   .../selftests/mincore/mincore_selftest.c      | 25 +++++++++++++++++--
>>   1 file changed, 23 insertions(+), 2 deletions(-)
>>
> 
> <snip>
> 
>>
>>   	errno = 0;
>> -	fd = open("/dev/shm", O_TMPFILE | O_RDWR, 0600);
>> +	/* Do not use large folios for tmpfs mincore testing */
>> +	retval = mount("tmpfs", tmpfs_loc, "tmpfs", 0, "huge=never,size=4M");
>> +	ASSERT_EQ(0, retval) {
>> +		TH_LOG("Unable to mount tmpfs for testing\n");
>> +	}
>> +
>> +	retval = snprintf(testfile, INPUT_MAX, "%s/test_file", tmpfs_loc);
>> +	ASSERT_GE(INPUT_MAX, retval) {
>> +		TH_LOG("Unable to create a tmpfs for testing\n");
>> +	}
>> +
>> +	fd = open(testfile, O_CREAT|O_RDWR, 0664);
> 
> The fd permission is changed from 0600 to 0664, but it probably does not
> matter.

It is just a temp file, so it doesn't matter.

> 
>>   	ASSERT_NE(-1, fd) {
>>   		TH_LOG("Can't create temporary file: %s",
>>   			strerror(errno));
>> @@ -363,6 +382,8 @@ TEST(check_tmpfs_mmap)
>>   	munmap(addr, FILE_SIZE);
>>   	close(fd);
>>   	free(vec);
>> +	umount(tmpfs_loc);
>> +	rmdir(tmpfs_loc);
>>   }
>>
>>   TEST_HARNESS_MAIN
>> -- 
>> 2.43.5
> 
> Otherwise, LGTM. Reviewed-by: Zi Yan <ziy@nvidia.com>

Thanks for reviewing.
David Hildenbrand April 1, 2025, 12:54 p.m. UTC | #3
On 26.03.25 04:38, Baolin Wang wrote:
> When running mincore test cases, I encountered the following failures:
> 
> "
> mincore_selftest.c:359:check_tmpfs_mmap:Expected ra_pages (511) == 0 (0)
> mincore_selftest.c:360:check_tmpfs_mmap:Read-ahead pages found in memory
> check_tmpfs_mmap: Test terminated by assertion
>            FAIL  global.check_tmpfs_mmap
> not ok 5 global.check_tmpfs_mmap
> FAILED: 4 / 5 tests passed
> "
> 
> The reason for the test case failure is that my system automatically enabled
> tmpfs large folio allocation by adding the 'transparent_hugepage_tmpfs=always'
> cmdline. However, the test case still expects the tmpfs mounted on /dev/shm to
> allocate small folios, which leads to assertion failures when verifying readahead
> pages.
> 
> To fix this issue, remount tmpfs to a new test directory and set the 'huge=never'
> parameter to avoid allocating large folios, which can pass the test.
> 
> Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
> ---
>   .../selftests/mincore/mincore_selftest.c      | 25 +++++++++++++++++--
>   1 file changed, 23 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/mincore/mincore_selftest.c b/tools/testing/selftests/mincore/mincore_selftest.c
> index e949a43a6145..e8d7a3a4739f 100644
> --- a/tools/testing/selftests/mincore/mincore_selftest.c
> +++ b/tools/testing/selftests/mincore/mincore_selftest.c
> @@ -12,6 +12,7 @@
>   #include <unistd.h>
>   #include <stdlib.h>
>   #include <sys/mman.h>
> +#include <sys/mount.h>
>   #include <string.h>
>   #include <fcntl.h>
>   
> @@ -283,7 +284,7 @@ TEST(check_file_mmap)
>   	free(vec);
>   }
>   
> -
> +#define INPUT_MAX 80
>   /*
>    * Test mincore() behavior on a page backed by a tmpfs file.  This test
>    * performs the same steps as the previous one. However, we don't expect
> @@ -291,6 +292,9 @@ TEST(check_file_mmap)
>    */
>   TEST(check_tmpfs_mmap)
>   {
> +	char tmpfs_template[] = "/tmp/check_tmpfs_XXXXXX";
> +	const char *tmpfs_loc = mkdtemp(tmpfs_template);
> +	char testfile[INPUT_MAX];
>   	unsigned char *vec;
>   	int vec_size;
>   	char *addr;
> @@ -300,6 +304,10 @@ TEST(check_tmpfs_mmap)
>   	int i;
>   	int ra_pages = 0;
>   
> +	ASSERT_NE(NULL, tmpfs_loc) {
> +		TH_LOG("Can't mkdir tmpfs dentry\n");
> +	}
> +
>   	page_size = sysconf(_SC_PAGESIZE);
>   	vec_size = FILE_SIZE / page_size;
>   	if (FILE_SIZE % page_size)
> @@ -311,7 +319,18 @@ TEST(check_tmpfs_mmap)
>   	}
>   
>   	errno = 0;
> -	fd = open("/dev/shm", O_TMPFILE | O_RDWR, 0600);
> +	/* Do not use large folios for tmpfs mincore testing */
> +	retval = mount("tmpfs", tmpfs_loc, "tmpfs", 0, "huge=never,size=4M");
> +	ASSERT_EQ(0, retval) {
> +		TH_LOG("Unable to mount tmpfs for testing\n");
> +	}
> +
> +	retval = snprintf(testfile, INPUT_MAX, "%s/test_file", tmpfs_loc);
> +	ASSERT_GE(INPUT_MAX, retval) {
> +		TH_LOG("Unable to create a tmpfs for testing\n");
> +	}
> +
> +	fd = open(testfile, O_CREAT|O_RDWR, 0664);
>   	ASSERT_NE(-1, fd) {
>   		TH_LOG("Can't create temporary file: %s",
>   			strerror(errno));
> @@ -363,6 +382,8 @@ TEST(check_tmpfs_mmap)
>   	munmap(addr, FILE_SIZE);
>   	close(fd);
>   	free(vec);
> +	umount(tmpfs_loc);
> +	rmdir(tmpfs_loc);
>   }
>   
>   TEST_HARNESS_MAIN

Is there anything that cleans up the mount in case something goes wrong 
(we run into an assertion), or will the directory+mount stick around 
forever?

But I also wonder whether check_tmpfs_mmap() should be changed to live 
with the fact that readahead ("faultaround") could now happen. What's 
the reason for not doing that?
diff mbox series

Patch

diff --git a/tools/testing/selftests/mincore/mincore_selftest.c b/tools/testing/selftests/mincore/mincore_selftest.c
index e949a43a6145..e8d7a3a4739f 100644
--- a/tools/testing/selftests/mincore/mincore_selftest.c
+++ b/tools/testing/selftests/mincore/mincore_selftest.c
@@ -12,6 +12,7 @@ 
 #include <unistd.h>
 #include <stdlib.h>
 #include <sys/mman.h>
+#include <sys/mount.h>
 #include <string.h>
 #include <fcntl.h>
 
@@ -283,7 +284,7 @@  TEST(check_file_mmap)
 	free(vec);
 }
 
-
+#define INPUT_MAX 80
 /*
  * Test mincore() behavior on a page backed by a tmpfs file.  This test
  * performs the same steps as the previous one. However, we don't expect
@@ -291,6 +292,9 @@  TEST(check_file_mmap)
  */
 TEST(check_tmpfs_mmap)
 {
+	char tmpfs_template[] = "/tmp/check_tmpfs_XXXXXX";
+	const char *tmpfs_loc = mkdtemp(tmpfs_template);
+	char testfile[INPUT_MAX];
 	unsigned char *vec;
 	int vec_size;
 	char *addr;
@@ -300,6 +304,10 @@  TEST(check_tmpfs_mmap)
 	int i;
 	int ra_pages = 0;
 
+	ASSERT_NE(NULL, tmpfs_loc) {
+		TH_LOG("Can't mkdir tmpfs dentry\n");
+	}
+
 	page_size = sysconf(_SC_PAGESIZE);
 	vec_size = FILE_SIZE / page_size;
 	if (FILE_SIZE % page_size)
@@ -311,7 +319,18 @@  TEST(check_tmpfs_mmap)
 	}
 
 	errno = 0;
-	fd = open("/dev/shm", O_TMPFILE | O_RDWR, 0600);
+	/* Do not use large folios for tmpfs mincore testing */
+	retval = mount("tmpfs", tmpfs_loc, "tmpfs", 0, "huge=never,size=4M");
+	ASSERT_EQ(0, retval) {
+		TH_LOG("Unable to mount tmpfs for testing\n");
+	}
+
+	retval = snprintf(testfile, INPUT_MAX, "%s/test_file", tmpfs_loc);
+	ASSERT_GE(INPUT_MAX, retval) {
+		TH_LOG("Unable to create a tmpfs for testing\n");
+	}
+
+	fd = open(testfile, O_CREAT|O_RDWR, 0664);
 	ASSERT_NE(-1, fd) {
 		TH_LOG("Can't create temporary file: %s",
 			strerror(errno));
@@ -363,6 +382,8 @@  TEST(check_tmpfs_mmap)
 	munmap(addr, FILE_SIZE);
 	close(fd);
 	free(vec);
+	umount(tmpfs_loc);
+	rmdir(tmpfs_loc);
 }
 
 TEST_HARNESS_MAIN