Skip to content

Commit e974dd0

Browse files
jpemartinsopsiff
authored andcommitted
hugetlb: batch PMD split for bulk vmemmap dedup
mainline inclusion from mainline-v6.7-rc1 category: performance In an effort to minimize amount of TLB flushes, batch all PMD splits belonging to a range of pages in order to perform only 1 (global) TLB flush. Add a flags field to the walker and pass whether it's a bulk allocation or just a single page to decide to remap. First value (VMEMMAP_SPLIT_NO_TLB_FLUSH) designates the request to not do the TLB flush when we split the PMD. Rebased and updated by Mike Kravetz Link: https://lkml.kernel.org/r/20231019023113.345257-7-mike.kravetz@oracle.com Signed-off-by: Joao Martins <joao.m.martins@oracle.com> Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com> Reviewed-by: Muchun Song <songmuchun@bytedance.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: Barry Song <21cnbao@gmail.com> Cc: David Hildenbrand <david@redhat.com> Cc: David Rientjes <rientjes@google.com> Cc: James Houghton <jthoughton@google.com> Cc: Konrad Dybcio <konradybcio@kernel.org> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Miaohe Lin <linmiaohe@huawei.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Naoya Horiguchi <naoya.horiguchi@linux.dev> Cc: Oscar Salvador <osalvador@suse.de> Cc: Sergey Senozhatsky <senozhatsky@chromium.org> Cc: Usama Arif <usama.arif@bytedance.com> Cc: Xiongchun Duan <duanxiongchun@bytedance.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> (cherry picked from commit f4b7e3e) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 1cdd4d8 commit e974dd0

1 file changed

Lines changed: 88 additions & 4 deletions

File tree

mm/hugetlb_vmemmap.c

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* @reuse_addr: the virtual address of the @reuse_page page.
2828
* @vmemmap_pages: the list head of the vmemmap pages that can be freed
2929
* or is mapped from.
30+
* @flags: used to modify behavior in vmemmap page table walking
31+
* operations.
3032
*/
3133
struct vmemmap_remap_walk {
3234
void (*remap_pte)(pte_t *pte, unsigned long addr,
@@ -35,9 +37,13 @@ struct vmemmap_remap_walk {
3537
struct page *reuse_page;
3638
unsigned long reuse_addr;
3739
struct list_head *vmemmap_pages;
40+
41+
/* Skip the TLB flush when we split the PMD */
42+
#define VMEMMAP_SPLIT_NO_TLB_FLUSH BIT(0)
43+
unsigned long flags;
3844
};
3945

40-
static int split_vmemmap_huge_pmd(pmd_t *pmd, unsigned long start)
46+
static int split_vmemmap_huge_pmd(pmd_t *pmd, unsigned long start, bool flush)
4147
{
4248
pmd_t __pmd;
4349
int i;
@@ -80,7 +86,8 @@ static int split_vmemmap_huge_pmd(pmd_t *pmd, unsigned long start)
8086
/* Make pte visible before pmd. See comment in pmd_install(). */
8187
smp_wmb();
8288
pmd_populate_kernel(&init_mm, pmd, pgtable);
83-
flush_tlb_kernel_range(start, start + PMD_SIZE);
89+
if (flush)
90+
flush_tlb_kernel_range(start, start + PMD_SIZE);
8491
} else {
8592
pte_free_kernel(&init_mm, pgtable);
8693
}
@@ -127,11 +134,20 @@ static int vmemmap_pmd_range(pud_t *pud, unsigned long addr,
127134
do {
128135
int ret;
129136

130-
ret = split_vmemmap_huge_pmd(pmd, addr & PMD_MASK);
137+
ret = split_vmemmap_huge_pmd(pmd, addr & PMD_MASK,
138+
!(walk->flags & VMEMMAP_SPLIT_NO_TLB_FLUSH));
131139
if (ret)
132140
return ret;
133141

134142
next = pmd_addr_end(addr, end);
143+
144+
/*
145+
* We are only splitting, not remapping the hugetlb vmemmap
146+
* pages.
147+
*/
148+
if (!walk->remap_pte)
149+
continue;
150+
135151
vmemmap_pte_range(pmd, addr, next, walk);
136152
} while (pmd++, addr = next, addr != end);
137153

@@ -198,7 +214,8 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end,
198214
return ret;
199215
} while (pgd++, addr = next, addr != end);
200216

201-
flush_tlb_kernel_range(start, end);
217+
if (walk->remap_pte)
218+
flush_tlb_kernel_range(start, end);
202219

203220
return 0;
204221
}
@@ -297,6 +314,36 @@ static void vmemmap_restore_pte(pte_t *pte, unsigned long addr,
297314
set_pte_at(&init_mm, addr, pte, mk_pte(page, pgprot));
298315
}
299316

317+
/**
318+
* vmemmap_remap_split - split the vmemmap virtual address range [@start, @end)
319+
* backing PMDs of the directmap into PTEs
320+
* @start: start address of the vmemmap virtual address range that we want
321+
* to remap.
322+
* @end: end address of the vmemmap virtual address range that we want to
323+
* remap.
324+
* @reuse: reuse address.
325+
*
326+
* Return: %0 on success, negative error code otherwise.
327+
*/
328+
static int vmemmap_remap_split(unsigned long start, unsigned long end,
329+
unsigned long reuse)
330+
{
331+
int ret;
332+
struct vmemmap_remap_walk walk = {
333+
.remap_pte = NULL,
334+
.flags = VMEMMAP_SPLIT_NO_TLB_FLUSH,
335+
};
336+
337+
/* See the comment in the vmemmap_remap_free(). */
338+
BUG_ON(start - reuse != PAGE_SIZE);
339+
340+
mmap_read_lock(&init_mm);
341+
ret = vmemmap_remap_range(reuse, end, &walk);
342+
mmap_read_unlock(&init_mm);
343+
344+
return ret;
345+
}
346+
300347
/**
301348
* vmemmap_remap_free - remap the vmemmap virtual address range [@start, @end)
302349
* to the page which @reuse is mapped to, then free vmemmap
@@ -320,6 +367,7 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
320367
.remap_pte = vmemmap_remap_pte,
321368
.reuse_addr = reuse,
322369
.vmemmap_pages = vmemmap_pages,
370+
.flags = 0,
323371
};
324372
int nid = page_to_nid((struct page *)reuse);
325373
gfp_t gfp_mask = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
@@ -368,6 +416,7 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
368416
.remap_pte = vmemmap_restore_pte,
369417
.reuse_addr = reuse,
370418
.vmemmap_pages = vmemmap_pages,
419+
.flags = 0,
371420
};
372421

373422
vmemmap_remap_range(reuse, end, &walk);
@@ -419,6 +468,7 @@ static int vmemmap_remap_alloc(unsigned long start, unsigned long end,
419468
.remap_pte = vmemmap_restore_pte,
420469
.reuse_addr = reuse,
421470
.vmemmap_pages = &vmemmap_pages,
471+
.flags = 0,
422472
};
423473

424474
/* See the comment in the vmemmap_remap_free(). */
@@ -628,11 +678,45 @@ void hugetlb_vmemmap_optimize(const struct hstate *h, struct page *head)
628678
free_vmemmap_page_list(&vmemmap_pages);
629679
}
630680

