Btrfs: raid56: fix race between merge_bio and rbio_orig_end_io
PATCH 1/1 Btrfs: raid56: fix race between merge_bio and rbio_orig_end_io
We're not allowed to take any new bios to rbio->bio_list in rbio_orig_end_io(), otherwise we may get merged with more bios and rbio->bio_list is not empty.
This should only happens in error-out cases, the normal path of recover and full stripe write have already set RBIO_RMW_LOCKED_BIT to disable merge before doing IO.
--- fs/btrfs/raid56.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
fs/btrfs/raid56.c +12 -1
@@ -859,12 +859,23 @@ static void free_raid_bio(struct btrfs_raid_bio *rbio)*/ static void rbio_orig_end_io (struct btrfs_raid_bio *rbio,blk_status_t err) { -struct bio *cur =bio_list_get (&rbio->bio_list); +struct bio *cur;struct bio *next; +/* +* We're not allowed to take any new bios to rbio->bio_list +* from now on, otherwise we may get merged with more bios and +* rbio->bio_list is not empty. +*/ +spin_lock (&rbio->bio_list_lock); +set_bit (RBIO_RMW_LOCKED_BIT , &rbio->flags); +spin_unlock (&rbio->bio_list_lock); +if (rbio->generic_bio_cnt)btrfs_bio_counter_sub (rbio->fs_info, rbio->generic_bio_cnt); + cur =bio_list_get (&rbio->bio_list); +free_raid_bio (rbio);while (cur) {
Signature · 1 line
2.9.4
(Add Jérôme Carretero.)
Thanks,
-liubo
Quoted 50 lines · Liu Bo
We're not allowed to take any new bios to
rbio->bio_listinrbio_orig_end_io(), otherwise we may get merged with more bios andrbio->bio_listis not empty.This should only happens in error-out cases, the normal path of recover and full stripe write have already set
RBIO_RMW_LOCKED_BITto disable merge before doing IO.Reported-by Jérôme Carretero <[email protected]>Signed-off-by Liu Bo <[email protected]>--- fs/btrfs/raid56.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)fs/btrfs/raid56.c +12
-1@@ -859,12 +859,23 @@ static void free_raid_bio(struct btrfs_raid_bio *rbio)*/ static void rbio_orig_end_io (struct btrfs_raid_bio *rbio,blk_status_t err) { -struct bio *cur =bio_list_get (&rbio->bio_list); +struct bio *cur;struct bio *next; +/* +* We're not allowed to take any new bios to rbio->bio_list +* from now on, otherwise we may get merged with more bios and +* rbio->bio_list is not empty. +*/ +spin_lock (&rbio->bio_list_lock); +set_bit (RBIO_RMW_LOCKED_BIT , &rbio->flags); +spin_unlock (&rbio->bio_list_lock); +if (rbio->generic_bio_cnt)btrfs_bio_counter_sub (rbio->fs_info, rbio->generic_bio_cnt); + cur =bio_list_get (&rbio->bio_list); +free_raid_bio (rbio);while (cur) {Signature · 6 lines
2.9.4 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 24 lines · Liu Bo
We're not allowed to take any new bios to
rbio->bio_listinrbio_orig_end_io(), otherwise we may get merged with more bios andrbio->bio_listis not empty.This should only happens in error-out cases, the normal path of recover and full stripe write have already set
RBIO_RMW_LOCKED_BITto disable merge before doing IO.Reported-by Jérôme Carretero <[email protected]>Signed-off-by Liu Bo <[email protected]>--- fs/btrfs/raid56.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)fs/btrfs/raid56.c +1
-1@@ -859,12 +859,23 @@ static void free_raid_bio(struct btrfs_raid_bio *rbio)*/ static void rbio_orig_end_io (struct btrfs_raid_bio *rbio,blk_status_t err) { -struct bio *cur =bio_list_get (&rbio->bio_list); +struct bio *cur;
fs/btrfs/raid56.c static void free_raid_bio(struct btrfs_raid_bio *rbio) · line 872
Quoted 10 lines
fs/btrfs/raid56.c +8
-0struct bio *next; +/* +* We're not allowed to take any new bios to rbio->bio_list +* from now on, otherwise we may get merged with more bios and +* rbio->bio_list is not empty. +*/ +spin_lock (&rbio->bio_list_lock); +set_bit (RBIO_RMW_LOCKED_BIT , &rbio->flags); +spin_unlock (&rbio->bio_list_lock);
do we really need the spinlock, bit operations are atomic?
Quoted 10 lines
fs/btrfs/raid56.c +3
-0+if (rbio->generic_bio_cnt)btrfs_bio_counter_sub (rbio->fs_info, rbio->generic_bio_cnt); + cur =bio_list_get (&rbio->bio_list); +free_raid_bio (rbio);while (cur) {
Quoted 29 lines · Nikolay Borisov
Quoted 26 lines · Liu Bo
We're not allowed to take any new bios to
rbio->bio_listinrbio_orig_end_io(), otherwise we may get merged with more bios andrbio->bio_listis not empty.This should only happens in error-out cases, the normal path of recover and full stripe write have already set
RBIO_RMW_LOCKED_BITto disable merge before doing IO.Reported-by Jérôme Carretero <[email protected]>Signed-off-by Liu Bo <[email protected]>--- fs/btrfs/raid56.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)fs/btrfs/raid56.c +1
-1@@ -859,12 +859,23 @@ static void free_raid_bio(struct btrfs_raid_bio *rbio)*/ static void rbio_orig_end_io (struct btrfs_raid_bio *rbio,blk_status_t err) { -struct bio *cur =bio_list_get (&rbio->bio_list); +struct bio *cur;struct bio *next;
Quoted 11 lines
fs/btrfs/raid56.c static void free_raid_bio(struct btrfs_raid_bio *rbio) · line 872
Quoted 8 lines
fs/btrfs/raid56.c +8
-0+/* +* We're not allowed to take any new bios to rbio->bio_list +* from now on, otherwise we may get merged with more bios and +* rbio->bio_list is not empty. +*/ +spin_lock (&rbio->bio_list_lock); +set_bit (RBIO_RMW_LOCKED_BIT , &rbio->flags); +spin_unlock (&rbio->bio_list_lock);do we really need the spinlock, bit operations are atomic?
Thanks for the question.
Atomicity doesn't really matter here, set_bit() needs to be done in the critical section so that merge_rbio() can do right things if merge_rbio() comes after rbio_orig_end_io().
thanks,
-liubo
Quoted 10 lines
Quoted 10 lines
fs/btrfs/raid56.c +3
-0+if (rbio->generic_bio_cnt)btrfs_bio_counter_sub (rbio->fs_info, rbio->generic_bio_cnt); + cur =bio_list_get (&rbio->bio_list); +free_raid_bio (rbio);while (cur) {
Quoted 10 lines · Liu Bo
We're not allowed to take any new bios to
rbio->bio_listinrbio_orig_end_io(), otherwise we may get merged with more bios andrbio->bio_listis not empty.This should only happens in error-out cases, the normal path of recover and full stripe write have already set
RBIO_RMW_LOCKED_BITto disable merge before doing IO.Reported-by Jérôme Carretero <[email protected]>Signed-off-by Liu Bo <[email protected]>
Added to next, thanks.
PATCH v2 1/1 Btrfs: raid56: fix race between merge_bio and rbio_orig_end_io
In reply to Liu BoBefore rbio_orig_end_io() goes to free rbio, rbio may get merged with more bios from other rbios and rbio->bio_list becomes non-empty, in that case, these newly merged bios don't end properly.
Once unlock_stripe() is done, rbio->bio_list will not be updated any more and we can call bio_endio() on all queued bios.
It should only happen in error-out cases, the normal path of recover and full stripe write have already set RBIO_RMW_LOCKED_BIT to disable merge before doing IO, so rbio_orig_end_io() called by them doesn't have the above issue.
---
v2: - Remove the usage spin_lock as there is a chance of deadlock in
interrupt context, it's reported by lockdep, although it'd never happen because we've taken care of it by saving irq flags at all places.
- Update commit log and comments of code to explain the new idea.
- This has been tested against btrfs/011 for 50 times.
fs/btrfs/raid56.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-)
fs/btrfs/raid56.c +25 -12
@@ -864,10 +864,17 @@ static void __free_raid_bio(struct btrfs_raid_bio *rbio)kfree (rbio); } -static void free_raid_bio (struct btrfs_raid_bio *rbio) +static void rbio_endio_bio_list (struct bio *cur,blk_status_t err) { -unlock_stripe (rbio); -__free_raid_bio (rbio); +struct bio *next; + +while (cur) { + next = cur->bi_next; + cur->bi_next =NULL ; + cur->bi_status = err; +bio_endio (cur); + cur = next; + } }/*
@@ -877,20 +884,26 @@ static void free_raid_bio(struct btrfs_raid_bio *rbio)static void rbio_orig_end_io (struct btrfs_raid_bio *rbio,blk_status_t err) {struct bio *cur =bio_list_get (&rbio->bio_list); -struct bio *next; +struct bio *extra;if (rbio->generic_bio_cnt)btrfs_bio_counter_sub (rbio->fs_info, rbio->generic_bio_cnt); -free_raid_bio (rbio); +/* +* At this moment, rbio->bio_list is empty, however since rbio does not +* always have RBIO_RMW_LOCKED_BIT set and rbio is still linked on the +* hash list, rbio may be merged with others so that rbio->bio_list +* becomes non-empty. +* Once unlock_stripe() is done, rbio->bio_list will not be updated any +* more and we can call bio_endio() on all queued bios. +*/ +unlock_stripe (rbio); + extra =bio_list_get (&rbio->bio_list); +__free_raid_bio (rbio); -while (cur) { - next = cur->bi_next; - cur->bi_next =NULL ; - cur->bi_status = err; -bio_endio (cur); - cur = next; - } +rbio_endio_bio_list (cur, err); +if (extra) +rbio_endio_bio_list (extra, err); }/*
Signature · 1 line
2.9.4