kprobes: initialize before using a hlist
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / taskstats.c
bloba4ef5426ca60eb6fbf9206cb4b15c891b0188862
1 /*
2 * taskstats.c - Export per-task statistics to userland
4 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
5 * (C) Balbir Singh, IBM Corp. 2006
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/taskstats_kern.h>
21 #include <linux/tsacct_kern.h>
22 #include <linux/delayacct.h>
23 #include <linux/cpumask.h>
24 #include <linux/percpu.h>
25 #include <linux/cgroupstats.h>
26 #include <linux/cgroup.h>
27 #include <linux/fs.h>
28 #include <linux/file.h>
29 #include <net/genetlink.h>
30 #include <asm/atomic.h>
33 * Maximum length of a cpumask that can be specified in
34 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
36 #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
38 static DEFINE_PER_CPU(__u32, taskstats_seqnum);
39 static int family_registered;
40 struct kmem_cache *taskstats_cache;
42 static 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,
49 static 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 },
53 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
54 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
56 static struct nla_policy
57 cgroupstats_cmd_get_policy[CGROUPSTATS_CMD_ATTR_MAX+1] __read_mostly = {
58 [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
61 struct listener {
62 struct list_head list;
63 pid_t pid;
64 char valid;
67 struct listener_list {
68 struct rw_semaphore sem;
69 struct list_head list;
71 static DEFINE_PER_CPU(struct listener_list, listener_array);
73 enum actions {
74 REGISTER,
75 DEREGISTER,
76 CPU_DONT_CARE
79 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
80 size_t size)
82 struct sk_buff *skb;
83 void *reply;
86 * If new attributes are added, please revisit this allocation
88 skb = genlmsg_new(size, GFP_KERNEL);
89 if (!skb)
90 return -ENOMEM;
92 if (!info) {
93 int seq = get_cpu_var(taskstats_seqnum)++;
94 put_cpu_var(taskstats_seqnum);
96 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
97 } else
98 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
99 if (reply == NULL) {
100 nlmsg_free(skb);
101 return -EINVAL;
104 *skbp = skb;
105 return 0;
109 * Send taskstats data in @skb to listener with nl_pid @pid
111 static int send_reply(struct sk_buff *skb, struct genl_info *info)
113 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
114 void *reply = genlmsg_data(genlhdr);
115 int rc;
117 rc = genlmsg_end(skb, reply);
118 if (rc < 0) {
119 nlmsg_free(skb);
120 return rc;
123 return genlmsg_reply(skb, info);
127 * Send taskstats data in @skb to listeners registered for @cpu's exit data
129 static void send_cpu_listeners(struct sk_buff *skb,
130 struct listener_list *listeners)
132 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
133 struct listener *s, *tmp;
134 struct sk_buff *skb_next, *skb_cur = skb;
135 void *reply = genlmsg_data(genlhdr);
136 int rc, delcount = 0;
138 rc = genlmsg_end(skb, reply);
139 if (rc < 0) {
140 nlmsg_free(skb);
141 return;
144 rc = 0;
145 down_read(&listeners->sem);
146 list_for_each_entry(s, &listeners->list, list) {
147 skb_next = NULL;
148 if (!list_is_last(&s->list, &listeners->list)) {
149 skb_next = skb_clone(skb_cur, GFP_KERNEL);
150 if (!skb_next)
151 break;
153 rc = genlmsg_unicast(&init_net, skb_cur, s->pid);
154 if (rc == -ECONNREFUSED) {
155 s->valid = 0;
156 delcount++;
158 skb_cur = skb_next;
160 up_read(&listeners->sem);
162 if (skb_cur)
163 nlmsg_free(skb_cur);
165 if (!delcount)
166 return;
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);
176 up_write(&listeners->sem);
179 static int fill_pid(pid_t pid, struct task_struct *tsk,
180 struct taskstats *stats)
182 int rc = 0;
184 if (!tsk) {
185 rcu_read_lock();
186 tsk = find_task_by_vpid(pid);
187 if (tsk)
188 get_task_struct(tsk);
189 rcu_read_unlock();
190 if (!tsk)
191 return -ESRCH;
192 } else
193 get_task_struct(tsk);
195 memset(stats, 0, sizeof(*stats));
197 * Each accounting subsystem adds calls to its functions to
198 * fill in relevant parts of struct taskstsats as follows
200 * per-task-foo(stats, tsk);
203 delayacct_add_tsk(stats, tsk);
205 /* fill in basic acct fields */
206 stats->version = TASKSTATS_VERSION;
207 stats->nvcsw = tsk->nvcsw;
208 stats->nivcsw = tsk->nivcsw;
209 bacct_add_tsk(stats, tsk);
211 /* fill in extended acct fields */
212 xacct_add_tsk(stats, tsk);
214 /* Define err: label here if needed */
215 put_task_struct(tsk);
216 return rc;
220 static int fill_tgid(pid_t tgid, struct task_struct *first,
221 struct taskstats *stats)
223 struct task_struct *tsk;
224 unsigned long flags;
225 int rc = -ESRCH;
228 * Add additional stats from live tasks except zombie thread group
229 * leaders who are already counted with the dead tasks
231 rcu_read_lock();
232 if (!first)
233 first = find_task_by_vpid(tgid);
235 if (!first || !lock_task_sighand(first, &flags))
236 goto out;
238 if (first->signal->stats)
239 memcpy(stats, first->signal->stats, sizeof(*stats));
240 else
241 memset(stats, 0, sizeof(*stats));
243 tsk = first;
244 do {
245 if (tsk->exit_state)
246 continue;
248 * Accounting subsystem can call its functions here to
249 * fill in relevant parts of struct taskstsats as follows
251 * per-task-foo(stats, tsk);
253 delayacct_add_tsk(stats, tsk);
255 stats->nvcsw += tsk->nvcsw;
256 stats->nivcsw += tsk->nivcsw;
257 } while_each_thread(first, tsk);
259 unlock_task_sighand(first, &flags);
260 rc = 0;
261 out:
262 rcu_read_unlock();
264 stats->version = TASKSTATS_VERSION;
266 * Accounting subsystems can also add calls here to modify
267 * fields of taskstats.
269 return rc;
273 static void fill_tgid_exit(struct task_struct *tsk)
275 unsigned long flags;
277 spin_lock_irqsave(&tsk->sighand->siglock, flags);
278 if (!tsk->signal->stats)
279 goto ret;
282 * Each accounting subsystem calls its functions here to
283 * accumalate its per-task stats for tsk, into the per-tgid structure
285 * per-task-foo(tsk->signal->stats, tsk);
287 delayacct_add_tsk(tsk->signal->stats, tsk);
288 ret:
289 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
290 return;
293 static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
295 struct listener_list *listeners;
296 struct listener *s, *tmp, *s2;
297 unsigned int cpu;
299 if (!cpumask_subset(mask, cpu_possible_mask))
300 return -EINVAL;
302 s = NULL;
303 if (isadd == REGISTER) {
304 for_each_cpu(cpu, mask) {
305 if (!s)
306 s = kmalloc_node(sizeof(struct listener),
307 GFP_KERNEL, cpu_to_node(cpu));
308 if (!s)
309 goto cleanup;
310 s->pid = pid;
311 INIT_LIST_HEAD(&s->list);
312 s->valid = 1;
314 listeners = &per_cpu(listener_array, cpu);
315 down_write(&listeners->sem);
316 list_for_each_entry_safe(s2, tmp, &listeners->list, list) {
317 if (s2->pid == pid)
318 goto next_cpu;
320 list_add(&s->list, &listeners->list);
321 s = NULL;
322 next_cpu:
323 up_write(&listeners->sem);
325 kfree(s);
326 return 0;
329 /* Deregister or cleanup */
330 cleanup:
331 for_each_cpu(cpu, mask) {
332 listeners = &per_cpu(listener_array, cpu);
333 down_write(&listeners->sem);
334 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
335 if (s->pid == pid) {
336 list_del(&s->list);
337 kfree(s);
338 break;
341 up_write(&listeners->sem);
343 return 0;
346 static int parse(struct nlattr *na, struct cpumask *mask)
348 char *data;
349 int len;
350 int ret;
352 if (na == NULL)
353 return 1;
354 len = nla_len(na);
355 if (len > TASKSTATS_CPUMASK_MAXLEN)
356 return -E2BIG;
357 if (len < 1)
358 return -EINVAL;
359 data = kmalloc(len, GFP_KERNEL);
360 if (!data)
361 return -ENOMEM;
362 nla_strlcpy(data, na, len);
363 ret = cpulist_parse(data, mask);
364 kfree(data);
365 return ret;
368 static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
370 struct nlattr *na, *ret;
371 int aggr;
373 aggr = (type == TASKSTATS_TYPE_PID)
374 ? TASKSTATS_TYPE_AGGR_PID
375 : TASKSTATS_TYPE_AGGR_TGID;
377 na = nla_nest_start(skb, aggr);
378 if (!na)
379 goto err;
380 if (nla_put(skb, type, sizeof(pid), &pid) < 0)
381 goto err;
382 ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
383 if (!ret)
384 goto err;
385 nla_nest_end(skb, na);
387 return nla_data(ret);
388 err:
389 return NULL;
392 static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
394 int rc = 0;
395 struct sk_buff *rep_skb;
396 struct cgroupstats *stats;
397 struct nlattr *na;
398 size_t size;
399 u32 fd;
400 struct file *file;
401 int fput_needed;
403 na = info->attrs[CGROUPSTATS_CMD_ATTR_FD];
404 if (!na)
405 return -EINVAL;
407 fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]);
408 file = fget_light(fd, &fput_needed);
409 if (!file)
410 return 0;
412 size = nla_total_size(sizeof(struct cgroupstats));
414 rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb,
415 size);
416 if (rc < 0)
417 goto err;
419 na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS,
420 sizeof(struct cgroupstats));
421 stats = nla_data(na);
422 memset(stats, 0, sizeof(*stats));
424 rc = cgroupstats_build(stats, file->f_dentry);
425 if (rc < 0) {
426 nlmsg_free(rep_skb);
427 goto err;
430 rc = send_reply(rep_skb, info);
432 err:
433 fput_light(file, fput_needed);
434 return rc;
437 static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
439 int rc;
440 struct sk_buff *rep_skb;
441 struct taskstats *stats;
442 size_t size;
443 cpumask_var_t mask;
445 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
446 return -ENOMEM;
448 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask);
449 if (rc < 0)
450 goto free_return_rc;
451 if (rc == 0) {
452 rc = add_del_listener(info->snd_pid, mask, REGISTER);
453 goto free_return_rc;
456 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask);
457 if (rc < 0)
458 goto free_return_rc;
459 if (rc == 0) {
460 rc = add_del_listener(info->snd_pid, mask, DEREGISTER);
461 free_return_rc:
462 free_cpumask_var(mask);
463 return rc;
465 free_cpumask_var(mask);
468 * Size includes space for nested attributes
470 size = nla_total_size(sizeof(u32)) +
471 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
473 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
474 if (rc < 0)
475 return rc;
477 rc = -EINVAL;
478 if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
479 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
480 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
481 if (!stats)
482 goto err;
484 rc = fill_pid(pid, NULL, stats);
485 if (rc < 0)
486 goto err;
487 } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
488 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
489 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
490 if (!stats)
491 goto err;
493 rc = fill_tgid(tgid, NULL, stats);
494 if (rc < 0)
495 goto err;
496 } else
497 goto err;
499 return send_reply(rep_skb, info);
500 err:
501 nlmsg_free(rep_skb);
502 return rc;
505 static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
507 struct signal_struct *sig = tsk->signal;
508 struct taskstats *stats;
510 if (sig->stats || thread_group_empty(tsk))
511 goto ret;
513 /* No problem if kmem_cache_zalloc() fails */
514 stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
516 spin_lock_irq(&tsk->sighand->siglock);
517 if (!sig->stats) {
518 sig->stats = stats;
519 stats = NULL;
521 spin_unlock_irq(&tsk->sighand->siglock);
523 if (stats)
524 kmem_cache_free(taskstats_cache, stats);
525 ret:
526 return sig->stats;
529 /* Send pid data out on exit */
530 void taskstats_exit(struct task_struct *tsk, int group_dead)
532 int rc;
533 struct listener_list *listeners;
534 struct taskstats *stats;
535 struct sk_buff *rep_skb;
536 size_t size;
537 int is_thread_group;
539 if (!family_registered)
540 return;
543 * Size includes space for nested attributes
545 size = nla_total_size(sizeof(u32)) +
546 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
548 is_thread_group = !!taskstats_tgid_alloc(tsk);
549 if (is_thread_group) {
550 /* PID + STATS + TGID + STATS */
551 size = 2 * size;
552 /* fill the tsk->signal->stats structure */
553 fill_tgid_exit(tsk);
556 listeners = &__raw_get_cpu_var(listener_array);
557 if (list_empty(&listeners->list))
558 return;
560 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
561 if (rc < 0)
562 return;
564 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
565 if (!stats)
566 goto err;
568 rc = fill_pid(-1, tsk, stats);
569 if (rc < 0)
570 goto err;
573 * Doesn't matter if tsk is the leader or the last group member leaving
575 if (!is_thread_group || !group_dead)
576 goto send;
578 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
579 if (!stats)
580 goto err;
582 memcpy(stats, tsk->signal->stats, sizeof(*stats));
584 send:
585 send_cpu_listeners(rep_skb, listeners);
586 return;
587 err:
588 nlmsg_free(rep_skb);
591 static struct genl_ops taskstats_ops = {
592 .cmd = TASKSTATS_CMD_GET,
593 .doit = taskstats_user_cmd,
594 .policy = taskstats_cmd_get_policy,
595 .flags = GENL_ADMIN_PERM,
598 static struct genl_ops cgroupstats_ops = {
599 .cmd = CGROUPSTATS_CMD_GET,
600 .doit = cgroupstats_user_cmd,
601 .policy = cgroupstats_cmd_get_policy,
604 /* Needed early in initialization */
605 void __init taskstats_init_early(void)
607 unsigned int i;
609 taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
610 for_each_possible_cpu(i) {
611 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
612 init_rwsem(&(per_cpu(listener_array, i).sem));
616 static int __init taskstats_init(void)
618 int rc;
620 rc = genl_register_family(&family);
621 if (rc)
622 return rc;
624 rc = genl_register_ops(&family, &taskstats_ops);
625 if (rc < 0)
626 goto err;
628 rc = genl_register_ops(&family, &cgroupstats_ops);
629 if (rc < 0)
630 goto err_cgroup_ops;
632 family_registered = 1;
633 printk("registered taskstats version %d\n", TASKSTATS_GENL_VERSION);
634 return 0;
635 err_cgroup_ops:
636 genl_unregister_ops(&family, &taskstats_ops);
637 err:
638 genl_unregister_family(&family);
639 return rc;
643 * late initcall ensures initialization of statistics collection
644 * mechanisms precedes initialization of the taskstats interface
646 late_initcall(taskstats_init);