xref: /linux/kernel/entry/common.c (revision 6fb5ff63b35b7e849cc8510957f25753f87f63d2)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/irq-entry-common.h>
4 #include <linux/resume_user_mode.h>
5 #include <linux/highmem.h>
6 #include <linux/jump_label.h>
7 #include <linux/kmsan.h>
8 #include <linux/livepatch.h>
9 #include <linux/tick.h>
10 
11 /* Workaround to allow gradual conversion of architecture code */
12 void __weak arch_do_signal_or_restart(struct pt_regs *regs) { }
13 
14 /**
15  * exit_to_user_mode_loop - do any pending work before leaving to user space
16  * @regs:	Pointer to pt_regs on entry stack
17  * @ti_work:	TIF work flags as read by the caller
18  */
19 __always_inline unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
20 						     unsigned long ti_work)
21 {
22 	/*
23 	 * Before returning to user space ensure that all pending work
24 	 * items have been completed.
25 	 */
26 	while (ti_work & EXIT_TO_USER_MODE_WORK) {
27 
28 		local_irq_enable_exit_to_user(ti_work);
29 
30 		if (ti_work & (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY))
31 			schedule();
32 
33 		if (ti_work & _TIF_UPROBE)
34 			uprobe_notify_resume(regs);
35 
36 		if (ti_work & _TIF_PATCH_PENDING)
37 			klp_update_patch_state(current);
38 
39 		if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
40 			arch_do_signal_or_restart(regs);
41 
42 		if (ti_work & _TIF_NOTIFY_RESUME)
43 			resume_user_mode_work(regs);
44 
45 		/* Architecture specific TIF work */
46 		arch_exit_to_user_mode_work(regs, ti_work);
47 
48 		/*
49 		 * Disable interrupts and reevaluate the work flags as they
50 		 * might have changed while interrupts and preemption was
51 		 * enabled above.
52 		 */
53 		local_irq_disable_exit_to_user();
54 
55 		/* Check if any of the above work has queued a deferred wakeup */
56 		tick_nohz_user_enter_prepare();
57 
58 		ti_work = read_thread_flags();
59 	}
60 
61 	/* Return the latest work state for arch_exit_to_user_mode() */
62 	return ti_work;
63 }
64 
65 noinstr void irqentry_enter_from_user_mode(struct pt_regs *regs)
66 {
67 	enter_from_user_mode(regs);
68 }
69 
70 noinstr void irqentry_exit_to_user_mode(struct pt_regs *regs)
71 {
72 	instrumentation_begin();
73 	exit_to_user_mode_prepare(regs);
74 	instrumentation_end();
75 	exit_to_user_mode();
76 }
77 
78 noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs)
79 {
80 	irqentry_state_t ret = {
81 		.exit_rcu = false,
82 	};
83 
84 	if (user_mode(regs)) {
85 		irqentry_enter_from_user_mode(regs);
86 		return ret;
87 	}
88 
89 	/*
90 	 * If this entry hit the idle task invoke ct_irq_enter() whether
91 	 * RCU is watching or not.
92 	 *
93 	 * Interrupts can nest when the first interrupt invokes softirq
94 	 * processing on return which enables interrupts.
95 	 *
96 	 * Scheduler ticks in the idle task can mark quiescent state and
97 	 * terminate a grace period, if and only if the timer interrupt is
98 	 * not nested into another interrupt.
99 	 *
100 	 * Checking for rcu_is_watching() here would prevent the nesting
101 	 * interrupt to invoke ct_irq_enter(). If that nested interrupt is
102 	 * the tick then rcu_flavor_sched_clock_irq() would wrongfully
103 	 * assume that it is the first interrupt and eventually claim
104 	 * quiescent state and end grace periods prematurely.
105 	 *
106 	 * Unconditionally invoke ct_irq_enter() so RCU state stays
107 	 * consistent.
108 	 *
109 	 * TINY_RCU does not support EQS, so let the compiler eliminate
110 	 * this part when enabled.
111 	 */
112 	if (!IS_ENABLED(CONFIG_TINY_RCU) && is_idle_task(current)) {
113 		/*
114 		 * If RCU is not watching then the same careful
115 		 * sequence vs. lockdep and tracing is required
116 		 * as in irqentry_enter_from_user_mode().
117 		 */
118 		lockdep_hardirqs_off(CALLER_ADDR0);
119 		ct_irq_enter();
120 		instrumentation_begin();
121 		kmsan_unpoison_entry_regs(regs);
122 		trace_hardirqs_off_finish();
123 		instrumentation_end();
124 
125 		ret.exit_rcu = true;
126 		return ret;
127 	}
128 
129 	/*
130 	 * If RCU is watching then RCU only wants to check whether it needs
131 	 * to restart the tick in NOHZ mode. rcu_irq_enter_check_tick()
132 	 * already contains a warning when RCU is not watching, so no point
133 	 * in having another one here.
134 	 */
135 	lockdep_hardirqs_off(CALLER_ADDR0);
136 	instrumentation_begin();
137 	kmsan_unpoison_entry_regs(regs);
138 	rcu_irq_enter_check_tick();
139 	trace_hardirqs_off_finish();
140 	instrumentation_end();
141 
142 	return ret;
143 }
144 
145 void raw_irqentry_exit_cond_resched(void)
146 {
147 	if (!preempt_count()) {
148 		/* Sanity check RCU and thread stack */
149 		rcu_irq_exit_check_preempt();
150 		if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
151 			WARN_ON_ONCE(!on_thread_stack());
152 		if (need_resched())
153 			preempt_schedule_irq();
154 	}
155 }
156 #ifdef CONFIG_PREEMPT_DYNAMIC
157 #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
158 DEFINE_STATIC_CALL(irqentry_exit_cond_resched, raw_irqentry_exit_cond_resched);
159 #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
160 DEFINE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
161 void dynamic_irqentry_exit_cond_resched(void)
162 {
163 	if (!static_branch_unlikely(&sk_dynamic_irqentry_exit_cond_resched))
164 		return;
165 	raw_irqentry_exit_cond_resched();
166 }
167 #endif
168 #endif
169 
170 noinstr void irqentry_exit(struct pt_regs *regs, irqentry_state_t state)
171 {
172 	lockdep_assert_irqs_disabled();
173 
174 	/* Check whether this returns to user mode */
175 	if (user_mode(regs)) {
176 		irqentry_exit_to_user_mode(regs);
177 	} else if (!regs_irqs_disabled(regs)) {
178 		/*
179 		 * If RCU was not watching on entry this needs to be done
180 		 * carefully and needs the same ordering of lockdep/tracing
181 		 * and RCU as the return to user mode path.
182 		 */
183 		if (state.exit_rcu) {
184 			instrumentation_begin();
185 			/* Tell the tracer that IRET will enable interrupts */
186 			trace_hardirqs_on_prepare();
187 			lockdep_hardirqs_on_prepare();
188 			instrumentation_end();
189 			ct_irq_exit();
190 			lockdep_hardirqs_on(CALLER_ADDR0);
191 			return;
192 		}
193 
194 		instrumentation_begin();
195 		if (IS_ENABLED(CONFIG_PREEMPTION))
196 			irqentry_exit_cond_resched();
197 
198 		/* Covers both tracing and lockdep */
199 		trace_hardirqs_on();
200 		instrumentation_end();
201 	} else {
202 		/*
203 		 * IRQ flags state is correct already. Just tell RCU if it
204 		 * was not watching on entry.
205 		 */
206 		if (state.exit_rcu)
207 			ct_irq_exit();
208 	}
209 }
210 
211 irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs)
212 {
213 	irqentry_state_t irq_state;
214 
215 	irq_state.lockdep = lockdep_hardirqs_enabled();
216 
217 	__nmi_enter();
218 	lockdep_hardirqs_off(CALLER_ADDR0);
219 	lockdep_hardirq_enter();
220 	ct_nmi_enter();
221 
222 	instrumentation_begin();
223 	kmsan_unpoison_entry_regs(regs);
224 	trace_hardirqs_off_finish();
225 	ftrace_nmi_enter();
226 	instrumentation_end();
227 
228 	return irq_state;
229 }
230 
231 void noinstr irqentry_nmi_exit(struct pt_regs *regs, irqentry_state_t irq_state)
232 {
233 	instrumentation_begin();
234 	ftrace_nmi_exit();
235 	if (irq_state.lockdep) {
236 		trace_hardirqs_on_prepare();
237 		lockdep_hardirqs_on_prepare();
238 	}
239 	instrumentation_end();
240 
241 	ct_nmi_exit();
242 	lockdep_hardirq_exit();
243 	if (irq_state.lockdep)
244 		lockdep_hardirqs_on(CALLER_ADDR0);
245 	__nmi_exit();
246 }
247