mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jialin Wang <wjl.linux@gmail.com>
To: david@kernel.org
Cc: akpm@linux-foundation.org, brendan.jackman@linux.dev,
	hannes@cmpxchg.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:31:15 +0800	[thread overview]
Message-ID: <20261202153115.273095-1-wjl.linux@gmail.com> (raw)
In-Reply-To: <5cda167e-4e2b-4267-b6df-a85242684de7@kernel.org>

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

      parent reply	other threads:[~2026-12-02 15:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 15:46 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 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=20261202153115.273095-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=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

all inboxes | Powered by JetHome®