681+
static int hugetlb_vmemmap_split(const struct hstate *h, struct page *head)
682+
{
683+
unsigned long vmemmap_start = (unsigned long)head, vmemmap_end;
684+
unsigned long vmemmap_reuse;
685+
686+
if (!vmemmap_should_optimize(h, head))
687+
return 0;
688+
689+
vmemmap_end = vmemmap_start + hugetlb_vmemmap_size(h);
690+
vmemmap_reuse = vmemmap_start;
691+
vmemmap_start += HUGETLB_VMEMMAP_RESERVE_SIZE;
692+
693+
/*
694+
* Split PMDs on the vmemmap virtual address range [@vmemmap_start,
695+
* @vmemmap_end]
696+
*/
697+
return vmemmap_remap_split(vmemmap_start, vmemmap_end, vmemmap_reuse);
698+
}
699+
631700
void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list)
632701
{
633702
struct folio *folio;
634703
LIST_HEAD(vmemmap_pages);
635704

705+
list_for_each_entry(folio, folio_list, lru) {
706+
int ret = hugetlb_vmemmap_split(h, &folio->page);
707+
708+
/*
709+
* Spliting the PMD requires allocating a page, thus lets fail
710+
* early once we encounter the first OOM. No point in retrying
711+
* as it can be dynamically done on remap with the memory
712+
* we get back from the vmemmap deduplication.
713+
*/
714+
if (ret == -ENOMEM)
715+
break;
716+
}
717+
718+
flush_tlb_all();
719+
636720
list_for_each_entry(folio, folio_list, lru) {
637721
int ret = __hugetlb_vmemmap_optimize(h, &folio->page,
638722
&vmemmap_pages);

0 commit comments

Comments
 (0)