All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH -next 1/2] selftests/mincore: Add checking of block dev for TEST(check_file_mmap)
@ 2022-10-21  7:10 Zhao Gongyi
  2022-10-21  7:10 ` [PATCH -next 2/2] selftests/mincore: Optimize TEST(check_file_mmap) accoring to filemap read around Zhao Gongyi
  0 siblings, 1 reply; 6+ messages in thread
From: Zhao Gongyi @ 2022-10-21  7:10 UTC (permalink / raw)
  To: linux-kselftest, linux-kernel
  Cc: keescook, luto, wad, shuah, zhaogongyi, cristian.marussi

TEST(check_file_mmap) will fail when we run the test on tmpfs and report:
  mincore_selftest.c:261:check_file_mmap:Expected ra_pages (0) > 0 (0)
  mincore_selftest.c:262:check_file_mmap:No read-ahead pages found in memory

For some embaded system, maybe there is only tmpfs file system exist,
run the test will fail, or we install the test on a filesystem that
has no backend, also it will fail as unepected.

So add a checking of block dev for the filesystem at first.

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 .../selftests/mincore/mincore_selftest.c      | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/tools/testing/selftests/mincore/mincore_selftest.c b/tools/testing/selftests/mincore/mincore_selftest.c
index 4c88238fc8f0..287351a599a2 100644
--- a/tools/testing/selftests/mincore/mincore_selftest.c
+++ b/tools/testing/selftests/mincore/mincore_selftest.c
@@ -14,6 +14,9 @@
 #include <sys/mman.h>
 #include <string.h>
 #include <fcntl.h>
+#include <mntent.h>
+#include <sys/stat.h>
+#include <linux/fs.h>

 #include "../kselftest.h"
 #include "../kselftest_harness.h"
@@ -173,6 +176,43 @@ TEST(check_huge_pages)
 	munmap(addr, page_size);
 }

+static struct mntent* find_mount_point(const char *name)
+{
+	struct stat s;
+	FILE *mtab_fp;
+	struct mntent *mountEntry;
+	dev_t devno_of_name;
+
+	if (stat(name, &s) != 0)
+		return NULL;
+
+	devno_of_name = s.st_dev;
+
+	mtab_fp = setmntent("/proc/mounts", "r");
+	if (!mtab_fp)
+		return NULL;
+
+	while ((mountEntry = getmntent(mtab_fp)) != NULL) {
+		if (strcmp(name, mountEntry->mnt_dir) == 0
+			|| strcmp(name, mountEntry->mnt_fsname) == 0) {
+			break;
+		}
+
+		if (mountEntry->mnt_fsname[0] == '/'
+			&& stat(mountEntry->mnt_fsname, &s) == 0
+			&& s.st_rdev == devno_of_name) {
+			break;
+		}
+
+		if (stat(mountEntry->mnt_dir, &s) == 0
+				&& s.st_dev == devno_of_name) {
+			break;
+		}
+	}
+	endmntent(mtab_fp);
+
+	return mountEntry;
+}

 /*
  * Test mincore() behavior on a file-backed page.
@@ -194,6 +234,19 @@ TEST(check_file_mmap)
 	int fd;
 	int i;
 	int ra_pages = 0;
+	struct stat s;
+	struct mntent *mount_entry;
+
+	mount_entry = find_mount_point(".");
+	ASSERT_NE(NULL, mount_entry) {
+		TH_LOG("Find mount point of '.' failed");
+	}
+
+	ASSERT_EQ(0, (stat(mount_entry->mnt_fsname, &s) != 0
+		|| !S_ISBLK(s.st_mode))) {
+		TH_LOG("There is no a block dev on mount point, "
+			"test is not supported");
+	}

 	page_size = sysconf(_SC_PAGESIZE);
 	vec_size = FILE_SIZE / page_size;
--
2.17.1


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

* [PATCH -next 2/2] selftests/mincore: Optimize TEST(check_file_mmap) accoring to filemap read around
  2022-10-21  7:10 [PATCH -next 1/2] selftests/mincore: Add checking of block dev for TEST(check_file_mmap) Zhao Gongyi
@ 2022-10-21  7:10 ` Zhao Gongyi
  0 siblings, 0 replies; 6+ messages in thread
