Re: [PATCH v3] mm: introduce copy_mc_highpages() and batch copy in folio_mc_copy()

From: Garg, Shivank

Date: Sun Nov 22 2026 - 12:13:39 EST


On Sat, 2026-11-21 at 23:30 -0800, Andrew Morton wrote:
> On Fri, 13 Nov 2026 17:09:21 +0000 Shivank Garg <shivankg@xxxxxxx> wrote:
>
> > folio_mc_copy() copies a folio one page at a time with copy_mc_highpage(),
> > so a 2MB folio makes 512 separate copy_mc_to_kernel() calls. Each page
> > pays the startup cost of the string instruction ("REP; MOVSB" on x86
> > CPUs with ERMS) plus a cond_resched().
> >
> > Introduce copy_mc_highpages(), the page range variant of
> > copy_mc_highpage(). When copy_mc_to_kernel() is available and the pages
> > are in the linear map, copy the whole range with a single
> > copy_mc_to_kernel() call, so the CPU can optimize for the larger extent.
> > Otherwise, fall back to copy_mc_highpage() one page at a time.
> >
> > On a machine check, queue the poisoned source page for memory_failure(),
> > as copy_mc_highpage() does, and return -EHWPOISON, as folio_mc_copy()
> > does today.
> >
> > copy_mc_to_kernel() cannot reschedule, so only take the single-call path
> > when CONFIG_PREEMPTION is set and the copy can be preempted. Since commit
> > 7dadeaa6e851 ("sched: Further restrict the preemption modes"), x86 and
> > powerpc, the only architectures that select ARCH_HAS_COPY_MC, only offer
> > the full, lazy and RT preemption models, also with PREEMPT_DYNAMIC, so
> > they always take this path. The per-page fallback calls cond_resched()
> > before each page, as clear_contig_highpages() does. Other architectures
> > keep copying one page at a time.
> >
> > Switch folio_mc_copy() to copy the whole folio with copy_mc_highpages().
>
> OK.
>
> > ---
> > On a Zen 6 system with CXL memory, move_pages() gets ~1.4x faster for
> > 2MB THPs and up to ~2.8x faster for 1GB hugetlb pages (numbers below).
> > When demoting to CXL, kswapd uses ~31% less CPU per GB demoted.
>
> But that's the most important part! It's the whole point of the patch!
> Yet here it is below the ---, which means "throw this away". It
> should be the first paragraph, in all caps.
>
> <edit edit fixed>

Sure, thanks. I'll keep this in mind.

>
> OK, thanks, queued,
>
> nit: does it really need cond_resched()? That's not appropriate for
> current mainline kernels. I guess "yes", because this patch looks like
> something which downstream people will eagerly backport into older
> kernels, even though we didn't cc:stable.
>
> And we care about downstream people. Globally: "Roughly 60% are on 6.x
> and 30% are on 5.x (with the remaining ~10% split between 7.x and 4.x
> or older)".
>
> I'll take a look at that Sashiko thing.


Agreed.

For older kernels, where the non-preemptible modes are still
available, the backport should look like v2 in my opinion (calling
cond_resched() every 32MB). Otherwise a 1GB hugetlb copy can run for
a long time without rescheduling. I'll leave this discussion for
backport time :)