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