task_work.c 3.13 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5
#include <linux/spinlock.h>
#include <linux/task_work.h>
#include <linux/tracehook.h>

6 7
static struct callback_head work_exited; /* all we need is ->next == NULL */

Oleg Nesterov's avatar
Oleg Nesterov committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
 * 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.
 *
22 23
 * Note: there is no ordering guarantee on works queued here.
 *
Oleg Nesterov's avatar
Oleg Nesterov committed
24 25 26
 * RETURNS:
 * 0 if succeeds or -ESRCH.
 */
27
int
28
task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
29
{
30
	struct callback_head *head;
31

32
	do {
33
		head = READ_ONCE(task->task_works);
34 35
		if (unlikely(head == &work_exited))
			return -ESRCH;
36 37
		work->next = head;
	} while (cmpxchg(&task->task_works, head, work) != head);
38

39
	if (notify)
40
		set_notify_resume(task);
41
	return 0;
42 43
}

Oleg Nesterov's avatar
Oleg Nesterov committed
44 45 46 47 48 49 50 51 52 53 54
/**
 * 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.
 */
55
struct callback_head *
56 57
task_work_cancel(struct task_struct *task, task_work_func_t func)
{
58
	struct callback_head **pprev = &task->task_works;
59
	struct callback_head *work;
60
	unsigned long flags;
61 62 63

	if (likely(!task->task_works))
		return NULL;
64 65 66 67
	/*
	 * 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
68
	 * we raced with task_work_run(), *pprev == NULL/exited.
69
	 */
70
	raw_spin_lock_irqsave(&task->pi_lock, flags);
71
	while ((work = READ_ONCE(*pprev))) {
72 73 74 75
		if (work->func != func)
			pprev = &work->next;
		else if (cmpxchg(pprev, work, work->next) == work)
			break;
76 77
	}
	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
78 79

	return work;
80 81
}

Oleg Nesterov's avatar
Oleg Nesterov committed
82 83 84 85 86 87 88 89
/**
 * 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.
 */
90 91 92
void task_work_run(void)
{
	struct task_struct *task = current;
93
	struct callback_head *work, *head, *next;
94

95
	for (;;) {
96 97 98 99
		/*
		 * work->func() can do task_work_add(), do not set
		 * work_exited unless the list is empty.
		 */
100
		raw_spin_lock_irq(&task->pi_lock);
101
		do {
102
			work = READ_ONCE(task->task_works);
103 104 105
			head = !work && (task->flags & PF_EXITING) ?
				&work_exited : NULL;
		} while (cmpxchg(&task->task_works, work, head) != work);
106
		raw_spin_unlock_irq(&task->pi_lock);
107

108 109
		if (!work)
			break;
110

111 112 113 114
		do {
			next = work->next;
			work->func(work);
			work = next;
115
			cond_resched();
116
		} while (work);
117 118
	}
}