Blame view
kernel/taskstats.c
16.2 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> |
4bd6e32ac
|
30 |
#include <linux/pid_namespace.h> |
c757249af
|
31 |
#include <net/genetlink.h> |
60063497a
|
32 |
#include <linux/atomic.h> |
c757249af
|
33 |
|
f9fd8914c
|
34 35 36 37 38 |
/* * 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
|
39 |
static DEFINE_PER_CPU(__u32, taskstats_seqnum); |
c757249af
|
40 |
static int family_registered; |
e18b890bb
|
41 |
struct kmem_cache *taskstats_cache; |
c757249af
|
42 43 44 45 46 47 48 |
static struct genl_family family = { .id = GENL_ID_GENERATE, .name = TASKSTATS_GENL_NAME, .version = TASKSTATS_GENL_VERSION, .maxattr = TASKSTATS_CMD_ATTR_MAX, }; |
b54452b07
|
49 |
static const struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = { |
c757249af
|
50 51 |
[TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 }, [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 }, |
f9fd8914c
|
52 53 |
[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING }, [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },}; |
b54452b07
|
54 |
static const struct nla_policy cgroupstats_cmd_get_policy[CGROUPSTATS_CMD_ATTR_MAX+1] = { |
846c7bb05
|
55 56 |
[CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 }, }; |
f9fd8914c
|
57 58 59 |
struct listener { struct list_head list; pid_t pid; |
bb129994c
|
60 |
char valid; |
c757249af
|
61 |
}; |
f9fd8914c
|
62 63 64 65 66 67 68 69 70 71 72 |
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
|
73 74 |
static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp, |
371674853
|
75 |
size_t size) |
c757249af
|
76 77 78 79 80 81 82 |
{ struct sk_buff *skb; void *reply; /* * If new attributes are added, please revisit this allocation */ |
3dabc7157
|
83 |
skb = genlmsg_new(size, GFP_KERNEL); |
c757249af
|
84 85 86 87 |
if (!skb) return -ENOMEM; if (!info) { |
cd85fc58c
|
88 |
int seq = this_cpu_inc_return(taskstats_seqnum) - 1; |
c757249af
|
89 |
|
17c157c88
|
90 |
reply = genlmsg_put(skb, 0, seq, &family, 0, cmd); |
c757249af
|
91 |
} else |
17c157c88
|
92 |
reply = genlmsg_put_reply(skb, info, &family, 0, cmd); |
c757249af
|
93 94 95 96 97 98 |
if (reply == NULL) { nlmsg_free(skb); return -EINVAL; } *skbp = skb; |
c757249af
|
99 100 |
return 0; } |
f9fd8914c
|
101 102 103 |
/* * Send taskstats data in @skb to listener with nl_pid @pid */ |
134e63756
|
104 |
static int send_reply(struct sk_buff *skb, struct genl_info *info) |
c757249af
|
105 |
{ |
b529ccf27
|
106 |
struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb)); |
f9fd8914c
|
107 |
void *reply = genlmsg_data(genlhdr); |
c757249af
|
108 |
int rc; |
c757249af
|
109 110 111 112 113 |
rc = genlmsg_end(skb, reply); if (rc < 0) { nlmsg_free(skb); return rc; } |
134e63756
|
114 |
return genlmsg_reply(skb, info); |
c757249af
|
115 |
} |
f9fd8914c
|
116 117 118 |
/* * Send taskstats data in @skb to listeners registered for @cpu's exit data */ |
115085ea0
|
119 120 |
static void send_cpu_listeners(struct sk_buff *skb, struct listener_list *listeners) |
f9fd8914c
|
121 |
{ |
b529ccf27
|
122 |
struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb)); |
f9fd8914c
|
123 124 125 |
struct listener *s, *tmp; struct sk_buff *skb_next, *skb_cur = skb; void *reply = genlmsg_data(genlhdr); |
d94a04151
|
126 |
int rc, delcount = 0; |
f9fd8914c
|
127 128 129 130 |
rc = genlmsg_end(skb, reply); if (rc < 0) { nlmsg_free(skb); |
d94a04151
|
131 |
return; |
f9fd8914c
|
132 133 134 |
} rc = 0; |
bb129994c
|
135 |
down_read(&listeners->sem); |
d94a04151
|
136 |
list_for_each_entry(s, &listeners->list, list) { |
f9fd8914c
|
137 138 139 |
skb_next = NULL; if (!list_is_last(&s->list, &listeners->list)) { skb_next = skb_clone(skb_cur, GFP_KERNEL); |
d94a04151
|
140 |
if (!skb_next) |
f9fd8914c
|
141 |
break; |
f9fd8914c
|
142 |
} |
134e63756
|
143 |
rc = genlmsg_unicast(&init_net, skb_cur, s->pid); |
d94a04151
|
144 |
if (rc == -ECONNREFUSED) { |
bb129994c
|
145 146 |
s->valid = 0; delcount++; |
f9fd8914c
|
147 148 149 |
} skb_cur = skb_next; } |
bb129994c
|
150 |
up_read(&listeners->sem); |
f9fd8914c
|
151 |
|
d94a04151
|
152 153 |
if (skb_cur) nlmsg_free(skb_cur); |
bb129994c
|
154 |
if (!delcount) |
d94a04151
|
155 |
return; |
bb129994c
|
156 157 158 159 160 161 162 163 164 165 |
/* 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
|
166 |
} |
4bd6e32ac
|
167 168 169 |
static void fill_stats(struct user_namespace *user_ns, struct pid_namespace *pid_ns, struct task_struct *tsk, struct taskstats *stats) |
c757249af
|
170 |
{ |
51de4d908
|
171 |
memset(stats, 0, sizeof(*stats)); |
c757249af
|
172 173 174 175 |
/* * Each accounting subsystem adds calls to its functions to * fill in relevant parts of struct taskstsats as follows * |
7d94dddd4
|
176 |
* per-task-foo(stats, tsk); |
c757249af
|
177 |
*/ |
7d94dddd4
|
178 |
delayacct_add_tsk(stats, tsk); |
f3cef7a99
|
179 180 |
/* fill in basic acct fields */ |
6f44993fe
|
181 |
stats->version = TASKSTATS_VERSION; |
b663a79c1
|
182 183 |
stats->nvcsw = tsk->nvcsw; stats->nivcsw = tsk->nivcsw; |
4bd6e32ac
|
184 |
bacct_add_tsk(user_ns, pid_ns, stats, tsk); |
6f44993fe
|
185 |
|
9acc18535
|
186 187 |
/* fill in extended acct fields */ xacct_add_tsk(stats, tsk); |
3d9e0cf1f
|
188 |
} |
9acc18535
|
189 |
|
3d9e0cf1f
|
190 191 192 |
static int fill_stats_for_pid(pid_t pid, struct taskstats *stats) { struct task_struct *tsk; |
c757249af
|
193 |
|
3d9e0cf1f
|
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; |
4bd6e32ac
|
201 |
fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats); |
3d9e0cf1f
|
202 203 |
put_task_struct(tsk); return 0; |
c757249af
|
204 |
} |
3d9e0cf1f
|
205 |
static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats) |
c757249af
|
206 |
{ |
3d9e0cf1f
|
207 |
struct task_struct *tsk, *first; |
ad4ecbcba
|
208 |
unsigned long flags; |
a98b60942
|
209 |
int rc = -ESRCH; |
c757249af
|
210 |
|
ad4ecbcba
|
211 212 213 214 |
/* * Add additional stats from live tasks except zombie thread group * leaders who are already counted with the dead tasks */ |
a98b60942
|
215 |
rcu_read_lock(); |
3d9e0cf1f
|
216 |
first = find_task_by_vpid(tgid); |
ad4ecbcba
|
217 |
|
a98b60942
|
218 219 |
if (!first || !lock_task_sighand(first, &flags)) goto out; |
ad4ecbcba
|
220 |
|
a98b60942
|
221 222 |
if (first->signal->stats) memcpy(stats, first->signal->stats, sizeof(*stats)); |
51de4d908
|
223 224 |
else memset(stats, 0, sizeof(*stats)); |
fca178c0c
|
225 |
|
a98b60942
|
226 |
tsk = first; |
c757249af
|
227 |
do { |
d7c3f5f23
|
228 |
if (tsk->exit_state) |
ad4ecbcba
|
229 |
continue; |
c757249af
|
230 |
/* |
ad4ecbcba
|
231 |
* Accounting subsystem can call its functions here to |
c757249af
|
232 233 |
* fill in relevant parts of struct taskstsats as follows * |
ad4ecbcba
|
234 |
* per-task-foo(stats, tsk); |
c757249af
|
235 |
*/ |
ad4ecbcba
|
236 |
delayacct_add_tsk(stats, tsk); |
6f44993fe
|
237 |
|
b663a79c1
|
238 239 |
stats->nvcsw += tsk->nvcsw; stats->nivcsw += tsk->nivcsw; |
c757249af
|
240 |
} while_each_thread(first, tsk); |
6f44993fe
|
241 |
|
a98b60942
|
242 243 244 245 246 247 |
unlock_task_sighand(first, &flags); rc = 0; out: rcu_read_unlock(); stats->version = TASKSTATS_VERSION; |
c757249af
|
248 |
/* |
3a4fa0a25
|
249 |
* Accounting subsystems can also add calls here to modify |
ad4ecbcba
|
250 |
* fields of taskstats. |
c757249af
|
251 |
*/ |
a98b60942
|
252 |
return rc; |
ad4ecbcba
|
253 |
} |
ad4ecbcba
|
254 255 256 |
static void fill_tgid_exit(struct task_struct *tsk) { unsigned long flags; |
b8534d7bd
|
257 |
spin_lock_irqsave(&tsk->sighand->siglock, flags); |
ad4ecbcba
|
258 259 260 261 262 263 264 265 266 267 268 |
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
|
269 |
spin_unlock_irqrestore(&tsk->sighand->siglock, flags); |
ad4ecbcba
|
270 |
return; |
c757249af
|
271 |
} |
41c7bb958
|
272 |
static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd) |
f9fd8914c
|
273 274 |
{ struct listener_list *listeners; |
26c4caea9
|
275 |
struct listener *s, *tmp, *s2; |
f9fd8914c
|
276 |
unsigned int cpu; |
0d20633b0
|
277 |
int ret = 0; |
ad4ecbcba
|
278 |
|
41c7bb958
|
279 |
if (!cpumask_subset(mask, cpu_possible_mask)) |
f9fd8914c
|
280 |
return -EINVAL; |
4bd6e32ac
|
281 282 283 284 285 |
if (current_user_ns() != &init_user_ns) return -EINVAL; if (task_active_pid_ns(current) != &init_pid_ns) return -EINVAL; |
f9fd8914c
|
286 |
if (isadd == REGISTER) { |
41c7bb958
|
287 |
for_each_cpu(cpu, mask) { |
dfc428b65
|
288 289 |
s = kmalloc_node(sizeof(struct listener), GFP_KERNEL, cpu_to_node(cpu)); |
0d20633b0
|
290 291 |
if (!s) { ret = -ENOMEM; |
f9fd8914c
|
292 |
goto cleanup; |
0d20633b0
|
293 |
} |
f9fd8914c
|
294 |
s->pid = pid; |
bb129994c
|
295 |
s->valid = 1; |
f9fd8914c
|
296 297 298 |
listeners = &per_cpu(listener_array, cpu); down_write(&listeners->sem); |
dfc428b65
|
299 |
list_for_each_entry(s2, &listeners->list, list) { |
a7295898a
|
300 |
if (s2->pid == pid && s2->valid) |
dfc428b65
|
301 |
goto exists; |
26c4caea9
|
302 |
} |
f9fd8914c
|
303 |
list_add(&s->list, &listeners->list); |
26c4caea9
|
304 |
s = NULL; |
dfc428b65
|
305 |
exists: |
f9fd8914c
|
306 |
up_write(&listeners->sem); |
dfc428b65
|
307 |
kfree(s); /* nop if NULL */ |
f9fd8914c
|
308 309 310 311 312 313 |
} return 0; } /* Deregister or cleanup */ cleanup: |
41c7bb958
|
314 |
for_each_cpu(cpu, mask) { |
f9fd8914c
|
315 316 317 318 319 320 321 322 323 324 325 |
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); } |
0d20633b0
|
326 |
return ret; |
f9fd8914c
|
327 |
} |
41c7bb958
|
328 |
static int parse(struct nlattr *na, struct cpumask *mask) |
f9fd8914c
|
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
{ 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
|
345 |
ret = cpulist_parse(data, mask); |
f9fd8914c
|
346 347 348 |
kfree(data); return ret; } |
9ab020cf0
|
349 |
#if defined(CONFIG_64BIT) && !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) |
4be2c95d1
|
350 351 |
#define TASKSTATS_NEEDS_PADDING 1 #endif |
51de4d908
|
352 |
static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid) |
68062b86f
|
353 |
{ |
51de4d908
|
354 |
struct nlattr *na, *ret; |
68062b86f
|
355 |
int aggr; |
371674853
|
356 357 358 |
aggr = (type == TASKSTATS_TYPE_PID) ? TASKSTATS_TYPE_AGGR_PID : TASKSTATS_TYPE_AGGR_TGID; |
68062b86f
|
359 |
|
4be2c95d1
|
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
/* * 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
|
383 |
na = nla_nest_start(skb, aggr); |
371674853
|
384 385 |
if (!na) goto err; |
4be2c95d1
|
386 |
|
3fa582663
|
387 388 |
if (nla_put(skb, type, sizeof(pid), &pid) < 0) { nla_nest_cancel(skb, na); |
51de4d908
|
389 |
goto err; |
3fa582663
|
390 |
} |
51de4d908
|
391 |
ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats)); |
3fa582663
|
392 393 |
if (!ret) { nla_nest_cancel(skb, na); |
51de4d908
|
394 |
goto err; |
3fa582663
|
395 |
} |
68062b86f
|
396 |
nla_nest_end(skb, na); |
51de4d908
|
397 398 399 |
return nla_data(ret); err: return NULL; |
68062b86f
|
400 |
} |
846c7bb05
|
401 402 403 404 405 406 407 408 |
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; |
2903ff019
|
409 |
struct fd f; |
846c7bb05
|
410 411 412 413 414 415 |
na = info->attrs[CGROUPSTATS_CMD_ATTR_FD]; if (!na) return -EINVAL; fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]); |
2903ff019
|
416 417 |
f = fdget(fd); if (!f.file) |
f96159840
|
418 |
return 0; |
846c7bb05
|
419 |
|
f96159840
|
420 |
size = nla_total_size(sizeof(struct cgroupstats)); |
846c7bb05
|
421 |
|
f96159840
|
422 423 424 425 |
rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb, size); if (rc < 0) goto err; |
846c7bb05
|
426 |
|
f96159840
|
427 428 |
na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS, sizeof(struct cgroupstats)); |
25353b337
|
429 |
if (na == NULL) { |
0324b5a45
|
430 |
nlmsg_free(rep_skb); |
25353b337
|
431 432 433 |
rc = -EMSGSIZE; goto err; } |
f96159840
|
434 435 |
stats = nla_data(na); memset(stats, 0, sizeof(*stats)); |
846c7bb05
|
436 |
|
b583043e9
|
437 |
rc = cgroupstats_build(stats, f.file->f_path.dentry); |
f96159840
|
438 439 440 |
if (rc < 0) { nlmsg_free(rep_skb); goto err; |
846c7bb05
|
441 |
} |
134e63756
|
442 |
rc = send_reply(rep_skb, info); |
f96159840
|
443 |
|
846c7bb05
|
444 |
err: |
2903ff019
|
445 |
fdput(f); |
846c7bb05
|
446 447 |
return rc; } |
932331259
|
448 |
static int cmd_attr_register_cpumask(struct genl_info *info) |
c757249af
|
449 |
{ |
41c7bb958
|
450 |
cpumask_var_t mask; |
932331259
|
451 |
int rc; |
41c7bb958
|
452 453 454 |
if (!alloc_cpumask_var(&mask, GFP_KERNEL)) return -ENOMEM; |
41c7bb958
|
455 |
rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask); |
f9fd8914c
|
456 |
if (rc < 0) |
932331259
|
457 |
goto out; |
15e473046
|
458 |
rc = add_del_listener(info->snd_portid, mask, REGISTER); |
932331259
|
459 460 461 462 463 464 465 466 467 |
out: free_cpumask_var(mask); return rc; } static int cmd_attr_deregister_cpumask(struct genl_info *info) { cpumask_var_t mask; int rc; |
f9fd8914c
|
468 |
|
932331259
|
469 470 |
if (!alloc_cpumask_var(&mask, GFP_KERNEL)) return -ENOMEM; |
41c7bb958
|
471 |
rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask); |
f9fd8914c
|
472 |
if (rc < 0) |
932331259
|
473 |
goto out; |
15e473046
|
474 |
rc = add_del_listener(info->snd_portid, mask, DEREGISTER); |
932331259
|
475 |
out: |
41c7bb958
|
476 |
free_cpumask_var(mask); |
932331259
|
477 478 |
return rc; } |
4be2c95d1
|
479 480 481 482 483 484 485 486 487 488 489 |
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
|
490 491 492 493 494 495 496 |
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
|
497 |
|
4be2c95d1
|
498 |
size = taskstats_packet_size(); |
c757249af
|
499 |
|
371674853
|
500 |
rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size); |
c757249af
|
501 502 |
if (rc < 0) return rc; |
51de4d908
|
503 |
rc = -EINVAL; |
932331259
|
504 505 506 |
pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]); stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid); if (!stats) |
c757249af
|
507 |
goto err; |
c757249af
|
508 |
|
3d9e0cf1f
|
509 |
rc = fill_stats_for_pid(pid, stats); |
932331259
|
510 511 |
if (rc < 0) goto err; |
134e63756
|
512 |
return send_reply(rep_skb, info); |
c757249af
|
513 514 515 516 |
err: nlmsg_free(rep_skb); return rc; } |
932331259
|
517 518 519 520 521 522 523 |
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
|
524 |
size = taskstats_packet_size(); |
932331259
|
525 526 527 528 529 530 531 532 533 534 |
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
|
535 |
rc = fill_stats_for_tgid(tgid, stats); |
932331259
|
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
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
|
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
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
|
580 |
/* Send pid data out on exit */ |
115085ea0
|
581 |
void taskstats_exit(struct task_struct *tsk, int group_dead) |
c757249af
|
582 583 |
{ int rc; |
115085ea0
|
584 |
struct listener_list *listeners; |
51de4d908
|
585 |
struct taskstats *stats; |
c757249af
|
586 |
struct sk_buff *rep_skb; |
c757249af
|
587 588 |
size_t size; int is_thread_group; |
c757249af
|
589 |
|
4a279ff1e
|
590 |
if (!family_registered) |
c757249af
|
591 |
return; |
c757249af
|
592 593 594 |
/* * Size includes space for nested attributes */ |
4be2c95d1
|
595 |
size = taskstats_packet_size(); |
c757249af
|
596 |
|
34ec12349
|
597 |
is_thread_group = !!taskstats_tgid_alloc(tsk); |
4a279ff1e
|
598 599 600 601 602 603 |
if (is_thread_group) { /* PID + STATS + TGID + STATS */ size = 2 * size; /* fill the tsk->signal->stats structure */ fill_tgid_exit(tsk); } |
4a32fea9d
|
604 |
listeners = raw_cpu_ptr(&listener_array); |
115085ea0
|
605 606 |
if (list_empty(&listeners->list)) return; |
371674853
|
607 |
rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size); |
c757249af
|
608 |
if (rc < 0) |
51de4d908
|
609 |
return; |
c757249af
|
610 |
|
4bd6e32ac
|
611 612 |
stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, task_pid_nr_ns(tsk, &init_pid_ns)); |
51de4d908
|
613 |
if (!stats) |
371674853
|
614 |
goto err; |
c757249af
|
615 |
|
4bd6e32ac
|
616 |
fill_stats(&init_user_ns, &init_pid_ns, tsk, stats); |
c757249af
|
617 |
|
c757249af
|
618 |
/* |
ad4ecbcba
|
619 |
* Doesn't matter if tsk is the leader or the last group member leaving |
c757249af
|
620 |
*/ |
68062b86f
|
621 |
if (!is_thread_group || !group_dead) |
ad4ecbcba
|
622 |
goto send; |
c757249af
|
623 |
|
4bd6e32ac
|
624 625 |
stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, task_tgid_nr_ns(tsk, &init_pid_ns)); |
51de4d908
|
626 |
if (!stats) |
371674853
|
627 |
goto err; |
51de4d908
|
628 629 |
memcpy(stats, tsk->signal->stats, sizeof(*stats)); |
c757249af
|
630 |
|
ad4ecbcba
|
631 |
send: |
115085ea0
|
632 |
send_cpu_listeners(rep_skb, listeners); |
ad4ecbcba
|
633 |
return; |
371674853
|
634 |
err: |
c757249af
|
635 |
nlmsg_free(rep_skb); |
c757249af
|
636 |
} |
4534de830
|
637 |
static const struct genl_ops taskstats_ops[] = { |
88d36a994
|
638 639 640 641 642 643 644 645 646 647 648 |
{ .cmd = TASKSTATS_CMD_GET, .doit = taskstats_user_cmd, .policy = taskstats_cmd_get_policy, .flags = GENL_ADMIN_PERM, }, { .cmd = CGROUPSTATS_CMD_GET, .doit = cgroupstats_user_cmd, .policy = cgroupstats_cmd_get_policy, }, |
846c7bb05
|
649 |
}; |
c757249af
|
650 651 652 |
/* Needed early in initialization */ void __init taskstats_init_early(void) { |
f9fd8914c
|
653 |
unsigned int i; |
0a31bd5f2
|
654 |
taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC); |
f9fd8914c
|
655 656 657 658 |
for_each_possible_cpu(i) { INIT_LIST_HEAD(&(per_cpu(listener_array, i).list)); init_rwsem(&(per_cpu(listener_array, i).sem)); } |
c757249af
|
659 660 661 662 663 |
} static int __init taskstats_init(void) { int rc; |
c53ed7423
|
664 |
rc = genl_register_family_with_ops(&family, taskstats_ops); |
c757249af
|
665 666 |
if (rc) return rc; |
c757249af
|
667 |
family_registered = 1; |
f9b182e24
|
668 669 |
pr_info("registered taskstats version %d ", TASKSTATS_GENL_VERSION); |
c757249af
|
670 |
return 0; |
c757249af
|
671 672 673 674 675 676 677 |
} /* * late initcall ensures initialization of statistics collection * mechanisms precedes initialization of the taskstats interface */ late_initcall(taskstats_init); |