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