Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm: compaction: skip DMA-pinned folios during isolation
@ 2026-12-01 15:46 Jialin Wang
  2026-12-01 17:48 ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 2+ messages in thread
From: Jialin Wang @ 2026-12-01 15:46 UTC (permalink / raw)
  To: Andrew Morton, Vlastimil Babka, Johannes Weiner
  Cc: Suren Baghdasaryan, Michal Hocko, Brendan Jackman, Zi Yan,
	David Hildenbrand, Khalid Aziz, linux-mm, linux-kernel,
	Jialin Wang

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



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 17:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 15:46 [PATCH] mm: compaction: skip DMA-pinned folios during isolation Jialin Wang
2026-12-01 17:48 ` David Hildenbrand (Arm)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox