1 /* drivers/misc/uid_cputime.c
3 * Copyright (C) 2014 - 2015 Google, Inc.
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.
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.
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>
29 #define UID_HASH_BITS 10
30 DECLARE_HASHTABLE(hash_table, UID_HASH_BITS);
32 static DEFINE_MUTEX(uid_lock);
33 static struct proc_dir_entry *parent;
39 cputime_t active_utime;
40 cputime_t active_stime;
41 struct hlist_node hash;
44 static struct uid_entry *find_uid_entry(uid_t uid)
46 struct uid_entry *uid_entry;
47 hash_for_each_possible(hash_table, uid_entry, hash, uid) {
48 if (uid_entry->uid == uid)
54 static struct uid_entry *find_or_register_uid(uid_t uid)
56 struct uid_entry *uid_entry;
58 uid_entry = find_uid_entry(uid);
62 uid_entry = kzalloc(sizeof(struct uid_entry), GFP_ATOMIC);
68 hash_add(hash_table, &uid_entry->hash, uid);
73 static int uid_stat_show(struct seq_file *m, void *v)
75 struct uid_entry *uid_entry;
76 struct task_struct *task;
81 mutex_lock(&uid_lock);
83 hash_for_each(hash_table, bkt, uid_entry, hash) {
84 uid_entry->active_stime = 0;
85 uid_entry->active_utime = 0;
88 read_lock(&tasklist_lock);
89 for_each_process(task) {
90 uid_entry = find_or_register_uid(task_uid(task));
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));
98 task_cputime_adjusted(task, &utime, &stime);
99 uid_entry->active_utime += utime;
100 uid_entry->active_stime += stime;
102 read_unlock(&tasklist_lock);
104 hash_for_each(hash_table, bkt, uid_entry, hash) {
105 cputime_t total_utime = uid_entry->utime +
106 uid_entry->active_utime;
107 cputime_t total_stime = uid_entry->stime +
108 uid_entry->active_stime;
109 seq_printf(m, "%d: %u %u\n", uid_entry->uid,
110 cputime_to_usecs(total_utime),
111 cputime_to_usecs(total_stime));
114 mutex_unlock(&uid_lock);
118 static int uid_stat_open(struct inode *inode, struct file *file)
120 return single_open(file, uid_stat_show, PDE_DATA(inode));
123 static const struct file_operations uid_stat_fops = {
124 .open = uid_stat_open,
127 .release = single_release,
130 static int uid_remove_open(struct inode *inode, struct file *file)
132 return single_open(file, NULL, NULL);
135 static ssize_t uid_remove_write(struct file *file,
136 const char __user *buffer, size_t count, loff_t *ppos)
138 struct uid_entry *uid_entry;
139 struct hlist_node *tmp;
141 char *start_uid, *end_uid = NULL;
142 long int uid_start = 0, uid_end = 0;
144 if (count >= sizeof(uids))
145 count = sizeof(uids) - 1;
147 if (copy_from_user(uids, buffer, count))
152 start_uid = strsep(&end_uid, "-");
154 if (!start_uid || !end_uid)
157 if (kstrtol(start_uid, 10, &uid_start) != 0 ||
158 kstrtol(end_uid, 10, &uid_end) != 0) {
162 mutex_lock(&uid_lock);
164 for (; uid_start <= uid_end; uid_start++) {
165 hash_for_each_possible_safe(hash_table, uid_entry, tmp,
167 hash_del(&uid_entry->hash);
172 mutex_unlock(&uid_lock);
176 static const struct file_operations uid_remove_fops = {
177 .open = uid_remove_open,
178 .release = single_release,
179 .write = uid_remove_write,
182 static int process_notifier(struct notifier_block *self,
183 unsigned long cmd, void *v)
185 struct task_struct *task = v;
186 struct uid_entry *uid_entry;
187 cputime_t utime, stime;
193 mutex_lock(&uid_lock);
194 uid = task_uid(task);
195 uid_entry = find_or_register_uid(uid);
197 pr_err("%s: failed to find uid %d\n", __func__, uid);
201 task_cputime_adjusted(task, &utime, &stime);
202 uid_entry->utime += utime;
203 uid_entry->stime += stime;
206 mutex_unlock(&uid_lock);
210 static struct notifier_block process_notifier_block = {
211 .notifier_call = process_notifier,
214 static int __init proc_uid_cputime_init(void)
216 hash_init(hash_table);
218 parent = proc_mkdir("uid_cputime", NULL);
220 pr_err("%s: failed to create proc entry\n", __func__);
224 proc_create_data("remove_uid_range", S_IWUGO, parent, &uid_remove_fops,
227 proc_create_data("show_uid_stat", S_IWUGO, parent, &uid_stat_fops,
230 profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block);
235 early_initcall(proc_uid_cputime_init);