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: send the notification if true
42 * Queue @work for task_work_run() below and notify the @task if @notify.
43 * Fails if the @task is exiting/exited and thus it can't process this @work.
44 * Otherwise @work->func() will be called when the @task returns from kernel
47 * This is like the signal handler which runs in kernel mode, but it doesn't
48 * try to wake up the @task.
50 * Note: there is no ordering guarantee on works queued here.
53 * 0 if succeeds or -ESRCH.
56 task_work_add(struct task_struct *task, struct callback_head *work, int notify)
58 struct callback_head *head;
61 head = READ_ONCE(task->task_works);
62 if (unlikely(head == &work_exited))
65 } while (cmpxchg(&task->task_works, head, work) != head);
69 set_notify_resume(task);
72 task_work_notify_signal(task);
80 * task_work_cancel - cancel a pending work added by task_work_add()
81 * @task: the task which should execute the work
82 * @func: identifies the work to remove
84 * Find the last queued pending work with ->func == @func and remove
88 * The found work or NULL if not found.
90 struct callback_head *
91 task_work_cancel(struct task_struct *task, task_work_func_t func)
93 struct callback_head **pprev = &task->task_works;
94 struct callback_head *work;
97 if (likely(!task->task_works))
100 * If cmpxchg() fails we continue without updating pprev.
101 * Either we raced with task_work_add() which added the
102 * new entry before this work, we will find it again. Or
103 * we raced with task_work_run(), *pprev == NULL/exited.
105 raw_spin_lock_irqsave(&task->pi_lock, flags);
106 while ((work = READ_ONCE(*pprev))) {
107 if (work->func != func)
109 else if (cmpxchg(pprev, work, work->next) == work)
112 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
118 * task_work_run - execute the works added by task_work_add()
120 * Flush the pending works. Should be used by the core kernel code.
121 * Called before the task returns to the user-mode or stops, or when
122 * it exits. In the latter case task_work_add() can no longer add the
123 * new work after task_work_run() returns.
125 void task_work_run(void)
127 struct task_struct *task = current;
128 struct callback_head *work, *head, *next;
132 * work->func() can do task_work_add(), do not set
133 * work_exited unless the list is empty.
137 work = READ_ONCE(task->task_works);
139 if (task->flags & PF_EXITING)
144 } while (cmpxchg(&task->task_works, work, head) != work);
149 * Synchronize with task_work_cancel(). It can not remove
150 * the first entry == work, cmpxchg(task_works) must fail.
151 * But it can remove another entry from the ->next list.
153 raw_spin_lock_irq(&task->pi_lock);
154 raw_spin_unlock_irq(&task->pi_lock);