Skip to contentSkip to conversation

block: add bio_truncate to fix guard_bio_eod

PATCH Superseded Ming Lei 2 participants 2 messages Dec 27, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: add bio_truncate to fix guard_bio_eod

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 dce30ca9e3b6 ("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 forever.

Also the current truncating on last segment is dangerous by updating the last bvec, given the bvec table becomes not immutable, and fs bio users may lose to retrieve 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 <[email protected]>
Fixed-by dce30ca9e3b6 ("fs: fix guard_bio_eod to check for real EOD errors")
Reported-by [email protected]
---
 block/bio.c         | 40 ++++++++++++++++++++++++++++++++++++++++
 fs/buffer.c         | 25 +------------------------
 include/linux/bio.h |  1 +
 3 files changed, 42 insertions(+), 24 deletions(-)
block/bio.c +40 -0
index a5d75f6bf4c7..a44d0b6e4d49 100644
@@ -538,6 +538,46 @@ void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
 }
 EXPORT_SYMBOL(zero_fill_bio_iter);
 
+void bio_truncate(struct bio *bio, unsigned new_size)
+{
+	struct bio_vec bv;
+	struct bvec_iter iter;
+	unsigned int done = 0;
+	bool truncated = false;
+
+	if (new_size >= bio->bi_iter.bi_size)
+		return;
+
+	if (bio_data_dir(bio) != READ)
+		goto exit;
+
+	bio_for_each_segment(bv, bio, iter) {
+		if (done + bv.bv_len > new_size) {
+			unsigned offset;
+
+			if (!truncated)
+				offset = new_size - done;
+			else
+				offset = 0;
+			zero_user(bv.bv_page, offset, bv.bv_len - offset);
+			truncated = true;
+		}
+		done += bv.bv_len;
+	}
+
+ exit:
+	/*
+	 * Don't touch bvec table here and make it really immutable, since
+	 * fs bio user has to retrieve all pages via bio_for_each_segment_all
+	 * in its .end_bio() callback.
+	 *
+	 * It is enough to truncate bio by updating .bi_size since we can make
+	 * correct bvec with the updated .bi_size for drivers.
+	 */
+	bio->bi_iter.bi_size = new_size;
+}
+EXPORT_SYMBOL(bio_truncate);
+
 /**
  * bio_put - release a reference to a bio
  * @bio:   bio to release reference to
fs/buffer.c +1 -24
index d8c7242426bb..e94a6619464c 100644
@@ -3034,8 +3034,6 @@ static void end_bio_bh_io_sync(struct bio *bio)
 void guard_bio_eod(int op, struct bio *bio)
 {
 	sector_t maxsector;
-	struct bio_vec *bvec = bio_last_bvec_all(bio);
-	unsigned truncated_bytes;
 	struct hd_struct *part;
 
 	rcu_read_lock();
@@ -3061,28 +3059,7 @@ void guard_bio_eod(int op, struct bio *bio)
 	if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
 		return;
 
-	/* Uhhuh. We've got a bio that straddles the device size! */
-	truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9);
-
-	/*
-	 * The bio contains more than one segment which spans EOD, just return
-	 * and let IO layer turn it into an EIO
-	 */
-	if (truncated_bytes > bvec->bv_len)
-		return;
-
-	/* Truncate the bio.. */
-	bio->bi_iter.bi_size -= truncated_bytes;
-	bvec->bv_len -= truncated_bytes;
-
-	/* ..and clear the end of the buffer for reads */
-	if (op == REQ_OP_READ) {
-		struct bio_vec bv;
-
-		mp_bvec_last_segment(bvec, &bv);
-		zero_user(bv.bv_page, bv.bv_offset + bv.bv_len,
-				truncated_bytes);
-	}
+	bio_truncate(bio, maxsector << 9);
 }
 
 static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
include/linux/bio.h +1 -0
index 3cdb84cdc488..853d92ceee64 100644
@@ -470,6 +470,7 @@ extern struct bio *bio_copy_user_iov(struct request_queue *,
 				     gfp_t);
 extern int bio_uncopy_user(struct bio *);
 void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter);
+void bio_truncate(struct bio *bio, unsigned new_size);
 
 static inline void zero_fill_bio(struct bio *bio)
 {
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 14 lines · Ming Lei

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 dce30ca9e3b6 ("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 forever.

Also the current truncating on last segment is dangerous by updating the last bvec, given the bvec table becomes not immutable, and fs bio users may lose to retrieve pages via bio_for_each_segment_all() in its .end_io callback.

Quoted 7 lines

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

This looks good to me, but we don't need the export of the symbol.

Signature · 1 line
Jens Axboe