|
2 | 2 | #define _LINUX_CONTEXT_TRACKING_H |
3 | 3 |
|
4 | 4 | #include <linux/sched.h> |
5 | | -#include <linux/percpu.h> |
6 | 5 | #include <linux/vtime.h> |
| 6 | +#include <linux/context_tracking_state.h> |
7 | 7 | #include <asm/ptrace.h> |
8 | 8 |
|
9 | | -struct context_tracking { |
10 | | - /* |
11 | | - * When active is false, probes are unset in order |
12 | | - * to minimize overhead: TIF flags are cleared |
13 | | - * and calls to user_enter/exit are ignored. This |
14 | | - * may be further optimized using static keys. |
15 | | - */ |
16 | | - bool active; |
17 | | - enum ctx_state { |
18 | | - IN_KERNEL = 0, |
19 | | - IN_USER, |
20 | | - } state; |
21 | | -}; |
22 | | - |
23 | | -static inline void __guest_enter(void) |
24 | | -{ |
25 | | - /* |
26 | | - * This is running in ioctl context so we can avoid |
27 | | - * the call to vtime_account() with its unnecessary idle check. |
28 | | - */ |
29 | | - vtime_account_system(current); |
30 | | - current->flags |= PF_VCPU; |
31 | | -} |
32 | | - |
33 | | -static inline void __guest_exit(void) |
34 | | -{ |
35 | | - /* |
36 | | - * This is running in ioctl context so we can avoid |
37 | | - * the call to vtime_account() with its unnecessary idle check. |
38 | | - */ |
39 | | - vtime_account_system(current); |
40 | | - current->flags &= ~PF_VCPU; |
41 | | -} |
42 | 9 |
|
43 | 10 | #ifdef CONFIG_CONTEXT_TRACKING |
44 | | -DECLARE_PER_CPU(struct context_tracking, context_tracking); |
| 11 | +extern void context_tracking_cpu_set(int cpu); |
45 | 12 |
|
46 | | -static inline bool context_tracking_in_user(void) |
| 13 | +extern void context_tracking_user_enter(void); |
| 14 | +extern void context_tracking_user_exit(void); |
| 15 | +extern void __context_tracking_task_switch(struct task_struct *prev, |
| 16 | + struct task_struct *next); |
| 17 | + |
| 18 | +static inline void user_enter(void) |
47 | 19 | { |
48 | | - return __this_cpu_read(context_tracking.state) == IN_USER; |
49 | | -} |
| 20 | + if (static_key_false(&context_tracking_enabled)) |
| 21 | + context_tracking_user_enter(); |
50 | 22 |
|
51 | | -static inline bool context_tracking_active(void) |
| 23 | +} |
| 24 | +static inline void user_exit(void) |
52 | 25 | { |
53 | | - return __this_cpu_read(context_tracking.active); |
| 26 | + if (static_key_false(&context_tracking_enabled)) |
| 27 | + context_tracking_user_exit(); |
54 | 28 | } |
55 | 29 |
|
56 | | -extern void user_enter(void); |
57 | | -extern void user_exit(void); |
58 | | - |
59 | | -extern void guest_enter(void); |
60 | | -extern void guest_exit(void); |
61 | | - |
62 | 30 | static inline enum ctx_state exception_enter(void) |
63 | 31 | { |
64 | 32 | enum ctx_state prev_ctx; |
65 | 33 |
|
| 34 | + if (!static_key_false(&context_tracking_enabled)) |
| 35 | + return 0; |
| 36 | + |
66 | 37 | prev_ctx = this_cpu_read(context_tracking.state); |
67 | | - user_exit(); |
| 38 | + context_tracking_user_exit(); |
68 | 39 |
|
69 | 40 | return prev_ctx; |
70 | 41 | } |
71 | 42 |
|
72 | 43 | static inline void exception_exit(enum ctx_state prev_ctx) |
73 | 44 | { |
74 | | - if (prev_ctx == IN_USER) |
75 | | - user_enter(); |
| 45 | + if (static_key_false(&context_tracking_enabled)) { |
| 46 | + if (prev_ctx == IN_USER) |
| 47 | + context_tracking_user_enter(); |
| 48 | + } |
76 | 49 | } |
77 | 50 |
|
78 | | -extern void context_tracking_task_switch(struct task_struct *prev, |
79 | | - struct task_struct *next); |
| 51 | +static inline void context_tracking_task_switch(struct task_struct *prev, |
| 52 | + struct task_struct *next) |
| 53 | +{ |
| 54 | + if (static_key_false(&context_tracking_enabled)) |
| 55 | + __context_tracking_task_switch(prev, next); |
| 56 | +} |
80 | 57 | #else |
81 | | -static inline bool context_tracking_in_user(void) { return false; } |
82 | 58 | static inline void user_enter(void) { } |
83 | 59 | static inline void user_exit(void) { } |
| 60 | +static inline enum ctx_state exception_enter(void) { return 0; } |
| 61 | +static inline void exception_exit(enum ctx_state prev_ctx) { } |
| 62 | +static inline void context_tracking_task_switch(struct task_struct *prev, |
| 63 | + struct task_struct *next) { } |
| 64 | +#endif /* !CONFIG_CONTEXT_TRACKING */ |
| 65 | + |
| 66 | + |
| 67 | +#ifdef CONFIG_CONTEXT_TRACKING_FORCE |
| 68 | +extern void context_tracking_init(void); |
| 69 | +#else |
| 70 | +static inline void context_tracking_init(void) { } |
| 71 | +#endif /* CONFIG_CONTEXT_TRACKING_FORCE */ |
| 72 | + |
84 | 73 |
|
| 74 | +#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
85 | 75 | static inline void guest_enter(void) |
86 | 76 | { |
87 | | - __guest_enter(); |
| 77 | + if (vtime_accounting_enabled()) |
| 78 | + vtime_guest_enter(current); |
| 79 | + else |
| 80 | + current->flags |= PF_VCPU; |
88 | 81 | } |
89 | 82 |
|
90 | 83 | static inline void guest_exit(void) |
91 | 84 | { |
92 | | - __guest_exit(); |
| 85 | + if (vtime_accounting_enabled()) |
| 86 | + vtime_guest_exit(current); |
| 87 | + else |
| 88 | + current->flags &= ~PF_VCPU; |
93 | 89 | } |
94 | 90 |
|
95 | | -static inline enum ctx_state exception_enter(void) { return 0; } |
96 | | -static inline void exception_exit(enum ctx_state prev_ctx) { } |
97 | | -static inline void context_tracking_task_switch(struct task_struct *prev, |
98 | | - struct task_struct *next) { } |
99 | | -#endif /* !CONFIG_CONTEXT_TRACKING */ |
| 91 | +#else |
| 92 | +static inline void guest_enter(void) |
| 93 | +{ |
| 94 | + /* |
| 95 | + * This is running in ioctl context so its safe |
| 96 | + * to assume that it's the stime pending cputime |
| 97 | + * to flush. |
| 98 | + */ |
| 99 | + vtime_account_system(current); |
| 100 | + current->flags |= PF_VCPU; |
| 101 | +} |
| 102 | + |
| 103 | +static inline void guest_exit(void) |
| 104 | +{ |
| 105 | + /* Flush the guest cputime we spent on the guest */ |
| 106 | + vtime_account_system(current); |
| 107 | + current->flags &= ~PF_VCPU; |
| 108 | +} |
| 109 | +#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ |
100 | 110 |
|
101 | 111 | #endif |
0 commit comments