From: Xiaole He <hexiaole1994@126.com>
To: Matthew Wilcox <willy@infradead.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, Xiaole He <hexiaole1994@126.com>
Subject: [PATCH v1] mm/readahead: Optimize nr_to_read boundary check
Date: Fri, 25 Jul 2025 23:28:34 +0800 [thread overview]
Message-ID: <20250725152834.7602-1-hexiaole1994@126.com> (raw)
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@126.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
next reply other threads:[~2025-07-25 15:29 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-25 15:28 Xiaole He [this message]
2025-07-25 15:45 ` Matthew Wilcox
2025-07-25 23:24 ` Xiaole He
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250725152834.7602-1-hexiaole1994@126.com \
--to=hexiaole1994@126.com \
--cc=akpm@linux-foundation.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox