Commit 61e96496 authored by Oleg Nesterov's avatar Oleg Nesterov Committed by Linus Torvalds

task_work: use READ_ONCE/lockless_dereference, avoid pi_lock if !task_works

Change task_work_cancel() to use lockless_dereference(), this is what
the code really wants but we didn't have this helper when it was
written.

Also add the fast-path task->task_works == NULL check, in the likely
case this task has no pending works and we can avoid
spin_lock(task->pi_lock).

While at it, change other users of ACCESS_ONCE() to use READ_ONCE().

Link: http://lkml.kernel.org/r/20160610150042.GA13868@redhat.comSigned-off-by: default avatarOleg Nesterov <oleg@redhat.com>
Cc: Andrea Parri <parri.andrea@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 949bed2f
...@@ -29,7 +29,7 @@ task_work_add(struct task_struct *task, struct callback_head *work, bool notify) ...@@ -29,7 +29,7 @@ task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
struct callback_head *head; struct callback_head *head;
do { do {
head = ACCESS_ONCE(task->task_works); head = READ_ONCE(task->task_works);
if (unlikely(head == &work_exited)) if (unlikely(head == &work_exited))
return -ESRCH; return -ESRCH;
work->next = head; work->next = head;
...@@ -57,6 +57,9 @@ task_work_cancel(struct task_struct *task, task_work_func_t func) ...@@ -57,6 +57,9 @@ task_work_cancel(struct task_struct *task, task_work_func_t func)
struct callback_head **pprev = &task->task_works; struct callback_head **pprev = &task->task_works;
struct callback_head *work; struct callback_head *work;
unsigned long flags; unsigned long flags;
if (likely(!task->task_works))
return NULL;
/* /*
* If cmpxchg() fails we continue without updating pprev. * If cmpxchg() fails we continue without updating pprev.
* Either we raced with task_work_add() which added the * Either we raced with task_work_add() which added the
...@@ -64,8 +67,7 @@ task_work_cancel(struct task_struct *task, task_work_func_t func) ...@@ -64,8 +67,7 @@ task_work_cancel(struct task_struct *task, task_work_func_t func)
* we raced with task_work_run(), *pprev == NULL/exited. * we raced with task_work_run(), *pprev == NULL/exited.
*/ */
raw_spin_lock_irqsave(&task->pi_lock, flags); raw_spin_lock_irqsave(&task->pi_lock, flags);
while ((work = ACCESS_ONCE(*pprev))) { while ((work = lockless_dereference(*pprev))) {
smp_read_barrier_depends();
if (work->func != func) if (work->func != func)
pprev = &work->next; pprev = &work->next;
else if (cmpxchg(pprev, work, work->next) == work) else if (cmpxchg(pprev, work, work->next) == work)
...@@ -95,7 +97,7 @@ void task_work_run(void) ...@@ -95,7 +97,7 @@ void task_work_run(void)
* work_exited unless the list is empty. * work_exited unless the list is empty.
*/ */
do { do {
work = ACCESS_ONCE(task->task_works); work = READ_ONCE(task->task_works);
head = !work && (task->flags & PF_EXITING) ? head = !work && (task->flags & PF_EXITING) ?
&work_exited : NULL; &work_exited : NULL;
} while (cmpxchg(&task->task_works, work, head) != work); } while (cmpxchg(&task->task_works, work, head) != work);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment