linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
To: Andrei Vagin <avagin@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org,
	David Hildenbrand <david@redhat.com>,
	Shuah Khan <shuah@kernel.org>, Jonathan Corbet <corbet@lwn.net>,
	Andrei Vagin <avagin@gmail.com>
Subject: Re: [PATCH 2/2] selftests/mm: add PAGEMAP_SCAN guard region test
Date: Thu, 20 Mar 2025 10:38:21 +0000	[thread overview]
Message-ID: <90d37a32-d913-4e15-bba3-1fd12747ff82@lucifer.local> (raw)
In-Reply-To: <20250320063903.2685882-3-avagin@google.com>

On Thu, Mar 20, 2025 at 06:39:03AM +0000, Andrei Vagin wrote:
> From: Andrei Vagin <avagin@gmail.com>
>
> Add a selftest to verify the PAGEMAP_SCAN ioctl correctly reports guard
> regions using the newly introduced PAGE_IS_GUARD flag.
>
> Signed-off-by: Andrei Vagin <avagin@gmail.com>
> ---
>  tools/testing/selftests/mm/guard-regions.c | 53 ++++++++++++++++++++++
>  1 file changed, 53 insertions(+)
>
> diff --git a/tools/testing/selftests/mm/guard-regions.c b/tools/testing/selftests/mm/guard-regions.c
> index 0c7183e8b661..24e09092fda5 100644
> --- a/tools/testing/selftests/mm/guard-regions.c
> +++ b/tools/testing/selftests/mm/guard-regions.c
> @@ -8,6 +8,7 @@
>  #include <fcntl.h>
>  #include <linux/limits.h>
>  #include <linux/userfaultfd.h>
> +#include <linux/fs.h>

This is insufficient, you also need to update tools/include/uapi/linux/fs.h
- we don't want to have to rely on make headers for these tests.

Basically just:

cp include/uapi/linux/fs.h tools/include/uapi/linux/fs.h

And commit this (you can see format of commit messages like this in git log
for that file).

Thanks!

>  #include <setjmp.h>
>  #include <signal.h>
>  #include <stdbool.h>
> @@ -2079,4 +2080,56 @@ TEST_F(guard_regions, pagemap)
>  	ASSERT_EQ(munmap(ptr, 10 * page_size), 0);
>  }
>
> +/*
> + * Assert that PAGEMAP_SCAN correctly reports guard region ranges.
> + */
> +TEST_F(guard_regions, pagemap_scan)
> +{
> +	const unsigned long page_size = self->page_size;
> +	struct page_region pm_regs[10];
> +	struct pm_scan_arg pm_scan_args = {
> +		.size = sizeof(struct pm_scan_arg),
> +		.category_anyof_mask = PAGE_IS_GUARD,
> +		.return_mask = PAGE_IS_GUARD,
> +		.vec = (long)&pm_regs,
> +		.vec_len = ARRAY_SIZE(pm_regs),
> +	};

Yeah this interface is quite nice actually... :)

> +	int proc_fd, i;
> +	char *ptr;
> +
> +	proc_fd = open("/proc/self/pagemap", O_RDONLY);
> +	ASSERT_NE(proc_fd, -1);
> +
> +	ptr = mmap_(self, variant, NULL, 10 * page_size,
> +		    PROT_READ | PROT_WRITE, 0, 0);
> +	ASSERT_NE(ptr, MAP_FAILED);
> +
> +	pm_scan_args.start = (long)ptr;
> +	pm_scan_args.end = (long)ptr + 10 * page_size;
> +	ASSERT_EQ(ioctl(proc_fd, PAGEMAP_SCAN, &pm_scan_args), 0);
> +	ASSERT_EQ(pm_scan_args.walk_end, (long)ptr + 10 * page_size);
> +
> +	/* Install a guard region in every other page. */
> +	for (i = 0; i < 10; i += 2) {
> +		char *ptr_p = &ptr[i * page_size];
> +
> +		ASSERT_EQ(syscall(__NR_madvise, ptr_p, page_size, MADV_GUARD_INSTALL), 0);
> +	}
> +
> +	ASSERT_EQ(ioctl(proc_fd, PAGEMAP_SCAN, &pm_scan_args), 5);

Nit but might be worth saying that you're asserting 5 because every other
over 10.

As trivial as;

/* Assert ioctl() returns the count of located pages */

Which I presume it does right?

> +	ASSERT_EQ(pm_scan_args.walk_end, (long)ptr + 10 * page_size);
> +
> +	/* Re-read from pagemap, and assert guard regions are detected. */
> +	for (i = 0; i < 5; i++) {
> +		long ptr_p = (long)&ptr[2 * i * page_size];
> +
> +		ASSERT_EQ(pm_regs[i].start, ptr_p);
> +		ASSERT_EQ(pm_regs[i].end, ptr_p + page_size);
> +		ASSERT_EQ(pm_regs[i].categories, PAGE_IS_GUARD);
> +	}
> +
> +	ASSERT_EQ(close(proc_fd), 0);
> +	ASSERT_EQ(munmap(ptr, 10 * page_size), 0);
> +}

Generally test looks ok though, thanks! Passing in my setup.

> +
>  TEST_HARNESS_MAIN
> --
> 2.49.0.rc1.451.g8f38331e32-goog
>


  reply	other threads:[~2025-03-20 10:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-20  6:39 [PATCH 0/2] fs/proc: extend the PAGEMAP_SCAN ioctl to report Andrei Vagin
2025-03-20  6:39 ` [PATCH 1/2] fs/proc: extend the PAGEMAP_SCAN ioctl to report guard regions Andrei Vagin
2025-03-20 10:51   ` Lorenzo Stoakes
2025-03-21 10:49   ` David Hildenbrand
2025-03-20  6:39 ` [PATCH 2/2] selftests/mm: add PAGEMAP_SCAN guard region test Andrei Vagin
2025-03-20 10:38   ` Lorenzo Stoakes [this message]
2025-03-20 10:57 ` [PATCH 0/2] fs/proc: extend the PAGEMAP_SCAN ioctl to report Lorenzo Stoakes
2025-03-20 15:00   ` Andrei Vagin
2025-03-21  9:49     ` Lorenzo Stoakes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=90d37a32-d913-4e15-bba3-1fd12747ff82@lucifer.local \
    --to=lorenzo.stoakes@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=avagin@gmail.com \
    --cc=avagin@google.com \
    --cc=corbet@lwn.net \
    --cc=david@redhat.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox