Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 2 | #include <linux/spinlock.h> |
| 3 | #include <linux/task_work.h> |
| 4 | #include <linux/tracehook.h> |
| 5 | |
Oleg Nesterov | 9da33de | 2012-08-26 21:12:11 +0200 | [diff] [blame] | 6 | static struct callback_head work_exited; /* all we need is ->next == NULL */ |
| 7 | |
Oleg Nesterov | 892f666 | 2013-09-11 14:23:31 -0700 | [diff] [blame] | 8 | /** |
| 9 | * task_work_add - ask the @task to execute @work->func() |
| 10 | * @task: the task which should run the callback |
| 11 | * @work: the callback to run |
| 12 | * @notify: send the notification if true |
| 13 | * |
| 14 | * Queue @work for task_work_run() below and notify the @task if @notify. |
| 15 | * Fails if the @task is exiting/exited and thus it can't process this @work. |
| 16 | * Otherwise @work->func() will be called when the @task returns from kernel |
| 17 | * mode or exits. |
| 18 | * |
| 19 | * This is like the signal handler which runs in kernel mode, but it doesn't |
| 20 | * try to wake up the @task. |
| 21 | * |
Eric Dumazet | c821990 | 2015-08-28 19:42:30 -0700 | [diff] [blame] | 22 | * Note: there is no ordering guarantee on works queued here. |
| 23 | * |
Oleg Nesterov | 892f666 | 2013-09-11 14:23:31 -0700 | [diff] [blame] | 24 | * RETURNS: |
| 25 | * 0 if succeeds or -ESRCH. |
| 26 | */ |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 27 | int |
Oleg Nesterov | e91b481 | 2020-06-30 17:32:54 +0200 | [diff] [blame] | 28 | task_work_add(struct task_struct *task, struct callback_head *work, int notify) |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 29 | { |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 30 | struct callback_head *head; |
Oleg Nesterov | e91b481 | 2020-06-30 17:32:54 +0200 | [diff] [blame] | 31 | unsigned long flags; |
Oleg Nesterov | 9da33de | 2012-08-26 21:12:11 +0200 | [diff] [blame] | 32 | |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 33 | do { |
Oleg Nesterov | 61e9649 | 2016-08-02 14:03:44 -0700 | [diff] [blame] | 34 | head = READ_ONCE(task->task_works); |
Oleg Nesterov | 9da33de | 2012-08-26 21:12:11 +0200 | [diff] [blame] | 35 | if (unlikely(head == &work_exited)) |
| 36 | return -ESRCH; |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 37 | work->next = head; |
| 38 | } while (cmpxchg(&task->task_works, head, work) != head); |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 39 | |
Oleg Nesterov | e91b481 | 2020-06-30 17:32:54 +0200 | [diff] [blame] | 40 | switch (notify) { |
| 41 | case TWA_RESUME: |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 42 | set_notify_resume(task); |
Oleg Nesterov | e91b481 | 2020-06-30 17:32:54 +0200 | [diff] [blame] | 43 | break; |
| 44 | case TWA_SIGNAL: |
Jens Axboe | ebf0d100d | 2020-08-13 09:01:38 -0600 | [diff] [blame] | 45 | /* |
| 46 | * Only grab the sighand lock if we don't already have some |
| 47 | * task_work pending. This pairs with the smp_store_mb() |
| 48 | * in get_signal(), see comment there. |
| 49 | */ |
| 50 | if (!(READ_ONCE(task->jobctl) & JOBCTL_TASK_WORK) && |
| 51 | lock_task_sighand(task, &flags)) { |
Oleg Nesterov | e91b481 | 2020-06-30 17:32:54 +0200 | [diff] [blame] | 52 | task->jobctl |= JOBCTL_TASK_WORK; |
| 53 | signal_wake_up(task, 0); |
| 54 | unlock_task_sighand(task, &flags); |
| 55 | } |
| 56 | break; |
| 57 | } |
| 58 | |
Al Viro | ed3e694 | 2012-06-27 11:31:24 +0400 | [diff] [blame] | 59 | return 0; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 60 | } |
| 61 | |
Oleg Nesterov | 892f666 | 2013-09-11 14:23:31 -0700 | [diff] [blame] | 62 | /** |
| 63 | * task_work_cancel - cancel a pending work added by task_work_add() |
| 64 | * @task: the task which should execute the work |
| 65 | * @func: identifies the work to remove |
| 66 | * |
| 67 | * Find the last queued pending work with ->func == @func and remove |
| 68 | * it from queue. |
| 69 | * |
| 70 | * RETURNS: |
| 71 | * The found work or NULL if not found. |
| 72 | */ |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 73 | struct callback_head * |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 74 | task_work_cancel(struct task_struct *task, task_work_func_t func) |
| 75 | { |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 76 | struct callback_head **pprev = &task->task_works; |
Oleg Nesterov | 205e550 | 2013-09-11 14:23:30 -0700 | [diff] [blame] | 77 | struct callback_head *work; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 78 | unsigned long flags; |
Oleg Nesterov | 61e9649 | 2016-08-02 14:03:44 -0700 | [diff] [blame] | 79 | |
| 80 | if (likely(!task->task_works)) |
| 81 | return NULL; |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 82 | /* |
| 83 | * If cmpxchg() fails we continue without updating pprev. |
| 84 | * Either we raced with task_work_add() which added the |
| 85 | * new entry before this work, we will find it again. Or |
Oleg Nesterov | 9da33de | 2012-08-26 21:12:11 +0200 | [diff] [blame] | 86 | * we raced with task_work_run(), *pprev == NULL/exited. |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 87 | */ |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 88 | raw_spin_lock_irqsave(&task->pi_lock, flags); |
Will Deacon | 506458e | 2017-10-24 11:22:48 +0100 | [diff] [blame] | 89 | while ((work = READ_ONCE(*pprev))) { |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 90 | if (work->func != func) |
| 91 | pprev = &work->next; |
| 92 | else if (cmpxchg(pprev, work, work->next) == work) |
| 93 | break; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 94 | } |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 95 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 96 | |
| 97 | return work; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 98 | } |
| 99 | |
Oleg Nesterov | 892f666 | 2013-09-11 14:23:31 -0700 | [diff] [blame] | 100 | /** |
| 101 | * task_work_run - execute the works added by task_work_add() |
| 102 | * |
| 103 | * Flush the pending works. Should be used by the core kernel code. |
| 104 | * Called before the task returns to the user-mode or stops, or when |
| 105 | * it exits. In the latter case task_work_add() can no longer add the |
| 106 | * new work after task_work_run() returns. |
| 107 | */ |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 108 | void task_work_run(void) |
| 109 | { |
| 110 | struct task_struct *task = current; |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 111 | struct callback_head *work, *head, *next; |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 112 | |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 113 | for (;;) { |
Oleg Nesterov | 9da33de | 2012-08-26 21:12:11 +0200 | [diff] [blame] | 114 | /* |
| 115 | * work->func() can do task_work_add(), do not set |
| 116 | * work_exited unless the list is empty. |
| 117 | */ |
| 118 | do { |
Oleg Nesterov | 6fb6149 | 2020-02-18 16:50:18 +0100 | [diff] [blame] | 119 | head = NULL; |
Oleg Nesterov | 61e9649 | 2016-08-02 14:03:44 -0700 | [diff] [blame] | 120 | work = READ_ONCE(task->task_works); |
Oleg Nesterov | 6fb6149 | 2020-02-18 16:50:18 +0100 | [diff] [blame] | 121 | if (!work) { |
| 122 | if (task->flags & PF_EXITING) |
| 123 | head = &work_exited; |
| 124 | else |
| 125 | break; |
| 126 | } |
Oleg Nesterov | 9da33de | 2012-08-26 21:12:11 +0200 | [diff] [blame] | 127 | } while (cmpxchg(&task->task_works, work, head) != work); |
| 128 | |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 129 | if (!work) |
| 130 | break; |
Oleg Nesterov | 6fb6149 | 2020-02-18 16:50:18 +0100 | [diff] [blame] | 131 | /* |
| 132 | * Synchronize with task_work_cancel(). It can not remove |
| 133 | * the first entry == work, cmpxchg(task_works) must fail. |
| 134 | * But it can remove another entry from the ->next list. |
| 135 | */ |
| 136 | raw_spin_lock_irq(&task->pi_lock); |
| 137 | raw_spin_unlock_irq(&task->pi_lock); |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 138 | |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 139 | do { |
| 140 | next = work->next; |
| 141 | work->func(work); |
| 142 | work = next; |
Eric Dumazet | f341861 | 2012-08-21 15:05:14 +0200 | [diff] [blame] | 143 | cond_resched(); |
Oleg Nesterov | ac3d0da | 2012-08-26 21:12:09 +0200 | [diff] [blame] | 144 | } while (work); |
Oleg Nesterov | e73f895 | 2012-05-11 10:59:07 +1000 | [diff] [blame] | 145 | } |
| 146 | } |