Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Frederic Weisbecker | e7358b3 | 2013-07-12 02:15:49 +0200 | [diff] [blame] | 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; |
Frederic Weisbecker | aed5ed4 | 2015-05-06 18:04:23 +0200 | [diff] [blame] | 16 | int recursion; |
Frederic Weisbecker | e7358b3 | 2013-07-12 02:15:49 +0200 | [diff] [blame] | 17 | enum ctx_state { |
Andy Lutomirski | f928164 | 2015-07-03 12:44:21 -0700 | [diff] [blame] | 18 | CONTEXT_DISABLED = -1, /* returned by ct_state() if unknown */ |
Frederic Weisbecker | c467ea7 | 2015-03-04 18:06:33 +0100 | [diff] [blame] | 19 | CONTEXT_KERNEL = 0, |
| 20 | CONTEXT_USER, |
Rik van Riel | 126a6a5 | 2015-02-10 15:27:54 -0500 | [diff] [blame] | 21 | CONTEXT_GUEST, |
Frederic Weisbecker | e7358b3 | 2013-07-12 02:15:49 +0200 | [diff] [blame] | 22 | } state; |
| 23 | }; |
| 24 | |
| 25 | #ifdef CONFIG_CONTEXT_TRACKING |
Andy Lutomirski | ed11a7f | 2015-11-12 12:59:01 -0800 | [diff] [blame] | 26 | extern struct static_key_false context_tracking_enabled; |
Frederic Weisbecker | e7358b3 | 2013-07-12 02:15:49 +0200 | [diff] [blame] | 27 | DECLARE_PER_CPU(struct context_tracking, context_tracking); |
| 28 | |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 29 | static inline bool context_tracking_is_enabled(void) |
| 30 | { |
Andy Lutomirski | ed11a7f | 2015-11-12 12:59:01 -0800 | [diff] [blame] | 31 | return static_branch_unlikely(&context_tracking_enabled); |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 32 | } |
Frederic Weisbecker | d0df09e | 2013-11-06 15:11:57 +0100 | [diff] [blame] | 33 | |
| 34 | static inline bool context_tracking_cpu_is_enabled(void) |
| 35 | { |
| 36 | return __this_cpu_read(context_tracking.active); |
| 37 | } |
| 38 | |
Frederic Weisbecker | e7358b3 | 2013-07-12 02:15:49 +0200 | [diff] [blame] | 39 | static inline bool context_tracking_in_user(void) |
| 40 | { |
Frederic Weisbecker | c467ea7 | 2015-03-04 18:06:33 +0100 | [diff] [blame] | 41 | return __this_cpu_read(context_tracking.state) == CONTEXT_USER; |
Frederic Weisbecker | e7358b3 | 2013-07-12 02:15:49 +0200 | [diff] [blame] | 42 | } |
Frederic Weisbecker | e7358b3 | 2013-07-12 02:15:49 +0200 | [diff] [blame] | 43 | #else |
| 44 | static inline bool context_tracking_in_user(void) { return false; } |
| 45 | static inline bool context_tracking_active(void) { return false; } |
Rik van Riel | a3a9c7d | 2015-02-10 15:27:51 -0500 | [diff] [blame] | 46 | static inline bool context_tracking_is_enabled(void) { return false; } |
| 47 | static inline bool context_tracking_cpu_is_enabled(void) { return false; } |
Frederic Weisbecker | e7358b3 | 2013-07-12 02:15:49 +0200 | [diff] [blame] | 48 | #endif /* CONFIG_CONTEXT_TRACKING */ |
| 49 | |
| 50 | #endif |