diff options
author | Qu Wenruo <quwenruo@cn.fujitsu.com> | 2017-03-28 21:33:21 -0400 |
---|---|---|
committer | David Sterba <dsterba@suse.com> | 2017-04-18 08:07:26 -0400 |
commit | ae6529c35bcc1c65c12131cef2aea63d8e2ea950 (patch) | |
tree | c1d613fb1076079758db18ac81f15a09d359d184 | |
parent | 9a33944bdf075ca93062cde206cb25e62044890e (diff) |
btrfs: Wait for in-flight bios before freeing target device for raid56
When raid56 dev-replace is cancelled by running scrub, we will free
target device without waiting for in-flight bios, causing the following
NULL pointer deference or general protection failure.
BUG: unable to handle kernel NULL pointer dereference at 00000000000005e0
IP: generic_make_request_checks+0x4d/0x610
CPU: 1 PID: 11676 Comm: kworker/u4:14 Tainted: G O 4.11.0-rc2 #72
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.10.2-20170228_101828-anatol 04/01/2014
Workqueue: btrfs-endio-raid56 btrfs_endio_raid56_helper [btrfs]
task: ffff88002875b4c0 task.stack: ffffc90001334000
RIP: 0010:generic_make_request_checks+0x4d/0x610
Call Trace:
? generic_make_request+0xc7/0x360
generic_make_request+0x24/0x360
? generic_make_request+0xc7/0x360
submit_bio+0x64/0x120
? page_in_rbio+0x4d/0x80 [btrfs]
? rbio_orig_end_io+0x80/0x80 [btrfs]
finish_rmw+0x3f4/0x540 [btrfs]
validate_rbio_for_rmw+0x36/0x40 [btrfs]
raid_rmw_end_io+0x7a/0x90 [btrfs]
bio_endio+0x56/0x60
end_workqueue_fn+0x3c/0x40 [btrfs]
btrfs_scrubparity_helper+0xef/0x620 [btrfs]
btrfs_endio_raid56_helper+0xe/0x10 [btrfs]
process_one_work+0x2af/0x720
? process_one_work+0x22b/0x720
worker_thread+0x4b/0x4f0
kthread+0x10f/0x150
? process_one_work+0x720/0x720
? kthread_create_on_node+0x40/0x40
ret_from_fork+0x2e/0x40
RIP: generic_make_request_checks+0x4d/0x610 RSP: ffffc90001337bb8
In btrfs_dev_replace_finishing(), we will call
btrfs_rm_dev_replace_blocked() to wait bios before destroying the target
device when scrub is finished normally.
However when dev-replace is aborted, either due to error or cancelled by
scrub, we didn't wait for bios, this can lead to use-after-free if there
are bios holding the target device.
Furthermore, for raid56 scrub, at least 2 places are calling
btrfs_map_sblock() without protection of bio_counter, leading to the
problem.
This patch fixes the problem:
1) Wait for bio_counter before freeing target device when canceling
replace
2) When calling btrfs_map_sblock() for raid56, use bio_counter to
protect the call.
Cc: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Reviewed-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r-- | fs/btrfs/dev-replace.c | 2 | ||||
-rw-r--r-- | fs/btrfs/raid56.c | 14 | ||||
-rw-r--r-- | fs/btrfs/scrub.c | 5 |
3 files changed, 21 insertions, 0 deletions
diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c index de7b2c897fe0..32b2ffbb5717 100644 --- a/fs/btrfs/dev-replace.c +++ b/fs/btrfs/dev-replace.c | |||
@@ -546,8 +546,10 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, | |||
546 | mutex_unlock(&fs_info->chunk_mutex); | 546 | mutex_unlock(&fs_info->chunk_mutex); |
547 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | 547 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
548 | mutex_unlock(&uuid_mutex); | 548 | mutex_unlock(&uuid_mutex); |
549 | btrfs_rm_dev_replace_blocked(fs_info); | ||
549 | if (tgt_device) | 550 | if (tgt_device) |
550 | btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device); | 551 | btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device); |
552 | btrfs_rm_dev_replace_unblocked(fs_info); | ||
551 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); | 553 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
552 | 554 | ||
553 | return scrub_ret; | 555 | return scrub_ret; |
diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c index 7bf65d9a9bd3..d8ea0eb76325 100644 --- a/fs/btrfs/raid56.c +++ b/fs/btrfs/raid56.c | |||
@@ -2198,6 +2198,8 @@ static void read_rebuild_work(struct btrfs_work *work) | |||
2198 | /* | 2198 | /* |
2199 | * The following code is used to scrub/replace the parity stripe | 2199 | * The following code is used to scrub/replace the parity stripe |
2200 | * | 2200 | * |
2201 | * Caller must have already increased bio_counter for getting @bbio. | ||
2202 | * | ||
2201 | * Note: We need make sure all the pages that add into the scrub/replace | 2203 | * Note: We need make sure all the pages that add into the scrub/replace |
2202 | * raid bio are correct and not be changed during the scrub/replace. That | 2204 | * raid bio are correct and not be changed during the scrub/replace. That |
2203 | * is those pages just hold metadata or file data with checksum. | 2205 | * is those pages just hold metadata or file data with checksum. |
@@ -2235,6 +2237,12 @@ raid56_parity_alloc_scrub_rbio(struct btrfs_fs_info *fs_info, struct bio *bio, | |||
2235 | ASSERT(rbio->stripe_npages == stripe_nsectors); | 2237 | ASSERT(rbio->stripe_npages == stripe_nsectors); |
2236 | bitmap_copy(rbio->dbitmap, dbitmap, stripe_nsectors); | 2238 | bitmap_copy(rbio->dbitmap, dbitmap, stripe_nsectors); |
2237 | 2239 | ||
2240 | /* | ||
2241 | * We have already increased bio_counter when getting bbio, record it | ||
2242 | * so we can free it at rbio_orig_end_io(). | ||
2243 | */ | ||
2244 | rbio->generic_bio_cnt = 1; | ||
2245 | |||
2238 | return rbio; | 2246 | return rbio; |
2239 | } | 2247 | } |
2240 | 2248 | ||
@@ -2677,6 +2685,12 @@ raid56_alloc_missing_rbio(struct btrfs_fs_info *fs_info, struct bio *bio, | |||
2677 | return NULL; | 2685 | return NULL; |
2678 | } | 2686 | } |
2679 | 2687 | ||
2688 | /* | ||
2689 | * When we get bbio, we have already increased bio_counter, record it | ||
2690 | * so we can free it at rbio_orig_end_io() | ||
2691 | */ | ||
2692 | rbio->generic_bio_cnt = 1; | ||
2693 | |||
2680 | return rbio; | 2694 | return rbio; |
2681 | } | 2695 | } |
2682 | 2696 | ||
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c index 26dbe563fed0..e9612016325f 100644 --- a/fs/btrfs/scrub.c +++ b/fs/btrfs/scrub.c | |||
@@ -2191,6 +2191,7 @@ static void scrub_missing_raid56_pages(struct scrub_block *sblock) | |||
2191 | int ret; | 2191 | int ret; |
2192 | int i; | 2192 | int i; |
2193 | 2193 | ||
2194 | btrfs_bio_counter_inc_blocked(fs_info); | ||
2194 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical, | 2195 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical, |
2195 | &length, &bbio); | 2196 | &length, &bbio); |
2196 | if (ret || !bbio || !bbio->raid_map) | 2197 | if (ret || !bbio || !bbio->raid_map) |
@@ -2235,6 +2236,7 @@ static void scrub_missing_raid56_pages(struct scrub_block *sblock) | |||
2235 | rbio_out: | 2236 | rbio_out: |
2236 | bio_put(bio); | 2237 | bio_put(bio); |
2237 | bbio_out: | 2238 | bbio_out: |
2239 | btrfs_bio_counter_dec(fs_info); | ||
2238 | btrfs_put_bbio(bbio); | 2240 | btrfs_put_bbio(bbio); |
2239 | spin_lock(&sctx->stat_lock); | 2241 | spin_lock(&sctx->stat_lock); |
2240 | sctx->stat.malloc_errors++; | 2242 | sctx->stat.malloc_errors++; |
@@ -2778,6 +2780,8 @@ static void scrub_parity_check_and_repair(struct scrub_parity *sparity) | |||
2778 | goto out; | 2780 | goto out; |
2779 | 2781 | ||
2780 | length = sparity->logic_end - sparity->logic_start; | 2782 | length = sparity->logic_end - sparity->logic_start; |
2783 | |||
2784 | btrfs_bio_counter_inc_blocked(fs_info); | ||
2781 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start, | 2785 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start, |
2782 | &length, &bbio); | 2786 | &length, &bbio); |
2783 | if (ret || !bbio || !bbio->raid_map) | 2787 | if (ret || !bbio || !bbio->raid_map) |
@@ -2805,6 +2809,7 @@ static void scrub_parity_check_and_repair(struct scrub_parity *sparity) | |||
2805 | rbio_out: | 2809 | rbio_out: |
2806 | bio_put(bio); | 2810 | bio_put(bio); |
2807 | bbio_out: | 2811 | bbio_out: |
2812 | btrfs_bio_counter_dec(fs_info); | ||
2808 | btrfs_put_bbio(bbio); | 2813 | btrfs_put_bbio(bbio); |
2809 | bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap, | 2814 | bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap, |
2810 | sparity->nsectors); | 2815 | sparity->nsectors); |