Skip to content

Commit f822a9a

Browse files
Dev Jainakpm00
authored andcommitted
mm: optimize mremap() by PTE batching
Use folio_pte_batch() to optimize move_ptes(). On arm64, if the ptes are painted with the contig bit, then ptep_get() will iterate through all 16 entries to collect a/d bits. Hence this optimization will result in a 16x reduction in the number of ptep_get() calls. Next, ptep_get_and_clear() will eventually call contpte_try_unfold() on every contig block, thus flushing the TLB for the complete large folio range. Instead, use get_and_clear_full_ptes() so as to elide TLBIs on each contig block, and only do them on the starting and ending contig block. For split folios, there will be no pte batching; nr_ptes will be 1. For pagetable splitting, the ptes will still point to the same large folio; for arm64, this results in the optimization described above, and for other arches (including the general case), a minor improvement is expected due to a reduction in the number of function calls. Link: https://lkml.kernel.org/r/20250610035043.75448-3-dev.jain@arm.com Signed-off-by: Dev Jain <dev.jain@arm.com> Reviewed-by: Barry Song <baohua@kernel.org> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reviewed-by: Pedro Falcato <pfalcato@suse.de> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: Bang Li <libang.li@antgroup.com> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: bibo mao <maobibo@loongson.cn> Cc: David Hildenbrand <david@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jann Horn <jannh@google.com> Cc: Lance Yang <ioworker0@gmail.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Peter Xu <peterx@redhat.com> Cc: Qi Zheng <zhengqi.arch@bytedance.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Yang Shi <yang@os.amperecomputing.com> Cc: Zi Yan <ziy@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 94dab12 commit f822a9a

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

mm/mremap.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,40 @@ static pte_t move_soft_dirty_pte(pte_t pte)
170170
return pte;
171171
}
172172

173+
static int mremap_folio_pte_batch(struct vm_area_struct *vma, unsigned long addr,
174+
pte_t *ptep, pte_t pte, int max_nr)
175+
{
176+
const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
177+
struct folio *folio;
178+
179+
if (max_nr == 1)
180+
return 1;
181+
182+
folio = vm_normal_folio(vma, addr, pte);
183+
if (!folio || !folio_test_large(folio))
184+
return 1;
185+
186+
return folio_pte_batch(folio, addr, ptep, pte, max_nr, flags, NULL,
187+
NULL, NULL);
188+
}
189+
173190
static int move_ptes(struct pagetable_move_control *pmc,
174191
unsigned long extent, pmd_t *old_pmd, pmd_t *new_pmd)
175192
{
176193
struct vm_area_struct *vma = pmc->old;
177194
bool need_clear_uffd_wp = vma_has_uffd_without_event_remap(vma);
178195
struct mm_struct *mm = vma->vm_mm;
179196
pte_t *old_ptep, *new_ptep;
180-
pte_t pte;
197+
pte_t old_pte, pte;
181198
pmd_t dummy_pmdval;
182199
spinlock_t *old_ptl, *new_ptl;
183200
bool force_flush = false;
184201
unsigned long old_addr = pmc->old_addr;
185202
unsigned long new_addr = pmc->new_addr;
186203
unsigned long old_end = old_addr + extent;
187204
unsigned long len = old_end - old_addr;
205+
int max_nr_ptes;
206+
int nr_ptes;
188207
int err = 0;
189208

190209
/*
@@ -236,14 +255,16 @@ static int move_ptes(struct pagetable_move_control *pmc,
236255
flush_tlb_batched_pending(vma->vm_mm);
237256
arch_enter_lazy_mmu_mode();
238257

239-
for (; old_addr < old_end; old_ptep++, old_addr += PAGE_SIZE,
240-
new_ptep++, new_addr += PAGE_SIZE) {
258+
for (; old_addr < old_end; old_ptep += nr_ptes, old_addr += nr_ptes * PAGE_SIZE,
259+
new_ptep += nr_ptes, new_addr += nr_ptes * PAGE_SIZE) {
241260
VM_WARN_ON_ONCE(!pte_none(*new_ptep));
242261

243-
if (pte_none(ptep_get(old_ptep)))
262+
nr_ptes = 1;
263+
max_nr_ptes = (old_end - old_addr) >> PAGE_SHIFT;
264+
old_pte = ptep_get(old_ptep);
265+
if (pte_none(old_pte))
244266
continue;
245267

246-
pte = ptep_get_and_clear(mm, old_addr, old_ptep);
247268
/*
248269
* If we are remapping a valid PTE, make sure
249270
* to flush TLB before we drop the PTL for the
@@ -255,8 +276,12 @@ static int move_ptes(struct pagetable_move_control *pmc,
255276
* the TLB entry for the old mapping has been
256277
* flushed.
257278
*/
258-
if (pte_present(pte))
279+
if (pte_present(old_pte)) {
280+
nr_ptes = mremap_folio_pte_batch(vma, old_addr, old_ptep,
281+
old_pte, max_nr_ptes);
259282
force_flush = true;
283+
}
284+
pte = get_and_clear_full_ptes(mm, old_addr, old_ptep, nr_ptes, 0);
260285
pte = move_pte(pte, old_addr, new_addr);
261286
pte = move_soft_dirty_pte(pte);
262287

@@ -269,7 +294,7 @@ static int move_ptes(struct pagetable_move_control *pmc,
269294
else if (is_swap_pte(pte))
270295
pte = pte_swp_clear_uffd_wp(pte);
271296
}
272-
set_pte_at(mm, new_addr, new_ptep, pte);
297+
set_ptes(mm, new_addr, new_ptep, pte, nr_ptes);
273298
}
274299
}
275300

0 commit comments

Comments
 (0)