blob: bd6be76303cf23b4c52b757a93bf1966bdbb4f0a [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>
Balbir Singh846c7bb2007-10-18 23:39:44 -070025#include <linux/cgroupstats.h>
26#include <linux/cgroup.h>
27#include <linux/fs.h>
28#include <linux/file.h>
Shailabh Nagarc7572492006-07-14 00:24:40 -070029#include <net/genetlink.h>
30#include <asm/atomic.h>
31
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070032/*
33 * Maximum length of a cpumask that can be specified in
34 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
35 */
36#define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
37
Vegard Nossumb81f3ea2008-07-25 01:48:55 -070038static DEFINE_PER_CPU(__u32, taskstats_seqnum);
Shailabh Nagarc7572492006-07-14 00:24:40 -070039static int family_registered;
Christoph Lametere18b8902006-12-06 20:33:20 -080040struct kmem_cache *taskstats_cache;
Shailabh Nagarc7572492006-07-14 00:24:40 -070041
42static struct genl_family family = {
43 .id = GENL_ID_GENERATE,
44 .name = TASKSTATS_GENL_NAME,
45 .version = TASKSTATS_GENL_VERSION,
46 .maxattr = TASKSTATS_CMD_ATTR_MAX,
47};
48
49static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
50__read_mostly = {
51 [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
52 [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070053 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
54 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
55
Balbir Singh846c7bb2007-10-18 23:39:44 -070056static struct nla_policy
57cgroupstats_cmd_get_policy[CGROUPSTATS_CMD_ATTR_MAX+1] __read_mostly = {
58 [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
59};
60
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070061struct listener {
62 struct list_head list;
63 pid_t pid;
Shailabh Nagarbb129992006-07-14 00:24:47 -070064 char valid;
Shailabh Nagarc7572492006-07-14 00:24:40 -070065};
66
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070067struct listener_list {
68 struct rw_semaphore sem;
69 struct list_head list;
70};
71static DEFINE_PER_CPU(struct listener_list, listener_array);
72
73enum actions {
74 REGISTER,
75 DEREGISTER,
76 CPU_DONT_CARE
77};
Shailabh Nagarc7572492006-07-14 00:24:40 -070078
79static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
Oleg Nesterov37167482006-12-06 20:36:55 -080080 size_t size)
Shailabh Nagarc7572492006-07-14 00:24:40 -070081{
82 struct sk_buff *skb;
83 void *reply;
84
85 /*
86 * If new attributes are added, please revisit this allocation
87 */
Thomas Graf3dabc712006-11-14 19:44:52 -080088 skb = genlmsg_new(size, GFP_KERNEL);
Shailabh Nagarc7572492006-07-14 00:24:40 -070089 if (!skb)
90 return -ENOMEM;
91
92 if (!info) {
93 int seq = get_cpu_var(taskstats_seqnum)++;
94 put_cpu_var(taskstats_seqnum);
95
Thomas Graf17c157c2006-11-14 19:46:02 -080096 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070097 } else
Thomas Graf17c157c2006-11-14 19:46:02 -080098 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070099 if (reply == NULL) {
100 nlmsg_free(skb);
101 return -EINVAL;
102 }
103
104 *skbp = skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700105 return 0;
106}
107
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700108/*
109 * Send taskstats data in @skb to listener with nl_pid @pid
110 */
111static int send_reply(struct sk_buff *skb, pid_t pid)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700112{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700113 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700114 void *reply = genlmsg_data(genlhdr);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700115 int rc;
116
Shailabh Nagarc7572492006-07-14 00:24:40 -0700117 rc = genlmsg_end(skb, reply);
118 if (rc < 0) {
119 nlmsg_free(skb);
120 return rc;
121 }
122
Shailabh Nagarc7572492006-07-14 00:24:40 -0700123 return genlmsg_unicast(skb, pid);
124}
125
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700126/*
127 * Send taskstats data in @skb to listeners registered for @cpu's exit data
128 */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800129static void send_cpu_listeners(struct sk_buff *skb,
130 struct listener_list *listeners)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700131{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700132 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700133 struct listener *s, *tmp;
134 struct sk_buff *skb_next, *skb_cur = skb;
135 void *reply = genlmsg_data(genlhdr);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700136 int rc, delcount = 0;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700137
138 rc = genlmsg_end(skb, reply);
139 if (rc < 0) {
140 nlmsg_free(skb);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700141 return;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700142 }
143
144 rc = 0;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700145 down_read(&listeners->sem);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700146 list_for_each_entry(s, &listeners->list, list) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700147 skb_next = NULL;
148 if (!list_is_last(&s->list, &listeners->list)) {
149 skb_next = skb_clone(skb_cur, GFP_KERNEL);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700150 if (!skb_next)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700151 break;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700152 }
Shailabh Nagard94a0412006-07-30 03:03:11 -0700153 rc = genlmsg_unicast(skb_cur, s->pid);
154 if (rc == -ECONNREFUSED) {
Shailabh Nagarbb129992006-07-14 00:24:47 -0700155 s->valid = 0;
156 delcount++;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700157 }
158 skb_cur = skb_next;
159 }
Shailabh Nagarbb129992006-07-14 00:24:47 -0700160 up_read(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700161
Shailabh Nagard94a0412006-07-30 03:03:11 -0700162 if (skb_cur)
163 nlmsg_free(skb_cur);
164
Shailabh Nagarbb129992006-07-14 00:24:47 -0700165 if (!delcount)
Shailabh Nagard94a0412006-07-30 03:03:11 -0700166 return;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700167
168 /* Delete invalidated entries */
169 down_write(&listeners->sem);
170 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
171 if (!s->valid) {
172 list_del(&s->list);
173 kfree(s);
174 }
175 }
176 up_write(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700177}
178
Oleg Nesterova98b6092006-10-28 10:38:54 -0700179static int fill_pid(pid_t pid, struct task_struct *tsk,
Shailabh Nagarc7572492006-07-14 00:24:40 -0700180 struct taskstats *stats)
181{
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700182 int rc = 0;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700183
Oleg Nesterova98b6092006-10-28 10:38:54 -0700184 if (!tsk) {
185 rcu_read_lock();
Pavel Emelyanovcb41d6d2008-04-30 00:54:23 -0700186 tsk = find_task_by_vpid(pid);
Oleg Nesterova98b6092006-10-28 10:38:54 -0700187 if (tsk)
188 get_task_struct(tsk);
189 rcu_read_unlock();
190 if (!tsk)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700191 return -ESRCH;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700192 } else
193 get_task_struct(tsk);
194
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800195 memset(stats, 0, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700196 /*
197 * Each accounting subsystem adds calls to its functions to
198 * fill in relevant parts of struct taskstsats as follows
199 *
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700200 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700201 */
202
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700203 delayacct_add_tsk(stats, tsk);
Jay Lanf3cef7a2006-09-30 23:28:55 -0700204
205 /* fill in basic acct fields */
Shailabh Nagar6f449932006-07-14 00:24:41 -0700206 stats->version = TASKSTATS_VERSION;
Maxim Uvarovb663a792007-07-15 23:40:48 -0700207 stats->nvcsw = tsk->nvcsw;
208 stats->nivcsw = tsk->nivcsw;
Jay Lanf3cef7a2006-09-30 23:28:55 -0700209 bacct_add_tsk(stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700210
Jay Lan9acc1852006-09-30 23:28:58 -0700211 /* fill in extended acct fields */
212 xacct_add_tsk(stats, tsk);
213
Shailabh Nagar6f449932006-07-14 00:24:41 -0700214 /* Define err: label here if needed */
Shailabh Nagarc7572492006-07-14 00:24:40 -0700215 put_task_struct(tsk);
216 return rc;
217
218}
219
Oleg Nesterova98b6092006-10-28 10:38:54 -0700220static int fill_tgid(pid_t tgid, struct task_struct *first,
Shailabh Nagarc7572492006-07-14 00:24:40 -0700221 struct taskstats *stats)
222{
Oleg Nesterova98b6092006-10-28 10:38:54 -0700223 struct task_struct *tsk;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700224 unsigned long flags;
Oleg Nesterova98b6092006-10-28 10:38:54 -0700225 int rc = -ESRCH;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700226
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700227 /*
228 * Add additional stats from live tasks except zombie thread group
229 * leaders who are already counted with the dead tasks
230 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700231 rcu_read_lock();
232 if (!first)
Pavel Emelyanovcb41d6d2008-04-30 00:54:23 -0700233 first = find_task_by_vpid(tgid);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700234
Oleg Nesterova98b6092006-10-28 10:38:54 -0700235 if (!first || !lock_task_sighand(first, &flags))
236 goto out;
237
238 if (first->signal->stats)
239 memcpy(stats, first->signal->stats, sizeof(*stats));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800240 else
241 memset(stats, 0, sizeof(*stats));
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700242
Shailabh Nagarc7572492006-07-14 00:24:40 -0700243 tsk = first;
244 do {
Oleg Nesterovd7c3f5f2006-10-28 10:38:54 -0700245 if (tsk->exit_state)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700246 continue;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700247 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700248 * Accounting subsystem can call its functions here to
Shailabh Nagarc7572492006-07-14 00:24:40 -0700249 * fill in relevant parts of struct taskstsats as follows
250 *
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700251 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700252 */
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700253 delayacct_add_tsk(stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700254
Maxim Uvarovb663a792007-07-15 23:40:48 -0700255 stats->nvcsw += tsk->nvcsw;
256 stats->nivcsw += tsk->nivcsw;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700257 } while_each_thread(first, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700258
Oleg Nesterova98b6092006-10-28 10:38:54 -0700259 unlock_task_sighand(first, &flags);
260 rc = 0;
261out:
262 rcu_read_unlock();
263
264 stats->version = TASKSTATS_VERSION;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700265 /*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200266 * Accounting subsystems can also add calls here to modify
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700267 * fields of taskstats.
Shailabh Nagarc7572492006-07-14 00:24:40 -0700268 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700269 return rc;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700270}
271
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700272
273static void fill_tgid_exit(struct task_struct *tsk)
274{
275 unsigned long flags;
276
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700277 spin_lock_irqsave(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700278 if (!tsk->signal->stats)
279 goto ret;
280
281 /*
282 * Each accounting subsystem calls its functions here to
283 * accumalate its per-task stats for tsk, into the per-tgid structure
284 *
285 * per-task-foo(tsk->signal->stats, tsk);
286 */
287 delayacct_add_tsk(tsk->signal->stats, tsk);
288ret:
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700289 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700290 return;
291}
292
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700293static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
294{
295 struct listener_list *listeners;
296 struct listener *s, *tmp;
297 unsigned int cpu;
298 cpumask_t mask = *maskp;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700299
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700300 if (!cpus_subset(mask, cpu_possible_map))
301 return -EINVAL;
302
303 if (isadd == REGISTER) {
Mike Travis363ab6f2008-05-12 21:21:13 +0200304 for_each_cpu_mask_nr(cpu, mask) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700305 s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
306 cpu_to_node(cpu));
307 if (!s)
308 goto cleanup;
309 s->pid = pid;
310 INIT_LIST_HEAD(&s->list);
Shailabh Nagarbb129992006-07-14 00:24:47 -0700311 s->valid = 1;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700312
313 listeners = &per_cpu(listener_array, cpu);
314 down_write(&listeners->sem);
315 list_add(&s->list, &listeners->list);
316 up_write(&listeners->sem);
317 }
318 return 0;
319 }
320
321 /* Deregister or cleanup */
322cleanup:
Mike Travis363ab6f2008-05-12 21:21:13 +0200323 for_each_cpu_mask_nr(cpu, mask) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700324 listeners = &per_cpu(listener_array, cpu);
325 down_write(&listeners->sem);
326 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
327 if (s->pid == pid) {
328 list_del(&s->list);
329 kfree(s);
330 break;
331 }
332 }
333 up_write(&listeners->sem);
334 }
335 return 0;
336}
337
338static int parse(struct nlattr *na, cpumask_t *mask)
339{
340 char *data;
341 int len;
342 int ret;
343
344 if (na == NULL)
345 return 1;
346 len = nla_len(na);
347 if (len > TASKSTATS_CPUMASK_MAXLEN)
348 return -E2BIG;
349 if (len < 1)
350 return -EINVAL;
351 data = kmalloc(len, GFP_KERNEL);
352 if (!data)
353 return -ENOMEM;
354 nla_strlcpy(data, na, len);
355 ret = cpulist_parse(data, *mask);
356 kfree(data);
357 return ret;
358}
359
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800360static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
Oleg Nesterov68062b82006-12-06 20:36:53 -0800361{
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800362 struct nlattr *na, *ret;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800363 int aggr;
364
Oleg Nesterov37167482006-12-06 20:36:55 -0800365 aggr = (type == TASKSTATS_TYPE_PID)
366 ? TASKSTATS_TYPE_AGGR_PID
367 : TASKSTATS_TYPE_AGGR_TGID;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800368
369 na = nla_nest_start(skb, aggr);
Oleg Nesterov37167482006-12-06 20:36:55 -0800370 if (!na)
371 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800372 if (nla_put(skb, type, sizeof(pid), &pid) < 0)
373 goto err;
374 ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
375 if (!ret)
376 goto err;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800377 nla_nest_end(skb, na);
378
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800379 return nla_data(ret);
380err:
381 return NULL;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800382}
383
Balbir Singh846c7bb2007-10-18 23:39:44 -0700384static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
385{
386 int rc = 0;
387 struct sk_buff *rep_skb;
388 struct cgroupstats *stats;
389 struct nlattr *na;
390 size_t size;
391 u32 fd;
392 struct file *file;
393 int fput_needed;
394
395 na = info->attrs[CGROUPSTATS_CMD_ATTR_FD];
396 if (!na)
397 return -EINVAL;
398
399 fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]);
400 file = fget_light(fd, &fput_needed);
Adrian Bunkf9615982007-11-14 17:00:37 -0800401 if (!file)
402 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700403
Adrian Bunkf9615982007-11-14 17:00:37 -0800404 size = nla_total_size(sizeof(struct cgroupstats));
Balbir Singh846c7bb2007-10-18 23:39:44 -0700405
Adrian Bunkf9615982007-11-14 17:00:37 -0800406 rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb,
407 size);
408 if (rc < 0)
409 goto err;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700410
Adrian Bunkf9615982007-11-14 17:00:37 -0800411 na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS,
412 sizeof(struct cgroupstats));
413 stats = nla_data(na);
414 memset(stats, 0, sizeof(*stats));
Balbir Singh846c7bb2007-10-18 23:39:44 -0700415
Adrian Bunkf9615982007-11-14 17:00:37 -0800416 rc = cgroupstats_build(stats, file->f_dentry);
417 if (rc < 0) {
418 nlmsg_free(rep_skb);
419 goto err;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700420 }
421
Adrian Bunkf9615982007-11-14 17:00:37 -0800422 rc = send_reply(rep_skb, info->snd_pid);
423
Balbir Singh846c7bb2007-10-18 23:39:44 -0700424err:
Adrian Bunkf9615982007-11-14 17:00:37 -0800425 fput_light(file, fput_needed);
Balbir Singh846c7bb2007-10-18 23:39:44 -0700426 return rc;
427}
428
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700429static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700430{
431 int rc = 0;
432 struct sk_buff *rep_skb;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800433 struct taskstats *stats;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700434 size_t size;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700435 cpumask_t mask;
436
437 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
438 if (rc < 0)
439 return rc;
440 if (rc == 0)
441 return add_del_listener(info->snd_pid, &mask, REGISTER);
442
443 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
444 if (rc < 0)
445 return rc;
446 if (rc == 0)
447 return add_del_listener(info->snd_pid, &mask, DEREGISTER);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700448
449 /*
450 * Size includes space for nested attributes
451 */
452 size = nla_total_size(sizeof(u32)) +
453 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
454
Oleg Nesterov37167482006-12-06 20:36:55 -0800455 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700456 if (rc < 0)
457 return rc;
458
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800459 rc = -EINVAL;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700460 if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
461 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800462 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
463 if (!stats)
Oleg Nesterov37167482006-12-06 20:36:55 -0800464 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700465
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800466 rc = fill_pid(pid, NULL, stats);
467 if (rc < 0)
Oleg Nesterov37167482006-12-06 20:36:55 -0800468 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700469 } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
470 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800471 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
472 if (!stats)
Oleg Nesterov37167482006-12-06 20:36:55 -0800473 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700474
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800475 rc = fill_tgid(tgid, NULL, stats);
476 if (rc < 0)
Oleg Nesterov37167482006-12-06 20:36:55 -0800477 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800478 } else
Shailabh Nagarc7572492006-07-14 00:24:40 -0700479 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700480
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700481 return send_reply(rep_skb, info->snd_pid);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700482err:
483 nlmsg_free(rep_skb);
484 return rc;
485}
486
Oleg Nesterov34ec1232006-12-06 20:36:52 -0800487static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
488{
489 struct signal_struct *sig = tsk->signal;
490 struct taskstats *stats;
491
492 if (sig->stats || thread_group_empty(tsk))
493 goto ret;
494
495 /* No problem if kmem_cache_zalloc() fails */
496 stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
497
498 spin_lock_irq(&tsk->sighand->siglock);
499 if (!sig->stats) {
500 sig->stats = stats;
501 stats = NULL;
502 }
503 spin_unlock_irq(&tsk->sighand->siglock);
504
505 if (stats)
506 kmem_cache_free(taskstats_cache, stats);
507ret:
508 return sig->stats;
509}
510
Shailabh Nagarc7572492006-07-14 00:24:40 -0700511/* Send pid data out on exit */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800512void taskstats_exit(struct task_struct *tsk, int group_dead)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700513{
514 int rc;
Oleg Nesterov115085e2006-12-06 20:36:51 -0800515 struct listener_list *listeners;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800516 struct taskstats *stats;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700517 struct sk_buff *rep_skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700518 size_t size;
519 int is_thread_group;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700520
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800521 if (!family_registered)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700522 return;
523
Shailabh Nagarc7572492006-07-14 00:24:40 -0700524 /*
525 * Size includes space for nested attributes
526 */
527 size = nla_total_size(sizeof(u32)) +
528 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
529
Oleg Nesterov34ec1232006-12-06 20:36:52 -0800530 is_thread_group = !!taskstats_tgid_alloc(tsk);
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800531 if (is_thread_group) {
532 /* PID + STATS + TGID + STATS */
533 size = 2 * size;
534 /* fill the tsk->signal->stats structure */
535 fill_tgid_exit(tsk);
536 }
537
Oleg Nesterov115085e2006-12-06 20:36:51 -0800538 listeners = &__raw_get_cpu_var(listener_array);
539 if (list_empty(&listeners->list))
540 return;
541
Oleg Nesterov37167482006-12-06 20:36:55 -0800542 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700543 if (rc < 0)
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800544 return;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700545
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800546 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
547 if (!stats)
Oleg Nesterov37167482006-12-06 20:36:55 -0800548 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800549
Pavel Emelyanovcb41d6d2008-04-30 00:54:23 -0700550 rc = fill_pid(-1, tsk, stats);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700551 if (rc < 0)
Oleg Nesterov37167482006-12-06 20:36:55 -0800552 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700553
Shailabh Nagarc7572492006-07-14 00:24:40 -0700554 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700555 * Doesn't matter if tsk is the leader or the last group member leaving
Shailabh Nagarc7572492006-07-14 00:24:40 -0700556 */
Oleg Nesterov68062b82006-12-06 20:36:53 -0800557 if (!is_thread_group || !group_dead)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700558 goto send;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700559
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800560 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
561 if (!stats)
Oleg Nesterov37167482006-12-06 20:36:55 -0800562 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800563
564 memcpy(stats, tsk->signal->stats, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700565
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700566send:
Oleg Nesterov115085e2006-12-06 20:36:51 -0800567 send_cpu_listeners(rep_skb, listeners);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700568 return;
Oleg Nesterov37167482006-12-06 20:36:55 -0800569err:
Shailabh Nagarc7572492006-07-14 00:24:40 -0700570 nlmsg_free(rep_skb);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700571}
572
573static struct genl_ops taskstats_ops = {
574 .cmd = TASKSTATS_CMD_GET,
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700575 .doit = taskstats_user_cmd,
Shailabh Nagarc7572492006-07-14 00:24:40 -0700576 .policy = taskstats_cmd_get_policy,
577};
578
Balbir Singh846c7bb2007-10-18 23:39:44 -0700579static struct genl_ops cgroupstats_ops = {
580 .cmd = CGROUPSTATS_CMD_GET,
581 .doit = cgroupstats_user_cmd,
582 .policy = cgroupstats_cmd_get_policy,
583};
584
Shailabh Nagarc7572492006-07-14 00:24:40 -0700585/* Needed early in initialization */
586void __init taskstats_init_early(void)
587{
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700588 unsigned int i;
589
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700590 taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700591 for_each_possible_cpu(i) {
592 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
593 init_rwsem(&(per_cpu(listener_array, i).sem));
594 }
Shailabh Nagarc7572492006-07-14 00:24:40 -0700595}
596
597static int __init taskstats_init(void)
598{
599 int rc;
600
601 rc = genl_register_family(&family);
602 if (rc)
603 return rc;
604
605 rc = genl_register_ops(&family, &taskstats_ops);
606 if (rc < 0)
607 goto err;
608
Balbir Singh846c7bb2007-10-18 23:39:44 -0700609 rc = genl_register_ops(&family, &cgroupstats_ops);
610 if (rc < 0)
611 goto err_cgroup_ops;
612
Shailabh Nagarc7572492006-07-14 00:24:40 -0700613 family_registered = 1;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700614 printk("registered taskstats version %d\n", TASKSTATS_GENL_VERSION);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700615 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700616err_cgroup_ops:
617 genl_unregister_ops(&family, &taskstats_ops);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700618err:
619 genl_unregister_family(&family);
620 return rc;
621}
622
623/*
624 * late initcall ensures initialization of statistics collection
625 * mechanisms precedes initialization of the taskstats interface
626 */
627late_initcall(taskstats_init);