Skip to content

Commit 24669e5

Browse files
kvaneeshtorvalds
authored andcommitted
hugetlb: use mmu_gather instead of a temporary linked list for accumulating pages
Use a mmu_gather instead of a temporary linked list for accumulating pages when we unmap a hugepage range Signed-off-by: Aneesh Kumar K.V <[email protected]> Reviewed-by: KAMEZAWA Hiroyuki <[email protected]> Cc: David Rientjes <[email protected]> Cc: Hillf Danton <[email protected]> Cc: Michal Hocko <[email protected]> Cc: KOSAKI Motohiro <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 972dc4d commit 24669e5

File tree

4 files changed

+59
-33
lines changed

4 files changed

+59
-33
lines changed

fs/hugetlbfs/inode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ hugetlb_vmtruncate_list(struct prio_tree_root *root, pgoff_t pgoff)
416416
else
417417
v_offset = 0;
418418

419-
__unmap_hugepage_range(vma,
420-
vma->vm_start + v_offset, vma->vm_end, NULL);
419+
unmap_hugepage_range(vma, vma->vm_start + v_offset,
420+
vma->vm_end, NULL);
421421
}
422422
}
423423

include/linux/hugetlb.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
struct ctl_table;
99
struct user_struct;
10+
struct mmu_gather;
1011

1112
#ifdef CONFIG_HUGETLB_PAGE
1213

@@ -40,9 +41,10 @@ int follow_hugetlb_page(struct mm_struct *, struct vm_area_struct *,
4041
struct page **, struct vm_area_struct **,
4142
unsigned long *, int *, int, unsigned int flags);
4243
void unmap_hugepage_range(struct vm_area_struct *,
43-
unsigned long, unsigned long, struct page *);
44-
void __unmap_hugepage_range(struct vm_area_struct *,
45-
unsigned long, unsigned long, struct page *);
44+
unsigned long, unsigned long, struct page *);
45+
void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
46+
unsigned long start, unsigned long end,
47+
struct page *ref_page);
4648
int hugetlb_prefault(struct address_space *, struct vm_area_struct *);
4749
void hugetlb_report_meminfo(struct seq_file *);
4850
int hugetlb_report_node_meminfo(int, char *);
@@ -98,7 +100,6 @@ static inline unsigned long hugetlb_total_pages(void)
98100
#define follow_huge_addr(mm, addr, write) ERR_PTR(-EINVAL)
99101
#define copy_hugetlb_page_range(src, dst, vma) ({ BUG(); 0; })
100102
#define hugetlb_prefault(mapping, vma) ({ BUG(); 0; })
101-
#define unmap_hugepage_range(vma, start, end, page) BUG()
102103
static inline void hugetlb_report_meminfo(struct seq_file *m)
103104
{
104105
}
@@ -112,13 +113,24 @@ static inline void hugetlb_report_meminfo(struct seq_file *m)
112113
#define hugetlb_free_pgd_range(tlb, addr, end, floor, ceiling) ({BUG(); 0; })
113114
#define hugetlb_fault(mm, vma, addr, flags) ({ BUG(); 0; })
114115
#define huge_pte_offset(mm, address) 0
115-
#define dequeue_hwpoisoned_huge_page(page) 0
116+
static inline int dequeue_hwpoisoned_huge_page(struct page *page)
117+
{
118+
return 0;
119+
}
120+
116121
static inline void copy_huge_page(struct page *dst, struct page *src)
117122
{
118123
}
119124

120125
#define hugetlb_change_protection(vma, address, end, newprot)
121126

127+
static inline void __unmap_hugepage_range(struct mmu_gather *tlb,
128+
struct vm_area_struct *vma, unsigned long start,
129+
unsigned long end, struct page *ref_page)
130+
{
131+
BUG();
132+
}
133+
122134
#endif /* !CONFIG_HUGETLB_PAGE */
123135

124136
#define HUGETLB_ANON_FILE "anon_hugepage"

mm/hugetlb.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424

2525
#include <asm/page.h>
2626
#include <asm/pgtable.h>
27-
#include <linux/io.h>
27+
#include <asm/tlb.h>
2828

29+
#include <linux/io.h>
2930
#include <linux/hugetlb.h>
3031
#include <linux/node.h>
3132
#include "internal.h"
@@ -2310,30 +2311,26 @@ static int is_hugetlb_entry_hwpoisoned(pte_t pte)
23102311
return 0;
23112312
}
23122313

