Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 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 |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/taskstats_kern.h> |
Jay Lan | f3cef7a | 2006-09-30 23:28:55 -0700 | [diff] [blame] | 11 | #include <linux/tsacct_kern.h> |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 12 | #include <linux/delayacct.h> |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 13 | #include <linux/cpumask.h> |
| 14 | #include <linux/percpu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 16 | #include <linux/cgroupstats.h> |
| 17 | #include <linux/cgroup.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/file.h> |
Eric W. Biederman | 4bd6e32 | 2012-02-07 17:56:49 -0800 | [diff] [blame] | 20 | #include <linux/pid_namespace.h> |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 21 | #include <net/genetlink.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 22 | #include <linux/atomic.h> |
Zhang Xiao | 8c73342 | 2017-05-08 15:56:45 -0700 | [diff] [blame] | 23 | #include <linux/sched/cputime.h> |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 24 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 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 | |
Vegard Nossum | b81f3ea | 2008-07-25 01:48:55 -0700 | [diff] [blame] | 31 | static DEFINE_PER_CPU(__u32, taskstats_seqnum); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 32 | static int family_registered; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 33 | struct kmem_cache *taskstats_cache; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 34 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 35 | static struct genl_family family; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 36 | |
Alexey Dobriyan | b54452b | 2010-02-18 08:14:31 +0000 | [diff] [blame] | 37 | static const struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = { |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 38 | [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 }, |
| 39 | [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 }, |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 40 | [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING }, |
| 41 | [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },}; |
| 42 | |
WANG Cong | 243d521 | 2016-11-03 09:42:36 -0700 | [diff] [blame] | 43 | /* |
| 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 | */ |
| 47 | static const struct nla_policy cgroupstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 48 | [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 }, |
| 49 | }; |
| 50 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 51 | struct listener { |
| 52 | struct list_head list; |
| 53 | pid_t pid; |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 54 | char valid; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 57 | struct listener_list { |
| 58 | struct rw_semaphore sem; |
| 59 | struct list_head list; |
| 60 | }; |
| 61 | static DEFINE_PER_CPU(struct listener_list, listener_array); |
| 62 | |
| 63 | enum actions { |
| 64 | REGISTER, |
| 65 | DEREGISTER, |
| 66 | CPU_DONT_CARE |
| 67 | }; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 68 | |
| 69 | static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp, |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 70 | size_t size) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 71 | { |
| 72 | struct sk_buff *skb; |
| 73 | void *reply; |
| 74 | |
| 75 | /* |
| 76 | * If new attributes are added, please revisit this allocation |
| 77 | */ |
Thomas Graf | 3dabc71 | 2006-11-14 19:44:52 -0800 | [diff] [blame] | 78 | skb = genlmsg_new(size, GFP_KERNEL); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 79 | if (!skb) |
| 80 | return -ENOMEM; |
| 81 | |
| 82 | if (!info) { |
Christoph Lameter | cd85fc5 | 2010-12-08 17:42:22 +0100 | [diff] [blame] | 83 | int seq = this_cpu_inc_return(taskstats_seqnum) - 1; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 84 | |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 85 | reply = genlmsg_put(skb, 0, seq, &family, 0, cmd); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 86 | } else |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 87 | reply = genlmsg_put_reply(skb, info, &family, 0, cmd); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 88 | if (reply == NULL) { |
| 89 | nlmsg_free(skb); |
| 90 | return -EINVAL; |
| 91 | } |
| 92 | |
| 93 | *skbp = skb; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 97 | /* |
| 98 | * Send taskstats data in @skb to listener with nl_pid @pid |
| 99 | */ |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 100 | static int send_reply(struct sk_buff *skb, struct genl_info *info) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 101 | { |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 102 | struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb)); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 103 | void *reply = genlmsg_data(genlhdr); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 104 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 105 | genlmsg_end(skb, reply); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 106 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 107 | return genlmsg_reply(skb, info); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 110 | /* |
| 111 | * Send taskstats data in @skb to listeners registered for @cpu's exit data |
| 112 | */ |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 113 | static void send_cpu_listeners(struct sk_buff *skb, |
| 114 | struct listener_list *listeners) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 115 | { |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 116 | struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb)); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 117 | struct listener *s, *tmp; |
| 118 | struct sk_buff *skb_next, *skb_cur = skb; |
| 119 | void *reply = genlmsg_data(genlhdr); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 120 | int rc, delcount = 0; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 121 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 122 | genlmsg_end(skb, reply); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 123 | |
| 124 | rc = 0; |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 125 | down_read(&listeners->sem); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 126 | list_for_each_entry(s, &listeners->list, list) { |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 127 | skb_next = NULL; |
| 128 | if (!list_is_last(&s->list, &listeners->list)) { |
| 129 | skb_next = skb_clone(skb_cur, GFP_KERNEL); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 130 | if (!skb_next) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 131 | break; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 132 | } |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 133 | rc = genlmsg_unicast(&init_net, skb_cur, s->pid); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 134 | if (rc == -ECONNREFUSED) { |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 135 | s->valid = 0; |
| 136 | delcount++; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 137 | } |
| 138 | skb_cur = skb_next; |
| 139 | } |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 140 | up_read(&listeners->sem); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 141 | |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 142 | if (skb_cur) |
| 143 | nlmsg_free(skb_cur); |
| 144 | |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 145 | if (!delcount) |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 146 | return; |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 147 | |
| 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 Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Eric W. Biederman | 4bd6e32 | 2012-02-07 17:56:49 -0800 | [diff] [blame] | 159 | static void fill_stats(struct user_namespace *user_ns, |
| 160 | struct pid_namespace *pid_ns, |
| 161 | struct task_struct *tsk, struct taskstats *stats) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 162 | { |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 163 | memset(stats, 0, sizeof(*stats)); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 164 | /* |
| 165 | * Each accounting subsystem adds calls to its functions to |
| 166 | * fill in relevant parts of struct taskstsats as follows |
| 167 | * |
Shailabh Nagar | 7d94ddd | 2006-07-30 03:03:10 -0700 | [diff] [blame] | 168 | * per-task-foo(stats, tsk); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 169 | */ |
| 170 | |
Shailabh Nagar | 7d94ddd | 2006-07-30 03:03:10 -0700 | [diff] [blame] | 171 | delayacct_add_tsk(stats, tsk); |
Jay Lan | f3cef7a | 2006-09-30 23:28:55 -0700 | [diff] [blame] | 172 | |
| 173 | /* fill in basic acct fields */ |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 174 | stats->version = TASKSTATS_VERSION; |
Maxim Uvarov | b663a79 | 2007-07-15 23:40:48 -0700 | [diff] [blame] | 175 | stats->nvcsw = tsk->nvcsw; |
| 176 | stats->nivcsw = tsk->nivcsw; |
Eric W. Biederman | 4bd6e32 | 2012-02-07 17:56:49 -0800 | [diff] [blame] | 177 | bacct_add_tsk(user_ns, pid_ns, stats, tsk); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 178 | |
Jay Lan | 9acc185 | 2006-09-30 23:28:58 -0700 | [diff] [blame] | 179 | /* fill in extended acct fields */ |
| 180 | xacct_add_tsk(stats, tsk); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Michael Holzheu | 3d9e0cf | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 183 | static int fill_stats_for_pid(pid_t pid, struct taskstats *stats) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 184 | { |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 185 | struct task_struct *tsk; |
Michael Holzheu | 3d9e0cf | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 186 | |
Mike Rapoport | 2ee0826 | 2018-02-06 15:40:17 -0800 | [diff] [blame] | 187 | tsk = find_get_task_by_vpid(pid); |
Michael Holzheu | 3d9e0cf | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 188 | if (!tsk) |
| 189 | return -ESRCH; |
Eric W. Biederman | 4bd6e32 | 2012-02-07 17:56:49 -0800 | [diff] [blame] | 190 | fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats); |
Michael Holzheu | 3d9e0cf | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 191 | put_task_struct(tsk); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats) |
| 196 | { |
| 197 | struct task_struct *tsk, *first; |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 198 | unsigned long flags; |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 199 | int rc = -ESRCH; |
Zhang Xiao | 8c73342 | 2017-05-08 15:56:45 -0700 | [diff] [blame] | 200 | u64 delta, utime, stime; |
| 201 | u64 start_time; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 202 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 203 | /* |
| 204 | * Add additional stats from live tasks except zombie thread group |
| 205 | * leaders who are already counted with the dead tasks |
| 206 | */ |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 207 | rcu_read_lock(); |
Michael Holzheu | 3d9e0cf | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 208 | first = find_task_by_vpid(tgid); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 209 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 210 | 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 Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 215 | else |
| 216 | memset(stats, 0, sizeof(*stats)); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 217 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 218 | tsk = first; |
Zhang Xiao | 8c73342 | 2017-05-08 15:56:45 -0700 | [diff] [blame] | 219 | start_time = ktime_get_ns(); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 220 | do { |
Oleg Nesterov | d7c3f5f | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 221 | if (tsk->exit_state) |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 222 | continue; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 223 | /* |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 224 | * Accounting subsystem can call its functions here to |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 225 | * fill in relevant parts of struct taskstsats as follows |
| 226 | * |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 227 | * per-task-foo(stats, tsk); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 228 | */ |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 229 | delayacct_add_tsk(stats, tsk); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 230 | |
Zhang Xiao | 8c73342 | 2017-05-08 15:56:45 -0700 | [diff] [blame] | 231 | /* 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 Uvarov | b663a79 | 2007-07-15 23:40:48 -0700 | [diff] [blame] | 241 | stats->nvcsw += tsk->nvcsw; |
| 242 | stats->nivcsw += tsk->nivcsw; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 243 | } while_each_thread(first, tsk); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 244 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 245 | unlock_task_sighand(first, &flags); |
| 246 | rc = 0; |
| 247 | out: |
| 248 | rcu_read_unlock(); |
| 249 | |
| 250 | stats->version = TASKSTATS_VERSION; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 251 | /* |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 252 | * Accounting subsystems can also add calls here to modify |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 253 | * fields of taskstats. |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 254 | */ |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 255 | return rc; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 258 | static void fill_tgid_exit(struct task_struct *tsk) |
| 259 | { |
| 260 | unsigned long flags; |
| 261 | |
Oleg Nesterov | b8534d7 | 2006-10-28 10:38:53 -0700 | [diff] [blame] | 262 | spin_lock_irqsave(&tsk->sighand->siglock, flags); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 263 | 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); |
| 273 | ret: |
Oleg Nesterov | b8534d7 | 2006-10-28 10:38:53 -0700 | [diff] [blame] | 274 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 278 | static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 279 | { |
| 280 | struct listener_list *listeners; |
Vasiliy Kulikov | 26c4cae | 2011-06-27 16:18:11 -0700 | [diff] [blame] | 281 | struct listener *s, *tmp, *s2; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 282 | unsigned int cpu; |
Chen Gang | 0d20633 | 2013-11-12 15:11:23 -0800 | [diff] [blame] | 283 | int ret = 0; |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 284 | |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 285 | if (!cpumask_subset(mask, cpu_possible_mask)) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 286 | return -EINVAL; |
| 287 | |
Eric W. Biederman | 4bd6e32 | 2012-02-07 17:56:49 -0800 | [diff] [blame] | 288 | 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 Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 294 | if (isadd == REGISTER) { |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 295 | for_each_cpu(cpu, mask) { |
Oleg Nesterov | dfc428b | 2011-08-03 16:21:04 -0700 | [diff] [blame] | 296 | s = kmalloc_node(sizeof(struct listener), |
| 297 | GFP_KERNEL, cpu_to_node(cpu)); |
Chen Gang | 0d20633 | 2013-11-12 15:11:23 -0800 | [diff] [blame] | 298 | if (!s) { |
| 299 | ret = -ENOMEM; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 300 | goto cleanup; |
Chen Gang | 0d20633 | 2013-11-12 15:11:23 -0800 | [diff] [blame] | 301 | } |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 302 | s->pid = pid; |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 303 | s->valid = 1; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 304 | |
| 305 | listeners = &per_cpu(listener_array, cpu); |
| 306 | down_write(&listeners->sem); |
Oleg Nesterov | dfc428b | 2011-08-03 16:21:04 -0700 | [diff] [blame] | 307 | list_for_each_entry(s2, &listeners->list, list) { |
Oleg Nesterov | a729589 | 2011-08-03 16:21:05 -0700 | [diff] [blame] | 308 | if (s2->pid == pid && s2->valid) |
Oleg Nesterov | dfc428b | 2011-08-03 16:21:04 -0700 | [diff] [blame] | 309 | goto exists; |
Vasiliy Kulikov | 26c4cae | 2011-06-27 16:18:11 -0700 | [diff] [blame] | 310 | } |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 311 | list_add(&s->list, &listeners->list); |
Vasiliy Kulikov | 26c4cae | 2011-06-27 16:18:11 -0700 | [diff] [blame] | 312 | s = NULL; |
Oleg Nesterov | dfc428b | 2011-08-03 16:21:04 -0700 | [diff] [blame] | 313 | exists: |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 314 | up_write(&listeners->sem); |
Oleg Nesterov | dfc428b | 2011-08-03 16:21:04 -0700 | [diff] [blame] | 315 | kfree(s); /* nop if NULL */ |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* Deregister or cleanup */ |
| 321 | cleanup: |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 322 | for_each_cpu(cpu, mask) { |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 323 | 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 Gang | 0d20633 | 2013-11-12 15:11:23 -0800 | [diff] [blame] | 334 | return ret; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 337 | static int parse(struct nlattr *na, struct cpumask *mask) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 338 | { |
| 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 Russell | 29c0177 | 2008-12-13 21:20:25 +1030 | [diff] [blame] | 354 | ret = cpulist_parse(data, mask); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 355 | kfree(data); |
| 356 | return ret; |
| 357 | } |
| 358 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 359 | static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid) |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 360 | { |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 361 | struct nlattr *na, *ret; |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 362 | int aggr; |
| 363 | |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 364 | aggr = (type == TASKSTATS_TYPE_PID) |
| 365 | ? TASKSTATS_TYPE_AGGR_PID |
| 366 | : TASKSTATS_TYPE_AGGR_TGID; |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 367 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 368 | na = nla_nest_start_noflag(skb, aggr); |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 369 | if (!na) |
| 370 | goto err; |
Jeff Mahoney | 4be2c95 | 2010-12-21 17:24:30 -0800 | [diff] [blame] | 371 | |
Chen Gang | 3fa5826 | 2013-11-12 15:11:22 -0800 | [diff] [blame] | 372 | if (nla_put(skb, type, sizeof(pid), &pid) < 0) { |
| 373 | nla_nest_cancel(skb, na); |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 374 | goto err; |
Chen Gang | 3fa5826 | 2013-11-12 15:11:22 -0800 | [diff] [blame] | 375 | } |
Nicolas Dichtel | 80df554 | 2016-04-22 17:31:24 +0200 | [diff] [blame] | 376 | ret = nla_reserve_64bit(skb, TASKSTATS_TYPE_STATS, |
| 377 | sizeof(struct taskstats), TASKSTATS_TYPE_NULL); |
Chen Gang | 3fa5826 | 2013-11-12 15:11:22 -0800 | [diff] [blame] | 378 | if (!ret) { |
| 379 | nla_nest_cancel(skb, na); |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 380 | goto err; |
Chen Gang | 3fa5826 | 2013-11-12 15:11:22 -0800 | [diff] [blame] | 381 | } |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 382 | nla_nest_end(skb, na); |
| 383 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 384 | return nla_data(ret); |
| 385 | err: |
| 386 | return NULL; |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 389 | static 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 Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 397 | struct fd f; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 398 | |
| 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 Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 404 | f = fdget(fd); |
| 405 | if (!f.file) |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 406 | return 0; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 407 | |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 408 | size = nla_total_size(sizeof(struct cgroupstats)); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 409 | |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 410 | rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb, |
| 411 | size); |
| 412 | if (rc < 0) |
| 413 | goto err; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 414 | |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 415 | na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS, |
| 416 | sizeof(struct cgroupstats)); |
Alan Cox | 25353b3 | 2012-07-30 14:42:49 -0700 | [diff] [blame] | 417 | if (na == NULL) { |
Jesper Juhl | 0324b5a | 2012-10-04 17:16:52 -0700 | [diff] [blame] | 418 | nlmsg_free(rep_skb); |
Alan Cox | 25353b3 | 2012-07-30 14:42:49 -0700 | [diff] [blame] | 419 | rc = -EMSGSIZE; |
| 420 | goto err; |
| 421 | } |
| 422 | |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 423 | stats = nla_data(na); |
| 424 | memset(stats, 0, sizeof(*stats)); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 425 | |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 426 | rc = cgroupstats_build(stats, f.file->f_path.dentry); |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 427 | if (rc < 0) { |
| 428 | nlmsg_free(rep_skb); |
| 429 | goto err; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 432 | rc = send_reply(rep_skb, info); |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 433 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 434 | err: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 435 | fdput(f); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 436 | return rc; |
| 437 | } |
| 438 | |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 439 | static int cmd_attr_register_cpumask(struct genl_info *info) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 440 | { |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 441 | cpumask_var_t mask; |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 442 | int rc; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 443 | |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 444 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
| 445 | return -ENOMEM; |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 446 | rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 447 | if (rc < 0) |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 448 | goto out; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 449 | rc = add_del_listener(info->snd_portid, mask, REGISTER); |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 450 | out: |
| 451 | free_cpumask_var(mask); |
| 452 | return rc; |
| 453 | } |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 454 | |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 455 | static 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 Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 462 | rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask); |
| 463 | if (rc < 0) |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 464 | goto out; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 465 | rc = add_del_listener(info->snd_portid, mask, DEREGISTER); |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 466 | out: |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 467 | free_cpumask_var(mask); |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 468 | return rc; |
| 469 | } |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 470 | |
Jeff Mahoney | 4be2c95 | 2010-12-21 17:24:30 -0800 | [diff] [blame] | 471 | static size_t taskstats_packet_size(void) |
| 472 | { |
| 473 | size_t size; |
| 474 | |
| 475 | size = nla_total_size(sizeof(u32)) + |
Nicolas Dichtel | 80df554 | 2016-04-22 17:31:24 +0200 | [diff] [blame] | 476 | nla_total_size_64bit(sizeof(struct taskstats)) + |
| 477 | nla_total_size(0); |
| 478 | |
Jeff Mahoney | 4be2c95 | 2010-12-21 17:24:30 -0800 | [diff] [blame] | 479 | return size; |
| 480 | } |
| 481 | |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 482 | static 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 Mahoney | 4be2c95 | 2010-12-21 17:24:30 -0800 | [diff] [blame] | 490 | size = taskstats_packet_size(); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 491 | |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 492 | rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 493 | if (rc < 0) |
| 494 | return rc; |
| 495 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 496 | rc = -EINVAL; |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 497 | pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]); |
| 498 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid); |
| 499 | if (!stats) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 500 | goto err; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 501 | |
Michael Holzheu | 3d9e0cf | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 502 | rc = fill_stats_for_pid(pid, stats); |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 503 | if (rc < 0) |
| 504 | goto err; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 505 | return send_reply(rep_skb, info); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 506 | err: |
| 507 | nlmsg_free(rep_skb); |
| 508 | return rc; |
| 509 | } |
| 510 | |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 511 | static 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 Mahoney | 4be2c95 | 2010-12-21 17:24:30 -0800 | [diff] [blame] | 519 | size = taskstats_packet_size(); |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 520 | |
| 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 Holzheu | 3d9e0cf | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 531 | rc = fill_stats_for_tgid(tgid, stats); |
Michael Holzheu | 9323312 | 2010-10-27 15:34:44 -0700 | [diff] [blame] | 532 | if (rc < 0) |
| 533 | goto err; |
| 534 | return send_reply(rep_skb, info); |
| 535 | err: |
| 536 | nlmsg_free(rep_skb); |
| 537 | return rc; |
| 538 | } |
| 539 | |
| 540 | static 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 Nesterov | 34ec1234 | 2006-12-06 20:36:52 -0800 | [diff] [blame] | 554 | static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk) |
| 555 | { |
| 556 | struct signal_struct *sig = tsk->signal; |
Christian Brauner | 0b8d616 | 2019-10-09 13:48:09 +0200 | [diff] [blame] | 557 | struct taskstats *stats_new, *stats; |
Oleg Nesterov | 34ec1234 | 2006-12-06 20:36:52 -0800 | [diff] [blame] | 558 | |
Christian Brauner | 0b8d616 | 2019-10-09 13:48:09 +0200 | [diff] [blame] | 559 | /* Pairs with smp_store_release() below. */ |
| 560 | stats = smp_load_acquire(&sig->stats); |
| 561 | if (stats || thread_group_empty(tsk)) |
| 562 | return stats; |
Oleg Nesterov | 34ec1234 | 2006-12-06 20:36:52 -0800 | [diff] [blame] | 563 | |
| 564 | /* No problem if kmem_cache_zalloc() fails */ |
Christian Brauner | 0b8d616 | 2019-10-09 13:48:09 +0200 | [diff] [blame] | 565 | stats_new = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL); |
Oleg Nesterov | 34ec1234 | 2006-12-06 20:36:52 -0800 | [diff] [blame] | 566 | |
| 567 | spin_lock_irq(&tsk->sighand->siglock); |
Christian Brauner | 0b8d616 | 2019-10-09 13:48:09 +0200 | [diff] [blame] | 568 | 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 Nesterov | 34ec1234 | 2006-12-06 20:36:52 -0800 | [diff] [blame] | 577 | } |
| 578 | spin_unlock_irq(&tsk->sighand->siglock); |
| 579 | |
Christian Brauner | 0b8d616 | 2019-10-09 13:48:09 +0200 | [diff] [blame] | 580 | if (stats_new) |
| 581 | kmem_cache_free(taskstats_cache, stats_new); |
| 582 | |
| 583 | return stats; |
Oleg Nesterov | 34ec1234 | 2006-12-06 20:36:52 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 586 | /* Send pid data out on exit */ |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 587 | void taskstats_exit(struct task_struct *tsk, int group_dead) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 588 | { |
| 589 | int rc; |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 590 | struct listener_list *listeners; |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 591 | struct taskstats *stats; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 592 | struct sk_buff *rep_skb; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 593 | size_t size; |
| 594 | int is_thread_group; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 595 | |
Oleg Nesterov | 4a279ff | 2006-10-30 22:07:15 -0800 | [diff] [blame] | 596 | if (!family_registered) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 597 | return; |
| 598 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 599 | /* |
| 600 | * Size includes space for nested attributes |
| 601 | */ |
Jeff Mahoney | 4be2c95 | 2010-12-21 17:24:30 -0800 | [diff] [blame] | 602 | size = taskstats_packet_size(); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 603 | |
Oleg Nesterov | 34ec1234 | 2006-12-06 20:36:52 -0800 | [diff] [blame] | 604 | is_thread_group = !!taskstats_tgid_alloc(tsk); |
Oleg Nesterov | 4a279ff | 2006-10-30 22:07:15 -0800 | [diff] [blame] | 605 | 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 Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 612 | listeners = raw_cpu_ptr(&listener_array); |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 613 | if (list_empty(&listeners->list)) |
| 614 | return; |
| 615 | |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 616 | rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 617 | if (rc < 0) |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 618 | return; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 619 | |
Eric W. Biederman | 4bd6e32 | 2012-02-07 17:56:49 -0800 | [diff] [blame] | 620 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, |
| 621 | task_pid_nr_ns(tsk, &init_pid_ns)); |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 622 | if (!stats) |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 623 | goto err; |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 624 | |
Eric W. Biederman | 4bd6e32 | 2012-02-07 17:56:49 -0800 | [diff] [blame] | 625 | fill_stats(&init_user_ns, &init_pid_ns, tsk, stats); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 626 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 627 | /* |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 628 | * Doesn't matter if tsk is the leader or the last group member leaving |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 629 | */ |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 630 | if (!is_thread_group || !group_dead) |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 631 | goto send; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 632 | |
Eric W. Biederman | 4bd6e32 | 2012-02-07 17:56:49 -0800 | [diff] [blame] | 633 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, |
| 634 | task_tgid_nr_ns(tsk, &init_pid_ns)); |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 635 | if (!stats) |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 636 | goto err; |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 637 | |
| 638 | memcpy(stats, tsk->signal->stats, sizeof(*stats)); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 639 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 640 | send: |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 641 | send_cpu_listeners(rep_skb, listeners); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 642 | return; |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 643 | err: |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 644 | nlmsg_free(rep_skb); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Johannes Berg | 4534de8 | 2013-11-14 17:14:46 +0100 | [diff] [blame] | 647 | static const struct genl_ops taskstats_ops[] = { |
Johannes Berg | 88d36a9 | 2013-11-14 17:14:39 +0100 | [diff] [blame] | 648 | { |
| 649 | .cmd = TASKSTATS_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 650 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Johannes Berg | 88d36a9 | 2013-11-14 17:14:39 +0100 | [diff] [blame] | 651 | .doit = taskstats_user_cmd, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 652 | /* policy enforced later */ |
| 653 | .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_HASPOL, |
Johannes Berg | 88d36a9 | 2013-11-14 17:14:39 +0100 | [diff] [blame] | 654 | }, |
| 655 | { |
| 656 | .cmd = CGROUPSTATS_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 657 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Johannes Berg | 88d36a9 | 2013-11-14 17:14:39 +0100 | [diff] [blame] | 658 | .doit = cgroupstats_user_cmd, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 659 | /* policy enforced later */ |
| 660 | .flags = GENL_CMD_CAP_HASPOL, |
Johannes Berg | 88d36a9 | 2013-11-14 17:14:39 +0100 | [diff] [blame] | 661 | }, |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 662 | }; |
| 663 | |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 664 | static 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 Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 680 | return nlmsg_validate_deprecated(info->nlhdr, GENL_HDRLEN, |
| 681 | TASKSTATS_CMD_ATTR_MAX, policy, |
| 682 | info->extack); |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 685 | static struct genl_family family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 686 | .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 Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 692 | .pre_doit = taskstats_pre_doit, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 693 | }; |
| 694 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 695 | /* Needed early in initialization */ |
| 696 | void __init taskstats_init_early(void) |
| 697 | { |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 698 | unsigned int i; |
| 699 | |
Christoph Lameter | 0a31bd5 | 2007-05-06 14:49:57 -0700 | [diff] [blame] | 700 | taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 701 | 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 Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static int __init taskstats_init(void) |
| 708 | { |
| 709 | int rc; |
| 710 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 711 | rc = genl_register_family(&family); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 712 | if (rc) |
| 713 | return rc; |
| 714 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 715 | family_registered = 1; |
Mandeep Singh Baines | f9b182e | 2011-03-23 16:43:27 -0700 | [diff] [blame] | 716 | pr_info("registered taskstats version %d\n", TASKSTATS_GENL_VERSION); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 717 | return 0; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* |
| 721 | * late initcall ensures initialization of statistics collection |
| 722 | * mechanisms precedes initialization of the taskstats interface |
| 723 | */ |
| 724 | late_initcall(taskstats_init); |