mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Libin <huawei.libin@huawei.com>
To: <akpm@linux-foundation.org>, <tj@kernel.org>,
	<viro@zeniv.linux.org.uk>, <eparis@redhat.com>,
	<tglx@linutronix.de>, <rusty@rustcorp.com.au>,
	<ebiederm@xmission.com>, <paulmck@linux.vnet.ibm.com>,
	<john.stultz@linaro.org>, <mingo@redhat.com>,
	<peterz@infradead.org>, <gregkh@linuxfoundation.org>
Cc: <linux-kernel@vger.kernel.org>, <lizefan@huawei.com>,
	<jovi.zhangwei@huawei.com>, <guohanjun@huawei.com>,
	<zhangdianfang@huawei.com>, <wangyijing@huawei.com>,
	<huawei.libin@huawei.com>
Subject: [PATCH 13/14] workqueue: Fix invalid wakeup in rescuer_thread
Date: Thu, 29 Aug 2013 21:57:48 +0800	[thread overview]
Message-ID: <1377784669-28140-14-git-send-email-huawei.libin@huawei.com> (raw)
In-Reply-To: <1377784669-28140-1-git-send-email-huawei.libin@huawei.com>

If thread is preempted before calling set_current_state(TASK_INTERRUPTIBLE),
and the other thread set the condition followed with wake_up_process. After
that when this thread is re-scheduled, calling set_current_state to set itself
as state TASK_INTERRUPTIBLE, if it is preempted again after that and before
__set_current_state(TASK_RUNNING), it triggers the invalid wakeup problem.

To solve this problem, using preempt_disable() to bound the operaion that
setting the task state and the conditions(set by the wake thread) validation.

Signed-off-by: Libin <huawei.libin@huawei.com>
---
 kernel/workqueue.c | 92 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 49 insertions(+), 43 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 7f5d4be..2dcdd30 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -2358,63 +2358,69 @@ static int rescuer_thread(void *__rescuer)
 	 * doesn't participate in concurrency management.
 	 */
 	rescuer->task->flags |= PF_WQ_WORKER;
-repeat:
-	set_current_state(TASK_INTERRUPTIBLE);
+	for (;;) {
 
-	if (kthread_should_stop()) {
+		if (kthread_should_stop()) {
+			rescuer->task->flags &= ~PF_WQ_WORKER;
+			return 0;
+		}
+
+		preempt_disable();
+		set_current_state(TASK_INTERRUPTIBLE);
+		if (list_empty(&wq->maydays)) {
+			preempt_enable();
+			schedule();
+			preempt_disable();
+		}
 		__set_current_state(TASK_RUNNING);
-		rescuer->task->flags &= ~PF_WQ_WORKER;
-		return 0;
-	}
+		preempt_enable();
 
-	/* see whether any pwq is asking for help */
-	spin_lock_irq(&wq_mayday_lock);
+		/* see whether any pwq is asking for help */
+		spin_lock_irq(&wq_mayday_lock);
 
-	while (!list_empty(&wq->maydays)) {
-		struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
+		while (!list_empty(&wq->maydays)) {
+			struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
 					struct pool_workqueue, mayday_node);
-		struct worker_pool *pool = pwq->pool;
-		struct work_struct *work, *n;
+			struct worker_pool *pool = pwq->pool;
+			struct work_struct *work, *n;
 
-		__set_current_state(TASK_RUNNING);
-		list_del_init(&pwq->mayday_node);
+			list_del_init(&pwq->mayday_node);
 
-		spin_unlock_irq(&wq_mayday_lock);
+			spin_unlock_irq(&wq_mayday_lock);
 
-		/* migrate to the target cpu if possible */
-		worker_maybe_bind_and_lock(pool);
-		rescuer->pool = pool;
+			/* migrate to the target cpu if possible */
+			worker_maybe_bind_and_lock(pool);
+			rescuer->pool = pool;
 
-		/*
-		 * Slurp in all works issued via this workqueue and
-		 * process'em.
-		 */
-		WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
-		list_for_each_entry_safe(work, n, &pool->worklist, entry)
-			if (get_work_pwq(work) == pwq)
-				move_linked_works(work, scheduled, &n);
+			/*
+			 * Slurp in all works issued via this workqueue and
+			 * process'em.
+			 */
+			WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
+			list_for_each_entry_safe(work, n, &pool->worklist, entry)
+				if (get_work_pwq(work) == pwq)
+					move_linked_works(work, scheduled, &n);
 
-		process_scheduled_works(rescuer);
+			process_scheduled_works(rescuer);
 
-		/*
-		 * Leave this pool.  If keep_working() is %true, notify a
-		 * regular worker; otherwise, we end up with 0 concurrency
-		 * and stalling the execution.
-		 */
-		if (keep_working(pool))
-			wake_up_worker(pool);
+			/*
+			 * Leave this pool.  If keep_working() is %true, notify a
+			 * regular worker; otherwise, we end up with 0 concurrency
+			 * and stalling the execution.
+			 */
+			if (keep_working(pool))
+				wake_up_worker(pool);
 
-		rescuer->pool = NULL;
-		spin_unlock(&pool->lock);
-		spin_lock(&wq_mayday_lock);
-	}
+			rescuer->pool = NULL;
+			spin_unlock(&pool->lock);
+			spin_lock(&wq_mayday_lock);
+		}
 
