Skip to content

Commit 5e41540

Browse files
mjkravetztorvalds
authored andcommitted
hugetlbfs: fix kernel BUG at fs/hugetlbfs/inode.c:444!
This bug has been experienced several times by the Oracle DB team. The BUG is in remove_inode_hugepages() as follows: /* * If page is mapped, it was faulted in after being * unmapped in caller. Unmap (again) now after taking * the fault mutex. The mutex will prevent faults * until we finish removing the page. * * This race can only happen in the hole punch case. * Getting here in a truncate operation is a bug. */ if (unlikely(page_mapped(page))) { BUG_ON(truncate_op); In this case, the elevated map count is not the result of a race. Rather it was incorrectly incremented as the result of a bug in the huge pmd sharing code. Consider the following: - Process A maps a hugetlbfs file of sufficient size and alignment (PUD_SIZE) that a pmd page could be shared. - Process B maps the same hugetlbfs file with the same size and alignment such that a pmd page is shared. - Process B then calls mprotect() to change protections for the mapping with the shared pmd. As a result, the pmd is 'unshared'. - Process B then calls mprotect() again to chage protections for the mapping back to their original value. pmd remains unshared. - Process B then forks and process C is created. During the fork process, we do dup_mm -> dup_mmap -> copy_page_range to copy page tables. Copying page tables for hugetlb mappings is done in the routine copy_hugetlb_page_range. In copy_hugetlb_page_range(), the destination pte is obtained by: dst_pte = huge_pte_alloc(dst, addr, sz); If pmd sharing is possible, the returned pointer will be to a pte in an existing page table. In the situation above, process C could share with either process A or process B. Since process A is first in the list, the returned pte is a pointer to a pte in process A's page table. However, the check for pmd sharing in copy_hugetlb_page_range is: /* If the pagetables are shared don't copy or take references */ if (dst_pte == src_pte) continue; Since process C is sharing with process A instead of process B, the above test fails. The code in copy_hugetlb_page_range which follows assumes dst_pte points to a huge_pte_none pte. It copies the pte entry from src_pte to dst_pte and increments this map count of the associated page. This is how we end up with an elevated map count. To solve, check the dst_pte entry for huge_pte_none. If !none, this implies PMD sharing so do not copy. Link: http://lkml.kernel.org/r/20181105212315.14125-1-mike.kravetz@oracle.com Fixes: c5c9942 ("fix hugepages leak due to pagetable page sharing") Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com> Reviewed-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Hugh Dickins <hughd@google.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: "Kirill A . Shutemov" <kirill.shutemov@linux.intel.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Prakash Sangappa <prakash.sangappa@oracle.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8fcb231 commit 5e41540

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

‎mm/hugetlb.c‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,7 +3233,7 @@ static int is_hugetlb_entry_hwpoisoned(pte_t pte)
32333233
int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
32343234
struct vm_area_struct *vma)
32353235
{
3236-
pte_t *src_pte, *dst_pte, entry;
3236+
pte_t *src_pte, *dst_pte, entry, dst_entry;
32373237
struct page *ptepage;
32383238
unsigned long addr;
32393239
int cow;
@@ -3261,15 +3261,30 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
32613261
break;
32623262
}
32633263

3264-
/* If the pagetables are shared don't copy or take references */
3265-
if (dst_pte == src_pte)
3264+
/*
3265+
* If the pagetables are shared don't copy or take references.
3266+
* dst_pte == src_pte is the common case of src/dest sharing.
3267+
*
3268+
* However, src could have 'unshared' and dst shares with
3269+
* another vma. If dst_pte !none, this implies sharing.
3270+
* Check here before taking page table lock, and once again
3271+
* after taking the lock below.
3272+
*/
3273+
dst_entry = huge_ptep_get(dst_pte);
3274+
if ((dst_pte == src_pte) || !huge_pte_none(dst_entry))
32663275
continue;
32673276

32683277
dst_ptl = huge_pte_lock(h, dst, dst_pte);
32693278
src_ptl = huge_pte_lockptr(h, src, src_pte);
32703279
spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
32713280
entry = huge_ptep_get(src_pte);
3272-
if (huge_pte_none(entry)) { /* skip none entry */
3281+
dst_entry = huge_ptep_get(dst_pte);
3282+
if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) {
3283+
/*
3284+
* Skip if src entry none. Also, skip in the
3285+
* unlikely case dst entry !none as this implies
3286+
* sharing with another vma.
3287+
*/
32733288
;
32743289
} else if (unlikely(is_hugetlb_entry_migration(entry) ||
32753290
is_hugetlb_entry_hwpoisoned(entry))) {

0 commit comments

Comments
 (0)