[PATCH] make taskstats sending completely independent of delay accounting on/off...
[linux-2.6] / 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/delayacct.h>
22 #include <linux/cpumask.h>
23 #include <linux/percpu.h>
24 #include <net/genetlink.h>
25 #include <asm/atomic.h>
26
27 /*
28  * Maximum length of a cpumask that can be specified in
29  * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
30  */
31 #define TASKSTATS_CPUMASK_MAXLEN        (100+6*NR_CPUS)
32
33 static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
34 static int family_registered;
35 kmem_cache_t *taskstats_cache;
36
37 static struct genl_family family = {
38         .id             = GENL_ID_GENERATE,
39         .name           = TASKSTATS_GENL_NAME,
40         .version        = TASKSTATS_GENL_VERSION,
41         .maxattr        = TASKSTATS_CMD_ATTR_MAX,
42 };
43
44 static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
45 __read_mostly = {
46         [TASKSTATS_CMD_ATTR_PID]  = { .type = NLA_U32 },
47         [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
48         [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
49         [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
50
51 struct listener {
52         struct list_head list;
53         pid_t pid;
54         char valid;
55 };
56
57 struct listener_list {
58         struct rw_semaphore sem;
59         struct list_head list;
60 };
61 static DEFINE_PER_CPU(struct listener_list, listener_array);
62
63 enum actions {
64         REGISTER,
65         DEREGISTER,
66         CPU_DONT_CARE
67 };
68
69 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
70                         void **replyp, size_t size)
71 {
72         struct sk_buff *skb;
73         void *reply;
74
75         /*
76          * If new attributes are added, please revisit this allocation
77          */
78         skb = nlmsg_new(size);
79         if (!skb)
80                 return -ENOMEM;
81
82         if (!info) {
83                 int seq = get_cpu_var(taskstats_seqnum)++;
84                 put_cpu_var(taskstats_seqnum);
85
86                 reply = genlmsg_put(skb, 0, seq,
87                                 family.id, 0, 0,
88                                 cmd, family.version);
89         } else
90                 reply = genlmsg_put(skb, info->snd_pid, info->snd_seq,
91                                 family.id, 0, 0,
92                                 cmd, family.version);
93         if (reply == NULL) {
94                 nlmsg_free(skb);
95                 return -EINVAL;
96         }
97
98         *skbp = skb;
99         *replyp = reply;
100         return 0;
101 }
102
103 /*
104  * Send taskstats data in @skb to listener with nl_pid @pid
105  */
106 static int send_reply(struct sk_buff *skb, pid_t pid)
107 {
108         struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
109         void *reply = genlmsg_data(genlhdr);
110         int rc;
111
112         rc = genlmsg_end(skb, reply);
113         if (rc < 0) {
114                 nlmsg_free(skb);
115                 return rc;
116         }
117
118         return genlmsg_unicast(skb, pid);
119 }
120
121 /*
122  * Send taskstats data in @skb to listeners registered for @cpu's exit data
123  */
124 static int send_cpu_listeners(struct sk_buff *skb, unsigned int cpu)
125 {
126         struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
127         struct listener_list *listeners;
128         struct listener *s, *tmp;
129         struct sk_buff *skb_next, *skb_cur = skb;
130         void *reply = genlmsg_data(genlhdr);
131         int rc, ret, delcount = 0;
132
133         rc = genlmsg_end(skb, reply);
134         if (rc < 0) {
135                 nlmsg_free(skb);
136                 return rc;
137         }
138
139         rc = 0;
140         listeners = &per_cpu(listener_array, cpu);
141         down_read(&listeners->sem);
142         list_for_each_entry_safe(s, tmp, &listeners->list, list) {
143                 skb_next = NULL;
144                 if (!list_is_last(&s->list, &listeners->list)) {
145                         skb_next = skb_clone(skb_cur, GFP_KERNEL);
146                         if (!skb_next) {
147                                 nlmsg_free(skb_cur);
148                                 rc = -ENOMEM;
149                                 break;
150                         }
151                 }
152                 ret = genlmsg_unicast(skb_cur, s->pid);
153                 if (ret == -ECONNREFUSED) {
154                         s->valid = 0;
155                         delcount++;
156                         rc = ret;
157                 }
158                 skb_cur = skb_next;
159         }
160         up_read(&listeners->sem);
161
162         if (!delcount)
163                 return rc;
164
165         /* Delete invalidated entries */
166         down_write(&listeners->sem);
167         list_for_each_entry_safe(s, tmp, &listeners->list, list) {
168                 if (!s->valid) {
169                         list_del(&s->list);
170                         kfree(s);
171                 }
172         }
173         up_write(&listeners->sem);
174         return rc;
175 }
176
177 static int fill_pid(pid_t pid, struct task_struct *pidtsk,
178                 struct taskstats *stats)
179 {
180         int rc = 0;
181         struct task_struct *tsk = pidtsk;
182
183         if (!pidtsk) {
184                 read_lock(&tasklist_lock);
185                 tsk = find_task_by_pid(pid);
186                 if (!tsk) {
187                         read_unlock(&tasklist_lock);
188                         return -ESRCH;
189                 }
190                 get_task_struct(tsk);
191                 read_unlock(&tasklist_lock);
192         } else
193                 get_task_struct(tsk);
194
195         /*
196          * Each accounting subsystem adds calls to its functions to
197          * fill in relevant parts of struct taskstsats as follows
198          *
199          *      per-task-foo(stats, tsk);
200          */
201
202         delayacct_add_tsk(stats, tsk);
203         stats->version = TASKSTATS_VERSION;
204
205         /* Define err: label here if needed */
206         put_task_struct(tsk);
207         return rc;
208
209 }
210
211 static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk,
212                 struct taskstats *stats)
213 {
214         struct task_struct *tsk, *first;
215         unsigned long flags;
216
217         /*
218          * Add additional stats from live tasks except zombie thread group
219          * leaders who are already counted with the dead tasks
220          */
221         first = tgidtsk;
222         if (!first) {
223                 read_lock(&tasklist_lock);
224                 first = find_task_by_pid(tgid);
225                 if (!first) {
226                         read_unlock(&tasklist_lock);
227                         return -ESRCH;
228                 }
229                 get_task_struct(first);
230                 read_unlock(&tasklist_lock);
231         } else
232                 get_task_struct(first);
233
234         /* Start with stats from dead tasks */
235         spin_lock_irqsave(&first->signal->stats_lock, flags);
236         if (first->signal->stats)
237                 memcpy(stats, first->signal->stats, sizeof(*stats));
238         spin_unlock_irqrestore(&first->signal->stats_lock, flags);
239
240         tsk = first;
241         read_lock(&tasklist_lock);
242         do {
243                 if (tsk->exit_state == EXIT_ZOMBIE && thread_group_leader(tsk))
244                         continue;
245                 /*
246                  * Accounting subsystem can call its functions here to
247                  * fill in relevant parts of struct taskstsats as follows
248                  *
249                  *      per-task-foo(stats, tsk);
250                  */
251                 delayacct_add_tsk(stats, tsk);
252
253         } while_each_thread(first, tsk);
254         read_unlock(&tasklist_lock);
255         stats->version = TASKSTATS_VERSION;
256
257         /*
258          * Accounting subsytems can also add calls here to modify
259          * fields of taskstats.
260          */
261
262         return 0;
263 }
264
265
266 static void fill_tgid_exit(struct task_struct *tsk)
267 {
268         unsigned long flags;
269
270         spin_lock_irqsave(&tsk->signal->stats_lock, flags);
271         if (!tsk->signal->stats)
272                 goto ret;
273
274         /*
275          * Each accounting subsystem calls its functions here to
276          * accumalate its per-task stats for tsk, into the per-tgid structure
277          *
278          *      per-task-foo(tsk->signal->stats, tsk);
279          */
280         delayacct_add_tsk(tsk->signal->stats, tsk);
281 ret:
282         spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
283         return;
284 }
285
286 static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
287 {
288         struct listener_list *listeners;
289         struct listener *s, *tmp;
290         unsigned int cpu;
291         cpumask_t mask = *maskp;
292
293         if (!cpus_subset(mask, cpu_possible_map))
294                 return -EINVAL;
295
296         if (isadd == REGISTER) {
297                 for_each_cpu_mask(cpu, mask) {
298                         s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
299                                          cpu_to_node(cpu));
300                         if (!s)
301                                 goto cleanup;
302                         s->pid = pid;
303                         INIT_LIST_HEAD(&s->list);
304                         s->valid = 1;
305
306                         listeners = &per_cpu(listener_array, cpu);
307                         down_write(&listeners->sem);
308                         list_add(&s->list, &listeners->list);
309                         up_write(&listeners->sem);
310                 }
311                 return 0;
312         }
313
314         /* Deregister or cleanup */
315 cleanup:
316         for_each_cpu_mask(cpu, mask) {
317                 listeners = &per_cpu(listener_array, cpu);
318                 down_write(&listeners->sem);
319                 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
320                         if (s->pid == pid) {
321                                 list_del(&s->list);
322                                 kfree(s);
323                                 break;
324                         }
325                 }
326                 up_write(&listeners->sem);
327         }
328         return 0;
329 }
330
331 static int parse(struct nlattr *na, cpumask_t *mask)
332 {
333         char *data;
334         int len;
335         int ret;
336
337         if (na == NULL)
338                 return 1;
339         len = nla_len(na);
340         if (len > TASKSTATS_CPUMASK_MAXLEN)
341                 return -E2BIG;
342         if (len < 1)
343                 return -EINVAL;
344         data = kmalloc(len, GFP_KERNEL);
345         if (!data)
346                 return -ENOMEM;
347         nla_strlcpy(data, na, len);
348         ret = cpulist_parse(data, *mask);
349         kfree(data);
350         return ret;
351 }
352
353 static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
354 {
355         int rc = 0;
356         struct sk_buff *rep_skb;
357         struct taskstats stats;
358         void *reply;
359         size_t size;
360         struct nlattr *na;
361         cpumask_t mask;
362
363         rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
364         if (rc < 0)
365                 return rc;
366         if (rc == 0)
367                 return add_del_listener(info->snd_pid, &mask, REGISTER);
368
369         rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
370         if (rc < 0)
371                 return rc;
372         if (rc == 0)
373                 return add_del_listener(info->snd_pid, &mask, DEREGISTER);
374
375         /*
376          * Size includes space for nested attributes
377          */
378         size = nla_total_size(sizeof(u32)) +
379                 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
380
381         memset(&stats, 0, sizeof(stats));
382         rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
383         if (rc < 0)
384                 return rc;
385
386         if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
387                 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
388                 rc = fill_pid(pid, NULL, &stats);
389                 if (rc < 0)
390                         goto err;
391
392                 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
393                 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
394                 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
395                                 stats);
396         } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
397                 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
398                 rc = fill_tgid(tgid, NULL, &stats);
399                 if (rc < 0)
400                         goto err;
401
402                 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
403                 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
404                 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
405                                 stats);
406         } else {
407                 rc = -EINVAL;
408                 goto err;
409         }
410
411         nla_nest_end(rep_skb, na);
412
413         return send_reply(rep_skb, info->snd_pid);
414
415 nla_put_failure:
416         return genlmsg_cancel(rep_skb, reply);
417 err:
418         nlmsg_free(rep_skb);
419         return rc;
420 }
421
422 void taskstats_exit_alloc(struct taskstats **ptidstats, unsigned int *mycpu)
423 {
424         struct listener_list *listeners;
425         struct taskstats *tmp;
426         /*
427          * This is the cpu on which the task is exiting currently and will
428          * be the one for which the exit event is sent, even if the cpu
429          * on which this function is running changes later.
430          */
431         *mycpu = raw_smp_processor_id();
432
433         *ptidstats = NULL;
434         tmp = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
435         if (!tmp)
436                 return;
437
438         listeners = &per_cpu(listener_array, *mycpu);
439         down_read(&listeners->sem);
440         if (!list_empty(&listeners->list)) {
441                 *ptidstats = tmp;
442                 tmp = NULL;
443         }
444         up_read(&listeners->sem);
445         kfree(tmp);
446 }
447
448 /* Send pid data out on exit */
449 void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
450                         int group_dead, unsigned int mycpu)
451 {
452         int rc;
453         struct sk_buff *rep_skb;
454         void *reply;
455         size_t size;
456         int is_thread_group;
457         struct nlattr *na;
458         unsigned long flags;
459
460         if (!family_registered || !tidstats)
461                 return;
462
463         spin_lock_irqsave(&tsk->signal->stats_lock, flags);
464         is_thread_group = tsk->signal->stats ? 1 : 0;
465         spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
466
467         rc = 0;
468         /*
469          * Size includes space for nested attributes
470          */
471         size = nla_total_size(sizeof(u32)) +
472                 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
473
474         if (is_thread_group)
475                 size = 2 * size;        /* PID + STATS + TGID + STATS */
476
477         rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
478         if (rc < 0)
479                 goto ret;
480
481         rc = fill_pid(tsk->pid, tsk, tidstats);
482         if (rc < 0)
483                 goto err_skb;
484
485         na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
486         NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
487         NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
488                         *tidstats);
489         nla_nest_end(rep_skb, na);
490
491         if (!is_thread_group)
492                 goto send;
493
494         /*
495          * tsk has/had a thread group so fill the tsk->signal->stats structure
496          * Doesn't matter if tsk is the leader or the last group member leaving
497          */
498
499         fill_tgid_exit(tsk);
500         if (!group_dead)
501                 goto send;
502
503         na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
504         NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
505         /* No locking needed for tsk->signal->stats since group is dead */
506         NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
507                         *tsk->signal->stats);
508         nla_nest_end(rep_skb, na);
509
510 send:
511         send_cpu_listeners(rep_skb, mycpu);
512         return;
513
514 nla_put_failure:
515         genlmsg_cancel(rep_skb, reply);
516         goto ret;
517 err_skb:
518         nlmsg_free(rep_skb);
519 ret:
520         return;
521 }
522
523 static struct genl_ops taskstats_ops = {
524         .cmd            = TASKSTATS_CMD_GET,
525         .doit           = taskstats_user_cmd,
526         .policy         = taskstats_cmd_get_policy,
527 };
528
529 /* Needed early in initialization */
530 void __init taskstats_init_early(void)
531 {
532         unsigned int i;
533
534         taskstats_cache = kmem_cache_create("taskstats_cache",
535                                                 sizeof(struct taskstats),
536                                                 0, SLAB_PANIC, NULL, NULL);
537         for_each_possible_cpu(i) {
538                 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
539                 init_rwsem(&(per_cpu(listener_array, i).sem));
540         }
541 }
542
543 static int __init taskstats_init(void)
544 {
545         int rc;
546
547         rc = genl_register_family(&family);
548         if (rc)
549                 return rc;
550
551         rc = genl_register_ops(&family, &taskstats_ops);
552         if (rc < 0)
553                 goto err;
554
555         family_registered = 1;
556         return 0;
557 err:
558         genl_unregister_family(&family);
559         return rc;
560 }
561
562 /*
563  * late initcall ensures initialization of statistics collection
564  * mechanisms precedes initialization of the taskstats interface
565  */
566 late_initcall(taskstats_init);