mac80211: fix deauth before assoc
[safe/jmp/linux-2.6] / kernel / taskstats.c
index d2a4133..11281d5 100644 (file)
 #include <linux/taskstats_kern.h>
 #include <linux/tsacct_kern.h>
 #include <linux/delayacct.h>
-#include <linux/tsacct_kern.h>
 #include <linux/cpumask.h>
 #include <linux/percpu.h>
+#include <linux/slab.h>
+#include <linux/cgroupstats.h>
+#include <linux/cgroup.h>
+#include <linux/fs.h>
+#include <linux/file.h>
 #include <net/genetlink.h>
 #include <asm/atomic.h>
 
@@ -32,7 +36,7 @@
  */
 #define TASKSTATS_CPUMASK_MAXLEN       (100+6*NR_CPUS)
 
-static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
+static DEFINE_PER_CPU(__u32, taskstats_seqnum);
 static int family_registered;
 struct kmem_cache *taskstats_cache;
 
@@ -43,13 +47,16 @@ static struct genl_family family = {
        .maxattr        = TASKSTATS_CMD_ATTR_MAX,
 };
 
-static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
-__read_mostly = {
+static const struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = {
        [TASKSTATS_CMD_ATTR_PID]  = { .type = NLA_U32 },
        [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
        [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
        [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
 
+static const struct nla_policy cgroupstats_cmd_get_policy[CGROUPSTATS_CMD_ATTR_MAX+1] = {
+       [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
+};
+
 struct listener {
        struct list_head list;
        pid_t pid;
@@ -69,7 +76,7 @@ enum actions {
 };
 
 static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
-                       void **replyp, size_t size)
+                               size_t size)
 {
        struct sk_buff *skb;
        void *reply;
@@ -94,16 +101,15 @@ static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
        }
 
        *skbp = skb;
-       *replyp = reply;
        return 0;
 }
 
 /*
  * Send taskstats data in @skb to listener with nl_pid @pid
  */
-static int send_reply(struct sk_buff *skb, pid_t pid)
+static int send_reply(struct sk_buff *skb, struct genl_info *info)
 {
-       struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
+       struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
        void *reply = genlmsg_data(genlhdr);
        int rc;
 
@@ -113,7 +119,7 @@ static int send_reply(struct sk_buff *skb, pid_t pid)
                return rc;
        }
 
-       return genlmsg_unicast(skb, pid);
+       return genlmsg_reply(skb, info);
 }
 
 /*
@@ -122,7 +128,7 @@ static int send_reply(struct sk_buff *skb, pid_t pid)
 static void send_cpu_listeners(struct sk_buff *skb,
                                        struct listener_list *listeners)
 {
-       struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
+       struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
        struct listener *s, *tmp;
        struct sk_buff *skb_next, *skb_cur = skb;
        void *reply = genlmsg_data(genlhdr);
@@ -143,7 +149,7 @@ static void send_cpu_listeners(struct sk_buff *skb,
                        if (!skb_next)
                                break;
                }
-               rc = genlmsg_unicast(skb_cur, s->pid);
+               rc = genlmsg_unicast(&init_net, skb_cur, s->pid);
                if (rc == -ECONNREFUSED) {
                        s->valid = 0;
                        delcount++;
@@ -176,7 +182,7 @@ static int fill_pid(pid_t pid, struct task_struct *tsk,
 
        if (!tsk) {
                rcu_read_lock();
-               tsk = find_task_by_pid(pid);
+               tsk = find_task_by_vpid(pid);
                if (tsk)
                        get_task_struct(tsk);
                rcu_read_unlock();
@@ -185,6 +191,7 @@ static int fill_pid(pid_t pid, struct task_struct *tsk,
        } else
                get_task_struct(tsk);
 
+       memset(stats, 0, sizeof(*stats));
        /*
         * Each accounting subsystem adds calls to its functions to
         * fill in relevant parts of struct taskstsats as follows
@@ -196,6 +203,8 @@ static int fill_pid(pid_t pid, struct task_struct *tsk,
 
        /* fill in basic acct fields */
        stats->version = TASKSTATS_VERSION;
+       stats->nvcsw = tsk->nvcsw;
+       stats->nivcsw = tsk->nivcsw;
        bacct_add_tsk(stats, tsk);
 
        /* fill in extended acct fields */
@@ -220,13 +229,15 @@ static int fill_tgid(pid_t tgid, struct task_struct *first,
         */
        rcu_read_lock();
        if (!first)
-               first = find_task_by_pid(tgid);
+               first = find_task_by_vpid(tgid);
 
        if (!first || !lock_task_sighand(first, &flags))
                goto out;
 
        if (first->signal->stats)
                memcpy(stats, first->signal->stats, sizeof(*stats));
+       else
+               memset(stats, 0, sizeof(*stats));
 
        tsk = first;
        do {
@@ -240,6 +251,8 @@ static int fill_tgid(pid_t tgid, struct task_struct *first,
                 */
                delayacct_add_tsk(stats, tsk);
 
+               stats->nvcsw += tsk->nvcsw;
+               stats->nivcsw += tsk->nivcsw;
        } while_each_thread(first, tsk);
 
        unlock_task_sighand(first, &flags);
@@ -249,7 +262,7 @@ out:
 
        stats->version = TASKSTATS_VERSION;
        /*
-        * Accounting subsytems can also add calls here to modify
+        * Accounting subsystems can also add calls here to modify
         * fields of taskstats.
         */
        return rc;
@@ -276,18 +289,17 @@ ret:
        return;
 }
 
-static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
+static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
 {
        struct listener_list *listeners;
        struct listener *s, *tmp;
        unsigned int cpu;
-       cpumask_t mask = *maskp;
 
-       if (!cpus_subset(mask, cpu_possible_map))
+       if (!cpumask_subset(mask, cpu_possible_mask))
                return -EINVAL;
 
        if (isadd == REGISTER) {
-               for_each_cpu_mask(cpu, mask) {
+               for_each_cpu(cpu, mask) {
                        s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
                                         cpu_to_node(cpu));
                        if (!s)
@@ -306,7 +318,7 @@ static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
 
        /* Deregister or cleanup */
 cleanup:
-       for_each_cpu_mask(cpu, mask) {
+       for_each_cpu(cpu, mask) {
                listeners = &per_cpu(listener_array, cpu);
                down_write(&listeners->sem);
                list_for_each_entry_safe(s, tmp, &listeners->list, list) {
@@ -321,7 +333,7 @@ cleanup:
        return 0;
 }
 
-static int parse(struct nlattr *na, cpumask_t *mask)
+static int parse(struct nlattr *na, struct cpumask *mask)
 {
        char *data;
        int len;
@@ -338,50 +350,109 @@ static int parse(struct nlattr *na, cpumask_t *mask)
        if (!data)
                return -ENOMEM;
        nla_strlcpy(data, na, len);
-       ret = cpulist_parse(data, *mask);
+       ret = cpulist_parse(data, mask);
        kfree(data);
        return ret;
 }
 
-static int mk_reply(struct sk_buff *skb, int type, u32 pid, struct taskstats *stats)
+static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
 {
-       struct nlattr *na;
+       struct nlattr *na, *ret;
        int aggr;
 
-       aggr = TASKSTATS_TYPE_AGGR_TGID;
-       if (type == TASKSTATS_TYPE_PID)
-               aggr = TASKSTATS_TYPE_AGGR_PID;
+       aggr = (type == TASKSTATS_TYPE_PID)
+                       ? TASKSTATS_TYPE_AGGR_PID
+                       : TASKSTATS_TYPE_AGGR_TGID;
 
        na = nla_nest_start(skb, aggr);
-       NLA_PUT_U32(skb, type, pid);
-       NLA_PUT_TYPE(skb, struct taskstats, TASKSTATS_TYPE_STATS, *stats);
+       if (!na)
+               goto err;
+       if (nla_put(skb, type, sizeof(pid), &pid) < 0)
+               goto err;
+       ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
+       if (!ret)
+               goto err;
        nla_nest_end(skb, na);
 
-       return 0;
-nla_put_failure:
-       return -1;
+       return nla_data(ret);
+err:
+       return NULL;
 }
 
-static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
+static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
 {
        int rc = 0;
        struct sk_buff *rep_skb;
-       struct taskstats stats;
-       void *reply;
+       struct cgroupstats *stats;
+       struct nlattr *na;
        size_t size;
-       cpumask_t mask;
+       u32 fd;
+       struct file *file;
+       int fput_needed;
+
+       na = info->attrs[CGROUPSTATS_CMD_ATTR_FD];
+       if (!na)
+               return -EINVAL;
+
+       fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]);
+       file = fget_light(fd, &fput_needed);
+       if (!file)
+               return 0;
+
+       size = nla_total_size(sizeof(struct cgroupstats));
 
-       rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
+       rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb,
+                               size);
        if (rc < 0)
-               return rc;
-       if (rc == 0)
-               return add_del_listener(info->snd_pid, &mask, REGISTER);
+               goto err;
+
+       na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS,
+                               sizeof(struct cgroupstats));
+       stats = nla_data(na);
+       memset(stats, 0, sizeof(*stats));
+
+       rc = cgroupstats_build(stats, file->f_dentry);
+       if (rc < 0) {
+               nlmsg_free(rep_skb);
+               goto err;
+       }
+
+       rc = send_reply(rep_skb, info);
+
+err:
+       fput_light(file, fput_needed);
+       return rc;
+}
+
+static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
+{
+       int rc;
+       struct sk_buff *rep_skb;
+       struct taskstats *stats;
+       size_t size;
+       cpumask_var_t mask;
+
+       if (!alloc_cpumask_var(&mask, GFP_KERNEL))
+               return -ENOMEM;
 
-       rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
+       rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask);
        if (rc < 0)
+               goto free_return_rc;
+       if (rc == 0) {
+               rc = add_del_listener(info->snd_pid, mask, REGISTER);
+               goto free_return_rc;
+       }
+
+       rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask);
+       if (rc < 0)
+               goto free_return_rc;
+       if (rc == 0) {
+               rc = add_del_listener(info->snd_pid, mask, DEREGISTER);
+free_return_rc:
+               free_cpumask_var(mask);
                return rc;
-       if (rc == 0)
-               return add_del_listener(info->snd_pid, &mask, DEREGISTER);
+       }
+       free_cpumask_var(mask);
 
        /*
         * Size includes space for nested attributes
@@ -389,36 +460,33 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
        size = nla_total_size(sizeof(u32)) +
                nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
 
-       memset(&stats, 0, sizeof(stats));
-       rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
+       rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
        if (rc < 0)
                return rc;
 
+       rc = -EINVAL;
        if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
                u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
-               rc = fill_pid(pid, NULL, &stats);
-               if (rc < 0)
+               stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
+               if (!stats)
                        goto err;
 
-               if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid, &stats))
-                       goto nla_put_failure;
+               rc = fill_pid(pid, NULL, stats);
+               if (rc < 0)
+                       goto err;
        } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
                u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
-               rc = fill_tgid(tgid, NULL, &stats);
-               if (rc < 0)
+               stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
+               if (!stats)
                        goto err;
 
-               if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid, &stats))
-                       goto nla_put_failure;
-       } else {
-               rc = -EINVAL;
+               rc = fill_tgid(tgid, NULL, stats);
+               if (rc < 0)
+                       goto err;
+       } else
                goto err;
-       }
 
-       return send_reply(rep_skb, info->snd_pid);
-
-nla_put_failure:
-       rc = genlmsg_cancel(rep_skb, reply);
+       return send_reply(rep_skb, info);
 err:
        nlmsg_free(rep_skb);
        return rc;
@@ -453,9 +521,8 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
 {
        int rc;
        struct listener_list *listeners;
-       struct taskstats *tidstats;
+       struct taskstats *stats;
        struct sk_buff *rep_skb;
-       void *reply;
        size_t size;
        int is_thread_group;
 
@@ -480,20 +547,17 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
        if (list_empty(&listeners->list))
                return;
 
-       tidstats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
-       if (!tidstats)
+       rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
+       if (rc < 0)
                return;
 
-       rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
-       if (rc < 0)
-               goto free_stats;
+       stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
+       if (!stats)
+               goto err;
 
-       rc = fill_pid(tsk->pid, tsk, tidstats);
+       rc = fill_pid(-1, tsk, stats);
        if (rc < 0)
-               goto err_skb;
-
-       if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid, tidstats))
-               goto nla_put_failure;
+               goto err;
 
        /*
         * Doesn't matter if tsk is the leader or the last group member leaving
@@ -501,20 +565,17 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
        if (!is_thread_group || !group_dead)
                goto send;
 
-       if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid, tsk->signal->stats))
-               goto nla_put_failure;
+       stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
+       if (!stats)
+               goto err;
+
+       memcpy(stats, tsk->signal->stats, sizeof(*stats));
 
 send:
        send_cpu_listeners(rep_skb, listeners);
-free_stats:
-       kmem_cache_free(taskstats_cache, tidstats);
        return;
-
-nla_put_failure:
-       genlmsg_cancel(rep_skb, reply);
-err_skb:
+err:
        nlmsg_free(rep_skb);
-       goto free_stats;
 }
 
 static struct genl_ops taskstats_ops = {
@@ -523,14 +584,18 @@ static struct genl_ops taskstats_ops = {
        .policy         = taskstats_cmd_get_policy,
 };
 
+static struct genl_ops cgroupstats_ops = {
+       .cmd            = CGROUPSTATS_CMD_GET,
+       .doit           = cgroupstats_user_cmd,
+       .policy         = cgroupstats_cmd_get_policy,
+};
+
 /* Needed early in initialization */
 void __init taskstats_init_early(void)
 {
        unsigned int i;
 
-       taskstats_cache = kmem_cache_create("taskstats_cache",
-                                               sizeof(struct taskstats),
-                                               0, SLAB_PANIC, NULL, NULL);
+       taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
        for_each_possible_cpu(i) {
                INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
                init_rwsem(&(per_cpu(listener_array, i).sem));
@@ -549,8 +614,15 @@ static int __init taskstats_init(void)
        if (rc < 0)
                goto err;
 
+       rc = genl_register_ops(&family, &cgroupstats_ops);
+       if (rc < 0)
+               goto err_cgroup_ops;
+
        family_registered = 1;
+       printk("registered taskstats version %d\n", TASKSTATS_GENL_VERSION);
        return 0;
+err_cgroup_ops:
+       genl_unregister_ops(&family, &taskstats_ops);
 err:
        genl_unregister_family(&family);
        return rc;