Re: [PATCH v6 4/4] selftests/mm: test fork of PMD-mapped shmem THPs
From: David Hildenbrand (Arm)
Date: Sun Nov 22 2026 - 11:45:13 EST
On 11/19/26 19:32, Andrew Morton wrote:
> On Thu, 19 Nov 2026 14:44:05 +0100 Konstantin Khorenko <khorenko@xxxxxxxxxxxxx> 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.
>>
>> ...
>>
>> +static void test_fork(bool shared)
>> +{
>> + const char *desc = shared ? "shared mapping with a guard region" :
>> + "private mapping with an anon_vma";
>> + long before = 0, after = 0;
>> + enum result res = NO_THP;
>> + int i, drops = 0;
>> + char *mem;
>> +
>> + for (i = 0; i < ATTEMPTS; i++) {
>> + mem = map_file(shared);
>> + /* Make fork copy the page tables; the last PMD gets zapped. */
>> + if (shared) {
>> + if (madvise(mem + size - pagesize, pagesize,
>> + MADV_GUARD_INSTALL)) {
>> + ksft_test_result_skip("%s: MADV_GUARD_INSTALL: %s\n",
>> + desc, strerror(errno));
>> + return;
>> + }
>> + } else {
>> + mem[size - pagesize] = 'p';
>> + }
>> +
>> + res = pmds_ok(mem) ? fork_once(mem, shared, &before, &after) :
>> + res == NO_THP ? NO_THP : LOST_PMDS;
>> + /* Later children get only their own mapping. */
>> + madvise(mem, size, MADV_DONTFORK);
>
> autobot want the return value checked:
>
> : If this fails, the next child inherits an earlier test mapping too.
> : Its exit can then change ShmemPmdMapped by more than the expected
> : amount, causing the test to skip a real failure.
>
> which seems ignorable - MADV_DONTFORK failures are super unlikely at present.
>
>>
>> ...
>>
>> +int main(void)
>> +{
>> + ksft_print_header();
>> + pagesize = getpagesize();
>> + pmdsize = read_pmd_pagesize();
>
> This is going to ruin Mark's day again, thanks to "kselftest: mm:
> rename read_pmd_pagesize() to pmd_psize()".
>
> I think I'll alter Yeoreum's above patch like this (presently untested):
>
> --- a/tools/testing/selftests/mm/vm_util.h~kselftest-mm-rename-read_pmd_pagesize-to-pmd_psize-fix
> +++ a/tools/testing/selftests/mm/vm_util.h
> @@ -93,6 +93,8 @@ unsigned long pagemap_get_pfn(int fd, ch
> void clear_softdirty(void);
> bool check_for_pattern(FILE *fp, const char *pattern, char *buf, size_t len);
> uint64_t pmd_psize(void);
> +/* temporary cross-tree compatibility thing: */
> +#define read_pmd_pagesize() pmd_psize()
Thanks! Otherwise we could route selftest through a single tree.
They would fail on the tree until the fix is in ... but that's rpecisely what
selftests are for.
--
Cheers,
David