blob: 9b8948dbdc605b1ac127af78dc858760f13cd45a [file] [log] [blame]
Oleg Nesterove73f8952012-05-11 10:59:07 +10001#include <linux/spinlock.h>
2#include <linux/task_work.h>
3#include <linux/tracehook.h>
4
5int
6task_work_add(struct task_struct *task, struct task_work *twork, bool notify)
7{
8 unsigned long flags;
9 int err = -ESRCH;
10
11#ifndef TIF_NOTIFY_RESUME
12 if (notify)
13 return -ENOTSUPP;
14#endif
15 /*
16 * We must not insert the new work if the task has already passed
17 * exit_task_work(). We rely on do_exit()->raw_spin_unlock_wait()
18 * and check PF_EXITING under pi_lock.
19 */
20 raw_spin_lock_irqsave(&task->pi_lock, flags);
21 if (likely(!(task->flags & PF_EXITING))) {
Al Viro158e1642012-06-27 09:24:13 +040022 struct task_work *last = task->task_works;
23 struct task_work *first = last ? last->next : twork;
24 twork->next = first;
25 if (last)
26 last->next = twork;
27 task->task_works = twork;
Oleg Nesterove73f8952012-05-11 10:59:07 +100028 err = 0;
29 }
30 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
31
32 /* test_and_set_bit() implies mb(), see tracehook_notify_resume(). */
33 if (likely(!err) && notify)
34 set_notify_resume(task);
35 return err;
36}
37
38struct task_work *
39task_work_cancel(struct task_struct *task, task_work_func_t func)
40{
41 unsigned long flags;
Al Viro158e1642012-06-27 09:24:13 +040042 struct task_work *last, *res = NULL;
Oleg Nesterove73f8952012-05-11 10:59:07 +100043
44 raw_spin_lock_irqsave(&task->pi_lock, flags);
Al Viro158e1642012-06-27 09:24:13 +040045 last = task->task_works;
46 if (last) {
47 struct task_work *q = last, *p = q->next;
48 while (1) {
49 if (p->func == func) {
50 q->next = p->next;
51 if (p == last)
52 task->task_works = q == p ? NULL : q;
53 res = p;
54 break;
55 }
56 if (p == last)
57 break;
58 q = p;
59 p = q->next;
Oleg Nesterove73f8952012-05-11 10:59:07 +100060 }
61 }
Oleg Nesterove73f8952012-05-11 10:59:07 +100062 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Al Viro158e1642012-06-27 09:24:13 +040063 return res;
Oleg Nesterove73f8952012-05-11 10:59:07 +100064}
65
66void task_work_run(void)
67{
68 struct task_struct *task = current;
Al Viro158e1642012-06-27 09:24:13 +040069 struct task_work *p, *q;
Oleg Nesterove73f8952012-05-11 10:59:07 +100070
71 raw_spin_lock_irq(&task->pi_lock);
Al Viro158e1642012-06-27 09:24:13 +040072 p = task->task_works;
73 task->task_works = NULL;
Oleg Nesterove73f8952012-05-11 10:59:07 +100074 raw_spin_unlock_irq(&task->pi_lock);
75
Al Viro158e1642012-06-27 09:24:13 +040076 if (unlikely(!p))
Oleg Nesterove73f8952012-05-11 10:59:07 +100077 return;
Oleg Nesterove73f8952012-05-11 10:59:07 +100078
Al Viro158e1642012-06-27 09:24:13 +040079 q = p->next; /* head */
80 p->next = NULL; /* cut it */
81 while (q) {
82 p = q->next;
83 q->func(q);
84 q = p;
Oleg Nesterove73f8952012-05-11 10:59:07 +100085 }
86}