Skip to content

Commit d005f8c

Browse files
Zhihao Chengrichardweinberger
authored andcommitted
ubi: check kthread_should_stop() after the setting of task state
A detach hung is possible when a race occurs between the detach process and the ubi background thread. The following sequences outline the race: ubi thread: if (list_empty(&ubi->works)... ubi detach: set_bit(KTHREAD_SHOULD_STOP, &kthread->flags) => by kthread_stop() wake_up_process() => ubi thread is still running, so 0 is returned ubi thread: set_current_state(TASK_INTERRUPTIBLE) schedule() => ubi thread will never be scheduled again ubi detach: wait_for_completion() => hung task! To fix that, we need to check kthread_should_stop() after we set the task state, so the ubi thread will either see the stop bit and exit or the task state is reset to runnable such that it isn't scheduled out indefinitely. Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com> Cc: <stable@vger.kernel.org> Fixes: 801c135 ("UBI: Unsorted Block Images") Reported-by: syzbot+853639d0cb16c31c7a14@syzkaller.appspotmail.com Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent 58f6e78 commit d005f8c

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

drivers/mtd/ubi/wl.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,19 @@ int ubi_thread(void *u)
16391639
!ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
16401640
set_current_state(TASK_INTERRUPTIBLE);
16411641
spin_unlock(&ubi->wl_lock);
1642+
1643+
/*
1644+
* Check kthread_should_stop() after we set the task
1645+
* state to guarantee that we either see the stop bit
1646+
* and exit or the task state is reset to runnable such
1647+
* that it's not scheduled out indefinitely and detects
1648+
* the stop bit at kthread_should_stop().
1649+
*/
1650+
if (kthread_should_stop()) {
1651+
set_current_state(TASK_RUNNING);
1652+
break;
1653+
}
1654+
16421655
schedule();
16431656
continue;
16441657
}

0 commit comments

Comments
 (0)