vtime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/sched/cputime.h>
  9. #include <linux/export.h>
  10. #include <linux/kernel.h>
  11. #include <linux/timex.h>
  12. #include <linux/types.h>
  13. #include <linux/time.h>
  14. #include <asm/vtimer.h>
  15. #include <asm/vtime.h>
  16. #include <asm/cpu_mf.h>
  17. #include <asm/smp.h>
  18. #include "entry.h"
  19. static void virt_timer_expire(void);
  20. static LIST_HEAD(virt_timer_list);
  21. static DEFINE_SPINLOCK(virt_timer_lock);
  22. static atomic64_t virt_timer_current;
  23. static atomic64_t virt_timer_elapsed;
  24. DEFINE_PER_CPU(u64, mt_cycles[8]);
  25. static DEFINE_PER_CPU(u64, mt_scaling_mult) = { 1 };
  26. static DEFINE_PER_CPU(u64, mt_scaling_div) = { 1 };
  27. static DEFINE_PER_CPU(u64, mt_scaling_jiffies);
  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. static void update_mt_scaling(void)
  53. {
  54. u64 cycles_new[8], *cycles_old;
  55. u64 delta, fac, mult, div;
  56. int i;
  57. stcctm5(smp_cpu_mtid + 1, cycles_new);
  58. cycles_old = this_cpu_ptr(mt_cycles);
  59. fac = 1;
  60. mult = div = 0;
  61. for (i = 0; i <= smp_cpu_mtid; i++) {
  62. delta = cycles_new[i] - cycles_old[i];
  63. div += delta;
  64. mult *= i + 1;
  65. mult += delta * fac;
  66. fac *= i + 1;
  67. }
  68. div *= fac;
  69. if (div > 0) {
  70. /* Update scaling factor */
  71. __this_cpu_write(mt_scaling_mult, mult);
  72. __this_cpu_write(mt_scaling_div, div);
  73. memcpy(cycles_old, cycles_new,
  74. sizeof(u64) * (smp_cpu_mtid + 1));
  75. }
  76. __this_cpu_write(mt_scaling_jiffies, jiffies_64);
  77. }
  78. static inline u64 update_tsk_timer(unsigned long *tsk_vtime, u64 new)
  79. {
  80. u64 delta;
  81. delta = new - *tsk_vtime;
  82. *tsk_vtime = new;
  83. return delta;
  84. }
  85. static inline u64 scale_vtime(u64 vtime)
  86. {
  87. u64 mult = __this_cpu_read(mt_scaling_mult);
  88. u64 div = __this_cpu_read(mt_scaling_div);
  89. if (smp_cpu_mtid)
  90. return vtime * mult / div;
  91. return vtime;
  92. }
  93. static void account_system_index_scaled(struct task_struct *p,
  94. u64 cputime, u64 scaled,
  95. enum cpu_usage_stat index)
  96. {
  97. p->stimescaled += cputime_to_nsecs(scaled);
  98. account_system_index_time(p, cputime_to_nsecs(cputime), index);
  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. static int do_account_vtime(struct task_struct *tsk)
  105. {
  106. u64 timer, clock, user, guest, system, hardirq, softirq, steal;
  107. timer = S390_lowcore.last_update_timer;
  108. clock = S390_lowcore.last_update_clock;
  109. asm volatile(
  110. " stpt %0\n" /* Store current cpu timer value */
  111. #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
  112. " stckf %1" /* Store current tod clock value */
  113. #else
  114. " stck %1" /* Store current tod clock value */
  115. #endif
  116. : "=m" (S390_lowcore.last_update_timer),
  117. "=m" (S390_lowcore.last_update_clock));
  118. clock = S390_lowcore.last_update_clock - clock;
  119. timer -= S390_lowcore.last_update_timer;
  120. if (hardirq_count())
  121. S390_lowcore.hardirq_timer += timer;
  122. else
  123. S390_lowcore.system_timer += timer;
  124. /* Update MT utilization calculation */
  125. if (smp_cpu_mtid &&
  126. time_after64(jiffies_64, this_cpu_read(mt_scaling_jiffies)))
  127. update_mt_scaling();
  128. /* Calculate cputime delta */
  129. user = update_tsk_timer(&tsk->thread.user_timer,
  130. READ_ONCE(S390_lowcore.user_timer));
  131. guest = update_tsk_timer(&tsk->thread.guest_timer,
  132. READ_ONCE(S390_lowcore.guest_timer));
  133. system = update_tsk_timer(&tsk->thread.system_timer,
  134. READ_ONCE(S390_lowcore.system_timer));
  135. hardirq = update_tsk_timer(&tsk->thread.hardirq_timer,
  136. READ_ONCE(S390_lowcore.hardirq_timer));
  137. softirq = update_tsk_timer(&tsk->thread.softirq_timer,
  138. READ_ONCE(S390_lowcore.softirq_timer));
  139. S390_lowcore.steal_timer +=
  140. clock - user - guest - system - hardirq - softirq;
  141. /* Push account value */
  142. if (user) {
  143. account_user_time(tsk, cputime_to_nsecs(user));
  144. tsk->utimescaled += cputime_to_nsecs(scale_vtime(user));
  145. }
  146. if (guest) {
  147. account_guest_time(tsk, cputime_to_nsecs(guest));
  148. tsk->utimescaled += cputime_to_nsecs(scale_vtime(guest));
  149. }
  150. if (system)
  151. account_system_index_scaled(tsk, system, scale_vtime(system),
  152. CPUTIME_SYSTEM);
  153. if (hardirq)
  154. account_system_index_scaled(tsk, hardirq, scale_vtime(hardirq),
  155. CPUTIME_IRQ);
  156. if (softirq)
  157. account_system_index_scaled(tsk, softirq, scale_vtime(softirq),
  158. CPUTIME_SOFTIRQ);
  159. steal = S390_lowcore.steal_timer;
  160. if ((s64) steal > 0) {
  161. S390_lowcore.steal_timer = 0;
  162. account_steal_time(cputime_to_nsecs(steal));
  163. }
  164. return virt_timer_forward(user + guest + system + hardirq + softirq);
  165. }
  166. void vtime_task_switch(struct task_struct *prev)
  167. {
  168. do_account_vtime(prev);
  169. prev->thread.user_timer = S390_lowcore.user_timer;
  170. prev->thread.guest_timer = S390_lowcore.guest_timer;
  171. prev->thread.system_timer = S390_lowcore.system_timer;
  172. prev->thread.hardirq_timer = S390_lowcore.hardirq_timer;
  173. prev->thread.softirq_timer = S390_lowcore.softirq_timer;
  174. S390_lowcore.user_timer = current->thread.user_timer;
  175. S390_lowcore.guest_timer = current->thread.guest_timer;
  176. S390_lowcore.system_timer = current->thread.system_timer;
  177. S390_lowcore.hardirq_timer = current->thread.hardirq_timer;
  178. S390_lowcore.softirq_timer = current->thread.softirq_timer;
  179. }
  180. /*
  181. * In s390, accounting pending user time also implies
  182. * accounting system time in order to correctly compute
  183. * the stolen time accounting.
  184. */
  185. void vtime_flush(struct task_struct *tsk)
  186. {
  187. if (do_account_vtime(tsk))
  188. virt_timer_expire();
  189. }
  190. /*
  191. * Update process times based on virtual cpu times stored by entry.S
  192. * to the lowcore fields user_timer, system_timer & steal_clock.
  193. */
  194. void vtime_account_irq_enter(struct task_struct *tsk)
  195. {
  196. u64 timer;
  197. timer = S390_lowcore.last_update_timer;
  198. S390_lowcore.last_update_timer = get_vtimer();
  199. timer -= S390_lowcore.last_update_timer;
  200. if ((tsk->flags & PF_VCPU) && (irq_count() == 0))
  201. S390_lowcore.guest_timer += timer;
  202. else if (hardirq_count())
  203. S390_lowcore.hardirq_timer += timer;
  204. else if (in_serving_softirq())
  205. S390_lowcore.softirq_timer += timer;
  206. else
  207. S390_lowcore.system_timer += timer;
  208. virt_timer_forward(timer);
  209. }
  210. EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
  211. void vtime_account_system(struct task_struct *tsk)
  212. __attribute__((alias("vtime_account_irq_enter")));
  213. EXPORT_SYMBOL_GPL(vtime_account_system);
  214. /*
  215. * Sorted add to a list. List is linear searched until first bigger
  216. * element is found.
  217. */
  218. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  219. {
  220. struct vtimer_list *tmp;
  221. list_for_each_entry(tmp, head, entry) {
  222. if (tmp->expires > timer->expires) {
  223. list_add_tail(&timer->entry, &tmp->entry);
  224. return;
  225. }
  226. }
  227. list_add_tail(&timer->entry, head);
  228. }
  229. /*
  230. * Handler for expired virtual CPU timer.
  231. */
  232. static void virt_timer_expire(void)
  233. {
  234. struct vtimer_list *timer, *tmp;
  235. unsigned long elapsed;
  236. LIST_HEAD(cb_list);
  237. /* walk timer list, fire all expired timers */
  238. spin_lock(&virt_timer_lock);
  239. elapsed = atomic64_read(&virt_timer_elapsed);
  240. list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) {
  241. if (timer->expires < elapsed)
  242. /* move expired timer to the callback queue */
  243. list_move_tail(&timer->entry, &cb_list);
  244. else
  245. timer->expires -= elapsed;
  246. }
  247. if (!list_empty(&virt_timer_list)) {
  248. timer = list_first_entry(&virt_timer_list,
  249. struct vtimer_list, entry);
  250. atomic64_set(&virt_timer_current, timer->expires);
  251. }
  252. atomic64_sub(elapsed, &virt_timer_elapsed);
  253. spin_unlock(&virt_timer_lock);
  254. /* Do callbacks and recharge periodic timers */
  255. list_for_each_entry_safe(timer, tmp, &cb_list, entry) {
  256. list_del_init(&timer->entry);
  257. timer->function(timer->data);
  258. if (timer->interval) {
  259. /* Recharge interval timer */
  260. timer->expires = timer->interval +
  261. atomic64_read(&virt_timer_elapsed);
  262. spin_lock(&virt_timer_lock);
  263. list_add_sorted(timer, &virt_timer_list);
  264. spin_unlock(&virt_timer_lock);
  265. }
  266. }
  267. }
  268. void init_virt_timer(struct vtimer_list *timer)
  269. {
  270. timer->function = NULL;
  271. INIT_LIST_HEAD(&timer->entry);
  272. }
  273. EXPORT_SYMBOL(init_virt_timer);
  274. static inline int vtimer_pending(struct vtimer_list *timer)
  275. {
  276. return !list_empty(&timer->entry);
  277. }
  278. static void internal_add_vtimer(struct vtimer_list *timer)
  279. {
  280. if (list_empty(&virt_timer_list)) {
  281. /* First timer, just program it. */
  282. atomic64_set(&virt_timer_current, timer->expires);
  283. atomic64_set(&virt_timer_elapsed, 0);
  284. list_add(&timer->entry, &virt_timer_list);
  285. } else {
  286. /* Update timer against current base. */
  287. timer->expires += atomic64_read(&virt_timer_elapsed);
  288. if (likely((s64) timer->expires <
  289. (s64) atomic64_read(&virt_timer_current)))
  290. /* The new timer expires before the current timer. */
  291. atomic64_set(&virt_timer_current, timer->expires);
  292. /* Insert new timer into the list. */
  293. list_add_sorted(timer, &virt_timer_list);
  294. }
  295. }
  296. static void __add_vtimer(struct vtimer_list *timer, int periodic)
  297. {
  298. unsigned long flags;
  299. timer->interval = periodic ? timer->expires : 0;
  300. spin_lock_irqsave(&virt_timer_lock, flags);
  301. internal_add_vtimer(timer);
  302. spin_unlock_irqrestore(&virt_timer_lock, flags);
  303. }
  304. /*
  305. * add_virt_timer - add a oneshot virtual CPU timer
  306. */
  307. void add_virt_timer(struct vtimer_list *timer)
  308. {
  309. __add_vtimer(timer, 0);
  310. }
  311. EXPORT_SYMBOL(add_virt_timer);
  312. /*
  313. * add_virt_timer_int - add an interval virtual CPU timer
  314. */
  315. void add_virt_timer_periodic(struct vtimer_list *timer)
  316. {
  317. __add_vtimer(timer, 1);
  318. }
  319. EXPORT_SYMBOL(add_virt_timer_periodic);
  320. static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
  321. {
  322. unsigned long flags;
  323. int rc;
  324. BUG_ON(!timer->function);
  325. if (timer->expires == expires && vtimer_pending(timer))
  326. return 1;
  327. spin_lock_irqsave(&virt_timer_lock, flags);
  328. rc = vtimer_pending(timer);
  329. if (rc)
  330. list_del_init(&timer->entry);
  331. timer->interval = periodic ? expires : 0;
  332. timer->expires = expires;
  333. internal_add_vtimer(timer);
  334. spin_unlock_irqrestore(&virt_timer_lock, flags);
  335. return rc;
  336. }
  337. /*
  338. * returns whether it has modified a pending timer (1) or not (0)
  339. */
  340. int mod_virt_timer(struct vtimer_list *timer, u64 expires)
  341. {
  342. return __mod_vtimer(timer, expires, 0);
  343. }
  344. EXPORT_SYMBOL(mod_virt_timer);
  345. /*
  346. * returns whether it has modified a pending timer (1) or not (0)
  347. */
  348. int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
  349. {
  350. return __mod_vtimer(timer, expires, 1);
  351. }
  352. EXPORT_SYMBOL(mod_virt_timer_periodic);
  353. /*
  354. * Delete a virtual timer.
  355. *
  356. * returns whether the deleted timer was pending (1) or not (0)
  357. */
  358. int del_virt_timer(struct vtimer_list *timer)
  359. {
  360. unsigned long flags;
  361. if (!vtimer_pending(timer))
  362. return 0;
  363. spin_lock_irqsave(&virt_timer_lock, flags);
  364. list_del_init(&timer->entry);
  365. spin_unlock_irqrestore(&virt_timer_lock, flags);
  366. return 1;
  367. }
  368. EXPORT_SYMBOL(del_virt_timer);
  369. /*
  370. * Start the virtual CPU timer on the current CPU.
  371. */
  372. void vtime_init(void)
  373. {
  374. /* set initial cpu timer */
  375. set_vtimer(VTIMER_MAX_SLICE);
  376. /* Setup initial MT scaling values */
  377. if (smp_cpu_mtid) {
  378. __this_cpu_write(mt_scaling_jiffies, jiffies);
  379. __this_cpu_write(mt_scaling_mult, 1);
  380. __this_cpu_write(mt_scaling_div, 1);
  381. stcctm5(smp_cpu_mtid + 1, this_cpu_ptr(mt_cycles));
  382. }
  383. }