kernel/taskstats.c: add nla_nest_cancel() for failure processing between nla_nest_sta...
[cascardo/linux.git] / kernel / taskstats.c
1 /*
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>
21 #include <linux/tsacct_kern.h>
22 #include <linux/delayacct.h>
23 #include <linux/cpumask.h>
24 #include <linux/percpu.h>
25 #include <linux/slab.h>
26 #include <linux/cgroupstats.h>
27 #include <linux/cgroup.h>
28 #include <linux/fs.h>
29 #include <linux/file.h>
30 #include <linux/pid_namespace.h>
31 #include <net/genetlink.h>
32 #include <linux/atomic.h>
33
34 /*
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
40 static DEFINE_PER_CPU(__u32, taskstats_seqnum);
41 static int family_registered;
42 struct kmem_cache *taskstats_cache;
43
44 static struct genl_family family = {
45         .id             = GENL_ID_GENERATE,
46         .name           = TASKSTATS_GENL_NAME,
47         .version        = TASKSTATS_GENL_VERSION,
48         .maxattr        = TASKSTATS_CMD_ATTR_MAX,
49 };
50
51 static const struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = {
52         [TASKSTATS_CMD_ATTR_PID]  = { .type = NLA_U32 },
53         [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
54         [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
55         [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
56
57 static const struct nla_policy cgroupstats_cmd_get_policy[CGROUPSTATS_CMD_ATTR_MAX+1] = {
58         [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
59 };
60
61 struct listener {
62         struct list_head list;
63         pid_t pid;
64         char valid;
65 };
66
67 struct listener_list {
68         struct rw_semaphore sem;
69         struct list_head list;
70 };
71 static DEFINE_PER_CPU(struct listener_list, listener_array);
72
73 enum actions {
74         REGISTER,
75         DEREGISTER,
76         CPU_DONT_CARE
77 };
78
79 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
80                                 size_t size)
81 {
82         struct sk_buff *skb;
83         void *reply;
84
85         /*
86          * If new attributes are added, please revisit this allocation
87          */
88         skb = genlmsg_new(size, GFP_KERNEL);
89         if (!skb)
90                 return -ENOMEM;
91
92         if (!info) {
93                 int seq = this_cpu_inc_return(taskstats_seqnum) - 1;
94
95                 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
96         } else
97                 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
98         if (reply == NULL) {
99                 nlmsg_free(skb);
100                 return -EINVAL;
101         }
102
103         *skbp = skb;
104         return 0;
105 }
106
107 /*
108  * Send taskstats data in @skb to listener with nl_pid @pid
109  */
110 static int send_reply(struct sk_buff *skb, struct genl_info *info)
111 {
112         struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
113         void *reply = genlmsg_data(genlhdr);
114         int rc;
115
116         rc = genlmsg_end(skb, reply);
117         if (rc < 0) {
118                 nlmsg_free(skb);
119                 return rc;
120         }
121
122         return genlmsg_reply(skb, info);
123 }
124
125 /*
126  * Send taskstats data in @skb to listeners registered for @cpu's exit data
127  */
128 static void send_cpu_listeners(struct sk_buff *skb,
129                                         struct listener_list *listeners)
130 {
131         struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
132         struct listener *s, *tmp;
133         struct sk_buff *skb_next, *skb_cur = skb;
134         void *reply = genlmsg_data(genlhdr);
135         int rc, delcount = 0;
136
137         rc = genlmsg_end(skb, reply);
138         if (rc < 0) {
139                 nlmsg_free(skb);
140                 return;
141         }
142
143         rc = 0;
144         down_read(&listeners->sem);
145         list_for_each_entry(s, &listeners->list, list) {
146                 skb_next = NULL;
147                 if (!list_is_last(&s->list, &listeners->list)) {
148                         skb_next = skb_clone(skb_cur, GFP_KERNEL);
149                         if (!skb_next)
150                                 break;
151                 }
152                 rc = genlmsg_unicast(&init_net, skb_cur, s->pid);
153                 if (rc == -ECONNREFUSED) {
154                         s->valid = 0;
155                         delcount++;
156                 }
157                 skb_cur = skb_next;
158         }
159         up_read(&listeners->sem);
160
161         if (skb_cur)
162                 nlmsg_free(skb_cur);
163
164         if (!delcount)
165                 return;
166
167         /* Delete invalidated entries */
168         down_write(&listeners->sem);
169         list_for_each_entry_safe(s, tmp, &listeners->list, list) {
170                 if (!s->valid) {
171                         list_del(&s->list);
172                         kfree(s);
173                 }
174         }
175         up_write(&listeners->sem);
176 }
177
178 static void fill_stats(struct user_namespace *user_ns,
179                        struct pid_namespace *pid_ns,
180                        struct task_struct *tsk, struct taskstats *stats)
181 {
182         memset(stats, 0, sizeof(*stats));
183         /*
184          * Each accounting subsystem adds calls to its functions to
185          * fill in relevant parts of struct taskstsats as follows
186          *
187          *      per-task-foo(stats, tsk);
188          */
189
190         delayacct_add_tsk(stats, tsk);
191
192         /* fill in basic acct fields */
193         stats->version = TASKSTATS_VERSION;
194         stats->nvcsw = tsk->nvcsw;
195         stats->nivcsw = tsk->nivcsw;
196         bacct_add_tsk(user_ns, pid_ns, stats, tsk);
197
198         /* fill in extended acct fields */
199         xacct_add_tsk(stats, tsk);
200 }
201
202 static int fill_stats_for_pid(pid_t pid, struct taskstats *stats)
203 {
204         struct task_struct *tsk;
205
206         rcu_read_lock();
207         tsk = find_task_by_vpid(pid);
208         if (tsk)
209                 get_task_struct(tsk);
210         rcu_read_unlock();
211         if (!tsk)
212                 return -ESRCH;
213         fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats);
214         put_task_struct(tsk);
215         return 0;
216 }
217
218 static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
219 {
220         struct task_struct *tsk, *first;
221         unsigned long flags;
222         int rc = -ESRCH;
223
224         /*
225          * Add additional stats from live tasks except zombie thread group
226          * leaders who are already counted with the dead tasks
227          */
228         rcu_read_lock();
229         first = find_task_by_vpid(tgid);
230
231         if (!first || !lock_task_sighand(first, &flags))
232                 goto out;
233
234         if (first->signal->stats)
235                 memcpy(stats, first->signal->stats, sizeof(*stats));
236         else
237                 memset(stats, 0, sizeof(*stats));
238
239         tsk = first;
240         do {
241                 if (tsk->exit_state)
242                         continue;
243                 /*
244                  * Accounting subsystem can call its functions here to
245                  * fill in relevant parts of struct taskstsats as follows
246                  *
247                  *      per-task-foo(stats, tsk);
248                  */
249                 delayacct_add_tsk(stats, tsk);
250
251                 stats->nvcsw += tsk->nvcsw;
252                 stats->nivcsw += tsk->nivcsw;
253         } while_each_thread(first, tsk);
254
255         unlock_task_sighand(first, &flags);
256         rc = 0;
257 out:
258         rcu_read_unlock();
259
260         stats->version = TASKSTATS_VERSION;
261         /*
262          * Accounting subsystems can also add calls here to modify
263          * fields of taskstats.
264          */
265         return rc;
266 }
267
268 static void fill_tgid_exit(struct task_struct *tsk)
269 {
270         unsigned long flags;
271
272         spin_lock_irqsave(&tsk->sighand->siglock, flags);
273         if (!tsk->signal->stats)
274                 goto ret;
275
276         /*
277          * Each accounting subsystem calls its functions here to
278          * accumalate its per-task stats for tsk, into the per-tgid structure
279          *
280          *      per-task-foo(tsk->signal->stats, tsk);
281          */
282         delayacct_add_tsk(tsk->signal->stats, tsk);
283 ret:
284         spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
285         return;
286 }
287
288 static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
289 {
290         struct listener_list *listeners;
291         struct listener *s, *tmp, *s2;
292         unsigned int cpu;
293
294         if (!cpumask_subset(mask, cpu_possible_mask))
295                 return -EINVAL;
296
297         if (current_user_ns() != &init_user_ns)
298                 return -EINVAL;
299
300         if (task_active_pid_ns(current) != &init_pid_ns)
301                 return -EINVAL;
302
303         if (isadd == REGISTER) {
304                 for_each_cpu(cpu, mask) {
305                         s = kmalloc_node(sizeof(struct listener),
306                                         GFP_KERNEL, cpu_to_node(cpu));
307                         if (!s)
308                                 goto cleanup;
309
310                         s->pid = pid;
311                         s->valid = 1;
312
313                         listeners = &per_cpu(listener_array, cpu);
314                         down_write(&listeners->sem);
315                         list_for_each_entry(s2, &listeners->list, list) {
316                                 if (s2->pid == pid && s2->valid)
317                                         goto exists;
318                         }
319                         list_add(&s->list, &listeners->list);
320                         s = NULL;
321 exists:
322                         up_write(&listeners->sem);
323                         kfree(s); /* nop if NULL */
324                 }
325                 return 0;
326         }
327
328         /* Deregister or cleanup */
329 cleanup:
330         for_each_cpu(cpu, mask) {
331                 listeners = &per_cpu(listener_array, cpu);
332                 down_write(&listeners->sem);
333                 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
334                         if (s->pid == pid) {
335                                 list_del(&s->list);
336                                 kfree(s);
337                                 break;
338                         }
339                 }
340                 up_write(&listeners->sem);
341         }
342         return 0;
343 }
344
345 static int parse(struct nlattr *na, struct cpumask *mask)
346 {
347         char *data;
348         int len;
349         int ret;
350
351         if (na == NULL)
352                 return 1;
353         len = nla_len(na);
354         if (len > TASKSTATS_CPUMASK_MAXLEN)
355                 return -E2BIG;
356         if (len < 1)
357                 return -EINVAL;
358         data = kmalloc(len, GFP_KERNEL);
359         if (!data)
360                 return -ENOMEM;
361         nla_strlcpy(data, na, len);
362         ret = cpulist_parse(data, mask);
363         kfree(data);
364         return ret;
365 }
366
367 #if defined(CONFIG_64BIT) && !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
368 #define TASKSTATS_NEEDS_PADDING 1
369 #endif
370
371 static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
372 {
373         struct nlattr *na, *ret;
374         int aggr;
375
376         aggr = (type == TASKSTATS_TYPE_PID)
377                         ? TASKSTATS_TYPE_AGGR_PID
378                         : TASKSTATS_TYPE_AGGR_TGID;
379
380         /*
381          * The taskstats structure is internally aligned on 8 byte
382          * boundaries but the layout of the aggregrate reply, with
383          * two NLA headers and the pid (each 4 bytes), actually
384          * force the entire structure to be unaligned. This causes
385          * the kernel to issue unaligned access warnings on some
386          * architectures like ia64. Unfortunately, some software out there
387          * doesn't properly unroll the NLA packet and assumes that the start
388          * of the taskstats structure will always be 20 bytes from the start
389          * of the netlink payload. Aligning the start of the taskstats
390          * structure breaks this software, which we don't want. So, for now
391          * the alignment only happens on architectures that require it
392          * and those users will have to update to fixed versions of those
393          * packages. Space is reserved in the packet only when needed.
394          * This ifdef should be removed in several years e.g. 2012 once
395          * we can be confident that fixed versions are installed on most
396          * systems. We add the padding before the aggregate since the
397          * aggregate is already a defined type.
398          */
399 #ifdef TASKSTATS_NEEDS_PADDING
400         if (nla_put(skb, TASKSTATS_TYPE_NULL, 0, NULL) < 0)
401                 goto err;
402 #endif
403         na = nla_nest_start(skb, aggr);
404         if (!na)
405                 goto err;
406
407         if (nla_put(skb, type, sizeof(pid), &pid) < 0) {
408                 nla_nest_cancel(skb, na);
409                 goto err;
410         }
411         ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
412         if (!ret) {
413                 nla_nest_cancel(skb, na);
414                 goto err;
415         }
416         nla_nest_end(skb, na);
417
418         return nla_data(ret);
419 err:
420         return NULL;
421 }
422
423 static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
424 {
425         int rc = 0;
426         struct sk_buff *rep_skb;
427         struct cgroupstats *stats;
428         struct nlattr *na;
429         size_t size;
430         u32 fd;
431         struct fd f;
432
433         na = info->attrs[CGROUPSTATS_CMD_ATTR_FD];
434         if (!na)
435                 return -EINVAL;
436
437         fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]);
438         f = fdget(fd);
439         if (!f.file)
440                 return 0;
441
442         size = nla_total_size(sizeof(struct cgroupstats));
443
444         rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb,
445                                 size);
446         if (rc < 0)
447                 goto err;
448
449         na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS,
450                                 sizeof(struct cgroupstats));
451         if (na == NULL) {
452                 nlmsg_free(rep_skb);
453                 rc = -EMSGSIZE;
454                 goto err;
455         }
456
457         stats = nla_data(na);
458         memset(stats, 0, sizeof(*stats));
459
460         rc = cgroupstats_build(stats, f.file->f_dentry);
461         if (rc < 0) {
462                 nlmsg_free(rep_skb);
463                 goto err;
464         }
465
466         rc = send_reply(rep_skb, info);
467
468 err:
469         fdput(f);
470         return rc;
471 }
472
473 static int cmd_attr_register_cpumask(struct genl_info *info)
474 {
475         cpumask_var_t mask;
476         int rc;
477
478         if (!alloc_cpumask_var(&mask, GFP_KERNEL))
479                 return -ENOMEM;
480         rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask);
481         if (rc < 0)
482                 goto out;
483         rc = add_del_listener(info->snd_portid, mask, REGISTER);
484 out:
485         free_cpumask_var(mask);
486         return rc;
487 }
488
489 static int cmd_attr_deregister_cpumask(struct genl_info *info)
490 {
491         cpumask_var_t mask;
492         int rc;
493
494         if (!alloc_cpumask_var(&mask, GFP_KERNEL))
495                 return -ENOMEM;
496         rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask);
497         if (rc < 0)
498                 goto out;
499         rc = add_del_listener(info->snd_portid, mask, DEREGISTER);
500 out:
501         free_cpumask_var(mask);
502         return rc;
503 }
504
505 static size_t taskstats_packet_size(void)
506 {
507         size_t size;
508
509         size = nla_total_size(sizeof(u32)) +
510                 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
511 #ifdef TASKSTATS_NEEDS_PADDING
512         size += nla_total_size(0); /* Padding for alignment */
513 #endif
514         return size;
515 }
516
517 static int cmd_attr_pid(struct genl_info *info)
518 {
519         struct taskstats *stats;
520         struct sk_buff *rep_skb;
521         size_t size;
522         u32 pid;
523         int rc;
524
525         size = taskstats_packet_size();
526
527         rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
528         if (rc < 0)
529                 return rc;
530
531         rc = -EINVAL;
532         pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
533         stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
534         if (!stats)
535                 goto err;
536
537         rc = fill_stats_for_pid(pid, stats);
538         if (rc < 0)
539                 goto err;
540         return send_reply(rep_skb, info);
541 err:
542         nlmsg_free(rep_skb);
543         return rc;
544 }
545
546 static int cmd_attr_tgid(struct genl_info *info)
547 {
548         struct taskstats *stats;
549         struct sk_buff *rep_skb;
550         size_t size;
551         u32 tgid;
552         int rc;
553
554         size = taskstats_packet_size();
555
556         rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
557         if (rc < 0)
558                 return rc;
559
560         rc = -EINVAL;
561         tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
562         stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
563         if (!stats)
564                 goto err;
565
566         rc = fill_stats_for_tgid(tgid, stats);
567         if (rc < 0)
568                 goto err;
569         return send_reply(rep_skb, info);
570 err:
571         nlmsg_free(rep_skb);
572         return rc;
573 }
574
575 static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
576 {
577         if (info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK])
578                 return cmd_attr_register_cpumask(info);
579         else if (info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK])
580                 return cmd_attr_deregister_cpumask(info);
581         else if (info->attrs[TASKSTATS_CMD_ATTR_PID])
582                 return cmd_attr_pid(info);
583         else if (info->attrs[TASKSTATS_CMD_ATTR_TGID])
584                 return cmd_attr_tgid(info);
585         else
586                 return -EINVAL;
587 }
588
589 static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
590 {
591         struct signal_struct *sig = tsk->signal;
592         struct taskstats *stats;
593
594         if (sig->stats || thread_group_empty(tsk))
595                 goto ret;
596
597         /* No problem if kmem_cache_zalloc() fails */
598         stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
599
600         spin_lock_irq(&tsk->sighand->siglock);
601         if (!sig->stats) {
602                 sig->stats = stats;
603                 stats = NULL;
604         }
605         spin_unlock_irq(&tsk->sighand->siglock);
606
607         if (stats)
608                 kmem_cache_free(taskstats_cache, stats);
609 ret:
610         return sig->stats;
611 }
612
613 /* Send pid data out on exit */
614 void taskstats_exit(struct task_struct *tsk, int group_dead)
615 {
616         int rc;
617         struct listener_list *listeners;
618         struct taskstats *stats;
619         struct sk_buff *rep_skb;
620         size_t size;
621         int is_thread_group;
622
623         if (!family_registered)
624                 return;
625
626         /*
627          * Size includes space for nested attributes
628          */
629         size = taskstats_packet_size();
630
631         is_thread_group = !!taskstats_tgid_alloc(tsk);
632         if (is_thread_group) {
633                 /* PID + STATS + TGID + STATS */
634                 size = 2 * size;
635                 /* fill the tsk->signal->stats structure */
636                 fill_tgid_exit(tsk);
637         }
638
639         listeners = __this_cpu_ptr(&listener_array);
640         if (list_empty(&listeners->list))
641                 return;
642
643         rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
644         if (rc < 0)
645                 return;
646
647         stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID,
648                          task_pid_nr_ns(tsk, &init_pid_ns));
649         if (!stats)
650                 goto err;
651
652         fill_stats(&init_user_ns, &init_pid_ns, tsk, stats);
653
654         /*
655          * Doesn't matter if tsk is the leader or the last group member leaving
656          */
657         if (!is_thread_group || !group_dead)
658                 goto send;
659
660         stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID,
661                          task_tgid_nr_ns(tsk, &init_pid_ns));
662         if (!stats)
663                 goto err;
664
665         memcpy(stats, tsk->signal->stats, sizeof(*stats));
666
667 send:
668         send_cpu_listeners(rep_skb, listeners);
669         return;
670 err:
671         nlmsg_free(rep_skb);
672 }
673
674 static struct genl_ops taskstats_ops = {
675         .cmd            = TASKSTATS_CMD_GET,
676         .doit           = taskstats_user_cmd,
677         .policy         = taskstats_cmd_get_policy,
678         .flags          = GENL_ADMIN_PERM,
679 };
680
681 static struct genl_ops cgroupstats_ops = {
682         .cmd            = CGROUPSTATS_CMD_GET,
683         .doit           = cgroupstats_user_cmd,
684         .policy         = cgroupstats_cmd_get_policy,
685 };
686
687 /* Needed early in initialization */
688 void __init taskstats_init_early(void)
689 {
690         unsigned int i;
691
692         taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
693         for_each_possible_cpu(i) {
694                 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
695                 init_rwsem(&(per_cpu(listener_array, i).sem));
696         }
697 }
698
699 static int __init taskstats_init(void)
700 {
701         int rc;
702
703         rc = genl_register_family(&family);
704         if (rc)
705                 return rc;
706
707         rc = genl_register_ops(&family, &taskstats_ops);
708         if (rc < 0)
709                 goto err;
710
711         rc = genl_register_ops(&family, &cgroupstats_ops);
712         if (rc < 0)
713                 goto err_cgroup_ops;
714
715         family_registered = 1;
716         pr_info("registered taskstats version %d\n", TASKSTATS_GENL_VERSION);
717         return 0;
718 err_cgroup_ops:
719         genl_unregister_ops(&family, &taskstats_ops);
720 err:
721         genl_unregister_family(&family);
722         return rc;
723 }
724
725 /*
726  * late initcall ensures initialization of statistics collection
727  * mechanisms precedes initialization of the taskstats interface
728  */
729 late_initcall(taskstats_init);