blob: 7d69dd5c2b75b3b67d3e67bf44d45aa4ad8a1c36 [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>
Amit Pundir0ea205a2017-08-17 18:52:04 +053022#include <linux/mm.h>
jinqian69014222015-03-11 10:44:50 -070023#include <linux/proc_fs.h>
24#include <linux/profile.h>
Wei Wang7f1ad082017-03-13 12:22:21 -070025#include <linux/rtmutex.h>
jinqian69014222015-03-11 10:44:50 -070026#include <linux/sched.h>
27#include <linux/seq_file.h>
28#include <linux/slab.h>
29#include <linux/uaccess.h>
30
Wei Wang7f1ad082017-03-13 12:22:21 -070031
jinqian69014222015-03-11 10:44:50 -070032#define UID_HASH_BITS 10
33DECLARE_HASHTABLE(hash_table, UID_HASH_BITS);
34
Wei Wang7f1ad082017-03-13 12:22:21 -070035static DEFINE_RT_MUTEX(uid_lock);
Jin Qian012f2002017-01-10 16:10:35 -080036static struct proc_dir_entry *cpu_parent;
37static struct proc_dir_entry *io_parent;
38static struct proc_dir_entry *proc_parent;
39
40struct io_stats {
41 u64 read_bytes;
42 u64 write_bytes;
43 u64 rchar;
44 u64 wchar;
Jin Qian62eb9f92017-03-02 13:39:43 -080045 u64 fsync;
Jin Qian012f2002017-01-10 16:10:35 -080046};
47
48#define UID_STATE_FOREGROUND 0
49#define UID_STATE_BACKGROUND 1
50#define UID_STATE_BUCKET_SIZE 2
51
52#define UID_STATE_TOTAL_CURR 2
53#define UID_STATE_TOTAL_LAST 3
Jin Qiancd5e19c2017-05-22 12:08:06 -070054#define UID_STATE_DEAD_TASKS 4
55#define UID_STATE_SIZE 5
jinqian69014222015-03-11 10:44:50 -070056
Yang Jin6e8f1e72017-07-26 12:52:22 -070057#define MAX_TASK_COMM_LEN 256
58
59struct task_entry {
60 char comm[MAX_TASK_COMM_LEN];
61 pid_t pid;
62 struct io_stats io[UID_STATE_SIZE];
63 struct hlist_node hash;
64};
65
jinqian69014222015-03-11 10:44:50 -070066struct uid_entry {
67 uid_t uid;
68 cputime_t utime;
69 cputime_t stime;
70 cputime_t active_utime;
71 cputime_t active_stime;
Jin Qian012f2002017-01-10 16:10:35 -080072 int state;
73 struct io_stats io[UID_STATE_SIZE];
jinqian69014222015-03-11 10:44:50 -070074 struct hlist_node hash;
Yang Jin6e8f1e72017-07-26 12:52:22 -070075#ifdef CONFIG_UID_SYS_STATS_DEBUG
76 DECLARE_HASHTABLE(task_entries, UID_HASH_BITS);
77#endif
jinqian69014222015-03-11 10:44:50 -070078};
79
Yang Jin6e8f1e72017-07-26 12:52:22 -070080static u64 compute_write_bytes(struct task_struct *task)
81{
82 if (task->ioac.write_bytes <= task->ioac.cancelled_write_bytes)
83 return 0;
84
85 return task->ioac.write_bytes - task->ioac.cancelled_write_bytes;
86}
87
88static void compute_io_bucket_stats(struct io_stats *io_bucket,
89 struct io_stats *io_curr,
90 struct io_stats *io_last,
91 struct io_stats *io_dead)
92{
93 /* tasks could switch to another uid group, but its io_last in the
94 * previous uid group could still be positive.
95 * therefore before each update, do an overflow check first
96 */
97 int64_t delta;
98
99 delta = io_curr->read_bytes + io_dead->read_bytes -
100 io_last->read_bytes;
101 io_bucket->read_bytes += delta > 0 ? delta : 0;
102 delta = io_curr->write_bytes + io_dead->write_bytes -
103 io_last->write_bytes;
104 io_bucket->write_bytes += delta > 0 ? delta : 0;
105 delta = io_curr->rchar + io_dead->rchar - io_last->rchar;
106 io_bucket->rchar += delta > 0 ? delta : 0;
107 delta = io_curr->wchar + io_dead->wchar - io_last->wchar;
108 io_bucket->wchar += delta > 0 ? delta : 0;
109 delta = io_curr->fsync + io_dead->fsync - io_last->fsync;
110 io_bucket->fsync += delta > 0 ? delta : 0;
111
112 io_last->read_bytes = io_curr->read_bytes;
113 io_last->write_bytes = io_curr->write_bytes;
114 io_last->rchar = io_curr->rchar;
115 io_last->wchar = io_curr->wchar;
116 io_last->fsync = io_curr->fsync;
117
118 memset(io_dead, 0, sizeof(struct io_stats));
119}
120
121#ifdef CONFIG_UID_SYS_STATS_DEBUG
122static void get_full_task_comm(struct task_entry *task_entry,
123 struct task_struct *task)
124{
125 int i = 0, offset = 0, len = 0;
126 /* save one byte for terminating null character */
127 int unused_len = MAX_TASK_COMM_LEN - TASK_COMM_LEN - 1;
128 char buf[unused_len];
129 struct mm_struct *mm = task->mm;
130
131 /* fill the first TASK_COMM_LEN bytes with thread name */
132 get_task_comm(task_entry->comm, task);
133 i = strlen(task_entry->comm);
134 while (i < TASK_COMM_LEN)
135 task_entry->comm[i++] = ' ';
136
137 /* next the executable file name */
138 if (mm) {
139 down_read(&mm->mmap_sem);
140 if (mm->exe_file) {
141 char *pathname = d_path(&mm->exe_file->f_path, buf,
142 unused_len);
143
144 if (!IS_ERR(pathname)) {
145 len = strlcpy(task_entry->comm + i, pathname,
146 unused_len);
147 i += len;
148 task_entry->comm[i++] = ' ';
149 unused_len--;
150 }
151 }
152 up_read(&mm->mmap_sem);
153 }
154 unused_len -= len;
155
156 /* fill the rest with command line argument
157 * replace each null or new line character
158 * between args in argv with whitespace */
159 len = get_cmdline(task, buf, unused_len);
160 while (offset < len) {
161 if (buf[offset] != '\0' && buf[offset] != '\n')
162 task_entry->comm[i++] = buf[offset];
163 else
164 task_entry->comm[i++] = ' ';
165 offset++;
166 }
167
168 /* get rid of trailing whitespaces in case when arg is memset to
169 * zero before being reset in userspace
170 */
171 while (task_entry->comm[i-1] == ' ')
172 i--;
173 task_entry->comm[i] = '\0';
174}
175
176static struct task_entry *find_task_entry(struct uid_entry *uid_entry,
177 struct task_struct *task)
178{
179 struct task_entry *task_entry;
180
181 hash_for_each_possible(uid_entry->task_entries, task_entry, hash,
182 task->pid) {
183 if (task->pid == task_entry->pid) {
184 /* if thread name changed, update the entire command */
185 int len = strnchr(task_entry->comm, ' ', TASK_COMM_LEN)
186 - task_entry->comm;
187
188 if (strncmp(task_entry->comm, task->comm, len))
189 get_full_task_comm(task_entry, task);
190 return task_entry;
191 }
192 }
193 return NULL;
194}
195
196static struct task_entry *find_or_register_task(struct uid_entry *uid_entry,
197 struct task_struct *task)
198{
199 struct task_entry *task_entry;
200 pid_t pid = task->pid;
201
202 task_entry = find_task_entry(uid_entry, task);
203 if (task_entry)
204 return task_entry;
205
206 task_entry = kzalloc(sizeof(struct task_entry), GFP_ATOMIC);
207 if (!task_entry)
208 return NULL;
209
210 get_full_task_comm(task_entry, task);
211
212 task_entry->pid = pid;
213 hash_add(uid_entry->task_entries, &task_entry->hash, (unsigned int)pid);
214
215 return task_entry;
216}
217
218static void remove_uid_tasks(struct uid_entry *uid_entry)
219{
220 struct task_entry *task_entry;
221 unsigned long bkt_task;
222 struct hlist_node *tmp_task;
223
224 hash_for_each_safe(uid_entry->task_entries, bkt_task,
225 tmp_task, task_entry, hash) {
226 hash_del(&task_entry->hash);
227 kfree(task_entry);
228 }
229}
230
231static void set_io_uid_tasks_zero(struct uid_entry *uid_entry)
232{
233 struct task_entry *task_entry;
234 unsigned long bkt_task;
235
236 hash_for_each(uid_entry->task_entries, bkt_task, task_entry, hash) {
237 memset(&task_entry->io[UID_STATE_TOTAL_CURR], 0,
238 sizeof(struct io_stats));
239 }
240}
241
242static void add_uid_tasks_io_stats(struct uid_entry *uid_entry,
243 struct task_struct *task, int slot)
244{
245 struct task_entry *task_entry = find_or_register_task(uid_entry, task);
246 struct io_stats *task_io_slot = &task_entry->io[slot];
247
248 task_io_slot->read_bytes += task->ioac.read_bytes;
249 task_io_slot->write_bytes += compute_write_bytes(task);
250 task_io_slot->rchar += task->ioac.rchar;
251 task_io_slot->wchar += task->ioac.wchar;
252 task_io_slot->fsync += task->ioac.syscfs;
253}
254
255static void compute_io_uid_tasks(struct uid_entry *uid_entry)
256{
257 struct task_entry *task_entry;
258 unsigned long bkt_task;
259
260 hash_for_each(uid_entry->task_entries, bkt_task, task_entry, hash) {
261 compute_io_bucket_stats(&task_entry->io[uid_entry->state],
262 &task_entry->io[UID_STATE_TOTAL_CURR],
263 &task_entry->io[UID_STATE_TOTAL_LAST],
264 &task_entry->io[UID_STATE_DEAD_TASKS]);
265 }
266}
267
268static void show_io_uid_tasks(struct seq_file *m, struct uid_entry *uid_entry)
269{
270 struct task_entry *task_entry;
271 unsigned long bkt_task;
272
273 hash_for_each(uid_entry->task_entries, bkt_task, task_entry, hash) {
274 /* Separated by comma because space exists in task comm */
275 seq_printf(m, "task,%s,%lu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu\n",
276 task_entry->comm,
277 (unsigned long)task_entry->pid,
278 task_entry->io[UID_STATE_FOREGROUND].rchar,
279 task_entry->io[UID_STATE_FOREGROUND].wchar,
280 task_entry->io[UID_STATE_FOREGROUND].read_bytes,
281 task_entry->io[UID_STATE_FOREGROUND].write_bytes,
282 task_entry->io[UID_STATE_BACKGROUND].rchar,
283 task_entry->io[UID_STATE_BACKGROUND].wchar,
284 task_entry->io[UID_STATE_BACKGROUND].read_bytes,
285 task_entry->io[UID_STATE_BACKGROUND].write_bytes,
286 task_entry->io[UID_STATE_FOREGROUND].fsync,
287 task_entry->io[UID_STATE_BACKGROUND].fsync);
288 }
289}
290#else
291static void remove_uid_tasks(struct uid_entry *uid_entry) {};
292static void set_io_uid_tasks_zero(struct uid_entry *uid_entry) {};
293static void add_uid_tasks_io_stats(struct uid_entry *uid_entry,
294 struct task_struct *task, int slot) {};
295static void compute_io_uid_tasks(struct uid_entry *uid_entry) {};
296static void show_io_uid_tasks(struct seq_file *m,
297 struct uid_entry *uid_entry) {}
298#endif
299
jinqian69014222015-03-11 10:44:50 -0700300static struct uid_entry *find_uid_entry(uid_t uid)
301{
302 struct uid_entry *uid_entry;
303 hash_for_each_possible(hash_table, uid_entry, hash, uid) {
304 if (uid_entry->uid == uid)
305 return uid_entry;
306 }
307 return NULL;
308}
309
310static struct uid_entry *find_or_register_uid(uid_t uid)
311{
312 struct uid_entry *uid_entry;
313
314 uid_entry = find_uid_entry(uid);
315 if (uid_entry)
316 return uid_entry;
317
318 uid_entry = kzalloc(sizeof(struct uid_entry), GFP_ATOMIC);
319 if (!uid_entry)
320 return NULL;
321
322 uid_entry->uid = uid;
Yang Jin6e8f1e72017-07-26 12:52:22 -0700323#ifdef CONFIG_UID_SYS_STATS_DEBUG
324 hash_init(uid_entry->task_entries);
325#endif
jinqian69014222015-03-11 10:44:50 -0700326 hash_add(hash_table, &uid_entry->hash, uid);
327
328 return uid_entry;
329}
330
Jin Qian012f2002017-01-10 16:10:35 -0800331static int uid_cputime_show(struct seq_file *m, void *v)
jinqian69014222015-03-11 10:44:50 -0700332{
Ganesh Mahendran2b3da4a2017-05-25 15:20:29 +0800333 struct uid_entry *uid_entry = NULL;
Ruchi Kandoi0a733772015-07-31 10:17:54 -0700334 struct task_struct *task, *temp;
Ganesh Mahendrane6ef73f2017-04-25 18:07:43 +0800335 struct user_namespace *user_ns = current_user_ns();
jinqian69014222015-03-11 10:44:50 -0700336 cputime_t utime;
337 cputime_t stime;
338 unsigned long bkt;
Ganesh Mahendrane6ef73f2017-04-25 18:07:43 +0800339 uid_t uid;
jinqian69014222015-03-11 10:44:50 -0700340
Wei Wang7f1ad082017-03-13 12:22:21 -0700341 rt_mutex_lock(&uid_lock);
jinqian69014222015-03-11 10:44:50 -0700342
343 hash_for_each(hash_table, bkt, uid_entry, hash) {
344 uid_entry->active_stime = 0;
345 uid_entry->active_utime = 0;
346 }
347
348 read_lock(&tasklist_lock);
Ruchi Kandoi0a733772015-07-31 10:17:54 -0700349 do_each_thread(temp, task) {
Ganesh Mahendrane6ef73f2017-04-25 18:07:43 +0800350 uid = from_kuid_munged(user_ns, task_uid(task));
Ganesh Mahendran2b3da4a2017-05-25 15:20:29 +0800351 if (!uid_entry || uid_entry->uid != uid)
352 uid_entry = find_or_register_uid(uid);
jinqian69014222015-03-11 10:44:50 -0700353 if (!uid_entry) {
354 read_unlock(&tasklist_lock);
Wei Wang7f1ad082017-03-13 12:22:21 -0700355 rt_mutex_unlock(&uid_lock);
jinqian69014222015-03-11 10:44:50 -0700356 pr_err("%s: failed to find the uid_entry for uid %d\n",
Ganesh Mahendrane6ef73f2017-04-25 18:07:43 +0800357 __func__, uid);
jinqian69014222015-03-11 10:44:50 -0700358 return -ENOMEM;
359 }
360 task_cputime_adjusted(task, &utime, &stime);
361 uid_entry->active_utime += utime;
362 uid_entry->active_stime += stime;
Ruchi Kandoi0a733772015-07-31 10:17:54 -0700363 } while_each_thread(temp, task);
jinqian69014222015-03-11 10:44:50 -0700364 read_unlock(&tasklist_lock);
365
366 hash_for_each(hash_table, bkt, uid_entry, hash) {
367 cputime_t total_utime = uid_entry->utime +
368 uid_entry->active_utime;
369 cputime_t total_stime = uid_entry->stime +
370 uid_entry->active_stime;
Amit Pundire4395b22015-12-14 11:56:35 +0530371 seq_printf(m, "%d: %llu %llu\n", uid_entry->uid,
Jin Qianbe7074f2015-07-13 18:16:55 -0700372 (unsigned long long)jiffies_to_msecs(
373 cputime_to_jiffies(total_utime)) * USEC_PER_MSEC,
374 (unsigned long long)jiffies_to_msecs(
Amit Pundire4395b22015-12-14 11:56:35 +0530375 cputime_to_jiffies(total_stime)) * USEC_PER_MSEC);
jinqian69014222015-03-11 10:44:50 -0700376 }
377
Wei Wang7f1ad082017-03-13 12:22:21 -0700378 rt_mutex_unlock(&uid_lock);
jinqian69014222015-03-11 10:44:50 -0700379 return 0;
380}
381
Jin Qian012f2002017-01-10 16:10:35 -0800382static int uid_cputime_open(struct inode *inode, struct file *file)
jinqian69014222015-03-11 10:44:50 -0700383{
Jin Qian012f2002017-01-10 16:10:35 -0800384 return single_open(file, uid_cputime_show, PDE_DATA(inode));
jinqian69014222015-03-11 10:44:50 -0700385}
386
Jin Qian012f2002017-01-10 16:10:35 -0800387static const struct file_operations uid_cputime_fops = {
388 .open = uid_cputime_open,
jinqian69014222015-03-11 10:44:50 -0700389 .read = seq_read,
390 .llseek = seq_lseek,
391 .release = single_release,
392};
393
394static int uid_remove_open(struct inode *inode, struct file *file)
395{
396 return single_open(file, NULL, NULL);
397}
398
399static ssize_t uid_remove_write(struct file *file,
400 const char __user *buffer, size_t count, loff_t *ppos)
401{
402 struct uid_entry *uid_entry;
403 struct hlist_node *tmp;
404 char uids[128];
405 char *start_uid, *end_uid = NULL;
406 long int uid_start = 0, uid_end = 0;
407
408 if (count >= sizeof(uids))
409 count = sizeof(uids) - 1;
410
411 if (copy_from_user(uids, buffer, count))
412 return -EFAULT;
413
414 uids[count] = '\0';
415 end_uid = uids;
416 start_uid = strsep(&end_uid, "-");
417
418 if (!start_uid || !end_uid)
419 return -EINVAL;
420
421 if (kstrtol(start_uid, 10, &uid_start) != 0 ||
422 kstrtol(end_uid, 10, &uid_end) != 0) {
423 return -EINVAL;
424 }
Wei Wang7f1ad082017-03-13 12:22:21 -0700425 rt_mutex_lock(&uid_lock);
jinqian69014222015-03-11 10:44:50 -0700426
427 for (; uid_start <= uid_end; uid_start++) {
428 hash_for_each_possible_safe(hash_table, uid_entry, tmp,
Ruchi Kandoi17f35ea2015-10-23 17:49:11 -0700429 hash, (uid_t)uid_start) {
430 if (uid_start == uid_entry->uid) {
Yang Jin6e8f1e72017-07-26 12:52:22 -0700431 remove_uid_tasks(uid_entry);
Ruchi Kandoi17f35ea2015-10-23 17:49:11 -0700432 hash_del(&uid_entry->hash);
433 kfree(uid_entry);
434 }
jinqian69014222015-03-11 10:44:50 -0700435 }
436 }
437
Wei Wang7f1ad082017-03-13 12:22:21 -0700438 rt_mutex_unlock(&uid_lock);
jinqian69014222015-03-11 10:44:50 -0700439 return count;
440}
441
442static const struct file_operations uid_remove_fops = {
443 .open = uid_remove_open,
444 .release = single_release,
445 .write = uid_remove_write,
446};
447
Jin Qian5c837b92017-02-28 15:09:42 -0800448
Jin Qiancd5e19c2017-05-22 12:08:06 -0700449static void add_uid_io_stats(struct uid_entry *uid_entry,
450 struct task_struct *task, int slot)
Jin Qian012f2002017-01-10 16:10:35 -0800451{
Jin Qiancd5e19c2017-05-22 12:08:06 -0700452 struct io_stats *io_slot = &uid_entry->io[slot];
Jin Qian012f2002017-01-10 16:10:35 -0800453
Jin Qiancd5e19c2017-05-22 12:08:06 -0700454 io_slot->read_bytes += task->ioac.read_bytes;
455 io_slot->write_bytes += compute_write_bytes(task);
456 io_slot->rchar += task->ioac.rchar;
457 io_slot->wchar += task->ioac.wchar;
458 io_slot->fsync += task->ioac.syscfs;
Jin Qian012f2002017-01-10 16:10:35 -0800459
Yang Jin6e8f1e72017-07-26 12:52:22 -0700460 add_uid_tasks_io_stats(uid_entry, task, slot);
Jin Qian012f2002017-01-10 16:10:35 -0800461}
462
Jin Qian67f252c2017-04-13 17:07:58 -0700463static void update_io_stats_all_locked(void)
Jin Qian012f2002017-01-10 16:10:35 -0800464{
Ganesh Mahendran2b3da4a2017-05-25 15:20:29 +0800465 struct uid_entry *uid_entry = NULL;
Jin Qian012f2002017-01-10 16:10:35 -0800466 struct task_struct *task, *temp;
Jin Qian67f252c2017-04-13 17:07:58 -0700467 struct user_namespace *user_ns = current_user_ns();
Jin Qian012f2002017-01-10 16:10:35 -0800468 unsigned long bkt;
Jin Qian67f252c2017-04-13 17:07:58 -0700469 uid_t uid;
Jin Qian012f2002017-01-10 16:10:35 -0800470
Yang Jin6e8f1e72017-07-26 12:52:22 -0700471 hash_for_each(hash_table, bkt, uid_entry, hash) {
Jin Qian012f2002017-01-10 16:10:35 -0800472 memset(&uid_entry->io[UID_STATE_TOTAL_CURR], 0,
473 sizeof(struct io_stats));
Yang Jin6e8f1e72017-07-26 12:52:22 -0700474 set_io_uid_tasks_zero(uid_entry);
475 }
Jin Qian012f2002017-01-10 16:10:35 -0800476
Jin Qian67f252c2017-04-13 17:07:58 -0700477 rcu_read_lock();
Jin Qian012f2002017-01-10 16:10:35 -0800478 do_each_thread(temp, task) {
Jin Qian67f252c2017-04-13 17:07:58 -0700479 uid = from_kuid_munged(user_ns, task_uid(task));
Ganesh Mahendran2b3da4a2017-05-25 15:20:29 +0800480 if (!uid_entry || uid_entry->uid != uid)
481 uid_entry = find_or_register_uid(uid);
Jin Qian012f2002017-01-10 16:10:35 -0800482 if (!uid_entry)
483 continue;
Jin Qiancd5e19c2017-05-22 12:08:06 -0700484 add_uid_io_stats(uid_entry, task, UID_STATE_TOTAL_CURR);
Jin Qian012f2002017-01-10 16:10:35 -0800485 } while_each_thread(temp, task);
Jin Qian67f252c2017-04-13 17:07:58 -0700486 rcu_read_unlock();
Jin Qian012f2002017-01-10 16:10:35 -0800487
488 hash_for_each(hash_table, bkt, uid_entry, hash) {
Yang Jin6e8f1e72017-07-26 12:52:22 -0700489 compute_io_bucket_stats(&uid_entry->io[uid_entry->state],
Jin Qiancd5e19c2017-05-22 12:08:06 -0700490 &uid_entry->io[UID_STATE_TOTAL_CURR],
491 &uid_entry->io[UID_STATE_TOTAL_LAST],
492 &uid_entry->io[UID_STATE_DEAD_TASKS]);
Yang Jin6e8f1e72017-07-26 12:52:22 -0700493 compute_io_uid_tasks(uid_entry);
Jin Qian012f2002017-01-10 16:10:35 -0800494 }
495}
496
Jin Qiancd5e19c2017-05-22 12:08:06 -0700497static void update_io_stats_uid_locked(struct uid_entry *uid_entry)
Jin Qian67f252c2017-04-13 17:07:58 -0700498{
Jin Qian67f252c2017-04-13 17:07:58 -0700499 struct task_struct *task, *temp;
Jin Qian67f252c2017-04-13 17:07:58 -0700500 struct user_namespace *user_ns = current_user_ns();
501
Jin Qian67f252c2017-04-13 17:07:58 -0700502 memset(&uid_entry->io[UID_STATE_TOTAL_CURR], 0,
503 sizeof(struct io_stats));
Yang Jin6e8f1e72017-07-26 12:52:22 -0700504 set_io_uid_tasks_zero(uid_entry);
Jin Qian67f252c2017-04-13 17:07:58 -0700505
506 rcu_read_lock();
507 do_each_thread(temp, task) {
Jin Qiancd5e19c2017-05-22 12:08:06 -0700508 if (from_kuid_munged(user_ns, task_uid(task)) != uid_entry->uid)
Jin Qian67f252c2017-04-13 17:07:58 -0700509 continue;
Jin Qiancd5e19c2017-05-22 12:08:06 -0700510 add_uid_io_stats(uid_entry, task, UID_STATE_TOTAL_CURR);
Jin Qian67f252c2017-04-13 17:07:58 -0700511 } while_each_thread(temp, task);
512 rcu_read_unlock();
513
Yang Jin6e8f1e72017-07-26 12:52:22 -0700514 compute_io_bucket_stats(&uid_entry->io[uid_entry->state],
Jin Qiancd5e19c2017-05-22 12:08:06 -0700515 &uid_entry->io[UID_STATE_TOTAL_CURR],
516 &uid_entry->io[UID_STATE_TOTAL_LAST],
517 &uid_entry->io[UID_STATE_DEAD_TASKS]);
Yang Jin6e8f1e72017-07-26 12:52:22 -0700518 compute_io_uid_tasks(uid_entry);
Jin Qian67f252c2017-04-13 17:07:58 -0700519}
520
Yang Jin6e8f1e72017-07-26 12:52:22 -0700521
Jin Qian012f2002017-01-10 16:10:35 -0800522static int uid_io_show(struct seq_file *m, void *v)
523{
524 struct uid_entry *uid_entry;
525 unsigned long bkt;
526
Wei Wang7f1ad082017-03-13 12:22:21 -0700527 rt_mutex_lock(&uid_lock);
Jin Qian012f2002017-01-10 16:10:35 -0800528
Jin Qian67f252c2017-04-13 17:07:58 -0700529 update_io_stats_all_locked();
Jin Qian012f2002017-01-10 16:10:35 -0800530
531 hash_for_each(hash_table, bkt, uid_entry, hash) {
Jin Qian62eb9f92017-03-02 13:39:43 -0800532 seq_printf(m, "%d %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Yang Jin6e8f1e72017-07-26 12:52:22 -0700533 uid_entry->uid,
534 uid_entry->io[UID_STATE_FOREGROUND].rchar,
535 uid_entry->io[UID_STATE_FOREGROUND].wchar,
536 uid_entry->io[UID_STATE_FOREGROUND].read_bytes,
537 uid_entry->io[UID_STATE_FOREGROUND].write_bytes,
538 uid_entry->io[UID_STATE_BACKGROUND].rchar,
539 uid_entry->io[UID_STATE_BACKGROUND].wchar,
540 uid_entry->io[UID_STATE_BACKGROUND].read_bytes,
541 uid_entry->io[UID_STATE_BACKGROUND].write_bytes,
542 uid_entry->io[UID_STATE_FOREGROUND].fsync,
543 uid_entry->io[UID_STATE_BACKGROUND].fsync);
544
545 show_io_uid_tasks(m, uid_entry);
Jin Qian012f2002017-01-10 16:10:35 -0800546 }
547
Wei Wang7f1ad082017-03-13 12:22:21 -0700548 rt_mutex_unlock(&uid_lock);
Jin Qian012f2002017-01-10 16:10:35 -0800549 return 0;
550}
551
552static int uid_io_open(struct inode *inode, struct file *file)
553{
554 return single_open(file, uid_io_show, PDE_DATA(inode));
555}
556
557static const struct file_operations uid_io_fops = {
558 .open = uid_io_open,
559 .read = seq_read,
560 .llseek = seq_lseek,
561 .release = single_release,
562};
563
564static int uid_procstat_open(struct inode *inode, struct file *file)
565{
566 return single_open(file, NULL, NULL);
567}
568
569static ssize_t uid_procstat_write(struct file *file,
570 const char __user *buffer, size_t count, loff_t *ppos)
571{
572 struct uid_entry *uid_entry;
573 uid_t uid;
574 int argc, state;
575 char input[128];
576
577 if (count >= sizeof(input))
578 return -EINVAL;
579
580 if (copy_from_user(input, buffer, count))
581 return -EFAULT;
582
583 input[count] = '\0';
584
585 argc = sscanf(input, "%u %d", &uid, &state);
586 if (argc != 2)
587 return -EINVAL;
588
589 if (state != UID_STATE_BACKGROUND && state != UID_STATE_FOREGROUND)
590 return -EINVAL;
591
Wei Wang7f1ad082017-03-13 12:22:21 -0700592 rt_mutex_lock(&uid_lock);
Jin Qian012f2002017-01-10 16:10:35 -0800593
594 uid_entry = find_or_register_uid(uid);
Jin Qian8782d8f2017-01-17 17:26:07 -0800595 if (!uid_entry) {
Wei Wang7f1ad082017-03-13 12:22:21 -0700596 rt_mutex_unlock(&uid_lock);
Jin Qian012f2002017-01-10 16:10:35 -0800597 return -EINVAL;
598 }
599
Jin Qian8782d8f2017-01-17 17:26:07 -0800600 if (uid_entry->state == state) {
Wei Wang7f1ad082017-03-13 12:22:21 -0700601 rt_mutex_unlock(&uid_lock);
Jin Qian8782d8f2017-01-17 17:26:07 -0800602 return count;
603 }
604
Jin Qiancd5e19c2017-05-22 12:08:06 -0700605 update_io_stats_uid_locked(uid_entry);
Jin Qian012f2002017-01-10 16:10:35 -0800606
607 uid_entry->state = state;
608
Wei Wang7f1ad082017-03-13 12:22:21 -0700609 rt_mutex_unlock(&uid_lock);
Jin Qian012f2002017-01-10 16:10:35 -0800610
611 return count;
612}
613
614static const struct file_operations uid_procstat_fops = {
615 .open = uid_procstat_open,
616 .release = single_release,
617 .write = uid_procstat_write,
618};
619
jinqian69014222015-03-11 10:44:50 -0700620static int process_notifier(struct notifier_block *self,
621 unsigned long cmd, void *v)
622{
623 struct task_struct *task = v;
624 struct uid_entry *uid_entry;
625 cputime_t utime, stime;
626 uid_t uid;
627
628 if (!task)
629 return NOTIFY_OK;
630
Wei Wang7f1ad082017-03-13 12:22:21 -0700631 rt_mutex_lock(&uid_lock);
Amit Pundir48a99062015-04-15 00:40:21 +0530632 uid = from_kuid_munged(current_user_ns(), task_uid(task));
jinqian69014222015-03-11 10:44:50 -0700633 uid_entry = find_or_register_uid(uid);
634 if (!uid_entry) {
635 pr_err("%s: failed to find uid %d\n", __func__, uid);
636 goto exit;
637 }
638
639 task_cputime_adjusted(task, &utime, &stime);
640 uid_entry->utime += utime;
641 uid_entry->stime += stime;
642
Jin Qiancd5e19c2017-05-22 12:08:06 -0700643 add_uid_io_stats(uid_entry, task, UID_STATE_DEAD_TASKS);
Jin Qian012f2002017-01-10 16:10:35 -0800644
jinqian69014222015-03-11 10:44:50 -0700645exit:
Wei Wang7f1ad082017-03-13 12:22:21 -0700646 rt_mutex_unlock(&uid_lock);
jinqian69014222015-03-11 10:44:50 -0700647 return NOTIFY_OK;
648}
649
650static struct notifier_block process_notifier_block = {
651 .notifier_call = process_notifier,
652};
653
Jin Qian012f2002017-01-10 16:10:35 -0800654static int __init proc_uid_sys_stats_init(void)
jinqian69014222015-03-11 10:44:50 -0700655{
656 hash_init(hash_table);
657
Jin Qian012f2002017-01-10 16:10:35 -0800658 cpu_parent = proc_mkdir("uid_cputime", NULL);
659 if (!cpu_parent) {
660 pr_err("%s: failed to create uid_cputime proc entry\n",
661 __func__);
662 goto err;
jinqian69014222015-03-11 10:44:50 -0700663 }
664
Jin Qian012f2002017-01-10 16:10:35 -0800665 proc_create_data("remove_uid_range", 0222, cpu_parent,
666 &uid_remove_fops, NULL);
667 proc_create_data("show_uid_stat", 0444, cpu_parent,
668 &uid_cputime_fops, NULL);
jinqian69014222015-03-11 10:44:50 -0700669
Jin Qian012f2002017-01-10 16:10:35 -0800670 io_parent = proc_mkdir("uid_io", NULL);
671 if (!io_parent) {
672 pr_err("%s: failed to create uid_io proc entry\n",
673 __func__);
674 goto err;
675 }
676
677 proc_create_data("stats", 0444, io_parent,
678 &uid_io_fops, NULL);
679
680 proc_parent = proc_mkdir("uid_procstat", NULL);
681 if (!proc_parent) {
682 pr_err("%s: failed to create uid_procstat proc entry\n",
683 __func__);
684 goto err;
685 }
686
687 proc_create_data("set", 0222, proc_parent,
688 &uid_procstat_fops, NULL);
jinqian69014222015-03-11 10:44:50 -0700689
690 profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block);
691
692 return 0;
Jin Qian012f2002017-01-10 16:10:35 -0800693
694err:
695 remove_proc_subtree("uid_cputime", NULL);
696 remove_proc_subtree("uid_io", NULL);
697 remove_proc_subtree("uid_procstat", NULL);
698 return -ENOMEM;
jinqian69014222015-03-11 10:44:50 -0700699}
700
Jin Qian012f2002017-01-10 16:10:35 -0800701early_initcall(proc_uid_sys_stats_init);