context_tracking.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef _LINUX_CONTEXT_TRACKING_H
  2. #define _LINUX_CONTEXT_TRACKING_H
  3. #include <linux/sched.h>
  4. #include <linux/percpu.h>
  5. #include <linux/vtime.h>
  6. #include <asm/ptrace.h>
  7. struct 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. static inline void __guest_enter(void)
  21. {
  22. /*
  23. * This is running in ioctl context so we can avoid
  24. * the call to vtime_account() with its unnecessary idle check.
  25. */
  26. vtime_account_system(current);
  27. current->flags |= PF_VCPU;
  28. }
  29. static inline void __guest_exit(void)
  30. {
  31. /*
  32. * This is running in ioctl context so we can avoid
  33. * the call to vtime_account() with its unnecessary idle check.
  34. */
  35. vtime_account_system(current);
  36. current->flags &= ~PF_VCPU;
  37. }
  38. #ifdef CONFIG_CONTEXT_TRACKING
  39. DECLARE_PER_CPU(struct context_tracking, context_tracking);
  40. static inline bool context_tracking_in_user(void)
  41. {
  42. return __this_cpu_read(context_tracking.state) == IN_USER;
  43. }
  44. static inline bool context_tracking_active(void)
  45. {
  46. return __this_cpu_read(context_tracking.active);
  47. }
  48. extern void user_enter(void);
  49. extern void user_exit(void);
  50. extern void guest_enter(void);
  51. extern void guest_exit(void);
  52. static inline enum ctx_state exception_enter(void)
  53. {
  54. enum ctx_state prev_ctx;
  55. prev_ctx = this_cpu_read(context_tracking.state);
  56. user_exit();
  57. return prev_ctx;
  58. }
  59. static inline void exception_exit(enum ctx_state prev_ctx)
  60. {
  61. if (prev_ctx == IN_USER)
  62. user_enter();
  63. }
  64. extern void context_tracking_task_switch(struct task_struct *prev,
  65. struct task_struct *next);
  66. #else
  67. static inline bool context_tracking_in_user(void) { return false; }
  68. static inline void user_enter(void) { }
  69. static inline void user_exit(void) { }
  70. static inline void guest_enter(void)
  71. {
  72. __guest_enter();
  73. }
  74. static inline void guest_exit(void)
  75. {
  76. __guest_exit();
  77. }
  78. static inline enum ctx_state exception_enter(void) { return 0; }
  79. static inline void exception_exit(enum ctx_state prev_ctx) { }
  80. static inline void context_tracking_task_switch(struct task_struct *prev,
  81. struct task_struct *next) { }
  82. #endif /* !CONFIG_CONTEXT_TRACKING */
  83. #endif