Skip to contentSkip to conversation

kernel BUG at drivers/block/virtio_blk.c:166!]

[email protected]: In review Maurizio Lombardi 4 participants 21 messages May 26, 2014 – May 30, 2014 Block layer

lore
Share & tools
lore
#0 lore raw

Hi Jens,

looks like that commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify-__bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduces a regression, as reported by Jet Chan.

Do you have any idea about the possible problem with this patch?

it is the one that performs a recount of the segments in case of failure in __bio_add_page()

http://www.spinics.net/lists/mm-commits/msg103684.html

I would not be surprised if the bug was introduced by fceb38f36f, because it contained a mystake that commit 3979ef4dcf supposedly fixed.
But learning that commit 3979ef4dcf is introducing a regression leaves me quite puzzled.

Thanks,
Maurizio Lombardi

#1 lore raw
Quoted 9 lines · Maurizio Lombardi

Hi Jens,

looks like that commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify-__bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduces a regression, as reported by Jet Chan.

Do you have any idea about the possible problem with this patch?

it is the one that performs a recount of the segments in case of failure in __bio_add_page()

Quoted 8 lines

http://www.spinics.net/lists/mm-commits/msg103684.html

I would not be surprised if the bug was introduced by fceb38f36f, because it contained a mystake that commit 3979ef4dcf supposedly fixed.
But learning that commit 3979ef4dcf is introducing a regression leaves me quite puzzled.

From code of __blk_recalc_rq_segments(), looks it

won't check if recounted physical segment number is bigger than queue_max_segments(), so wondering if blk_recount_segments() can always decrease physical segment number.

Thanks,
--
Ming Lei

#2 lore raw
Quoted 17 lines · Ming Lei
Quoted 7 lines · Maurizio Lombardi

Hi Jens,

looks like that commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify-__bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduces a regression, as reported by Jet Chan.

Do you have any idea about the possible problem with this patch?

Quoted 8 lines

it is the one that performs a recount of the segments in case of failure in __bio_add_page()

http://www.spinics.net/lists/mm-commits/msg103684.html

I would not be surprised if the bug was introduced by fceb38f36f, because it contained a mystake that commit 3979ef4dcf supposedly fixed.
But learning that commit 3979ef4dcf is introducing a regression leaves me quite puzzled.

Quoted 6 lines

From code of __blk_recalc_rq_segments(), looks it won't check if recounted physical segment number is bigger than queue_max_segments(), so wondering if blk_recount_segments() can always decrease physical segment number.

This is what __bio_add_page() did before both fceb38f36f and 3979ef4dcf at line 757

        while (bio->bi_phys_segments >= queue_max_segments(q)) {

                if (retried_segments)
                        return 0;

                retried_segments = 1;
                blk_recount_segments(q, bio);
        }

so it is possible, in case of error, to return from the function even if the recounted physical segments are bigger than queue_max_segments(q).

---------

But now I'm suspicious of this part of commit 3979ef4dcf:

failed:

        bvec->bv_page = NULL;
        bvec->bv_len = 0;
        bvec->bv_offset = 0;
        bio->bi_vcnt--;  <----------------
        blk_recount_segments(q, bio);
        return 0;

Is decreasing bi_vcnt sufficient to guarantee that blk_recount_segments() recalculates the correct number of physical segments? Looking@the __blk_recalc_rq_segments() it appears it may not be the case.

The question is how can we restore the correct number of physical segments in case of failure without breaking anything...

Regards,
Maurizio Lombardi

#3 lore raw
Quoted 11 lines · Maurizio Lombardi

But now I'm suspicious of this part of commit 3979ef4dcf:

failed:

        bvec->bv_page = NULL;
        bvec->bv_len = 0;
        bvec->bv_offset = 0;
        bio->bi_vcnt--;  <----------------
        blk_recount_segments(q, bio);
        return 0;
Quoted 7 lines

Is decreasing bi_vcnt sufficient to guarantee that blk_recount_segments() recalculates the correct number of physical segments?
Looking at the __blk_recalc_rq_segments() it appears it may not be the case.

The question is how can we restore the correct number of physical segments in case of failure without breaking anything...

If my hypothesis is correct, the following patch should trigger a kernel panic, Jet Chen, can you try it and let me know whether the BUG_ON is hit or not?

