Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3108181imu; Mon, 19 Nov 2018 10:43:52 -0800 (PST) X-Google-Smtp-Source: AJdET5fi1zJCMu/gOBJo5yuV9Cz2gpnSGZK5LLdZROKrbP4qsWFA85omDfXfFPiFvKK9CUkKYH3B X-Received: by 2002:a63:65c7:: with SMTP id z190mr21173947pgb.249.1542653032636; Mon, 19 Nov 2018 10:43:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542653032; cv=none; d=google.com; s=arc-20160816; b=jcAcx5WcG22c+7AZZfrO3v1hOOWNZo7AokJKs1yKdVoOcDV67zNY81boYrnp7dKvQC +DZvcDrRn8AIEoaxKnV/JW7+WTl9q/yFFTIaJKFTGlQRO+sjMcPgKa8GPCU4flY6UJdk 8jBRnu3vsM0pbEgmWIZEOhQD5yVig1GrXEmXd6V40aoHWd54cc4oeb3a7czePhEzwGvU O13ihrrybMR+qCDKx8U2TwmJvz2vtxiTwMLt/32/N57ep42bH82tr+fk23I5GPrIwkmf e+iIa+GypshhMDdcstJc6q1Xd/JS3PhHY1v+OtDxozwAyWidoqbJm0Q7z6vJoNlXTIJz vQ+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=mg/JEU4uaeCRg0BhK4TLjHoBSB7I2ls85xp+AD407oE=; b=GC2ioqYFfg/G9YC80zxDH/PZGfOLDu+qs6+VlTpyT+N1jJlAbKBu88f7m1a6q2XnTn 9hr/36LKzHw+CbpOiTe9EYnGKzwrd2TKhlhs0E9Ww8qq+LV4/GCv9K3L79ypsA+4fHi+ Mb6Kp1m0G5zGaeLqpKJwVVAOitjOPExerrklg6iuxhSx2/lJM7i/N5f9aWTc+zWWOahx cIjXHuizUmVQCGd1bLbw53SC2Ua9fmHX87kUgiwO1h3MZqF9f6sreBlimM7MDCHKlMi2 esneejTpGh7O0v30rrzPl7K8txlXJ2a/w9ZL62zzysGaY5OoW48+qa19zJ7XfDSe+PNk CjMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1VMYiLpS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 204si28071259pfu.273.2018.11.19.10.43.37; Mon, 19 Nov 2018 10:43:52 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1VMYiLpS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732245AbeKTDBo (ORCPT + 99 others); Mon, 19 Nov 2018 22:01:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:33740 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732210AbeKTDBm (ORCPT ); Mon, 19 Nov 2018 22:01:42 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EDFA7208E4; Mon, 19 Nov 2018 16:37:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542645453; bh=QIIgRc+EgH61k/s9V3+llpbVX279T97paRBxNlX3GX4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1VMYiLpS+m1zA+ffndRF8CgbY4FoKRq5EZpNTpwHNYeO4vk7JPcqV1M/r4lRGd/Jn uA7RU/TEg+gqUht6cFJEkKClrxRh194G8Rp81byx+JkwgYuAGHSYyLnRP7Qi4Cidc1 F0sHnQOFIpDxQHo+UoovNh5v38n94Evf6UdFbMHI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mike Kravetz , Naoya Horiguchi , Michal Hocko , Hugh Dickins , Andrea Arcangeli , "Kirill A . Shutemov" , Davidlohr Bueso , Prakash Sangappa , Andrew Morton , Linus Torvalds Subject: [PATCH 4.19 168/205] hugetlbfs: fix kernel BUG at fs/hugetlbfs/inode.c:444! Date: Mon, 19 Nov 2018 17:27:55 +0100 Message-Id: <20181119162640.232171384@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181119162616.586062722@linuxfoundation.org> References: <20181119162616.586062722@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Mike Kravetz commit 5e41540c8a0f0e98c337dda8b391e5dda0cde7cf upstream. 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: c5c99429fa57 ("fix hugepages leak due to pagetable page sharing") Signed-off-by: Mike Kravetz Reviewed-by: Naoya Horiguchi Cc: Michal Hocko Cc: Hugh Dickins Cc: Andrea Arcangeli Cc: "Kirill A . Shutemov" Cc: Davidlohr Bueso Cc: Prakash Sangappa Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/hugetlb.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3233,7 +3233,7 @@ static int is_hugetlb_entry_hwpoisoned(p int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma) { - pte_t *src_pte, *dst_pte, entry; + pte_t *src_pte, *dst_pte, entry, dst_entry; struct page *ptepage; unsigned long addr; int cow; @@ -3261,15 +3261,30 @@ int copy_hugetlb_page_range(struct mm_st break; } - /* If the pagetables are shared don't copy or take references */ - if (dst_pte == src_pte) + /* + * If the pagetables are shared don't copy or take references. + * dst_pte == src_pte is the common case of src/dest sharing. + * + * However, src could have 'unshared' and dst shares with + * another vma. If dst_pte !none, this implies sharing. + * Check here before taking page table lock, and once again + * after taking the lock below. + */ + dst_entry = huge_ptep_get(dst_pte); + if ((dst_pte == src_pte) || !huge_pte_none(dst_entry)) continue; dst_ptl = huge_pte_lock(h, dst, dst_pte); src_ptl = huge_pte_lockptr(h, src, src_pte); spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); entry = huge_ptep_get(src_pte); - if (huge_pte_none(entry)) { /* skip none entry */ + dst_entry = huge_ptep_get(dst_pte); + if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) { + /* + * Skip if src entry none. Also, skip in the + * unlikely case dst entry !none as this implies + * sharing with another vma. + */ ; } else if (unlikely(is_hugetlb_entry_migration(entry) || is_hugetlb_entry_hwpoisoned(entry))) {