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 |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 6 | task_work_add(struct task_struct *task, struct callback_head *work, bool notify) |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 7 | { |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 8 | struct callback_head *head; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 9 | /* |
Al Viro | ed3e694 | 2012-06-27 11:31:24 +0400 | [diff] [blame] | 10 | * Not inserting the new work if the task has already passed |
| 11 | * exit_task_work() is the responisbility of callers. |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 12 | */ |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 13 | do { |
| 14 | head = ACCESS_ONCE(task->task_works); |
| 15 | work->next = head; |
| 16 | } while (cmpxchg(&task->task_works, head, work) != head); |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 17 | |
Al Viro | ed3e694 | 2012-06-27 11:31:24 +0400 | [diff] [blame] | 18 | if (notify) |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 19 | set_notify_resume(task); |
Al Viro | ed3e694 | 2012-06-27 11:31:24 +0400 | [diff] [blame] | 20 | return 0; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 21 | } |
| 22 | |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 23 | struct callback_head * |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 24 | task_work_cancel(struct task_struct *task, task_work_func_t func) |
| 25 | { |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 26 | struct callback_head **pprev = &task->task_works; |
| 27 | struct callback_head *work = NULL; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 28 | unsigned long flags; |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 29 | /* |
| 30 | * If cmpxchg() fails we continue without updating pprev. |
| 31 | * Either we raced with task_work_add() which added the |
| 32 | * new entry before this work, we will find it again. Or |
| 33 | * we raced with task_work_run(), *pprev == NULL. |
| 34 | */ |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 35 | raw_spin_lock_irqsave(&task->pi_lock, flags); |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 36 | while ((work = ACCESS_ONCE(*pprev))) { |
| 37 | read_barrier_depends(); |
| 38 | if (work->func != func) |
| 39 | pprev = &work->next; |
| 40 | else if (cmpxchg(pprev, work, work->next) == work) |
| 41 | break; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 42 | } |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 43 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 44 | |
| 45 | return work; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void task_work_run(void) |
| 49 | { |
| 50 | struct task_struct *task = current; |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 51 | struct callback_head *work, *head, *next; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 52 | |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 53 | for (;;) { |
| 54 | work = xchg(&task->task_works, NULL); |
| 55 | if (!work) |
| 56 | break; |
| 57 | /* |
| 58 | * Synchronize with task_work_cancel(). It can't remove |
| 59 | * the first entry == work, cmpxchg(task_works) should |
| 60 | * fail, but it can play with *work and other entries. |
| 61 | */ |
| 62 | raw_spin_unlock_wait(&task->pi_lock); |
| 63 | smp_mb(); |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 64 | |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 65 | /* Reverse the list to run the works in fifo order */ |
| 66 | head = NULL; |
| 67 | do { |
| 68 | next = work->next; |
| 69 | work->next = head; |
| 70 | head = work; |
| 71 | work = next; |
| 72 | } while (work); |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 73 | |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 74 | work = head; |
| 75 | do { |
| 76 | next = work->next; |
| 77 | work->func(work); |
| 78 | work = next; |
Eric Dumazet | f341861 | 2012-08-21 15:05:14 +0200 | [diff] [blame] | 79 | cond_resched(); |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame^] | 80 | } while (work); |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 81 | } |
| 82 | } |