From: Chi Zhiling <chizhiling@163.com>
To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
Matthew Wilcox <willy@infradead.org>,
Andrew Morton <akpm@linux-foundation.org>,
Namjae Jeon <linkinjeon@kernel.org>,
Sungjong Seo <sj1557.seo@samsung.com>,
Yuezhang Mo <yuezhang.mo@sony.com>,
Chi Zhiling <chizhiling@kylinos.cn>
Subject: [PATCH 2/3] mpage: clean up do_mpage_readpage()
Date: Tue, 12 Aug 2025 15:22:24 +0800 [thread overview]
Message-ID: <20250812072225.181798-2-chizhiling@163.com> (raw)
In-Reply-To: <20250812072225.181798-1-chizhiling@163.com>
From: Chi Zhiling <chizhiling@kylinos.cn>
Replace two loop iterations with direct calculations.
The variable nblocks now represents the number of avalid blocks we can
obtain from map_bh. no functional change.
Signed-off-by: Chi Zhiling <chizhiling@kylinos.cn>
---
fs/mpage.c | 42 +++++++++++++++++-------------------------
1 file changed, 17 insertions(+), 25 deletions(-)
diff --git a/fs/mpage.c b/fs/mpage.c
index b6510b8dfa2b..a81a71de8f59 100644
--- a/fs/mpage.c
+++ b/fs/mpage.c
@@ -158,7 +158,6 @@ static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)
struct buffer_head *map_bh = &args->map_bh;
sector_t block_in_file;
sector_t last_block;
- sector_t last_block_in_file;
sector_t first_block;
unsigned page_block;
unsigned first_hole = blocks_per_folio;
@@ -180,9 +179,8 @@ static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)
block_in_file = folio_pos(folio) >> blkbits;
last_block = block_in_file + ((args->nr_pages * PAGE_SIZE) >> blkbits);
- last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
- if (last_block > last_block_in_file)
- last_block = last_block_in_file;
+ last_block = min_t(sector_t, last_block,
+ (i_size_read(inode) + blocksize - 1) >> blkbits);
page_block = 0;
/*
@@ -193,19 +191,15 @@ static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)
block_in_file > args->first_logical_block &&
block_in_file < (args->first_logical_block + nblocks)) {
unsigned map_offset = block_in_file - args->first_logical_block;
- unsigned last = nblocks - map_offset;
first_block = map_bh->b_blocknr + map_offset;
- for (relative_block = 0; ; relative_block++) {
- if (relative_block == last) {
- clear_buffer_mapped(map_bh);
- break;
- }
- if (page_block == blocks_per_folio)
- break;
- page_block++;
- block_in_file++;
- }
+ nblocks -= map_offset;
+ if (nblocks > blocks_per_folio - page_block)
+ nblocks = blocks_per_folio - page_block;
+ else
+ clear_buffer_mapped(map_bh);
+ page_block += nblocks;
+ block_in_file += nblocks;
bdev = map_bh->b_bdev;
}
@@ -243,7 +237,7 @@ static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)
map_buffer_to_folio(folio, map_bh, page_block);
goto confused;
}
-
+
if (first_hole != blocks_per_folio)
goto confused; /* hole -> non-hole */
@@ -252,16 +246,14 @@ static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)
first_block = map_bh->b_blocknr;
else if (first_block + page_block != map_bh->b_blocknr)
goto confused;
+
nblocks = map_bh->b_size >> blkbits;
- for (relative_block = 0; ; relative_block++) {
- if (relative_block == nblocks) {
- clear_buffer_mapped(map_bh);
- break;
- } else if (page_block == blocks_per_folio)
- break;
- page_block++;
- block_in_file++;
- }
+ if (nblocks > blocks_per_folio - page_block)
+ nblocks = blocks_per_folio - page_block;
+ else
+ clear_buffer_mapped(map_bh);
+ page_block += nblocks;
+ block_in_file += nblocks;
bdev = map_bh->b_bdev;
}
--
2.43.0
next prev parent reply other threads:[~2025-08-12 7:23 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-12 7:22 [PATCH 1/3] mpage: terminate read-ahead on read error Chi Zhiling
2025-08-12 7:22 ` Chi Zhiling [this message]
2025-08-12 7:22 ` [PATCH 3/3] mpage: convert do_mpage_readpage() to return int type Chi Zhiling
2025-08-18 2:41 ` [PATCH 1/3] mpage: terminate read-ahead on read error Andrew Morton
2025-08-18 10:04 ` Chi Zhiling
2025-08-18 14:33 ` Matthew Wilcox
2025-08-19 12:22 ` Chi Zhiling
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=20250812072225.181798-2-chizhiling@163.com \
--to=chizhiling@163.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=chizhiling@kylinos.cn \
--cc=jack@suse.cz \
--cc=linkinjeon@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=sj1557.seo@samsung.com \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.org \
--cc=yuezhang.mo@sony.com \
/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