Merge tag 'mmc-v4.8' of git://git.linaro.org/people/ulf.hansson/mmc
[cascardo/linux.git] / include / linux / context_tracking.h
1 #ifndef _LINUX_CONTEXT_TRACKING_H
2 #define _LINUX_CONTEXT_TRACKING_H
3
4 #include <linux/sched.h>
5 #include <linux/vtime.h>
6 #include <linux/context_tracking_state.h>
7 #include <asm/ptrace.h>
8
9
10 #ifdef CONFIG_CONTEXT_TRACKING
11 extern void context_tracking_cpu_set(int cpu);
12
13 /* Called with interrupts disabled.  */
14 extern void __context_tracking_enter(enum ctx_state state);
15 extern void __context_tracking_exit(enum ctx_state state);
16
17 extern void context_tracking_enter(enum ctx_state state);
18 extern void context_tracking_exit(enum ctx_state state);
19 extern void context_tracking_user_enter(void);
20 extern void context_tracking_user_exit(void);
21
22 static inline void user_enter(void)
23 {
24         if (context_tracking_is_enabled())
25                 context_tracking_enter(CONTEXT_USER);
26
27 }
28 static inline void user_exit(void)
29 {
30         if (context_tracking_is_enabled())
31                 context_tracking_exit(CONTEXT_USER);
32 }
33
34 /* Called with interrupts disabled.  */
35 static inline void user_enter_irqoff(void)
36 {
37         if (context_tracking_is_enabled())
38                 __context_tracking_enter(CONTEXT_USER);
39
40 }
41 static inline void user_exit_irqoff(void)
42 {
43         if (context_tracking_is_enabled())
44                 __context_tracking_exit(CONTEXT_USER);
45 }
46
47 static inline enum ctx_state exception_enter(void)
48 {
49         enum ctx_state prev_ctx;
50
51         if (!context_tracking_is_enabled())
52                 return 0;
53
54         prev_ctx = this_cpu_read(context_tracking.state);
55         if (prev_ctx != CONTEXT_KERNEL)
56                 context_tracking_exit(prev_ctx);
57
58         return prev_ctx;
59 }
60
61 static inline void exception_exit(enum ctx_state prev_ctx)
62 {
63         if (context_tracking_is_enabled()) {
64                 if (prev_ctx != CONTEXT_KERNEL)
65                         context_tracking_enter(prev_ctx);
66         }
67 }
68
69
70 /**
71  * ct_state() - return the current context tracking state if known
72  *
73  * Returns the current cpu's context tracking state if context tracking
74  * is enabled.  If context tracking is disabled, returns
75  * CONTEXT_DISABLED.  This should be used primarily for debugging.
76  */
77 static inline enum ctx_state ct_state(void)
78 {
79         return context_tracking_is_enabled() ?
80                 this_cpu_read(context_tracking.state) : CONTEXT_DISABLED;
81 }
82 #else
83 static inline void user_enter(void) { }
84 static inline void user_exit(void) { }
85 static inline void user_enter_irqoff(void) { }
86 static inline void user_exit_irqoff(void) { }
87 static inline enum ctx_state exception_enter(void) { return 0; }
88 static inline void exception_exit(enum ctx_state prev_ctx) { }
89 static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; }
90 #endif /* !CONFIG_CONTEXT_TRACKING */
91
92 #define CT_WARN_ON(cond) WARN_ON(context_tracking_is_enabled() && (cond))
93
94 #ifdef CONFIG_CONTEXT_TRACKING_FORCE
95 extern void context_tracking_init(void);
96 #else
97 static inline void context_tracking_init(void) { }
98 #endif /* CONFIG_CONTEXT_TRACKING_FORCE */
99
100
101 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
102 static inline void guest_enter(void)
103 {
104         if (vtime_accounting_cpu_enabled())
105                 vtime_guest_enter(current);
106         else
107                 current->flags |= PF_VCPU;
108
109         if (context_tracking_is_enabled())
110                 __context_tracking_enter(CONTEXT_GUEST);
111 }
112
113 static inline void guest_exit(void)
114 {
115         if (context_tracking_is_enabled())
116                 __context_tracking_exit(CONTEXT_GUEST);
117
118         if (vtime_accounting_cpu_enabled())
119                 vtime_guest_exit(current);
120         else
121                 current->flags &= ~PF_VCPU;
122 }
123
124 #else
125 static inline void guest_enter(void)
126 {
127         /*
128          * This is running in ioctl context so its safe
129          * to assume that it's the stime pending cputime
130          * to flush.
131          */
132         vtime_account_system(current);
133         current->flags |= PF_VCPU;
134 }
135
136 static inline void guest_exit(void)
137 {
138         /* Flush the guest cputime we spent on the guest */
139         vtime_account_system(current);
140         current->flags &= ~PF_VCPU;
141 }
142 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
143
144 #endif