Blame view
kernel/task_work.c
3.13 KB
b24413180
|
1 |
// SPDX-License-Identifier: GPL-2.0 |
e73f8959a
|
2 3 4 |
#include <linux/spinlock.h> #include <linux/task_work.h> #include <linux/tracehook.h> |
9da33de62
|
5 |
static struct callback_head work_exited; /* all we need is ->next == NULL */ |
892f6668f
|
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
|
20 21 |
* Note: there is no ordering guarantee on works queued here. * |
892f6668f
|
22 23 24 |
* RETURNS: * 0 if succeeds or -ESRCH. */ |
e73f8959a
|
25 |
int |
ac3d0da8f
|
26 |
task_work_add(struct task_struct *task, struct callback_head *work, bool notify) |
e73f8959a
|
27 |
{ |
ac3d0da8f
|
28 |
struct callback_head *head; |
9da33de62
|
29 |
|
ac3d0da8f
|
30 |
do { |
61e96496d
|
31 |
head = READ_ONCE(task->task_works); |
9da33de62
|
32 33 |
if (unlikely(head == &work_exited)) return -ESRCH; |
ac3d0da8f
|
34 35 |
work->next = head; } while (cmpxchg(&task->task_works, head, work) != head); |
e73f8959a
|
36 |
|
ed3e694d7
|
37 |
if (notify) |
e73f8959a
|
38 |
set_notify_resume(task); |
ed3e694d7
|
39 |
return 0; |
e73f8959a
|
40 |
} |
892f6668f
|
41 42 43 44 45 46 47 48 49 50 51 |
/** * 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
|
52 |
struct callback_head * |
e73f8959a
|
53 54 |
task_work_cancel(struct task_struct *task, task_work_func_t func) { |
ac3d0da8f
|
55 |
struct callback_head **pprev = &task->task_works; |
205e550a0
|
56 |
struct callback_head *work; |
e73f8959a
|
57 |
unsigned long flags; |
61e96496d
|
58 59 60 |
if (likely(!task->task_works)) return NULL; |
ac3d0da8f
|
61 62 63 64 |
/* * 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
|
65 |
* we raced with task_work_run(), *pprev == NULL/exited. |
ac3d0da8f
|
66 |
*/ |
e73f8959a
|
67 |
raw_spin_lock_irqsave(&task->pi_lock, flags); |
506458efa
|
68 |
while ((work = READ_ONCE(*pprev))) { |
ac3d0da8f
|
69 70 71 72 |
if (work->func != func) pprev = &work->next; else if (cmpxchg(pprev, work, work->next) == work) break; |
e73f8959a
|
73 |
} |
e73f8959a
|
74 |
raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
ac3d0da8f
|
75 76 |
return work; |
e73f8959a
|
77 |
} |
892f6668f
|
78 79 80 81 82 83 84 85 |
/** * 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
|
86 87 88 |
void task_work_run(void) { struct task_struct *task = current; |
ac3d0da8f
|
89 |
struct callback_head *work, *head, *next; |
e73f8959a
|
90 |
|
ac3d0da8f
|
91 |
for (;;) { |
9da33de62
|
92 93 94 95 |
/* * work->func() can do task_work_add(), do not set * work_exited unless the list is empty. */ |
f274f1e72
|
96 |
raw_spin_lock_irq(&task->pi_lock); |
9da33de62
|
97 |
do { |
61e96496d
|
98 |
work = READ_ONCE(task->task_works); |
9da33de62
|
99 100 101 |
head = !work && (task->flags & PF_EXITING) ? &work_exited : NULL; } while (cmpxchg(&task->task_works, work, head) != work); |
f274f1e72
|
102 |
raw_spin_unlock_irq(&task->pi_lock); |
9da33de62
|
103 |
|
ac3d0da8f
|
104 105 |
if (!work) break; |
e73f8959a
|
106 |
|
ac3d0da8f
|
107 108 109 110 |
do { next = work->next; work->func(work); work = next; |
f341861fb
|
111 |
cond_resched(); |
ac3d0da8f
|
112 |
} while (work); |
e73f8959a
|
113 114 |
} } |