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.