Merge patch series "Use composable cache instead of L2 cache"
[linux-block.git] / kernel / rseq.c
CommitLineData
d7822b1e
MD
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Restartable sequences system call
4 *
5 * Copyright (C) 2015, Google, Inc.,
6 * Paul Turner <pjt@google.com> and Andrew Hunter <ahh@google.com>
7 * Copyright (C) 2015-2018, EfficiOS Inc.,
8 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 */
10
11#include <linux/sched.h>
12#include <linux/uaccess.h>
13#include <linux/syscalls.h>
14#include <linux/rseq.h>
15#include <linux/types.h>
16#include <asm/ptrace.h>
17
18#define CREATE_TRACE_POINTS
19#include <trace/events/rseq.h>
20
0190e419
MD
21#define RSEQ_CS_NO_RESTART_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT | \
22 RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL | \
23 RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE)
d7822b1e
MD
24
25/*
26 *
27 * Restartable sequences are a lightweight interface that allows
28 * user-level code to be executed atomically relative to scheduler
29 * preemption and signal delivery. Typically used for implementing
30 * per-cpu operations.
31 *
32 * It allows user-space to perform update operations on per-cpu data
33 * without requiring heavy-weight atomic operations.
34 *
35 * Detailed algorithm of rseq user-space assembly sequences:
36 *
37 * init(rseq_cs)
38 * cpu = TLS->rseq::cpu_id_start
39 * [1] TLS->rseq::rseq_cs = rseq_cs
40 * [start_ip] ----------------------------
41 * [2] if (cpu != TLS->rseq::cpu_id)
42 * goto abort_ip;
43 * [3] <last_instruction_in_cs>
44 * [post_commit_ip] ----------------------------
45 *
46 * The address of jump target abort_ip must be outside the critical
47 * region, i.e.:
48 *
49 * [abort_ip] < [start_ip] || [abort_ip] >= [post_commit_ip]
50 *
51 * Steps [2]-[3] (inclusive) need to be a sequence of instructions in
52 * userspace that can handle being interrupted between any of those
53 * instructions, and then resumed to the abort_ip.
54 *
55 * 1. Userspace stores the address of the struct rseq_cs assembly
56 * block descriptor into the rseq_cs field of the registered
57 * struct rseq TLS area. This update is performed through a single
58 * store within the inline assembly instruction sequence.
59 * [start_ip]
60 *
61 * 2. Userspace tests to check whether the current cpu_id field match
62 * the cpu number loaded before start_ip, branching to abort_ip
63 * in case of a mismatch.
64 *
65 * If the sequence is preempted or interrupted by a signal
66 * at or after start_ip and before post_commit_ip, then the kernel
67 * clears TLS->__rseq_abi::rseq_cs, and sets the user-space return
68 * ip to abort_ip before returning to user-space, so the preempted
69 * execution resumes at abort_ip.
70 *
71 * 3. Userspace critical section final instruction before
72 * post_commit_ip is the commit. The critical section is
73 * self-terminating.
74 * [post_commit_ip]
75 *
76 * 4. <success>
77 *
78 * On failure at [2], or if interrupted by preempt or signal delivery
79 * between [1] and [3]:
80 *
81 * [abort_ip]
82 * F1. <failure>
83 */
84
85static int rseq_update_cpu_id(struct task_struct *t)
86{
87 u32 cpu_id = raw_smp_processor_id();
60af388d 88 struct rseq __user *rseq = t->rseq;
d7822b1e 89
60af388d
ED
90 if (!user_write_access_begin(rseq, sizeof(*rseq)))
91 goto efault;
92 unsafe_put_user(cpu_id, &rseq->cpu_id_start, efault_end);
93 unsafe_put_user(cpu_id, &rseq->cpu_id, efault_end);
94 user_write_access_end();
d7822b1e
MD
95 trace_rseq_update(t);
96 return 0;
60af388d
ED
97
98efault_end:
99 user_write_access_end();
100efault:
101 return -EFAULT;
d7822b1e
MD
102}
103
104static int rseq_reset_rseq_cpu_id(struct task_struct *t)
105{
106 u32 cpu_id_start = 0, cpu_id = RSEQ_CPU_ID_UNINITIALIZED;
107
108 /*
109 * Reset cpu_id_start to its initial state (0).
110 */
8f281770 111 if (put_user(cpu_id_start, &t->rseq->cpu_id_start))
d7822b1e
MD
112 return -EFAULT;
113 /*
114 * Reset cpu_id to RSEQ_CPU_ID_UNINITIALIZED, so any user coming
115 * in after unregistration can figure out that rseq needs to be
116 * registered again.
117 */
8f281770 118 if (put_user(cpu_id, &t->rseq->cpu_id))
d7822b1e
MD
119 return -EFAULT;
120 return 0;
121}
122
123static int rseq_get_rseq_cs(struct task_struct *t, struct rseq_cs *rseq_cs)
124{
125 struct rseq_cs __user *urseq_cs;
ec9c82e0 126 u64 ptr;
d7822b1e
MD
127 u32 __user *usig;
128 u32 sig;
129 int ret;
130
5e0ccd4a 131#ifdef CONFIG_64BIT
bfdf4e62 132 if (get_user(ptr, &t->rseq->rseq_cs))
5e0ccd4a
ED
133 return -EFAULT;
134#else
bfdf4e62 135 if (copy_from_user(&ptr, &t->rseq->rseq_cs, sizeof(ptr)))
ec9c82e0 136 return -EFAULT;
5e0ccd4a 137#endif
d7822b1e
MD
138 if (!ptr) {
139 memset(rseq_cs, 0, sizeof(*rseq_cs));
140 return 0;
141 }
ec9c82e0
MD
142 if (ptr >= TASK_SIZE)
143 return -EINVAL;
144 urseq_cs = (struct rseq_cs __user *)(unsigned long)ptr;
d7822b1e
MD
145 if (copy_from_user(rseq_cs, urseq_cs, sizeof(*rseq_cs)))
146 return -EFAULT;
d7822b1e 147
e96d7135
MD
148 if (rseq_cs->start_ip >= TASK_SIZE ||
149 rseq_cs->start_ip + rseq_cs->post_commit_offset >= TASK_SIZE ||
150 rseq_cs->abort_ip >= TASK_SIZE ||
151 rseq_cs->version > 0)
152 return -EINVAL;
153 /* Check for overflow. */
154 if (rseq_cs->start_ip + rseq_cs->post_commit_offset < rseq_cs->start_ip)
155 return -EINVAL;
d7822b1e
MD
156 /* Ensure that abort_ip is not in the critical section. */
157 if (rseq_cs->abort_ip - rseq_cs->start_ip < rseq_cs->post_commit_offset)
158 return -EINVAL;
159
e96d7135 160 usig = (u32 __user *)(unsigned long)(rseq_cs->abort_ip - sizeof(u32));
d7822b1e
MD
161 ret = get_user(sig, usig);
162 if (ret)
163 return ret;
164
165 if (current->rseq_sig != sig) {
166 printk_ratelimited(KERN_WARNING
167 "Possible attack attempt. Unexpected rseq signature 0x%x, expecting 0x%x (pid=%d, addr=%p).\n",
168 sig, current->rseq_sig, current->pid, usig);
e96d7135 169 return -EINVAL;
d7822b1e
MD
170 }
171 return 0;
172}
173
174static int rseq_need_restart(struct task_struct *t, u32 cs_flags)
175{
176 u32 flags, event_mask;
177 int ret;
178
c17a6ff9 179 if (WARN_ON_ONCE(cs_flags & RSEQ_CS_NO_RESTART_FLAGS) || cs_flags)
0190e419
MD
180 return -EINVAL;
181
d7822b1e 182 /* Get thread flags. */
8f281770 183 ret = get_user(flags, &t->rseq->flags);
d7822b1e
MD
184 if (ret)
185 return ret;
186
c17a6ff9 187 if (WARN_ON_ONCE(flags & RSEQ_CS_NO_RESTART_FLAGS) || flags)
d7822b1e
MD
188 return -EINVAL;
189
190 /*
191 * Load and clear event mask atomically with respect to
192 * scheduler preemption.
193 */
194 preempt_disable();
195 event_mask = t->rseq_event_mask;
196 t->rseq_event_mask = 0;
197 preempt_enable();
198
0190e419 199 return !!event_mask;
d7822b1e
MD
200}
201
202static int clear_rseq_cs(struct task_struct *t)
203{
204 /*
205 * The rseq_cs field is set to NULL on preemption or signal
206 * delivery on top of rseq assembly block, as well as on top
207 * of code outside of the rseq assembly block. This performs
208 * a lazy clear of the rseq_cs field.
209 *
0fb9a1ab 210 * Set rseq_cs to NULL.
d7822b1e 211 */
5e0ccd4a 212#ifdef CONFIG_64BIT
bfdf4e62 213 return put_user(0UL, &t->rseq->rseq_cs);
5e0ccd4a 214#else
bfdf4e62 215 if (clear_user(&t->rseq->rseq_cs, sizeof(t->rseq->rseq_cs)))
ec9c82e0
MD
216 return -EFAULT;
217 return 0;
5e0ccd4a 218#endif
d7822b1e
MD
219}
220
221/*
222 * Unsigned comparison will be true when ip >= start_ip, and when
223 * ip < start_ip + post_commit_offset.
224 */
225static bool in_rseq_cs(unsigned long ip, struct rseq_cs *rseq_cs)
226{
227 return ip - rseq_cs->start_ip < rseq_cs->post_commit_offset;
228}
229
230static int rseq_ip_fixup(struct pt_regs *regs)
231{
232 unsigned long ip = instruction_pointer(regs);
233 struct task_struct *t = current;
234 struct rseq_cs rseq_cs;
235 int ret;
236
237 ret = rseq_get_rseq_cs(t, &rseq_cs);
238 if (ret)
239 return ret;
240
241 /*
242 * Handle potentially not being within a critical section.
243 * If not nested over a rseq critical section, restart is useless.
244 * Clear the rseq_cs pointer and return.
245 */
246 if (!in_rseq_cs(ip, &rseq_cs))
247 return clear_rseq_cs(t);
248 ret = rseq_need_restart(t, rseq_cs.flags);
249 if (ret <= 0)
250 return ret;
251 ret = clear_rseq_cs(t);
252 if (ret)
253 return ret;
254 trace_rseq_ip_fixup(ip, rseq_cs.start_ip, rseq_cs.post_commit_offset,
255 rseq_cs.abort_ip);
256 instruction_pointer_set(regs, (unsigned long)rseq_cs.abort_ip);
257 return 0;
258}
259
260/*
261 * This resume handler must always be executed between any of:
262 * - preemption,
263 * - signal delivery,
264 * and return to user-space.
265 *
bff9504b 266 * This is how we can ensure that the entire rseq critical section
d7822b1e
MD
267 * will issue the commit instruction only if executed atomically with
268 * respect to other threads scheduled on the same CPU, and with respect
269 * to signal handlers.
270 */
784e0300 271void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
d7822b1e
MD
272{
273 struct task_struct *t = current;
784e0300 274 int ret, sig;
d7822b1e
MD
275
276 if (unlikely(t->flags & PF_EXITING))
277 return;
8646e536
SC
278
279 /*
280 * regs is NULL if and only if the caller is in a syscall path. Skip
281 * fixup and leave rseq_cs as is so that rseq_sycall() will detect and
282 * kill a misbehaving userspace on debug kernels.
283 */
284 if (regs) {
285 ret = rseq_ip_fixup(regs);
286 if (unlikely(ret < 0))
287 goto error;
288 }
d7822b1e
MD
289 if (unlikely(rseq_update_cpu_id(t)))
290 goto error;
291 return;
292
293error:
784e0300 294 sig = ksig ? ksig->sig : 0;
cb44c9a0 295 force_sigsegv(sig);
d7822b1e
MD
296}
297
298#ifdef CONFIG_DEBUG_RSEQ
299
300/*
301 * Terminate the process if a syscall is issued within a restartable
302 * sequence.
303 */
304void rseq_syscall(struct pt_regs *regs)
305{
306 unsigned long ip = instruction_pointer(regs);
307 struct task_struct *t = current;
308 struct rseq_cs rseq_cs;
309
310 if (!t->rseq)
311 return;
0ed96051 312 if (rseq_get_rseq_cs(t, &rseq_cs) || in_rseq_cs(ip, &rseq_cs))
3cf5d076 313 force_sig(SIGSEGV);
d7822b1e
MD
314}
315
316#endif
317
318/*
319 * sys_rseq - setup restartable sequences for caller thread.
320 */
321SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
322 int, flags, u32, sig)
323{
324 int ret;
325
326 if (flags & RSEQ_FLAG_UNREGISTER) {
66528a45
MD
327 if (flags & ~RSEQ_FLAG_UNREGISTER)
328 return -EINVAL;
d7822b1e
MD
329 /* Unregister rseq for current thread. */
330 if (current->rseq != rseq || !current->rseq)
331 return -EINVAL;
83b0b15b 332 if (rseq_len != sizeof(*rseq))
d7822b1e
MD
333 return -EINVAL;
334 if (current->rseq_sig != sig)
335 return -EPERM;
336 ret = rseq_reset_rseq_cpu_id(current);
337 if (ret)
338 return ret;
339 current->rseq = NULL;
d7822b1e
MD
340 current->rseq_sig = 0;
341 return 0;
342 }
343
344 if (unlikely(flags))
345 return -EINVAL;
346
347 if (current->rseq) {
348 /*
349 * If rseq is already registered, check whether
350 * the provided address differs from the prior
351 * one.
352 */
83b0b15b 353 if (current->rseq != rseq || rseq_len != sizeof(*rseq))
d7822b1e
MD
354 return -EINVAL;
355 if (current->rseq_sig != sig)
356 return -EPERM;
357 /* Already registered. */
358 return -EBUSY;
359 }
360
361 /*
362 * If there was no rseq previously registered,
363 * ensure the provided rseq is properly aligned and valid.
364 */
365 if (!IS_ALIGNED((unsigned long)rseq, __alignof__(*rseq)) ||
366 rseq_len != sizeof(*rseq))
367 return -EINVAL;
96d4f267 368 if (!access_ok(rseq, rseq_len))
d7822b1e
MD
369 return -EFAULT;
370 current->rseq = rseq;
d7822b1e
MD
371 current->rseq_sig = sig;
372 /*
373 * If rseq was previously inactive, and has just been
374 * registered, ensure the cpu_id_start and cpu_id fields
375 * are updated before returning to user-space.
376 */
377 rseq_set_notify_resume(current);
378
379 return 0;
380}