From: sashiko-bot@kernel.org
To: "Andrey Mokrushin" <a.mokrushin@ideco.ru>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Jason Gunthorpe" <jgg@ziepe.ca>,
"Leon Romanovsky" <leon@kernel.org>
Cc: linux-mm@kvack.org, lvc-project@linuxtesting.org
Subject: Re: [PATCH] lib/test_hmm: don't allocate device memory chunks under mmap_lock
Date: Thu, 03 Dec 2026 06:16:08 +0000 [thread overview]
Message-ID: <sashiko-outbox-247223@kernel.org> (raw)
In-Reply-To: <20261202061535.38176-1-a.mokrushin@ideco.ru>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm/hmm/test: broken base-page fallback in
dmirror_migrate_alloc_and_copy()
- [Medium] mm/hmm/test: partial split crash or loop in
dmirror_migrate_alloc_and_copy()
- [Medium] mm/hmm/test: unbounded chunk allocation loop in
dmirror_migrate_to_device()
--
commit acf453213f1810f4cf9d9256c95b4bd7dcb938c3
Author: Andrey Mokrushin <a.mokrushin@ideco.ru>
lib/test_hmm: don't allocate device memory chunks under mmap_lock
This commit moves device memory chunk allocation out of
dmirror_devmem_alloc_page() so that memremap_pages() is not called while
holding mmap_lock, instead retrying the range in
dmirror_migrate_to_device() after dropping mmap_lock and allocating the
needed chunks.
> diff --git a/lib/test_hmm.c b/lib/test_hmm.c
> index 893caab93bac2..8d9f51443d209 100644
> --- a/lib/test_hmm.c
> +++ b/lib/test_hmm.c
[ ... ]
> @@ -757,29 +757,9 @@ static int dmirror_allocate_chunk(struct dmirror_device *mdevice,
> pfn++;
> }
>
> - ret = 0;
> - if (ppage) {
> - if (is_large) {
> - if (!mdevice->free_folios) {
> - ret = -ENOMEM;
> - goto err_unlock;
> - }
> - *ppage = folio_page(mdevice->free_folios, 0);
> - mdevice->free_folios = (*ppage)->zone_device_data;
> - mdevice->calloc += HPAGE_PMD_NR;
> - } else if (mdevice->free_pages) {
> - *ppage = mdevice->free_pages;
> - mdevice->free_pages = (*ppage)->zone_device_data;
> - mdevice->calloc++;
> - } else {
> - ret = -ENOMEM;
> - goto err_unlock;
> - }
> - }
> -err_unlock:
> spin_unlock(&mdevice->lock);
>
> - return ret;
> + return 0;
[Severity: Medium]
Can removing the !mdevice->free_folios check when is_large is true cause
dmirror_migrate_to_device() to allocate 256 MB chunks in an unbounded
loop?
On architectures where HPAGE_PMD_SIZE exceeds DEVMEM_CHUNK_SIZE (such as
64 KB base pages where HPAGE_PMD_SIZE is 512 MB and DEVMEM_CHUNK_SIZE is
256 MB), pfn + HPAGE_PMD_NR <= pfn_last is never true, so
dmirror_allocate_chunk(mdevice, true) puts every page onto
mdevice->free_pages, leaves mdevice->free_folios empty, and returns 0.
dmirror_migrate_to_device() then sets next = addr and retries the same
range until memory or iomem_resource is exhausted.
[ ... ]
> @@ -874,9 +890,20 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
>
> if (dmirror->flags & HMM_DMIRROR_FLAG_FAIL_ALLOC) {
> dmirror->flags &= ~HMM_DMIRROR_FLAG_FAIL_ALLOC;
> + fail_alloc = true;
> dpage = NULL;
> - } else
> - dpage = dmirror_devmem_alloc_page(dmirror, is_large);
> + } else {
> + bool empty = false;
> +
> + dpage = dmirror_devmem_alloc_page(dmirror, is_large,
> + &empty);
> + if (empty) {
> + /* Retried after a new chunk is allocated */
> + *need_chunk |= is_large ? DMIRROR_NEED_FOLIOS :
> + DMIRROR_NEED_PAGES;
> + goto next;
> + }
> + }
[Severity: Medium]
Does jumping to next when is_large is true and mdevice->free_folios is
empty break the fallback that splits a compound migration into base pages
from mdevice->free_pages?
dmirror_device_init() allocates the initial chunk with is_large = false,
leaving mdevice->free_folios empty. On HMM_DMIRROR_MEMORY_DEVICE_COHERENT
devices, dmirror_allocate_chunk() uses a fixed physical address range that
is already mapped by that initial chunk, so a later
dmirror_allocate_chunk(mdevice, true) call always fails. Before this
change, that failure returned NULL from dmirror_devmem_alloc_page() and
fell through to the if (!dpage) block to allocate order-0 pages from
mdevice->free_pages; now goto next skips the split, and when
dmirror_allocate_chunks() fails in dmirror_migrate_to_device(), the
compound page is left unmigrated and dmirror_do_read() fails with -ENOENT.
>
> if (!dpage) {
> struct folio *folio;
> @@ -894,8 +921,22 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
> nr = folio_nr_pages(folio);
> }
>
> + /* Split into base pages only if all of them are free */
> + if (!dmirror_has_free_pages(dmirror->mdevice, nr)) {
> + *need_chunk |= DMIRROR_NEED_PAGES;
> + if (fail_alloc)
> + dmirror->flags |= HMM_DMIRROR_FLAG_FAIL_ALLOC;
> + goto next;
> + }
> +
> for (i = 0; i < nr && addr < args->end; i++) {
> - dpage = dmirror_devmem_alloc_page(dmirror, false);
> + bool empty = false;
> +
> + dpage = dmirror_devmem_alloc_page(dmirror, false,
> + &empty);
> + /* Only if another thread raced for the pages */
> + if (!dpage)
> + break;
[Severity: Medium]
What happens if dmirror_devmem_alloc_page() fails partway through this
split loop (either because another thread took pages from
mdevice->free_pages after dmirror_has_free_pages() dropped mdevice->lock,
or because alloc_pages() failed)?
If it fails at i > 0, dst[0 .. i-1] remain populated without
MIGRATE_PFN_COMPOUND while dst[i .. nr-1] remain 0; when spage is NULL (an
unbacked compound entry), __migrate_device_pages() then calls
migrate_vma_insert_page() across all nr subpages and dereferences NULL in
page_folio(migrate_pfn_to_page(*dst)) at entry i:
mm/migrate_device.c:__migrate_device_pages() {
...
if ((src_pfns[i] & MIGRATE_PFN_COMPOUND) &&
(!(dst_pfns[i] & MIGRATE_PFN_COMPOUND))) {
nr = migrate_vma_nr_pages(&src_pfns[i]);
src_pfns[i] &= ~MIGRATE_PFN_COMPOUND;
} else {
nr = 1;
}
for (j = 0; j < nr && i + j < npages; j++) {
src_pfns[i+j] |= MIGRATE_PFN_MIGRATE;
migrate_vma_insert_page(migrate,
addr + j * PAGE_SIZE,
&dst_pfns[i+j], &src_pfns[i+j]);
}
...
}
If it fails at i == 0, break exits the inner loop before src, dst and addr
are incremented, and the following continue re-runs the outer loop at the
same address without setting *need_chunk.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202061535.38176-1-a.mokrushin@ideco.ru?part=1
prev parent reply other threads:[~2026-12-03 6:16 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 6:15 [PATCH] lib/test_hmm: don't allocate device memory chunks under mmap_lock Andrey Mokrushin
2026-12-02 23:07 ` Alistair Popple
2026-12-03 6:16 ` sashiko-bot [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=sashiko-outbox-247223@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=a.mokrushin@ideco.ru \
--cc=akpm@linux-foundation.org \
--cc=jgg@ziepe.ca \
--cc=leon@kernel.org \
--cc=linux-mm@kvack.org \
--cc=lvc-project@linuxtesting.org \
--cc=sashiko-reviews@lists.linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.