cputime.c 27 KB

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