Re: [PATCH v3 4/4] mm: pass the number of PTEs to move_pte()

From: Pedro Falcato

Date: Fri Oct 23 2026 - 13:28:15 EST


On Thu, Oct 22, 2026 at 01:33:40AM +0200, Imre Kaloz wrote:
> Since commit f822a9a81a31 ("mm: optimize mremap() by PTE batching"),
> move_ptes() moves a batch of PTEs mapping consecutive pages of a folio
> at a time, but calls move_pte() once per batch with the first PTE
> only. sparc64, the only architecture that overrides move_pte(), flushes
> from the D-cache the page that PTE maps when the move changes its
> colour, so the other pages of a batch would keep lines of the old
> colour. This cannot happen today only because sparc64 has no
> pte_batch_hint(), and mremap_folio_pte_batch() then never batches.
>
> Pass nr_ptes to move_pte(), as get_and_clear_ptes() takes it. The
> generic version ignores it, and sparc64 flushes every page of the
> batch.
>
> Signed-off-by: Imre Kaloz <kaloz@xxxxxxxxxx>
> ---
> arch/sparc/include/asm/pgtable_64.h | 16 +++++++++-------
> include/linux/pgtable.h | 2 +-
> mm/mremap.c | 2 +-
> 3 files changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/arch/sparc/include/asm/pgtable_64.h b/arch/sparc/include/asm/pgtable_64.h
> index 1430489b9ac5..9ca787b3cbfe 100644
> --- a/arch/sparc/include/asm/pgtable_64.h
> +++ b/arch/sparc/include/asm/pgtable_64.h
> @@ -946,21 +946,23 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
> set_pte_at((mm), (addr), (ptep), __pte(0UL))
>
> #ifdef DCACHE_ALIASING_POSSIBLE
> -/* This flushes only the page the PTE maps, so it relies on move_ptes()
> - * calling it for one PTE at a time, which the default pte_batch_hint()
> - * guarantees.
> +/* The nr PTEs map consecutive pages of one folio and all move by the
> + * same offset, so either every page of the batch aliases or none does.
> */
> #define __HAVE_ARCH_MOVE_PTE
> -#define move_pte(pte, old_addr, new_addr) \
> +#define move_pte(pte, old_addr, new_addr, nr) \

The \ is now misaligned (at least according to my email client), perhaps you
can fix it up :)

The only nit I have is regarding the function's name. move_pte with a nr is
no longer really move_pte, move_ptes() is the calling function's name... Yuck.

Also it seems a bit overengineered at this point, move_pte() returns a pte
that sparc never modifies.

> ({ \
> pte_t newpte = (pte); \
> if (tlb_type != hypervisor && pte_present(pte)) { \
> unsigned long this_pfn = pte_pfn(pte); \
> + unsigned int i; \
> \
> if (pfn_valid(this_pfn) && \
> - (((old_addr) ^ (new_addr)) & (1 << 13))) \
> - flush_dcache_page_all(current->mm, \
> - pfn_to_page(this_pfn)); \
> + (((old_addr) ^ (new_addr)) & (1 << 13))) { \
> + for (i = 0; i < (nr); i++) \
> + flush_dcache_page_all(current->mm, \
> + pfn_to_page(this_pfn + i)); \

Is there no way to pass nr_ptes directly to flushing code?

Anyway,

Reviewed-by: Pedro Falcato <pfalcato@xxxxxxx>

--
Pedro