misc: uidstat: avoid create_stat() race and blockage.
[firefly-linux-kernel-4.4.55.git] / drivers / misc / uid_stat.c
1 /* drivers/misc/uid_stat.c
2  *
3  * Copyright (C) 2008 - 2009 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <asm/atomic.h>
17
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/proc_fs.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/stat.h>
26 #include <linux/uid_stat.h>
27 #include <net/activity_stats.h>
28
29 static DEFINE_SPINLOCK(uid_lock);
30 static LIST_HEAD(uid_list);
31 static struct proc_dir_entry *parent;
32
33 struct uid_stat {
34         struct list_head link;
35         uid_t uid;
36         atomic_t tcp_rcv;
37         atomic_t tcp_snd;
38 };
39
40 static struct uid_stat *find_uid_stat(uid_t uid) {
41         struct uid_stat *entry;
42
43         list_for_each_entry(entry, &uid_list, link) {
44                 if (entry->uid == uid) {
45                         return entry;
46                 }
47         }
48         return NULL;
49 }
50
51 static int tcp_snd_read_proc(char *page, char **start, off_t off,
52                                 int count, int *eof, void *data)
53 {
54         int len;
55         unsigned int bytes;
56         char *p = page;
57         struct uid_stat *uid_entry = (struct uid_stat *) data;
58         if (!data)
59                 return 0;
60
61         bytes = (unsigned int) (atomic_read(&uid_entry->tcp_snd) + INT_MIN);
62         p += sprintf(p, "%u\n", bytes);
63         len = (p - page) - off;
64         *eof = (len <= count) ? 1 : 0;
65         *start = page + off;
66         return len;
67 }
68
69 static int tcp_rcv_read_proc(char *page, char **start, off_t off,
70                                 int count, int *eof, void *data)
71 {
72         int len;
73         unsigned int bytes;
74         char *p = page;
75         struct uid_stat *uid_entry = (struct uid_stat *) data;
76         if (!data)
77                 return 0;
78
79         bytes = (unsigned int) (atomic_read(&uid_entry->tcp_rcv) + INT_MIN);
80         p += sprintf(p, "%u\n", bytes);
81         len = (p - page) - off;
82         *eof = (len <= count) ? 1 : 0;
83         *start = page + off;
84         return len;
85 }
86
87 /* Create a new entry for tracking the specified uid. */
88 static struct uid_stat *create_stat(uid_t uid) {
89         struct uid_stat *new_uid;
90         /* Create the uid stat struct and append it to the list. */
91         new_uid = kmalloc(sizeof(struct uid_stat), GFP_ATOMIC);
92         if (!new_uid)
93                 return NULL;
94
95         new_uid->uid = uid;
96         /* Counters start at INT_MIN, so we can track 4GB of network traffic. */
97         atomic_set(&new_uid->tcp_rcv, INT_MIN);
98         atomic_set(&new_uid->tcp_snd, INT_MIN);
99
100         list_add_tail(&new_uid->link, &uid_list);
101         return new_uid;
102 }
103
104 static void create_stat_proc(struct uid_stat *new_uid)
105 {
106         char uid_s[32];
107         struct proc_dir_entry *entry;
108         sprintf(uid_s, "%d", new_uid->uid);
109         entry = proc_mkdir(uid_s, parent);
110
111         /* Keep reference to uid_stat so we know what uid to read stats from. */
112         create_proc_read_entry("tcp_snd", S_IRUGO, entry , tcp_snd_read_proc,
113                 (void *) new_uid);
114
115         create_proc_read_entry("tcp_rcv", S_IRUGO, entry, tcp_rcv_read_proc,
116                 (void *) new_uid);
117 }
118
119 static struct uid_stat *find_or_create_uid_stat(uid_t uid)
120 {
121         struct uid_stat *entry;
122         unsigned long flags;
123         spin_lock_irqsave(&uid_lock, flags);
124         entry = find_uid_stat(uid);
125         if (entry) {
126                 spin_unlock_irqrestore(&uid_lock, flags);
127                 return entry;
128         }
129         entry = create_stat(uid);
130         spin_unlock_irqrestore(&uid_lock, flags);
131         if (entry)
132                 create_stat_proc(entry);
133         return entry;
134 }
135
136 int uid_stat_tcp_snd(uid_t uid, int size) {
137         struct uid_stat *entry;
138         activity_stats_update();
139         entry = find_or_create_uid_stat(uid);
140         if (!entry)
141                 return -1;
142         atomic_add(size, &entry->tcp_snd);
143         return 0;
144 }
145
146 int uid_stat_tcp_rcv(uid_t uid, int size) {
147         struct uid_stat *entry;
148         activity_stats_update();
149         entry = find_or_create_uid_stat(uid);
150         if (!entry)
151                 return -1;
152         atomic_add(size, &entry->tcp_rcv);
153         return 0;
154 }
155
156 static int __init uid_stat_init(void)
157 {
158         parent = proc_mkdir("uid_stat", NULL);
159         if (!parent) {
160                 pr_err("uid_stat: failed to create proc entry\n");
161                 return -1;
162         }
163         return 0;
164 }
165
166 __initcall(uid_stat_init);