ea9506de3b855fd82414102a63ce0a37b24d9881
[linux-2.6-microblaze.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/delayacct.h>
22 #include <net/genetlink.h>
23 #include <asm/atomic.h>
24
25 static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
26 static int family_registered;
27 kmem_cache_t *taskstats_cache;
28
29 static struct genl_family family = {
30         .id             = GENL_ID_GENERATE,
31         .name           = TASKSTATS_GENL_NAME,
32         .version        = TASKSTATS_GENL_VERSION,
33         .maxattr        = TASKSTATS_CMD_ATTR_MAX,
34 };
35
36 static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
37 __read_mostly = {
38         [TASKSTATS_CMD_ATTR_PID]  = { .type = NLA_U32 },
39         [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
40 };
41
42
43 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
44                         void **replyp, size_t size)
45 {
46         struct sk_buff *skb;
47         void *reply;
48
49         /*
50          * If new attributes are added, please revisit this allocation
51          */
52         skb = nlmsg_new(size);
53         if (!skb)
54                 return -ENOMEM;
55
56         if (!info) {
57                 int seq = get_cpu_var(taskstats_seqnum)++;
58                 put_cpu_var(taskstats_seqnum);
59
60                 reply = genlmsg_put(skb, 0, seq,
61                                 family.id, 0, 0,
62                                 cmd, family.version);
63         } else
64                 reply = genlmsg_put(skb, info->snd_pid, info->snd_seq,
65                                 family.id, 0, 0,
66                                 cmd, family.version);
67         if (reply == NULL) {
68                 nlmsg_free(skb);
69                 return -EINVAL;
70         }
71
72         *skbp = skb;
73         *replyp = reply;
74         return 0;
75 }
76
77 static int send_reply(struct sk_buff *skb, pid_t pid, int event)
78 {
79         struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
80         void *reply;
81         int rc;
82
83         reply = genlmsg_data(genlhdr);
84
85         rc = genlmsg_end(skb, reply);
86         if (rc < 0) {
87                 nlmsg_free(skb);
88                 return rc;
89         }
90
91         if (event == TASKSTATS_MSG_MULTICAST)
92                 return genlmsg_multicast(skb, pid, TASKSTATS_LISTEN_GROUP);
93         return genlmsg_unicast(skb, pid);
94 }
95
96 static int fill_pid(pid_t pid, struct task_struct *pidtsk,
97                 struct taskstats *stats)
98 {
99         int rc;
100         struct task_struct *tsk = pidtsk;
101
102         if (!pidtsk) {
103                 read_lock(&tasklist_lock);
104                 tsk = find_task_by_pid(pid);
105                 if (!tsk) {
106                         read_unlock(&tasklist_lock);
107                         return -ESRCH;
108                 }
109                 get_task_struct(tsk);
110                 read_unlock(&tasklist_lock);
111         } else
112                 get_task_struct(tsk);
113
114         /*
115          * Each accounting subsystem adds calls to its functions to
116          * fill in relevant parts of struct taskstsats as follows
117          *
118          *      rc = per-task-foo(stats, tsk);
119          *      if (rc)
120          *              goto err;
121          */
122
123         rc = delayacct_add_tsk(stats, tsk);
124         stats->version = TASKSTATS_VERSION;
125
126         /* Define err: label here if needed */
127         put_task_struct(tsk);
128         return rc;
129
130 }
131
132 static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk,
133                 struct taskstats *stats)
134 {
135         int rc;
136         struct task_struct *tsk, *first;
137
138         first = tgidtsk;
139         read_lock(&tasklist_lock);
140         if (!first) {
141                 first = find_task_by_pid(tgid);
142                 if (!first) {
143                         read_unlock(&tasklist_lock);
144                         return -ESRCH;
145                 }
146         }
147         tsk = first;
148         do {
149                 /*
150                  * Each accounting subsystem adds calls its functions to
151                  * fill in relevant parts of struct taskstsats as follows
152                  *
153                  *      rc = per-task-foo(stats, tsk);
154                  *      if (rc)
155                  *              break;
156                  */
157
158                 rc = delayacct_add_tsk(stats, tsk);
159                 if (rc)
160                         break;
161
162         } while_each_thread(first, tsk);
163         read_unlock(&tasklist_lock);
164         stats->version = TASKSTATS_VERSION;
165
166
167         /*
168          * Accounting subsytems can also add calls here if they don't
169          * wish to aggregate statistics for per-tgid stats
170          */
171
172         return rc;
173 }
174
175 static int taskstats_send_stats(struct sk_buff *skb, struct genl_info *info)
176 {
177         int rc = 0;
178         struct sk_buff *rep_skb;
179         struct taskstats stats;
180         void *reply;
181         size_t size;
182         struct nlattr *na;
183
184         /*
185          * Size includes space for nested attributes
186          */
187         size = nla_total_size(sizeof(u32)) +
188                 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
189
190         memset(&stats, 0, sizeof(stats));
191         rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
192         if (rc < 0)
193                 return rc;
194
195         if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
196                 u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
197                 rc = fill_pid(pid, NULL, &stats);
198                 if (rc < 0)
199                         goto err;
200
201                 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
202                 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
203                 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
204                                 stats);
205         } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
206                 u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
207                 rc = fill_tgid(tgid, NULL, &stats);
208                 if (rc < 0)
209                         goto err;
210
211                 na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
212                 NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
213                 NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
214                                 stats);
215         } else {
216                 rc = -EINVAL;
217                 goto err;
218         }
219
220         nla_nest_end(rep_skb, na);
221
222         return send_reply(rep_skb, info->snd_pid, TASKSTATS_MSG_UNICAST);
223
224 nla_put_failure:
225         return genlmsg_cancel(rep_skb, reply);
226 err:
227         nlmsg_free(rep_skb);
228         return rc;
229 }
230
231 /* Send pid data out on exit */
232 void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
233                         struct taskstats *tgidstats)
234 {
235         int rc;
236         struct sk_buff *rep_skb;
237         void *reply;
238         size_t size;
239         int is_thread_group;
240         struct nlattr *na;
241
242         if (!family_registered || !tidstats)
243                 return;
244
245         is_thread_group = !thread_group_empty(tsk);
246         rc = 0;
247
248         /*
249          * Size includes space for nested attributes
250          */
251         size = nla_total_size(sizeof(u32)) +
252                 nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
253
254         if (is_thread_group)
255                 size = 2 * size;        /* PID + STATS + TGID + STATS */
256
257         rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
258         if (rc < 0)
259                 goto ret;
260
261         rc = fill_pid(tsk->pid, tsk, tidstats);
262         if (rc < 0)
263                 goto err_skb;
264
265         na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
266         NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
267         NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
268                         *tidstats);
269         nla_nest_end(rep_skb, na);
270
271         if (!is_thread_group || !tgidstats) {
272                 send_reply(rep_skb, 0, TASKSTATS_MSG_MULTICAST);
273                 goto ret;
274         }
275
276         rc = fill_tgid(tsk->pid, tsk, tgidstats);
277         /*
278          * If fill_tgid() failed then one probable reason could be that the
279          * thread group leader has exited. fill_tgid() will fail, send out
280          * the pid statistics collected earlier.
281          */
282         if (rc < 0) {
283                 send_reply(rep_skb, 0, TASKSTATS_MSG_MULTICAST);
284                 goto ret;
285         }
286
287         na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
288         NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
289         NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
290                         *tgidstats);
291         nla_nest_end(rep_skb, na);
292
293         send_reply(rep_skb, 0, TASKSTATS_MSG_MULTICAST);
294         goto ret;
295
296 nla_put_failure:
297         genlmsg_cancel(rep_skb, reply);
298         goto ret;
299 err_skb:
300         nlmsg_free(rep_skb);
301 ret:
302         return;
303 }
304
305 static struct genl_ops taskstats_ops = {
306         .cmd            = TASKSTATS_CMD_GET,
307         .doit           = taskstats_send_stats,
308         .policy         = taskstats_cmd_get_policy,
309 };
310
311 /* Needed early in initialization */
312 void __init taskstats_init_early(void)
313 {
314         taskstats_cache = kmem_cache_create("taskstats_cache",
315                                                 sizeof(struct taskstats),
316                                                 0, SLAB_PANIC, NULL, NULL);
317 }
318
319 static int __init taskstats_init(void)
320 {
321         int rc;
322
323         rc = genl_register_family(&family);
324         if (rc)
325                 return rc;
326
327         rc = genl_register_ops(&family, &taskstats_ops);
328         if (rc < 0)
329                 goto err;
330
331         family_registered = 1;
332         return 0;
333 err:
334         genl_unregister_family(&family);
335         return rc;
336 }
337
338 /*
339  * late initcall ensures initialization of statistics collection
340  * mechanisms precedes initialization of the taskstats interface
341  */
342 late_initcall(taskstats_init);