cputime.c 24 KB

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