Skip to content

Commit bf09720

Browse files
jankaragregkh
authored andcommitted
bdi: avoid oops on device removal
commit 5acda9d upstream. After commit 839a8e8 ("writeback: replace custom worker pool implementation with unbound workqueue") when device is removed while we are writing to it we crash in bdi_writeback_workfn() -> set_worker_desc() because bdi->dev is NULL. This can happen because even though bdi_unregister() cancels all pending flushing work, nothing really prevents new ones from being queued from balance_dirty_pages() or other places. Fix the problem by clearing BDI_registered bit in bdi_unregister() and checking it before scheduling of any flushing work. Fixes: 839a8e8 Reviewed-by: Tejun Heo <tj@kernel.org> Signed-off-by: Jan Kara <jack@suse.cz> Cc: Derek Basehore <dbasehore@chromium.org> Cc: Jens Axboe <axboe@kernel.dk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 39305a6 commit bf09720

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎fs/fs-writeback.c‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,29 @@ static inline struct inode *wb_inode(struct list_head *head)
8787
#define CREATE_TRACE_POINTS
8888
#include <trace/events/writeback.h>
8989

90+
static void bdi_wakeup_thread(struct backing_dev_info *bdi)
91+
{
92+
spin_lock_bh(&bdi->wb_lock);
93+
if (test_bit(BDI_registered, &bdi->state))
94+
mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
95+
spin_unlock_bh(&bdi->wb_lock);
96+
}
97+
9098
static void bdi_queue_work(struct backing_dev_info *bdi,
9199
struct wb_writeback_work *work)
92100
{
93101
trace_writeback_queue(bdi, work);
94102

95103
spin_lock_bh(&bdi->wb_lock);
104+
if (!test_bit(BDI_registered, &bdi->state)) {
105+
if (work->done)
106+
complete(work->done);
107+
goto out_unlock;
108+
}
96109
list_add_tail(&work->list, &bdi->work_list);
97-
spin_unlock_bh(&bdi->wb_lock);
98-
99110
mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
111+
out_unlock:
112+
spin_unlock_bh(&bdi->wb_lock);
100113
}
101114

102115
static void
@@ -112,7 +125,7 @@ __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
112125
work = kzalloc(sizeof(*work), GFP_ATOMIC);
113126
if (!work) {
114127
trace_writeback_nowork(bdi);
115-
mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
128+
bdi_wakeup_thread(bdi);
116129
return;
117130
}
118131

@@ -159,7 +172,7 @@ void bdi_start_background_writeback(struct backing_dev_info *bdi)
159172
* writeback as soon as there is no other work to do.
160173
*/
161174
trace_writeback_wake_background(bdi);
162-
mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
175+
bdi_wakeup_thread(bdi);
163176
}
164177

165178
/*
@@ -1016,7 +1029,7 @@ void bdi_writeback_workfn(struct work_struct *work)
10161029
current->flags |= PF_SWAPWRITE;
10171030

10181031
if (likely(!current_is_workqueue_rescuer() ||
1019-
list_empty(&bdi->bdi_list))) {
1032+
!test_bit(BDI_registered, &bdi->state))) {
10201033
/*
10211034
* The normal path. Keep writing back @bdi until its
10221035
* work_list is empty. Note that this path is also taken

‎include/linux/backing-dev.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ struct backing_dev_info {
9595
unsigned int max_ratio, max_prop_frac;
9696

9797
struct bdi_writeback wb; /* default writeback info for this bdi */
98-
spinlock_t wb_lock; /* protects work_list */
98+
spinlock_t wb_lock; /* protects work_list & wb.dwork scheduling */
9999

100100
struct list_head work_list;
101101

‎mm/backing-dev.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,10 @@ void bdi_wakeup_thread_delayed(struct backing_dev_info *bdi)
296296
unsigned long timeout;
297297

298298
timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
299-
queue_delayed_work(bdi_wq, &bdi->wb.dwork, timeout);
299+
spin_lock_bh(&bdi->wb_lock);
300+
if (test_bit(BDI_registered, &bdi->state))
301+
queue_delayed_work(bdi_wq, &bdi->wb.dwork, timeout);
302+
spin_unlock_bh(&bdi->wb_lock);
300303
}
301304

302305
/*
@@ -309,9 +312,6 @@ static void bdi_remove_from_list(struct backing_dev_info *bdi)
309312
spin_unlock_bh(&bdi_lock);
310313

311314
synchronize_rcu_expedited();
312-
313-
/* bdi_list is now unused, clear it to mark @bdi dying */
314-
INIT_LIST_HEAD(&bdi->bdi_list);
315315
}
316316

317317
int bdi_register(struct backing_dev_info *bdi, struct device *parent,
@@ -362,6 +362,11 @@ static void bdi_wb_shutdown(struct backing_dev_info *bdi)
362362
*/
363363
bdi_remove_from_list(bdi);
364364

365+
/* Make sure nobody queues further work */
366+
spin_lock_bh(&bdi->wb_lock);
367+
clear_bit(BDI_registered, &bdi->state);
368+
spin_unlock_bh(&bdi->wb_lock);
369+
365370
/*
366371
* Drain work list and shutdown the delayed_work. At this point,
367372
* @bdi->bdi_list is empty telling bdi_Writeback_workfn() that @bdi

0 commit comments

Comments
 (0)