Skip to content

Commit efb2b1d

Browse files
committed
Merge branch 'for-3.15-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
Pull workqueue fixes from Tejun Heo: "Fixes for two bugs in workqueue. One is exiting with internal mutex held in a failure path of wq_update_unbound_numa(). The other is a subtle and unlikely use-after-possible-last-put in the rescuer logic. Both have been around for quite some time now and are unlikely to have triggered noticeably often. All patches are marked for -stable backport" * 'for-3.15-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq: workqueue: fix a possible race condition between rescuer and pwq-release workqueue: make rescuer_thread() empty wq->maydays list before exiting workqueue: fix bugs in wq_update_unbound_numa() failure path
2 parents 26a41cd + 77668c8 commit efb2b1d

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

kernel/workqueue.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,12 @@ static void send_mayday(struct work_struct *work)
19161916

19171917
/* mayday mayday mayday */
19181918
if (list_empty(&pwq->mayday_node)) {
1919+
/*
1920+
* If @pwq is for an unbound wq, its base ref may be put at
1921+
* any time due to an attribute change. Pin @pwq until the
1922+
* rescuer is done with it.
1923+
*/
1924+
get_pwq(pwq);
19191925
list_add_tail(&pwq->mayday_node, &wq->maydays);
19201926
wake_up_process(wq->rescuer->task);
19211927
}
@@ -2398,6 +2404,7 @@ static int rescuer_thread(void *__rescuer)
23982404
struct worker *rescuer = __rescuer;
23992405
struct workqueue_struct *wq = rescuer->rescue_wq;
24002406
struct list_head *scheduled = &rescuer->scheduled;
2407+
bool should_stop;
24012408

24022409
set_user_nice(current, RESCUER_NICE_LEVEL);
24032410

@@ -2409,11 +2416,15 @@ static int rescuer_thread(void *__rescuer)
24092416
repeat:
24102417
set_current_state(TASK_INTERRUPTIBLE);
24112418

2412-
if (kthread_should_stop()) {
2413-
__set_current_state(TASK_RUNNING);
2414-
rescuer->task->flags &= ~PF_WQ_WORKER;
2415-
return 0;
2416-
}
2419+
/*
2420+
* By the time the rescuer is requested to stop, the workqueue
2421+
* shouldn't have any work pending, but @wq->maydays may still have
2422+
* pwq(s) queued. This can happen by non-rescuer workers consuming
2423+
* all the work items before the rescuer got to them. Go through
2424+
* @wq->maydays processing before acting on should_stop so that the
2425+
* list is always empty on exit.
2426+
*/
2427+
should_stop = kthread_should_stop();
24172428

24182429
/* see whether any pwq is asking for help */
24192430
spin_lock_irq(&wq_mayday_lock);
@@ -2444,6 +2455,12 @@ static int rescuer_thread(void *__rescuer)
24442455

24452456
process_scheduled_works(rescuer);
24462457

2458+
/*
2459+
* Put the reference grabbed by send_mayday(). @pool won't
2460+
* go away while we're holding its lock.
2461+
*/
2462+
put_pwq(pwq);
2463+
24472464
/*
24482465
* Leave this pool. If keep_working() is %true, notify a
24492466
* regular worker; otherwise, we end up with 0 concurrency
@@ -2459,6 +2476,12 @@ static int rescuer_thread(void *__rescuer)
24592476

24602477
spin_unlock_irq(&wq_mayday_lock);
24612478

2479+
if (should_stop) {
2480+
__set_current_state(TASK_RUNNING);
2481+
rescuer->task->flags &= ~PF_WQ_WORKER;
2482+
return 0;
2483+
}
2484+
24622485
/* rescuers should never participate in concurrency management */
24632486
WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
24642487
schedule();
@@ -4100,7 +4123,8 @@ static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
41004123
if (!pwq) {
41014124
pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
41024125
wq->name);
4103-
goto out_unlock;
4126+
mutex_lock(&wq->mutex);
4127+
goto use_dfl_pwq;
41044128
}
41054129

41064130
/*

0 commit comments

Comments
 (0)