Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_CONTEXT_TRACKING_H |
| 3 | #define _LINUX_CONTEXT_TRACKING_H |
| 4 | |
| 5 | #include <linux/sched.h> |
| 6 | #include <linux/vtime.h> |
| 7 | #include <linux/context_tracking_state.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8 | #include <linux/instrumentation.h> |
| 9 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10 | #include <asm/ptrace.h> |
| 11 | |
| 12 | |
| 13 | #ifdef CONFIG_CONTEXT_TRACKING |
| 14 | extern void context_tracking_cpu_set(int cpu); |
| 15 | |
| 16 | /* Called with interrupts disabled. */ |
| 17 | extern void __context_tracking_enter(enum ctx_state state); |
| 18 | extern void __context_tracking_exit(enum ctx_state state); |
| 19 | |
| 20 | extern void context_tracking_enter(enum ctx_state state); |
| 21 | extern void context_tracking_exit(enum ctx_state state); |
| 22 | extern void context_tracking_user_enter(void); |
| 23 | extern void context_tracking_user_exit(void); |
| 24 | |
| 25 | static inline void user_enter(void) |
| 26 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 27 | if (context_tracking_enabled()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | context_tracking_enter(CONTEXT_USER); |
| 29 | |
| 30 | } |
| 31 | static inline void user_exit(void) |
| 32 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 33 | if (context_tracking_enabled()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 34 | context_tracking_exit(CONTEXT_USER); |
| 35 | } |
| 36 | |
| 37 | /* Called with interrupts disabled. */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 38 | static __always_inline void user_enter_irqoff(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 39 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 40 | if (context_tracking_enabled()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | __context_tracking_enter(CONTEXT_USER); |
| 42 | |
| 43 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 44 | static __always_inline void user_exit_irqoff(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 45 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 46 | if (context_tracking_enabled()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 47 | __context_tracking_exit(CONTEXT_USER); |
| 48 | } |
| 49 | |
| 50 | static inline enum ctx_state exception_enter(void) |
| 51 | { |
| 52 | enum ctx_state prev_ctx; |
| 53 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 54 | if (!context_tracking_enabled()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 55 | return 0; |
| 56 | |
| 57 | prev_ctx = this_cpu_read(context_tracking.state); |
| 58 | if (prev_ctx != CONTEXT_KERNEL) |
| 59 | context_tracking_exit(prev_ctx); |
| 60 | |
| 61 | return prev_ctx; |
| 62 | } |
| 63 | |
| 64 | static inline void exception_exit(enum ctx_state prev_ctx) |
| 65 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 66 | if (context_tracking_enabled()) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 67 | if (prev_ctx != CONTEXT_KERNEL) |
| 68 | context_tracking_enter(prev_ctx); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * ct_state() - return the current context tracking state if known |
| 75 | * |
| 76 | * Returns the current cpu's context tracking state if context tracking |
| 77 | * is enabled. If context tracking is disabled, returns |
| 78 | * CONTEXT_DISABLED. This should be used primarily for debugging. |
| 79 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 80 | static __always_inline enum ctx_state ct_state(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 81 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 82 | return context_tracking_enabled() ? |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 83 | this_cpu_read(context_tracking.state) : CONTEXT_DISABLED; |
| 84 | } |
| 85 | #else |
| 86 | static inline void user_enter(void) { } |
| 87 | static inline void user_exit(void) { } |
| 88 | static inline void user_enter_irqoff(void) { } |
| 89 | static inline void user_exit_irqoff(void) { } |
| 90 | static inline enum ctx_state exception_enter(void) { return 0; } |
| 91 | static inline void exception_exit(enum ctx_state prev_ctx) { } |
| 92 | static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; } |
| 93 | #endif /* !CONFIG_CONTEXT_TRACKING */ |
| 94 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 95 | #define CT_WARN_ON(cond) WARN_ON(context_tracking_enabled() && (cond)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 96 | |
| 97 | #ifdef CONFIG_CONTEXT_TRACKING_FORCE |
| 98 | extern void context_tracking_init(void); |
| 99 | #else |
| 100 | static inline void context_tracking_init(void) { } |
| 101 | #endif /* CONFIG_CONTEXT_TRACKING_FORCE */ |
| 102 | |
| 103 | |
| 104 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
| 105 | /* must be called with irqs disabled */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 106 | static __always_inline void guest_enter_irqoff(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 107 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 108 | instrumentation_begin(); |
| 109 | if (vtime_accounting_enabled_this_cpu()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 110 | vtime_guest_enter(current); |
| 111 | else |
| 112 | current->flags |= PF_VCPU; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 113 | instrumentation_end(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 114 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 115 | if (context_tracking_enabled()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 116 | __context_tracking_enter(CONTEXT_GUEST); |
| 117 | |
| 118 | /* KVM does not hold any references to rcu protected data when it |
| 119 | * switches CPU into a guest mode. In fact switching to a guest mode |
| 120 | * is very similar to exiting to userspace from rcu point of view. In |
| 121 | * addition CPU may stay in a guest mode for quite a long time (up to |
| 122 | * one time slice). Lets treat guest mode as quiescent state, just like |
| 123 | * we do with user-mode execution. |
| 124 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 125 | if (!context_tracking_enabled_this_cpu()) { |
| 126 | instrumentation_begin(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 127 | rcu_virt_note_context_switch(smp_processor_id()); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 128 | instrumentation_end(); |
| 129 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 132 | static __always_inline void context_tracking_guest_exit(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 133 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 134 | if (context_tracking_enabled()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 135 | __context_tracking_exit(CONTEXT_GUEST); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 136 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 137 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 138 | static __always_inline void vtime_account_guest_exit(void) |
| 139 | { |
| 140 | if (vtime_accounting_enabled_this_cpu()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 141 | vtime_guest_exit(current); |
| 142 | else |
| 143 | current->flags &= ~PF_VCPU; |
| 144 | } |
| 145 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 146 | static __always_inline void guest_exit_irqoff(void) |
| 147 | { |
| 148 | context_tracking_guest_exit(); |
| 149 | |
| 150 | instrumentation_begin(); |
| 151 | vtime_account_guest_exit(); |
| 152 | instrumentation_end(); |
| 153 | } |
| 154 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 155 | #else |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 156 | static __always_inline void guest_enter_irqoff(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 157 | { |
| 158 | /* |
| 159 | * This is running in ioctl context so its safe |
| 160 | * to assume that it's the stime pending cputime |
| 161 | * to flush. |
| 162 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 163 | instrumentation_begin(); |
| 164 | vtime_account_kernel(current); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 165 | current->flags |= PF_VCPU; |
| 166 | rcu_virt_note_context_switch(smp_processor_id()); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 167 | instrumentation_end(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 170 | static __always_inline void context_tracking_guest_exit(void) { } |
| 171 | |
| 172 | static __always_inline void vtime_account_guest_exit(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 173 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 174 | vtime_account_kernel(current); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 175 | current->flags &= ~PF_VCPU; |
| 176 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 177 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 178 | static __always_inline void guest_exit_irqoff(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 179 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 180 | instrumentation_begin(); |
| 181 | /* Flush the guest cputime we spent on the guest */ |
| 182 | vtime_account_guest_exit(); |
| 183 | instrumentation_end(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 184 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 185 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 186 | |
| 187 | static inline void guest_exit(void) |
| 188 | { |
| 189 | unsigned long flags; |
| 190 | |
| 191 | local_irq_save(flags); |
| 192 | guest_exit_irqoff(); |
| 193 | local_irq_restore(flags); |
| 194 | } |
| 195 | |
| 196 | #endif |