context_tracking.c 6.9 KB

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