vtime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Virtual cpu timer based timer functions.
  4. *
  5. * Copyright IBM Corp. 2004, 2012
  6. * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
  7. */
  8. #include <linux/kernel_stat.h>
  9. #include <linux/sched/cputime.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 <asm/vtimer.h>
  16. #include <asm/vtime.h>
  17. #include <asm/cpu_mf.h>
  18. #include <asm/smp.h>
  19. #include "entry.h"
  20. static void virt_timer_expire(void);
  21. static LIST_HEAD(virt_timer_list);
  22. static DEFINE_SPINLOCK(virt_timer_lock);
  23. static atomic64_t virt_timer_current;
  24. static atomic64_t virt_timer_elapsed;
  25. DEFINE_PER_CPU(u64, mt_cycles[8]);
  26. static DEFINE_PER_CPU(u64, mt_scaling_mult) = { 1 };
  27. static DEFINE_PER_CPU(u64, mt_scaling_div) = { 1 };
  28. static DEFINE_PER_CPU(u64, mt_scaling_jiffies);
  29. static inline u64 get_vtimer(void)
  30. {
  31. u64 timer;
  32. asm volatile("stpt %0" : "=m" (timer));
  33. return timer;
  34. }
  35. static inline void set_vtimer(u64 expires)
  36. {
  37. u64 timer;
  38. asm volatile(
  39. " stpt %0\n" /* Store current cpu timer value */
  40. " spt %1" /* Set new value imm. afterwards */
  41. : "=m" (timer) : "m" (expires));
  42. S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
  43. S390_lowcore.last_update_timer = expires;
  44. }
  45. static inline int virt_timer_forward(u64 elapsed)
  46. {
  47. BUG_ON(!irqs_disabled());
  48. if (list_empty(&virt_timer_list))
  49. return 0;
  50. elapsed = atomic64_add_return(elapsed, &virt_timer_elapsed);
  51. return elapsed >= atomic64_read(&virt_timer_current);
  52. }
  53. static void update_mt_scaling(void)
  54. {
  55. u64 cycles_new[8], *cycles_old;
  56. u64 delta, fac, mult, div;
  57. int i;
  58. stcctm5(smp_cpu_mtid + 1, cycles_new);
  59. cycles_old = this_cpu_ptr(mt_cycles);
  60. fac = 1;
  61. mult = div = 0;
  62. for (i = 0; i <= smp_cpu_mtid; i++) {
  63. delta = cycles_new[i] - cycles_old[i];
  64. div += delta;
  65. mult *= i + 1;
  66. mult += delta * fac;
  67. fac *= i + 1;
  68. }
  69. div *= fac;
  70. if (div > 0) {
  71. /* Update scaling factor */
  72. __this_cpu_write(mt_scaling_mult, mult);
  73. __this_cpu_write(mt_scaling_div, div);
  74. memcpy(cycles_old, cycles_new,
  75. sizeof(u64) * (smp_cpu_mtid + 1));
  76. }
  77. __this_cpu_write(mt_scaling_jiffies, jiffies_64);
  78. }
  79. static inline u64 update_tsk_timer(unsigned long *tsk_vtime, u64 new)
  80. {
  81. u64 delta;
  82. delta = new - *tsk_vtime;
  83. *tsk_vtime = new;
  84. return delta;
  85. }
  86. static inline u64 scale_vtime(u64 vtime)
  87. {
  88. u64 mult = __this_cpu_read(mt_scaling_mult);
  89. u64 div = __this_cpu_read(mt_scaling_div);
  90. if (smp_cpu_mtid)
  91. return vtime * mult / div;
  92. return vtime;
  93. }
  94. static void account_system_index_scaled(struct task_struct *p, u64 cputime,
  95. enum cpu_usage_stat index)
  96. {
  97. p->stimescaled += cputime_to_nsecs(scale_vtime(cputime));
  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, CPUTIME_SYSTEM);
  152. if (hardirq)
  153. account_system_index_scaled(tsk, hardirq, CPUTIME_IRQ);
  154. if (softirq)
  155. account_system_index_scaled(tsk, softirq, CPUTIME_SOFTIRQ);
  156. steal = S390_lowcore.steal_timer;
  157. if ((s64) steal > 0) {
  158. S390_lowcore.steal_timer = 0;
  159. account_steal_time(cputime_to_nsecs(steal));
  160. }
  161. return virt_timer_forward(user + guest + system + hardirq + softirq);
  162. }
  163. void vtime_task_switch(struct task_struct *prev)
  164. {
  165. do_account_vtime(prev);
  166. prev->thread.user_timer = S390_lowcore.user_timer;
  167. prev->thread.guest_timer = S390_lowcore.guest_timer;
  168. prev->thread.system_timer = S390_lowcore.system_timer;
  169. prev->thread.hardirq_timer = S390_lowcore.hardirq_timer;
  170. prev->thread.softirq_timer = S390_lowcore.softirq_timer;
  171. S390_lowcore.user_timer = current->thread.user_timer;
  172. S390_lowcore.guest_timer = current->thread.guest_timer;
  173. S390_lowcore.system_timer = current->thread.system_timer;
  174. S390_lowcore.hardirq_timer = current->thread.hardirq_timer;
  175. S390_lowcore.softirq_timer = current->thread.softirq_timer;
  176. }
  177. /*
  178. * In s390, accounting pending user time also implies
  179. * accounting system time in order to correctly compute
  180. * the stolen time accounting.
  181. */
  182. void vtime_flush(struct task_struct *tsk)
  183. {
  184. if (do_account_vtime(tsk))
  185. virt_timer_expire();
  186. }
  187. /*
  188. * Update process times based on virtual cpu times stored by entry.S
  189. * to the lowcore fields user_timer, system_timer & steal_clock.
  190. */
  191. void vtime_account_irq_enter(struct task_struct *tsk)
  192. {
  193. u64 timer;
  194. timer = S390_lowcore.last_update_timer;
  195. S390_lowcore.last_update_timer = get_vtimer();
  196. timer -= S390_lowcore.last_update_timer;
  197. if ((tsk->flags & PF_VCPU) && (irq_count() == 0))
  198. S390_lowcore.guest_timer += timer;
  199. else if (hardirq_count())
  200. S390_lowcore.hardirq_timer += timer;
  201. else if (in_serving_softirq())
  202. S390_lowcore.softirq_timer += timer;
  203. else
  204. S390_lowcore.system_timer += timer;
  205. virt_timer_forward(timer);
  206. }
  207. EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
  208. void vtime_account_system(struct task_struct *tsk)
  209. __attribute__((alias("vtime_account_irq_enter")));
  210. EXPORT_SYMBOL_GPL(vtime_account_system);
  211. /*
  212. * Sorted add to a list. List is linear searched until first bigger
  213. * element is found.
  214. */
  215. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  216. {
  217. struct vtimer_list *tmp;
  218. list_for_each_entry(tmp, head, entry) {
  219. if (tmp->expires > timer->expires) {
  220. list_add_tail(&timer->entry, &tmp->entry);
  221. return;
  222. }
  223. }
  224. list_add_tail(&timer->entry, head);
  225. }
  226. /*
  227. * Handler for expired virtual CPU timer.
  228. */
  229. static void virt_timer_expire(void)
  230. {
  231. struct vtimer_list *timer, *tmp;
  232. unsigned long elapsed;
  233. LIST_HEAD(cb_list);
  234. /* walk timer list, fire all expired timers */
  235. spin_lock(&virt_timer_lock);
  236. elapsed = atomic64_read(&virt_timer_elapsed);
  237. list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) {
  238. if (timer->expires < elapsed)
  239. /* move expired timer to the callback queue */
  240. list_move_tail(&timer->entry, &cb_list);
  241. else
  242. timer->expires -= elapsed;
  243. }
  244. if (!list_empty(&virt_timer_list)) {
  245. timer = list_first_entry(&virt_timer_list,
  246. struct vtimer_list, entry);
  247. atomic64_set(&virt_timer_current, timer->expires);
  248. }
  249. atomic64_sub(elapsed, &virt_timer_elapsed);
  250. spin_unlock(&virt_timer_lock);
  251. /* Do callbacks and recharge periodic timers */
  252. list_for_each_entry_safe(timer, tmp, &cb_list, entry) {
  253. list_del_init(&timer->entry);
  254. timer->function(timer->data);
  255. if (timer->interval) {
  256. /* Recharge interval timer */
  257. timer->expires = timer->interval +
  258. atomic64_read(&virt_timer_elapsed);
  259. spin_lock(&virt_timer_lock);
  260. list_add_sorted(timer, &virt_timer_list);
  261. spin_unlock(&virt_timer_lock);
  262. }
  263. }
  264. }
  265. void init_virt_timer(struct vtimer_list *timer)
  266. {
  267. timer->function = NULL;
  268. INIT_LIST_HEAD(&timer->entry);
  269. }
  270. EXPORT_SYMBOL(init_virt_timer);
  271. static inline int vtimer_pending(struct vtimer_list *timer)
  272. {
  273. return !list_empty(&timer->entry);
  274. }
  275. static void internal_add_vtimer(struct vtimer_list *timer)
  276. {
  277. if (list_empty(&virt_timer_list)) {
  278. /* First timer, just program it. */
  279. atomic64_set(&virt_timer_current, timer->expires);
  280. atomic64_set(&virt_timer_elapsed, 0);
  281. list_add(&timer->entry, &virt_timer_list);
  282. } else {
  283. /* Update timer against current base. */
  284. timer->expires += atomic64_read(&virt_timer_elapsed);
  285. if (likely((s64) timer->expires <
  286. (s64) atomic64_read(&virt_timer_current)))
  287. /* The new timer expires before the current timer. */
  288. atomic64_set(&virt_timer_current, timer->expires);
  289. /* Insert new timer into the list. */
  290. list_add_sorted(timer, &virt_timer_list);
  291. }
  292. }
  293. static void __add_vtimer(struct vtimer_list *timer, int periodic)
  294. {
  295. unsigned long flags;
  296. timer->interval = periodic ? timer->expires : 0;
  297. spin_lock_irqsave(&virt_timer_lock, flags);
  298. internal_add_vtimer(timer);
  299. spin_unlock_irqrestore(&virt_timer_lock, flags);
  300. }
  301. /*
  302. * add_virt_timer - add a oneshot virtual CPU timer
  303. */
  304. void add_virt_timer(struct vtimer_list *timer)
  305. {
  306. __add_vtimer(timer, 0);
  307. }
  308. EXPORT_SYMBOL(add_virt_timer);
  309. /*
  310. * add_virt_timer_int - add an interval virtual CPU timer
  311. */
  312. void add_virt_timer_periodic(struct vtimer_list *timer)
  313. {
  314. __add_vtimer(timer, 1);
  315. }
  316. EXPORT_SYMBOL(add_virt_timer_periodic);
  317. static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
  318. {
  319. unsigned long flags;
  320. int rc;
  321. BUG_ON(!timer->function);
  322. if (timer->expires == expires && vtimer_pending(timer))
  323. return 1;
  324. spin_lock_irqsave(&virt_timer_lock, flags);
  325. rc = vtimer_pending(timer);
  326. if (rc)
  327. list_del_init(&timer->entry);
  328. timer->interval = periodic ? expires : 0;
  329. timer->expires = expires;
  330. internal_add_vtimer(timer);
  331. spin_unlock_irqrestore(&virt_timer_lock, flags);
  332. return rc;
  333. }
  334. /*
  335. * returns whether it has modified a pending timer (1) or not (0)
  336. */
  337. int mod_virt_timer(struct vtimer_list *timer, u64 expires)
  338. {
  339. return __mod_vtimer(timer, expires, 0);
  340. }
  341. EXPORT_SYMBOL(mod_virt_timer);
  342. /*
  343. * returns whether it has modified a pending timer (1) or not (0)
  344. */
  345. int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
  346. {
  347. return __mod_vtimer(timer, expires, 1);
  348. }
  349. EXPORT_SYMBOL(mod_virt_timer_periodic);
  350. /*
  351. * Delete a virtual timer.
  352. *
  353. * returns whether the deleted timer was pending (1) or not (0)
  354. */
  355. int del_virt_timer(struct vtimer_list *timer)
  356. {
  357. unsigned long flags;
  358. if (!vtimer_pending(timer))
  359. return 0;
  360. spin_lock_irqsave(&virt_timer_lock, flags);
  361. list_del_init(&timer->entry);
  362. spin_unlock_irqrestore(&virt_timer_lock, flags);
  363. return 1;
  364. }
  365. EXPORT_SYMBOL(del_virt_timer);
  366. /*
  367. * Start the virtual CPU timer on the current CPU.
  368. */
  369. void vtime_init(void)
  370. {
  371. /* set initial cpu timer */
  372. set_vtimer(VTIMER_MAX_SLICE);
  373. /* Setup initial MT scaling values */
  374. if (smp_cpu_mtid) {
  375. __this_cpu_write(mt_scaling_jiffies, jiffies);
  376. __this_cpu_write(mt_scaling_mult, 1);
  377. __this_cpu_write(mt_scaling_div, 1);
  378. stcctm5(smp_cpu_mtid + 1, this_cpu_ptr(mt_cycles));
  379. }
  380. }