All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] block: Fix iterating over an empty bio with bio_for_each_folio_all
@ 2024-01-16 21:29 Matthew Wilcox (Oracle)
  2024-01-16 22:03 ` Jens Axboe
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Wilcox (Oracle) @ 2024-01-16 21:29 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Matthew Wilcox (Oracle),
	linux-block, linux-kernel, syzbot+8b23309d5788a79d3eea,
	syzbot+004c1e0fced2b4bc3dcc, stable

If the bio contains no data, bio_first_folio() calls page_folio() on a
NULL pointer and oopses.  Move the test that we've reached the end of
the bio from bio_next_folio() to bio_first_folio().

Reported-by: syzbot+8b23309d5788a79d3eea@syzkaller.appspotmail.com
Reported-by: syzbot+004c1e0fced2b4bc3dcc@syzkaller.appspotmail.com
Fixes: 640d1930bef4 ("block: Add bio_for_each_folio_all()")
Cc: stable@vger.kernel.org
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 include/linux/bio.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/include/linux/bio.h b/include/linux/bio.h
index ec4db73e5f4e..1518f1201ddd 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -286,6 +286,11 @@ static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
 {
 	struct bio_vec *bvec = bio_first_bvec_all(bio) + i;
 
+	if (i >= bio->bi_vcnt) {
+		fi->folio = NULL;
+		return;
+	}
+
 	fi->folio = page_folio(bvec->bv_page);
 	fi->offset = bvec->bv_offset +
 			PAGE_SIZE * (bvec->bv_page - &fi->folio->page);
@@ -303,10 +308,8 @@ static inline void bio_next_folio(struct folio_iter *fi, struct bio *bio)
 		fi->offset = 0;
 		fi->length = min(folio_size(fi->folio), fi->_seg_count);
 		fi->_next = folio_next(fi->folio);
-	} else if (fi->_i + 1 < bio->bi_vcnt) {
-		bio_first_folio(fi, bio, fi->_i + 1);
 	} else {
-		fi->folio = NULL;
+		bio_first_folio(fi, bio, fi->_i + 1);
 	}
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] block: Fix iterating over an empty bio with bio_for_each_folio_all
  2024-01-16 21:29 [PATCH] block: Fix iterating over an empty bio with bio_for_each_folio_all Matthew Wilcox (Oracle)
@ 2024-01-16 22:03 ` Jens Axboe
  2024-01-19 16:03   ` Matthew Wilcox
  0 siblings, 1 reply; 3+ messages in thread
From: Jens Axboe @ 2024-01-16 22:03 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle)
  Cc: linux-block, linux-kernel, syzbot+8b23309d5788a79d3eea,
	syzbot+004c1e0fced2b4bc3dcc, stable


On Tue, 16 Jan 2024 21:29:59 +0000, Matthew Wilcox (Oracle) wrote:
> If the bio contains no data, bio_first_folio() calls page_folio() on a
> NULL pointer and oopses.  Move the test that we've reached the end of
> the bio from bio_next_folio() to bio_first_folio().
> 
> 

Applied, thanks!

[1/1] block: Fix iterating over an empty bio with bio_for_each_folio_all
      commit: 7bed6f3d08b7af27b7015da8dc3acf2b9c1f21d7

Best regards,
-- 
Jens Axboe




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] block: Fix iterating over an empty bio with bio_for_each_folio_all
  2024-01-16 22:03 ` Jens Axboe
@ 2024-01-19 16:03   ` Matthew Wilcox
  0 siblings, 0 replies; 3+ messages in thread
From: Matthew Wilcox @ 2024-01-19 16:03 UTC (permalink / raw)
  To: Jens Axboe
  Cc: linux-block, linux-kernel, syzbot+8b23309d5788a79d3eea,
	syzbot+004c1e0fced2b4bc3dcc, stable

On Tue, Jan 16, 2024 at 03:03:46PM -0700, Jens Axboe wrote:
> 
> On Tue, 16 Jan 2024 21:29:59 +0000, Matthew Wilcox (Oracle) wrote:
> > If the bio contains no data, bio_first_folio() calls page_folio() on a
> > NULL pointer and oopses.  Move the test that we've reached the end of
> > the bio from bio_next_folio() to bio_first_folio().
> > 
> > 
> 
> Applied, thanks!
> 
> [1/1] block: Fix iterating over an empty bio with bio_for_each_folio_all
>       commit: 7bed6f3d08b7af27b7015da8dc3acf2b9c1f21d7

I see you added an unlikely(), and I'm not sure it's justified.
It's going to be true at the end of each iteration.  For a bio that
contains one bio_vec, it will be false once and true once.  It'd be like
writing a for loop as:

	for (i = 0; likely(i < n); i++) {
	}

which I've never seen us do.

I don't know that it's worth taking out, but I wouldn't've put it in.
I wouldn't be surprised to see benchmarks show it's a bad idea.

Could you at least _say_ when you're going to make that kind of change?
I wouldn't've noticed except that I got a merge conflict.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-19 16:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 21:29 [PATCH] block: Fix iterating over an empty bio with bio_for_each_folio_all Matthew Wilcox (Oracle)
2024-01-16 22:03 ` Jens Axboe
2024-01-19 16:03   ` Matthew Wilcox

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.