From patchwork Tue Aug 16 13:05:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12944898 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80A4CC28B2B for ; Tue, 16 Aug 2022 13:06:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FCD28D0006; Tue, 16 Aug 2022 09:06:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F2CDA8D0009; Tue, 16 Aug 2022 09:06:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B32EC8D0006; Tue, 16 Aug 2022 09:06:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8CF528D0008 for ; Tue, 16 Aug 2022 09:06:24 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 509EE1C68AE for ; Tue, 16 Aug 2022 13:06:24 +0000 (UTC) X-FDA: 79805479488.20.BFBA0A0 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf17.hostedemail.com (Postfix) with ESMTP id 63195401C6 for ; Tue, 16 Aug 2022 13:06:23 +0000 (UTC) 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 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 ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf17.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660655183; a=rsa-sha256; cv=none; b=zzvVo1juO3ZY+O0jGhcIlnL4tX6LvkAIJ4BHAGUX6N9T8IW3FpYwuIkiC2nhrDfzuE7V4A C0Qqeoxup3W1IY4s5PcWYf4QYThYwvjNEP5m20U7HihtUlGG2QvYxvMGzfVgEYI31tJRt9 u5TBVlzFkAqiIcGWv+8WvIgIbp4fC4w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660655183; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i52LnC/SKqRKQBLmUqiGxyypLtHKsokj+klQMRxKyRI=; b=WkheyXeIGnjW50514XCE4yzd0uRgozxFRhHfNnkEVVcl1o6vbHEgA63ZRTduRWnKrI1fRX PbH7GwDUSvqLoQG8kUHl8pxkbedkNimAOegmsXdLvnYtQMgCLExXaYICouQDEaP54gaiig tYkh2Xlaj+cbO3LH2Ko0nmVnHIU3N5Y= Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf17.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Stat-Signature: iizeim7qzjjmwo9ebsn87x5epxabiedw X-Rspamd-Server: rspam09 X-Rspam-User: X-Rspamd-Queue-Id: 63195401C6 X-HE-Tag: 1660655183-544354 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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, /* * If the pagetables are shared don't copy or take references. - * dst_pte == src_pte is the common case of src/dest sharing. * + * 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. + * another vma. So page_count of ptep page is checked instead + * to reliably determine whether pte is shared. */ - dst_entry = huge_ptep_get(dst_pte); - if ((dst_pte == src_pte) || !huge_pte_none(dst_entry)) { + if (page_count(virt_to_page(dst_pte)) > 1) { addr |= last_addr_mask; continue; } @@ -4814,11 +4812,9 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, entry = huge_ptep_get(src_pte); dst_entry = 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))) {