From: Jialin Wang <wjl.linux@gmail.com>
To: harry@kernel.org
Cc: akpm@linux-foundation.org, brendan.jackman@linux.dev,
david@kernel.org, hannes@cmpxchg.org, kas@kernel.org,
khalid@kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, mhocko@suse.com, surenb@google.com,
vbabka@kernel.org, wjl.linux@gmail.com, ziy@nvidia.com
Subject: Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation
Date: Wed, 2 Dec 2026 23:34:00 +0800 [thread overview]
Message-ID: <20261202153400.274475-1-wjl.linux@gmail.com> (raw)
In-Reply-To: <aw91lU_1m__NsxVN@thinkstation>
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
next prev parent reply other threads:[~2026-12-02 15:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-12-02 15:31 ` Jialin Wang
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=20261202153400.274475-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=harry@kernel.org \
--cc=kas@kernel.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