blob: ee956c528fabf78cefcb58a67ff38aaaf621fcaf [file] [log] [blame]
Frederic Weisbeckere7358b32013-07-12 02:15:49 +02001#ifndef _LINUX_CONTEXT_TRACKING_STATE_H
2#define _LINUX_CONTEXT_TRACKING_STATE_H
3
4#include <linux/percpu.h>
5#include <linux/static_key.h>
6
7struct context_tracking {
8 /*
9 * When active is false, probes are unset in order
10 * to minimize overhead: TIF flags are cleared
11 * and calls to user_enter/exit are ignored. This
12 * may be further optimized using static keys.
13 */
14 bool active;
Frederic Weisbeckeraed5ed42015-05-06 18:04:23 +020015 int recursion;
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020016 enum ctx_state {
Andy Lutomirskif9281642015-07-03 12:44:21 -070017 CONTEXT_DISABLED = -1, /* returned by ct_state() if unknown */
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +010018 CONTEXT_KERNEL = 0,
19 CONTEXT_USER,
Rik van Riel126a6a52015-02-10 15:27:54 -050020 CONTEXT_GUEST,
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020021 } state;
22};
23
24#ifdef CONFIG_CONTEXT_TRACKING
25extern struct static_key context_tracking_enabled;
26DECLARE_PER_CPU(struct context_tracking, context_tracking);
27
Frederic Weisbecker58135f52013-11-06 14:45:57 +010028static inline bool context_tracking_is_enabled(void)
29{
30 return static_key_false(&context_tracking_enabled);
31}
Frederic Weisbeckerd0df09e2013-11-06 15:11:57 +010032
33static inline bool context_tracking_cpu_is_enabled(void)
34{
35 return __this_cpu_read(context_tracking.active);
36}
37
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020038static inline bool context_tracking_in_user(void)
39{
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +010040 return __this_cpu_read(context_tracking.state) == CONTEXT_USER;
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020041}
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020042#else
43static inline bool context_tracking_in_user(void) { return false; }
44static inline bool context_tracking_active(void) { return false; }
Rik van Riela3a9c7d2015-02-10 15:27:51 -050045static inline bool context_tracking_is_enabled(void) { return false; }
46static inline bool context_tracking_cpu_is_enabled(void) { return false; }
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020047#endif /* CONFIG_CONTEXT_TRACKING */
48
49#endif