From nobody Fri Apr 10 20:15:58 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 3F5EFC32771 for ; Fri, 19 Aug 2022 10:13:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347731AbiHSKNQ (ORCPT ); Fri, 19 Aug 2022 06:13:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346395AbiHSKNK (ORCPT ); Fri, 19 Aug 2022 06:13:10 -0400 Received: from out30-54.freemail.mail.aliyun.com (out30-54.freemail.mail.aliyun.com [115.124.30.54]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D319D9D56 for ; Fri, 19 Aug 2022 03:13:08 -0700 (PDT) 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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 --- 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_str= uct *vma, if (unlikely(pmd_bad(*pmd))) return no_page_table(vma, flags); =20 - ptep =3D pte_offset_map_lock(mm, pmd, address, &ptl); - pte =3D *ptep; + /* + * Considering PTE level hugetlb, like continuous-PTE hugetlb on + * ARM64 architecture. + */ + if (is_vm_hugetlb_page(vma)) { + struct hstate *hstate =3D hstate_vma(vma); + unsigned long size =3D huge_page_size(hstate); + + ptep =3D huge_pte_offset(mm, address, size); + if (!ptep) + return no_page_table(vma, flags); + + ptl =3D huge_pte_lockptr(hstate, mm, ptep); + spin_lock(ptl); + pte =3D huge_ptep_get(ptep); + } else { + ptep =3D pte_offset_map_lock(mm, pmd, address, &ptl); + pte =3D *ptep; + } + if (!pte_present(pte)) { swp_entry_t entry; /* --=20 1.8.3.1 From nobody Fri Apr 10 20:15:58 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 A976BC32771 for ; Fri, 19 Aug 2022 10:13:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347856AbiHSKNU (ORCPT ); Fri, 19 Aug 2022 06:13:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346395AbiHSKNR (ORCPT ); Fri, 19 Aug 2022 06:13:17 -0400 Received: from out30-44.freemail.mail.aliyun.com (out30-44.freemail.mail.aliyun.com [115.124.30.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA971D9D62 for ; Fri, 19 Aug 2022 03:13:09 -0700 (PDT) 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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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, uns= igned 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 add= ress, 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_are= a_struct *vma, return NULL; } =20 -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_stru= ct *vma, if (pmd_none(pmdval)) return no_page_table(vma, flags); if (pmd_huge(pmdval) && is_vm_hugetlb_page(vma)) { - page =3D follow_huge_pmd(mm, address, pmd, flags); + page =3D 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 } =20 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 =3D vma->vm_mm; + struct hstate *hstate =3D hstate_vma(vma); struct page *page =3D NULL; spinlock_t *ptl; pte_t pte; @@ -6975,8 +6977,7 @@ struct page * __weak return NULL; =20 retry: - ptl =3D pmd_lockptr(mm, pmd); - spin_lock(ptl); + ptl =3D huge_pte_lock(hstate, mm, (pte_t *)pmd); /* * make sure that the address range covered by this pmd is not * unmapped from other threads. --=20 1.8.3.1 From nobody Fri Apr 10 20:15:58 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 5A0ACC32771 for ; Fri, 19 Aug 2022 10:13:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345441AbiHSKNX (ORCPT ); Fri, 19 Aug 2022 06:13:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346395AbiHSKNU (ORCPT ); Fri, 19 Aug 2022 06:13:20 -0400 Received: from out30-44.freemail.mail.aliyun.com (out30-44.freemail.mail.aliyun.com [115.124.30.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8AB9109 for ; Fri, 19 Aug 2022 03:13:10 -0700 (PDT) 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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 =3D 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 =3D NULL; + goto out; + } + if (is_hugetlb_entry_migration(pte)) { spin_unlock(ptl); __migration_entry_wait(mm, (pte_t *)pud, ptl); --=20 1.8.3.1