Skip to content

Commit 915c3a2

Browse files
Hugh DickinsSasha Levin
authored andcommitted
mm/thp: fix page_vma_mapped_walk() if THP mapped by ptes
commit a9a7504 upstream. 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/fedb8632-1798-de42-f39e-873551d5bc81@google.com Fixes: ace71a1 ("mm: introduce page_vma_mapped_walk()") Signed-off-by: Hugh Dickins <hughd@google.com> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Alistair Popple <apopple@nvidia.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Peter Xu <peterx@redhat.com> Cc: Ralph Campbell <rcampbell@nvidia.com> Cc: Wang Yugui <wangyugui@e16-tech.com> Cc: Will Deacon <will@kernel.org> Cc: Yang Shi <shy828301@gmail.com> Cc: Zi Yan <ziy@nvidia.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 90073ae commit 915c3a2

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

‎mm/page_vma_mapped.c‎

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

118+
static void step_forward(struct page_vma_mapped_walk *pvmw, unsigned long size)
119+
{
120+
pvmw->address = (pvmw->address + size) & ~(size - 1);
121+
if (!pvmw->address)
122+
pvmw->address = ULONG_MAX;
123+
}
124+
118125
/**
119126
* page_vma_mapped_walk - check if @pvmw->page is mapped in @pvmw->vma at
120127
* @pvmw->address
@@ -182,16 +189,22 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
182189
if (pvmw->pte)
183190
goto next_pte;
184191
restart:
185-
{
192+
do {
186193
pgd = pgd_offset(mm, pvmw->address);
187-
if (!pgd_present(*pgd))
188-
return false;
194+
if (!pgd_present(*pgd)) {
195+
step_forward(pvmw, PGDIR_SIZE);
196+
continue;
197+
}
189198
p4d = p4d_offset(pgd, pvmw->address);
190-
if (!p4d_present(*p4d))
191-
return false;
199+
if (!p4d_present(*p4d)) {
200+
step_forward(pvmw, P4D_SIZE);
201+
continue;
202+
}
192203
pud = pud_offset(p4d, pvmw->address);
193-
if (!pud_present(*pud))
194-
return false;
204+
if (!pud_present(*pud)) {
205+
step_forward(pvmw, PUD_SIZE);
206+
continue;
207+
}
195208

196209
pvmw->pmd = pmd_offset(pud, pvmw->address);
197210
/*
@@ -238,7 +251,8 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
238251

239252
spin_unlock(ptl);
240253
}
241-
return false;
254+
step_forward(pvmw, PMD_SIZE);
255+
continue;
242256
}
243257
if (!map_pte(pvmw))
244258
goto next_pte;
@@ -268,7 +282,9 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
268282
spin_lock(pvmw->ptl);
269283
}
270284
goto this_pte;
271-
}
285+
} while (pvmw->address < end);
286+
287+
return false;
272288
}
273289

274290
/**

0 commit comments

Comments
 (0)