bcache device failure handling fixes for 4.17-rc4
Subsystems (2)
v2 cover bcache device failure handling fixes for 4.17-rc4
Hi Jens,
I receive bug reports from partners for the bcache cache device failure handling patch set (which is just merged into 4.17-rc1). Fortunately we are still in 4.17 merge window, I suggest to have these fixes to go into 4.17 merge window too.
These patches have non peer reviewer so far, Tang Junhui is not able to be available temporarily, and no other active code review since last patch set post. In order to have these patches go into linux-4.17 in time, I have to send these patches only with my Signed-off-by.
The patches are well commented IMHO and pass my own regression tests. Thanks for your help in advance.
Coly Li
---
Coly Li (6):
bcache: store disk name in struct cache and struct cached_dev
bcache: set CACHE_SET_IO_DISABLE in bch_cached_dev_error()
bcache: count backing device I/O error for writeback I/O
bcache: add wait_for_kthread_stop() in bch_allocator_thread()
bcache: set dc->io_disable to true in conditional_stop_bcache_device()
bcache: use pr_info() to inform duplicated CACHE_SET_IO_DISABLE set
drivers/md/bcache/alloc.c | 5 ++- drivers/md/bcache/bcache.h | 4 +++ drivers/md/bcache/debug.c | 3 +- drivers/md/bcache/io.c | 8 ++--- drivers/md/bcache/request.c | 5 +-- drivers/md/bcache/super.c | 75 ++++++++++++++++++++++++++++++------------- drivers/md/bcache/writeback.c | 4 ++- 7 files changed, 68 insertions(+), 36 deletions(-)
Signature · 1 line
2.16.3
PATCH v2 1/6 bcache: store disk name in struct cache and struct cached_dev
Current code uses bdevname() or bio_devname() to reference gendisk disk name when bcache needs to display the disk names in kernel message. It was safe before bcache device failure handling patch set merged in, because when devices are failed, there was deadlock to prevent bcache printing error messages with gendisk disk name. But after the failure handling patch set merged, the deadlock is fixed, so it is possible that the gendisk structure bdev->hd_disk is released when bdevname() is called to reference bdev->bd_disk->disk_name[]. This is why I receive bug report of NULL pointers deference panic.
This patch stores gendisk disk name in a buffer inside struct cache and struct cached_dev, then print out the offline device name won't reference bdev->hd_disk anymore. And this patch also avoids extra function calls of bdevname() and bio_devnmae().
Changelog:
v2, call bdevname() earlier in register_bdev() v1, first version with segguestion from Junhui Tang.
--- drivers/md/bcache/bcache.h | 4 ++++ drivers/md/bcache/debug.c | 3 +-- drivers/md/bcache/io.c | 8 +++----- drivers/md/bcache/request.c | 5 +---- drivers/md/bcache/super.c | 44 +++++++++++++++++++++----------------------- 5 files changed, 30 insertions(+), 34 deletions(-)
drivers/md/bcache/bcache.h +4 -0
@@ -392,6 +392,8 @@ struct cached_dev {#define DEFAULT_CACHED_DEV_ERROR_LIMIT 64atomic_t io_errors;unsigned error_limit; + +char backing_dev_name[BDEVNAME_SIZE ]; };enum alloc_reserve {
@@ -464,6 +466,8 @@ struct cache {atomic_long_t meta_sectors_written;atomic_long_t btree_sectors_written;atomic_long_t sectors_written; + +char cache_dev_name[BDEVNAME_SIZE ]; };struct gc_stat {
drivers/md/bcache/debug.c +1 -2
@@ -106,7 +106,6 @@ void bch_btree_verify(struct btree *b)void bch_data_verify (struct cached_dev *dc,struct bio *bio) { -char name[BDEVNAME_SIZE ];struct bio *check;struct bio_vec bv, cbv;struct bvec_iter iter, citer = {0 };
@@ -134,7 +133,7 @@ void bch_data_verify(struct cached_dev *dc, struct bio *bio) bv.bv_len), dc->disk.c,"verify failed at dev %s sector %llu" , -bdevname (dc->bdev, name), + dc->backing_dev_name, (uint64_t) bio->bi_iter.bi_sector);kunmap_atomic (p1);
drivers/md/bcache/io.c +3 -5
@@ -52,7 +52,6 @@ void bch_submit_bbio(struct bio *bio, struct cache_set *c,/* IO errors */ void bch_count_backing_io_errors (struct cached_dev *dc,struct bio *bio) { -char buf[BDEVNAME_SIZE ];unsigned errors;WARN_ONCE (!dc,"NULL pointer of struct cached_dev" );
@@ -60,7 +59,7 @@ void bch_count_backing_io_errors(struct cached_dev *dc, struct bio *bio) errors =atomic_add_return (1 , &dc->io_errors);if (errors < dc->error_limit)pr_err ("%s: IO error on backing device, unrecoverable" , -bio_devname (bio, buf)); + dc->backing_dev_name);else bch_cached_dev_error (dc); }
@@ -105,19 +104,18 @@ void bch_count_io_errors(struct cache *ca, }if (error) { -char buf[BDEVNAME_SIZE ];unsigned errors =atomic_add_return (1 <<IO_ERROR_SHIFT , &ca->io_errors); errors >>=IO_ERROR_SHIFT ;if (errors < ca->set->error_limit)pr_err ("%s: IO error on %s%s" , -bdevname (ca->bdev, buf), m, + ca->cache_dev_name, m, is_read ?", recovering." :"." );else bch_cache_set_error (ca->set,"%s: too many IO errors %s" , -bdevname (ca->bdev, buf), m); + ca->cache_dev_name, m); } }
drivers/md/bcache/request.c +1 -4
@@ -649,11 +649,8 @@ static void backing_request_endio(struct bio *bio)*/ if (unlikely (s->iop.writeback && bio->bi_opf &REQ_PREFLUSH )) { -char buf[BDEVNAME_SIZE ]; - -bio_devname (bio, buf);pr_err ("Can't flush %s: returned bi_status %i" , - buf, bio->bi_status); + dc->backing_dev_name, bio->bi_status); }else {/* set to orig_bio->bi_status in bio_complete() */ s->iop.status = bio->bi_status;
drivers/md/bcache/super.c +21 -23
162 lines not on this page · raw message
Signature · 1 line
2.16.3
Re: [PATCH 1/6] bcache: store disk name in struct cache and struct cached_dev
Quoted 22 lines · Coly Li
Current code uses
bdevname()orbio_devname()to reference gendisk disk name when bcache needs to display the disk names in kernel message. It was safe before bcache device failure handling patch set merged in, because when devices are failed, there was deadlock to prevent bcache printing error messages with gendisk disk name. But after the failure handling patch set merged, the deadlock is fixed, so it is possible that the gendisk structurebdev->hd_diskis released whenbdevname()is called to reference bdev->bd_disk->disk_name[]. This is why I receive bug report of NULL pointers deference panic.This patch stores gendisk disk name in a buffer inside struct cache and struct
cached_dev, then print out the offline device name won't referencebdev->hd_diskanymore. And this patch also avoids extra function calls ofbdevname()andbio_devnmae().Changelog:
v2, callbdevname()earlier inregister_bdev()v1, first version with segguestion from Junhui Tang.Fixes c7b7bd07404c5 ("bcache: add io_disable to struct cached_dev")Fixes 5138ac6748e38 ("bcache: fix misleading error message in bch_count_io_errors()")Signed-off-by Coly Li <[email protected]>
Quoted 8 lines
--- drivers/md/bcache/bcache.h | 4 ++++ drivers/md/bcache/debug.c | 3 +-- drivers/md/bcache/io.c | 8 +++----- drivers/md/bcache/request.c | 5 +---- drivers/md/bcache/super.c | 44 +++++++++++++++++++++----------------------- 5 files changed, 30 insertions(+), 34 deletions(-)
Cheers,
Hannes
PATCH v2 2/6 bcache: set CACHE_SET_IO_DISABLE in bch_cached_dev_error()
In reply to Coly LiCommit c7b7bd07404c5 ("bcache: add io_disable to struct cached_dev") tries to stop bcache device by calling bcache_device_stop() when too many I/O errors happened on backing device. But if there is internal I/O happening on cache device (writeback scan, garbage collection, etc), a regular I/O request triggers the internal I/Os may still holds a refcount of dc->count, and the refcount may only be dropped after the internal I/O stopped.
By this patch, bch_cached_dev_error() will check if the backing device is attached to a cache set, if yes that CACHE_SET_IO_DISABLE will be set to flags of this cache set. Then internal I/Os on cache device will be rejected and stopped immediately, and the bcache device can be stopped.
For people who are not familiar with the interesting refcount dependance, let me explain a bit more how the fix works. Example the writeback thread will scan cache device for dirty data writeback purpose. Before it stopps, it holds a refcount of dc->count. When CACHE_SET_IO_DISABLE bit is set, the internal I/O will stopped and the while-loop in bch_writeback_thread() quits and calls cached_dev_put() to drop dc->count. If this is the last refcount to drop, then cached_dev_detach_finish() will be called. In this call back function, in turn closure_put(dc->disk.cl) is called to drop a refcount of closure dc->disk.cl. If this is the last refcount of this closure to drop, then cached_dev_flush() will be called. Then the cached device is freed. So if CACHE_SET_IO_DISABLE is not set, the bache device can not be stopped until all inernal cache device I/O stopped. For large size cache device, and writeback thread competes locks with gc thread, there might be a quite long time to wait.
--- drivers/md/bcache/super.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/md/bcache/super.c +17 -0
@@ -1369,6 +1369,8 @@ int bch_flash_dev_create(struct cache_set *c, uint64_t size)bool bch_cached_dev_error (struct cached_dev *dc) { +struct cache_set *c; +if (!dc ||test_bit (BCACHE_DEV_CLOSING , &dc->disk.flags))return false;
@@ -1379,6 +1381,21 @@ bool bch_cached_dev_error(struct cached_dev *dc)pr_err ("stop %s: too many IO errors on backing device %s\n" , dc->disk.disk->disk_name, dc->backing_dev_name); +/* +* If the cached device is still attached to a cache set, +* even dc->io_disable is true and no more I/O requests +* accepted, cache device internal I/O (writeback scan or +* garbage collection) may still prevent bcache device from +* being stopped. So here CACHE_SET_IO_DISABLE should be +* set to c->flags too, to make the internal I/O to cache +* device rejected and stopped immediately. +* If c is NULL, that means the bcache device is not attached +* to any cache set, then no CACHE_SET_IO_DISABLE bit to set. +*/ + c = dc->disk.c; +if (c &&test_and_set_bit (CACHE_SET_IO_DISABLE , &c->flags)) +pr_warn ("CACHE_SET_IO_DISABLE already set" ); +bcache_device_stop (&dc->disk);return true; }
Signature · 1 line
2.16.3
Re: [PATCH 2/6] bcache: set CACHE_SET_IO_DISABLE in bch_cached_dev_error()
Quoted 59 lines · Coly Li
Commit
c7b7bd07404c5("bcache: addio_disableto structcached_dev") tries to stop bcache device by callingbcache_device_stop()when too many I/O errors happened on backing device. But if there is internal I/O happening on cache device (writeback scan, garbage collection, etc), a regular I/O request triggers the internal I/Os may still holds a refcount ofdc->count, and the refcount may only be dropped after the internal I/O stopped.By this patch,
bch_cached_dev_error()will check if the backing device is attached to a cache set, if yes thatCACHE_SET_IO_DISABLEwill be set to flags of this cache set. Then internal I/Os on cache device will be rejected and stopped immediately, and the bcache device can be stopped.For people who are not familiar with the interesting refcount dependance, let me explain a bit more how the fix works. Example the writeback thread will scan cache device for dirty data writeback purpose. Before it stopps, it holds a refcount of
dc->count. WhenCACHE_SET_IO_DISABLEbit is set, the internal I/O will stopped and the while-loop inbch_writeback_thread()quits and callscached_dev_put()to dropdc->count. If this is the last refcount to drop, thencached_dev_detach_finish()will be called. In this call back function, in turnclosure_put(dc->disk.cl)is called to drop a refcount of closuredc->disk.cl. If this is the last refcount of this closure to drop, thencached_dev_flush()will be called. Then the cached device is freed. So ifCACHE_SET_IO_DISABLEis not set, the bache device can not be stopped until all inernal cache device I/O stopped. For large size cache device, and writeback thread competes locks with gc thread, there might be a quite long time to wait.Fixes c7b7bd07404c5 ("bcache: add io_disable to struct cached_dev")Signed-off-by Coly Li <[email protected]>--- drivers/md/bcache/super.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/md/bcache/super.c +10
-0@@ -1369,6 +1369,8 @@ int bch_flash_dev_create(struct cache_set *c, uint64_t size)bool bch_cached_dev_error (struct cached_dev *dc) { +struct cache_set *c; +if (!dc ||test_bit (BCACHE_DEV_CLOSING , &dc->disk.flags))return false;@@ -1379,6 +1381,21 @@ bool bch_cached_dev_error(struct cached_dev *dc)pr_err ("stop %s: too many IO errors on backing device %s\n" , dc->disk.disk->disk_name, dc->backing_dev_name); +/* +* If the cached device is still attached to a cache set, +* even dc->io_disable is true and no more I/O requests +* accepted, cache device internal I/O (writeback scan or +* garbage collection) may still prevent bcache device from +* being stopped. So here CACHE_SET_IO_DISABLE should be +* set to c->flags too, to make the internal I/O to cache +* device rejected and stopped immediately.
drivers/md/bcache/super.c bool bch_cached_dev_error(struct cached_dev *dc) · line 1401
Quoted 10 lines
drivers/md/bcache/super.c +7
-0+* If c is NULL, that means the bcache device is not attached +* to any cache set, then no CACHE_SET_IO_DISABLE bit to set. +*/ + c = dc->disk.c; +if (c &&test_and_set_bit (CACHE_SET_IO_DISABLE , &c->flags)) +pr_warn ("CACHE_SET_IO_DISABLE already set" ); +bcache_device_stop (&dc->disk);return true; }
Neat.
Cheers,
Hannes
PATCH v2 3/6 bcache: count backing device I/O error for writeback I/O
In reply to Coly LiCommit c7b7bd07404c5 ("bcache: add io_disable to struct cached_dev") counts backing device I/O requets and set dc->io_disable to true if error counters exceeds dc->io_error_limit. But it only counts I/O errors for regular I/O request, neglects errors of write back I/Os when backing device is offline.
This patch counts the errors of writeback I/Os, in dirty_endio() if bio->bi_status is not 0, it means error happens when writing dirty keys to backing device, then bch_count_backing_io_errors() is called.
By this fix, even there is no reqular I/O request coming, if writeback I/O errors exceed dc->io_error_limit, the bcache device may still be stopped for the broken backing device.
--- drivers/md/bcache/writeback.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/bcache/writeback.c +3 -1
@@ -244,8 +244,10 @@ static void dirty_endio(struct bio *bio)struct keybuf_key *w = bio->bi_private;struct dirty_io *io = w->private; -if (bio->bi_status) +if (bio->bi_status) {SET_KEY_DIRTY (&w->key, false); +bch_count_backing_io_errors (io->dc, bio); + }closure_put (&io->cl); }
Signature · 1 line
2.16.3
Re: [PATCH 3/6] bcache: count backing device I/O error for writeback I/O
Quoted 27 lines · Coly Li
Commit
c7b7bd07404c5("bcache: addio_disableto structcached_dev") counts backing device I/O requets and setdc->io_disableto true if error counters exceedsdc->io_error_limit. But it only counts I/O errors for regular I/O request, neglects errors of write back I/Os when backing device is offline.This patch counts the errors of writeback I/Os, in
dirty_endio()ifbio->bi_statusis not 0, it means error happens when writing dirty keys to backing device, thenbch_count_backing_io_errors()is called.By this fix, even there is no reqular I/O request coming, if writeback I/O errors exceed
dc->io_error_limit, the bcache device may still be stopped for the broken backing device.Fixes c7b7bd07404c5 ("bcache: add io_disable to struct cached_dev")Signed-off-by Coly Li <[email protected]>--- drivers/md/bcache/writeback.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/md/bcache/writeback.c +0
-0@@ -244,8 +244,10 @@ static void dirty_endio(struct bio *bio)struct keybuf_key *w = bio->bi_private;
drivers/md/bcache/writeback.c static void dirty_endio(struct bio *bio) · line 253
Quoted 10 lines
drivers/md/bcache/writeback.c +3
-1struct dirty_io *io = w->private; -if (bio->bi_status) +if (bio->bi_status) {SET_KEY_DIRTY (&w->key, false); +bch_count_backing_io_errors (io->dc, bio); + }closure_put (&io->cl); }
Cheers,
Hannes
PATCH v2 4/6 bcache: add wait_for_kthread_stop() in bch_allocator_thread()
In reply to Coly LiWhen CACHE_SET_IO_DISABLE is set on cache set flags, bcache allocator thread routine bch_allocator_thread() may stop the while-loops and exit. Then it is possible to observe the following kernel oops message,
[ 631.068366] bcache: bch_btree_insert() error -5 [ 631.069115] bcache: cached_dev_detach_finish() Caching disabled for sdf [ 631.070220] BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 [ 631.070250] PGD 0 P4D 0 [ 631.070261] Oops: 0002 [#1] SMP PTI [snipped] [ 631.070578] Workqueue: events cache_set_flush [bcache] [ 631.070597] RIP: 0010:exit_creds+0x1b/0x50 [ 631.070610] RSP: 0018:ffffc9000705fe08 EFLAGS: 00010246 [ 631.070626] RAX: 0000000000000001 RBX: ffff880a622ad300 RCX: 000000000000000b [ 631.070645] RDX: 0000000000000601 RSI: 000000000000000c RDI: 0000000000000000 [ 631.070663] RBP: ffff880a622ad300 R08: ffffea00190c66e0 R09: 0000000000000200 [ 631.070682] R10: ffff880a48123000 R11: ffff880000000000 R12: 0000000000000000 [ 631.070700] R13: ffff880a4b160e40 R14: ffff880a4b160000 R15: 0ffff880667e2530 [ 631.070719] FS: 0000000000000000(0000) GS:ffff880667e00000(0000) knlGS:0000000000000000 [ 631.070740] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 631.070755] CR2: 0000000000000000 CR3: 000000000200a001 CR4: 00000000003606e0 [ 631.070774] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 631.070793] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 631.070811] Call Trace: [ 631.070828] __put_task_struct+0x55/0x160 [ 631.070845] kthread_stop+0xee/0x100 [ 631.070863] cache_set_flush+0x11d/0x1a0 [bcache] [ 631.070879] process_one_work+0x146/0x340 [ 631.070892] worker_thread+0x47/0x3e0 [ 631.070906] kthread+0xf5/0x130 [ 631.070917] ? max_active_store+0x60/0x60 [ 631.070930] ? kthread_bind+0x10/0x10 [ 631.070945] ret_from_fork+0x35/0x40 [snipped] [ 631.071017] RIP: exit_creds+0x1b/0x50 RSP: ffffc9000705fe08 [ 631.071033] CR2: 0000000000000000 [ 631.071045] ---[ end trace 011c63a24b22c927 ]--- [ 631.071085] bcache: bcache_device_free() bcache0 stopped
The reason is when cache_set_flush() tries to call kthread_stop() to stop allocator thread, but it exits already due to cache device I/O errors.
This patch adds wait_for_kthread_stop() at tail of bch_allocator_thread(), to prevent the thread routine exiting directly. Then the allocator thread can be blocked at wait_for_kthread_stop() and wait for cache_set_flush() to stop it by calling kthread_stop().
changelog:
v2: not directly return from allocator_wait(), move 'return 0' to tail of
bch_allocator_thread().
v1: initial version.
--- drivers/md/bcache/alloc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/md/bcache/alloc.c +4 -1
@@ -290,7 +290,7 @@ do { \if (kthread_should_stop () || \test_bit (CACHE_SET_IO_DISABLE , &ca->set->flags)) { \set_current_state (TASK_RUNNING ); \ -return 0 ; \ + goto out; \ } \ \schedule (); \
@@ -378,6 +378,9 @@ static int bch_allocator_thread(void *arg)bch_prio_write (ca); } } +out: +wait_for_kthread_stop (); +return 0 ; }/* Allocation */
Signature · 1 line
2.16.3
Re: [PATCH 4/6] bcache: add wait_for_kthread_stop() in bch_allocator_thread()
Quoted 73 lines · Coly Li
When
CACHE_SET_IO_DISABLEis set on cache set flags, bcache allocator thread routinebch_allocator_thread()may stop the while-loops and exit. Then it is possible to observe the following kernel oops message,[ 631.068366] bcache: bch_btree_insert() error -5 [ 631.069115] bcache: cached_dev_detach_finish() Caching disabled for sdf [ 631.070220] BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 [ 631.070250] PGD 0 P4D 0 [ 631.070261] Oops: 0002 [#1] SMP PTI [snipped] [ 631.070578] Workqueue: events cache_set_flush [bcache] [ 631.070597] RIP: 0010:exit_creds+0x1b/0x50 [ 631.070610] RSP: 0018:ffffc9000705fe08 EFLAGS: 00010246 [ 631.070626] RAX: 0000000000000001 RBX: ffff880a622ad300 RCX: 000000000000000b [ 631.070645] RDX: 0000000000000601 RSI: 000000000000000c RDI: 0000000000000000 [ 631.070663] RBP: ffff880a622ad300 R08: ffffea00190c66e0 R09: 0000000000000200 [ 631.070682] R10: ffff880a48123000 R11: ffff880000000000 R12: 0000000000000000 [ 631.070700] R13: ffff880a4b160e40 R14: ffff880a4b160000 R15: 0ffff880667e2530 [ 631.070719] FS: 0000000000000000(0000) GS:ffff880667e00000(0000) knlGS:0000000000000000 [ 631.070740] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 631.070755] CR2: 0000000000000000 CR3: 000000000200a001 CR4: 00000000003606e0 [ 631.070774] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 631.070793] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 631.070811] Call Trace: [ 631.070828] __put_task_struct+0x55/0x160 [ 631.070845] kthread_stop+0xee/0x100 [ 631.070863] cache_set_flush+0x11d/0x1a0 [bcache] [ 631.070879] process_one_work+0x146/0x340 [ 631.070892] worker_thread+0x47/0x3e0 [ 631.070906] kthread+0xf5/0x130 [ 631.070917] ? max_active_store+0x60/0x60 [ 631.070930] ? kthread_bind+0x10/0x10 [ 631.070945] ret_from_fork+0x35/0x40 [snipped] [ 631.071017] RIP: exit_creds+0x1b/0x50 RSP: ffffc9000705fe08 [ 631.071033] CR2: 0000000000000000 [ 631.071045] ---[ end trace 011c63a24b22c927 ]--- [ 631.071085] bcache: bcache_device_free() bcache0 stoppedThe reason is when
cache_set_flush()tries to callkthread_stop()to stop allocator thread, but it exits already due to cache device I/O errors.This patch adds
wait_for_kthread_stop()at tail ofbch_allocator_thread(), to prevent the thread routine exiting directly. Then the allocator thread can be blocked atwait_for_kthread_stop()and wait forcache_set_flush()to stop it by callingkthread_stop().changelog:
v2: not directly return fromallocator_wait(), move 'return 0' to tail of
bch_allocator_thread().
v1: initial version.Fixes 771f393e8ffc ("bcache: add CACHE_SET_IO_DISABLE to struct cache_set flags")Signed-off-by Coly Li <[email protected]>--- drivers/md/bcache/alloc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/md/bcache/alloc.c +1
-1@@ -290,7 +290,7 @@ do { \if (kthread_should_stop () || \test_bit (CACHE_SET_IO_DISABLE , &ca->set->flags)) { \set_current_state (TASK_RUNNING ); \ -return 0 ; \ + goto out; \ } \ \schedule (); \
drivers/md/bcache/alloc.c static int bch_allocator_thread(void *arg) · line 386
Quoted 9 lines
drivers/md/bcache/alloc.c +3
-0@@ -378,6 +378,9 @@ static int bch_allocator_thread(void *arg)bch_prio_write (ca); } } +out: +wait_for_kthread_stop (); +return 0 ; }/* Allocation */
Cheers,
Hannes
PATCH v2 5/6 bcache: set dc->io_disable to true in conditional_stop_bcache_device()
In reply to Coly LiCommit 7e027ca4b534b ("bcache: add stop_when_cache_set_failed option to backing device") adds stop_when_cache_set_failed option and stops bcache device if stop_when_cache_set_failed is auto and there is dirty data on broken cache device. There might exists a small time gap that the cache set is released and set to NULL but bcache device is not released yet (because they are released in parallel). During this time gap, dc->c is NULL so CACHE_SET_IO_DISABLE won't be checked, and dc->io_disable is still false, so new coming I/O requests will be accepted and directly go into backing device as no cache set attached to. If there is dirty data on cache device, this behavior may introduce potential inconsistent data.
This patch sets dc->io_disable to true before calling bcache_device_stop() to make sure the backing device will reject new coming I/O request as well, so even in the small time gap no I/O will directly go into backing device to corrupt data consistency.
--- drivers/md/bcache/super.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/md/bcache/super.c +14 -0
@@ -1556,6 +1556,20 @@ static void conditional_stop_bcache_device(struct cache_set *c,*/ pr_warn ("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption." , d->disk->disk_name); +/* +* There might be a small time gap that cache set is +* released but bcache device is not. Inside this time +* gap, regular I/O requests will directly go into +* backing device as no cache set attached to. This +* behavior may also introduce potential inconsistence +* data in writeback mode while cache is dirty. +* Therefore before calling bcache_device_stop() due +* to a broken cache device, dc->io_disable should be +* explicitly set to true. +*/ + dc->io_disable = true; +/* make others know io_disable is true earlier */ +smp_mb ();bcache_device_stop (d); }else {/*
Signature · 1 line
2.16.3
Re: [PATCH 5/6] bcache: set dc->io_disable to true in conditional_stop_bcache_device()
Quoted 38 lines · Coly Li
Commit
7e027ca4b534b("bcache: addstop_when_cache_set_failedoption to backing device") addsstop_when_cache_set_failedoption and stops bcache device ifstop_when_cache_set_failedis auto and there is dirty data on broken cache device. There might exists a small time gap that the cache set is released and set to NULL but bcache device is not released yet (because they are released in parallel). During this time gap,dc->cis NULL soCACHE_SET_IO_DISABLEwon't be checked, anddc->io_disableis still false, so new coming I/O requests will be accepted and directly go into backing device as no cache set attached to. If there is dirty data on cache device, this behavior may introduce potential inconsistent data.This patch sets
dc->io_disableto true before callingbcache_device_stop()to make sure the backing device will reject new coming I/O request as well, so even in the small time gap no I/O will directly go into backing device to corrupt data consistency.Fixes 7e027ca4b534b ("bcache: add stop_when_cache_set_failed option to backing device")Signed-off-by Coly Li <[email protected]>--- drivers/md/bcache/super.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/md/bcache/super.c +7
-0@@ -1556,6 +1556,20 @@ static void conditional_stop_bcache_device(struct cache_set *c,*/ pr_warn ("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption." , d->disk->disk_name); +/* +* There might be a small time gap that cache set is +* released but bcache device is not. Inside this time +* gap, regular I/O requests will directly go into +* backing device as no cache set attached to. This +* behavior may also introduce potential inconsistence +* data in writeback mode while cache is dirty.
drivers/md/bcache/super.c static void conditional_stop_bcache_device(struct cache_set *c, · line 1575
Quoted 10 lines
drivers/md/bcache/super.c +7
-0+* Therefore before calling bcache_device_stop() due +* to a broken cache device, dc->io_disable should be +* explicitly set to true. +*/ + dc->io_disable = true; +/* make others know io_disable is true earlier */ +smp_mb ();bcache_device_stop (d); }else {/*
Cheers,
Hannes
PATCH v2 6/6 bcache: use pr_info() to inform duplicated CACHE_SET_IO_DISABLE set
In reply to Coly LiIt is possible that multiple I/O requests hits on failed cache device or backing device, therefore it is quite common that CACHE_SET_IO_DISABLE is set already when a task tries to set the bit from bch_cached_dev_error() or bch_cache_set_error(). Currently the message "CACHE_SET_IO_DISABLE already set" is printed by pr_warn(), which might mislead users to think a serious fault happens in source code.
This patch uses pr_info() to print the information in such situations, avoid extra worries. This information is helpful to understand bcache behavior in cache device failures, so I still keep them in source code.
--- drivers/md/bcache/super.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/md/bcache/super.c +2 -2
@@ -1394,7 +1394,7 @@ bool bch_cached_dev_error(struct cached_dev *dc)*/ c = dc->disk.c;if (c &&test_and_set_bit (CACHE_SET_IO_DISABLE , &c->flags)) -pr_warn ("CACHE_SET_IO_DISABLE already set" ); +pr_info ("CACHE_SET_IO_DISABLE already set" );bcache_device_stop (&dc->disk);return true;
@@ -1412,7 +1412,7 @@ bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)return false;if (test_and_set_bit (CACHE_SET_IO_DISABLE , &c->flags)) -pr_warn ("CACHE_SET_IO_DISABLE already set" ); +pr_info ("CACHE_SET_IO_DISABLE already set" );/* XXX: we can be called from atomic context acquire_console_sem();
Signature · 1 line
2.16.3
Re: [PATCH 6/6] bcache: use pr_info() to inform duplicated CACHE_SET_IO_DISABLE set
Quoted 11 lines · Coly Li
It is possible that multiple I/O requests hits on failed cache device or backing device, therefore it is quite common that
CACHE_SET_IO_DISABLEis set already when a task tries to set the bit frombch_cached_dev_error()orbch_cache_set_error(). Currently the message "CACHE_SET_IO_DISABLEalready set" is printed bypr_warn(), which might mislead users to think a serious fault happens in source code.This patch uses
pr_info()to print the information in such situations, avoid extra worries. This information is helpful to understand bcache behavior in cache device failures, so I still keep them in source code.
Hi Jens,
Quoted 1 line
Fixes a705642fd4ded ("bcache: set CACHE_SET_IO_DISABLE in bch_cached_dev_error()")
I just realize this is not upstream yet, and only in my private tree. so I need to modify this patch in following way.
Quoted 11 lines
Fixes 771f393e8ffc9 ("bcache: add CACHE_SET_IO_DISABLE to struct cache_set flags")Signed-off-by Coly Li <[email protected]>--- drivers/md/bcache/super.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/md/bcache/super.c +0
-0
drivers/md/bcache/super.c bool bch_cached_dev_error(struct cached_dev *dc) · line 1398
Quoted 6 lines
drivers/md/bcache/super.c +1
-1@@ -1394,7 +1394,7 @@ bool bch_cached_dev_error(struct cached_dev *dc)*/ c = dc->disk.c;if (c &&test_and_set_bit (CACHE_SET_IO_DISABLE , &c->flags)) -pr_warn ("CACHE_SET_IO_DISABLE already set" ); +pr_info ("CACHE_SET_IO_DISABLE already set" );
This part will go directly into patch "bcache: set CACHE_SET_IO_DISABLE in bch_cached_dev_error()".
drivers/md/bcache/super.c bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...) · line 1418
Quoted 12 lines
drivers/md/bcache/super.c +1
-1bcache_device_stop (&dc->disk);return true;@@ -1412,7 +1412,7 @@ bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)return false;if (test_and_set_bit (CACHE_SET_IO_DISABLE , &c->flags)) -pr_warn ("CACHE_SET_IO_DISABLE already set" ); +pr_info ("CACHE_SET_IO_DISABLE already set" );/* XXX: we can be called from atomic context acquire_console_sem();
And only this part should be in this patch.
I will send out a v3 patch set very soon.
Sorry for the noise.
Coly Li
Re: [PATCH 6/6] bcache: use pr_info() to inform duplicated CACHE_SET_IO_DISABLE set
In reply to Coly LiQuoted 33 lines · Coly Li
It is possible that multiple I/O requests hits on failed cache device or backing device, therefore it is quite common that
CACHE_SET_IO_DISABLEis set already when a task tries to set the bit frombch_cached_dev_error()orbch_cache_set_error(). Currently the message "CACHE_SET_IO_DISABLEalready set" is printed bypr_warn(), which might mislead users to think a serious fault happens in source code.This patch uses
pr_info()to print the information in such situations, avoid extra worries. This information is helpful to understand bcache behavior in cache device failures, so I still keep them in source code.Fixes a705642fd4ded ("bcache: set CACHE_SET_IO_DISABLE in bch_cached_dev_error()")Fixes 771f393e8ffc9 ("bcache: add CACHE_SET_IO_DISABLE to struct cache_set flags")Signed-off-by Coly Li <[email protected]>--- drivers/md/bcache/super.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/md/bcache/super.c +1
-1@@ -1394,7 +1394,7 @@ bool bch_cached_dev_error(struct cached_dev *dc)*/ c = dc->disk.c;if (c &&test_and_set_bit (CACHE_SET_IO_DISABLE , &c->flags)) -pr_warn ("CACHE_SET_IO_DISABLE already set" ); +pr_info ("CACHE_SET_IO_DISABLE already set" );bcache_device_stop (&dc->disk);return true;
drivers/md/bcache/super.c bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...) · line 1418
Quoted 8 lines
drivers/md/bcache/super.c +1
-1@@ -1412,7 +1412,7 @@ bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)return false;if (test_and_set_bit (CACHE_SET_IO_DISABLE , &c->flags)) -pr_warn ("CACHE_SET_IO_DISABLE already set" ); +pr_info ("CACHE_SET_IO_DISABLE already set" );/* XXX: we can be called from atomic context acquire_console_sem();
Cheers,
Hannes