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