Re: [PATCH v4 5/5] ksm: add mremap selftests for ksm_rmap_walk
From: David Hildenbrand (Arm)
Date: Mon May 18 2026 - 05:22:55 EST
On 5/16/26 04:30, xu.xin16@xxxxxxxxxx wrote:
>>> --- 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.
At least the test did not reveal the problem in patch #3 :)
Can't we just use a single VMA (e.g., 32 pages), fill it with pages of the same
content, let it merge, then mremap half of it, and then migrate a page etc?
--
Cheers,
David