Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Konstantin Khorenko <khorenko@virtuozzo.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Zi Yan <ziy@nvidia.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	"Liam R. Howlett" <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>,
	Kiryl Shutsemau <kas@kernel.org>, Peter Xu <peterx@redhat.com>
Cc: "Alex Williamson" <alex@shazbot.org>,
	kvm@vger.kernel.org,
	"Loïc Molinari" <loic.molinari@collabora.com>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Shuah Khan" <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org, dri-devel@lists.freedesktop.org,
	"Madhavan Srinivasan" <maddy@linux.ibm.com>,
	"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
	linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, "Jason Gunthorpe" <jgg@ziepe.ca>
Subject: Re: [PATCH v6 4/4] selftests/mm: test fork of PMD-mapped shmem THPs
Date: Sun, 22 Nov 2026 17:52:32 +0100	[thread overview]
Message-ID: <c02a4f0a-7790-4949-9037-b346ffbe3921@kernel.org> (raw)
In-Reply-To: <20261119-b4-prep-vstor-149847-copy_huge_pmd-recheck-special-pmd-v6-4-efa7817a2bb3@virtuozzo.com>

On 11/19/26 14:44, Konstantin Khorenko wrote:
> Fork a process that maps shmem THPs with PMDs, in a VMA whose page
> tables fork copies: a private mapping with an anon_vma, and a shared
> one with a guard region. The child must find the PMDs before it
> touches the mapping, and its exit must leave the parent's mapping and
> the system-wide ShmemPmdMapped counter as they were.
> 
> With folio_dup_file_rmap_pmd() adding PTE mappings, the child's exit
> takes the parent's share off ShmemPmdMapped while the parent still maps
> the folios. Unmapping them drops it once more, so every mapping stays
> until exit, where the kernel reports "Bad page cache", or hits a
> VM_BUG_ON with CONFIG_DEBUG_VM. A kernel that leaves file THPs to a
> refault in the child skips the test.
> 
> ShmemPmdMapped is system-wide, and reclaim or NUMA balancing can take a
> PMD from the parent while the test runs. So the test makes up to three
> attempts, each on a fresh file, and fails only if all of them see the
> counter drop by exactly the parent's share: the bug does that on every
> fork, other processes only by chance. It passes on the first clean
> attempt and skips when none is clean.
> 
> The test mounts a tmpfs with huge=always over /tmp in a private mount
> namespace, so it leaves the THP settings and the host's mounts and
> files alone.
> 
> Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
> ---
>  MAINTAINERS                                |   1 +
>  tools/testing/selftests/mm/Makefile        |   1 +
>  tools/testing/selftests/mm/fork_file_thp.c | 256 +++++++++++++++++++++++++++++
>  tools/testing/selftests/mm/run_vmtests.sh  |   1 +
>  4 files changed, 259 insertions(+)
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index d738990161bd0..7ade132e3d3ab 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -17941,6 +17941,7 @@ F:	mm/huge_memory.c
>  F:	mm/khugepaged.c
>  F:	mm/mm_slot.h
>  F:	tools/mm/thpmaps
> +F:	tools/testing/selftests/mm/fork_file_thp.c
>  F:	tools/testing/selftests/mm/khugepaged.c
>  F:	tools/testing/selftests/mm/split_huge_page_test.c
>  F:	tools/testing/selftests/mm/transhuge-stress.c
> diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
> index 0ea72e9979347..2489027b2a36b 100644
> --- a/tools/testing/selftests/mm/Makefile
> +++ b/tools/testing/selftests/mm/Makefile
> @@ -108,6 +108,7 @@ TEST_GEN_FILES += folio_split_race_test
>  TEST_GEN_FILES += folio_order_check
>  TEST_GEN_FILES += khugepaged_sync_check
>  TEST_GEN_FILES += khugepaged_race
> +TEST_GEN_FILES += fork_file_thp
>  TEST_GEN_FILES += soft-dirty
>  
>  ifeq ($(ARCH),x86_64)
> diff --git a/tools/testing/selftests/mm/fork_file_thp.c b/tools/testing/selftests/mm/fork_file_thp.c
> new file mode 100644
> index 0000000000000..aaecf7ba6f406
> --- /dev/null
> +++ b/tools/testing/selftests/mm/fork_file_thp.c
> @@ -0,0 +1,256 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * fork() of a process that maps shmem THPs with PMDs, in a VMA whose page
> + * tables fork copies: a private mapping with an anon_vma, and a shared
> + * mapping with a guard region.
> + *
> + * The child must get the PMDs without faulting, and its exit must leave the
> + * parent's mapping and the system-wide ShmemPmdMapped counter as they were.
> + * Kernels that leave file THPs to a refault in the child skip the test.
> + */
> +#define _GNU_SOURCE
> +#include <asm-generic/mman.h> /* Force the import of the tools version. */
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <sched.h>
> +#include <sys/mman.h>
> +#include <sys/mount.h>
> +#include <sys/wait.h>
> +
> +#include "kselftest.h"
> +#include "vm_util.h"
> +
> +#define NR_PMDS		4
> +#define MAX_PMDSIZE	(32UL << 20)
> +/* Other processes, reclaim and NUMA balancing can disturb an attempt. */
> +#define ATTEMPTS	3
> +
> +static size_t pagesize, pmdsize, size;
> +
> +static long meminfo_kb(const char *key)
> +{
> +	char line[256];
> +	long kb = -1;
> +	FILE *f = fopen("/proc/meminfo", "r");
> +
> +	if (!f)
> +		return -1;
> +	while (fgets(line, sizeof(line), f))
> +		if (!strncmp(line, key, strlen(key))) {
> +			kb = strtol(line + strlen(key), NULL, 10);
> +			break;
> +		}
> +	fclose(f);
> +	return kb;
> +}
> +
> +/* Skips the last page, which may be a guard page, and @skip. */
> +static bool data_ok(const char *mem, size_t skip)
> +{
> +	size_t off;
> +
> +	for (off = 0; off < size - pagesize; off += pagesize)
> +		if (off != skip && mem[off] != (char)(off / pmdsize + 1))
> +			return false;
> +	return true;
> +}
> +
> +/* The last PMD is zapped by the setup, the others must be PMD-mapped. */
> +static bool pmds_ok(char *mem)
> +{
> +	return check_huge_file(mem, size - pmdsize, NR_PMDS - 1, pmdsize);
> +}
> +
> +/* Maps a fresh shmem file of NR_PMDS PMDs, PMD-aligned, until exit. */
> +static char *map_file(bool shared)
> +{
> +	char *area, *buf, *mem;
> +	size_t off;
> +	int fd;
> +
> +	fd = open("/tmp/fork_file_thp", O_RDWR | O_CREAT | O_TRUNC, 0600);
> +	if (fd < 0)
> +		ksft_exit_fail_perror("open");
> +	unlink("/tmp/fork_file_thp");
> +	buf = malloc(pmdsize);
> +	if (!buf)
> +		ksft_exit_fail_msg("malloc failed\n");
> +	for (off = 0; off < size; off += pmdsize) {
> +		memset(buf, off / pmdsize + 1, pmdsize);
> +		if (pwrite(fd, buf, pmdsize, off) != (ssize_t)pmdsize)
> +			ksft_exit_fail_perror("pwrite");
> +	}
> +	free(buf);
> +
> +	area = mmap(NULL, size + pmdsize, PROT_NONE,
> +		    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +	if (area == MAP_FAILED)
> +		ksft_exit_fail_perror("mmap");
> +	mem = (char *)(((unsigned long)area + pmdsize - 1) & ~(pmdsize - 1));
> +	mem = mmap(mem, size, PROT_READ | PROT_WRITE,
> +		   (shared ? MAP_SHARED : MAP_PRIVATE) | MAP_FIXED, fd, 0);
> +	if (mem == MAP_FAILED)
> +		ksft_exit_fail_perror("mmap");
> +	close(fd);
> +	for (off = 0; off < size; off += pmdsize)
> +		FORCE_READ(mem[off]);
> +	return mem;
> +}
> +
> +/* Child exit codes, apart from what kselftest helpers exit with. */
> +enum result { PASS, NO_PMDS = 10, FAIL_DATA, FAIL_COUNTER, FAIL_PARENT,
> +	      FAIL_KILLED, COUNTER_OFF, CHANGED, LOST_PMDS, NO_THP };
> +
> +static enum result fork_once(char *mem, bool shared, long *before, long *after)
> +{
> +	/* The parent alone maps this much with PMDs. */
> +	long own = (NR_PMDS - 1) * (pmdsize >> 10);
> +	bool parent_pmds;
> +	int status;
> +	pid_t pid;
> +
> +	*before = *after = meminfo_kb("ShmemPmdMapped:");

That's not how we test for PMD mappings from other code.

We have check_huge_file() etc helpers in vm_util.c for that.


Further, some things look a bit odd, like:

+static void test_fork(bool shared)
+{
+	const char *desc = shared ? "shared mapping with a guard region" :
+				    "private mapping with an anon_vma";

Why guard regions? Why boolean parameters?

This selftest should probably be written using the kselftest harness instead.

BUT I am not convinced the selftest as is is worth it. It check for too too
detailed corner cases.

I think we'd want something fairly simple, nothing the does detailed accounting
checks.

I'll queue 1-3 for now, thanks!

-- 
Cheers,

David


  parent reply	other threads:[~2026-11-22 16:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 13:44 [PATCH v6 0/4] mm/huge_memory: copy PMDs at fork like the PTE path does Konstantin Khorenko
2026-11-19 13:44 ` [PATCH v6 1/4] mm/rmap: duplicate a PMD mapping as one in folio_dup_file_rmap_pmd() Konstantin Khorenko
2026-11-19 13:44 ` [PATCH v6 2/4] mm/huge_memory: copy file THPs at fork like copy_present_ptes() does Konstantin Khorenko
2026-11-22 16:38   ` David Hildenbrand (Arm)
2026-11-19 13:44 ` [PATCH v6 3/4] mm/huge_memory: read a special PMD under the lock in copy_huge_pmd() Konstantin Khorenko
2026-11-19 13:44 ` [PATCH v6 4/4] selftests/mm: test fork of PMD-mapped shmem THPs Konstantin Khorenko
2026-11-19 18:32   ` Andrew Morton
2026-11-22 16:44     ` David Hildenbrand (Arm)
2026-11-22 16:52   ` David Hildenbrand (Arm) [this message]
2026-11-22 17:00 ` (subset) [PATCH v6 0/4] mm/huge_memory: copy PMDs at fork like the PTE path does David Hildenbrand

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=c02a4f0a-7790-4949-9037-b346ffbe3921@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alex@shazbot.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=dev.jain@arm.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jgg@ziepe.ca \
    --cc=kas@kernel.org \
    --cc=khorenko@virtuozzo.com \
    --cc=kvm@vger.kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=ljs@kernel.org \
    --cc=loic.molinari@collabora.com \
    --cc=maddy@linux.ibm.com \
    --cc=nico.pache@linux.dev \
    --cc=peterx@redhat.com \
    --cc=ritesh.list@gmail.com \
    --cc=ryan.roberts@arm.com \
    --cc=shuah@kernel.org \
    --cc=usama.arif@linux.dev \
    --cc=ziy@nvidia.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