Skip to content

Commit 275b37a

Browse files
joannekoonggregkh
authored andcommitted
iomap: adjust read range correctly for non-block-aligned positions
[ Upstream commit 7aa6bc3 ] iomap_adjust_read_range() assumes that the position and length passed in are block-aligned. This is not always the case however, as shown in the syzbot generated case for erofs. This causes too many bytes to be skipped for uptodate blocks, which results in returning the incorrect position and length to read in. If all the blocks are uptodate, this underflows length and returns a position beyond the folio. Fix the calculation to also take into account the block offset when calculating how many bytes can be skipped for uptodate blocks. Signed-off-by: Joanne Koong <joannelkoong@gmail.com> Tested-by: syzbot@syzkaller.appspotmail.com Reviewed-by: Brian Foster <bfoster@redhat.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Miguel Gazquez (Schneider Electric) <miguel.gazquez@bootlin.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 347201c commit 275b37a

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

‎fs/iomap/buffered-io.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,24 @@ iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
103103
* to avoid reading in already uptodate ranges.
104104
*/
105105
if (iop) {
106-
unsigned int i;
106+
unsigned int i, blocks_skipped;
107107

108108
/* move forward for each leading block marked uptodate */
109-
for (i = first; i <= last; i++) {
109+
for (i = first; i <= last; i++)
110110
if (!test_bit(i, iop->uptodate))
111111
break;
112-
*pos += block_size;
113-
poff += block_size;
114-
plen -= block_size;
115-
first++;
112+
113+
blocks_skipped = i - first;
114+
if (blocks_skipped) {
115+
unsigned long block_offset = *pos & (block_size - 1);
116+
unsigned bytes_skipped =
117+
(blocks_skipped << block_bits) - block_offset;
118+
119+
*pos += bytes_skipped;
120+
poff += bytes_skipped;
121+
plen -= bytes_skipped;
116122
}
123+
first = i;
117124

118125
/* truncate len if we find any trailing uptodate block(s) */
119126
for ( ; i <= last; i++) {

0 commit comments

Comments
 (0)