at commit 709ba1c5

1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_RSEQ_ENTRY_H
3 #define _LINUX_RSEQ_ENTRY_H
4
5 /* Must be outside the CONFIG_RSEQ guard to resolve the stubs */
6 #ifdef CONFIG_RSEQ_STATS
7 #include <linux/percpu.h>
8
9 struct rseq_stats {
10 unsigned long exit;
11 unsigned long signal;
12 unsigned long slowpath;
13 unsigned long fastpath;
14 unsigned long ids;
15 unsigned long cs;
16 unsigned long clear;
17 unsigned long fixup;
18 unsigned long s_granted;
19 unsigned long s_expired;
20 unsigned long s_revoked;
21 unsigned long s_yielded;
22 unsigned long s_aborted;
23 };
24
25 DECLARE_PER_CPU(struct rseq_stats, rseq_stats);
26
27 /*
28 * Slow path has interrupts and preemption enabled, but the fast path
29 * runs with interrupts disabled so there is no point in having the
30 * preemption checks implied in __this_cpu_inc() for every operation.
31 */
32 #ifdef RSEQ_BUILD_SLOW_PATH
33 #define rseq_stat_inc(which) this_cpu_inc((which))
34 #else
35 #define rseq_stat_inc(which) raw_cpu_inc((which))
36 #endif
37
38 #else /* CONFIG_RSEQ_STATS */
39 #define rseq_stat_inc(x) do { } while (0)
40 #endif /* !CONFIG_RSEQ_STATS */
41
42 #ifdef CONFIG_RSEQ
43 #include <linux/hrtimer_rearm.h>
44 #include <linux/jump_label.h>
45 #include <linux/rseq.h>
46 #include <linux/sched/signal.h>
47 #include <linux/uaccess.h>
48
49 #include <linux/tracepoint-defs.h>
50
51 #ifdef CONFIG_TRACEPOINTS
52 DECLARE_TRACEPOINT(rseq_update);
53 DECLARE_TRACEPOINT(rseq_ip_fixup);
54 void __rseq_trace_update(struct task_struct *t);
55 void __rseq_trace_ip_fixup(unsigned long ip, unsigned long start_ip,
56 unsigned long offset, unsigned long abort_ip);
57
58 static inline void rseq_trace_update(struct task_struct *t, struct rseq_ids *ids)
59 {
60 if (tracepoint_enabled(rseq_update) && ids)
61 __rseq_trace_update(t);
62 }
63
64 static inline void rseq_trace_ip_fixup(unsigned long ip, unsigned long start_ip,
65 unsigned long offset, unsigned long abort_ip)
66 {
67 if (tracepoint_enabled(rseq_ip_fixup))
68 __rseq_trace_ip_fixup(ip, start_ip, offset, abort_ip);
69 }
70
71 #else /* CONFIG_TRACEPOINT */
72 static inline void rseq_trace_update(struct task_struct *t, struct rseq_ids *ids) { }
73 static inline void rseq_trace_ip_fixup(unsigned long ip, unsigned long start_ip,
74 unsigned long offset, unsigned long abort_ip) { }
75 #endif /* !CONFIG_TRACEPOINT */
76
77 DECLARE_STATIC_KEY_MAYBE(CONFIG_RSEQ_DEBUG_DEFAULT_ENABLE, rseq_debug_enabled);
78
79 #ifdef RSEQ_BUILD_SLOW_PATH
80 #define rseq_inline
81 #else
82 #define rseq_inline __always_inline
83 #endif
84
85 #ifdef CONFIG_RSEQ_SLICE_EXTENSION
86 DECLARE_STATIC_KEY_TRUE(rseq_slice_extension_key);
87
88 static __always_inline bool rseq_slice_extension_enabled(void)
89 {
90 return static_branch_likely(&rseq_slice_extension_key);
91 }
92
93 extern unsigned int rseq_slice_ext_nsecs;
94 bool __rseq_arm_slice_extension_timer(void);
95
96 static __always_inline bool rseq_arm_slice_extension_timer(void)
97 {
98 if (!rseq_slice_extension_enabled())
99 return false;
100
101 if (likely(!current->rseq.slice.state.granted))
102 return false;
103
104 return __rseq_arm_slice_extension_timer();
105 }
106
107 static __always_inline void rseq_slice_clear_grant(struct task_struct *t)
108 {
109 if (IS_ENABLED(CONFIG_RSEQ_STATS) && t->rseq.slice.state.granted)
110 rseq_stat_inc(rseq_stats.s_revoked);
111 t->rseq.slice.state.granted = false;
112 }
113
114 /*
115 * Open coded, so it can be invoked within a user access region.
116 *
117 * This clears the user space state of the time slice extensions field only when
118 * the task has registered the optimized RSEQ_ABI V2. Some legacy registrations,
119 * e.g. TCMalloc, have conflicting non-ABI fields in struct RSEQ, which would be
120 * overwritten by an unconditional write.
121 */
122 #define rseq_slice_clear_user(rseq, efault) \
123 do { \
124 if (rseq_slice_extension_enabled()) \
125 unsafe_put_user(0U, &rseq->slice_ctrl.all, efault); \
126 } while (0)
127
128 static __always_inline bool __rseq_grant_slice_extension(bool work_pending)
129 {
130 struct task_struct *curr = current;
131 struct rseq_slice_ctrl usr_ctrl;
132 union rseq_slice_state state;
133 struct rseq __user *rseq;
134
135 if (!rseq_slice_extension_enabled())
136 return false;
137
138 /* If not enabled or not a return from interrupt, nothing to do. */
139 state = curr->rseq.slice.state;
140 state.enabled &= curr->rseq.event.user_irq;
141 if (likely(!state.state))
142 return false;
143
144 rseq = curr->rseq.usrptr;
145 scoped_user_rw_access(rseq, efault) {
146
147 /*
148 * Quick check conditions where a grant is not possible or
149 * needs to be revoked.
150 *
151 * 1) Any TIF bit which needs to do extra work aside of
152 * rescheduling prevents a grant.
153 *
154 * 2) A previous rescheduling request resulted in a slice
155 * extension grant.
156 */
157 if (unlikely(work_pending || state.granted)) {
158 /* Clear user control unconditionally. No point for checking */
159 unsafe_put_user(0U, &rseq->slice_ctrl.all, efault);
160 rseq_slice_clear_grant(curr);
161 return false;
162 }
163
164 unsafe_get_user(usr_ctrl.all, &rseq->slice_ctrl.all, efault);
165 if (likely(!(usr_ctrl.request)))
166 return false;
167
168 /* Grant the slice extention */
169 usr_ctrl.request = 0;
170 usr_ctrl.granted = 1;
171 unsafe_put_user(usr_ctrl.all, &rseq->slice_ctrl.all, efault);
172 }
173
174 rseq_stat_inc(rseq_stats.s_granted);
175
176 curr->rseq.slice.state.granted = true;
177 /* Store expiry time for arming the timer on the way out */
178 curr->rseq.slice.expires = data_race(rseq_slice_ext_nsecs) + ktime_get_mono_fast_ns();
179 /*
180 * This is racy against a remote CPU setting TIF_NEED_RESCHED in
181 * several ways:
182 *
183 * 1)
184 * CPU0 CPU1
185 * clear_tsk()
186 * set_tsk()
187 * clear_preempt()
188 * Raise scheduler IPI on CPU0
189 * --> IPI
190 * fold_need_resched() -> Folds correctly
191 * 2)
192 * CPU0 CPU1
193 * set_tsk()
194 * clear_tsk()
195 * clear_preempt()
196 * Raise scheduler IPI on CPU0
197 * --> IPI
198 * fold_need_resched() <- NOOP as TIF_NEED_RESCHED is false
199 *
200 * #1 is not any different from a regular remote reschedule as it
201 * sets the previously not set bit and then raises the IPI which
202 * folds it into the preempt counter
203 *
204 * #2 is obviously incorrect from a scheduler POV, but it's not
205 * differently incorrect than the code below clearing the
206 * reschedule request with the safety net of the timer.
207 *
208 * The important part is that the clearing is protected against the
209 * scheduler IPI and also against any other interrupt which might
210 * end up waking up a task and setting the bits in the middle of
211 * the operation:
212 *
213 * clear_tsk()
214 * ---> Interrupt
215 * wakeup_on_this_cpu()
216 * set_tsk()
217 * set_preempt()
218 * clear_preempt()
219 *
220 * which would be inconsistent state.
221 */
222 scoped_guard(irq) {
223 clear_tsk_need_resched(curr);
224 clear_preempt_need_resched();
225 }
226 return true;
227
228 efault:
229 force_sig(SIGSEGV);
230 return false;
231 }
232
233 static __always_inline bool rseq_grant_slice_extension(unsigned long ti_work, unsigned long mask)
234 {
235 if (unlikely(__rseq_grant_slice_extension(ti_work & mask))) {
236 guard(irq)();
237 hrtimer_rearm_deferred_tif(ti_work);
238 return true;
239 }
240 return false;
241 }
242
243 #else /* CONFIG_RSEQ_SLICE_EXTENSION */
244 static __always_inline bool rseq_slice_extension_enabled(void) { return false; }
245 static __always_inline bool rseq_arm_slice_extension_timer(void) { return false; }
246 static __always_inline void rseq_slice_clear_grant(struct task_struct *t) { }
247 static __always_inline bool rseq_grant_slice_extension(unsigned long ti_work, unsigned long mask) { return false; }
248 #define rseq_slice_clear_user(rseq, efault) do { } while (0)
249 #endif /* !CONFIG_RSEQ_SLICE_EXTENSION */
250
251 bool rseq_debug_update_user_cs(struct task_struct *t, struct pt_regs *regs, unsigned long csaddr);
252
253 static __always_inline void rseq_note_user_irq_entry(void)
254 {
255 if (IS_ENABLED(CONFIG_GENERIC_IRQ_ENTRY))
256 current->rseq.event.user_irq = true;
257 }
258
259 /*
260 * Check whether there is a valid critical section and whether the
261 * instruction pointer in @regs is inside the critical section.
262 *
263 * - If the critical section is invalid, terminate the task.
264 *
265 * - If valid and the instruction pointer is inside, set it to the abort IP.
266 *
267 * - If valid and the instruction pointer is outside, clear the critical
268 * section address.
269 *
270 * Returns true, if the section was valid and either fixup or clear was
271 * done, false otherwise.
272 *
273 * In the failure case task::rseq_event::fatal is set when a invalid
274 * section was found. It's clear when the failure was an unresolved page
275 * fault.
276 *
277 * If inlined into the exit to user path with interrupts disabled, the
278 * caller has to protect against page faults with pagefault_disable().
279 *
280 * In preemptible task context this would be counterproductive as the page
281 * faults could not be fully resolved. As a consequence unresolved page
282 * faults in task context are fatal too.
283 */
284
285 #ifdef RSEQ_BUILD_SLOW_PATH
286 /*
287 * The debug version is put out of line, but kept here so the code stays
288 * together.
289 *
290 * @csaddr has already been checked by the caller to be in user space
291 */
292 bool rseq_debug_update_user_cs(struct task_struct *t, struct pt_regs *regs,
293 unsigned long csaddr)
294 {
295 struct rseq_cs __user *ucs = (struct rseq_cs __user *)(unsigned long)csaddr;
296 u64 start_ip, abort_ip, offset, cs_end, head, tasksize = TASK_SIZE;
297 unsigned long ip = instruction_pointer(regs);
298 u64 __user *uc_head = (u64 __user *) ucs;
299 u32 usig, __user *uc_sig;
300
301 scoped_user_rw_access(ucs, efault) {
302 /*
303 * Evaluate the user pile and exit if one of the conditions
304 * is not fulfilled.
305 */
306 unsafe_get_user(start_ip, &ucs->start_ip, efault);
307 if (unlikely(start_ip >= tasksize))
308 goto die;
309 /* If outside, just clear the critical section. */
310 if (ip < start_ip)
311 goto clear;
312
313 unsafe_get_user(offset, &ucs->post_commit_offset, efault);
314 cs_end = start_ip + offset;
315 /* Check for overflow and wraparound */
316 if (unlikely(cs_end >= tasksize || cs_end < start_ip))
317 goto die;
318
319 /* If not inside, clear it. */
320 if (ip >= cs_end)
321 goto clear;
322
323 unsafe_get_user(abort_ip, &ucs->abort_ip, efault);
324 /* Ensure it's "valid" */
325 if (unlikely(abort_ip >= tasksize || abort_ip < sizeof(*uc_sig)))
326 goto die;
327 /* Validate that the abort IP is not in the critical section */
328 if (unlikely(abort_ip - start_ip < offset))
329 goto die;
330
331 /*
332 * Check version and flags for 0. No point in emitting
333 * deprecated warnings before dying. That could be done in
334 * the slow path eventually, but *shrug*.
335 */
336 unsafe_get_user(head, uc_head, efault);
337 if (unlikely(head))
338 goto die;
339
340 /* abort_ip - 4 is >= 0. See abort_ip check above */
341 uc_sig = (u32 __user *)(unsigned long)(abort_ip - sizeof(*uc_sig));
342 unsafe_get_user(usig, uc_sig, efault);
343 if (unlikely(usig != t->rseq.sig))
344 goto die;
345
346 /* rseq_event.user_irq is only valid if CONFIG_GENERIC_IRQ_ENTRY=y */
347 if (IS_ENABLED(CONFIG_GENERIC_IRQ_ENTRY)) {
348 /* If not in interrupt from user context, let it die */
349 if (unlikely(!t->rseq.event.user_irq))
350 goto die;
351 }
352 unsafe_put_user(0ULL, &t->rseq.usrptr->rseq_cs, efault);
353 instruction_pointer_set(regs, (unsigned long)abort_ip);
354 rseq_stat_inc(rseq_stats.fixup);
355 break;
356 clear:
357 unsafe_put_user(0ULL, &t->rseq.usrptr->rseq_cs, efault);
358 rseq_stat_inc(rseq_stats.clear);
359 abort_ip = 0ULL;
360 }
361
362 if (unlikely(abort_ip))
363 rseq_trace_ip_fixup(ip, start_ip, offset, abort_ip);
364 return true;
365 die:
366 t->rseq.event.fatal = true;
367 efault:
368 return false;
369 }
370
371 #endif /* RSEQ_BUILD_SLOW_PATH */
372
373 /*
374 * This only ensures that abort_ip is in the user address space and
375 * validates that it is preceded by the signature.
376 *
377 * No other sanity checks are done here, that's what the debug code is for.
378 */
379 static rseq_inline bool
380 rseq_update_user_cs(struct task_struct *t, struct pt_regs *regs, unsigned long csaddr)
381 {
382 struct rseq_cs __user *ucs = (struct rseq_cs __user *)(unsigned long)csaddr;
383 unsigned long ip = instruction_pointer(regs);
384 unsigned long tasksize = TASK_SIZE;
385 u64 start_ip, abort_ip, offset;
386 u32 usig, __user *uc_sig;
387
388 rseq_stat_inc(rseq_stats.cs);
389
390 if (unlikely(csaddr >= tasksize)) {
391 t->rseq.event.fatal = true;
392 return false;
393 }
394
395 if (static_branch_unlikely(&rseq_debug_enabled))
396 return rseq_debug_update_user_cs(t, regs, csaddr);
397
398 scoped_user_rw_access(ucs, efault) {
399 unsafe_get_user(start_ip, &ucs->start_ip, efault);
400 unsafe_get_user(offset, &ucs->post_commit_offset, efault);
401 unsafe_get_user(abort_ip, &ucs->abort_ip, efault);
402
403 /*
404 * No sanity checks. If user space screwed it up, it can
405 * keep the pieces. That's what debug code is for.
406 *
407 * If outside, just clear the critical section.
408 */
409 if (ip - start_ip >= offset)
410 goto clear;
411
412 /*
413 * Two requirements for @abort_ip:
414 * - Must be in user space as x86 IRET would happily return to
415 * the kernel.
416 * - The four bytes preceding the instruction at @abort_ip must
417 * contain the signature.
418 *
419 * The latter protects against the following attack vector:
420 *
421 * An attacker with limited abilities to write, creates a critical
422 * section descriptor, sets the abort IP to a library function or
423 * some other ROP gadget and stores the address of the descriptor
424 * in TLS::rseq::rseq_cs. An RSEQ abort would then evade ROP
425 * protection.
426 */
427 if (unlikely(abort_ip >= tasksize || abort_ip < sizeof(*uc_sig)))
428 goto die;
429
430 /* The address is guaranteed to be >= 0 and < TASK_SIZE */
431 uc_sig = (u32 __user *)(unsigned long)(abort_ip - sizeof(*uc_sig));
432 unsafe_get_user(usig, uc_sig, efault);
433 if (unlikely(usig != t->rseq.sig))
434 goto die;
435
436 /* Invalidate the critical section */
437 unsafe_put_user(0ULL, &t->rseq.usrptr->rseq_cs, efault);
438 /* Update the instruction pointer */
439 instruction_pointer_set(regs, (unsigned long)abort_ip);
440 rseq_stat_inc(rseq_stats.fixup);
441 break;
442 clear:
443 unsafe_put_user(0ULL, &t->rseq.usrptr->rseq_cs, efault);
444 rseq_stat_inc(rseq_stats.clear);
445 abort_ip = 0ULL;
446 }
447
448 if (unlikely(abort_ip))
449 rseq_trace_ip_fixup(ip, start_ip, offset, abort_ip);
450 return true;
451 die:
452 t->rseq.event.fatal = true;
453 efault:
454 return false;
455 }
456
457 /*
458 * Updates CPU ID, Node ID and MM CID and reads the critical section
459 * address, when @csaddr != NULL. This allows to put the ID update and the
460 * read under the same uaccess region to spare a separate begin/end.
461 *
462 * As this is either invoked from a C wrapper with @csaddr = NULL or from
463 * the fast path code with a valid pointer, a clever compiler should be
464 * able to optimize the read out. Spares a duplicate implementation.
465 *
466 * Returns true, if the operation was successful, false otherwise.
467 *
468 * In the failure case task::rseq_event::fatal is set when invalid data
469 * was found on debug kernels. It's clear when the failure was an unresolved page
470 * fault.
471 *
472 * If inlined into the exit to user path with interrupts disabled, the
473 * caller has to protect against page faults with pagefault_disable().
474 *
475 * In preemptible task context this would be counterproductive as the page
476 * faults could not be fully resolved. As a consequence unresolved page
477 * faults in task context are fatal too.
478 */
479 static rseq_inline
480 bool rseq_set_ids_get_csaddr(struct task_struct *t, struct rseq_ids *ids, u64 *csaddr)
481 {
482 struct rseq __user *rseq = t->rseq.usrptr;
483
484 scoped_user_rw_access(rseq, efault) {
485 /* Validate the R/O fields for debug and optimized mode */
486 if (static_branch_unlikely(&rseq_debug_enabled) || rseq_v2(t)) {
487 u32 cpu_id, uval;
488
489 unsafe_get_user(cpu_id, &rseq->cpu_id_start, efault);
490 if (cpu_id != t->rseq.ids.cpu_id)
491 goto die;
492 unsafe_get_user(uval, &rseq->cpu_id, efault);
493 if (uval != cpu_id)
494 goto die;
495 unsafe_get_user(uval, &rseq->node_id, efault);
496 if (uval != t->rseq.ids.node_id)
497 goto die;
498 unsafe_get_user(uval, &rseq->mm_cid, efault);
499 if (uval != t->rseq.ids.mm_cid)
500 goto die;
501 }
502
503 unsafe_put_user(ids->cpu_id, &rseq->cpu_id_start, efault);
504 unsafe_put_user(ids->cpu_id, &rseq->cpu_id, efault);
505 unsafe_put_user(ids->node_id, &rseq->node_id, efault);
506 unsafe_put_user(ids->mm_cid, &rseq->mm_cid, efault);
507 if (csaddr)
508 unsafe_get_user(*csaddr, &rseq->rseq_cs, efault);
509
510 /* RSEQ ABI V2 only operations */
511 if (rseq_v2(t))
512 rseq_slice_clear_user(rseq, efault);
513 }
514
515 rseq_slice_clear_grant(t);
516 /* Cache the new values */
517 t->rseq.ids = *ids;
518 rseq_stat_inc(rseq_stats.ids);
519 rseq_trace_update(t, ids);
520 return true;
521
522 die:
523 t->rseq.event.fatal = true;
524 efault:
525 return false;
526 }
527
528 /*
529 * Update user space with new IDs and conditionally check whether the task
530 * is in a critical section.
531 */
532 static rseq_inline bool rseq_update_usr(struct task_struct *t, struct pt_regs *regs,
533 struct rseq_ids *ids)
534 {
535 u64 csaddr;
536
537 if (!rseq_set_ids_get_csaddr(t, ids, &csaddr))
538 return false;
539
540 /*
541 * On architectures which utilize the generic entry code this
542 * allows to skip the critical section when the entry was not from
543 * a user space interrupt, unless debug mode is enabled.
544 */
545 if (IS_ENABLED(CONFIG_GENERIC_IRQ_ENTRY)) {
546 if (!static_branch_unlikely(&rseq_debug_enabled)) {
547 if (likely(!t->rseq.event.user_irq))
548 return true;
549 }
550 }
551 if (likely(!csaddr))
552 return true;
553 /* Sigh, this really needs to do work */
554 return rseq_update_user_cs(t, regs, csaddr);
555 }
556
557 /*
558 * If you want to use this then convert your architecture to the generic
559 * entry code. I'm tired of building workarounds for people who can't be
560 * bothered to make the maintenance of generic infrastructure less
561 * burdensome. Just sucking everything into the architecture code and
562 * thereby making others chase the horrible hacks and keep them working is
563 * neither acceptable nor sustainable.
564 */
565 #ifdef CONFIG_GENERIC_ENTRY
566
567 /*
568 * This is inlined into the exit path because:
569 *
570 * 1) It's a one time comparison in the fast path when there is no event to
571 * handle
572 *
573 * 2) The access to the user space rseq memory (TLS) is unlikely to fault
574 * so the straight inline operation is:
575 *
576 * - Four 32-bit stores only if CPU ID/ MM CID need to be updated
577 * - One 64-bit load to retrieve the critical section address
578 *
579 * 3) In the unlikely case that the critical section address is != NULL:
580 *
581 * - One 64-bit load to retrieve the start IP
582 * - One 64-bit load to retrieve the offset for calculating the end
583 * - One 64-bit load to retrieve the abort IP
584 * - One 64-bit load to retrieve the signature
585 * - One store to clear the critical section address
586 *
587 * The non-debug case implements only the minimal required checking. It
588 * provides protection against a rogue abort IP in kernel space, which
589 * would be exploitable at least on x86, and also against a rogue CS
590 * descriptor by checking the signature at the abort IP. Any fallout from
591 * invalid critical section descriptors is a user space problem. The debug
592 * case provides the full set of checks and terminates the task if a
593 * condition is not met.
594 *
595 * In case of a fault or an invalid value, this sets TIF_NOTIFY_RESUME and
596 * tells the caller to loop back into exit_to_user_mode_loop(). The rseq
597 * slow path there will handle the failure.
598 */
599 static __always_inline bool rseq_exit_user_update(struct pt_regs *regs, struct task_struct *t)
600 {
601 /*
602 * Page faults need to be disabled as this is called with
603 * interrupts disabled
604 */
605 guard(pagefault)();
606 /*
607 * This optimization is only valid when the task registered for the
608 * optimized RSEQ_ABI_V2 variant. Some legacy users rely on the original
609 * RSEQ implementation behaviour which unconditionally updated the IDs.
610 * rseq_sched_switch_event() ensures that legacy registrations always
611 * have both sched_switch and ids_changed set, which is compatible with
612 * the historical TIF_NOTIFY_RESUME behaviour.
613 */
614 if (likely(!t->rseq.event.ids_changed)) {
615 struct rseq __user *rseq = t->rseq.usrptr;
616 /*
617 * If IDs have not changed rseq_event::user_irq must be true
618 * See rseq_sched_switch_event().
619 */
620 u64 csaddr;
621
622 scoped_user_rw_access(rseq, efault) {
623 unsafe_get_user(csaddr, &rseq->rseq_cs, efault);
624
625 /* RSEQ ABI V2 only operations */
626 if (rseq_v2(t))
627 rseq_slice_clear_user(rseq, efault);
628 }
629
630 rseq_slice_clear_grant(t);
631
632 if (static_branch_unlikely(&rseq_debug_enabled) || unlikely(csaddr)) {
633 if (unlikely(!rseq_update_user_cs(t, regs, csaddr)))
634 return false;
635 }
636 return true;
637 }
638
639 int cpu = task_cpu(t);
640 struct rseq_ids ids = {
641 .cpu_id = cpu,
642 .mm_cid = task_mm_cid(t),
643 .node_id = cpu_to_node(cpu),
644 };
645
646 return rseq_update_usr(t, regs, &ids);
647 efault:
648 return false;
649 }
650
651 static __always_inline bool __rseq_exit_to_user_mode_restart(struct pt_regs *regs)
652 {
653 struct task_struct *t = current;
654
655 /*
656 * If the task did not go through schedule or got the flag enforced
657 * by the rseq syscall or execve, then nothing to do here.
658 *
659 * CPU ID and MM CID can only change when going through a context
660 * switch.
661 *
662 * rseq_sched_switch_event() sets the rseq_event::sched_switch bit
663 * only when rseq_event::has_rseq is true. That conditional is
664 * required to avoid setting the TIF bit if RSEQ is not registered
665 * for a task. rseq_event::sched_switch is cleared when RSEQ is
666 * unregistered by a task so it's sufficient to check for the
667 * sched_switch bit alone.
668 *
669 * A sane compiler requires three instructions for the nothing to do
670 * case including clearing the events, but your mileage might vary.
671 */
672 if (unlikely((t->rseq.event.sched_switch))) {
673 rseq_stat_inc(rseq_stats.fastpath);
674
675 if (unlikely(!rseq_exit_user_update(regs, t)))
676 return true;
677 }
678 /* Clear state so next entry starts from a clean slate */
679 t->rseq.event.events = 0;
680 return false;
681 }
682
683 /* Required to allow conversion to GENERIC_ENTRY w/o GENERIC_TIF_BITS */
684 #ifdef CONFIG_HAVE_GENERIC_TIF_BITS
685 static __always_inline bool test_tif_rseq(unsigned long ti_work)
686 {
687 return ti_work & _TIF_RSEQ;
688 }
689
690 static __always_inline void clear_tif_rseq(void)
691 {
692 static_assert(TIF_RSEQ != TIF_NOTIFY_RESUME);
693 clear_thread_flag(TIF_RSEQ);
694 }
695 #else
696 static __always_inline bool test_tif_rseq(unsigned long ti_work) { return true; }
697 static __always_inline void clear_tif_rseq(void) { }
698 #endif
699
700 static __always_inline bool
701 rseq_exit_to_user_mode_restart(struct pt_regs *regs, unsigned long ti_work)
702 {
703 if (unlikely(test_tif_rseq(ti_work))) {
704 if (unlikely(__rseq_exit_to_user_mode_restart(regs))) {
705 current->rseq.event.slowpath = true;
706 set_tsk_thread_flag(current, TIF_NOTIFY_RESUME);
707 return true;
708 }
709 clear_tif_rseq();
710 }
711 /*
712 * Arm the slice extension timer if nothing to do anymore and the
713 * task really goes out to user space.
714 */
715 return rseq_arm_slice_extension_timer();
716 }
717
718 #else /* CONFIG_GENERIC_ENTRY */
719 static inline bool rseq_exit_to_user_mode_restart(struct pt_regs *regs, unsigned long ti_work)
720 {
721 return false;
722 }
723 #endif /* !CONFIG_GENERIC_ENTRY */
724
725 static __always_inline void rseq_syscall_exit_to_user_mode(void)
726 {
727 struct rseq_event *ev = &current->rseq.event;
728
729 rseq_stat_inc(rseq_stats.exit);
730
731 /* Needed to remove the store for the !lockdep case */
732 if (IS_ENABLED(CONFIG_LOCKDEP)) {
733 WARN_ON_ONCE(ev->sched_switch);
734 ev->events = 0;
735 }
736 }
737
738 static __always_inline void rseq_irqentry_exit_to_user_mode(void)
739 {
740 struct rseq_event *ev = &current->rseq.event;
741
742 rseq_stat_inc(rseq_stats.exit);
743
744 lockdep_assert_once(!ev->sched_switch);
745
746 /*
747 * Ensure that event (especially user_irq) is cleared when the
748 * interrupt did not result in a schedule and therefore the
749 * rseq processing could not clear it.
750 */
751 ev->events = 0;
752 }
753
754 void __rseq_debug_syscall_return(struct pt_regs *regs);
755
756 static __always_inline void rseq_debug_syscall_return(struct pt_regs *regs)
757 {
758 if (static_branch_unlikely(&rseq_debug_enabled))
759 __rseq_debug_syscall_return(regs);
760 }
761 #else /* CONFIG_RSEQ */
762 static inline void rseq_note_user_irq_entry(void) { }
763 static inline bool rseq_exit_to_user_mode_restart(struct pt_regs *regs, unsigned long ti_work)
764 {
765 return false;
766 }
767 static inline void rseq_syscall_exit_to_user_mode(void) { }
768 static inline void rseq_irqentry_exit_to_user_mode(void) { }
769 static inline void rseq_debug_syscall_return(struct pt_regs *regs) { }
770 static inline bool rseq_grant_slice_extension(unsigned long ti_work, unsigned long mask) { return false; }
771 #endif /* !CONFIG_RSEQ */
772
773 #endif /* _LINUX_RSEQ_ENTRY_H */