]>
Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
c757249a SN |
2 | /* |
3 | * taskstats.c - Export per-task statistics to userland | |
4 | * | |
5 | * Copyright (C) Shailabh Nagar, IBM Corp. 2006 | |
6 | * (C) Balbir Singh, IBM Corp. 2006 | |
c757249a SN |
7 | */ |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/taskstats_kern.h> | |
f3cef7a9 | 11 | #include <linux/tsacct_kern.h> |
6f44993f | 12 | #include <linux/delayacct.h> |
f9fd8914 SN |
13 | #include <linux/cpumask.h> |
14 | #include <linux/percpu.h> | |
5a0e3ad6 | 15 | #include <linux/slab.h> |
846c7bb0 BS |
16 | #include <linux/cgroupstats.h> |
17 | #include <linux/cgroup.h> | |
18 | #include <linux/fs.h> | |
19 | #include <linux/file.h> | |
4bd6e32a | 20 | #include <linux/pid_namespace.h> |
c757249a | 21 | #include <net/genetlink.h> |
60063497 | 22 | #include <linux/atomic.h> |
8c733420 | 23 | #include <linux/sched/cputime.h> |
c757249a | 24 | |
f9fd8914 SN |
25 | /* |
26 | * Maximum length of a cpumask that can be specified in | |
27 | * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute | |
28 | */ | |
29 | #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS) | |
30 | ||
b81f3ea9 | 31 | static DEFINE_PER_CPU(__u32, taskstats_seqnum); |
c757249a | 32 | static int family_registered; |
e18b890b | 33 | struct kmem_cache *taskstats_cache; |
c757249a | 34 | |
489111e5 | 35 | static struct genl_family family; |
c757249a | 36 | |
7c1e0926 | 37 | static const struct nla_policy taskstats_cmd_get_policy[] = { |
c757249a SN |
38 | [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 }, |
39 | [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 }, | |
f9fd8914 SN |
40 | [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING }, |
41 | [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },}; | |
42 | ||
7c1e0926 | 43 | static const struct nla_policy cgroupstats_cmd_get_policy[] = { |
846c7bb0 BS |
44 | [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 }, |
45 | }; | |
46 | ||
f9fd8914 SN |
47 | struct listener { |
48 | struct list_head list; | |
49 | pid_t pid; | |
bb129994 | 50 | char valid; |
c757249a SN |
51 | }; |
52 | ||
f9fd8914 SN |
53 | struct listener_list { |
54 | struct rw_semaphore sem; | |
55 | struct list_head list; | |
56 | }; | |
57 | static DEFINE_PER_CPU(struct listener_list, listener_array); | |
58 | ||
59 | enum actions { | |
60 | REGISTER, | |
61 | DEREGISTER, | |
62 | CPU_DONT_CARE | |
63 | }; | |
c757249a SN |
64 | |
65 | static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp, | |
37167485 | 66 | size_t size) |
c757249a SN |
67 | { |
68 | struct sk_buff *skb; | |
69 | void *reply; | |
70 | ||
71 | /* | |
72 | * If new attributes are added, please revisit this allocation | |
73 | */ | |
3dabc715 | 74 | skb = genlmsg_new(size, GFP_KERNEL); |
c757249a SN |
75 | if (!skb) |
76 | return -ENOMEM; | |
77 | ||
78 | if (!info) { | |
cd85fc58 | 79 | int seq = this_cpu_inc_return(taskstats_seqnum) - 1; |
c757249a | 80 | |
17c157c8 | 81 | reply = genlmsg_put(skb, 0, seq, &family, 0, cmd); |
c757249a | 82 | } else |
17c157c8 | 83 | reply = genlmsg_put_reply(skb, info, &family, 0, cmd); |
c757249a SN |
84 | if (reply == NULL) { |
85 | nlmsg_free(skb); | |
86 | return -EINVAL; | |
87 | } | |
88 | ||
89 | *skbp = skb; | |
c757249a SN |
90 | return 0; |
91 | } | |
92 | ||
f9fd8914 SN |
93 | /* |
94 | * Send taskstats data in @skb to listener with nl_pid @pid | |
95 | */ | |
134e6375 | 96 | static int send_reply(struct sk_buff *skb, struct genl_info *info) |
c757249a | 97 | { |
b529ccf2 | 98 | struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb)); |
f9fd8914 | 99 | void *reply = genlmsg_data(genlhdr); |
c757249a | 100 | |
053c095a | 101 | genlmsg_end(skb, reply); |
c757249a | 102 | |
134e6375 | 103 | return genlmsg_reply(skb, info); |
c757249a SN |
104 | } |
105 | ||
f9fd8914 SN |
106 | /* |
107 | * Send taskstats data in @skb to listeners registered for @cpu's exit data | |
108 | */ | |
115085ea ON |
109 | static void send_cpu_listeners(struct sk_buff *skb, |
110 | struct listener_list *listeners) | |
f9fd8914 | 111 | { |
b529ccf2 | 112 | struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb)); |
f9fd8914 SN |
113 | struct listener *s, *tmp; |
114 | struct sk_buff *skb_next, *skb_cur = skb; | |
115 | void *reply = genlmsg_data(genlhdr); | |
92333baa | 116 | int delcount = 0; |
f9fd8914 | 117 | |
053c095a | 118 | genlmsg_end(skb, reply); |
f9fd8914 | 119 | |
bb129994 | 120 | down_read(&listeners->sem); |
d94a0415 | 121 | list_for_each_entry(s, &listeners->list, list) { |
92333baa LB |
122 | int rc; |
123 | ||
f9fd8914 SN |
124 | skb_next = NULL; |
125 | if (!list_is_last(&s->list, &listeners->list)) { | |
126 | skb_next = skb_clone(skb_cur, GFP_KERNEL); | |
d94a0415 | 127 | if (!skb_next) |
f9fd8914 | 128 | break; |
f9fd8914 | 129 | } |
134e6375 | 130 | rc = genlmsg_unicast(&init_net, skb_cur, s->pid); |
d94a0415 | 131 | if (rc == -ECONNREFUSED) { |
bb129994 SN |
132 | s->valid = 0; |
133 | delcount++; | |
f9fd8914 SN |
134 | } |
135 | skb_cur = skb_next; | |
136 | } | |
bb129994 | 137 | up_read(&listeners->sem); |
f9fd8914 | 138 | |
d94a0415 SN |
139 | if (skb_cur) |
140 | nlmsg_free(skb_cur); | |
141 | ||
bb129994 | 142 | if (!delcount) |
d94a0415 | 143 | return; |
bb129994 SN |
144 | |
145 | /* Delete invalidated entries */ | |
146 | down_write(&listeners->sem); | |
147 | list_for_each_entry_safe(s, tmp, &listeners->list, list) { | |
148 | if (!s->valid) { | |
149 | list_del(&s->list); | |
150 | kfree(s); | |
151 | } | |
152 | } | |
153 | up_write(&listeners->sem); | |
f9fd8914 SN |
154 | } |
155 | ||
4bd6e32a EB |
156 | static void fill_stats(struct user_namespace *user_ns, |
157 | struct pid_namespace *pid_ns, | |
158 | struct task_struct *tsk, struct taskstats *stats) | |
c757249a | 159 | { |
51de4d90 | 160 | memset(stats, 0, sizeof(*stats)); |
c757249a SN |
161 | /* |
162 | * Each accounting subsystem adds calls to its functions to | |
163 | * fill in relevant parts of struct taskstsats as follows | |
164 | * | |
7d94dddd | 165 | * per-task-foo(stats, tsk); |
c757249a SN |
166 | */ |
167 | ||
7d94dddd | 168 | delayacct_add_tsk(stats, tsk); |
f3cef7a9 JL |
169 | |
170 | /* fill in basic acct fields */ | |
6f44993f | 171 | stats->version = TASKSTATS_VERSION; |
b663a79c MU |
172 | stats->nvcsw = tsk->nvcsw; |
173 | stats->nivcsw = tsk->nivcsw; | |
4bd6e32a | 174 | bacct_add_tsk(user_ns, pid_ns, stats, tsk); |
6f44993f | 175 | |
9acc1853 JL |
176 | /* fill in extended acct fields */ |
177 | xacct_add_tsk(stats, tsk); | |
3d9e0cf1 | 178 | } |
9acc1853 | 179 | |
3d9e0cf1 MH |
180 | static int fill_stats_for_pid(pid_t pid, struct taskstats *stats) |
181 | { | |
182 | struct task_struct *tsk; | |
c757249a | 183 | |
2ee08260 | 184 | tsk = find_get_task_by_vpid(pid); |
3d9e0cf1 MH |
185 | if (!tsk) |
186 | return -ESRCH; | |
4bd6e32a | 187 | fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats); |
3d9e0cf1 MH |
188 | put_task_struct(tsk); |
189 | return 0; | |
c757249a SN |
190 | } |
191 | ||
3d9e0cf1 | 192 | static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats) |
c757249a | 193 | { |
3d9e0cf1 | 194 | struct task_struct *tsk, *first; |
ad4ecbcb | 195 | unsigned long flags; |
a98b6094 | 196 | int rc = -ESRCH; |
8c733420 ZX |
197 | u64 delta, utime, stime; |
198 | u64 start_time; | |
c757249a | 199 | |
ad4ecbcb SN |
200 | /* |
201 | * Add additional stats from live tasks except zombie thread group | |
202 | * leaders who are already counted with the dead tasks | |
203 | */ | |
a98b6094 | 204 | rcu_read_lock(); |
3d9e0cf1 | 205 | first = find_task_by_vpid(tgid); |
ad4ecbcb | 206 | |
a98b6094 ON |
207 | if (!first || !lock_task_sighand(first, &flags)) |
208 | goto out; | |
ad4ecbcb | 209 | |
a98b6094 ON |
210 | if (first->signal->stats) |
211 | memcpy(stats, first->signal->stats, sizeof(*stats)); | |
51de4d90 ON |
212 | else |
213 | memset(stats, 0, sizeof(*stats)); | |
fca178c0 | 214 | |
a98b6094 | 215 | tsk = first; |
8c733420 | 216 | start_time = ktime_get_ns(); |
c757249a | 217 | do { |
d7c3f5f2 | 218 | if (tsk->exit_state) |
ad4ecbcb | 219 | continue; |
c757249a | 220 | /* |
ad4ecbcb | 221 | * Accounting subsystem can call its functions here to |
c757249a SN |
222 | * fill in relevant parts of struct taskstsats as follows |
223 | * | |
ad4ecbcb | 224 | * per-task-foo(stats, tsk); |
c757249a | 225 | */ |
ad4ecbcb | 226 | delayacct_add_tsk(stats, tsk); |
6f44993f | 227 | |
8c733420 ZX |
228 | /* calculate task elapsed time in nsec */ |
229 | delta = start_time - tsk->start_time; | |
230 | /* Convert to micro seconds */ | |
231 | do_div(delta, NSEC_PER_USEC); | |
232 | stats->ac_etime += delta; | |
233 | ||
234 | task_cputime(tsk, &utime, &stime); | |
235 | stats->ac_utime += div_u64(utime, NSEC_PER_USEC); | |
236 | stats->ac_stime += div_u64(stime, NSEC_PER_USEC); | |
237 | ||
b663a79c MU |
238 | stats->nvcsw += tsk->nvcsw; |
239 | stats->nivcsw += tsk->nivcsw; | |
c757249a | 240 | } while_each_thread(first, tsk); |
6f44993f | 241 | |
a98b6094 ON |
242 | unlock_task_sighand(first, &flags); |
243 | rc = 0; | |
244 | out: | |
245 | rcu_read_unlock(); | |
246 | ||
247 | stats->version = TASKSTATS_VERSION; | |
c757249a | 248 | /* |
3a4fa0a2 | 249 | * Accounting subsystems can also add calls here to modify |
ad4ecbcb | 250 | * fields of taskstats. |
c757249a | 251 | */ |
a98b6094 | 252 | return rc; |
ad4ecbcb SN |
253 | } |
254 | ||
ad4ecbcb SN |
255 | static void fill_tgid_exit(struct task_struct *tsk) |
256 | { | |
257 | unsigned long flags; | |
258 | ||
b8534d7b | 259 | spin_lock_irqsave(&tsk->sighand->siglock, flags); |
ad4ecbcb SN |
260 | if (!tsk->signal->stats) |
261 | goto ret; | |
262 | ||
263 | /* | |
264 | * Each accounting subsystem calls its functions here to | |
265 | * accumalate its per-task stats for tsk, into the per-tgid structure | |
266 | * | |
267 | * per-task-foo(tsk->signal->stats, tsk); | |
268 | */ | |
269 | delayacct_add_tsk(tsk->signal->stats, tsk); | |
270 | ret: | |
b8534d7b | 271 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); |
ad4ecbcb | 272 | return; |
c757249a SN |
273 | } |
274 | ||
41c7bb95 | 275 | static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd) |
f9fd8914 SN |
276 | { |
277 | struct listener_list *listeners; | |
26c4caea | 278 | struct listener *s, *tmp, *s2; |
f9fd8914 | 279 | unsigned int cpu; |
0d20633b | 280 | int ret = 0; |
ad4ecbcb | 281 | |
41c7bb95 | 282 | if (!cpumask_subset(mask, cpu_possible_mask)) |
f9fd8914 SN |
283 | return -EINVAL; |
284 | ||
4bd6e32a EB |
285 | if (current_user_ns() != &init_user_ns) |
286 | return -EINVAL; | |
287 | ||
288 | if (task_active_pid_ns(current) != &init_pid_ns) | |
289 | return -EINVAL; | |
290 | ||
f9fd8914 | 291 | if (isadd == REGISTER) { |
41c7bb95 | 292 | for_each_cpu(cpu, mask) { |
dfc428b6 ON |
293 | s = kmalloc_node(sizeof(struct listener), |
294 | GFP_KERNEL, cpu_to_node(cpu)); | |
0d20633b CG |
295 | if (!s) { |
296 | ret = -ENOMEM; | |
f9fd8914 | 297 | goto cleanup; |
0d20633b | 298 | } |
f9fd8914 | 299 | s->pid = pid; |
bb129994 | 300 | s->valid = 1; |
f9fd8914 SN |
301 | |
302 | listeners = &per_cpu(listener_array, cpu); | |
303 | down_write(&listeners->sem); | |
dfc428b6 | 304 | list_for_each_entry(s2, &listeners->list, list) { |
a7295898 | 305 | if (s2->pid == pid && s2->valid) |
dfc428b6 | 306 | goto exists; |
26c4caea | 307 | } |
f9fd8914 | 308 | list_add(&s->list, &listeners->list); |
26c4caea | 309 | s = NULL; |
dfc428b6 | 310 | exists: |
f9fd8914 | 311 | up_write(&listeners->sem); |
dfc428b6 | 312 | kfree(s); /* nop if NULL */ |
f9fd8914 SN |
313 | } |
314 | return 0; | |
315 | } | |
316 | ||
317 | /* Deregister or cleanup */ | |
318 | cleanup: | |
41c7bb95 | 319 | for_each_cpu(cpu, mask) { |
f9fd8914 SN |
320 | listeners = &per_cpu(listener_array, cpu); |
321 | down_write(&listeners->sem); | |
322 | list_for_each_entry_safe(s, tmp, &listeners->list, list) { | |
323 | if (s->pid == pid) { | |
324 | list_del(&s->list); | |
325 | kfree(s); | |
326 | break; | |
327 | } | |
328 | } | |
329 | up_write(&listeners->sem); | |
330 | } | |
0d20633b | 331 | return ret; |
f9fd8914 SN |
332 | } |
333 | ||
41c7bb95 | 334 | static int parse(struct nlattr *na, struct cpumask *mask) |
f9fd8914 SN |
335 | { |
336 | char *data; | |
337 | int len; | |
338 | int ret; | |
339 | ||
340 | if (na == NULL) | |
341 | return 1; | |
342 | len = nla_len(na); | |
343 | if (len > TASKSTATS_CPUMASK_MAXLEN) | |
344 | return -E2BIG; | |
345 | if (len < 1) | |
346 | return -EINVAL; | |
347 | data = kmalloc(len, GFP_KERNEL); | |
348 | if (!data) | |
349 | return -ENOMEM; | |
872f6903 | 350 | nla_strscpy(data, na, len); |
29c0177e | 351 | ret = cpulist_parse(data, mask); |
f9fd8914 SN |
352 | kfree(data); |
353 | return ret; | |
354 | } | |
355 | ||
51de4d90 | 356 | static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid) |
68062b86 | 357 | { |
51de4d90 | 358 | struct nlattr *na, *ret; |
68062b86 ON |
359 | int aggr; |
360 | ||
37167485 ON |
361 | aggr = (type == TASKSTATS_TYPE_PID) |
362 | ? TASKSTATS_TYPE_AGGR_PID | |
363 | : TASKSTATS_TYPE_AGGR_TGID; | |
68062b86 | 364 | |
ae0be8de | 365 | na = nla_nest_start_noflag(skb, aggr); |
37167485 ON |
366 | if (!na) |
367 | goto err; | |
4be2c95d | 368 | |
3fa58266 CG |
369 | if (nla_put(skb, type, sizeof(pid), &pid) < 0) { |
370 | nla_nest_cancel(skb, na); | |
51de4d90 | 371 | goto err; |
3fa58266 | 372 | } |
80df5542 ND |
373 | ret = nla_reserve_64bit(skb, TASKSTATS_TYPE_STATS, |
374 | sizeof(struct taskstats), TASKSTATS_TYPE_NULL); | |
3fa58266 CG |
375 | if (!ret) { |
376 | nla_nest_cancel(skb, na); | |
51de4d90 | 377 | goto err; |
3fa58266 | 378 | } |
68062b86 ON |
379 | nla_nest_end(skb, na); |
380 | ||
51de4d90 ON |
381 | return nla_data(ret); |
382 | err: | |
383 | return NULL; | |
68062b86 ON |
384 | } |
385 | ||
846c7bb0 BS |
386 | static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info) |
387 | { | |
388 | int rc = 0; | |
389 | struct sk_buff *rep_skb; | |
390 | struct cgroupstats *stats; | |
391 | struct nlattr *na; | |
392 | size_t size; | |
393 | u32 fd; | |
2903ff01 | 394 | struct fd f; |
846c7bb0 BS |
395 | |
396 | na = info->attrs[CGROUPSTATS_CMD_ATTR_FD]; | |
397 | if (!na) | |
398 | return -EINVAL; | |
399 | ||
400 | fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]); | |
2903ff01 AV |
401 | f = fdget(fd); |
402 | if (!f.file) | |
f9615984 | 403 | return 0; |
846c7bb0 | 404 | |
f9615984 | 405 | size = nla_total_size(sizeof(struct cgroupstats)); |
846c7bb0 | 406 | |
f9615984 AB |
407 | rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb, |
408 | size); | |
409 | if (rc < 0) | |
410 | goto err; | |
846c7bb0 | 411 | |
f9615984 AB |
412 | na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS, |
413 | sizeof(struct cgroupstats)); | |
25353b33 | 414 | if (na == NULL) { |
0324b5a4 | 415 | nlmsg_free(rep_skb); |
25353b33 AC |
416 | rc = -EMSGSIZE; |
417 | goto err; | |
418 | } | |
419 | ||
f9615984 AB |
420 | stats = nla_data(na); |
421 | memset(stats, 0, sizeof(*stats)); | |
846c7bb0 | 422 | |
b583043e | 423 | rc = cgroupstats_build(stats, f.file->f_path.dentry); |
f9615984 AB |
424 | if (rc < 0) { |
425 | nlmsg_free(rep_skb); | |
426 | goto err; | |
846c7bb0 BS |
427 | } |
428 | ||
134e6375 | 429 | rc = send_reply(rep_skb, info); |
f9615984 | 430 | |
846c7bb0 | 431 | err: |
2903ff01 | 432 | fdput(f); |
846c7bb0 BS |
433 | return rc; |
434 | } | |
435 | ||
93233125 | 436 | static int cmd_attr_register_cpumask(struct genl_info *info) |
c757249a | 437 | { |
41c7bb95 | 438 | cpumask_var_t mask; |
93233125 | 439 | int rc; |
41c7bb95 RR |
440 | |
441 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | |
442 | return -ENOMEM; | |
41c7bb95 | 443 | rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask); |
f9fd8914 | 444 | if (rc < 0) |
93233125 | 445 | goto out; |
15e47304 | 446 | rc = add_del_listener(info->snd_portid, mask, REGISTER); |
93233125 MH |
447 | out: |
448 | free_cpumask_var(mask); | |
449 | return rc; | |
450 | } | |
451 | ||
452 | static int cmd_attr_deregister_cpumask(struct genl_info *info) | |
453 | { | |
454 | cpumask_var_t mask; | |
455 | int rc; | |
f9fd8914 | 456 | |
93233125 MH |
457 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
458 | return -ENOMEM; | |
41c7bb95 | 459 | rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask); |
f9fd8914 | 460 | if (rc < 0) |
93233125 | 461 | goto out; |
15e47304 | 462 | rc = add_del_listener(info->snd_portid, mask, DEREGISTER); |
93233125 | 463 | out: |
41c7bb95 | 464 | free_cpumask_var(mask); |
93233125 MH |
465 | return rc; |
466 | } | |
467 | ||
4be2c95d JM |
468 | static size_t taskstats_packet_size(void) |
469 | { | |
470 | size_t size; | |
471 | ||
472 | size = nla_total_size(sizeof(u32)) + | |
80df5542 ND |
473 | nla_total_size_64bit(sizeof(struct taskstats)) + |
474 | nla_total_size(0); | |
475 | ||
4be2c95d JM |
476 | return size; |
477 | } | |
478 | ||
93233125 MH |
479 | static int cmd_attr_pid(struct genl_info *info) |
480 | { | |
481 | struct taskstats *stats; | |
482 | struct sk_buff *rep_skb; | |
483 | size_t size; | |
484 | u32 pid; | |
485 | int rc; | |
c757249a | 486 | |
4be2c95d | 487 | size = taskstats_packet_size(); |
c757249a | 488 | |
37167485 | 489 | rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size); |
c757249a SN |
490 | if (rc < 0) |
491 | return rc; | |
492 | ||
51de4d90 | 493 | rc = -EINVAL; |
93233125 MH |
494 | pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]); |
495 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid); | |
496 | if (!stats) | |
c757249a | 497 | goto err; |
c757249a | 498 | |
3d9e0cf1 | 499 | rc = fill_stats_for_pid(pid, stats); |
93233125 MH |
500 | if (rc < 0) |
501 | goto err; | |
134e6375 | 502 | return send_reply(rep_skb, info); |
c757249a SN |
503 | err: |
504 | nlmsg_free(rep_skb); | |
505 | return rc; | |
506 | } | |
507 | ||
93233125 MH |
508 | static int cmd_attr_tgid(struct genl_info *info) |
509 | { | |
510 | struct taskstats *stats; | |
511 | struct sk_buff *rep_skb; | |
512 | size_t size; | |
513 | u32 tgid; | |
514 | int rc; | |
515 | ||
4be2c95d | 516 | size = taskstats_packet_size(); |
93233125 MH |
517 | |
518 | rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size); | |
519 | if (rc < 0) | |
520 | return rc; | |
521 | ||
522 | rc = -EINVAL; | |
523 | tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]); | |
524 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid); | |
525 | if (!stats) | |
526 | goto err; | |
527 | ||
3d9e0cf1 | 528 | rc = fill_stats_for_tgid(tgid, stats); |
93233125 MH |
529 | if (rc < 0) |
530 | goto err; | |
531 | return send_reply(rep_skb, info); | |
532 | err: | |
533 | nlmsg_free(rep_skb); | |
534 | return rc; | |
535 | } | |
536 | ||
537 | static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) | |
538 | { | |
539 | if (info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK]) | |
540 | return cmd_attr_register_cpumask(info); | |
541 | else if (info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK]) | |
542 | return cmd_attr_deregister_cpumask(info); | |
543 | else if (info->attrs[TASKSTATS_CMD_ATTR_PID]) | |
544 | return cmd_attr_pid(info); | |
545 | else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) | |
546 | return cmd_attr_tgid(info); | |
547 | else | |
548 | return -EINVAL; | |
549 | } | |
550 | ||
34ec1234 ON |
551 | static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk) |
552 | { | |
553 | struct signal_struct *sig = tsk->signal; | |
0b8d616f | 554 | struct taskstats *stats_new, *stats; |
34ec1234 | 555 | |
0b8d616f CB |
556 | /* Pairs with smp_store_release() below. */ |
557 | stats = smp_load_acquire(&sig->stats); | |
558 | if (stats || thread_group_empty(tsk)) | |
559 | return stats; | |
34ec1234 ON |
560 | |
561 | /* No problem if kmem_cache_zalloc() fails */ | |
0b8d616f | 562 | stats_new = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL); |
34ec1234 ON |
563 | |
564 | spin_lock_irq(&tsk->sighand->siglock); | |
0b8d616f CB |
565 | stats = sig->stats; |
566 | if (!stats) { | |
567 | /* | |
568 | * Pairs with smp_store_release() above and order the | |
569 | * kmem_cache_zalloc(). | |
570 | */ | |
571 | smp_store_release(&sig->stats, stats_new); | |
572 | stats = stats_new; | |
573 | stats_new = NULL; | |
34ec1234 ON |
574 | } |
575 | spin_unlock_irq(&tsk->sighand->siglock); | |
576 | ||
0b8d616f CB |
577 | if (stats_new) |
578 | kmem_cache_free(taskstats_cache, stats_new); | |
579 | ||
580 | return stats; | |
34ec1234 ON |
581 | } |
582 | ||
c757249a | 583 | /* Send pid data out on exit */ |
115085ea | 584 | void taskstats_exit(struct task_struct *tsk, int group_dead) |
c757249a SN |
585 | { |
586 | int rc; | |
115085ea | 587 | struct listener_list *listeners; |
51de4d90 | 588 | struct taskstats *stats; |
c757249a | 589 | struct sk_buff *rep_skb; |
c757249a SN |
590 | size_t size; |
591 | int is_thread_group; | |
c757249a | 592 | |
4a279ff1 | 593 | if (!family_registered) |
c757249a SN |
594 | return; |
595 | ||
c757249a SN |
596 | /* |
597 | * Size includes space for nested attributes | |
598 | */ | |
4be2c95d | 599 | size = taskstats_packet_size(); |
c757249a | 600 | |
34ec1234 | 601 | is_thread_group = !!taskstats_tgid_alloc(tsk); |
4a279ff1 ON |
602 | if (is_thread_group) { |
603 | /* PID + STATS + TGID + STATS */ | |
604 | size = 2 * size; | |
605 | /* fill the tsk->signal->stats structure */ | |
606 | fill_tgid_exit(tsk); | |
607 | } | |
608 | ||
4a32fea9 | 609 | listeners = raw_cpu_ptr(&listener_array); |
115085ea ON |
610 | if (list_empty(&listeners->list)) |
611 | return; | |
612 | ||
37167485 | 613 | rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size); |
c757249a | 614 | if (rc < 0) |
51de4d90 | 615 | return; |
c757249a | 616 | |
4bd6e32a EB |
617 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, |
618 | task_pid_nr_ns(tsk, &init_pid_ns)); | |
51de4d90 | 619 | if (!stats) |
37167485 | 620 | goto err; |
c757249a | 621 | |
4bd6e32a | 622 | fill_stats(&init_user_ns, &init_pid_ns, tsk, stats); |
c757249a | 623 | |
c757249a | 624 | /* |
ad4ecbcb | 625 | * Doesn't matter if tsk is the leader or the last group member leaving |
c757249a | 626 | */ |
68062b86 | 627 | if (!is_thread_group || !group_dead) |
ad4ecbcb | 628 | goto send; |
c757249a | 629 | |
4bd6e32a EB |
630 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, |
631 | task_tgid_nr_ns(tsk, &init_pid_ns)); | |
51de4d90 | 632 | if (!stats) |
37167485 | 633 | goto err; |
51de4d90 ON |
634 | |
635 | memcpy(stats, tsk->signal->stats, sizeof(*stats)); | |
c757249a | 636 | |
ad4ecbcb | 637 | send: |
115085ea | 638 | send_cpu_listeners(rep_skb, listeners); |
ad4ecbcb | 639 | return; |
37167485 | 640 | err: |
c757249a | 641 | nlmsg_free(rep_skb); |
c757249a SN |
642 | } |
643 | ||
7c1e0926 | 644 | static const struct genl_ops taskstats_ops[] = { |
88d36a99 JB |
645 | { |
646 | .cmd = TASKSTATS_CMD_GET, | |
ef6243ac | 647 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
88d36a99 | 648 | .doit = taskstats_user_cmd, |
7c1e0926 JK |
649 | .policy = taskstats_cmd_get_policy, |
650 | .maxattr = ARRAY_SIZE(taskstats_cmd_get_policy) - 1, | |
651 | .flags = GENL_ADMIN_PERM, | |
88d36a99 JB |
652 | }, |
653 | { | |
654 | .cmd = CGROUPSTATS_CMD_GET, | |
ef6243ac | 655 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
88d36a99 | 656 | .doit = cgroupstats_user_cmd, |
7c1e0926 JK |
657 | .policy = cgroupstats_cmd_get_policy, |
658 | .maxattr = ARRAY_SIZE(cgroupstats_cmd_get_policy) - 1, | |
88d36a99 | 659 | }, |
846c7bb0 BS |
660 | }; |
661 | ||
56989f6d | 662 | static struct genl_family family __ro_after_init = { |
489111e5 JB |
663 | .name = TASKSTATS_GENL_NAME, |
664 | .version = TASKSTATS_GENL_VERSION, | |
489111e5 | 665 | .module = THIS_MODULE, |
7c1e0926 JK |
666 | .ops = taskstats_ops, |
667 | .n_ops = ARRAY_SIZE(taskstats_ops), | |
489111e5 JB |
668 | }; |
669 | ||
c757249a SN |
670 | /* Needed early in initialization */ |
671 | void __init taskstats_init_early(void) | |
672 | { | |
f9fd8914 SN |
673 | unsigned int i; |
674 | ||
0a31bd5f | 675 | taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC); |
f9fd8914 SN |
676 | for_each_possible_cpu(i) { |
677 | INIT_LIST_HEAD(&(per_cpu(listener_array, i).list)); | |
678 | init_rwsem(&(per_cpu(listener_array, i).sem)); | |
679 | } | |
c757249a SN |
680 | } |
681 | ||
682 | static int __init taskstats_init(void) | |
683 | { | |
684 | int rc; | |
685 | ||
489111e5 | 686 | rc = genl_register_family(&family); |
c757249a SN |
687 | if (rc) |
688 | return rc; | |
689 | ||
c757249a | 690 | family_registered = 1; |
f9b182e2 | 691 | pr_info("registered taskstats version %d\n", TASKSTATS_GENL_VERSION); |
c757249a | 692 | return 0; |
c757249a SN |
693 | } |
694 | ||
695 | /* | |
696 | * late initcall ensures initialization of statistics collection | |
697 | * mechanisms precedes initialization of the taskstats interface | |
698 | */ | |
699 | late_initcall(taskstats_init); |