Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_CONTEXT_TRACKING_STATE_H |
| 3 | #define _LINUX_CONTEXT_TRACKING_STATE_H |
| 4 | |
| 5 | #include <linux/percpu.h> |
| 6 | #include <linux/static_key.h> |
| 7 | |
| 8 | struct context_tracking { |
| 9 | /* |
| 10 | * When active is false, probes are unset in order |
| 11 | * to minimize overhead: TIF flags are cleared |
| 12 | * and calls to user_enter/exit are ignored. This |
| 13 | * may be further optimized using static keys. |
| 14 | */ |
| 15 | bool active; |
| 16 | int recursion; |
| 17 | enum ctx_state { |
| 18 | CONTEXT_DISABLED = -1, /* returned by ct_state() if unknown */ |
| 19 | CONTEXT_KERNEL = 0, |
| 20 | CONTEXT_USER, |
| 21 | CONTEXT_GUEST, |
| 22 | } state; |
| 23 | }; |
| 24 | |
| 25 | #ifdef CONFIG_CONTEXT_TRACKING |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 26 | extern struct static_key_false context_tracking_key; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 27 | DECLARE_PER_CPU(struct context_tracking, context_tracking); |
| 28 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 29 | static __always_inline bool context_tracking_enabled(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 30 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 31 | return static_branch_unlikely(&context_tracking_key); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 34 | static __always_inline bool context_tracking_enabled_cpu(int cpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 36 | return context_tracking_enabled() && per_cpu(context_tracking.active, cpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 39 | static inline bool context_tracking_enabled_this_cpu(void) |
| 40 | { |
| 41 | return context_tracking_enabled() && __this_cpu_read(context_tracking.active); |
| 42 | } |
| 43 | |
| 44 | static __always_inline bool context_tracking_in_user(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 45 | { |
| 46 | return __this_cpu_read(context_tracking.state) == CONTEXT_USER; |
| 47 | } |
| 48 | #else |
| 49 | static inline bool context_tracking_in_user(void) { return false; } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 50 | static inline bool context_tracking_enabled(void) { return false; } |
| 51 | static inline bool context_tracking_enabled_cpu(int cpu) { return false; } |
| 52 | static inline bool context_tracking_enabled_this_cpu(void) { return false; } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 53 | #endif /* CONFIG_CONTEXT_TRACKING */ |
| 54 | |
| 55 | #endif |