On 01/06/26 2:05 pm, Lorenzo Stoakes wrote:
> On Mon, Jun 01, 2026 at 10:40:48AM +0530, Dev Jain wrote:
>>
>>
>> On 01/06/26 5:39 am, Balbir Singh wrote:
>>> On 5/30/26 18:54, Dev Jain wrote:
>>>> To cover pagemap paths scanning PMD entries, add assertions to check
>>>> whether a device-private PMD entry has the correct pagemap information -
>>>> the PM_SWAP bit must be on in the pagemap entry. Before that, we must
>>>> assert through HMM_DMIRROR_SNAPSHOT snapshot that the leaf entry is
>>>> at PMD level and not PTE level.
>>>>
>>>> Signed-off-by: Dev Jain <[email protected]>
>>>> ---
>>>> tools/testing/selftests/mm/hmm-tests.c | 29 ++++++++++++++++++++++++++
>>>> 1 file changed, 29 insertions(+)
>>>>
>>>> diff --git a/tools/testing/selftests/mm/hmm-tests.c
>>>> b/tools/testing/selftests/mm/hmm-tests.c
>>>> index e1c8a679a4cf3..d09d4a9081de1 100644
>>>> --- a/tools/testing/selftests/mm/hmm-tests.c
>>>> +++ b/tools/testing/selftests/mm/hmm-tests.c
>>>> @@ -2276,8 +2276,11 @@ TEST_F(hmm, migrate_anon_huge_fault)
>>>> unsigned long npages;
>>>> unsigned long size;
>>>> unsigned long i;
>>>> + unsigned char *m;
>>>> + uint64_t entry;
>>>> void *old_ptr;
>>>> void *map;
>>>> + int pagemap_fd;
>>>> int *ptr;
>>>> int ret;
>>>>
>>>> @@ -2318,6 +2321,32 @@ TEST_F(hmm, migrate_anon_huge_fault)
>>>> for (i = 0, ptr = buffer->mirror; i < size / sizeof(*ptr); ++i)
>>>> ASSERT_EQ(ptr[i], i);
>>>>
>>>> + if (!hmm_is_coherent_type(variant->device_number)) {
>>>> + ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_SNAPSHOT,
>>>> + buffer, npages);
>>>> + ASSERT_EQ(ret, 0);
>>>> + ASSERT_EQ(buffer->cpages, npages);
>>>> +
>>>> + m = buffer->mirror;
>>>> + for (i = 0; i < npages; ++i)
>>>> + ASSERT_EQ(m[i], HMM_DMIRROR_PROT_DEV_PRIVATE_LOCAL |
>>>> + HMM_DMIRROR_PROT_WRITE |
>>>> + HMM_DMIRROR_PROT_PMD);
>>>
>>> madvise(..., MADV_HUGEPAGE) is not sufficient to guarantee that the
>>> allocation
>>> was indeed converted to THP. Might be worth using the kpageflags interface
>>> (but that
>>> requires elevated privileges) and then KPF_THP? Otherwise the
>>> HMM_DMIRROR_PROT_PMD
>>> can be a miss from time to time. One other option is not to assert, but to
>>> check
>>> and inform?
>>
>> I'll then use the existing check_huge_anon() to assert that a PMD THP got
>> allocated.
>
> Would MADV_COLLAPSE work here?
So MADV_COLLAPSE requires at least one page to be faulted in (or precisely,
HPAGE_PMD_NR - khugepaged_max_ptes_none). So I rejected that, but now I
noticed that we are initializing the buffer first.
So yes I will use this, thanks.
>
>>
>>>
>>>> +
>>>> + pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
>>>> + ASSERT_GE(pagemap_fd, 0);
>>>> +
>>>> + for (i = 0; i < npages; ++i) {
>>>> + entry = pagemap_get_entry(pagemap_fd,
>>>> + (char *)buffer->ptr + i *
>>>> self->page_size);
>>>> +
>>>
>>> If this is a THP entry, do we have valid pagemap entries for offset of i *
>>> page_size?
>>
>> Yep we do, see the populate_pagemap label in pagemap_pmd_range_thp.
>>
>>>
>>>> + ASSERT_NE(entry & PM_SWAP, 0);
>>>> + ASSERT_EQ(entry & PM_PRESENT, 0);
>>>
>>> Nit: You can use PAGEMAP_PRESENT()
>>
>> Okay.
>>
>>>
>>>> + }
>>>> +
>>>> + close(pagemap_fd);
>>>> + }
>>>> +
>>>> /* Fault pages back to system memory and check them. */
>>>> for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i)
>>>> ASSERT_EQ(ptr[i], i);
>>>
>>> Balbir Singh
>>
>
> Cheers, Lorenzo