From: Alistair Popple <apopple@nvidia.com>
To: Andrey Mokrushin <a.mokrushin@ideco.ru>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Jason Gunthorpe <jgg@ziepe.ca>,
Leon Romanovsky <leon@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
lvc-project@linuxtesting.org
Subject: Re: [PATCH] lib/test_hmm: don't allocate device memory chunks under mmap_lock
Date: Thu, 3 Dec 2026 10:07:39 +1100 [thread overview]
Message-ID: <axCkSIvcBFd9qMoy@nvdebian.thelocal> (raw)
In-Reply-To: <20261202061535.38176-1-a.mokrushin@ideco.ru>
On 2026-12-02 at 17:15 +1100, Andrey Mokrushin <a.mokrushin@ideco.ru> wrote...
> dmirror_migrate_to_device() holds mmap_read_lock() of the mirrored mm
> and, when the device runs out of free pages, dmirror_devmem_alloc_page()
> allocates a new chunk with memremap_pages(), which takes
> cpu_hotplug_lock via mem_hotplug_begin(). This nests cpu_hotplug_lock
> inside mmap_lock, while cpuset nests mmap_lock inside cpu_hotplug_lock
> and cpuset_mutex (cgroup_attach_lock() -> cpuset_attach() ->
> mpol_rebind_mm(), also cpuset_handle_hotplug() -> mpol_rebind_mm()).
> Lockdep reports it while running hmm-tests after a task has been moved
> into a cpuset with a different set of memory nodes:
>
> WARNING: possible circular locking dependency detected
> hmm-tests is trying to acquire lock:
> (&mdevice->devmem_lock), at: dmirror_allocate_chunk+0xc7/0x520 [test_hmm]
> but task is already holding lock:
> (&mm->mmap_lock), at: mmap_read_lock+0x36/0xc0 [test_hmm]
>
> Chain exists of:
> &mdevice->devmem_lock --> cpuset_mutex --> &mm->mmap_lock
>
> The deadlock is real if a CPU hotplug writer is waiting on
> cpu_hotplug_lock at the same time.
>
> Don't allocate chunks from dmirror_devmem_alloc_page(). Report an empty
> free list to dmirror_migrate_to_device() instead, which allocates the
> missing chunk after dropping mmap_lock and retries the current range.
> Pages that were already migrated are skipped on the retry, as they are
> no longer system memory. When a huge page has to be split into base
> pages, do it only if enough base pages are free, so the split is not
> left half done.
>
> Found by Linux Verification Center (linuxtesting.org).
>
> Fixes: b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM")
> Signed-off-by: Andrey Mokrushin <a.mokrushin@ideco.ru>
> ---
> The analysis of the lockdep report, the fix and the changelog were drafted
> with an AI coding assistant (Claude Code); it was also used to run the
> tests below. I reviewed the change, checked the results and take
> responsibility for the patch.
It will take me a while to review the fix but the deadlock sounds plausible.
From memory Nouveau also copied a similar pattern from here, have you (or your
agent) looked at that to see if it suffers the same issue?
Thanks.
- Alistair
> Tested on mm-unstable 890dc639d8a in QEMU with 2 NUMA nodes and lockdep:
> cpuset.mems of a cgroup holding a task is changed, then hmm-tests is run.
> without the patch: the circular locking report above, 2 of 2 runs;
> with the patch: no lockdep reports, 2 of 2 runs.
> hmm-tests results are the same with and without the patch:
> 39 passed, 0 failed, 39 skipped.
>
> lib/test_hmm.c | 118 ++++++++++++++++++++++++++++++++++++-------------
> 1 file changed, 87 insertions(+), 31 deletions(-)
>
> diff --git a/lib/test_hmm.c b/lib/test_hmm.c
> index 893caab93ba..8d9f51443d2 100644
> --- a/lib/test_hmm.c
> +++ b/lib/test_hmm.c
> @@ -661,7 +661,7 @@ static int dmirror_write(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd)
> }
>
> static int dmirror_allocate_chunk(struct dmirror_device *mdevice,
> - struct page **ppage, bool is_large)
> + bool is_large)
> {
> struct dmirror_chunk *devmem;
> struct resource *res = NULL;
> @@ -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;
>
> err_release:
> mutex_unlock(&mdevice->devmem_lock);
> @@ -792,8 +772,42 @@ static int dmirror_allocate_chunk(struct dmirror_device *mdevice,
> return ret;
> }
>
> +#define DMIRROR_NEED_PAGES BIT(0)
> +#define DMIRROR_NEED_FOLIOS BIT(1)
> +
> +static int dmirror_allocate_chunks(struct dmirror_device *mdevice,
> + unsigned int need)
> +{
> + int ret = 0;
> +
> + if (need & DMIRROR_NEED_PAGES)
> + ret = dmirror_allocate_chunk(mdevice, false);
> + if (!ret && (need & DMIRROR_NEED_FOLIOS))
> + ret = dmirror_allocate_chunk(mdevice, true);
> + return ret;
> +}
> +
> +static bool dmirror_has_free_pages(struct dmirror_device *mdevice,
> + unsigned long nr)
> +{
> + struct page *page;
> +
> + spin_lock(&mdevice->lock);
> + for (page = mdevice->free_pages; page && nr; page = page->zone_device_data)
> + nr--;
> + spin_unlock(&mdevice->lock);
> + return !nr;
> +}
> +
> +/*
> + * Called with the mmap_lock of the mirrored mm held, so it must not allocate
> + * a new chunk: memremap_pages() takes cpu_hotplug_lock, which nests outside
> + * of mmap_lock (e.g. cpuset_attach() -> mpol_rebind_mm()). If the free list
> + * is empty, *empty is set and the caller allocates a chunk after dropping
> + * mmap_lock.
> + */
> static struct page *dmirror_devmem_alloc_page(struct dmirror *dmirror,
> - bool is_large)
> + bool is_large, bool *empty)
> {
> struct page *dpage = NULL;
> struct page *rpage = NULL;
> @@ -825,8 +839,8 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror *dmirror,
> spin_unlock(&mdevice->lock);
> } else {
> spin_unlock(&mdevice->lock);
> - if (dmirror_allocate_chunk(mdevice, &dpage, is_large))
> - goto error;
> + *empty = true;
> + goto error;
> }
>
> if (rpage && order)
> @@ -845,7 +859,8 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror *dmirror,
> }
>
> static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
> - struct dmirror *dmirror)
> + struct dmirror *dmirror,
> + unsigned int *need_chunk)
> {
> const unsigned long *src = args->src;
> unsigned long *dst = args->dst;
> @@ -858,6 +873,7 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
> bool is_large = *src & MIGRATE_PFN_COMPOUND;
> int write = (*src & MIGRATE_PFN_WRITE) ? MIGRATE_PFN_WRITE : 0;
> unsigned long nr = 1;
> + bool fail_alloc = false;
>
> if (!(*src & MIGRATE_PFN_MIGRATE))
> goto next;
> @@ -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;
> + }
> + }
>
> 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;
> rpage = BACKING_PAGE(dpage);
> rpage->zone_device_data = dmirror;
>
> @@ -1307,6 +1348,7 @@ static int dmirror_migrate_to_device(struct dmirror *dmirror,
> int ret;
> unsigned long *src_pfns = NULL;
> unsigned long *dst_pfns = NULL;
> + unsigned int need_chunk;
>
> start = cmd->addr;
> end = start + size;
> @@ -1347,10 +1389,24 @@ static int dmirror_migrate_to_device(struct dmirror *dmirror,
> goto out;
>
> pr_debug("Migrating from sys mem to device mem\n");
> - dmirror_migrate_alloc_and_copy(&args, dmirror);
> + need_chunk = 0;
> + dmirror_migrate_alloc_and_copy(&args, dmirror, &need_chunk);
> migrate_vma_pages(&args);
> dmirror_migrate_finalize_and_map(&args, dmirror);
> migrate_vma_finalize(&args);
> +
> + /*
> + * Out of free device pages: allocate a chunk without holding
> + * mmap_lock and retry the rest of this range.
> + */
> + if (need_chunk) {
> + mmap_read_unlock(mm);
> + if (dmirror_allocate_chunks(dmirror->mdevice, need_chunk))
> + need_chunk = 0;
> + mmap_read_lock(mm);
> + if (need_chunk)
> + next = addr;
> + }
> }
> mmap_read_unlock(mm);
> mmput(mm);
> @@ -1891,7 +1947,7 @@ static int dmirror_device_init(struct dmirror_device *mdevice, int id)
> goto put_device;
>
> /* Build a list of free ZONE_DEVICE struct pages */
> - ret = dmirror_allocate_chunk(mdevice, NULL, false);
> + ret = dmirror_allocate_chunk(mdevice, false);
> if (ret)
> goto put_device;
>
> --
> 2.43.0
>
>
>
prev parent reply other threads:[~2026-12-02 23:07 UTC|newest]
Thread overview: 2+ 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 [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=axCkSIvcBFd9qMoy@nvdebian.thelocal \
--to=apopple@nvidia.com \
--cc=a.mokrushin@ideco.ru \
--cc=akpm@linux-foundation.org \
--cc=jgg@ziepe.ca \
--cc=leon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lvc-project@linuxtesting.org \
/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