[PATCH] taskstats_exit_alloc: optimize/simplify
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / taskstats.c
blobd9d7c35762388d763bb93901205a44c380a244ab
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/tsacct_kern.h>
24 #include <linux/cpumask.h>
25 #include <linux/percpu.h>
26 #include <net/genetlink.h>
27 #include <asm/atomic.h>
30 * Maximum length of a cpumask that can be specified in
31 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
33 #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
35 static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
36 static int family_registered;
37 struct kmem_cache *taskstats_cache;
39 static 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,
46 static 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 },
50 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
51 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
53 struct listener {
54 struct list_head list;
55 pid_t pid;
56 char valid;
59 struct listener_list {
60 struct rw_semaphore sem;
61 struct list_head list;
63 static DEFINE_PER_CPU(struct listener_list, listener_array);
65 enum actions {
66 REGISTER,
67 DEREGISTER,
68 CPU_DONT_CARE
71 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
72 void **replyp, size_t size)
74 struct sk_buff *skb;
75 void *reply;
78 * If new attributes are added, please revisit this allocation
80 skb = genlmsg_new(size, GFP_KERNEL);
81 if (!skb)
82 return -ENOMEM;
84 if (!info) {
85 int seq = get_cpu_var(taskstats_seqnum)++;
86 put_cpu_var(taskstats_seqnum);
88 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
89 } else
90 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
91 if (reply == NULL) {
92 nlmsg_free(skb);
93 return -EINVAL;
96 *skbp = skb;
97 *replyp = reply;
98 return 0;
102 * Send taskstats data in @skb to listener with nl_pid @pid
104 static int send_reply(struct sk_buff *skb, pid_t pid)
106 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
107 void *reply = genlmsg_data(genlhdr);
108 int rc;
110 rc = genlmsg_end(skb, reply);
111 if (rc < 0) {
112 nlmsg_free(skb);
113 return rc;
116 return genlmsg_unicast(skb, pid);
120 * Send taskstats data in @skb to listeners registered for @cpu's exit data
122 static void send_cpu_listeners(struct sk_buff *skb, unsigned int cpu)
124 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
125 struct listener_list *listeners;
126 struct listener *s, *tmp;
127 struct sk_buff *skb_next, *skb_cur = skb;
128 void *reply = genlmsg_data(genlhdr);
129 int rc, delcount = 0;
131 rc = genlmsg_end(skb, reply);
132 if (rc < 0) {
133 nlmsg_free(skb);
134 return;
137 rc = 0;
138 listeners = &per_cpu(listener_array, cpu);
139 down_read(&listeners->sem);
140 list_for_each_entry(s, &listeners->list, list) {
141 skb_next = NULL;
142 if (!list_is_last(&s->list, &listeners->list)) {
143 skb_next = skb_clone(skb_cur, GFP_KERNEL);
144 if (!skb_next)
145 break;
147 rc = genlmsg_unicast(skb_cur, s->pid);
148 if (rc == -ECONNREFUSED) {
149 s->valid = 0;
150 delcount++;
152 skb_cur = skb_next;
154 up_read(&listeners->sem);
156 if (skb_cur)
157 nlmsg_free(skb_cur);
159 if (!delcount)
160 return;
162 /* Delete invalidated entries */
163 down_write(&listeners->sem);
164 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
165 if (!s->valid) {
166 list_del(&s->list);
167 kfree(s);
170 up_write(&listeners->sem);
173 static int fill_pid(pid_t pid, struct task_struct *tsk,
174 struct taskstats *stats)
176 int rc = 0;
178 if (!tsk) {
179 rcu_read_lock();
180 tsk = find_task_by_pid(pid);
181 if (tsk)
182 get_task_struct(tsk);
183 rcu_read_unlock();
184 if (!tsk)
185 return -ESRCH;
186 } else
187 get_task_struct(tsk);
190 * Each accounting subsystem adds calls to its functions to
191 * fill in relevant parts of struct taskstsats as follows
193 * per-task-foo(stats, tsk);
196 delayacct_add_tsk(stats, tsk);
198 /* fill in basic acct fields */
199 stats->version = TASKSTATS_VERSION;
200 bacct_add_tsk(stats, tsk);
202 /* fill in extended acct fields */
203 xacct_add_tsk(stats, tsk);
205 /* Define err: label here if needed */
206 put_task_struct(tsk);
207 return rc;
211 static int fill_tgid(pid_t tgid, struct task_struct *first,
212 struct taskstats *stats)
214 struct task_struct *tsk;
215 unsigned long flags;
216 int rc = -ESRCH;
219 * Add additional stats from live tasks except zombie thread group
220 * leaders who are already counted with the dead tasks
222 rcu_read_lock();
223 if (!first)
224 first = find_task_by_pid(tgid);
226 if (!first || !lock_task_sighand(first, &flags))
227 goto out;
229 if (first->signal->stats)
230 memcpy(stats, first->signal->stats, sizeof(*stats));
232 tsk = first;
233 do {
234 if (tsk->exit_state)
235 continue;
237 * Accounting subsystem can call its functions here to
238 * fill in relevant parts of struct taskstsats as follows
240 * per-task-foo(stats, tsk);
242 delayacct_add_tsk(stats, tsk);
244 } while_each_thread(first, tsk);
246 unlock_task_sighand(first, &flags);
247 rc = 0;
248 out:
249 rcu_read_unlock();
251 stats->version = TASKSTATS_VERSION;
253 * Accounting subsytems can also add calls here to modify
254 * fields of taskstats.
256 return rc;
260 static void fill_tgid_exit(struct task_struct *tsk)
262 unsigned long flags;
264 spin_lock_irqsave(&tsk->sighand->siglock, flags);
265 if (!tsk->signal->stats)
266 goto ret;
269 * Each accounting subsystem calls its functions here to
270 * accumalate its per-task stats for tsk, into the per-tgid structure
272 * per-task-foo(tsk->signal->stats, tsk);
274 delayacct_add_tsk(tsk->signal->stats, tsk);
275 ret:
276 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
277 return;
280 static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
282 struct listener_list *listeners;
283 struct listener *s, *tmp;
284 unsigned int cpu;
285 cpumask_t mask = *maskp;
287 if (!cpus_subset(mask, cpu_possible_map))
288 return -EINVAL;
290 if (isadd == REGISTER) {
291 for_each_cpu_mask(cpu, mask) {
292 s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
293 cpu_to_node(cpu));
294 if (!s)
295 goto cleanup;
296 s->pid = pid;
297 INIT_LIST_HEAD(&s->list);
298 s->valid = 1;
300 listeners = &per_cpu(listener_array, cpu);
301 down_write(&listeners->sem);
302 list_add(&s->list, &listeners->list);
303 up_write(&listeners->sem);
305 return 0;
308 /* Deregister or cleanup */
309 cleanup:
310 for_each_cpu_mask(cpu, mask) {
311 listeners = &per_cpu(listener_array, cpu);
312 down_write(&listeners->sem);
313 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
314 if (s->pid == pid) {
315 list_del(&s->list);
316 kfree(s);
317 break;
320 up_write(&listeners->sem);
322 return 0;
325 static int parse(struct nlattr *na, cpumask_t *mask)
327 char *data;
328 int len;
329 int ret;
331 if (na == NULL)
332 return 1;
333 len = nla_len(na);
334 if (len > TASKSTATS_CPUMASK_MAXLEN)
335 return -E2BIG;
336 if (len < 1)
337 return -EINVAL;
338 data = kmalloc(len, GFP_KERNEL);
339 if (!data)
340 return -ENOMEM;
341 nla_strlcpy(data, na, len);
342 ret = cpulist_parse(data, *mask);
343 kfree(data);
344 return ret;
347 static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
349 int rc = 0;
350 struct sk_buff *rep_skb;
351 struct taskstats stats;
352 void *reply;
353 size_t size;
354 struct nlattr *na;
355 cpumask_t mask;
357 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
358 if (rc < 0)
359 return rc;
360 if (rc == 0)
361 return add_del_listener(info->snd_pid, &mask, REGISTER);
363 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
364 if (rc < 0)
365 return rc;
366 if (rc == 0)
367 return add_del_listener(info->snd_pid, &mask, DEREGISTER);
370 * Size includes space for nested attributes
372 size = nla_total_size(sizeof(u32)) +
373 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
375 memset(&stats, 0, sizeof(stats));
376 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
377 if (rc < 0)
378 return rc;
380 if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
381 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
382 rc = fill_pid(pid, NULL, &stats);
383 if (rc < 0)
384 goto err;
386 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
387 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
388 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
389 stats);
390 } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
391 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
392 rc = fill_tgid(tgid, NULL, &stats);
393 if (rc < 0)
394 goto err;
396 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
397 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
398 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
399 stats);
400 } else {
401 rc = -EINVAL;
402 goto err;
405 nla_nest_end(rep_skb, na);
407 return send_reply(rep_skb, info->snd_pid);
409 nla_put_failure:
410 rc = genlmsg_cancel(rep_skb, reply);
411 err:
412 nlmsg_free(rep_skb);
413 return rc;
416 void taskstats_exit_alloc(struct taskstats **ptidstats, unsigned int *mycpu)
418 struct listener_list *listeners;
420 * This is the cpu on which the task is exiting currently and will
421 * be the one for which the exit event is sent, even if the cpu
422 * on which this function is running changes later.
424 *mycpu = raw_smp_processor_id();
426 listeners = &per_cpu(listener_array, *mycpu);
428 *ptidstats = NULL;
429 if (!list_empty(&listeners->list))
430 *ptidstats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
433 /* Send pid data out on exit */
434 void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
435 int group_dead, unsigned int mycpu)
437 int rc;
438 struct sk_buff *rep_skb;
439 void *reply;
440 size_t size;
441 int is_thread_group;
442 struct nlattr *na;
444 if (!family_registered)
445 return;
448 * Size includes space for nested attributes
450 size = nla_total_size(sizeof(u32)) +
451 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
453 is_thread_group = (tsk->signal->stats != NULL);
454 if (is_thread_group) {
455 /* PID + STATS + TGID + STATS */
456 size = 2 * size;
457 /* fill the tsk->signal->stats structure */
458 fill_tgid_exit(tsk);
461 if (!tidstats)
462 return;
464 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
465 if (rc < 0)
466 goto ret;
468 rc = fill_pid(tsk->pid, tsk, tidstats);
469 if (rc < 0)
470 goto err_skb;
472 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
473 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
474 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
475 *tidstats);
476 nla_nest_end(rep_skb, na);
478 if (!is_thread_group)
479 goto send;
482 * Doesn't matter if tsk is the leader or the last group member leaving
484 if (!group_dead)
485 goto send;
487 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
488 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
489 /* No locking needed for tsk->signal->stats since group is dead */
490 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
491 *tsk->signal->stats);
492 nla_nest_end(rep_skb, na);
494 send:
495 send_cpu_listeners(rep_skb, mycpu);
496 return;
498 nla_put_failure:
499 genlmsg_cancel(rep_skb, reply);
500 err_skb:
501 nlmsg_free(rep_skb);
502 ret:
503 return;
506 static struct genl_ops taskstats_ops = {
507 .cmd = TASKSTATS_CMD_GET,
508 .doit = taskstats_user_cmd,
509 .policy = taskstats_cmd_get_policy,
512 /* Needed early in initialization */
513 void __init taskstats_init_early(void)
515 unsigned int i;
517 taskstats_cache = kmem_cache_create("taskstats_cache",
518 sizeof(struct taskstats),
519 0, SLAB_PANIC, NULL, NULL);
520 for_each_possible_cpu(i) {
521 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
522 init_rwsem(&(per_cpu(listener_array, i).sem));
526 static int __init taskstats_init(void)
528 int rc;
530 rc = genl_register_family(&family);
531 if (rc)
532 return rc;
534 rc = genl_register_ops(&family, &taskstats_ops);
535 if (rc < 0)
536 goto err;
538 family_registered = 1;
539 return 0;
540 err:
541 genl_unregister_family(&family);
542 return rc;
546 * late initcall ensures initialization of statistics collection
547 * mechanisms precedes initialization of the taskstats interface
549 late_initcall(taskstats_init);