context_tracking.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <linux/context_tracking.h>
  2. #include <linux/rcupdate.h>
  3. #include <linux/sched.h>
  4. #include <linux/percpu.h>
  5. #include <linux/hardirq.h>
  6. struct context_tracking {
  7. /*
  8. * When active is false, hooks are not set to
  9. * minimize overhead: TIF flags are cleared
  10. * and calls to user_enter/exit are ignored. This
  11. * may be further optimized using static keys.
  12. */
  13. bool active;
  14. enum {
  15. IN_KERNEL = 0,
  16. IN_USER,
  17. } state;
  18. };
  19. static DEFINE_PER_CPU(struct context_tracking, context_tracking) = {
  20. #ifdef CONFIG_CONTEXT_TRACKING_FORCE
  21. .active = true,
  22. #endif
  23. };
  24. void user_enter(void)
  25. {
  26. unsigned long flags;
  27. /*
  28. * Some contexts may involve an exception occuring in an irq,
  29. * leading to that nesting:
  30. * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
  31. * This would mess up the dyntick_nesting count though. And rcu_irq_*()
  32. * helpers are enough to protect RCU uses inside the exception. So
  33. * just return immediately if we detect we are in an IRQ.
  34. */
  35. if (in_interrupt())
  36. return;
  37. WARN_ON_ONCE(!current->mm);
  38. local_irq_save(flags);
  39. if (__this_cpu_read(context_tracking.active) &&
  40. __this_cpu_read(context_tracking.state) != IN_USER) {
  41. __this_cpu_write(context_tracking.state, IN_USER);
  42. rcu_user_enter();
  43. }
  44. local_irq_restore(flags);
  45. }
  46. void user_exit(void)
  47. {
  48. unsigned long flags;
  49. /*
  50. * Some contexts may involve an exception occuring in an irq,
  51. * leading to that nesting:
  52. * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
  53. * This would mess up the dyntick_nesting count though. And rcu_irq_*()
  54. * helpers are enough to protect RCU uses inside the exception. So
  55. * just return immediately if we detect we are in an IRQ.
  56. */
  57. if (in_interrupt())
  58. return;
  59. local_irq_save(flags);
  60. if (__this_cpu_read(context_tracking.state) == IN_USER) {
  61. __this_cpu_write(context_tracking.state, IN_KERNEL);
  62. rcu_user_exit();
  63. }
  64. local_irq_restore(flags);
  65. }
  66. void context_tracking_task_switch(struct task_struct *prev,
  67. struct task_struct *next)
  68. {
  69. if (__this_cpu_read(context_tracking.active)) {
  70. clear_tsk_thread_flag(prev, TIF_NOHZ);
  71. set_tsk_thread_flag(next, TIF_NOHZ);
  72. }
  73. }