blob: 059431ed67db4bd10b90bf126b32ff59fd4057f9 [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>
Jay Lan9acc1852006-09-30 23:28:58 -070023#include <linux/tsacct_kern.h>
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070024#include <linux/cpumask.h>
25#include <linux/percpu.h>
Shailabh Nagarc7572492006-07-14 00:24:40 -070026#include <net/genetlink.h>
27#include <asm/atomic.h>
28
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070029/*
30 * Maximum length of a cpumask that can be specified in
31 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
32 */
33#define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
34
Shailabh Nagarc7572492006-07-14 00:24:40 -070035static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
36static int family_registered;
Christoph Lametere18b8902006-12-06 20:33:20 -080037struct kmem_cache *taskstats_cache;
Shailabh Nagarc7572492006-07-14 00:24:40 -070038
39static struct genl_family family = {
40 .id = GENL_ID_GENERATE,
41 .name = TASKSTATS_GENL_NAME,
42 .version = TASKSTATS_GENL_VERSION,
43 .maxattr = TASKSTATS_CMD_ATTR_MAX,
44};
45
46static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
47__read_mostly = {
48 [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
49 [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070050 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
51 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
52
53struct listener {
54 struct list_head list;
55 pid_t pid;
Shailabh Nagarbb129992006-07-14 00:24:47 -070056 char valid;
Shailabh Nagarc7572492006-07-14 00:24:40 -070057};
58
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070059struct listener_list {
60 struct rw_semaphore sem;
61 struct list_head list;
62};
63static DEFINE_PER_CPU(struct listener_list, listener_array);
64
65enum actions {
66 REGISTER,
67 DEREGISTER,
68 CPU_DONT_CARE
69};
Shailabh Nagarc7572492006-07-14 00:24:40 -070070
71static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
Oleg Nesterov371674852006-12-06 20:36:55 -080072 size_t size)
Shailabh Nagarc7572492006-07-14 00:24:40 -070073{
74 struct sk_buff *skb;
75 void *reply;
76
77 /*
78 * If new attributes are added, please revisit this allocation
79 */
Thomas Graf3dabc712006-11-14 19:44:52 -080080 skb = genlmsg_new(size, GFP_KERNEL);
Shailabh Nagarc7572492006-07-14 00:24:40 -070081 if (!skb)
82 return -ENOMEM;
83
84 if (!info) {
85 int seq = get_cpu_var(taskstats_seqnum)++;
86 put_cpu_var(taskstats_seqnum);
87
Thomas Graf17c157c2006-11-14 19:46:02 -080088 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070089 } else
Thomas Graf17c157c2006-11-14 19:46:02 -080090 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070091 if (reply == NULL) {
92 nlmsg_free(skb);
93 return -EINVAL;
94 }
95
96 *skbp = skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -070097 return 0;
98}
99
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700100/*
101 * Send taskstats data in @skb to listener with nl_pid @pid
102 */
103static int send_reply(struct sk_buff *skb, pid_t pid)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700104{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700105 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700106 void *reply = genlmsg_data(genlhdr);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700107 int rc;
108
Shailabh Nagarc7572492006-07-14 00:24:40 -0700109 rc = genlmsg_end(skb, reply);
110 if (rc < 0) {
111 nlmsg_free(skb);
112 return rc;
113 }
114
Shailabh Nagarc7572492006-07-14 00:24:40 -0700115 return genlmsg_unicast(skb, pid);
116}
117
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700118/*
119 * Send taskstats data in @skb to listeners registered for @cpu's exit data
120 */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800121static void send_cpu_listeners(struct sk_buff *skb,
122 struct listener_list *listeners)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700123{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700124 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700125 struct listener *s, *tmp;
126 struct sk_buff *skb_next, *skb_cur = skb;
127 void *reply = genlmsg_data(genlhdr);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700128 int rc, delcount = 0;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700129
130 rc = genlmsg_end(skb, reply);
131 if (rc < 0) {
132 nlmsg_free(skb);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700133 return;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700134 }
135
136 rc = 0;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700137 down_read(&listeners->sem);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700138 list_for_each_entry(s, &listeners->list, list) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700139 skb_next = NULL;
140 if (!list_is_last(&s->list, &listeners->list)) {
141 skb_next = skb_clone(skb_cur, GFP_KERNEL);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700142 if (!skb_next)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700143 break;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700144 }
Shailabh Nagard94a0412006-07-30 03:03:11 -0700145 rc = genlmsg_unicast(skb_cur, s->pid);
146 if (rc == -ECONNREFUSED) {
Shailabh Nagarbb129992006-07-14 00:24:47 -0700147 s->valid = 0;
148 delcount++;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700149 }
150 skb_cur = skb_next;
151 }
Shailabh Nagarbb129992006-07-14 00:24:47 -0700152 up_read(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700153
Shailabh Nagard94a0412006-07-30 03:03:11 -0700154 if (skb_cur)
155 nlmsg_free(skb_cur);
156
Shailabh Nagarbb129992006-07-14 00:24:47 -0700157 if (!delcount)
Shailabh Nagard94a0412006-07-30 03:03:11 -0700158 return;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700159
160 /* Delete invalidated entries */
161 down_write(&listeners->sem);
162 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
163 if (!s->valid) {
164 list_del(&s->list);
165 kfree(s);
166 }
167 }
168 up_write(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700169}
170
Oleg Nesterova98b6092006-10-28 10:38:54 -0700171static int fill_pid(pid_t pid, struct task_struct *tsk,
Shailabh Nagarc7572492006-07-14 00:24:40 -0700172 struct taskstats *stats)
173{
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700174 int rc = 0;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700175
Oleg Nesterova98b6092006-10-28 10:38:54 -0700176 if (!tsk) {
177 rcu_read_lock();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700178 tsk = find_task_by_pid(pid);
Oleg Nesterova98b6092006-10-28 10:38:54 -0700179 if (tsk)
180 get_task_struct(tsk);
181 rcu_read_unlock();
182 if (!tsk)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700183 return -ESRCH;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700184 } else
185 get_task_struct(tsk);
186
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800187 memset(stats, 0, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700188 /*
189 * Each accounting subsystem adds calls to its functions to
190 * fill in relevant parts of struct taskstsats as follows
191 *
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700192 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700193 */
194
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700195 delayacct_add_tsk(stats, tsk);
Jay Lanf3cef7a2006-09-30 23:28:55 -0700196
197 /* fill in basic acct fields */
Shailabh Nagar6f449932006-07-14 00:24:41 -0700198 stats->version = TASKSTATS_VERSION;
Maxim Uvarovb663a792007-07-15 23:40:48 -0700199 stats->nvcsw = tsk->nvcsw;
200 stats->nivcsw = tsk->nivcsw;
Jay Lanf3cef7a2006-09-30 23:28:55 -0700201 bacct_add_tsk(stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700202
Jay Lan9acc1852006-09-30 23:28:58 -0700203 /* fill in extended acct fields */
204 xacct_add_tsk(stats, tsk);
205
Shailabh Nagar6f449932006-07-14 00:24:41 -0700206 /* Define err: label here if needed */
Shailabh Nagarc7572492006-07-14 00:24:40 -0700207 put_task_struct(tsk);
208 return rc;
209
210}
211
Oleg Nesterova98b6092006-10-28 10:38:54 -0700212static int fill_tgid(pid_t tgid, struct task_struct *first,
Shailabh Nagarc7572492006-07-14 00:24:40 -0700213 struct taskstats *stats)
214{
Oleg Nesterova98b6092006-10-28 10:38:54 -0700215 struct task_struct *tsk;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700216 unsigned long flags;
Oleg Nesterova98b6092006-10-28 10:38:54 -0700217 int rc = -ESRCH;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700218
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700219 /*
220 * Add additional stats from live tasks except zombie thread group
221 * leaders who are already counted with the dead tasks
222 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700223 rcu_read_lock();
224 if (!first)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700225 first = find_task_by_pid(tgid);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700226
Oleg Nesterova98b6092006-10-28 10:38:54 -0700227 if (!first || !lock_task_sighand(first, &flags))
228 goto out;
229
230 if (first->signal->stats)
231 memcpy(stats, first->signal->stats, sizeof(*stats));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800232 else
233 memset(stats, 0, sizeof(*stats));
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700234
Shailabh Nagarc7572492006-07-14 00:24:40 -0700235 tsk = first;
236 do {
Oleg Nesterovd7c3f5f2006-10-28 10:38:54 -0700237 if (tsk->exit_state)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700238 continue;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700239 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700240 * Accounting subsystem can call its functions here to
Shailabh Nagarc7572492006-07-14 00:24:40 -0700241 * fill in relevant parts of struct taskstsats as follows
242 *
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700243 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700244 */
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700245 delayacct_add_tsk(stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700246
Maxim Uvarovb663a792007-07-15 23:40:48 -0700247 stats->nvcsw += tsk->nvcsw;
248 stats->nivcsw += tsk->nivcsw;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700249 } while_each_thread(first, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700250
Oleg Nesterova98b6092006-10-28 10:38:54 -0700251 unlock_task_sighand(first, &flags);
252 rc = 0;
253out:
254 rcu_read_unlock();
255
256 stats->version = TASKSTATS_VERSION;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700257 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700258 * Accounting subsytems can also add calls here to modify
259 * fields of taskstats.
Shailabh Nagarc7572492006-07-14 00:24:40 -0700260 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700261 return rc;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700262}
263
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700264
265static void fill_tgid_exit(struct task_struct *tsk)
266{
267 unsigned long flags;
268
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700269 spin_lock_irqsave(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700270 if (!tsk->signal->stats)
271 goto ret;
272
273 /*
274 * Each accounting subsystem calls its functions here to
275 * accumalate its per-task stats for tsk, into the per-tgid structure
276 *
277 * per-task-foo(tsk->signal->stats, tsk);
278 */
279 delayacct_add_tsk(tsk->signal->stats, tsk);
280ret:
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700281 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700282 return;
283}
284
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700285static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
286{
287 struct listener_list *listeners;
288 struct listener *s, *tmp;
289 unsigned int cpu;
290 cpumask_t mask = *maskp;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700291
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700292 if (!cpus_subset(mask, cpu_possible_map))
293 return -EINVAL;
294
295 if (isadd == REGISTER) {
296 for_each_cpu_mask(cpu, mask) {
297 s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
298 cpu_to_node(cpu));
299 if (!s)
300 goto cleanup;
301 s->pid = pid;
302 INIT_LIST_HEAD(&s->list);
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);
307 list_add(&s->list, &listeners->list);
308 up_write(&listeners->sem);
309 }
310 return 0;
311 }
312
313 /* Deregister or cleanup */
314cleanup:
315 for_each_cpu_mask(cpu, mask) {
316 listeners = &per_cpu(listener_array, cpu);
317 down_write(&listeners->sem);
318 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
319 if (s->pid == pid) {
320 list_del(&s->list);
321 kfree(s);
322 break;
323 }
324 }
325 up_write(&listeners->sem);
326 }
327 return 0;
328}
329
330static int parse(struct nlattr *na, cpumask_t *mask)
331{
332 char *data;
333 int len;
334 int ret;
335
336 if (na == NULL)
337 return 1;
338 len = nla_len(na);
339 if (len > TASKSTATS_CPUMASK_MAXLEN)
340 return -E2BIG;
341 if (len < 1)
342 return -EINVAL;
343 data = kmalloc(len, GFP_KERNEL);
344 if (!data)
345 return -ENOMEM;
346 nla_strlcpy(data, na, len);
347 ret = cpulist_parse(data, *mask);
348 kfree(data);
349 return ret;
350}
351
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800352static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
Oleg Nesterov68062b82006-12-06 20:36:53 -0800353{
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800354 struct nlattr *na, *ret;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800355 int aggr;
356
Oleg Nesterov371674852006-12-06 20:36:55 -0800357 aggr = (type == TASKSTATS_TYPE_PID)
358 ? TASKSTATS_TYPE_AGGR_PID
359 : TASKSTATS_TYPE_AGGR_TGID;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800360
361 na = nla_nest_start(skb, aggr);
Oleg Nesterov371674852006-12-06 20:36:55 -0800362 if (!na)
363 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800364 if (nla_put(skb, type, sizeof(pid), &pid) < 0)
365 goto err;
366 ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
367 if (!ret)
368 goto err;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800369 nla_nest_end(skb, na);
370
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800371 return nla_data(ret);
372err:
373 return NULL;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800374}
375
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700376static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700377{
378 int rc = 0;
379 struct sk_buff *rep_skb;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800380 struct taskstats *stats;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700381 size_t size;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700382 cpumask_t mask;
383
384 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
385 if (rc < 0)
386 return rc;
387 if (rc == 0)
388 return add_del_listener(info->snd_pid, &mask, REGISTER);
389
390 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
391 if (rc < 0)
392 return rc;
393 if (rc == 0)
394 return add_del_listener(info->snd_pid, &mask, DEREGISTER);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700395
396 /*
397 * Size includes space for nested attributes
398 */
399 size = nla_total_size(sizeof(u32)) +
400 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
401
Oleg Nesterov371674852006-12-06 20:36:55 -0800402 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700403 if (rc < 0)
404 return rc;
405
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800406 rc = -EINVAL;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700407 if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
408 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800409 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
410 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800411 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700412
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800413 rc = fill_pid(pid, NULL, stats);
414 if (rc < 0)
Oleg Nesterov371674852006-12-06 20:36:55 -0800415 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700416 } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
417 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800418 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
419 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800420 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700421
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800422 rc = fill_tgid(tgid, NULL, stats);
423 if (rc < 0)
Oleg Nesterov371674852006-12-06 20:36:55 -0800424 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800425 } else
Shailabh Nagarc7572492006-07-14 00:24:40 -0700426 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700427
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700428 return send_reply(rep_skb, info->snd_pid);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700429err:
430 nlmsg_free(rep_skb);
431 return rc;
432}
433
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800434static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
435{
436 struct signal_struct *sig = tsk->signal;
437 struct taskstats *stats;
438
439 if (sig->stats || thread_group_empty(tsk))
440 goto ret;
441
442 /* No problem if kmem_cache_zalloc() fails */
443 stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
444
445 spin_lock_irq(&tsk->sighand->siglock);
446 if (!sig->stats) {
447 sig->stats = stats;
448 stats = NULL;
449 }
450 spin_unlock_irq(&tsk->sighand->siglock);
451
452 if (stats)
453 kmem_cache_free(taskstats_cache, stats);
454ret:
455 return sig->stats;
456}
457
Shailabh Nagarc7572492006-07-14 00:24:40 -0700458/* Send pid data out on exit */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800459void taskstats_exit(struct task_struct *tsk, int group_dead)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700460{
461 int rc;
Oleg Nesterov115085e2006-12-06 20:36:51 -0800462 struct listener_list *listeners;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800463 struct taskstats *stats;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700464 struct sk_buff *rep_skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700465 size_t size;
466 int is_thread_group;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700467
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800468 if (!family_registered)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700469 return;
470
Shailabh Nagarc7572492006-07-14 00:24:40 -0700471 /*
472 * Size includes space for nested attributes
473 */
474 size = nla_total_size(sizeof(u32)) +
475 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
476
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800477 is_thread_group = !!taskstats_tgid_alloc(tsk);
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800478 if (is_thread_group) {
479 /* PID + STATS + TGID + STATS */
480 size = 2 * size;
481 /* fill the tsk->signal->stats structure */
482 fill_tgid_exit(tsk);
483 }
484
Oleg Nesterov115085e2006-12-06 20:36:51 -0800485 listeners = &__raw_get_cpu_var(listener_array);
486 if (list_empty(&listeners->list))
487 return;
488
Oleg Nesterov371674852006-12-06 20:36:55 -0800489 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700490 if (rc < 0)
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800491 return;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700492
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800493 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
494 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800495 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800496
497 rc = fill_pid(tsk->pid, tsk, stats);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700498 if (rc < 0)
Oleg Nesterov371674852006-12-06 20:36:55 -0800499 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700500
Shailabh Nagarc7572492006-07-14 00:24:40 -0700501 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700502 * Doesn't matter if tsk is the leader or the last group member leaving
Shailabh Nagarc7572492006-07-14 00:24:40 -0700503 */
Oleg Nesterov68062b82006-12-06 20:36:53 -0800504 if (!is_thread_group || !group_dead)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700505 goto send;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700506
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800507 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
508 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800509 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800510
511 memcpy(stats, tsk->signal->stats, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700512
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700513send:
Oleg Nesterov115085e2006-12-06 20:36:51 -0800514 send_cpu_listeners(rep_skb, listeners);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700515 return;
Oleg Nesterov371674852006-12-06 20:36:55 -0800516err:
Shailabh Nagarc7572492006-07-14 00:24:40 -0700517 nlmsg_free(rep_skb);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700518}
519
520static struct genl_ops taskstats_ops = {
521 .cmd = TASKSTATS_CMD_GET,
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700522 .doit = taskstats_user_cmd,
Shailabh Nagarc7572492006-07-14 00:24:40 -0700523 .policy = taskstats_cmd_get_policy,
524};
525
526/* Needed early in initialization */
527void __init taskstats_init_early(void)
528{
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700529 unsigned int i;
530
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700531 taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700532 for_each_possible_cpu(i) {
533 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
534 init_rwsem(&(per_cpu(listener_array, i).sem));
535 }
Shailabh Nagarc7572492006-07-14 00:24:40 -0700536}
537
538static int __init taskstats_init(void)
539{
540 int rc;
541
542 rc = genl_register_family(&family);
543 if (rc)
544 return rc;
545
546 rc = genl_register_ops(&family, &taskstats_ops);
547 if (rc < 0)
548 goto err;
549
550 family_registered = 1;
551 return 0;
552err:
553 genl_unregister_family(&family);
554 return rc;
555}
556
557/*
558 * late initcall ensures initialization of statistics collection
559 * mechanisms precedes initialization of the taskstats interface
560 */
561late_initcall(taskstats_init);