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 72EDDC25B6D for ; Wed, 25 Oct 2023 02:02:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 001BF6B0302; Tue, 24 Oct 2023 22:02:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECCE76B0305; Tue, 24 Oct 2023 22:02:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6CF56B0306; Tue, 24 Oct 2023 22:02:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C1DCE6B0302 for ; Tue, 24 Oct 2023 22:02:47 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 983EF12061E for ; Wed, 25 Oct 2023 02:02:47 +0000 (UTC) X-FDA: 81382335174.23.7195F8D Received: from out30-111.freemail.mail.aliyun.com (out30-111.freemail.mail.aliyun.com [115.124.30.111]) by imf05.hostedemail.com (Postfix) with ESMTP id 70B66100013 for ; Wed, 25 Oct 2023 02:02:41 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf05.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.111 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698199366; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BlNrEeBm3pvzEaRgFUZT/A9PzBROjnBRtd807jZv6M4=; b=WhsEz2B3JgPXl73M/eUKObOSBdum/S3X+ZgP18vUMpqwU0jwCrmE2wqyMNik3t4W84iqbm mu+25w2fuRAhLiyA9oofjNO31j+asBOUjB/S1Z1yr+67HFkO2ApSmdXpY270v6b4a5yfL2 mhvtFUCjQRXYvXZEiKC84MPSsSoabNU= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf05.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.111 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698199366; a=rsa-sha256; cv=none; b=STcgxxS8pzVH6P1H9T3mb222REc5UtUbZYkf6c6DQItf6r5UCCpCD+WvHw+kEm85mDL/Zr /HSPNN29kniUflEMfczKh9DFktE2gImCcgYfIW/PGIpP2PjdqGi0ZuP8vWydFMjUhboA1X Otfr6a6QrWLluH4S0lKWk8QUV+s9qKM= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R481e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046051;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=9;SR=0;TI=SMTPD_---0VusYSvA_1698199355; Received: from 30.97.48.63(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VusYSvA_1698199355) by smtp.aliyun-inc.com; Wed, 25 Oct 2023 10:02:36 +0800 Message-ID: Date: Wed, 25 Oct 2023 10:02:51 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH] arm64: mm: drop tlb flush operation when clearing the access bit To: Barry Song <21cnbao@gmail.com> Cc: catalin.marinas@arm.com, will@kernel.org, akpm@linux-foundation.org, v-songbaohua@oppo.com, yuzhao@google.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org References: From: Baolin Wang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 70B66100013 X-Stat-Signature: sc8t8giyrbc9h3wxrregnqjrwennu9im X-HE-Tag: 1698199361-955466 X-HE-Meta: U2FsdGVkX1/rWsMN76C+4EgI9dpF9o1BeGyI10c3Zowl5Tbfw5T9Z69I/1jRZ/PZsQcFfZjzxoP0/pesN1UD4ef1n3YsqZYUW1T0qYkxOz12Lad+mj3XIKqr/hf3bM5aOCATevOLCYRPvw9UHL6Qv3EfTyS6poZHbNR+/KWDBbEHj7W8R9oNRSS3+t/iYB0H2QtPohd1wvLu3N6Vtk5QQ3doY2OGCwhFkIJMxINTM4k2CxgpJkS5AU/hTfeVEhAXPPcV+pnwmYssBrdyz63JpgTLnDNV1Lh2LSIvGDIshdWIuQ9+mH2TYJArz2fftUZPXv+Jvf9d3MqHo39WABvyK0dRNPuixhSS811bX3wm0MiOucaWXCIihwz3j8dZEDTAFnTYSqih6yu8rtC7dQdjbl5cf9k1e4reOG2nXmf3tUfSGP7iPCkYlIKoX2eDVrymhUd+BuVEp4LPBQJy7SS9UcEBCUoDqEANSJtPsvU1O8NPZYLIrmn5YhbYXFFHREJptSTP6sz+eDTYUqiBybkM4i20sINrbqO34yy4RdKFg9hwGPUpGSw+8x9eoNN25IWW+bFI7HCWPqcaMEbNypKjhLkP9YnqqkClgrtnVT5qCY3/Y4+AxDW4bamJiIfxF8JLUPdvZoAnvGMennZLo1yYNeEbqbfd3wVCwKhG2C+QhOmyJaJM3FIIYErxwqUwrGsTGUcVa2XO1qewa8aIQhFSBYAQJTLu09GhtAp/IuWumx8TrKjmnoNY628Tu2UGTwMvIyfuKG6XXw+0fRb+QNJwq7ZNt4L5FGbfV64n+LGeX4weJ1D87PkuqPKcGCUwXwYihecaxyrjBIbTgW3bbxUaLgKQxpdi5MxvZ/YiYyoFzZFCN0L939pVRfMvqZQw9s2whbxoWQpOyWcXk2JAGhGk3wxUhCjxYjOZlfU0Wy5O9rpVK1VGlP2oq7Qpnw3EUyHMqs4onrkBTWE96cP8SK1 ensZGLiD T8o0Az7rd9/eNJL3WxEt/MEq7xbceVAIidrJb6oFZEIU8w7kcPrPjiH5CobnxnxorFA62x7Crul+5OVue1yrBYPE7oj/N2q0T+8oi7eXosQP7zH5bL77XmUoTbg== 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: List-Subscribe: List-Unsubscribe: On 10/25/2023 7:31 AM, Barry Song wrote: > On Wed, Oct 25, 2023 at 7:16 AM Barry Song <21cnbao@gmail.com> wrote: >> >> On Tue, Oct 24, 2023 at 8:57 PM Baolin Wang >> wrote: >>> >>> Now ptep_clear_flush_young() is only called by folio_referenced() to >>> check if the folio was referenced, and now it will call a tlb flush on >>> ARM64 architecture. However the tlb flush can be expensive on ARM64 >>> servers, especially for the systems with a large CPU numbers. >>> >>> Similar to the x86 architecture, below comments also apply equally to >>> ARM64 architecture. So we can drop the tlb flush operation in >>> ptep_clear_flush_young() on ARM64 architecture to improve the performance. >>> " >>> /* Clearing the accessed bit without a TLB flush >>> * doesn't cause data corruption. [ It could cause incorrect >>> * page aging and the (mistaken) reclaim of hot pages, but the >>> * chance of that should be relatively low. ] >>> * >>> * So as a performance optimization don't flush the TLB when >>> * clearing the accessed bit, it will eventually be flushed by >>> * a context switch or a VM operation anyway. [ In the rare >>> * event of it not getting flushed for a long time the delay >>> * shouldn't really matter because there's no real memory >>> * pressure for swapout to react to. ] >>> */ >>> " >>> Running the thpscale to show some obvious improvements for compaction >>> latency with this patch: >>> base patched >>> Amean fault-both-1 1093.19 ( 0.00%) 1084.57 * 0.79%* >>> Amean fault-both-3 2566.22 ( 0.00%) 2228.45 * 13.16%* >>> Amean fault-both-5 3591.22 ( 0.00%) 3146.73 * 12.38%* >>> Amean fault-both-7 4157.26 ( 0.00%) 4113.67 * 1.05%* >>> Amean fault-both-12 6184.79 ( 0.00%) 5218.70 * 15.62%* >>> Amean fault-both-18 9103.70 ( 0.00%) 7739.71 * 14.98%* >>> Amean fault-both-24 12341.73 ( 0.00%) 10684.23 * 13.43%* >>> Amean fault-both-30 15519.00 ( 0.00%) 13695.14 * 11.75%* >>> Amean fault-both-32 16189.15 ( 0.00%) 14365.73 * 11.26%* >>> base patched >>> Duration User 167.78 161.03 >>> Duration System 1836.66 1673.01 >>> Duration Elapsed 2074.58 2059.75 >>> >>> Barry Song submitted a similar patch [1] before, that replaces the >>> ptep_clear_flush_young_notify() with ptep_clear_young_notify() in >>> folio_referenced_one(). However, I'm not sure if removing the tlb flush >>> operation is applicable to every architecture in kernel, so dropping >>> the tlb flush for ARM64 seems a sensible change. >>> >>> Note: I am okay for both approach, if someone can help to ensure that >>> all architectures do not need the tlb flush when clearing the accessed >>> bit, then I also think Barry's patch is better (hope Barry can resend >>> his patch). >>> >> >> Thanks! >> >> ptep_clear_flush_young() with "flush" in its name clearly says it needs a >> flush. but it happens in arm64, all other code which needs a flush has >> called other variants, for example __flush_tlb_page_nosync(): >> >> static inline void arch_tlbbatch_add_pending(struct >> arch_tlbflush_unmap_batch *batch, >> struct mm_struct *mm, unsigned long uaddr) >> { >> __flush_tlb_page_nosync(mm, uaddr); >> } >> >> so it seems folio_referenced is the only left user of this >> ptep_clear_flush_young(). >> The fact makes Baolin's patch look safe now. >> >> but this function still has "flush" in its name, so one day, one person might >> call it with the understanding that it will flush tlb but actually it >> won't. This is >> bad smell in code. Agree. I think this is jsut a start, we can replace ptep_clear_flush_young() once other architectures have completed the conversion, if we can confirm that other architectures also do not require tlb flush when clearing the accessed bit. >> I guess one side effect of not flushing tlb while clearing the access >> flag is that >> hardware won't see this cleared flag in the tlb, so it might not set this bit in >> memory even though the bit has been cleared before in memory(but not in tlb) >> while the page is accessed *again*. >> >> next time, someone reads the access flag in memory again by folio_referenced, >> he/she will see the page is cold as hardware has lost a chance to set >> the bit again >> since it finds tlb already has a true access flag. >> >> But anyway, tlb is so small, it will be flushed by context switch and >> other running >> code soon. so it seems we don't actually require the access flag being instantly >> updated. the time gap, in which access flag might lose the new set by hardware, >> seems to be too short to really affect the accuracy of page reclamation. but its >> cost is large. >> >> (A). Constant flush cost vs. (B). very very occasional reclaimed hot >> page, B might >> be a correct choice. > > Plus, I doubt B is really going to happen. as after a page is promoted to > the head of lru list or new generation, it needs a long time to slide back > to the inactive list tail or to the candidate generation of mglru. the time > should have been large enough for tlb to be flushed. If the page is really > hot, the hardware will get second, third, fourth etc opportunity to set an > access flag in the long time in which the page is re-moved to the tail > as the page can be accessed multiple times if it is really hot. Thanks Barry, that's also what I thought. On the other hand, even if there is no tlb flush for a long time, I think the system is not under memory pressure at that time, so the incorrect page aging would not have much impact.