2 * Context tracking: Probe on high level context boundaries such as kernel
3 * and userspace. This includes syscalls and exceptions entry/exit.
5 * This is used by RCU to remove its dependency on the timer tick while a CPU
8 * Started by Frederic Weisbecker:
10 * Copyright (C) 2012 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
12 * Many thanks to Gilad Ben-Yossef, Paul McKenney, Ingo Molnar, Andrew Morton,
13 * Steven Rostedt, Peter Zijlstra for suggestions and improvements.
17 #include <linux/context_tracking.h>
18 #include <linux/rcupdate.h>
19 #include <linux/sched.h>
20 #include <linux/hardirq.h>
21 #include <linux/export.h>
23 DEFINE_PER_CPU(struct context_tracking, context_tracking) = {
24 #ifdef CONFIG_CONTEXT_TRACKING_FORCE
29 void context_tracking_cpu_set(int cpu)
31 per_cpu(context_tracking.active, cpu) = true;
35 * user_enter - Inform the context tracking that the CPU is going to
36 * enter userspace mode.
38 * This function must be called right before we switch from the kernel
39 * to userspace, when it's guaranteed the remaining kernel instructions
40 * to execute won't use any RCU read side critical section because this
41 * function sets RCU in extended quiescent state.
48 * Some contexts may involve an exception occuring in an irq,
49 * leading to that nesting:
50 * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
51 * This would mess up the dyntick_nesting count though. And rcu_irq_*()
52 * helpers are enough to protect RCU uses inside the exception. So
53 * just return immediately if we detect we are in an IRQ.
58 /* Kernel threads aren't supposed to go to userspace */
59 WARN_ON_ONCE(!current->mm);
61 local_irq_save(flags);
62 if ( __this_cpu_read(context_tracking.state) != IN_USER) {
63 if (__this_cpu_read(context_tracking.active)) {
65 * At this stage, only low level arch entry code remains and
66 * then we'll run in userspace. We can assume there won't be
67 * any RCU read-side critical section until the next call to
68 * user_exit() or rcu_irq_enter(). Let's remove RCU's dependency
71 vtime_user_enter(current);
75 * Even if context tracking is disabled on this CPU, because it's outside
76 * the full dynticks mask for example, we still have to keep track of the
77 * context transitions and states to prevent inconsistency on those of
79 * If a task triggers an exception in userspace, sleep on the exception
80 * handler and then migrate to another CPU, that new CPU must know where
81 * the exception returns by the time we call exception_exit().
82 * This information can only be provided by the previous CPU when it called
84 * OTOH we can spare the calls to vtime and RCU when context_tracking.active
85 * is false because we know that CPU is not tickless.
87 __this_cpu_write(context_tracking.state, IN_USER);
89 local_irq_restore(flags);
94 * preempt_schedule_context - preempt_schedule called by tracing
96 * The tracing infrastructure uses preempt_enable_notrace to prevent
97 * recursion and tracing preempt enabling caused by the tracing
98 * infrastructure itself. But as tracing can happen in areas coming
99 * from userspace or just about to enter userspace, a preempt enable
100 * can occur before user_exit() is called. This will cause the scheduler
101 * to be called when the system is still in usermode.
103 * To prevent this, the preempt_enable_notrace will use this function
104 * instead of preempt_schedule() to exit user context if needed before
105 * calling the scheduler.
107 void __sched notrace preempt_schedule_context(void)
109 enum ctx_state prev_ctx;
111 if (likely(!preemptible()))
115 * Need to disable preemption in case user_exit() is traced
116 * and the tracer calls preempt_enable_notrace() causing
117 * an infinite recursion.
119 preempt_disable_notrace();
120 prev_ctx = exception_enter();
121 preempt_enable_no_resched_notrace();
125 preempt_disable_notrace();
126 exception_exit(prev_ctx);
127 preempt_enable_notrace();
129 EXPORT_SYMBOL_GPL(preempt_schedule_context);
130 #endif /* CONFIG_PREEMPT */
133 * user_exit - Inform the context tracking that the CPU is
134 * exiting userspace mode and entering the kernel.
136 * This function must be called after we entered the kernel from userspace
137 * before any use of RCU read side critical section. This potentially include
138 * any high level kernel code like syscalls, exceptions, signal handling, etc...
140 * This call supports re-entrancy. This way it can be called from any exception
141 * handler without needing to know if we came from userspace or not.
150 local_irq_save(flags);
151 if (__this_cpu_read(context_tracking.state) == IN_USER) {
152 if (__this_cpu_read(context_tracking.active)) {
154 * We are going to run code that may use RCU. Inform
155 * RCU core about that (ie: we may need the tick again).
158 vtime_user_exit(current);
160 __this_cpu_write(context_tracking.state, IN_KERNEL);
162 local_irq_restore(flags);
165 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
166 void guest_enter(void)
168 if (vtime_accounting_enabled())
169 vtime_guest_enter(current);
171 current->flags |= PF_VCPU;
173 EXPORT_SYMBOL_GPL(guest_enter);
175 void guest_exit(void)
177 if (vtime_accounting_enabled())
178 vtime_guest_exit(current);
180 current->flags &= ~PF_VCPU;
182 EXPORT_SYMBOL_GPL(guest_exit);
183 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
187 * context_tracking_task_switch - context switch the syscall callbacks
188 * @prev: the task that is being switched out
189 * @next: the task that is being switched in
191 * The context tracking uses the syscall slow path to implement its user-kernel
192 * boundaries probes on syscalls. This way it doesn't impact the syscall fast
193 * path on CPUs that don't do context tracking.
195 * But we need to clear the flag on the previous task because it may later
196 * migrate to some CPU that doesn't do the context tracking. As such the TIF
197 * flag may not be desired there.
199 void context_tracking_task_switch(struct task_struct *prev,
200 struct task_struct *next)
202 clear_tsk_thread_flag(prev, TIF_NOHZ);
203 set_tsk_thread_flag(next, TIF_NOHZ);