context_tracking.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /* Kernel threads aren't supposed to go to userspace */
  54. WARN_ON_ONCE(!current->mm);
  55. if (!context_tracking_recursion_enter())
  56. return;
  57. if ( __this_cpu_read(context_tracking.state) != state) {
  58. if (__this_cpu_read(context_tracking.active)) {
  59. /*
  60. * At this stage, only low level arch entry code remains and
  61. * then we'll run in userspace. We can assume there won't be
  62. * any RCU read-side critical section until the next call to
  63. * user_exit() or rcu_irq_enter(). Let's remove RCU's dependency
  64. * on the tick.
  65. */
  66. if (state == CONTEXT_USER) {
  67. trace_user_enter(0);
  68. vtime_user_enter(current);
  69. }
  70. rcu_user_enter();
  71. }
  72. /*
  73. * Even if context tracking is disabled on this CPU, because it's outside
  74. * the full dynticks mask for example, we still have to keep track of the
  75. * context transitions and states to prevent inconsistency on those of
  76. * other CPUs.
  77. * If a task triggers an exception in userspace, sleep on the exception
  78. * handler and then migrate to another CPU, that new CPU must know where
  79. * the exception returns by the time we call exception_exit().
  80. * This information can only be provided by the previous CPU when it called
  81. * exception_enter().
  82. * OTOH we can spare the calls to vtime and RCU when context_tracking.active
  83. * is false because we know that CPU is not tickless.
  84. */
  85. __this_cpu_write(context_tracking.state, state);
  86. }
  87. context_tracking_recursion_exit();
  88. }
  89. NOKPROBE_SYMBOL(__context_tracking_enter);
  90. EXPORT_SYMBOL_GPL(__context_tracking_enter);
  91. void context_tracking_enter(enum ctx_state state)
  92. {
  93. unsigned long flags;
  94. /*
  95. * Some contexts may involve an exception occuring in an irq,
  96. * leading to that nesting:
  97. * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
  98. * This would mess up the dyntick_nesting count though. And rcu_irq_*()
  99. * helpers are enough to protect RCU uses inside the exception. So
  100. * just return immediately if we detect we are in an IRQ.
  101. */
  102. if (in_interrupt())
  103. return;
  104. local_irq_save(flags);
  105. __context_tracking_enter(state);
  106. local_irq_restore(flags);
  107. }
  108. NOKPROBE_SYMBOL(context_tracking_enter);
  109. EXPORT_SYMBOL_GPL(context_tracking_enter);
  110. void context_tracking_user_enter(void)
  111. {
  112. user_enter();
  113. }
  114. NOKPROBE_SYMBOL(context_tracking_user_enter);
  115. /**
  116. * context_tracking_exit - Inform the context tracking that the CPU is
  117. * exiting user or guest mode and entering the kernel.
  118. *
  119. * This function must be called after we entered the kernel from user or
  120. * guest space before any use of RCU read side critical section. This
  121. * potentially include any high level kernel code like syscalls, exceptions,
  122. * signal handling, etc...
  123. *
  124. * This call supports re-entrancy. This way it can be called from any exception
  125. * handler without needing to know if we came from userspace or not.
  126. */
  127. void __context_tracking_exit(enum ctx_state state)
  128. {
  129. if (!context_tracking_recursion_enter())
  130. return;
  131. if (__this_cpu_read(context_tracking.state) == state) {
  132. if (__this_cpu_read(context_tracking.active)) {
  133. /*
  134. * We are going to run code that may use RCU. Inform
  135. * RCU core about that (ie: we may need the tick again).
  136. */
  137. rcu_user_exit();
  138. if (state == CONTEXT_USER) {
  139. vtime_user_exit(current);
  140. trace_user_exit(0);
  141. }
  142. }
  143. __this_cpu_write(context_tracking.state, CONTEXT_KERNEL);
  144. }
  145. context_tracking_recursion_exit();
  146. }
  147. NOKPROBE_SYMBOL(__context_tracking_exit);
  148. EXPORT_SYMBOL_GPL(__context_tracking_exit);
  149. void context_tracking_exit(enum ctx_state state)
  150. {
  151. unsigned long flags;
  152. if (in_interrupt())
  153. return;
  154. local_irq_save(flags);
  155. __context_tracking_exit(state);
  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. user_exit();
  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