aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/context_tracking.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/context_tracking.c')
-rw-r--r--kernel/context_tracking.c75
1 files changed, 65 insertions, 10 deletions
diff --git a/kernel/context_tracking.c b/kernel/context_tracking.c
index e0e07fd55508..d566aba7e801 100644
--- a/kernel/context_tracking.c
+++ b/kernel/context_tracking.c
@@ -1,3 +1,19 @@
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
1#include <linux/context_tracking.h> 17#include <linux/context_tracking.h>
2#include <linux/rcupdate.h> 18#include <linux/rcupdate.h>
3#include <linux/sched.h> 19#include <linux/sched.h>
@@ -6,8 +22,8 @@
6 22
7struct context_tracking { 23struct context_tracking {
8 /* 24 /*
9 * When active is false, hooks are not set to 25 * When active is false, probes are unset in order
10 * minimize overhead: TIF flags are cleared 26 * to minimize overhead: TIF flags are cleared
11 * and calls to user_enter/exit are ignored. This 27 * and calls to user_enter/exit are ignored. This
12 * may be further optimized using static keys. 28 * may be further optimized using static keys.
13 */ 29 */
@@ -24,6 +40,15 @@ static DEFINE_PER_CPU(struct context_tracking, context_tracking) = {
24#endif 40#endif
25}; 41};
26 42
43/**
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 */
27void user_enter(void) 52void user_enter(void)
28{ 53{
29 unsigned long flags; 54 unsigned long flags;
@@ -39,40 +64,70 @@ void user_enter(void)
39 if (in_interrupt()) 64 if (in_interrupt())
40 return; 65 return;
41 66
67 /* Kernel threads aren't supposed to go to userspace */
42 WARN_ON_ONCE(!current->mm); 68 WARN_ON_ONCE(!current->mm);
43 69
44 local_irq_save(flags); 70 local_irq_save(flags);
45 if (__this_cpu_read(context_tracking.active) && 71 if (__this_cpu_read(context_tracking.active) &&
46 __this_cpu_read(context_tracking.state) != IN_USER) { 72 __this_cpu_read(context_tracking.state) != IN_USER) {
47 __this_cpu_write(context_tracking.state, IN_USER); 73 __this_cpu_write(context_tracking.state, IN_USER);
74 /*
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 */
48 rcu_user_enter(); 81 rcu_user_enter();
49 } 82 }
50 local_irq_restore(flags); 83 local_irq_restore(flags);
51} 84}
52 85
86
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 */
53void user_exit(void) 98void user_exit(void)
54{ 99{
55 unsigned long flags; 100 unsigned long flags;
56 101
57 /*
58 * Some contexts may involve an exception occuring in an irq,
59 * leading to that nesting:
60 * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
61 * This would mess up the dyntick_nesting count though. And rcu_irq_*()
62 * helpers are enough to protect RCU uses inside the exception. So
63 * just return immediately if we detect we are in an IRQ.
64 */
65 if (in_interrupt()) 102 if (in_interrupt())
66 return; 103 return;
67 104
68 local_irq_save(flags); 105 local_irq_save(flags);
69 if (__this_cpu_read(context_tracking.state) == IN_USER) { 106 if (__this_cpu_read(context_tracking.state) == IN_USER) {
70 __this_cpu_write(context_tracking.state, IN_KERNEL); 107 __this_cpu_write(context_tracking.state, IN_KERNEL);
108 /*
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 */
71 rcu_user_exit(); 112 rcu_user_exit();
72 } 113 }
73 local_irq_restore(flags); 114 local_irq_restore(flags);
74} 115}
75 116
117
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 */
76void context_tracking_task_switch(struct task_struct *prev, 131void context_tracking_task_switch(struct task_struct *prev,
77 struct task_struct *next) 132 struct task_struct *next)
78{ 133{