public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [V2 -stable PATCH 1/2] workqueue: fix leak of active
@ 2012-09-18  5:57 Lai Jiangshan
  2012-09-18  5:57 ` [V2 for-next PATCH 2/2] workqueue: remove @delayed argument from cwq_dec_nr_in_flight() Lai Jiangshan
  0 siblings, 1 reply; 3+ messages in thread
From: Lai Jiangshan @ 2012-09-18  5:57 UTC (permalink / raw)
  To: Tejun Heo, linux-kernel; +Cc: Lai Jiangshan

try_to_grab_pending() leave LINKED tagalong in delayed queue when
it deletes a work. This behavior will cause future
cwq_activate_first_delayed() increase the ->nr_active wrongly,
and may cause the whole cwq frozen.

example:

state: cwq->max_active = 1, cwq->nr_active = 1
       one work in cwq->pool, many in cwq->delayed_works.

step1: try_to_grab_pending() remove a work from delayed_works
       but leave tagalong.
step2: when the work in cwq->pool is finished,
       cwq_activate_first_delayed() move the tagalong to cwq->pool
       and increase the ->nr_active.

current state: cwq->nr_active = 1, but works of the cwq
               in cwq->pool are all NO_COLOR, so even when
               these works are finished, cwq->nr_active will
               not be decreased, and no work will be moved from
               cwq->delayed_works. the whole cwq is frozen.

Fix it by moving the work to cwq->pool before delete it
in try_to_grab_pending(), thus the tagalong is left in
cwq->pool like as grabbing non-delayed work.

Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
---
 kernel/workqueue.c |   26 +++++++++++++++++++++++---
 1 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 7b91332..834aa62 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -973,10 +973,9 @@ static void move_linked_works(struct work_struct *work, struct list_head *head,
 		*nextp = n;
 }
 
-static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
+static void cwq_activate_delayed_work(struct work_struct *work)
 {
-	struct work_struct *work = list_first_entry(&cwq->delayed_works,
-						    struct work_struct, entry);
+	struct cpu_workqueue_struct *cwq = get_work_cwq(work);
 
 	trace_workqueue_activate_work(work);
 	move_linked_works(work, &cwq->pool->worklist, NULL);
@@ -984,6 +983,14 @@ static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
 	cwq->nr_active++;
 }
 
+static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
+{
+	struct work_struct *work = list_first_entry(&cwq->delayed_works,
+						    struct work_struct, entry);
+
+	cwq_activate_delayed_work(work);
+}
+
 /**
  * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
  * @cwq: cwq of interest
@@ -1102,6 +1109,19 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
 		smp_rmb();
 		if (gcwq == get_work_gcwq(work)) {
 			debug_work_deactivate(work);
+
+			/*
+			 * We cannot remove delayed work directly.
+			 * Otherwise we may leave some LINKED
+			 * tagalong(if exist) in the ->delayed_works,
+			 * and future cwq_activate_first_delayed() will
+			 * move this tagalong works((which are all NO_COLOR)
+			 * to cwq->pool and increase the ->nr_active,
+			 * and it may cause the whole cwq frozen.
+			 */
+			if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
+				cwq_activate_delayed_work(work);
+
 			list_del_init(&work->entry);
 			cwq_dec_nr_in_flight(get_work_cwq(work),
 				get_work_color(work),
-- 
1.7.4.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [V2 for-next PATCH 2/2] workqueue: remove @delayed argument from cwq_dec_nr_in_flight()
  2012-09-18  5:57 [V2 -stable PATCH 1/2] workqueue: fix leak of active Lai Jiangshan
@ 2012-09-18  5:57 ` Lai Jiangshan
  2012-09-18 17:41   ` Tejun Heo
  0 siblings, 1 reply; 3+ messages in thread
From: Lai Jiangshan @ 2012-09-18  5:57 UTC (permalink / raw)
  To: Tejun Heo, linux-kernel; +Cc: Lai Jiangshan

The argument @delayed is always false in all call site,
we simply remove it.

Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
---
 kernel/workqueue.c |   21 ++++++++-------------
 1 files changed, 8 insertions(+), 13 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 834aa62..d15d383 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -995,7 +995,6 @@ static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
  * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
  * @cwq: cwq of interest
  * @color: color of work which left the queue
- * @delayed: for a delayed work
  *
  * A work either has completed or is removed from pending queue,
  * decrement nr_in_flight of its cwq and handle workqueue flushing.
@@ -1003,8 +1002,7 @@ static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
  * CONTEXT:
  * spin_lock_irq(gcwq->lock).
  */
-static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
-				 bool delayed)
+static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
 {
 	/* ignore uncolored works */
 	if (color == WORK_NO_COLOR)
@@ -1012,13 +1010,11 @@ static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
 
 	cwq->nr_in_flight[color]--;
 
-	if (!delayed) {
-		cwq->nr_active--;
-		if (!list_empty(&cwq->delayed_works)) {
-			/* one down, submit a delayed one */
-			if (cwq->nr_active < cwq->max_active)
-				cwq_activate_first_delayed(cwq);
-		}
+	cwq->nr_active--;
+	if (!list_empty(&cwq->delayed_works)) {
+		/* one down, submit a delayed one */
+		if (cwq->nr_active < cwq->max_active)
+			cwq_activate_first_delayed(cwq);
 	}
 
 	/* is flush in progress and are we at the flushing tip? */
@@ -1124,8 +1120,7 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
 
 			list_del_init(&work->entry);
 			cwq_dec_nr_in_flight(get_work_cwq(work),
-				get_work_color(work),
-				*work_data_bits(work) & WORK_STRUCT_DELAYED);
+				get_work_color(work));
 
 			spin_unlock(&gcwq->lock);
 			return 1;
@@ -2336,7 +2331,7 @@ __acquires(&gcwq->lock)
 	hlist_del_init(&worker->hentry);
 	worker->current_work = NULL;
 	worker->current_cwq = NULL;
-	cwq_dec_nr_in_flight(cwq, work_color, false);
+	cwq_dec_nr_in_flight(cwq, work_color);
 }
 
 /**
-- 
1.7.4.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [V2 for-next PATCH 2/2] workqueue: remove @delayed argument from cwq_dec_nr_in_flight()
  2012-09-18  5:57 ` [V2 for-next PATCH 2/2] workqueue: remove @delayed argument from cwq_dec_nr_in_flight() Lai Jiangshan
@ 2012-09-18 17:41   ` Tejun Heo
  0 siblings, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2012-09-18 17:41 UTC (permalink / raw)
  To: Lai Jiangshan; +Cc: linux-kernel

On Tue, Sep 18, 2012 at 01:57:34PM +0800, Lai Jiangshan wrote:
> The argument @delayed is always false in all call site,
> we simply remove it.
> 
> Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>

Applied 1-2 to wq/for-3.7 with comment / description updates.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-09-18 17:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-18  5:57 [V2 -stable PATCH 1/2] workqueue: fix leak of active Lai Jiangshan
2012-09-18  5:57 ` [V2 for-next PATCH 2/2] workqueue: remove @delayed argument from cwq_dec_nr_in_flight() Lai Jiangshan
2012-09-18 17:41   ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox