context_tracking: Export context state for generic vtime
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / context_tracking.c
CommitLineData
91d1aa43
FW
1#include <linux/context_tracking.h>
2#include <linux/rcupdate.h>
3#include <linux/sched.h>
91d1aa43
FW
4#include <linux/hardirq.h>
5
91d1aa43 6
95a79fd4 7DEFINE_PER_CPU(struct context_tracking, context_tracking) = {
91d1aa43
FW
8#ifdef CONFIG_CONTEXT_TRACKING_FORCE
9 .active = true,
10#endif
11};
12
13void user_enter(void)
14{
15 unsigned long flags;
16
17 /*
18 * Some contexts may involve an exception occuring in an irq,
19 * leading to that nesting:
20 * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
21 * This would mess up the dyntick_nesting count though. And rcu_irq_*()
22 * helpers are enough to protect RCU uses inside the exception. So
23 * just return immediately if we detect we are in an IRQ.
24 */
25 if (in_interrupt())
26 return;
27
28 WARN_ON_ONCE(!current->mm);
29
30 local_irq_save(flags);
31 if (__this_cpu_read(context_tracking.active) &&
32 __this_cpu_read(context_tracking.state) != IN_USER) {
33 __this_cpu_write(context_tracking.state, IN_USER);
34 rcu_user_enter();
35 }
36 local_irq_restore(flags);
37}
38
39void user_exit(void)
40{
41 unsigned long flags;
42
43 /*
44 * Some contexts may involve an exception occuring in an irq,
45 * leading to that nesting:
46 * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
47 * This would mess up the dyntick_nesting count though. And rcu_irq_*()
48 * helpers are enough to protect RCU uses inside the exception. So
49 * just return immediately if we detect we are in an IRQ.
50 */
51 if (in_interrupt())
52 return;
53
54 local_irq_save(flags);
55 if (__this_cpu_read(context_tracking.state) == IN_USER) {
56 __this_cpu_write(context_tracking.state, IN_KERNEL);
57 rcu_user_exit();
58 }
59 local_irq_restore(flags);
60}
61
62void context_tracking_task_switch(struct task_struct *prev,
63 struct task_struct *next)
64{
65 if (__this_cpu_read(context_tracking.active)) {
66 clear_tsk_thread_flag(prev, TIF_NOHZ);
67 set_tsk_thread_flag(next, TIF_NOHZ);
68 }
69}