vtime.c 10 KB

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