xref: /linux/kernel/task_work.c (revision 0d6ccfe6b319d56da63b7d7cfbcecd92780a680d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/irq_work.h>
3 #include <linux/spinlock.h>
4 #include <linux/task_work.h>
5 #include <linux/resume_user_mode.h>
6 
7 static struct callback_head work_exited; /* all we need is ->next == NULL */
8 
9 static void task_work_set_notify_irq(struct irq_work *entry)
10 {
11 	test_and_set_tsk_thread_flag(current, TIF_NOTIFY_RESUME);
12 }
13 static DEFINE_PER_CPU(struct irq_work, irq_work_NMI_resume) =
14 	IRQ_WORK_INIT_HARD(task_work_set_notify_irq);
15 
16 /**
17  * task_work_add - ask the @task to execute @work->func()
18  * @task: the task which should run the callback
19  * @work: the callback to run
20  * @notify: how to notify the targeted task
21  *
22  * Queue @work for task_work_run() below and notify the @task if @notify
23  * is @TWA_RESUME, @TWA_SIGNAL, @TWA_SIGNAL_NO_IPI or @TWA_NMI_CURRENT.
24  *
25  * @TWA_SIGNAL works like signals, in that the it will interrupt the targeted
26  * task and run the task_work, regardless of whether the task is currently
27  * running in the kernel or userspace.
28  * @TWA_SIGNAL_NO_IPI works like @TWA_SIGNAL, except it doesn't send a
29  * reschedule IPI to force the targeted task to reschedule and run task_work.
30  * This can be advantageous if there's no strict requirement that the
31  * task_work be run as soon as possible, just whenever the task enters the
32  * kernel anyway.
33  * @TWA_RESUME work is run only when the task exits the kernel and returns to
34  * user mode, or before entering guest mode.
35  * @TWA_NMI_CURRENT works like @TWA_RESUME, except it can only be used for the
36  * current @task and if the current context is NMI.
37  *
38  * Fails if the @task is exiting/exited and thus it can't process this @work.
39  * Otherwise @work->func() will be called when the @task goes through one of
40  * the aforementioned transitions, or exits.
41  *
42  * If the targeted task is exiting, then an error is returned and the work item
43  * is not queued. It's up to the caller to arrange for an alternative mechanism
44  * in that case.
45  *
46  * Note: there is no ordering guarantee on works queued here. The task_work
47  * list is LIFO.
48  *
49  * RETURNS:
50  * 0 if succeeds or -ESRCH.
51  */
52 int task_work_add(struct task_struct *task, struct callback_head *work,
53 		  enum task_work_notify_mode notify)
54 {
55 	struct callback_head *head;
56 
57 	if (notify == TWA_NMI_CURRENT) {
58 		if (WARN_ON_ONCE(task != current))
59 			return -EINVAL;
60 	} else {
61 		/* record the work call stack in order to print it in KASAN reports */
62 		kasan_record_aux_stack(work);
63 	}
64 
65 	head = READ_ONCE(task->task_works);
66 	do {
67 		if (unlikely(head == &work_exited))
68 			return -ESRCH;
69 		work->next = head;
70 	} while (!try_cmpxchg(&task->task_works, &head, work));
71 
72 	switch (notify) {
73 	case TWA_NONE:
74 		break;
75 	case TWA_RESUME:
76 		set_notify_resume(task);
77 		break;
78 	case TWA_SIGNAL:
79 		set_notify_signal(task);
80 		break;
81 	case TWA_SIGNAL_NO_IPI:
82 		__set_notify_signal(task);
83 		break;
84 	case TWA_NMI_CURRENT:
85 		irq_work_queue(this_cpu_ptr(&irq_work_NMI_resume));
86 		break;
87 	default:
88 		WARN_ON_ONCE(1);
89 		break;
90 	}
91 
92 	return 0;
93 }
94 
95 /**
96  * task_work_cancel_match - cancel a pending work added by task_work_add()
97  * @task: the task which should execute the work
98  * @match: match function to call
99  * @data: data to be passed in to match function
100  *
101  * RETURNS:
102  * The found work or NULL if not found.
103  */
104 struct callback_head *
105 task_work_cancel_match(struct task_struct *task,
106 		       bool (*match)(struct callback_head *, void *data),
107 		       void *data)
108 {
109 	struct callback_head **pprev = &task->task_works;
110 	struct callback_head *work;
111 	unsigned long flags;
112 
113 	if (likely(!task_work_pending(task)))
114 		return NULL;
115 	/*
116 	 * If cmpxchg() fails we continue without updating pprev.
117 	 * Either we raced with task_work_add() which added the
118 	 * new entry before this work, we will find it again. Or
119 	 * we raced with task_work_run(), *pprev == NULL/exited.
120 	 */
121 	raw_spin_lock_irqsave(&task->pi_lock, flags);
122 	work = READ_ONCE(*pprev);
123 	while (work) {
124 		if (!match(work, data)) {
125 			pprev = &work->next;
126 			work = READ_ONCE(*pprev);
127 		} else if (try_cmpxchg(pprev, &work, work->next))
128 			break;
129 	}
130 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
131 
132 	return work;
133 }
134 
135 static bool task_work_func_match(struct callback_head *cb, void *data)
136 {
137 	return cb->func == data;
138 }
139 
140 /**
141  * task_work_cancel_func - cancel a pending work matching a function added by task_work_add()
142  * @task: the task which should execute the func's work
143  * @func: identifies the func to match with a work to remove
144  *
145  * Find the last queued pending work with ->func == @func and remove
146  * it from queue.
147  *
148  * RETURNS:
149  * The found work or NULL if not found.
150  */
151 struct callback_head *
152 task_work_cancel_func(struct task_struct *task, task_work_func_t func)
153 {
154 	return task_work_cancel_match(task, task_work_func_match, func);
155 }
156 
157 static bool task_work_match(struct callback_head *cb, void *data)
158 {
159 	return cb == data;
160 }
161 
162 /**
163  * task_work_cancel - cancel a pending work added by task_work_add()
164  * @task: the task which should execute the work
165  * @cb: the callback to remove if queued
166  *
167  * Remove a callback from a task's queue if queued.
168  *
169  * RETURNS:
170  * True if the callback was queued and got cancelled, false otherwise.
171  */
172 bool task_work_cancel(struct task_struct *task, struct callback_head *cb)
173 {
174 	struct callback_head *ret;
175 
176 	ret = task_work_cancel_match(task, task_work_match, cb);
177 
178 	return ret == cb;
179 }
180 
181 /**
182  * task_work_run - execute the works added by task_work_add()
183  *
184  * Flush the pending works. Should be used by the core kernel code.
185  * Called before the task returns to the user-mode or stops, or when
186  * it exits. In the latter case task_work_add() can no longer add the
187  * new work after task_work_run() returns.
188  */
189 void task_work_run(void)
190 {
191 	struct task_struct *task = current;
192 	struct callback_head *work, *head, *next;
193 
194 	for (;;) {
195 		/*
196 		 * work->func() can do task_work_add(), do not set
197 		 * work_exited unless the list is empty.
198 		 */
199 		work = READ_ONCE(task->task_works);
200 		do {
201 			head = NULL;
202 			if (!work) {
203 				if (task->flags & PF_EXITING)
204 					head = &work_exited;
205 				else
206 					break;
207 			}
208 		} while (!try_cmpxchg(&task->task_works, &work, head));
209 
210 		if (!work)
211 			break;
212 		/*
213 		 * Synchronize with task_work_cancel_match(). It can not remove
214 		 * the first entry == work, cmpxchg(task_works) must fail.
215 		 * But it can remove another entry from the ->next list.
216 		 */
217 		raw_spin_lock_irq(&task->pi_lock);
218 		raw_spin_unlock_irq(&task->pi_lock);
219 
220 		do {
221 			next = work->next;
222 			work->func(work);
223 			work = next;
224 			cond_resched();
225 		} while (work);
226 	}
227 }
228