Skip to content

Commit b8d3c4c

Browse files
minchanktorvalds
authored andcommitted
mm/huge_memory.c: don't split THP page when MADV_FREE syscall is called
We don't need to split THP page when MADV_FREE syscall is called if [start, len] is aligned with THP size. The split could be done when VM decide to free it in reclaim path if memory pressure is heavy. With that, we could avoid unnecessary THP split. For the feature, this patch changes pte dirtness marking logic of THP. Now, it marks every ptes of pages dirty unconditionally in splitting, which makes MADV_FREE void. So, instead, this patch propagates pmd dirtiness to all pages via PG_dirty and restores pte dirtiness from PG_dirty. With this, if pmd is clean(ie, MADV_FREEed) when split happens(e,g, shrink_page_list), all of pages are clean too so we could discard them. Signed-off-by: Minchan Kim <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: "James E.J. Bottomley" <[email protected]> Cc: "Kirill A. Shutemov" <[email protected]> Cc: Shaohua Li <[email protected]> Cc: <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Chen Gang <[email protected]> Cc: Chris Zankel <[email protected]> Cc: Daniel Micay <[email protected]> Cc: Darrick J. Wong <[email protected]> Cc: David S. Miller <[email protected]> Cc: Helge Deller <[email protected]> Cc: Ivan Kokshaysky <[email protected]> Cc: Jason Evans <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: KOSAKI Motohiro <[email protected]> Cc: Matt Turner <[email protected]> Cc: Max Filippov <[email protected]> Cc: Mel Gorman <[email protected]> Cc: Michael Kerrisk <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Mika Penttil <[email protected]> Cc: Ralf Baechle <[email protected]> Cc: Richard Henderson <[email protected]> Cc: Rik van Riel <[email protected]> Cc: Roland Dreier <[email protected]> Cc: Russell King <[email protected]> Cc: Shaohua Li <[email protected]> Cc: Will Deacon <[email protected]> Cc: Wu Fengguang <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 05ee26d commit b8d3c4c

File tree

3 files changed

+92
-6
lines changed

3 files changed

+92
-6
lines changed

include/linux/huge_mm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ extern struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
1919
unsigned long addr,
2020
pmd_t *pmd,
2121
unsigned int flags);
22+
extern int madvise_free_huge_pmd(struct mmu_gather *tlb,
23+
struct vm_area_struct *vma,
24+
pmd_t *pmd, unsigned long addr, unsigned long next);
2225
extern int zap_huge_pmd(struct mmu_gather *tlb,
2326
struct vm_area_struct *vma,
2427
pmd_t *pmd, unsigned long addr);

mm/huge_memory.c

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,77 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
15011501
return 0;
15021502
}
15031503

