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) From patchwork Tue Aug 23 07:50:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 12951828 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 68573C32789 for ; Tue, 23 Aug 2022 07:50:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5856B6B0075; Tue, 23 Aug 2022 03:50:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 510D16B007B; Tue, 23 Aug 2022 03:50:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D5E68D0001; Tue, 23 Aug 2022 03:50:20 -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 2C37A6B0075 for ; Tue, 23 Aug 2022 03:50:20 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 06E6A1414CB for ; Tue, 23 Aug 2022 07:50:20 +0000 (UTC) X-FDA: 79830084600.18.E9D76DC Received: from out30-45.freemail.mail.aliyun.com (out30-45.freemail.mail.aliyun.com [115.124.30.45]) by imf30.hostedemail.com (Postfix) with ESMTP id 4BC0180030 for ; Tue, 23 Aug 2022 07:50:18 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;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_---0VN0jAoy_1661241014; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VN0jAoy_1661241014) 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 2/5] mm/hugetlb: use PTE page lock to protect CONT-PTE entries Date: Tue, 23 Aug 2022 15:50:02 +0800 Message-Id: <064489292e6e224ef4406af990c7cdc3c054ca77.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; imf30.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf30.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=1661241019; a=rsa-sha256; cv=none; b=yFstALDXBPou89VhRQcDhT3lekd1IWFr4u8spFc/0fNsTd9C5yjvK+3pgvawEF6+kWfqqN 2lrdJg+BTNJUD3CZddxoZrjWLD+YkjRVw0mLGL/ZKtzAKJK3amKi7AmJu7kbQTyktnP7GH BSkfZR6Igua9CJnteuCmloUrzhVcLGI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661241019; 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=Vxq4jQjTVRmCFHX+ypx5+Q7BgjT32/Ki4HshQDYrp+U=; b=JEVkOEI5utphwiWUG1spOsbVW9yA5D7BSLB9x2i0hyu2UacSJvn/j8K+j8jl2OLxawGkL8 6Je+guVB1BujefBs6bw4oGCwB49XzHV8oAp+gU1rVSjFKnYIAxnMaQLZOF6EERuq03lBcL XGKEaZJ17+6l+OmMZxAEsZrvYliJouM= X-Rspam-User: Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf30.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: dwk6csw5p3xxq9o8qzfz7n1wrm87ogoa X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 4BC0180030 X-HE-Tag: 1661241018-103372 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: Considering the pte entries of a CONT-PTE hugetlb can not span on multiple PTEs, we can change to use the PTE page lock, which can be much finer grain that lock in the mm. Signed-off-by: Baolin Wang --- include/linux/hugetlb.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index d491138..4b172a7 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -892,9 +892,23 @@ static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask) static inline spinlock_t *huge_pte_lockptr(struct hstate *h, struct mm_struct *mm, pte_t *pte) { - if (huge_page_size(h) == PMD_SIZE) - return pmd_lockptr(mm, (pmd_t *) pte); VM_BUG_ON(huge_page_size(h) == PAGE_SIZE); + + if (huge_page_size(h) == PMD_SIZE) { + return pmd_lockptr(mm, (pmd_t *) pte); + } else if (huge_page_size(h) < PMD_SIZE) { + unsigned long mask = ~(PTRS_PER_PTE * sizeof(pte_t) - 1); + struct page *page = + virt_to_page((void *)((unsigned long)pte & mask)); + + /* + * Considering CONT-PTE size hugetlb, since the CONT-PTE + * entry can not span multiple PTEs, we can use the PTE + * page lock to get a fine grained lock. + */ + return ptlock_ptr(page); + } + return &mm->page_table_lock; } 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. From patchwork Tue Aug 23 07:50:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 12951829 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 C42ADC32774 for ; Tue, 23 Aug 2022 07:50:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CB38C6B007B; Tue, 23 Aug 2022 03:50:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C129C8D0001; Tue, 23 Aug 2022 03:50:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB21E6B007E; Tue, 23 Aug 2022 03:50:20 -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 9337B6B007B for ; Tue, 23 Aug 2022 03:50:20 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 6BF331A0973 for ; Tue, 23 Aug 2022 07:50:20 +0000 (UTC) X-FDA: 79830084600.28.316E8CE Received: from out199-8.us.a.mail.aliyun.com (out199-8.us.a.mail.aliyun.com [47.90.199.8]) by imf15.hostedemail.com (Postfix) with ESMTP id 9E076A0015 for ; Tue, 23 Aug 2022 07:50:19 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045168;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VN0jApx_1661241015; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VN0jApx_1661241015) by smtp.aliyun-inc.com; Tue, 23 Aug 2022 15:50:16 +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 4/5] mm/hugetlb: use PMD page lock to protect CONT-PTE entries Date: Tue, 23 Aug 2022 15:50:04 +0800 Message-Id: <88c8a8c68d87429f0fc48e81100f19b71f6e664f.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661241020; a=rsa-sha256; cv=none; b=ERAcEk02KAYiJ96Aoxf6q+Myb3kxTozfiYR79/5kE9zB1wQd32sgDD/ZSdWencf6YwwRgv cT5bliqCDmIGuyF1cvXdekffPIC5REfBGqUs6H8NVSCbJN9fcmzWEMYwE91ilmDBvxEiwz mbTiTLyQpH/2MbHtEQk8FQpw8DyKz30= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 47.90.199.8 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661241020; 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=d2Ha4WQMIISYnm+R/5D+HZGmRRVnm0vkSBD0oMctpi4=; b=tKmhu77lFJqANARHofRYAlua3hDlUo14lpcHY8XAO5xf0TQhld9NTQH8ek1q94k2hrAPQ1 ds6Lg630Qmb1CwDY4VXUSHJWijnSU/+eNdES/Rgz9Jc3c1UzbBDRD1wLNWVHk6zR1ecE97 qlNWN9QLRqr1hk3Wj/WVculF5kkXoLI= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9E076A0015 Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 47.90.199.8 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com X-Stat-Signature: 7ca5fxtk87ainout5ch6mqxghj3f669c X-Rspam-User: X-HE-Tag: 1661241019-372476 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: Considering the pmd entries of a CONT-PMD hugetlb can not span on multiple PMDs, we can change to use the PMD page lock, which can be much finer grain that lock in the mm. Signed-off-by: Baolin Wang --- include/linux/hugetlb.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 3a96f67..d4803a89 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -892,9 +892,17 @@ static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask) static inline spinlock_t *huge_pte_lockptr(struct hstate *h, struct mm_struct *mm, pte_t *pte) { - VM_BUG_ON(huge_page_size(h) == PAGE_SIZE); + unsigned long hp_size = huge_page_size(h); - if (huge_page_size(h) == PMD_SIZE) { + VM_BUG_ON(hp_size == PAGE_SIZE); + + /* + * Considering CONT-PMD size hugetlb, since the CONT-PMD entry + * can not span multiple PMDs, then we can use the fine grained + * PMD page lock. + */ + if (hp_size == PMD_SIZE || + (hp_size > PMD_SIZE && hp_size < PUD_SIZE)) { return pmd_lockptr(mm, (pmd_t *) pte); } else if (huge_page_size(h) < PMD_SIZE) { unsigned long mask = ~(PTRS_PER_PTE * sizeof(pte_t) - 1); From patchwork Tue Aug 23 07:50:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 12951830 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 3A43CC32789 for ; Tue, 23 Aug 2022 07:50:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB6A16B007D; Tue, 23 Aug 2022 03:50:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C9B96B007E; Tue, 23 Aug 2022 03:50:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F7346B0080; Tue, 23 Aug 2022 03:50:21 -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 69BA16B007D for ; Tue, 23 Aug 2022 03:50:21 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 477BD409AD for ; Tue, 23 Aug 2022 07:50:21 +0000 (UTC) X-FDA: 79830084642.11.58DB21F Received: from out30-56.freemail.mail.aliyun.com (out30-56.freemail.mail.aliyun.com [115.124.30.56]) by imf09.hostedemail.com (Postfix) with ESMTP id 71A8F14003F for ; Tue, 23 Aug 2022 07:50:20 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R201e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VN0l0NV_1661241016; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VN0l0NV_1661241016) by smtp.aliyun-inc.com; Tue, 23 Aug 2022 15:50:16 +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 5/5] mm/hugetlb: add FOLL_MIGRATION validation before waiting for a migration entry Date: Tue, 23 Aug 2022 15:50:05 +0800 Message-Id: <2aa2856012baa9f7251c993ee0f1406a51185a83.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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661241020; 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=2C7YTbTQkBY6EHwaiMA6P10lGvhEfSZEECYlfbhGS7E=; b=z8fX0fa5SViCs+NTlf8QIew4jM+phwKGyitMQ3xPGPbQ5SQd/iqy01ZWDKdufpGHIq4xEs aTiP/0Lsh3+6+K/Qk5AsuGq+8iR5RE0cCahcPX/aVtu4LuZDxAqs9RgmswU1/mfEp8U4rB OEyl4TeP12UXjih0OzS2l94B/loxFWA= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.56 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=1661241020; a=rsa-sha256; cv=none; b=bHs7++OZuN/cm7r1FS/0ipeS+3DVCZ5nE+PELZySAD0yAvdnt6yfaM5Yoc3kGm9i1Hgbik dZgZSgGMlPnl4Gi8CtfQjGd/4i7R7cp/8HD3SDkCHtsuJ9F7j5fnT4kPVKRGRg85wnkoJn JZsX7eP3EanvRK3eaLPCrpUpdvx0E1Y= X-Stat-Signature: cmjkd47qewi35rux57xh7rcmazhrt58c Authentication-Results: imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.56 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 71A8F14003F X-Rspam-User: X-HE-Tag: 1661241020-586452 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 2c4048a..6430b74 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -7075,6 +7075,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); @@ -7113,6 +7118,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);