blob: d566aba7e801e1559db28220d63245e7f05aac18 [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>
20#include <linux/percpu.h>
21#include <linux/hardirq.h>
22
23struct context_tracking {
24 /*
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +010025 * When active is false, probes are unset in order
26 * to minimize overhead: TIF flags are cleared
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010027 * and calls to user_enter/exit are ignored. This
28 * may be further optimized using static keys.
29 */
30 bool active;
31 enum {
32 IN_KERNEL = 0,
33 IN_USER,
34 } state;
35};
36
37static DEFINE_PER_CPU(struct context_tracking, context_tracking) = {
38#ifdef CONFIG_CONTEXT_TRACKING_FORCE
39 .active = true,
40#endif
41};
42
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +010043/**
44 * user_enter - Inform the context tracking that the CPU is going to
45 * enter userspace mode.
46 *
47 * This function must be called right before we switch from the kernel
48 * to userspace, when it's guaranteed the remaining kernel instructions
49 * to execute won't use any RCU read side critical section because this
50 * function sets RCU in extended quiescent state.
51 */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010052void user_enter(void)
53{
54 unsigned long flags;
55
56 /*
57 * Some contexts may involve an exception occuring in an irq,
58 * leading to that nesting:
59 * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
60 * This would mess up the dyntick_nesting count though. And rcu_irq_*()
61 * helpers are enough to protect RCU uses inside the exception. So
62 * just return immediately if we detect we are in an IRQ.
63 */
64 if (in_interrupt())
65 return;
66
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +010067 /* Kernel threads aren't supposed to go to userspace */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010068 WARN_ON_ONCE(!current->mm);
69
70 local_irq_save(flags);
71 if (__this_cpu_read(context_tracking.active) &&
72 __this_cpu_read(context_tracking.state) != IN_USER) {
73 __this_cpu_write(context_tracking.state, IN_USER);
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +010074 /*
75 * At this stage, only low level arch entry code remains and
76 * then we'll run in userspace. We can assume there won't be
77 * any RCU read-side critical section until the next call to
78 * user_exit() or rcu_irq_enter(). Let's remove RCU's dependency
79 * on the tick.
80 */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010081 rcu_user_enter();
82 }
83 local_irq_restore(flags);
84}
85
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +010086
87/**
88 * user_exit - Inform the context tracking that the CPU is
89 * exiting userspace mode and entering the kernel.
90 *
91 * This function must be called after we entered the kernel from userspace
92 * before any use of RCU read side critical section. This potentially include
93 * any high level kernel code like syscalls, exceptions, signal handling, etc...
94 *
95 * This call supports re-entrancy. This way it can be called from any exception
96 * handler without needing to know if we came from userspace or not.
97 */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010098void user_exit(void)
99{
100 unsigned long flags;
101
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100102 if (in_interrupt())
103 return;
104
105 local_irq_save(flags);
106 if (__this_cpu_read(context_tracking.state) == IN_USER) {
107 __this_cpu_write(context_tracking.state, IN_KERNEL);
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +0100108 /*
109 * We are going to run code that may use RCU. Inform
110 * RCU core about that (ie: we may need the tick again).
111 */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100112 rcu_user_exit();
113 }
114 local_irq_restore(flags);
115}
116
Frederic Weisbecker4eacdf12013-01-16 17:16:37 +0100117
118/**
119 * context_tracking_task_switch - context switch the syscall callbacks
120 * @prev: the task that is being switched out
121 * @next: the task that is being switched in
122 *
123 * The context tracking uses the syscall slow path to implement its user-kernel
124 * boundaries probes on syscalls. This way it doesn't impact the syscall fast
125 * path on CPUs that don't do context tracking.
126 *
127 * But we need to clear the flag on the previous task because it may later
128 * migrate to some CPU that doesn't do the context tracking. As such the TIF
129 * flag may not be desired there.
130 */
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100131void context_tracking_task_switch(struct task_struct *prev,
132 struct task_struct *next)
133{
134 if (__this_cpu_read(context_tracking.active)) {
135 clear_tsk_thread_flag(prev, TIF_NOHZ);
136 set_tsk_thread_flag(next, TIF_NOHZ);
137 }
138}