context_tracking.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. struct thread_info *ti = current_thread_info();
  84. enum ctx_state prev_ctx;
  85. if (likely(ti->preempt_count || irqs_disabled()))
  86. return;
  87. /*
  88. * Need to disable preemption in case user_exit() is traced
  89. * and the tracer calls preempt_enable_notrace() causing
  90. * an infinite recursion.
  91. */
  92. preempt_disable_notrace();
  93. prev_ctx = exception_enter();
  94. preempt_enable_no_resched_notrace();
  95. preempt_schedule();
  96. preempt_disable_notrace();
  97. exception_exit(prev_ctx);
  98. preempt_enable_notrace();
  99. }
  100. EXPORT_SYMBOL_GPL(preempt_schedule_context);
  101. #endif /* CONFIG_PREEMPT */
  102. /**
  103. * user_exit - Inform the context tracking that the CPU is
  104. * exiting userspace mode and entering the kernel.
  105. *
  106. * This function must be called after we entered the kernel from userspace
  107. * before any use of RCU read side critical section. This potentially include
  108. * any high level kernel code like syscalls, exceptions, signal handling, etc...
  109. *
  110. * This call supports re-entrancy. This way it can be called from any exception
  111. * handler without needing to know if we came from userspace or not.
  112. */
  113. void user_exit(void)
  114. {
  115. unsigned long flags;
  116. if (in_interrupt())
  117. return;
  118. local_irq_save(flags);
  119. if (__this_cpu_read(context_tracking.state) == IN_USER) {
  120. /*
  121. * We are going to run code that may use RCU. Inform
  122. * RCU core about that (ie: we may need the tick again).
  123. */
  124. rcu_user_exit();
  125. vtime_user_exit(current);
  126. __this_cpu_write(context_tracking.state, IN_KERNEL);
  127. }
  128. local_irq_restore(flags);
  129. }
  130. void guest_enter(void)
  131. {
  132. if (vtime_accounting_enabled())
  133. vtime_guest_enter(current);
  134. else
  135. __guest_enter();
  136. }
  137. EXPORT_SYMBOL_GPL(guest_enter);
  138. void guest_exit(void)
  139. {
  140. if (vtime_accounting_enabled())
  141. vtime_guest_exit(current);
  142. else
  143. __guest_exit();
  144. }
  145. EXPORT_SYMBOL_GPL(guest_exit);
  146. /**
  147. * context_tracking_task_switch - context switch the syscall callbacks
  148. * @prev: the task that is being switched out
  149. * @next: the task that is being switched in
  150. *
  151. * The context tracking uses the syscall slow path to implement its user-kernel
  152. * boundaries probes on syscalls. This way it doesn't impact the syscall fast
  153. * path on CPUs that don't do context tracking.
  154. *
  155. * But we need to clear the flag on the previous task because it may later
  156. * migrate to some CPU that doesn't do the context tracking. As such the TIF
  157. * flag may not be desired there.
  158. */
  159. void context_tracking_task_switch(struct task_struct *prev,
  160. struct task_struct *next)
  161. {
  162. if (__this_cpu_read(context_tracking.active)) {
  163. clear_tsk_thread_flag(prev, TIF_NOHZ);
  164. set_tsk_thread_flag(next, TIF_NOHZ);
  165. }
  166. }