1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/spinlock.h>
3 #include <linux/task_work.h>
4 #include <linux/tracehook.h>
6 static struct callback_head work_exited; /* all we need is ->next == NULL */
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.
15 static void task_work_notify_signal(struct task_struct *task)
17 #if defined(TIF_NOTIFY_SIGNAL)
18 set_notify_signal(task);
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.
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);
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
40 * @notify: how to notify the targeted task
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.
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
54 * Note: there is no ordering guarantee on works queued here. The task_work
58 * 0 if succeeds or -ESRCH.
60 int task_work_add(struct task_struct *task, struct callback_head *work,
61 enum task_work_notify_mode notify)
63 struct callback_head *head;
66 head = READ_ONCE(task->task_works);
67 if (unlikely(head == &work_exited))
70 } while (cmpxchg(&task->task_works, head, work) != head);
76 set_notify_resume(task);
79 task_work_notify_signal(task);
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
94 * Find the last queued pending work with ->func == @func and remove
98 * The found work or NULL if not found.
100 struct callback_head *
101 task_work_cancel(struct task_struct *task, task_work_func_t func)
103 struct callback_head **pprev = &task->task_works;
104 struct callback_head *work;
107 if (likely(!task->task_works))
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
113 * we raced with task_work_run(), *pprev == NULL/exited.
115 raw_spin_lock_irqsave(&task->pi_lock, flags);
116 while ((work = READ_ONCE(*pprev))) {
117 if (work->func != func)
119 else if (cmpxchg(pprev, work, work->next) == work)
122 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
128 * task_work_run - execute the works added by task_work_add()
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.
135 void task_work_run(void)
137 struct task_struct *task = current;
138 struct callback_head *work, *head, *next;
142 * work->func() can do task_work_add(), do not set
143 * work_exited unless the list is empty.
147 work = READ_ONCE(task->task_works);
149 if (task->flags & PF_EXITING)
154 } while (cmpxchg(&task->task_works, work, head) != work);
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.
163 raw_spin_lock_irq(&task->pi_lock);
164 raw_spin_unlock_irq(&task->pi_lock);