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; /* From patchwork Fri Aug 19 10:12:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 12948686 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 48DD0C32771 for ; Fri, 19 Aug 2022 10:17:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9F888D0003; Fri, 19 Aug 2022 06:17:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D4E048D0002; Fri, 19 Aug 2022 06:17:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C16828D0003; Fri, 19 Aug 2022 06:17:07 -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 AEB968D0002 for ; Fri, 19 Aug 2022 06:17:07 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 89B4780267 for ; Fri, 19 Aug 2022 10:17:07 +0000 (UTC) X-FDA: 79815939294.17.624ABC6 Received: from out30-44.freemail.mail.aliyun.com (out30-44.freemail.mail.aliyun.com [115.124.30.44]) by imf22.hostedemail.com (Postfix) with ESMTP id 11A64C0024 for ; Fri, 19 Aug 2022 10:13:09 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045176;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VMfbQOi_1660903986; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VMfbQOi_1660903986) by smtp.aliyun-inc.com; Fri, 19 Aug 2022 18:13:06 +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 2/3] mm/hugetlb: fix races when looking up a CONT-PMD size hugetlb page Date: Fri, 19 Aug 2022 18:12:57 +0800 Message-Id: <673a1f7c1ae994fa6bdcbcf5db54afa0d7184f70.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-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=LtB6V6qaAHtqSKarg53DGwTFBpxdWEQUXI1z6BiyC/U=; b=juItOIg4ciPoQ5i4AliGQzqiStywJGC/fFeSOm9H1seOcON72WyX4h/BdoZ3mHgh6Ehp4M 37DxEz2eelXD1q/e1c50PF979s48/maMZwF3fUnbusD2jCkC7KMLcFY/ETIGl9DSmQdAdB 4zTV05yqxpqTg8rLeLx4Da/RXsqe2es= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf22.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660903990; a=rsa-sha256; cv=none; b=nA6/g+Z4MWaeEOHw9akTynuH+9RNt2IM5elnrMljQ1WEAdrXztDeggpAFMRJzcXAYcLyED 1MkL/RitpVEuTo6JaxNMizstRjiTwVCHhamy3E0HIGRbscwSj3LiT6Q3Am0jKCB7jXAmR0 Ajt7gA7G5bOsJ0bek9fR/cGnpKz8jxk= X-Rspam-User: Authentication-Results: imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf22.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-Stat-Signature: w5mkdq4n9pmhxcubf9k8kpziidrur5w7 X-Rspamd-Queue-Id: 11A64C0024 X-Rspamd-Server: rspam05 X-HE-Tag: 1660903989-736166 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-PMD size hugetlb page by follow_page(), it will always use the pmd lock to protect the pmd entry in follow_huge_pmd(). However this is not the correct lock for CONT-PMD size hugetlb, instead we should use mm->page_table_lock for the CONT-PMD size hugetlb to make sure the pmd entry is stable. Thus changing to use huge_pte_lock() to get the correct pmd entry lock for CONT-PMD size hugetlb to fix the potential race. Signed-off-by: Baolin Wang --- include/linux/hugetlb.h | 4 ++-- mm/gup.c | 2 +- mm/hugetlb.c | 7 ++++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 3ec981a..dbc2773 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -207,7 +207,7 @@ struct page *follow_huge_addr(struct mm_struct *mm, unsigned long address, struct page *follow_huge_pd(struct vm_area_struct *vma, unsigned long address, hugepd_t hpd, int flags, int pdshift); -struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address, +struct page *follow_huge_pmd(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, int flags); struct page *follow_huge_pud(struct mm_struct *mm, unsigned long address, pud_t *pud, int flags); @@ -312,7 +312,7 @@ static inline struct page *follow_huge_pd(struct vm_area_struct *vma, return NULL; } -static inline struct page *follow_huge_pmd(struct mm_struct *mm, +static inline struct page *follow_huge_pmd(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, int flags) { return NULL; diff --git a/mm/gup.c b/mm/gup.c index 3b2fa86..0856964 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -680,7 +680,7 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma, if (pmd_none(pmdval)) return no_page_table(vma, flags); if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) { - page = follow_huge_pmd(mm, address, pmd, flags); + page = follow_huge_pmd(vma, address, pmd, flags); if (page) return page; return no_page_table(vma, flags); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ea1c7bf..efb53ba 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6960,9 +6960,11 @@ struct page * __weak } struct page * __weak -follow_huge_pmd(struct mm_struct *mm, unsigned long address, +follow_huge_pmd(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, int flags) { + struct mm_struct *mm = vma->vm_mm; + struct hstate *hstate = hstate_vma(vma); struct page *page = NULL; spinlock_t *ptl; pte_t pte; @@ -6975,8 +6977,7 @@ struct page * __weak return NULL; retry: - ptl = pmd_lockptr(mm, pmd); - spin_lock(ptl); + ptl = huge_pte_lock(hstate, mm, (pte_t *)pmd); /* * make sure that the address range covered by this pmd is not * unmapped from other threads. From patchwork Fri Aug 19 10:12:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 12948679 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 B367DC32772 for ; Fri, 19 Aug 2022 10:13:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 538828D0005; Fri, 19 Aug 2022 06:13:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C2028D0002; Fri, 19 Aug 2022 06:13:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B00D8D0005; Fri, 19 Aug 2022 06:13:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2AEE08D0002 for ; Fri, 19 Aug 2022 06:13:13 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0119D14025A for ; Fri, 19 Aug 2022 10:13:12 +0000 (UTC) X-FDA: 79815929424.28.263274B Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com [115.124.30.133]) by imf24.hostedemail.com (Postfix) with ESMTP id 3B299180025 for ; Fri, 19 Aug 2022 10:13:10 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045192;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VMfcxWM_1660903986; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VMfcxWM_1660903986) by smtp.aliyun-inc.com; Fri, 19 Aug 2022 18:13:07 +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 3/3] mm/hugetlb: add FOLL_MIGRATION validation before waiting for a migration entry Date: Fri, 19 Aug 2022 18:12:58 +0800 Message-Id: <0ec5d75781452af85e6226e1b5940ff557c6f5e7.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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660903991; 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=LY4hC85K4yJkKfzR8/I6etsFIvV6kzER/hMw22TDn/w=; b=kuaS2lOmE8LgSh9NpszQPq9N5vMH/O76Zm6/tticofjxgH2l/cOsON6W5Rk8Lo23w1xH/a d7zMZaHYmVoL0xz88WuG560zLa42RnjkPfxOSQMsoR+14L1QWnLjM+CvWEukvQ3xLNJm7s KyOeCWU0mxQoIbyX9Hdtrc8kKqwPdXg= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf24.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660903991; a=rsa-sha256; cv=none; b=wPZRsu5DGSd+ovnrE3icFVy9b0Ktwbz3Va/ClE3V2x8sz04s2GSYZEmB5Y+Mkcv/EObJbA rRQTv9fjaRNxe2bgnxcFoHhAUuqYhJU+IgFl2sJfgUJ8UfxF22pMfixgzGybQ043ToBnee lDAzlnPRcZn645cf06l1nJIuyIITp3M= Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf24.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 3B299180025 X-Stat-Signature: zmpfyai56t3erxjguxsc7hhyg7drg46r X-Rspam-User: X-HE-Tag: 1660903990-251992 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: The hugetlb should keep the same logics with normal page when waiting for a migration pte entry, that means we should also validate if the FOLL_MIGRATION flag is set before waiting for a migration pte entry of a hugetlb page. Signed-off-by: Baolin Wang --- mm/hugetlb.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index efb53ba..fac1b33 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -7000,6 +7000,11 @@ struct page * __weak goto out; } } else { + if (!(flags & FOLL_MIGRATION)) { + page = NULL; + goto out; + } + if (is_hugetlb_entry_migration(pte)) { spin_unlock(ptl); __migration_entry_wait_huge((pte_t *)pmd, ptl); @@ -7038,6 +7043,11 @@ struct page * __weak goto out; } } else { + if (!(flags & FOLL_MIGRATION)) { + page = NULL; + goto out; + } + if (is_hugetlb_entry_migration(pte)) { spin_unlock(ptl); __migration_entry_wait(mm, (pte_t *)pud, ptl);