Skip to content

Commit 99361bb

Browse files
Coly Liaxboe
authored andcommitted
bcache: properly set task state in bch_writeback_thread()
Kernel thread routine bch_writeback_thread() has the following code block, 447 down_write(&dc->writeback_lock); 448~450 if (check conditions) { 451 up_write(&dc->writeback_lock); 452 set_current_state(TASK_INTERRUPTIBLE); 453 454 if (kthread_should_stop()) 455 return 0; 456 457 schedule(); 458 continue; 459 } If condition check is true, its task state is set to TASK_INTERRUPTIBLE and call schedule() to wait for others to wake up it. There are 2 issues in current code, 1, Task state is set to TASK_INTERRUPTIBLE after the condition checks, if another process changes the condition and call wake_up_process(dc-> writeback_thread), then at line 452 task state is set back to TASK_INTERRUPTIBLE, the writeback kernel thread will lose a chance to be waken up. 2, At line 454 if kthread_should_stop() is true, writeback kernel thread will return to kernel/kthread.c:kthread() with TASK_INTERRUPTIBLE and call do_exit(). It is not good to enter do_exit() with task state TASK_INTERRUPTIBLE, in following code path might_sleep() is called and a warning message is reported by __might_sleep(): "WARNING: do not call blocking ops when !TASK_RUNNING; state=1 set at [xxxx]". For the first issue, task state should be set before condition checks. Ineed because dc->writeback_lock is required when modifying all the conditions, calling set_current_state() inside code block where dc-> writeback_lock is hold is safe. But this is quite implicit, so I still move set_current_state() before all the condition checks. For the second issue, frankley speaking it does not hurt when kernel thread exits with TASK_INTERRUPTIBLE state, but this warning message scares users, makes them feel there might be something risky with bcache and hurt their data. Setting task state to TASK_RUNNING before returning fixes this problem. In alloc.c:allocator_wait(), there is also a similar issue, and is also fixed in this patch. Changelog: v3: merge two similar fixes into one patch v2: fix the race issue in v1 patch. v1: initial buggy fix. Signed-off-by: Coly Li <[email protected]> Reviewed-by: Hannes Reinecke <[email protected]> Reviewed-by: Michael Lyle <[email protected]> Cc: Michael Lyle <[email protected]> Cc: Junhui Tang <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent c4dc249 commit 99361bb

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

drivers/md/bcache/alloc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,10 @@ do { \
287287
break; \
288288
\
289289
mutex_unlock(&(ca)->set->bucket_lock); \
290-
if (kthread_should_stop()) \
290+
if (kthread_should_stop()) { \
291+
set_current_state(TASK_RUNNING); \
291292
return 0; \
293+
} \
292294
\
293295
schedule(); \
294296
mutex_lock(&(ca)->set->bucket_lock); \

drivers/md/bcache/writeback.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,18 +564,21 @@ static int bch_writeback_thread(void *arg)
564564

565565
while (!kthread_should_stop()) {
566566
down_write(&dc->writeback_lock);
567+
set_current_state(TASK_INTERRUPTIBLE);
567568
if (!atomic_read(&dc->has_dirty) ||
568569
(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) &&
569570
!dc->writeback_running)) {
570571
up_write(&dc->writeback_lock);
571-
set_current_state(TASK_INTERRUPTIBLE);
572572

573-
if (kthread_should_stop())
573+
if (kthread_should_stop()) {
574+
set_current_state(TASK_RUNNING);
574575
return 0;
576+
}
575577

576578
schedule();
577579
continue;
578580
}
581+
set_current_state(TASK_RUNNING);
579582

580583
searched_full_index = refill_dirty(dc);
581584

0 commit comments

Comments
 (0)