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 unsigned long long active_power;
42 unsigned long long power;
43 struct hlist_node hash;
46 static struct uid_entry *find_uid_entry(uid_t uid)
48 struct uid_entry *uid_entry;
49 hash_for_each_possible(hash_table, uid_entry, hash, uid) {
50 if (uid_entry->uid == uid)
56 static struct uid_entry *find_or_register_uid(uid_t uid)
58 struct uid_entry *uid_entry;
60 uid_entry = find_uid_entry(uid);
64 uid_entry = kzalloc(sizeof(struct uid_entry), GFP_ATOMIC);
70 hash_add(hash_table, &uid_entry->hash, uid);
75 static int uid_stat_show(struct seq_file *m, void *v)
77 struct uid_entry *uid_entry;
78 struct task_struct *task;
83 mutex_lock(&uid_lock);
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;
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)));
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(),
103 /* if this task is exiting, we have already accounted for the
106 if (task->cpu_power == ULLONG_MAX)
108 task_cputime_adjusted(task, &utime, &stime);
109 uid_entry->active_utime += utime;
110 uid_entry->active_stime += stime;
111 uid_entry->active_power += task->cpu_power;
113 read_unlock(&tasklist_lock);
115 hash_for_each(hash_table, bkt, uid_entry, hash) {
116 cputime_t total_utime = uid_entry->utime +
117 uid_entry->active_utime;
118 cputime_t total_stime = uid_entry->stime +
119 uid_entry->active_stime;
120 unsigned long long total_power = uid_entry->power +
121 uid_entry->active_power;
122 seq_printf(m, "%d: %llu %llu %llu\n", uid_entry->uid,
123 (unsigned long long)jiffies_to_msecs(
124 cputime_to_jiffies(total_utime)) * USEC_PER_MSEC,
125 (unsigned long long)jiffies_to_msecs(
126 cputime_to_jiffies(total_stime)) * USEC_PER_MSEC,
130 mutex_unlock(&uid_lock);
134 static int uid_stat_open(struct inode *inode, struct file *file)
136 return single_open(file, uid_stat_show, PDE_DATA(inode));
139 static const struct file_operations uid_stat_fops = {
140 .open = uid_stat_open,
143 .release = single_release,
146 static int uid_remove_open(struct inode *inode, struct file *file)
148 return single_open(file, NULL, NULL);
151 static ssize_t uid_remove_write(struct file *file,
152 const char __user *buffer, size_t count, loff_t *ppos)
154 struct uid_entry *uid_entry;
155 struct hlist_node *tmp;
157 char *start_uid, *end_uid = NULL;
158 long int uid_start = 0, uid_end = 0;
160 if (count >= sizeof(uids))
161 count = sizeof(uids) - 1;
163 if (copy_from_user(uids, buffer, count))
168 start_uid = strsep(&end_uid, "-");
170 if (!start_uid || !end_uid)
173 if (kstrtol(start_uid, 10, &uid_start) != 0 ||
174 kstrtol(end_uid, 10, &uid_end) != 0) {
178 mutex_lock(&uid_lock);
180 for (; uid_start <= uid_end; uid_start++) {
181 hash_for_each_possible_safe(hash_table, uid_entry, tmp,
183 hash_del(&uid_entry->hash);
188 mutex_unlock(&uid_lock);
192 static const struct file_operations uid_remove_fops = {
193 .open = uid_remove_open,
194 .release = single_release,
195 .write = uid_remove_write,
198 static int process_notifier(struct notifier_block *self,
199 unsigned long cmd, void *v)
201 struct task_struct *task = v;
202 struct uid_entry *uid_entry;
203 cputime_t utime, stime;
209 mutex_lock(&uid_lock);
210 uid = from_kuid_munged(current_user_ns(), task_uid(task));
211 uid_entry = find_or_register_uid(uid);
213 pr_err("%s: failed to find uid %d\n", __func__, uid);
217 task_cputime_adjusted(task, &utime, &stime);
218 uid_entry->utime += utime;
219 uid_entry->stime += stime;
220 uid_entry->power += task->cpu_power;
221 task->cpu_power = ULLONG_MAX;
224 mutex_unlock(&uid_lock);
228 static struct notifier_block process_notifier_block = {
229 .notifier_call = process_notifier,
232 static int __init proc_uid_cputime_init(void)
234 hash_init(hash_table);
236 parent = proc_mkdir("uid_cputime", NULL);
238 pr_err("%s: failed to create proc entry\n", __func__);
242 proc_create_data("remove_uid_range", S_IWUGO, parent, &uid_remove_fops,
245 proc_create_data("show_uid_stat", S_IRUGO, parent, &uid_stat_fops,
248 profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block);
253 early_initcall(proc_uid_cputime_init);