cputime.c 22 KB

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