From patchwork Tue Aug 23 07:50:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 12951827 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 364B3C32796 for ; Tue, 23 Aug 2022 07:50:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B04706B0078; Tue, 23 Aug 2022 03:50:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB26E6B0075; Tue, 23 Aug 2022 03:50:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A1F18D0001; Tue, 23 Aug 2022 03:50:18 -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 8B0476B0074 for ; Tue, 23 Aug 2022 03:50:18 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 65388ABB97 for ; Tue, 23 Aug 2022 07:50:18 +0000 (UTC) X-FDA: 79830084516.02.F5DCD6C Received: from out30-45.freemail.mail.aliyun.com (out30-45.freemail.mail.aliyun.com [115.124.30.45]) by imf20.hostedemail.com (Postfix) with ESMTP id 1B24B1C000B for ; Tue, 23 Aug 2022 07:50:16 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R941e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VN0jAoJ_1661241013; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VN0jAoJ_1661241013) by smtp.aliyun-inc.com; Tue, 23 Aug 2022 15:50:14 +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 1/5] mm/hugetlb: fix races when looking up a CONT-PTE size hugetlb page Date: Tue, 23 Aug 2022 15:50:01 +0800 Message-Id: <0e5d92da043d147a867f634b17acbcc97a7f0e64.1661240170.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf20.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.45 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661241018; a=rsa-sha256; cv=none; b=8lvedl7RrKkz9Qt8q8ebT0BIle72MvzF9oED84A1eVUeW3ozwHzgzeUYqtEyCIeRmNd25M 21yl9Wt2b53cXixshhaWJT9BLspokHgnOQcqX7G4trG1kQC01j+NWRjO1Rpfk9hdqaV6QB vtOv0r618jPL9OqbzKl4L0IQJQad3rY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661241018; 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=OSW0i+fDRoMzNK24XRtf+50I5fgM3Sqz/iiTXDx0zxg=; b=ksW7Sit2Da5R0AiU+nsBN33ig+o2/eNa7mXY5w9g0md0NtY88OJvBwSBg/GqMXzwsKXVui p6MzxcJgibfTh0FF9uwpaxep8qCDuJTJp7fG5RQyBB6Nz7Qu9TpdHNJa4Z9nNSgXHljvBA w7eH6eYRrccBRhk1LDM34G9QZiHGiGI= X-Rspam-User: Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf20.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.45 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com X-Stat-Signature: ksumd45pd6tbqs9cncjcep1h3zf8hkrs X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1B24B1C000B X-HE-Tag: 1661241016-367693 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. So when looking up a CONT-PTE size hugetlb page by follow_page(), it will use pte_offset_map_lock() to get the pte entry lock for the CONT-PTE size hugetlb in follow_page_pte(). However this pte entry lock is incorrect for the CONT-PTE size hugetlb, since we should use huge_pte_lock() to get the correct lock, which is mm->page_table_lock. 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, and following pte_xxx() validation is also unstable in follow_page_pte(), even though they are under the 'pte lock'. Moreover we should use huge_ptep_get() to get the pte entry value of the CONT-PTE size hugetlb, which already takes into account the subpages' dirty or young bits in case we missed the dirty or young state of the CONT-PTE size hugetlb. To fix above issues, introducing a new helper follow_huge_pte() to look up a CONT-PTE size hugetlb page, which uses huge_pte_lock() to get the correct pte entry lock to make the pte entry stable, as well as supporting non-present pte handling. Signed-off-by: Baolin Wang Signed-off-by: Mike Kravetz --- include/linux/hugetlb.h | 8 ++++++++ mm/gup.c | 11 ++++++++++ mm/hugetlb.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 3ec981a..d491138 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -207,6 +207,8 @@ 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_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, pmd_t *pmd, int flags); struct page *follow_huge_pud(struct mm_struct *mm, unsigned long address, @@ -312,6 +314,12 @@ static inline struct page *follow_huge_pd(struct vm_area_struct *vma, return NULL; } +static inline struct page *follow_huge_pte(struct vm_area_struct *vma, + unsigned long address, pmd_t *pmd, int flags) +{ + return NULL; +} + static inline struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address, pmd_t *pmd, int flags) { diff --git a/mm/gup.c b/mm/gup.c index 3b656b7..87a94f5 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -534,6 +534,17 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, if (unlikely(pmd_bad(*pmd))) return no_page_table(vma, flags); + /* + * Considering PTE level hugetlb, like continuous-PTE hugetlb on + * ARM64 architecture. + */ + if (is_vm_hugetlb_page(vma)) { + page = follow_huge_pte(vma, address, pmd, flags); + if (page) + return page; + return no_page_table(vma, flags); + } + ptep = pte_offset_map_lock(mm, pmd, address, &ptl); pte = *ptep; if (!pte_present(pte)) { diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 6c00ba1..cf742d1 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6981,6 +6981,59 @@ struct page * __weak return NULL; } +/* Support looking up a CONT-PTE size hugetlb page. */ +struct page * __weak +follow_huge_pte(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); + unsigned long size = huge_page_size(hstate); + struct page *page = NULL; + spinlock_t *ptl; + pte_t *ptep, pte; + + /* + * FOLL_PIN is not supported for follow_page(). Ordinary GUP goes via + * follow_hugetlb_page(). + */ + if (WARN_ON_ONCE(flags & FOLL_PIN)) + return NULL; + + ptep = huge_pte_offset(mm, address, size); + if (!ptep) + return NULL; + +retry: + ptl = huge_pte_lock(hstate, mm, ptep); + pte = huge_ptep_get(ptep); + if (pte_present(pte)) { + page = pte_page(pte); + if (WARN_ON_ONCE(!try_grab_page(page, flags))) { + page = NULL; + goto out; + } + } else { + if (!(flags & FOLL_MIGRATION)) { + page = NULL; + goto out; + } + + if (is_hugetlb_entry_migration(pte)) { + spin_unlock(ptl); + __migration_entry_wait_huge(ptep, ptl); + goto retry; + } + /* + * hwpoisoned entry is treated as no_page_table in + * follow_page_mask(). + */ + } +out: + spin_unlock(ptl); + return page; +} + struct page * __weak follow_huge_pmd(struct mm_struct *mm, unsigned long address, pmd_t *pmd, int flags)