From: Zhao Gongyi @ 2022-10-21  7:10 UTC (permalink / raw)
  To: linux-kselftest, linux-kernel
  Cc: keescook, luto, wad, shuah, zhaogongyi, cristian.marussi

TEST(check_file_mmap) will fail when we set the ra_pages through
blockdev like:
  /sbin/blockdev --setra 8196 /dev/sda

And, for file mmap, kernel will read 'ra_pages/2' pages of the address
on the left hand or right hand. So, add a checking of ra_pages according
to the ra_pages setting of the block dev.

I have tested it on my system like:
  i=0
  while [ $i -lt 9000 ]
  do
        /sbin/blockdev --setra $i /dev/openeuler/*
        ./mincore_selftest  || { echo "$i test failed"; exit 1; }
        let i=$i+1
  done

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 tools/testing/selftests/kselftest_harness.h   |  2 +
 .../selftests/mincore/mincore_selftest.c      | 74 +++++++++++++++----
 2 files changed, 62 insertions(+), 14 deletions(-)

diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index 25f4d54067c0..fa40c85a1099 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -781,6 +781,8 @@
 	} \
 }

+#define MIN(a, b)       ((a) < (b) ? (a) : (b))
+
 struct __test_results {
 	char reason[1024];	/* Reason for test result */
 };
diff --git a/tools/testing/selftests/mincore/mincore_selftest.c b/tools/testing/selftests/mincore/mincore_selftest.c
index 287351a599a2..5eace1750fef 100644
--- a/tools/testing/selftests/mincore/mincore_selftest.c
+++ b/tools/testing/selftests/mincore/mincore_selftest.c
@@ -17,6 +17,7 @@
 #include <mntent.h>
 #include <sys/stat.h>
 #include <linux/fs.h>
+#include <sys/ioctl.h>

 #include "../kselftest.h"
 #include "../kselftest_harness.h"
@@ -218,11 +219,7 @@ static struct mntent* find_mount_point(const char *name)
  * Test mincore() behavior on a file-backed page.
  * No pages should be loaded into memory right after the mapping. Then,
  * accessing any address in the mapping range should load the page
- * containing the address and a number of subsequent pages (readahead).
- *
- * The actual readahead settings depend on the test environment, so we
- * can't make a lot of assumptions about that. This test covers the most
- * general cases.
+ * containing the address and a number of around pages (read around).
  */
 TEST(check_file_mmap)
 {
@@ -236,6 +233,10 @@ TEST(check_file_mmap)
 	int ra_pages = 0;
 	struct stat s;
 	struct mntent *mount_entry;
+	int ra_size;
+	int exp_ra_pages;
+	int odd = 0;
+	int dev_ra_pages;

 	mount_entry = find_mount_point(".");
 	ASSERT_NE(NULL, mount_entry) {
@@ -248,11 +249,29 @@ TEST(check_file_mmap)
 			"test is not supported");
 	}

+	errno = 0;
+	fd = open(mount_entry->mnt_fsname, O_RDONLY);
+	ASSERT_LT(0, fd) {
+		TH_LOG("Open %s failed: %s",
+			mount_entry->mnt_fsname, strerror(errno));
+	}
+
+	errno = 0;
+	retval = ioctl(fd, BLKRAGET, &ra_size);
+	ASSERT_EQ(0, retval) {
+		TH_LOG("Get block readahead size failed: %s", strerror(errno));
+	}
+
 	page_size = sysconf(_SC_PAGESIZE);
 	vec_size = FILE_SIZE / page_size;
 	if (FILE_SIZE % page_size)
 		vec_size++;

+	dev_ra_pages = (ra_size * 512) / page_size;
+	exp_ra_pages = MIN(vec_size / 2, dev_ra_pages / 2);
+	if (dev_ra_pages <= vec_size)
+		odd = dev_ra_pages % 2;
+
 	vec = calloc(vec_size, sizeof(unsigned char));
 	ASSERT_NE(NULL, vec) {
 		TH_LOG("Can't allocate array");
@@ -296,7 +315,7 @@ TEST(check_file_mmap)

 	/*
 	 * Touch a page in the middle of the mapping. We expect the next
-	 * few pages (the readahead window) to be populated too.
+	 * few pages (the read around window) to be populated too.
 	 */
 	addr[FILE_SIZE / 2] = 1;
 	retval = mincore(addr, FILE_SIZE, vec);
@@ -310,22 +329,49 @@ TEST(check_file_mmap)
 		ra_pages++;
 		i++;
 	}
