mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: mingo@kernel.org, linux-kernel@vger.kernel.org
Cc: peter@hurleysoftware.com, oleg@redhat.com, rjw@rjwysocki.net,
	torvalds@linux-foundation.org, tglx@linutronix.de,
	eparis@redhat.com, umgwanakikbuti@gmail.com, marcel@holtmann.org,
	ebiederm@xmission.com, davem@davemloft.net,
	fengguang.wu@intel.com, Peter Zijlstra <peterz@infradead.org>
Subject: [PATCH 1/7] sched,wait: Fix a kthread race with wait_woken()
Date: Fri, 31 Oct 2014 12:10:38 +0100	[thread overview]
Message-ID: <20141031111549.492532161@infradead.org> (raw)
In-Reply-To: <20141031111037.936236584@infradead.org>

[-- Attachment #1: peterz-wait_woken-kthread.patch --]
[-- Type: text/plain, Size: 2015 bytes --]

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).

Cc: Peter Hurley <peter@hurleysoftware.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 kernel/sched/wait.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

--- a/kernel/sched/wait.c
+++ b/kernel/sched/wait.c
@@ -9,6 +9,7 @@
 #include <linux/mm.h>
 #include <linux/wait.h>
 #include <linux/hash.h>
+#include <linux/kthread.h>
 
 void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *key)
 {
@@ -297,6 +298,10 @@ int autoremove_wake_function(wait_queue_
 }
 EXPORT_SYMBOL(autoremove_wake_function);
 
+static inline bool is_kthread_should_stop(void)
+{
+	return (current->flags & PF_KTHREAD) && kthread_should_stop();
+}
 
 /*
  * DEFINE_WAIT_FUNC(wait, woken_wake_func);
@@ -326,7 +331,7 @@ long wait_woken(wait_queue_t *wait, unsi
 	 * woken_wake_function() such that if we observe WQ_FLAG_WOKEN we must
 	 * also observe all state before the wakeup.
 	 */
-	if (!(wait->flags & WQ_FLAG_WOKEN))
+	if (!(wait->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop())
 		timeout = schedule_timeout(timeout);
 	__set_current_state(TASK_RUNNING);
 



  reply	other threads:[~2014-10-31 11:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-31 11:10 [PATCH 0/7] Various fixes for nested sleep stuff Peter Zijlstra
2014-10-31 11:10 ` Peter Zijlstra [this message]
2014-10-31 22:13   ` [PATCH 1/7] sched,wait: Fix a kthread race with wait_woken() Oleg Nesterov
2014-10-31 11:10 ` [PATCH 2/7] wait: Reimplement wait_event_freezable() Peter Zijlstra
2014-10-31 17:41   ` Peter Zijlstra
2014-10-31 21:38   ` Oleg Nesterov
2014-10-31 21:48     ` Peter Zijlstra
2014-10-31 21:53     ` Oleg Nesterov
2014-10-31 11:10 ` [PATCH 3/7] wait: Remove wait_event_freezekillable() Peter Zijlstra
2014-10-31 11:10 ` [PATCH 4/7] audit,wait: Fixup kauditd_thread wait loop Peter Zijlstra
2014-10-31 11:10 ` [PATCH 5/7] rfcomm: Fix broken wait construct Peter Zijlstra
2014-10-31 11:10 ` [PATCH 6/7] netdev: Fix sleeping inside wait event Peter Zijlstra
2014-10-31 11:10 ` [PATCH 7/7] sched: Use WARN_ONCE for the might_sleep() TASK_RUNNING test Peter Zijlstra
2014-10-31 22:42   ` Oleg Nesterov
2014-11-03  7:56     ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20141031111549.492532161@infradead.org \
    --to=peterz@infradead.org \
    --cc=davem@davemloft.net \
    --cc=ebiederm@xmission.com \
    --cc=eparis@redhat.com \
    --cc=fengguang.wu@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcel@holtmann.org \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peter@hurleysoftware.com \
    --cc=rjw@rjwysocki.net \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=umgwanakikbuti@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome