From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta22.hihonor.com (mta22.hihonor.com [81.70.192.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DBAD1A9F86 for ; Tue, 7 Apr 2026 13:37:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.192.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775569041; cv=none; b=DO48lH8ImAgETWSzGQxJzoxZKpP6pphfVFaK1c+rH4hm5dgYESrcgmFKWO0shLZ5V7r1XKHk+3zZoyMmdUnv70Utg2sMODembnJ+7gywlcm9uQfWsQzNhQrSdHopy3VvwVIv4I9NeMi4QhKB6tTq+QbGDqTPOe2YfHhVZ1d+7wI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775569041; c=relaxed/simple; bh=13Dlv+aNdXMozVPzoedRPID5GZ07n9FtL+5rRaslawA=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=Yc6/0ieABCix7FCwqy4noQBnOLGtAlMuQcVyjhwpwifjCEJSjW+HwSE4NS2gWHipPhqqz278lVraYoOtzh6x7yQbNpQPvtCW1Y7VLqCU1j2Be+3CpP4vBkEk4XWfxQ+bLw7WsE/NjgvqfAcsOyWVGBJwVY5wO99RPweuGf0HPIs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com; spf=pass smtp.mailfrom=honor.com; dkim=pass (1024-bit key) header.d=honor.com header.i=@honor.com header.b=chkKpsB+; arc=none smtp.client-ip=81.70.192.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=honor.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=honor.com header.i=@honor.com header.b="chkKpsB+" dkim-signature: v=1; a=rsa-sha256; d=honor.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=To:From; bh=daoAa5k1kSxW3Z4c9BEZEd4leck4VaWEW7nNrwN5c/w=; b=chkKpsB+5OtryPLyYG28VQGyj5u45lqpODL4ijE+o37CXCQ8MMvEIKTHMd4bu7y7VSjJ5LZdz OGpSlLgfoauDnIFwlsU9LCOEArzVaQnForQzc8j/QQsIdK6vcUZp9hwZPweqsFvZhh+My3BBYsE tdIZ7hHjeBEd6nBRffZzugY= Received: from w011.hihonor.com (unknown [10.68.20.122]) by mta22.hihonor.com (SkyGuard) with ESMTPS id 4fqnK86dMBzYmvjb; Tue, 7 Apr 2026 21:36:12 +0800 (CST) Received: from TA011.hihonor.com (10.77.232.85) by w011.hihonor.com (10.68.20.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.27; Tue, 7 Apr 2026 21:37:09 +0800 Received: from TA011.hihonor.com (10.77.232.85) by TA011.hihonor.com (10.77.232.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.17; Tue, 7 Apr 2026 21:37:09 +0800 Received: from TA011.hihonor.com ([fe80::1391:8a1:1ddc:4344]) by TA011.hihonor.com ([fe80::1391:8a1:1ddc:4344%8]) with mapi id 15.02.2562.017; Tue, 7 Apr 2026 21:37:09 +0800 From: wangzhen To: Andrew Morton CC: Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , "Lorenzo Stoakes" , Axel Rasmussen , Yuanchu Xie , Wei Xu , "kasong@tencent.com" , "baolin.wang@linux.alibaba.com" , "baohua@kernel.org" , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" Subject: [PATCH RFC] mm/vmscan:Fix the hot/cold inversion when swappiness = 0 or 201 Thread-Topic: [PATCH RFC] mm/vmscan:Fix the hot/cold inversion when swappiness = 0 or 201 Thread-Index: AdzGkkmRmH7PUgfKQEOsIlMMxaeSQwAASDjw Date: Tue, 7 Apr 2026 13:37:08 +0000 Message-ID: <4451bdc432864aebb54f401eee51ea53@honor.com> References: <7829b070df1b405dbc97dd6a028d8c8a@honor.com> In-Reply-To: <7829b070df1b405dbc97dd6a028d8c8a@honor.com> Accept-Language: en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 >From ac731b061f152cba05b9aa351652a04f933986e0 Mon Sep 17 00:00:00 2001 From: w00021541 Date: Tue, 7 Apr 2026 16:17:53 +0800 Subject: [PATCH RFC] mm/vmscan:Fix the hot/cold inversion when swappiness = =3D 0 or 201 In some cases, when swappiness is set to 0 or 201, the oldest generation pa= ges will be changed to the newest generation incorrectly. Consider the following aging scenario: MAX_NR_GENS=3D4, MIN_NR_GENS=3D2, swappiness=3D201, 3 anon gens, 4 file gen= s. 1. When swappiness =3D 201, should_run_aging will only check anon type. should_run_aging return true. 2. In inc_max_seq, if the anon and file type have MAX_NR_GENS, inc_min_seq = will move the oldest generation pages to the second oldest to prepare for i= ncreasing max_seq. Here, the file type will enter inc_min_seq. 3. In inc_min_seq, first goto is true, the pages migration was skipped, res= ulting in the inversion of cold/hot pages. In fact, when MAX_NR_GENS=3D4 and MIN_NR_GENS=3D2, the for loop after the g= oto is unreachable. Consider the code in inc_max_seq: if (get_nr_gens(lruvec, type) ! =3D MAX_NR_GENS) continue; This means that only get_nr_gens=3D=3D4 can enter the inc_min_seq. Discuss the swappiness in three different scenarios: 1<=3Dswappiness<=3D200: If should_run_aging returns true, both anon and file types must satisfy get= _nr_gens<=3D3, indicating that no type satisfies get_nr_gens=3D=3DMAX_NR_GE= NS. Therefore, both cannot enter inc_min_seq. swappiness=3D201: If should_run_aging returns true, the anon type must satisfy get_nr_gens<= =3D3. Only file type can satisfy get_nr_gens=3D=3DMAX_NR_GENS. After entering inc_min_seq, type && (swappiness =3D=3D SWAPPINESS_ANON_ONLY= ) is true, the for loop will be skipped. swappiness=3D0: Same as swappiness=3D201 so the two goto statements should be removed. This ensures that when swappi= ness=3D0 or 201, the oldest generation pages are correctly promoted to the = second oldest generation. (When 1<=3D swappiness<=3D200, only both anon and file types get_nr_gens<= =3D3 will age, preventing the inversion of hot/cold pages). Signed-off-by: w00021541 --- mm/vmscan.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 0fc9373e8251..54c835b07d3e 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3843,7 +3843,7 @@ static void clear_mm_walk(void) kfree(walk); } =20 -static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness) +static bool inc_min_seq(struct lruvec *lruvec, int type) { int zone; int remaining =3D MAX_LRU_BATCH; @@ -3851,14 +3851,6 @@ static bool inc_min_seq(struct lruvec *lruvec, int t= ype, int swappiness) int hist =3D lru_hist_from_seq(lrugen->min_seq[type]); int new_gen, old_gen =3D lru_gen_from_seq(lrugen->min_seq[type]); =20 - /* For file type, skip the check if swappiness is anon only */ - if (type && (swappiness =3D=3D SWAPPINESS_ANON_ONLY)) - goto done; - - /* For anon type, skip the check if swappiness is zero (file only) */ - if (!type && !swappiness) - goto done; - /* prevent cold/hot inversion if the type is evictable */ for (zone =3D 0; zone < MAX_NR_ZONES; zone++) { struct list_head *head =3D &lrugen->folios[old_gen][type][zone]; @@ -3889,7 +3881,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int ty= pe, int swappiness) return false; } } -done: + reset_ctrl_pos(lruvec, type, true); WRITE_ONCE(lrugen->min_seq[type], lrugen->min_seq[type] + 1); =20 @@ -3975,7 +3967,7 @@ static bool inc_max_seq(struct lruvec *lruvec, unsign= ed long seq, int swappiness if (get_nr_gens(lruvec, type) !=3D MAX_NR_GENS) continue; =20 - if (inc_min_seq(lruvec, type, swappiness)) + if (inc_min_seq(lruvec, type)) continue; =20 spin_unlock_irq(&lruvec->lru_lock); -- 2.17.1