sched/wait: Fix a kthread race with wait_woken()
authorPeter Zijlstra <peterz@infradead.org>
Fri, 31 Oct 2014 10:57:30 +0000 (11:57 +0100)
committerIngo Molnar <mingo@kernel.org>
Tue, 4 Nov 2014 06:17:44 +0000 (07:17 +0100)
commitcb6538e740d7543cd989128625cf8cac4b471e0a
treec2b2c8d97c12fe06ead2f810471bd6f0317da2d0
parent3427445afd26bd2395f29241319283a93f362cd0
sched/wait: Fix a kthread race with wait_woken()

There is a race between kthread_stop() and the new wait_woken() that
can result in a lack of progress.

CPU 0                                    | CPU 1
                                         |
rfcomm_run()                             | kthread_stop()
  ...                                    |
  if (!test_bit(KTHREAD_SHOULD_STOP))    |
                                         |   set_bit(KTHREAD_SHOULD_STOP)
                                         |   wake_up_process()
    wait_woken()                         |   wait_for_completion()
      set_current_state(INTERRUPTIBLE)   |
      if (!WQ_FLAG_WOKEN)                |
        schedule_timeout()               |
                                         |

After which both tasks will wait.. forever.

Fix this by having wait_woken() check for kthread_should_stop() but
only for kthreads (obviously).

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Peter Hurley <peter@hurleysoftware.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
kernel/sched/wait.c