2017-11-01 15:07:57 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2012-11-27 19:33:25 +01:00
|
|
|
#ifndef _LINUX_CONTEXT_TRACKING_H
|
|
|
|
|
#define _LINUX_CONTEXT_TRACKING_H
|
|
|
|
|
|
|
|
|
|
#include <linux/sched.h>
|
2013-05-16 01:21:38 +02:00
|
|
|
#include <linux/vtime.h>
|
2013-07-12 02:15:49 +02:00
|
|
|
#include <linux/context_tracking_state.h>
|
2020-07-24 13:50:25 +02:00
|
|
|
#include <linux/instrumentation.h>
|
|
|
|
|
|
2013-02-24 00:23:25 +01:00
|
|
|
#include <asm/ptrace.h>
|
2013-01-07 18:12:14 +01:00
|
|
|
|
2013-05-16 01:21:38 +02:00
|
|
|
|
2022-06-08 16:40:24 +02:00
|
|
|
#ifdef CONFIG_CONTEXT_TRACKING_USER
|
2022-06-08 16:40:23 +02:00
|
|
|
extern void ct_cpu_track_user(int cpu);
|
2013-07-10 00:55:25 +02:00
|
|
|
|
2015-10-28 02:39:56 +01:00
|
|
|
/* Called with interrupts disabled. */
|
2022-06-08 16:40:20 +02:00
|
|
|
extern void __ct_user_enter(enum ctx_state state);
|
|
|
|
|
extern void __ct_user_exit(enum ctx_state state);
|
2015-10-28 02:39:56 +01:00
|
|
|
|
2022-06-08 16:40:22 +02:00
|
|
|
extern void ct_user_enter(enum ctx_state state);
|
|
|
|
|
extern void ct_user_exit(enum ctx_state state);
|
|
|
|
|
|
2022-06-08 16:40:21 +02:00
|
|
|
extern void user_enter_callable(void);
|
|
|
|
|
extern void user_exit_callable(void);
|
2013-07-10 02:44:35 +02:00
|
|
|
|
|
|
|
|
static inline void user_enter(void)
|
|
|
|
|
{
|
2019-10-16 04:56:51 +02:00
|
|
|
if (context_tracking_enabled())
|
2023-07-25 12:08:50 +01:00
|
|
|
ct_user_enter(CT_STATE_USER);
|
2013-07-10 02:44:35 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
static inline void user_exit(void)
|
|
|
|
|
{
|
2019-10-16 04:56:51 +02:00
|
|
|
if (context_tracking_enabled())
|
2023-07-25 12:08:50 +01:00
|
|
|
ct_user_exit(CT_STATE_USER);
|
2013-07-10 02:44:35 +02:00
|
|
|
}
|
2013-02-24 00:23:25 +01:00
|
|
|
|
2016-06-20 16:58:29 +02:00
|
|
|
/* Called with interrupts disabled. */
|
2020-03-04 11:05:22 +01:00
|
|
|
static __always_inline void user_enter_irqoff(void)
|
2016-06-20 16:58:29 +02:00
|
|
|
{
|
2019-10-16 04:56:51 +02:00
|
|
|
if (context_tracking_enabled())
|
2023-07-25 12:08:50 +01:00
|
|
|
__ct_user_enter(CT_STATE_USER);
|
2016-06-20 16:58:29 +02:00
|
|
|
|
|
|
|
|
}
|
2020-03-04 11:05:22 +01:00
|
|
|
static __always_inline void user_exit_irqoff(void)
|
2016-06-20 16:58:29 +02:00
|
|
|
{
|
2019-10-16 04:56:51 +02:00
|
|
|
if (context_tracking_enabled())
|
2023-07-25 12:08:50 +01:00
|
|
|
__ct_user_exit(CT_STATE_USER);
|
2016-06-20 16:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
2013-02-24 01:19:14 +01:00
|
|
|
static inline enum ctx_state exception_enter(void)
|
2013-02-24 00:23:25 +01:00
|
|
|
{
|
2013-02-24 01:19:14 +01:00
|
|
|
enum ctx_state prev_ctx;
|
|
|
|
|
|
2022-06-08 16:40:24 +02:00
|
|
|
if (IS_ENABLED(CONFIG_HAVE_CONTEXT_TRACKING_USER_OFFSTACK) ||
|
2020-11-17 16:16:34 +01:00
|
|
|
!context_tracking_enabled())
|
2013-07-10 02:44:35 +02:00
|
|
|
return 0;
|
|
|
|
|
|
2022-06-08 16:40:35 +02:00
|
|
|
prev_ctx = __ct_state();
|
2023-07-25 12:08:50 +01:00
|
|
|
if (prev_ctx != CT_STATE_KERNEL)
|
2022-06-08 16:40:22 +02:00
|
|
|
ct_user_exit(prev_ctx);
|
2013-02-24 01:19:14 +01:00
|
|
|
|
|
|
|
|
return prev_ctx;
|
2013-02-24 00:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
2013-02-24 01:19:14 +01:00
|
|
|
static inline void exception_exit(enum ctx_state prev_ctx)
|
2013-02-24 00:23:25 +01:00
|
|
|
{
|
2022-06-08 16:40:24 +02:00
|
|
|
if (!IS_ENABLED(CONFIG_HAVE_CONTEXT_TRACKING_USER_OFFSTACK) &&
|
2020-11-17 16:16:34 +01:00
|
|
|
context_tracking_enabled()) {
|
2023-07-25 12:08:50 +01:00
|
|
|
if (prev_ctx != CT_STATE_KERNEL)
|
2022-06-08 16:40:22 +02:00
|
|
|
ct_user_enter(prev_ctx);
|
2013-07-10 02:44:35 +02:00
|
|
|
}
|
2013-02-24 00:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-04 17:27:33 -07:00
|
|
|
static __always_inline bool context_tracking_guest_enter(void)
|
|
|
|
|
{
|
|
|
|
|
if (context_tracking_enabled())
|
2023-07-25 12:08:50 +01:00
|
|
|
__ct_user_enter(CT_STATE_GUEST);
|
2021-05-04 17:27:33 -07:00
|
|
|
|
|
|
|
|
return context_tracking_enabled_this_cpu();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 23:05:56 -03:00
|
|
|
static __always_inline bool context_tracking_guest_exit(void)
|
2021-05-04 17:27:33 -07:00
|
|
|
{
|
|
|
|
|
if (context_tracking_enabled())
|
2023-07-25 12:08:50 +01:00
|
|
|
__ct_user_exit(CT_STATE_GUEST);
|
2024-05-10 23:05:56 -03:00
|
|
|
|
|
|
|
|
return context_tracking_enabled_this_cpu();
|
2021-05-04 17:27:33 -07:00
|
|
|
}
|
2015-07-03 12:44:21 -07:00
|
|
|
|
2022-06-08 16:40:35 +02:00
|
|
|
#define CT_WARN_ON(cond) WARN_ON(context_tracking_enabled() && (cond))
|
|
|
|
|
|
2012-11-27 19:33:25 +01:00
|
|
|
#else
|
|
|
|
|
static inline void user_enter(void) { }
|
|
|
|
|
static inline void user_exit(void) { }
|
2016-06-20 16:58:29 +02:00
|
|
|
static inline void user_enter_irqoff(void) { }
|
|
|
|
|
static inline void user_exit_irqoff(void) { }
|
2022-06-08 16:40:35 +02:00
|
|
|
static inline int exception_enter(void) { return 0; }
|
2013-02-24 01:19:14 +01:00
|
|
|
static inline void exception_exit(enum ctx_state prev_ctx) { }
|
2022-06-08 16:40:35 +02:00
|
|
|
static inline int ct_state(void) { return -1; }
|
2023-02-25 16:01:36 -08:00
|
|
|
static inline int __ct_state(void) { return -1; }
|
2021-06-24 11:41:05 +02:00
|
|
|
static __always_inline bool context_tracking_guest_enter(void) { return false; }
|
2024-05-10 23:05:56 -03:00
|
|
|
static __always_inline bool context_tracking_guest_exit(void) { return false; }
|
2022-06-08 16:40:35 +02:00
|
|
|
#define CT_WARN_ON(cond) do { } while (0)
|
2022-06-08 16:40:24 +02:00
|
|
|
#endif /* !CONFIG_CONTEXT_TRACKING_USER */
|
2012-11-27 19:33:25 +01:00
|
|
|
|
2022-06-08 16:40:24 +02:00
|
|
|
#ifdef CONFIG_CONTEXT_TRACKING_USER_FORCE
|
2013-07-11 19:12:32 +02:00
|
|
|
extern void context_tracking_init(void);
|
|
|
|
|
#else
|
|
|
|
|
static inline void context_tracking_init(void) { }
|
2022-06-08 16:40:24 +02:00
|
|
|
#endif /* CONFIG_CONTEXT_TRACKING_USER_FORCE */
|
2013-07-11 19:12:32 +02:00
|
|
|
|
2022-06-08 16:40:25 +02:00
|
|
|
#ifdef CONFIG_CONTEXT_TRACKING_IDLE
|
|
|
|
|
extern void ct_idle_enter(void);
|
|
|
|
|
extern void ct_idle_exit(void);
|
2022-06-08 16:40:33 +02:00
|
|
|
|
|
|
|
|
/*
|
2024-04-16 11:01:23 +02:00
|
|
|
* Is RCU watching the current CPU (IOW, it is not in an extended quiescent state)?
|
|
|
|
|
*
|
|
|
|
|
* Note that this returns the actual boolean data (watching / not watching),
|
|
|
|
|
* whereas ct_rcu_watching() returns the RCU_WATCHING subvariable of
|
|
|
|
|
* context_tracking.state.
|
2022-06-08 16:40:33 +02:00
|
|
|
*
|
|
|
|
|
* No ordering, as we are sampling CPU-local information.
|
|
|
|
|
*/
|
2024-04-16 11:01:23 +02:00
|
|
|
static __always_inline bool rcu_is_watching_curr_cpu(void)
|
2022-06-08 16:40:33 +02:00
|
|
|
{
|
2024-04-16 11:01:23 +02:00
|
|
|
return raw_atomic_read(this_cpu_ptr(&context_tracking.state)) & CT_RCU_WATCHING;
|
2022-06-08 16:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2022-06-08 16:40:35 +02:00
|
|
|
* Increment the current CPU's context_tracking structure's ->state field
|
2022-06-08 16:40:33 +02:00
|
|
|
* with ordering. Return the new value.
|
|
|
|
|
*/
|
2022-06-08 16:40:35 +02:00
|
|
|
static __always_inline unsigned long ct_state_inc(int incby)
|
2022-06-08 16:40:33 +02:00
|
|
|
{
|
2023-06-05 08:01:15 +01:00
|
|
|
return raw_atomic_add_return(incby, this_cpu_ptr(&context_tracking.state));
|
2022-06-08 16:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 16:08:31 +01:00
|
|
|
static __always_inline bool warn_rcu_enter(void)
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Horrible hack to shut up recursive RCU isn't watching fail since
|
|
|
|
|
* lots of the actual reporting also relies on RCU.
|
|
|
|
|
*/
|
|
|
|
|
preempt_disable_notrace();
|
2024-04-16 11:01:23 +02:00
|
|
|
if (!rcu_is_watching_curr_cpu()) {
|
2023-01-26 16:08:31 +01:00
|
|
|
ret = true;
|
2023-07-25 12:19:01 +01:00
|
|
|
ct_state_inc(CT_RCU_WATCHING);
|
2023-01-26 16:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static __always_inline void warn_rcu_exit(bool rcu)
|
|
|
|
|
{
|
|
|
|
|
if (rcu)
|
2023-07-25 12:19:01 +01:00
|
|
|
ct_state_inc(CT_RCU_WATCHING);
|
2023-01-26 16:08:31 +01:00
|
|
|
preempt_enable_notrace();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 16:40:25 +02:00
|
|
|
#else
|
|
|
|
|
static inline void ct_idle_enter(void) { }
|
|
|
|
|
static inline void ct_idle_exit(void) { }
|
2023-01-26 16:08:31 +01:00
|
|
|
|
|
|
|
|
static __always_inline bool warn_rcu_enter(void) { return false; }
|
|
|
|
|
static __always_inline void warn_rcu_exit(bool rcu) { }
|
2022-06-08 16:40:25 +02:00
|
|
|
#endif /* !CONFIG_CONTEXT_TRACKING_IDLE */
|
|
|
|
|
|
2012-11-27 19:33:25 +01:00
|
|
|
#endif
|