Skip to content

Commit 943cd69

Browse files
Ming Leigregkh
authored andcommitted
block: add bio_truncate to fix guard_bio_eod
[ Upstream commit 85a8ce6 ] Some filesystem, such as vfat, may send bio which crosses device boundary, and the worse thing is that the IO request starting within device boundaries can contain more than one segment past EOD. Commit dce30ca ("fs: fix guard_bio_eod to check for real EOD errors") tries to fix this issue by returning -EIO for this situation. However, this way lets fs user code lose chance to handle -EIO, then sync_inodes_sb() may hang for ever. Also the current truncating on last segment is dangerous by updating the last bvec, given bvec table becomes not immutable any more, and fs bio users may not retrieve the truncated pages via bio_for_each_segment_all() in its .end_io callback. Fixes this issue by supporting multi-segment truncating. And the approach is simpler: - just update bio size since block layer can make correct bvec with the updated bio size. Then bvec table becomes really immutable. - zero all truncated segments for read bio Cc: Carlos Maiolino <cmaiolino@redhat.com> Cc: linux-fsdevel@vger.kernel.org Fixed-by: dce30ca ("fs: fix guard_bio_eod to check for real EOD errors") Reported-by: syzbot+2b9e54155c8c25d8d165@syzkaller.appspotmail.com Signed-off-by: Ming Lei <ming.lei@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 2922cf5 commit 943cd69

3 files changed

Lines changed: 41 additions & 24 deletions

File tree

‎block/bio.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,45 @@ void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
535535
}
536536
EXPORT_SYMBOL(zero_fill_bio_iter);
537537

538+
void bio_truncate(struct bio *bio, unsigned new_size)
539+
{
540+
struct bio_vec bv;
541+
struct bvec_iter iter;
542+
unsigned int done = 0;
543+
bool truncated = false;
544+
545+
if (new_size >= bio->bi_iter.bi_size)
546+
return;
547+
548+
if (bio_data_dir(bio) != READ)
549+
goto exit;
550+
551+
bio_for_each_segment(bv, bio, iter) {
552+
if (done + bv.bv_len > new_size) {
553+
unsigned offset;
554+
555+
if (!truncated)
556+
offset = new_size - done;
557+
else
558+
offset = 0;
559+
zero_user(bv.bv_page, offset, bv.bv_len - offset);
560+
truncated = true;
561+
}
562+
done += bv.bv_len;
563+
}
564+
565+
exit:
566+
/*
567+
* Don't touch bvec table here and make it really immutable, since
568+
* fs bio user has to retrieve all pages via bio_for_each_segment_all
569+
* in its .end_bio() callback.
570+
*
571+
* It is enough to truncate bio by updating .bi_size since we can make
572+
* correct bvec with the updated .bi_size for drivers.
573+
*/
574+
bio->bi_iter.bi_size = new_size;
575+
}
576+
538577
/**
539578
* bio_put - release a reference to a bio
540579
* @bio: bio to release reference to

‎fs/buffer.c‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,8 +2994,6 @@ static void end_bio_bh_io_sync(struct bio *bio)
29942994
void guard_bio_eod(int op, struct bio *bio)
29952995
{
29962996
sector_t maxsector;
2997-
struct bio_vec *bvec = bio_last_bvec_all(bio);
2998-
unsigned truncated_bytes;
29992997
struct hd_struct *part;
30002998

30012999
rcu_read_lock();
@@ -3021,28 +3019,7 @@ void guard_bio_eod(int op, struct bio *bio)
30213019
if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
30223020
return;
30233021

3024-
/* Uhhuh. We've got a bio that straddles the device size! */
3025-
truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9);
3026-
3027-
/*
3028-
* The bio contains more than one segment which spans EOD, just return
3029-
* and let IO layer turn it into an EIO
3030-
*/
3031-
if (truncated_bytes > bvec->bv_len)
3032-
return;
3033-
3034-
/* Truncate the bio.. */
3035-
bio->bi_iter.bi_size -= truncated_bytes;
3036-
bvec->bv_len -= truncated_bytes;
3037-
3038-
/* ..and clear the end of the buffer for reads */
3039-
if (op == REQ_OP_READ) {
3040-
struct bio_vec bv;
3041-
3042-
mp_bvec_last_segment(bvec, &bv);
3043-
zero_user(bv.bv_page, bv.bv_offset + bv.bv_len,
3044-
truncated_bytes);
3045-
}
3022+
bio_truncate(bio, maxsector << 9);
30463023
}
30473024

30483025
static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,

‎include/linux/bio.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ extern struct bio *bio_copy_user_iov(struct request_queue *,
470470
gfp_t);
471471
extern int bio_uncopy_user(struct bio *);
472472
void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter);
473+
void bio_truncate(struct bio *bio, unsigned new_size);
473474

474475
static inline void zero_fill_bio(struct bio *bio)
475476
{

0 commit comments

Comments
 (0)