lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250703023946.65315-1-jiahao.kernel@gmail.com>
Date: Thu,  3 Jul 2025 10:39:46 +0800
From: Hao Jia <jiahao.kernel@...il.com>
To: akpm@...ux-foundation.org,
	yuzhao@...gle.com,
	yuanchu@...gle.com,
	shakeel.butt@...ux.dev,
	mhocko@...nel.org,
	lorenzo.stoakes@...cle.com,
	kinseyho@...gle.com,
	hannes@...xchg.org,
	gthelen@...gle.com,
	david@...hat.com,
	axelrasmussen@...gle.com,
	zhengqi.arch@...edance.com
Cc: linux-mm@...ck.org,
	linux-kernel@...r.kernel.org,
	Hao Jia <jiahao1@...iang.com>
Subject: [PATCH v2] mm/mglru: Stop try_to_inc_min_seq() if min_seq[type] has not increased

From: Hao Jia <jiahao1@...iang.com>

In try_to_inc_min_seq(), if min_seq[type] has not increased. In other
words, min_seq[type] == lrugen->min_seq[type]. Then we should return
directly to avoid unnecessary overhead later.

Corollary: If min_seq[type] of both anonymous and file is not increased,
try_to_inc_min_seq() will fail.

Proof:
It is known that min_seq[type] has not increased, that is, min_seq[type]
is equal to lrugen->min_seq[type], then the following:

case 1: min_seq[type] has not been reassigned and changed before
judgment min_seq[type] <= lrugen->min_seq[type].
Then the subsequent min_seq[type] <= lrugen->min_seq[type] judgment
will always be true.

case 2: min_seq[type] is reassigned to seq, before judgment
min_seq[type] <= lrugen->min_seq[type].
Then at least the condition of min_seq[type] > seq must be met
before min_seq[type] will be reassigned to seq.
That is to say, before the reassignment, lrugen->min_seq[type] > seq
is met, and then min_seq[type] = seq.
Then the following min_seq[type](seq) <= lrugen->min_seq[type] judgment
is always true.

Therefore, in try_to_inc_min_seq(), If min_seq[type] of both anonymous
and file is not increased, we can return false directly to avoid
unnecessary overhead.

Suggested-by: Yuanchu Xie <yuanchu@...gle.com>
Signed-off-by: Hao Jia <jiahao1@...iang.com>
---
v1 to v2:
 - Modify commit message to make proof clearer.
 - Use one bool to detect any increments in min_seq[type], suggested by Yuanchu Xie.

Link to v1: https://lore.kernel.org/all/20250630080603.36171-1-jiahao.kernel@gmail.com

 mm/vmscan.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index d48e8d365502..d5b6924aeb8f 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -3919,6 +3919,7 @@ static bool try_to_inc_min_seq(struct lruvec *lruvec, int swappiness)
 {
 	int gen, type, zone;
 	bool success = false;
+	bool seq_inc_flag = false;
 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
 	DEFINE_MIN_SEQ(lruvec);
 
@@ -3935,11 +3936,20 @@ static bool try_to_inc_min_seq(struct lruvec *lruvec, int swappiness)
 			}
 
 			min_seq[type]++;
+			seq_inc_flag = true;
 		}
 next:
 		;
 	}
 
+	/*
+	 * If min_seq[type] of both anonymous and file is not increased,
+	 * we can directly return false to avoid unnecessary checking
+	 * overhead later.
+	 */
+	if (!seq_inc_flag)
+		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


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