xref: /linux/kernel/entry/common.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1142781e1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
2142781e1SThomas Gleixner 
3142781e1SThomas Gleixner #include <linux/context_tracking.h>
4142781e1SThomas Gleixner #include <linux/entry-common.h>
503248addSEric W. Biederman #include <linux/resume_user_mode.h>
65fbda3ecSThomas Gleixner #include <linux/highmem.h>
799cf983cSMark Rutland #include <linux/jump_label.h>
86cae637fSAlexander Potapenko #include <linux/kmsan.h>
9a9f3a74aSThomas Gleixner #include <linux/livepatch.h>
10a9f3a74aSThomas Gleixner #include <linux/audit.h>
11f268c373SFrederic Weisbecker #include <linux/tick.h>
12142781e1SThomas Gleixner 
1311894468SGabriel Krisman Bertazi #include "common.h"
1411894468SGabriel Krisman Bertazi 
15142781e1SThomas Gleixner #define CREATE_TRACE_POINTS
16142781e1SThomas Gleixner #include <trace/events/syscalls.h>
17142781e1SThomas Gleixner 
syscall_enter_audit(struct pt_regs * regs,long syscall)18142781e1SThomas Gleixner static inline void syscall_enter_audit(struct pt_regs *regs, long syscall)
19142781e1SThomas Gleixner {
20142781e1SThomas Gleixner 	if (unlikely(audit_context())) {
21142781e1SThomas Gleixner 		unsigned long args[6];
22142781e1SThomas Gleixner 
23142781e1SThomas Gleixner 		syscall_get_arguments(current, regs, args);
24142781e1SThomas Gleixner 		audit_syscall_entry(syscall, args[0], args[1], args[2], args[3]);
25142781e1SThomas Gleixner 	}
26142781e1SThomas Gleixner }
27142781e1SThomas Gleixner 
syscall_trace_enter(struct pt_regs * regs,long syscall,unsigned long work)28221a1640SSven Schnelle long syscall_trace_enter(struct pt_regs *regs, long syscall,
2929915524SGabriel Krisman Bertazi 				unsigned long work)
30142781e1SThomas Gleixner {
31142781e1SThomas Gleixner 	long ret = 0;
32142781e1SThomas Gleixner 
3311894468SGabriel Krisman Bertazi 	/*
3411894468SGabriel Krisman Bertazi 	 * Handle Syscall User Dispatch.  This must comes first, since
3511894468SGabriel Krisman Bertazi 	 * the ABI here can be something that doesn't make sense for
3611894468SGabriel Krisman Bertazi 	 * other syscall_work features.
3711894468SGabriel Krisman Bertazi 	 */
3811894468SGabriel Krisman Bertazi 	if (work & SYSCALL_WORK_SYSCALL_USER_DISPATCH) {
3911894468SGabriel Krisman Bertazi 		if (syscall_user_dispatch(regs))
4011894468SGabriel Krisman Bertazi 			return -1L;
4111894468SGabriel Krisman Bertazi 	}
4211894468SGabriel Krisman Bertazi 
43142781e1SThomas Gleixner 	/* Handle ptrace */
4464eb35f7SGabriel Krisman Bertazi 	if (work & (SYSCALL_WORK_SYSCALL_TRACE | SYSCALL_WORK_SYSCALL_EMU)) {
450cfcb2b9SEric W. Biederman 		ret = ptrace_report_syscall_entry(regs);
4664eb35f7SGabriel Krisman Bertazi 		if (ret || (work & SYSCALL_WORK_SYSCALL_EMU))
47142781e1SThomas Gleixner 			return -1L;
48142781e1SThomas Gleixner 	}
49142781e1SThomas Gleixner 
50142781e1SThomas Gleixner 	/* Do seccomp after ptrace, to catch any tracer changes. */
5123d67a54SGabriel Krisman Bertazi 	if (work & SYSCALL_WORK_SECCOMP) {
52142781e1SThomas Gleixner 		ret = __secure_computing(NULL);
53142781e1SThomas Gleixner 		if (ret == -1L)
54142781e1SThomas Gleixner 			return ret;
55142781e1SThomas Gleixner 	}
56142781e1SThomas Gleixner 
57b6ec4134SKees Cook 	/* Either of the above might have changed the syscall number */
58b6ec4134SKees Cook 	syscall = syscall_get_nr(current, regs);
59b6ec4134SKees Cook 
60fb13b11dSAndré Rösti 	if (unlikely(work & SYSCALL_WORK_SYSCALL_TRACEPOINT)) {
61142781e1SThomas Gleixner 		trace_sys_enter(regs, syscall);
62fb13b11dSAndré Rösti 		/*
63fb13b11dSAndré Rösti 		 * Probes or BPF hooks in the tracepoint may have changed the
64fb13b11dSAndré Rösti 		 * system call number as well.
65fb13b11dSAndré Rösti 		 */
66fb13b11dSAndré Rösti 		syscall = syscall_get_nr(current, regs);
67fb13b11dSAndré Rösti 	}
68142781e1SThomas Gleixner 
69142781e1SThomas Gleixner 	syscall_enter_audit(regs, syscall);
70142781e1SThomas Gleixner 
71142781e1SThomas Gleixner 	return ret ? : syscall;
72142781e1SThomas Gleixner }
73142781e1SThomas Gleixner 
syscall_enter_from_user_mode_prepare(struct pt_regs * regs)744facb95bSThomas Gleixner noinstr void syscall_enter_from_user_mode_prepare(struct pt_regs *regs)
754facb95bSThomas Gleixner {
76caf4062eSSven Schnelle 	enter_from_user_mode(regs);
774facb95bSThomas Gleixner 	instrumentation_begin();
784facb95bSThomas Gleixner 	local_irq_enable();
794facb95bSThomas Gleixner 	instrumentation_end();
804facb95bSThomas Gleixner }
814facb95bSThomas Gleixner 
82a9f3a74aSThomas Gleixner /* Workaround to allow gradual conversion of architecture code */
arch_do_signal_or_restart(struct pt_regs * regs)838ba62d37SEric W. Biederman void __weak arch_do_signal_or_restart(struct pt_regs *regs) { }
84a9f3a74aSThomas Gleixner 
85d6801947SSven Schnelle /**
86d6801947SSven Schnelle  * exit_to_user_mode_loop - do any pending work before leaving to user space
87d6801947SSven Schnelle  * @regs:	Pointer to pt_regs on entry stack
88d6801947SSven Schnelle  * @ti_work:	TIF work flags as read by the caller
89d6801947SSven Schnelle  */
exit_to_user_mode_loop(struct pt_regs * regs,unsigned long ti_work)90d6801947SSven Schnelle __always_inline unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
91a9f3a74aSThomas Gleixner 						     unsigned long ti_work)
92a9f3a74aSThomas Gleixner {
93a9f3a74aSThomas Gleixner 	/*
94a9f3a74aSThomas Gleixner 	 * Before returning to user space ensure that all pending work
95a9f3a74aSThomas Gleixner 	 * items have been completed.
96a9f3a74aSThomas Gleixner 	 */
97a9f3a74aSThomas Gleixner 	while (ti_work & EXIT_TO_USER_MODE_WORK) {
98a9f3a74aSThomas Gleixner 
99a9f3a74aSThomas Gleixner 		local_irq_enable_exit_to_user(ti_work);
100a9f3a74aSThomas Gleixner 
101a9f3a74aSThomas Gleixner 		if (ti_work & _TIF_NEED_RESCHED)
102a9f3a74aSThomas Gleixner 			schedule();
103a9f3a74aSThomas Gleixner 
104a9f3a74aSThomas Gleixner 		if (ti_work & _TIF_UPROBE)
105a9f3a74aSThomas Gleixner 			uprobe_notify_resume(regs);
106a9f3a74aSThomas Gleixner 
107a9f3a74aSThomas Gleixner 		if (ti_work & _TIF_PATCH_PENDING)
108a9f3a74aSThomas Gleixner 			klp_update_patch_state(current);
109a9f3a74aSThomas Gleixner 
11012db8b69SJens Axboe 		if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
1118ba62d37SEric W. Biederman 			arch_do_signal_or_restart(regs);
112a9f3a74aSThomas Gleixner 
113a68de80fSSean Christopherson 		if (ti_work & _TIF_NOTIFY_RESUME)
11403248addSEric W. Biederman 			resume_user_mode_work(regs);
115a9f3a74aSThomas Gleixner 
116a9f3a74aSThomas Gleixner 		/* Architecture specific TIF work */
117a9f3a74aSThomas Gleixner 		arch_exit_to_user_mode_work(regs, ti_work);
118a9f3a74aSThomas Gleixner 
119a9f3a74aSThomas Gleixner 		/*
120a9f3a74aSThomas Gleixner 		 * Disable interrupts and reevaluate the work flags as they
121a9f3a74aSThomas Gleixner 		 * might have changed while interrupts and preemption was
122a9f3a74aSThomas Gleixner 		 * enabled above.
123a9f3a74aSThomas Gleixner 		 */
124a9f3a74aSThomas Gleixner 		local_irq_disable_exit_to_user();
12547b8ff19SFrederic Weisbecker 
12647b8ff19SFrederic Weisbecker 		/* Check if any of the above work has queued a deferred wakeup */
127f268c373SFrederic Weisbecker 		tick_nohz_user_enter_prepare();
12847b8ff19SFrederic Weisbecker 
1296ce89512SMark Rutland 		ti_work = read_thread_flags();
130a9f3a74aSThomas Gleixner 	}
131a9f3a74aSThomas Gleixner 
132a9f3a74aSThomas Gleixner 	/* Return the latest work state for arch_exit_to_user_mode() */
133a9f3a74aSThomas Gleixner 	return ti_work;
134a9f3a74aSThomas Gleixner }
135a9f3a74aSThomas Gleixner 
136a9f3a74aSThomas Gleixner /*
13764eb35f7SGabriel Krisman Bertazi  * If SYSCALL_EMU is set, then the only reason to report is when
1386342adcaSGabriel Krisman Bertazi  * SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP).  This syscall
139900ffe39SKees Cook  * instruction has been already reported in syscall_enter_from_user_mode().
140a9f3a74aSThomas Gleixner  */
report_single_step(unsigned long work)14164eb35f7SGabriel Krisman Bertazi static inline bool report_single_step(unsigned long work)
142a9f3a74aSThomas Gleixner {
14341c1a06dSYuxuan Shui 	if (work & SYSCALL_WORK_SYSCALL_EMU)
14464eb35f7SGabriel Krisman Bertazi 		return false;
14564eb35f7SGabriel Krisman Bertazi 
1466342adcaSGabriel Krisman Bertazi 	return work & SYSCALL_WORK_SYSCALL_EXIT_TRAP;
147a9f3a74aSThomas Gleixner }
14829915524SGabriel Krisman Bertazi 
syscall_exit_work(struct pt_regs * regs,unsigned long work)14929915524SGabriel Krisman Bertazi static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
150a9f3a74aSThomas Gleixner {
151a9f3a74aSThomas Gleixner 	bool step;
152a9f3a74aSThomas Gleixner 
15311894468SGabriel Krisman Bertazi 	/*
15411894468SGabriel Krisman Bertazi 	 * If the syscall was rolled back due to syscall user dispatching,
15511894468SGabriel Krisman Bertazi 	 * then the tracers below are not invoked for the same reason as
15611894468SGabriel Krisman Bertazi 	 * the entry side was not invoked in syscall_trace_enter(): The ABI
15711894468SGabriel Krisman Bertazi 	 * of these syscalls is unknown.
15811894468SGabriel Krisman Bertazi 	 */
15911894468SGabriel Krisman Bertazi 	if (work & SYSCALL_WORK_SYSCALL_USER_DISPATCH) {
16011894468SGabriel Krisman Bertazi 		if (unlikely(current->syscall_dispatch.on_dispatch)) {
16111894468SGabriel Krisman Bertazi 			current->syscall_dispatch.on_dispatch = false;
16211894468SGabriel Krisman Bertazi 			return;
16311894468SGabriel Krisman Bertazi 		}
16411894468SGabriel Krisman Bertazi 	}
16511894468SGabriel Krisman Bertazi 
166a9f3a74aSThomas Gleixner 	audit_syscall_exit(regs);
167a9f3a74aSThomas Gleixner 
168524666cbSGabriel Krisman Bertazi 	if (work & SYSCALL_WORK_SYSCALL_TRACEPOINT)
169a9f3a74aSThomas Gleixner 		trace_sys_exit(regs, syscall_get_return_value(current, regs));
170a9f3a74aSThomas Gleixner 
17164eb35f7SGabriel Krisman Bertazi 	step = report_single_step(work);
17264c19ba2SGabriel Krisman Bertazi 	if (step || work & SYSCALL_WORK_SYSCALL_TRACE)
1730cfcb2b9SEric W. Biederman 		ptrace_report_syscall_exit(regs, step);
174a9f3a74aSThomas Gleixner }
175a9f3a74aSThomas Gleixner 
176a9f3a74aSThomas Gleixner /*
177a9f3a74aSThomas Gleixner  * Syscall specific exit to user mode preparation. Runs with interrupts
178a9f3a74aSThomas Gleixner  * enabled.
179a9f3a74aSThomas Gleixner  */
syscall_exit_to_user_mode_prepare(struct pt_regs * regs)180a9f3a74aSThomas Gleixner static void syscall_exit_to_user_mode_prepare(struct pt_regs *regs)
181a9f3a74aSThomas Gleixner {
182b86678cfSGabriel Krisman Bertazi 	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
183a9f3a74aSThomas Gleixner 	unsigned long nr = syscall_get_nr(current, regs);
184a9f3a74aSThomas Gleixner 
185*d65d411cSValentin Schneider 	CT_WARN_ON(ct_state() != CT_STATE_KERNEL);
186a9f3a74aSThomas Gleixner 
187a9f3a74aSThomas Gleixner 	if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
188a9f3a74aSThomas Gleixner 		if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr))
189a9f3a74aSThomas Gleixner 			local_irq_enable();
190a9f3a74aSThomas Gleixner 	}
191a9f3a74aSThomas Gleixner 
192a9f3a74aSThomas Gleixner 	rseq_syscall(regs);
193a9f3a74aSThomas Gleixner 
194a9f3a74aSThomas Gleixner 	/*
195a9f3a74aSThomas Gleixner 	 * Do one-time syscall specific work. If these work items are
196a9f3a74aSThomas Gleixner 	 * enabled, we want to run them exactly once per syscall exit with
197a9f3a74aSThomas Gleixner 	 * interrupts enabled.
198a9f3a74aSThomas Gleixner 	 */
19929915524SGabriel Krisman Bertazi 	if (unlikely(work & SYSCALL_WORK_EXIT))
20029915524SGabriel Krisman Bertazi 		syscall_exit_work(regs, work);
201a9f3a74aSThomas Gleixner }
202a9f3a74aSThomas Gleixner 
__syscall_exit_to_user_mode_work(struct pt_regs * regs)203c6156e1dSSven Schnelle static __always_inline void __syscall_exit_to_user_mode_work(struct pt_regs *regs)
204a9f3a74aSThomas Gleixner {
205a9f3a74aSThomas Gleixner 	syscall_exit_to_user_mode_prepare(regs);
206a9f3a74aSThomas Gleixner 	local_irq_disable_exit_to_user();
207a9f3a74aSThomas Gleixner 	exit_to_user_mode_prepare(regs);
208c6156e1dSSven Schnelle }
209c6156e1dSSven Schnelle 
syscall_exit_to_user_mode_work(struct pt_regs * regs)210c6156e1dSSven Schnelle void syscall_exit_to_user_mode_work(struct pt_regs *regs)
211c6156e1dSSven Schnelle {
212c6156e1dSSven Schnelle 	__syscall_exit_to_user_mode_work(regs);
213c6156e1dSSven Schnelle }
214c6156e1dSSven Schnelle 
syscall_exit_to_user_mode(struct pt_regs * regs)215c6156e1dSSven Schnelle __visible noinstr void syscall_exit_to_user_mode(struct pt_regs *regs)
216c6156e1dSSven Schnelle {
217c6156e1dSSven Schnelle 	instrumentation_begin();
218c6156e1dSSven Schnelle 	__syscall_exit_to_user_mode_work(regs);
219a9f3a74aSThomas Gleixner 	instrumentation_end();
220d6801947SSven Schnelle 	exit_to_user_mode();
221a9f3a74aSThomas Gleixner }
222a9f3a74aSThomas Gleixner 
irqentry_enter_from_user_mode(struct pt_regs * regs)223142781e1SThomas Gleixner noinstr void irqentry_enter_from_user_mode(struct pt_regs *regs)
224142781e1SThomas Gleixner {
225caf4062eSSven Schnelle 	enter_from_user_mode(regs);
226142781e1SThomas Gleixner }
227a9f3a74aSThomas Gleixner 
irqentry_exit_to_user_mode(struct pt_regs * regs)228a9f3a74aSThomas Gleixner noinstr void irqentry_exit_to_user_mode(struct pt_regs *regs)
229a9f3a74aSThomas Gleixner {
230a9f3a74aSThomas Gleixner 	instrumentation_begin();
231a9f3a74aSThomas Gleixner 	exit_to_user_mode_prepare(regs);
232a9f3a74aSThomas Gleixner 	instrumentation_end();
233d6801947SSven Schnelle 	exit_to_user_mode();
234a9f3a74aSThomas Gleixner }
235a5497babSThomas Gleixner 
irqentry_enter(struct pt_regs * regs)236aadfc2f9SIngo Molnar noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs)
237a5497babSThomas Gleixner {
238a5497babSThomas Gleixner 	irqentry_state_t ret = {
239a5497babSThomas Gleixner 		.exit_rcu = false,
240a5497babSThomas Gleixner 	};
241a5497babSThomas Gleixner 
242a5497babSThomas Gleixner 	if (user_mode(regs)) {
243a5497babSThomas Gleixner 		irqentry_enter_from_user_mode(regs);
244a5497babSThomas Gleixner 		return ret;
245a5497babSThomas Gleixner 	}
246a5497babSThomas Gleixner 
247a5497babSThomas Gleixner 	/*
2486f0e6c15SFrederic Weisbecker 	 * If this entry hit the idle task invoke ct_irq_enter() whether
249a5497babSThomas Gleixner 	 * RCU is watching or not.
250a5497babSThomas Gleixner 	 *
25178a56e04SIra Weiny 	 * Interrupts can nest when the first interrupt invokes softirq
252a5497babSThomas Gleixner 	 * processing on return which enables interrupts.
253a5497babSThomas Gleixner 	 *
254a5497babSThomas Gleixner 	 * Scheduler ticks in the idle task can mark quiescent state and
255a5497babSThomas Gleixner 	 * terminate a grace period, if and only if the timer interrupt is
256a5497babSThomas Gleixner 	 * not nested into another interrupt.
257a5497babSThomas Gleixner 	 *
2587f2a53c2SPaul E. McKenney 	 * Checking for rcu_is_watching() here would prevent the nesting
2596f0e6c15SFrederic Weisbecker 	 * interrupt to invoke ct_irq_enter(). If that nested interrupt is
260a5497babSThomas Gleixner 	 * the tick then rcu_flavor_sched_clock_irq() would wrongfully
26197258ce9SIngo Molnar 	 * assume that it is the first interrupt and eventually claim
26278a56e04SIra Weiny 	 * quiescent state and end grace periods prematurely.
263a5497babSThomas Gleixner 	 *
2646f0e6c15SFrederic Weisbecker 	 * Unconditionally invoke ct_irq_enter() so RCU state stays
265a5497babSThomas Gleixner 	 * consistent.
266a5497babSThomas Gleixner 	 *
267a5497babSThomas Gleixner 	 * TINY_RCU does not support EQS, so let the compiler eliminate
268a5497babSThomas Gleixner 	 * this part when enabled.
269a5497babSThomas Gleixner 	 */
270a5497babSThomas Gleixner 	if (!IS_ENABLED(CONFIG_TINY_RCU) && is_idle_task(current)) {
271a5497babSThomas Gleixner 		/*
272a5497babSThomas Gleixner 		 * If RCU is not watching then the same careful
273a5497babSThomas Gleixner 		 * sequence vs. lockdep and tracing is required
27445ff5105SIra Weiny 		 * as in irqentry_enter_from_user_mode().
275a5497babSThomas Gleixner 		 */
276a5497babSThomas Gleixner 		lockdep_hardirqs_off(CALLER_ADDR0);
2776f0e6c15SFrederic Weisbecker 		ct_irq_enter();
278a5497babSThomas Gleixner 		instrumentation_begin();
2796cae637fSAlexander Potapenko 		kmsan_unpoison_entry_regs(regs);
280a5497babSThomas Gleixner 		trace_hardirqs_off_finish();
281a5497babSThomas Gleixner 		instrumentation_end();
282a5497babSThomas Gleixner 
283a5497babSThomas Gleixner 		ret.exit_rcu = true;
284a5497babSThomas Gleixner 		return ret;
285a5497babSThomas Gleixner 	}
286a5497babSThomas Gleixner 
287a5497babSThomas Gleixner 	/*
288a5497babSThomas Gleixner 	 * If RCU is watching then RCU only wants to check whether it needs
289a5497babSThomas Gleixner 	 * to restart the tick in NOHZ mode. rcu_irq_enter_check_tick()
290a5497babSThomas Gleixner 	 * already contains a warning when RCU is not watching, so no point
291a5497babSThomas Gleixner 	 * in having another one here.
292a5497babSThomas Gleixner 	 */
2939d820f68SThomas Gleixner 	lockdep_hardirqs_off(CALLER_ADDR0);
294a5497babSThomas Gleixner 	instrumentation_begin();
2956cae637fSAlexander Potapenko 	kmsan_unpoison_entry_regs(regs);
296a5497babSThomas Gleixner 	rcu_irq_enter_check_tick();
2979d820f68SThomas Gleixner 	trace_hardirqs_off_finish();
298a5497babSThomas Gleixner 	instrumentation_end();
299a5497babSThomas Gleixner 
300a5497babSThomas Gleixner 	return ret;
301a5497babSThomas Gleixner }
302a5497babSThomas Gleixner 
raw_irqentry_exit_cond_resched(void)3034624a14fSMark Rutland void raw_irqentry_exit_cond_resched(void)
304a5497babSThomas Gleixner {
305a5497babSThomas Gleixner 	if (!preempt_count()) {
306a5497babSThomas Gleixner 		/* Sanity check RCU and thread stack */
307a5497babSThomas Gleixner 		rcu_irq_exit_check_preempt();
308a5497babSThomas Gleixner 		if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
309a5497babSThomas Gleixner 			WARN_ON_ONCE(!on_thread_stack());
310a5497babSThomas Gleixner 		if (need_resched())
311a5497babSThomas Gleixner 			preempt_schedule_irq();
312a5497babSThomas Gleixner 	}
313a5497babSThomas Gleixner }
31440607ee9SPeter Zijlstra (Intel) #ifdef CONFIG_PREEMPT_DYNAMIC
31599cf983cSMark Rutland #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
3164624a14fSMark Rutland DEFINE_STATIC_CALL(irqentry_exit_cond_resched, raw_irqentry_exit_cond_resched);
31799cf983cSMark Rutland #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
31899cf983cSMark Rutland DEFINE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
dynamic_irqentry_exit_cond_resched(void)31999cf983cSMark Rutland void dynamic_irqentry_exit_cond_resched(void)
32099cf983cSMark Rutland {
3210a70045eSSven Schnelle 	if (!static_branch_unlikely(&sk_dynamic_irqentry_exit_cond_resched))
32299cf983cSMark Rutland 		return;
32399cf983cSMark Rutland 	raw_irqentry_exit_cond_resched();
32499cf983cSMark Rutland }
32599cf983cSMark Rutland #endif
32640607ee9SPeter Zijlstra (Intel) #endif
327a5497babSThomas Gleixner 
irqentry_exit(struct pt_regs * regs,irqentry_state_t state)328aadfc2f9SIngo Molnar noinstr void irqentry_exit(struct pt_regs *regs, irqentry_state_t state)
329a5497babSThomas Gleixner {
330a5497babSThomas Gleixner 	lockdep_assert_irqs_disabled();
331a5497babSThomas Gleixner 
332a5497babSThomas Gleixner 	/* Check whether this returns to user mode */
333a5497babSThomas Gleixner 	if (user_mode(regs)) {
334a5497babSThomas Gleixner 		irqentry_exit_to_user_mode(regs);
335a5497babSThomas Gleixner 	} else if (!regs_irqs_disabled(regs)) {
336a5497babSThomas Gleixner 		/*
337a5497babSThomas Gleixner 		 * If RCU was not watching on entry this needs to be done
338a5497babSThomas Gleixner 		 * carefully and needs the same ordering of lockdep/tracing
339a5497babSThomas Gleixner 		 * and RCU as the return to user mode path.
340a5497babSThomas Gleixner 		 */
341a5497babSThomas Gleixner 		if (state.exit_rcu) {
342a5497babSThomas Gleixner 			instrumentation_begin();
343a5497babSThomas Gleixner 			/* Tell the tracer that IRET will enable interrupts */
344a5497babSThomas Gleixner 			trace_hardirqs_on_prepare();
3458b023accSNick Desaulniers 			lockdep_hardirqs_on_prepare();
346a5497babSThomas Gleixner 			instrumentation_end();
3476f0e6c15SFrederic Weisbecker 			ct_irq_exit();
348a5497babSThomas Gleixner 			lockdep_hardirqs_on(CALLER_ADDR0);
349a5497babSThomas Gleixner 			return;
350a5497babSThomas Gleixner 		}
351a5497babSThomas Gleixner 
352a5497babSThomas Gleixner 		instrumentation_begin();
3534624a14fSMark Rutland 		if (IS_ENABLED(CONFIG_PREEMPTION))
354a5497babSThomas Gleixner 			irqentry_exit_cond_resched();
3554624a14fSMark Rutland 
356a5497babSThomas Gleixner 		/* Covers both tracing and lockdep */
357a5497babSThomas Gleixner 		trace_hardirqs_on();
358a5497babSThomas Gleixner 		instrumentation_end();
359a5497babSThomas Gleixner 	} else {
360a5497babSThomas Gleixner 		/*
361a5497babSThomas Gleixner 		 * IRQ flags state is correct already. Just tell RCU if it
362a5497babSThomas Gleixner 		 * was not watching on entry.
363a5497babSThomas Gleixner 		 */
364a5497babSThomas Gleixner 		if (state.exit_rcu)
3656f0e6c15SFrederic Weisbecker 			ct_irq_exit();
366a5497babSThomas Gleixner 	}
367a5497babSThomas Gleixner }
368b6be002bSThomas Gleixner 
irqentry_nmi_enter(struct pt_regs * regs)369b6be002bSThomas Gleixner irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs)
370b6be002bSThomas Gleixner {
371b6be002bSThomas Gleixner 	irqentry_state_t irq_state;
372b6be002bSThomas Gleixner 
373b6be002bSThomas Gleixner 	irq_state.lockdep = lockdep_hardirqs_enabled();
374b6be002bSThomas Gleixner 
375b6be002bSThomas Gleixner 	__nmi_enter();
376b6be002bSThomas Gleixner 	lockdep_hardirqs_off(CALLER_ADDR0);
377b6be002bSThomas Gleixner 	lockdep_hardirq_enter();
378493c1822SFrederic Weisbecker 	ct_nmi_enter();
379b6be002bSThomas Gleixner 
380b6be002bSThomas Gleixner 	instrumentation_begin();
3816cae637fSAlexander Potapenko 	kmsan_unpoison_entry_regs(regs);
382b6be002bSThomas Gleixner 	trace_hardirqs_off_finish();
383b6be002bSThomas Gleixner 	ftrace_nmi_enter();
384b6be002bSThomas Gleixner 	instrumentation_end();
385b6be002bSThomas Gleixner 
386b6be002bSThomas Gleixner 	return irq_state;
387b6be002bSThomas Gleixner }
388b6be002bSThomas Gleixner 
irqentry_nmi_exit(struct pt_regs * regs,irqentry_state_t irq_state)389b6be002bSThomas Gleixner void noinstr irqentry_nmi_exit(struct pt_regs *regs, irqentry_state_t irq_state)
390b6be002bSThomas Gleixner {
391b6be002bSThomas Gleixner 	instrumentation_begin();
392b6be002bSThomas Gleixner 	ftrace_nmi_exit();
393b6be002bSThomas Gleixner 	if (irq_state.lockdep) {
394b6be002bSThomas Gleixner 		trace_hardirqs_on_prepare();
3958b023accSNick Desaulniers 		lockdep_hardirqs_on_prepare();
396b6be002bSThomas Gleixner 	}
397b6be002bSThomas Gleixner 	instrumentation_end();
398b6be002bSThomas Gleixner 
399493c1822SFrederic Weisbecker 	ct_nmi_exit();
400b6be002bSThomas Gleixner 	lockdep_hardirq_exit();
401b6be002bSThomas Gleixner 	if (irq_state.lockdep)
402b6be002bSThomas Gleixner 		lockdep_hardirqs_on(CALLER_ADDR0);
403b6be002bSThomas Gleixner 	__nmi_exit();
404b6be002bSThomas Gleixner }
405