1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_CONTEXT_TRACKING_STATE_H
3 #define _LINUX_CONTEXT_TRACKING_STATE_H
5 #include <linux/percpu.h>
6 #include <linux/static_key.h>
7 #include <linux/context_tracking_irq.h>
9 /* Offset to allow distinguishing irq vs. task-based idle entry/exit. */
10 #define CT_NESTING_IRQ_NONIDLE ((LONG_MAX / 2) + 1)
13 CT_STATE_DISABLED
= -1, /* returned by ct_state() if unknown */
21 /* Odd value for watching, else even. */
22 #define CT_RCU_WATCHING CT_STATE_MAX
24 #define CT_STATE_MASK (CT_STATE_MAX - 1)
25 #define CT_RCU_WATCHING_MASK (~CT_STATE_MASK)
27 struct context_tracking
{
28 #ifdef CONFIG_CONTEXT_TRACKING_USER
30 * When active is false, probes are unset in order
31 * to minimize overhead: TIF flags are cleared
32 * and calls to user_enter/exit are ignored. This
33 * may be further optimized using static keys.
38 #ifdef CONFIG_CONTEXT_TRACKING
41 #ifdef CONFIG_CONTEXT_TRACKING_IDLE
42 long nesting
; /* Track process nesting level. */
43 long nmi_nesting
; /* Track irq/NMI nesting level. */
47 #ifdef CONFIG_CONTEXT_TRACKING
48 DECLARE_PER_CPU(struct context_tracking
, context_tracking
);
51 #ifdef CONFIG_CONTEXT_TRACKING_USER
52 static __always_inline
int __ct_state(void)
54 return raw_atomic_read(this_cpu_ptr(&context_tracking
.state
)) & CT_STATE_MASK
;
58 #ifdef CONFIG_CONTEXT_TRACKING_IDLE
59 static __always_inline
int ct_rcu_watching(void)
61 return atomic_read(this_cpu_ptr(&context_tracking
.state
)) & CT_RCU_WATCHING_MASK
;
64 static __always_inline
int ct_rcu_watching_cpu(int cpu
)
66 struct context_tracking
*ct
= per_cpu_ptr(&context_tracking
, cpu
);
68 return atomic_read(&ct
->state
) & CT_RCU_WATCHING_MASK
;
71 static __always_inline
int ct_rcu_watching_cpu_acquire(int cpu
)
73 struct context_tracking
*ct
= per_cpu_ptr(&context_tracking
, cpu
);
75 return atomic_read_acquire(&ct
->state
) & CT_RCU_WATCHING_MASK
;
78 static __always_inline
long ct_nesting(void)
80 return __this_cpu_read(context_tracking
.nesting
);
83 static __always_inline
long ct_nesting_cpu(int cpu
)
85 struct context_tracking
*ct
= per_cpu_ptr(&context_tracking
, cpu
);
90 static __always_inline
long ct_nmi_nesting(void)
92 return __this_cpu_read(context_tracking
.nmi_nesting
);
95 static __always_inline
long ct_nmi_nesting_cpu(int cpu
)
97 struct context_tracking
*ct
= per_cpu_ptr(&context_tracking
, cpu
);
99 return ct
->nmi_nesting
;
101 #endif /* #ifdef CONFIG_CONTEXT_TRACKING_IDLE */
103 #ifdef CONFIG_CONTEXT_TRACKING_USER
104 extern struct static_key_false context_tracking_key
;
106 static __always_inline
bool context_tracking_enabled(void)
108 return static_branch_unlikely(&context_tracking_key
);
111 static __always_inline
bool context_tracking_enabled_cpu(int cpu
)
113 return context_tracking_enabled() && per_cpu(context_tracking
.active
, cpu
);
116 static __always_inline
bool context_tracking_enabled_this_cpu(void)
118 return context_tracking_enabled() && __this_cpu_read(context_tracking
.active
);
122 * ct_state() - return the current context tracking state if known
124 * Returns the current cpu's context tracking state if context tracking
125 * is enabled. If context tracking is disabled, returns
126 * CT_STATE_DISABLED. This should be used primarily for debugging.
128 static __always_inline
int ct_state(void)
132 if (!context_tracking_enabled())
133 return CT_STATE_DISABLED
;
143 static __always_inline
bool context_tracking_enabled(void) { return false; }
144 static __always_inline
bool context_tracking_enabled_cpu(int cpu
) { return false; }
145 static __always_inline
bool context_tracking_enabled_this_cpu(void) { return false; }
146 #endif /* CONFIG_CONTEXT_TRACKING_USER */