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> |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 23 | #include <linux/cpumask.h> |
| 24 | #include <linux/percpu.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 25 | #include <linux/cgroupstats.h> |
| 26 | #include <linux/cgroup.h> |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/file.h> |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 29 | #include <net/genetlink.h> |
| 30 | #include <asm/atomic.h> |
| 31 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 32 | /* |
| 33 | * Maximum length of a cpumask that can be specified in |
| 34 | * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute |
| 35 | */ |
| 36 | #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS) |
| 37 | |
Vegard Nossum | b81f3ea | 2008-07-25 01:48:55 -0700 | [diff] [blame] | 38 | static DEFINE_PER_CPU(__u32, taskstats_seqnum); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 39 | static int family_registered; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 40 | struct kmem_cache *taskstats_cache; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 41 | |
| 42 | static struct genl_family family = { |
| 43 | .id = GENL_ID_GENERATE, |
| 44 | .name = TASKSTATS_GENL_NAME, |
| 45 | .version = TASKSTATS_GENL_VERSION, |
| 46 | .maxattr = TASKSTATS_CMD_ATTR_MAX, |
| 47 | }; |
| 48 | |
Alexey Dobriyan | b54452b | 2010-02-18 08:14:31 +0000 | [diff] [blame] | 49 | 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] | 50 | [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 }, |
| 51 | [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 }, |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 52 | [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING }, |
| 53 | [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },}; |
| 54 | |
Alexey Dobriyan | b54452b | 2010-02-18 08:14:31 +0000 | [diff] [blame] | 55 | static const struct nla_policy cgroupstats_cmd_get_policy[CGROUPSTATS_CMD_ATTR_MAX+1] = { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 56 | [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 }, |
| 57 | }; |
| 58 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 59 | struct listener { |
| 60 | struct list_head list; |
| 61 | pid_t pid; |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 62 | char valid; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 65 | struct listener_list { |
| 66 | struct rw_semaphore sem; |
| 67 | struct list_head list; |
| 68 | }; |
| 69 | static DEFINE_PER_CPU(struct listener_list, listener_array); |
| 70 | |
| 71 | enum actions { |
| 72 | REGISTER, |
| 73 | DEREGISTER, |
| 74 | CPU_DONT_CARE |
| 75 | }; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 76 | |
| 77 | 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] | 78 | size_t size) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 79 | { |
| 80 | struct sk_buff *skb; |
| 81 | void *reply; |
| 82 | |
| 83 | /* |
| 84 | * If new attributes are added, please revisit this allocation |
| 85 | */ |
Thomas Graf | 3dabc71 | 2006-11-14 19:44:52 -0800 | [diff] [blame] | 86 | skb = genlmsg_new(size, GFP_KERNEL); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 87 | if (!skb) |
| 88 | return -ENOMEM; |
| 89 | |
| 90 | if (!info) { |
| 91 | int seq = get_cpu_var(taskstats_seqnum)++; |
| 92 | put_cpu_var(taskstats_seqnum); |
| 93 | |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 94 | reply = genlmsg_put(skb, 0, seq, &family, 0, cmd); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 95 | } else |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 96 | reply = genlmsg_put_reply(skb, info, &family, 0, cmd); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 97 | if (reply == NULL) { |
| 98 | nlmsg_free(skb); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | *skbp = skb; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 106 | /* |
| 107 | * Send taskstats data in @skb to listener with nl_pid @pid |
| 108 | */ |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 109 | static int send_reply(struct sk_buff *skb, struct genl_info *info) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 110 | { |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 111 | struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb)); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 112 | void *reply = genlmsg_data(genlhdr); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 113 | int rc; |
| 114 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 115 | rc = genlmsg_end(skb, reply); |
| 116 | if (rc < 0) { |
| 117 | nlmsg_free(skb); |
| 118 | return rc; |
| 119 | } |
| 120 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 121 | return genlmsg_reply(skb, info); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 124 | /* |
| 125 | * Send taskstats data in @skb to listeners registered for @cpu's exit data |
| 126 | */ |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 127 | static void send_cpu_listeners(struct sk_buff *skb, |
| 128 | struct listener_list *listeners) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 129 | { |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 130 | struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb)); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 131 | struct listener *s, *tmp; |
| 132 | struct sk_buff *skb_next, *skb_cur = skb; |
| 133 | void *reply = genlmsg_data(genlhdr); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 134 | int rc, delcount = 0; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 135 | |
| 136 | rc = genlmsg_end(skb, reply); |
| 137 | if (rc < 0) { |
| 138 | nlmsg_free(skb); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 139 | return; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | rc = 0; |
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 | } |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 151 | rc = genlmsg_unicast(&init_net, skb_cur, s->pid); |
Shailabh Nagar | d94a041 | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 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(); |
Pavel Emelyanov | cb41d6d | 2008-04-30 00:54:23 -0700 | [diff] [blame] | 184 | tsk = find_task_by_vpid(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 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 193 | memset(stats, 0, sizeof(*stats)); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 194 | /* |
| 195 | * Each accounting subsystem adds calls to its functions to |
| 196 | * fill in relevant parts of struct taskstsats as follows |
| 197 | * |
Shailabh Nagar | 7d94ddd | 2006-07-30 03:03:10 -0700 | [diff] [blame] | 198 | * per-task-foo(stats, tsk); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 199 | */ |
| 200 | |
Shailabh Nagar | 7d94ddd | 2006-07-30 03:03:10 -0700 | [diff] [blame] | 201 | delayacct_add_tsk(stats, tsk); |
Jay Lan | f3cef7a | 2006-09-30 23:28:55 -0700 | [diff] [blame] | 202 | |
| 203 | /* fill in basic acct fields */ |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 204 | stats->version = TASKSTATS_VERSION; |
Maxim Uvarov | b663a79 | 2007-07-15 23:40:48 -0700 | [diff] [blame] | 205 | stats->nvcsw = tsk->nvcsw; |
| 206 | stats->nivcsw = tsk->nivcsw; |
Jay Lan | f3cef7a | 2006-09-30 23:28:55 -0700 | [diff] [blame] | 207 | bacct_add_tsk(stats, tsk); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 208 | |
Jay Lan | 9acc185 | 2006-09-30 23:28:58 -0700 | [diff] [blame] | 209 | /* fill in extended acct fields */ |
| 210 | xacct_add_tsk(stats, tsk); |
| 211 | |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 212 | /* Define err: label here if needed */ |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 213 | put_task_struct(tsk); |
| 214 | return rc; |
| 215 | |
| 216 | } |
| 217 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 218 | static int fill_tgid(pid_t tgid, struct task_struct *first, |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 219 | struct taskstats *stats) |
| 220 | { |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 221 | struct task_struct *tsk; |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 222 | unsigned long flags; |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 223 | int rc = -ESRCH; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 224 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 225 | /* |
| 226 | * Add additional stats from live tasks except zombie thread group |
| 227 | * leaders who are already counted with the dead tasks |
| 228 | */ |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 229 | rcu_read_lock(); |
| 230 | if (!first) |
Pavel Emelyanov | cb41d6d | 2008-04-30 00:54:23 -0700 | [diff] [blame] | 231 | first = find_task_by_vpid(tgid); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 232 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 233 | if (!first || !lock_task_sighand(first, &flags)) |
| 234 | goto out; |
| 235 | |
| 236 | if (first->signal->stats) |
| 237 | memcpy(stats, first->signal->stats, sizeof(*stats)); |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 238 | else |
| 239 | memset(stats, 0, sizeof(*stats)); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 240 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 241 | tsk = first; |
| 242 | do { |
Oleg Nesterov | d7c3f5f | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 243 | if (tsk->exit_state) |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 244 | continue; |
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 | * Accounting subsystem can call its functions here to |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 247 | * fill in relevant parts of struct taskstsats as follows |
| 248 | * |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 249 | * per-task-foo(stats, tsk); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 250 | */ |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 251 | delayacct_add_tsk(stats, tsk); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 252 | |
Maxim Uvarov | b663a79 | 2007-07-15 23:40:48 -0700 | [diff] [blame] | 253 | stats->nvcsw += tsk->nvcsw; |
| 254 | stats->nivcsw += tsk->nivcsw; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 255 | } while_each_thread(first, tsk); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 256 | |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 257 | unlock_task_sighand(first, &flags); |
| 258 | rc = 0; |
| 259 | out: |
| 260 | rcu_read_unlock(); |
| 261 | |
| 262 | stats->version = TASKSTATS_VERSION; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 263 | /* |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 264 | * Accounting subsystems can also add calls here to modify |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 265 | * fields of taskstats. |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 266 | */ |
Oleg Nesterov | a98b609 | 2006-10-28 10:38:54 -0700 | [diff] [blame] | 267 | return rc; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 270 | |
| 271 | static void fill_tgid_exit(struct task_struct *tsk) |
| 272 | { |
| 273 | unsigned long flags; |
| 274 | |
Oleg Nesterov | b8534d7 | 2006-10-28 10:38:53 -0700 | [diff] [blame] | 275 | spin_lock_irqsave(&tsk->sighand->siglock, flags); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 276 | if (!tsk->signal->stats) |
| 277 | goto ret; |
| 278 | |
| 279 | /* |
| 280 | * Each accounting subsystem calls its functions here to |
| 281 | * accumalate its per-task stats for tsk, into the per-tgid structure |
| 282 | * |
| 283 | * per-task-foo(tsk->signal->stats, tsk); |
| 284 | */ |
| 285 | delayacct_add_tsk(tsk->signal->stats, tsk); |
| 286 | ret: |
Oleg Nesterov | b8534d7 | 2006-10-28 10:38:53 -0700 | [diff] [blame] | 287 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 291 | 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] | 292 | { |
| 293 | struct listener_list *listeners; |
| 294 | struct listener *s, *tmp; |
| 295 | unsigned int cpu; |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 296 | |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 297 | if (!cpumask_subset(mask, cpu_possible_mask)) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 298 | return -EINVAL; |
| 299 | |
| 300 | if (isadd == REGISTER) { |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 301 | for_each_cpu(cpu, mask) { |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 302 | s = kmalloc_node(sizeof(struct listener), GFP_KERNEL, |
| 303 | cpu_to_node(cpu)); |
| 304 | if (!s) |
| 305 | goto cleanup; |
| 306 | s->pid = pid; |
| 307 | INIT_LIST_HEAD(&s->list); |
Shailabh Nagar | bb12999 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 308 | s->valid = 1; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 309 | |
| 310 | listeners = &per_cpu(listener_array, cpu); |
| 311 | down_write(&listeners->sem); |
| 312 | list_add(&s->list, &listeners->list); |
| 313 | up_write(&listeners->sem); |
| 314 | } |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | /* Deregister or cleanup */ |
| 319 | cleanup: |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 320 | for_each_cpu(cpu, mask) { |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 321 | listeners = &per_cpu(listener_array, cpu); |
| 322 | down_write(&listeners->sem); |
| 323 | list_for_each_entry_safe(s, tmp, &listeners->list, list) { |
| 324 | if (s->pid == pid) { |
| 325 | list_del(&s->list); |
| 326 | kfree(s); |
| 327 | break; |
| 328 | } |
| 329 | } |
| 330 | up_write(&listeners->sem); |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 335 | static int parse(struct nlattr *na, struct cpumask *mask) |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 336 | { |
| 337 | char *data; |
| 338 | int len; |
| 339 | int ret; |
| 340 | |
| 341 | if (na == NULL) |
| 342 | return 1; |
| 343 | len = nla_len(na); |
| 344 | if (len > TASKSTATS_CPUMASK_MAXLEN) |
| 345 | return -E2BIG; |
| 346 | if (len < 1) |
| 347 | return -EINVAL; |
| 348 | data = kmalloc(len, GFP_KERNEL); |
| 349 | if (!data) |
| 350 | return -ENOMEM; |
| 351 | nla_strlcpy(data, na, len); |
Rusty Russell | 29c0177 | 2008-12-13 21:20:25 +1030 | [diff] [blame] | 352 | ret = cpulist_parse(data, mask); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 353 | kfree(data); |
| 354 | return ret; |
| 355 | } |
| 356 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 357 | 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] | 358 | { |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 359 | struct nlattr *na, *ret; |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 360 | int aggr; |
| 361 | |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 362 | aggr = (type == TASKSTATS_TYPE_PID) |
| 363 | ? TASKSTATS_TYPE_AGGR_PID |
| 364 | : TASKSTATS_TYPE_AGGR_TGID; |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 365 | |
| 366 | na = nla_nest_start(skb, aggr); |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 367 | if (!na) |
| 368 | goto err; |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 369 | if (nla_put(skb, type, sizeof(pid), &pid) < 0) |
| 370 | goto err; |
| 371 | ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats)); |
| 372 | if (!ret) |
| 373 | goto err; |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 374 | nla_nest_end(skb, na); |
| 375 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 376 | return nla_data(ret); |
| 377 | err: |
| 378 | return NULL; |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 381 | static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info) |
| 382 | { |
| 383 | int rc = 0; |
| 384 | struct sk_buff *rep_skb; |
| 385 | struct cgroupstats *stats; |
| 386 | struct nlattr *na; |
| 387 | size_t size; |
| 388 | u32 fd; |
| 389 | struct file *file; |
| 390 | int fput_needed; |
| 391 | |
| 392 | na = info->attrs[CGROUPSTATS_CMD_ATTR_FD]; |
| 393 | if (!na) |
| 394 | return -EINVAL; |
| 395 | |
| 396 | fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]); |
| 397 | file = fget_light(fd, &fput_needed); |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 398 | if (!file) |
| 399 | return 0; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 400 | |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 401 | size = nla_total_size(sizeof(struct cgroupstats)); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 402 | |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 403 | rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb, |
| 404 | size); |
| 405 | if (rc < 0) |
| 406 | goto err; |
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 | na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS, |
| 409 | sizeof(struct cgroupstats)); |
| 410 | stats = nla_data(na); |
| 411 | memset(stats, 0, sizeof(*stats)); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 412 | |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 413 | rc = cgroupstats_build(stats, file->f_dentry); |
| 414 | if (rc < 0) { |
| 415 | nlmsg_free(rep_skb); |
| 416 | goto err; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 419 | rc = send_reply(rep_skb, info); |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 420 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 421 | err: |
Adrian Bunk | f961598 | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 422 | fput_light(file, fput_needed); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 423 | return rc; |
| 424 | } |
| 425 | |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 426 | 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] | 427 | { |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 428 | int rc; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 429 | struct sk_buff *rep_skb; |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 430 | struct taskstats *stats; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 431 | size_t size; |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 432 | cpumask_var_t mask; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 433 | |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 434 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
| 435 | return -ENOMEM; |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 436 | |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 437 | rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 438 | if (rc < 0) |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 439 | goto free_return_rc; |
| 440 | if (rc == 0) { |
| 441 | rc = add_del_listener(info->snd_pid, mask, REGISTER); |
| 442 | goto free_return_rc; |
| 443 | } |
| 444 | |
| 445 | rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask); |
| 446 | if (rc < 0) |
| 447 | goto free_return_rc; |
| 448 | if (rc == 0) { |
| 449 | rc = add_del_listener(info->snd_pid, mask, DEREGISTER); |
| 450 | free_return_rc: |
| 451 | free_cpumask_var(mask); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 452 | return rc; |
Rusty Russell | 41c7bb9 | 2009-01-01 10:12:28 +1030 | [diff] [blame] | 453 | } |
| 454 | free_cpumask_var(mask); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 455 | |
| 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 | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 462 | rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 463 | if (rc < 0) |
| 464 | return rc; |
| 465 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 466 | rc = -EINVAL; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 467 | if (info->attrs[TASKSTATS_CMD_ATTR_PID]) { |
| 468 | u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]); |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 469 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid); |
| 470 | if (!stats) |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 471 | goto err; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 472 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 473 | rc = fill_pid(pid, NULL, stats); |
| 474 | if (rc < 0) |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 475 | goto err; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 476 | } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) { |
| 477 | u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]); |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 478 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid); |
| 479 | if (!stats) |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 480 | goto err; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 481 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 482 | rc = fill_tgid(tgid, NULL, stats); |
| 483 | if (rc < 0) |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 484 | goto err; |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 485 | } else |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 486 | goto err; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 487 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 488 | return send_reply(rep_skb, info); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 489 | err: |
| 490 | nlmsg_free(rep_skb); |
| 491 | return rc; |
| 492 | } |
| 493 | |
Oleg Nesterov | 34ec1234 | 2006-12-06 20:36:52 -0800 | [diff] [blame] | 494 | static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk) |
| 495 | { |
| 496 | struct signal_struct *sig = tsk->signal; |
| 497 | struct taskstats *stats; |
| 498 | |
| 499 | if (sig->stats || thread_group_empty(tsk)) |
| 500 | goto ret; |
| 501 | |
| 502 | /* No problem if kmem_cache_zalloc() fails */ |
| 503 | stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL); |
| 504 | |
| 505 | spin_lock_irq(&tsk->sighand->siglock); |
| 506 | if (!sig->stats) { |
| 507 | sig->stats = stats; |
| 508 | stats = NULL; |
| 509 | } |
| 510 | spin_unlock_irq(&tsk->sighand->siglock); |
| 511 | |
| 512 | if (stats) |
| 513 | kmem_cache_free(taskstats_cache, stats); |
| 514 | ret: |
| 515 | return sig->stats; |
| 516 | } |
| 517 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 518 | /* Send pid data out on exit */ |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 519 | void taskstats_exit(struct task_struct *tsk, int group_dead) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 520 | { |
| 521 | int rc; |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 522 | struct listener_list *listeners; |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 523 | struct taskstats *stats; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 524 | struct sk_buff *rep_skb; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 525 | size_t size; |
| 526 | int is_thread_group; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 527 | |
Oleg Nesterov | 4a279ff | 2006-10-30 22:07:15 -0800 | [diff] [blame] | 528 | if (!family_registered) |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 529 | return; |
| 530 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 531 | /* |
| 532 | * Size includes space for nested attributes |
| 533 | */ |
| 534 | size = nla_total_size(sizeof(u32)) + |
| 535 | nla_total_size(sizeof(struct taskstats)) + nla_total_size(0); |
| 536 | |
Oleg Nesterov | 34ec1234 | 2006-12-06 20:36:52 -0800 | [diff] [blame] | 537 | is_thread_group = !!taskstats_tgid_alloc(tsk); |
Oleg Nesterov | 4a279ff | 2006-10-30 22:07:15 -0800 | [diff] [blame] | 538 | if (is_thread_group) { |
| 539 | /* PID + STATS + TGID + STATS */ |
| 540 | size = 2 * size; |
| 541 | /* fill the tsk->signal->stats structure */ |
| 542 | fill_tgid_exit(tsk); |
| 543 | } |
| 544 | |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 545 | listeners = &__raw_get_cpu_var(listener_array); |
| 546 | if (list_empty(&listeners->list)) |
| 547 | return; |
| 548 | |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 549 | rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 550 | if (rc < 0) |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 551 | return; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 552 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 553 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid); |
| 554 | if (!stats) |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 555 | goto err; |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 556 | |
Pavel Emelyanov | cb41d6d | 2008-04-30 00:54:23 -0700 | [diff] [blame] | 557 | rc = fill_pid(-1, tsk, stats); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 558 | if (rc < 0) |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 559 | goto err; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 560 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 561 | /* |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 562 | * 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] | 563 | */ |
Oleg Nesterov | 68062b8 | 2006-12-06 20:36:53 -0800 | [diff] [blame] | 564 | if (!is_thread_group || !group_dead) |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 565 | goto send; |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 566 | |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 567 | stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid); |
| 568 | if (!stats) |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 569 | goto err; |
Oleg Nesterov | 51de4d9 | 2006-12-06 20:36:54 -0800 | [diff] [blame] | 570 | |
| 571 | memcpy(stats, tsk->signal->stats, sizeof(*stats)); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 572 | |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 573 | send: |
Oleg Nesterov | 115085e | 2006-12-06 20:36:51 -0800 | [diff] [blame] | 574 | send_cpu_listeners(rep_skb, listeners); |
Shailabh Nagar | ad4ecbc | 2006-07-14 00:24:44 -0700 | [diff] [blame] | 575 | return; |
Oleg Nesterov | 37167485 | 2006-12-06 20:36:55 -0800 | [diff] [blame] | 576 | err: |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 577 | nlmsg_free(rep_skb); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static struct genl_ops taskstats_ops = { |
| 581 | .cmd = TASKSTATS_CMD_GET, |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 582 | .doit = taskstats_user_cmd, |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 583 | .policy = taskstats_cmd_get_policy, |
| 584 | }; |
| 585 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 586 | static struct genl_ops cgroupstats_ops = { |
| 587 | .cmd = CGROUPSTATS_CMD_GET, |
| 588 | .doit = cgroupstats_user_cmd, |
| 589 | .policy = cgroupstats_cmd_get_policy, |
| 590 | }; |
| 591 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 592 | /* Needed early in initialization */ |
| 593 | void __init taskstats_init_early(void) |
| 594 | { |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 595 | unsigned int i; |
| 596 | |
Christoph Lameter | 0a31bd5 | 2007-05-06 14:49:57 -0700 | [diff] [blame] | 597 | taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC); |
Shailabh Nagar | f9fd891 | 2006-07-14 00:24:47 -0700 | [diff] [blame] | 598 | for_each_possible_cpu(i) { |
| 599 | INIT_LIST_HEAD(&(per_cpu(listener_array, i).list)); |
| 600 | init_rwsem(&(per_cpu(listener_array, i).sem)); |
| 601 | } |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static int __init taskstats_init(void) |
| 605 | { |
| 606 | int rc; |
| 607 | |
| 608 | rc = genl_register_family(&family); |
| 609 | if (rc) |
| 610 | return rc; |
| 611 | |
| 612 | rc = genl_register_ops(&family, &taskstats_ops); |
| 613 | if (rc < 0) |
| 614 | goto err; |
| 615 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 616 | rc = genl_register_ops(&family, &cgroupstats_ops); |
| 617 | if (rc < 0) |
| 618 | goto err_cgroup_ops; |
| 619 | |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 620 | family_registered = 1; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 621 | printk("registered taskstats version %d\n", TASKSTATS_GENL_VERSION); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 622 | return 0; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 623 | err_cgroup_ops: |
| 624 | genl_unregister_ops(&family, &taskstats_ops); |
Shailabh Nagar | c757249 | 2006-07-14 00:24:40 -0700 | [diff] [blame] | 625 | err: |
| 626 | genl_unregister_family(&family); |
| 627 | return rc; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * late initcall ensures initialization of statistics collection |
| 632 | * mechanisms precedes initialization of the taskstats interface |
| 633 | */ |
| 634 | late_initcall(taskstats_init); |