1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_CONTEXT_TRACKING_H
3 #define _LINUX_CONTEXT_TRACKING_H
5 #include <linux/sched.h>
6 #include <linux/vtime.h>
7 #include <linux/context_tracking_state.h>
8 #include <asm/ptrace.h>
11 #ifdef CONFIG_CONTEXT_TRACKING
12 extern void context_tracking_cpu_set(int cpu);
14 /* Called with interrupts disabled. */
15 extern void __context_tracking_enter(enum ctx_state state);
16 extern void __context_tracking_exit(enum ctx_state state);
18 extern void context_tracking_enter(enum ctx_state state);
19 extern void context_tracking_exit(enum ctx_state state);
20 extern void context_tracking_user_enter(void);
21 extern void context_tracking_user_exit(void);
23 static inline void user_enter(void)
25 if (context_tracking_enabled())
26 context_tracking_enter(CONTEXT_USER);
29 static inline void user_exit(void)
31 if (context_tracking_enabled())
32 context_tracking_exit(CONTEXT_USER);
35 /* Called with interrupts disabled. */
36 static inline void user_enter_irqoff(void)
38 if (context_tracking_enabled())
39 __context_tracking_enter(CONTEXT_USER);
42 static inline void user_exit_irqoff(void)
44 if (context_tracking_enabled())
45 __context_tracking_exit(CONTEXT_USER);
48 static inline enum ctx_state exception_enter(void)
50 enum ctx_state prev_ctx;
52 if (!context_tracking_enabled())
55 prev_ctx = this_cpu_read(context_tracking.state);
56 if (prev_ctx != CONTEXT_KERNEL)
57 context_tracking_exit(prev_ctx);
62 static inline void exception_exit(enum ctx_state prev_ctx)
64 if (context_tracking_enabled()) {
65 if (prev_ctx != CONTEXT_KERNEL)
66 context_tracking_enter(prev_ctx);
72 * ct_state() - return the current context tracking state if known
74 * Returns the current cpu's context tracking state if context tracking
75 * is enabled. If context tracking is disabled, returns
76 * CONTEXT_DISABLED. This should be used primarily for debugging.
78 static inline enum ctx_state ct_state(void)
80 return context_tracking_enabled() ?
81 this_cpu_read(context_tracking.state) : CONTEXT_DISABLED;
84 static inline void user_enter(void) { }
85 static inline void user_exit(void) { }
86 static inline void user_enter_irqoff(void) { }
87 static inline void user_exit_irqoff(void) { }
88 static inline enum ctx_state exception_enter(void) { return 0; }
89 static inline void exception_exit(enum ctx_state prev_ctx) { }
90 static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; }
91 #endif /* !CONFIG_CONTEXT_TRACKING */
93 #define CT_WARN_ON(cond) WARN_ON(context_tracking_enabled() && (cond))
95 #ifdef CONFIG_CONTEXT_TRACKING_FORCE
96 extern void context_tracking_init(void);
98 static inline void context_tracking_init(void) { }
99 #endif /* CONFIG_CONTEXT_TRACKING_FORCE */
102 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
103 /* must be called with irqs disabled */
104 static __always_inline void guest_enter_irqoff(void)
106 instrumentation_begin();
107 if (vtime_accounting_enabled_this_cpu())
108 vtime_guest_enter(current);
110 current->flags |= PF_VCPU;
111 instrumentation_end();
113 if (context_tracking_enabled())
114 __context_tracking_enter(CONTEXT_GUEST);
116 /* KVM does not hold any references to rcu protected data when it
117 * switches CPU into a guest mode. In fact switching to a guest mode
118 * is very similar to exiting to userspace from rcu point of view. In
119 * addition CPU may stay in a guest mode for quite a long time (up to
120 * one time slice). Lets treat guest mode as quiescent state, just like
121 * we do with user-mode execution.
123 if (!context_tracking_enabled_this_cpu()) {
124 instrumentation_begin();
125 rcu_virt_note_context_switch(smp_processor_id());
126 instrumentation_end();
130 static __always_inline void guest_exit_irqoff(void)
132 if (context_tracking_enabled())
133 __context_tracking_exit(CONTEXT_GUEST);
135 instrumentation_begin();
136 if (vtime_accounting_enabled_this_cpu())
137 vtime_guest_exit(current);
139 current->flags &= ~PF_VCPU;
140 instrumentation_end();
144 static __always_inline void guest_enter_irqoff(void)
147 * This is running in ioctl context so its safe
148 * to assume that it's the stime pending cputime
151 instrumentation_begin();
152 vtime_account_kernel(current);
153 current->flags |= PF_VCPU;
154 rcu_virt_note_context_switch(smp_processor_id());
155 instrumentation_end();
158 static __always_inline void guest_exit_irqoff(void)
160 instrumentation_begin();
161 /* Flush the guest cputime we spent on the guest */
162 vtime_account_kernel(current);
163 current->flags &= ~PF_VCPU;
164 instrumentation_end();
166 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
168 static inline void guest_exit(void)
172 local_irq_save(flags);
174 local_irq_restore(flags);