blob: 0fef395662a6ea6f38301e92f74d14f60b3f145d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Oleg Nesterove73f8952012-05-11 10:59:07 +10002#include <linux/spinlock.h>
3#include <linux/task_work.h>
4#include <linux/tracehook.h>
5
Oleg Nesterov9da33de2012-08-26 21:12:11 +02006static struct callback_head work_exited; /* all we need is ->next == NULL */
7
Oleg Nesterov892f6662013-09-11 14:23:31 -07008/**
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 Dumazetc8219902015-08-28 19:42:30 -070022 * Note: there is no ordering guarantee on works queued here.
23 *
Oleg Nesterov892f6662013-09-11 14:23:31 -070024 * RETURNS:
25 * 0 if succeeds or -ESRCH.
26 */
Oleg Nesterove73f8952012-05-11 10:59:07 +100027int
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020028task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
Oleg Nesterove73f8952012-05-11 10:59:07 +100029{
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020030 struct callback_head *head;
Oleg Nesterov9da33de2012-08-26 21:12:11 +020031
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020032 do {
Oleg Nesterov61e96492016-08-02 14:03:44 -070033 head = READ_ONCE(task->task_works);
Oleg Nesterov9da33de2012-08-26 21:12:11 +020034 if (unlikely(head == &work_exited))
35 return -ESRCH;
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020036 work->next = head;
37 } while (cmpxchg(&task->task_works, head, work) != head);
Oleg Nesterove73f8952012-05-11 10:59:07 +100038
Al Viroed3e6942012-06-27 11:31:24 +040039 if (notify)
Oleg Nesterove73f8952012-05-11 10:59:07 +100040 set_notify_resume(task);
Al Viroed3e6942012-06-27 11:31:24 +040041 return 0;
Oleg Nesterove73f8952012-05-11 10:59:07 +100042}
43
Oleg Nesterov892f6662013-09-11 14:23:31 -070044/**
45 * task_work_cancel - cancel a pending work added by task_work_add()
46 * @task: the task which should execute the work
47 * @func: identifies the work to remove
48 *
49 * Find the last queued pending work with ->func == @func and remove
50 * it from queue.
51 *
52 * RETURNS:
53 * The found work or NULL if not found.
54 */
Al Viro67d12142012-06-27 11:07:19 +040055struct callback_head *
Oleg Nesterove73f8952012-05-11 10:59:07 +100056task_work_cancel(struct task_struct *task, task_work_func_t func)
57{
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020058 struct callback_head **pprev = &task->task_works;
Oleg Nesterov205e5502013-09-11 14:23:30 -070059 struct callback_head *work;
Oleg Nesterove73f8952012-05-11 10:59:07 +100060 unsigned long flags;
Oleg Nesterov61e96492016-08-02 14:03:44 -070061
62 if (likely(!task->task_works))
63 return NULL;
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020064 /*
65 * If cmpxchg() fails we continue without updating pprev.
66 * Either we raced with task_work_add() which added the
67 * new entry before this work, we will find it again. Or
Oleg Nesterov9da33de2012-08-26 21:12:11 +020068 * we raced with task_work_run(), *pprev == NULL/exited.
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020069 */
Oleg Nesterove73f8952012-05-11 10:59:07 +100070 raw_spin_lock_irqsave(&task->pi_lock, flags);
Will Deacon506458e2017-10-24 11:22:48 +010071 while ((work = READ_ONCE(*pprev))) {
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020072 if (work->func != func)
73 pprev = &work->next;
74 else if (cmpxchg(pprev, work, work->next) == work)
75 break;
Oleg Nesterove73f8952012-05-11 10:59:07 +100076 }
Oleg Nesterove73f8952012-05-11 10:59:07 +100077 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020078
79 return work;
Oleg Nesterove73f8952012-05-11 10:59:07 +100080}
81
Oleg Nesterov892f6662013-09-11 14:23:31 -070082/**
83 * task_work_run - execute the works added by task_work_add()
84 *
85 * Flush the pending works. Should be used by the core kernel code.
86 * Called before the task returns to the user-mode or stops, or when
87 * it exits. In the latter case task_work_add() can no longer add the
88 * new work after task_work_run() returns.
89 */
Oleg Nesterove73f8952012-05-11 10:59:07 +100090void task_work_run(void)
91{
92 struct task_struct *task = current;
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020093 struct callback_head *work, *head, *next;
Oleg Nesterove73f8952012-05-11 10:59:07 +100094
Oleg Nesterovac3d0da2012-08-26 21:12:09 +020095 for (;;) {
Oleg Nesterov9da33de2012-08-26 21:12:11 +020096 /*
97 * work->func() can do task_work_add(), do not set
98 * work_exited unless the list is empty.
99 */
Oleg Nesterovf274f1e2017-06-30 13:13:59 -0700100 raw_spin_lock_irq(&task->pi_lock);
Oleg Nesterov9da33de2012-08-26 21:12:11 +0200101 do {
Oleg Nesterov61e96492016-08-02 14:03:44 -0700102 work = READ_ONCE(task->task_works);
Oleg Nesterov9da33de2012-08-26 21:12:11 +0200103 head = !work && (task->flags & PF_EXITING) ?
104 &work_exited : NULL;
105 } while (cmpxchg(&task->task_works, work, head) != work);
Oleg Nesterovf274f1e2017-06-30 13:13:59 -0700106 raw_spin_unlock_irq(&task->pi_lock);
Oleg Nesterov9da33de2012-08-26 21:12:11 +0200107
Oleg Nesterovac3d0da2012-08-26 21:12:09 +0200108 if (!work)
109 break;
Oleg Nesterove73f8952012-05-11 10:59:07 +1000110
Oleg Nesterovac3d0da2012-08-26 21:12:09 +0200111 do {
112 next = work->next;
113 work->func(work);
114 work = next;
Eric Dumazetf3418612012-08-21 15:05:14 +0200115 cond_resched();
Oleg Nesterovac3d0da2012-08-26 21:12:09 +0200116 } while (work);
Oleg Nesterove73f8952012-05-11 10:59:07 +1000117 }
118}