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