Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 80194606A323 for ; Wed, 2 Dec 2026 06:15:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=ideco.ru header.i=@ideco.ru header.a=rsa-sha256 header.s=ics header.b=pNTPwakb Received: by kanga.kvack.org (Postfix) id A43AE6B060F; Wed, 2 Dec 2026 01:15:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A1B596B0615; Wed, 2 Dec 2026 01:15:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9327A6B0619; Wed, 2 Dec 2026 01:15:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 72AD96B060F for ; Wed, 2 Dec 2026 01:15:48 -0500 (EST) Received: from smtpin25.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6A5F71401D9 for ; Wed, 2 Dec 2026 06:15:47 +0000 (UTC) X-FDA: 85498031934.25.167D3CB Received: from smtp.ideco.ru (smtp.ideco.ru [51.250.56.165]) by imf27.hostedemail.com (Postfix) with ESMTP id 50A6B40004 for ; Wed, 2 Dec 2026 06:15:45 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=ideco.ru header.s=ics header.b=pNTPwakb; dmarc=pass (policy=reject) header.from=ideco.ru; spf=pass (imf27.hostedemail.com: domain of a.mokrushin@ideco.ru designates 51.250.56.165 as permitted sender) smtp.mailfrom=a.mokrushin@ideco.ru ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796192146; b=fJZdqBL2Bm12BBus9Id77D2SHWAAxf6thWNQR1sn3tEQJ1npjGBZ3E9nAfAPRPoRom0Wnn vo/fXj570GQqZEdiySkZd9xv7NK3s4IYepwL6jRcvG7BkYUEzPAaECoO1usgiixsH9XOha 2xTAmXnMkzTrIxf/lLQ69XIIihIgQwM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796192146; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=daltShBgqGtPUN1zcpASHg6rh13j3BzrjQXeuZM2uvo=; b=Hjqa7r4BOa6xTqkV935Y6V3E0MaM+TY9sVOYR9D0eHfVw5xOoXaildJgf6gR5ITZ5vtp6X CNSD4UEXE7sFS8bh71ghk7HahMxJkJBjui0BYAkh9dWOiihrJLiiLFOoLvI8be2FfTF89L GhuRH7+mBk3icMeXcnQnKj/SkcBEQIQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=ideco.ru header.s=ics header.b=pNTPwakb; dmarc=pass (policy=reject) header.from=ideco.ru; spf=pass (imf27.hostedemail.com: domain of a.mokrushin@ideco.ru designates 51.250.56.165 as permitted sender) smtp.mailfrom=a.mokrushin@ideco.ru 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: jkxi6fipqy6iyq7npq8dfajtyquyuxd9 X-Rspam-User: X-Rspamd-Queue-Id: 50A6B40004 X-Rspamd-Server: rspam12 X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796192145-127991 X-HE-Meta: U2FsdGVkX1/Wo7LTEaxPJtbHwaEKXqwPMjWGtnuRTdKN22Jt+qXXaRb0dkBGzojz/fZ+/OM3OWgdWhiGt7POGkbbx4NavKN1P+XkOMsMUOmOKFLmeOzXHelqcE1Tv936BzdW6tjbGiItci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uvjaokIjNOa8ZRWZ5iMx+nxDV8Sb04dYsUknXR8RiH9kM3enTuCRA++kN28yBluKZrL+e+OXCtt2YZt5704cQ622z7jTAZ4vv2alBImt9j9TXOFIuTGbzOIn7qXL/mGf8KIVft3kOzILIlR60cbIzmY7ZafQEC9UyRpmDEdi7PuJ7jEB6Z1MsDvzs6GLmVEKxuv0YKjcOzmlg6oi+SbLP0Z+su28XZp/Zbv4F/bUnNiSLklYj7RoxJSOA/7e+zKPiMyzenT2fJ7JZALbANgM9o1J3f3hlSuiD4K6/KglTassF29Q2MjUI38eksDWtnk1TiFoJHUuFrXPVPjYoHkPtKMe4Du/OTf9lnjbeths7ptxRujSl0kdUa2tg31fgBglAA Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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