vtime.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/notifier.h>
  9. #include <linux/kprobes.h>
  10. #include <linux/export.h>
  11. #include <linux/kernel.h>
  12. #include <linux/timex.h>
  13. #include <linux/types.h>
  14. #include <linux/time.h>
  15. #include <linux/cpu.h>
  16. #include <linux/smp.h>
  17. #include <asm/irq_regs.h>
  18. #include <asm/cputime.h>
  19. #include <asm/vtimer.h>
  20. #include <asm/irq.h>
  21. #include "entry.h"
  22. static void virt_timer_expire(void);
  23. DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
  24. static LIST_HEAD(virt_timer_list);
  25. static DEFINE_SPINLOCK(virt_timer_lock);
  26. static atomic64_t virt_timer_current;
  27. static atomic64_t virt_timer_elapsed;
  28. static inline u64 get_vtimer(void)
  29. {
  30. u64 timer;
  31. asm volatile("stpt %0" : "=m" (timer));
  32. return timer;
  33. }
  34. static inline void set_vtimer(u64 expires)
  35. {
  36. u64 timer;
  37. asm volatile(
  38. " stpt %0\n" /* Store current cpu timer value */
  39. " spt %1" /* Set new value imm. afterwards */
  40. : "=m" (timer) : "m" (expires));
  41. S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
  42. S390_lowcore.last_update_timer = expires;
  43. }
  44. static inline int virt_timer_forward(u64 elapsed)
  45. {
  46. BUG_ON(!irqs_disabled());
  47. if (list_empty(&virt_timer_list))
  48. return 0;
  49. elapsed = atomic64_add_return(elapsed, &virt_timer_elapsed);
  50. return elapsed >= atomic64_read(&virt_timer_current);
  51. }
  52. /*
  53. * Update process times based on virtual cpu times stored by entry.S
  54. * to the lowcore fields user_timer, system_timer & steal_clock.
  55. */
  56. static int do_account_vtime(struct task_struct *tsk, int hardirq_offset)
  57. {
  58. struct thread_info *ti = task_thread_info(tsk);
  59. u64 timer, clock, user, system, steal;
  60. timer = S390_lowcore.last_update_timer;
  61. clock = S390_lowcore.last_update_clock;
  62. asm volatile(
  63. " stpt %0\n" /* Store current cpu timer value */
  64. " stck %1" /* Store current tod clock value */
  65. : "=m" (S390_lowcore.last_update_timer),
  66. "=m" (S390_lowcore.last_update_clock));
  67. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  68. S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
  69. user = S390_lowcore.user_timer - ti->user_timer;
  70. S390_lowcore.steal_timer -= user;
  71. ti->user_timer = S390_lowcore.user_timer;
  72. account_user_time(tsk, user, user);
  73. system = S390_lowcore.system_timer - ti->system_timer;
  74. S390_lowcore.steal_timer -= system;
  75. ti->system_timer = S390_lowcore.system_timer;
  76. account_system_time(tsk, hardirq_offset, system, system);
  77. steal = S390_lowcore.steal_timer;
  78. if ((s64) steal > 0) {
  79. S390_lowcore.steal_timer = 0;
  80. account_steal_time(steal);
  81. }
  82. return virt_timer_forward(user + system);
  83. }
  84. void vtime_task_switch(struct task_struct *prev)
  85. {
  86. struct thread_info *ti;
  87. do_account_vtime(prev, 0);
  88. ti = task_thread_info(prev);
  89. ti->user_timer = S390_lowcore.user_timer;
  90. ti->system_timer = S390_lowcore.system_timer;
  91. ti = task_thread_info(current);
  92. S390_lowcore.user_timer = ti->user_timer;
  93. S390_lowcore.system_timer = ti->system_timer;
  94. }
  95. /*
  96. * In s390, accounting pending user time also implies
  97. * accounting system time in order to correctly compute
  98. * the stolen time accounting.
  99. */
  100. void vtime_account_user(struct task_struct *tsk)
  101. {
  102. if (do_account_vtime(tsk, HARDIRQ_OFFSET))
  103. virt_timer_expire();
  104. }
  105. /*
  106. * Update process times based on virtual cpu times stored by entry.S
  107. * to the lowcore fields user_timer, system_timer & steal_clock.
  108. */
  109. void vtime_account_irq_enter(struct task_struct *tsk)
  110. {
  111. struct thread_info *ti = task_thread_info(tsk);
  112. u64 timer, system;
  113. WARN_ON_ONCE(!irqs_disabled());
  114. timer = S390_lowcore.last_update_timer;
  115. S390_lowcore.last_update_timer = get_vtimer();
  116. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  117. system = S390_lowcore.system_timer - ti->system_timer;
  118. S390_lowcore.steal_timer -= system;
  119. ti->system_timer = S390_lowcore.system_timer;
  120. account_system_time(tsk, 0, system, system);
  121. virt_timer_forward(system);
  122. }
  123. EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
  124. void vtime_account_system(struct task_struct *tsk)
  125. __attribute__((alias("vtime_account_irq_enter")));
  126. EXPORT_SYMBOL_GPL(vtime_account_system);
  127. void __kprobes vtime_stop_cpu(void)
  128. {
  129. struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
  130. unsigned long long idle_time;
  131. unsigned long psw_mask;
  132. trace_hardirqs_on();
  133. /* Wait for external, I/O or machine check interrupt. */
  134. psw_mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_DAT |
  135. PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  136. idle->nohz_delay = 0;
  137. /* Call the assembler magic in entry.S */
  138. psw_idle(idle, psw_mask);
  139. /* Account time spent with enabled wait psw loaded as idle time. */
  140. idle->sequence++;
  141. smp_wmb();
  142. idle_time = idle->clock_idle_exit - idle->clock_idle_enter;
  143. idle->clock_idle_enter = idle->clock_idle_exit = 0ULL;
  144. idle->idle_time += idle_time;
  145. idle->idle_count++;
  146. account_idle_time(idle_time);
  147. smp_wmb();
  148. idle->sequence++;
  149. }
  150. cputime64_t s390_get_idle_time(int cpu)
  151. {
  152. struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
  153. unsigned long long now, idle_enter, idle_exit;
  154. unsigned int sequence;
  155. do {
  156. now = get_tod_clock();
  157. sequence = ACCESS_ONCE(idle->sequence);
  158. idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
  159. idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
  160. } while ((sequence & 1) || (idle->sequence != sequence));
  161. return idle_enter ? ((idle_exit ?: now) - idle_enter) : 0;
  162. }
  163. /*
  164. * Sorted add to a list. List is linear searched until first bigger
  165. * element is found.
  166. */
  167. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  168. {
  169. struct vtimer_list *tmp;
  170. list_for_each_entry(tmp, head, entry) {
  171. if (tmp->expires > timer->expires) {
  172. list_add_tail(&timer->entry, &tmp->entry);
  173. return;
  174. }
  175. }
  176. list_add_tail(&timer->entry, head);
  177. }
  178. /*
  179. * Handler for expired virtual CPU timer.
  180. */
  181. static void virt_timer_expire(void)
  182. {
  183. struct vtimer_list *timer, *tmp;
  184. unsigned long elapsed;
  185. LIST_HEAD(cb_list);
  186. /* walk timer list, fire all expired timers */
  187. spin_lock(&virt_timer_lock);
  188. elapsed = atomic64_read(&virt_timer_elapsed);
  189. list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) {
  190. if (timer->expires < elapsed)
  191. /* move expired timer to the callback queue */
  192. list_move_tail(&timer->entry, &cb_list);
  193. else
  194. timer->expires -= elapsed;
  195. }
  196. if (!list_empty(&virt_timer_list)) {
  197. timer = list_first_entry(&virt_timer_list,
  198. struct vtimer_list, entry);
  199. atomic64_set(&virt_timer_current, timer->expires);
  200. }
  201. atomic64_sub(elapsed, &virt_timer_elapsed);
  202. spin_unlock(&virt_timer_lock);
  203. /* Do callbacks and recharge periodic timers */
  204. list_for_each_entry_safe(timer, tmp, &cb_list, entry) {
  205. list_del_init(&timer->entry);
  206. timer->function(timer->data);
  207. if (timer->interval) {
  208. /* Recharge interval timer */
  209. timer->expires = timer->interval +
  210. atomic64_read(&virt_timer_elapsed);
  211. spin_lock(&virt_timer_lock);
  212. list_add_sorted(timer, &virt_timer_list);
  213. spin_unlock(&virt_timer_lock);
  214. }
  215. }
  216. }
  217. void init_virt_timer(struct vtimer_list *timer)
  218. {
  219. timer->function = NULL;
  220. INIT_LIST_HEAD(&timer->entry);
  221. }
  222. EXPORT_SYMBOL(init_virt_timer);
  223. static inline int vtimer_pending(struct vtimer_list *timer)
  224. {
  225. return !list_empty(&timer->entry);
  226. }
  227. static void internal_add_vtimer(struct vtimer_list *timer)
  228. {
  229. if (list_empty(&virt_timer_list)) {
  230. /* First timer, just program it. */
  231. atomic64_set(&virt_timer_current, timer->expires);
  232. atomic64_set(&virt_timer_elapsed, 0);
  233. list_add(&timer->entry, &virt_timer_list);
  234. } else {
  235. /* Update timer against current base. */
  236. timer->expires += atomic64_read(&virt_timer_elapsed);
  237. if (likely((s64) timer->expires <
  238. (s64) atomic64_read(&virt_timer_current)))
  239. /* The new timer expires before the current timer. */
  240. atomic64_set(&virt_timer_current, timer->expires);
  241. /* Insert new timer into the list. */
  242. list_add_sorted(timer, &virt_timer_list);
  243. }
  244. }
  245. static void __add_vtimer(struct vtimer_list *timer, int periodic)
  246. {
  247. unsigned long flags;
  248. timer->interval = periodic ? timer->expires : 0;
  249. spin_lock_irqsave(&virt_timer_lock, flags);
  250. internal_add_vtimer(timer);
  251. spin_unlock_irqrestore(&virt_timer_lock, flags);
  252. }
  253. /*
  254. * add_virt_timer - add an oneshot virtual CPU timer
  255. */
  256. void add_virt_timer(struct vtimer_list *timer)
  257. {
  258. __add_vtimer(timer, 0);
  259. }
  260. EXPORT_SYMBOL(add_virt_timer);
  261. /*
  262. * add_virt_timer_int - add an interval virtual CPU timer
  263. */
  264. void add_virt_timer_periodic(struct vtimer_list *timer)
  265. {
  266. __add_vtimer(timer, 1);
  267. }
  268. EXPORT_SYMBOL(add_virt_timer_periodic);
  269. static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
  270. {
  271. unsigned long flags;
  272. int rc;
  273. BUG_ON(!timer->function);
  274. if (timer->expires == expires && vtimer_pending(timer))
  275. return 1;
  276. spin_lock_irqsave(&virt_timer_lock, flags);
  277. rc = vtimer_pending(timer);
  278. if (rc)
  279. list_del_init(&timer->entry);
  280. timer->interval = periodic ? expires : 0;
  281. timer->expires = expires;
  282. internal_add_vtimer(timer);
  283. spin_unlock_irqrestore(&virt_timer_lock, flags);
  284. return rc;
  285. }
  286. /*
  287. * returns whether it has modified a pending timer (1) or not (0)
  288. */
  289. int mod_virt_timer(struct vtimer_list *timer, u64 expires)
  290. {
  291. return __mod_vtimer(timer, expires, 0);
  292. }
  293. EXPORT_SYMBOL(mod_virt_timer);
  294. /*
  295. * returns whether it has modified a pending timer (1) or not (0)
  296. */
  297. int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
  298. {
  299. return __mod_vtimer(timer, expires, 1);
  300. }
  301. EXPORT_SYMBOL(mod_virt_timer_periodic);
  302. /*
  303. * Delete a virtual timer.
  304. *
  305. * returns whether the deleted timer was pending (1) or not (0)
  306. */
  307. int del_virt_timer(struct vtimer_list *timer)
  308. {
  309. unsigned long flags;
  310. if (!vtimer_pending(timer))
  311. return 0;
  312. spin_lock_irqsave(&virt_timer_lock, flags);
  313. list_del_init(&timer->entry);
  314. spin_unlock_irqrestore(&virt_timer_lock, flags);
  315. return 1;
  316. }
  317. EXPORT_SYMBOL(del_virt_timer);
  318. /*
  319. * Start the virtual CPU timer on the current CPU.
  320. */
  321. void __cpuinit init_cpu_vtimer(void)
  322. {
  323. /* set initial cpu timer */
  324. set_vtimer(VTIMER_MAX_SLICE);
  325. }
  326. static int __cpuinit s390_nohz_notify(struct notifier_block *self,
  327. unsigned long action, void *hcpu)
  328. {
  329. struct s390_idle_data *idle;
  330. long cpu = (long) hcpu;
  331. idle = &per_cpu(s390_idle, cpu);
  332. switch (action & ~CPU_TASKS_FROZEN) {
  333. case CPU_DYING:
  334. idle->nohz_delay = 0;
  335. default:
  336. break;
  337. }
  338. return NOTIFY_OK;
  339. }
  340. void __init vtime_init(void)
  341. {
  342. /* Enable cpu timer interrupts on the boot cpu. */
  343. init_cpu_vtimer();
  344. cpu_notifier(s390_nohz_notify, 0);
  345. }