blob: 72bcb2570d3e0db89bddf7f8e2aa770751c49eec [file] [log] [blame]
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +01001/*
2 * Context tracking: Probe on high level context boundaries such as kernel
3 * and userspace. This includes syscalls and exceptions entry/exit.
4 *
5 * This is used by RCU to remove its dependency on the timer tick while a CPU
6 * runs in userspace.
7 *
8 * Started by Frederic Weisbecker:
9 *
10 * Copyright (C) 2012 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
11 *
12 * Many thanks to Gilad Ben-Yossef, Paul McKenney, Ingo Molnar, Andrew Morton,
13 * Steven Rostedt, Peter Zijlstra for suggestions and improvements.
14 *
15 */
16
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010017#include <linux/context_tracking.h>
18#include <linux/rcupdate.h>
19#include <linux/sched.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010020#include <linux/hardirq.h>
Frederic Weisbecker6a616712012-12-16 20:00:34 +010021#include <linux/export.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010022
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010023DEFINE_PER_CPU(struct context_tracking, context_tracking) = {
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010024#ifdef CONFIG_CONTEXT_TRACKING_FORCE
25 .active = true,
26#endif
27};
28
Frederic Weisbecker2e709332013-07-10 00:55:25 +020029void context_tracking_cpu_set(int cpu)
30{
31 per_cpu(context_tracking.active, cpu) = true;
32}
33
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +010034/**
35 * user_enter - Inform the context tracking that the CPU is going to
36 * enter userspace mode.
37 *
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.
42 */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010043void user_enter(void)
44{
45 unsigned long flags;
46
47 /*
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.
54 */
55 if (in_interrupt())
56 return;
57
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +010058 /* Kernel threads aren't supposed to go to userspace */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010059 WARN_ON_ONCE(!current->mm);
60
61 local_irq_save(flags);
Frederic Weisbeckerd65ec122013-07-11 23:59:33 +020062 if ( __this_cpu_read(context_tracking.state) != IN_USER) {
63 if (__this_cpu_read(context_tracking.active)) {
64 /*
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
69 * on the tick.
70 */
71 vtime_user_enter(current);
72 rcu_user_enter();
73 }
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +010074 /*
Frederic Weisbeckerd65ec122013-07-11 23:59:33 +020075 * 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
78 * other CPUs.
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
83 * exception_enter().
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.
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +010086 */
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +020087 __this_cpu_write(context_tracking.state, IN_USER);
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010088 }
89 local_irq_restore(flags);
90}
91
Steven Rostedt29bb9e52013-05-24 15:23:40 -040092#ifdef CONFIG_PREEMPT
93/**
94 * preempt_schedule_context - preempt_schedule called by tracing
95 *
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.
102 *
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.
106 */
107void __sched notrace preempt_schedule_context(void)
108{
Steven Rostedt29bb9e52013-05-24 15:23:40 -0400109 enum ctx_state prev_ctx;
110
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +0200111 if (likely(!preemptible()))
Steven Rostedt29bb9e52013-05-24 15:23:40 -0400112 return;
113
114 /*
115 * Need to disable preemption in case user_exit() is traced
116 * and the tracer calls preempt_enable_notrace() causing
117 * an infinite recursion.
118 */
119 preempt_disable_notrace();
120 prev_ctx = exception_enter();
121 preempt_enable_no_resched_notrace();
122
123 preempt_schedule();
124
125 preempt_disable_notrace();
126 exception_exit(prev_ctx);
127 preempt_enable_notrace();
128}
129EXPORT_SYMBOL_GPL(preempt_schedule_context);
130#endif /* CONFIG_PREEMPT */
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +0100131
132/**
133 * user_exit - Inform the context tracking that the CPU is
134 * exiting userspace mode and entering the kernel.
135 *
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...
139 *
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.
142 */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100143void user_exit(void)
144{
145 unsigned long flags;
146
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100147 if (in_interrupt())
148 return;
149
150 local_irq_save(flags);
151 if (__this_cpu_read(context_tracking.state) == IN_USER) {
Frederic Weisbeckerd65ec122013-07-11 23:59:33 +0200152 if (__this_cpu_read(context_tracking.active)) {
153 /*
154 * We are going to run code that may use RCU. Inform
155 * RCU core about that (ie: we may need the tick again).
156 */
157 rcu_user_exit();
158 vtime_user_exit(current);
159 }
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200160 __this_cpu_write(context_tracking.state, IN_KERNEL);
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100161 }
162 local_irq_restore(flags);
163}
164
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200165#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100166void guest_enter(void)
167{
168 if (vtime_accounting_enabled())
169 vtime_guest_enter(current);
170 else
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200171 current->flags |= PF_VCPU;
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100172}
173EXPORT_SYMBOL_GPL(guest_enter);
174
175void guest_exit(void)
176{
177 if (vtime_accounting_enabled())
178 vtime_guest_exit(current);
179 else
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200180 current->flags &= ~PF_VCPU;
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100181}
182EXPORT_SYMBOL_GPL(guest_exit);
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200183#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100184
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +0100185
186/**
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
190 *
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.
194 *
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.
198 */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100199void context_tracking_task_switch(struct task_struct *prev,
200 struct task_struct *next)
201{
Frederic Weisbeckerd65ec122013-07-11 23:59:33 +0200202 clear_tsk_thread_flag(prev, TIF_NOHZ);
203 set_tsk_thread_flag(next, TIF_NOHZ);
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100204}