context_tracking.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 user_enter(void);
  34. extern void user_exit(void);
  35. static inline enum ctx_state exception_enter(void)
  36. {
  37. enum ctx_state prev_ctx;
  38. prev_ctx = this_cpu_read(context_tracking.state);
  39. user_exit();
  40. return prev_ctx;
  41. }
  42. static inline void exception_exit(enum ctx_state prev_ctx)
  43. {
  44. if (prev_ctx == IN_USER)
  45. user_enter();
  46. }
  47. extern void context_tracking_task_switch(struct task_struct *prev,
  48. struct task_struct *next);
  49. #else
  50. static inline bool context_tracking_in_user(void) { return false; }
  51. static inline void user_enter(void) { }
  52. static inline void user_exit(void) { }
  53. static inline enum ctx_state exception_enter(void) { return 0; }
  54. static inline void exception_exit(enum ctx_state prev_ctx) { }
  55. static inline void context_tracking_task_switch(struct task_struct *prev,
  56. struct task_struct *next) { }
  57. #endif /* !CONFIG_CONTEXT_TRACKING */
  58. #ifdef CONFIG_CONTEXT_TRACKING_FORCE
  59. extern void context_tracking_init(void);
  60. #else
  61. static inline void context_tracking_init(void) { }
  62. #endif /* CONFIG_CONTEXT_TRACKING_FORCE */
  63. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  64. extern void guest_enter(void);
  65. extern void guest_exit(void);
  66. #else
  67. static inline void guest_enter(void)
  68. {
  69. /*
  70. * This is running in ioctl context so its safe
  71. * to assume that it's the stime pending cputime
  72. * to flush.
  73. */
  74. vtime_account_system(current);
  75. current->flags |= PF_VCPU;
  76. }
  77. static inline void guest_exit(void)
  78. {
  79. /* Flush the guest cputime we spent on the guest */
  80. vtime_account_system(current);
  81. current->flags &= ~PF_VCPU;
  82. }
  83. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
  84. #endif