cputime.c 26 KB

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