blob: c138c24bad1a5232420dbc4781a6c55c6c48751d [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 Weisbecker95a79fd2013-01-07 18:12:14 +01005#include <linux/percpu.h>
Frederic Weisbecker521921b2013-05-16 01:21:38 +02006#include <linux/vtime.h>
Frederic Weisbecker65f382f2013-07-11 19:12:32 +02007#include <linux/static_key.h>
Frederic Weisbecker56dd9472013-02-24 00:23:25 +01008#include <asm/ptrace.h>
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +01009
10struct context_tracking {
11 /*
12 * When active is false, probes are unset in order
13 * to minimize overhead: TIF flags are cleared
14 * and calls to user_enter/exit are ignored. This
15 * may be further optimized using static keys.
16 */
17 bool active;
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010018 enum ctx_state {
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010019 IN_KERNEL = 0,
20 IN_USER,
21 } state;
22};
23
Frederic Weisbecker521921b2013-05-16 01:21:38 +020024
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010025#ifdef CONFIG_CONTEXT_TRACKING
Frederic Weisbecker65f382f2013-07-11 19:12:32 +020026extern struct static_key context_tracking_enabled;
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010027DECLARE_PER_CPU(struct context_tracking, context_tracking);
28
29static inline bool context_tracking_in_user(void)
30{
31 return __this_cpu_read(context_tracking.state) == IN_USER;
32}
33
34static inline bool context_tracking_active(void)
35{
36 return __this_cpu_read(context_tracking.active);
37}
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010038
Frederic Weisbecker2e709332013-07-10 00:55:25 +020039extern void context_tracking_cpu_set(int cpu);
40
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010041extern void user_enter(void);
42extern void user_exit(void);
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010043
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010044static inline enum ctx_state exception_enter(void)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010045{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010046 enum ctx_state prev_ctx;
47
48 prev_ctx = this_cpu_read(context_tracking.state);
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010049 user_exit();
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010050
51 return prev_ctx;
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010052}
53
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010054static inline void exception_exit(enum ctx_state prev_ctx)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010055{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010056 if (prev_ctx == IN_USER)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010057 user_enter();
58}
59
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010060extern void context_tracking_task_switch(struct task_struct *prev,
61 struct task_struct *next);
62#else
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010063static inline bool context_tracking_in_user(void) { return false; }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010064static inline void user_enter(void) { }
65static inline void user_exit(void) { }
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010066static inline enum ctx_state exception_enter(void) { return 0; }
67static inline void exception_exit(enum ctx_state prev_ctx) { }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010068static inline void context_tracking_task_switch(struct task_struct *prev,
69 struct task_struct *next) { }
70#endif /* !CONFIG_CONTEXT_TRACKING */
71
Frederic Weisbecker65f382f2013-07-11 19:12:32 +020072
73#ifdef CONFIG_CONTEXT_TRACKING_FORCE
74extern void context_tracking_init(void);
75#else
76static inline void context_tracking_init(void) { }
77#endif /* CONFIG_CONTEXT_TRACKING_FORCE */
78
79
Frederic Weisbecker2d854e52013-07-12 19:02:30 +020080#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
81extern void guest_enter(void);
82extern void guest_exit(void);
83#else
84static inline void guest_enter(void)
85{
86 /*
Frederic Weisbecker5b206d42013-07-12 19:05:14 +020087 * This is running in ioctl context so its safe
88 * to assume that it's the stime pending cputime
89 * to flush.
Frederic Weisbecker2d854e52013-07-12 19:02:30 +020090 */
91 vtime_account_system(current);
92 current->flags |= PF_VCPU;
93}
94
95static inline void guest_exit(void)
96{
Frederic Weisbecker5b206d42013-07-12 19:05:14 +020097 /* Flush the guest cputime we spent on the guest */
Frederic Weisbecker2d854e52013-07-12 19:02:30 +020098 vtime_account_system(current);
99 current->flags &= ~PF_VCPU;
100}
101#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
102
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100103#endif