context_tracking: Remove full dynticks' hacky dependency on wide context tracking
[deliverable/linux.git] / kernel / context_tracking.c
CommitLineData
4eacdf18
FW
1/*
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
91d1aa43
FW
17#include <linux/context_tracking.h>
18#include <linux/rcupdate.h>
19#include <linux/sched.h>
91d1aa43 20#include <linux/hardirq.h>
6a61671b 21#include <linux/export.h>
91d1aa43 22
95a79fd4 23DEFINE_PER_CPU(struct context_tracking, context_tracking) = {
91d1aa43
FW
24#ifdef CONFIG_CONTEXT_TRACKING_FORCE
25 .active = true,
26#endif
27};
28
2e709338
FW
29void context_tracking_cpu_set(int cpu)
30{
31 per_cpu(context_tracking.active, cpu) = true;
32}
33
4eacdf18
FW
34/**
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 */
91d1aa43
FW
43void 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
4eacdf18 58 /* Kernel threads aren't supposed to go to userspace */
91d1aa43
FW
59 WARN_ON_ONCE(!current->mm);
60
61 local_irq_save(flags);
d65ec121
FW
62 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 }
4eacdf18 74 /*
d65ec121
FW
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
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.
4eacdf18 86 */
abf917cd 87 __this_cpu_write(context_tracking.state, IN_USER);
91d1aa43
FW
88 }
89 local_irq_restore(flags);
90}
91
29bb9e5a
SR
92#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{
29bb9e5a
SR
109 enum ctx_state prev_ctx;
110
fbb00b56 111 if (likely(!preemptible()))
29bb9e5a
SR
112 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 */
4eacdf18
FW
131
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 */
91d1aa43
FW
143void user_exit(void)
144{
145 unsigned long flags;
146
91d1aa43
FW
147 if (in_interrupt())
148 return;
149
150 local_irq_save(flags);
151 if (__this_cpu_read(context_tracking.state) == IN_USER) {
d65ec121
FW
152 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 }
abf917cd 160 __this_cpu_write(context_tracking.state, IN_KERNEL);
91d1aa43
FW
161 }
162 local_irq_restore(flags);
163}
164
2d854e57 165#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
6a61671b
FW
166void guest_enter(void)
167{
168 if (vtime_accounting_enabled())
169 vtime_guest_enter(current);
170 else
2d854e57 171 current->flags |= PF_VCPU;
6a61671b
FW
172}
173EXPORT_SYMBOL_GPL(guest_enter);
174
175void guest_exit(void)
176{
177 if (vtime_accounting_enabled())
178 vtime_guest_exit(current);
179 else
2d854e57 180 current->flags &= ~PF_VCPU;
6a61671b
FW
181}
182EXPORT_SYMBOL_GPL(guest_exit);
2d854e57 183#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
6a61671b 184
4eacdf18
FW
185
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 */
91d1aa43
FW
199void context_tracking_task_switch(struct task_struct *prev,
200 struct task_struct *next)
201{
d65ec121
FW
202 clear_tsk_thread_flag(prev, TIF_NOHZ);
203 set_tsk_thread_flag(next, TIF_NOHZ);
91d1aa43 204}
This page took 0.067713 seconds and 5 git commands to generate.