89bfba6c5b6aa91bb4ed63560f65fce3f4134bfc
[firefly-linux-kernel-4.4.55.git] / drivers / misc / uid_cputime.c
1 /* drivers/misc/uid_cputime.c
2  *
3  * Copyright (C) 2014 - 2015 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 <linux/atomic.h>
17 #include <linux/err.h>
18 #include <linux/hashtable.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/profile.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
28
29 #define UID_HASH_BITS   10
30 DECLARE_HASHTABLE(hash_table, UID_HASH_BITS);
31
32 static DEFINE_MUTEX(uid_lock);
33 static struct proc_dir_entry *parent;
34
35 struct uid_entry {
36         uid_t uid;
37         cputime_t utime;
38         cputime_t stime;
39         cputime_t active_utime;
40         cputime_t active_stime;
41         unsigned long long active_power;
42         unsigned long long power;
43         struct hlist_node hash;
44 };
45
46 static struct uid_entry *find_uid_entry(uid_t uid)
47 {
48         struct uid_entry *uid_entry;
49         hash_for_each_possible(hash_table, uid_entry, hash, uid) {
50                 if (uid_entry->uid == uid)
51                         return uid_entry;
52         }
53         return NULL;
54 }
55
56 static struct uid_entry *find_or_register_uid(uid_t uid)
57 {
58         struct uid_entry *uid_entry;
59
60         uid_entry = find_uid_entry(uid);
61         if (uid_entry)
62                 return uid_entry;
63
64         uid_entry = kzalloc(sizeof(struct uid_entry), GFP_ATOMIC);
65         if (!uid_entry)
66                 return NULL;
67
68         uid_entry->uid = uid;
69
70         hash_add(hash_table, &uid_entry->hash, uid);
71
72         return uid_entry;
73 }
74
75 static int uid_stat_show(struct seq_file *m, void *v)
76 {
77         struct uid_entry *uid_entry;
78         struct task_struct *task;
79         cputime_t utime;
80         cputime_t stime;
81         unsigned long bkt;
82
83         mutex_lock(&uid_lock);
84
85         hash_for_each(hash_table, bkt, uid_entry, hash) {
86                 uid_entry->active_stime = 0;
87                 uid_entry->active_utime = 0;
88                 uid_entry->active_power = 0;
89         }
90
91         read_lock(&tasklist_lock);
92         for_each_process(task) {
93                 uid_entry = find_or_register_uid(from_kuid_munged(
94                         current_user_ns(), task_uid(task)));
95                 if (!uid_entry) {
96                         read_unlock(&tasklist_lock);
97                         mutex_unlock(&uid_lock);
98                         pr_err("%s: failed to find the uid_entry for uid %d\n",
99                                 __func__, from_kuid_munged(current_user_ns(),
100                                 task_uid(task)));
101                         return -ENOMEM;
102                 }
103                 task_cputime_adjusted(task, &utime, &stime);
104                 uid_entry->active_utime += utime;
105                 uid_entry->active_stime += stime;
106                 uid_entry->active_power += task->cpu_power;
107         }
108         read_unlock(&tasklist_lock);
109
110         hash_for_each(hash_table, bkt, uid_entry, hash) {
111                 cputime_t total_utime = uid_entry->utime +
112                                                         uid_entry->active_utime;
113                 cputime_t total_stime = uid_entry->stime +
114                                                         uid_entry->active_stime;
115                 unsigned long long total_power = uid_entry->power +
116                                                         uid_entry->active_power;
117                 seq_printf(m, "%d: %u %u %llu\n", uid_entry->uid,
118                                                 cputime_to_usecs(total_utime),
119                                                 cputime_to_usecs(total_stime),
120                                                 total_power);
121         }
122
123         mutex_unlock(&uid_lock);
124         return 0;
125 }
126
127 static int uid_stat_open(struct inode *inode, struct file *file)
128 {
129         return single_open(file, uid_stat_show, PDE_DATA(inode));
130 }
131
132 static const struct file_operations uid_stat_fops = {
133         .open           = uid_stat_open,
134         .read           = seq_read,
135         .llseek         = seq_lseek,
136         .release        = single_release,
137 };
138
139 static int uid_remove_open(struct inode *inode, struct file *file)
140 {
141         return single_open(file, NULL, NULL);
142 }
143
144 static ssize_t uid_remove_write(struct file *file,
145                         const char __user *buffer, size_t count, loff_t *ppos)
146 {
147         struct uid_entry *uid_entry;
148         struct hlist_node *tmp;
149         char uids[128];
150         char *start_uid, *end_uid = NULL;
151         long int uid_start = 0, uid_end = 0;
152
153         if (count >= sizeof(uids))
154                 count = sizeof(uids) - 1;
155
156         if (copy_from_user(uids, buffer, count))
157                 return -EFAULT;
158
159         uids[count] = '\0';
160         end_uid = uids;
161         start_uid = strsep(&end_uid, "-");
162
163         if (!start_uid || !end_uid)
164                 return -EINVAL;
165
166         if (kstrtol(start_uid, 10, &uid_start) != 0 ||
167                 kstrtol(end_uid, 10, &uid_end) != 0) {
168                 return -EINVAL;
169         }
170
171         mutex_lock(&uid_lock);
172
173         for (; uid_start <= uid_end; uid_start++) {
174                 hash_for_each_possible_safe(hash_table, uid_entry, tmp,
175                                                         hash, uid_start) {
176                         hash_del(&uid_entry->hash);
177                         kfree(uid_entry);
178                 }
179         }
180
181         mutex_unlock(&uid_lock);
182         return count;
183 }
184
185 static const struct file_operations uid_remove_fops = {
186         .open           = uid_remove_open,
187         .release        = single_release,
188         .write          = uid_remove_write,
189 };
190
191 static int process_notifier(struct notifier_block *self,
192                         unsigned long cmd, void *v)
193 {
194         struct task_struct *task = v;
195         struct uid_entry *uid_entry;
196         cputime_t utime, stime;
197         uid_t uid;
198
199         if (!task)
200                 return NOTIFY_OK;
201
202         mutex_lock(&uid_lock);
203         uid = from_kuid_munged(current_user_ns(), task_uid(task));
204         uid_entry = find_or_register_uid(uid);
205         if (!uid_entry) {
206                 pr_err("%s: failed to find uid %d\n", __func__, uid);
207                 goto exit;
208         }
209
210         task_cputime_adjusted(task, &utime, &stime);
211         uid_entry->utime += utime;
212         uid_entry->stime += stime;
213         uid_entry->power += task->cpu_power;
214
215 exit:
216         mutex_unlock(&uid_lock);
217         return NOTIFY_OK;
218 }
219
220 static struct notifier_block process_notifier_block = {
221         .notifier_call  = process_notifier,
222 };
223
224 static int __init proc_uid_cputime_init(void)
225 {
226         hash_init(hash_table);
227
228         parent = proc_mkdir("uid_cputime", NULL);
229         if (!parent) {
230                 pr_err("%s: failed to create proc entry\n", __func__);
231                 return -ENOMEM;
232         }
233
234         proc_create_data("remove_uid_range", S_IWUGO, parent, &uid_remove_fops,
235                                         NULL);
236
237         proc_create_data("show_uid_stat", S_IRUGO, parent, &uid_stat_fops,
238                                         NULL);
239
240         profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block);
241
242         return 0;
243 }
244
245 early_initcall(proc_uid_cputime_init);