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