Skip to content

Commit e158488

Browse files
Xie YongjiIngo Molnar
authored andcommitted
locking/rwsem: Fix (possible) missed wakeup
Because wake_q_add() can imply an immediate wakeup (cmpxchg failure case), we must not rely on the wakeup being delayed. However, commit: e385139 ("locking/rwsem: Rework zeroing reader waiter->task") relies on exactly that behaviour in that the wakeup must not happen until after we clear waiter->task. [ peterz: Added changelog. ] Signed-off-by: Xie Yongji <xieyongji@baidu.com> Signed-off-by: Zhang Yu <zhangyu31@baidu.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Fixes: e385139 ("locking/rwsem: Rework zeroing reader waiter->task") Link: https://lkml.kernel.org/r/1543495830-2644-1-git-send-email-xieyongji@baidu.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent b061c38 commit e158488

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

kernel/locking/rwsem-xadd.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,22 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem,
198198
woken++;
199199
tsk = waiter->task;
200200

201-
wake_q_add(wake_q, tsk);
201+
get_task_struct(tsk);
202202
list_del(&waiter->list);
203203
/*
204-
* Ensure that the last operation is setting the reader
204+
* Ensure calling get_task_struct() before setting the reader
205205
* waiter to nil such that rwsem_down_read_failed() cannot
206206
* race with do_exit() by always holding a reference count
207207
* to the task to wakeup.
208208
*/
209209
smp_store_release(&waiter->task, NULL);
210+
/*
211+
* Ensure issuing the wakeup (either by us or someone else)
212+
* after setting the reader waiter to nil.
213+
*/
214+
wake_q_add(wake_q, tsk);
215+
/* wake_q_add() already take the task ref */
216+
put_task_struct(tsk);
210217
}
211218

212219
adjustment = woken * RWSEM_ACTIVE_READ_BIAS - adjustment;

0 commit comments

Comments
 (0)