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 inline void guest_enter_irqoff(void)
106 if (vtime_accounting_enabled_this_cpu())
107 vtime_guest_enter(current
);
109 current
->flags
|= PF_VCPU
;
111 if (context_tracking_enabled())
112 __context_tracking_enter(CONTEXT_GUEST
);
114 /* KVM does not hold any references to rcu protected data when it
115 * switches CPU into a guest mode. In fact switching to a guest mode
116 * is very similar to exiting to userspace from rcu point of view. In
117 * addition CPU may stay in a guest mode for quite a long time (up to
118 * one time slice). Lets treat guest mode as quiescent state, just like
119 * we do with user-mode execution.
121 if (!context_tracking_enabled_this_cpu())
122 rcu_virt_note_context_switch(smp_processor_id());
125 static inline void guest_exit_irqoff(void)
127 if (context_tracking_enabled())
128 __context_tracking_exit(CONTEXT_GUEST
);
130 if (vtime_accounting_enabled_this_cpu())
131 vtime_guest_exit(current
);
133 current
->flags
&= ~PF_VCPU
;
137 static inline void guest_enter_irqoff(void)
140 * This is running in ioctl context so its safe
141 * to assume that it's the stime pending cputime
144 vtime_account_kernel(current
);
145 current
->flags
|= PF_VCPU
;
146 rcu_virt_note_context_switch(smp_processor_id());
149 static inline void guest_exit_irqoff(void)
151 /* Flush the guest cputime we spent on the guest */
152 vtime_account_kernel(current
);
153 current
->flags
&= ~PF_VCPU
;
155 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
157 static inline void guest_exit(void)
161 local_irq_save(flags
);
163 local_irq_restore(flags
);