1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/cgroup.h>
3 #include <linux/slab.h>
4 #include <linux/percpu.h>
5 #include <linux/spinlock.h>
6 #include <linux/cpumask.h>
7 #include <linux/seq_file.h>
8 #include <linux/rcupdate.h>
9 #include <linux/kernel_stat.h>
10 #include <linux/err.h>
15 * CPU accounting code for task groups.
17 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
18 * (balbir@in.ibm.com).
21 /* Time spent by the tasks of the cpu accounting group executing in ... */
22 enum cpuacct_stat_index {
23 CPUACCT_STAT_USER, /* ... user mode */
24 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
29 static const char * const cpuacct_stat_desc[] = {
30 [CPUACCT_STAT_USER] = "user",
31 [CPUACCT_STAT_SYSTEM] = "system",
34 struct cpuacct_usage {
35 u64 usages[CPUACCT_STAT_NSTATS];
38 /* track cpu usage of a group of tasks and its child groups */
40 struct cgroup_subsys_state css;
41 /* cpuusage holds pointer to a u64-type object on every cpu */
42 struct cpuacct_usage __percpu *cpuusage;
43 struct kernel_cpustat __percpu *cpustat;
46 static inline struct cpuacct *css_ca(struct cgroup_subsys_state *css)
48 return css ? container_of(css, struct cpuacct, css) : NULL;
51 /* return cpu accounting group to which this task belongs */
52 static inline struct cpuacct *task_ca(struct task_struct *tsk)
54 return css_ca(task_css(tsk, cpuacct_cgrp_id));
57 static inline struct cpuacct *parent_ca(struct cpuacct *ca)
59 return css_ca(ca->css.parent);
62 static DEFINE_PER_CPU(struct cpuacct_usage, root_cpuacct_cpuusage);
63 static struct cpuacct root_cpuacct = {
64 .cpustat = &kernel_cpustat,
65 .cpuusage = &root_cpuacct_cpuusage,
68 /* create a new cpu accounting group */
69 static struct cgroup_subsys_state *
70 cpuacct_css_alloc(struct cgroup_subsys_state *parent_css)
75 return &root_cpuacct.css;
77 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
81 ca->cpuusage = alloc_percpu(struct cpuacct_usage);
85 ca->cpustat = alloc_percpu(struct kernel_cpustat);
87 goto out_free_cpuusage;
92 free_percpu(ca->cpuusage);
96 return ERR_PTR(-ENOMEM);
99 /* destroy an existing cpu accounting group */
100 static void cpuacct_css_free(struct cgroup_subsys_state *css)
102 struct cpuacct *ca = css_ca(css);
104 free_percpu(ca->cpustat);
105 free_percpu(ca->cpuusage);
109 static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu,
110 enum cpuacct_stat_index index)
112 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
116 * We allow index == CPUACCT_STAT_NSTATS here to read
119 BUG_ON(index > CPUACCT_STAT_NSTATS);
123 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
125 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
128 if (index == CPUACCT_STAT_NSTATS) {
132 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
133 data += cpuusage->usages[i];
135 data = cpuusage->usages[index];
139 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
145 static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
147 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
152 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
154 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
157 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
158 cpuusage->usages[i] = val;
161 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
165 /* return total cpu usage (in nanoseconds) of a group */
166 static u64 __cpuusage_read(struct cgroup_subsys_state *css,
167 enum cpuacct_stat_index index)
169 struct cpuacct *ca = css_ca(css);
170 u64 totalcpuusage = 0;
173 for_each_possible_cpu(i)
174 totalcpuusage += cpuacct_cpuusage_read(ca, i, index);
176 return totalcpuusage;
179 static u64 cpuusage_user_read(struct cgroup_subsys_state *css,
182 return __cpuusage_read(css, CPUACCT_STAT_USER);
185 static u64 cpuusage_sys_read(struct cgroup_subsys_state *css,
188 return __cpuusage_read(css, CPUACCT_STAT_SYSTEM);
191 static u64 cpuusage_read(struct cgroup_subsys_state *css, struct cftype *cft)
193 return __cpuusage_read(css, CPUACCT_STAT_NSTATS);
196 static int cpuusage_write(struct cgroup_subsys_state *css, struct cftype *cft,
199 struct cpuacct *ca = css_ca(css);
203 * Only allow '0' here to do a reset.
208 for_each_possible_cpu(cpu)
209 cpuacct_cpuusage_write(ca, cpu, 0);
214 static int __cpuacct_percpu_seq_show(struct seq_file *m,
215 enum cpuacct_stat_index index)
217 struct cpuacct *ca = css_ca(seq_css(m));
221 for_each_possible_cpu(i) {
222 percpu = cpuacct_cpuusage_read(ca, i, index);
223 seq_printf(m, "%llu ", (unsigned long long) percpu);
229 static int cpuacct_percpu_user_seq_show(struct seq_file *m, void *V)
231 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_USER);
234 static int cpuacct_percpu_sys_seq_show(struct seq_file *m, void *V)
236 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_SYSTEM);
239 static int cpuacct_percpu_seq_show(struct seq_file *m, void *V)
241 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_NSTATS);
244 static int cpuacct_all_seq_show(struct seq_file *m, void *V)
246 struct cpuacct *ca = css_ca(seq_css(m));
251 for (index = 0; index < CPUACCT_STAT_NSTATS; index++)
252 seq_printf(m, " %s", cpuacct_stat_desc[index]);
255 for_each_possible_cpu(cpu) {
256 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
258 seq_printf(m, "%d", cpu);
260 for (index = 0; index < CPUACCT_STAT_NSTATS; index++) {
263 * Take rq->lock to make 64-bit read safe on 32-bit
266 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
269 seq_printf(m, " %llu", cpuusage->usages[index]);
272 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
280 static int cpuacct_stats_show(struct seq_file *sf, void *v)
282 struct cpuacct *ca = css_ca(seq_css(sf));
283 s64 val[CPUACCT_STAT_NSTATS];
287 memset(val, 0, sizeof(val));
288 for_each_possible_cpu(cpu) {
289 u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
291 val[CPUACCT_STAT_USER] += cpustat[CPUTIME_USER];
292 val[CPUACCT_STAT_USER] += cpustat[CPUTIME_NICE];
293 val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SYSTEM];
294 val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_IRQ];
295 val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SOFTIRQ];
298 for (stat = 0; stat < CPUACCT_STAT_NSTATS; stat++) {
299 seq_printf(sf, "%s %lld\n",
300 cpuacct_stat_desc[stat],
301 (long long)nsec_to_clock_t(val[stat]));
307 static struct cftype files[] = {
310 .read_u64 = cpuusage_read,
311 .write_u64 = cpuusage_write,
314 .name = "usage_user",
315 .read_u64 = cpuusage_user_read,
319 .read_u64 = cpuusage_sys_read,
322 .name = "usage_percpu",
323 .seq_show = cpuacct_percpu_seq_show,
326 .name = "usage_percpu_user",
327 .seq_show = cpuacct_percpu_user_seq_show,
330 .name = "usage_percpu_sys",
331 .seq_show = cpuacct_percpu_sys_seq_show,
335 .seq_show = cpuacct_all_seq_show,
339 .seq_show = cpuacct_stats_show,
345 * charge this task's execution time to its accounting group.
347 * called with rq->lock held.
349 void cpuacct_charge(struct task_struct *tsk, u64 cputime)
352 int index = CPUACCT_STAT_SYSTEM;
353 struct pt_regs *regs = task_pt_regs(tsk);
355 if (regs && user_mode(regs))
356 index = CPUACCT_STAT_USER;
360 for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
361 this_cpu_ptr(ca->cpuusage)->usages[index] += cputime;
367 * Add user/system time to cpuacct.
369 * Note: it's the caller that updates the account of the root cgroup.
371 void cpuacct_account_field(struct task_struct *tsk, int index, u64 val)
376 for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
377 this_cpu_ptr(ca->cpustat)->cpustat[index] += val;
381 struct cgroup_subsys cpuacct_cgrp_subsys = {
382 .css_alloc = cpuacct_css_alloc,
383 .css_free = cpuacct_css_free,
384 .legacy_cftypes = files,