Loading...
1#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 {
16 IN_KERNEL = 0,
17 IN_USER,
18 } state;
19};
20
21#ifdef CONFIG_CONTEXT_TRACKING
22extern struct static_key context_tracking_enabled;
23DECLARE_PER_CPU(struct context_tracking, context_tracking);
24
25static inline bool context_tracking_is_enabled(void)
26{
27 return static_key_false(&context_tracking_enabled);
28}
29
30static inline bool context_tracking_cpu_is_enabled(void)
31{
32 return __this_cpu_read(context_tracking.active);
33}
34
35static inline bool context_tracking_in_user(void)
36{
37 return __this_cpu_read(context_tracking.state) == IN_USER;
38}
39#else
40static inline bool context_tracking_in_user(void) { return false; }
41static inline bool context_tracking_active(void) { return false; }
42#endif /* CONFIG_CONTEXT_TRACKING */
43
44#endif
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
8struct 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
26extern struct static_key_false context_tracking_enabled;
27DECLARE_PER_CPU(struct context_tracking, context_tracking);
28
29static inline bool context_tracking_is_enabled(void)
30{
31 return static_branch_unlikely(&context_tracking_enabled);
32}
33
34static inline bool context_tracking_cpu_is_enabled(void)
35{
36 return __this_cpu_read(context_tracking.active);
37}
38
39static inline bool context_tracking_in_user(void)
40{
41 return __this_cpu_read(context_tracking.state) == CONTEXT_USER;
42}
43#else
44static inline bool context_tracking_in_user(void) { return false; }
45static inline bool context_tracking_active(void) { return false; }
46static inline bool context_tracking_is_enabled(void) { return false; }
47static inline bool context_tracking_cpu_is_enabled(void) { return false; }
48#endif /* CONFIG_CONTEXT_TRACKING */
49
50#endif