1504+
int madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1505+
pmd_t *pmd, unsigned long addr, unsigned long next)
1506+
1507+
{
1508+
spinlock_t *ptl;
1509+
pmd_t orig_pmd;
1510+
struct page *page;
1511+
struct mm_struct *mm = tlb->mm;
1512+
int ret = 0;
1513+
1514+
if (!pmd_trans_huge_lock(pmd, vma, &ptl))
1515+
goto out;
1516+
1517+
orig_pmd = *pmd;
1518+
if (is_huge_zero_pmd(orig_pmd)) {
1519+
ret = 1;
1520+
goto out;
1521+
}
1522+
1523+
page = pmd_page(orig_pmd);
1524+
/*
1525+
* If other processes are mapping this page, we couldn't discard
1526+
* the page unless they all do MADV_FREE so let's skip the page.
1527+
*/
1528+
if (page_mapcount(page) != 1)
1529+
goto out;
1530+
1531+
if (!trylock_page(page))
1532+
goto out;
1533+
1534+
/*
1535+
* If user want to discard part-pages of THP, split it so MADV_FREE
1536+
* will deactivate only them.
1537+
*/
1538+
if (next - addr != HPAGE_PMD_SIZE) {
1539+
get_page(page);
1540+
spin_unlock(ptl);
1541+
if (split_huge_page(page)) {
1542+
put_page(page);
1543+
unlock_page(page);
1544+
goto out_unlocked;
1545+
}
1546+
put_page(page);
1547+
unlock_page(page);
1548+
ret = 1;
1549+
goto out_unlocked;
1550+
}
1551+
1552+
if (PageDirty(page))
1553+
ClearPageDirty(page);
1554+
unlock_page(page);
1555+
1556+
if (PageActive(page))
1557+
deactivate_page(page);
1558+
1559+
if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1560+
orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1561+
tlb->fullmm);
1562+
orig_pmd = pmd_mkold(orig_pmd);
1563+
orig_pmd = pmd_mkclean(orig_pmd);
1564+
1565+
set_pmd_at(mm, addr, pmd, orig_pmd);
1566+
tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1567+
}
1568+
ret = 1;
1569+
out:
1570+
spin_unlock(ptl);
1571+
out_unlocked:
1572+
return ret;
1573+
}
1574+
15041575
int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
15051576
pmd_t *pmd, unsigned long addr)
15061577
{
@@ -2710,7 +2781,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
27102781
struct page *page;
27112782
pgtable_t pgtable;
27122783
pmd_t _pmd;
2713-
bool young, write;
2784+
bool young, write, dirty;
27142785
int i;
27152786

27162787
VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
@@ -2734,6 +2805,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
27342805
atomic_add(HPAGE_PMD_NR - 1, &page->_count);
27352806
write = pmd_write(*pmd);
27362807
young = pmd_young(*pmd);
2808+
dirty = pmd_dirty(*pmd);
27372809

27382810
pgtable = pgtable_trans_huge_withdraw(mm, pmd);
27392811
pmd_populate(mm, &_pmd, pgtable);
@@ -2751,12 +2823,14 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
27512823
entry = swp_entry_to_pte(swp_entry);
27522824
} else {
27532825
entry = mk_pte(page + i, vma->vm_page_prot);
2754-
entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2826+
entry = maybe_mkwrite(entry, vma);
27552827
if (!write)
27562828
entry = pte_wrprotect(entry);
27572829
if (!young)
27582830
entry = pte_mkold(entry);
27592831
}
2832+
if (dirty)
2833+
SetPageDirty(page + i);
27602834
pte = pte_offset_map(&_pmd, haddr);
27612835
BUG_ON(!pte_none(*pte));
27622836
set_pte_at(mm, haddr, pte, entry);
@@ -2962,6 +3036,8 @@ static void freeze_page_vma(struct vm_area_struct *vma, struct page *page,
29623036
continue;
29633037
flush_cache_page(vma, address, page_to_pfn(page));
29643038
entry = ptep_clear_flush(vma, address, pte + i);
3039+
if (pte_dirty(entry))
3040+
SetPageDirty(page);
29653041
swp_entry = make_migration_entry(page, pte_write(entry));
29663042
swp_pte = swp_entry_to_pte(swp_entry);
29673043
if (pte_soft_dirty(entry))
@@ -3028,7 +3104,8 @@ static void unfreeze_page_vma(struct vm_area_struct *vma, struct page *page,
30283104
page_add_anon_rmap(page, vma, address, false);
30293105

30303106
entry = pte_mkold(mk_pte(page, vma->vm_page_prot));
3031-
entry = pte_mkdirty(entry);
3107+
if (PageDirty(page))
3108+
entry = pte_mkdirty(entry);
30323109
if (is_write_migration_entry(swp_entry))
30333110
entry = maybe_mkwrite(entry, vma);
30343111

@@ -3089,8 +3166,8 @@ static int __split_huge_page_tail(struct page *head, int tail,
30893166
(1L << PG_uptodate) |
30903167
(1L << PG_active) |
30913168
(1L << PG_locked) |
3092-
(1L << PG_unevictable)));
3093-
page_tail->flags |= (1L << PG_dirty);
3169+
(1L << PG_unevictable) |
3170+
(1L << PG_dirty)));
30943171

30953172
/*
30963173
* After clearing PageTail the gup refcount can be released.

mm/madvise.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,13 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
271271
pte_t *orig_pte, *pte, ptent;
272272
struct page *page;
273273
int nr_swap = 0;
274+
unsigned long next;
275+
276+
next = pmd_addr_end(addr, end);
277+
if (pmd_trans_huge(*pmd))
278+
if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
279+
goto next;
274280

275-
split_huge_pmd(vma, pmd, addr);
276281
if (pmd_trans_unstable(pmd))
277282
return 0;
278283

@@ -383,6 +388,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
383388
arch_leave_lazy_mmu_mode();
384389
pte_unmap_unlock(orig_pte, ptl);
385390
cond_resched();
391+
next:
386392
return 0;
387393
}
388394

0 commit comments

Comments
 (0)