blob: 6b7b96a32b753a614ef36ea8bb6c1d5a83641b21 [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;
15 enum ctx_state {
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +010016 CONTEXT_KERNEL = 0,
17 CONTEXT_USER,
Rik van Riel126a6a52015-02-10 15:27:54 -050018 CONTEXT_GUEST,
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020019 } state;
20};
21
22#ifdef CONFIG_CONTEXT_TRACKING
23extern struct static_key context_tracking_enabled;
24DECLARE_PER_CPU(struct context_tracking, context_tracking);
25
Frederic Weisbecker58135f52013-11-06 14:45:57 +010026static inline bool context_tracking_is_enabled(void)
27{
28 return static_key_false(&context_tracking_enabled);
29}
Frederic Weisbeckerd0df09e2013-11-06 15:11:57 +010030
31static inline bool context_tracking_cpu_is_enabled(void)
32{
33 return __this_cpu_read(context_tracking.active);
34}
35
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020036static inline bool context_tracking_in_user(void)
37{
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +010038 return __this_cpu_read(context_tracking.state) == CONTEXT_USER;
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020039}
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020040#else
41static inline bool context_tracking_in_user(void) { return false; }
42static inline bool context_tracking_active(void) { return false; }
Rik van Riela3a9c7d2015-02-10 15:27:51 -050043static inline bool context_tracking_is_enabled(void) { return false; }
44static inline bool context_tracking_cpu_is_enabled(void) { return false; }
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020045#endif /* CONFIG_CONTEXT_TRACKING */
46
47#endif