blob: c78fc27418f218c3d343a4c176602a50ad563236 [file] [log] [blame]
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01001#ifndef _LINUX_CONTEXT_TRACKING_H
2#define _LINUX_CONTEXT_TRACKING_H
3
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01004#include <linux/sched.h>
Frederic Weisbecker521921b2013-05-16 01:21:38 +02005#include <linux/vtime.h>
Frederic Weisbeckere7358b32013-07-12 02:15:49 +02006#include <linux/context_tracking_state.h>
Frederic Weisbecker56dd9472013-02-24 00:23:25 +01007#include <asm/ptrace.h>
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +01008
Frederic Weisbecker521921b2013-05-16 01:21:38 +02009
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010010#ifdef CONFIG_CONTEXT_TRACKING
Frederic Weisbecker2e709332013-07-10 00:55:25 +020011extern void context_tracking_cpu_set(int cpu);
12
Paolo Bonzinid0e536d2015-10-28 02:39:56 +010013/* Called with interrupts disabled. */
14extern void __context_tracking_enter(enum ctx_state state);
15extern void __context_tracking_exit(enum ctx_state state);
16
Rik van Riel3aab4f52015-02-10 15:27:50 -050017extern void context_tracking_enter(enum ctx_state state);
18extern void context_tracking_exit(enum ctx_state state);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020019extern void context_tracking_user_enter(void);
20extern void context_tracking_user_exit(void);
21
22static inline void user_enter(void)
23{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010024 if (context_tracking_is_enabled())
Paolo Bonzinif70cd6b2015-10-28 02:39:55 +010025 context_tracking_enter(CONTEXT_USER);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020026
27}
28static inline void user_exit(void)
29{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010030 if (context_tracking_is_enabled())
Paolo Bonzinif70cd6b2015-10-28 02:39:55 +010031 context_tracking_exit(CONTEXT_USER);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020032}
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010033
Paolo Bonzini2e9d1e12016-06-20 16:58:29 +020034/* Called with interrupts disabled. */
35static inline void user_enter_irqoff(void)
36{
37 if (context_tracking_is_enabled())
38 __context_tracking_enter(CONTEXT_USER);
39
40}
41static inline void user_exit_irqoff(void)
42{
43 if (context_tracking_is_enabled())
44 __context_tracking_exit(CONTEXT_USER);
45}
46
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010047static inline enum ctx_state exception_enter(void)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010048{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010049 enum ctx_state prev_ctx;
50
Frederic Weisbecker58135f52013-11-06 14:45:57 +010051 if (!context_tracking_is_enabled())
Frederic Weisbeckerad657822013-07-10 02:44:35 +020052 return 0;
53
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010054 prev_ctx = this_cpu_read(context_tracking.state);
Rik van Riel3aab4f52015-02-10 15:27:50 -050055 if (prev_ctx != CONTEXT_KERNEL)
56 context_tracking_exit(prev_ctx);
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010057
58 return prev_ctx;
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010059}
60
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010061static inline void exception_exit(enum ctx_state prev_ctx)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010062{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010063 if (context_tracking_is_enabled()) {
Rik van Riel3aab4f52015-02-10 15:27:50 -050064 if (prev_ctx != CONTEXT_KERNEL)
65 context_tracking_enter(prev_ctx);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020066 }
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010067}
68
Andy Lutomirskif9281642015-07-03 12:44:21 -070069
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 */
77static inline enum ctx_state ct_state(void)
78{
79 return context_tracking_is_enabled() ?
80 this_cpu_read(context_tracking.state) : CONTEXT_DISABLED;
81}
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010082#else
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010083static inline void user_enter(void) { }
84static inline void user_exit(void) { }
Paolo Bonzini2e9d1e12016-06-20 16:58:29 +020085static inline void user_enter_irqoff(void) { }
86static inline void user_exit_irqoff(void) { }
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010087static inline enum ctx_state exception_enter(void) { return 0; }
88static inline void exception_exit(enum ctx_state prev_ctx) { }
Andy Lutomirskif9281642015-07-03 12:44:21 -070089static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010090#endif /* !CONFIG_CONTEXT_TRACKING */
91
Andy Lutomirskif9281642015-07-03 12:44:21 -070092#define CT_WARN_ON(cond) WARN_ON(context_tracking_is_enabled() && (cond))
Frederic Weisbecker65f382f2013-07-11 19:12:32 +020093
94#ifdef CONFIG_CONTEXT_TRACKING_FORCE
95extern void context_tracking_init(void);
96#else
97static inline void context_tracking_init(void) { }
98#endif /* CONFIG_CONTEXT_TRACKING_FORCE */
99
100
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200101#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
Paolo Bonziniebaac172016-06-15 15:09:28 +0200102/* must be called with irqs disabled */
103static inline void guest_enter_irqoff(void)
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200104{
Frederic Weisbecker55dbdcf2015-11-19 16:47:32 +0100105 if (vtime_accounting_cpu_enabled())
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200106 vtime_guest_enter(current);
107 else
108 current->flags |= PF_VCPU;
Rik van Riel126a6a52015-02-10 15:27:54 -0500109
110 if (context_tracking_is_enabled())
Paolo Bonzinid0e536d2015-10-28 02:39:56 +0100111 __context_tracking_enter(CONTEXT_GUEST);
Paolo Bonziniebaac172016-06-15 15:09:28 +0200112
113 /* KVM does not hold any references to rcu protected data when it
114 * switches CPU into a guest mode. In fact switching to a guest mode
115 * is very similar to exiting to userspace from rcu point of view. In
116 * addition CPU may stay in a guest mode for quite a long time (up to
117 * one time slice). Lets treat guest mode as quiescent state, just like
118 * we do with user-mode execution.
119 */
120 if (!context_tracking_cpu_is_enabled())
121 rcu_virt_note_context_switch(smp_processor_id());
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200122}
123
Paolo Bonziniebaac172016-06-15 15:09:28 +0200124static inline void guest_exit_irqoff(void)
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200125{
Rik van Riel126a6a52015-02-10 15:27:54 -0500126 if (context_tracking_is_enabled())
Paolo Bonzinid0e536d2015-10-28 02:39:56 +0100127 __context_tracking_exit(CONTEXT_GUEST);
Rik van Riel126a6a52015-02-10 15:27:54 -0500128
Frederic Weisbecker55dbdcf2015-11-19 16:47:32 +0100129 if (vtime_accounting_cpu_enabled())
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200130 vtime_guest_exit(current);
131 else
132 current->flags &= ~PF_VCPU;
133}
Frederic Weisbecker73d424f2013-07-11 19:42:13 +0200134
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200135#else
Paolo Bonziniebaac172016-06-15 15:09:28 +0200136static inline void guest_enter_irqoff(void)
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200137{
138 /*
Frederic Weisbecker5b206d42013-07-12 19:05:14 +0200139 * This is running in ioctl context so its safe
140 * to assume that it's the stime pending cputime
141 * to flush.
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200142 */
143 vtime_account_system(current);
144 current->flags |= PF_VCPU;
Paolo Bonziniebaac172016-06-15 15:09:28 +0200145 rcu_virt_note_context_switch(smp_processor_id());
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200146}
147
Paolo Bonziniebaac172016-06-15 15:09:28 +0200148static inline void guest_exit_irqoff(void)
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200149{
Frederic Weisbecker5b206d42013-07-12 19:05:14 +0200150 /* Flush the guest cputime we spent on the guest */
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200151 vtime_account_system(current);
152 current->flags &= ~PF_VCPU;
153}
154#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
155
Paolo Bonziniebaac172016-06-15 15:09:28 +0200156static inline void guest_enter(void)
157{
158 unsigned long flags;
159
160 local_irq_save(flags);
161 guest_enter_irqoff();
162 local_irq_restore(flags);
163}
164
165static inline void guest_exit(void)
166{
167 unsigned long flags;
168
169 local_irq_save(flags);
170 guest_exit_irqoff();
171 local_irq_restore(flags);
172}
173
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100174#endif