Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2267672rda; Tue, 24 Oct 2023 19:03:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHrlRG1djq13wexAR4xJS9PZZg/ObP6S/dDK8p56N9NyR1m/fN10fPD8kvfdoYuO03FjlGu X-Received: by 2002:a05:690c:dca:b0:5a8:a04:2c8f with SMTP id db10-20020a05690c0dca00b005a80a042c8fmr18623527ywb.2.1698199398143; Tue, 24 Oct 2023 19:03:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698199398; cv=none; d=google.com; s=arc-20160816; b=Y9/PZn/Onw+ZTWj5hmnL5MJo48vUNHx/uQKXySj5Q12zC9Bdvy0XxrydKw4H5DeK65 uJb5YFpKJzQXYZADsb0NHRYBIJcEm2D7ZzZT66+r0RiRa84PzFYdWN3ouTG2rUwDndAV yAFE92q60+5PsdisA7vvn70rjw2RpYw8l/4HsxbeKQbhEtmGVuWwqDEzbOuXCKdAAGRO /QU7drbavExp0NqpgYqeawvfX4kCYGd2qcm7opYBvSl02Os1u8o75MuCt2so5q2oSNyo vDjkMcBfpuAqmn/M6GeFfLFgc2tZ4dK/lzmW7JVer7KRr6OG8jwbgIdbvubEXGysemr/ aCGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=BlNrEeBm3pvzEaRgFUZT/A9PzBROjnBRtd807jZv6M4=; fh=ozdeA66WibpnhWUBQxhg+XvwUhsKCyHaT4cfgAmYe5w=; b=P8l+6wBgKkjszWDFojz7aRNyLZMrRy9R0uYJ8waEVFxz9QfCRd+lXmnxbndTqX2Myh WIg/7rNy4WMD9e10wWhXLBXsAvmSbX+BH9Qa/SS+jvPFV0LLsz2ljuvKI89V+gWwGSya wbCByIbHDuqqU1iFyD/i+zZ0YeQBnIcLAtXJltQpi3Hf9iSx21dusPO7UyTTm9Wbbmjh kOst/TfaD9BAw9VNe9/4nwIo/CD/i9clVvFa/0F97owGvn2ed6wp32jqiCghxmai7ugF Fm1cEf5i8iBFjPv7qB3iYsuAUZgBpZi8AobhCrxgmctSIH2+CHlqCebvD38R4xwfvW2K A5sQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id d4-20020a0df404000000b00583acdad086si9528030ywf.542.2023.10.24.19.03.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 19:03:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 1B19980B8DCA; Tue, 24 Oct 2023 19:03:14 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229548AbjJYCCo (ORCPT + 99 others); Tue, 24 Oct 2023 22:02:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbjJYCCn (ORCPT ); Tue, 24 Oct 2023 22:02:43 -0400 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7502610D4 for ; Tue, 24 Oct 2023 19:02:39 -0700 (PDT) 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-Spam-Status: No, score=-4.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 24 Oct 2023 19:03:14 -0700 (PDT) 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.