context_tracking.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_PER_CPU(struct context_tracking, context_tracking) = {
  22. #ifdef CONFIG_CONTEXT_TRACKING_FORCE
  23. .active = true,
  24. #endif
  25. };
  26. /**
  27. * user_enter - Inform the context tracking that the CPU is going to
  28. * enter userspace mode.
  29. *
  30. * This function must be called right before we switch from the kernel
  31. * to userspace, when it's guaranteed the remaining kernel instructions
  32. * to execute won't use any RCU read side critical section because this
  33. * function sets RCU in extended quiescent state.
  34. */
  35. void user_enter(void)
  36. {
  37. unsigned long flags;
  38. /*
  39. * Some contexts may involve an exception occuring in an irq,
  40. * leading to that nesting:
  41. * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
  42. * This would mess up the dyntick_nesting count though. And rcu_irq_*()
  43. * helpers are enough to protect RCU uses inside the exception. So
  44. * just return immediately if we detect we are in an IRQ.
  45. */
  46. if (in_interrupt())
  47. return;
  48. /* Kernel threads aren't supposed to go to userspace */
  49. WARN_ON_ONCE(!current->mm);
  50. local_irq_save(flags);
  51. if (__this_cpu_read(context_tracking.active) &&
  52. __this_cpu_read(context_tracking.state) != IN_USER) {
  53. /*
  54. * At this stage, only low level arch entry code remains and
  55. * then we'll run in userspace. We can assume there won't be
  56. * any RCU read-side critical section until the next call to
  57. * user_exit() or rcu_irq_enter(). Let's remove RCU's dependency
  58. * on the tick.
  59. */
  60. vtime_user_enter(current);
  61. rcu_user_enter();
  62. __this_cpu_write(context_tracking.state, IN_USER);
  63. }
  64. local_irq_restore(flags);
  65. }
  66. #ifdef CONFIG_PREEMPT
  67. /**
  68. * preempt_schedule_context - preempt_schedule called by tracing
  69. *
  70. * The tracing infrastructure uses preempt_enable_notrace to prevent
  71. * recursion and tracing preempt enabling caused by the tracing
  72. * infrastructure itself. But as tracing can happen in areas coming
  73. * from userspace or just about to enter userspace, a preempt enable
  74. * can occur before user_exit() is called. This will cause the scheduler
  75. * to be called when the system is still in usermode.
  76. *
  77. * To prevent this, the preempt_enable_notrace will use this function
  78. * instead of preempt_schedule() to exit user context if needed before
  79. * calling the scheduler.
  80. */
  81. void __sched notrace preempt_schedule_context(void)
  82. {
  83. enum ctx_state prev_ctx;
  84. if (likely(!preemptible()))
  85. return;
  86. /*
  87. * Need to disable preemption in case user_exit() is traced
  88. * and the tracer calls preempt_enable_notrace() causing
  89. * an infinite recursion.
  90. */
  91. preempt_disable_notrace();
  92. prev_ctx = exception_enter();
  93. preempt_enable_no_resched_notrace();
  94. preempt_schedule();
  95. preempt_disable_notrace();
  96. exception_exit(prev_ctx);
  97. preempt_enable_notrace();
  98. }
  99. EXPORT_SYMBOL_GPL(preempt_schedule_context);
  100. #endif /* CONFIG_PREEMPT */
  101. /**
  102. * user_exit - Inform the context tracking that the CPU is
  103. * exiting userspace mode and entering the kernel.
  104. *
  105. * This function must be called after we entered the kernel from userspace
  106. * before any use of RCU read side critical section. This potentially include
  107. * any high level kernel code like syscalls, exceptions, signal handling, etc...
  108. *
  109. * This call supports re-entrancy. This way it can be called from any exception
  110. * handler without needing to know if we came from userspace or not.
  111. */
  112. void user_exit(void)
  113. {
  114. unsigned long flags;
  115. if (in_interrupt())
  116. return;
  117. local_irq_save(flags);
  118. if (__this_cpu_read(context_tracking.state) == IN_USER) {
  119. /*
  120. * We are going to run code that may use RCU. Inform
  121. * RCU core about that (ie: we may need the tick again).
  122. */
  123. rcu_user_exit();
  124. vtime_user_exit(current);
  125. __this_cpu_write(context_tracking.state, IN_KERNEL);
  126. }
  127. local_irq_restore(flags);
  128. }
  129. void guest_enter(void)
  130. {
  131. if (vtime_accounting_enabled())
  132. vtime_guest_enter(current);
  133. else
  134. __guest_enter();
  135. }
  136. EXPORT_SYMBOL_GPL(guest_enter);
  137. void guest_exit(void)
  138. {
  139. if (vtime_accounting_enabled())
  140. vtime_guest_exit(current);
  141. else
  142. __guest_exit();
  143. }
  144. EXPORT_SYMBOL_GPL(guest_exit);
  145. /**
  146. * context_tracking_task_switch - context switch the syscall callbacks
  147. * @prev: the task that is being switched out
  148. * @next: the task that is being switched in
  149. *
  150. * The context tracking uses the syscall slow path to implement its user-kernel
  151. * boundaries probes on syscalls. This way it doesn't impact the syscall fast
  152. * path on CPUs that don't do context tracking.
  153. *
  154. * But we need to clear the flag on the previous task because it may later
  155. * migrate to some CPU that doesn't do the context tracking. As such the TIF
  156. * flag may not be desired there.
  157. */
  158. void context_tracking_task_switch(struct task_struct *prev,
  159. struct task_struct *next)
  160. {
  161. if (__this_cpu_read(context_tracking.active)) {
  162. clear_tsk_thread_flag(prev, TIF_NOHZ);
  163. set_tsk_thread_flag(next, TIF_NOHZ);
  164. }
  165. }