-	EXPECT_GT(ra_pages, 0) {
-		TH_LOG("No read-ahead pages found in memory");
-	}

-	EXPECT_LT(i, vec_size) {
-		TH_LOG("Read-ahead pages reached the end of the file");
+	if (!exp_ra_pages) {
+		EXPECT_EQ(ra_pages, exp_ra_pages) {
+			TH_LOG("Check read-around pages failed");
+		}
+	} else {
+		EXPECT_EQ(ra_pages + (odd ? 0 : 1), exp_ra_pages) {
+			TH_LOG("Check read-around pages failed");
+		}
 	}
+
 	/*
-	 * End of the readahead window. The rest of the pages shouldn't
-	 * be in memory.
+	 * End of the read around window. The rest of the pages shouldn't
+	 * be in memory for the right hand.
 	 */
 	if (i < vec_size) {
 		while (i < vec_size && !vec[i])
 			i++;
 		EXPECT_EQ(vec_size, i) {
-			TH_LOG("Unexpected page in memory beyond readahead window");
+			TH_LOG("Unexpected page in memory beyond read around window");
+		}
+	}
+
+	i = FILE_SIZE / 2 / page_size - 1;
+	ra_pages = 0;
+	while (i >= 0 && vec[i]) {
+		ra_pages++;
+		i--;
+	}
+
+	EXPECT_EQ(ra_pages, exp_ra_pages) {
+		TH_LOG("Check read-around pages failed");
+	}
+
+	/*
+	 * End of the read around window. The rest of the pages shouldn't
+	 * be in memory for the left hand.
+	 */
+	if (i >= 0) {
+		while (i >= 0 && !vec[i])
+			i--;
+		EXPECT_EQ(-1, i) {
+			TH_LOG("Unexpected page in memory beyond read around window");
 		}
 	}

--
2.17.1


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

* Re: [PATCH -next 2/2] selftests/mincore: Optimize TEST(check_file_mmap) accoring to filemap read around
@ 2022-11-16  9:25 zhaogongyi
  0 siblings, 0 replies; 6+ messages in thread
From: zhaogongyi @ 2022-11-16  9:25 UTC (permalink / raw)
  To: linux-kselftest, linux-kernel, linux-fsdevel
  Cc: keescook, luto, wad, shuah, cristian.marussi, Matthew Wilcox

Hi!

> 
> TEST(check_file_mmap) will fail when we set the ra_pages through
> blockdev like:
>   /sbin/blockdev --setra 8196 /dev/sda
> 
> And, for file mmap, kernel will read 'ra_pages/2' pages of the address on
> the left hand or right hand. So, add a checking of ra_pages according to
> the ra_pages setting of the block dev.
> 
> I have tested it on my system like:
>   i=0
>   while [ $i -lt 9000 ]
>   do
>         /sbin/blockdev --setra $i /dev/openeuler/*
>         ./mincore_selftest  || { echo "$i test failed"; exit 1; }
>         let i=$i+1
>   done
> 
> Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
> ---
>  tools/testing/selftests/kselftest_harness.h   |  2 +
>  .../selftests/mincore/mincore_selftest.c      | 74 +++++++++++++++----
>  2 files changed, 62 insertions(+), 14 deletions(-)
> 
> diff --git a/tools/testing/selftests/kselftest_harness.h
> b/tools/testing/selftests/kselftest_harness.h
> index 25f4d54067c0..fa40c85a1099 100644
> --- a/tools/testing/selftests/kselftest_harness.h
> +++ b/tools/testing/selftests/kselftest_harness.h
> @@ -781,6 +781,8 @@
>  	} \
>  }
> 
> +#define MIN(a, b)       ((a) < (b) ? (a) : (b))
> +
>  struct __test_results {
>  	char reason[1024];	/* Reason for test result */
>  };
> diff --git a/tools/testing/selftests/mincore/mincore_selftest.c
> b/tools/testing/selftests/mincore/mincore_selftest.c
> index 287351a599a2..5eace1750fef 100644
> --- a/tools/testing/selftests/mincore/mincore_selftest.c
> +++ b/tools/testing/selftests/mincore/mincore_selftest.c
> @@ -17,6 +17,7 @@
>  #include <mntent.h>
>  #include <sys/stat.h>
>  #include <linux/fs.h>
> +#include <sys/ioctl.h>
> 
>  #include "../kselftest.h"
>  #include "../kselftest_harness.h"
> @@ -218,11 +219,7 @@ static struct mntent* find_mount_point(const
> char *name)
>   * Test mincore() behavior on a file-backed page.
>   * No pages should be loaded into memory right after the mapping. Then,
>   * accessing any address in the mapping range should load the page
> - * containing the address and a number of subsequent pages
> (readahead).
> - *
> - * The actual readahead settings depend on the test environment, so we
> - * can't make a lot of assumptions about that. This test covers the most
> - * general cases.
> + * containing the address and a number of around pages (read around).
>   */
>  TEST(check_file_mmap)
>  {
> @@ -236,6 +233,10 @@ TEST(check_file_mmap)
>  	int ra_pages = 0;
>  	struct stat s;
>  	struct mntent *mount_entry;
> +	int ra_size;
> +	int exp_ra_pages;
> +	int odd = 0;
> +	int dev_ra_pages;
> 
>  	mount_entry = find_mount_point(".");
>  	ASSERT_NE(NULL, mount_entry) {
> @@ -248,11 +249,29 @@ TEST(check_file_mmap)
>  			"test is not supported");
>  	}
> 
> +	errno = 0;
> +	fd = open(mount_entry->mnt_fsname, O_RDONLY);
> +	ASSERT_LT(0, fd) {
> +		TH_LOG("Open %s failed: %s",
> +			mount_entry->mnt_fsname, strerror(errno));
> +	}
> +
> +	errno = 0;
> +	retval = ioctl(fd, BLKRAGET, &ra_size);
> +	ASSERT_EQ(0, retval) {
> +		TH_LOG("Get block readahead size failed: %s", strerror(errno));
> +	}
> +
>  	page_size = sysconf(_SC_PAGESIZE);
>  	vec_size = FILE_SIZE / page_size;
>  	if (FILE_SIZE % page_size)
>  		vec_size++;
> 
> +	dev_ra_pages = (ra_size * 512) / page_size;
> +	exp_ra_pages = MIN(vec_size / 2, dev_ra_pages / 2);
> +	if (dev_ra_pages <= vec_size)
> +		odd = dev_ra_pages % 2;
> +
>  	vec = calloc(vec_size, sizeof(unsigned char));
>  	ASSERT_NE(NULL, vec) {
>  		TH_LOG("Can't allocate array");
> @@ -296,7 +315,7 @@ TEST(check_file_mmap)
> 
>  	/*
>  	 * Touch a page in the middle of the mapping. We expect the next
> -	 * few pages (the readahead window) to be populated too.
> +	 * few pages (the read around window) to be populated too.
>  	 */
>  	addr[FILE_SIZE / 2] = 1;
>  	retval = mincore(addr, FILE_SIZE, vec); @@ -310,22 +329,49 @@
> TEST(check_file_mmap)
>  		ra_pages++;
>  		i++;
>  	}
> -	EXPECT_GT(ra_pages, 0) {
> -		TH_LOG("No read-ahead pages found in memory");
> -	}
> 
> -	EXPECT_LT(i, vec_size) {
> -		TH_LOG("Read-ahead pages reached the end of the file");
> +	if (!exp_ra_pages) {
> +		EXPECT_EQ(ra_pages, exp_ra_pages) {
> +			TH_LOG("Check read-around pages failed");
> +		}
> +	} else {
> +		EXPECT_EQ(ra_pages + (odd ? 0 : 1), exp_ra_pages) {
> +			TH_LOG("Check read-around pages failed");
> +		}
>  	}
> +
>  	/*
> -	 * End of the readahead window. The rest of the pages shouldn't
> -	 * be in memory.
> +	 * End of the read around window. The rest of the pages shouldn't
> +	 * be in memory for the right hand.
>  	 */
>  	if (i < vec_size) {
>  		while (i < vec_size && !vec[i])
>  			i++;
>  		EXPECT_EQ(vec_size, i) {
> -			TH_LOG("Unexpected page in memory beyond readahead
> window");
> +			TH_LOG("Unexpected page in memory beyond read around
> window");
> +		}
> +	}
> +
> +	i = FILE_SIZE / 2 / page_size - 1;
> +	ra_pages = 0;
> +	while (i >= 0 && vec[i]) {
> +		ra_pages++;
> +		i--;
> +	}
> +
> +	EXPECT_EQ(ra_pages, exp_ra_pages) {
> +		TH_LOG("Check read-around pages failed");
> +	}
> +
> +	/*
> +	 * End of the read around window. The rest of the pages shouldn't
> +	 * be in memory for the left hand.
> +	 */
> +	if (i >= 0) {
> +		while (i >= 0 && !vec[i])
> +			i--;
> +		EXPECT_EQ(-1, i) {
> +			TH_LOG("Unexpected page in memory beyond read around
> window");
>  		}
>  	}
> 
> --
> 2.17.1


Gentele ping.

Regards,
Gongyi


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

* Re: [PATCH -next 2/2] selftests/mincore: Optimize TEST(check_file_mmap) accoring to filemap read around
@ 2022-10-31 12:13 zhaogongyi
  0 siblings, 0 replies; 6+ messages in thread
From: zhaogongyi @ 2022-10-31 12:13 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: linux-kselftest, linux-kernel, linux-fsdevel, keescook, luto,
	wad, shuah, cristian.marussi

Hi!

> On Mon, Oct 31, 2022 at 07:22:11AM +0000, zhaogongyi wrote:
> > Hi!
> >
> > +to linux-fsdevel@vger.kernel.org
> > +cc willy@infradead.org
> >
> > Regards,
> > Gongyi
> 
> what?

I have submitted tow patches reference to the testing of page cache, please see: https://patchwork.kernel.org/project/linux-kselftest/patch/20221021071052.143393-2-zhaogongyi@huawei.com/

The patches have not responded for a while, so I'm guessing that's the reason for my lack of cc to linux-fsdevel or page cache maintainer?

Best Regards,
Gongyi

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

* Re: [PATCH -next 2/2] selftests/mincore: Optimize TEST(check_file_mmap) accoring to filemap read around
  2022-10-31  7:22 zhaogongyi
@ 2022-10-31 10:27 ` Matthew Wilcox
  0 siblings, 0 replies; 6+ messages in thread
From: Matthew Wilcox @ 2022-10-31 10:27 UTC (permalink / raw)
  To: zhaogongyi
  Cc: linux-kselftest, linux-kernel, linux-fsdevel, keescook, luto,
	wad, shuah, cristian.marussi

On Mon, Oct 31, 2022 at 07:22:11AM +0000, zhaogongyi wrote:
> Hi!
> 
> +to linux-fsdevel@vger.kernel.org
> +cc willy@infradead.org
> 
> Regards,
> Gongyi

what?

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

* Re: [PATCH -next 2/2] selftests/mincore: Optimize TEST(check_file_mmap) accoring to filemap read around
@ 2022-10-31  7:22 zhaogongyi
  2022-10-31 10:27 ` Matthew Wilcox
  0 siblings, 1 reply; 6+ messages in thread
From: zhaogongyi @ 2022-10-31  7:22 UTC (permalink / raw)
  To: linux-kselftest, linux-kernel, linux-fsdevel
  Cc: keescook, luto, wad, shuah, cristian.marussi, willy

Hi!

+to linux-fsdevel@vger.kernel.org
+cc willy@infradead.org

Regards,
Gongyi


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

end of thread, other threads:[~2022-11-16  9:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-21  7:10 [PATCH -next 1/2] selftests/mincore: Add checking of block dev for TEST(check_file_mmap) Zhao Gongyi
2022-10-21  7:10 ` [PATCH -next 2/2] selftests/mincore: Optimize TEST(check_file_mmap) accoring to filemap read around Zhao Gongyi
2022-10-31  7:22 zhaogongyi
2022-10-31 10:27 ` Matthew Wilcox
2022-10-31 12:13 zhaogongyi
2022-11-16  9:25 zhaogongyi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.