Skip to content

Commit ca0984c

Browse files
ebruAkagunduztorvalds
authored andcommitted
mm: incorporate zero pages into transparent huge pages
This patch improves THP collapse rates, by allowing zero pages. Currently THP can collapse 4kB pages into a THP when there are up to khugepaged_max_ptes_none pte_none ptes in a 2MB range. This patch counts pte none and mapped zero pages with the same variable. The patch was tested with a program that allocates 800MB of memory, and performs interleaved reads and writes, in a pattern that causes some 2MB areas to first see read accesses, resulting in the zero pfn being mapped there. To simulate memory fragmentation at allocation time, I modified do_huge_pmd_anonymous_page to return VM_FAULT_FALLBACK for read faults. Without the patch, only %50 of the program was collapsed into THP and the percentage did not increase over time. With this patch after 10 minutes of waiting khugepaged had collapsed %99 of the program's memory. [aarcange@redhat.com: fix bogus BUG()] Signed-off-by: Ebru Akagunduz <ebru.akagunduz@gmail.com> Reviewed-by: Rik van Riel <riel@redhat.com> Reviewed-by: Andrea Arcangeli <aarcange@redhat.com> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Acked-by: Vlastimil Babka <vbabka@suse.cz> Cc: Hugh Dickins <hughd@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Sasha Levin <sasha.levin@oracle.com> Cc: David Rientjes <rientjes@google.com> Cc: Mel Gorman <mel@csn.ul.ie> Signed-off-by: Andrea Arcangeli <aarcange@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2149cda commit ca0984c

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

mm/huge_memory.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte)
21092109
{
21102110
while (--_pte >= pte) {
21112111
pte_t pteval = *_pte;
2112-
if (!pte_none(pteval))
2112+
if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
21132113
release_pte_page(pte_page(pteval));
21142114
}
21152115
}
@@ -2120,13 +2120,13 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
21202120
{
21212121
struct page *page;
21222122
pte_t *_pte;
2123-
int none = 0;
2123+
int none_or_zero = 0;
21242124
bool referenced = false, writable = false;
21252125
for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
21262126
_pte++, address += PAGE_SIZE) {
21272127
pte_t pteval = *_pte;
2128-
if (pte_none(pteval)) {
2129-
if (++none <= khugepaged_max_ptes_none)
2128+
if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
2129+
if (++none_or_zero <= khugepaged_max_ptes_none)
21302130
continue;
21312131
else
21322132
goto out;
@@ -2207,9 +2207,21 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
22072207
pte_t pteval = *_pte;
22082208
struct page *src_page;
22092209

2210-
if (pte_none(pteval)) {
2210+
if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
22112211
clear_user_highpage(page, address);
22122212
add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2213+
if (is_zero_pfn(pte_pfn(pteval))) {
2214+
/*
2215+
* ptl mostly unnecessary.
2216+
*/
2217+
spin_lock(ptl);
2218+
/*
2219+
* paravirt calls inside pte_clear here are
2220+
* superfluous.
2221+
*/
2222+
pte_clear(vma->vm_mm, address, _pte);
2223+
spin_unlock(ptl);
2224+
}
22132225
} else {
22142226
src_page = pte_page(pteval);
22152227
copy_user_highpage(page, src_page, address, vma);
@@ -2543,7 +2555,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
25432555
{
25442556
pmd_t *pmd;
25452557
pte_t *pte, *_pte;
2546-
int ret = 0, none = 0;
2558+
int ret = 0, none_or_zero = 0;
25472559
struct page *page;
25482560
unsigned long _address;
25492561
spinlock_t *ptl;
@@ -2561,8 +2573,8 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
25612573
for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
25622574
_pte++, _address += PAGE_SIZE) {
25632575
pte_t pteval = *_pte;
2564-
if (pte_none(pteval)) {
2565-
if (++none <= khugepaged_max_ptes_none)
2576+
if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
2577+
if (++none_or_zero <= khugepaged_max_ptes_none)
25662578
continue;
25672579
else
25682580
goto out_unmap;

0 commit comments

Comments
 (0)