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 2AED2C7EE30 for ; Wed, 2 Jul 2025 02:19:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A87948E000A; Tue, 1 Jul 2025 22:19:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A5F998E0009; Tue, 1 Jul 2025 22:19:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99C6C8E000A; Tue, 1 Jul 2025 22:19:42 -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 896BD8E0009 for ; Tue, 1 Jul 2025 22:19:42 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 72021BAE26 for ; Wed, 2 Jul 2025 02:19:41 +0000 (UTC) X-FDA: 83617718562.02.795FC7A Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf03.hostedemail.com (Postfix) with ESMTP id 778C520003 for ; Wed, 2 Jul 2025 02:19:39 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IuYVOEAJ; spf=pass (imf03.hostedemail.com: domain of jiahao.kernel@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=jiahao.kernel@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751422779; 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:dkim-signature; bh=pUDwcQQaJ+Nt3QTglNYXtKd690XxoTBtPIqXRJoWd+4=; b=f+F4Tu/YXOwBLBi9RhBkUmAHw91tmLtDGL+l+haE10woc5qi9slgMqBVm/A579mQK79DYp gGi1gBlRBE2rlTHsI1Gm1bU11PCJev4lXadxewfMDs6fELuGikhHnReuM/zT9LkdzBFU9O 2MT8CqZhh4cNRh3RpQDPgbjKc3wNgKw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IuYVOEAJ; spf=pass (imf03.hostedemail.com: domain of jiahao.kernel@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=jiahao.kernel@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751422779; a=rsa-sha256; cv=none; b=zBXn/o16iVjtxGPKJxIIb8f4Q8hXTK5wkoD0cI2N4JndS734mTbUOgct5zbmyLCAYW38fb LDDJUff590RD4+C2d5SM0q5Wdr0SQOv113GoEVoM4CtrkkAdSMERLe5sFzfv0wLOVkry6P z9mjCt34wKFiEJPbcssyuUbMFq0zIq4= Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-23508d30142so48402055ad.0 for ; Tue, 01 Jul 2025 19:19:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1751422778; x=1752027578; darn=kvack.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=pUDwcQQaJ+Nt3QTglNYXtKd690XxoTBtPIqXRJoWd+4=; b=IuYVOEAJ1hk8hUdHlDoFbCnwvqhdpEUlfTq59KZB6+3lYbdHq4OLnF11wz9v1Gt5dV Db0W3oP3zeU763YAl0OaP/KarqABXP4X25PI7YfowECsoGiLvQX4gjs97sBuu+HWonrU BiKd8KOOYG4P3CgYiwid1kATp6S8zD+yu5u+xoVDztUh4yffF7Ts40bpCSTVa/5sXz5C Q0xoBogaKTRBsuSArJmXMArGDgAiJXxm0wSr+AvD9AupakSc/oUaKRxk2tG8FzpkE8f5 NArc+/OwUoPWdiyTBvcIhjmGbi0NLIba2E66XtN8f9hOuviLiITE2zbhQpLOq2utN8yy 4tyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751422778; x=1752027578; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=pUDwcQQaJ+Nt3QTglNYXtKd690XxoTBtPIqXRJoWd+4=; b=CmJUhnFrzIn4qv9BgethFSDku5k5ZIIeNb68HoLVw4qnCwQWYplhgOQny0qlqrEF8T mq+oxeRqmk2dlKn/ljXqJh9Pix4Q75Z5DQl/1eh9giEIyAWRPy6u68WuCnO1EXHE8xTO ZPf6+jW5zZkGEIBlnHU2ntFio1mXIqUJ2XIANBVXJmlP58MdFSP4NCQP3hliGvPqOmPT IxtFs+KZq9YV5k63x+Nzn+sZqh9pv806q9Aa8OER7jjTrmQ5NDfFbeM8iBE8vZa3ffH0 6dqvIemO83Gtw0AwncNuH1E6Krx6FnAPtEwMqZzLpivI+hPq4q6JqpSU2CdRsn5+K+hE nJlQ== X-Forwarded-Encrypted: i=1; AJvYcCWTJhAGfyLWQqasB3xTKalrds6Mu60nMB0WKjp+tmWK83pWZlVJZp6uE3llG7MbY0rUjaF9So0yOw==@kvack.org X-Gm-Message-State: AOJu0YwpieFl6eVbYZe2ojc2RarmllcXDDLqfd+6d22lLRbvSicg38qV 5QQBaz2I6whMzBYYEZfm4Jr1kjpKxQfD9fPR+x/cqFIt3ehCAxEcPQkZ X-Gm-Gg: ASbGnctzPPcfeMqMt6W1EoiYbVQYbggc2AoIrHc1sQh0DsgKt5yICQRjP2fFzhpux6I pNs4vDIxCpJfVO11TCPny7gYd2JM+Cz4u4zFv8jvWI/4qg1sk5t5ctsPLzDrMzlup7hkUXriZXy /in4rpC2Ngv6G4PS+hUqnsd+TrK++dPLzogMRluAP39AViVAAoHYn6FqDswQRjn2r60F/PqhA2D cUjN3tO2G0ZWqonuZWkB7qjzPWWGOzs0DnfoD4J5yll8KgcibNH2d/v2gQNqwHNMsXOPCPV1U/w QLHKevb22nnmzCWKj7rMSYpb9SxYVOfgp+fOlHFXPk+XYzB5P0X9+lXfv8iuUFl/ycGMm+RGoT8 qwgrp/lrS33Nht8toYw== X-Google-Smtp-Source: AGHT+IEZ/UhovT4QDpnoTFw5/an2zyQLPRdHxdVepISIlWWFTzDsZdPY/0xdPIFbvzkIA5Sj2ryb9w== X-Received: by 2002:a17:903:1b2d:b0:234:f200:51a1 with SMTP id d9443c01a7336-23c6e48ce1emr17076415ad.9.1751422778072; Tue, 01 Jul 2025 19:19:38 -0700 (PDT) Received: from [10.125.192.71] ([210.184.73.204]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-23acb39bac3sm114889435ad.133.2025.07.01.19.19.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 01 Jul 2025 19:19:37 -0700 (PDT) Message-ID: <0b316beb-0c49-444f-983c-e8a8a3e76dfc@gmail.com> Date: Wed, 2 Jul 2025 10:19:30 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Subject: Re: [PATCH] mm/mglru: Stop try_to_inc_min_seq() if the oldest generation LRU lists are not empty To: Yuanchu Xie Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, yuzhao@google.com, kinseyho@google.com, david@redhat.com, mhocko@kernel.org, zhengqi.arch@bytedance.com, shakeel.butt@linux.dev, lorenzo.stoakes@oracle.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hao Jia References: <20250630080603.36171-1-jiahao.kernel@gmail.com> From: Hao Jia In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 778C520003 X-Stat-Signature: wks748z1ehtjgacfrwocsujb1wkhko55 X-Rspam-User: X-HE-Tag: 1751422779-137369 X-HE-Meta: U2FsdGVkX1+0kqYmHyJsTuzTuzX6468o2toD8S8ZXo/GwVgnpbzkCBuZ8+rdnmb504XPuGO0qvG3DtettJD65qsFgqx3yNfJUDv8xttbiDhcrLJvO2sPu1hhE+5Gb8KtQMTbdfHCi5YYbIiDKJ1g3hmm7GcOK/f1opDyShJqmhn2Dv43A7NwnK3VPiizI6aS8vx9OB+3Ac2H9vzHMqQ0kAan1dHNt+RIcPxn+J/orSego0FcCvMQnLakO9G7ES04bmGR/cWktuoss9tYsNGX+Pqu3EJXuAj9WzJZ2kaqi20nhAj+dfHB+wkDl3N3JYtDgrKbbDEnF4f/yrpVPUHx8+EDVjHQlcaBzJcL5221I2HfUqy8MW39d5LDor3Iz4tNelJ2p9RDIFPxKaMYxxTXr4X6xD2dfFwc+nitxgTk7jo0Qxm1NJvJo17bnZ358kKGVnPvILOYzzP8xmOT5qiks0y5HSbGdgaKUa/9kIdjGhO+AOkafBrYhYsj3eU9FVBO0YMeAU7LpvQEmMsxoxIAGpXYnbDz59IEaY4BfnYqc6LL12i34npPkMv/NFF2el4aBpWgC4AfVFYIgUc77E/hQGIGANKlSdBSD5Wt4LGe249MkOadrmz0trD7d3PHB7zyCGeiM94OC1g6dGUY1fLasCHxqjTp0uoRAHgugz8oYZd30DrEnh1tq406c8nVlQzJYRL9Ev6wVqN/0D99bNMHRv2N0spZIeQ4xxcgpAXP1SzzNuIylS6O2JKDq1TefBxkx3O02LMa+rX8RI6UsW1ugL32sHTdiv1K4kI8T7giWp9UtofRkdbAejVP/KTdmrwOxNPUDpQkj94eSCRHHb4FJBSSnB9FtriavZkAbNEOJFtEmMoygR5wgIjASl0NUay4QsC3dew4iQ7FlHxOCFG2+oDrIrfuLakDm2R1TFAFALAPC+rV8l/gg53ObWlm/Bvt1sBvMXDR1XXky1vGEr6 nMsSDByQ 0X8C7zO04e/+jMWccnrVHa+HJCki2ZzSdDdxWC7uwMM9mPGaYfJtS+1jNA7ijpHrf56w7PP1Hes7CfgJ1FsrVIxzqzCSDh29U20OQKAf0ysEZlYWkupSZP8t7d3TkERCP9gMMiY4Vph8wK2giejS21xYLabkty1sWlVpu1Cpuwgp/ZdTIIRmoNTxePujXnToOLM3CS6a+znBfcFN5ZNWXlaZIjytpzsmXPhql5xlbCsYgdfLQeXHAcWkRBEDN3knK5kgO6iH5cUWZH6S+1dd9hVIJo6fM5Ihj2TbkxhDPwu9pAl4cdf1iX4N6ICUhs4wkwM8OB+Tgq/GCyXqKAePdr2ZkLQnRPQ9wbycBCK72Tz+pndgehqf2KAH7i56v8Px1Y9GUtLr0VlgbT/276U29kVNRRUwbD/YBIFRXSaXOxjAadKv1vFDwqrDoQNpwSHsMJx3Fmr/ABWHLjihhb4VbpwOCCq8WM3JwspbIM3N3ObvkzOgBqTAkNQ1iUiU5Ac0ImAJBDXUmZ6ZhiPSNrJo5eaCNz4J1L8mRCqaA1fHXI7kO/32B7OHwvo+a6EDUi0iy+TnfpY570MLiZbzvwnbr9LfTsg== 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 2025/7/2 08:31, Yuanchu Xie wrote: Sorry, I got my email wrong. I'll reply again to make sure the kernel mail lists can receive it. > On Mon, Jun 30, 2025 at 1:06 AM Hao Jia wrote: >> >> From: Hao Jia >> >> In try_to_inc_min_seq(), if the oldest generation of LRU lists >> (anonymous and file) are not empty. Then we should return directly >> to avoid unnecessary subsequent overhead. >> >> Corollary: If the lrugen->folios[gen][type][zone] lists of both >> anonymous and file are not empty, try_to_inc_min_seq() will fail. >> >> Proof: Taking LRU_GEN_ANON as an example, consider the following two cases: >> >> Case 1: min_seq[LRU_GEN_ANON] <= seq (seq is lrugen->max_seq - MIN_NR_GENS) >> >> Since min_seq[LRU_GEN_ANON] has not increased, >> so min_seq[LRU_GEN_ANON] is still equal to lrugen->min_seq[LRU_GEN_ANON]. >> Therefore, in the following judgment: >> min_seq[LRU_GEN_ANON] <= lrugen->min_seq[LRU_GEN_ANON] is always true. >> So, we will not increase the seq of the oldest generation of anonymous, >> and try_to_inc_min_seq() will return false. >> >> case 2: min_seq[LRU_GEN_ANON] > seq (seq is lrugen->max_seq - MIN_NR_GENS) >> >> If min_seq[LRU_GEN_ANON] > seq, that is, lrugen->min_seq[LRU_GEN_ANON] > seq > This part doesn't make sense to me. > The code is as follows: > > /* find the oldest populated generation */ > for_each_evictable_type(type, swappiness) { > while (min_seq[type] + MIN_NR_GENS <= lrugen->max_seq) { > gen = lru_gen_from_seq(min_seq[type]); > > for (zone = 0; zone < MAX_NR_ZONES; zone++) { > if (!list_empty(&lrugen->folios[gen][type][zone])) > goto next; > } > > min_seq[type]++; > } > > Here, it could be that , min_seq[type] > lrugen->max_seq - MIN_NR_GENS > (what you refer to as seq) > However, this is a result of incrementing a copy of > lrugen->min_seq[type] as this piece of code finds the oldest populated > generation. > Hi, Yuanchu Sorry for the confusion. I am assuming that if the oldest generation LRU lists (anonymous and file) are not empty, in other words, *min_seq[type]* has not increased. The above part has been executed, and it is known that min_seq[type] has not increased(that is, min_seq[type]=lrugen->min_seq[type] at this time), so the rest of the reasoning. Maybe you mean that under the above premise min_seq[type] is impossible to be greater than seq (seq is lrugen->max_seq - MIN_NR_GENS)? If so, case2 does not need to be discussed and reasoned. In either case, my patch will work well. Thanks, Hao > next: > ; > } > >> Then min_seq[LRU_GEN_ANON] is assigned seq. > This is not necessarily true, because swappiness can be 0, and the > assignments happen to prevent one LRU type from going more than 1 gen > past the other. > so if `min_seq[LRU_GEN_ANON] > seq && min_seq[LRU_GEN_FILE] == seq` is > true, then min_seq[LRU_GEN_ANON] is not assigned seq. > Yes, if min_seq[LRU_GEN_ANON] is not assigned seq, then the situation is the same as case 1. min_seq[LRU_GEN_ANON] is equal to lrugen->min_seq[LRU_GEN_ANON]. in the following judgment: min_seq[LRU_GEN_ANON] <= lrugen->min_seq[LRU_GEN_ANON] is always true. Case 2 wants to discuss another situation, that is, when min_seq[LRU_GEN_ANON] is assigned to seq. The following judgment is whether min_seq[LRU_GEN_ANON] <= lrugen->min_seq[LRU_GEN_ANON] is always true. > >> Therefore, in the following judgment: >> min_seq[LRU_GEN_ANON] (seq) <= lrugen->min_seq[LRU_GEN_ANON] is always true. >> So, we will not update the oldest generation seq of anonymous, >> and try_to_inc_min_seq() will return false. >> >> It is similar for LRU_GEN_FILE. Therefore, in try_to_inc_min_seq(), >> if the oldest generation LRU lists (anonymous and file) are not empty, >> in other words, min_seq[type] has not increased. >> we can directly return false to avoid unnecessary checking overhead later. > Yeah I don't think this proof holds. If you think it does please > elaborate more and make your assumptions more clear. > >> >> Signed-off-by: Hao Jia >> --- >> mm/vmscan.c | 10 ++++++++++ >> 1 file changed, 10 insertions(+) >> >> diff --git a/mm/vmscan.c b/mm/vmscan.c >> index f8dfd2864bbf..3ba63d87563f 100644 >> --- a/mm/vmscan.c >> +++ b/mm/vmscan.c >> @@ -3928,6 +3928,7 @@ static bool try_to_inc_min_seq(struct lruvec *lruvec, int swappiness) >> int gen, type, zone; >> bool success = false; >> struct lru_gen_folio *lrugen = &lruvec->lrugen; >> + int seq_inc_flags[ANON_AND_FILE] = {0}; >> DEFINE_MIN_SEQ(lruvec); >> >> VM_WARN_ON_ONCE(!seq_is_valid(lruvec)); >> @@ -3943,11 +3944,20 @@ static bool try_to_inc_min_seq(struct lruvec *lruvec, int swappiness) >> } >> >> min_seq[type]++; >> + seq_inc_flags[type] = 1; >> } >> next: >> ; >> } >> >> + /* >> + * If the oldest generation of LRU lists (anonymous and file) >> + * are not empty, we can directly return false to avoid unnecessary >> + * checking overhead later. >> + */ >> + if (!seq_inc_flags[LRU_GEN_ANON] && !seq_inc_flags[LRU_GEN_FILE]) >> + return success; >> + >> /* see the comment on lru_gen_folio */ >> if (swappiness && swappiness <= MAX_SWAPPINESS) { >> unsigned long seq = lrugen->max_seq - MIN_NR_GENS; >> -- >> 2.34.1 >> >> > I don't understand what problem this patch tries to solve. > > Yuanchu My pathch is that if we already know that min_seq[type] (including anonymous and file) has not increased, we can directly let try_to_inc_min_seq() return failure to reduce unnecessary checking overhead later. After my above reasoning, this does not change the original behavior of try_to_inc_min_seq(). I added some code to count the number of try_to_inc_min_seq() calls and the number of times the situation mentioned in my patch is hit. Run the test in tools/testing/selftests/cgroup/test_memcontrol on my machine. hit_cnt: 1215 total_cnt: 1702 The hit rate is about 71% Thanks, Hao