Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation

From: Harry Yoo

Date: Tue Dec 01 2026 - 21:20:04 EST


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