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: <20250725152834.7602-1-hexiaole1994@126.com>
Date: Fri, 25 Jul 2025 23:28:34 +0800
From: Xiaole He <hexiaole1994@....com>
To: Matthew Wilcox <willy@...radead.org>,
	Andrew Morton <akpm@...ux-foundation.org>
Cc: linux-fsdevel@...r.kernel.org,
	linux-mm@...ck.org,
	linux-kernel@...r.kernel.org,
	Xiaole He <hexiaole1994@....com>
Subject: [PATCH v1] mm/readahead: Optimize nr_to_read boundary check

The current boundary check for nr_to_read in do_page_cache_ra can lead
to a redundant self-assignment when the desired read length precisely
matches the remaining pages to the end of the file.

Currently, the code is:

if (nr_to_read > end_index - index)
    nr_to_read = end_index - index + 1;

If nr_to_read is, for instance, 3, and end_index - index + 1 is also 3
(meaning 3 pages remain), the condition 3 > 2 evaluates to true, leading
 to nr_to_read being assigned 3 again. While compilers might optimize
this trivial self-assignment, it introduces unnecessary logical overhead
 and reduces code clarity.

This patch refines the condition to be more explicit and avoid this
redundant assignment:

if (nr_to_read > end_index - index + 1)
    nr_to_read = end_index - index + 1;

This ensures the assignment only occurs when nr_to_read genuinely
exceeds the available pages, improving code precision and slightly
enhancing readability without altering the core functionality.

Signed-off-by: Xiaole He <hexiaole1994@....com>
---
 mm/readahead.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/readahead.c b/mm/readahead.c
index 20d36d6b055e..bbcfbebe7569 100644
--- a/mm/readahead.c
+++ b/mm/readahead.c
@@ -321,7 +321,7 @@ static void do_page_cache_ra(struct readahead_control *ractl,
 	if (index > end_index)
 		return;
 	/* Don't read past the page containing the last byte of the file */
-	if (nr_to_read > end_index - index)
+	if (nr_to_read > end_index - index + 1)
 		nr_to_read = end_index - index + 1;
 
 	page_cache_ra_unbounded(ractl, nr_to_read, lookahead_size);
-- 
2.43.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