blob: 8a5e44236f78d3ba069e0c81ef3304f4be5ba7fd [file] [log] [blame]
Shailabh Nagarc7572492006-07-14 00:24:40 -07001/*
2 * taskstats.c - Export per-task statistics to userland
3 *
4 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
5 * (C) Balbir Singh, IBM Corp. 2006
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/taskstats_kern.h>
Jay Lanf3cef7a2006-09-30 23:28:55 -070021#include <linux/tsacct_kern.h>
Shailabh Nagar6f449932006-07-14 00:24:41 -070022#include <linux/delayacct.h>
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070023#include <linux/cpumask.h>
24#include <linux/percpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070026#include <linux/cgroupstats.h>
27#include <linux/cgroup.h>
28#include <linux/fs.h>
29#include <linux/file.h>
Eric W. Biederman4bd6e322012-02-07 17:56:49 -080030#include <linux/pid_namespace.h>
Shailabh Nagarc7572492006-07-14 00:24:40 -070031#include <net/genetlink.h>
Arun Sharma600634972011-07-26 16:09:06 -070032#include <linux/atomic.h>
Shailabh Nagarc7572492006-07-14 00:24:40 -070033
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070034/*
35 * Maximum length of a cpumask that can be specified in
36 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
37 */
38#define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
39
Vegard Nossumb81f3ea2008-07-25 01:48:55 -070040static DEFINE_PER_CPU(__u32, taskstats_seqnum);
Shailabh Nagarc7572492006-07-14 00:24:40 -070041static int family_registered;
Christoph Lametere18b8902006-12-06 20:33:20 -080042struct kmem_cache *taskstats_cache;
Shailabh Nagarc7572492006-07-14 00:24:40 -070043
Johannes Berg489111e2016-10-24 14:40:03 +020044static struct genl_family family;
Shailabh Nagarc7572492006-07-14 00:24:40 -070045
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000046static const struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = {
Shailabh Nagarc7572492006-07-14 00:24:40 -070047 [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
48 [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070049 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
50 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
51
WANG Cong243d5212016-11-03 09:42:36 -070052/*
53 * We have to use TASKSTATS_CMD_ATTR_MAX here, it is the maxattr in the family.
54 * Make sure they are always aligned.
55 */
56static const struct nla_policy cgroupstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = {
Balbir Singh846c7bb2007-10-18 23:39:44 -070057 [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
58};
59
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070060struct listener {
61 struct list_head list;
62 pid_t pid;
Shailabh Nagarbb129992006-07-14 00:24:47 -070063 char valid;
Shailabh Nagarc7572492006-07-14 00:24:40 -070064};
65
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070066struct listener_list {
67 struct rw_semaphore sem;
68 struct list_head list;
69};
70static DEFINE_PER_CPU(struct listener_list, listener_array);
71
72enum actions {
73 REGISTER,
74 DEREGISTER,
75 CPU_DONT_CARE
76};
Shailabh Nagarc7572492006-07-14 00:24:40 -070077
78static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
Oleg Nesterov371674852006-12-06 20:36:55 -080079 size_t size)
Shailabh Nagarc7572492006-07-14 00:24:40 -070080{
81 struct sk_buff *skb;
82 void *reply;
83
84 /*
85 * If new attributes are added, please revisit this allocation
86 */
Thomas Graf3dabc712006-11-14 19:44:52 -080087 skb = genlmsg_new(size, GFP_KERNEL);
Shailabh Nagarc7572492006-07-14 00:24:40 -070088 if (!skb)
89 return -ENOMEM;
90
91 if (!info) {
Christoph Lametercd85fc52010-12-08 17:42:22 +010092 int seq = this_cpu_inc_return(taskstats_seqnum) - 1;
Shailabh Nagarc7572492006-07-14 00:24:40 -070093
Thomas Graf17c157c2006-11-14 19:46:02 -080094 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070095 } else
Thomas Graf17c157c2006-11-14 19:46:02 -080096 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070097 if (reply == NULL) {
98 nlmsg_free(skb);
99 return -EINVAL;
100 }
101
102 *skbp = skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700103 return 0;
104}
105
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700106/*
107 * Send taskstats data in @skb to listener with nl_pid @pid
108 */
Johannes Berg134e6372009-07-10 09:51:34 +0000109static int send_reply(struct sk_buff *skb, struct genl_info *info)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700110{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700111 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700112 void *reply = genlmsg_data(genlhdr);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700113
Johannes Berg053c0952015-01-16 22:09:00 +0100114 genlmsg_end(skb, reply);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700115
Johannes Berg134e6372009-07-10 09:51:34 +0000116 return genlmsg_reply(skb, info);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700117}
118
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700119/*
120 * Send taskstats data in @skb to listeners registered for @cpu's exit data
121 */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800122static void send_cpu_listeners(struct sk_buff *skb,
123 struct listener_list *listeners)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700124{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700125 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700126 struct listener *s, *tmp;
127 struct sk_buff *skb_next, *skb_cur = skb;
128 void *reply = genlmsg_data(genlhdr);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700129 int rc, delcount = 0;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700130
Johannes Berg053c0952015-01-16 22:09:00 +0100131 genlmsg_end(skb, reply);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700132
133 rc = 0;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700134 down_read(&listeners->sem);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700135 list_for_each_entry(s, &listeners->list, list) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700136 skb_next = NULL;
137 if (!list_is_last(&s->list, &listeners->list)) {
138 skb_next = skb_clone(skb_cur, GFP_KERNEL);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700139 if (!skb_next)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700140 break;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700141 }
Johannes Berg134e6372009-07-10 09:51:34 +0000142 rc = genlmsg_unicast(&init_net, skb_cur, s->pid);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700143 if (rc == -ECONNREFUSED) {
Shailabh Nagarbb129992006-07-14 00:24:47 -0700144 s->valid = 0;
145 delcount++;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700146 }
147 skb_cur = skb_next;
148 }
Shailabh Nagarbb129992006-07-14 00:24:47 -0700149 up_read(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700150
Shailabh Nagard94a0412006-07-30 03:03:11 -0700151 if (skb_cur)
152 nlmsg_free(skb_cur);
153
Shailabh Nagarbb129992006-07-14 00:24:47 -0700154 if (!delcount)
Shailabh Nagard94a0412006-07-30 03:03:11 -0700155 return;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700156
157 /* Delete invalidated entries */
158 down_write(&listeners->sem);
159 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
160 if (!s->valid) {
161 list_del(&s->list);
162 kfree(s);
163 }
164 }
165 up_write(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700166}
167
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800168static void fill_stats(struct user_namespace *user_ns,
169 struct pid_namespace *pid_ns,
170 struct task_struct *tsk, struct taskstats *stats)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700171{
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800172 memset(stats, 0, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700173 /*
174 * Each accounting subsystem adds calls to its functions to
175 * fill in relevant parts of struct taskstsats as follows
176 *
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700177 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700178 */
179
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700180 delayacct_add_tsk(stats, tsk);
Jay Lanf3cef7a2006-09-30 23:28:55 -0700181
182 /* fill in basic acct fields */
Shailabh Nagar6f449932006-07-14 00:24:41 -0700183 stats->version = TASKSTATS_VERSION;
Maxim Uvarovb663a792007-07-15 23:40:48 -0700184 stats->nvcsw = tsk->nvcsw;
185 stats->nivcsw = tsk->nivcsw;
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800186 bacct_add_tsk(user_ns, pid_ns, stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700187
Jay Lan9acc1852006-09-30 23:28:58 -0700188 /* fill in extended acct fields */
189 xacct_add_tsk(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700190}
191
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700192static int fill_stats_for_pid(pid_t pid, struct taskstats *stats)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700193{
Oleg Nesterova98b6092006-10-28 10:38:54 -0700194 struct task_struct *tsk;
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700195
196 rcu_read_lock();
197 tsk = find_task_by_vpid(pid);
198 if (tsk)
199 get_task_struct(tsk);
200 rcu_read_unlock();
201 if (!tsk)
202 return -ESRCH;
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800203 fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats);
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700204 put_task_struct(tsk);
205 return 0;
206}
207
208static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
209{
210 struct task_struct *tsk, *first;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700211 unsigned long flags;
Oleg Nesterova98b6092006-10-28 10:38:54 -0700212 int rc = -ESRCH;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700213
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700214 /*
215 * Add additional stats from live tasks except zombie thread group
216 * leaders who are already counted with the dead tasks
217 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700218 rcu_read_lock();
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700219 first = find_task_by_vpid(tgid);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700220
Oleg Nesterova98b6092006-10-28 10:38:54 -0700221 if (!first || !lock_task_sighand(first, &flags))
222 goto out;
223
224 if (first->signal->stats)
225 memcpy(stats, first->signal->stats, sizeof(*stats));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800226 else
227 memset(stats, 0, sizeof(*stats));
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700228
Shailabh Nagarc7572492006-07-14 00:24:40 -0700229 tsk = first;
230 do {
Oleg Nesterovd7c3f5f2006-10-28 10:38:54 -0700231 if (tsk->exit_state)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700232 continue;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700233 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700234 * Accounting subsystem can call its functions here to
Shailabh Nagarc7572492006-07-14 00:24:40 -0700235 * fill in relevant parts of struct taskstsats as follows
236 *
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700237 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700238 */
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700239 delayacct_add_tsk(stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700240
Maxim Uvarovb663a792007-07-15 23:40:48 -0700241 stats->nvcsw += tsk->nvcsw;
242 stats->nivcsw += tsk->nivcsw;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700243 } while_each_thread(first, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700244
Oleg Nesterova98b6092006-10-28 10:38:54 -0700245 unlock_task_sighand(first, &flags);
246 rc = 0;
247out:
248 rcu_read_unlock();
249
250 stats->version = TASKSTATS_VERSION;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700251 /*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200252 * Accounting subsystems can also add calls here to modify
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700253 * fields of taskstats.
Shailabh Nagarc7572492006-07-14 00:24:40 -0700254 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700255 return rc;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700256}
257
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700258static void fill_tgid_exit(struct task_struct *tsk)
259{
260 unsigned long flags;
261
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700262 spin_lock_irqsave(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700263 if (!tsk->signal->stats)
264 goto ret;
265
266 /*
267 * Each accounting subsystem calls its functions here to
268 * accumalate its per-task stats for tsk, into the per-tgid structure
269 *
270 * per-task-foo(tsk->signal->stats, tsk);
271 */
272 delayacct_add_tsk(tsk->signal->stats, tsk);
273ret:
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700274 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700275 return;
276}
277
Rusty Russell41c7bb92009-01-01 10:12:28 +1030278static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700279{
280 struct listener_list *listeners;
Vasiliy Kulikov26c4cae2011-06-27 16:18:11 -0700281 struct listener *s, *tmp, *s2;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700282 unsigned int cpu;
Chen Gang0d206332013-11-12 15:11:23 -0800283 int ret = 0;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700284
Rusty Russell41c7bb92009-01-01 10:12:28 +1030285 if (!cpumask_subset(mask, cpu_possible_mask))
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700286 return -EINVAL;
287
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800288 if (current_user_ns() != &init_user_ns)
289 return -EINVAL;
290
291 if (task_active_pid_ns(current) != &init_pid_ns)
292 return -EINVAL;
293
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700294 if (isadd == REGISTER) {
Rusty Russell41c7bb92009-01-01 10:12:28 +1030295 for_each_cpu(cpu, mask) {
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700296 s = kmalloc_node(sizeof(struct listener),
297 GFP_KERNEL, cpu_to_node(cpu));
Chen Gang0d206332013-11-12 15:11:23 -0800298 if (!s) {
299 ret = -ENOMEM;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700300 goto cleanup;
Chen Gang0d206332013-11-12 15:11:23 -0800301 }
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700302 s->pid = pid;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700303 s->valid = 1;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700304
305 listeners = &per_cpu(listener_array, cpu);
306 down_write(&listeners->sem);
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700307 list_for_each_entry(s2, &listeners->list, list) {
Oleg Nesterova7295892011-08-03 16:21:05 -0700308 if (s2->pid == pid && s2->valid)
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700309 goto exists;
Vasiliy Kulikov26c4cae2011-06-27 16:18:11 -0700310 }
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700311 list_add(&s->list, &listeners->list);
Vasiliy Kulikov26c4cae2011-06-27 16:18:11 -0700312 s = NULL;
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700313exists:
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700314 up_write(&listeners->sem);
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700315 kfree(s); /* nop if NULL */
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700316 }
317 return 0;
318 }
319
320 /* Deregister or cleanup */
321cleanup:
Rusty Russell41c7bb92009-01-01 10:12:28 +1030322 for_each_cpu(cpu, mask) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700323 listeners = &per_cpu(listener_array, cpu);
324 down_write(&listeners->sem);
325 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
326 if (s->pid == pid) {
327 list_del(&s->list);
328 kfree(s);
329 break;
330 }
331 }
332 up_write(&listeners->sem);
333 }
Chen Gang0d206332013-11-12 15:11:23 -0800334 return ret;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700335}
336
Rusty Russell41c7bb92009-01-01 10:12:28 +1030337static int parse(struct nlattr *na, struct cpumask *mask)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700338{
339 char *data;
340 int len;
341 int ret;
342
343 if (na == NULL)
344 return 1;
345 len = nla_len(na);
346 if (len > TASKSTATS_CPUMASK_MAXLEN)
347 return -E2BIG;
348 if (len < 1)
349 return -EINVAL;
350 data = kmalloc(len, GFP_KERNEL);
351 if (!data)
352 return -ENOMEM;
353 nla_strlcpy(data, na, len);
Rusty Russell29c01772008-12-13 21:20:25 +1030354 ret = cpulist_parse(data, mask);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700355 kfree(data);
356 return ret;
357}
358
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800359static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
Oleg Nesterov68062b82006-12-06 20:36:53 -0800360{
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800361 struct nlattr *na, *ret;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800362 int aggr;
363
Oleg Nesterov371674852006-12-06 20:36:55 -0800364 aggr = (type == TASKSTATS_TYPE_PID)
365 ? TASKSTATS_TYPE_AGGR_PID
366 : TASKSTATS_TYPE_AGGR_TGID;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800367
368 na = nla_nest_start(skb, aggr);
Oleg Nesterov371674852006-12-06 20:36:55 -0800369 if (!na)
370 goto err;
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800371
Chen Gang3fa58262013-11-12 15:11:22 -0800372 if (nla_put(skb, type, sizeof(pid), &pid) < 0) {
373 nla_nest_cancel(skb, na);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800374 goto err;
Chen Gang3fa58262013-11-12 15:11:22 -0800375 }
Nicolas Dichtel80df5542016-04-22 17:31:24 +0200376 ret = nla_reserve_64bit(skb, TASKSTATS_TYPE_STATS,
377 sizeof(struct taskstats), TASKSTATS_TYPE_NULL);
Chen Gang3fa58262013-11-12 15:11:22 -0800378 if (!ret) {
379 nla_nest_cancel(skb, na);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800380 goto err;
Chen Gang3fa58262013-11-12 15:11:22 -0800381 }
Oleg Nesterov68062b82006-12-06 20:36:53 -0800382 nla_nest_end(skb, na);
383
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800384 return nla_data(ret);
385err:
386 return NULL;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800387}
388
Balbir Singh846c7bb2007-10-18 23:39:44 -0700389static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
390{
391 int rc = 0;
392 struct sk_buff *rep_skb;
393 struct cgroupstats *stats;
394 struct nlattr *na;
395 size_t size;
396 u32 fd;
Al Viro2903ff02012-08-28 12:52:22 -0400397 struct fd f;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700398
399 na = info->attrs[CGROUPSTATS_CMD_ATTR_FD];
400 if (!na)
401 return -EINVAL;
402
403 fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]);
Al Viro2903ff02012-08-28 12:52:22 -0400404 f = fdget(fd);
405 if (!f.file)
Adrian Bunkf9615982007-11-14 17:00:37 -0800406 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700407
Adrian Bunkf9615982007-11-14 17:00:37 -0800408 size = nla_total_size(sizeof(struct cgroupstats));
Balbir Singh846c7bb2007-10-18 23:39:44 -0700409
Adrian Bunkf9615982007-11-14 17:00:37 -0800410 rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb,
411 size);
412 if (rc < 0)
413 goto err;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700414
Adrian Bunkf9615982007-11-14 17:00:37 -0800415 na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS,
416 sizeof(struct cgroupstats));
Alan Cox25353b32012-07-30 14:42:49 -0700417 if (na == NULL) {
Jesper Juhl0324b5a2012-10-04 17:16:52 -0700418 nlmsg_free(rep_skb);
Alan Cox25353b32012-07-30 14:42:49 -0700419 rc = -EMSGSIZE;
420 goto err;
421 }
422
Adrian Bunkf9615982007-11-14 17:00:37 -0800423 stats = nla_data(na);
424 memset(stats, 0, sizeof(*stats));
Balbir Singh846c7bb2007-10-18 23:39:44 -0700425
Al Virob5830432014-10-31 01:22:04 -0400426 rc = cgroupstats_build(stats, f.file->f_path.dentry);
Adrian Bunkf9615982007-11-14 17:00:37 -0800427 if (rc < 0) {
428 nlmsg_free(rep_skb);
429 goto err;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700430 }
431
Johannes Berg134e6372009-07-10 09:51:34 +0000432 rc = send_reply(rep_skb, info);
Adrian Bunkf9615982007-11-14 17:00:37 -0800433
Balbir Singh846c7bb2007-10-18 23:39:44 -0700434err:
Al Viro2903ff02012-08-28 12:52:22 -0400435 fdput(f);
Balbir Singh846c7bb2007-10-18 23:39:44 -0700436 return rc;
437}
438
Michael Holzheu93233122010-10-27 15:34:44 -0700439static int cmd_attr_register_cpumask(struct genl_info *info)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700440{
Rusty Russell41c7bb92009-01-01 10:12:28 +1030441 cpumask_var_t mask;
Michael Holzheu93233122010-10-27 15:34:44 -0700442 int rc;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700443
Rusty Russell41c7bb92009-01-01 10:12:28 +1030444 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
445 return -ENOMEM;
Rusty Russell41c7bb92009-01-01 10:12:28 +1030446 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700447 if (rc < 0)
Michael Holzheu93233122010-10-27 15:34:44 -0700448 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000449 rc = add_del_listener(info->snd_portid, mask, REGISTER);
Michael Holzheu93233122010-10-27 15:34:44 -0700450out:
451 free_cpumask_var(mask);
452 return rc;
453}
Rusty Russell41c7bb92009-01-01 10:12:28 +1030454
Michael Holzheu93233122010-10-27 15:34:44 -0700455static int cmd_attr_deregister_cpumask(struct genl_info *info)
456{
457 cpumask_var_t mask;
458 int rc;
459
460 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
461 return -ENOMEM;
Rusty Russell41c7bb92009-01-01 10:12:28 +1030462 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask);
463 if (rc < 0)
Michael Holzheu93233122010-10-27 15:34:44 -0700464 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000465 rc = add_del_listener(info->snd_portid, mask, DEREGISTER);
Michael Holzheu93233122010-10-27 15:34:44 -0700466out:
Rusty Russell41c7bb92009-01-01 10:12:28 +1030467 free_cpumask_var(mask);
Michael Holzheu93233122010-10-27 15:34:44 -0700468 return rc;
469}
Shailabh Nagarc7572492006-07-14 00:24:40 -0700470
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800471static size_t taskstats_packet_size(void)
472{
473 size_t size;
474
475 size = nla_total_size(sizeof(u32)) +
Nicolas Dichtel80df5542016-04-22 17:31:24 +0200476 nla_total_size_64bit(sizeof(struct taskstats)) +
477 nla_total_size(0);
478
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800479 return size;
480}
481
Michael Holzheu93233122010-10-27 15:34:44 -0700482static int cmd_attr_pid(struct genl_info *info)
483{
484 struct taskstats *stats;
485 struct sk_buff *rep_skb;
486 size_t size;
487 u32 pid;
488 int rc;
489
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800490 size = taskstats_packet_size();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700491
Oleg Nesterov371674852006-12-06 20:36:55 -0800492 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700493 if (rc < 0)
494 return rc;
495
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800496 rc = -EINVAL;
Michael Holzheu93233122010-10-27 15:34:44 -0700497 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
498 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
499 if (!stats)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700500 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700501
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700502 rc = fill_stats_for_pid(pid, stats);
Michael Holzheu93233122010-10-27 15:34:44 -0700503 if (rc < 0)
504 goto err;
Johannes Berg134e6372009-07-10 09:51:34 +0000505 return send_reply(rep_skb, info);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700506err:
507 nlmsg_free(rep_skb);
508 return rc;
509}
510
Michael Holzheu93233122010-10-27 15:34:44 -0700511static int cmd_attr_tgid(struct genl_info *info)
512{
513 struct taskstats *stats;
514 struct sk_buff *rep_skb;
515 size_t size;
516 u32 tgid;
517 int rc;
518
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800519 size = taskstats_packet_size();
Michael Holzheu93233122010-10-27 15:34:44 -0700520
521 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
522 if (rc < 0)
523 return rc;
524
525 rc = -EINVAL;
526 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
527 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
528 if (!stats)
529 goto err;
530
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700531 rc = fill_stats_for_tgid(tgid, stats);
Michael Holzheu93233122010-10-27 15:34:44 -0700532 if (rc < 0)
533 goto err;
534 return send_reply(rep_skb, info);
535err:
536 nlmsg_free(rep_skb);
537 return rc;
538}
539
540static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
541{
542 if (info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK])
543 return cmd_attr_register_cpumask(info);
544 else if (info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK])
545 return cmd_attr_deregister_cpumask(info);
546 else if (info->attrs[TASKSTATS_CMD_ATTR_PID])
547 return cmd_attr_pid(info);
548 else if (info->attrs[TASKSTATS_CMD_ATTR_TGID])
549 return cmd_attr_tgid(info);
550 else
551 return -EINVAL;
552}
553
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800554static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
555{
556 struct signal_struct *sig = tsk->signal;
557 struct taskstats *stats;
558
559 if (sig->stats || thread_group_empty(tsk))
560 goto ret;
561
562 /* No problem if kmem_cache_zalloc() fails */
563 stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
564
565 spin_lock_irq(&tsk->sighand->siglock);
566 if (!sig->stats) {
567 sig->stats = stats;
568 stats = NULL;
569 }
570 spin_unlock_irq(&tsk->sighand->siglock);
571
572 if (stats)
573 kmem_cache_free(taskstats_cache, stats);
574ret:
575 return sig->stats;
576}
577
Shailabh Nagarc7572492006-07-14 00:24:40 -0700578/* Send pid data out on exit */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800579void taskstats_exit(struct task_struct *tsk, int group_dead)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700580{
581 int rc;
Oleg Nesterov115085e2006-12-06 20:36:51 -0800582 struct listener_list *listeners;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800583 struct taskstats *stats;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700584 struct sk_buff *rep_skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700585 size_t size;
586 int is_thread_group;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700587
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800588 if (!family_registered)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700589 return;
590
Shailabh Nagarc7572492006-07-14 00:24:40 -0700591 /*
592 * Size includes space for nested attributes
593 */
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800594 size = taskstats_packet_size();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700595
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800596 is_thread_group = !!taskstats_tgid_alloc(tsk);
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800597 if (is_thread_group) {
598 /* PID + STATS + TGID + STATS */
599 size = 2 * size;
600 /* fill the tsk->signal->stats structure */
601 fill_tgid_exit(tsk);
602 }
603
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500604 listeners = raw_cpu_ptr(&listener_array);
Oleg Nesterov115085e2006-12-06 20:36:51 -0800605 if (list_empty(&listeners->list))
606 return;
607
Oleg Nesterov371674852006-12-06 20:36:55 -0800608 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700609 if (rc < 0)
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800610 return;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700611
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800612 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID,
613 task_pid_nr_ns(tsk, &init_pid_ns));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800614 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800615 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800616
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800617 fill_stats(&init_user_ns, &init_pid_ns, tsk, stats);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700618
Shailabh Nagarc7572492006-07-14 00:24:40 -0700619 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700620 * Doesn't matter if tsk is the leader or the last group member leaving
Shailabh Nagarc7572492006-07-14 00:24:40 -0700621 */
Oleg Nesterov68062b82006-12-06 20:36:53 -0800622 if (!is_thread_group || !group_dead)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700623 goto send;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700624
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800625 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID,
626 task_tgid_nr_ns(tsk, &init_pid_ns));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800627 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800628 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800629
630 memcpy(stats, tsk->signal->stats, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700631
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700632send:
Oleg Nesterov115085e2006-12-06 20:36:51 -0800633 send_cpu_listeners(rep_skb, listeners);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700634 return;
Oleg Nesterov371674852006-12-06 20:36:55 -0800635err:
Shailabh Nagarc7572492006-07-14 00:24:40 -0700636 nlmsg_free(rep_skb);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700637}
638
Johannes Berg4534de82013-11-14 17:14:46 +0100639static const struct genl_ops taskstats_ops[] = {
Johannes Berg88d36a92013-11-14 17:14:39 +0100640 {
641 .cmd = TASKSTATS_CMD_GET,
642 .doit = taskstats_user_cmd,
643 .policy = taskstats_cmd_get_policy,
644 .flags = GENL_ADMIN_PERM,
645 },
646 {
647 .cmd = CGROUPSTATS_CMD_GET,
648 .doit = cgroupstats_user_cmd,
649 .policy = cgroupstats_cmd_get_policy,
650 },
Balbir Singh846c7bb2007-10-18 23:39:44 -0700651};
652
Johannes Berg56989f62016-10-24 14:40:05 +0200653static struct genl_family family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200654 .name = TASKSTATS_GENL_NAME,
655 .version = TASKSTATS_GENL_VERSION,
656 .maxattr = TASKSTATS_CMD_ATTR_MAX,
657 .module = THIS_MODULE,
658 .ops = taskstats_ops,
659 .n_ops = ARRAY_SIZE(taskstats_ops),
660};
661
Shailabh Nagarc7572492006-07-14 00:24:40 -0700662/* Needed early in initialization */
663void __init taskstats_init_early(void)
664{
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700665 unsigned int i;
666
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700667 taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700668 for_each_possible_cpu(i) {
669 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
670 init_rwsem(&(per_cpu(listener_array, i).sem));
671 }
Shailabh Nagarc7572492006-07-14 00:24:40 -0700672}
673
674static int __init taskstats_init(void)
675{
676 int rc;
677
Johannes Berg489111e2016-10-24 14:40:03 +0200678 rc = genl_register_family(&family);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700679 if (rc)
680 return rc;
681
Shailabh Nagarc7572492006-07-14 00:24:40 -0700682 family_registered = 1;
Mandeep Singh Bainesf9b182e2011-03-23 16:43:27 -0700683 pr_info("registered taskstats version %d\n", TASKSTATS_GENL_VERSION);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700684 return 0;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700685}
686
687/*
688 * late initcall ensures initialization of statistics collection
689 * mechanisms precedes initialization of the taskstats interface
690 */
691late_initcall(taskstats_init);