context_tracking.c 5.5 KB

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