Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 1 | #include <linux/spinlock.h> |
| 2 | #include <linux/task_work.h> |
| 3 | #include <linux/tracehook.h> |
| 4 | |
| 5 | int |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 6 | task_work_add(struct task_struct *task, struct callback_head *twork, bool notify) |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 7 | { |
Al Viro | ed3e694 | 2012-06-27 11:31:24 +0400 | [diff] [blame] | 8 | struct callback_head *last, *first; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 9 | unsigned long flags; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 10 | |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 11 | /* |
Al Viro | ed3e694 | 2012-06-27 11:31:24 +0400 | [diff] [blame] | 12 | * Not inserting the new work if the task has already passed |
| 13 | * exit_task_work() is the responisbility of callers. |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 14 | */ |
| 15 | raw_spin_lock_irqsave(&task->pi_lock, flags); |
Al Viro | ed3e694 | 2012-06-27 11:31:24 +0400 | [diff] [blame] | 16 | last = task->task_works; |
| 17 | first = last ? last->next : twork; |
| 18 | twork->next = first; |
| 19 | if (last) |
| 20 | last->next = twork; |
| 21 | task->task_works = twork; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 22 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
| 23 | |
| 24 | /* test_and_set_bit() implies mb(), see tracehook_notify_resume(). */ |
Al Viro | ed3e694 | 2012-06-27 11:31:24 +0400 | [diff] [blame] | 25 | if (notify) |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 26 | set_notify_resume(task); |
Al Viro | ed3e694 | 2012-06-27 11:31:24 +0400 | [diff] [blame] | 27 | return 0; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 28 | } |
| 29 | |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 30 | struct callback_head * |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 31 | task_work_cancel(struct task_struct *task, task_work_func_t func) |
| 32 | { |
| 33 | unsigned long flags; |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 34 | struct callback_head *last, *res = NULL; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 35 | |
| 36 | raw_spin_lock_irqsave(&task->pi_lock, flags); |
Al Viro | 158e164 | 2012-06-27 09:24:13 +0400 | [diff] [blame] | 37 | last = task->task_works; |
| 38 | if (last) { |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 39 | struct callback_head *q = last, *p = q->next; |
Al Viro | 158e164 | 2012-06-27 09:24:13 +0400 | [diff] [blame] | 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; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 52 | } |
| 53 | } |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 54 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
Al Viro | 158e164 | 2012-06-27 09:24:13 +0400 | [diff] [blame] | 55 | return res; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void task_work_run(void) |
| 59 | { |
| 60 | struct task_struct *task = current; |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 61 | struct callback_head *p, *q; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 62 | |
Al Viro | a2d4c71 | 2012-06-27 11:33:29 +0400 | [diff] [blame] | 63 | while (1) { |
| 64 | raw_spin_lock_irq(&task->pi_lock); |
| 65 | p = task->task_works; |
| 66 | task->task_works = NULL; |
| 67 | raw_spin_unlock_irq(&task->pi_lock); |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 68 | |
Al Viro | a2d4c71 | 2012-06-27 11:33:29 +0400 | [diff] [blame] | 69 | if (unlikely(!p)) |
| 70 | return; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 71 | |
Al Viro | a2d4c71 | 2012-06-27 11:33:29 +0400 | [diff] [blame] | 72 | q = p->next; /* head */ |
| 73 | p->next = NULL; /* cut it */ |
| 74 | while (q) { |
| 75 | p = q->next; |
| 76 | q->func(q); |
| 77 | q = p; |
Eric Dumazet | f341861 | 2012-08-21 15:05:14 +0200 | [diff] [blame] | 78 | cond_resched(); |
Al Viro | a2d4c71 | 2012-06-27 11:33:29 +0400 | [diff] [blame] | 79 | } |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 80 | } |
| 81 | } |