Skip to content

Commit 22d0229

Browse files
Dev Jainakpm00
authored andcommitted
khugepaged: optimize collapse_pte_mapped_thp() by PTE batching
Use PTE batching to batch process PTEs mapping the same large folio. An improvement is expected due to batching mapcount manipulation on the folios, and for arm64 which supports contig mappings, the number of TLB flushes is also reduced. Note that we do not need to make a change to the check "if (folio_page(folio, i) != page)"; if i'th page of the folio is equal to the first page of our batch, then i + 1, .... i + nr_batch_ptes - 1 pages of the folio will be equal to the corresponding pages of our batch mapping consecutive pages. Link: https://lkml.kernel.org/r/20250724052301.23844-4-dev.jain@arm.com Signed-off-by: Dev Jain <dev.jain@arm.com> Acked-by: David Hildenbrand <david@redhat.com> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reviewed-by: Zi Yan <ziy@nvidia.com> Cc: Barry Song <baohua@kernel.org> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Mariano Pache <npache@redhat.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 4ea3594 commit 22d0229

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

mm/khugepaged.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,15 +1503,17 @@ static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
15031503
int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
15041504
bool install_pmd)
15051505
{
1506+
int nr_mapped_ptes = 0, result = SCAN_FAIL;
1507+
unsigned int nr_batch_ptes;
15061508
struct mmu_notifier_range range;
15071509
bool notified = false;
15081510
unsigned long haddr = addr & HPAGE_PMD_MASK;
1511+
unsigned long end = haddr + HPAGE_PMD_SIZE;
15091512
struct vm_area_struct *vma = vma_lookup(mm, haddr);
15101513
struct folio *folio;
15111514
pte_t *start_pte, *pte;
15121515
pmd_t *pmd, pgt_pmd;
15131516
spinlock_t *pml = NULL, *ptl;
1514-
int nr_ptes = 0, result = SCAN_FAIL;
15151517
int i;
15161518

15171519
mmap_assert_locked(mm);
@@ -1625,11 +1627,15 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
16251627
goto abort;
16261628

16271629
/* step 2: clear page table and adjust rmap */
1628-
for (i = 0, addr = haddr, pte = start_pte;
1629-
i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1630+
for (i = 0, addr = haddr, pte = start_pte; i < HPAGE_PMD_NR;
1631+
i += nr_batch_ptes, addr += nr_batch_ptes * PAGE_SIZE,
1632+
pte += nr_batch_ptes) {
1633+
unsigned int max_nr_batch_ptes = (end - addr) >> PAGE_SHIFT;
16301634
struct page *page;
16311635
pte_t ptent = ptep_get(pte);
16321636

1637+
nr_batch_ptes = 1;
1638+
16331639
if (pte_none(ptent))
16341640
continue;
16351641
/*
@@ -1643,26 +1649,29 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
16431649
goto abort;
16441650
}
16451651
page = vm_normal_page(vma, addr, ptent);
1652+
16461653
if (folio_page(folio, i) != page)
16471654
goto abort;
16481655

1656+
nr_batch_ptes = folio_pte_batch(folio, pte, ptent, max_nr_batch_ptes);
1657+
16491658
/*
16501659
* Must clear entry, or a racing truncate may re-remove it.
16511660
* TLB flush can be left until pmdp_collapse_flush() does it.
16521661
* PTE dirty? Shmem page is already dirty; file is read-only.
16531662
*/
1654-
ptep_clear(mm, addr, pte);
1655-
folio_remove_rmap_pte(folio, page, vma);
1656-
nr_ptes++;
1663+
clear_ptes(mm, addr, pte, nr_batch_ptes);
1664+
folio_remove_rmap_ptes(folio, page, nr_batch_ptes, vma);
1665+
nr_mapped_ptes += nr_batch_ptes;
16571666
}
16581667

16591668
if (!pml)
16601669
spin_unlock(ptl);
16611670

16621671
/* step 3: set proper refcount and mm_counters. */
1663-
if (nr_ptes) {
1664-
folio_ref_sub(folio, nr_ptes);
1665-
add_mm_counter(mm, mm_counter_file(folio), -nr_ptes);
1672+
if (nr_mapped_ptes) {
1673+
folio_ref_sub(folio, nr_mapped_ptes);
1674+
add_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes);
16661675
}
16671676

16681677
/* step 4: remove empty page table */
@@ -1695,10 +1704,10 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
16951704
: SCAN_SUCCEED;
16961705
goto drop_folio;
16971706
abort:
1698-
if (nr_ptes) {
1707+
if (nr_mapped_ptes) {
16991708
flush_tlb_mm(mm);
1700-
folio_ref_sub(folio, nr_ptes);
1701-
add_mm_counter(mm, mm_counter_file(folio), -nr_ptes);
1709+
folio_ref_sub(folio, nr_mapped_ptes);
1710+
add_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes);
17021711
}
17031712
unlock:
17041713
if (start_pte)

0 commit comments

Comments
 (0)