From mboxrd@z Thu Jan 1 00:00:00 1970 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 A3D55C433EF for ; Thu, 28 Apr 2022 07:05:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEF5D6B0071; Thu, 28 Apr 2022 03:05:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E9EBB6B0073; Thu, 28 Apr 2022 03:05:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DBFA76B0071; Thu, 28 Apr 2022 03:05:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id C926B6B0071 for ; Thu, 28 Apr 2022 03:05:33 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 942FC2983E for ; Thu, 28 Apr 2022 07:05:33 +0000 (UTC) X-FDA: 79405402146.06.726542D Received: from out30-42.freemail.mail.aliyun.com (out30-42.freemail.mail.aliyun.com [115.124.30.42]) by imf13.hostedemail.com (Postfix) with ESMTP id 8ACF52005F for ; Thu, 28 Apr 2022 07:05:23 +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=e01e04423;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VBYvof._1651129525; Received: from 30.32.85.129(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VBYvof._1651129525) by smtp.aliyun-inc.com(127.0.0.1); Thu, 28 Apr 2022 15:05:26 +0800 Message-ID: <09bf0a55-d66e-40ee-e9b3-d8b7be136ac9@linux.alibaba.com> Date: Thu, 28 Apr 2022 15:06:13 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Subject: Re: [PATCH v2 2/3] mm: rmap: Move the cache flushing to the correct place for hugetlb PMD sharing To: Muchun Song Cc: akpm@linux-foundation.org, mike.kravetz@oracle.com, almasrymina@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <4f7ae6dfdc838ab71e1655188b657c032ff1f28f.1651056365.git.baolin.wang@linux.alibaba.com> From: Baolin Wang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.42 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8ACF52005F X-Stat-Signature: s3fbfeskiywss47b3cy645ozrgtan579 X-HE-Tag: 1651129523-600874 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 4/28/2022 1:55 PM, Muchun Song wrote: > On Wed, Apr 27, 2022 at 06:52:06PM +0800, Baolin Wang wrote: >> The cache level flush will always be first when changing an existing >> virtual–>physical mapping to a new value, since this allows us to >> properly handle systems whose caches are strict and require a >> virtual–>physical translation to exist for a virtual address. So we >> should move the cache flushing before huge_pmd_unshare(). >> > > Right. > >> As Muchun pointed out[1], now the architectures whose supporting hugetlb >> PMD sharing have no cache flush issues in practice. But I think we >> should still follow the cache/TLB flushing rules when changing a valid >> virtual address mapping in case of potential issues in future. > > Right. One point i need to clarify. I do not object this change but > want you to clarify this (not an issue in practice) in commit log > to let others know they do not need to bp this. > >> >> [1] https://lore.kernel.org/all/YmT%2F%2FhuUbFX+KHcy@FVFYT0MHHV2J.usts.net/ >> Signed-off-by: Baolin Wang >> --- >> mm/rmap.c | 40 ++++++++++++++++++++++------------------ >> 1 file changed, 22 insertions(+), 18 deletions(-) >> >> diff --git a/mm/rmap.c b/mm/rmap.c >> index 61e63db..4f0d115 100644 >> --- a/mm/rmap.c >> +++ b/mm/rmap.c >> @@ -1535,15 +1535,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, >> * do this outside rmap routines. >> */ >> VM_BUG_ON(!(flags & TTU_RMAP_LOCKED)); >> + /* >> + * huge_pmd_unshare may unmap an entire PMD page. >> + * There is no way of knowing exactly which PMDs may >> + * be cached for this mm, so we must flush them all. >> + * start/end were already adjusted above to cover this >> + * range. >> + */ >> + flush_cache_range(vma, range.start, range.end); >> + > > flush_cache_range() is always called even if we do not need to flush. Right, this is intended. In the original code, if it is not a shared PMD, we will use flush_cache_page() to do cache flushing. However the flush_cache_page() can not cover the whole size of a hugetlb page on some architectures, which is fixed by patch 3. > How about introducing a new helper like hugetlb_pmd_shared() which > returns true for shared PMD? Then: > > if (hugetlb_pmd_shared(mm, vma, pvmw.pte)) { > flush_cache_range(vma, range.start, range.end); > huge_pmd_unshare(mm, vma, &address, pvmw.pte); > flush_tlb_range(vma, range.start, range.end); > } > > The code could be a little simpler. Right? IMHO after patch 3, the code will be changed as below, so seems no need to separate the validation of the shared PMDs from huge_pmd_unshare() into a new function. if (folio_test_hugetlb(folio)) { flush_cache_range(vma, range.start, range.end); if (!folio_test_anon(folio)) { VM_BUG_ON(!(flags & TTU_RMAP_LOCKED)); if (hugetlb_pmd_shared(mm, vma, pvmw.pte)) { huge_pmd_unshare(mm, vma, &address, pvmw.pte)); flush_tlb_range(vma, range.start, range.end); ...... break; } } } else { ...... }