blob: 1abd325e733addd71f37a46577c226369004ee43 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Li Zefan2e76c242013-03-29 14:36:31 +08002#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>
Ingo Molnarb329fd52013-04-10 15:10:50 +020010#include <linux/err.h>
Li Zefan2e76c242013-03-29 14:36:31 +080011
12#include "sched.h"
13
14/*
15 * CPU accounting code for task groups.
16 *
17 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
18 * (balbir@in.ibm.com).
19 */
20
Ingo Molnar97fb7a02018-03-03 14:01:12 +010021/* Time spent by the tasks of the CPU accounting group executing in ... */
Li Zefand1712792013-03-29 14:38:13 +080022enum cpuacct_stat_index {
23 CPUACCT_STAT_USER, /* ... user mode */
24 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
25
26 CPUACCT_STAT_NSTATS,
27};
28
Zhao Lei9acacc22016-06-20 17:37:18 +080029static const char * const cpuacct_stat_desc[] = {
30 [CPUACCT_STAT_USER] = "user",
31 [CPUACCT_STAT_SYSTEM] = "system",
Dongsheng Yangd7400372016-03-22 16:37:08 +080032};
33
34struct cpuacct_usage {
Zhao Lei9acacc22016-06-20 17:37:18 +080035 u64 usages[CPUACCT_STAT_NSTATS];
Dongsheng Yangd7400372016-03-22 16:37:08 +080036};
37
Ingo Molnar97fb7a02018-03-03 14:01:12 +010038/* track CPU usage of a group of tasks and its child groups */
Li Zefand1712792013-03-29 14:38:13 +080039struct cpuacct {
Ingo Molnar97fb7a02018-03-03 14:01:12 +010040 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;
Li Zefand1712792013-03-29 14:38:13 +080044};
45
Tejun Heoa7c6d552013-08-08 20:11:23 -040046static inline struct cpuacct *css_ca(struct cgroup_subsys_state *css)
47{
48 return css ? container_of(css, struct cpuacct, css) : NULL;
49}
50
Ingo Molnar97fb7a02018-03-03 14:01:12 +010051/* Return CPU accounting group to which this task belongs */
Li Zefand1712792013-03-29 14:38:13 +080052static inline struct cpuacct *task_ca(struct task_struct *tsk)
53{
Tejun Heo073219e2014-02-08 10:36:58 -050054 return css_ca(task_css(tsk, cpuacct_cgrp_id));
Li Zefand1712792013-03-29 14:38:13 +080055}
56
Li Zefand1712792013-03-29 14:38:13 +080057static inline struct cpuacct *parent_ca(struct cpuacct *ca)
58{
Tejun Heo5c9d5352014-05-16 13:22:48 -040059 return css_ca(ca->css.parent);
Li Zefand1712792013-03-29 14:38:13 +080060}
61
Dongsheng Yangd7400372016-03-22 16:37:08 +080062static DEFINE_PER_CPU(struct cpuacct_usage, root_cpuacct_cpuusage);
Li Zefan14c6d3c2013-03-29 14:44:04 +080063static struct cpuacct root_cpuacct = {
64 .cpustat = &kernel_cpustat,
65 .cpuusage = &root_cpuacct_cpuusage,
66};
Li Zefan2e76c242013-03-29 14:36:31 +080067
Ingo Molnar97fb7a02018-03-03 14:01:12 +010068/* Create a new CPU accounting group */
Tejun Heoeb954192013-08-08 20:11:23 -040069static struct cgroup_subsys_state *
70cpuacct_css_alloc(struct cgroup_subsys_state *parent_css)
Li Zefan2e76c242013-03-29 14:36:31 +080071{
72 struct cpuacct *ca;
73
Tejun Heoeb954192013-08-08 20:11:23 -040074 if (!parent_css)
Li Zefan2e76c242013-03-29 14:36:31 +080075 return &root_cpuacct.css;
76
77 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
78 if (!ca)
79 goto out;
80
Dongsheng Yangd7400372016-03-22 16:37:08 +080081 ca->cpuusage = alloc_percpu(struct cpuacct_usage);
Li Zefan2e76c242013-03-29 14:36:31 +080082 if (!ca->cpuusage)
83 goto out_free_ca;
84
85 ca->cpustat = alloc_percpu(struct kernel_cpustat);
86 if (!ca->cpustat)
87 goto out_free_cpuusage;
88
89 return &ca->css;
90
91out_free_cpuusage:
92 free_percpu(ca->cpuusage);
93out_free_ca:
94 kfree(ca);
95out:
96 return ERR_PTR(-ENOMEM);
97}
98
Ingo Molnar97fb7a02018-03-03 14:01:12 +010099/* Destroy an existing CPU accounting group */
Tejun Heoeb954192013-08-08 20:11:23 -0400100static void cpuacct_css_free(struct cgroup_subsys_state *css)
Li Zefan2e76c242013-03-29 14:36:31 +0800101{
Tejun Heoeb954192013-08-08 20:11:23 -0400102 struct cpuacct *ca = css_ca(css);
Li Zefan2e76c242013-03-29 14:36:31 +0800103
104 free_percpu(ca->cpustat);
105 free_percpu(ca->cpuusage);
106 kfree(ca);
107}
108
Dongsheng Yangd7400372016-03-22 16:37:08 +0800109static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu,
Zhao Lei9acacc22016-06-20 17:37:18 +0800110 enum cpuacct_stat_index index)
Li Zefan2e76c242013-03-29 14:36:31 +0800111{
Dongsheng Yangd7400372016-03-22 16:37:08 +0800112 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Li Zefan2e76c242013-03-29 14:36:31 +0800113 u64 data;
114
Dongsheng Yangd7400372016-03-22 16:37:08 +0800115 /*
Zhao Lei9acacc22016-06-20 17:37:18 +0800116 * We allow index == CPUACCT_STAT_NSTATS here to read
Dongsheng Yangd7400372016-03-22 16:37:08 +0800117 * the sum of suages.
118 */
Zhao Lei9acacc22016-06-20 17:37:18 +0800119 BUG_ON(index > CPUACCT_STAT_NSTATS);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800120
Li Zefan2e76c242013-03-29 14:36:31 +0800121#ifndef CONFIG_64BIT
122 /*
123 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
124 */
125 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800126#endif
127
Zhao Lei9acacc22016-06-20 17:37:18 +0800128 if (index == CPUACCT_STAT_NSTATS) {
Dongsheng Yangd7400372016-03-22 16:37:08 +0800129 int i = 0;
130
131 data = 0;
Zhao Lei9acacc22016-06-20 17:37:18 +0800132 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
Dongsheng Yangd7400372016-03-22 16:37:08 +0800133 data += cpuusage->usages[i];
134 } else {
135 data = cpuusage->usages[index];
136 }
137
138#ifndef CONFIG_64BIT
Li Zefan2e76c242013-03-29 14:36:31 +0800139 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Li Zefan2e76c242013-03-29 14:36:31 +0800140#endif
141
142 return data;
143}
144
145static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
146{
Dongsheng Yangd7400372016-03-22 16:37:08 +0800147 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
148 int i;
Li Zefan2e76c242013-03-29 14:36:31 +0800149
150#ifndef CONFIG_64BIT
151 /*
152 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
153 */
154 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800155#endif
156
Zhao Lei9acacc22016-06-20 17:37:18 +0800157 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
Dongsheng Yangd7400372016-03-22 16:37:08 +0800158 cpuusage->usages[i] = val;
159
160#ifndef CONFIG_64BIT
Li Zefan2e76c242013-03-29 14:36:31 +0800161 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Li Zefan2e76c242013-03-29 14:36:31 +0800162#endif
163}
164
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100165/* Return total CPU usage (in nanoseconds) of a group */
Dongsheng Yangd7400372016-03-22 16:37:08 +0800166static u64 __cpuusage_read(struct cgroup_subsys_state *css,
Zhao Lei9acacc22016-06-20 17:37:18 +0800167 enum cpuacct_stat_index index)
Li Zefan2e76c242013-03-29 14:36:31 +0800168{
Tejun Heo182446d2013-08-08 20:11:24 -0400169 struct cpuacct *ca = css_ca(css);
Li Zefan2e76c242013-03-29 14:36:31 +0800170 u64 totalcpuusage = 0;
171 int i;
172
Zhao Lei5ca37262016-03-22 16:37:07 +0800173 for_each_possible_cpu(i)
Dongsheng Yangd7400372016-03-22 16:37:08 +0800174 totalcpuusage += cpuacct_cpuusage_read(ca, i, index);
Li Zefan2e76c242013-03-29 14:36:31 +0800175
176 return totalcpuusage;
177}
178
Dongsheng Yangd7400372016-03-22 16:37:08 +0800179static u64 cpuusage_user_read(struct cgroup_subsys_state *css,
180 struct cftype *cft)
181{
Zhao Lei9acacc22016-06-20 17:37:18 +0800182 return __cpuusage_read(css, CPUACCT_STAT_USER);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800183}
184
185static u64 cpuusage_sys_read(struct cgroup_subsys_state *css,
186 struct cftype *cft)
187{
Zhao Lei9acacc22016-06-20 17:37:18 +0800188 return __cpuusage_read(css, CPUACCT_STAT_SYSTEM);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800189}
190
191static u64 cpuusage_read(struct cgroup_subsys_state *css, struct cftype *cft)
192{
Zhao Lei9acacc22016-06-20 17:37:18 +0800193 return __cpuusage_read(css, CPUACCT_STAT_NSTATS);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800194}
195
Tejun Heo182446d2013-08-08 20:11:24 -0400196static int cpuusage_write(struct cgroup_subsys_state *css, struct cftype *cft,
Dongsheng Yang1a736b72015-12-21 19:14:42 +0800197 u64 val)
Li Zefan2e76c242013-03-29 14:36:31 +0800198{
Tejun Heo182446d2013-08-08 20:11:24 -0400199 struct cpuacct *ca = css_ca(css);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800200 int cpu;
Li Zefan2e76c242013-03-29 14:36:31 +0800201
Dongsheng Yang1a736b72015-12-21 19:14:42 +0800202 /*
203 * Only allow '0' here to do a reset.
204 */
Dongsheng Yangd7400372016-03-22 16:37:08 +0800205 if (val)
206 return -EINVAL;
Li Zefan2e76c242013-03-29 14:36:31 +0800207
Dongsheng Yangd7400372016-03-22 16:37:08 +0800208 for_each_possible_cpu(cpu)
209 cpuacct_cpuusage_write(ca, cpu, 0);
Li Zefan2e76c242013-03-29 14:36:31 +0800210
Dongsheng Yangd7400372016-03-22 16:37:08 +0800211 return 0;
Li Zefan2e76c242013-03-29 14:36:31 +0800212}
213
Dongsheng Yangd7400372016-03-22 16:37:08 +0800214static int __cpuacct_percpu_seq_show(struct seq_file *m,
Zhao Lei9acacc22016-06-20 17:37:18 +0800215 enum cpuacct_stat_index index)
Li Zefan2e76c242013-03-29 14:36:31 +0800216{
Tejun Heo2da8ca82013-12-05 12:28:04 -0500217 struct cpuacct *ca = css_ca(seq_css(m));
Li Zefan2e76c242013-03-29 14:36:31 +0800218 u64 percpu;
219 int i;
220
Zhao Lei5ca37262016-03-22 16:37:07 +0800221 for_each_possible_cpu(i) {
Dongsheng Yangd7400372016-03-22 16:37:08 +0800222 percpu = cpuacct_cpuusage_read(ca, i, index);
Li Zefan2e76c242013-03-29 14:36:31 +0800223 seq_printf(m, "%llu ", (unsigned long long) percpu);
224 }
225 seq_printf(m, "\n");
226 return 0;
227}
228
Dongsheng Yangd7400372016-03-22 16:37:08 +0800229static int cpuacct_percpu_user_seq_show(struct seq_file *m, void *V)
230{
Zhao Lei9acacc22016-06-20 17:37:18 +0800231 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_USER);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800232}
233
234static int cpuacct_percpu_sys_seq_show(struct seq_file *m, void *V)
235{
Zhao Lei9acacc22016-06-20 17:37:18 +0800236 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_SYSTEM);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800237}
238
239static int cpuacct_percpu_seq_show(struct seq_file *m, void *V)
240{
Zhao Lei9acacc22016-06-20 17:37:18 +0800241 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_NSTATS);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800242}
243
Zhao Lei277a13e2016-06-20 17:37:20 +0800244static int cpuacct_all_seq_show(struct seq_file *m, void *V)
245{
246 struct cpuacct *ca = css_ca(seq_css(m));
247 int index;
248 int cpu;
249
250 seq_puts(m, "cpu");
251 for (index = 0; index < CPUACCT_STAT_NSTATS; index++)
252 seq_printf(m, " %s", cpuacct_stat_desc[index]);
253 seq_puts(m, "\n");
254
255 for_each_possible_cpu(cpu) {
256 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
257
258 seq_printf(m, "%d", cpu);
259
260 for (index = 0; index < CPUACCT_STAT_NSTATS; index++) {
261#ifndef CONFIG_64BIT
262 /*
263 * Take rq->lock to make 64-bit read safe on 32-bit
264 * platforms.
265 */
266 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
267#endif
268
269 seq_printf(m, " %llu", cpuusage->usages[index]);
270
271#ifndef CONFIG_64BIT
272 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
273#endif
274 }
275 seq_puts(m, "\n");
276 }
277 return 0;
278}
279
Tejun Heo2da8ca82013-12-05 12:28:04 -0500280static int cpuacct_stats_show(struct seq_file *sf, void *v)
Li Zefan2e76c242013-03-29 14:36:31 +0800281{
Tejun Heo2da8ca82013-12-05 12:28:04 -0500282 struct cpuacct *ca = css_ca(seq_css(sf));
Zhao Lei8e546bf2016-06-20 17:37:19 +0800283 s64 val[CPUACCT_STAT_NSTATS];
Li Zefan2e76c242013-03-29 14:36:31 +0800284 int cpu;
Zhao Lei8e546bf2016-06-20 17:37:19 +0800285 int stat;
Li Zefan2e76c242013-03-29 14:36:31 +0800286
Zhao Lei8e546bf2016-06-20 17:37:19 +0800287 memset(val, 0, sizeof(val));
Zhao Lei5ca37262016-03-22 16:37:07 +0800288 for_each_possible_cpu(cpu) {
Zhao Lei8e546bf2016-06-20 17:37:19 +0800289 u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
Li Zefan2e76c242013-03-29 14:36:31 +0800290
Zhao Lei8e546bf2016-06-20 17:37:19 +0800291 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];
Li Zefan2e76c242013-03-29 14:36:31 +0800296 }
297
Zhao Lei8e546bf2016-06-20 17:37:19 +0800298 for (stat = 0; stat < CPUACCT_STAT_NSTATS; stat++) {
299 seq_printf(sf, "%s %lld\n",
300 cpuacct_stat_desc[stat],
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100301 (long long)nsec_to_clock_t(val[stat]));
Zhao Lei8e546bf2016-06-20 17:37:19 +0800302 }
Li Zefan2e76c242013-03-29 14:36:31 +0800303
304 return 0;
305}
306
307static struct cftype files[] = {
308 {
309 .name = "usage",
310 .read_u64 = cpuusage_read,
311 .write_u64 = cpuusage_write,
312 },
313 {
Dongsheng Yangd7400372016-03-22 16:37:08 +0800314 .name = "usage_user",
315 .read_u64 = cpuusage_user_read,
316 },
317 {
318 .name = "usage_sys",
319 .read_u64 = cpuusage_sys_read,
320 },
321 {
Li Zefan2e76c242013-03-29 14:36:31 +0800322 .name = "usage_percpu",
Tejun Heo2da8ca82013-12-05 12:28:04 -0500323 .seq_show = cpuacct_percpu_seq_show,
Li Zefan2e76c242013-03-29 14:36:31 +0800324 },
325 {
Dongsheng Yangd7400372016-03-22 16:37:08 +0800326 .name = "usage_percpu_user",
327 .seq_show = cpuacct_percpu_user_seq_show,
328 },
329 {
330 .name = "usage_percpu_sys",
331 .seq_show = cpuacct_percpu_sys_seq_show,
332 },
333 {
Zhao Lei277a13e2016-06-20 17:37:20 +0800334 .name = "usage_all",
335 .seq_show = cpuacct_all_seq_show,
336 },
337 {
Li Zefan2e76c242013-03-29 14:36:31 +0800338 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -0500339 .seq_show = cpuacct_stats_show,
Li Zefan2e76c242013-03-29 14:36:31 +0800340 },
341 { } /* terminate */
342};
343
344/*
345 * charge this task's execution time to its accounting group.
346 *
347 * called with rq->lock held.
348 */
349void cpuacct_charge(struct task_struct *tsk, u64 cputime)
350{
351 struct cpuacct *ca;
Zhao Lei9acacc22016-06-20 17:37:18 +0800352 int index = CPUACCT_STAT_SYSTEM;
Anton Blanchardbd928832016-04-06 21:59:50 +1000353 struct pt_regs *regs = task_pt_regs(tsk);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800354
Anton Blanchardbd928832016-04-06 21:59:50 +1000355 if (regs && user_mode(regs))
Zhao Lei9acacc22016-06-20 17:37:18 +0800356 index = CPUACCT_STAT_USER;
Li Zefan2e76c242013-03-29 14:36:31 +0800357
358 rcu_read_lock();
Dongsheng Yangd7400372016-03-22 16:37:08 +0800359
Zhao Lei73e6aaf2016-03-17 12:19:43 +0800360 for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
Dongsheng Yangd7400372016-03-22 16:37:08 +0800361 this_cpu_ptr(ca->cpuusage)->usages[index] += cputime;
362
Li Zefan2e76c242013-03-29 14:36:31 +0800363 rcu_read_unlock();
364}
365
Li Zefan1966aaf2013-03-29 14:37:06 +0800366/*
367 * Add user/system time to cpuacct.
368 *
369 * Note: it's the caller that updates the account of the root cgroup.
370 */
Zhao Lei73e6aaf2016-03-17 12:19:43 +0800371void cpuacct_account_field(struct task_struct *tsk, int index, u64 val)
Li Zefan1966aaf2013-03-29 14:37:06 +0800372{
Li Zefan1966aaf2013-03-29 14:37:06 +0800373 struct cpuacct *ca;
374
Li Zefan1966aaf2013-03-29 14:37:06 +0800375 rcu_read_lock();
Zhao Lei73e6aaf2016-03-17 12:19:43 +0800376 for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
377 this_cpu_ptr(ca->cpustat)->cpustat[index] += val;
Li Zefan1966aaf2013-03-29 14:37:06 +0800378 rcu_read_unlock();
379}
380
Tejun Heo073219e2014-02-08 10:36:58 -0500381struct cgroup_subsys cpuacct_cgrp_subsys = {
Li Zefan621e2de2013-03-29 14:44:15 +0800382 .css_alloc = cpuacct_css_alloc,
383 .css_free = cpuacct_css_free,
Tejun Heo55779642014-07-15 11:05:09 -0400384 .legacy_cftypes = files,
Tejun Heob38e42e2016-02-23 10:00:50 -0500385 .early_init = true,
Li Zefan2e76c242013-03-29 14:36:31 +0800386};