-	spin_unlock_irq(&wq_mayday_lock);
+		spin_unlock_irq(&wq_mayday_lock);
 
-	/* rescuers should never participate in concurrency management */
-	WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
-	schedule();
-	goto repeat;
+		/* rescuers should never participate in concurrency management */
+		WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
+	}
 }
 
 struct wq_barrier {
-- 
1.8.2.1



  parent reply	other threads:[~2013-08-29 14:02 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-29 13:57 [PATCH 00/14] Fix bug about invalid wake up problem Libin
2013-08-29 13:57 ` [PATCH 01/14] kthread: Fix invalid wakeup in kthreadd Libin
2013-08-30  0:20   ` Paul E. McKenney
2013-08-29 13:57 ` [PATCH 02/14] audit: Fix invalid wakeup in kauditd_thread Libin
2013-08-29 13:57 ` [PATCH 03/14] audit: Fix invalid wakeup in wait_for_auditd Libin
2013-08-29 13:57 ` [PATCH 04/14] exit: Fix invalid wakeup in do_wait Libin
2013-08-29 13:57 ` [PATCH 05/14] hrtimer: Fix invalid wakeup in do_nanosleep Libin
2013-09-12 13:33   ` Thomas Gleixner
2013-08-29 13:57 ` [PATCH 06/14] irq: Fix invalid wakeup in irq_wait_for_interrupt Libin
2013-09-12 13:36   ` Thomas Gleixner
2013-08-29 13:57 ` [PATCH 07/14] module: Fix invalid wakeup in wait_for_zero_refcount Libin
2013-08-29 13:57 ` [PATCH 08/14] namespace: Fix invalid wakeup in zap_pid_ns_processes Libin
2013-08-29 13:57 ` [PATCH 09/14] rcutree: Fix invalid wakeup in rcu_wait Libin
2013-08-30  0:23   ` Paul E. McKenney
2013-08-29 13:57 ` [PATCH 10/14] time: Fix invalid wakeup in alarmtimer_do_nsleep Libin
2013-09-12 13:36   ` Thomas Gleixner
2013-08-29 13:57 ` [PATCH 11/14] trace: Fix invalid wakeup in wait_to_die Libin
2013-08-29 13:57 ` [PATCH 12/14] trace: Fix invalid wakeup in ring_buffer_consumer_thread Libin
2013-08-29 13:57 ` Libin [this message]
2013-08-29 13:57 ` [PATCH 14/14] klist: Fix invalid wakeup in klist_remove Libin
2013-08-29 14:08 ` [PATCH 00/14] Fix bug about invalid wake up problem Libin
2013-08-29 14:12   ` Tejun Heo
2013-08-29 14:10 ` Tejun Heo
2013-08-29 23:39 ` Libin
2013-08-30  0:18 ` Paul E. McKenney

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=1377784669-28140-14-git-send-email-huawei.libin@huawei.com \
    --to=huawei.libin@huawei.com \
    --cc=akpm@linux-foundation.org \
    --cc=ebiederm@xmission.com \
    --cc=eparis@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=guohanjun@huawei.com \
    --cc=john.stultz@linaro.org \
    --cc=jovi.zhangwei@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=mingo@redhat.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=rusty@rustcorp.com.au \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wangyijing@huawei.com \
    --cc=zhangdianfang@huawei.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

all inboxes | Powered by JetHome®