mm/hmm: account for subpage offsets in device-private PMDs
Subsystems (6)
PATCH 1/1 mm/hmm: account for subpage offsets in device-private PMDs
When a query starts inside a device-private PMD owned by range->dev_private_owner, hmm_vma_handle_absent_pmd() still fills the PFN array from the folio's first page. The returned PFNs therefore refer to earlier pages rather than the requested subrange.
Add the query's PMD-relative page offset to the initial PFN, as the present PMD path already does.
---
Found during a synthetic test_hmm check of device-private THP handling. Tested PMD-aligned and non-aligned subranges, PMD boundaries, split PTEs and fake-device data accesses with test_hmm in QEMU TCG; all executed tests passed with the fix. No real GPU testing was performed.
--- mm/hmm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/hmm.c +2 -1
@@ -355,7 +355,8 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, 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 pfn =softleaf_to_pfn (entry) + + ((start & ~PMD_MASK ) >>PAGE_SHIFT );unsigned long i;if (softleaf_is_device_private_write (entry))
base-commit: d89e1d186ec9271bf78e62771779e9fc03bf0fc3
Signature · 1 line
2.56.0
Andrew Morton
maintainerQuoted 8 lines · On Sat, 31 Oct 2026 15:07:58 +1100 Aveline Noir <[email protected]> wrote:
When a query starts inside a device-private PMD owned by
range->dev_private_owner,hmm_vma_handle_absent_pmd()still fills the PFN array from the folio's first page. The returned PFNs therefore refer to earlier pages rather than the requested subrange.Add the query's PMD-relative page offset to the initial PFN, as the present PMD path already does.
Quoted 8 lines
Fixes 10b9feee2d0d ("mm/hmm: populate PFNs from PMD swap entry")Signed-off-by Aveline Noir <[email protected]>---
Found during a synthetictest_hmmcheck of device-private THP handling. Tested PMD-aligned and non-aligned subranges, PMD boundaries, split PTEs and fake-device data accesses withtest_hmmin QEMU TCG; all executed tests passed with the fix. No real GPU testing was performed.
Thanks. It appears that the changelog somewhat understates the seriousness of this so I came up with a more assertive one, below. Please check that.
I fed this through LLM-driven QEMU-based runtime testing and it looks like it pretty much replicated your testing:
: I tested "mm/hmm: account for subpage offsets in device-private : PMDs" on matched x86-64 guest kernels. An unprivileged process : (UID 65534) migrated an anonymous 2 MiB THP to the test_hmm : device. A second open of the device read pages inside the PMD : while the first remained open. A snapshot confirmed a : device-private PMD, and temporary kernel log messages confirmed : that the reads reached hmm_vma_handle_absent_pmd().
:
: Without the patch, requests for pages 1, 7, 255 and 511 returned : page 0. A request for pages 31-33 returned pages 0-2. With the : patch, all seven pages returned the requested contents. Page 0 : read correctly on both kernels. Thus an unprivileged device user : can receive data from the wrong pages.
:
: The fake device needed an identical temporary backing-page : correction on both kernels to make this read path work. That : change was removed after testing. This exercises the HMM path : through test_hmm; I did not test GPU hardware.
So it accidentally found a bug in lib/test_hmm.c on the way. "The QEMU run exposed a real test_hmm bug: a second open of /dev/hmm_dmirror0 can put a device-private page into its page table, then dmirror_do_read() tries to copy from that non-CPU-accessible page and oopses". I'll prep a fix for that separately.
I'll queue the below in mm.git's mm-hotfixes queue and shall await maintainer input (please).
From: Aveline Noir <[email protected]>
Subject: mm/hmm: account for subpage offsets in device-private PMDs
Date: Sat, 31 Oct 2026 15:07:58 +1100
When a query starts inside a device-private PMD owned by range->dev_private_owner, hmm_vma_handle_absent_pmd() still fills the PFN array from the folio's first page. The returned PFNs therefore refer to earlier pages rather than the requested subrange.
Nouveau's GPU fault path can query one page of a device-private PMD at a time. Xe's SVM fault, prefetch and userptr mapping paths can also query subranges of their own device-private PMDs. An unprivileged process with access to the GPU render node can reach these paths on hardware supporting device-private THP migration. Xe can read the wrong application data or silently overwrite earlier pages of the same process's device-private folio.
Nouveau aligns a one-page GPU fault to the PMD head by subtracting the fault offset from the returned page. The faulty HMM PFN already refers to the head, so this subtraction can select a page before the folio. If the preceding page belongs to another allocation, GPU access can expose or overwrite unrelated userspace data; an invalid page can also fault the kernel during its address conversion. This worst case follows from the source, but has not been demonstrated on a real GPU.
Add the query's PMD-relative page offset to the initial PFN, as the present PMD path already does.
The device-private PMD migration machinery first shipped in 6.19. Backport this fix to the maintained 7.3.y stable series; the HMM path exists in 6.18.y but cannot be reached through an in-tree PMD-producing device-private migration path there.
mm/hmm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/hmm.c renamed from mm/hmm.c~mm-hmm-account-for-subpage-offsets-in-device-private-pmds +2 -1
@@ -355,7 +355,8 @@ static int hmm_vma_handle_absent_pmd(str 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 pfn =softleaf_to_pfn (entry) + + ((start & ~PMD_MASK ) >>PAGE_SHIFT );unsigned long i;if (softleaf_is_device_private_write (entry))
_
+ mm-hmm-account-for-subpage-offsets-in-device-private-pmds.patch added to mm-hotfixes-unstable branch
In reply to Aveline NoirThe patch titled Subject: mm/hmm: account for subpage offsets in device-private PMDs has been added to the -mm mm-hotfixes-unstable branch. Its filename is…
The patch titled
Subject: mm/hmm: account for subpage offsets in device-private PMDs
has been added to the -mm mm-hotfixes-unstable branch. Its filename is
mm-hmm-account-for-subpage-offsets-in-device-private-pmds.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hmm-account-for-subpage-offsets-in-device-private-pmds.patch
This patch will later appear in the mm-hotfixes-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Aveline Noir <[email protected]>
Subject: mm/hmm: account for subpage offsets in device-private PMDs
Date: Sat, 31 Oct 2026 15:07:58 +1100
When a query starts inside a device-private PMD owned by range->dev_private_owner, hmm_vma_handle_absent_pmd() still fills the PFN array from the folio's first page. The returned PFNs therefore refer to earlier pages rather than the requested subrange.
Nouveau's GPU fault path can query one page of a device-private PMD at a time. Xe's SVM fault, prefetch and userptr mapping paths can also query subranges of their own device-private PMDs. An unprivileged process with access to the GPU render node can reach these paths on hardware supporting device-private THP migration. Xe can read the wrong application data or silently overwrite earlier pages of the same process's device-private folio.
Nouveau aligns a one-page GPU fault to the PMD head by subtracting the fault offset from the returned page. The faulty HMM PFN already refers to the head, so this subtraction can select a page before the folio. If the preceding page belongs to another allocation, GPU access can expose or overwrite unrelated userspace data; an invalid page can also fault the kernel during its address conversion. This worst case follows from the source, but has not been demonstrated on a real GPU.
Add the query's PMD-relative page offset to the initial PFN, as the present PMD path already does.
The device-private PMD migration machinery first shipped in 6.19. Backport this fix to the maintained 7.3.y stable series; the HMM path exists in 6.18.y but cannot be reached through an in-tree PMD-producing device-private migration path there.
mm/hmm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/hmm.c renamed from mm/hmm.c~mm-hmm-account-for-subpage-offsets-in-device-private-pmds +2 -1
@@ -355,7 +355,8 @@ static int hmm_vma_handle_absent_pmd(str 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 pfn =softleaf_to_pfn (entry) + + ((start & ~PMD_MASK ) >>PAGE_SHIFT );unsigned long i;if (softleaf_is_device_private_write (entry))
_
Patches currently in -mm which might be from [email protected] are
mm-hmm-account-for-subpage-offsets-in-device-private-pmds.patch mm-damon-ops-common-align-mmu-notifier-range-when-clearing-pmd-young-bit.patch mm-damon-stat-avoid-32-bit-overflow-in-idle-percentiles.patch mm-damon-pvaddr-skip-folios-already-on-the-migration-target-node.patch docs-admin-guide-mm-damon-start-fix-grammar.patch docs-admin-guide-mm-damon-stat-fix-pronoun-agreement.patch docs-admin-guide-mm-damon-usage-fix-grammar.patch docs-admin-guide-mm-damon-lru_sort-fix-remaining-grammar.patch docs-mm-damon-design-fix-grammar-and-typos.patch docs-mm-damon-api-fix-verb-form.patch docs-mm-damon-maintainer-profile-fix-subject-verb-agreement.patch docs-mm-damon-monitoring_intervals_tuning_example-fix-grammar-and-typos.patch mm-damon-paddr-add-kunit-tests-for-addr_unit-conversion.patch mm-damon-paddr-add-kunit-tests-for-addr_unit-conversion-fix.patch
Mika Penttilä
Reviewed-byQuoted 44 lines · Andrew Morton
Quoted 8 lines · On Sat, 31 Oct 2026 15:07:58 +1100 Aveline Noir <[email protected]> wrote:
When a query starts inside a device-private PMD owned by
range->dev_private_owner,hmm_vma_handle_absent_pmd()still fills the PFN array from the folio's first page. The returned PFNs therefore refer to earlier pages rather than the requested subrange.Add the query's PMD-relative page offset to the initial PFN, as the present PMD path already does.
Quoted 8 lines
Fixes 10b9feee2d0d ("mm/hmm: populate PFNs from PMD swap entry")Signed-off-by Aveline Noir <[email protected]>---
Found during a synthetictest_hmmcheck of device-private THP handling. Tested PMD-aligned and non-aligned subranges, PMD boundaries, split PTEs and fake-device data accesses withtest_hmmin QEMU TCG; all executed tests passed with the fix. No real GPU testing was performed.Thanks. It appears that the changelog somewhat understates the seriousness of this so I came up with a more assertive one, below. Please check that.
I fed this through LLM-driven QEMU-based runtime testing and it looks like it pretty much replicated your testing:
: I tested "mm/hmm: account for subpage offsets in device-private : PMDs" on matched x86-64 guest kernels. An unprivileged process : (UID 65534) migrated an anonymous 2 MiB THP to the
test_hmm: device. A second open of the device read pages inside the PMD : while the first remained open. A snapshot confirmed a : device-private PMD, and temporary kernel log messages confirmed : that the reads reachedhmm_vma_handle_absent_pmd().
:
: Without the patch, requests for pages 1, 7, 255 and 511 returned : page 0. A request for pages 31-33 returned pages 0-2. With the : patch, all seven pages returned the requested contents. Page 0 : read correctly on both kernels. Thus an unprivileged device user : can receive data from the wrong pages.
:
: The fake device needed an identical temporary backing-page : correction on both kernels to make this read path work. That : change was removed after testing. This exercises the HMM path : throughtest_hmm; I did not test GPU hardware.
Quoted 5 lines
So it accidentally found a bug in
lib/test_hmm.con the way. "The QEMU run exposed a realtest_hmmbug: a second open of/dev/hmm_dmirror0can put a device-private page into its page table, thendmirror_do_read()tries to copy from that non-CPU-accessible page and oopses". I'll prep a fix for that separately.
I noticed that also and will make changes around it in my series.
Quoted 76 lines
I'll queue the below in mm.git's mm-hotfixes queue and shall await maintainer input (please).
From: Aveline Noir <[email protected]>
Subject: mm/hmm: account for subpage offsets in device-private PMDs
Date: Sat, 31 Oct 2026 15:07:58 +1100When a query starts inside a device-private PMD owned by
range->dev_private_owner,hmm_vma_handle_absent_pmd()still fills the PFN array from the folio's first page. The returned PFNs therefore refer to earlier pages rather than the requested subrange.Nouveau's GPU fault path can query one page of a device-private PMD at a time. Xe's SVM fault, prefetch and userptr mapping paths can also query subranges of their own device-private PMDs. An unprivileged process with access to the GPU render node can reach these paths on hardware supporting device-private THP migration. Xe can read the wrong application data or silently overwrite earlier pages of the same process's device-private folio.
Nouveau aligns a one-page GPU fault to the PMD head by subtracting the fault offset from the returned page. The faulty HMM PFN already refers to the head, so this subtraction can select a page before the folio. If the preceding page belongs to another allocation, GPU access can expose or overwrite unrelated userspace data; an invalid page can also fault the kernel during its address conversion. This worst case follows from the source, but has not been demonstrated on a real GPU.
Add the query's PMD-relative page offset to the initial PFN, as the present PMD path already does.
The device-private PMD migration machinery first shipped in 6.19. Backport this fix to the maintained 7.3.y stable series; the HMM path exists in 6.18.y but cannot be reached through an in-tree PMD-producing device-private migration path there.
Fixes 10b9feee2d0d ("mm/hmm: populate PFNs from PMD swap entry")Signed-off-by Aveline Noir <[email protected]>Signed-off-by Andrew Morton <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Leon Romanovsky <[email protected]>Cc Francois Dugast <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Matthew Brost <[email protected]>Cc Patchwork <[email protected]>Cc SJ Park <[email protected]>Cc Vlastimil Babka <[email protected]>Cc David Airlie <[email protected]>Cc Alistair Popple <[email protected]>Cc Balbir Singh <[email protected]>Cc Christian König <[email protected]>Cc Mika Penttilä <[email protected]>Cc Thomas Hellström <[email protected]>Cc Zi Yan <[email protected]>Cc <[email protected]>mm/hmm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)mm/hmm.c renamed from mm/hmm.c~mm-hmm-account-for-subpage-offsets-in-device-private-pmds +2
-1@@ -355,7 +355,8 @@ static int hmm_vma_handle_absent_pmd(str 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 pfn =softleaf_to_pfn (entry) + + ((start & ~PMD_MASK ) >>PAGE_SHIFT );unsigned long i;if (softleaf_is_device_private_write (entry))
Quoted 1 line
_
For this change:
Andrew Morton
maintainerQuoted 7 lines · On Sun, 22 Nov 2026 07:55:37 +0200 Mika Penttilä <[email protected]> wrote:
Quoted 5 lines
So it accidentally found a bug in
lib/test_hmm.con the way. "The QEMU run exposed a realtest_hmmbug: a second open of/dev/hmm_dmirror0can put a device-private page into its page table, thendmirror_do_read()tries to copy from that non-CPU-accessible page and oopses". I'll prep a fix for that separately.I noticed that also and will make changes around it in my series.
I came up with the below. It all tested out OK. wdyt? I guess it should have cc:stable>
From: Andrew Morton <[email protected]>
Subject: lib/test_hmm: use backing pages for device-private mirror faults
Date: Sat, 21 Nov 2026 17:06:45 -0800
When one hmm_dmirror descriptor migrates memory to device-private and a second descriptor reads it, dmirror_do_fault() stores the device page in the second descriptor's page table. dmirror_do_read() then passes that page to memcpy_from_page(), which can oops the kernel. This was reproduced with an unprivileged process using the fake device in a QEMU guest.
Store the corresponding RAM backing page instead. For a large device folio, retain the subpage offset when selecting the backing page; using the backing folio's head for every entry would return the wrong data. Extend the existing PMD migration selftest to read the last subpage through a second open before faulting the memory back to RAM.
---
tl;dr: The unpatched kernel oopsed when an unprivileged process opened the fake HMM device twice, migrated an anonymous 2 MB PMD to device-private memory through one descriptor, then accessed its last page through the other. The patched kernel returned the expected bytes on all three ioctl paths.
A was reproduced and saved before B was built. As uid 65534, a static userspace program verified that all 512 pages were device-private PMD entries. A READ of the last page through the second descriptor produced a KASAN-enabled kernel Oops in dmirror_do_read(). Independent A boots also oopsed on WRITE and READ_UNLOCKED at the same last-page access.
With only this patch added, READ and READ_UNLOCKED returned the expected 0x0a byte, and WRITE returned the expected 0xa5 byte after migration back to RAM. The modified in-tree hmm_device_private.migrate_anon_huge_fault selftest also passed (1/1).
This reproduces a userspace-triggered kernel Oops in the fake HMM device used by test_hmm; it does not establish reachability through physical GPU hardware. QEMU guests used matching kernel configurations with CONFIG_TEST_HMM=y and KASAN. The guest device permissions were set for uid 65534.
lib/test_hmm.c | 7 +++++++ tools/testing/selftests/mm/hmm-tests.c | 14 ++++++++++++++ 2 files changed, 21 insertions(+)
lib/test_hmm.c renamed from lib/test_hmm.c~lib-test_hmm-use-backing-pages-for-device-private-mirror-faults +7 -0
@@ -292,6 +292,13 @@ 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 =folio_page (page_folio (backing), +folio_page_idx (folio, page)); + } entry = page;if (*pfns &HMM_PFN_WRITE ) entry =xa_tag_pointer (entry,DPT_XA_TAG_WRITE );
tools/testing/selftests/mm/hmm-tests.c renamed from tools/testing/selftests/mm/hmm-tests.c~lib-test_hmm-use-backing-pages-for-device-private-mirror-faults +14 -0
@@ -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,25 @@ TEST_F(hmm, migrate_anon_huge_fault) }close (pagemap_fd); + +/* A second open must read the last device-private subpage. */ + 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 ) +ASSERT_EQ (close (fd2),0 ); buffer->ptr = old_ptr;hmm_buffer_free (buffer); }
_
Quoted 68 lines · Andrew Morton
Quoted 6 lines · On Sun, 22 Nov 2026 07:55:37 +0200 Mika Penttilä <[email protected]> wrote:
Quoted 5 lines
So it accidentally found a bug in
lib/test_hmm.con the way. "The QEMU run exposed a realtest_hmmbug: a second open of/dev/hmm_dmirror0can put a device-private page into its page table, thendmirror_do_read()tries to copy from that non-CPU-accessible page and oopses". I'll prep a fix for that separately.I noticed that also and will make changes around it in my series.
I came up with the below. It all tested out OK. wdyt? I guess it should have cc:stable>
From: Andrew Morton <[email protected]>
Subject:lib/test_hmm: use backing pages for device-private mirror faults
Date: Sat, 21 Nov 2026 17:06:45 -0800When one
hmm_dmirrordescriptor migrates memory to device-private and a second descriptor reads it,dmirror_do_fault()stores the device page in the second descriptor's page table.dmirror_do_read()then passes that page tomemcpy_from_page(), which can oops the kernel. This was reproduced with an unprivileged process using the fake device in a QEMU guest.Store the corresponding RAM backing page instead. For a large device folio, retain the subpage offset when selecting the backing page; using the backing folio's head for every entry would return the wrong data. Extend the existing PMD migration selftest to read the last subpage through a second open before faulting the memory back to RAM.
Fixes b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM")Signed-off-by Andrew Morton <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Leon Romanovsky <[email protected]>Cc Shuah Khan <[email protected]>---
tl;dr: The unpatched kernel oopsed when an unprivileged process opened the fake HMM device twice, migrated an anonymous 2 MB PMD to device-private memory through one descriptor, then accessed its last page through the other. The patched kernel returned the expected bytes on all three ioctl paths.A was reproduced and saved before B was built. As uid 65534, a static userspace program verified that all 512 pages were device-private PMD entries. A READ of the last page through the second descriptor produced a KASAN-enabled kernel Oops in
dmirror_do_read(). Independent A boots also oopsed on WRITE andREAD_UNLOCKEDat the same last-page access.With only this patch added, READ and
READ_UNLOCKEDreturned the expected0x0abyte, and WRITE returned the expected0xa5byte after migration back to RAM. The modified in-tree hmm_device_private.migrate_anon_huge_fault selftest also passed (1/1).This reproduces a userspace-triggered kernel Oops in the fake HMM device used by
test_hmm; it does not establish reachability through physical GPU hardware. QEMU guests used matching kernel configurations withCONFIG_TEST_HMM=yand KASAN. The guest device permissions were set for uid 65534.lib/test_hmm.c | 7 +++++++ tools/testing/selftests/mm/hmm-tests.c | 14 ++++++++++++++ 2 files changed, 21 insertions(+)lib/test_hmm.c renamed from lib/test_hmm.c~lib-test_hmm-use-backing-pages-for-device-private-mirror-faults +0
-0@@ -292,6 +292,13 @@ static int dmirror_do_fault(struct dmirr page =hmm_pfn_to_page (*pfns);WARN_ON (!page);
lib/test_hmm.c static int dmirror_do_fault(struct dmirr · line 303
Quoted 10 lines
lib/test_hmm.c renamed from lib/test_hmm.c~lib-test_hmm-use-backing-pages-for-device-private-mirror-faults +7
-0+if (is_device_private_page (page)) { +struct folio *folio =page_folio (page); +struct page *backing =BACKING_PAGE (&folio->page); + + page =folio_page (page_folio (backing), +folio_page_idx (folio, page)); + } entry = page;if (*pfns &HMM_PFN_WRITE )
I have this same chunk. I think it's needed, all dmirror readers expect to copy_to_user() from pt.
Quoted 37 lines
lib/test_hmm.c renamed from lib/test_hmm.c~lib-test_hmm-use-backing-pages-for-device-private-mirror-faults +0
-0entry =xa_tag_pointer (entry,DPT_XA_TAG_WRITE );tools/testing/selftests/mm/hmm-tests.c renamed from tools/testing/selftests/mm/hmm-tests.c~lib-test_hmm-use-backing-pages-for-device-private-mirror-faults +14
-0@@ -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,25 @@ TEST_F(hmm, migrate_anon_huge_fault) }close (pagemap_fd); + +/* A second open must read the last device-private subpage. */ + 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 ) +ASSERT_EQ (close (fd2),0 ); buffer->ptr = old_ptr;hmm_buffer_free (buffer); }
Quoted 1 line
_
Looks good to me also!
--Mika
Hi Andrew,
Quoted 1 line · On Sun, 22 Nov 2026 01:52:46 -0800 Andrew Morton wrote:
Store the corresponding RAM backing page instead.
This avoids the oops on the first read through the second descriptor, but leaves a stale backing page pointer after CPU faultback.
Since the mirror XArray entries don't take a page reference, they rely on invalidation. When the CPU later faults the page back to RAM, dmirror_interval_invalidate() skips MMU_NOTIFY_MIGRATE for the same mdevice, and dmirror_devmem_fault_alloc_and_copy() only clears the migrating descriptor's entry before the backing folio is freed. The second descriptor still points to that freed backing page.
Reading the same page again through the second descriptor in QEMU triggers a KASAN use-after-free in dmirror_do_read(), and the read returns the stale data instead of what the CPU wrote.
The second descriptor's entry probably needs to be invalidated before the backing page is freed, and it'd be nice to cover a second read after faultback in the selftest too.
Thanks,
Aveline Noir
Quoted 15 lines · Aveline Noir
Hi Andrew,
Quoted 1 line · On Sun, 22 Nov 2026 01:52:46 -0800 Andrew Morton wrote:
Store the corresponding RAM backing page instead.
This avoids the oops on the first read through the second descriptor, but leaves a stale backing page pointer after CPU faultback.
Since the mirror XArray entries don't take a page reference, they rely on invalidation. When the CPU later faults the page back to RAM,
dmirror_interval_invalidate()skipsMMU_NOTIFY_MIGRATEfor the same mdevice, anddmirror_devmem_fault_alloc_and_copy()only clears the migrating descriptor's entry before the backing folio is freed. The second descriptor still points to that freed backing page.
Quoted 3 lines
Reading the same page again through the second descriptor in QEMU triggers a KASAN use-after-free in
dmirror_do_read(), and the read returns the stale data instead of what the CPU wrote.
I think this is right, but a fairly artificial scenario: it needs two descriptors open on the same test device in one test, a migration through one, a read through the other, a CPU fault back, and then another read through the second descriptor..
The fix lets the device private page content be read without an oops in the context of one fd which is an improvement.
Quoted 6 lines
The second descriptor's entry probably needs to be invalidated before the backing page is freed, and it'd be nice to cover a second read after faultback in the selftest too.
Thanks,
Aveline Noir
--Mika
Quoted 15 lines · Aveline Noir
Hi Andrew,
Quoted 1 line · On Sun, 22 Nov 2026 01:52:46 -0800 Andrew Morton wrote:
Store the corresponding RAM backing page instead.
This avoids the oops on the first read through the second descriptor, but leaves a stale backing page pointer after CPU faultback.
Since the mirror XArray entries don't take a page reference, they rely on invalidation. When the CPU later faults the page back to RAM,
dmirror_interval_invalidate()skipsMMU_NOTIFY_MIGRATEfor the same mdevice, anddmirror_devmem_fault_alloc_and_copy()only clears the migrating descriptor's entry before the backing folio is freed. The second descriptor still points to that freed backing page.
Quoted 3 lines
Reading the same page again through the second descriptor in QEMU triggers a KASAN use-after-free in
dmirror_do_read(), and the read returns the stale data instead of what the CPU wrote.
I think this is right, but a fairly artificial scenario: it needs two descriptors open on the same test device in one test, a migration through one, a read through the other, a CPU fault back, and then another read through the second descriptor..
The fix lets the device private page content be read without an oops in the context of one fd which is an improvement.
Quoted 7 lines
The second descriptor's entry probably needs to be invalidated before the backing page is freed, and it'd be nice to cover a second read after faultback in the selftest too.
Thanks,
Aveline Noir
--Mika
Andrew Morton
maintainerQuoted 22 lines · On Sun, 22 Nov 2026 18:41:39 +0200 Mika Penttilä <[email protected]> wrote:
Quoted 15 lines · Aveline Noir
Hi Andrew,
Quoted 1 line · On Sun, 22 Nov 2026 01:52:46 -0800 Andrew Morton wrote:
Store the corresponding RAM backing page instead.
This avoids the oops on the first read through the second descriptor, but leaves a stale backing page pointer after CPU faultback.
Since the mirror XArray entries don't take a page reference, they rely on invalidation. When the CPU later faults the page back to RAM,
dmirror_interval_invalidate()skipsMMU_NOTIFY_MIGRATEfor the same mdevice, anddmirror_devmem_fault_alloc_and_copy()only clears the migrating descriptor's entry before the backing folio is freed. The second descriptor still points to that freed backing page.Quoted 3 lines
Reading the same page again through the second descriptor in QEMU triggers a KASAN use-after-free in
dmirror_do_read(), and the read returns the stale data instead of what the CPU wrote.
Quoted 7 lines
I think this is right, but a fairly artificial scenario: it needs two descriptors open on the same test device in one test, a migration through one, a read through the other, a CPU fault back, and then another read through the second descriptor..
The fix lets the device private page content be read without an oops in the context of one fd which is an improvement.
May as well fix it. I'll test this updated version.
Mika, please feel free to take this over, perhaps integrate it (or something like it) into whatever you've got going on? Or not, let me know ;)
From: Andrew Morton <[email protected]>
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.
lib/test_hmm.c | 20 +++++++----------- tools/testing/selftests/mm/hmm-tests.c | 25 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-)
lib/test_hmm.c renamed from lib/test_hmm.c~lib-test_hmm-invalidate-all-mirrors-on-device-private-migration +8 -12
@@ -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); + + page =BACKING_PAGE (&folio->page) + +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:
tools/testing/selftests/mm/hmm-tests.c renamed from tools/testing/selftests/mm/hmm-tests.c~lib-test_hmm-invalidate-all-mirrors-on-device-private-migration +25 -0
@@ -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); }
_
Quoted 27 lines · Andrew Morton
Quoted 25 lines · On Sun, 22 Nov 2026 18:41:39 +0200 Mika Penttilä <[email protected]> wrote:
Quoted 15 lines · Aveline Noir
Hi Andrew,
Quoted 1 line · On Sun, 22 Nov 2026 01:52:46 -0800 Andrew Morton wrote:
Store the corresponding RAM backing page instead.
This avoids the oops on the first read through the second descriptor, but leaves a stale backing page pointer after CPU faultback.
Since the mirror XArray entries don't take a page reference, they rely on invalidation. When the CPU later faults the page back to RAM,
dmirror_interval_invalidate()skipsMMU_NOTIFY_MIGRATEfor the same mdevice, anddmirror_devmem_fault_alloc_and_copy()only clears the migrating descriptor's entry before the backing folio is freed. The second descriptor still points to that freed backing page.Quoted 3 lines
Reading the same page again through the second descriptor in QEMU triggers a KASAN use-after-free in
dmirror_do_read(), and the read returns the stale data instead of what the CPU wrote.I think this is right, but a fairly artificial scenario: it needs two descriptors open on the same test device in one test, a migration through one, a read through the other, a CPU fault back, and then another read through the second descriptor..
Quoted 7 lines
Quoted 2 lines
The fix lets the device private page content be read without an oops in the context of one fd which is an improvement.
May as well fix it. I'll test this updated version.
Mika, please feel free to take this over, perhaps integrate it (or something like it) into whatever you've got going on? Or not, let me know ;)
I can handle it in the context where it made difference for me.
Quoted 150 lines
From: Andrew Morton <[email protected]>
Subject:lib/test_hmm: invalidate all mirrors on device-private migration
Date: Sun, 22 Nov 2026 08:53:12 -0800When 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, butdmirror_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 aftermigrate_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*whenCONFIG_TEST_HMMis enabled; it does not depend on physical GPU hardware.Fixes b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM")Signed-off-by Andrew Morton <[email protected]>Reported-by Aveline Noir <[email protected]>Cc Ralph Campbell <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Leon Romanovsky <[email protected]>Cc Mika Penttilä <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Liam R. Howlett <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Mike Rapoport <[email protected]>Cc Michal Hocko <[email protected]>Cc <[email protected]>lib/test_hmm.c | 20 +++++++----------- tools/testing/selftests/mm/hmm-tests.c | 25 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-)lib/test_hmm.c renamed from lib/test_hmm.c~lib-test_hmm-invalidate-all-mirrors-on-device-private-migration +8
-12@@ -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); + + page =BACKING_PAGE (&folio->page) + +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:tools/testing/selftests/mm/hmm-tests.c renamed from tools/testing/selftests/mm/hmm-tests.c~lib-test_hmm-invalidate-all-mirrors-on-device-private-migration +25
-0@@ -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); }
Quoted 1 line
_
--Mika
Quoted 26 lines · Aveline Noir
When a query starts inside a device-private PMD owned by
range->dev_private_owner,hmm_vma_handle_absent_pmd()still fills the PFN array from the folio's first page. The returned PFNs therefore refer to earlier pages rather than the requested subrange.Add the query's PMD-relative page offset to the initial PFN, as the present PMD path already does.
Fixes 10b9feee2d0d ("mm/hmm: populate PFNs from PMD swap entry")Signed-off-by Aveline Noir <[email protected]>---
Found during a synthetictest_hmmcheck of device-private THP handling. Tested PMD-aligned and non-aligned subranges, PMD boundaries, split PTEs and fake-device data accesses withtest_hmmin QEMU TCG; all executed tests passed with the fix. No real GPU testing was performed.--- mm/hmm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)mm/hmm.c +0
-0
mm/hmm.c static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, · line 362
Quoted 9 lines
mm/hmm.c +2
-1@@ -355,7 +355,8 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, 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 pfn =softleaf_to_pfn (entry) + + ((start & ~PMD_MASK ) >>PAGE_SHIFT );unsigned long i;if (softleaf_is_device_private_write (entry))
Looks good to me!