From: Elkin Cruz <elkin@google.com>
To: David Hildenbrand <david@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Zi Yan <ziy@nvidia.com>, Matthew Brost <matthew.brost@intel.com>,
Joshua Hahn <joshua.hahnjy@gmail.com>,
Byungchul Park <byungchul@sk.com>,
Gregory Price <gourry@gourry.net>,
Ying Huang <ying.huang@linux.alibaba.com>,
Alistair Popple <apopple@nvidia.com>,
linux-mm@kvack.org, Lorenzo Stoakes <ljs@kernel.org>,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
irogers@google.com, Elkin Cruz <elkin@google.com>,
stable@vger.kernel.org
Subject: [PATCH] mm/migrate_device: use locked folio in migrate_device_{range,pfns}()
Date: Wed, 25 Nov 2026 22:27:13 +0000 [thread overview]
Message-ID: <20261125222713.1450385-1-elkin@google.com> (raw)
migrate_device_range() and migrate_device_pfns() read a speculative
folio pointer via page_folio() prior to taking a reference or lock in
migrate_device_pfn_lock(). If the page splits or changes compound state
before lock acquisition, folio_nr_pages(folio) inspects stale state and
the nr > npages - i error path calls unpin_anon_rmap(), folio_unlock(),
and folio_put() on the unowned speculative pointer while leaking the
reference and lock on the folio actually locked by
migrate_device_pfn_lock().
Return the referenced, locked folio directly from
migrate_device_pfn_lock() and skip unlocked entries cleanly.
Fixes: dc41e961a269 ("mm/migrate_device: avoid out-of-bounds writes for compound folios")
Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
Cc: stable@vger.kernel.org
Signed-off-by: Elkin Cruz <elkin@google.com>
---
I am trying out new Clang static analysis checks for kernel bug
classes, and they flagged this code as possibly buggy. The findings
were triaged/fixed and commit descriptions were drafted with help of AI,
then checked against linux-next (not runtime-tested on hardware).
Thanks,
Elkin
mm/migrate_device.c | 41 ++++++++++++++++++++++-------------------
1 file changed, 22 insertions(+), 19 deletions(-)
diff --git a/mm/migrate_device.c b/mm/migrate_device.c
index a953e7acbea6..a599d5baa340 100644
--- a/mm/migrate_device.c
+++ b/mm/migrate_device.c
@@ -1416,11 +1416,13 @@ void migrate_vma_finalize(struct migrate_vma *migrate)
}
EXPORT_SYMBOL(migrate_vma_finalize);
-static unsigned long migrate_device_pfn_lock(unsigned long pfn)
+static unsigned long migrate_device_pfn_lock(unsigned long pfn,
+ struct folio **foliop)
{
unsigned long src_pfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
struct folio *folio;
+ *foliop = NULL;
folio = folio_get_nontail_page(pfn_to_page(pfn));
if (!folio)
return 0;
@@ -1436,6 +1438,7 @@ static unsigned long migrate_device_pfn_lock(unsigned long pfn)
return 0;
}
+ *foliop = folio;
return src_pfn;
}
@@ -1464,18 +1467,18 @@ int migrate_device_range(unsigned long *src_pfns, unsigned long start,
unsigned long i, j, 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;
+ struct folio *folio;
+ unsigned int nr;
+
+ src_pfns[i] = migrate_device_pfn_lock(pfn, &folio);
+ if (!folio)
+ continue;
- 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);
- }
+ unpin_anon_rmap(folio, src_pfns[i]);
+ folio_unlock(folio);
+ folio_put(folio);
memset(&src_pfns[i], 0,
(npages - i) * sizeof(*src_pfns));
break;
@@ -1508,18 +1511,18 @@ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages)
unsigned long i, j;
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;
+ struct folio *folio;
+ unsigned int nr;
+
+ src_pfns[i] = migrate_device_pfn_lock(src_pfns[i], &folio);
+ if (!folio)
+ continue;
- 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);
- }
+ unpin_anon_rmap(folio, src_pfns[i]);
+ folio_unlock(folio);
+ folio_put(folio);
memset(&src_pfns[i], 0,
(npages - i) * sizeof(*src_pfns));
break;
--
2.56.1.839.g22a8290db9-goog
next reply other threads:[~2026-11-25 22:27 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 22:27 Elkin Cruz [this message]
2026-11-25 22:41 ` [PATCH] mm/migrate_device: use locked folio in migrate_device_{range,pfns}() David Hildenbrand (Arm)
2026-11-25 22:58 ` Elkin Cruz
2026-11-26 11:31 ` David Hildenbrand (Arm)
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=20261125222713.1450385-1-elkin@google.com \
--to=elkin@google.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=byungchul@sk.com \
--cc=david@kernel.org \
--cc=gourry@gourry.net \
--cc=irogers@google.com \
--cc=joshua.hahnjy@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=llvm@lists.linux.dev \
--cc=matthew.brost@intel.com \
--cc=stable@vger.kernel.org \
--cc=ying.huang@linux.alibaba.com \
--cc=ziy@nvidia.com \
/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.