vtime.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Virtual cpu timer based timer functions.
  3. *
  4. * Copyright IBM Corp. 2004, 2012
  5. * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
  6. */
  7. #include <linux/kernel_stat.h>
  8. #include <linux/export.h>
  9. #include <linux/kernel.h>
  10. #include <linux/timex.h>
  11. #include <linux/types.h>
  12. #include <linux/time.h>
  13. #include <asm/cputime.h>
  14. #include <asm/vtimer.h>
  15. #include <asm/vtime.h>
  16. static void virt_timer_expire(void);
  17. static LIST_HEAD(virt_timer_list);
  18. static DEFINE_SPINLOCK(virt_timer_lock);
  19. static atomic64_t virt_timer_current;
  20. static atomic64_t virt_timer_elapsed;
  21. static inline u64 get_vtimer(void)
  22. {
  23. u64 timer;
  24. asm volatile("stpt %0" : "=m" (timer));
  25. return timer;
  26. }
  27. static inline void set_vtimer(u64 expires)
  28. {
  29. u64 timer;
  30. asm volatile(
  31. " stpt %0\n" /* Store current cpu timer value */
  32. " spt %1" /* Set new value imm. afterwards */
  33. : "=m" (timer) : "m" (expires));
  34. S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
  35. S390_lowcore.last_update_timer = expires;
  36. }
  37. static inline int virt_timer_forward(u64 elapsed)
  38. {
  39. BUG_ON(!irqs_disabled());
  40. if (list_empty(&virt_timer_list))
  41. return 0;
  42. elapsed = atomic64_add_return(elapsed, &virt_timer_elapsed);
  43. return elapsed >= atomic64_read(&virt_timer_current);
  44. }
  45. /*
  46. * Update process times based on virtual cpu times stored by entry.S
  47. * to the lowcore fields user_timer, system_timer & steal_clock.
  48. */
  49. static int do_account_vtime(struct task_struct *tsk, int hardirq_offset)
  50. {
  51. struct thread_info *ti = task_thread_info(tsk);
  52. u64 timer, clock, user, system, steal;
  53. timer = S390_lowcore.last_update_timer;
  54. clock = S390_lowcore.last_update_clock;
  55. asm volatile(
  56. " stpt %0\n" /* Store current cpu timer value */
  57. " stck %1" /* Store current tod clock value */
  58. : "=m" (S390_lowcore.last_update_timer),
  59. "=m" (S390_lowcore.last_update_clock));
  60. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  61. S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
  62. user = S390_lowcore.user_timer - ti->user_timer;
  63. S390_lowcore.steal_timer -= user;
  64. ti->user_timer = S390_lowcore.user_timer;
  65. account_user_time(tsk, user, user);
  66. system = S390_lowcore.system_timer - ti->system_timer;
  67. S390_lowcore.steal_timer -= system;
  68. ti->system_timer = S390_lowcore.system_timer;
  69. account_system_time(tsk, hardirq_offset, system, system);
  70. steal = S390_lowcore.steal_timer;
  71. if ((s64) steal > 0) {
  72. S390_lowcore.steal_timer = 0;
  73. account_steal_time(steal);
  74. }
  75. return virt_timer_forward(user + system);
  76. }
  77. void vtime_task_switch(struct task_struct *prev)
  78. {
  79. struct thread_info *ti;
  80. do_account_vtime(prev, 0);
  81. ti = task_thread_info(prev);
  82. ti->user_timer = S390_lowcore.user_timer;
  83. ti->system_timer = S390_lowcore.system_timer;
  84. ti = task_thread_info(current);
  85. S390_lowcore.user_timer = ti->user_timer;
  86. S390_lowcore.system_timer = ti->system_timer;
  87. }
  88. /*
  89. * In s390, accounting pending user time also implies
  90. * accounting system time in order to correctly compute
  91. * the stolen time accounting.
  92. */
  93. void vtime_account_user(struct task_struct *tsk)
  94. {
  95. if (do_account_vtime(tsk, HARDIRQ_OFFSET))
  96. virt_timer_expire();
  97. }
  98. /*
  99. * Update process times based on virtual cpu times stored by entry.S
  100. * to the lowcore fields user_timer, system_timer & steal_clock.
  101. */
  102. void vtime_account_irq_enter(struct task_struct *tsk)
  103. {
  104. struct thread_info *ti = task_thread_info(tsk);
  105. u64 timer, system;
  106. WARN_ON_ONCE(!irqs_disabled());
  107. timer = S390_lowcore.last_update_timer;
  108. S390_lowcore.last_update_timer = get_vtimer();
  109. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  110. system = S390_lowcore.system_timer - ti->system_timer;
  111. S390_lowcore.steal_timer -= system;
  112. ti->system_timer = S390_lowcore.system_timer;
  113. account_system_time(tsk, 0, system, system);
  114. virt_timer_forward(system);
  115. }
  116. EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
  117. void vtime_account_system(struct task_struct *tsk)
  118. __attribute__((alias("vtime_account_irq_enter")));
  119. EXPORT_SYMBOL_GPL(vtime_account_system);
  120. /*
  121. * Sorted add to a list. List is linear searched until first bigger
  122. * element is found.
  123. */
  124. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  125. {
  126. struct vtimer_list *tmp;
  127. list_for_each_entry(tmp, head, entry) {
  128. if (tmp->expires > timer->expires) {
  129. list_add_tail(&timer->entry, &tmp->entry);
  130. return;
  131. }
  132. }
  133. list_add_tail(&timer->entry, head);
  134. }
  135. /*
  136. * Handler for expired virtual CPU timer.
  137. */
  138. static void virt_timer_expire(void)
  139. {
  140. struct vtimer_list *timer, *tmp;
  141. unsigned long elapsed;
  142. LIST_HEAD(cb_list);
  143. /* walk timer list, fire all expired timers */
  144. spin_lock(&virt_timer_lock);
  145. elapsed = atomic64_read(&virt_timer_elapsed);
  146. list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) {
  147. if (timer->expires < elapsed)
  148. /* move expired timer to the callback queue */
  149. list_move_tail(&timer->entry, &cb_list);
  150. else
  151. timer->expires -= elapsed;
  152. }
  153. if (!list_empty(&virt_timer_list)) {
  154. timer = list_first_entry(&virt_timer_list,
  155. struct vtimer_list, entry);
  156. atomic64_set(&virt_timer_current, timer->expires);
  157. }
  158. atomic64_sub(elapsed, &virt_timer_elapsed);
  159. spin_unlock(&virt_timer_lock);
  160. /* Do callbacks and recharge periodic timers */
  161. list_for_each_entry_safe(timer, tmp, &cb_list, entry) {
  162. list_del_init(&timer->entry);
  163. timer->function(timer->data);
  164. if (timer->interval) {
  165. /* Recharge interval timer */
  166. timer->expires = timer->interval +
  167. atomic64_read(&virt_timer_elapsed);
  168. spin_lock(&virt_timer_lock);
  169. list_add_sorted(timer, &virt_timer_list);
  170. spin_unlock(&virt_timer_lock);
  171. }
  172. }
  173. }
  174. void init_virt_timer(struct vtimer_list *timer)
  175. {
  176. timer->function = NULL;
  177. INIT_LIST_HEAD(&timer->entry);
  178. }
  179. EXPORT_SYMBOL(init_virt_timer);
  180. static inline int vtimer_pending(struct vtimer_list *timer)
  181. {
  182. return !list_empty(&timer->entry);
  183. }
  184. static void internal_add_vtimer(struct vtimer_list *timer)
  185. {
  186. if (list_empty(&virt_timer_list)) {
  187. /* First timer, just program it. */
  188. atomic64_set(&virt_timer_current, timer->expires);
  189. atomic64_set(&virt_timer_elapsed, 0);
  190. list_add(&timer->entry, &virt_timer_list);
  191. } else {
  192. /* Update timer against current base. */
  193. timer->expires += atomic64_read(&virt_timer_elapsed);
  194. if (likely((s64) timer->expires <
  195. (s64) atomic64_read(&virt_timer_current)))
  196. /* The new timer expires before the current timer. */
  197. atomic64_set(&virt_timer_current, timer->expires);
  198. /* Insert new timer into the list. */
  199. list_add_sorted(timer, &virt_timer_list);
  200. }
  201. }
  202. static void __add_vtimer(struct vtimer_list *timer, int periodic)
  203. {
  204. unsigned long flags;
  205. timer->interval = periodic ? timer->expires : 0;
  206. spin_lock_irqsave(&virt_timer_lock, flags);
  207. internal_add_vtimer(timer);
  208. spin_unlock_irqrestore(&virt_timer_lock, flags);
  209. }
  210. /*
  211. * add_virt_timer - add an oneshot virtual CPU timer
  212. */
  213. void add_virt_timer(struct vtimer_list *timer)
  214. {
  215. __add_vtimer(timer, 0);
  216. }
  217. EXPORT_SYMBOL(add_virt_timer);
  218. /*
  219. * add_virt_timer_int - add an interval virtual CPU timer
  220. */
  221. void add_virt_timer_periodic(struct vtimer_list *timer)
  222. {
  223. __add_vtimer(timer, 1);
  224. }
  225. EXPORT_SYMBOL(add_virt_timer_periodic);
  226. static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
  227. {
  228. unsigned long flags;
  229. int rc;
  230. BUG_ON(!timer->function);
  231. if (timer->expires == expires && vtimer_pending(timer))
  232. return 1;
  233. spin_lock_irqsave(&virt_timer_lock, flags);
  234. rc = vtimer_pending(timer);
  235. if (rc)
  236. list_del_init(&timer->entry);
  237. timer->interval = periodic ? expires : 0;
  238. timer->expires = expires;
  239. internal_add_vtimer(timer);
  240. spin_unlock_irqrestore(&virt_timer_lock, flags);
  241. return rc;
  242. }
  243. /*
  244. * returns whether it has modified a pending timer (1) or not (0)
  245. */
  246. int mod_virt_timer(struct vtimer_list *timer, u64 expires)
  247. {
  248. return __mod_vtimer(timer, expires, 0);
  249. }
  250. EXPORT_SYMBOL(mod_virt_timer);
  251. /*
  252. * returns whether it has modified a pending timer (1) or not (0)
  253. */
  254. int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
  255. {
  256. return __mod_vtimer(timer, expires, 1);
  257. }
  258. EXPORT_SYMBOL(mod_virt_timer_periodic);
  259. /*
  260. * Delete a virtual timer.
  261. *
  262. * returns whether the deleted timer was pending (1) or not (0)
  263. */
  264. int del_virt_timer(struct vtimer_list *timer)
  265. {
  266. unsigned long flags;
  267. if (!vtimer_pending(timer))
  268. return 0;
  269. spin_lock_irqsave(&virt_timer_lock, flags);
  270. list_del_init(&timer->entry);
  271. spin_unlock_irqrestore(&virt_timer_lock, flags);
  272. return 1;
  273. }
  274. EXPORT_SYMBOL(del_virt_timer);
  275. /*
  276. * Start the virtual CPU timer on the current CPU.
  277. */
  278. void vtime_init(void)
  279. {
  280. /* set initial cpu timer */
  281. set_vtimer(VTIMER_MAX_SLICE);
  282. }