vtime.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. /* Don't trace preempt off for idle. */
  134. stop_critical_timings();
  135. /* Wait for external, I/O or machine check interrupt. */
  136. psw_mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_DAT |
  137. PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  138. idle->nohz_delay = 0;
  139. /* Call the assembler magic in entry.S */
  140. psw_idle(idle, psw_mask);
  141. /* Reenable preemption tracer. */
  142. start_critical_timings();
  143. /* Account time spent with enabled wait psw loaded as idle time. */
  144. idle->sequence++;
  145. smp_wmb();
  146. idle_time = idle->clock_idle_exit - idle->clock_idle_enter;
  147. idle->clock_idle_enter = idle->clock_idle_exit = 0ULL;
  148. idle->idle_time += idle_time;
  149. idle->idle_count++;
  150. account_idle_time(idle_time);
  151. smp_wmb();
  152. idle->sequence++;
  153. }
  154. cputime64_t s390_get_idle_time(int cpu)
  155. {
  156. struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
  157. unsigned long long now, idle_enter, idle_exit;
  158. unsigned int sequence;
  159. do {
  160. now = get_tod_clock();
  161. sequence = ACCESS_ONCE(idle->sequence);
  162. idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
  163. idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
  164. } while ((sequence & 1) || (idle->sequence != sequence));
  165. return idle_enter ? ((idle_exit ?: now) - idle_enter) : 0;
  166. }
  167. /*
  168. * Sorted add to a list. List is linear searched until first bigger
  169. * element is found.
  170. */
  171. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  172. {
  173. struct vtimer_list *tmp;
  174. list_for_each_entry(tmp, head, entry) {
  175. if (tmp->expires > timer->expires) {
  176. list_add_tail(&timer->entry, &tmp->entry);
  177. return;
  178. }
  179. }
  180. list_add_tail(&timer->entry, head);
  181. }
  182. /*
  183. * Handler for expired virtual CPU timer.
  184. */
  185. static void virt_timer_expire(void)
  186. {
  187. struct vtimer_list *timer, *tmp;
  188. unsigned long elapsed;
  189. LIST_HEAD(cb_list);
  190. /* walk timer list, fire all expired timers */
  191. spin_lock(&virt_timer_lock);
  192. elapsed = atomic64_read(&virt_timer_elapsed);
  193. list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) {
  194. if (timer->expires < elapsed)
  195. /* move expired timer to the callback queue */
  196. list_move_tail(&timer->entry, &cb_list);
  197. else
  198. timer->expires -= elapsed;
  199. }
  200. if (!list_empty(&virt_timer_list)) {
  201. timer = list_first_entry(&virt_timer_list,
  202. struct vtimer_list, entry);
  203. atomic64_set(&virt_timer_current, timer->expires);
  204. }
  205. atomic64_sub(elapsed, &virt_timer_elapsed);
  206. spin_unlock(&virt_timer_lock);
  207. /* Do callbacks and recharge periodic timers */
  208. list_for_each_entry_safe(timer, tmp, &cb_list, entry) {
  209. list_del_init(&timer->entry);
  210. timer->function(timer->data);
  211. if (timer->interval) {
  212. /* Recharge interval timer */
  213. timer->expires = timer->interval +
  214. atomic64_read(&virt_timer_elapsed);
  215. spin_lock(&virt_timer_lock);
  216. list_add_sorted(timer, &virt_timer_list);
  217. spin_unlock(&virt_timer_lock);
  218. }
  219. }
  220. }
  221. void init_virt_timer(struct vtimer_list *timer)
  222. {
  223. timer->function = NULL;
  224. INIT_LIST_HEAD(&timer->entry);
  225. }
  226. EXPORT_SYMBOL(init_virt_timer);
  227. static inline int vtimer_pending(struct vtimer_list *timer)
  228. {
  229. return !list_empty(&timer->entry);
  230. }
  231. static void internal_add_vtimer(struct vtimer_list *timer)
  232. {
  233. if (list_empty(&virt_timer_list)) {
  234. /* First timer, just program it. */
  235. atomic64_set(&virt_timer_current, timer->expires);
  236. atomic64_set(&virt_timer_elapsed, 0);
  237. list_add(&timer->entry, &virt_timer_list);
  238. } else {
  239. /* Update timer against current base. */
  240. timer->expires += atomic64_read(&virt_timer_elapsed);
  241. if (likely((s64) timer->expires <
  242. (s64) atomic64_read(&virt_timer_current)))
  243. /* The new timer expires before the current timer. */
  244. atomic64_set(&virt_timer_current, timer->expires);
  245. /* Insert new timer into the list. */
  246. list_add_sorted(timer, &virt_timer_list);
  247. }
  248. }
  249. static void __add_vtimer(struct vtimer_list *timer, int periodic)
  250. {
  251. unsigned long flags;
  252. timer->interval = periodic ? timer->expires : 0;
  253. spin_lock_irqsave(&virt_timer_lock, flags);
  254. internal_add_vtimer(timer);
  255. spin_unlock_irqrestore(&virt_timer_lock, flags);
  256. }
  257. /*
  258. * add_virt_timer - add an oneshot virtual CPU timer
  259. */
  260. void add_virt_timer(struct vtimer_list *timer)
  261. {
  262. __add_vtimer(timer, 0);
  263. }
  264. EXPORT_SYMBOL(add_virt_timer);
  265. /*
  266. * add_virt_timer_int - add an interval virtual CPU timer
  267. */
  268. void add_virt_timer_periodic(struct vtimer_list *timer)
  269. {
  270. __add_vtimer(timer, 1);
  271. }
  272. EXPORT_SYMBOL(add_virt_timer_periodic);
  273. static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
  274. {
  275. unsigned long flags;
  276. int rc;
  277. BUG_ON(!timer->function);
  278. if (timer->expires == expires && vtimer_pending(timer))
  279. return 1;
  280. spin_lock_irqsave(&virt_timer_lock, flags);
  281. rc = vtimer_pending(timer);
  282. if (rc)
  283. list_del_init(&timer->entry);
  284. timer->interval = periodic ? expires : 0;
  285. timer->expires = expires;
  286. internal_add_vtimer(timer);
  287. spin_unlock_irqrestore(&virt_timer_lock, flags);
  288. return rc;
  289. }
  290. /*
  291. * returns whether it has modified a pending timer (1) or not (0)
  292. */
  293. int mod_virt_timer(struct vtimer_list *timer, u64 expires)
  294. {
  295. return __mod_vtimer(timer, expires, 0);
  296. }
  297. EXPORT_SYMBOL(mod_virt_timer);
  298. /*
  299. * returns whether it has modified a pending timer (1) or not (0)
  300. */
  301. int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
  302. {
  303. return __mod_vtimer(timer, expires, 1);
  304. }
  305. EXPORT_SYMBOL(mod_virt_timer_periodic);
  306. /*
  307. * Delete a virtual timer.
  308. *
  309. * returns whether the deleted timer was pending (1) or not (0)
  310. */
  311. int del_virt_timer(struct vtimer_list *timer)
  312. {
  313. unsigned long flags;
  314. if (!vtimer_pending(timer))
  315. return 0;
  316. spin_lock_irqsave(&virt_timer_lock, flags);
  317. list_del_init(&timer->entry);
  318. spin_unlock_irqrestore(&virt_timer_lock, flags);
  319. return 1;
  320. }
  321. EXPORT_SYMBOL(del_virt_timer);
  322. /*
  323. * Start the virtual CPU timer on the current CPU.
  324. */
  325. void __cpuinit init_cpu_vtimer(void)
  326. {
  327. /* set initial cpu timer */
  328. set_vtimer(VTIMER_MAX_SLICE);
  329. }
  330. static int __cpuinit s390_nohz_notify(struct notifier_block *self,
  331. unsigned long action, void *hcpu)
  332. {
  333. struct s390_idle_data *idle;
  334. long cpu = (long) hcpu;
  335. idle = &per_cpu(s390_idle, cpu);
  336. switch (action & ~CPU_TASKS_FROZEN) {
  337. case CPU_DYING:
  338. idle->nohz_delay = 0;
  339. default:
  340. break;
  341. }
  342. return NOTIFY_OK;
  343. }
  344. void __init vtime_init(void)
  345. {
  346. /* Enable cpu timer interrupts on the boot cpu. */
  347. init_cpu_vtimer();
  348. cpu_notifier(s390_nohz_notify, 0);
  349. }