From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=51.250.56.165 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796192152; b=EJm2owaOFMStq5LYEwDOdGxsPf42MEj4SlOT4iz0nK8j6puahdQtDUd088VYYftWGDKe KHqjCUuFu6A2v4cSZOXasvm/p+6CaYfSf2fT0xV/SFJ8LxL4+u4BvYewwDo2hzRa9RflL BE0GJoCuake3k0FDv4zRp+MZj+ZAa+MxKP3JTGMltyDXCSqAji5cI49eaKlcUrzL2O++y RZoLd1bScakDERnhAA4AiTG4WGDJuClUeCCXoPXdOsvZieRewEAZy5J0Rx/sILqCPySnW Wmq9/RBl7OQVJ/nGW0hLESVAILzOCmRPBPK2fpAy55h4EZXNDDvAeV2R25TNf2RS7iA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796192152; h=DMARC-Filter:Received:Received:DKIM-Filter:DKIM-Signature:From:To:Cc: Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=daltShBgqGtPUN1zcpASHg6rh13j3BzrjQXeuZM2uvo=; b=w8ZI6HUjcsDhm4eG17WgGyAM/MqWCO+mIh5IvpKd5KcasIOwgDSms0/x1bughCs9zUSv URQhyu5tIqUrkfFvURHK90yRnYgar4FF/VxCQke+LHfu/DpUCy6y/xrqmLVZDvpKBWvcK SmeDF1B3HrzGeeWVVzVh9Y7O6EM7zvPw97EVpxFdx5K8olol6TJCdu7lm8F/Onyx9pXt4 Qb4F5e4KtIfZQrx4Cyuuj0P+ISoXiKReJRfMBmNofn6vOa/ftMvXN1jT+TmK4yDNRG910 GCYCTzzZefvgWFTgNSxTOcXQEOrPrTF4AMYv+zUhWvUynSmHjar7vzhWkcb/mDxtBWA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=ideco.ru; spf=pass smtp.mailfrom=ideco.ru; dkim=pass header.d=ideco.ru header.i=@ideco.ru header.a=rsa-sha256 header.s=ics header.b=pNTPwakb; arc=none smtp.remote-ip=51.250.56.165 Received: from smtp.ideco.ru (smtp.ideco.ru [51.250.56.165]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C39A5606A31A for ; Wed, 2 Dec 2026 06:15:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C39A5606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ideco.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ideco.ru Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ideco.ru header.i=@ideco.ru header.a=rsa-sha256 header.s=ics header.b=pNTPwakb Received: from [169.254.254.254] (localhost [127.0.0.1]) by smtp.ideco.ru (Postfix) with ESMTP id 273EA2021579; Wed, 02 Dec 2026 11:15:43 +0500 (+05) Received: from home.mcr.net (unknown [5.165.224.75]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp.ideco.ru (Postfix) with ESMTPSA id EEDB2202031D; Wed, 02 Dec 2026 11:15:40 +0500 (+05) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.ideco.ru EEDB2202031D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ideco.ru; s=ics; t=1796192142; bh=daltShBgqGtPUN1zcpASHg6rh13j3BzrjQXeuZM2uvo=; h=From:To:Cc:Subject:Date; b=pNTPwakbKEwzhki+T0y+CuvsfYko9S2ddZwzBXCZT0wlfIqLPgAYJO/L2tkW0gRwf nf9AgHWhsJCZJMuk41mRvynWCH0d/RAbsR+Iz8oO0SzslnJ5wm6K35013HOJ2Mbeoa 8FhkzI6I5iguhQent7fEUY9IySIHB7WFTxILEm4aF2drt6ISRMuQqbIcTpRiZNO5ZD HK1VXSMZsu6aYNhnWq9Fkr5qJ/rRXol1/BYIvBpUhTIg6R6ftCSTICag8PdQgPvsG0 2XSyxVP0xrr/dPMRWoDl61WxyV589jJKB+cLblCpom5cAzb5kNaIi/hhEzd5WKWepw e3VF3QsqCJqtA== From: Andrey Mokrushin To: Andrew Morton , Jason Gunthorpe , Leon Romanovsky Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, lvc-project@linuxtesting.org Subject: [PATCH] lib/test_hmm: don't allocate device memory chunks under mmap_lock Date: Wed, 2 Dec 2026 11:15:35 +0500 Message-ID: <20261202061535.38176-1-a.mokrushin@ideco.ru> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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. 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