blob: 50f10290b3fdf905d4584ba78452469cf64a5e0d [file] [log] [blame]
Jin Qiana1ad2292015-09-07 14:55:10 +05301/* 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;
Srinivasarao Pe168fec2015-09-24 13:08:20 +053047 struct hlist_node *node;
48 hash_for_each_possible(hash_table, uid_entry, node, hash, uid) {
Jin Qiana1ad2292015-09-07 14:55:10 +053049 if (uid_entry->uid == uid)
50 return uid_entry;
51 }
52 return NULL;
53}
54
55static struct uid_entry *find_or_register_uid(uid_t uid)
56{
57 struct uid_entry *uid_entry;
58
59 uid_entry = find_uid_entry(uid);
60 if (uid_entry)
61 return uid_entry;
62
63 uid_entry = kzalloc(sizeof(struct uid_entry), GFP_ATOMIC);
64 if (!uid_entry)
65 return NULL;
66
67 uid_entry->uid = uid;
68
69 hash_add(hash_table, &uid_entry->hash, uid);
70
71 return uid_entry;
72}
73
74static int uid_stat_show(struct seq_file *m, void *v)
75{
76 struct uid_entry *uid_entry;
77 struct task_struct *task;
Jin Qiana1ad2292015-09-07 14:55:10 +053078 unsigned long bkt;
Srinivasarao Pe168fec2015-09-24 13:08:20 +053079 struct hlist_node *node;
Jin Qiana1ad2292015-09-07 14:55:10 +053080
81 mutex_lock(&uid_lock);
82
Srinivasarao Pe168fec2015-09-24 13:08:20 +053083 hash_for_each(hash_table, bkt, node, uid_entry, hash) {
Jin Qiana1ad2292015-09-07 14:55:10 +053084 uid_entry->active_stime = 0;
85 uid_entry->active_utime = 0;
86 }
87
88 read_lock(&tasklist_lock);
89 for_each_process(task) {
90 uid_entry = find_or_register_uid(task_uid(task));
91 if (!uid_entry) {
92 read_unlock(&tasklist_lock);
93 mutex_unlock(&uid_lock);
94 pr_err("%s: failed to find the uid_entry for uid %d\n",
95 __func__, task_uid(task));
96 return -ENOMEM;
97 }
Srinivasarao Pe168fec2015-09-24 13:08:20 +053098 uid_entry->active_utime += task->utime;
99 uid_entry->active_stime += task->stime;
Jin Qiana1ad2292015-09-07 14:55:10 +0530100 }
101 read_unlock(&tasklist_lock);
102
Srinivasarao Pe168fec2015-09-24 13:08:20 +0530103 hash_for_each(hash_table, bkt, node, uid_entry, hash) {
Jin Qiana1ad2292015-09-07 14:55:10 +0530104 cputime_t total_utime = uid_entry->utime +
105 uid_entry->active_utime;
106 cputime_t total_stime = uid_entry->stime +
107 uid_entry->active_stime;
108 seq_printf(m, "%d: %u %u\n", uid_entry->uid,
109 cputime_to_usecs(total_utime),
110 cputime_to_usecs(total_stime));
111 }
112
113 mutex_unlock(&uid_lock);
114 return 0;
115}
116
117static int uid_stat_open(struct inode *inode, struct file *file)
118{
Srinivasarao Pe168fec2015-09-24 13:08:20 +0530119 return single_open(file, uid_stat_show, PDE(inode)->data);
Jin Qiana1ad2292015-09-07 14:55:10 +0530120}
121
122static const struct file_operations uid_stat_fops = {
123 .open = uid_stat_open,
124 .read = seq_read,
125 .llseek = seq_lseek,
126 .release = single_release,
127};
128
129static int uid_remove_open(struct inode *inode, struct file *file)
130{
131 return single_open(file, NULL, NULL);
132}
133
134static ssize_t uid_remove_write(struct file *file,
135 const char __user *buffer, size_t count, loff_t *ppos)
136{
137 struct uid_entry *uid_entry;
138 struct hlist_node *tmp;
139 char uids[128];
140 char *start_uid, *end_uid = NULL;
141 long int uid_start = 0, uid_end = 0;
Srinivasarao Pe168fec2015-09-24 13:08:20 +0530142 struct hlist_node *node;
Jin Qiana1ad2292015-09-07 14:55:10 +0530143
144 if (count >= sizeof(uids))
145 count = sizeof(uids) - 1;
146
147 if (copy_from_user(uids, buffer, count))
148 return -EFAULT;
149
150 uids[count] = '\0';
151 end_uid = uids;
152 start_uid = strsep(&end_uid, "-");
153
154 if (!start_uid || !end_uid)
155 return -EINVAL;
156
157 if (kstrtol(start_uid, 10, &uid_start) != 0 ||
158 kstrtol(end_uid, 10, &uid_end) != 0) {
159 return -EINVAL;
160 }
161
162 mutex_lock(&uid_lock);
163
164 for (; uid_start <= uid_end; uid_start++) {
Srinivasarao Pe168fec2015-09-24 13:08:20 +0530165 hash_for_each_possible_safe(hash_table, uid_entry, node,
166 tmp, hash, uid_start) {
Jin Qiana1ad2292015-09-07 14:55:10 +0530167 hash_del(&uid_entry->hash);
168 kfree(uid_entry);
169 }
170 }
171
172 mutex_unlock(&uid_lock);
173 return count;
174}
175
176static const struct file_operations uid_remove_fops = {
177 .open = uid_remove_open,
178 .release = single_release,
179 .write = uid_remove_write,
180};
181
182static int process_notifier(struct notifier_block *self,
183 unsigned long cmd, void *v)
184{
185 struct task_struct *task = v;
186 struct uid_entry *uid_entry;
Jin Qiana1ad2292015-09-07 14:55:10 +0530187 uid_t uid;
188
189 if (!task)
190 return NOTIFY_OK;
191
192 mutex_lock(&uid_lock);
193 uid = task_uid(task);
194 uid_entry = find_or_register_uid(uid);
195 if (!uid_entry) {
196 pr_err("%s: failed to find uid %d\n", __func__, uid);
197 goto exit;
198 }
199
Srinivasarao Pe168fec2015-09-24 13:08:20 +0530200 uid_entry->utime += task->utime;
201 uid_entry->stime += task->stime;
Jin Qiana1ad2292015-09-07 14:55:10 +0530202
203exit:
204 mutex_unlock(&uid_lock);
205 return NOTIFY_OK;
206}
207
208static struct notifier_block process_notifier_block = {
209 .notifier_call = process_notifier,
210};
211
212static int __init proc_uid_cputime_init(void)
213{
214 hash_init(hash_table);
215
216 parent = proc_mkdir("uid_cputime", NULL);
217 if (!parent) {
218 pr_err("%s: failed to create proc entry\n", __func__);
219 return -ENOMEM;
220 }
221
222 proc_create_data("remove_uid_range", S_IWUGO, parent, &uid_remove_fops,
223 NULL);
224
Jin Qian78a59752015-05-11 17:57:52 -0700225 proc_create_data("show_uid_stat", S_IRUGO, parent, &uid_stat_fops,
Jin Qiana1ad2292015-09-07 14:55:10 +0530226 NULL);
227
228 profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block);
229
230 return 0;
231}
232
233early_initcall(proc_uid_cputime_init);