2313-
void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
2314-
unsigned long end, struct page *ref_page)
2314+
void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
2315+
unsigned long start, unsigned long end,
2316+
struct page *ref_page)
23152317
{
2318+
int force_flush = 0;
23162319
struct mm_struct *mm = vma->vm_mm;
23172320
unsigned long address;
23182321
pte_t *ptep;
23192322
pte_t pte;
23202323
struct page *page;
2321-
struct page *tmp;
23222324
struct hstate *h = hstate_vma(vma);
23232325
unsigned long sz = huge_page_size(h);
23242326

2325-
/*
2326-
* A page gathering list, protected by per file i_mmap_mutex. The
2327-
* lock is used to avoid list corruption from multiple unmapping
2328-
* of the same page since we are using page->lru.
2329-
*/
2330-
LIST_HEAD(page_list);
2331-
23322327
WARN_ON(!is_vm_hugetlb_page(vma));
23332328
BUG_ON(start & ~huge_page_mask(h));
23342329
BUG_ON(end & ~huge_page_mask(h));
23352330

2331+
tlb_start_vma(tlb, vma);
23362332
mmu_notifier_invalidate_range_start(mm, start, end);
2333+
again:
23372334
spin_lock(&mm->page_table_lock);
23382335
for (address = start; address < end; address += sz) {
23392336
ptep = huge_pte_offset(mm, address);
@@ -2372,30 +2369,45 @@ void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
23722369
}
23732370

23742371
pte = huge_ptep_get_and_clear(mm, address, ptep);
2372+
tlb_remove_tlb_entry(tlb, ptep, address);
23752373
if (pte_dirty(pte))
23762374
set_page_dirty(page);
2377-
list_add(&page->lru, &page_list);
23782375

2376+
page_remove_rmap(page);
2377+
force_flush = !__tlb_remove_page(tlb, page);
2378+
if (force_flush)
2379+
break;
23792380
/* Bail out after unmapping reference page if supplied */
23802381
if (ref_page)
23812382
break;
23822383
}
2383-
flush_tlb_range(vma, start, end);
23842384
spin_unlock(&mm->page_table_lock);
2385-
mmu_notifier_invalidate_range_end(mm, start, end);
2386-
list_for_each_entry_safe(page, tmp, &page_list, lru) {
2387-
page_remove_rmap(page);
2388-
list_del(&page->lru);
2389-
put_page(page);
2385+
/*
2386+
* mmu_gather ran out of room to batch pages, we break out of
2387+
* the PTE lock to avoid doing the potential expensive TLB invalidate
2388+
* and page-free while holding it.
2389+
*/
2390+
if (force_flush) {
2391+
force_flush = 0;
2392+
tlb_flush_mmu(tlb);
2393+
if (address < end && !ref_page)
2394+
goto again;
23902395
}
2396+
mmu_notifier_invalidate_range_end(mm, start, end);
2397+
tlb_end_vma(tlb, vma);
23912398
}
23922399

23932400
void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
23942401
unsigned long end, struct page *ref_page)
23952402
{
2396-
mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
2397-
__unmap_hugepage_range(vma, start, end, ref_page);
2398-
mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
2403+
struct mm_struct *mm;
2404+
struct mmu_gather tlb;
2405+
2406+
mm = vma->vm_mm;
2407+
2408+
tlb_gather_mmu(&tlb, mm, 0);
2409+
__unmap_hugepage_range(&tlb, vma, start, end, ref_page);
2410+
tlb_finish_mmu(&tlb, start, end);
23992411
}
24002412

24012413
/*
@@ -2440,9 +2452,8 @@ static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
24402452
* from the time of fork. This would look like data corruption
24412453
*/
24422454
if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
2443-
__unmap_hugepage_range(iter_vma,
2444-
address, address + huge_page_size(h),
2445-
page);
2455+
unmap_hugepage_range(iter_vma, address,
2456+
address + huge_page_size(h), page);
24462457
}
24472458
mutex_unlock(&mapping->i_mmap_mutex);
24482459

mm/memory.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,11 @@ static void unmap_single_vma(struct mmu_gather *tlb,
13431343
* Since no pte has actually been setup, it is
13441344
* safe to do nothing in this case.
13451345
*/
1346-
if (vma->vm_file)
1347-
unmap_hugepage_range(vma, start, end, NULL);
1346+
if (vma->vm_file) {
1347+
mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
1348+
__unmap_hugepage_range(tlb, vma, start, end, NULL);
1349+
mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
1350+
}
13481351
} else
13491352
unmap_page_range(tlb, vma, start, end, details);
13501353
}

0 commit comments

Comments
 (0)