cputime.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Simple CPU accounting cgroup controller
  4. */
  5. #include <linux/cpufreq_times.h>
  6. #include "sched.h"
  7. #include <trace/hooks/sched.h>
  8. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  9. /*
  10. * There are no locks covering percpu hardirq/softirq time.
  11. * They are only modified in vtime_account, on corresponding CPU
  12. * with interrupts disabled. So, writes are safe.
  13. * They are read and saved off onto struct rq in update_rq_clock().
  14. * This may result in other CPU reading this CPU's irq time and can
  15. * race with irq/vtime_account on this CPU. We would either get old
  16. * or new value with a side effect of accounting a slice of irq time to wrong
  17. * task when irq is in progress while we read rq->clock. That is a worthy
  18. * compromise in place of having locks on each irq in account_system_time.
  19. */
  20. DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
  21. EXPORT_PER_CPU_SYMBOL_GPL(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. trace_android_rvh_account_irq(curr, cpu, delta);
  66. }
  67. EXPORT_SYMBOL_GPL(irqtime_account_irq);
  68. static u64 irqtime_tick_accounted(u64 maxtime)
  69. {
  70. struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
  71. u64 delta;
  72. delta = min(irqtime->tick_delta, maxtime);
  73. irqtime->tick_delta -= delta;
  74. return delta;
  75. }
  76. #else /* CONFIG_IRQ_TIME_ACCOUNTING */
  77. #define sched_clock_irqtime (0)
  78. static u64 irqtime_tick_accounted(u64 dummy)
  79. {
  80. return 0;
  81. }
  82. #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
  83. static inline void task_group_account_field(struct task_struct *p, int index,
  84. u64 tmp)
  85. {
  86. /*
  87. * Since all updates are sure to touch the root cgroup, we
  88. * get ourselves ahead and touch it first. If the root cgroup
  89. * is the only cgroup, then nothing else should be necessary.
  90. *
  91. */
  92. __this_cpu_add(kernel_cpustat.cpustat[index], tmp);
  93. cgroup_account_cputime_field(p, index, tmp);
  94. }
  95. /*
  96. * Account user CPU time to a process.
  97. * @p: the process that the CPU time gets accounted to
  98. * @cputime: the CPU time spent in user space since the last update
  99. */
  100. void account_user_time(struct task_struct *p, u64 cputime)
  101. {
  102. int index;
  103. /* Add user time to process. */
  104. p->utime += cputime;
  105. account_group_user_time(p, cputime);
  106. index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
  107. /* Add user time to cpustat. */
  108. task_group_account_field(p, index, cputime);
  109. /* Account for user time used */
  110. acct_account_cputime(p);
  111. /* Account power usage for user time */
  112. cpufreq_acct_update_power(p, cputime);
  113. }
  114. /*
  115. * Account guest CPU time to a process.
  116. * @p: the process that the CPU time gets accounted to
  117. * @cputime: the CPU time spent in virtual machine since the last update
  118. */
  119. void account_guest_time(struct task_struct *p, u64 cputime)
  120. {
  121. u64 *cpustat = kcpustat_this_cpu->cpustat;
  122. /* Add guest time to process. */
  123. p->utime += cputime;
  124. account_group_user_time(p, cputime);
  125. p->gtime += cputime;
  126. /* Add guest time to cpustat. */
  127. if (task_nice(p) > 0) {
  128. task_group_account_field(p, CPUTIME_NICE, cputime);
  129. cpustat[CPUTIME_GUEST_NICE] += cputime;
  130. } else {
  131. task_group_account_field(p, CPUTIME_USER, cputime);
  132. cpustat[CPUTIME_GUEST] += cputime;
  133. }
  134. }
  135. /*
  136. * Account system CPU time to a process and desired cpustat field
  137. * @p: the process that the CPU time gets accounted to
  138. * @cputime: the CPU time spent in kernel space since the last update
  139. * @index: pointer to cpustat field that has to be updated
  140. */
  141. void account_system_index_time(struct task_struct *p,
  142. u64 cputime, enum cpu_usage_stat index)
  143. {
  144. /* Add system time to process. */
  145. p->stime += cputime;
  146. account_group_system_time(p, cputime);
  147. /* Add system time to cpustat. */
  148. task_group_account_field(p, index, cputime);
  149. /* Account for system time used */
  150. acct_account_cputime(p);
  151. /* Account power usage for system time */
  152. cpufreq_acct_update_power(p, cputime);
  153. }
  154. /*
  155. * Account system CPU time to a process.
  156. * @p: the process that the CPU time gets accounted to
  157. * @hardirq_offset: the offset to subtract from hardirq_count()
  158. * @cputime: the CPU time spent in kernel space since the last update
  159. */
  160. void account_system_time(struct task_struct *p, int hardirq_offset, u64 cputime)
  161. {
  162. int index;
  163. if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
  164. account_guest_time(p, cputime);
  165. return;
  166. }
  167. if (hardirq_count() - hardirq_offset)
  168. index = CPUTIME_IRQ;
  169. else if (in_serving_softirq())
  170. index = CPUTIME_SOFTIRQ;
  171. else
  172. index = CPUTIME_SYSTEM;
  173. account_system_index_time(p, cputime, index);
  174. }
  175. /*
  176. * Account for involuntary wait time.
  177. * @cputime: the CPU time spent in involuntary wait
  178. */
  179. void account_steal_time(u64 cputime)
  180. {
  181. u64 *cpustat = kcpustat_this_cpu->cpustat;
  182. cpustat[CPUTIME_STEAL] += cputime;
  183. }
  184. /*
  185. * Account for idle time.
  186. * @cputime: the CPU time spent in idle wait
  187. */
  188. void account_idle_time(u64 cputime)
  189. {
  190. u64 *cpustat = kcpustat_this_cpu->cpustat;
  191. struct rq *rq = this_rq();
  192. if (atomic_read(&rq->nr_iowait) > 0)
  193. cpustat[CPUTIME_IOWAIT] += cputime;
  194. else
  195. cpustat[CPUTIME_IDLE] += cputime;
  196. }
  197. /*
  198. * When a guest is interrupted for a longer amount of time, missed clock
  199. * ticks are not redelivered later. Due to that, this function may on
  200. * occasion account more time than the calling functions think elapsed.
  201. */
  202. static __always_inline u64 steal_account_process_time(u64 maxtime)
  203. {
  204. #ifdef CONFIG_PARAVIRT
  205. if (static_key_false(&paravirt_steal_enabled)) {
  206. u64 steal;
  207. steal = paravirt_steal_clock(smp_processor_id());
  208. steal -= this_rq()->prev_steal_time;
  209. steal = min(steal, maxtime);
  210. account_steal_time(steal);
  211. this_rq()->prev_steal_time += steal;
  212. return steal;
  213. }
  214. #endif
  215. return 0;
  216. }
  217. /*
  218. * Account how much elapsed time was spent in steal, irq, or softirq time.
  219. */
  220. static inline u64 account_other_time(u64 max)
  221. {
  222. u64 accounted;
  223. lockdep_assert_irqs_disabled();
  224. accounted = steal_account_process_time(max);
  225. if (accounted < max)
  226. accounted += irqtime_tick_accounted(max - accounted);
  227. return accounted;
  228. }
  229. #ifdef CONFIG_64BIT
  230. static inline u64 read_sum_exec_runtime(struct task_struct *t)
  231. {
  232. return t->se.sum_exec_runtime;
  233. }
  234. #else
  235. static u64 read_sum_exec_runtime(struct task_struct *t)
  236. {
  237. u64 ns;
  238. struct rq_flags rf;
  239. struct rq *rq;
  240. rq = task_rq_lock(t, &rf);
  241. ns = t->se.sum_exec_runtime;
  242. task_rq_unlock(rq, t, &rf);
  243. return ns;
  244. }
  245. #endif
  246. /*
  247. * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
  248. * tasks (sum on group iteration) belonging to @tsk's group.
  249. */
  250. void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
  251. {
  252. struct signal_struct *sig = tsk->signal;
  253. u64 utime, stime;
  254. struct task_struct *t;
  255. unsigned int seq, nextseq;
  256. unsigned long flags;
  257. /*
  258. * Update current task runtime to account pending time since last
  259. * scheduler action or thread_group_cputime() call. This thread group
  260. * might have other running tasks on different CPUs, but updating
  261. * their runtime can affect syscall performance, so we skip account
  262. * those pending times and rely only on values updated on tick or
  263. * other scheduler action.
  264. */
  265. if (same_thread_group(current, tsk))
  266. (void) task_sched_runtime(current);
  267. rcu_read_lock();
  268. /* Attempt a lockless read on the first round. */
  269. nextseq = 0;
  270. do {
  271. seq = nextseq;
  272. flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
  273. times->utime = sig->utime;
  274. times->stime = sig->stime;
  275. times->sum_exec_runtime = sig->sum_sched_runtime;
  276. for_each_thread(tsk, t) {
  277. task_cputime(t, &utime, &stime);
  278. times->utime += utime;
  279. times->stime += stime;
  280. times->sum_exec_runtime += read_sum_exec_runtime(t);
  281. }
  282. /* If lockless access failed, take the lock. */
  283. nextseq = 1;
  284. } while (need_seqretry(&sig->stats_lock, seq));
  285. done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
  286. rcu_read_unlock();
  287. }
  288. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  289. /*
  290. * Account a tick to a process and cpustat
  291. * @p: the process that the CPU time gets accounted to
  292. * @user_tick: is the tick from userspace
  293. * @rq: the pointer to rq
  294. *
  295. * Tick demultiplexing follows the order
  296. * - pending hardirq update
  297. * - pending softirq update
  298. * - user_time
  299. * - idle_time
  300. * - system time
  301. * - check for guest_time
  302. * - else account as system_time
  303. *
  304. * Check for hardirq is done both for system and user time as there is
  305. * no timer going off while we are on hardirq and hence we may never get an
  306. * opportunity to update it solely in system time.
  307. * p->stime and friends are only updated on system time and not on irq
  308. * softirq as those do not count in task exec_runtime any more.
  309. */
  310. static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
  311. int ticks)
  312. {
  313. u64 other, cputime = TICK_NSEC * ticks;
  314. /*
  315. * When returning from idle, many ticks can get accounted at
  316. * once, including some ticks of steal, irq, and softirq time.
  317. * Subtract those ticks from the amount of time accounted to
  318. * idle, or potentially user or system time. Due to rounding,
  319. * other time can exceed ticks occasionally.
  320. */
  321. other = account_other_time(ULONG_MAX);
  322. if (other >= cputime)
  323. return;
  324. cputime -= other;
  325. if (this_cpu_ksoftirqd() == p) {
  326. /*
  327. * ksoftirqd time do not get accounted in cpu_softirq_time.
  328. * So, we have to handle it separately here.
  329. * Also, p->stime needs to be updated for ksoftirqd.
  330. */
  331. account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
  332. } else if (user_tick) {
  333. account_user_time(p, cputime);
  334. } else if (p == this_rq()->idle) {
  335. account_idle_time(cputime);
  336. } else if (p->flags & PF_VCPU) { /* System time or guest time */
  337. account_guest_time(p, cputime);
  338. } else {
  339. account_system_index_time(p, cputime, CPUTIME_SYSTEM);
  340. }
  341. trace_android_vh_irqtime_account_process_tick(p, this_rq(), user_tick, ticks);
  342. }
  343. static void irqtime_account_idle_ticks(int ticks)
  344. {
  345. irqtime_account_process_tick(current, 0, ticks);
  346. }
  347. #else /* CONFIG_IRQ_TIME_ACCOUNTING */
  348. static inline void irqtime_account_idle_ticks(int ticks) { }
  349. static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
  350. int nr_ticks) { }
  351. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  352. /*
  353. * Use precise platform statistics if available:
  354. */
  355. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  356. # ifndef __ARCH_HAS_VTIME_TASK_SWITCH
  357. void vtime_task_switch(struct task_struct *prev)
  358. {
  359. if (is_idle_task(prev))
  360. vtime_account_idle(prev);
  361. else
  362. vtime_account_kernel(prev);
  363. vtime_flush(prev);
  364. arch_vtime_task_switch(prev);
  365. }
  366. # endif
  367. /*
  368. * Archs that account the whole time spent in the idle task
  369. * (outside irq) as idle time can rely on this and just implement
  370. * vtime_account_kernel() and vtime_account_idle(). Archs that
  371. * have other meaning of the idle time (s390 only includes the
  372. * time spent by the CPU when it's in low power mode) must override
  373. * vtime_account().
  374. */
  375. #ifndef __ARCH_HAS_VTIME_ACCOUNT
  376. void vtime_account_irq_enter(struct task_struct *tsk)
  377. {
  378. if (!in_interrupt() && is_idle_task(tsk))
  379. vtime_account_idle(tsk);
  380. else
  381. vtime_account_kernel(tsk);
  382. }
  383. EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
  384. #endif /* __ARCH_HAS_VTIME_ACCOUNT */
  385. void cputime_adjust(struct task_cputime *curr, struct prev_cputime *prev,
  386. u64 *ut, u64 *st)
  387. {
  388. *ut = curr->utime;
  389. *st = curr->stime;
  390. }
  391. void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
  392. {
  393. *ut = p->utime;
  394. *st = p->stime;
  395. }
  396. EXPORT_SYMBOL_GPL(task_cputime_adjusted);
  397. void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
  398. {
  399. struct task_cputime cputime;
  400. thread_group_cputime(p, &cputime);
  401. *ut = cputime.utime;
  402. *st = cputime.stime;
  403. }
  404. EXPORT_SYMBOL_GPL(thread_group_cputime_adjusted);
  405. #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE: */
  406. /*
  407. * Account a single tick of CPU time.
  408. * @p: the process that the CPU time gets accounted to
  409. * @user_tick: indicates if the tick is a user or a system tick
  410. */
  411. void account_process_tick(struct task_struct *p, int user_tick)
  412. {
  413. u64 cputime, steal;
  414. if (vtime_accounting_enabled_this_cpu())
  415. return;
  416. trace_android_vh_account_task_time(p, this_rq(), user_tick);
  417. if (sched_clock_irqtime) {
  418. irqtime_account_process_tick(p, user_tick, 1);
  419. return;
  420. }
  421. cputime = TICK_NSEC;
  422. steal = steal_account_process_time(ULONG_MAX);
  423. if (steal >= cputime)
  424. return;
  425. cputime -= steal;
  426. if (user_tick)
  427. account_user_time(p, cputime);
  428. else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
  429. account_system_time(p, HARDIRQ_OFFSET, cputime);
  430. else
  431. account_idle_time(cputime);
  432. }
  433. /*
  434. * Account multiple ticks of idle time.
  435. * @ticks: number of stolen ticks
  436. */
  437. void account_idle_ticks(unsigned long ticks)
  438. {
  439. u64 cputime, steal;
  440. if (sched_clock_irqtime) {
  441. irqtime_account_idle_ticks(ticks);
  442. return;
  443. }
  444. cputime = ticks * TICK_NSEC;
  445. steal = steal_account_process_time(ULONG_MAX);
  446. if (steal >= cputime)
  447. return;
  448. cputime -= steal;
  449. account_idle_time(cputime);
  450. }
  451. /*
  452. * Adjust tick based cputime random precision against scheduler runtime
  453. * accounting.
  454. *
  455. * Tick based cputime accounting depend on random scheduling timeslices of a
  456. * task to be interrupted or not by the timer. Depending on these
  457. * circumstances, the number of these interrupts may be over or
  458. * under-optimistic, matching the real user and system cputime with a variable
  459. * precision.
  460. *
  461. * Fix this by scaling these tick based values against the total runtime
  462. * accounted by the CFS scheduler.
  463. *
  464. * This code provides the following guarantees:
  465. *
  466. * stime + utime == rtime
  467. * stime_i+1 >= stime_i, utime_i+1 >= utime_i
  468. *
  469. * Assuming that rtime_i+1 >= rtime_i.
  470. */
  471. void cputime_adjust(struct task_cputime *curr, struct prev_cputime *prev,
  472. u64 *ut, u64 *st)
  473. {
  474. u64 rtime, stime, utime;
  475. unsigned long flags;
  476. /* Serialize concurrent callers such that we can honour our guarantees */
  477. raw_spin_lock_irqsave(&prev->lock, flags);
  478. rtime = curr->sum_exec_runtime;
  479. /*
  480. * This is possible under two circumstances:
  481. * - rtime isn't monotonic after all (a bug);
  482. * - we got reordered by the lock.
  483. *
  484. * In both cases this acts as a filter such that the rest of the code
  485. * can assume it is monotonic regardless of anything else.
  486. */
  487. if (prev->stime + prev->utime >= rtime)
  488. goto out;
  489. stime = curr->stime;
  490. utime = curr->utime;
  491. /*
  492. * If either stime or utime are 0, assume all runtime is userspace.
  493. * Once a task gets some ticks, the monotonicy code at 'update:'
  494. * will ensure things converge to the observed ratio.
  495. */
  496. if (stime == 0) {
  497. utime = rtime;
  498. goto update;
  499. }
  500. if (utime == 0) {
  501. stime = rtime;
  502. goto update;
  503. }
  504. stime = mul_u64_u64_div_u64(stime, rtime, stime + utime);
  505. update:
  506. /*
  507. * Make sure stime doesn't go backwards; this preserves monotonicity
  508. * for utime because rtime is monotonic.
  509. *
  510. * utime_i+1 = rtime_i+1 - stime_i
  511. * = rtime_i+1 - (rtime_i - utime_i)
  512. * = (rtime_i+1 - rtime_i) + utime_i
  513. * >= utime_i
  514. */
  515. if (stime < prev->stime)
  516. stime = prev->stime;
  517. utime = rtime - stime;
  518. /*
  519. * Make sure utime doesn't go backwards; this still preserves
  520. * monotonicity for stime, analogous argument to above.
  521. */
  522. if (utime < prev->utime) {
  523. utime = prev->utime;
  524. stime = rtime - utime;
  525. }
  526. prev->stime = stime;
  527. prev->utime = utime;
  528. out:
  529. *ut = prev->utime;
  530. *st = prev->stime;
  531. raw_spin_unlock_irqrestore(&prev->lock, flags);
  532. }
  533. void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
  534. {
  535. struct task_cputime cputime = {
  536. .sum_exec_runtime = p->se.sum_exec_runtime,
  537. };
  538. task_cputime(p, &cputime.utime, &cputime.stime);
  539. cputime_adjust(&cputime, &p->prev_cputime, ut, st);
  540. }
  541. EXPORT_SYMBOL_GPL(task_cputime_adjusted);
  542. void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
  543. {
  544. struct task_cputime cputime;
  545. thread_group_cputime(p, &cputime);
  546. cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
  547. }
  548. EXPORT_SYMBOL_GPL(thread_group_cputime_adjusted);
  549. #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  550. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  551. static u64 vtime_delta(struct vtime *vtime)
  552. {
  553. unsigned long long clock;
  554. clock = sched_clock();
  555. if (clock < vtime->starttime)
  556. return 0;
  557. return clock - vtime->starttime;
  558. }
  559. static u64 get_vtime_delta(struct vtime *vtime)
  560. {
  561. u64 delta = vtime_delta(vtime);
  562. u64 other;
  563. /*
  564. * Unlike tick based timing, vtime based timing never has lost
  565. * ticks, and no need for steal time accounting to make up for
  566. * lost ticks. Vtime accounts a rounded version of actual
  567. * elapsed time. Limit account_other_time to prevent rounding
  568. * errors from causing elapsed vtime to go negative.
  569. */
  570. other = account_other_time(delta);
  571. WARN_ON_ONCE(vtime->state == VTIME_INACTIVE);
  572. vtime->starttime += delta;
  573. return delta - other;
  574. }
  575. static void vtime_account_system(struct task_struct *tsk,
  576. struct vtime *vtime)
  577. {
  578. vtime->stime += get_vtime_delta(vtime);
  579. if (vtime->stime >= TICK_NSEC) {
  580. account_system_time(tsk, irq_count(), vtime->stime);
  581. vtime->stime = 0;
  582. }
  583. }
  584. static void vtime_account_guest(struct task_struct *tsk,
  585. struct vtime *vtime)
  586. {
  587. vtime->gtime += get_vtime_delta(vtime);
  588. if (vtime->gtime >= TICK_NSEC) {
  589. account_guest_time(tsk, vtime->gtime);
  590. vtime->gtime = 0;
  591. }
  592. }
  593. static void __vtime_account_kernel(struct task_struct *tsk,
  594. struct vtime *vtime)
  595. {
  596. /* We might have scheduled out from guest path */
  597. if (vtime->state == VTIME_GUEST)
  598. vtime_account_guest(tsk, vtime);
  599. else
  600. vtime_account_system(tsk, vtime);
  601. }
  602. void vtime_account_kernel(struct task_struct *tsk)
  603. {
  604. struct vtime *vtime = &tsk->vtime;
  605. if (!vtime_delta(vtime))
  606. return;
  607. write_seqcount_begin(&vtime->seqcount);
  608. __vtime_account_kernel(tsk, vtime);
  609. write_seqcount_end(&vtime->seqcount);
  610. }
  611. void vtime_user_enter(struct task_struct *tsk)
  612. {
  613. struct vtime *vtime = &tsk->vtime;
  614. write_seqcount_begin(&vtime->seqcount);
  615. vtime_account_system(tsk, vtime);
  616. vtime->state = VTIME_USER;
  617. write_seqcount_end(&vtime->seqcount);
  618. }
  619. void vtime_user_exit(struct task_struct *tsk)
  620. {
  621. struct vtime *vtime = &tsk->vtime;
  622. write_seqcount_begin(&vtime->seqcount);
  623. vtime->utime += get_vtime_delta(vtime);
  624. if (vtime->utime >= TICK_NSEC) {
  625. account_user_time(tsk, vtime->utime);
  626. vtime->utime = 0;
  627. }
  628. vtime->state = VTIME_SYS;
  629. write_seqcount_end(&vtime->seqcount);
  630. }
  631. void vtime_guest_enter(struct task_struct *tsk)
  632. {
  633. struct vtime *vtime = &tsk->vtime;
  634. /*
  635. * The flags must be updated under the lock with
  636. * the vtime_starttime flush and update.
  637. * That enforces a right ordering and update sequence
  638. * synchronization against the reader (task_gtime())
  639. * that can thus safely catch up with a tickless delta.
  640. */
  641. write_seqcount_begin(&vtime->seqcount);
  642. vtime_account_system(tsk, vtime);
  643. tsk->flags |= PF_VCPU;
  644. vtime->state = VTIME_GUEST;
  645. write_seqcount_end(&vtime->seqcount);
  646. }
  647. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  648. void vtime_guest_exit(struct task_struct *tsk)
  649. {
  650. struct vtime *vtime = &tsk->vtime;
  651. write_seqcount_begin(&vtime->seqcount);
  652. vtime_account_guest(tsk, vtime);
  653. tsk->flags &= ~PF_VCPU;
  654. vtime->state = VTIME_SYS;
  655. write_seqcount_end(&vtime->seqcount);
  656. }
  657. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  658. void vtime_account_idle(struct task_struct *tsk)
  659. {
  660. account_idle_time(get_vtime_delta(&tsk->vtime));
  661. }
  662. void vtime_task_switch_generic(struct task_struct *prev)
  663. {
  664. struct vtime *vtime = &prev->vtime;
  665. write_seqcount_begin(&vtime->seqcount);
  666. if (vtime->state == VTIME_IDLE)
  667. vtime_account_idle(prev);
  668. else
  669. __vtime_account_kernel(prev, vtime);
  670. vtime->state = VTIME_INACTIVE;
  671. vtime->cpu = -1;
  672. write_seqcount_end(&vtime->seqcount);
  673. vtime = &current->vtime;
  674. write_seqcount_begin(&vtime->seqcount);
  675. if (is_idle_task(current))
  676. vtime->state = VTIME_IDLE;
  677. else if (current->flags & PF_VCPU)
  678. vtime->state = VTIME_GUEST;
  679. else
  680. vtime->state = VTIME_SYS;
  681. vtime->starttime = sched_clock();
  682. vtime->cpu = smp_processor_id();
  683. write_seqcount_end(&vtime->seqcount);
  684. }
  685. void vtime_init_idle(struct task_struct *t, int cpu)
  686. {
  687. struct vtime *vtime = &t->vtime;
  688. unsigned long flags;
  689. local_irq_save(flags);
  690. write_seqcount_begin(&vtime->seqcount);
  691. vtime->state = VTIME_IDLE;
  692. vtime->starttime = sched_clock();
  693. vtime->cpu = cpu;
  694. write_seqcount_end(&vtime->seqcount);
  695. local_irq_restore(flags);
  696. }
  697. u64 task_gtime(struct task_struct *t)
  698. {
  699. struct vtime *vtime = &t->vtime;
  700. unsigned int seq;
  701. u64 gtime;
  702. if (!vtime_accounting_enabled())
  703. return t->gtime;
  704. do {
  705. seq = read_seqcount_begin(&vtime->seqcount);
  706. gtime = t->gtime;
  707. if (vtime->state == VTIME_GUEST)
  708. gtime += vtime->gtime + vtime_delta(vtime);
  709. } while (read_seqcount_retry(&vtime->seqcount, seq));
  710. return gtime;
  711. }
  712. /*
  713. * Fetch cputime raw values from fields of task_struct and
  714. * add up the pending nohz execution time since the last
  715. * cputime snapshot.
  716. */
  717. void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
  718. {
  719. struct vtime *vtime = &t->vtime;
  720. unsigned int seq;
  721. u64 delta;
  722. if (!vtime_accounting_enabled()) {
  723. *utime = t->utime;
  724. *stime = t->stime;
  725. return;
  726. }
  727. do {
  728. seq = read_seqcount_begin(&vtime->seqcount);
  729. *utime = t->utime;
  730. *stime = t->stime;
  731. /* Task is sleeping or idle, nothing to add */
  732. if (vtime->state < VTIME_SYS)
  733. continue;
  734. delta = vtime_delta(vtime);
  735. /*
  736. * Task runs either in user (including guest) or kernel space,
  737. * add pending nohz time to the right place.
  738. */
  739. if (vtime->state == VTIME_SYS)
  740. *stime += vtime->stime + delta;
  741. else
  742. *utime += vtime->utime + delta;
  743. } while (read_seqcount_retry(&vtime->seqcount, seq));
  744. }
  745. static int vtime_state_fetch(struct vtime *vtime, int cpu)
  746. {
  747. int state = READ_ONCE(vtime->state);
  748. /*
  749. * We raced against a context switch, fetch the
  750. * kcpustat task again.
  751. */
  752. if (vtime->cpu != cpu && vtime->cpu != -1)
  753. return -EAGAIN;
  754. /*
  755. * Two possible things here:
  756. * 1) We are seeing the scheduling out task (prev) or any past one.
  757. * 2) We are seeing the scheduling in task (next) but it hasn't
  758. * passed though vtime_task_switch() yet so the pending
  759. * cputime of the prev task may not be flushed yet.
  760. *
  761. * Case 1) is ok but 2) is not. So wait for a safe VTIME state.
  762. */
  763. if (state == VTIME_INACTIVE)
  764. return -EAGAIN;
  765. return state;
  766. }
  767. static u64 kcpustat_user_vtime(struct vtime *vtime)
  768. {
  769. if (vtime->state == VTIME_USER)
  770. return vtime->utime + vtime_delta(vtime);
  771. else if (vtime->state == VTIME_GUEST)
  772. return vtime->gtime + vtime_delta(vtime);
  773. return 0;
  774. }
  775. static int kcpustat_field_vtime(u64 *cpustat,
  776. struct task_struct *tsk,
  777. enum cpu_usage_stat usage,
  778. int cpu, u64 *val)
  779. {
  780. struct vtime *vtime = &tsk->vtime;
  781. unsigned int seq;
  782. do {
  783. int state;
  784. seq = read_seqcount_begin(&vtime->seqcount);
  785. state = vtime_state_fetch(vtime, cpu);
  786. if (state < 0)
  787. return state;
  788. *val = cpustat[usage];
  789. /*
  790. * Nice VS unnice cputime accounting may be inaccurate if
  791. * the nice value has changed since the last vtime update.
  792. * But proper fix would involve interrupting target on nice
  793. * updates which is a no go on nohz_full (although the scheduler
  794. * may still interrupt the target if rescheduling is needed...)
  795. */
  796. switch (usage) {
  797. case CPUTIME_SYSTEM:
  798. if (state == VTIME_SYS)
  799. *val += vtime->stime + vtime_delta(vtime);
  800. break;
  801. case CPUTIME_USER:
  802. if (task_nice(tsk) <= 0)
  803. *val += kcpustat_user_vtime(vtime);
  804. break;
  805. case CPUTIME_NICE:
  806. if (task_nice(tsk) > 0)
  807. *val += kcpustat_user_vtime(vtime);
  808. break;
  809. case CPUTIME_GUEST:
  810. if (state == VTIME_GUEST && task_nice(tsk) <= 0)
  811. *val += vtime->gtime + vtime_delta(vtime);
  812. break;
  813. case CPUTIME_GUEST_NICE:
  814. if (state == VTIME_GUEST && task_nice(tsk) > 0)
  815. *val += vtime->gtime + vtime_delta(vtime);
  816. break;
  817. default:
  818. break;
  819. }
  820. } while (read_seqcount_retry(&vtime->seqcount, seq));
  821. return 0;
  822. }
  823. u64 kcpustat_field(struct kernel_cpustat *kcpustat,
  824. enum cpu_usage_stat usage, int cpu)
  825. {
  826. u64 *cpustat = kcpustat->cpustat;
  827. u64 val = cpustat[usage];
  828. struct rq *rq;
  829. int err;
  830. if (!vtime_accounting_enabled_cpu(cpu))
  831. return val;
  832. rq = cpu_rq(cpu);
  833. for (;;) {
  834. struct task_struct *curr;
  835. rcu_read_lock();
  836. curr = rcu_dereference(rq->curr);
  837. if (WARN_ON_ONCE(!curr)) {
  838. rcu_read_unlock();
  839. return cpustat[usage];
  840. }
  841. err = kcpustat_field_vtime(cpustat, curr, usage, cpu, &val);
  842. rcu_read_unlock();
  843. if (!err)
  844. return val;
  845. cpu_relax();
  846. }
  847. }
  848. EXPORT_SYMBOL_GPL(kcpustat_field);
  849. static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
  850. const struct kernel_cpustat *src,
  851. struct task_struct *tsk, int cpu)
  852. {
  853. struct vtime *vtime = &tsk->vtime;
  854. unsigned int seq;
  855. do {
  856. u64 *cpustat;
  857. u64 delta;
  858. int state;
  859. seq = read_seqcount_begin(&vtime->seqcount);
  860. state = vtime_state_fetch(vtime, cpu);
  861. if (state < 0)
  862. return state;
  863. *dst = *src;
  864. cpustat = dst->cpustat;
  865. /* Task is sleeping, dead or idle, nothing to add */
  866. if (state < VTIME_SYS)
  867. continue;
  868. delta = vtime_delta(vtime);
  869. /*
  870. * Task runs either in user (including guest) or kernel space,
  871. * add pending nohz time to the right place.
  872. */
  873. if (state == VTIME_SYS) {
  874. cpustat[CPUTIME_SYSTEM] += vtime->stime + delta;
  875. } else if (state == VTIME_USER) {
  876. if (task_nice(tsk) > 0)
  877. cpustat[CPUTIME_NICE] += vtime->utime + delta;
  878. else
  879. cpustat[CPUTIME_USER] += vtime->utime + delta;
  880. } else {
  881. WARN_ON_ONCE(state != VTIME_GUEST);
  882. if (task_nice(tsk) > 0) {
  883. cpustat[CPUTIME_GUEST_NICE] += vtime->gtime + delta;
  884. cpustat[CPUTIME_NICE] += vtime->gtime + delta;
  885. } else {
  886. cpustat[CPUTIME_GUEST] += vtime->gtime + delta;
  887. cpustat[CPUTIME_USER] += vtime->gtime + delta;
  888. }
  889. }
  890. } while (read_seqcount_retry(&vtime->seqcount, seq));
  891. return 0;
  892. }
  893. void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
  894. {
  895. const struct kernel_cpustat *src = &kcpustat_cpu(cpu);
  896. struct rq *rq;
  897. int err;
  898. if (!vtime_accounting_enabled_cpu(cpu)) {
  899. *dst = *src;
  900. return;
  901. }
  902. rq = cpu_rq(cpu);
  903. for (;;) {
  904. struct task_struct *curr;
  905. rcu_read_lock();
  906. curr = rcu_dereference(rq->curr);
  907. if (WARN_ON_ONCE(!curr)) {
  908. rcu_read_unlock();
  909. *dst = *src;
  910. return;
  911. }
  912. err = kcpustat_cpu_fetch_vtime(dst, src, curr, cpu);
  913. rcu_read_unlock();
  914. if (!err)
  915. return;
  916. cpu_relax();
  917. }
  918. }
  919. EXPORT_SYMBOL_GPL(kcpustat_cpu_fetch);
  920. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */