From: Jialin Wang <wjl.linux@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Vlastimil Babka <vbabka@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>
Cc: Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>,
Brendan Jackman <brendan.jackman@linux.dev>,
Zi Yan <ziy@nvidia.com>, David Hildenbrand <david@kernel.org>,
Khalid Aziz <khalid@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Jialin Wang <wjl.linux@gmail.com>
Subject: [PATCH] mm: compaction: skip DMA-pinned folios during isolation
Date: Tue, 1 Dec 2026 23:46:06 +0800 [thread overview]
Message-ID: <20261201154606.548937-1-wjl.linux@gmail.com> (raw)
Migration fails while a folio is pinned, so isolating it during compaction
wastes CPU. Currently, isolate_migratepages_block() only checks for extra
references when folio_mapping() is NULL (anonymous folios not in the
swapcache). Folios with a mapping are not checked, including:
- shmem folios allocated by cudaHostAlloc() via
mmap(MAP_SHARED | MAP_ANONYMOUS) and pinned by the NVIDIA driver
with FOLL_PIN | FOLL_LONGTERM;
- shmem/memfd folios pinned by VFIO with FOLL_PIN | FOLL_LONGTERM [1];
- other folios that can be long-term DMA-pinned, such as swapcache
folios, ramfs folios, and file-backed folios pinned read-only
(without FOLL_WRITE).
During direct compaction and kcompactd runs, compaction repeatedly
isolates these pinned folios, unmaps them via rmap, flushes TLBs, fails
migration, and restores the mappings. On GPU hosts with lots of pinned
memory and heavy high-order allocations, this makes direct compaction
very slow and keeps kcompactd at 100% CPU for long periods.
Skip these folios early in isolate_migratepages_block(). To avoid
hurting other workloads:
- Do not change cc->alloc_contig (such as CMA and virtio-mem).
- Only target FOLL_PIN, not every extra reference (such as FOLL_GET or
short-lived pagecache references). Checking for any extra refcount
could skip normal pagecache folios during read(), or folios with extra
references that can still migrate (such as AIO ring folios).
- Add a helper folio_likely_dma_pinned() instead of calling
folio_maybe_dma_pinned() directly. For folios without _pincount, it
uses folio_expected_ref_count() so a folio mapped many times (around
GUP_PIN_COUNTING_BIAS, i.e., 1024) is not always treated as pinned.
- Check folio_is_longterm_pinnable() so we do not skip folios in areas
where long-term pinning is not allowed (such as ZONE_MOVABLE and
MIGRATE_CMA). Any pin there must be short-term.
Also move the !folio_test_lru() check right after
folio_get_nontail_page(). It makes sure folio_mapping(),
folio_likely_dma_pinned() (which may call folio_expected_ref_count()),
and the anonymous refcount check only run on LRU folios. If a page is
freed and reused as a non-LRU or typed page, we will not inspect its
non-LRU fields or risk hitting folio_expected_ref_count()'s
WARN_ON_ONCE().
Benchmark results (8GB VM, 4 vCPUs, 5120MB of 4KB shmem pages pinned with
FOLL_PIN | FOLL_LONGTERM, 50 x 2MB GFP_TRANSHUGE allocations per trial,
with fragments memory, mean over 3 trials) [2]:
Metric Before After Delta
alloc_ms 38134.772 ms 961.924 ms -97.48% (39.64x)
compact_migrate_scanned 175,671,741 130,391,479 -25.78%
compact_free_scanned 1,004,877 343,528 -65.81%
compact_isolated 61,953,531 32,272 -99.95%
pgmigrate_success 47,742 13,435 -71.86%
pgmigrate_fail 61,784,613 72 -99.99%
[1] https://lore.kernel.org/all/20230525191507.160076-1-khalid.aziz@oracle.com/
[2] https://github.com/hyuuko1/compact_dma_pin_bench
Signed-off-by: Jialin Wang <wjl.linux@gmail.com>
---
mm/compaction.c | 83 +++++++++++++++++++++++++++++++++++++++----------
1 file changed, 67 insertions(+), 16 deletions(-)
diff --git a/mm/compaction.c b/mm/compaction.c
index 1ceee7a6e46e..9afed3f25b9c 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -834,6 +834,44 @@ static bool skip_isolation_on_order(int order, int target_order)
return order >= pageblock_order;
}
+/**
+ * folio_likely_dma_pinned() - Check if a folio is likely DMA-pinned
+ * @folio: The folio
+ *
+ * Compared to folio_maybe_dma_pinned():
+ *
+ * - For folios without _pincount, this accounts for expected references to
+ * avoid permanent false positives when a folio is mapped
+ * >= GUP_PIN_COUNTING_BIAS times.
+ * - Because it reads refcount and mapcount without locks, concurrent page
+ * table mappings between the two reads can cause transient false negatives.
+ * Do not use it where zero false negatives are required for correctness.
+ *
+ * Context: Caller must hold a folio reference and ensure the folio is not
+ * a typed non-hugetlb page, or folio_expected_ref_count() will warn.
+ * Return: True if the folio is likely DMA-pinned, false otherwise.
+ */
+static bool folio_likely_dma_pinned(const struct folio *folio)
+{
+ unsigned int ref_count;
+
+ if (folio_has_pincount(folio))
+ return atomic_read(&folio->_pincount) > 0;
+
+ /* Fast check: definitely not DMA-pinned if refcount is below bias. */
+ ref_count = folio_ref_count(folio);
+ if (ref_count < GUP_PIN_COUNTING_BIAS)
+ return false;
+
+ /*
+ * Use half the bias as the threshold. This leaves room for the
+ * caller's own reference, short extra references, and concurrent
+ * map/unmap between the two reads.
+ */
+ return ref_count >=
+ folio_expected_ref_count(folio) + GUP_PIN_COUNTING_BIAS / 2;
+}
+
/**
* isolate_migratepages_block() - isolate all migrate-able pages within
* a single pageblock
@@ -1093,26 +1131,39 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
if (unlikely(!folio))
goto isolate_fail;
- /*
- * Migration will fail if an anonymous page is pinned in memory,
- * so avoid taking lru_lock and isolating it unnecessarily in an
- * admittedly racy check.
- */
- mapping = folio_mapping(folio);
- if (!mapping && (folio_ref_count(folio) - 1) > folio_mapcount(folio))
- goto isolate_fail_put;
-
- /*
- * Only allow to migrate anonymous pages in GFP_NOFS context
- * because those do not depend on fs locks.
- */
- if (!(cc->gfp_mask & __GFP_FS) && mapping)
- goto isolate_fail_put;
-
/* Only take pages on LRU: a check now makes later tests safe */
if (!folio_test_lru(folio))
goto isolate_fail_put;
+ mapping = folio_mapping(folio);
+ if (mapping) {
+ /*
+ * Only allow to migrate anonymous pages in GFP_NOFS
+ * context because those do not depend on fs locks.
+ */
+ if (!(cc->gfp_mask & __GFP_FS))
+ goto isolate_fail_put;
+
+ /*
+ * Skip DMA-pinned folios (such as VFIO or GPU-pinned
+ * shmem) because migration will fail. Leave
+ * alloc_contig unchanged, and do not skip folios in
+ * areas where long-term pinning is not allowed (any
+ * pin there must be short-term).
+ */
+ if (!cc->alloc_contig &&
+ folio_likely_dma_pinned(folio) &&
+ folio_is_longterm_pinnable(folio))
+ goto isolate_fail_put;
+ } else if ((folio_ref_count(folio) - 1) > folio_mapcount(folio)) {
+ /*
+ * Migration will fail if an anonymous page is pinned in
+ * memory, so avoid taking lru_lock and isolating it
+ * unnecessarily in an admittedly racy check.
+ */
+ goto isolate_fail_put;
+ }
+
is_unevictable = folio_test_unevictable(folio);
/* Compaction might skip unevictable pages but CMA takes them */
--
2.56.0
reply other threads:[~2026-12-01 15:46 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261201154606.548937-1-wjl.linux@gmail.com \
--to=wjl.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=brendan.jackman@linux.dev \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=khalid@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox