Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * taskstats.c - Export per-task statistics to userland |
| 3 | * |
| 4 | * Copyright (C) Shailabh Nagar, IBM Corp. 2006 |
| 5 | * (C) Balbir Singh, IBM Corp. 2006 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/taskstats_kern.h> |
Jay Lan | f3cef7a | 2006-09-30 23:28:55 -0700 | [diff] [blame] | 21 | #include <linux/tsacct_kern.h> |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 22 | #include <linux/delayacct.h> |
Jay Lan | 9acc185 | 2006-09-30 23:28:58 -0700 | [diff] [blame] | 23 | #include <linux/tsacct_kern.h> |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 24 | #include <linux/cpumask.h> |
| 25 | #include <linux/percpu.h> |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 26 | #include <net/genetlink.h> |
| 27 | #include <asm/atomic.h> |
| 28 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 29 | /* |
| 30 | * Maximum length of a cpumask that can be specified in |
| 31 | * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute |
| 32 | */ |
| 33 | #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS) |
| 34 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 35 | static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 }; |
| 36 | static int family_registered; |
| 37 | kmem_cache_t *taskstats_cache; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 38 | |
| 39 | static struct genl_family family = { |
| 40 | .id = GENL_ID_GENERATE, |
| 41 | .name = TASKSTATS_GENL_NAME, |
| 42 | .version = TASKSTATS_GENL_VERSION, |
| 43 | .maxattr = TASKSTATS_CMD_ATTR_MAX, |
| 44 | }; |
| 45 | |
| 46 | static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] |
| 47 | __read_mostly = { |
| 48 | [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 }, |
| 49 | [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 }, |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 50 | [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING }, |
| 51 | [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },}; |
| 52 | |
| 53 | struct listener { |
| 54 | struct list_head list; |
| 55 | pid_t pid; |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 56 | char valid; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 59 | struct listener_list { |
| 60 | struct rw_semaphore sem; |
| 61 | struct list_head list; |
| 62 | }; |
| 63 | static DEFINE_PER_CPU(struct listener_list, listener_array); |
| 64 | |
| 65 | enum actions { |
| 66 | REGISTER, |
| 67 | DEREGISTER, |
| 68 | CPU_DONT_CARE |
| 69 | }; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 70 | |
| 71 | static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp, |
| 72 | void **replyp, size_t size) |
| 73 | { |
| 74 | struct sk_buff *skb; |
| 75 | void *reply; |
| 76 | |
| 77 | /* |
| 78 | * If new attributes are added, please revisit this allocation |
| 79 | */ |
Thomas Graf | 3dabc71 | 2006-11-14 19:44:52 -0800 | [diff] [blame^] | 80 | skb = genlmsg_new(size, GFP_KERNEL); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 81 | if (!skb) |
| 82 | return -ENOMEM; |
| 83 | |
| 84 | if (!info) { |
| 85 | int seq = get_cpu_var(taskstats_seqnum)++; |
| 86 | put_cpu_var(taskstats_seqnum); |
| 87 | |
| 88 | reply = genlmsg_put(skb, 0, seq, |
| 89 | family.id, 0, 0, |
| 90 | cmd, family.version); |
| 91 | } else |
| 92 | reply = genlmsg_put(skb, info->snd_pid, info->snd_seq, |
| 93 | family.id, 0, 0, |
| 94 | cmd, family.version); |
| 95 | if (reply == NULL) { |
| 96 | nlmsg_free(skb); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | |
| 100 | *skbp = skb; |
| 101 | *replyp = reply; |
| 102 | return 0; |
| 103 | } |
| 104 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 105 | /* |
| 106 | * Send taskstats data in @skb to listener with nl_pid @pid |
| 107 | */ |
| 108 | static int send_reply(struct sk_buff *skb, pid_t pid) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 109 | { |
| 110 | struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 111 | void *reply = genlmsg_data(genlhdr); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 112 | int rc; |
| 113 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 114 | rc = genlmsg_end(skb, reply); |
| 115 | if (rc < 0) { |
| 116 | nlmsg_free(skb); |
| 117 | return rc; |
| 118 | } |
| 119 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 120 | return genlmsg_unicast(skb, pid); |
| 121 | } |
| 122 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Send taskstats data in @skb to listeners registered for @cpu's exit data |
| 125 | */ |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 126 | static void send_cpu_listeners(struct sk_buff *skb, unsigned int cpu) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 127 | { |
| 128 | struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data); |
| 129 | struct listener_list *listeners; |
| 130 | struct listener *s, *tmp; |
| 131 | struct sk_buff *skb_next, *skb_cur = skb; |
| 132 | void *reply = genlmsg_data(genlhdr); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 133 | int rc, delcount = 0; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 134 | |
| 135 | rc = genlmsg_end(skb, reply); |
| 136 | if (rc < 0) { |
| 137 | nlmsg_free(skb); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 138 | return; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | rc = 0; |
| 142 | listeners = &per_cpu(listener_array, cpu); |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 143 | down_read(&listeners->sem); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 144 | list_for_each_entry(s, &listeners->list, list) { |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 145 | skb_next = NULL; |
| 146 | if (!list_is_last(&s->list, &listeners->list)) { |
| 147 | skb_next = skb_clone(skb_cur, GFP_KERNEL); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 148 | if (!skb_next) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 149 | break; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 150 | } |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 151 | rc = genlmsg_unicast(skb_cur, s->pid); |
| 152 | if (rc == -ECONNREFUSED) { |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 153 | s->valid = 0; |
| 154 | delcount++; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 155 | } |
| 156 | skb_cur = skb_next; |
| 157 | } |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 158 | up_read(&listeners->sem); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 159 | |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 160 | if (skb_cur) |
| 161 | nlmsg_free(skb_cur); |
| 162 | |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 163 | if (!delcount) |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 164 | return; |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 165 | |
| 166 | /* Delete invalidated entries */ |
| 167 | down_write(&listeners->sem); |
| 168 | list_for_each_entry_safe(s, tmp, &listeners->list, list) { |
| 169 | if (!s->valid) { |
| 170 | list_del(&s->list); |
| 171 | kfree(s); |
| 172 | } |
| 173 | } |
| 174 | up_write(&listeners->sem); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 177 | static int fill_pid(pid_t pid, struct task_struct *tsk, |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 178 | struct taskstats *stats) |
| 179 | { |
Shailabh Nagar | 7d94ddd | 2006-07-30 03:03:10 -0700 | [diff] [blame] | 180 | int rc = 0; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 181 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 182 | if (!tsk) { |
| 183 | rcu_read_lock(); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 184 | tsk = find_task_by_pid(pid); |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 185 | if (tsk) |
| 186 | get_task_struct(tsk); |
| 187 | rcu_read_unlock(); |
| 188 | if (!tsk) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 189 | return -ESRCH; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 190 | } else |
| 191 | get_task_struct(tsk); |
| 192 | |
| 193 | /* |
| 194 | * Each accounting subsystem adds calls to its functions to |
| 195 | * fill in relevant parts of struct taskstsats as follows |
| 196 | * |
Shailabh Nagar | 7d94ddd | 2006-07-30 03:03:10 -0700 | [diff] [blame] | 197 | * per-task-foo(stats, tsk); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 198 | */ |
| 199 | |
Shailabh Nagar | 7d94ddd | 2006-07-30 03:03:10 -0700 | [diff] [blame] | 200 | delayacct_add_tsk(stats, tsk); |
Jay Lan | f3cef7a | 2006-09-30 23:28:55 -0700 | [diff] [blame] | 201 | |
| 202 | /* fill in basic acct fields */ |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 203 | stats->version = TASKSTATS_VERSION; |
Jay Lan | f3cef7a | 2006-09-30 23:28:55 -0700 | [diff] [blame] | 204 | bacct_add_tsk(stats, tsk); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 205 | |
Jay Lan | 9acc185 | 2006-09-30 23:28:58 -0700 | [diff] [blame] | 206 | /* fill in extended acct fields */ |
| 207 | xacct_add_tsk(stats, tsk); |
| 208 | |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 209 | /* Define err: label here if needed */ |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 210 | put_task_struct(tsk); |
| 211 | return rc; |
| 212 | |
| 213 | } |
| 214 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 215 | static int fill_tgid(pid_t tgid, struct task_struct *first, |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 216 | struct taskstats *stats) |
| 217 | { |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 218 | struct task_struct *tsk; |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 219 | unsigned long flags; |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 220 | int rc = -ESRCH; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 221 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 222 | /* |
| 223 | * Add additional stats from live tasks except zombie thread group |
| 224 | * leaders who are already counted with the dead tasks |
| 225 | */ |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 226 | rcu_read_lock(); |
| 227 | if (!first) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 228 | first = find_task_by_pid(tgid); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 229 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 230 | if (!first || !lock_task_sighand(first, &flags)) |
| 231 | goto out; |
| 232 | |
| 233 | if (first->signal->stats) |
| 234 | memcpy(stats, first->signal->stats, sizeof(*stats)); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 235 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 236 | tsk = first; |
| 237 | do { |
Oleg Nesterov | d7c3f5f | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 238 | if (tsk->exit_state) |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 239 | continue; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 240 | /* |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 241 | * Accounting subsystem can call its functions here to |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 242 | * fill in relevant parts of struct taskstsats as follows |
| 243 | * |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 244 | * per-task-foo(stats, tsk); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 245 | */ |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 246 | delayacct_add_tsk(stats, tsk); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 247 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 248 | } while_each_thread(first, tsk); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 249 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 250 | unlock_task_sighand(first, &flags); |
| 251 | rc = 0; |
| 252 | out: |
| 253 | rcu_read_unlock(); |
| 254 | |
| 255 | stats->version = TASKSTATS_VERSION; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 256 | /* |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 257 | * Accounting subsytems can also add calls here to modify |
| 258 | * fields of taskstats. |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 259 | */ |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 260 | return rc; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 263 | |
| 264 | static void fill_tgid_exit(struct task_struct *tsk) |
| 265 | { |
| 266 | unsigned long flags; |
| 267 | |
Oleg Nesterov | b8534d7 | 2006-10-28 10:38:53 -0700 | [diff] [blame] | 268 | spin_lock_irqsave(&tsk->sighand->siglock, flags); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 269 | if (!tsk->signal->stats) |
| 270 | goto ret; |
| 271 | |
| 272 | /* |
| 273 | * Each accounting subsystem calls its functions here to |
| 274 | * accumalate its per-task stats for tsk, into the per-tgid structure |
| 275 | * |
| 276 | * per-task-foo(tsk->signal->stats, tsk); |
| 277 | */ |
| 278 | delayacct_add_tsk(tsk->signal->stats, tsk); |
| 279 | ret: |
Oleg Nesterov | b8534d7 | 2006-10-28 10:38:53 -0700 | [diff] [blame] | 280 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 284 | static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd) |
| 285 | { |
| 286 | struct listener_list *listeners; |
| 287 | struct listener *s, *tmp; |
| 288 | unsigned int cpu; |
| 289 | cpumask_t mask = *maskp; |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 290 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 291 | if (!cpus_subset(mask, cpu_possible_map)) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | if (isadd == REGISTER) { |
| 295 | for_each_cpu_mask(cpu, mask) { |
| 296 | s = kmalloc_node(sizeof(struct listener), GFP_KERNEL, |
| 297 | cpu_to_node(cpu)); |
| 298 | if (!s) |
| 299 | goto cleanup; |
| 300 | s->pid = pid; |
| 301 | INIT_LIST_HEAD(&s->list); |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 302 | s->valid = 1; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 303 | |
| 304 | listeners = &per_cpu(listener_array, cpu); |
| 305 | down_write(&listeners->sem); |
| 306 | list_add(&s->list, &listeners->list); |
| 307 | up_write(&listeners->sem); |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /* Deregister or cleanup */ |
| 313 | cleanup: |
| 314 | for_each_cpu_mask(cpu, mask) { |
| 315 | listeners = &per_cpu(listener_array, cpu); |
| 316 | down_write(&listeners->sem); |
| 317 | list_for_each_entry_safe(s, tmp, &listeners->list, list) { |
| 318 | if (s->pid == pid) { |
| 319 | list_del(&s->list); |
| 320 | kfree(s); |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | up_write(&listeners->sem); |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int parse(struct nlattr *na, cpumask_t *mask) |
| 330 | { |
| 331 | char *data; |
| 332 | int len; |
| 333 | int ret; |
| 334 | |
| 335 | if (na == NULL) |
| 336 | return 1; |
| 337 | len = nla_len(na); |
| 338 | if (len > TASKSTATS_CPUMASK_MAXLEN) |
| 339 | return -E2BIG; |
| 340 | if (len < 1) |
| 341 | return -EINVAL; |
| 342 | data = kmalloc(len, GFP_KERNEL); |
| 343 | if (!data) |
| 344 | return -ENOMEM; |
| 345 | nla_strlcpy(data, na, len); |
| 346 | ret = cpulist_parse(data, *mask); |
| 347 | kfree(data); |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 352 | { |
| 353 | int rc = 0; |
| 354 | struct sk_buff *rep_skb; |
| 355 | struct taskstats stats; |
| 356 | void *reply; |
| 357 | size_t size; |
| 358 | struct nlattr *na; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 359 | cpumask_t mask; |
| 360 | |
| 361 | rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask); |
| 362 | if (rc < 0) |
| 363 | return rc; |
| 364 | if (rc == 0) |
| 365 | return add_del_listener(info->snd_pid, &mask, REGISTER); |
| 366 | |
| 367 | rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask); |
| 368 | if (rc < 0) |
| 369 | return rc; |
| 370 | if (rc == 0) |
| 371 | return add_del_listener(info->snd_pid, &mask, DEREGISTER); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 372 | |
| 373 | /* |
| 374 | * Size includes space for nested attributes |
| 375 | */ |
| 376 | size = nla_total_size(sizeof(u32)) + |
| 377 | nla_total_size(sizeof(struct taskstats)) + nla_total_size(0); |
| 378 | |
| 379 | memset(&stats, 0, sizeof(stats)); |
| 380 | rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size); |
| 381 | if (rc < 0) |
| 382 | return rc; |
| 383 | |
| 384 | if (info->attrs[TASKSTATS_CMD_ATTR_PID]) { |
| 385 | u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]); |
| 386 | rc = fill_pid(pid, NULL, &stats); |
| 387 | if (rc < 0) |
| 388 | goto err; |
| 389 | |
| 390 | na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID); |
| 391 | NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid); |
| 392 | NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS, |
| 393 | stats); |
| 394 | } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) { |
| 395 | u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]); |
| 396 | rc = fill_tgid(tgid, NULL, &stats); |
| 397 | if (rc < 0) |
| 398 | goto err; |
| 399 | |
| 400 | na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID); |
| 401 | NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid); |
| 402 | NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS, |
| 403 | stats); |
| 404 | } else { |
| 405 | rc = -EINVAL; |
| 406 | goto err; |
| 407 | } |
| 408 | |
| 409 | nla_nest_end(rep_skb, na); |
| 410 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 411 | return send_reply(rep_skb, info->snd_pid); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 412 | |
| 413 | nla_put_failure: |
Oleg Nesterov | d46a3d0 | 2006-10-29 16:45:58 +0300 | [diff] [blame] | 414 | rc = genlmsg_cancel(rep_skb, reply); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 415 | err: |
| 416 | nlmsg_free(rep_skb); |
| 417 | return rc; |
| 418 | } |
| 419 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 420 | void taskstats_exit_alloc(struct taskstats **ptidstats, unsigned int *mycpu) |
| 421 | { |
| 422 | struct listener_list *listeners; |
| 423 | struct taskstats *tmp; |
| 424 | /* |
| 425 | * This is the cpu on which the task is exiting currently and will |
| 426 | * be the one for which the exit event is sent, even if the cpu |
| 427 | * on which this function is running changes later. |
| 428 | */ |
| 429 | *mycpu = raw_smp_processor_id(); |
| 430 | |
| 431 | *ptidstats = NULL; |
| 432 | tmp = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL); |
| 433 | if (!tmp) |
| 434 | return; |
| 435 | |
| 436 | listeners = &per_cpu(listener_array, *mycpu); |
| 437 | down_read(&listeners->sem); |
| 438 | if (!list_empty(&listeners->list)) { |
| 439 | *ptidstats = tmp; |
| 440 | tmp = NULL; |
| 441 | } |
| 442 | up_read(&listeners->sem); |
| 443 | kfree(tmp); |
| 444 | } |
| 445 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 446 | /* Send pid data out on exit */ |
| 447 | void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats, |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 448 | int group_dead, unsigned int mycpu) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 449 | { |
| 450 | int rc; |
| 451 | struct sk_buff *rep_skb; |
| 452 | void *reply; |
| 453 | size_t size; |
| 454 | int is_thread_group; |
| 455 | struct nlattr *na; |
| 456 | |
Oleg Nesterov | 4a279ff | 2006-10-30 22:07:15 -0800 | [diff] [blame] | 457 | if (!family_registered) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 458 | return; |
| 459 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 460 | /* |
| 461 | * Size includes space for nested attributes |
| 462 | */ |
| 463 | size = nla_total_size(sizeof(u32)) + |
| 464 | nla_total_size(sizeof(struct taskstats)) + nla_total_size(0); |
| 465 | |
Oleg Nesterov | b8534d7 | 2006-10-28 10:38:53 -0700 | [diff] [blame] | 466 | is_thread_group = (tsk->signal->stats != NULL); |
Oleg Nesterov | 4a279ff | 2006-10-30 22:07:15 -0800 | [diff] [blame] | 467 | if (is_thread_group) { |
| 468 | /* PID + STATS + TGID + STATS */ |
| 469 | size = 2 * size; |
| 470 | /* fill the tsk->signal->stats structure */ |
| 471 | fill_tgid_exit(tsk); |
| 472 | } |
| 473 | |
| 474 | if (!tidstats) |
| 475 | return; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 476 | |
| 477 | rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size); |
| 478 | if (rc < 0) |
| 479 | goto ret; |
| 480 | |
| 481 | rc = fill_pid(tsk->pid, tsk, tidstats); |
| 482 | if (rc < 0) |
| 483 | goto err_skb; |
| 484 | |
| 485 | na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID); |
| 486 | NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid); |
| 487 | NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS, |
| 488 | *tidstats); |
| 489 | nla_nest_end(rep_skb, na); |
| 490 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 491 | if (!is_thread_group) |
| 492 | goto send; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 493 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 494 | /* |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 495 | * 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] | 496 | */ |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 497 | if (!group_dead) |
| 498 | goto send; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 499 | |
| 500 | na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID); |
| 501 | NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 502 | /* No locking needed for tsk->signal->stats since group is dead */ |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 503 | NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS, |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 504 | *tsk->signal->stats); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 505 | nla_nest_end(rep_skb, na); |
| 506 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 507 | send: |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 508 | send_cpu_listeners(rep_skb, mycpu); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 509 | return; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 510 | |
| 511 | nla_put_failure: |
| 512 | genlmsg_cancel(rep_skb, reply); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 513 | err_skb: |
| 514 | nlmsg_free(rep_skb); |
| 515 | ret: |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | |
| 519 | static struct genl_ops taskstats_ops = { |
| 520 | .cmd = TASKSTATS_CMD_GET, |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 521 | .doit = taskstats_user_cmd, |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 522 | .policy = taskstats_cmd_get_policy, |
| 523 | }; |
| 524 | |
| 525 | /* Needed early in initialization */ |
| 526 | void __init taskstats_init_early(void) |
| 527 | { |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 528 | unsigned int i; |
| 529 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 530 | taskstats_cache = kmem_cache_create("taskstats_cache", |
| 531 | sizeof(struct taskstats), |
| 532 | 0, SLAB_PANIC, NULL, NULL); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 533 | for_each_possible_cpu(i) { |
| 534 | INIT_LIST_HEAD(&(per_cpu(listener_array, i).list)); |
| 535 | init_rwsem(&(per_cpu(listener_array, i).sem)); |
| 536 | } |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static int __init taskstats_init(void) |
| 540 | { |
| 541 | int rc; |
| 542 | |
| 543 | rc = genl_register_family(&family); |
| 544 | if (rc) |
| 545 | return rc; |
| 546 | |
| 547 | rc = genl_register_ops(&family, &taskstats_ops); |
| 548 | if (rc < 0) |
| 549 | goto err; |
| 550 | |
| 551 | family_registered = 1; |
| 552 | return 0; |
| 553 | err: |
| 554 | genl_unregister_family(&family); |
| 555 | return rc; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * late initcall ensures initialization of statistics collection |
| 560 | * mechanisms precedes initialization of the taskstats interface |
| 561 | */ |
| 562 | late_initcall(taskstats_init); |