block/bio.c +5 -0
index 0443694..763868f 100644
@@ -701,6 +701,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 			  unsigned int max_sectors)
 {
 	int retried_segments = 0;
+	unsigned int phys_segments_orig;
 	struct bio_vec *bvec;
 
 	/*
@@ -751,6 +752,9 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	if (bio->bi_vcnt >= bio->bi_max_vecs)
 		return 0;
 
+	blk_recount_segments(q, bio);
+	phys_segments_orig = bio->bi_phys_segments;
+
 	/*
 	 * setup the new entry, we might clear it again later if we
 	 * cannot add the page
@@ -811,6 +815,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_offset = 0;
 	bio->bi_vcnt--;
 	blk_recount_segments(q, bio);
+	BUG_ON(phys_segments_orig != bio->bi_phys_segments);
 	return 0;
 }
 

Regards,
Maurizio Lombardi

#4 lore raw

Hi,

Quoted 16 lines · Maurizio Lombardi
Quoted 15 lines · Maurizio Lombardi

But now I'm suspicious of this part of commit 3979ef4dcf:

failed:

        bvec->bv_page = NULL;
        bvec->bv_len = 0;
        bvec->bv_offset = 0;
        bio->bi_vcnt--;  <----------------
        blk_recount_segments(q, bio);
        return 0;

Is decreasing bi_vcnt sufficient to guarantee that blk_recount_segments() recalculates the correct number of physical segments?
Looking at the __blk_recalc_rq_segments() it appears it may not be the case.

Quoted 5 lines
Quoted 2 lines

The question is how can we restore the correct number of physical segments in case of failure without breaking anything...

If my hypothesis is correct, the following patch should trigger a kernel panic, Jet Chen, can you try it and let me know whether the BUG_ON is hit or not?

I was also able to reproduce this bug just as reported by Jet Chen, and now I think I've found out a solution. I'll send out a patch.

Regards,
Dongsu

Quoted 38 lines
block/bio.c +5 -0
index 0443694..763868f 100644
@@ -701,6 +701,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 			  unsigned int max_sectors)
 {
 	int retried_segments = 0;
+	unsigned int phys_segments_orig;
 	struct bio_vec *bvec;
 
 	/*
@@ -751,6 +752,9 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	if (bio->bi_vcnt >= bio->bi_max_vecs)
 		return 0;
 
+	blk_recount_segments(q, bio);
+	phys_segments_orig = bio->bi_phys_segments;
+
 	/*
 	 * setup the new entry, we might clear it again later if we
 	 * cannot add the page
@@ -811,6 +815,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_offset = 0;
 	bio->bi_vcnt--;
 	blk_recount_segments(q, bio);
+	BUG_ON(phys_segments_orig != bio->bi_phys_segments);
 	return 0;
 }
 

Regards,
Maurizio Lombardi
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo(a)vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#5 lore raw

PATCH 1/1 bio: decrease bi_iter.bi_size by len in the fail path

In reply to Maurizio Lombardi

From: Dongsu Park <[email protected]>

Commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify- __bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduced a regression as reported by Jet Chen.
That results in a kernel BUG at drivers/block/virtio_blk.c:166.

To fix that, bi_iter.bi_size must be decreased by len, before recounting the number of physical segments.

Tested on with kernel 3.15.0-rc7-next-20140527 on qemu guest, by running xfstests/ext4/271.

Cc Jens Axboe <[email protected]>
Cc Jet Chen <[email protected]>
Cc Maurizio Lombardi <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +1 -0
index 0443694ccbb4..67d7cba1e5fd 100644
@@ -810,6 +810,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_len = 0;
 	bvec->bv_offset = 0;
 	bio->bi_vcnt--;
+	bio->bi_iter.bi_size -= len;
 	blk_recount_segments(q, bio);
 	return 0;
 }
Signature · 1 line
1.9.1
#6 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

Hi Dongsu,

Quoted 26 lines · Dongsu Park

From: Dongsu Park <[email protected]>

Commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify- __bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduced a regression as reported by Jet Chen.
That results in a kernel BUG at drivers/block/virtio_blk.c:166.

To fix that, bi_iter.bi_size must be decreased by len, before recounting the number of physical segments.

Tested on with kernel 3.15.0-rc7-next-20140527 on qemu guest, by running xfstests/ext4/271.

Cc Jens Axboe <[email protected]>
Cc Jet Chen <[email protected]>
Cc Maurizio Lombardi <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +0 -0
index 0443694ccbb4..67d7cba1e5fd 100644

block/bio.c static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page · line 813

Quoted 4 lines
block/bio.c +1 -0
@@ -810,6 +810,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
        bvec->bv_len = 0;
        bvec->bv_offset = 0;
        bio->bi_vcnt--;
+       bio->bi_iter.bi_size -= len;

Would you mind explaining why bi_iter.bi_size need to be decreased by 'len'? In the failure path, it wasn't added by 'len', was it?

Thanks,

Signature · 1 line
Ming Lei
#7 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

Quoted 25 lines · Ming Lei

Hi Dongsu,

Quoted 21 lines · Dongsu Park

From: Dongsu Park <[email protected]>

Commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify- __bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduced a regression as reported by Jet Chen.
That results in a kernel BUG at drivers/block/virtio_blk.c:166.

To fix that, bi_iter.bi_size must be decreased by len, before recounting the number of physical segments.

Tested on with kernel 3.15.0-rc7-next-20140527 on qemu guest, by running xfstests/ext4/271.

Cc Jens Axboe <[email protected]>
Cc Jet Chen <[email protected]>
Cc Maurizio Lombardi <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 13 lines

block/bio.c static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page · line 813

Quoted 9 lines
block/bio.c +1 -0
index 0443694ccbb4..67d7cba1e5fd 100644
@@ -810,6 +810,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
        bvec->bv_len = 0;
        bvec->bv_offset = 0;
        bio->bi_vcnt--;
+       bio->bi_iter.bi_size -= len;

Would you mind explaining why bi_iter.bi_size need to be decreased by 'len'? In the failure path, it wasn't added by 'len', was it?

Actually, the correct thing may be like what did in the attached patch, as Maurizio discussed with me[1].

Very interestingly, I have reproduced the problem one time with ext4/271 ext4/301 ext4/305, but won't with the attached patch after running it for 3 rounds.

[tom@localhost xfstests]$ sudo ./check ext4/271 ext4/301 ext4/305

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 localhost 3.15.0-rc7-next-20140527+
MKFS_OPTIONS  -- /dev/vdc

MOUNT_OPTIONS -- -o acl,user_xattr /dev/vdc /mnt/scratch

ext4/271 1s ... 1s
ext4/301 31s ... 32s
ext4/305 181s ... 180s
Ran: ext4/271 ext4/301 ext4/305
Passed all 3 tests

Jet, could you test the attached patch?

[1], https://lkml.org/lkml/2014/5/27/327

Thanks,

Signature · 1 line
Ming Lei
#8 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

Hi Ming,

Quoted 20 lines · Ming Lei

Actually, the correct thing may be like what did in the attached patch, as Maurizio discussed with me[1].

Very interestingly, I have reproduced the problem one time with ext4/271 ext4/301 ext4/305, but won't with the attached patch after running it for 3 rounds.

[tom@localhost xfstests]$ sudo ./check ext4/271 ext4/301 ext4/305

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 localhost 3.15.0-rc7-next-20140527+
MKFS_OPTIONS  -- /dev/vdc

MOUNT_OPTIONS -- -o acl,user_xattr /dev/vdc /mnt/scratch

ext4/271 1s ... 1s
ext4/301 31s ... 32s
ext4/305 181s ... 180s
Ran: ext4/271 ext4/301 ext4/305
Passed all 3 tests

Quoted 3 lines

Jet, could you test the attached patch?

[1], https://lkml.org/lkml/2014/5/27/327

There is a little mistake in your patch, you removed bio->bi_iter.bi_size += len; after the "done" label,
but be careful that at line 747 there is a "goto done"... bi_size should be incremented before jumping there.

Thanks,
Maurizio Lombardi

#9 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

Quoted 23 lines · Maurizio Lombardi

Hi Ming,

Quoted 20 lines · Ming Lei

Actually, the correct thing may be like what did in the attached patch, as Maurizio discussed with me[1].

Very interestingly, I have reproduced the problem one time with ext4/271 ext4/301 ext4/305, but won't with the attached patch after running it for 3 rounds.

[tom@localhost xfstests]$ sudo ./check ext4/271 ext4/301 ext4/305

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 localhost 3.15.0-rc7-next-20140527+
MKFS_OPTIONS  -- /dev/vdc

MOUNT_OPTIONS -- -o acl,user_xattr /dev/vdc /mnt/scratch

ext4/271 1s ... 1s
ext4/301 31s ... 32s
ext4/305 181s ... 180s
Ran: ext4/271 ext4/301 ext4/305
Passed all 3 tests

Quoted 8 lines
Quoted 3 lines

Jet, could you test the attached patch?

[1], https://lkml.org/lkml/2014/5/27/327

There is a little mistake in your patch, you removed bio->bi_iter.bi_size += len; after the "done" label,
but be careful that at line 747 there is a "goto done"... bi_size should be incremented before jumping there.

Good catch, thanks Maurizio.

Jet, please test the attached patch in this mail and ignore previous one.

The story behind the patch should be like below:

- one page is added in __bio_add_page() 'successfully', and bio->bi_phys_segments is equal to queue_max_segments(q), but it should have been rejected since the last vector isn't covered

- next time, __bio_add_page() is called to add one page, but this time blk_recount_segments() can figure out the actual physical segments and find it is more than max segments, so failure is triggered, but the bio->bi_phys_segments is updated with max segments plus one

- the oops is triggered and reported by Jet, :-)

Thanks,

Signature · 1 line
Ming Lei
block/bio.c +3 -1
index 0443694..f9bae56 100644
@@ -744,6 +744,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 				}
 			}
 
+			bio->bi_iter.bi_size += len;
 			goto done;
 		}
 	}
@@ -761,6 +762,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_offset = offset;
 	bio->bi_vcnt++;
 	bio->bi_phys_segments++;
+	bio->bi_iter.bi_size += len;
 
 	/*
 	 * Perform a recount if the number of segments is greater
@@ -802,7 +804,6 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 		bio->bi_flags &= ~(1 << BIO_SEG_VALID);
 
  done:
-	bio->bi_iter.bi_size += len;
 	return len;
 
  failed:
@@ -810,6 +811,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_len = 0;
 	bvec->bv_offset = 0;
 	bio->bi_vcnt--;
+	bio->bi_iter.bi_size -= len;
 	blk_recount_segments(q, bio);
 	return 0;
 }
Tested-by
#10 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

Quoted 50 lines · Ming Lei
Quoted 23 lines · Maurizio Lombardi

Hi Ming,

Quoted 20 lines · Ming Lei

Actually, the correct thing may be like what did in the attached patch, as Maurizio discussed with me[1].

Very interestingly, I have reproduced the problem one time with ext4/271 ext4/301 ext4/305, but won't with the attached patch after running it for 3 rounds.

[tom@localhost xfstests]$ sudo ./check ext4/271 ext4/301 ext4/305

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 localhost 3.15.0-rc7-next-20140527+
MKFS_OPTIONS  -- /dev/vdc

MOUNT_OPTIONS -- -o acl,user_xattr /dev/vdc /mnt/scratch

ext4/271 1s ... 1s
ext4/301 31s ... 32s
ext4/305 181s ... 180s
Ran: ext4/271 ext4/301 ext4/305
Passed all 3 tests

Quoted 8 lines
Quoted 3 lines

Jet, could you test the attached patch?

[1], https://lkml.org/lkml/2014/5/27/327

There is a little mistake in your patch, you removed bio->bi_iter.bi_size += len; after the "done" label,
but be careful that at line 747 there is a "goto done"... bi_size should be incremented before jumping there.

Good catch, thanks Maurizio.

Jet, please test the attached patch in this mail and ignore previous one.

The story behind the patch should be like below:

- one page is added in __bio_add_page() 'successfully', and bio->bi_phys_segments is equal to queue_max_segments(q), but it should have been rejected since the last vector isn't covered

- next time, __bio_add_page() is called to add one page, but this time blk_recount_segments() can figure out the actual physical segments and find it is more than max segments, so failure is triggered, but the bio->bi_phys_segments is updated with max segments plus one

Quoted 5 lines

- the oops is triggered and reported by Jet, :-)

Thanks,

This patch works, thanks.

block/bio.c +3 -1
index 0443694..f9bae56 100644
@@ -744,6 +744,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 				}
 			}
 
+			bio->bi_iter.bi_size += len;
 			goto done;
 		}
 	}
@@ -761,6 +762,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_offset = offset;
 	bio->bi_vcnt++;
 	bio->bi_phys_segments++;
+	bio->bi_iter.bi_size += len;
 
 	/*
 	 * Perform a recount if the number of segments is greater
@@ -802,7 +804,6 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 		bio->bi_flags &= ~(1 << BIO_SEG_VALID);
 
  done:
-	bio->bi_iter.bi_size += len;
 	return len;
 
  failed:
@@ -810,6 +811,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_len = 0;
 	bvec->bv_offset = 0;
 	bio->bi_vcnt--;
+	bio->bi_iter.bi_size -= len;
 	blk_recount_segments(q, bio);
 	return 0;
 }
#11 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

Quoted 34 lines · Jet Chen

This patch works, thanks.

block/bio.c +2 -1
index 0443694..f9bae56 100644
@@ -744,6 +744,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 				}
 			}
 
+			bio->bi_iter.bi_size += len;
 			goto done;
 		}
 	}
@@ -761,6 +762,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_offset = offset;
 	bio->bi_vcnt++;
 	bio->bi_phys_segments++;
+	bio->bi_iter.bi_size += len;
 
 	/*
 	 * Perform a recount if the number of segments is greater
@@ -802,7 +804,6 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 		bio->bi_flags &= ~(1 << BIO_SEG_VALID);
 
  done:
-	bio->bi_iter.bi_size += len;
 	return len;
 
  failed:

block/bio.c static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page · line 817

Quoted 7 lines
block/bio.c +1 -0
@@ -810,6 +811,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_len = 0;
 	bvec->bv_offset = 0;
 	bio->bi_vcnt--;
+	bio->bi_iter.bi_size -= len;
 	blk_recount_segments(q, bio);
 	return 0;
 }

Good!

Jens, can you review and merge it?

Thanks,
Maurizio Lombardi

#12 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

Quoted 4 lines · Jet Chen

On 05/29/2014 01:44 AM, Ming Lei wrote:
This patch works, thanks.

Jet, thanks for your test.

Quoted 2 lines · Maurizio Lombardi

Jens, can you review and merge it?

I will prepare a formal one for merging.

It should be better to merge it via -mm tree because the other two commits which caused the regression are from -mm.

Thanks,

Signature · 1 line
Ming Lei
#13 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

In reply to Ming Lei
Quoted 53 lines · Ming Lei
Quoted 25 lines · Ming Lei

Hi Dongsu,

Quoted 21 lines · Dongsu Park

From: Dongsu Park <[email protected]>

Commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify- __bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduced a regression as reported by Jet Chen.
That results in a kernel BUG at drivers/block/virtio_blk.c:166.

To fix that, bi_iter.bi_size must be decreased by len, before recounting the number of physical segments.

Tested on with kernel 3.15.0-rc7-next-20140527 on qemu guest, by running xfstests/ext4/271.

Cc Jens Axboe <[email protected]>
Cc Jet Chen <[email protected]>
Cc Maurizio Lombardi <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 13 lines

block/bio.c static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page · line 813

Quoted 9 lines
block/bio.c +1 -0
index 0443694ccbb4..67d7cba1e5fd 100644
@@ -810,6 +810,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
        bvec->bv_len = 0;
        bvec->bv_offset = 0;
        bio->bi_vcnt--;
+       bio->bi_iter.bi_size -= len;

Would you mind explaining why bi_iter.bi_size need to be decreased by 'len'? In the failure path, it wasn't added by 'len', was it?

Actually, the correct thing may be like what did in the attached patch, as Maurizio discussed with me[1].

Very interestingly, I have reproduced the problem one time with ext4/271 ext4/301 ext4/305, but won't with the attached patch after running it for 3 rounds.

[tom@localhost xfstests]$ sudo ./check ext4/271 ext4/301 ext4/305

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 localhost 3.15.0-rc7-next-20140527+
MKFS_OPTIONS  -- /dev/vdc

MOUNT_OPTIONS -- -o acl,user_xattr /dev/vdc /mnt/scratch

Quoted 7 lines

ext4/271 1s ... 1s
ext4/301 31s ... 32s
ext4/305 181s ... 180s
Ran: ext4/271 ext4/301 ext4/305
Passed all 3 tests

Jet, could you test the attached patch?

sorry, could you specify which patch need me to test ? actually I got confused. I only find

[PATCH V3] bio: modify __bio_add_page() to accept pages that don't start a new segment

in this mail thread. is it need to be tested ?

on next/master branch,

commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d

Author: Maurizio Lombardi <[email protected]>
Date:   Sat May 17 23:17:30 2014 +1000

bio-modify-__bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3

Changes in V3:

In case of error, V2 restored the previous number of segments but left the BIO_SEG_FLAG set.
To avoid problems, after the page is removed from the bio vec, V3 performs a recount of the segments in the error code path.

Cc Al Viro <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Jens Axboe <[email protected]>

commit fceb38f36f4fecabf9ca33aa44a3f943f133cb78

Author: Maurizio Lombardi <[email protected]>
Date:   Sat May 17 23:17:30 2014 +1000

bio: modify __bio_add_page() to accept pages that don't start a new segment

The original behaviour is to refuse to add a new page if the maximum number of segments has been reached, regardless of the fact the page we

3979ef4dcf3d1de55a560a3a4016c30a835df44d is the first bad commit.

Quoted 6 lines

[1], https://lkml.org/lkml/2014/5/27/327

Thanks,

#14 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

Quoted 60 lines · Jet Chen
Quoted 59 lines · Ming Lei
Quoted 25 lines · Ming Lei

Hi Dongsu,

Quoted 21 lines · Dongsu Park

From: Dongsu Park <[email protected]>

Commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify- __bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduced a regression as reported by Jet Chen.
That results in a kernel BUG at drivers/block/virtio_blk.c:166.

To fix that, bi_iter.bi_size must be decreased by len, before recounting the number of physical segments.

Tested on with kernel 3.15.0-rc7-next-20140527 on qemu guest, by running xfstests/ext4/271.

Cc Jens Axboe <[email protected]>
Cc Jet Chen <[email protected]>
Cc Maurizio Lombardi <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 13 lines

block/bio.c static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page · line 813

Quoted 9 lines
block/bio.c +1 -0
index 0443694ccbb4..67d7cba1e5fd 100644
@@ -810,6 +810,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
        bvec->bv_len = 0;
        bvec->bv_offset = 0;
        bio->bi_vcnt--;
+       bio->bi_iter.bi_size -= len;

Would you mind explaining why bi_iter.bi_size need to be decreased by 'len'? In the failure path, it wasn't added by 'len', was it?

Actually, the correct thing may be like what did in the attached patch, as Maurizio discussed with me[1].

Very interestingly, I have reproduced the problem one time with ext4/271 ext4/301 ext4/305, but won't with the attached patch after running it for 3 rounds.

[tom@localhost xfstests]$ sudo ./check ext4/271 ext4/301 ext4/305

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 localhost 3.15.0-rc7-next-20140527+
MKFS_OPTIONS  -- /dev/vdc

MOUNT_OPTIONS -- -o acl,user_xattr /dev/vdc /mnt/scratch

ext4/271 1s ... 1s
ext4/301 31s ... 32s
ext4/305 181s ... 180s
Ran: ext4/271 ext4/301 ext4/305
Passed all 3 tests

Quoted 4 lines
Quoted 1 line

Jet, could you test the attached patch?

sorry, could you specify which patch need me to test ? actually I got confused. I only find

Firstly, dongsu's patch is wrong, and it doesn't make sense to test that.

Secondly, it is the patch attached in my last email, and the name is 'fix_compute_segments.patch'.

Please let me know if you can find the patch, if you still can't, I may resend to you.

Thanks,

Signature · 1 line
Ming Lei
#15 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

Quoted 66 lines · Ming Lei
Quoted 60 lines · Jet Chen
Quoted 59 lines · Ming Lei
Quoted 25 lines · Ming Lei

Hi Dongsu,

Quoted 21 lines · Dongsu Park

From: Dongsu Park <[email protected]>

Commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify- __bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduced a regression as reported by Jet Chen.
That results in a kernel BUG at drivers/block/virtio_blk.c:166.

To fix that, bi_iter.bi_size must be decreased by len, before recounting the number of physical segments.

Tested on with kernel 3.15.0-rc7-next-20140527 on qemu guest, by running xfstests/ext4/271.

Cc Jens Axboe <[email protected]>
Cc Jet Chen <[email protected]>
Cc Maurizio Lombardi <[email protected]>
---
  block/bio.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 13 lines

block/bio.c static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page · line 813

Quoted 9 lines
block/bio.c +1 -0
index 0443694ccbb4..67d7cba1e5fd 100644
@@ -810,6 +810,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
         bvec->bv_len = 0;
         bvec->bv_offset = 0;
         bio->bi_vcnt--;
+       bio->bi_iter.bi_size -= len;

Would you mind explaining why bi_iter.bi_size need to be decreased by 'len'? In the failure path, it wasn't added by 'len', was it?

Actually, the correct thing may be like what did in the attached patch, as Maurizio discussed with me[1].

Very interestingly, I have reproduced the problem one time with ext4/271 ext4/301 ext4/305, but won't with the attached patch after running it for 3 rounds.

[tom@localhost xfstests]$ sudo ./check ext4/271 ext4/301 ext4/305

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 localhost 3.15.0-rc7-next-20140527+
MKFS_OPTIONS  -- /dev/vdc

MOUNT_OPTIONS -- -o acl,user_xattr /dev/vdc /mnt/scratch

ext4/271 1s ... 1s
ext4/301 31s ... 32s
ext4/305 181s ... 180s
Ran: ext4/271 ext4/301 ext4/305
Passed all 3 tests

Quoted 4 lines
Quoted 1 line

Jet, could you test the attached patch?

sorry, could you specify which patch need me to test ? actually I got confused. I only find

Quoted 9 lines

Firstly, dongsu's patch is wrong, and it doesn't make sense to test that.

Secondly, it is the patch attached in my last email, and the name is 'fix_compute_segments.patch'.

Please let me know if you can find the patch, if you still can't, I may resend to you.

Just got your email which attached that patch, thanks. I guess there is some network problem on my side which leads to some latency. Will test it out.

Quoted 3 lines

Thanks,

Tested-by
#16 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

In reply to Ming Lei

Hi Ming,

Quoted 7 lines · Ming Lei
Quoted 3 lines · Ming Lei

Would you mind explaining why bi_iter.bi_size need to be decreased by 'len'? In the failure path, it wasn't added by 'len', was it?

Actually, the correct thing may be like what did in the attached patch, as Maurizio discussed with me[1].

Yes, I agree. Your patch is the correct fix.
Please ignore my patch, and take yours,
<https://lkml.org/lkml/2014/5/29/370>

Quoted 3 lines

Very interestingly, I have reproduced the problem one time with ext4/271 ext4/301 ext4/305, but won't with the attached patch after running it for 3 rounds.

I also had a trouble with reproducing this bug on my home desktop, while I can reliably reproduce the bug on my office workstation. I'm not quite sure what makes the difference.
(The same kernel, the same qemu version, the same xfstests version, etc.)

Anyway, coming back to my office, now I can confirm that your patch fixes the bug. You can add:

Thanks,
Dongsu

Quoted 20 lines

[tom@localhost xfstests]$ sudo ./check ext4/271 ext4/301 ext4/305

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 localhost 3.15.0-rc7-next-20140527+
MKFS_OPTIONS  -- /dev/vdc

MOUNT_OPTIONS -- -o acl,user_xattr /dev/vdc /mnt/scratch

ext4/271 1s ... 1s
ext4/301 31s ... 32s
ext4/305 181s ... 180s
Ran: ext4/271 ext4/301 ext4/305
Passed all 3 tests

Jet, could you test the attached patch?

[1], https://lkml.org/lkml/2014/5/27/327

Thanks,

Signature · 1 line
Ming Lei
#17 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

In reply to Dongsu Park

Hi,

thanks for the patch

Quoted 9 lines · Dongsu Park

From: Dongsu Park <[email protected]>

Commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify- __bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduced a regression as reported by Jet Chen.
That results in a kernel BUG at drivers/block/virtio_blk.c:166.

To fix that, bi_iter.bi_size must be decreased by len, before recounting the number of physical segments.

I don't understand exactly why it must be decreased by len. Can you provide more info?

Thanks,
Maurizio Lombardi

Quoted 27 lines

Tested on with kernel 3.15.0-rc7-next-20140527 on qemu guest, by running xfstests/ext4/271.

Cc Jens Axboe <[email protected]>
Cc Jet Chen <[email protected]>
Cc Maurizio Lombardi <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +1 -0
index 0443694ccbb4..67d7cba1e5fd 100644
@@ -810,6 +810,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_len = 0;
 	bvec->bv_offset = 0;
 	bio->bi_vcnt--;
+	bio->bi_iter.bi_size -= len;
 	blk_recount_segments(q, bio);
 	return 0;
 }
Signature · 1 line
1.9.1
Tested-by on v1 1/1
#18 lore raw

Re: [PATCH] bio: decrease bi_iter.bi_size by len in the fail path

In reply to Dongsu Park
Quoted 27 lines · Dongsu Park

From: Dongsu Park <[email protected]>

Commit 3979ef4dcf3d1de55a560a3a4016c30a835df44d ("bio-modify- __bio_add_page-to-accept-pages-that-dont-start-a-new-segment-v3") introduced a regression as reported by Jet Chen.
That results in a kernel BUG at drivers/block/virtio_blk.c:166.

To fix that, bi_iter.bi_size must be decreased by len, before recounting the number of physical segments.

Tested on with kernel 3.15.0-rc7-next-20140527 on qemu guest, by running xfstests/ext4/271.

Cc Jens Axboe <[email protected]>
Cc Jet Chen <[email protected]>
Cc Maurizio Lombardi <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +0 -0
index 0443694ccbb4..67d7cba1e5fd 100644

block/bio.c static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page · line 816

Quoted 7 lines
block/bio.c +1 -0
@@ -810,6 +810,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_len = 0;
 	bvec->bv_offset = 0;
 	bio->bi_vcnt--;
+	bio->bi_iter.bi_size -= len;
 	blk_recount_segments(q, bio);
 	return 0;
 }

This patch fixes the problem.

#19 lore raw
In reply to Maurizio Lombardi
Quoted 16 lines · Maurizio Lombardi
Quoted 15 lines · Maurizio Lombardi

But now I'm suspicious of this part of commit 3979ef4dcf:

failed:

        bvec->bv_page = NULL;
        bvec->bv_len = 0;
        bvec->bv_offset = 0;
        bio->bi_vcnt--;  <----------------
        blk_recount_segments(q, bio);
        return 0;

Is decreasing bi_vcnt sufficient to guarantee that blk_recount_segments() recalculates the correct number of physical segments?
Looking at the __blk_recalc_rq_segments() it appears it may not be the case.

Quoted 6 lines
Quoted 3 lines

The question is how can we restore the correct number of physical segments in case of failure without breaking anything...

If my hypothesis is correct, the following patch should trigger a kernel panic, Jet Chen, can you try it and let me know whether the BUG_ON is hit or not?

Sorry for late respond. Dongsu has sent a patch for this issue.

	message-id <[email protected]>

Do you still need me to test the following patch ?

Quoted 35 lines
block/bio.c +5 -0
index 0443694..763868f 100644
@@ -701,6 +701,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 			  unsigned int max_sectors)
 {
 	int retried_segments = 0;
+	unsigned int phys_segments_orig;
 	struct bio_vec *bvec;
 
 	/*
@@ -751,6 +752,9 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	if (bio->bi_vcnt >= bio->bi_max_vecs)
 		return 0;
 
+	blk_recount_segments(q, bio);
+	phys_segments_orig = bio->bi_phys_segments;
+
 	/*
 	 * setup the new entry, we might clear it again later if we
 	 * cannot add the page
@@ -811,6 +815,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	bvec->bv_offset = 0;
 	bio->bi_vcnt--;
 	blk_recount_segments(q, bio);
+	BUG_ON(phys_segments_orig != bio->bi_phys_segments);
 	return 0;
 }
 

Regards,
Maurizio Lombardi

#20 lore raw

Hi,

Quoted 4 lines · Jet Chen

Sorry for late respond. Dongsu has sent a patch for this issue.

	message-id <[email protected]>

Do you still need me to test the following patch ?

No, don't test my patch, it is not needed anymore, please test Dongsu's patch instead so we are sure the regression is fixed.

Thanks,
Maurizio Lombardi