1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 << 3 /* 2 /* 4 * CPU accounting code for task groups. 3 * CPU accounting code for task groups. 5 * 4 * 6 * Based on the work by Paul Menage (menage@go 5 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh 7 * (balbir@in.ibm.com). 6 * (balbir@in.ibm.com). 8 */ 7 */ >> 8 #include <asm/irq_regs.h> >> 9 #include "sched.h" 9 10 10 /* Time spent by the tasks of the CPU accounti 11 /* Time spent by the tasks of the CPU accounting group executing in ... */ 11 enum cpuacct_stat_index { 12 enum cpuacct_stat_index { 12 CPUACCT_STAT_USER, /* ... user mo 13 CPUACCT_STAT_USER, /* ... user mode */ 13 CPUACCT_STAT_SYSTEM, /* ... kernel 14 CPUACCT_STAT_SYSTEM, /* ... kernel mode */ 14 15 15 CPUACCT_STAT_NSTATS, 16 CPUACCT_STAT_NSTATS, 16 }; 17 }; 17 18 18 static const char * const cpuacct_stat_desc[] 19 static const char * const cpuacct_stat_desc[] = { 19 [CPUACCT_STAT_USER] = "user", 20 [CPUACCT_STAT_USER] = "user", 20 [CPUACCT_STAT_SYSTEM] = "system", 21 [CPUACCT_STAT_SYSTEM] = "system", 21 }; 22 }; 22 23 >> 24 struct cpuacct_usage { >> 25 u64 usages[CPUACCT_STAT_NSTATS]; >> 26 }; >> 27 23 /* track CPU usage of a group of tasks and its 28 /* track CPU usage of a group of tasks and its child groups */ 24 struct cpuacct { 29 struct cpuacct { 25 struct cgroup_subsys_state css; 30 struct cgroup_subsys_state css; 26 /* cpuusage holds pointer to a u64-typ 31 /* cpuusage holds pointer to a u64-type object on every CPU */ 27 u64 __percpu *cpuusage; !! 32 struct cpuacct_usage __percpu *cpuusage; 28 struct kernel_cpustat __percpu *cpust 33 struct kernel_cpustat __percpu *cpustat; 29 }; 34 }; 30 35 31 static inline struct cpuacct *css_ca(struct cg 36 static inline struct cpuacct *css_ca(struct cgroup_subsys_state *css) 32 { 37 { 33 return css ? container_of(css, struct 38 return css ? container_of(css, struct cpuacct, css) : NULL; 34 } 39 } 35 40 36 /* Return CPU accounting group to which this t 41 /* Return CPU accounting group to which this task belongs */ 37 static inline struct cpuacct *task_ca(struct t 42 static inline struct cpuacct *task_ca(struct task_struct *tsk) 38 { 43 { 39 return css_ca(task_css(tsk, cpuacct_cg 44 return css_ca(task_css(tsk, cpuacct_cgrp_id)); 40 } 45 } 41 46 42 static inline struct cpuacct *parent_ca(struct 47 static inline struct cpuacct *parent_ca(struct cpuacct *ca) 43 { 48 { 44 return css_ca(ca->css.parent); 49 return css_ca(ca->css.parent); 45 } 50 } 46 51 47 static DEFINE_PER_CPU(u64, root_cpuacct_cpuusa !! 52 static DEFINE_PER_CPU(struct cpuacct_usage, root_cpuacct_cpuusage); 48 static struct cpuacct root_cpuacct = { 53 static struct cpuacct root_cpuacct = { 49 .cpustat = &kernel_cpustat, 54 .cpustat = &kernel_cpustat, 50 .cpuusage = &root_cpuacct_cpuusa 55 .cpuusage = &root_cpuacct_cpuusage, 51 }; 56 }; 52 57 53 /* Create a new CPU accounting group */ 58 /* Create a new CPU accounting group */ 54 static struct cgroup_subsys_state * 59 static struct cgroup_subsys_state * 55 cpuacct_css_alloc(struct cgroup_subsys_state * 60 cpuacct_css_alloc(struct cgroup_subsys_state *parent_css) 56 { 61 { 57 struct cpuacct *ca; 62 struct cpuacct *ca; 58 63 59 if (!parent_css) 64 if (!parent_css) 60 return &root_cpuacct.css; 65 return &root_cpuacct.css; 61 66 62 ca = kzalloc(sizeof(*ca), GFP_KERNEL); 67 ca = kzalloc(sizeof(*ca), GFP_KERNEL); 63 if (!ca) 68 if (!ca) 64 goto out; 69 goto out; 65 70 66 ca->cpuusage = alloc_percpu(u64); !! 71 ca->cpuusage = alloc_percpu(struct cpuacct_usage); 67 if (!ca->cpuusage) 72 if (!ca->cpuusage) 68 goto out_free_ca; 73 goto out_free_ca; 69 74 70 ca->cpustat = alloc_percpu(struct kern 75 ca->cpustat = alloc_percpu(struct kernel_cpustat); 71 if (!ca->cpustat) 76 if (!ca->cpustat) 72 goto out_free_cpuusage; 77 goto out_free_cpuusage; 73 78 74 return &ca->css; 79 return &ca->css; 75 80 76 out_free_cpuusage: 81 out_free_cpuusage: 77 free_percpu(ca->cpuusage); 82 free_percpu(ca->cpuusage); 78 out_free_ca: 83 out_free_ca: 79 kfree(ca); 84 kfree(ca); 80 out: 85 out: 81 return ERR_PTR(-ENOMEM); 86 return ERR_PTR(-ENOMEM); 82 } 87 } 83 88 84 /* Destroy an existing CPU accounting group */ 89 /* Destroy an existing CPU accounting group */ 85 static void cpuacct_css_free(struct cgroup_sub 90 static void cpuacct_css_free(struct cgroup_subsys_state *css) 86 { 91 { 87 struct cpuacct *ca = css_ca(css); 92 struct cpuacct *ca = css_ca(css); 88 93 89 free_percpu(ca->cpustat); 94 free_percpu(ca->cpustat); 90 free_percpu(ca->cpuusage); 95 free_percpu(ca->cpuusage); 91 kfree(ca); 96 kfree(ca); 92 } 97 } 93 98 94 static u64 cpuacct_cpuusage_read(struct cpuacc 99 static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu, 95 enum cpuacct_ 100 enum cpuacct_stat_index index) 96 { 101 { 97 u64 *cpuusage = per_cpu_ptr(ca->cpuusa !! 102 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); 98 u64 *cpustat = per_cpu_ptr(ca->cpustat << 99 u64 data; 103 u64 data; 100 104 101 /* 105 /* 102 * We allow index == CPUACCT_STAT_NSTA 106 * We allow index == CPUACCT_STAT_NSTATS here to read 103 * the sum of usages. 107 * the sum of usages. 104 */ 108 */ 105 if (WARN_ON_ONCE(index > CPUACCT_STAT_ !! 109 BUG_ON(index > CPUACCT_STAT_NSTATS); 106 return 0; << 107 110 108 #ifndef CONFIG_64BIT 111 #ifndef CONFIG_64BIT 109 /* 112 /* 110 * Take rq->lock to make 64-bit read s 113 * Take rq->lock to make 64-bit read safe on 32-bit platforms. 111 */ 114 */ 112 raw_spin_rq_lock_irq(cpu_rq(cpu)); !! 115 raw_spin_lock_irq(&cpu_rq(cpu)->lock); 113 #endif 116 #endif 114 117 115 switch (index) { !! 118 if (index == CPUACCT_STAT_NSTATS) { 116 case CPUACCT_STAT_USER: !! 119 int i = 0; 117 data = cpustat[CPUTIME_USER] + !! 120 118 break; !! 121 data = 0; 119 case CPUACCT_STAT_SYSTEM: !! 122 for (i = 0; i < CPUACCT_STAT_NSTATS; i++) 120 data = cpustat[CPUTIME_SYSTEM] !! 123 data += cpuusage->usages[i]; 121 cpustat[CPUTIME_SOFTIR !! 124 } else { 122 break; !! 125 data = cpuusage->usages[index]; 123 case CPUACCT_STAT_NSTATS: << 124 data = *cpuusage; << 125 break; << 126 } 126 } 127 127 128 #ifndef CONFIG_64BIT 128 #ifndef CONFIG_64BIT 129 raw_spin_rq_unlock_irq(cpu_rq(cpu)); !! 129 raw_spin_unlock_irq(&cpu_rq(cpu)->lock); 130 #endif 130 #endif 131 131 132 return data; 132 return data; 133 } 133 } 134 134 135 static void cpuacct_cpuusage_write(struct cpua !! 135 static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val) 136 { 136 { 137 u64 *cpuusage = per_cpu_ptr(ca->cpuusa !! 137 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); 138 u64 *cpustat = per_cpu_ptr(ca->cpustat !! 138 int i; 139 << 140 /* Don't allow to reset global kernel_ << 141 if (ca == &root_cpuacct) << 142 return; << 143 139 144 #ifndef CONFIG_64BIT 140 #ifndef CONFIG_64BIT 145 /* 141 /* 146 * Take rq->lock to make 64-bit write 142 * Take rq->lock to make 64-bit write safe on 32-bit platforms. 147 */ 143 */ 148 raw_spin_rq_lock_irq(cpu_rq(cpu)); !! 144 raw_spin_lock_irq(&cpu_rq(cpu)->lock); 149 #endif 145 #endif 150 *cpuusage = 0; !! 146 151 cpustat[CPUTIME_USER] = cpustat[CPUTIM !! 147 for (i = 0; i < CPUACCT_STAT_NSTATS; i++) 152 cpustat[CPUTIME_SYSTEM] = cpustat[CPUT !! 148 cpuusage->usages[i] = val; 153 cpustat[CPUTIME_SOFTIRQ] = 0; << 154 149 155 #ifndef CONFIG_64BIT 150 #ifndef CONFIG_64BIT 156 raw_spin_rq_unlock_irq(cpu_rq(cpu)); !! 151 raw_spin_unlock_irq(&cpu_rq(cpu)->lock); 157 #endif 152 #endif 158 } 153 } 159 154 160 /* Return total CPU usage (in nanoseconds) of 155 /* Return total CPU usage (in nanoseconds) of a group */ 161 static u64 __cpuusage_read(struct cgroup_subsy 156 static u64 __cpuusage_read(struct cgroup_subsys_state *css, 162 enum cpuacct_stat_i 157 enum cpuacct_stat_index index) 163 { 158 { 164 struct cpuacct *ca = css_ca(css); 159 struct cpuacct *ca = css_ca(css); 165 u64 totalcpuusage = 0; 160 u64 totalcpuusage = 0; 166 int i; 161 int i; 167 162 168 for_each_possible_cpu(i) 163 for_each_possible_cpu(i) 169 totalcpuusage += cpuacct_cpuus 164 totalcpuusage += cpuacct_cpuusage_read(ca, i, index); 170 165 171 return totalcpuusage; 166 return totalcpuusage; 172 } 167 } 173 168 174 static u64 cpuusage_user_read(struct cgroup_su 169 static u64 cpuusage_user_read(struct cgroup_subsys_state *css, 175 struct cftype *c 170 struct cftype *cft) 176 { 171 { 177 return __cpuusage_read(css, CPUACCT_ST 172 return __cpuusage_read(css, CPUACCT_STAT_USER); 178 } 173 } 179 174 180 static u64 cpuusage_sys_read(struct cgroup_sub 175 static u64 cpuusage_sys_read(struct cgroup_subsys_state *css, 181 struct cftype *cf 176 struct cftype *cft) 182 { 177 { 183 return __cpuusage_read(css, CPUACCT_ST 178 return __cpuusage_read(css, CPUACCT_STAT_SYSTEM); 184 } 179 } 185 180 186 static u64 cpuusage_read(struct cgroup_subsys_ 181 static u64 cpuusage_read(struct cgroup_subsys_state *css, struct cftype *cft) 187 { 182 { 188 return __cpuusage_read(css, CPUACCT_ST 183 return __cpuusage_read(css, CPUACCT_STAT_NSTATS); 189 } 184 } 190 185 191 static int cpuusage_write(struct cgroup_subsys 186 static int cpuusage_write(struct cgroup_subsys_state *css, struct cftype *cft, 192 u64 val) 187 u64 val) 193 { 188 { 194 struct cpuacct *ca = css_ca(css); 189 struct cpuacct *ca = css_ca(css); 195 int cpu; 190 int cpu; 196 191 197 /* 192 /* 198 * Only allow '' here to do a reset. 193 * Only allow '' here to do a reset. 199 */ 194 */ 200 if (val) 195 if (val) 201 return -EINVAL; 196 return -EINVAL; 202 197 203 for_each_possible_cpu(cpu) 198 for_each_possible_cpu(cpu) 204 cpuacct_cpuusage_write(ca, cpu !! 199 cpuacct_cpuusage_write(ca, cpu, 0); 205 200 206 return 0; 201 return 0; 207 } 202 } 208 203 209 static int __cpuacct_percpu_seq_show(struct se 204 static int __cpuacct_percpu_seq_show(struct seq_file *m, 210 enum cpua 205 enum cpuacct_stat_index index) 211 { 206 { 212 struct cpuacct *ca = css_ca(seq_css(m) 207 struct cpuacct *ca = css_ca(seq_css(m)); 213 u64 percpu; 208 u64 percpu; 214 int i; 209 int i; 215 210 216 for_each_possible_cpu(i) { 211 for_each_possible_cpu(i) { 217 percpu = cpuacct_cpuusage_read 212 percpu = cpuacct_cpuusage_read(ca, i, index); 218 seq_printf(m, "%llu ", (unsign 213 seq_printf(m, "%llu ", (unsigned long long) percpu); 219 } 214 } 220 seq_printf(m, "\n"); 215 seq_printf(m, "\n"); 221 return 0; 216 return 0; 222 } 217 } 223 218 224 static int cpuacct_percpu_user_seq_show(struct 219 static int cpuacct_percpu_user_seq_show(struct seq_file *m, void *V) 225 { 220 { 226 return __cpuacct_percpu_seq_show(m, CP 221 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_USER); 227 } 222 } 228 223 229 static int cpuacct_percpu_sys_seq_show(struct 224 static int cpuacct_percpu_sys_seq_show(struct seq_file *m, void *V) 230 { 225 { 231 return __cpuacct_percpu_seq_show(m, CP 226 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_SYSTEM); 232 } 227 } 233 228 234 static int cpuacct_percpu_seq_show(struct seq_ 229 static int cpuacct_percpu_seq_show(struct seq_file *m, void *V) 235 { 230 { 236 return __cpuacct_percpu_seq_show(m, CP 231 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_NSTATS); 237 } 232 } 238 233 239 static int cpuacct_all_seq_show(struct seq_fil 234 static int cpuacct_all_seq_show(struct seq_file *m, void *V) 240 { 235 { 241 struct cpuacct *ca = css_ca(seq_css(m) 236 struct cpuacct *ca = css_ca(seq_css(m)); 242 int index; 237 int index; 243 int cpu; 238 int cpu; 244 239 245 seq_puts(m, "cpu"); 240 seq_puts(m, "cpu"); 246 for (index = 0; index < CPUACCT_STAT_N 241 for (index = 0; index < CPUACCT_STAT_NSTATS; index++) 247 seq_printf(m, " %s", cpuacct_s 242 seq_printf(m, " %s", cpuacct_stat_desc[index]); 248 seq_puts(m, "\n"); 243 seq_puts(m, "\n"); 249 244 250 for_each_possible_cpu(cpu) { 245 for_each_possible_cpu(cpu) { >> 246 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); >> 247 251 seq_printf(m, "%d", cpu); 248 seq_printf(m, "%d", cpu); 252 for (index = 0; index < CPUACC !! 249 253 seq_printf(m, " %llu", !! 250 for (index = 0; index < CPUACCT_STAT_NSTATS; index++) { 254 cpuacct_cpu !! 251 #ifndef CONFIG_64BIT >> 252 /* >> 253 * Take rq->lock to make 64-bit read safe on 32-bit >> 254 * platforms. >> 255 */ >> 256 raw_spin_lock_irq(&cpu_rq(cpu)->lock); >> 257 #endif >> 258 >> 259 seq_printf(m, " %llu", cpuusage->usages[index]); >> 260 >> 261 #ifndef CONFIG_64BIT >> 262 raw_spin_unlock_irq(&cpu_rq(cpu)->lock); >> 263 #endif >> 264 } 255 seq_puts(m, "\n"); 265 seq_puts(m, "\n"); 256 } 266 } 257 return 0; 267 return 0; 258 } 268 } 259 269 260 static int cpuacct_stats_show(struct seq_file 270 static int cpuacct_stats_show(struct seq_file *sf, void *v) 261 { 271 { 262 struct cpuacct *ca = css_ca(seq_css(sf 272 struct cpuacct *ca = css_ca(seq_css(sf)); 263 struct task_cputime cputime; !! 273 s64 val[CPUACCT_STAT_NSTATS]; 264 u64 val[CPUACCT_STAT_NSTATS]; << 265 int cpu; 274 int cpu; 266 int stat; 275 int stat; 267 276 268 memset(&cputime, 0, sizeof(cputime)); !! 277 memset(val, 0, sizeof(val)); 269 for_each_possible_cpu(cpu) { 278 for_each_possible_cpu(cpu) { 270 u64 *cpustat = per_cpu_ptr(ca- 279 u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat; 271 280 272 cputime.utime += cpustat[CPUTI !! 281 val[CPUACCT_STAT_USER] += cpustat[CPUTIME_USER]; 273 cputime.utime += cpustat[CPUTI !! 282 val[CPUACCT_STAT_USER] += cpustat[CPUTIME_NICE]; 274 cputime.stime += cpustat[CPUTI !! 283 val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SYSTEM]; 275 cputime.stime += cpustat[CPUTI !! 284 val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_IRQ]; 276 cputime.stime += cpustat[CPUTI !! 285 val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SOFTIRQ]; 277 << 278 cputime.sum_exec_runtime += *p << 279 } 286 } 280 287 281 cputime_adjust(&cputime, &seq_css(sf)- << 282 &val[CPUACCT_STAT_USER], &val[ << 283 << 284 for (stat = 0; stat < CPUACCT_STAT_NST 288 for (stat = 0; stat < CPUACCT_STAT_NSTATS; stat++) { 285 seq_printf(sf, "%s %llu\n", cp !! 289 seq_printf(sf, "%s %lld\n", 286 nsec_to_clock_t(val[st !! 290 cpuacct_stat_desc[stat], >> 291 (long long)nsec_to_clock_t(val[stat])); 287 } 292 } 288 293 289 return 0; 294 return 0; 290 } 295 } 291 296 292 static struct cftype files[] = { 297 static struct cftype files[] = { 293 { 298 { 294 .name = "usage", 299 .name = "usage", 295 .read_u64 = cpuusage_read, 300 .read_u64 = cpuusage_read, 296 .write_u64 = cpuusage_write, 301 .write_u64 = cpuusage_write, 297 }, 302 }, 298 { 303 { 299 .name = "usage_user", 304 .name = "usage_user", 300 .read_u64 = cpuusage_user_read 305 .read_u64 = cpuusage_user_read, 301 }, 306 }, 302 { 307 { 303 .name = "usage_sys", 308 .name = "usage_sys", 304 .read_u64 = cpuusage_sys_read, 309 .read_u64 = cpuusage_sys_read, 305 }, 310 }, 306 { 311 { 307 .name = "usage_percpu", 312 .name = "usage_percpu", 308 .seq_show = cpuacct_percpu_seq 313 .seq_show = cpuacct_percpu_seq_show, 309 }, 314 }, 310 { 315 { 311 .name = "usage_percpu_user", 316 .name = "usage_percpu_user", 312 .seq_show = cpuacct_percpu_use 317 .seq_show = cpuacct_percpu_user_seq_show, 313 }, 318 }, 314 { 319 { 315 .name = "usage_percpu_sys", 320 .name = "usage_percpu_sys", 316 .seq_show = cpuacct_percpu_sys 321 .seq_show = cpuacct_percpu_sys_seq_show, 317 }, 322 }, 318 { 323 { 319 .name = "usage_all", 324 .name = "usage_all", 320 .seq_show = cpuacct_all_seq_sh 325 .seq_show = cpuacct_all_seq_show, 321 }, 326 }, 322 { 327 { 323 .name = "stat", 328 .name = "stat", 324 .seq_show = cpuacct_stats_show 329 .seq_show = cpuacct_stats_show, 325 }, 330 }, 326 { } /* terminate */ 331 { } /* terminate */ 327 }; 332 }; 328 333 329 /* 334 /* 330 * charge this task's execution time to its ac 335 * charge this task's execution time to its accounting group. 331 * 336 * 332 * called with rq->lock held. 337 * called with rq->lock held. 333 */ 338 */ 334 void cpuacct_charge(struct task_struct *tsk, u 339 void cpuacct_charge(struct task_struct *tsk, u64 cputime) 335 { 340 { 336 unsigned int cpu = task_cpu(tsk); << 337 struct cpuacct *ca; 341 struct cpuacct *ca; >> 342 int index = CPUACCT_STAT_SYSTEM; >> 343 struct pt_regs *regs = get_irq_regs() ? : task_pt_regs(tsk); 338 344 339 lockdep_assert_rq_held(cpu_rq(cpu)); !! 345 if (regs && user_mode(regs)) >> 346 index = CPUACCT_STAT_USER; >> 347 >> 348 rcu_read_lock(); 340 349 341 for (ca = task_ca(tsk); ca; ca = paren 350 for (ca = task_ca(tsk); ca; ca = parent_ca(ca)) 342 *per_cpu_ptr(ca->cpuusage, cpu !! 351 __this_cpu_add(ca->cpuusage->usages[index], cputime); >> 352 >> 353 rcu_read_unlock(); 343 } 354 } 344 355 345 /* 356 /* 346 * Add user/system time to cpuacct. 357 * Add user/system time to cpuacct. 347 * 358 * 348 * Note: it's the caller that updates the acco 359 * Note: it's the caller that updates the account of the root cgroup. 349 */ 360 */ 350 void cpuacct_account_field(struct task_struct 361 void cpuacct_account_field(struct task_struct *tsk, int index, u64 val) 351 { 362 { 352 struct cpuacct *ca; 363 struct cpuacct *ca; 353 364 >> 365 rcu_read_lock(); 354 for (ca = task_ca(tsk); ca != &root_cp 366 for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca)) 355 __this_cpu_add(ca->cpustat->cp 367 __this_cpu_add(ca->cpustat->cpustat[index], val); >> 368 rcu_read_unlock(); 356 } 369 } 357 370 358 struct cgroup_subsys cpuacct_cgrp_subsys = { 371 struct cgroup_subsys cpuacct_cgrp_subsys = { 359 .css_alloc = cpuacct_css_alloc, 372 .css_alloc = cpuacct_css_alloc, 360 .css_free = cpuacct_css_free, 373 .css_free = cpuacct_css_free, 361 .legacy_cftypes = files, 374 .legacy_cftypes = files, 362 .early_init = true, 375 .early_init = true, 363 }; 376 }; 364 377
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.