* Re: [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration
2026-11-25 6:53 [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration Andrew Morton
@ 2026-11-25 14:15 ` Alexandru Elisei
2026-11-25 14:25 ` Aveline Noir
2026-11-25 15:06 ` Mika Penttilä
2026-11-25 15:35 ` Aveline Noir
2026-11-26 6:53 ` sashiko-bot
2 siblings, 2 replies; 10+ messages in thread
From: Alexandru Elisei @ 2026-11-25 14:15 UTC (permalink / raw)
To: Andrew Morton
Cc: Mika Penttilä, linux-mm, Aveline Noir, Ralph Campbell,
Jason Gunthorpe, Leon Romanovsky, Shuah Khan, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
stable
Hi,
I tried to test this patch, applied on top of v7.4-rc4.
With the changes to the migrate_anon_huge_fault test, but with no changes to
lib/test_hmm.c, to see if the test fails without the fix to the kernel, I get a
kernel fault:
[ 95.571212] Unable to handle kernel paging request at virtual address ffff7fffdfe00000
[ 95.571221] Mem abort info:
[ 95.571223] ESR = 0x0000000096000004
[ 95.571225] EC = 0x25: DABT (current EL), IL = 32 bits
[ 95.571228] SET = 0, FnV = 0
[ 95.571230] EA = 0, S1PTW = 0
[ 95.571231] FSC = 0x04: level 0 translation fault
[ 95.571234] Data abort info:
[ 95.571235] ISV = 0, ISS = 0x00000004, ISS2 = 0x00000000
[ 95.571237] CM = 0, WnR = 0, TnD = 0, TagAccess = 0
[ 95.571239] GCS = 0, Overlay = 0, DirtyBit = 0
[ 95.571242] swapper pgtable: 4k pages, 48-bit VAs, pgdp=00000001cc71d000
[ 95.571245] [ffff7fffdfe00000] pgd=0000000000000000, p4d=0000000000000000
[..]
[ 95.692002] pc : __pi_memcpy_generic+0x110/0x230
[ 95.696623] lr : dmirror_do_read+0x7c/0xf0
[..]
decode_stacktrace.sh says that happens in lib/test_hmm.c:503.
This looks more serious that the use-after-free that KASAN reported.
With the changes to the test and to the kernel, I get this error when running
the test:
$ sudo tools/testing/selftests/mm/hmm-tests -r hmm.hmm_device_private.migrate_anon_huge_fault
# [INFO] detected hugetlb page size: 2048 KiB
# [INFO] detected hugetlb page size: 32768 KiB
# [INFO] detected hugetlb page size: 64 KiB
# [INFO] detected hugetlb page size: 1048576 KiB
TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN hmm.hmm_device_private.migrate_anon_huge_fault ...
# hmm-tests.c:2156:migrate_anon_huge_fault:Expected *(int *)buffer->mirror (0) == (npages - 1) * self->page_size / sizeof(int) (523264)
# migrate_anon_huge_fault: Test terminated by assertion
# FAIL hmm.hmm_device_private.migrate_anon_huge_fault
not ok 1 hmm.hmm_device_private.migrate_anon_huge_fault
# FAILED: 0 / 1 tests passed.
# Totals: pass:0 fail:1 xfail:0 xpass:0 skip:0 error:0
Might be something on my side? I'll investigate further, but I'm not very
familiar with the code.
Thanks,
Alex
On Tue, Nov 24, 2026 at 10:53:38PM -0800, Andrew Morton wrote:
> From: Andrew Morton <akpm@linux-foundation.org>
> Subject: lib/test_hmm: invalidate all mirrors on device-private migration
> Date: Sun, 22 Nov 2026 08:53:12 -0800
>
> When one open of the fake HMM device migrates a folio to device-private
> memory, a second open can fault the same address into its mirror page
> table. The second mirror then stores the inaccessible device page, which
> can oops the kernel when dmirror_do_read() copies from it. Store the
> corresponding RAM backing subpage instead.
>
> That conversion also makes the backing page's lifetime significant to
> the second mirror. migrate_vma_setup() notifies all mirrors before CPU
> faultback, but dmirror_interval_invalidate() skips the notification for
> every open on the same device. The faulting open clears only its own
> XArray entry. The other open can read or write the backing page after
> migrate_vma_finalize() frees it. Aveline reported a KASAN use-after-free
> and stale data through the second open.
>
> Handle migration invalidation for every mirror under its mutex. This
> clears pointers before faultback can free the backing pages and waits
> for an active mirror read or write. During system-to-device migration,
> the migrating open installs new entries after migration; other opens
> refault on their next access. Remove the now redundant XArray erase
> from the faultback copy loop.
>
> Extend the PMD migration selftest to read its last device-private
> subpage through a second open, then fault it back to RAM, change it,
> and read the new value through that open. The trigger is available to
> a process permitted to open /dev/hmm_dmirror* when CONFIG_TEST_HMM is
> enabled; it does not depend on physical GPU hardware.
>
> [akpm@linux-foundation.org: fix device-private backing subpage selection]
> Link: https://lore.kernel.org/r/20261122015246.d8b6b98ff5a8157168156339@linux-foundation.org
> Link: https://lore.kernel.org/r/179535872126.34.4789080961796247940.b4-reply@b4
> Fixes: b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM")
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> Reported-by: Aveline Noir <jm5905938@gmail.com>
> Cc: Ralph Campbell <rcampbell@nvidia.com>
> Cc: Jason Gunthorpe <jgg@ziepe.ca>
> Cc: Leon Romanovsky <leon@kernel.org>
> Cc: Mika Penttil?? <mpenttil@redhat.com>
> Cc: Shuah Khan <shuah@kernel.org>
> Cc: David Hildenbrand <david@kernel.org>
> Cc: Liam R. Howlett <liam@infradead.org>
> Cc: Vlastimil Babka <vbabka@kernel.org>
> Cc: Mike Rapoport <rppt@kernel.org>
> Cc: Michal Hocko <mhocko@suse.com>
> Cc: <stable@vger.kernel.org>
>
> lib/test_hmm.c | 20 +++++++-----------
> tools/testing/selftests/mm/hmm-tests.c | 25 +++++++++++++++++++++++
> 2 files changed, 33 insertions(+), 12 deletions(-)
>
> --- a/lib/test_hmm.c~lib-test_hmm-invalidate-all-mirrors-on-device-private-migration
> +++ a/lib/test_hmm.c
> @@ -292,6 +292,12 @@ static int dmirror_do_fault(struct dmirr
> page = hmm_pfn_to_page(*pfns);
> WARN_ON(!page);
>
> + if (is_device_private_page(page)) {
> + struct folio *folio = page_folio(page);
> + struct page *backing = BACKING_PAGE(&folio->page);
> +
> + page = backing + folio_page_idx(folio, page);
> + }
> entry = page;
> if (*pfns & HMM_PFN_WRITE)
> entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE);
> @@ -327,14 +333,6 @@ static bool dmirror_interval_invalidate(
> {
> struct dmirror *dmirror = container_of(mni, struct dmirror, notifier);
>
> - /*
> - * Ignore invalidation callbacks for device private pages since
> - * the invalidation is handled as part of the migration process.
> - */
> - if (range->event == MMU_NOTIFY_MIGRATE &&
> - range->owner == dmirror->mdevice)
> - return true;
> -
> if (mmu_notifier_range_blockable(range))
> mutex_lock(&dmirror->mutex);
> else if (!mutex_trylock(&dmirror->mutex))
> @@ -1198,7 +1196,6 @@ static vm_fault_t dmirror_devmem_fault_a
>
> src_page = pfn_to_page(page_to_pfn(spage) + i);
>
> - xa_erase(&dmirror->pt, addr >> PAGE_SHIFT);
> addr += PAGE_SIZE;
> copy_highpage(dst_page, src_page);
> }
> @@ -1822,9 +1819,8 @@ static vm_fault_t dmirror_devmem_fault(s
> }
> migrate_vma_pages(&args);
> /*
> - * No device finalize step is needed since
> - * dmirror_devmem_fault_alloc_and_copy() will have already
> - * invalidated the device page table.
> + * The migration notifier has invalidated all mirror page tables
> + * before the backing pages can be freed.
> */
> migrate_vma_finalize(&args);
> err:
> --- a/tools/testing/selftests/mm/hmm-tests.c~lib-test_hmm-invalidate-all-mirrors-on-device-private-migration
> +++ a/tools/testing/selftests/mm/hmm-tests.c
> @@ -2087,6 +2087,7 @@ TEST_F(hmm, migrate_anon_huge_fault)
> void *old_ptr;
> void *map;
> int pagemap_fd;
> + int fd2 = -1;
> int *ptr;
> int ret;
>
> @@ -2143,12 +2144,36 @@ TEST_F(hmm, migrate_anon_huge_fault)
> }
>
> close(pagemap_fd);
> +
> + /* Read a device-private subpage through another open. */
> + fd2 = hmm_open(variant->device_number);
> + ASSERT_GE(fd2, 0);
> + buffer->ptr = (char *)map + (npages - 1) * self->page_size;
> + ret = hmm_dmirror_cmd(fd2, HMM_DMIRROR_READ, buffer, 1);
> + buffer->ptr = map;
> + ASSERT_EQ(ret, 0);
> + ASSERT_EQ(buffer->cpages, 1);
> + ASSERT_EQ(*(int *)buffer->mirror,
> + (npages - 1) * self->page_size / sizeof(int));
> }
>
> /* 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);
>
> + if (fd2 >= 0) {
> + /* This open must not retain the freed device backing page. */
> + ptr[(npages - 1) * self->page_size / sizeof(*ptr)] = -1;
> + buffer->ptr = (char *)map + (npages - 1) * self->page_size;
> + ret = hmm_dmirror_cmd(fd2, HMM_DMIRROR_READ, buffer, 1);
> + buffer->ptr = map;
> + ASSERT_EQ(ret, 0);
> + ASSERT_EQ(buffer->cpages, 1);
> + ASSERT_EQ(buffer->faults, 1);
> + ASSERT_EQ(*(int *)buffer->mirror, -1);
> + ASSERT_EQ(close(fd2), 0);
> + }
> +
> buffer->ptr = old_ptr;
> hmm_buffer_free(buffer);
> }
> _
>
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration
2026-11-25 14:15 ` Alexandru Elisei
@ 2026-11-25 14:25 ` Aveline Noir
2026-11-25 15:06 ` Alexandru Elisei
2026-11-25 15:06 ` Mika Penttilä
1 sibling, 1 reply; 10+ messages in thread
From: Aveline Noir @ 2026-11-25 14:25 UTC (permalink / raw)
To: Alexandru Elisei
Cc: Andrew Morton, Mika Penttilä, linux-mm, Ralph Campbell,
Jason Gunthorpe, Leon Romanovsky, Shuah Khan, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
stable
Hi Alex
On Thu, Nov 26, 2026 at 1:15 AM Alexandru Elisei
<alexandru.elisei@arm.com> wrote:
> Might be something on my side? I'll investigate further, but I'm not very
> familiar with the code.
I reproduced the same 0 versus 523264 assertion on x86-64 when the separate
mm/hmm: account for subpage offsets in device-private PMDs fix was absent.
With that fix, the test passed. May you check whether it is in your test tree?
Thanks,
Aveline Noir
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration
2026-11-25 14:25 ` Aveline Noir
@ 2026-11-25 15:06 ` Alexandru Elisei
2026-11-25 16:40 ` Mika Penttilä
0 siblings, 1 reply; 10+ messages in thread
From: Alexandru Elisei @ 2026-11-25 15:06 UTC (permalink / raw)
To: Aveline Noir
Cc: Andrew Morton, Mika Penttilä, linux-mm, Ralph Campbell,
Jason Gunthorpe, Leon Romanovsky, Shuah Khan, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
stable
Hi,
On Thu, Nov 26, 2026 at 01:25:41AM +1100, Aveline Noir wrote:
> Hi Alex
>
> On Thu, Nov 26, 2026 at 1:15 AM Alexandru Elisei
> <alexandru.elisei@arm.com> wrote:
>
> > Might be something on my side? I'll investigate further, but I'm not very
> > familiar with the code.
>
> I reproduced the same 0 versus 523264 assertion on x86-64 when the separate
> mm/hmm: account for subpage offsets in device-private PMDs fix was absent.
> With that fix, the test passed. May you check whether it is in your test tree?
Indeed, I didn't have the patch in my tree, now the test passes!
Tested on a Radxa Orion O6 board (arm64 architecture):
Tested-by: Alexandru Elisei <alexandru.elisei@arm.com>
Thanks,
Alex
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration
2026-11-25 15:06 ` Alexandru Elisei
@ 2026-11-25 16:40 ` Mika Penttilä
2026-11-26 1:58 ` Aveline Noir
2026-11-26 12:23 ` Alexandru Elisei
0 siblings, 2 replies; 10+ messages in thread
From: Mika Penttilä @ 2026-11-25 16:40 UTC (permalink / raw)
To: Alexandru Elisei, Aveline Noir
Cc: Andrew Morton, linux-mm, Ralph Campbell, Jason Gunthorpe,
Leon Romanovsky, Shuah Khan, David Hildenbrand, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Michal Hocko, stable
Hi,
On 11/25/26 17:06, Alexandru Elisei wrote:
> Hi,
>
> On Thu, Nov 26, 2026 at 01:25:41AM +1100, Aveline Noir wrote:
>> Hi Alex
>>
>> On Thu, Nov 26, 2026 at 1:15 AM Alexandru Elisei
>> <alexandru.elisei@arm.com> wrote:
>>
>>> Might be something on my side? I'll investigate further, but I'm not very
>>> familiar with the code.
>>
>> I reproduced the same 0 versus 523264 assertion on x86-64 when the separate
>> mm/hmm: account for subpage offsets in device-private PMDs fix was absent.
>> With that fix, the test passed. May you check whether it is in your test tree?
>
> Indeed, I didn't have the patch in my tree, now the test passes!
>
> Tested on a Radxa Orion O6 board (arm64 architecture):
>
> Tested-by: Alexandru Elisei <alexandru.elisei@arm.com>
>
> Thanks,
> Alex
>
Did you run the full selftest?
Without skipping own invalidations the normal migrate to device test fails
( With no skip, migrate_vma_setup()'s invalidation erases the caller's own entries) :
# RUN hmm.hmm_device_private.migrate_fault ...
# hmm-tests.c:1023:migrate_fault:Expected ret (-2) == 0 (0)
# migrate_fault: Test terminated by assertion
# FAIL hmm.hmm_device_private.migrate_fault
not ok 14 hmm.hmm_device_private.migrate_fault
As told I will be sending a proper fix for the two fds invalidaton case as a follow up to my other changes to test.hmm.c
--Mika
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration
2026-11-25 16:40 ` Mika Penttilä
@ 2026-11-26 1:58 ` Aveline Noir
2026-11-26 12:23 ` Alexandru Elisei
1 sibling, 0 replies; 10+ messages in thread
From: Aveline Noir @ 2026-11-26 1:58 UTC (permalink / raw)
To: Mika Penttilä, Alexandru Elisei
Cc: Andrew Morton, linux-mm, Ralph Campbell, Jason Gunthorpe,
Leon Romanovsky, Shuah Khan, David Hildenbrand, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Michal Hocko, stable
Hi Mika,
> As told I will be sending a proper fix for the two fds invalidation case
> as a follow up to my other changes to test.hmm.c
I only ran the selftest that Alexandru mentioned.
Thanks for the clarification! Looking forward to your patch. :)
On Wed, 25 Nov 2026 18:40:12 +0200, Mika Penttilä <mpenttil@redhat.com> wrote:
> Hi,
>
> On 11/25/26 17:06, Alexandru Elisei wrote:
> > Hi,
> >
> > On Thu, Nov 26, 2026 at 01:25:41AM +1100, Aveline Noir wrote:
> >> Hi Alex
> >>
> >> On Thu, Nov 26, 2026 at 1:15 AM Alexandru Elisei
> >> <alexandru.elisei@arm.com> wrote:
> >>
> >>> Might be something on my side? I'll investigate further, but I'm not very
> >>> familiar with the code.
> >>
> >> I reproduced the same 0 versus 523264 assertion on x86-64 when the separate
> >> mm/hmm: account for subpage offsets in device-private PMDs fix was absent.
> >> With that fix, the test passed. May you check whether it is in your test tree?
> >
> > Indeed, I didn't have the patch in my tree, now the test passes!
> >
> > Tested on a Radxa Orion O6 board (arm64 architecture):
> >
> > Tested-by: Alexandru Elisei <alexandru.elisei@arm.com>
> >
> > Thanks,
> > Alex
> >
> Did you run the full selftest?
>
> Without skipping own invalidations the normal migrate to device test fails
> ( With no skip, migrate_vma_setup()'s invalidation erases the caller's own entries) :
>
> # RUN hmm.hmm_device_private.migrate_fault ...
> # hmm-tests.c:1023:migrate_fault:Expected ret (-2) == 0 (0)
> # migrate_fault: Test terminated by assertion
> # FAIL hmm.hmm_device_private.migrate_fault
> not ok 14 hmm.hmm_device_private.migrate_fault
>
>
> As told I will be sending a proper fix for the two fds invalidaton case as a follow up to my other changes to test.hmm.c
>
> --Mika
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration
2026-11-25 16:40 ` Mika Penttilä
2026-11-26 1:58 ` Aveline Noir
@ 2026-11-26 12:23 ` Alexandru Elisei
1 sibling, 0 replies; 10+ messages in thread
From: Alexandru Elisei @ 2026-11-26 12:23 UTC (permalink / raw)
To: Mika Penttilä
Cc: Aveline Noir, Andrew Morton, linux-mm, Ralph Campbell,
Jason Gunthorpe, Leon Romanovsky, Shuah Khan, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
stable
Hi Mika,
On Wed, Nov 25, 2026 at 06:40:12PM +0200, Mika Penttilä wrote:
> Hi,
>
> On 11/25/26 17:06, Alexandru Elisei wrote:
> > Hi,
> >
> > On Thu, Nov 26, 2026 at 01:25:41AM +1100, Aveline Noir wrote:
> >> Hi Alex
> >>
> >> On Thu, Nov 26, 2026 at 1:15 AM Alexandru Elisei
> >> <alexandru.elisei@arm.com> wrote:
> >>
> >>> Might be something on my side? I'll investigate further, but I'm not very
> >>> familiar with the code.
> >>
> >> I reproduced the same 0 versus 523264 assertion on x86-64 when the separate
> >> mm/hmm: account for subpage offsets in device-private PMDs fix was absent.
> >> With that fix, the test passed. May you check whether it is in your test tree?
> >
> > Indeed, I didn't have the patch in my tree, now the test passes!
> >
> > Tested on a Radxa Orion O6 board (arm64 architecture):
> >
> > Tested-by: Alexandru Elisei <alexandru.elisei@arm.com>
> >
> > Thanks,
> > Alex
> >
> Did you run the full selftest?
I dropped the ball here, the output of the full selftest is a couple of screens,
so I just focused on running this one test while I was trying to get it to work.
>
> Without skipping own invalidations the normal migrate to device test fails
> ( With no skip, migrate_vma_setup()'s invalidation erases the caller's own entries) :
>
> # RUN hmm.hmm_device_private.migrate_fault ...
> # hmm-tests.c:1023:migrate_fault:Expected ret (-2) == 0 (0)
> # migrate_fault: Test terminated by assertion
> # FAIL hmm.hmm_device_private.migrate_fault
> not ok 14 hmm.hmm_device_private.migrate_fault
I can reproduce that:
$ sudo tools/testing/selftests/mm/hmm-tests -r hmm.hmm_device_private.migrate_fault
# [INFO] detected hugetlb page size: 2048 KiB
# [INFO] detected hugetlb page size: 32768 KiB
# [INFO] detected hugetlb page size: 64 KiB
# [INFO] detected hugetlb page size: 1048576 KiB
TAP version 13
1..1
# Starting 1 tests from 1 test cases.
# RUN hmm.hmm_device_private.migrate_fault ...
# hmm-tests.c:1023:migrate_fault:Expected ret (-2) == 0 (0)
# migrate_fault: Test terminated by assertion
# FAIL hmm.hmm_device_private.migrate_fault
not ok 1 hmm.hmm_device_private.migrate_fault
# FAILED: 0 / 1 tests passed.
# Totals: pass:0 fail:1 xfail:0 xpass:0 skip:0 error:0
I ran all the device private tests this time. I don't have a device with
coherent memory on my machine:
[..]
# FAILED: 77 / 78 tests passed.
# 40 skipped test(s) detected. Consider enabling relevant config options to improve coverage.
# Totals: pass:37 fail:1 xfail:0 xpass:0 skip:40 error:0
Thanks,
Alex
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration
2026-11-25 14:15 ` Alexandru Elisei
2026-11-25 14:25 ` Aveline Noir
@ 2026-11-25 15:06 ` Mika Penttilä
1 sibling, 0 replies; 10+ messages in thread
From: Mika Penttilä @ 2026-11-25 15:06 UTC (permalink / raw)
To: Alexandru Elisei, Andrew Morton
Cc: linux-mm, Aveline Noir, Ralph Campbell, Jason Gunthorpe,
Leon Romanovsky, Shuah Khan, David Hildenbrand, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Michal Hocko, stable
Hi,
On 11/25/26 16:15, Alexandru Elisei wrote:
> Hi,
>
> I tried to test this patch, applied on top of v7.4-rc4.
FYI, I am doing changes to test_hmm.c invalidation handling and will fix the two fd invalidation as a follow up also.
>
> With the changes to the migrate_anon_huge_fault test, but with no changes to
> lib/test_hmm.c, to see if the test fails without the fix to the kernel, I get a
> kernel fault:
>
> [ 95.571212] Unable to handle kernel paging request at virtual address ffff7fffdfe00000
> [ 95.571221] Mem abort info:
> [ 95.571223] ESR = 0x0000000096000004
> [ 95.571225] EC = 0x25: DABT (current EL), IL = 32 bits
> [ 95.571228] SET = 0, FnV = 0
> [ 95.571230] EA = 0, S1PTW = 0
> [ 95.571231] FSC = 0x04: level 0 translation fault
> [ 95.571234] Data abort info:
> [ 95.571235] ISV = 0, ISS = 0x00000004, ISS2 = 0x00000000
> [ 95.571237] CM = 0, WnR = 0, TnD = 0, TagAccess = 0
> [ 95.571239] GCS = 0, Overlay = 0, DirtyBit = 0
> [ 95.571242] swapper pgtable: 4k pages, 48-bit VAs, pgdp=00000001cc71d000
> [ 95.571245] [ffff7fffdfe00000] pgd=0000000000000000, p4d=0000000000000000
> [..]
> [ 95.692002] pc : __pi_memcpy_generic+0x110/0x230
> [ 95.696623] lr : dmirror_do_read+0x7c/0xf0
> [..]
>
> decode_stacktrace.sh says that happens in lib/test_hmm.c:503.
>
> This looks more serious that the use-after-free that KASAN reported.
>
> With the changes to the test and to the kernel, I get this error when running
> the test:
>
> $ sudo tools/testing/selftests/mm/hmm-tests -r hmm.hmm_device_private.migrate_anon_huge_fault
> # [INFO] detected hugetlb page size: 2048 KiB
> # [INFO] detected hugetlb page size: 32768 KiB
> # [INFO] detected hugetlb page size: 64 KiB
> # [INFO] detected hugetlb page size: 1048576 KiB
> TAP version 13
> 1..1
> # Starting 1 tests from 1 test cases.
> # RUN hmm.hmm_device_private.migrate_anon_huge_fault ...
> # hmm-tests.c:2156:migrate_anon_huge_fault:Expected *(int *)buffer->mirror (0) == (npages - 1) * self->page_size / sizeof(int) (523264)
> # migrate_anon_huge_fault: Test terminated by assertion
> # FAIL hmm.hmm_device_private.migrate_anon_huge_fault
> not ok 1 hmm.hmm_device_private.migrate_anon_huge_fault
> # FAILED: 0 / 1 tests passed.
> # Totals: pass:0 fail:1 xfail:0 xpass:0 skip:0 error:0
>
> Might be something on my side? I'll investigate further, but I'm not very
> familiar with the code.
>
> Thanks,
> Alex
--Mika
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration
2026-11-25 6:53 [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration Andrew Morton
2026-11-25 14:15 ` Alexandru Elisei
@ 2026-11-25 15:35 ` Aveline Noir
2026-11-26 6:53 ` sashiko-bot
2 siblings, 0 replies; 10+ messages in thread
From: Aveline Noir @ 2026-11-25 15:35 UTC (permalink / raw)
To: akpm
Cc: mpenttil, linux-mm, rcampbell, jgg, leon, shuah, david, liam,
vbabka, rppt, mhocko, stable
Reviewed-by: Aveline Noir <jm5905938@gmail.com>
Tested-by: Aveline Noir <jm5905938@gmail.com>
On Tue, 24 Nov 2026 22:53:38 -0800, Andrew Morton
<akpm@linux-foundation.org> wrote:
> From: Andrew Morton <akpm@linux-foundation.org>
> Subject: lib/test_hmm: invalidate all mirrors on device-private migration
> Date: Sun, 22 Nov 2026 08:53:12 -0800
>
> When one open of the fake HMM device migrates a folio to device-private
> memory, a second open can fault the same address into its mirror page
> table. The second mirror then stores the inaccessible device page, which
> can oops the kernel when dmirror_do_read() copies from it. Store the
> corresponding RAM backing subpage instead.
>
> That conversion also makes the backing page's lifetime significant to
> the second mirror. migrate_vma_setup() notifies all mirrors before CPU
> faultback, but dmirror_interval_invalidate() skips the notification for
> every open on the same device. The faulting open clears only its own
> XArray entry. The other open can read or write the backing page after
> migrate_vma_finalize() frees it. Aveline reported a KASAN use-after-free
> and stale data through the second open.
>
> Handle migration invalidation for every mirror under its mutex. This
> clears pointers before faultback can free the backing pages and waits
> for an active mirror read or write. During system-to-device migration,
> the migrating open installs new entries after migration; other opens
> refault on their next access. Remove the now redundant XArray erase
> from the faultback copy loop.
>
> Extend the PMD migration selftest to read its last device-private
> subpage through a second open, then fault it back to RAM, change it,
> and read the new value through that open. The trigger is available to
> a process permitted to open /dev/hmm_dmirror* when CONFIG_TEST_HMM is
> enabled; it does not depend on physical GPU hardware.
>
> [akpm@linux-foundation.org: fix device-private backing subpage selection]
> Link: https://lore.kernel.org/r/20261122015246.d8b6b98ff5a8157168156339@linux-foundation.org
> Link: https://lore.kernel.org/r/179535872126.34.4789080961796247940.b4-reply@b4
> Fixes: b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM")
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> Reported-by: Aveline Noir <jm5905938@gmail.com>
> Cc: Ralph Campbell <rcampbell@nvidia.com>
> Cc: Jason Gunthorpe <jgg@ziepe.ca>
> Cc: Leon Romanovsky <leon@kernel.org>
> Cc: Mika Penttilä <mpenttil@redhat.com>
> Cc: Shuah Khan <shuah@kernel.org>
> Cc: David Hildenbrand <david@kernel.org>
> Cc: Liam R. Howlett <liam@infradead.org>
> Cc: Vlastimil Babka <vbabka@kernel.org>
> Cc: Mike Rapoport <rppt@kernel.org>
> Cc: Michal Hocko <mhocko@suse.com>
> Cc: <stable@vger.kernel.org>
>
> lib/test_hmm.c | 20 +++++++-----------
> tools/testing/selftests/mm/hmm-tests.c | 25 +++++++++++++++++++++++
> 2 files changed, 33 insertions(+), 12 deletions(-)
>
> --- a/lib/test_hmm.c~lib-test_hmm-invalidate-all-mirrors-on-device-private-migration
> +++ a/lib/test_hmm.c
> @@ -292,6 +292,12 @@ static int dmirror_do_fault(struct dmirr
> page = hmm_pfn_to_page(*pfns);
> WARN_ON(!page);
>
> + if (is_device_private_page(page)) {
> + struct folio *folio = page_folio(page);
> + struct page *backing = BACKING_PAGE(&folio->page);
> +
> + page = backing + folio_page_idx(folio, page);
> + }
> entry = page;
> if (*pfns & HMM_PFN_WRITE)
> entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE);
> @@ -327,14 +333,6 @@ static bool dmirror_interval_invalidate(
> {
> struct dmirror *dmirror = container_of(mni, struct dmirror, notifier);
>
> - /*
> - * Ignore invalidation callbacks for device private pages since
> - * the invalidation is handled as part of the migration process.
> - */
> - if (range->event == MMU_NOTIFY_MIGRATE &&
> - range->owner == dmirror->mdevice)
> - return true;
> -
> if (mmu_notifier_range_blockable(range))
> mutex_lock(&dmirror->mutex);
> else if (!mutex_trylock(&dmirror->mutex))
> @@ -1198,7 +1196,6 @@ static vm_fault_t dmirror_devmem_fault_a
>
> src_page = pfn_to_page(page_to_pfn(spage) + i);
>
> - xa_erase(&dmirror->pt, addr >> PAGE_SHIFT);
> addr += PAGE_SIZE;
> copy_highpage(dst_page, src_page);
> }
> @@ -1822,9 +1819,8 @@ static vm_fault_t dmirror_devmem_fault(s
> }
> migrate_vma_pages(&args);
> /*
> - * No device finalize step is needed since
> - * dmirror_devmem_fault_alloc_and_copy() will have already
> - * invalidated the device page table.
> + * The migration notifier has invalidated all mirror page tables
> + * before the backing pages can be freed.
> */
> migrate_vma_finalize(&args);
> err:
> --- a/tools/testing/selftests/mm/hmm-tests.c~lib-test_hmm-invalidate-all-mirrors-on-device-private-migration
> +++ a/tools/testing/selftests/mm/hmm-tests.c
> @@ -2087,6 +2087,7 @@ TEST_F(hmm, migrate_anon_huge_fault)
> void *old_ptr;
> void *map;
> int pagemap_fd;
> + int fd2 = -1;
> int *ptr;
> int ret;
>
> @@ -2143,12 +2144,36 @@ TEST_F(hmm, migrate_anon_huge_fault)
> }
>
> close(pagemap_fd);
> +
> + /* Read a device-private subpage through another open. */
> + fd2 = hmm_open(variant->device_number);
> + ASSERT_GE(fd2, 0);
> + buffer->ptr = (char *)map + (npages - 1) * self->page_size;
> + ret = hmm_dmirror_cmd(fd2, HMM_DMIRROR_READ, buffer, 1);
> + buffer->ptr = map;
> + ASSERT_EQ(ret, 0);
> + ASSERT_EQ(buffer->cpages, 1);
> + ASSERT_EQ(*(int *)buffer->mirror,
> + (npages - 1) * self->page_size / sizeof(int));
> }
>
> /* 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);
>
> + if (fd2 >= 0) {
> + /* This open must not retain the freed device backing page. */
> + ptr[(npages - 1) * self->page_size / sizeof(*ptr)] = -1;
> + buffer->ptr = (char *)map + (npages - 1) * self->page_size;
> + ret = hmm_dmirror_cmd(fd2, HMM_DMIRROR_READ, buffer, 1);
> + buffer->ptr = map;
> + ASSERT_EQ(ret, 0);
> + ASSERT_EQ(buffer->cpages, 1);
> + ASSERT_EQ(buffer->faults, 1);
> + ASSERT_EQ(*(int *)buffer->mirror, -1);
> + ASSERT_EQ(close(fd2), 0);
> + }
> +
> buffer->ptr = old_ptr;
> hmm_buffer_free(buffer);
> }
> _
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration
2026-11-25 6:53 [PATCH] lib/test_hmm: invalidate all mirrors on device-private migration Andrew Morton
2026-11-25 14:15 ` Alexandru Elisei
2026-11-25 15:35 ` Aveline Noir
@ 2026-11-26 6:53 ` sashiko-bot
2 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-11-26 6:53 UTC (permalink / raw)
To: Andrew Morton, Mika Penttilä
Cc: linux-mm, Aveline Noir, Ralph Campbell, Jason Gunthorpe,
Leon Romanovsky, Shuah Khan, David Hildenbrand, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Michal Hocko
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] lib/test_hmm: dmirror_migrate_to_device() fails on resident
device pages
- [Medium] lib/test_hmm: dmirror_do_fault() maps subpage 0 on PMD
device-private fault
--
commit fd992ab990d0dc726b0adb39191efdbd1d7b171d
Author: Andrew Morton <akpm@linux-foundation.org>
lib/test_hmm: invalidate all mirrors on device-private migration
This commit stores the RAM backing subpage instead of the device-private
page in mirror page tables on fault, and removes the same-owner
MMU_NOTIFY_MIGRATE bypass in dmirror_interval_invalidate() so all
mirrors are invalidated on migration.
Link: https://lore.kernel.org/r/20261122015246.d8b6b98ff5a8157168156339@linux-foundation.org
Link: https://lore.kernel.org/r/179535872126.34.4789080961796247940.b4-reply@b4
> diff --git a/lib/test_hmm.c b/lib/test_hmm.c
> index a09a766b15b05..ad68e2636f313 100644
> --- a/lib/test_hmm.c
> +++ b/lib/test_hmm.c
> @@ -290,6 +290,12 @@ static int dmirror_do_fault(struct dmirror *dmirror, struct hmm_range *range)
> page = hmm_pfn_to_page(*pfns);
> WARN_ON(!page);
>
> + if (is_device_private_page(page)) {
> + struct folio *folio = page_folio(page);
> + struct page *backing = BACKING_PAGE(&folio->page);
> +
> + page = backing + folio_page_idx(folio, page);
> + }
> entry = page;
> if (*pfns & HMM_PFN_WRITE)
> entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE);
[Severity: Medium]
Does folio_page_idx(folio, page) always evaluate to 0 when faulting a
non-PMD-aligned subpage of a PMD-mapped device-private folio?
When hmm_range_fault() walks a PMD device-private entry starting at a
non-zero offset within the PMD, hmm_vma_handle_absent_pmd() initializes
pfn to the folio's head PFN without adding the within-PMD page offset
((addr & ~PMD_MASK) >> PAGE_SHIFT):
mm/hmm.c:hmm_vma_handle_absent_pmd() {
...
if (softleaf_is_device_private(entry) &&
softleaf_to_folio(entry)->pgmap->owner ==
range->dev_private_owner) {
unsigned long cpu_flags = HMM_PFN_VALID |
hmm_pfn_flags_order(PMD_SHIFT - PAGE_SHIFT);
unsigned long pfn = softleaf_to_pfn(entry);
unsigned long i;
...
for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS;
hmm_pfns[i] |= pfn | cpu_flags;
}
...
}
As a result, hmm_pfn_to_page(*pfns) is &folio->page rather than the
requested tail subpage, so dmirror_do_fault() maps backing subpage 0 into
dmirror->pt. The new subpage read check in TEST_F(hmm,
migrate_anon_huge_fault) reads subpage 0 instead of subpage npages - 1
and fails its assertion.
> @@ -325,14 +331,6 @@ static bool dmirror_interval_invalidate(struct mmu_interval_notifier *mni,
> {
> struct dmirror *dmirror = container_of(mni, struct dmirror, notifier);
>
> - /*
> - * Ignore invalidation callbacks for device private pages since
> - * the invalidation is handled as part of the migration process.
> - */
> - if (range->event == MMU_NOTIFY_MIGRATE &&
> - range->owner == dmirror->mdevice)
> - return true;
> -
> if (mmu_notifier_range_blockable(range))
> mutex_lock(&dmirror->mutex);
> else if (!mutex_trylock(&dmirror->mutex))
[Severity: Medium]
Does removing this check cause a regression in dmirror_migrate_to_device()
when part of the requested range is already resident in device memory on
the same mirror?
During dmirror_migrate_to_device(), migrate_vma_setup() runs with
MIGRATE_VMA_SELECT_SYSTEM and fires MMU_NOTIFY_MIGRATE across
[args.start, args.end). Without the same-owner check,
dmirror_interval_invalidate() calls dmirror_do_update() on the migrating
dmirror instance and erases all of its dmirror->pt entries in that range,
including pages already residing on the device (plus the entry at
end >> PAGE_SHIFT because xa_for_each_range() takes an inclusive upper
bound). Because those already-resident device pages do not have
MIGRATE_PFN_MIGRATE set, dmirror_migrate_finalize_and_map() skips them
and leaves them unmapped in dmirror->pt, causing the verification
dmirror_do_read() at the end of dmirror_migrate_to_device() to fail with
-ENOENT (which breaks TEST_F(hmm, migrate_fault) in hmm-tests).
--
Sashiko AI review · https://sashiko.dev/#/patchset/bae41f6fc4114357a8a74c8b958f2bdc.1@linux-foundation.org?part=1
^ permalink raw reply [flat|nested] 10+ messages in thread