block: Fix block device shutdown related races
Subsystems (7)
Jan Kara
maintainerv5 cover block: Fix block device shutdown related races
Hello,
this is a series with the remaining patches (on top of 4.11-rc2) to fix several different races and issues I've found when testing device shutdown and reuse. The first patch fixes possible (theoretical) problems when opening of a block device races with shutdown of a gendisk structure. Patches 2-8 fix oops that is triggered by __blkdev_put() calling inode_detach_wb() too early (the problem reported by Thiago). Patches 9 and 10 fix oops due to a bug in gendisk code where get_gendisk() can return already freed gendisk structure (again triggered by Omar's stress test).
All patches got reviewed by Tejun and also tested by Thiago (thanks!). Jens, can you please queue these fixes for the next merge window? Thanks!
Changes since v4:
* Dropped patch fixing possible gendisk shutdown vs blkdev_open race - needs more thinking
* Added Tejun's ack
Changes since v3:
* Rebased on top of 4.11-rc2
* Reworked patch 2 (block: Fix race of bdev open with gendisk shutdown) based on Tejun's feedback
* Significantly updated patch 5 (and dropped previous Tejun's ack) to accommodate for fixes to SCSI re-registration of BDI that went to 4.11-rc2
Changes since v2:
* Added Tejun's acks
* Rebased on top of 4.11-rc1
* Fixed two possible races between blkdev_open() and del_gendisk()
* Fixed possible race between concurrent shutdown of cgwb spotted by Tejun
Changes since v1:
* Added Acks and Tested-by tags for patches in areas that did not change
* Reworked inode_detach_wb() related fixes based on Tejun's feedback
Honza
Jan Kara
maintainerPATCH v5 1/10 block: Fix bdi assignment to bdev inode when racing with disk delete
When disk->fops->open() in __blkdev_get() returns -ERESTARTSYS, we restart the process of opening the block device. However we forget to switch bdev->bd_bdi back to noop_backing_dev_info and as a result bdev inode will be pointing to a stale bdi. Fix the problem by setting bdev->bd_bdi later when __blkdev_get() is already guaranteed to succeed.
--- fs/block_dev.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
fs/block_dev.c +3 -4
@@ -1556,8 +1556,6 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) bdev->bd_disk = disk; bdev->bd_queue = disk->queue; bdev->bd_contains = bdev; -if (bdev->bd_bdi == &noop_backing_dev_info) - bdev->bd_bdi =bdi_get (disk->queue->backing_dev_info);if (!partno) { ret = -ENXIO ;
@@ -1622,6 +1620,9 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) }bd_set_size (bdev, (loff_t )bdev->bd_part->nr_sects <<9 ); } + +if (bdev->bd_bdi == &noop_backing_dev_info) + bdev->bd_bdi =bdi_get (disk->queue->backing_dev_info); }else {if (bdev->bd_contains == bdev) { ret =0 ;
@@ -1653,8 +1654,6 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) bdev->bd_disk =NULL ; bdev->bd_part =NULL ; bdev->bd_queue =NULL ; -bdi_put (bdev->bd_bdi); - bdev->bd_bdi = &noop_backing_dev_info;if (bdev != bdev->bd_contains)__blkdev_put (bdev->bd_contains, mode,1 ); bdev->bd_contains =NULL ;
Signature · 1 line
2.10.2
Jan Kara
maintainerPATCH v5 2/10 bdi: Mark congested->bdi as internal
In reply to Jan Karacongested->bdi pointer is used only to be able to remove congested structure from bdi->cgwb_congested_tree on structure release. Moreover the pointer can become NULL when we unregister the bdi. Rename the field to __bdi and add a comment to make it more explicit this is internal stuff of memcg writeback code and people should not use the field as such use will be likely race prone.
We do not bother with converting congested->bdi to a proper refcounted reference. It will be slightly ugly to special-case bdi->wb.congested to avoid effectively a cyclic reference of bdi to itself and the reference gets cleared from bdi_unregister() making it impossible to reference a freed bdi.
--- include/linux/backing-dev-defs.h | 4 +++- mm/backing-dev.c | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-)
include/linux/backing-dev-defs.h +3 -1
@@ -54,7 +54,9 @@ struct bdi_writeback_congested {atomic_t refcnt;/* nr of attached wb's and blkg */ #ifdef CONFIG_CGROUP_WRITEBACK -struct backing_dev_info *bdi;/* the associated bdi */ +struct backing_dev_info *__bdi;/* the associated bdi, set to NULL +* on bdi unregistration. For memcg-wb +* internal use only! */ int blkcg_id;/* ID of the associated blkcg */ struct rb_node rb_node;/* on bdi->cgwb_congestion_tree */ #endif
mm/backing-dev.c +5 -5
@@ -438,7 +438,7 @@ wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)return NULL ;atomic_set (&new_congested->refcnt,0 ); - new_congested->bdi = bdi; + new_congested->__bdi = bdi; new_congested->blkcg_id = blkcg_id; goto retry;
@@ -466,10 +466,10 @@ void wb_congested_put(struct bdi_writeback_congested *congested) }/* bdi might already have been destroyed leaving @congested unlinked */ -if (congested->bdi) { +if (congested->__bdi) {rb_erase (&congested->rb_node, - &congested->bdi->cgwb_congested_tree); - congested->bdi =NULL ; + &congested->__bdi->cgwb_congested_tree); + congested->__bdi =NULL ; }spin_unlock_irqrestore (&cgwb_lock, flags);
@@ -752,7 +752,7 @@ static void cgwb_bdi_exit(struct backing_dev_info *bdi) rb_entry(rbn, struct bdi_writeback_congested, rb_node);rb_erase (rbn, &bdi->cgwb_congested_tree); - congested->bdi =NULL ;/* mark @congested unlinked */ + congested->__bdi =NULL ;/* mark @congested unlinked */ }spin_unlock_irq (&cgwb_lock); }
Signature · 1 line
2.10.2
Jan Kara
maintainerPATCH v5 3/10 bdi: Make wb->bdi a proper reference
In reply to Jan KaraMake wb->bdi a proper refcounted reference to bdi for all bdi_writeback structures except for the one embedded inside struct backing_dev_info. That will allow us to simplify bdi unregistration.
--- mm/backing-dev.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
mm/backing-dev.c +11 -2
@@ -294,6 +294,8 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,memset (wb,0 ,sizeof (*wb)); +if (wb != &bdi->wb) +bdi_get (bdi); wb->bdi = bdi; wb->last_old_flush = jiffies;INIT_LIST_HEAD (&wb->b_dirty);
@@ -314,8 +316,10 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi, wb->dirty_sleep = jiffies; wb->congested =wb_congested_get_create (bdi, blkcg_id, gfp); -if (!wb->congested) -return -ENOMEM ; +if (!wb->congested) { + err = -ENOMEM ; + goto out_put_bdi; + } err =fprop_local_init_percpu (&wb->completions, gfp);if (err)
@@ -335,6 +339,9 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,fprop_local_destroy_percpu (&wb->completions); out_put_cong:wb_congested_put (wb->congested); +out_put_bdi: +if (wb != &bdi->wb) +bdi_put (bdi);return err; }
@@ -372,6 +379,8 @@ static void wb_exit(struct bdi_writeback *wb)fprop_local_destroy_percpu (&wb->completions);wb_congested_put (wb->congested); +if (wb != &wb->bdi->wb) +bdi_put (wb->bdi); }#ifdef CONFIG_CGROUP_WRITEBACK
Signature · 1 line
2.10.2
Jan Kara
maintainerPATCH v5 4/10 bdi: Unify bdi->wb_list handling for root wb_writeback
In reply to Jan KaraCurrently root wb_writeback structure is added to bdi->wb_list in bdi_init() and never removed. That is different from all other wb_writeback structures which get added to the list when created and removed from it before wb_shutdown().
So move list addition of root bdi_writeback to bdi_register() and list removal of all wb_writeback structures to wb_shutdown(). That way a wb_writeback structure is on bdi->wb_list if and only if it can handle writeback and it will make it easier for us to handle shutdown of all wb_writeback structures in bdi_unregister().
--- mm/backing-dev.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-)
mm/backing-dev.c +28 -6
@@ -345,6 +345,8 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,return err; } +static void cgwb_remove_from_bdi_list (struct bdi_writeback *wb); +/* * Remove bdi from the global list and shutdown any threads we have running */
@@ -358,6 +360,7 @@ static void wb_shutdown(struct bdi_writeback *wb) }spin_unlock_bh (&wb->work_lock); +cgwb_remove_from_bdi_list (wb);/* * Drain work list and shutdown the delayed_work. !WB_registered * tells wb_workfn() that @wb is dying and its work_list needs to
@@ -491,10 +494,6 @@ static void cgwb_release_workfn(struct work_struct *work) release_work);struct backing_dev_info *bdi = wb->bdi; -spin_lock_irq (&cgwb_lock); -list_del_rcu (&wb->bdi_node); -spin_unlock_irq (&cgwb_lock); -wb_shutdown (wb);css_put (wb->memcg_css);
@@ -526,6 +525,13 @@ static void cgwb_kill(struct bdi_writeback *wb)percpu_ref_kill (&wb->refcnt); } +static void cgwb_remove_from_bdi_list (struct bdi_writeback *wb) +{ +spin_lock_irq (&cgwb_lock); +list_del_rcu (&wb->bdi_node); +spin_unlock_irq (&cgwb_lock); +} +static int cgwb_create (struct backing_dev_info *bdi,struct cgroup_subsys_state *memcg_css,gfp_t gfp) {
@@ -766,6 +772,13 @@ static void cgwb_bdi_exit(struct backing_dev_info *bdi) spin_unlock_irq(&cgwb_lock); } +static void cgwb_bdi_register (struct backing_dev_info *bdi) +{ +spin_lock_irq (&cgwb_lock); +list_add_tail_rcu (&bdi->wb.bdi_node, &bdi->wb_list); +spin_unlock_irq (&cgwb_lock); +} +#else /* CONFIG_CGROUP_WRITEBACK */ static int cgwb_bdi_init (struct backing_dev_info *bdi)
@@ -793,6 +806,16 @@ static void cgwb_bdi_exit(struct backing_dev_info *bdi) wb_congested_put(bdi->wb_congested); } +static void cgwb_bdi_register (struct backing_dev_info *bdi) +{ +list_add_tail_rcu (&bdi->wb.bdi_node, &bdi->wb_list); +} + +static void cgwb_remove_from_bdi_list (struct bdi_writeback *wb) +{ +list_del_rcu (&wb->bdi_node); +} +#endif /* CONFIG_CGROUP_WRITEBACK */ int bdi_init (struct backing_dev_info *bdi)
@@ -811,8 +834,6 @@ int bdi_init(struct backing_dev_info *bdi) ret =cgwb_bdi_init (bdi); -list_add_tail_rcu (&bdi->wb.bdi_node, &bdi->wb_list); -return ret; }EXPORT_SYMBOL (bdi_init);
@@ -848,6 +869,7 @@ int bdi_register(struct backing_dev_info *bdi, struct device *parent,if (IS_ERR (dev))return PTR_ERR (dev); +cgwb_bdi_register (bdi); bdi->dev = dev;bdi_debug_register (bdi,dev_name (dev));
Signature · 1 line
2.10.2
Jan Kara
maintainerPATCH v5 5/10 bdi: Shutdown writeback on all cgwbs in cgwb_bdi_destroy()
In reply to Jan KaraCurrently we waited for all cgwbs to get freed in cgwb_bdi_destroy() which also means that writeback has been shutdown on them. Since this wait is going away, directly shutdown writeback on cgwbs from cgwb_bdi_destroy() to avoid live writeback structures after bdi_unregister() has finished. To make that safe with concurrent shutdown from cgwb_release_workfn(), we also have to make sure wb_shutdown() returns only after the bdi_writeback structure is really shutdown.
--- include/linux/backing-dev-defs.h | 1 + mm/backing-dev.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+)
include/linux/backing-dev-defs.h +1 -0
@@ -21,6 +21,7 @@ struct dentry;*/ enum wb_state { WB_registered,/* bdi_register() was done */ + WB_shutting_down,/* wb_shutdown() in progress */ WB_writeback_running,/* Writeback is in progress */ WB_has_dirty_io,/* Dirty inodes on ->b_{dirty|io|more_io} */ };
mm/backing-dev.c +22 -0
@@ -356,8 +356,15 @@ static void wb_shutdown(struct bdi_writeback *wb)spin_lock_bh (&wb->work_lock);if (!test_and_clear_bit (WB_registered, &wb->state)) {spin_unlock_bh (&wb->work_lock); +/* +* Wait for wb shutdown to finish if someone else is just +* running wb_shutdown(). Otherwise we could proceed to wb / +* bdi destruction before wb_shutdown() is finished. +*/ +wait_on_bit (&wb->state, WB_shutting_down,TASK_UNINTERRUPTIBLE );return ; } +set_bit (WB_shutting_down, &wb->state);spin_unlock_bh (&wb->work_lock);cgwb_remove_from_bdi_list (wb);
@@ -369,6 +376,12 @@ static void wb_shutdown(struct bdi_writeback *wb)mod_delayed_work (bdi_wq, &wb->dwork,0 );flush_delayed_work (&wb->dwork);WARN_ON (!list_empty (&wb->work_list)); +/* +* Make sure bit gets cleared after shutdown is finished. Matches with +* the barrier provided by test_and_clear_bit() above. +*/ +smp_wmb (); +clear_bit (WB_shutting_down, &wb->state); }static void wb_exit (struct bdi_writeback *wb)
@@ -699,12 +712,21 @@ static void cgwb_bdi_destroy(struct backing_dev_info *bdi) {struct radix_tree_iter iter;void **slot; +struct bdi_writeback *wb;WARN_ON (test_bit (WB_registered, &bdi->wb.state));spin_lock_irq (&cgwb_lock);radix_tree_for_each_slot (slot, &bdi->cgwb_tree, &iter,0 )cgwb_kill (*slot); + +while (!list_empty (&bdi->wb_list)) { + wb = list_first_entry(&bdi->wb_list, struct bdi_writeback, + bdi_node); +spin_unlock_irq (&cgwb_lock); +wb_shutdown (wb); +spin_lock_irq (&cgwb_lock); + }spin_unlock_irq (&cgwb_lock);/*
Signature · 1 line
2.10.2
Jan Kara
maintainerPATCH v5 6/10 bdi: Do not wait for cgwbs release in bdi_unregister()
In reply to Jan KaraCurrently we wait for all cgwbs to get released in cgwb_bdi_destroy() (called from bdi_unregister()). That is however unnecessary now when cgwb->bdi is a proper refcounted reference (thus bdi cannot get released before all cgwbs are released) and when cgwb_bdi_destroy() shuts down writeback directly.
--- include/linux/backing-dev-defs.h | 1 - mm/backing-dev.c | 22 +--------------------- 2 files changed, 1 insertion(+), 22 deletions(-)
include/linux/backing-dev-defs.h +0 -1
@@ -164,7 +164,6 @@ struct backing_dev_info {#ifdef CONFIG_CGROUP_WRITEBACK struct radix_tree_root cgwb_tree;/* radix tree of active cgroup wbs */ struct rb_root cgwb_congested_tree;/* their congested states */ -atomic_t usage_cnt;/* counts both cgwbs and cgwb_contested's */ #else struct bdi_writeback_congested *wb_congested;#endif
mm/backing-dev.c +1 -21
@@ -406,11 +406,9 @@ static void wb_exit(struct bdi_writeback *wb)/* * cgwb_lock protects bdi->cgwb_tree, bdi->cgwb_congested_tree, * blkcg->cgwb_list, and memcg->cgwb_list. bdi->cgwb_tree is also RCU -* protected. cgwb_release_wait is used to wait for the completion of cgwb -* releases from bdi destruction path. +* protected. */ static DEFINE_SPINLOCK (cgwb_lock); -static DECLARE_WAIT_QUEUE_HEAD (cgwb_release_wait);/** * wb_congested_get_create - get or create a wb_congested
@@ -505,7 +503,6 @@ static void cgwb_release_workfn(struct work_struct *work) { struct bdi_writeback *wb = container_of(work, struct bdi_writeback, release_work); -struct backing_dev_info *bdi = wb->bdi;wb_shutdown (wb);
@@ -516,9 +513,6 @@ static void cgwb_release_workfn(struct work_struct *work)percpu_ref_exit (&wb->refcnt);wb_exit (wb);kfree_rcu (wb, rcu); - -if (atomic_dec_and_test (&bdi->usage_cnt)) -wake_up_all (&cgwb_release_wait); }static void cgwb_release (struct percpu_ref *refcnt)
@@ -608,7 +602,6 @@ static int cgwb_create(struct backing_dev_info *bdi,/* we might have raced another instance of this function */ ret =radix_tree_insert (&bdi->cgwb_tree, memcg_css->id, wb);if (!ret) { -atomic_inc (&bdi->usage_cnt);list_add_tail_rcu (&wb->bdi_node, &bdi->wb_list);list_add (&wb->memcg_node, memcg_cgwb_list);list_add (&wb->blkcg_node, blkcg_cgwb_list);
@@ -698,7 +691,6 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi)INIT_RADIX_TREE (&bdi->cgwb_tree,GFP_ATOMIC ); bdi->cgwb_congested_tree =RB_ROOT ; -atomic_set (&bdi->usage_cnt,1 ); ret =wb_init (&bdi->wb, bdi,1 ,GFP_KERNEL );if (!ret) {
@@ -728,18 +720,6 @@ static void cgwb_bdi_destroy(struct backing_dev_info *bdi)spin_lock_irq (&cgwb_lock); }spin_unlock_irq (&cgwb_lock); - -/* -* All cgwb's must be shutdown and released before returning. Drain -* the usage counter to wait for all cgwb's ever created on @bdi. -*/ -atomic_dec (&bdi->usage_cnt); -wait_event (cgwb_release_wait, !atomic_read (&bdi->usage_cnt)); -/* -* Grab back our reference so that we hold it when @bdi gets -* re-registered. -*/ -atomic_inc (&bdi->usage_cnt); }/**
Signature · 1 line
2.10.2
Jan Kara
maintainerPATCH v5 7/10 bdi: Rename cgwb_bdi_destroy() to cgwb_bdi_unregister()
In reply to Jan KaraRename cgwb_bdi_destroy() to cgwb_bdi_unregister() as it gets called from bdi_unregister() which is not necessarily called from bdi_destroy() and thus the name is somewhat misleading.
--- mm/backing-dev.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/backing-dev.c +3 -3
@@ -700,7 +700,7 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi)return ret; } -static void cgwb_bdi_destroy (struct backing_dev_info *bdi) +static void cgwb_bdi_unregister (struct backing_dev_info *bdi) {struct radix_tree_iter iter;void **slot;
@@ -801,7 +801,7 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi)return 0 ; } -static void cgwb_bdi_destroy (struct backing_dev_info *bdi) { } +static void cgwb_bdi_unregister (struct backing_dev_info *bdi) { }static void cgwb_bdi_exit (struct backing_dev_info *bdi) {
@@ -925,7 +925,7 @@ void bdi_unregister(struct backing_dev_info *bdi)/* make sure nobody finds us on the bdi_list anymore */ bdi_remove_from_list (bdi);wb_shutdown (&bdi->wb); -cgwb_bdi_destroy (bdi); +cgwb_bdi_unregister (bdi);if (bdi->dev) {bdi_debug_unregister (bdi);
Signature · 1 line
2.10.2
Jan Kara
maintainerPATCH v5 8/10 block: Fix oops in locked_inode_to_wb_and_lock_list()
In reply to Jan KaraWhen block device is closed, we call inode_detach_wb() in __blkdev_put() which sets inode->i_wb to NULL. That is contrary to expectations that inode->i_wb stays valid once set during the whole inode's lifetime and leads to oops in wb_get() in locked_inode_to_wb_and_lock_list() because inode_to_wb() returned NULL.
The reason why we called inode_detach_wb() is not valid anymore though. BDI is guaranteed to stay along until we call bdi_put() from bdev_evict_inode() so we can postpone calling inode_detach_wb() to that moment.
Also add a warning to catch if someone uses inode_detach_wb() in a dangerous way.
--- fs/block_dev.c | 8 ++------ include/linux/writeback.h | 1 + 2 files changed, 3 insertions(+), 6 deletions(-)
fs/block_dev.c +2 -6
@@ -885,6 +885,8 @@ static void bdev_evict_inode(struct inode *inode)spin_lock (&bdev_lock);list_del_init (&bdev->bd_list);spin_unlock (&bdev_lock); +/* Detach inode from wb early as bdi_put() may free bdi->wb */ +inode_detach_wb (inode);if (bdev->bd_bdi != &noop_backing_dev_info) {bdi_put (bdev->bd_bdi); bdev->bd_bdi = &noop_backing_dev_info;
@@ -1875,12 +1877,6 @@ static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)kill_bdev (bdev);bdev_write_inode (bdev); -/* -* Detaching bdev inode from its wb in __destroy_inode() -* is too late: the queue which embeds its bdi (along with -* root wb) can be gone as soon as we put_disk() below. -*/ -inode_detach_wb (bdev->bd_inode); }if (bdev->bd_contains == bdev) {if (disk->fops->release)
include/linux/writeback.h +1 -0
@@ -237,6 +237,7 @@ static inline void inode_attach_wb(struct inode *inode, struct page *page)static inline void inode_detach_wb (struct inode *inode) {if (inode->i_wb) { +WARN_ON_ONCE (!(inode->i_state &I_CLEAR ));wb_put (inode->i_wb); inode->i_wb =NULL ; }
Signature · 1 line
2.10.2
Jan Kara
maintainerPATCH v5 9/10 kobject: Export kobject_get_unless_zero()
In reply to Jan KaraMake the function available for outside use and fortify it against NULL kobject.
--- include/linux/kobject.h | 2 ++ lib/kobject.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-)
include/linux/kobject.h +2 -0
@@ -108,6 +108,8 @@ extern int __must_check kobject_rename(struct kobject *, const char *new_name);extern int __must_check kobject_move (struct kobject *,struct kobject *);extern struct kobject *kobject_get (struct kobject *kobj); +extern struct kobject *__must_check kobject_get_unless_zero ( +struct kobject *kobj);extern void kobject_put (struct kobject *kobj);extern const void *kobject_namespace (struct kobject *kobj);
lib/kobject.c +4 -1
@@ -601,12 +601,15 @@ struct kobject *kobject_get(struct kobject *kobj) }EXPORT_SYMBOL (kobject_get); -static struct kobject *__must_check kobject_get_unless_zero (struct kobject *kobj) +struct kobject *__must_check kobject_get_unless_zero (struct kobject *kobj) { +if (!kobj) +return NULL ;if (!kref_get_unless_zero (&kobj->kref)) kobj =NULL ;return kobj; } +EXPORT_SYMBOL (kobject_get_unless_zero);/* * kobject_cleanup - free kobject resources.
Signature · 1 line
2.10.2
Jan Kara
maintainerPATCH v5 10/10 block: Fix oops scsi_disk_get()
In reply to Jan KaraWhen device open races with device shutdown, we can get the following oops in scsi_disk_get():
[11863.044351] general protection fault: 0000 [#1] SMP [11863.045561] Modules linked in: scsi_debug xfs libcrc32c netconsole btrfs raid6_pq zlib_deflate lzo_compress xor [last unloaded: loop] [11863.047853] CPU: 3 PID: 13042 Comm: hald-probe-stor Tainted: G W 4.10.0-rc2-xen+ #35 [11863.048030] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011 [11863.048030] task: ffff88007f438200 task.stack: ffffc90000fd0000 [11863.048030] RIP: 0010:scsi_disk_get+0x43/0x70 [11863.048030] RSP: 0018:ffffc90000fd3a08 EFLAGS: 00010202 [11863.048030] RAX: 6b6b6b6b6b6b6b6b RBX: ffff88007f56d000 RCX: 0000000000000000 [11863.048030] RDX: 0000000000000001 RSI: 0000000000000004 RDI: ffffffff81a8d880 [11863.048030] RBP: ffffc90000fd3a18 R08: 0000000000000000 R09: 0000000000000001 [11863.059217] R10: 0000000000000000 R11: 0000000000000000 R12: 00000000fffffffa [11863.059217] R13: ffff880078872800 R14: ffff880070915540 R15: 000000000000001d [11863.059217] FS: 00007f2611f71800(0000) GS:ffff88007f0c0000(0000) knlGS:0000000000000000 [11863.059217] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [11863.059217] CR2: 000000000060e048 CR3: 00000000778d4000 CR4: 00000000000006e0 [11863.059217] Call Trace: [11863.059217] ? disk_get_part+0x22/0x1f0 [11863.059217] sd_open+0x39/0x130 [11863.059217] __blkdev_get+0x69/0x430 [11863.059217] ? bd_acquire+0x7f/0xc0 [11863.059217] ? bd_acquire+0x96/0xc0 [11863.059217] ? blkdev_get+0x350/0x350 [11863.059217] blkdev_get+0x126/0x350 [11863.059217] ? _raw_spin_unlock+0x2b/0x40 [11863.059217] ? bd_acquire+0x7f/0xc0 [11863.059217] ? blkdev_get+0x350/0x350 [11863.059217] blkdev_open+0x65/0x80
...
As you can see RAX value is already poisoned showing that gendisk we got is already freed. The problem is that get_gendisk() looks up device number in ext_devt_idr and then does get_disk() which does kobject_get() on the disks kobject. However the disk gets removed from ext_devt_idr only in disk_release() (through blk_free_devt()) at which moment it has already 0 refcount and is already on its way to be freed. Indeed we've got a warning from kobject_get() about 0 refcount shortly before the oops.
We fix the problem by using kobject_get_unless_zero() in get_disk() so that get_disk() cannot get reference on a disk that is already being freed.
--- block/genhd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/genhd.c +1 -1
@@ -1352,7 +1352,7 @@ struct kobject *get_disk(struct gendisk *disk) owner = disk->fops->owner;if (owner && !try_module_get (owner))return NULL ; - kobj =kobject_get (&disk_to_dev (disk)->kobj); + kobj =kobject_get_unless_zero (&disk_to_dev (disk)->kobj);if (kobj ==NULL ) {module_put (owner);return NULL ;
Signature · 1 line
2.10.2
Jens Axboe
maintainerQuoted 12 lines · Jan Kara
Hello,
this is a series with the remaining patches (on top of 4.11-rc2) to fix several different races and issues I've found when testing device shutdown and reuse. The first patch fixes possible (theoretical) problems when opening of a block device races with shutdown of a gendisk structure. Patches 2-8 fix oops that is triggered by
__blkdev_put()callinginode_detach_wb()too early (the problem reported by Thiago). Patches 9 and 10 fix oops due to a bug in gendisk code whereget_gendisk()can return already freed gendisk structure (again triggered by Omar's stress test).
Quoted 3 lines
All patches got reviewed by Tejun and also tested by Thiago (thanks!). Jens, can you please queue these fixes for the next merge window? Thanks!
Done! Thanks a lot for working on this, Jan.
Signature · 1 line
Jens Axboe
Hello,
Am Donnerstag, 23. M=E4rz 2017, 01:36:52 BRT schrieb Jan Kara:
Quoted 6 lines
this is a series with the remaining patches (on top of 4.11-rc2) to fix several different races and issues I've found when testing device shutdown and reuse. The first patch fixes possible (theoretical) problems when opening of a block device races with shutdown of a gendisk structure. Patches 2-8 fix oops that is triggered by
__blkdev_put()callinginode_detach_wb()too early (the problem reported by Thiago). Patches 9 a=
nd
Quoted 4 lines
10 fix oops due to a bug in gendisk code where
get_gendisk()can return already freed gendisk structure (again triggered by Omar's stress test). =20
All patches got reviewed by Tejun and also tested by Thiago (thanks!). Je=
ns,
Quoted 1 line
can you please queue these fixes for the next merge window? Thanks!
Lekshmi tested these patches on top of v4.11-rc4 and they worked fine. Sorry for the delay, it takes at least 72h of running time to be sure we're= =20
not hitting the race condition, and I had trouble with v4.11-rc2.
=2D-=20
Thiago Jung Bauermann
IBM Linux Technology Center
Jan Kara
maintainerQuoted 12 lines · Thiago Jung Bauermann
Hello,
Am Donnerstag, 23. M�rz 2017, 01:36:52 BRT schrieb Jan Kara:
Quoted 9 lines
this is a series with the remaining patches (on top of 4.11-rc2) to fix several different races and issues I've found when testing device shutdown and reuse. The first patch fixes possible (theoretical) problems when opening of a block device races with shutdown of a gendisk structure. Patches 2-8 fix oops that is triggered by
__blkdev_put()callinginode_detach_wb()too early (the problem reported by Thiago). Patches 9 and 10 fix oops due to a bug in gendisk code whereget_gendisk()can return already freed gendisk structure (again triggered by Omar's stress test).
Quoted 6 lines
Quoted 2 lines
All patches got reviewed by Tejun and also tested by Thiago (thanks!). Jens, can you please queue these fixes for the next merge window? Thanks!
Lekshmi tested these patches on top of v4.11-rc4 and they worked fine. Sorry for the delay, it takes at least 72h of running time to be sure we're not hitting the race condition, and I had trouble with v4.11-rc2.
Great. Thanks for testing!
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR