Skip to content

Commit a9a7504

Browse files
Hugh Dickinstorvalds
authored andcommitted
mm/thp: fix page_vma_mapped_walk() if THP mapped by ptes
Running certain tests with a DEBUG_VM kernel would crash within hours, on the total_mapcount BUG() in split_huge_page_to_list(), while trying to free up some memory by punching a hole in a shmem huge page: split's try_to_unmap() was unable to find all the mappings of the page (which, on a !DEBUG_VM kernel, would then keep the huge page pinned in memory). Crash dumps showed two tail pages of a shmem huge page remained mapped by pte: ptes in a non-huge-aligned vma of a gVisor process, at the end of a long unmapped range; and no page table had yet been allocated for the head of the huge page to be mapped into. Although designed to handle these odd misaligned huge-page-mapped-by-pte cases, page_vma_mapped_walk() falls short by returning false prematurely when !pmd_present or !pud_present or !p4d_present or !pgd_present: there are cases when a huge page may span the boundary, with ptes present in the next. Restructure page_vma_mapped_walk() as a loop to continue in these cases, while keeping its layout much as before. Add a step_forward() helper to advance pvmw->address across those boundaries: originally I tried to use mm's standard p?d_addr_end() macros, but hit the same crash 512 times less often: because of the way redundant levels are folded together, but folded differently in different configurations, it was just too difficult to use them correctly; and step_forward() is simpler anyway. Link: https://lkml.kernel.org/r/[email protected] Fixes: ace71a1 ("mm: introduce page_vma_mapped_walk()") Signed-off-by: Hugh Dickins <[email protected]> Acked-by: Kirill A. Shutemov <[email protected]> Cc: Alistair Popple <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Peter Xu <[email protected]> Cc: Ralph Campbell <[email protected]> Cc: Wang Yugui <[email protected]> Cc: Will Deacon <[email protected]> Cc: Yang Shi <[email protected]> Cc: Zi Yan <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent a765c41 commit a9a7504

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

mm/page_vma_mapped.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw)
116116
return pfn_is_match(pvmw->page, pfn);
117117
}
118118

119+
static void step_forward(struct page_vma_mapped_walk *pvmw, unsigned long size)
120+
{
121+
pvmw->address = (pvmw->address + size) & ~(size - 1);
122+
if (!pvmw->address)
123+
pvmw->address = ULONG_MAX;
124+
}
125+
119126
/**
120127
* page_vma_mapped_walk - check if @pvmw->page is mapped in @pvmw->vma at
121128
* @pvmw->address
@@ -183,16 +190,22 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
183190
if (pvmw->pte)
184191
goto next_pte;
185192
restart:
186-
{
193+
do {
187194
pgd = pgd_offset(mm, pvmw->address);
188-
if (!pgd_present(*pgd))
189-
return false;
195+
if (!pgd_present(*pgd)) {
196+
step_forward(pvmw, PGDIR_SIZE);
197+
continue;
198+
}
190199
p4d = p4d_offset(pgd, pvmw->address);
191-
if (!p4d_present(*p4d))
192-
return false;
200+
if (!p4d_present(*p4d)) {
201+
step_forward(pvmw, P4D_SIZE);
202+
continue;
203+
}
193204
pud = pud_offset(p4d, pvmw->address);
194-
if (!pud_present(*pud))
195-
return false;
205+
if (!pud_present(*pud)) {
206+
step_forward(pvmw, PUD_SIZE);
207+
continue;
208+
}
196209

197210
pvmw->pmd = pmd_offset(pud, pvmw->address);
198211
/*
@@ -239,7 +252,8 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
239252

240253
spin_unlock(ptl);
241254
}
242-
return false;
255+
step_forward(pvmw, PMD_SIZE);
256+
continue;
243257
}
244258
if (!map_pte(pvmw))
245259
goto next_pte;
@@ -269,7 +283,9 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
269283
spin_lock(pvmw->ptl);
270284
}
271285
goto this_pte;
272-
}
286+
} while (pvmw->address < end);
287+
288+
return false;
273289
}
274290

275291
/**

0 commit comments

Comments
 (0)