diff options
Diffstat (limited to 'kernel/task_work.c')
-rw-r--r-- | kernel/task_work.c | 94 |
1 files changed, 45 insertions, 49 deletions
diff --git a/kernel/task_work.c b/kernel/task_work.c index 82d1c794066d..91d4e1742a0c 100644 --- a/kernel/task_work.c +++ b/kernel/task_work.c | |||
@@ -3,82 +3,78 @@ | |||
3 | #include <linux/tracehook.h> | 3 | #include <linux/tracehook.h> |
4 | 4 | ||
5 | int | 5 | int |
6 | task_work_add(struct task_struct *task, struct task_work *twork, bool notify) | 6 | task_work_add(struct task_struct *task, struct callback_head *twork, bool notify) |
7 | { | 7 | { |
8 | struct callback_head *last, *first; | ||
8 | unsigned long flags; | 9 | unsigned long flags; |
9 | int err = -ESRCH; | ||
10 | 10 | ||
11 | #ifndef TIF_NOTIFY_RESUME | ||
12 | if (notify) | ||
13 | return -ENOTSUPP; | ||
14 | #endif | ||
15 | /* | 11 | /* |
16 | * We must not insert the new work if the task has already passed | 12 | * Not inserting the new work if the task has already passed |
17 | * exit_task_work(). We rely on do_exit()->raw_spin_unlock_wait() | 13 | * exit_task_work() is the responisbility of callers. |
18 | * and check PF_EXITING under pi_lock. | ||
19 | */ | 14 | */ |
20 | raw_spin_lock_irqsave(&task->pi_lock, flags); | 15 | raw_spin_lock_irqsave(&task->pi_lock, flags); |
21 | if (likely(!(task->flags & PF_EXITING))) { | 16 | last = task->task_works; |
22 | hlist_add_head(&twork->hlist, &task->task_works); | 17 | first = last ? last->next : twork; |
23 | err = 0; | 18 | twork->next = first; |
24 | } | 19 | if (last) |
20 | last->next = twork; | ||
21 | task->task_works = twork; | ||
25 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); | 22 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
26 | 23 | ||
27 | /* test_and_set_bit() implies mb(), see tracehook_notify_resume(). */ | 24 | /* test_and_set_bit() implies mb(), see tracehook_notify_resume(). */ |
28 | if (likely(!err) && notify) | 25 | if (notify) |
29 | set_notify_resume(task); | 26 | set_notify_resume(task); |
30 | return err; | 27 | return 0; |
31 | } | 28 | } |
32 | 29 | ||
33 | struct task_work * | 30 | struct callback_head * |
34 | task_work_cancel(struct task_struct *task, task_work_func_t func) | 31 | task_work_cancel(struct task_struct *task, task_work_func_t func) |
35 | { | 32 | { |
36 | unsigned long flags; | 33 | unsigned long flags; |
37 | struct task_work *twork; | 34 | struct callback_head *last, *res = NULL; |
38 | struct hlist_node *pos; | ||
39 | 35 | ||
40 | raw_spin_lock_irqsave(&task->pi_lock, flags); | 36 | raw_spin_lock_irqsave(&task->pi_lock, flags); |
41 | hlist_for_each_entry(twork, pos, &task->task_works, hlist) { | 37 | last = task->task_works; |
42 | if (twork->func == func) { | 38 | if (last) { |
43 | hlist_del(&twork->hlist); | 39 | struct callback_head *q = last, *p = q->next; |
44 | goto found; | 40 | while (1) { |
41 | if (p->func == func) { | ||
42 | q->next = p->next; | ||
43 | if (p == last) | ||
44 | task->task_works = q == p ? NULL : q; | ||
45 | res = p; | ||
46 | break; | ||
47 | } | ||
48 | if (p == last) | ||
49 | break; | ||
50 | q = p; | ||
51 | p = q->next; | ||
45 | } | 52 | } |
46 | } | 53 | } |
47 | twork = NULL; | ||
48 | found: | ||
49 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); | 54 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
50 | 55 | return res; | |
51 | return twork; | ||
52 | } | 56 | } |
53 | 57 | ||
54 | void task_work_run(void) | 58 | void task_work_run(void) |
55 | { | 59 | { |
56 | struct task_struct *task = current; | 60 | struct task_struct *task = current; |
57 | struct hlist_head task_works; | 61 | struct callback_head *p, *q; |
58 | struct hlist_node *pos; | ||
59 | 62 | ||
60 | raw_spin_lock_irq(&task->pi_lock); | 63 | while (1) { |
61 | hlist_move_list(&task->task_works, &task_works); | 64 | raw_spin_lock_irq(&task->pi_lock); |
62 | raw_spin_unlock_irq(&task->pi_lock); | 65 | p = task->task_works; |
66 | task->task_works = NULL; | ||
67 | raw_spin_unlock_irq(&task->pi_lock); | ||
63 | 68 | ||
64 | if (unlikely(hlist_empty(&task_works))) | 69 | if (unlikely(!p)) |
65 | return; | 70 | return; |
66 | /* | ||
67 | * We use hlist to save the space in task_struct, but we want fifo. | ||
68 | * Find the last entry, the list should be short, then process them | ||
69 | * in reverse order. | ||
70 | */ | ||
71 | for (pos = task_works.first; pos->next; pos = pos->next) | ||
72 | ; | ||
73 | 71 | ||
74 | for (;;) { | 72 | q = p->next; /* head */ |
75 | struct hlist_node **pprev = pos->pprev; | 73 | p->next = NULL; /* cut it */ |
76 | struct task_work *twork = container_of(pos, struct task_work, | 74 | while (q) { |
77 | hlist); | 75 | p = q->next; |
78 | twork->func(twork); | 76 | q->func(q); |
79 | 77 | q = p; | |
80 | if (pprev == &task_works.first) | 78 | } |
81 | break; | ||
82 | pos = container_of(pprev, struct hlist_node, next); | ||
83 | } | 79 | } |
84 | } | 80 | } |