Skip to content

Commit 2406c45

Browse files
Hugh Dickinsgregkh
authored andcommitted
khugepaged: retract_page_tables() remember to test exit
commit 18e7760 upstream. Only once have I seen this scenario (and forgot even to notice what forced the eventual crash): a sequence of "BUG: Bad page map" alerts from vm_normal_page(), from zap_pte_range() servicing exit_mmap(); pmd:00000000, pte values corresponding to data in physical page 0. The pte mappings being zapped in this case were supposed to be from a huge page of ext4 text (but could as well have been shmem): my belief is that it was racing with collapse_file()'s retract_page_tables(), found *pmd pointing to a page table, locked it, but *pmd had become 0 by the time start_pte was decided. In most cases, that possibility is excluded by holding mmap lock; but exit_mmap() proceeds without mmap lock. Most of what's run by khugepaged checks khugepaged_test_exit() after acquiring mmap lock: khugepaged_collapse_pte_mapped_thps() and hugepage_vma_revalidate() do so, for example. But retract_page_tables() did not: fix that. The fix is for retract_page_tables() to check khugepaged_test_exit(), after acquiring mmap lock, before doing anything to the page table. Getting the mmap lock serializes with __mmput(), which briefly takes and drops it in __khugepaged_exit(); then the khugepaged_test_exit() check on mm_users makes sure we don't touch the page table once exit_mmap() might reach it, since exit_mmap() will be proceeding without mmap lock, not expecting anyone to be racing with it. Fixes: f3f0e1d ("khugepaged: add support of collapse for tmpfs/shmem pages") Signed-off-by: Hugh Dickins <hughd@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Song Liu <songliubraving@fb.com> Cc: <stable@vger.kernel.org> [4.8+] Link: http://lkml.kernel.org/r/alpine.LSU.2.11.2008021215400.27773@eggly.anvils Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 014ec97 commit 2406c45

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

mm/khugepaged.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ static void collect_mm_slot(struct mm_slot *mm_slot)
12511251
static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
12521252
{
12531253
struct vm_area_struct *vma;
1254+
struct mm_struct *mm;
12541255
unsigned long addr;
12551256
pmd_t *pmd, _pmd;
12561257

@@ -1264,7 +1265,8 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
12641265
continue;
12651266
if (vma->vm_end < addr + HPAGE_PMD_SIZE)
12661267
continue;
1267-
pmd = mm_find_pmd(vma->vm_mm, addr);
1268+
mm = vma->vm_mm;
1269+
pmd = mm_find_pmd(mm, addr);
12681270
if (!pmd)
12691271
continue;
12701272
/*
@@ -1273,14 +1275,16 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
12731275
* re-fault. Not ideal, but it's more important to not disturb
12741276
* the system too much.
12751277
*/
1276-
if (down_write_trylock(&vma->vm_mm->mmap_sem)) {
1277-
spinlock_t *ptl = pmd_lock(vma->vm_mm, pmd);
1278-
/* assume page table is clear */
1279-
_pmd = pmdp_collapse_flush(vma, addr, pmd);
1280-
spin_unlock(ptl);
1281-
up_write(&vma->vm_mm->mmap_sem);
1282-
mm_dec_nr_ptes(vma->vm_mm);
1283-
pte_free(vma->vm_mm, pmd_pgtable(_pmd));
1278+
if (down_write_trylock(&mm->mmap_sem)) {
1279+
if (!khugepaged_test_exit(mm)) {
1280+
spinlock_t *ptl = pmd_lock(mm, pmd);
1281+
/* assume page table is clear */
1282+
_pmd = pmdp_collapse_flush(vma, addr, pmd);
1283+
spin_unlock(ptl);
1284+
mm_dec_nr_ptes(mm);
1285+
pte_free(mm, pmd_pgtable(_pmd));
1286+
}
1287+
up_write(&mm->mmap_sem);
12841288
}
12851289
}
12861290
i_mmap_unlock_write(mapping);

0 commit comments

Comments
 (0)