context_tracking.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Context tracking: Probe on high level context boundaries such as kernel
  3. * and userspace. This includes syscalls and exceptions entry/exit.
  4. *
  5. * This is used by RCU to remove its dependency on the timer tick while a CPU
  6. * runs in userspace.
  7. *
  8. * Started by Frederic Weisbecker:
  9. *
  10. * Copyright (C) 2012 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
  11. *
  12. * Many thanks to Gilad Ben-Yossef, Paul McKenney, Ingo Molnar, Andrew Morton,
  13. * Steven Rostedt, Peter Zijlstra for suggestions and improvements.
  14. *
  15. */
  16. #include <linux/context_tracking.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/sched.h>
  19. #include <linux/percpu.h>
  20. #include <linux/hardirq.h>
  21. struct context_tracking {
  22. /*
  23. * When active is false, probes are unset in order
  24. * to minimize overhead: TIF flags are cleared
  25. * and calls to user_enter/exit are ignored. This
  26. * may be further optimized using static keys.
  27. */
  28. bool active;
  29. enum {
  30. IN_KERNEL = 0,
  31. IN_USER,
  32. } state;
  33. };
  34. static DEFINE_PER_CPU(struct context_tracking, context_tracking) = {
  35. #ifdef CONFIG_CONTEXT_TRACKING_FORCE
  36. .active = true,
  37. #endif
  38. };
  39. /**
  40. * user_enter - Inform the context tracking that the CPU is going to
  41. * enter userspace mode.
  42. *
  43. * This function must be called right before we switch from the kernel
  44. * to userspace, when it's guaranteed the remaining kernel instructions
  45. * to execute won't use any RCU read side critical section because this
  46. * function sets RCU in extended quiescent state.
  47. */
  48. void user_enter(void)
  49. {
  50. unsigned long flags;
  51. /*
  52. * Some contexts may involve an exception occuring in an irq,
  53. * leading to that nesting:
  54. * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
  55. * This would mess up the dyntick_nesting count though. And rcu_irq_*()
  56. * helpers are enough to protect RCU uses inside the exception. So
  57. * just return immediately if we detect we are in an IRQ.
  58. */
  59. if (in_interrupt())
  60. return;
  61. /* Kernel threads aren't supposed to go to userspace */
  62. WARN_ON_ONCE(!current->mm);
  63. local_irq_save(flags);
  64. if (__this_cpu_read(context_tracking.active) &&
  65. __this_cpu_read(context_tracking.state) != IN_USER) {
  66. __this_cpu_write(context_tracking.state, IN_USER);
  67. /*
  68. * At this stage, only low level arch entry code remains and
  69. * then we'll run in userspace. We can assume there won't be
  70. * any RCU read-side critical section until the next call to
  71. * user_exit() or rcu_irq_enter(). Let's remove RCU's dependency
  72. * on the tick.
  73. */
  74. rcu_user_enter();
  75. }
  76. local_irq_restore(flags);
  77. }
  78. /**
  79. * user_exit - Inform the context tracking that the CPU is
  80. * exiting userspace mode and entering the kernel.
  81. *
  82. * This function must be called after we entered the kernel from userspace
  83. * before any use of RCU read side critical section. This potentially include
  84. * any high level kernel code like syscalls, exceptions, signal handling, etc...
  85. *
  86. * This call supports re-entrancy. This way it can be called from any exception
  87. * handler without needing to know if we came from userspace or not.
  88. */
  89. void user_exit(void)
  90. {
  91. unsigned long flags;
  92. if (in_interrupt())
  93. return;
  94. local_irq_save(flags);
  95. if (__this_cpu_read(context_tracking.state) == IN_USER) {
  96. __this_cpu_write(context_tracking.state, IN_KERNEL);
  97. /*
  98. * We are going to run code that may use RCU. Inform
  99. * RCU core about that (ie: we may need the tick again).
  100. */
  101. rcu_user_exit();
  102. }
  103. local_irq_restore(flags);
  104. }
  105. /**
  106. * context_tracking_task_switch - context switch the syscall callbacks
  107. * @prev: the task that is being switched out
  108. * @next: the task that is being switched in
  109. *
  110. * The context tracking uses the syscall slow path to implement its user-kernel
  111. * boundaries probes on syscalls. This way it doesn't impact the syscall fast
  112. * path on CPUs that don't do context tracking.
  113. *
  114. * But we need to clear the flag on the previous task because it may later
  115. * migrate to some CPU that doesn't do the context tracking. As such the TIF
  116. * flag may not be desired there.
  117. */
  118. void context_tracking_task_switch(struct task_struct *prev,
  119. struct task_struct *next)
  120. {
  121. if (__this_cpu_read(context_tracking.active)) {
  122. clear_tsk_thread_flag(prev, TIF_NOHZ);
  123. set_tsk_thread_flag(next, TIF_NOHZ);
  124. }
  125. }