Skip to content

Commit bc44972

Browse files
joannekoongopsiff
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> (cherry picked from commit 142194fb21afe964d2d194cab1fc357cbf87e899) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 8fd8da0 commit bc44972

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
@@ -240,17 +240,24 @@ static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
240240
* to avoid reading in already uptodate ranges.
241241
*/
242242
if (ifs) {
243-
unsigned int i;
243+
unsigned int i, blocks_skipped;
244244

245245
/* move forward for each leading block marked uptodate */
246-
for (i = first; i <= last; i++) {
246+
for (i = first; i <= last; i++)
247247
if (!ifs_block_is_uptodate(ifs, i))
248248
break;
249-
*pos += block_size;
250-
poff += block_size;
251-
plen -= block_size;
252-
first++;
249+
250+
blocks_skipped = i - first;
251+
if (blocks_skipped) {
252+
unsigned long block_offset = *pos & (block_size - 1);
253+
unsigned bytes_skipped =
254+
(blocks_skipped << block_bits) - block_offset;
255+
256+
*pos += bytes_skipped;
257+
poff += bytes_skipped;
258+
plen -= bytes_skipped;
253259
}
260+
first = i;
254261

255262
/* truncate len if we find any trailing uptodate block(s) */
256263
while (++i <= last) {

0 commit comments

Comments
 (0)