1 #include <linux/spinlock.h>
2 #include <linux/task_work.h>
3 #include <linux/tracehook.h>
5 static struct callback_head work_exited
; /* all we need is ->next == NULL */
8 task_work_add(struct task_struct
*task
, struct callback_head
*work
, bool notify
)
10 struct callback_head
*head
;
13 head
= ACCESS_ONCE(task
->task_works
);
14 if (unlikely(head
== &work_exited
))
17 } while (cmpxchg(&task
->task_works
, head
, work
) != head
);
20 set_notify_resume(task
);
24 struct callback_head
*
25 task_work_cancel(struct task_struct
*task
, task_work_func_t func
)
27 struct callback_head
**pprev
= &task
->task_works
;
28 struct callback_head
*work
= NULL
;
31 * If cmpxchg() fails we continue without updating pprev.
32 * Either we raced with task_work_add() which added the
33 * new entry before this work, we will find it again. Or
34 * we raced with task_work_run(), *pprev == NULL/exited.
36 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
37 while ((work
= ACCESS_ONCE(*pprev
))) {
38 read_barrier_depends();
39 if (work
->func
!= func
)
41 else if (cmpxchg(pprev
, work
, work
->next
) == work
)
44 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
49 void task_work_run(void)
51 struct task_struct
*task
= current
;
52 struct callback_head
*work
, *head
, *next
;
56 * work->func() can do task_work_add(), do not set
57 * work_exited unless the list is empty.
60 work
= ACCESS_ONCE(task
->task_works
);
61 head
= !work
&& (task
->flags
& PF_EXITING
) ?
63 } while (cmpxchg(&task
->task_works
, work
, head
) != work
);
68 * Synchronize with task_work_cancel(). It can't remove
69 * the first entry == work, cmpxchg(task_works) should
70 * fail, but it can play with *work and other entries.
72 raw_spin_unlock_wait(&task
->pi_lock
);
75 /* Reverse the list to run the works in fifo order */