context_tracking.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <linux/static_key.h>
  7. #include <asm/ptrace.h>
  8. struct 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. enum ctx_state {
  17. IN_KERNEL = 0,
  18. IN_USER,
  19. } state;
  20. };
  21. #ifdef CONFIG_CONTEXT_TRACKING
  22. extern struct static_key context_tracking_enabled;
  23. DECLARE_PER_CPU(struct context_tracking, context_tracking);
  24. static inline bool context_tracking_in_user(void)
  25. {
  26. return __this_cpu_read(context_tracking.state) == IN_USER;
  27. }
  28. static inline bool context_tracking_active(void)
  29. {
  30. return __this_cpu_read(context_tracking.active);
  31. }
  32. extern void context_tracking_cpu_set(int cpu);
  33. extern void context_tracking_user_enter(void);
  34. extern void context_tracking_user_exit(void);
  35. static inline void user_enter(void)
  36. {
  37. if (static_key_false(&context_tracking_enabled))
  38. context_tracking_user_enter();
  39. }
  40. static inline void user_exit(void)
  41. {
  42. if (static_key_false(&context_tracking_enabled))
  43. context_tracking_user_exit();
  44. }
  45. static inline enum ctx_state exception_enter(void)
  46. {
  47. enum ctx_state prev_ctx;
  48. if (!static_key_false(&context_tracking_enabled))
  49. return 0;
  50. prev_ctx = this_cpu_read(context_tracking.state);
  51. context_tracking_user_exit();
  52. return prev_ctx;
  53. }
  54. static inline void exception_exit(enum ctx_state prev_ctx)
  55. {
  56. if (static_key_false(&context_tracking_enabled)) {
  57. if (prev_ctx == IN_USER)
  58. context_tracking_user_enter();
  59. }
  60. }
  61. extern void context_tracking_task_switch(struct task_struct *prev,
  62. struct task_struct *next);
  63. #else
  64. static inline bool context_tracking_in_user(void) { return false; }
  65. static inline void user_enter(void) { }
  66. static inline void user_exit(void) { }
  67. static inline enum ctx_state exception_enter(void) { return 0; }
  68. static inline void exception_exit(enum ctx_state prev_ctx) { }
  69. static inline void context_tracking_task_switch(struct task_struct *prev,
  70. struct task_struct *next) { }
  71. #endif /* !CONFIG_CONTEXT_TRACKING */
  72. #ifdef CONFIG_CONTEXT_TRACKING_FORCE
  73. extern void context_tracking_init(void);
  74. #else
  75. static inline void context_tracking_init(void) { }
  76. #endif /* CONFIG_CONTEXT_TRACKING_FORCE */
  77. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  78. extern void guest_enter(void);
  79. extern void guest_exit(void);
  80. #else
  81. static inline void guest_enter(void)
  82. {
  83. /*
  84. * This is running in ioctl context so its safe
  85. * to assume that it's the stime pending cputime
  86. * to flush.
  87. */
  88. vtime_account_system(current);
  89. current->flags |= PF_VCPU;
  90. }
  91. static inline void guest_exit(void)
  92. {
  93. /* Flush the guest cputime we spent on the guest */
  94. vtime_account_system(current);
  95. current->flags &= ~PF_VCPU;
  96. }
  97. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
  98. #endif