cputime.c 23 KB

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