The patch titled Subject: khugepaged: add comments for retract_page_tables() has been removed from the -mm tree. Its filename was khugepaged-enable-collapse-pmd-for-pte-mapped-thp-fix.patch This patch was dropped because it was folded into khugepaged-enable-collapse-pmd-for-pte-mapped-thp.patch ------------------------------------------------------ From: "Kirill A. Shutemov" <kirill.shutemov@xxxxxxxxxxxxxxx> Subject: khugepaged: add comments for retract_page_tables() Oleg Nesterov pointed that logic behind checks in retract_page_tables() are not obvious. Add comments to clarify the reasoning for the checks and why they are safe. Link: http://lkml.kernel.org/r/20190816145443.6ard3iilytc6jlgv@box Signed-off-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx> Cc: Song Liu <songliubraving@xxxxxx> Cc: Johannes Weiner <hannes@xxxxxxxxxxx> Cc: Oleg Nesterov <oleg@xxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- mm/khugepaged.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) --- a/mm/khugepaged.c~khugepaged-enable-collapse-pmd-for-pte-mapped-thp-fix +++ a/mm/khugepaged.c @@ -1421,7 +1421,22 @@ static void retract_page_tables(struct a i_mmap_lock_write(mapping); vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { - /* probably overkill */ + /* + * Check vma->anon_vma to exclude MAP_PRIVATE mappings that + * got written to. These VMAs are likely not worth investing + * down_write(mmap_sem) as PMD-mapping is likely to be split + * later. + * + * Not that vma->anon_vma check is racy: it can be set up after + * the check but before we took mmap_sem by the fault path. + * But page lock would prevent establishing any new ptes of the + * page, so we are safe. + * + * An alternative would be drop the check, but check that page + * table is clear before calling pmdp_collapse_flush() under + * ptl. It has higher chance to recover THP for the VMA, but + * has higher cost too. + */ if (vma->anon_vma) continue; addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); @@ -1434,9 +1449,10 @@ static void retract_page_tables(struct a continue; /* * We need exclusive mmap_sem to retract page table. - * If trylock fails we would end up with pte-mapped THP after - * re-fault. Not ideal, but it's more important to not disturb - * the system too much. + * + * We use trylock due to lock inversion: we need to acquire + * mmap_sem while holding page lock. Fault path does it in + * reverse order. Trylock is a way to avoid deadlock. */ if (down_write_trylock(&vma->vm_mm->mmap_sem)) { spinlock_t *ptl = pmd_lock(vma->vm_mm, pmd); @@ -1446,8 +1462,10 @@ static void retract_page_tables(struct a up_write(&vma->vm_mm->mmap_sem); mm_dec_nr_ptes(vma->vm_mm); pte_free(vma->vm_mm, pmd_pgtable(_pmd)); - } else + } else { + /* Try again later */ khugepaged_add_pte_mapped_thp(vma->vm_mm, addr); + } } i_mmap_unlock_write(mapping); } _ Patches currently in -mm which might be from kirill.shutemov@xxxxxxxxxxxxxxx are khugepaged-enable-collapse-pmd-for-pte-mapped-thp.patch