Blame view
kernel/task_work.c
3.81 KB
b24413180 License cleanup: ... |
1 |
// SPDX-License-Identifier: GPL-2.0 |
e73f8959a task_work_add: ge... |
2 3 4 |
#include <linux/spinlock.h> #include <linux/task_work.h> #include <linux/tracehook.h> |
9da33de62 task_work: task_w... |
5 |
static struct callback_head work_exited; /* all we need is ->next == NULL */ |
892f6668f task_work: docume... |
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * task_work_add - ask the @task to execute @work->func() * @task: the task which should run the callback * @work: the callback to run * @notify: send the notification if true * * Queue @work for task_work_run() below and notify the @task if @notify. * Fails if the @task is exiting/exited and thus it can't process this @work. * Otherwise @work->func() will be called when the @task returns from kernel * mode or exits. * * This is like the signal handler which runs in kernel mode, but it doesn't * try to wake up the @task. * |
c82199061 task_work: remove... |
20 21 |
* Note: there is no ordering guarantee on works queued here. * |
892f6668f task_work: docume... |
22 23 24 |
* RETURNS: * 0 if succeeds or -ESRCH. */ |
e73f8959a task_work_add: ge... |
25 |
int |
e91b48162 task_work: teach ... |
26 |
task_work_add(struct task_struct *task, struct callback_head *work, int notify) |
e73f8959a task_work_add: ge... |
27 |
{ |
ac3d0da8f task_work: Make t... |
28 |
struct callback_head *head; |
e91b48162 task_work: teach ... |
29 |
unsigned long flags; |
9da33de62 task_work: task_w... |
30 |
|
ac3d0da8f task_work: Make t... |
31 |
do { |
61e96496d task_work: use RE... |
32 |
head = READ_ONCE(task->task_works); |
9da33de62 task_work: task_w... |
33 34 |
if (unlikely(head == &work_exited)) return -ESRCH; |
ac3d0da8f task_work: Make t... |
35 36 |
work->next = head; } while (cmpxchg(&task->task_works, head, work) != head); |
e73f8959a task_work_add: ge... |
37 |
|
e91b48162 task_work: teach ... |
38 39 |
switch (notify) { case TWA_RESUME: |
e73f8959a task_work_add: ge... |
40 |
set_notify_resume(task); |
e91b48162 task_work: teach ... |
41 42 |
break; case TWA_SIGNAL: |
ebf0d100d task_work: only g... |
43 44 45 46 47 48 49 |
/* * Only grab the sighand lock if we don't already have some * task_work pending. This pairs with the smp_store_mb() * in get_signal(), see comment there. */ if (!(READ_ONCE(task->jobctl) & JOBCTL_TASK_WORK) && lock_task_sighand(task, &flags)) { |
e91b48162 task_work: teach ... |
50 51 52 53 54 55 |
task->jobctl |= JOBCTL_TASK_WORK; signal_wake_up(task, 0); unlock_task_sighand(task, &flags); } break; } |
ed3e694d7 move exit_task_wo... |
56 |
return 0; |
e73f8959a task_work_add: ge... |
57 |
} |
892f6668f task_work: docume... |
58 59 60 61 62 63 64 65 66 67 68 |
/** * task_work_cancel - cancel a pending work added by task_work_add() * @task: the task which should execute the work * @func: identifies the work to remove * * Find the last queued pending work with ->func == @func and remove * it from queue. * * RETURNS: * The found work or NULL if not found. */ |
67d121455 merge task_work a... |
69 |
struct callback_head * |
e73f8959a task_work_add: ge... |
70 71 |
task_work_cancel(struct task_struct *task, task_work_func_t func) { |
ac3d0da8f task_work: Make t... |
72 |
struct callback_head **pprev = &task->task_works; |
205e550a0 task_work: minor ... |
73 |
struct callback_head *work; |
e73f8959a task_work_add: ge... |
74 |
unsigned long flags; |
61e96496d task_work: use RE... |
75 76 77 |
if (likely(!task->task_works)) return NULL; |
ac3d0da8f task_work: Make t... |
78 79 80 81 |
/* * If cmpxchg() fails we continue without updating pprev. * Either we raced with task_work_add() which added the * new entry before this work, we will find it again. Or |
9da33de62 task_work: task_w... |
82 |
* we raced with task_work_run(), *pprev == NULL/exited. |
ac3d0da8f task_work: Make t... |
83 |
*/ |
e73f8959a task_work_add: ge... |
84 |
raw_spin_lock_irqsave(&task->pi_lock, flags); |
506458efa locking/barriers:... |
85 |
while ((work = READ_ONCE(*pprev))) { |
ac3d0da8f task_work: Make t... |
86 87 88 89 |
if (work->func != func) pprev = &work->next; else if (cmpxchg(pprev, work, work->next) == work) break; |
e73f8959a task_work_add: ge... |
90 |
} |
e73f8959a task_work_add: ge... |
91 |
raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
ac3d0da8f task_work: Make t... |
92 93 |
return work; |
e73f8959a task_work_add: ge... |
94 |
} |
892f6668f task_work: docume... |
95 96 97 98 99 100 101 102 |
/** * task_work_run - execute the works added by task_work_add() * * Flush the pending works. Should be used by the core kernel code. * Called before the task returns to the user-mode or stops, or when * it exits. In the latter case task_work_add() can no longer add the * new work after task_work_run() returns. */ |
e73f8959a task_work_add: ge... |
103 104 105 |
void task_work_run(void) { struct task_struct *task = current; |
ac3d0da8f task_work: Make t... |
106 |
struct callback_head *work, *head, *next; |
e73f8959a task_work_add: ge... |
107 |
|
ac3d0da8f task_work: Make t... |
108 |
for (;;) { |
9da33de62 task_work: task_w... |
109 110 111 112 113 |
/* * work->func() can do task_work_add(), do not set * work_exited unless the list is empty. */ do { |
6fb614920 task_work_run: do... |
114 |
head = NULL; |
61e96496d task_work: use RE... |
115 |
work = READ_ONCE(task->task_works); |
6fb614920 task_work_run: do... |
116 117 118 119 120 121 |
if (!work) { if (task->flags & PF_EXITING) head = &work_exited; else break; } |
9da33de62 task_work: task_w... |
122 |
} while (cmpxchg(&task->task_works, work, head) != work); |
ac3d0da8f task_work: Make t... |
123 124 |
if (!work) break; |
6fb614920 task_work_run: do... |
125 126 127 128 129 130 131 |
/* * Synchronize with task_work_cancel(). It can not remove * the first entry == work, cmpxchg(task_works) must fail. * But it can remove another entry from the ->next list. */ raw_spin_lock_irq(&task->pi_lock); raw_spin_unlock_irq(&task->pi_lock); |
e73f8959a task_work_add: ge... |
132 |
|
ac3d0da8f task_work: Make t... |
133 134 135 136 |
do { next = work->next; work->func(work); work = next; |
f341861fb task_work: add a ... |
137 |
cond_resched(); |
ac3d0da8f task_work: Make t... |
138 |
} while (work); |
e73f8959a task_work_add: ge... |
139 140 |
} } |