* [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; 6+ 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] 6+ messages in thread
* Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation
2026-12-01 15:46 [PATCH] mm: compaction: skip DMA-pinned folios during isolation Jialin Wang
@ 2026-12-01 17:48 ` David Hildenbrand (Arm)
2026-12-02 2:19 ` Harry Yoo
2026-12-02 15:31 ` Jialin Wang
0 siblings, 2 replies; 6+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-01 17:48 UTC (permalink / raw)
To: Jialin Wang, Andrew Morton, Vlastimil Babka, Johannes Weiner
Cc: Suren Baghdasaryan, Michal Hocko, Brendan Jackman, Zi Yan,
Khalid Aziz, linux-mm, linux-kernel
On 12/1/26 16:46, Jialin Wang wrote:
> 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]:
But what about short-term pins that are long gone until you actually try migrating?
--
Cheers,
David
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation
2026-12-01 17:48 ` David Hildenbrand (Arm)
@ 2026-12-02 2:19 ` Harry Yoo
2026-12-02 13:37 ` David Hildenbrand (Arm)
2026-12-02 15:34 ` Jialin Wang
2026-12-02 15:31 ` Jialin Wang
1 sibling, 2 replies; 6+ messages in thread
From: Harry Yoo @ 2026-12-02 2:19 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Jialin Wang, Andrew Morton, Vlastimil Babka, Johannes Weiner,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman, Zi Yan,
Khalid Aziz, linux-mm, linux-kernel, kas
On Tue, Dec 01, 2026 at 06:48:01PM +0100, David Hildenbrand (Arm) wrote:
> On 12/1/26 16:46, Jialin Wang wrote:
> > 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]:
>
> But what about short-term pins that are long gone until you actually try migrating?
Forcing all long-term pins to migrate pages to an unmovable
pageblock would probably help here?
Movable allocations won't try to isolate nonmovable pageblocks until
we're really desperate.
Separately from this issue, long-term pins shouldn't really cause
trouble for compaction even outside ZONE_MOVABLE :)
(The idea is originally from an independent off-list discussion with
Kiryl a while ago)
--
Cheers,
Harry / Hyeonggon
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation
2026-12-02 2:19 ` Harry Yoo
@ 2026-12-02 13:37 ` David Hildenbrand (Arm)
2026-12-02 15:34 ` Jialin Wang
1 sibling, 0 replies; 6+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-02 13:37 UTC (permalink / raw)
To: Harry Yoo
Cc: Jialin Wang, Andrew Morton, Vlastimil Babka, Johannes Weiner,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman, Zi Yan,
Khalid Aziz, linux-mm, linux-kernel, kas
On 12/2/26 03:19, Harry Yoo wrote:
> On Tue, Dec 01, 2026 at 06:48:01PM +0100, David Hildenbrand (Arm) wrote:
>> On 12/1/26 16:46, Jialin Wang wrote:
>>> 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]:
>>
>> But what about short-term pins that are long gone until you actually try migrating?
>
> Forcing all long-term pins to migrate pages to an unmovable
> pageblock would probably help here?
I recall discussing that a while ago at LSF/MM.
I think there were some concerns around added pinning latency and unpinning
quickly after (long-term just means "possibly unbounded" in practice, not
actually how long it will be).
Because once you unpin it would be misplaced.
--
Cheers,
David
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation
2026-12-01 17:48 ` David Hildenbrand (Arm)
2026-12-02 2:19 ` Harry Yoo
@ 2026-12-02 15:31 ` Jialin Wang
1 sibling, 0 replies; 6+ messages in thread
From: Jialin Wang @ 2026-12-02 15:31 UTC (permalink / raw)
To: david
Cc: akpm, brendan.jackman, hannes, khalid, linux-kernel, linux-mm,
mhocko, surenb, vbabka, wjl.linux, ziy
On 2026-12-01 18:48 +0100, David Hildenbrand (Arm) wrote:
> On 12/1/26 16:46, Jialin Wang wrote:
> > 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]:
>
> But what about short-term pins that are long gone until you actually try migrating?
Hi David,
Good point. Right now we have no way to tell short-term and long-term
pins apart on a folio, since FOLL_LONGTERM is not tracked after pinning.
So a short-term pinned folio would also be skipped here.
I think skipping a folio that is DMA-pinned at isolation time is still a
reasonable trade-off for regular compaction:
1. Regular compaction (direct compaction and kcompactd) is best-effort.
It mostly runs in MIGRATE_ASYNC or MIGRATE_SYNC_LIGHT mode with
ignore_skip_hint = false, and already skips some migratable folios
(such as writeback or dirty folios under ISOLATE_ASYNC_MIGRATE) to
avoid wasted work. Also, the existing anonymous folio check right
below already skips on any extra refcount, including short-term pins.
2. Unlike writeback (where MIGRATE_SYNC can wait in
folio_wait_writeback()), migrate_pages() does not wait for FOLL_PIN;
it only retries in a tight loop. Looking at the possible cases for a
pinned folio:
- If the pin lasts a long time (e.g., long-term pins or slower I/O),
skipping it is clearly better than isolating, unmapping, failing
all retries, and remapping.
- If the pin is short but happens very often on the same folio,
skipping it is also reasonable because migration is still likely to
fail.
- If the pin is short and rare, compaction is unlikely to hit it
often. Even if it gets skipped once, the pin will likely be gone
on the next scan, and compaction can just move on to other blocks.
Your question also made me realize I should probably limit this check to
(mode & ISOLATE_ASYNC_MIGRATE) (i.e., cc->mode != MIGRATE_SYNC), just
like the writeback check below. That way, direct compaction and
kcompactd still get the benefit, while full MIGRATE_SYNC compaction
(such as /proc/sys/vm/compact_memory) will still try to migrate
short-term pinned folios.
What do you think? Would adding (mode & ISOLATE_ASYNC_MIGRATE) (or
further limiting this to shmem/swapbacked folios) address your concern?
--
Best regards,
Jialin
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation
2026-12-02 2:19 ` Harry Yoo
2026-12-02 13:37 ` David Hildenbrand (Arm)
@ 2026-12-02 15:34 ` Jialin Wang
1 sibling, 0 replies; 6+ messages in thread
From: Jialin Wang @ 2026-12-02 15:34 UTC (permalink / raw)
To: harry
Cc: akpm, brendan.jackman, david, hannes, kas, khalid, linux-kernel,
linux-mm, mhocko, surenb, vbabka, wjl.linux, ziy
On 2026-12-02 02:19 +0000, Harry Yoo wrote:
> On Tue, Dec 01, 2026 at 06:48:01PM +0100, David Hildenbrand (Arm) wrote:
> > On 12/1/26 16:46, Jialin Wang wrote:
> > > 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]:
> >
> > But what about short-term pins that are long gone until you actually try migrating?
>
> Forcing all long-term pins to migrate pages to an unmovable
> pageblock would probably help here?
>
> Movable allocations won't try to isolate nonmovable pageblocks until
> we're really desperate.
>
> Separately from this issue, long-term pins shouldn't really cause
> trouble for compaction even outside ZONE_MOVABLE :)
>
> (The idea is originally from an independent off-list discussion with
> Kiryl a while ago)
Hi Harry,
Thanks for the idea! Moving long-term pinned pages into
MIGRATE_UNMOVABLE pageblocks makes sense for reducing fragmentation.
I tested a quick prototype of this idea. Here is what I saw in testing:
1. It does not fix the compaction problem.
After pinning 5120MB, folios were moved to MIGRATE_UNMOVABLE.
However, compaction still scans MIGRATE_UNMOVABLE
pageblocks in common paths because suitable_migration_source()
returns true when:
- cc->mode != MIGRATE_ASYNC (direct compaction switches to
MIGRATE_SYNC_LIGHT right after async fails), or
- !cc->direct_compaction (kcompactd, proactive compaction, and
/proc/sys/vm/compact_memory).
Because pinned shmem folios are still on the LRU, they still get
isolated and fail migration over and over:
- Direct compaction benchmark (50 x 2MB GFP_TRANSHUGE, 3-trial mean):
alloc_ms: 34,846 ms (vs 962 ms with my patch)
compact_isolated: 57,447,043 (vs 32,272 with my patch)
pgmigrate_fail: 57,343,328 (vs 72 with my patch)
- Proactive kcompactd (proactiveness=50 for 3s):
compact_isolated: 1,318,427
pgmigrate_fail: 1,309,575
- /proc/sys/vm/compact_memory (MIGRATE_SYNC):
time: 842 ms
pgmigrate_fail: 1,309,575
2. It adds large pin latency for already-faulted memory.
When pages are faulted in before pinning (common for apps that fill
a buffer and then register it with GPU/VFIO), they start in
MIGRATE_MOVABLE. Forcing migration skips gup_fast and copies every
page:
- 2048MB pre-faulted shmem (all in MIGRATE_MOVABLE, no CMA hit):
baseline: pin_ms = 13.4 ms, pgmigrate_success = 0
unmovable: pin_ms = 698.8 ms (52x slower),
pgmigrate_success = 524,288
- 5120MB pre-faulted shmem (fragmented, 844MB CMA enabled):
baseline: pin_ms = 598.3 ms, pgmigrate_success = 342,896
unmovable: pin_ms = 2,550.1 ms (4.3x slower),
pgmigrate_success = 2,181,701 (1.66x per page)
So I think the two ideas work together rather than replace each
other. Even if we move long-term pins to MIGRATE_UNMOVABLE
pageblocks, skipping DMA-pinned LRU folios in
isolate_migratepages_block() is still needed whenever kcompactd or
sync compaction scans those blocks.
--
Best regards,
Jialin
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 15:34 UTC | newest]
Thread overview: 6+ 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)
2026-12-02 2:19 ` Harry Yoo
2026-12-02 13:37 ` David Hildenbrand (Arm)
2026-12-02 15:34 ` Jialin Wang
2026-12-02 15:31 ` Jialin Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox