From nobody Sat Apr 11 04:00:49 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56FBFC25B0E for ; Tue, 16 Aug 2022 13:06:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235451AbiHPNGp (ORCPT ); Tue, 16 Aug 2022 09:06:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234359AbiHPNGX (ORCPT ); Tue, 16 Aug 2022 09:06:23 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D107DD108 for ; Tue, 16 Aug 2022 06:06:22 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4M6WXh6pW3z1M8ym; Tue, 16 Aug 2022 21:03:00 +0800 (CST) Received: from huawei.com (10.175.124.27) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 16 Aug 2022 21:06:20 +0800 From: Miaohe Lin To: , , CC: , , Subject: [PATCH 6/6] mm/hugetlb: make detecting shared pte more reliable Date: Tue, 16 Aug 2022 21:05:53 +0800 Message-ID: <20220816130553.31406-7-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20220816130553.31406-1-linmiaohe@huawei.com> References: <20220816130553.31406-1-linmiaohe@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If the pagetables are shared, we shouldn't copy or take references. Since src could have unshared and dst shares with another vma, huge_pte_none() is thus used to determine whether dst_pte is shared. But this check isn't reliable. A shared pte could have pte none in pagetable in fact. The page count of ptep page should be checked here in order to reliably determine whether pte is shared. Signed-off-by: Miaohe Lin Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index e1356ad57087..25db6d07479e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4795,15 +4795,13 @@ int copy_hugetlb_page_range(struct mm_struct *dst, = struct mm_struct *src, =20 /* * If the pagetables are shared don't copy or take references. - * dst_pte =3D=3D src_pte is the common case of src/dest sharing. * + * dst_pte =3D=3D 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. + * another vma. So page_count of ptep page is checked instead + * to reliably determine whether pte is shared. */ - dst_entry =3D huge_ptep_get(dst_pte); - if ((dst_pte =3D=3D src_pte) || !huge_pte_none(dst_entry)) { + if (page_count(virt_to_page(dst_pte)) > 1) { addr |=3D last_addr_mask; continue; } @@ -4814,11 +4812,9 @@ int copy_hugetlb_page_range(struct mm_struct *dst, s= truct mm_struct *src, entry =3D huge_ptep_get(src_pte); dst_entry =3D huge_ptep_get(dst_pte); again: - if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) { + if (huge_pte_none(entry)) { /* - * Skip if src entry none. Also, skip in the - * unlikely case dst entry !none as this implies - * sharing with another vma. + * Skip if src entry none. */ ; } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) { --=20 2.23.0