Skip to content

Commit 77668c8

Browse files
Lai Jiangshanhtejun
authored andcommitted
workqueue: fix a possible race condition between rescuer and pwq-release
There is a race condition between rescuer_thread() and pwq_unbound_release_workfn(). Even after a pwq is scheduled for rescue, the associated work items may be consumed by any worker. If all of them are consumed before the rescuer gets to them and the pwq's base ref was put due to attribute change, the pwq may be released while still being linked on @wq->maydays list making the rescuer dereference already freed pwq later. Make send_mayday() pin the target pwq until the rescuer is done with it. tj: Updated comment and patch description. Signed-off-by: Lai Jiangshan <[email protected]> Signed-off-by: Tejun Heo <[email protected]> Cc: [email protected] # v3.10+
1 parent 4d595b8 commit 77668c8

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

kernel/workqueue.c

Lines changed: 12 additions & 0 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
}
@@ -2449,6 +2455,12 @@ static int rescuer_thread(void *__rescuer)
24492455

24502456
process_scheduled_works(rescuer);
24512457

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+
24522464
/*
24532465
* Leave this pool. If keep_working() is %true, notify a
24542466
* regular worker; otherwise, we end up with 0 concurrency

0 commit comments

Comments
 (0)