cputime.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. #include <linux/export.h>
  2. #include <linux/sched.h>
  3. #include <linux/tsacct_kern.h>
  4. #include <linux/kernel_stat.h>
  5. #include <linux/static_key.h>
  6. #include <linux/context_tracking.h>
  7. #include <linux/sched/cputime.h>
  8. #include <linux/cpufreq_times.h>
  9. #include "sched.h"
  10. #include "walt.h"
  11. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  12. /*
  13. * There are no locks covering percpu hardirq/softirq time.
  14. * They are only modified in vtime_account, on corresponding CPU
  15. * with interrupts disabled. So, writes are safe.
  16. * They are read and saved off onto struct rq in update_rq_clock().
  17. * This may result in other CPU reading this CPU's irq time and can
  18. * race with irq/vtime_account on this CPU. We would either get old
  19. * or new value with a side effect of accounting a slice of irq time to wrong
  20. * task when irq is in progress while we read rq->clock. That is a worthy
  21. * compromise in place of having locks on each irq in account_system_time.
  22. */
  23. DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
  24. static int sched_clock_irqtime;
  25. void enable_sched_clock_irqtime(void)
  26. {
  27. sched_clock_irqtime = 1;
  28. }
  29. void disable_sched_clock_irqtime(void)
  30. {
  31. sched_clock_irqtime = 0;
  32. }
  33. static void irqtime_account_delta(struct irqtime *irqtime, u64 delta,
  34. enum cpu_usage_stat idx)
  35. {
  36. u64 *cpustat = kcpustat_this_cpu->cpustat;
  37. u64_stats_update_begin(&irqtime->sync);
  38. cpustat[idx] += delta;
  39. irqtime->total += delta;
  40. irqtime->tick_delta += delta;
  41. u64_stats_update_end(&irqtime->sync);
  42. }
  43. /*
  44. * Called before incrementing preempt_count on {soft,}irq_enter
  45. * and before decrementing preempt_count on {soft,}irq_exit.
  46. */
  47. void irqtime_account_irq(struct task_struct *curr)
  48. {
  49. struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
  50. s64 delta;
  51. int cpu;
  52. #ifdef CONFIG_SCHED_WALT
  53. u64 wallclock;
  54. bool account = true;
  55. #endif
  56. if (!sched_clock_irqtime)
  57. return;
  58. cpu = smp_processor_id();
  59. #ifdef CONFIG_SCHED_WALT
  60. wallclock = sched_clock_cpu(cpu);
  61. #endif
  62. delta = sched_clock_cpu(cpu) - irqtime->irq_start_time;
  63. irqtime->irq_start_time += delta;
  64. /*
  65. * We do not account for softirq time from ksoftirqd here.
  66. * We want to continue accounting softirq time to ksoftirqd thread
  67. * in that case, so as not to confuse scheduler with a special task
  68. * that do not consume any time, but still wants to run.
  69. */
  70. if (hardirq_count())
  71. irqtime_account_delta(irqtime, delta, CPUTIME_IRQ);
  72. else if (in_serving_softirq() && curr != this_cpu_ksoftirqd())
  73. irqtime_account_delta(irqtime, delta, CPUTIME_SOFTIRQ);
  74. #ifdef CONFIG_SCHED_WALT
  75. else
  76. account = false;
  77. if (account)
  78. walt_account_irqtime(cpu, curr, delta, wallclock);
  79. #endif
  80. }
  81. EXPORT_SYMBOL_GPL(irqtime_account_irq);
  82. static u64 irqtime_tick_accounted(u64 maxtime)
  83. {
  84. struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
  85. u64 delta;
  86. delta = min(irqtime->tick_delta, maxtime);
  87. irqtime->tick_delta -= delta;
  88. return delta;
  89. }
  90. #else /* CONFIG_IRQ_TIME_ACCOUNTING */
  91. #define sched_clock_irqtime (0)
  92. static u64 irqtime_tick_accounted(u64 dummy)
  93. {
  94. return 0;
  95. }
  96. #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
  97. static inline void task_group_account_field(struct task_struct *p, int index,
  98. u64 tmp)
  99. {
  100. /*
  101. * Since all updates are sure to touch the root cgroup, we
  102. * get ourselves ahead and touch it first. If the root cgroup
  103. * is the only cgroup, then nothing else should be necessary.
  104. *
  105. */
  106. __this_cpu_add(kernel_cpustat.cpustat[index], tmp);
  107. cpuacct_account_field(p, index, tmp);
  108. }
  109. /*
  110. * Account user cpu time to a process.
  111. * @p: the process that the cpu time gets accounted to
  112. * @cputime: the cpu time spent in user space since the last update
  113. */
  114. void account_user_time(struct task_struct *p, u64 cputime)
  115. {
  116. int index;
  117. /* Add user time to process. */
  118. p->utime += cputime;
  119. account_group_user_time(p, cputime);
  120. index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
  121. /* Add user time to cpustat. */
  122. task_group_account_field(p, index, cputime);
  123. /* Account for user time used */
  124. acct_account_cputime(p);
  125. /* Account power usage for user time */
  126. cpufreq_acct_update_power(p, cputime);
  127. }
  128. /*
  129. * Account guest cpu time to a process.
  130. * @p: the process that the cpu time gets accounted to
  131. * @cputime: the cpu time spent in virtual machine since the last update
  132. */
  133. void account_guest_time(struct task_struct *p, u64 cputime)
  134. {
  135. u64 *cpustat = kcpustat_this_cpu->cpustat;
  136. /* Add guest time to process. */
  137. p->utime += cputime;
  138. account_group_user_time(p, cputime);
  139. p->gtime += cputime;
  140. /* Add guest time to cpustat. */
  141. if (task_nice(p) > 0) {
  142. cpustat[CPUTIME_NICE] += cputime;
  143. cpustat[CPUTIME_GUEST_NICE] += cputime;
  144. } else {
  145. cpustat[CPUTIME_USER] += cputime;
  146. cpustat[CPUTIME_GUEST] += cputime;
  147. }
  148. }
  149. /*
  150. * Account system cpu time to a process and desired cpustat field
  151. * @p: the process that the cpu time gets accounted to
  152. * @cputime: the cpu time spent in kernel space since the last update
  153. * @index: pointer to cpustat field that has to be updated
  154. */
  155. void account_system_index_time(struct task_struct *p,
  156. u64 cputime, enum cpu_usage_stat index)
  157. {
  158. /* Add system time to process. */
  159. p->stime += cputime;
  160. account_group_system_time(p, cputime);
  161. /* Add system time to cpustat. */
  162. task_group_account_field(p, index, cputime);
  163. /* Account for system time used */
  164. acct_account_cputime(p);
  165. /* Account power usage for system time */
  166. cpufreq_acct_update_power(p, cputime);
  167. }
  168. /*
  169. * Account system cpu time to a process.
  170. * @p: the process that the cpu time gets accounted to
  171. * @hardirq_offset: the offset to subtract from hardirq_count()
  172. * @cputime: the cpu time spent in kernel space since the last update
  173. */
  174. void account_system_time(struct task_struct *p, int hardirq_offset, u64 cputime)
  175. {
  176. int index;
  177. if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
  178. account_guest_time(p, cputime);
  179. return;
  180. }
  181. if (hardirq_count() - hardirq_offset)
  182. index = CPUTIME_IRQ;
  183. else if (in_serving_softirq())
  184. index = CPUTIME_SOFTIRQ;
  185. else
  186. index = CPUTIME_SYSTEM;
  187. account_system_index_time(p, cputime, index);
  188. }
  189. /*
  190. * Account for involuntary wait time.
  191. * @cputime: the cpu time spent in involuntary wait
  192. */
  193. void account_steal_time(u64 cputime)
  194. {
  195. u64 *cpustat = kcpustat_this_cpu->cpustat;
  196. cpustat[CPUTIME_STEAL] += cputime;
  197. }
  198. /*
  199. * Account for idle time.
  200. * @cputime: the cpu time spent in idle wait
  201. */
  202. void account_idle_time(u64 cputime)
  203. {
  204. u64 *cpustat = kcpustat_this_cpu->cpustat;
  205. struct rq *rq = this_rq();
  206. if (atomic_read(&rq->nr_iowait) > 0)
  207. cpustat[CPUTIME_IOWAIT] += cputime;
  208. else
  209. cpustat[CPUTIME_IDLE] += cputime;
  210. }
  211. /*
  212. * When a guest is interrupted for a longer amount of time, missed clock
  213. * ticks are not redelivered later. Due to that, this function may on
  214. * occasion account more time than the calling functions think elapsed.
  215. */
  216. static __always_inline u64 steal_account_process_time(u64 maxtime)
  217. {
  218. #ifdef CONFIG_PARAVIRT
  219. if (static_key_false(&paravirt_steal_enabled)) {
  220. u64 steal;
  221. steal = paravirt_steal_clock(smp_processor_id());
  222. steal -= this_rq()->prev_steal_time;
  223. steal = min(steal, maxtime);
  224. account_steal_time(steal);
  225. this_rq()->prev_steal_time += steal;
  226. return steal;
  227. }
  228. #endif
  229. return 0;
  230. }
  231. /*
  232. * Account how much elapsed time was spent in steal, irq, or softirq time.
  233. */
  234. static inline u64 account_other_time(u64 max)
  235. {
  236. u64 accounted;
  237. /* Shall be converted to a lockdep-enabled lightweight check */
  238. WARN_ON_ONCE(!irqs_disabled());
  239. accounted = steal_account_process_time(max);
  240. if (accounted < max)
  241. accounted += irqtime_tick_accounted(max - accounted);
  242. return accounted;
  243. }
  244. #ifdef CONFIG_64BIT
  245. static inline u64 read_sum_exec_runtime(struct task_struct *t)
  246. {
  247. return t->se.sum_exec_runtime;
  248. }
  249. #else
  250. static u64 read_sum_exec_runtime(struct task_struct *t)
  251. {
  252. u64 ns;
  253. struct rq_flags rf;
  254. struct rq *rq;
  255. rq = task_rq_lock(t, &rf);
  256. ns = t->se.sum_exec_runtime;
  257. task_rq_unlock(rq, t, &rf);
  258. return ns;
  259. }
  260. #endif
  261. /*
  262. * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
  263. * tasks (sum on group iteration) belonging to @tsk's group.
  264. */
  265. void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
  266. {
  267. struct signal_struct *sig = tsk->signal;
  268. u64 utime, stime;
  269. struct task_struct *t;
  270. unsigned int seq, nextseq;
  271. unsigned long flags;
  272. /*
  273. * Update current task runtime to account pending time since last
  274. * scheduler action or thread_group_cputime() call. This thread group
  275. * might have other running tasks on different CPUs, but updating
  276. * their runtime can affect syscall performance, so we skip account
  277. * those pending times and rely only on values updated on tick or
  278. * other scheduler action.
  279. */
  280. if (same_thread_group(current, tsk))
  281. (void) task_sched_runtime(current);
  282. rcu_read_lock();
  283. /* Attempt a lockless read on the first round. */
  284. nextseq = 0;
  285. do {
  286. seq = nextseq;
  287. flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
  288. times->utime = sig->utime;
  289. times->stime = sig->stime;
  290. times->sum_exec_runtime = sig->sum_sched_runtime;
  291. for_each_thread(tsk, t) {
  292. task_cputime(t, &utime, &stime);
  293. times->utime += utime;
  294. times->stime += stime;
  295. times->sum_exec_runtime += read_sum_exec_runtime(t);
  296. }
  297. /* If lockless access failed, take the lock. */
  298. nextseq = 1;
  299. } while (need_seqretry(&sig->stats_lock, seq));
  300. done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
  301. rcu_read_unlock();
  302. }
  303. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  304. /*
  305. * Account a tick to a process and cpustat
  306. * @p: the process that the cpu time gets accounted to
  307. * @user_tick: is the tick from userspace
  308. * @rq: the pointer to rq
  309. *
  310. * Tick demultiplexing follows the order
  311. * - pending hardirq update
  312. * - pending softirq update
  313. * - user_time
  314. * - idle_time
  315. * - system time
  316. * - check for guest_time
  317. * - else account as system_time
  318. *
  319. * Check for hardirq is done both for system and user time as there is
  320. * no timer going off while we are on hardirq and hence we may never get an
  321. * opportunity to update it solely in system time.
  322. * p->stime and friends are only updated on system time and not on irq
  323. * softirq as those do not count in task exec_runtime any more.
  324. */
  325. static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
  326. struct rq *rq, int ticks)
  327. {
  328. u64 other, cputime = TICK_NSEC * ticks;
  329. /*
  330. * When returning from idle, many ticks can get accounted at
  331. * once, including some ticks of steal, irq, and softirq time.
  332. * Subtract those ticks from the amount of time accounted to
  333. * idle, or potentially user or system time. Due to rounding,
  334. * other time can exceed ticks occasionally.
  335. */
  336. other = account_other_time(ULONG_MAX);
  337. if (other >= cputime)
  338. return;
  339. cputime -= other;
  340. if (this_cpu_ksoftirqd() == p) {
  341. /*
  342. * ksoftirqd time do not get accounted in cpu_softirq_time.
  343. * So, we have to handle it separately here.
  344. * Also, p->stime needs to be updated for ksoftirqd.
  345. */
  346. account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
  347. } else if (user_tick) {
  348. account_user_time(p, cputime);
  349. } else if (p == rq->idle) {
  350. account_idle_time(cputime);
  351. } else if (p->flags & PF_VCPU) { /* System time or guest time */
  352. account_guest_time(p, cputime);
  353. } else {
  354. account_system_index_time(p, cputime, CPUTIME_SYSTEM);
  355. }
  356. }
  357. static void irqtime_account_idle_ticks(int ticks)
  358. {
  359. struct rq *rq = this_rq();
  360. irqtime_account_process_tick(current, 0, rq, ticks);
  361. }
  362. #else /* CONFIG_IRQ_TIME_ACCOUNTING */
  363. static inline void irqtime_account_idle_ticks(int ticks) {}
  364. static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
  365. struct rq *rq, int nr_ticks) {}
  366. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  367. /*
  368. * Use precise platform statistics if available:
  369. */
  370. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  371. #ifndef __ARCH_HAS_VTIME_TASK_SWITCH
  372. void vtime_common_task_switch(struct task_struct *prev)
  373. {
  374. if (is_idle_task(prev))
  375. vtime_account_idle(prev);
  376. else
  377. vtime_account_system(prev);
  378. vtime_flush(prev);
  379. arch_vtime_task_switch(prev);
  380. }
  381. #endif
  382. #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
  383. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  384. /*
  385. * Archs that account the whole time spent in the idle task
  386. * (outside irq) as idle time can rely on this and just implement
  387. * vtime_account_system() and vtime_account_idle(). Archs that
  388. * have other meaning of the idle time (s390 only includes the
  389. * time spent by the CPU when it's in low power mode) must override
  390. * vtime_account().
  391. */
  392. #ifndef __ARCH_HAS_VTIME_ACCOUNT
  393. void vtime_account_irq_enter(struct task_struct *tsk)
  394. {
  395. if (!in_interrupt() && is_idle_task(tsk))
  396. vtime_account_idle(tsk);
  397. else
  398. vtime_account_system(tsk);
  399. }
  400. EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
  401. #endif /* __ARCH_HAS_VTIME_ACCOUNT */
  402. void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
  403. {
  404. *ut = p->utime;
  405. *st = p->stime;
  406. }
  407. EXPORT_SYMBOL_GPL(task_cputime_adjusted);
  408. void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
  409. {
  410. struct task_cputime cputime;
  411. thread_group_cputime(p, &cputime);
  412. *ut = cputime.utime;
  413. *st = cputime.stime;
  414. }
  415. #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  416. /*
  417. * Account a single tick of cpu time.
  418. * @p: the process that the cpu time gets accounted to
  419. * @user_tick: indicates if the tick is a user or a system tick
  420. */
  421. void account_process_tick(struct task_struct *p, int user_tick)
  422. {
  423. u64 cputime, steal;
  424. struct rq *rq = this_rq();
  425. if (vtime_accounting_cpu_enabled())
  426. return;
  427. if (sched_clock_irqtime) {
  428. irqtime_account_process_tick(p, user_tick, rq, 1);
  429. return;
  430. }
  431. cputime = TICK_NSEC;
  432. steal = steal_account_process_time(ULONG_MAX);
  433. if (steal >= cputime)
  434. return;
  435. cputime -= steal;
  436. if (user_tick)
  437. account_user_time(p, cputime);
  438. else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
  439. account_system_time(p, HARDIRQ_OFFSET, cputime);
  440. else
  441. account_idle_time(cputime);
  442. }
  443. /*
  444. * Account multiple ticks of idle time.
  445. * @ticks: number of stolen ticks
  446. */
  447. void account_idle_ticks(unsigned long ticks)
  448. {
  449. u64 cputime, steal;
  450. if (sched_clock_irqtime) {
  451. irqtime_account_idle_ticks(ticks);
  452. return;
  453. }
  454. cputime = ticks * TICK_NSEC;
  455. steal = steal_account_process_time(ULONG_MAX);
  456. if (steal >= cputime)
  457. return;
  458. cputime -= steal;
  459. account_idle_time(cputime);
  460. }
  461. /*
  462. * Perform (stime * rtime) / total, but avoid multiplication overflow by
  463. * loosing precision when the numbers are big.
  464. */
  465. static u64 scale_stime(u64 stime, u64 rtime, u64 total)
  466. {
  467. u64 scaled;
  468. for (;;) {
  469. /* Make sure "rtime" is the bigger of stime/rtime */
  470. if (stime > rtime)
  471. swap(rtime, stime);
  472. /* Make sure 'total' fits in 32 bits */
  473. if (total >> 32)
  474. goto drop_precision;
  475. /* Does rtime (and thus stime) fit in 32 bits? */
  476. if (!(rtime >> 32))
  477. break;
  478. /* Can we just balance rtime/stime rather than dropping bits? */
  479. if (stime >> 31)
  480. goto drop_precision;
  481. /* We can grow stime and shrink rtime and try to make them both fit */
  482. stime <<= 1;
  483. rtime >>= 1;
  484. continue;
  485. drop_precision:
  486. /* We drop from rtime, it has more bits than stime */
  487. rtime >>= 1;
  488. total >>= 1;
  489. }
  490. /*
  491. * Make sure gcc understands that this is a 32x32->64 multiply,
  492. * followed by a 64/32->64 divide.
  493. */
  494. scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total);
  495. return scaled;
  496. }
  497. /*
  498. * Adjust tick based cputime random precision against scheduler runtime
  499. * accounting.
  500. *
  501. * Tick based cputime accounting depend on random scheduling timeslices of a
  502. * task to be interrupted or not by the timer. Depending on these
  503. * circumstances, the number of these interrupts may be over or
  504. * under-optimistic, matching the real user and system cputime with a variable
  505. * precision.
  506. *
  507. * Fix this by scaling these tick based values against the total runtime
  508. * accounted by the CFS scheduler.
  509. *
  510. * This code provides the following guarantees:
  511. *
  512. * stime + utime == rtime
  513. * stime_i+1 >= stime_i, utime_i+1 >= utime_i
  514. *
  515. * Assuming that rtime_i+1 >= rtime_i.
  516. */
  517. static void cputime_adjust(struct task_cputime *curr,
  518. struct prev_cputime *prev,
  519. u64 *ut, u64 *st)
  520. {
  521. u64 rtime, stime, utime;
  522. unsigned long flags;
  523. /* Serialize concurrent callers such that we can honour our guarantees */
  524. raw_spin_lock_irqsave(&prev->lock, flags);
  525. rtime = curr->sum_exec_runtime;
  526. /*
  527. * This is possible under two circumstances:
  528. * - rtime isn't monotonic after all (a bug);
  529. * - we got reordered by the lock.
  530. *
  531. * In both cases this acts as a filter such that the rest of the code
  532. * can assume it is monotonic regardless of anything else.
  533. */
  534. if (prev->stime + prev->utime >= rtime)
  535. goto out;
  536. stime = curr->stime;
  537. utime = curr->utime;
  538. /*
  539. * If either stime or utime are 0, assume all runtime is userspace.
  540. * Once a task gets some ticks, the monotonicy code at 'update:'
  541. * will ensure things converge to the observed ratio.
  542. */
  543. if (stime == 0) {
  544. utime = rtime;
  545. goto update;
  546. }
  547. if (utime == 0) {
  548. stime = rtime;
  549. goto update;
  550. }
  551. stime = scale_stime(stime, rtime, stime + utime);
  552. update:
  553. /*
  554. * Make sure stime doesn't go backwards; this preserves monotonicity
  555. * for utime because rtime is monotonic.
  556. *
  557. * utime_i+1 = rtime_i+1 - stime_i
  558. * = rtime_i+1 - (rtime_i - utime_i)
  559. * = (rtime_i+1 - rtime_i) + utime_i
  560. * >= utime_i
  561. */
  562. if (stime < prev->stime)
  563. stime = prev->stime;
  564. utime = rtime - stime;
  565. /*
  566. * Make sure utime doesn't go backwards; this still preserves
  567. * monotonicity for stime, analogous argument to above.
  568. */
  569. if (utime < prev->utime) {
  570. utime = prev->utime;
  571. stime = rtime - utime;
  572. }
  573. prev->stime = stime;
  574. prev->utime = utime;
  575. out:
  576. *ut = prev->utime;
  577. *st = prev->stime;
  578. raw_spin_unlock_irqrestore(&prev->lock, flags);
  579. }
  580. void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
  581. {
  582. struct task_cputime cputime = {
  583. .sum_exec_runtime = p->se.sum_exec_runtime,
  584. };
  585. task_cputime(p, &cputime.utime, &cputime.stime);
  586. cputime_adjust(&cputime, &p->prev_cputime, ut, st);
  587. }
  588. EXPORT_SYMBOL_GPL(task_cputime_adjusted);
  589. void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
  590. {
  591. struct task_cputime cputime;
  592. thread_group_cputime(p, &cputime);
  593. cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
  594. }
  595. #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  596. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  597. static u64 vtime_delta(struct vtime *vtime)
  598. {
  599. unsigned long long clock;
  600. clock = sched_clock();
  601. if (clock < vtime->starttime)
  602. return 0;
  603. return clock - vtime->starttime;
  604. }
  605. static u64 get_vtime_delta(struct vtime *vtime)
  606. {
  607. u64 delta = vtime_delta(vtime);
  608. u64 other;
  609. /*
  610. * Unlike tick based timing, vtime based timing never has lost
  611. * ticks, and no need for steal time accounting to make up for
  612. * lost ticks. Vtime accounts a rounded version of actual
  613. * elapsed time. Limit account_other_time to prevent rounding
  614. * errors from causing elapsed vtime to go negative.
  615. */
  616. other = account_other_time(delta);
  617. WARN_ON_ONCE(vtime->state == VTIME_INACTIVE);
  618. vtime->starttime += delta;
  619. return delta - other;
  620. }
  621. static void __vtime_account_system(struct task_struct *tsk,
  622. struct vtime *vtime)
  623. {
  624. vtime->stime += get_vtime_delta(vtime);
  625. if (vtime->stime >= TICK_NSEC) {
  626. account_system_time(tsk, irq_count(), vtime->stime);
  627. vtime->stime = 0;
  628. }
  629. }
  630. static void vtime_account_guest(struct task_struct *tsk,
  631. struct vtime *vtime)
  632. {
  633. vtime->gtime += get_vtime_delta(vtime);
  634. if (vtime->gtime >= TICK_NSEC) {
  635. account_guest_time(tsk, vtime->gtime);
  636. vtime->gtime = 0;
  637. }
  638. }
  639. void vtime_account_system(struct task_struct *tsk)
  640. {
  641. struct vtime *vtime = &tsk->vtime;
  642. if (!vtime_delta(vtime))
  643. return;
  644. write_seqcount_begin(&vtime->seqcount);
  645. /* We might have scheduled out from guest path */
  646. if (tsk->flags & PF_VCPU)
  647. vtime_account_guest(tsk, vtime);
  648. else
  649. __vtime_account_system(tsk, vtime);
  650. write_seqcount_end(&vtime->seqcount);
  651. }
  652. void vtime_user_enter(struct task_struct *tsk)
  653. {
  654. struct vtime *vtime = &tsk->vtime;
  655. write_seqcount_begin(&vtime->seqcount);
  656. __vtime_account_system(tsk, vtime);
  657. vtime->state = VTIME_USER;
  658. write_seqcount_end(&vtime->seqcount);
  659. }
  660. void vtime_user_exit(struct task_struct *tsk)
  661. {
  662. struct vtime *vtime = &tsk->vtime;
  663. write_seqcount_begin(&vtime->seqcount);
  664. vtime->utime += get_vtime_delta(vtime);
  665. if (vtime->utime >= TICK_NSEC) {
  666. account_user_time(tsk, vtime->utime);
  667. vtime->utime = 0;
  668. }
  669. vtime->state = VTIME_SYS;
  670. write_seqcount_end(&vtime->seqcount);
  671. }
  672. void vtime_guest_enter(struct task_struct *tsk)
  673. {
  674. struct vtime *vtime = &tsk->vtime;
  675. /*
  676. * The flags must be updated under the lock with
  677. * the vtime_starttime flush and update.
  678. * That enforces a right ordering and update sequence
  679. * synchronization against the reader (task_gtime())
  680. * that can thus safely catch up with a tickless delta.
  681. */
  682. write_seqcount_begin(&vtime->seqcount);
  683. __vtime_account_system(tsk, vtime);
  684. tsk->flags |= PF_VCPU;
  685. write_seqcount_end(&vtime->seqcount);
  686. }
  687. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  688. void vtime_guest_exit(struct task_struct *tsk)
  689. {
  690. struct vtime *vtime = &tsk->vtime;
  691. write_seqcount_begin(&vtime->seqcount);
  692. vtime_account_guest(tsk, vtime);
  693. tsk->flags &= ~PF_VCPU;
  694. write_seqcount_end(&vtime->seqcount);
  695. }
  696. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  697. void vtime_account_idle(struct task_struct *tsk)
  698. {
  699. account_idle_time(get_vtime_delta(&tsk->vtime));
  700. }
  701. void arch_vtime_task_switch(struct task_struct *prev)
  702. {
  703. struct vtime *vtime = &prev->vtime;
  704. write_seqcount_begin(&vtime->seqcount);
  705. vtime->state = VTIME_INACTIVE;
  706. write_seqcount_end(&vtime->seqcount);
  707. vtime = &current->vtime;
  708. write_seqcount_begin(&vtime->seqcount);
  709. vtime->state = VTIME_SYS;
  710. vtime->starttime = sched_clock();
  711. write_seqcount_end(&vtime->seqcount);
  712. }
  713. void vtime_init_idle(struct task_struct *t, int cpu)
  714. {
  715. struct vtime *vtime = &t->vtime;
  716. unsigned long flags;
  717. local_irq_save(flags);
  718. write_seqcount_begin(&vtime->seqcount);
  719. vtime->state = VTIME_SYS;
  720. vtime->starttime = sched_clock();
  721. write_seqcount_end(&vtime->seqcount);
  722. local_irq_restore(flags);
  723. }
  724. u64 task_gtime(struct task_struct *t)
  725. {
  726. struct vtime *vtime = &t->vtime;
  727. unsigned int seq;
  728. u64 gtime;
  729. if (!vtime_accounting_enabled())
  730. return t->gtime;
  731. do {
  732. seq = read_seqcount_begin(&vtime->seqcount);
  733. gtime = t->gtime;
  734. if (vtime->state == VTIME_SYS && t->flags & PF_VCPU)
  735. gtime += vtime->gtime + vtime_delta(vtime);
  736. } while (read_seqcount_retry(&vtime->seqcount, seq));
  737. return gtime;
  738. }
  739. /*
  740. * Fetch cputime raw values from fields of task_struct and
  741. * add up the pending nohz execution time since the last
  742. * cputime snapshot.
  743. */
  744. void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
  745. {
  746. struct vtime *vtime = &t->vtime;
  747. unsigned int seq;
  748. u64 delta;
  749. if (!vtime_accounting_enabled()) {
  750. *utime = t->utime;
  751. *stime = t->stime;
  752. return;
  753. }
  754. do {
  755. seq = read_seqcount_begin(&vtime->seqcount);
  756. *utime = t->utime;
  757. *stime = t->stime;
  758. /* Task is sleeping, nothing to add */
  759. if (vtime->state == VTIME_INACTIVE || is_idle_task(t))
  760. continue;
  761. delta = vtime_delta(vtime);
  762. /*
  763. * Task runs either in user or kernel space, add pending nohz time to
  764. * the right place.
  765. */
  766. if (vtime->state == VTIME_USER || t->flags & PF_VCPU)
  767. *utime += vtime->utime + delta;
  768. else if (vtime->state == VTIME_SYS)
  769. *stime += vtime->stime + delta;
  770. } while (read_seqcount_retry(&vtime->seqcount, seq));
  771. }
  772. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */