From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 33318606A685 for ; Fri, 4 Dec 2026 19:29:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=hC4PPqwL Received: by kanga.kvack.org (Postfix) id 39EE16B00EF; Fri, 4 Dec 2026 14:29:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 34F5A6B05B1; Fri, 4 Dec 2026 14:29:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 23F386B05B2; Fri, 4 Dec 2026 14:29:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CD3656B00EF for ; Fri, 4 Dec 2026 14:29:18 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5819312057F for ; Fri, 4 Dec 2026 19:29:18 +0000 (UTC) X-FDA: 85507289196.09.CAE9696 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) by imf11.hostedemail.com (Postfix) with ESMTP id 9C77840007 for ; Fri, 4 Dec 2026 19:29:16 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b=hC4PPqwL; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3ihQTawUKCF8BIHFKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--elkin.bounces.google.com designates 74.125.82.70 as permitted sender) smtp.mailfrom=3ihQTawUKCF8BIHFKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--elkin.bounces.google.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796412556; b=2rn6Lvwlwtsxt91rvWGv/DK5hj+wRZ+OXP+FbZ10UK3cjxamaOqf7/nz39qhyszJ66/JoX bkiX48iIIj78UUHPWFWMxTBKWULSxvVCKXzTlTB2MyEM0pcZp4Raayalv22up5G+gwPaZw wQc9ISQ8rZban0vWIbnmxgsd7QoQY2o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796412556; 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-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=JDdTxYRAAER7JeY9D1cAdb6jiz83Jp6cSxpJGIKKYZg=; b=8oya5PiAPiHEJNVJ+RW1sPCmnKjAbf+o1SFJ2Lhs7hR92iKkjH+lODyM/b8IBmPSn63HhE ip3/mG86zJPzpeYPtCl97tZQqLW+2zI8KRsocRlL5qPUQTHoHarvaVZF3sjrTntWXZ3aDV 7bCwb4mwcCm6ZmdtVD8/ZUYrKO1+rhI= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b=hC4PPqwL; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3ihQTawUKCF8BIHFKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--elkin.bounces.google.com designates 74.125.82.70 as permitted sender) smtp.mailfrom=3ihQTawUKCF8BIHFKDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--elkin.bounces.google.com Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-1b5d5f27b91so1996300c88.1 for ; Fri, 04 Dec 2026 11:29:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796412555; x=1797017355; darn=kvack.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=JDdTxYRAAER7JeY9D1cAdb6jiz83Jp6cSxpJGIKKYZg=; b=hC4PPqwLPn6KBr/0Lt1il9esbLhPu3fo5Lrbiyafz7uJggchMMmiZ8xrIY78MN6eoW hO82t8pa6XTR0BnpR/Ju4klOvuPAuckArXGGZDeqpojgEteMeJfLyE/RbXsD3lwbPsIg pqptZYe1eyjno8f5aikUhDcCQbG8AHWk9FxHJ+88KfvKqxJ4qbFDoQvWGVbIUVeIQ34n /oNWldayTKLsexdR75MO6RKF0ES7BMEeLNoXDlhzY/WcKeIPbAG4DlutzJFdtQpbkYFc yBHjlfFFlEBpljENj8F3h6BCfA90+VFhYlI1vp+TwNuOHhBMmn4xtmNWMOhL1LFPT47v 6Piw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796412555; x=1797017355; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=JDdTxYRAAER7JeY9D1cAdb6jiz83Jp6cSxpJGIKKYZg=; b=SdP+z0LRsNTg5W+ovexXerbAqau4aWVrcpxVA8UvoNY41BD/L4SLffs72W4DhbzN1E nq/PSu9p/fh9ABxhyb23f9m3piz21wrQklRPQHreCCnjvFhjdTagSrSBXeyxOZEg/339 rfSAskLktJtNgo6tPsbhW++jRCoRfnsx2rCfrcLsZq61co1o3HumWz0WpZH2+6eRRO+6 tTH/RxnlaWmwJfEAzL2kB5s2chErDSHYUnitl2cROjr6xmYjWqJSNWCFInXIK96e9Mi0 7fndE3Vbx3Ts7fqCpnzjDpFJWH8lqpBZQfnZI6l6VGj+ZQR0PYVJ/22twRAwkwXtx2VA l+6Q== X-Forwarded-Encrypted: i=1; AL+hErET520rFUiOnYkJ7DDH++7DvadwkGgS1L+FKFPn/4RsGMn1qvVE6LEqn9vRHlhx30LQGHd1UrFoPg==@kvack.org X-Gm-Message-State: APuU4Z8/fEbkvkG3rP2vd7ZzXhL4VIWy7KeqnQPehvxHgBFvPRoYE65o ItpDGVynOd3jbXMKbCR84Jyw4xgIBYvv8rgKQ24o2meyasZZSPEkk22c4RQSfTsCZpsFcXfYQEW IDQ== X-Received: from dlbvv2.prod.google.com ([2002:a05:7022:5f02:b0:1b5:ca58:64cc]) (user=elkin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:1113:b0:15a:4155:3d9d with SMTP id a92af1059eb24-1b5d71202b6mr2753182c88.31.1796412554854; Fri, 04 Dec 2026 11:29:14 -0800 (PST) Date: Fri, 4 Dec 2026 19:29:12 +0000 X-Archived-By: Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.987.g2ec0ab4260-goog Message-ID: <20261204192912.513546-1-elkin@google.com> Subject: [PATCH v5] mm/migrate_device: consolidate compound folio handling From: Elkin Cruz To: David Hildenbrand , Andrew Morton Cc: Zi Yan , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , linux-mm@kvack.org, Lorenzo Stoakes , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, irogers@google.com, Hui Su , rppt@kernel.org, vbabka@kernel.org, Balbir Singh , Rakie Kim , Elkin Cruz Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: wa63oytzr9hagqm7nhktwnhw667yw3dq X-Spam-Status: No, score=-26.5 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, 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,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 9C77840007 X-HE-Tag: 1796412556-84619 X-HE-Meta: U2FsdGVkX18Zc4AnUpuW8EryTsR4/e+e9U3ImRcbNEU9iy81E5UjmN8KHZAYG5a+7uyJE6Z4osPo3c1ZrLERet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k4zj6LVr2Z0EaT4iNrh2a1GfM0pk0ugIMhAG4fx0sUVqHray5bAAvkg2Ir3ihP0tv1RqYHe4WtvxEpajHZCONyzERaE0HttePjmbR14CBahl+oYQW4bq+OjYj7L+cJtG5IkmFAEI28UVqGq8sHPXROr0XjeqYAWK04oecIzju9laISm2gR3urJ5cIWdQ6pxMw1qNSkFWRUZ5ciUT0NdKX1RQZrH++FFj6c1ZK9mq4ww7qhSOyRQtHedRNFmytqxhpEpt9J4BVtgxqkyHH3VQl6Bvb4lo2JuClAO/81zU8H1diAVGi2GUT0/3TDQUviR92TPYP6clYaG7EtWDTJkJRvzZoiCuqxe4Gy6ZHLsmdaUibTea/jt+1fVEE+aIc2YhvyGiBMxJAZSJu1BV6BSv6rqroyFLoACbec7MAjhOBSpczOxN2pGtMBlOs3ySMaM8vy6hWitmsWMWP3AcMm4xypwQwZFYiSIDXyr2lrqm9FxeBGuAo70nbFxrIDT/4d0dUjm9+GKXlg+ACMapSi84K94Fh6lW9BajLfSuLWGoyywdcbbMieLrI7GgoRh66QIbqrMuqpeBniTaYV0pLDlXPTms1JPELJhRD+ojR7fo4lrP5x84gH025eYk4oR5nhUDgxikYkAV43UGNGRh4BtHSpnFKxmpuotTDuTAbHTPgBSfVygqPY7WZTGU80qOhymlELzlJbAWxXGRosndoeHSKMDvpXlBeUPpA3NpmqzbliICMQ= 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 From: Hui Su Commit dc41e961a269 ("mm/migrate_device: avoid out-of-bounds writes for compound folios") added handling for compound folios that do not fit in the remaining PFN array. migrate_device_range() and migrate_device_pfns() duplicate the logic for locking device PFNs, encoding compound folios, and handling this boundary condition. A compound folio cannot be represented partially for migration. Use WARN_ON_ONCE() when one does not fit in the remaining PFN array, while retaining the defensive handling: release the current folio's lock and reference, clear the remaining entries, and stop collecting. Move the shared collection and encoding logic into a helper so both interfaces handle compound folios consistently. Keep the reference, lock, size lookup, and PFN encoding together so cleanup acts on the folio whose reference and lock were acquired. Also use memset() for compound folio tail entries instead of open-coding the clearing loop. If a PFN cannot be referenced or its folio cannot be locked, leave that source entry clear and continue at page granularity. Compound tail pages have a zero page->_refcount, so folio_get_nontail_page() cannot acquire them. They are therefore skipped one slot at a time. This preserves collection of later allocated pages in a device chunk containing free PFNs. Document that an encountered compound folio must fit entirely in the remaining range or PFN array. Link: https://lore.kernel.org/r/c99ca53a-73ef-4a0c-8738-eba1cc89bea2@kernel.org Link: https://lore.kernel.org/r/60634ab5-3fcc-42fb-8722-4ba6771acfda@kernel.org Suggested-by: David Hildenbrand Signed-off-by: Hui Su [elkin@google.com: rebase onto next-20261204 to retain pin_anon_rmap()] Signed-off-by: Elkin Cruz --- Changes in v5 (refreshed by Elkin Cruz): - Rebase Hui Su's v4 onto next-20261204 as requested by David Hildenbrand. - Retain pin_anon_rmap() from commit 11990871862e ("mm/migrate_device: fix use-after-free of the anon rmap") inside migrate_device_collect_folio(), checking WARN_ON_ONCE(nr > remaining) before pin_anon_rmap() to avoid an unnecessary pin/unpin on truncated compound folios. Testing on v5: - W=1 compile-tested (mm/migrate_device.o) against next-20261204 only. No KUnit tests were run. - Disclosure: I had AI assistance for this effort. I checked that the code seemed sensible and adapted well from Hui's v4. It was compile-tested against next-20261204. v4 (Hui Su): https://lore.kernel.org/linux-mm/20260924041210.13d04b8be24495fc593665283d2e4453d449502f@163.com/ Related thread (withdrawn): https://lore.kernel.org/r/20261125222713.1450385-1-elkin@google.com Prior v4 testing note (Hui Su, on x86_64 with KASAN): - 7/7 private-device HMM THP cases passed on the exact v4 tree. - Local KUnit validation covered success, get/trylock failure, and truncated compound-folio paths. Changes in v4: - Fold migrate_device_pfn_lock() into migrate_device_collect_folio() so the folio whose reference and lock are acquired is also used for size and cleanup decisions. - When a PFN cannot be referenced or its folio cannot be locked, leave only the current entry clear and continue at page granularity. Compound tails have a zero page->_refcount, so folio_get_nontail_page() cannot acquire them. - Use WARN_ON_ONCE() with defensive cleanup when a compound folio does not fit in the remaining array, as suggested by David Hildenbrand. - Document that a compound folio occupies consecutive page-granular entries and must fit entirely in the remaining PFN array. Changes in v3: - Keep the source entry zero when migrate_device_pfn_lock() fails instead of setting MIGRATE_PFN_COMPOUND, while still consuming and clearing all slots belonging to the compound folio. - Use VM_WARN_ON_ONCE() for the truncated compound-folio invariant while keeping defensive cleanup independent of CONFIG_DEBUG_VM, as suggested by Balbir Singh. Changes in v2: - Fix the helper parameter indentation and keep the declaration to two lines, as suggested by David Hildenbrand. base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9 mm/migrate_device.c | 101 +++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 49 deletions(-) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index a953e7acbea6..4e9de8160cb0 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -1416,27 +1416,55 @@ void migrate_vma_finalize(struct migrate_vma *migrate) } EXPORT_SYMBOL(migrate_vma_finalize); -static unsigned long migrate_device_pfn_lock(unsigned long pfn) +/* + * Collect a device folio into the page-granular PFN array. + * + * Return the number of entries consumed. Return 1 with a clear source entry + * if the current PFN cannot be referenced, locked, or have its anonymous + * rmap pinned. Return 0 if a compound folio does not fit in the remaining + * array and collection should stop. + */ +static unsigned int migrate_device_collect_folio(unsigned long *src_pfn, + unsigned long pfn, unsigned long remaining) { - unsigned long src_pfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; + unsigned long entry = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; struct folio *folio; + unsigned int nr; + + *src_pfn = 0; folio = folio_get_nontail_page(pfn_to_page(pfn)); if (!folio) - return 0; + return 1; if (!folio_trylock(folio)) { folio_put(folio); - return 0; + return 1; } - if (!pin_anon_rmap(folio, &src_pfn)) { + nr = folio_nr_pages(folio); + + if (WARN_ON_ONCE(nr > remaining)) { folio_unlock(folio); folio_put(folio); + memset(src_pfn, 0, remaining * sizeof(*src_pfn)); return 0; } - return src_pfn; + if (!pin_anon_rmap(folio, &entry)) { + folio_unlock(folio); + folio_put(folio); + return 1; + } + + *src_pfn = entry; + + if (nr > 1) { + *src_pfn |= MIGRATE_PFN_COMPOUND; + memset(src_pfn + 1, 0, (nr - 1) * sizeof(*src_pfn)); + } + + return nr; } /** @@ -1457,36 +1485,22 @@ static unsigned long migrate_device_pfn_lock(unsigned long pfn) * migrating pages that aren't free before unmapping them. Drivers may then * allocate destination pages and start copying data from the device to CPU * memory before calling migrate_device_pages(). + * + * A compound folio must fit entirely in the remaining range. */ int migrate_device_range(unsigned long *src_pfns, unsigned long start, unsigned long npages) { - unsigned long i, j, pfn; + unsigned long i, pfn; for (pfn = start, i = 0; i < npages; pfn++, i++) { - struct page *page = pfn_to_page(pfn); - struct folio *folio = page_folio(page); - unsigned int nr = 1; + unsigned int nr; - src_pfns[i] = migrate_device_pfn_lock(pfn); - nr = folio_nr_pages(folio); - if (nr > npages - i) { - if (src_pfns[i] & MIGRATE_PFN_MIGRATE) { - unpin_anon_rmap(folio, src_pfns[i]); - folio_unlock(folio); - folio_put(folio); - } - memset(&src_pfns[i], 0, - (npages - i) * sizeof(*src_pfns)); + nr = migrate_device_collect_folio(&src_pfns[i], pfn, npages - i); + if (!nr) break; - } - if (nr > 1) { - src_pfns[i] |= MIGRATE_PFN_COMPOUND; - for (j = 1; j < nr; j++) - src_pfns[i+j] = 0; - i += j - 1; - pfn += j - 1; - } + i += nr - 1; + pfn += nr - 1; } migrate_device_unmap(src_pfns, npages, NULL); @@ -1502,34 +1516,23 @@ EXPORT_SYMBOL(migrate_device_range); * * Similar to migrate_device_range() but supports non-contiguous pre-populated * array of device pages to migrate. + * + * Entries for different folios may be non-contiguous, but a compound folio + * must occupy consecutive page-granular entries and fit entirely in the + * remaining PFN array. */ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) { - unsigned long i, j; + unsigned long i; for (i = 0; i < npages; i++) { - struct page *page = pfn_to_page(src_pfns[i]); - struct folio *folio = page_folio(page); - unsigned int nr = 1; + unsigned long pfn = src_pfns[i]; + unsigned int nr; - src_pfns[i] = migrate_device_pfn_lock(src_pfns[i]); - nr = folio_nr_pages(folio); - if (nr > npages - i) { - if (src_pfns[i] & MIGRATE_PFN_MIGRATE) { - unpin_anon_rmap(folio, src_pfns[i]); - folio_unlock(folio); - folio_put(folio); - } - memset(&src_pfns[i], 0, - (npages - i) * sizeof(*src_pfns)); + nr = migrate_device_collect_folio(&src_pfns[i], pfn, npages - i); + if (!nr) break; - } - if (nr > 1) { - src_pfns[i] |= MIGRATE_PFN_COMPOUND; - for (j = 1; j < nr; j++) - src_pfns[i+j] = 0; - i += j - 1; - } + i += nr - 1; } migrate_device_unmap(src_pfns, npages, NULL); -- 2.56.1.987.g2ec0ab4260-goog