Re: [PATCH v4 5/5] ksm: add mremap selftests for ksm_rmap_walk
From: xu.xin16
Date: Fri May 15 2026 - 22:30:44 EST
> > --- a/tools/testing/selftests/mm/rmap.c
> > +++ b/tools/testing/selftests/mm/rmap.c
> > @@ -430,4 +430,83 @@ TEST_F(migrate, ksm)
> > propagate_children(_metadata, data);
> > }
> >
> > +static void prepare_two_pages(struct global_data *data)
> > +{
> > + /* Allocate exactly 2 pages for the test */
> > + data->mapsize = 2 * getpagesize();
> > + data->region = mmap(NULL, data->mapsize, PROT_READ | PROT_WRITE,
> > + MAP_PRIVATE | MAP_ANON, -1, 0);
> > + if (data->region == MAP_FAILED)
> > + ksft_exit_fail_perror("mmap failed");
> > +
> > + /* Fill both pages with identical content to encourage KSM merging */
> > + memset(data->region, 0x77, data->mapsize);
> > +}
> > +
> > +static int mremap_merge_and_migrate(struct global_data *data)
> > +{
> > + int ret, pagemap_fd;
> > + void *old_region = data->region;
> > + unsigned long page_sz = getpagesize();
> > +
> > + /*
> > + * Mremap the second page to the first page's location (FIXED).
> > + * This effectively overwrites the first page, leaving the second page
> > + * unmapped. The physical page originally at the second page is now
> > + * mapped at the first page's virtual address.
> > + */
>
>
> But if there is only a single page, why should it get merged by KSM?
This test runs in multi-threads, So there are not only one page from this thread but
also many other pages which are mergeable in all other threads.