Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH RFC] Btrfs: only subtract from len_to_oe_boundary when it is tracking an extent
@ 2023-07-30 19:02 Chris Mason
  2023-07-30 20:27 ` Sweet Tea Dorminy
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Chris Mason @ 2023-07-30 19:02 UTC (permalink / raw)
  To: linux-btrfs, dsterba, josef, hch

[ This is an RFC because Christoph switched us to almost always set
len_to_oe_boundary in a patch in for-next  I think we still need this
commit for strange corners, but it's already pretty hard to hit reliably
so I wanted to toss it out for discussion. We should consider either
Christoph's "btrfs: limit write bios to a single ordered extent" or this
commit for 6.4 stable as well ]

bio_ctrl->len_to_oe_boundary is used to make sure we stay inside an
extent as we submit bios.  Every time we add a page to the bio, we
decrement those bytes from len_to_oe_boundary, and then we submit the
bio if we happen to hit zero.

Most of the time, len_to_oe_boundary gets set to U32_MAX.  With
Christoph's incoming ("btrfs: limit write bios to a single ordered
extent") we're almost always setting len_to_oe_boundary, so we might not
need this commit moving forward.  But, there's a corner of a corner in here
where we can still create a massive bio, so talking through it:

submit_extent_page() adds pages into our bio, and the size of the bio
ends up limited by:

- Are we contiguous on disk?
- Does bio_add_page() allow us to stuff more in?
- is len_to_oe_boundary > 0?

The len_to_oe_boundary math starts with U32_MAX, which isn't page or
sector aligned, and subtracts from it until it hits zero.  In the
non-ordered extent case, the last IO we submit before we hit zero is
going to be unaligned, triggering BUGs and other sadness.

This is hard to trigger because bio_add_page() isn't going to make a bio
of U32_MAX size unless you give it a perfect set of pages and fully
contiguous extents on disk.  We can hit it pretty reliably while making
large swapfiles during provisioning because the machine is freshly
booted, mostly idle, and the disk is freshly formatted.

The code has been cleaned up and shifted around a few times, but this flaw
has been lurking since the counter was added.  I think Christoph's
commit ended up exposing the bug, but it's pretty tricky to get bios
big enough to prove if older kernels have the same problem.

The fix used here is to skip doing math on len_to_oe_boundary unless
we've changed it from the default U32_MAX value.  bio_add_page() is the
real limited we want, and there's no reason to do extra math when Jens
is doing it for us.

Signed-off-by: Chris Mason <clm@fb.com>
Fixes: 24e6c8082208 ("btrfs: simplify main loop in submit_extent_page")
---
 fs/btrfs/extent_io.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 6b40189a1a3e..bb2d2d405d04 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -849,7 +849,17 @@ static void submit_extent_page(struct btrfs_bio_ctrl *bio_ctrl,
 		size -= len;
 		pg_offset += len;
 		disk_bytenr += len;
-		bio_ctrl->len_to_oe_boundary -= len;
+
+		/*
+		 * len_to_oe_boundary defaults to U32_MAX, which isn't page or
+		 * sector aligned.  So, we don't really want to do math on
+		 * len_to_oe_boundary unless it has been intentionally set by
+		 * alloc_new_bio().  If we decrement here, we'll potentially
+		 * end up sending down an unaligned bio once we get close to
+		 * zero.
+		 */
+		if (bio_ctrl->len_to_oe_boundary != U32_MAX)
+			bio_ctrl->len_to_oe_boundary -= len;
 
 		/* Ordered extent boundary: move on to a new bio. */
 		if (bio_ctrl->len_to_oe_boundary == 0)
-- 
2.34.1


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

end of thread, other threads:[~2023-08-01  3:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-30 19:02 [PATCH RFC] Btrfs: only subtract from len_to_oe_boundary when it is tracking an extent Chris Mason
2023-07-30 20:27 ` Sweet Tea Dorminy
2023-07-31 19:22   ` Chris Mason
2023-08-01  2:59     ` Sweet Tea Dorminy
2023-07-31  2:27 ` Qu Wenruo
2023-07-31  7:02   ` Christoph Hellwig
2023-07-31 18:10     ` Chris Mason
2023-08-01  0:58       ` Qu Wenruo
2023-07-31  7:00 ` Christoph Hellwig
2023-07-31 18:52   ` Chris Mason
2023-07-31 19:35     ` Christoph Hellwig
2023-07-31 21:05       ` Chris Mason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox