context_tracking.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/hardirq.h>
  20. #include <linux/export.h>
  21. #define CREATE_TRACE_POINTS
  22. #include <trace/events/context_tracking.h>
  23. struct static_key context_tracking_enabled = STATIC_KEY_INIT_FALSE;
  24. EXPORT_SYMBOL_GPL(context_tracking_enabled);
  25. DEFINE_PER_CPU(struct context_tracking, context_tracking);
  26. EXPORT_SYMBOL_GPL(context_tracking);
  27. void context_tracking_cpu_set(int cpu)
  28. {
  29. if (!per_cpu(context_tracking.active, cpu)) {
  30. per_cpu(context_tracking.active, cpu) = true;
  31. static_key_slow_inc(&context_tracking_enabled);
  32. }
  33. }
  34. /**
  35. * context_tracking_user_enter - Inform the context tracking that the CPU is going to
  36. * enter userspace mode.
  37. *
  38. * This function must be called right before we switch from the kernel
  39. * to userspace, when it's guaranteed the remaining kernel instructions
  40. * to execute won't use any RCU read side critical section because this
  41. * function sets RCU in extended quiescent state.
  42. */
  43. void context_tracking_user_enter(void)
  44. {
  45. unsigned long flags;
  46. /*
  47. * Repeat the user_enter() check here because some archs may be calling
  48. * this from asm and if no CPU needs context tracking, they shouldn't
  49. * go further. Repeat the check here until they support the static key
  50. * check.
  51. */
  52. if (!static_key_false(&context_tracking_enabled))
  53. return;
  54. /*
  55. * Some contexts may involve an exception occuring in an irq,
  56. * leading to that nesting:
  57. * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
  58. * This would mess up the dyntick_nesting count though. And rcu_irq_*()
  59. * helpers are enough to protect RCU uses inside the exception. So
  60. * just return immediately if we detect we are in an IRQ.
  61. */
  62. if (in_interrupt())
  63. return;
  64. /* Kernel threads aren't supposed to go to userspace */
  65. WARN_ON_ONCE(!current->mm);
  66. local_irq_save(flags);
  67. if ( __this_cpu_read(context_tracking.state) != IN_USER) {
  68. if (__this_cpu_read(context_tracking.active)) {
  69. trace_user_enter(0);
  70. /*
  71. * At this stage, only low level arch entry code remains and
  72. * then we'll run in userspace. We can assume there won't be
  73. * any RCU read-side critical section until the next call to
  74. * user_exit() or rcu_irq_enter(). Let's remove RCU's dependency
  75. * on the tick.
  76. */
  77. vtime_user_enter(current);
  78. rcu_user_enter();
  79. }
  80. /*
  81. * Even if context tracking is disabled on this CPU, because it's outside
  82. * the full dynticks mask for example, we still have to keep track of the
  83. * context transitions and states to prevent inconsistency on those of
  84. * other CPUs.
  85. * If a task triggers an exception in userspace, sleep on the exception
  86. * handler and then migrate to another CPU, that new CPU must know where
  87. * the exception returns by the time we call exception_exit().
  88. * This information can only be provided by the previous CPU when it called
  89. * exception_enter().
  90. * OTOH we can spare the calls to vtime and RCU when context_tracking.active
  91. * is false because we know that CPU is not tickless.
  92. */
  93. __this_cpu_write(context_tracking.state, IN_USER);
  94. }
  95. local_irq_restore(flags);
  96. }
  97. #ifdef CONFIG_PREEMPT
  98. /**
  99. * preempt_schedule_context - preempt_schedule called by tracing
  100. *
  101. * The tracing infrastructure uses preempt_enable_notrace to prevent
  102. * recursion and tracing preempt enabling caused by the tracing
  103. * infrastructure itself. But as tracing can happen in areas coming
  104. * from userspace or just about to enter userspace, a preempt enable
  105. * can occur before user_exit() is called. This will cause the scheduler
  106. * to be called when the system is still in usermode.
  107. *
  108. * To prevent this, the preempt_enable_notrace will use this function
  109. * instead of preempt_schedule() to exit user context if needed before
  110. * calling the scheduler.
  111. */
  112. void __sched notrace preempt_schedule_context(void)
  113. {
  114. enum ctx_state prev_ctx;
  115. if (likely(!preemptible()))
  116. return;
  117. /*
  118. * Need to disable preemption in case user_exit() is traced
  119. * and the tracer calls preempt_enable_notrace() causing
  120. * an infinite recursion.
  121. */
  122. preempt_disable_notrace();
  123. prev_ctx = exception_enter();
  124. preempt_enable_no_resched_notrace();
  125. preempt_schedule();
  126. preempt_disable_notrace();
  127. exception_exit(prev_ctx);
  128. preempt_enable_notrace();
  129. }
  130. EXPORT_SYMBOL_GPL(preempt_schedule_context);
  131. #endif /* CONFIG_PREEMPT */
  132. /**
  133. * context_tracking_user_exit - Inform the context tracking that the CPU is
  134. * exiting userspace mode and entering the kernel.
  135. *
  136. * This function must be called after we entered the kernel from userspace
  137. * before any use of RCU read side critical section. This potentially include
  138. * any high level kernel code like syscalls, exceptions, signal handling, etc...
  139. *
  140. * This call supports re-entrancy. This way it can be called from any exception
  141. * handler without needing to know if we came from userspace or not.
  142. */
  143. void context_tracking_user_exit(void)
  144. {
  145. unsigned long flags;
  146. if (!static_key_false(&context_tracking_enabled))
  147. return;
  148. if (in_interrupt())
  149. return;
  150. local_irq_save(flags);
  151. if (__this_cpu_read(context_tracking.state) == IN_USER) {
  152. if (__this_cpu_read(context_tracking.active)) {
  153. /*
  154. * We are going to run code that may use RCU. Inform
  155. * RCU core about that (ie: we may need the tick again).
  156. */
  157. rcu_user_exit();
  158. vtime_user_exit(current);
  159. trace_user_exit(0);
  160. }
  161. __this_cpu_write(context_tracking.state, IN_KERNEL);
  162. }
  163. local_irq_restore(flags);
  164. }
  165. /**
  166. * __context_tracking_task_switch - context switch the syscall callbacks
  167. * @prev: the task that is being switched out
  168. * @next: the task that is being switched in
  169. *
  170. * The context tracking uses the syscall slow path to implement its user-kernel
  171. * boundaries probes on syscalls. This way it doesn't impact the syscall fast
  172. * path on CPUs that don't do context tracking.
  173. *
  174. * But we need to clear the flag on the previous task because it may later
  175. * migrate to some CPU that doesn't do the context tracking. As such the TIF
  176. * flag may not be desired there.
  177. */
  178. void __context_tracking_task_switch(struct task_struct *prev,
  179. struct task_struct *next)
  180. {
  181. clear_tsk_thread_flag(prev, TIF_NOHZ);
  182. set_tsk_thread_flag(next, TIF_NOHZ);
  183. }
  184. #ifdef CONFIG_CONTEXT_TRACKING_FORCE
  185. void __init context_tracking_init(void)
  186. {
  187. int cpu;
  188. for_each_possible_cpu(cpu)
  189. context_tracking_cpu_set(cpu);
  190. }
  191. #endif