From patchwork Tue Aug 23 07:50:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 12951831 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 BE823C32774 for ; Tue, 23 Aug 2022 07:50:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 848776B007E; Tue, 23 Aug 2022 03:50:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7316E6B0080; Tue, 23 Aug 2022 03:50:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 583946B0081; Tue, 23 Aug 2022 03:50:22 -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 3D4696B007E for ; Tue, 23 Aug 2022 03:50:22 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 12CBE120ABF for ; Tue, 23 Aug 2022 07:50:22 +0000 (UTC) X-FDA: 79830084684.02.232259B Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com [115.124.30.133]) by imf15.hostedemail.com (Postfix) with ESMTP id 2E477A0013 for ; Tue, 23 Aug 2022 07:50:20 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;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_---0VN0m784_1661241015; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VN0m784_1661241015) by smtp.aliyun-inc.com; Tue, 23 Aug 2022 15:50:15 +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 v2 3/5] mm/hugetlb: fix races when looking up a CONT-PMD size hugetlb page Date: Tue, 23 Aug 2022 15:50:03 +0800 Message-Id: X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661241021; a=rsa-sha256; cv=none; b=7X8nB2mUYjsLKOuESg+IGJx9J2JfN+rnahVNAwkEZUehW3IVJ63iCFiYPaKmB9TL/h5f2G Z4dNyWu4KEHfxJjfh/O9a8z5LCSl7QkHo6d6zrTcoAS6e7rgX8Cn68SUd9uY8CgQlK2RSl Y+e9j3xPEb3Vw35BbBGPBMHfr9i3z7c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661241021; 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=wLnXtJu6Gw7KfbS6dWx/tongd02uxCmicQ22l8TIj7c=; b=jWIhHFWMRzhivlskGLLSuQs22mglgb/NPCuW/EElGADcj+Dj1/jFGjXbAu1gmn8Fub2NW2 AL1+L/MzEMOxh4FmOxVSTOUfhebNIK9ErMXXHhP06++3cn7TzBxVDyYN1wfWXxE8SE3TUo yDaZFilMm0ZrVsDRbLV+Jgi9wK5L85E= Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com X-Stat-Signature: io9x11kjajtz3c7yhjtdziou9sueshzp X-Rspamd-Queue-Id: 2E477A0013 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1661241020-694800 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 page lock to protect the pmd entry in follow_huge_pmd(). However this is not the correct lock for CONT-PMD size hugetlb, and the pmd entry will be unstable under the incorrect lock, which means it still can be migrated or poisoned, and can not get the correct CONT-PMD size page. 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 4b172a7..3a96f67 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -209,7 +209,7 @@ struct page *follow_huge_pd(struct vm_area_struct *vma, int flags, int pdshift); struct page *follow_huge_pte(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, int flags); -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); @@ -320,7 +320,7 @@ static inline struct page *follow_huge_pte(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 87a94f5..014accd 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -673,7 +673,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 cf742d1..2c4048a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -7035,9 +7035,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; @@ -7050,8 +7052,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.