blob: 2b4898b4752eda2af2eabaa590b2e301a57f9255 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Shailabh Nagarc7572492006-07-14 00:24:40 -07002/*
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
Shailabh Nagarc7572492006-07-14 00:24:40 -07007 */
8
9#include <linux/kernel.h>
10#include <linux/taskstats_kern.h>
Jay Lanf3cef7a2006-09-30 23:28:55 -070011#include <linux/tsacct_kern.h>
Shailabh Nagar6f449932006-07-14 00:24:41 -070012#include <linux/delayacct.h>
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070013#include <linux/cpumask.h>
14#include <linux/percpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070016#include <linux/cgroupstats.h>
17#include <linux/cgroup.h>
18#include <linux/fs.h>
19#include <linux/file.h>
Eric W. Biederman4bd6e322012-02-07 17:56:49 -080020#include <linux/pid_namespace.h>
Shailabh Nagarc7572492006-07-14 00:24:40 -070021#include <net/genetlink.h>
Arun Sharma600634972011-07-26 16:09:06 -070022#include <linux/atomic.h>
Zhang Xiao8c733422017-05-08 15:56:45 -070023#include <linux/sched/cputime.h>
Shailabh Nagarc7572492006-07-14 00:24:40 -070024
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070025/*
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
Vegard Nossumb81f3ea2008-07-25 01:48:55 -070031static DEFINE_PER_CPU(__u32, taskstats_seqnum);
Shailabh Nagarc7572492006-07-14 00:24:40 -070032static int family_registered;
Christoph Lametere18b8902006-12-06 20:33:20 -080033struct kmem_cache *taskstats_cache;
Shailabh Nagarc7572492006-07-14 00:24:40 -070034
Johannes Berg489111e2016-10-24 14:40:03 +020035static struct genl_family family;
Shailabh Nagarc7572492006-07-14 00:24:40 -070036
Jakub Kicinski7c1e0922020-10-02 14:49:58 -070037static const struct nla_policy taskstats_cmd_get_policy[] = {
Shailabh Nagarc7572492006-07-14 00:24:40 -070038 [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
39 [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070040 [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
41 [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
42
Jakub Kicinski7c1e0922020-10-02 14:49:58 -070043static const struct nla_policy cgroupstats_cmd_get_policy[] = {
Balbir Singh846c7bb2007-10-18 23:39:44 -070044 [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
45};
46
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070047struct listener {
48 struct list_head list;
49 pid_t pid;
Shailabh Nagarbb129992006-07-14 00:24:47 -070050 char valid;
Shailabh Nagarc7572492006-07-14 00:24:40 -070051};
52
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070053struct listener_list {
54 struct rw_semaphore sem;
55 struct list_head list;
56};
57static DEFINE_PER_CPU(struct listener_list, listener_array);
58
59enum actions {
60 REGISTER,
61 DEREGISTER,
62 CPU_DONT_CARE
63};
Shailabh Nagarc7572492006-07-14 00:24:40 -070064
65static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
Oleg Nesterov371674852006-12-06 20:36:55 -080066 size_t size)
Shailabh Nagarc7572492006-07-14 00:24:40 -070067{
68 struct sk_buff *skb;
69 void *reply;
70
71 /*
72 * If new attributes are added, please revisit this allocation
73 */
Thomas Graf3dabc712006-11-14 19:44:52 -080074 skb = genlmsg_new(size, GFP_KERNEL);
Shailabh Nagarc7572492006-07-14 00:24:40 -070075 if (!skb)
76 return -ENOMEM;
77
78 if (!info) {
Christoph Lametercd85fc52010-12-08 17:42:22 +010079 int seq = this_cpu_inc_return(taskstats_seqnum) - 1;
Shailabh Nagarc7572492006-07-14 00:24:40 -070080
Thomas Graf17c157c2006-11-14 19:46:02 -080081 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070082 } else
Thomas Graf17c157c2006-11-14 19:46:02 -080083 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070084 if (reply == NULL) {
85 nlmsg_free(skb);
86 return -EINVAL;
87 }
88
89 *skbp = skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -070090 return 0;
91}
92
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070093/*
94 * Send taskstats data in @skb to listener with nl_pid @pid
95 */
Johannes Berg134e6372009-07-10 09:51:34 +000096static int send_reply(struct sk_buff *skb, struct genl_info *info)
Shailabh Nagarc7572492006-07-14 00:24:40 -070097{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -070098 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070099 void *reply = genlmsg_data(genlhdr);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700100
Johannes Berg053c0952015-01-16 22:09:00 +0100101 genlmsg_end(skb, reply);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700102
Johannes Berg134e6372009-07-10 09:51:34 +0000103 return genlmsg_reply(skb, info);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700104}
105
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700106/*
107 * Send taskstats data in @skb to listeners registered for @cpu's exit data
108 */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800109static void send_cpu_listeners(struct sk_buff *skb,
110 struct listener_list *listeners)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700111{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700112 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700113 struct listener *s, *tmp;
114 struct sk_buff *skb_next, *skb_cur = skb;
115 void *reply = genlmsg_data(genlhdr);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700116 int rc, delcount = 0;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700117
Johannes Berg053c0952015-01-16 22:09:00 +0100118 genlmsg_end(skb, reply);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700119
120 rc = 0;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700121 down_read(&listeners->sem);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700122 list_for_each_entry(s, &listeners->list, list) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700123 skb_next = NULL;
124 if (!list_is_last(&s->list, &listeners->list)) {
125 skb_next = skb_clone(skb_cur, GFP_KERNEL);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700126 if (!skb_next)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700127 break;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700128 }
Johannes Berg134e6372009-07-10 09:51:34 +0000129 rc = genlmsg_unicast(&init_net, skb_cur, s->pid);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700130 if (rc == -ECONNREFUSED) {
Shailabh Nagarbb129992006-07-14 00:24:47 -0700131 s->valid = 0;
132 delcount++;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700133 }
134 skb_cur = skb_next;
135 }
Shailabh Nagarbb129992006-07-14 00:24:47 -0700136 up_read(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700137
Shailabh Nagard94a0412006-07-30 03:03:11 -0700138 if (skb_cur)
139 nlmsg_free(skb_cur);
140
Shailabh Nagarbb129992006-07-14 00:24:47 -0700141 if (!delcount)
Shailabh Nagard94a0412006-07-30 03:03:11 -0700142 return;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700143
144 /* Delete invalidated entries */
145 down_write(&listeners->sem);
146 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
147 if (!s->valid) {
148 list_del(&s->list);
149 kfree(s);
150 }
151 }
152 up_write(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700153}
154
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800155static void fill_stats(struct user_namespace *user_ns,
156 struct pid_namespace *pid_ns,
157 struct task_struct *tsk, struct taskstats *stats)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700158{
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800159 memset(stats, 0, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700160 /*
161 * Each accounting subsystem adds calls to its functions to
162 * fill in relevant parts of struct taskstsats as follows
163 *
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700164 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700165 */
166
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700167 delayacct_add_tsk(stats, tsk);
Jay Lanf3cef7a2006-09-30 23:28:55 -0700168
169 /* fill in basic acct fields */
Shailabh Nagar6f449932006-07-14 00:24:41 -0700170 stats->version = TASKSTATS_VERSION;
Maxim Uvarovb663a792007-07-15 23:40:48 -0700171 stats->nvcsw = tsk->nvcsw;
172 stats->nivcsw = tsk->nivcsw;
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800173 bacct_add_tsk(user_ns, pid_ns, stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700174
Jay Lan9acc1852006-09-30 23:28:58 -0700175 /* fill in extended acct fields */
176 xacct_add_tsk(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700177}
178
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700179static int fill_stats_for_pid(pid_t pid, struct taskstats *stats)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700180{
Oleg Nesterova98b6092006-10-28 10:38:54 -0700181 struct task_struct *tsk;
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700182
Mike Rapoport2ee08262018-02-06 15:40:17 -0800183 tsk = find_get_task_by_vpid(pid);
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700184 if (!tsk)
185 return -ESRCH;
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800186 fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats);
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700187 put_task_struct(tsk);
188 return 0;
189}
190
191static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
192{
193 struct task_struct *tsk, *first;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700194 unsigned long flags;
Oleg Nesterova98b6092006-10-28 10:38:54 -0700195 int rc = -ESRCH;
Zhang Xiao8c733422017-05-08 15:56:45 -0700196 u64 delta, utime, stime;
197 u64 start_time;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700198
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700199 /*
200 * Add additional stats from live tasks except zombie thread group
201 * leaders who are already counted with the dead tasks
202 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700203 rcu_read_lock();
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700204 first = find_task_by_vpid(tgid);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700205
Oleg Nesterova98b6092006-10-28 10:38:54 -0700206 if (!first || !lock_task_sighand(first, &flags))
207 goto out;
208
209 if (first->signal->stats)
210 memcpy(stats, first->signal->stats, sizeof(*stats));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800211 else
212 memset(stats, 0, sizeof(*stats));
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700213
Shailabh Nagarc7572492006-07-14 00:24:40 -0700214 tsk = first;
Zhang Xiao8c733422017-05-08 15:56:45 -0700215 start_time = ktime_get_ns();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700216 do {
Oleg Nesterovd7c3f5f2006-10-28 10:38:54 -0700217 if (tsk->exit_state)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700218 continue;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700219 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700220 * Accounting subsystem can call its functions here to
Shailabh Nagarc7572492006-07-14 00:24:40 -0700221 * fill in relevant parts of struct taskstsats as follows
222 *
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700223 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700224 */
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700225 delayacct_add_tsk(stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700226
Zhang Xiao8c733422017-05-08 15:56:45 -0700227 /* calculate task elapsed time in nsec */
228 delta = start_time - tsk->start_time;
229 /* Convert to micro seconds */
230 do_div(delta, NSEC_PER_USEC);
231 stats->ac_etime += delta;
232
233 task_cputime(tsk, &utime, &stime);
234 stats->ac_utime += div_u64(utime, NSEC_PER_USEC);
235 stats->ac_stime += div_u64(stime, NSEC_PER_USEC);
236
Maxim Uvarovb663a792007-07-15 23:40:48 -0700237 stats->nvcsw += tsk->nvcsw;
238 stats->nivcsw += tsk->nivcsw;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700239 } while_each_thread(first, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700240
Oleg Nesterova98b6092006-10-28 10:38:54 -0700241 unlock_task_sighand(first, &flags);
242 rc = 0;
243out:
244 rcu_read_unlock();
245
246 stats->version = TASKSTATS_VERSION;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700247 /*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200248 * Accounting subsystems can also add calls here to modify
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700249 * fields of taskstats.
Shailabh Nagarc7572492006-07-14 00:24:40 -0700250 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700251 return rc;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700252}
253
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700254static void fill_tgid_exit(struct task_struct *tsk)
255{
256 unsigned long flags;
257
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700258 spin_lock_irqsave(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700259 if (!tsk->signal->stats)
260 goto ret;
261
262 /*
263 * Each accounting subsystem calls its functions here to
264 * accumalate its per-task stats for tsk, into the per-tgid structure
265 *
266 * per-task-foo(tsk->signal->stats, tsk);
267 */
268 delayacct_add_tsk(tsk->signal->stats, tsk);
269ret:
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700270 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700271 return;
272}
273
Rusty Russell41c7bb92009-01-01 10:12:28 +1030274static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700275{
276 struct listener_list *listeners;
Vasiliy Kulikov26c4cae2011-06-27 16:18:11 -0700277 struct listener *s, *tmp, *s2;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700278 unsigned int cpu;
Chen Gang0d206332013-11-12 15:11:23 -0800279 int ret = 0;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700280
Rusty Russell41c7bb92009-01-01 10:12:28 +1030281 if (!cpumask_subset(mask, cpu_possible_mask))
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700282 return -EINVAL;
283
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800284 if (current_user_ns() != &init_user_ns)
285 return -EINVAL;
286
287 if (task_active_pid_ns(current) != &init_pid_ns)
288 return -EINVAL;
289
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700290 if (isadd == REGISTER) {
Rusty Russell41c7bb92009-01-01 10:12:28 +1030291 for_each_cpu(cpu, mask) {
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700292 s = kmalloc_node(sizeof(struct listener),
293 GFP_KERNEL, cpu_to_node(cpu));
Chen Gang0d206332013-11-12 15:11:23 -0800294 if (!s) {
295 ret = -ENOMEM;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700296 goto cleanup;
Chen Gang0d206332013-11-12 15:11:23 -0800297 }
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700298 s->pid = pid;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700299 s->valid = 1;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700300
301 listeners = &per_cpu(listener_array, cpu);
302 down_write(&listeners->sem);
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700303 list_for_each_entry(s2, &listeners->list, list) {
Oleg Nesterova7295892011-08-03 16:21:05 -0700304 if (s2->pid == pid && s2->valid)
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700305 goto exists;
Vasiliy Kulikov26c4cae2011-06-27 16:18:11 -0700306 }
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700307 list_add(&s->list, &listeners->list);
Vasiliy Kulikov26c4cae2011-06-27 16:18:11 -0700308 s = NULL;
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700309exists:
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700310 up_write(&listeners->sem);
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700311 kfree(s); /* nop if NULL */
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700312 }
313 return 0;
314 }
315
316 /* Deregister or cleanup */
317cleanup:
Rusty Russell41c7bb92009-01-01 10:12:28 +1030318 for_each_cpu(cpu, mask) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700319 listeners = &per_cpu(listener_array, cpu);
320 down_write(&listeners->sem);
321 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
322 if (s->pid == pid) {
323 list_del(&s->list);
324 kfree(s);
325 break;
326 }
327 }
328 up_write(&listeners->sem);
329 }
Chen Gang0d206332013-11-12 15:11:23 -0800330 return ret;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700331}
332
Rusty Russell41c7bb92009-01-01 10:12:28 +1030333static int parse(struct nlattr *na, struct cpumask *mask)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700334{
335 char *data;
336 int len;
337 int ret;
338
339 if (na == NULL)
340 return 1;
341 len = nla_len(na);
342 if (len > TASKSTATS_CPUMASK_MAXLEN)
343 return -E2BIG;
344 if (len < 1)
345 return -EINVAL;
346 data = kmalloc(len, GFP_KERNEL);
347 if (!data)
348 return -ENOMEM;
Francis Laniel872f6902020-11-15 18:08:06 +0100349 nla_strscpy(data, na, len);
Rusty Russell29c01772008-12-13 21:20:25 +1030350 ret = cpulist_parse(data, mask);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700351 kfree(data);
352 return ret;
353}
354
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800355static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
Oleg Nesterov68062b82006-12-06 20:36:53 -0800356{
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800357 struct nlattr *na, *ret;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800358 int aggr;
359
Oleg Nesterov371674852006-12-06 20:36:55 -0800360 aggr = (type == TASKSTATS_TYPE_PID)
361 ? TASKSTATS_TYPE_AGGR_PID
362 : TASKSTATS_TYPE_AGGR_TGID;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800363
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200364 na = nla_nest_start_noflag(skb, aggr);
Oleg Nesterov371674852006-12-06 20:36:55 -0800365 if (!na)
366 goto err;
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800367
Chen Gang3fa58262013-11-12 15:11:22 -0800368 if (nla_put(skb, type, sizeof(pid), &pid) < 0) {
369 nla_nest_cancel(skb, na);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800370 goto err;
Chen Gang3fa58262013-11-12 15:11:22 -0800371 }
Nicolas Dichtel80df5542016-04-22 17:31:24 +0200372 ret = nla_reserve_64bit(skb, TASKSTATS_TYPE_STATS,
373 sizeof(struct taskstats), TASKSTATS_TYPE_NULL);
Chen Gang3fa58262013-11-12 15:11:22 -0800374 if (!ret) {
375 nla_nest_cancel(skb, na);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800376 goto err;
Chen Gang3fa58262013-11-12 15:11:22 -0800377 }
Oleg Nesterov68062b82006-12-06 20:36:53 -0800378 nla_nest_end(skb, na);
379
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800380 return nla_data(ret);
381err:
382 return NULL;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800383}
384
Balbir Singh846c7bb2007-10-18 23:39:44 -0700385static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
386{
387 int rc = 0;
388 struct sk_buff *rep_skb;
389 struct cgroupstats *stats;
390 struct nlattr *na;
391 size_t size;
392 u32 fd;
Al Viro2903ff02012-08-28 12:52:22 -0400393 struct fd f;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700394
395 na = info->attrs[CGROUPSTATS_CMD_ATTR_FD];
396 if (!na)
397 return -EINVAL;
398
399 fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]);
Al Viro2903ff02012-08-28 12:52:22 -0400400 f = fdget(fd);
401 if (!f.file)
Adrian Bunkf9615982007-11-14 17:00:37 -0800402 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700403
Adrian Bunkf9615982007-11-14 17:00:37 -0800404 size = nla_total_size(sizeof(struct cgroupstats));
Balbir Singh846c7bb2007-10-18 23:39:44 -0700405
Adrian Bunkf9615982007-11-14 17:00:37 -0800406 rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb,
407 size);
408 if (rc < 0)
409 goto err;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700410
Adrian Bunkf9615982007-11-14 17:00:37 -0800411 na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS,
412 sizeof(struct cgroupstats));
Alan Cox25353b32012-07-30 14:42:49 -0700413 if (na == NULL) {
Jesper Juhl0324b5a2012-10-04 17:16:52 -0700414 nlmsg_free(rep_skb);
Alan Cox25353b32012-07-30 14:42:49 -0700415 rc = -EMSGSIZE;
416 goto err;
417 }
418
Adrian Bunkf9615982007-11-14 17:00:37 -0800419 stats = nla_data(na);
420 memset(stats, 0, sizeof(*stats));
Balbir Singh846c7bb2007-10-18 23:39:44 -0700421
Al Virob5830432014-10-31 01:22:04 -0400422 rc = cgroupstats_build(stats, f.file->f_path.dentry);
Adrian Bunkf9615982007-11-14 17:00:37 -0800423 if (rc < 0) {
424 nlmsg_free(rep_skb);
425 goto err;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700426 }
427
Johannes Berg134e6372009-07-10 09:51:34 +0000428 rc = send_reply(rep_skb, info);
Adrian Bunkf9615982007-11-14 17:00:37 -0800429
Balbir Singh846c7bb2007-10-18 23:39:44 -0700430err:
Al Viro2903ff02012-08-28 12:52:22 -0400431 fdput(f);
Balbir Singh846c7bb2007-10-18 23:39:44 -0700432 return rc;
433}
434
Michael Holzheu93233122010-10-27 15:34:44 -0700435static int cmd_attr_register_cpumask(struct genl_info *info)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700436{
Rusty Russell41c7bb92009-01-01 10:12:28 +1030437 cpumask_var_t mask;
Michael Holzheu93233122010-10-27 15:34:44 -0700438 int rc;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700439
Rusty Russell41c7bb92009-01-01 10:12:28 +1030440 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
441 return -ENOMEM;
Rusty Russell41c7bb92009-01-01 10:12:28 +1030442 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700443 if (rc < 0)
Michael Holzheu93233122010-10-27 15:34:44 -0700444 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000445 rc = add_del_listener(info->snd_portid, mask, REGISTER);
Michael Holzheu93233122010-10-27 15:34:44 -0700446out:
447 free_cpumask_var(mask);
448 return rc;
449}
Rusty Russell41c7bb92009-01-01 10:12:28 +1030450
Michael Holzheu93233122010-10-27 15:34:44 -0700451static int cmd_attr_deregister_cpumask(struct genl_info *info)
452{
453 cpumask_var_t mask;
454 int rc;
455
456 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
457 return -ENOMEM;
Rusty Russell41c7bb92009-01-01 10:12:28 +1030458 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask);
459 if (rc < 0)
Michael Holzheu93233122010-10-27 15:34:44 -0700460 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000461 rc = add_del_listener(info->snd_portid, mask, DEREGISTER);
Michael Holzheu93233122010-10-27 15:34:44 -0700462out:
Rusty Russell41c7bb92009-01-01 10:12:28 +1030463 free_cpumask_var(mask);
Michael Holzheu93233122010-10-27 15:34:44 -0700464 return rc;
465}
Shailabh Nagarc7572492006-07-14 00:24:40 -0700466
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800467static size_t taskstats_packet_size(void)
468{
469 size_t size;
470
471 size = nla_total_size(sizeof(u32)) +
Nicolas Dichtel80df5542016-04-22 17:31:24 +0200472 nla_total_size_64bit(sizeof(struct taskstats)) +
473 nla_total_size(0);
474
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800475 return size;
476}
477
Michael Holzheu93233122010-10-27 15:34:44 -0700478static int cmd_attr_pid(struct genl_info *info)
479{
480 struct taskstats *stats;
481 struct sk_buff *rep_skb;
482 size_t size;
483 u32 pid;
484 int rc;
485
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800486 size = taskstats_packet_size();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700487
Oleg Nesterov371674852006-12-06 20:36:55 -0800488 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700489 if (rc < 0)
490 return rc;
491
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800492 rc = -EINVAL;
Michael Holzheu93233122010-10-27 15:34:44 -0700493 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
494 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
495 if (!stats)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700496 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700497
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700498 rc = fill_stats_for_pid(pid, stats);
Michael Holzheu93233122010-10-27 15:34:44 -0700499 if (rc < 0)
500 goto err;
Johannes Berg134e6372009-07-10 09:51:34 +0000501 return send_reply(rep_skb, info);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700502err:
503 nlmsg_free(rep_skb);
504 return rc;
505}
506
Michael Holzheu93233122010-10-27 15:34:44 -0700507static int cmd_attr_tgid(struct genl_info *info)
508{
509 struct taskstats *stats;
510 struct sk_buff *rep_skb;
511 size_t size;
512 u32 tgid;
513 int rc;
514
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800515 size = taskstats_packet_size();
Michael Holzheu93233122010-10-27 15:34:44 -0700516
517 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
518 if (rc < 0)
519 return rc;
520
521 rc = -EINVAL;
522 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
523 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
524 if (!stats)
525 goto err;
526
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700527 rc = fill_stats_for_tgid(tgid, stats);
Michael Holzheu93233122010-10-27 15:34:44 -0700528 if (rc < 0)
529 goto err;
530 return send_reply(rep_skb, info);
531err:
532 nlmsg_free(rep_skb);
533 return rc;
534}
535
536static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
537{
538 if (info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK])
539 return cmd_attr_register_cpumask(info);
540 else if (info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK])
541 return cmd_attr_deregister_cpumask(info);
542 else if (info->attrs[TASKSTATS_CMD_ATTR_PID])
543 return cmd_attr_pid(info);
544 else if (info->attrs[TASKSTATS_CMD_ATTR_TGID])
545 return cmd_attr_tgid(info);
546 else
547 return -EINVAL;
548}
549
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800550static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
551{
552 struct signal_struct *sig = tsk->signal;
Christian Brauner0b8d6162019-10-09 13:48:09 +0200553 struct taskstats *stats_new, *stats;
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800554
Christian Brauner0b8d6162019-10-09 13:48:09 +0200555 /* Pairs with smp_store_release() below. */
556 stats = smp_load_acquire(&sig->stats);
557 if (stats || thread_group_empty(tsk))
558 return stats;
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800559
560 /* No problem if kmem_cache_zalloc() fails */
Christian Brauner0b8d6162019-10-09 13:48:09 +0200561 stats_new = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800562
563 spin_lock_irq(&tsk->sighand->siglock);
Christian Brauner0b8d6162019-10-09 13:48:09 +0200564 stats = sig->stats;
565 if (!stats) {
566 /*
567 * Pairs with smp_store_release() above and order the
568 * kmem_cache_zalloc().
569 */
570 smp_store_release(&sig->stats, stats_new);
571 stats = stats_new;
572 stats_new = NULL;
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800573 }
574 spin_unlock_irq(&tsk->sighand->siglock);
575
Christian Brauner0b8d6162019-10-09 13:48:09 +0200576 if (stats_new)
577 kmem_cache_free(taskstats_cache, stats_new);
578
579 return stats;
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800580}
581
Shailabh Nagarc7572492006-07-14 00:24:40 -0700582/* Send pid data out on exit */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800583void taskstats_exit(struct task_struct *tsk, int group_dead)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700584{
585 int rc;
Oleg Nesterov115085e2006-12-06 20:36:51 -0800586 struct listener_list *listeners;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800587 struct taskstats *stats;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700588 struct sk_buff *rep_skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700589 size_t size;
590 int is_thread_group;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700591
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800592 if (!family_registered)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700593 return;
594
Shailabh Nagarc7572492006-07-14 00:24:40 -0700595 /*
596 * Size includes space for nested attributes
597 */
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800598 size = taskstats_packet_size();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700599
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800600 is_thread_group = !!taskstats_tgid_alloc(tsk);
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800601 if (is_thread_group) {
602 /* PID + STATS + TGID + STATS */
603 size = 2 * size;
604 /* fill the tsk->signal->stats structure */
605 fill_tgid_exit(tsk);
606 }
607
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500608 listeners = raw_cpu_ptr(&listener_array);
Oleg Nesterov115085e2006-12-06 20:36:51 -0800609 if (list_empty(&listeners->list))
610 return;
611
Oleg Nesterov371674852006-12-06 20:36:55 -0800612 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700613 if (rc < 0)
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800614 return;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700615
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800616 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID,
617 task_pid_nr_ns(tsk, &init_pid_ns));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800618 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800619 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800620
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800621 fill_stats(&init_user_ns, &init_pid_ns, tsk, stats);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700622
Shailabh Nagarc7572492006-07-14 00:24:40 -0700623 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700624 * Doesn't matter if tsk is the leader or the last group member leaving
Shailabh Nagarc7572492006-07-14 00:24:40 -0700625 */
Oleg Nesterov68062b82006-12-06 20:36:53 -0800626 if (!is_thread_group || !group_dead)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700627 goto send;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700628
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800629 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID,
630 task_tgid_nr_ns(tsk, &init_pid_ns));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800631 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800632 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800633
634 memcpy(stats, tsk->signal->stats, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700635
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700636send:
Oleg Nesterov115085e2006-12-06 20:36:51 -0800637 send_cpu_listeners(rep_skb, listeners);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700638 return;
Oleg Nesterov371674852006-12-06 20:36:55 -0800639err:
Shailabh Nagarc7572492006-07-14 00:24:40 -0700640 nlmsg_free(rep_skb);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700641}
642
Jakub Kicinski7c1e0922020-10-02 14:49:58 -0700643static const struct genl_ops taskstats_ops[] = {
Johannes Berg88d36a92013-11-14 17:14:39 +0100644 {
645 .cmd = TASKSTATS_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200646 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Johannes Berg88d36a92013-11-14 17:14:39 +0100647 .doit = taskstats_user_cmd,
Jakub Kicinski7c1e0922020-10-02 14:49:58 -0700648 .policy = taskstats_cmd_get_policy,
649 .maxattr = ARRAY_SIZE(taskstats_cmd_get_policy) - 1,
650 .flags = GENL_ADMIN_PERM,
Johannes Berg88d36a92013-11-14 17:14:39 +0100651 },
652 {
653 .cmd = CGROUPSTATS_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200654 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Johannes Berg88d36a92013-11-14 17:14:39 +0100655 .doit = cgroupstats_user_cmd,
Jakub Kicinski7c1e0922020-10-02 14:49:58 -0700656 .policy = cgroupstats_cmd_get_policy,
657 .maxattr = ARRAY_SIZE(cgroupstats_cmd_get_policy) - 1,
Johannes Berg88d36a92013-11-14 17:14:39 +0100658 },
Balbir Singh846c7bb2007-10-18 23:39:44 -0700659};
660
Johannes Berg56989f62016-10-24 14:40:05 +0200661static struct genl_family family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200662 .name = TASKSTATS_GENL_NAME,
663 .version = TASKSTATS_GENL_VERSION,
Johannes Berg489111e2016-10-24 14:40:03 +0200664 .module = THIS_MODULE,
Jakub Kicinski7c1e0922020-10-02 14:49:58 -0700665 .ops = taskstats_ops,
666 .n_ops = ARRAY_SIZE(taskstats_ops),
Johannes Berg489111e2016-10-24 14:40:03 +0200667};
668
Shailabh Nagarc7572492006-07-14 00:24:40 -0700669/* Needed early in initialization */
670void __init taskstats_init_early(void)
671{
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700672 unsigned int i;
673
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700674 taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700675 for_each_possible_cpu(i) {
676 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
677 init_rwsem(&(per_cpu(listener_array, i).sem));
678 }
Shailabh Nagarc7572492006-07-14 00:24:40 -0700679}
680
681static int __init taskstats_init(void)
682{
683 int rc;
684
Johannes Berg489111e2016-10-24 14:40:03 +0200685 rc = genl_register_family(&family);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700686 if (rc)
687 return rc;
688
Shailabh Nagarc7572492006-07-14 00:24:40 -0700689 family_registered = 1;
Mandeep Singh Bainesf9b182e2011-03-23 16:43:27 -0700690 pr_info("registered taskstats version %d\n", TASKSTATS_GENL_VERSION);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700691 return 0;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700692}
693
694/*
695 * late initcall ensures initialization of statistics collection
696 * mechanisms precedes initialization of the taskstats interface
697 */
698late_initcall(taskstats_init);