]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
e73f8959 ON |
2 | #include <linux/spinlock.h> |
3 | #include <linux/task_work.h> | |
4 | #include <linux/tracehook.h> | |
5 | ||
9da33de6 ON |
6 | static struct callback_head work_exited; /* all we need is ->next == NULL */ |
7 | ||
114518eb JA |
8 | /* |
9 | * TWA_SIGNAL signaling - use TIF_NOTIFY_SIGNAL, if available, as it's faster | |
10 | * than TIF_SIGPENDING as there's no dependency on ->sighand. The latter is | |
11 | * shared for threads, and can cause contention on sighand->lock. Even for | |
12 | * the non-threaded case TIF_NOTIFY_SIGNAL is more efficient, as no locking | |
13 | * or IRQ disabling is involved for notification (or running) purposes. | |
14 | */ | |
15 | static void task_work_notify_signal(struct task_struct *task) | |
16 | { | |
17 | #if defined(TIF_NOTIFY_SIGNAL) | |
18 | set_notify_signal(task); | |
19 | #else | |
20 | unsigned long flags; | |
21 | ||
22 | /* | |
23 | * Only grab the sighand lock if we don't already have some | |
24 | * task_work pending. This pairs with the smp_store_mb() | |
25 | * in get_signal(), see comment there. | |
26 | */ | |
27 | if (!(READ_ONCE(task->jobctl) & JOBCTL_TASK_WORK) && | |
28 | lock_task_sighand(task, &flags)) { | |
29 | task->jobctl |= JOBCTL_TASK_WORK; | |
30 | signal_wake_up(task, 0); | |
31 | unlock_task_sighand(task, &flags); | |
32 | } | |
33 | #endif | |
34 | } | |
35 | ||
892f6668 ON |
36 | /** |
37 | * task_work_add - ask the @task to execute @work->func() | |
38 | * @task: the task which should run the callback | |
39 | * @work: the callback to run | |
91989c70 | 40 | * @notify: how to notify the targeted task |
892f6668 | 41 | * |
91989c70 JA |
42 | * Queue @work for task_work_run() below and notify the @task if @notify |
43 | * is @TWA_RESUME or @TWA_SIGNAL. @TWA_SIGNAL works like signals, in that the | |
44 | * it will interrupt the targeted task and run the task_work. @TWA_RESUME | |
45 | * work is run only when the task exits the kernel and returns to user mode, | |
46 | * or before entering guest mode. Fails if the @task is exiting/exited and thus | |
47 | * it can't process this @work. Otherwise @work->func() will be called when the | |
48 | * @task goes through one of the aforementioned transitions, or exits. | |
892f6668 | 49 | * |
91989c70 JA |
50 | * If the targeted task is exiting, then an error is returned and the work item |
51 | * is not queued. It's up to the caller to arrange for an alternative mechanism | |
52 | * in that case. | |
892f6668 | 53 | * |
91989c70 JA |
54 | * Note: there is no ordering guarantee on works queued here. The task_work |
55 | * list is LIFO. | |
c8219906 | 56 | * |
892f6668 ON |
57 | * RETURNS: |
58 | * 0 if succeeds or -ESRCH. | |
59 | */ | |
91989c70 JA |
60 | int task_work_add(struct task_struct *task, struct callback_head *work, |
61 | enum task_work_notify_mode notify) | |
e73f8959 | 62 | { |
ac3d0da8 | 63 | struct callback_head *head; |
9da33de6 | 64 | |
ac3d0da8 | 65 | do { |
61e96496 | 66 | head = READ_ONCE(task->task_works); |
9da33de6 ON |
67 | if (unlikely(head == &work_exited)) |
68 | return -ESRCH; | |
ac3d0da8 ON |
69 | work->next = head; |
70 | } while (cmpxchg(&task->task_works, head, work) != head); | |
e73f8959 | 71 | |
e91b4816 | 72 | switch (notify) { |
91989c70 JA |
73 | case TWA_NONE: |
74 | break; | |
e91b4816 | 75 | case TWA_RESUME: |
e73f8959 | 76 | set_notify_resume(task); |
e91b4816 ON |
77 | break; |
78 | case TWA_SIGNAL: | |
114518eb | 79 | task_work_notify_signal(task); |
e91b4816 | 80 | break; |
91989c70 JA |
81 | default: |
82 | WARN_ON_ONCE(1); | |
83 | break; | |
e91b4816 ON |
84 | } |
85 | ||
ed3e694d | 86 | return 0; |
e73f8959 ON |
87 | } |
88 | ||
892f6668 ON |
89 | /** |
90 | * task_work_cancel - cancel a pending work added by task_work_add() | |
91 | * @task: the task which should execute the work | |
92 | * @func: identifies the work to remove | |
93 | * | |
94 | * Find the last queued pending work with ->func == @func and remove | |
95 | * it from queue. | |
96 | * | |
97 | * RETURNS: | |
98 | * The found work or NULL if not found. | |
99 | */ | |
67d12145 | 100 | struct callback_head * |
e73f8959 ON |
101 | task_work_cancel(struct task_struct *task, task_work_func_t func) |
102 | { | |
ac3d0da8 | 103 | struct callback_head **pprev = &task->task_works; |
205e550a | 104 | struct callback_head *work; |
e73f8959 | 105 | unsigned long flags; |
61e96496 ON |
106 | |
107 | if (likely(!task->task_works)) | |
108 | return NULL; | |
ac3d0da8 ON |
109 | /* |
110 | * If cmpxchg() fails we continue without updating pprev. | |
111 | * Either we raced with task_work_add() which added the | |
112 | * new entry before this work, we will find it again. Or | |
9da33de6 | 113 | * we raced with task_work_run(), *pprev == NULL/exited. |
ac3d0da8 | 114 | */ |
e73f8959 | 115 | raw_spin_lock_irqsave(&task->pi_lock, flags); |
506458ef | 116 | while ((work = READ_ONCE(*pprev))) { |
ac3d0da8 ON |
117 | if (work->func != func) |
118 | pprev = &work->next; | |
119 | else if (cmpxchg(pprev, work, work->next) == work) | |
120 | break; | |
e73f8959 | 121 | } |
e73f8959 | 122 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
ac3d0da8 ON |
123 | |
124 | return work; | |
e73f8959 ON |
125 | } |
126 | ||
892f6668 ON |
127 | /** |
128 | * task_work_run - execute the works added by task_work_add() | |
129 | * | |
130 | * Flush the pending works. Should be used by the core kernel code. | |
131 | * Called before the task returns to the user-mode or stops, or when | |
132 | * it exits. In the latter case task_work_add() can no longer add the | |
133 | * new work after task_work_run() returns. | |
134 | */ | |
e73f8959 ON |
135 | void task_work_run(void) |
136 | { | |
137 | struct task_struct *task = current; | |
ac3d0da8 | 138 | struct callback_head *work, *head, *next; |
e73f8959 | 139 | |
ac3d0da8 | 140 | for (;;) { |
9da33de6 ON |
141 | /* |
142 | * work->func() can do task_work_add(), do not set | |
143 | * work_exited unless the list is empty. | |
144 | */ | |
145 | do { | |
6fb61492 | 146 | head = NULL; |
61e96496 | 147 | work = READ_ONCE(task->task_works); |
6fb61492 ON |
148 | if (!work) { |
149 | if (task->flags & PF_EXITING) | |
150 | head = &work_exited; | |
151 | else | |
152 | break; | |
153 | } | |
9da33de6 ON |
154 | } while (cmpxchg(&task->task_works, work, head) != work); |
155 | ||
ac3d0da8 ON |
156 | if (!work) |
157 | break; | |
6fb61492 ON |
158 | /* |
159 | * Synchronize with task_work_cancel(). It can not remove | |
160 | * the first entry == work, cmpxchg(task_works) must fail. | |
161 | * But it can remove another entry from the ->next list. | |
162 | */ | |
163 | raw_spin_lock_irq(&task->pi_lock); | |
164 | raw_spin_unlock_irq(&task->pi_lock); | |
e73f8959 | 165 | |
ac3d0da8 ON |
166 | do { |
167 | next = work->next; | |
168 | work->func(work); | |
169 | work = next; | |
f341861f | 170 | cond_resched(); |
ac3d0da8 | 171 | } while (work); |
e73f8959 ON |
172 | } |
173 | } |