blob: f128dc3be0dfeed67f7a25e9a4863cff17346937 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Frederic Weisbeckere7358b32013-07-12 02:15:49 +02002#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;
Frederic Weisbeckeraed5ed42015-05-06 18:04:23 +020016 int recursion;
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020017 enum ctx_state {
Andy Lutomirskif9281642015-07-03 12:44:21 -070018 CONTEXT_DISABLED = -1, /* returned by ct_state() if unknown */
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +010019 CONTEXT_KERNEL = 0,
20 CONTEXT_USER,
Rik van Riel126a6a52015-02-10 15:27:54 -050021 CONTEXT_GUEST,
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020022 } state;
23};
24
25#ifdef CONFIG_CONTEXT_TRACKING
Andy Lutomirskied11a7f2015-11-12 12:59:01 -080026extern struct static_key_false context_tracking_enabled;
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020027DECLARE_PER_CPU(struct context_tracking, context_tracking);
28
Frederic Weisbecker58135f52013-11-06 14:45:57 +010029static inline bool context_tracking_is_enabled(void)
30{
Andy Lutomirskied11a7f2015-11-12 12:59:01 -080031 return static_branch_unlikely(&context_tracking_enabled);
Frederic Weisbecker58135f52013-11-06 14:45:57 +010032}
Frederic Weisbeckerd0df09e2013-11-06 15:11:57 +010033
34static inline bool context_tracking_cpu_is_enabled(void)
35{
36 return __this_cpu_read(context_tracking.active);
37}
38
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020039static inline bool context_tracking_in_user(void)
40{
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +010041 return __this_cpu_read(context_tracking.state) == CONTEXT_USER;
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020042}
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020043#else
44static inline bool context_tracking_in_user(void) { return false; }
45static inline bool context_tracking_active(void) { return false; }
Rik van Riela3a9c7d2015-02-10 15:27:51 -050046static inline bool context_tracking_is_enabled(void) { return false; }
47static inline bool context_tracking_cpu_is_enabled(void) { return false; }
Frederic Weisbeckere7358b32013-07-12 02:15:49 +020048#endif /* CONFIG_CONTEXT_TRACKING */
49
50#endif