context_tracking.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef _LINUX_CONTEXT_TRACKING_H
  2. #define _LINUX_CONTEXT_TRACKING_H
  3. #include <linux/sched.h>
  4. #include <linux/vtime.h>
  5. #include <linux/context_tracking_state.h>
  6. #include <asm/ptrace.h>
  7. #ifdef CONFIG_CONTEXT_TRACKING
  8. extern void context_tracking_cpu_set(int cpu);
  9. extern void context_tracking_user_enter(void);
  10. extern void context_tracking_user_exit(void);
  11. extern void __context_tracking_task_switch(struct task_struct *prev,
  12. struct task_struct *next);
  13. static inline void user_enter(void)
  14. {
  15. if (static_key_false(&context_tracking_enabled))
  16. context_tracking_user_enter();
  17. }
  18. static inline void user_exit(void)
  19. {
  20. if (static_key_false(&context_tracking_enabled))
  21. context_tracking_user_exit();
  22. }
  23. static inline enum ctx_state exception_enter(void)
  24. {
  25. enum ctx_state prev_ctx;
  26. if (!static_key_false(&context_tracking_enabled))
  27. return 0;
  28. prev_ctx = this_cpu_read(context_tracking.state);
  29. context_tracking_user_exit();
  30. return prev_ctx;
  31. }
  32. static inline void exception_exit(enum ctx_state prev_ctx)
  33. {
  34. if (static_key_false(&context_tracking_enabled)) {
  35. if (prev_ctx == IN_USER)
  36. context_tracking_user_enter();
  37. }
  38. }
  39. static inline void context_tracking_task_switch(struct task_struct *prev,
  40. struct task_struct *next)
  41. {
  42. if (static_key_false(&context_tracking_enabled))
  43. __context_tracking_task_switch(prev, next);
  44. }
  45. #else
  46. static inline void user_enter(void) { }
  47. static inline void user_exit(void) { }
  48. static inline enum ctx_state exception_enter(void) { return 0; }
  49. static inline void exception_exit(enum ctx_state prev_ctx) { }
  50. static inline void context_tracking_task_switch(struct task_struct *prev,
  51. struct task_struct *next) { }
  52. #endif /* !CONFIG_CONTEXT_TRACKING */
  53. #ifdef CONFIG_CONTEXT_TRACKING_FORCE
  54. extern void context_tracking_init(void);
  55. #else
  56. static inline void context_tracking_init(void) { }
  57. #endif /* CONFIG_CONTEXT_TRACKING_FORCE */
  58. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  59. static inline void guest_enter(void)
  60. {
  61. if (static_key_false(&context_tracking_enabled) &&
  62. vtime_accounting_enabled())
  63. vtime_guest_enter(current);
  64. else
  65. current->flags |= PF_VCPU;
  66. }
  67. static inline void guest_exit(void)
  68. {
  69. if (static_key_false(&context_tracking_enabled) &&
  70. vtime_accounting_enabled())
  71. vtime_guest_exit(current);
  72. else
  73. current->flags &= ~PF_VCPU;
  74. }
  75. #else
  76. static inline void guest_enter(void)
  77. {
  78. /*
  79. * This is running in ioctl context so its safe
  80. * to assume that it's the stime pending cputime
  81. * to flush.
  82. */
  83. vtime_account_system(current);
  84. current->flags |= PF_VCPU;
  85. }
  86. static inline void guest_exit(void)
  87. {
  88. /* Flush the guest cputime we spent on the guest */
  89. vtime_account_system(current);
  90. current->flags &= ~PF_VCPU;
  91. }
  92. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
  93. #endif