mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: James Houghton <jthoughton@google.com>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Lorenzo Stoakes <ljs@kernel.org>, Zi Yan <ziy@nvidia.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	liam@infradead.org, Nico Pache <nico.pache@linux.dev>,
	Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
	Usama Arif <usama.arif@linux.dev>, Yang Shi <shy828301@gmail.com>,
	zokeefe@google.com, hughd@google.com,
	Kiryl Shutsemau <kas@kernel.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/2] mm: selftests: Adjust the MADV_COLLAPSE uffd-minor selftests
Date: Mon, 21 Sep 2026 20:59:50 +0200	[thread overview]
Message-ID: <4de1dfb3-b349-4916-9fe0-6b9697583ac7@kernel.org> (raw)
In-Reply-To: <20260910023411.514987-2-jthoughton@google.com>

On 9/10/26 04:34, James Houghton wrote:
> The behavior of MADV_COLLAPSE with uffd-minor VMAs has changed:
> MADV_COLLAPSE will no longer install PTEs where none existed before.

Misleading: MADV_COLLAPSE installed a PMD not PTEs?

> Update the selftest to demonstrate this new behavior.
> 
> On an unpatched kernel, the test will hit "unexpected memory contents
> after collapse".
> 
> If the selftest is left unpatched but the kernel is patched, the
> selftest will SKIP when it gets EINVAL back from MADV_COLLAPSE.
> 
> Signed-off-by: James Houghton <jthoughton@google.com>
> ---
>  tools/testing/selftests/mm/uffd-unit-tests.c | 75 ++++++++++++++++++--
>  1 file changed, 68 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c
> index ef9b3956bdcf..6f2360f9b75d 100644
> --- a/tools/testing/selftests/mm/uffd-unit-tests.c
> +++ b/tools/testing/selftests/mm/uffd-unit-tests.c
> @@ -518,19 +518,34 @@ static void uffd_wp_fork_pin_with_event_test(uffd_global_test_opts_t *gopts, uff
>  	uffd_wp_fork_pin_test_common(gopts, args, true);
>  }
>  
> -static void check_memory_contents(uffd_global_test_opts_t *gopts, char *p)
> +static int __check_memory_contents(unsigned long offset,
> +				   unsigned long nr_pages,
> +				   uffd_global_test_opts_t *gopts,
> +				   char *p)

Two tabs look better ;)

>  {
>  	unsigned long i, j;
>  	uint8_t expected_byte;
>  
> -	for (i = 0; i < gopts->nr_pages; ++i) {
> +	if (nr_pages + offset < nr_pages)
> +		err("overflow in memory check");
> +	if (nr_pages + offset > gopts->nr_pages)
> +		err("out of bounds memory check");
> +
> +	for (i = offset; i < offset + nr_pages; ++i) {
>  		expected_byte = ~((uint8_t)(i % ((uint8_t)-1)));
>  		for (j = 0; j < gopts->page_size; j++) {
>  			uint8_t v = *(uint8_t *)(p + (i * gopts->page_size) + j);
>  			if (v != expected_byte)
> -				err("unexpected page contents");
> +				return 1;
>  		}
>  	}
> +
> +	return 0;
> +}
> +
> +static int check_memory_contents(uffd_global_test_opts_t *gopts, char *p)
> +{
> +	return __check_memory_contents(0, gopts->nr_pages, gopts, p);
>  }
>  
>  static void uffd_minor_test_common(uffd_global_test_opts_t *gopts, bool test_collapse, bool test_wp)
> @@ -539,6 +554,8 @@ static void uffd_minor_test_common(uffd_global_test_opts_t *gopts, bool test_col
>  	pthread_t uffd_mon;
>  	char c = '\0';
>  	struct uffd_args args = { 0 };
> +	unsigned long checked = 0;
> +	bool bad_contents;

Through in an empty line. Or better

struct uffd_args args = { .gopts = gopts };

>  
>  	/*
> @@ -564,24 +581,65 @@ static void uffd_minor_test_common(uffd_global_test_opts_t *gopts, bool test_col
>  	if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
>  		err("uffd_poll_thread create");
>  
> +	if (test_collapse) {
> +		/*
> +		 * Read just a single page and try collapsing. The collapse
> +		 * should either be rejected or be a no-op.
> +		 */
> +		if (__check_memory_contents(0, 1, gopts, gopts->area_dst_alias))
> +			err("unexpected memory contents before collapse");
> +
> +		/* MADV_COLLAPSE might return EINVAL for uffd-minor VMAs. */
> +		madvise(gopts->area_dst_alias, gopts->nr_pages * gopts->page_size,
> +			MADV_COLLAPSE);

Add an explicit check for expected errnos?

> +		/*
> +		 * If the above collapse mapped pages that were not explicitly
> +		 * CONTINUE'd, the below __check_memory_contents() will not
> +		 * fault on some pages, resulting in incorrect contents. The
> +		 * PTE for the first page may get retracted, so avoid checking
> +		 * that page, as we might take a second fault, flipping the
> +		 * contents a second time.
> +		 */
> +		checked = 1;

I'm not an expert on this code. But a trivial check to see whether there is
something in the page tables that shouldn't be there is through the help of
pagemap_is_populated().

Maybe that would help to simplify things, not sure.


-- 
Cheers,

David

  reply	other threads:[~2026-09-21 19:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10  2:34 [PATCH v3 1/2] mm/khugepaged: Never install PMDs in uffd-minor-registered VMAs James Houghton
2026-09-10  2:34 ` [PATCH v3 2/2] mm: selftests: Adjust the MADV_COLLAPSE uffd-minor selftests James Houghton
2026-09-21 18:59   ` David Hildenbrand (Arm) [this message]
2026-09-10 23:24 ` [PATCH v3 1/2] mm/khugepaged: Never install PMDs in uffd-minor-registered VMAs Andrew Morton
2026-09-11  0:02   ` James Houghton
2026-09-11  0:06     ` James Houghton
2026-09-11  0:57     ` James Houghton
2026-09-21 18:52 ` David Hildenbrand (Arm)

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=4de1dfb3-b349-4916-9fe0-6b9697583ac7@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=dev.jain@arm.com \
    --cc=hughd@google.com \
    --cc=jthoughton@google.com \
    --cc=kas@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=nico.pache@linux.dev \
    --cc=ryan.roberts@arm.com \
    --cc=shy828301@gmail.com \
    --cc=usama.arif@linux.dev \
    --cc=ziy@nvidia.com \
    --cc=zokeefe@google.com \
    /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

all inboxes | Powered by JetHome®