From patchwork Fri Aug 19 10:12:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 12948678 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 A9AC9C32771 for ; Fri, 19 Aug 2022 10:13:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 279598D0003; Fri, 19 Aug 2022 06:13:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 229238D0002; Fri, 19 Aug 2022 06:13:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 118798D0003; Fri, 19 Aug 2022 06:13:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 048168D0002 for ; Fri, 19 Aug 2022 06:13:11 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D20D8AADAD for ; Fri, 19 Aug 2022 10:13:10 +0000 (UTC) X-FDA: 79815929340.05.B1B5C36 Received: from out30-44.freemail.mail.aliyun.com (out30-44.freemail.mail.aliyun.com [115.124.30.44]) by imf02.hostedemail.com (Postfix) with ESMTP id 0B53E80019 for ; Fri, 19 Aug 2022 10:13:09 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R791e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046056;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VMfbQOG_1660903985; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VMfbQOG_1660903985) by smtp.aliyun-inc.com; Fri, 19 Aug 2022 18:13:05 +0800 From: Baolin Wang To: akpm@linux-foundation.org, songmuchun@bytedance.com, mike.kravetz@oracle.com Cc: baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] mm/gup: fix races when looking up a CONT-PTE size hugetlb page Date: Fri, 19 Aug 2022 18:12:56 +0800 Message-Id: <0f3df6604059011bf78a286c2cf5da5c4b41ccb1.1660902741.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660903990; a=rsa-sha256; cv=none; b=UeTuNtBIe+jQCpk0UdZtm5/uvJKBebdHnKxlv0PaH5Y4iqWvsCCnw7WKIzn26tZTWWhjO+ 6HaF0Lr1bMQgzRmXGl/Ts4USX22a5H7goVOrxsVElXcUwnFHBe+w+Il6yLb1IwtR6hlCF3 cAyyWVKWk22rqD76GWnKgEnui25Dxb0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf02.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.44 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660903990; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to:in-reply-to: references:references:references; bh=nbJ6bipwKd2S8t+QD+un8f3UrG7DSE+W+7nphjdEydk=; b=LNFNlMgqQ9RH9Kc1OSTtIn0Rb38nb1/q+sYoZlks/7vXf6ypk1ja1Dc390hPq49/+/P/gA 4MEWLhARpZOUgF5B+DaPh83gxZosowzZBGjfSBDdmquuvKC/medR/oLrtLXkiwDEcDZ/LD GXJIZEYqOjqcP6nN120jAitm5Zr4S8Q= X-Rspamd-Server: rspam06 X-Rspam-User: X-Stat-Signature: bxgb67emkr5c43w5j5nps54iz1g3ftqy Authentication-Results: imf02.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf02.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.44 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com X-Rspamd-Queue-Id: 0B53E80019 X-HE-Tag: 1660903989-167231 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: On some architectures (like ARM64), it can support CONT-PTE/PMD size hugetlb, which means it can support not only PMD/PUD size hugetlb: 2M and 1G, but also CONT-PTE/PMD size: 64K and 32M if a 4K page size specified. When looking up a CONT-PTE size hugetlb page by follow_page(), it will use pte_offset_map_lock() to get the pte lock for the CONT-PTE size hugetlb in follow_page_pte(). However this pte lock is incorrect for the CONT-PTE size hugetlb, since we should use mm->page_table_lock by huge_pte_lockptr(). That means the pte entry of the CONT-PTE size hugetlb under current pte lock is unstable in follow_page_pte(), we can continue to migrate or poison the pte entry of the CONT-PTE size hugetlb, which can cause some potential race issues, since the pte entry is unstable, and following pte_xxx() validation is also incorrect in follow_page_pte(), even though they are under the 'pte lock'. To fix this issue, we should validate if it is a CONT-PTE size VMA at first, and use huge_pte_lockptr() to get the correct pte lock and get the pte value by huge_ptep_get() to make the pte entry stable under the correct pte lock. Signed-off-by: Baolin Wang Reported-by: kernel test robot Reported-by: kernel test robot --- mm/gup.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 5aa7531..3b2fa86 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -534,8 +534,26 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, if (unlikely(pmd_bad(*pmd))) return no_page_table(vma, flags); - ptep = pte_offset_map_lock(mm, pmd, address, &ptl); - pte = *ptep; + /* + * Considering PTE level hugetlb, like continuous-PTE hugetlb on + * ARM64 architecture. + */ + if (is_vm_hugetlb_page(vma)) { + struct hstate *hstate = hstate_vma(vma); + unsigned long size = huge_page_size(hstate); + + ptep = huge_pte_offset(mm, address, size); + if (!ptep) + return no_page_table(vma, flags); + + ptl = huge_pte_lockptr(hstate, mm, ptep); + spin_lock(ptl); + pte = huge_ptep_get(ptep); + } else { + ptep = pte_offset_map_lock(mm, pmd, address, &ptl); + pte = *ptep; + } + if (!pte_present(pte)) { swp_entry_t entry; /*