* [PATCH] mm/hmm: account for subpage offsets in device-private PMDs
@ 2026-10-31 4:07 Aveline Noir
2026-11-22 0:00 ` Andrew Morton
0 siblings, 1 reply; 4+ messages in thread
From: Aveline Noir @ 2026-10-31 4:07 UTC (permalink / raw)
To: jgg, leonro, akpm
Cc: airlied, apopple, balbirs, christian.koenig, dri-devel,
Francois Dugast, intel-xe, jgg, leon, linux-kernel, linux-mm,
Lorenzo Stoakes, Matthew Brost, mm-commits, mpenttil, Patchwork,
SeongJae Park, thomas.hellstrom, Vlastimil Babka, ziy
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 <jm5905938@gmail.com>
---
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(-)
diff --git a/mm/hmm.c b/mm/hmm.c
index 2f1e98c6b644..e89f140d3e5c 100644
--- a/mm/hmm.c
+++ b/mm/hmm.c
@@ -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
--
2.56.0
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] mm/hmm: account for subpage offsets in device-private PMDs 2026-10-31 4:07 [PATCH] mm/hmm: account for subpage offsets in device-private PMDs Aveline Noir @ 2026-11-22 0:00 ` Andrew Morton 2026-11-22 5:55 ` Mika Penttilä 0 siblings, 1 reply; 4+ messages in thread From: Andrew Morton @ 2026-11-22 0:00 UTC (permalink / raw) To: Aveline Noir Cc: jgg, leonro, airlied, apopple, balbirs, christian.koenig, dri-devel, Francois Dugast, intel-xe, jgg, leon, linux-kernel, linux-mm, Lorenzo Stoakes, Matthew Brost, mm-commits, mpenttil, Patchwork, SeongJae Park, thomas.hellstrom, Vlastimil Babka, ziy On Sat, 31 Oct 2026 15:07:58 +1100 Aveline Noir <jm5905938@gmail.com> 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. > > Fixes: 10b9feee2d0d ("mm/hmm: populate PFNs from PMD swap entry") > Signed-off-by: Aveline Noir <jm5905938@gmail.com> > > --- > 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. 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 <jm5905938@gmail.com> 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. Link: https://patch.msgid.link/179342234355.4.16705453209886955762@gmail.com Fixes: 10b9feee2d0d ("mm/hmm: populate PFNs from PMD swap entry") Signed-off-by: Aveline Noir <jm5905938@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: Leon Romanovsky <leon@kernel.org> Cc: Francois Dugast <francois.dugast@intel.com> Cc: Lorenzo Stoakes <ljs@kernel.org> Cc: Matthew Brost <matthew.brost@intel.com> Cc: Patchwork <patchwork@emeril.freedesktop.org> Cc: SJ Park <sj@kernel.org> Cc: Vlastimil Babka <vbabka@kernel.org> Cc: David Airlie <airlied@gmail.com> Cc: Alistair Popple <apopple@nvidia.com> Cc: Balbir Singh <balbirs@nvidia.com> Cc: Christian König <christian.koenig@amd.com> Cc: Mika Penttilä <mpenttil@redhat.com> Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com> Cc: Zi Yan <ziy@nvidia.com> Cc: <stable@vger.kernel.org> mm/hmm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) --- a/mm/hmm.c~mm-hmm-account-for-subpage-offsets-in-device-private-pmds +++ a/mm/hmm.c @@ -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)) _ ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm/hmm: account for subpage offsets in device-private PMDs 2026-11-22 0:00 ` Andrew Morton @ 2026-11-22 5:55 ` Mika Penttilä 2026-11-22 9:52 ` Andrew Morton 0 siblings, 1 reply; 4+ messages in thread From: Mika Penttilä @ 2026-11-22 5:55 UTC (permalink / raw) To: Andrew Morton, Aveline Noir Cc: jgg, leonro, airlied, apopple, balbirs, christian.koenig, dri-devel, Francois Dugast, intel-xe, jgg, leon, linux-kernel, linux-mm, Lorenzo Stoakes, Matthew Brost, mm-commits, Patchwork, SeongJae Park, thomas.hellstrom, Vlastimil Babka, ziy On 11/22/26 02:00, Andrew Morton wrote: > On Sat, 31 Oct 2026 15:07:58 +1100 Aveline Noir <jm5905938@gmail.com> 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. >> >> Fixes: 10b9feee2d0d ("mm/hmm: populate PFNs from PMD swap entry") >> Signed-off-by: Aveline Noir <jm5905938@gmail.com> >> >> --- >> 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. > 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 noticed that also and will make changes around it in my series. > > > > I'll queue the below in mm.git's mm-hotfixes queue and shall await > maintainer input (please). > > > From: Aveline Noir <jm5905938@gmail.com> > 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. > > Link: https://patch.msgid.link/179342234355.4.16705453209886955762@gmail.com > Fixes: 10b9feee2d0d ("mm/hmm: populate PFNs from PMD swap entry") > Signed-off-by: Aveline Noir <jm5905938@gmail.com> > Signed-off-by: Andrew Morton <akpm@linux-foundation.org> > Cc: Jason Gunthorpe <jgg@ziepe.ca> > Cc: Leon Romanovsky <leon@kernel.org> > Cc: Francois Dugast <francois.dugast@intel.com> > Cc: Lorenzo Stoakes <ljs@kernel.org> > Cc: Matthew Brost <matthew.brost@intel.com> > Cc: Patchwork <patchwork@emeril.freedesktop.org> > Cc: SJ Park <sj@kernel.org> > Cc: Vlastimil Babka <vbabka@kernel.org> > Cc: David Airlie <airlied@gmail.com> > Cc: Alistair Popple <apopple@nvidia.com> > Cc: Balbir Singh <balbirs@nvidia.com> > Cc: Christian König <christian.koenig@amd.com> > Cc: Mika Penttilä <mpenttil@redhat.com> > Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com> > Cc: Zi Yan <ziy@nvidia.com> > Cc: <stable@vger.kernel.org> > > mm/hmm.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > --- a/mm/hmm.c~mm-hmm-account-for-subpage-offsets-in-device-private-pmds > +++ a/mm/hmm.c > @@ -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)) > _ For this change: Reviewed-by: Mika Penttilä <mpenttil@redhat.com> ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm/hmm: account for subpage offsets in device-private PMDs 2026-11-22 5:55 ` Mika Penttilä @ 2026-11-22 9:52 ` Andrew Morton 0 siblings, 0 replies; 4+ messages in thread From: Andrew Morton @ 2026-11-22 9:52 UTC (permalink / raw) To: Mika Penttilä Cc: Aveline Noir, jgg, leonro, airlied, apopple, balbirs, christian.koenig, dri-devel, Francois Dugast, intel-xe, jgg, leon, linux-kernel, linux-mm, Lorenzo Stoakes, Matthew Brost, mm-commits, Patchwork, SeongJae Park, thomas.hellstrom, Vlastimil Babka, ziy On Sun, 22 Nov 2026 07:55:37 +0200 Mika Penttilä <mpenttil@redhat.com> wrote: > > 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 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 <akpm@linux-foundation.org> 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. Fixes: b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM") Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: Leon Romanovsky <leon@kernel.org> Cc: Shuah Khan <shuah@kernel.org> --- 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(+) --- a/lib/test_hmm.c~lib-test_hmm-use-backing-pages-for-device-private-mirror-faults +++ a/lib/test_hmm.c @@ -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); --- a/tools/testing/selftests/mm/hmm-tests.c~lib-test_hmm-use-backing-pages-for-device-private-mirror-faults +++ 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,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); } _ ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-22 9:52 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-10-31 4:07 [PATCH] mm/hmm: account for subpage offsets in device-private PMDs Aveline Noir 2026-11-22 0:00 ` Andrew Morton 2026-11-22 5:55 ` Mika Penttilä 2026-11-22 9:52 ` Andrew Morton
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®