cputime.c 24 KB

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