From: "Mika Penttilä" <mpenttil@redhat.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Aveline Noir <jm5905938@gmail.com>,
jgg@nvidia.com, leonro@nvidia.com, airlied@gmail.com,
apopple@nvidia.com, balbirs@nvidia.com, christian.koenig@amd.com,
dri-devel@lists.freedesktop.org,
Francois Dugast <francois.dugast@intel.com>,
intel-xe@lists.freedesktop.org, jgg@ziepe.ca, leon@kernel.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
Matthew Brost <matthew.brost@intel.com>,
mm-commits@vger.kernel.org,
Patchwork <patchwork@emeril.freedesktop.org>,
SeongJae Park <sj@kernel.org>,
thomas.hellstrom@linux.intel.com,
Vlastimil Babka <vbabka@suse.cz>,
ziy@nvidia.com
Subject: Re: [PATCH] mm/hmm: account for subpage offsets in device-private PMDs
Date: Sun, 22 Nov 2026 14:04:34 +0200 [thread overview]
Message-ID: <ab91c700-311c-4bd5-8d8e-d90d573f57c0@redhat.com> (raw)
In-Reply-To: <20261122015246.d8b6b98ff5a8157168156339@linux-foundation.org>
On 11/22/26 11:52, Andrew Morton wrote:
> 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)
I have this same chunk. I think it's needed, all dmirror readers expect to
copy_to_user() from pt.
> 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);
> }
> _
Looks good to me also!
--Mika
prev parent reply other threads:[~2026-11-22 12:04 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-31 4:07 Aveline Noir
2026-11-22 0:00 ` Andrew Morton
2026-11-22 5:55 ` Mika Penttilä
2026-11-22 9:52 ` Andrew Morton
2026-11-22 12:04 ` Mika Penttilä [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ab91c700-311c-4bd5-8d8e-d90d573f57c0@redhat.com \
--to=mpenttil@redhat.com \
--cc=airlied@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=balbirs@nvidia.com \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=francois.dugast@intel.com \
--cc=intel-xe@lists.freedesktop.org \
--cc=jgg@nvidia.com \
--cc=jgg@ziepe.ca \
--cc=jm5905938@gmail.com \
--cc=leon@kernel.org \
--cc=leonro@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=matthew.brost@intel.com \
--cc=mm-commits@vger.kernel.org \
--cc=patchwork@emeril.freedesktop.org \
--cc=sj@kernel.org \
--cc=thomas.hellstrom@linux.intel.com \
--cc=vbabka@suse.cz \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®