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