Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Harry Yoo <harry@kernel.org>
To: "David Hildenbrand (Arm)" <david@kernel.org>
Cc: Jialin Wang <wjl.linux@gmail.com>,
	 Andrew Morton <akpm@linux-foundation.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	 Johannes Weiner <hannes@cmpxchg.org>,
	Suren Baghdasaryan <surenb@google.com>,
	 Michal Hocko <mhocko@suse.com>,
	Brendan Jackman <brendan.jackman@linux.dev>,
	 Zi Yan <ziy@nvidia.com>, Khalid Aziz <khalid@kernel.org>,
	linux-mm@kvack.org,  linux-kernel@vger.kernel.org,
	kas@kernel.org
Subject: Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation
Date: Wed, 2 Dec 2026 02:19:58 +0000	[thread overview]
Message-ID: <aw91lU_1m__NsxVN@thinkstation> (raw)
In-Reply-To: <5cda167e-4e2b-4267-b6df-a85242684de7@kernel.org>

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


      reply	other threads:[~2026-12-02  2:20 UTC|newest]

Thread overview: 3+ 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 [this message]

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=aw91lU_1m__NsxVN@thinkstation \
    --to=harry@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=brendan.jackman@linux.dev \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.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=wjl.linux@gmail.com \
    --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