blob: c1ad5246f564e5c1a0b210820492b785f7b76d4e [file] [log] [blame]
jinqian69014222015-03-11 10:44:50 -07001/* 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
30DECLARE_HASHTABLE(hash_table, UID_HASH_BITS);
31
32static DEFINE_MUTEX(uid_lock);
33static struct proc_dir_entry *parent;
34
35struct 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 struct hlist_node hash;
42};
43
44static struct uid_entry *find_uid_entry(uid_t uid)
45{
46 struct uid_entry *uid_entry;
47 hash_for_each_possible(hash_table, uid_entry, hash, uid) {
48 if (uid_entry->uid == uid)
49 return uid_entry;
50 }
51 return NULL;
52}
53
54static struct uid_entry *find_or_register_uid(uid_t uid)
55{
56 struct uid_entry *uid_entry;
57
58 uid_entry = find_uid_entry(uid);
59 if (uid_entry)
60 return uid_entry;
61
62 uid_entry = kzalloc(sizeof(struct uid_entry), GFP_ATOMIC);
63 if (!uid_entry)
64 return NULL;
65
66 uid_entry->uid = uid;
67
68 hash_add(hash_table, &uid_entry->hash, uid);
69
70 return uid_entry;
71}
72
73static int uid_stat_show(struct seq_file *m, void *v)
74{
75 struct uid_entry *uid_entry;
Ruchi Kandoi0a733772015-07-31 10:17:54 -070076 struct task_struct *task, *temp;
jinqian69014222015-03-11 10:44:50 -070077 cputime_t utime;
78 cputime_t stime;
79 unsigned long bkt;
80
81 mutex_lock(&uid_lock);
82
83 hash_for_each(hash_table, bkt, uid_entry, hash) {
84 uid_entry->active_stime = 0;
85 uid_entry->active_utime = 0;
86 }
87
88 read_lock(&tasklist_lock);
Ruchi Kandoi0a733772015-07-31 10:17:54 -070089 do_each_thread(temp, task) {
Amit Pundir48a99062015-04-15 00:40:21 +053090 uid_entry = find_or_register_uid(from_kuid_munged(
91 current_user_ns(), task_uid(task)));
jinqian69014222015-03-11 10:44:50 -070092 if (!uid_entry) {
93 read_unlock(&tasklist_lock);
94 mutex_unlock(&uid_lock);
95 pr_err("%s: failed to find the uid_entry for uid %d\n",
Amit Pundir48a99062015-04-15 00:40:21 +053096 __func__, from_kuid_munged(current_user_ns(),
97 task_uid(task)));
jinqian69014222015-03-11 10:44:50 -070098 return -ENOMEM;
99 }
100 task_cputime_adjusted(task, &utime, &stime);
101 uid_entry->active_utime += utime;
102 uid_entry->active_stime += stime;
Ruchi Kandoi0a733772015-07-31 10:17:54 -0700103 } while_each_thread(temp, task);
jinqian69014222015-03-11 10:44:50 -0700104 read_unlock(&tasklist_lock);
105
106 hash_for_each(hash_table, bkt, uid_entry, hash) {
107 cputime_t total_utime = uid_entry->utime +
108 uid_entry->active_utime;
109 cputime_t total_stime = uid_entry->stime +
110 uid_entry->active_stime;
Amit Pundire4395b22015-12-14 11:56:35 +0530111 seq_printf(m, "%d: %llu %llu\n", uid_entry->uid,
Jin Qianbe7074f2015-07-13 18:16:55 -0700112 (unsigned long long)jiffies_to_msecs(
113 cputime_to_jiffies(total_utime)) * USEC_PER_MSEC,
114 (unsigned long long)jiffies_to_msecs(
Amit Pundire4395b22015-12-14 11:56:35 +0530115 cputime_to_jiffies(total_stime)) * USEC_PER_MSEC);
jinqian69014222015-03-11 10:44:50 -0700116 }
117
118 mutex_unlock(&uid_lock);
119 return 0;
120}
121
122static int uid_stat_open(struct inode *inode, struct file *file)
123{
124 return single_open(file, uid_stat_show, PDE_DATA(inode));
125}
126
127static const struct file_operations uid_stat_fops = {
128 .open = uid_stat_open,
129 .read = seq_read,
130 .llseek = seq_lseek,
131 .release = single_release,
132};
133
134static int uid_remove_open(struct inode *inode, struct file *file)
135{
136 return single_open(file, NULL, NULL);
137}
138
139static ssize_t uid_remove_write(struct file *file,
140 const char __user *buffer, size_t count, loff_t *ppos)
141{
142 struct uid_entry *uid_entry;
143 struct hlist_node *tmp;
144 char uids[128];
145 char *start_uid, *end_uid = NULL;
146 long int uid_start = 0, uid_end = 0;
147
148 if (count >= sizeof(uids))
149 count = sizeof(uids) - 1;
150
151 if (copy_from_user(uids, buffer, count))
152 return -EFAULT;
153
154 uids[count] = '\0';
155 end_uid = uids;
156 start_uid = strsep(&end_uid, "-");
157
158 if (!start_uid || !end_uid)
159 return -EINVAL;
160
161 if (kstrtol(start_uid, 10, &uid_start) != 0 ||
162 kstrtol(end_uid, 10, &uid_end) != 0) {
163 return -EINVAL;
164 }
jinqian69014222015-03-11 10:44:50 -0700165 mutex_lock(&uid_lock);
166
167 for (; uid_start <= uid_end; uid_start++) {
168 hash_for_each_possible_safe(hash_table, uid_entry, tmp,
Ruchi Kandoi17f35ea2015-10-23 17:49:11 -0700169 hash, (uid_t)uid_start) {
170 if (uid_start == uid_entry->uid) {
171 hash_del(&uid_entry->hash);
172 kfree(uid_entry);
173 }
jinqian69014222015-03-11 10:44:50 -0700174 }
175 }
176
177 mutex_unlock(&uid_lock);
178 return count;
179}
180
181static const struct file_operations uid_remove_fops = {
182 .open = uid_remove_open,
183 .release = single_release,
184 .write = uid_remove_write,
185};
186
187static int process_notifier(struct notifier_block *self,
188 unsigned long cmd, void *v)
189{
190 struct task_struct *task = v;
191 struct uid_entry *uid_entry;
192 cputime_t utime, stime;
193 uid_t uid;
194
195 if (!task)
196 return NOTIFY_OK;
197
198 mutex_lock(&uid_lock);
Amit Pundir48a99062015-04-15 00:40:21 +0530199 uid = from_kuid_munged(current_user_ns(), task_uid(task));
jinqian69014222015-03-11 10:44:50 -0700200 uid_entry = find_or_register_uid(uid);
201 if (!uid_entry) {
202 pr_err("%s: failed to find uid %d\n", __func__, uid);
203 goto exit;
204 }
205
206 task_cputime_adjusted(task, &utime, &stime);
207 uid_entry->utime += utime;
208 uid_entry->stime += stime;
209
210exit:
211 mutex_unlock(&uid_lock);
212 return NOTIFY_OK;
213}
214
215static struct notifier_block process_notifier_block = {
216 .notifier_call = process_notifier,
217};
218
219static int __init proc_uid_cputime_init(void)
220{
221 hash_init(hash_table);
222
223 parent = proc_mkdir("uid_cputime", NULL);
224 if (!parent) {
225 pr_err("%s: failed to create proc entry\n", __func__);
226 return -ENOMEM;
227 }
228
229 proc_create_data("remove_uid_range", S_IWUGO, parent, &uid_remove_fops,
230 NULL);
231
Jin Qian453ac312015-05-11 17:57:52 -0700232 proc_create_data("show_uid_stat", S_IRUGO, parent, &uid_stat_fops,
jinqian69014222015-03-11 10:44:50 -0700233 NULL);
234
235 profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block);
236
237 return 0;
238}
239
240early_initcall(proc_uid_cputime_init);