context_tracking.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /**
  68. * user_exit - Inform the context tracking that the CPU is
  69. * exiting userspace mode and entering the kernel.
  70. *
  71. * This function must be called after we entered the kernel from userspace
  72. * before any use of RCU read side critical section. This potentially include
  73. * any high level kernel code like syscalls, exceptions, signal handling, etc...
  74. *
  75. * This call supports re-entrancy. This way it can be called from any exception
  76. * handler without needing to know if we came from userspace or not.
  77. */
  78. void user_exit(void)
  79. {
  80. unsigned long flags;
  81. if (in_interrupt())
  82. return;
  83. local_irq_save(flags);
  84. if (__this_cpu_read(context_tracking.state) == IN_USER) {
  85. /*
  86. * We are going to run code that may use RCU. Inform
  87. * RCU core about that (ie: we may need the tick again).
  88. */
  89. rcu_user_exit();
  90. vtime_user_exit(current);
  91. __this_cpu_write(context_tracking.state, IN_KERNEL);
  92. }
  93. local_irq_restore(flags);
  94. }
  95. void guest_enter(void)
  96. {
  97. if (vtime_accounting_enabled())
  98. vtime_guest_enter(current);
  99. else
  100. __guest_enter();
  101. }
  102. EXPORT_SYMBOL_GPL(guest_enter);
  103. void guest_exit(void)
  104. {
  105. if (vtime_accounting_enabled())
  106. vtime_guest_exit(current);
  107. else
  108. __guest_exit();
  109. }
  110. EXPORT_SYMBOL_GPL(guest_exit);
  111. /**
  112. * context_tracking_task_switch - context switch the syscall callbacks
  113. * @prev: the task that is being switched out
  114. * @next: the task that is being switched in
  115. *
  116. * The context tracking uses the syscall slow path to implement its user-kernel
  117. * boundaries probes on syscalls. This way it doesn't impact the syscall fast
  118. * path on CPUs that don't do context tracking.
  119. *
  120. * But we need to clear the flag on the previous task because it may later
  121. * migrate to some CPU that doesn't do the context tracking. As such the TIF
  122. * flag may not be desired there.
  123. */
  124. void context_tracking_task_switch(struct task_struct *prev,
  125. struct task_struct *next)
  126. {
  127. if (__this_cpu_read(context_tracking.active)) {
  128. clear_tsk_thread_flag(prev, TIF_NOHZ);
  129. set_tsk_thread_flag(next, TIF_NOHZ);
  130. }
  131. }