1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/* drivers/misc/uid_cputime.c
*
* Copyright (C) 2014 - 2015 Google, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/atomic.h>
#include <linux/err.h>
#include <linux/hashtable.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/proc_fs.h>
#include <linux/profile.h>
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#define UID_HASH_BITS 10
DECLARE_HASHTABLE(hash_table, UID_HASH_BITS);
static DEFINE_MUTEX(uid_lock);
static struct proc_dir_entry *parent;
struct uid_entry {
uid_t uid;
cputime_t utime;
cputime_t stime;
cputime_t active_utime;
cputime_t active_stime;
unsigned long long active_power;
unsigned long long power;
struct hlist_node hash;
};
static struct uid_entry *find_uid_entry(uid_t uid)
{
struct uid_entry *uid_entry;
struct hlist_node *node;
hash_for_each_possible(hash_table, uid_entry, node, hash, uid) {
if (uid_entry->uid == uid)
return uid_entry;
}
return NULL;
}
static struct uid_entry *find_or_register_uid(uid_t uid)
{
struct uid_entry *uid_entry;
uid_entry = find_uid_entry(uid);
if (uid_entry)
return uid_entry;
uid_entry = kzalloc(sizeof(struct uid_entry), GFP_ATOMIC);
if (!uid_entry)
return NULL;
uid_entry->uid = uid;
hash_add(hash_table, &uid_entry->hash, uid);
return uid_entry;
}
static int uid_stat_show(struct seq_file *m, void *v)
{
struct uid_entry *uid_entry;
struct task_struct *task, *temp;
struct hlist_node *node;
cputime_t utime;
cputime_t stime;
unsigned long bkt;
mutex_lock(&uid_lock);
hash_for_each(hash_table, bkt, node, uid_entry, hash) {
uid_entry->active_stime = 0;
uid_entry->active_utime = 0;
uid_entry->active_power = 0;
}
read_lock(&tasklist_lock);
do_each_thread(temp, task) {
uid_entry = find_or_register_uid(task_uid(task));
if (!uid_entry) {
read_unlock(&tasklist_lock);
mutex_unlock(&uid_lock);
pr_err("%s: failed to find the uid_entry for uid %d\n",
__func__, task_uid(task));
return -ENOMEM;
}
/* if this task is exiting, we have already accounted for the
* time and power. */
#if 0 /* 3.0 fix */
if (task->cpu_power == ULLONG_MAX)
continue;
#endif
task_times(task, &utime, &stime);
uid_entry->active_utime += utime;
uid_entry->active_stime += stime;
#if 0 /* 3.0 fix */
uid_entry->active_power += task->cpu_power;
#endif
} while_each_thread(temp, task);
read_unlock(&tasklist_lock);
hash_for_each(hash_table, bkt, node, uid_entry, hash) {
cputime_t total_utime = uid_entry->utime +
uid_entry->active_utime;
cputime_t total_stime = uid_entry->stime +
uid_entry->active_stime;
unsigned long long total_power = uid_entry->power +
uid_entry->active_power;
seq_printf(m, "%d: %llu %llu %llu\n", uid_entry->uid,
(unsigned long long)jiffies_to_msecs(
cputime_to_jiffies(total_utime)) * USEC_PER_MSEC,
(unsigned long long)jiffies_to_msecs(
cputime_to_jiffies(total_stime)) * USEC_PER_MSEC,
total_power);
}
mutex_unlock(&uid_lock);
return 0;
}
static int uid_stat_open(struct inode *inode, struct file *file)
{
return single_open(file, uid_stat_show, PDE(inode)->data);
}
static const struct file_operations uid_stat_fops = {
.open = uid_stat_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int uid_remove_open(struct inode *inode, struct file *file)
{
return single_open(file, NULL, NULL);
}
static ssize_t uid_remove_write(struct file *file,
const char __user *buffer, size_t count, loff_t *ppos)
{
struct uid_entry *uid_entry;
struct hlist_node *node, *tmp;
char uids[128];
char *start_uid, *end_uid = NULL;
long int uid_start = 0, uid_end = 0;
if (count >= sizeof(uids))
count = sizeof(uids) - 1;
if (copy_from_user(uids, buffer, count))
return -EFAULT;
uids[count] = '\0';
end_uid = uids;
start_uid = strsep(&end_uid, "-");
if (!start_uid || !end_uid)
return -EINVAL;
if (kstrtol(start_uid, 10, &uid_start) != 0 ||
kstrtol(end_uid, 10, &uid_end) != 0) {
return -EINVAL;
}
mutex_lock(&uid_lock);
for (; uid_start <= uid_end; uid_start++) {
hash_for_each_possible_safe(hash_table, uid_entry, node, tmp,
hash, uid_start) {
hash_del(&uid_entry->hash);
kfree(uid_entry);
}
}
mutex_unlock(&uid_lock);
return count;
}
static const struct file_operations uid_remove_fops = {
.open = uid_remove_open,
.release = single_release,
.write = uid_remove_write,
};
static int process_notifier(struct notifier_block *self,
unsigned long cmd, void *v)
{
struct task_struct *task = v;
struct uid_entry *uid_entry;
cputime_t utime, stime;
uid_t uid;
if (!task)
return NOTIFY_OK;
mutex_lock(&uid_lock);
uid = task_uid(task);
uid_entry = find_or_register_uid(uid);
if (!uid_entry) {
pr_err("%s: failed to find uid %d\n", __func__, uid);
goto exit;
}
task_times(task, &utime, &stime);
uid_entry->utime += utime;
uid_entry->stime += stime;
#if 0 /* 3.0 fix */
uid_entry->power += task->cpu_power;
task->cpu_power = ULLONG_MAX;
#endif
exit:
mutex_unlock(&uid_lock);
return NOTIFY_OK;
}
static struct notifier_block process_notifier_block = {
.notifier_call = process_notifier,
};
static int __init proc_uid_cputime_init(void)
{
hash_init(hash_table);
parent = proc_mkdir("uid_cputime", NULL);
if (!parent) {
pr_err("%s: failed to create proc entry\n", __func__);
return -ENOMEM;
}
proc_create_data("remove_uid_range", S_IWUGO, parent, &uid_remove_fops,
NULL);
proc_create_data("show_uid_stat", S_IRUGO, parent, &uid_stat_fops,
NULL);
profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block);
return 0;
}
early_initcall(proc_uid_cputime_init);
|