context_tracking.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. static bool context_tracking_recursion_enter(void)
  29. {
  30. int recursion;
  31. recursion = __this_cpu_inc_return(context_tracking.recursion);
  32. if (recursion == 1)
  33. return true;
  34. WARN_ONCE((recursion < 1), "Invalid context tracking recursion value %d\n", recursion);
  35. __this_cpu_dec(context_tracking.recursion);
  36. return false;
  37. }
  38. static void context_tracking_recursion_exit(void)
  39. {
  40. __this_cpu_dec(context_tracking.recursion);
  41. }
  42. /**
  43. * context_tracking_enter - Inform the context tracking that the CPU is going
  44. * enter user or guest space mode.
  45. *
  46. * This function must be called right before we switch from the kernel
  47. * to user or guest space, when it's guaranteed the remaining kernel
  48. * instructions to execute won't use any RCU read side critical section
  49. * because this function sets RCU in extended quiescent state.
  50. */
  51. void context_tracking_enter(enum ctx_state state)
  52. {
  53. unsigned long flags;
  54. /*
  55. * Repeat the user_enter() check here because some archs may be calling
  56. * this from asm and if no CPU needs context tracking, they shouldn't
  57. * go further. Repeat the check here until they support the inline static
  58. * key check.
  59. */
  60. if (!context_tracking_is_enabled())
  61. return;
  62. /*
  63. * Some contexts may involve an exception occuring in an irq,
  64. * leading to that nesting:
  65. * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
  66. * This would mess up the dyntick_nesting count though. And rcu_irq_*()
  67. * helpers are enough to protect RCU uses inside the exception. So
  68. * just return immediately if we detect we are in an IRQ.
  69. */
  70. if (in_interrupt())
  71. return;
  72. /* Kernel threads aren't supposed to go to userspace */
  73. WARN_ON_ONCE(!current->mm);
  74. local_irq_save(flags);
  75. if (!context_tracking_recursion_enter())
  76. goto out_irq_restore;
  77. if ( __this_cpu_read(context_tracking.state) != state) {
  78. if (__this_cpu_read(context_tracking.active)) {
  79. /*
  80. * At this stage, only low level arch entry code remains and
  81. * then we'll run in userspace. We can assume there won't be
  82. * any RCU read-side critical section until the next call to
  83. * user_exit() or rcu_irq_enter(). Let's remove RCU's dependency
  84. * on the tick.
  85. */
  86. if (state == CONTEXT_USER) {
  87. trace_user_enter(0);
  88. vtime_user_enter(current);
  89. }
  90. rcu_user_enter();
  91. }
  92. /*
  93. * Even if context tracking is disabled on this CPU, because it's outside
  94. * the full dynticks mask for example, we still have to keep track of the
  95. * context transitions and states to prevent inconsistency on those of
  96. * other CPUs.
  97. * If a task triggers an exception in userspace, sleep on the exception
  98. * handler and then migrate to another CPU, that new CPU must know where
  99. * the exception returns by the time we call exception_exit().
  100. * This information can only be provided by the previous CPU when it called
  101. * exception_enter().
  102. * OTOH we can spare the calls to vtime and RCU when context_tracking.active
  103. * is false because we know that CPU is not tickless.
  104. */
  105. __this_cpu_write(context_tracking.state, state);
  106. }
  107. context_tracking_recursion_exit();
  108. out_irq_restore:
  109. local_irq_restore(flags);
  110. }
  111. NOKPROBE_SYMBOL(context_tracking_enter);
  112. EXPORT_SYMBOL_GPL(context_tracking_enter);
  113. void context_tracking_user_enter(void)
  114. {
  115. context_tracking_enter(CONTEXT_USER);
  116. }
  117. NOKPROBE_SYMBOL(context_tracking_user_enter);
  118. /**
  119. * context_tracking_exit - Inform the context tracking that the CPU is
  120. * exiting user or guest mode and entering the kernel.
  121. *
  122. * This function must be called after we entered the kernel from user or
  123. * guest space before any use of RCU read side critical section. This
  124. * potentially include any high level kernel code like syscalls, exceptions,
  125. * signal handling, etc...
  126. *
  127. * This call supports re-entrancy. This way it can be called from any exception
  128. * handler without needing to know if we came from userspace or not.
  129. */
  130. void context_tracking_exit(enum ctx_state state)
  131. {
  132. unsigned long flags;
  133. if (!context_tracking_is_enabled())
  134. return;
  135. if (in_interrupt())
  136. return;
  137. local_irq_save(flags);
  138. if (!context_tracking_recursion_enter())
  139. goto out_irq_restore;
  140. if (__this_cpu_read(context_tracking.state) == state) {
  141. if (__this_cpu_read(context_tracking.active)) {
  142. /*
  143. * We are going to run code that may use RCU. Inform
  144. * RCU core about that (ie: we may need the tick again).
  145. */
  146. rcu_user_exit();
  147. if (state == CONTEXT_USER) {
  148. vtime_user_exit(current);
  149. trace_user_exit(0);
  150. }
  151. }
  152. __this_cpu_write(context_tracking.state, CONTEXT_KERNEL);
  153. }
  154. context_tracking_recursion_exit();
  155. out_irq_restore:
  156. local_irq_restore(flags);
  157. }
  158. NOKPROBE_SYMBOL(context_tracking_exit);
  159. EXPORT_SYMBOL_GPL(context_tracking_exit);
  160. void context_tracking_user_exit(void)
  161. {
  162. context_tracking_exit(CONTEXT_USER);
  163. }
  164. NOKPROBE_SYMBOL(context_tracking_user_exit);
  165. void __init context_tracking_cpu_set(int cpu)
  166. {
  167. static __initdata bool initialized = false;
  168. if (!per_cpu(context_tracking.active, cpu)) {
  169. per_cpu(context_tracking.active, cpu) = true;
  170. static_key_slow_inc(&context_tracking_enabled);
  171. }
  172. if (initialized)
  173. return;
  174. /*
  175. * Set TIF_NOHZ to init/0 and let it propagate to all tasks through fork
  176. * This assumes that init is the only task at this early boot stage.
  177. */
  178. set_tsk_thread_flag(&init_task, TIF_NOHZ);
  179. WARN_ON_ONCE(!tasklist_empty());
  180. initialized = true;
  181. }
  182. #ifdef CONFIG_CONTEXT_TRACKING_FORCE
  183. void __init context_tracking_init(void)
  184. {
  185. int cpu;
  186. for_each_possible_cpu(cpu)
  187. context_tracking_cpu_set(cpu);
  188. }
  189. #endif