blob: e2ac0e37c4ae7bac1b574e8b660a1203d01a75fd [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
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000037static const struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = {
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
WANG Cong243d5212016-11-03 09:42:36 -070043/*
44 * We have to use TASKSTATS_CMD_ATTR_MAX here, it is the maxattr in the family.
45 * Make sure they are always aligned.
46 */
47static const struct nla_policy cgroupstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = {
Balbir Singh846c7bb2007-10-18 23:39:44 -070048 [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
49};
50
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070051struct listener {
52 struct list_head list;
53 pid_t pid;
Shailabh Nagarbb129992006-07-14 00:24:47 -070054 char valid;
Shailabh Nagarc7572492006-07-14 00:24:40 -070055};
56
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070057struct listener_list {
58 struct rw_semaphore sem;
59 struct list_head list;
60};
61static DEFINE_PER_CPU(struct listener_list, listener_array);
62
63enum actions {
64 REGISTER,
65 DEREGISTER,
66 CPU_DONT_CARE
67};
Shailabh Nagarc7572492006-07-14 00:24:40 -070068
69static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
Oleg Nesterov371674852006-12-06 20:36:55 -080070 size_t size)
Shailabh Nagarc7572492006-07-14 00:24:40 -070071{
72 struct sk_buff *skb;
73 void *reply;
74
75 /*
76 * If new attributes are added, please revisit this allocation
77 */
Thomas Graf3dabc712006-11-14 19:44:52 -080078 skb = genlmsg_new(size, GFP_KERNEL);
Shailabh Nagarc7572492006-07-14 00:24:40 -070079 if (!skb)
80 return -ENOMEM;
81
82 if (!info) {
Christoph Lametercd85fc52010-12-08 17:42:22 +010083 int seq = this_cpu_inc_return(taskstats_seqnum) - 1;
Shailabh Nagarc7572492006-07-14 00:24:40 -070084
Thomas Graf17c157c2006-11-14 19:46:02 -080085 reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070086 } else
Thomas Graf17c157c2006-11-14 19:46:02 -080087 reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
Shailabh Nagarc7572492006-07-14 00:24:40 -070088 if (reply == NULL) {
89 nlmsg_free(skb);
90 return -EINVAL;
91 }
92
93 *skbp = skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -070094 return 0;
95}
96
Shailabh Nagarf9fd8912006-07-14 00:24:47 -070097/*
98 * Send taskstats data in @skb to listener with nl_pid @pid
99 */
Johannes Berg134e6372009-07-10 09:51:34 +0000100static int send_reply(struct sk_buff *skb, struct genl_info *info)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700101{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700102 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700103 void *reply = genlmsg_data(genlhdr);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700104
Johannes Berg053c0952015-01-16 22:09:00 +0100105 genlmsg_end(skb, reply);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700106
Johannes Berg134e6372009-07-10 09:51:34 +0000107 return genlmsg_reply(skb, info);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700108}
109
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700110/*
111 * Send taskstats data in @skb to listeners registered for @cpu's exit data
112 */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800113static void send_cpu_listeners(struct sk_buff *skb,
114 struct listener_list *listeners)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700115{
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700116 struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700117 struct listener *s, *tmp;
118 struct sk_buff *skb_next, *skb_cur = skb;
119 void *reply = genlmsg_data(genlhdr);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700120 int rc, delcount = 0;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700121
Johannes Berg053c0952015-01-16 22:09:00 +0100122 genlmsg_end(skb, reply);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700123
124 rc = 0;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700125 down_read(&listeners->sem);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700126 list_for_each_entry(s, &listeners->list, list) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700127 skb_next = NULL;
128 if (!list_is_last(&s->list, &listeners->list)) {
129 skb_next = skb_clone(skb_cur, GFP_KERNEL);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700130 if (!skb_next)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700131 break;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700132 }
Johannes Berg134e6372009-07-10 09:51:34 +0000133 rc = genlmsg_unicast(&init_net, skb_cur, s->pid);
Shailabh Nagard94a0412006-07-30 03:03:11 -0700134 if (rc == -ECONNREFUSED) {
Shailabh Nagarbb129992006-07-14 00:24:47 -0700135 s->valid = 0;
136 delcount++;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700137 }
138 skb_cur = skb_next;
139 }
Shailabh Nagarbb129992006-07-14 00:24:47 -0700140 up_read(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700141
Shailabh Nagard94a0412006-07-30 03:03:11 -0700142 if (skb_cur)
143 nlmsg_free(skb_cur);
144
Shailabh Nagarbb129992006-07-14 00:24:47 -0700145 if (!delcount)
Shailabh Nagard94a0412006-07-30 03:03:11 -0700146 return;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700147
148 /* Delete invalidated entries */
149 down_write(&listeners->sem);
150 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
151 if (!s->valid) {
152 list_del(&s->list);
153 kfree(s);
154 }
155 }
156 up_write(&listeners->sem);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700157}
158
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800159static void fill_stats(struct user_namespace *user_ns,
160 struct pid_namespace *pid_ns,
161 struct task_struct *tsk, struct taskstats *stats)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700162{
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800163 memset(stats, 0, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700164 /*
165 * Each accounting subsystem adds calls to its functions to
166 * fill in relevant parts of struct taskstsats as follows
167 *
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700168 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700169 */
170
Shailabh Nagar7d94ddd2006-07-30 03:03:10 -0700171 delayacct_add_tsk(stats, tsk);
Jay Lanf3cef7a2006-09-30 23:28:55 -0700172
173 /* fill in basic acct fields */
Shailabh Nagar6f449932006-07-14 00:24:41 -0700174 stats->version = TASKSTATS_VERSION;
Maxim Uvarovb663a792007-07-15 23:40:48 -0700175 stats->nvcsw = tsk->nvcsw;
176 stats->nivcsw = tsk->nivcsw;
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800177 bacct_add_tsk(user_ns, pid_ns, stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700178
Jay Lan9acc1852006-09-30 23:28:58 -0700179 /* fill in extended acct fields */
180 xacct_add_tsk(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700181}
182
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700183static int fill_stats_for_pid(pid_t pid, struct taskstats *stats)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700184{
Oleg Nesterova98b6092006-10-28 10:38:54 -0700185 struct task_struct *tsk;
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700186
Mike Rapoport2ee08262018-02-06 15:40:17 -0800187 tsk = find_get_task_by_vpid(pid);
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700188 if (!tsk)
189 return -ESRCH;
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800190 fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats);
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700191 put_task_struct(tsk);
192 return 0;
193}
194
195static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
196{
197 struct task_struct *tsk, *first;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700198 unsigned long flags;
Oleg Nesterova98b6092006-10-28 10:38:54 -0700199 int rc = -ESRCH;
Zhang Xiao8c733422017-05-08 15:56:45 -0700200 u64 delta, utime, stime;
201 u64 start_time;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700202
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700203 /*
204 * Add additional stats from live tasks except zombie thread group
205 * leaders who are already counted with the dead tasks
206 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700207 rcu_read_lock();
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700208 first = find_task_by_vpid(tgid);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700209
Oleg Nesterova98b6092006-10-28 10:38:54 -0700210 if (!first || !lock_task_sighand(first, &flags))
211 goto out;
212
213 if (first->signal->stats)
214 memcpy(stats, first->signal->stats, sizeof(*stats));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800215 else
216 memset(stats, 0, sizeof(*stats));
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700217
Shailabh Nagarc7572492006-07-14 00:24:40 -0700218 tsk = first;
Zhang Xiao8c733422017-05-08 15:56:45 -0700219 start_time = ktime_get_ns();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700220 do {
Oleg Nesterovd7c3f5f2006-10-28 10:38:54 -0700221 if (tsk->exit_state)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700222 continue;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700223 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700224 * Accounting subsystem can call its functions here to
Shailabh Nagarc7572492006-07-14 00:24:40 -0700225 * fill in relevant parts of struct taskstsats as follows
226 *
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700227 * per-task-foo(stats, tsk);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700228 */
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700229 delayacct_add_tsk(stats, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700230
Zhang Xiao8c733422017-05-08 15:56:45 -0700231 /* calculate task elapsed time in nsec */
232 delta = start_time - tsk->start_time;
233 /* Convert to micro seconds */
234 do_div(delta, NSEC_PER_USEC);
235 stats->ac_etime += delta;
236
237 task_cputime(tsk, &utime, &stime);
238 stats->ac_utime += div_u64(utime, NSEC_PER_USEC);
239 stats->ac_stime += div_u64(stime, NSEC_PER_USEC);
240
Maxim Uvarovb663a792007-07-15 23:40:48 -0700241 stats->nvcsw += tsk->nvcsw;
242 stats->nivcsw += tsk->nivcsw;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700243 } while_each_thread(first, tsk);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700244
Oleg Nesterova98b6092006-10-28 10:38:54 -0700245 unlock_task_sighand(first, &flags);
246 rc = 0;
247out:
248 rcu_read_unlock();
249
250 stats->version = TASKSTATS_VERSION;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700251 /*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200252 * Accounting subsystems can also add calls here to modify
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700253 * fields of taskstats.
Shailabh Nagarc7572492006-07-14 00:24:40 -0700254 */
Oleg Nesterova98b6092006-10-28 10:38:54 -0700255 return rc;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700256}
257
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700258static void fill_tgid_exit(struct task_struct *tsk)
259{
260 unsigned long flags;
261
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700262 spin_lock_irqsave(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700263 if (!tsk->signal->stats)
264 goto ret;
265
266 /*
267 * Each accounting subsystem calls its functions here to
268 * accumalate its per-task stats for tsk, into the per-tgid structure
269 *
270 * per-task-foo(tsk->signal->stats, tsk);
271 */
272 delayacct_add_tsk(tsk->signal->stats, tsk);
273ret:
Oleg Nesterovb8534d72006-10-28 10:38:53 -0700274 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700275 return;
276}
277
Rusty Russell41c7bb92009-01-01 10:12:28 +1030278static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700279{
280 struct listener_list *listeners;
Vasiliy Kulikov26c4cae2011-06-27 16:18:11 -0700281 struct listener *s, *tmp, *s2;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700282 unsigned int cpu;
Chen Gang0d206332013-11-12 15:11:23 -0800283 int ret = 0;
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700284
Rusty Russell41c7bb92009-01-01 10:12:28 +1030285 if (!cpumask_subset(mask, cpu_possible_mask))
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700286 return -EINVAL;
287
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800288 if (current_user_ns() != &init_user_ns)
289 return -EINVAL;
290
291 if (task_active_pid_ns(current) != &init_pid_ns)
292 return -EINVAL;
293
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700294 if (isadd == REGISTER) {
Rusty Russell41c7bb92009-01-01 10:12:28 +1030295 for_each_cpu(cpu, mask) {
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700296 s = kmalloc_node(sizeof(struct listener),
297 GFP_KERNEL, cpu_to_node(cpu));
Chen Gang0d206332013-11-12 15:11:23 -0800298 if (!s) {
299 ret = -ENOMEM;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700300 goto cleanup;
Chen Gang0d206332013-11-12 15:11:23 -0800301 }
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700302 s->pid = pid;
Shailabh Nagarbb129992006-07-14 00:24:47 -0700303 s->valid = 1;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700304
305 listeners = &per_cpu(listener_array, cpu);
306 down_write(&listeners->sem);
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700307 list_for_each_entry(s2, &listeners->list, list) {
Oleg Nesterova7295892011-08-03 16:21:05 -0700308 if (s2->pid == pid && s2->valid)
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700309 goto exists;
Vasiliy Kulikov26c4cae2011-06-27 16:18:11 -0700310 }
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700311 list_add(&s->list, &listeners->list);
Vasiliy Kulikov26c4cae2011-06-27 16:18:11 -0700312 s = NULL;
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700313exists:
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700314 up_write(&listeners->sem);
Oleg Nesterovdfc428b2011-08-03 16:21:04 -0700315 kfree(s); /* nop if NULL */
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700316 }
317 return 0;
318 }
319
320 /* Deregister or cleanup */
321cleanup:
Rusty Russell41c7bb92009-01-01 10:12:28 +1030322 for_each_cpu(cpu, mask) {
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700323 listeners = &per_cpu(listener_array, cpu);
324 down_write(&listeners->sem);
325 list_for_each_entry_safe(s, tmp, &listeners->list, list) {
326 if (s->pid == pid) {
327 list_del(&s->list);
328 kfree(s);
329 break;
330 }
331 }
332 up_write(&listeners->sem);
333 }
Chen Gang0d206332013-11-12 15:11:23 -0800334 return ret;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700335}
336
Rusty Russell41c7bb92009-01-01 10:12:28 +1030337static int parse(struct nlattr *na, struct cpumask *mask)
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700338{
339 char *data;
340 int len;
341 int ret;
342
343 if (na == NULL)
344 return 1;
345 len = nla_len(na);
346 if (len > TASKSTATS_CPUMASK_MAXLEN)
347 return -E2BIG;
348 if (len < 1)
349 return -EINVAL;
350 data = kmalloc(len, GFP_KERNEL);
351 if (!data)
352 return -ENOMEM;
353 nla_strlcpy(data, na, len);
Rusty Russell29c01772008-12-13 21:20:25 +1030354 ret = cpulist_parse(data, mask);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700355 kfree(data);
356 return ret;
357}
358
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800359static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
Oleg Nesterov68062b82006-12-06 20:36:53 -0800360{
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800361 struct nlattr *na, *ret;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800362 int aggr;
363
Oleg Nesterov371674852006-12-06 20:36:55 -0800364 aggr = (type == TASKSTATS_TYPE_PID)
365 ? TASKSTATS_TYPE_AGGR_PID
366 : TASKSTATS_TYPE_AGGR_TGID;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800367
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200368 na = nla_nest_start_noflag(skb, aggr);
Oleg Nesterov371674852006-12-06 20:36:55 -0800369 if (!na)
370 goto err;
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800371
Chen Gang3fa58262013-11-12 15:11:22 -0800372 if (nla_put(skb, type, sizeof(pid), &pid) < 0) {
373 nla_nest_cancel(skb, na);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800374 goto err;
Chen Gang3fa58262013-11-12 15:11:22 -0800375 }
Nicolas Dichtel80df5542016-04-22 17:31:24 +0200376 ret = nla_reserve_64bit(skb, TASKSTATS_TYPE_STATS,
377 sizeof(struct taskstats), TASKSTATS_TYPE_NULL);
Chen Gang3fa58262013-11-12 15:11:22 -0800378 if (!ret) {
379 nla_nest_cancel(skb, na);
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800380 goto err;
Chen Gang3fa58262013-11-12 15:11:22 -0800381 }
Oleg Nesterov68062b82006-12-06 20:36:53 -0800382 nla_nest_end(skb, na);
383
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800384 return nla_data(ret);
385err:
386 return NULL;
Oleg Nesterov68062b82006-12-06 20:36:53 -0800387}
388
Balbir Singh846c7bb2007-10-18 23:39:44 -0700389static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
390{
391 int rc = 0;
392 struct sk_buff *rep_skb;
393 struct cgroupstats *stats;
394 struct nlattr *na;
395 size_t size;
396 u32 fd;
Al Viro2903ff02012-08-28 12:52:22 -0400397 struct fd f;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700398
399 na = info->attrs[CGROUPSTATS_CMD_ATTR_FD];
400 if (!na)
401 return -EINVAL;
402
403 fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]);
Al Viro2903ff02012-08-28 12:52:22 -0400404 f = fdget(fd);
405 if (!f.file)
Adrian Bunkf9615982007-11-14 17:00:37 -0800406 return 0;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700407
Adrian Bunkf9615982007-11-14 17:00:37 -0800408 size = nla_total_size(sizeof(struct cgroupstats));
Balbir Singh846c7bb2007-10-18 23:39:44 -0700409
Adrian Bunkf9615982007-11-14 17:00:37 -0800410 rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb,
411 size);
412 if (rc < 0)
413 goto err;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700414
Adrian Bunkf9615982007-11-14 17:00:37 -0800415 na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS,
416 sizeof(struct cgroupstats));
Alan Cox25353b32012-07-30 14:42:49 -0700417 if (na == NULL) {
Jesper Juhl0324b5a2012-10-04 17:16:52 -0700418 nlmsg_free(rep_skb);
Alan Cox25353b32012-07-30 14:42:49 -0700419 rc = -EMSGSIZE;
420 goto err;
421 }
422
Adrian Bunkf9615982007-11-14 17:00:37 -0800423 stats = nla_data(na);
424 memset(stats, 0, sizeof(*stats));
Balbir Singh846c7bb2007-10-18 23:39:44 -0700425
Al Virob5830432014-10-31 01:22:04 -0400426 rc = cgroupstats_build(stats, f.file->f_path.dentry);
Adrian Bunkf9615982007-11-14 17:00:37 -0800427 if (rc < 0) {
428 nlmsg_free(rep_skb);
429 goto err;
Balbir Singh846c7bb2007-10-18 23:39:44 -0700430 }
431
Johannes Berg134e6372009-07-10 09:51:34 +0000432 rc = send_reply(rep_skb, info);
Adrian Bunkf9615982007-11-14 17:00:37 -0800433
Balbir Singh846c7bb2007-10-18 23:39:44 -0700434err:
Al Viro2903ff02012-08-28 12:52:22 -0400435 fdput(f);
Balbir Singh846c7bb2007-10-18 23:39:44 -0700436 return rc;
437}
438
Michael Holzheu93233122010-10-27 15:34:44 -0700439static int cmd_attr_register_cpumask(struct genl_info *info)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700440{
Rusty Russell41c7bb92009-01-01 10:12:28 +1030441 cpumask_var_t mask;
Michael Holzheu93233122010-10-27 15:34:44 -0700442 int rc;
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700443
Rusty Russell41c7bb92009-01-01 10:12:28 +1030444 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
445 return -ENOMEM;
Rusty Russell41c7bb92009-01-01 10:12:28 +1030446 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700447 if (rc < 0)
Michael Holzheu93233122010-10-27 15:34:44 -0700448 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000449 rc = add_del_listener(info->snd_portid, mask, REGISTER);
Michael Holzheu93233122010-10-27 15:34:44 -0700450out:
451 free_cpumask_var(mask);
452 return rc;
453}
Rusty Russell41c7bb92009-01-01 10:12:28 +1030454
Michael Holzheu93233122010-10-27 15:34:44 -0700455static int cmd_attr_deregister_cpumask(struct genl_info *info)
456{
457 cpumask_var_t mask;
458 int rc;
459
460 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
461 return -ENOMEM;
Rusty Russell41c7bb92009-01-01 10:12:28 +1030462 rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask);
463 if (rc < 0)
Michael Holzheu93233122010-10-27 15:34:44 -0700464 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000465 rc = add_del_listener(info->snd_portid, mask, DEREGISTER);
Michael Holzheu93233122010-10-27 15:34:44 -0700466out:
Rusty Russell41c7bb92009-01-01 10:12:28 +1030467 free_cpumask_var(mask);
Michael Holzheu93233122010-10-27 15:34:44 -0700468 return rc;
469}
Shailabh Nagarc7572492006-07-14 00:24:40 -0700470
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800471static size_t taskstats_packet_size(void)
472{
473 size_t size;
474
475 size = nla_total_size(sizeof(u32)) +
Nicolas Dichtel80df5542016-04-22 17:31:24 +0200476 nla_total_size_64bit(sizeof(struct taskstats)) +
477 nla_total_size(0);
478
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800479 return size;
480}
481
Michael Holzheu93233122010-10-27 15:34:44 -0700482static int cmd_attr_pid(struct genl_info *info)
483{
484 struct taskstats *stats;
485 struct sk_buff *rep_skb;
486 size_t size;
487 u32 pid;
488 int rc;
489
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800490 size = taskstats_packet_size();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700491
Oleg Nesterov371674852006-12-06 20:36:55 -0800492 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700493 if (rc < 0)
494 return rc;
495
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800496 rc = -EINVAL;
Michael Holzheu93233122010-10-27 15:34:44 -0700497 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
498 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
499 if (!stats)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700500 goto err;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700501
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700502 rc = fill_stats_for_pid(pid, stats);
Michael Holzheu93233122010-10-27 15:34:44 -0700503 if (rc < 0)
504 goto err;
Johannes Berg134e6372009-07-10 09:51:34 +0000505 return send_reply(rep_skb, info);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700506err:
507 nlmsg_free(rep_skb);
508 return rc;
509}
510
Michael Holzheu93233122010-10-27 15:34:44 -0700511static int cmd_attr_tgid(struct genl_info *info)
512{
513 struct taskstats *stats;
514 struct sk_buff *rep_skb;
515 size_t size;
516 u32 tgid;
517 int rc;
518
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800519 size = taskstats_packet_size();
Michael Holzheu93233122010-10-27 15:34:44 -0700520
521 rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
522 if (rc < 0)
523 return rc;
524
525 rc = -EINVAL;
526 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
527 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
528 if (!stats)
529 goto err;
530
Michael Holzheu3d9e0cf2010-10-27 15:34:44 -0700531 rc = fill_stats_for_tgid(tgid, stats);
Michael Holzheu93233122010-10-27 15:34:44 -0700532 if (rc < 0)
533 goto err;
534 return send_reply(rep_skb, info);
535err:
536 nlmsg_free(rep_skb);
537 return rc;
538}
539
540static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
541{
542 if (info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK])
543 return cmd_attr_register_cpumask(info);
544 else if (info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK])
545 return cmd_attr_deregister_cpumask(info);
546 else if (info->attrs[TASKSTATS_CMD_ATTR_PID])
547 return cmd_attr_pid(info);
548 else if (info->attrs[TASKSTATS_CMD_ATTR_TGID])
549 return cmd_attr_tgid(info);
550 else
551 return -EINVAL;
552}
553
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800554static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
555{
556 struct signal_struct *sig = tsk->signal;
Christian Brauner0b8d6162019-10-09 13:48:09 +0200557 struct taskstats *stats_new, *stats;
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800558
Christian Brauner0b8d6162019-10-09 13:48:09 +0200559 /* Pairs with smp_store_release() below. */
560 stats = smp_load_acquire(&sig->stats);
561 if (stats || thread_group_empty(tsk))
562 return stats;
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800563
564 /* No problem if kmem_cache_zalloc() fails */
Christian Brauner0b8d6162019-10-09 13:48:09 +0200565 stats_new = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800566
567 spin_lock_irq(&tsk->sighand->siglock);
Christian Brauner0b8d6162019-10-09 13:48:09 +0200568 stats = sig->stats;
569 if (!stats) {
570 /*
571 * Pairs with smp_store_release() above and order the
572 * kmem_cache_zalloc().
573 */
574 smp_store_release(&sig->stats, stats_new);
575 stats = stats_new;
576 stats_new = NULL;
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800577 }
578 spin_unlock_irq(&tsk->sighand->siglock);
579
Christian Brauner0b8d6162019-10-09 13:48:09 +0200580 if (stats_new)
581 kmem_cache_free(taskstats_cache, stats_new);
582
583 return stats;
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800584}
585
Shailabh Nagarc7572492006-07-14 00:24:40 -0700586/* Send pid data out on exit */
Oleg Nesterov115085e2006-12-06 20:36:51 -0800587void taskstats_exit(struct task_struct *tsk, int group_dead)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700588{
589 int rc;
Oleg Nesterov115085e2006-12-06 20:36:51 -0800590 struct listener_list *listeners;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800591 struct taskstats *stats;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700592 struct sk_buff *rep_skb;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700593 size_t size;
594 int is_thread_group;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700595
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800596 if (!family_registered)
Shailabh Nagarc7572492006-07-14 00:24:40 -0700597 return;
598
Shailabh Nagarc7572492006-07-14 00:24:40 -0700599 /*
600 * Size includes space for nested attributes
601 */
Jeff Mahoney4be2c952010-12-21 17:24:30 -0800602 size = taskstats_packet_size();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700603
Oleg Nesterov34ec12342006-12-06 20:36:52 -0800604 is_thread_group = !!taskstats_tgid_alloc(tsk);
Oleg Nesterov4a279ff2006-10-30 22:07:15 -0800605 if (is_thread_group) {
606 /* PID + STATS + TGID + STATS */
607 size = 2 * size;
608 /* fill the tsk->signal->stats structure */
609 fill_tgid_exit(tsk);
610 }
611
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500612 listeners = raw_cpu_ptr(&listener_array);
Oleg Nesterov115085e2006-12-06 20:36:51 -0800613 if (list_empty(&listeners->list))
614 return;
615
Oleg Nesterov371674852006-12-06 20:36:55 -0800616 rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700617 if (rc < 0)
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800618 return;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700619
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800620 stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID,
621 task_pid_nr_ns(tsk, &init_pid_ns));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800622 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800623 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800624
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800625 fill_stats(&init_user_ns, &init_pid_ns, tsk, stats);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700626
Shailabh Nagarc7572492006-07-14 00:24:40 -0700627 /*
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700628 * Doesn't matter if tsk is the leader or the last group member leaving
Shailabh Nagarc7572492006-07-14 00:24:40 -0700629 */
Oleg Nesterov68062b82006-12-06 20:36:53 -0800630 if (!is_thread_group || !group_dead)
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700631 goto send;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700632
Eric W. Biederman4bd6e322012-02-07 17:56:49 -0800633 stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID,
634 task_tgid_nr_ns(tsk, &init_pid_ns));
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800635 if (!stats)
Oleg Nesterov371674852006-12-06 20:36:55 -0800636 goto err;
Oleg Nesterov51de4d92006-12-06 20:36:54 -0800637
638 memcpy(stats, tsk->signal->stats, sizeof(*stats));
Shailabh Nagarc7572492006-07-14 00:24:40 -0700639
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700640send:
Oleg Nesterov115085e2006-12-06 20:36:51 -0800641 send_cpu_listeners(rep_skb, listeners);
Shailabh Nagarad4ecbc2006-07-14 00:24:44 -0700642 return;
Oleg Nesterov371674852006-12-06 20:36:55 -0800643err:
Shailabh Nagarc7572492006-07-14 00:24:40 -0700644 nlmsg_free(rep_skb);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700645}
646
Johannes Berg4534de82013-11-14 17:14:46 +0100647static const struct genl_ops taskstats_ops[] = {
Johannes Berg88d36a92013-11-14 17:14:39 +0100648 {
649 .cmd = TASKSTATS_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200650 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Johannes Berg88d36a92013-11-14 17:14:39 +0100651 .doit = taskstats_user_cmd,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100652 /* policy enforced later */
653 .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_HASPOL,
Johannes Berg88d36a92013-11-14 17:14:39 +0100654 },
655 {
656 .cmd = CGROUPSTATS_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200657 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Johannes Berg88d36a92013-11-14 17:14:39 +0100658 .doit = cgroupstats_user_cmd,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100659 /* policy enforced later */
660 .flags = GENL_CMD_CAP_HASPOL,
Johannes Berg88d36a92013-11-14 17:14:39 +0100661 },
Balbir Singh846c7bb2007-10-18 23:39:44 -0700662};
663
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100664static int taskstats_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
665 struct genl_info *info)
666{
667 const struct nla_policy *policy = NULL;
668
669 switch (ops->cmd) {
670 case TASKSTATS_CMD_GET:
671 policy = taskstats_cmd_get_policy;
672 break;
673 case CGROUPSTATS_CMD_GET:
674 policy = cgroupstats_cmd_get_policy;
675 break;
676 default:
677 return -EINVAL;
678 }
679
Johannes Berg8cb08172019-04-26 14:07:28 +0200680 return nlmsg_validate_deprecated(info->nlhdr, GENL_HDRLEN,
681 TASKSTATS_CMD_ATTR_MAX, policy,
682 info->extack);
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100683}
684
Johannes Berg56989f62016-10-24 14:40:05 +0200685static struct genl_family family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200686 .name = TASKSTATS_GENL_NAME,
687 .version = TASKSTATS_GENL_VERSION,
688 .maxattr = TASKSTATS_CMD_ATTR_MAX,
689 .module = THIS_MODULE,
690 .ops = taskstats_ops,
691 .n_ops = ARRAY_SIZE(taskstats_ops),
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100692 .pre_doit = taskstats_pre_doit,
Johannes Berg489111e2016-10-24 14:40:03 +0200693};
694
Shailabh Nagarc7572492006-07-14 00:24:40 -0700695/* Needed early in initialization */
696void __init taskstats_init_early(void)
697{
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700698 unsigned int i;
699
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700700 taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
Shailabh Nagarf9fd8912006-07-14 00:24:47 -0700701 for_each_possible_cpu(i) {
702 INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
703 init_rwsem(&(per_cpu(listener_array, i).sem));
704 }
Shailabh Nagarc7572492006-07-14 00:24:40 -0700705}
706
707static int __init taskstats_init(void)
708{
709 int rc;
710
Johannes Berg489111e2016-10-24 14:40:03 +0200711 rc = genl_register_family(&family);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700712 if (rc)
713 return rc;
714
Shailabh Nagarc7572492006-07-14 00:24:40 -0700715 family_registered = 1;
Mandeep Singh Bainesf9b182e2011-03-23 16:43:27 -0700716 pr_info("registered taskstats version %d\n", TASKSTATS_GENL_VERSION);
Shailabh Nagarc7572492006-07-14 00:24:40 -0700717 return 0;
Shailabh Nagarc7572492006-07-14 00:24:40 -0700718}
719
720/*
721 * late initcall ensures initialization of statistics collection
722 * mechanisms precedes initialization of the taskstats interface
723 */
724late_initcall(taskstats_init);