jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 1 | /* 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> |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 24 | #include <linux/rtmutex.h> |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 25 | #include <linux/sched.h> |
| 26 | #include <linux/seq_file.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 30 | |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 31 | #define UID_HASH_BITS 10 |
| 32 | DECLARE_HASHTABLE(hash_table, UID_HASH_BITS); |
| 33 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 34 | static DEFINE_RT_MUTEX(uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 35 | static struct proc_dir_entry *cpu_parent; |
| 36 | static struct proc_dir_entry *io_parent; |
| 37 | static struct proc_dir_entry *proc_parent; |
| 38 | |
| 39 | struct io_stats { |
| 40 | u64 read_bytes; |
| 41 | u64 write_bytes; |
| 42 | u64 rchar; |
| 43 | u64 wchar; |
Jin Qian | 62eb9f9 | 2017-03-02 13:39:43 -0800 | [diff] [blame] | 44 | u64 fsync; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | #define UID_STATE_FOREGROUND 0 |
| 48 | #define UID_STATE_BACKGROUND 1 |
| 49 | #define UID_STATE_BUCKET_SIZE 2 |
| 50 | |
| 51 | #define UID_STATE_TOTAL_CURR 2 |
| 52 | #define UID_STATE_TOTAL_LAST 3 |
| 53 | #define UID_STATE_SIZE 4 |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 54 | |
| 55 | struct uid_entry { |
| 56 | uid_t uid; |
| 57 | cputime_t utime; |
| 58 | cputime_t stime; |
| 59 | cputime_t active_utime; |
| 60 | cputime_t active_stime; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 61 | int state; |
| 62 | struct io_stats io[UID_STATE_SIZE]; |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 63 | struct hlist_node hash; |
| 64 | }; |
| 65 | |
| 66 | static struct uid_entry *find_uid_entry(uid_t uid) |
| 67 | { |
| 68 | struct uid_entry *uid_entry; |
| 69 | hash_for_each_possible(hash_table, uid_entry, hash, uid) { |
| 70 | if (uid_entry->uid == uid) |
| 71 | return uid_entry; |
| 72 | } |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | static struct uid_entry *find_or_register_uid(uid_t uid) |
| 77 | { |
| 78 | struct uid_entry *uid_entry; |
| 79 | |
| 80 | uid_entry = find_uid_entry(uid); |
| 81 | if (uid_entry) |
| 82 | return uid_entry; |
| 83 | |
| 84 | uid_entry = kzalloc(sizeof(struct uid_entry), GFP_ATOMIC); |
| 85 | if (!uid_entry) |
| 86 | return NULL; |
| 87 | |
| 88 | uid_entry->uid = uid; |
| 89 | |
| 90 | hash_add(hash_table, &uid_entry->hash, uid); |
| 91 | |
| 92 | return uid_entry; |
| 93 | } |
| 94 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 95 | static int uid_cputime_show(struct seq_file *m, void *v) |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 96 | { |
| 97 | struct uid_entry *uid_entry; |
Ruchi Kandoi | 0a73377 | 2015-07-31 10:17:54 -0700 | [diff] [blame] | 98 | struct task_struct *task, *temp; |
Ganesh Mahendran | e6ef73f | 2017-04-25 18:07:43 +0800 | [diff] [blame] | 99 | struct user_namespace *user_ns = current_user_ns(); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 100 | cputime_t utime; |
| 101 | cputime_t stime; |
| 102 | unsigned long bkt; |
Ganesh Mahendran | e6ef73f | 2017-04-25 18:07:43 +0800 | [diff] [blame] | 103 | uid_t uid; |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 104 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 105 | rt_mutex_lock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 106 | |
| 107 | hash_for_each(hash_table, bkt, uid_entry, hash) { |
| 108 | uid_entry->active_stime = 0; |
| 109 | uid_entry->active_utime = 0; |
| 110 | } |
| 111 | |
| 112 | read_lock(&tasklist_lock); |
Ruchi Kandoi | 0a73377 | 2015-07-31 10:17:54 -0700 | [diff] [blame] | 113 | do_each_thread(temp, task) { |
Ganesh Mahendran | e6ef73f | 2017-04-25 18:07:43 +0800 | [diff] [blame] | 114 | uid = from_kuid_munged(user_ns, task_uid(task)); |
| 115 | uid_entry = find_or_register_uid(uid); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 116 | if (!uid_entry) { |
| 117 | read_unlock(&tasklist_lock); |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 118 | rt_mutex_unlock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 119 | pr_err("%s: failed to find the uid_entry for uid %d\n", |
Ganesh Mahendran | e6ef73f | 2017-04-25 18:07:43 +0800 | [diff] [blame] | 120 | __func__, uid); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 121 | return -ENOMEM; |
| 122 | } |
| 123 | task_cputime_adjusted(task, &utime, &stime); |
| 124 | uid_entry->active_utime += utime; |
| 125 | uid_entry->active_stime += stime; |
Ruchi Kandoi | 0a73377 | 2015-07-31 10:17:54 -0700 | [diff] [blame] | 126 | } while_each_thread(temp, task); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 127 | read_unlock(&tasklist_lock); |
| 128 | |
| 129 | hash_for_each(hash_table, bkt, uid_entry, hash) { |
| 130 | cputime_t total_utime = uid_entry->utime + |
| 131 | uid_entry->active_utime; |
| 132 | cputime_t total_stime = uid_entry->stime + |
| 133 | uid_entry->active_stime; |
Amit Pundir | e4395b2 | 2015-12-14 11:56:35 +0530 | [diff] [blame] | 134 | seq_printf(m, "%d: %llu %llu\n", uid_entry->uid, |
Jin Qian | be7074f | 2015-07-13 18:16:55 -0700 | [diff] [blame] | 135 | (unsigned long long)jiffies_to_msecs( |
| 136 | cputime_to_jiffies(total_utime)) * USEC_PER_MSEC, |
| 137 | (unsigned long long)jiffies_to_msecs( |
Amit Pundir | e4395b2 | 2015-12-14 11:56:35 +0530 | [diff] [blame] | 138 | cputime_to_jiffies(total_stime)) * USEC_PER_MSEC); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 141 | rt_mutex_unlock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 145 | static int uid_cputime_open(struct inode *inode, struct file *file) |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 146 | { |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 147 | return single_open(file, uid_cputime_show, PDE_DATA(inode)); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 150 | static const struct file_operations uid_cputime_fops = { |
| 151 | .open = uid_cputime_open, |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 152 | .read = seq_read, |
| 153 | .llseek = seq_lseek, |
| 154 | .release = single_release, |
| 155 | }; |
| 156 | |
| 157 | static int uid_remove_open(struct inode *inode, struct file *file) |
| 158 | { |
| 159 | return single_open(file, NULL, NULL); |
| 160 | } |
| 161 | |
| 162 | static ssize_t uid_remove_write(struct file *file, |
| 163 | const char __user *buffer, size_t count, loff_t *ppos) |
| 164 | { |
| 165 | struct uid_entry *uid_entry; |
| 166 | struct hlist_node *tmp; |
| 167 | char uids[128]; |
| 168 | char *start_uid, *end_uid = NULL; |
| 169 | long int uid_start = 0, uid_end = 0; |
| 170 | |
| 171 | if (count >= sizeof(uids)) |
| 172 | count = sizeof(uids) - 1; |
| 173 | |
| 174 | if (copy_from_user(uids, buffer, count)) |
| 175 | return -EFAULT; |
| 176 | |
| 177 | uids[count] = '\0'; |
| 178 | end_uid = uids; |
| 179 | start_uid = strsep(&end_uid, "-"); |
| 180 | |
| 181 | if (!start_uid || !end_uid) |
| 182 | return -EINVAL; |
| 183 | |
| 184 | if (kstrtol(start_uid, 10, &uid_start) != 0 || |
| 185 | kstrtol(end_uid, 10, &uid_end) != 0) { |
| 186 | return -EINVAL; |
| 187 | } |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 188 | rt_mutex_lock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 189 | |
| 190 | for (; uid_start <= uid_end; uid_start++) { |
| 191 | hash_for_each_possible_safe(hash_table, uid_entry, tmp, |
Ruchi Kandoi | 17f35ea | 2015-10-23 17:49:11 -0700 | [diff] [blame] | 192 | hash, (uid_t)uid_start) { |
| 193 | if (uid_start == uid_entry->uid) { |
| 194 | hash_del(&uid_entry->hash); |
| 195 | kfree(uid_entry); |
| 196 | } |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 200 | rt_mutex_unlock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 201 | return count; |
| 202 | } |
| 203 | |
| 204 | static const struct file_operations uid_remove_fops = { |
| 205 | .open = uid_remove_open, |
| 206 | .release = single_release, |
| 207 | .write = uid_remove_write, |
| 208 | }; |
| 209 | |
Jin Qian | 5c837b9 | 2017-02-28 15:09:42 -0800 | [diff] [blame] | 210 | static u64 compute_write_bytes(struct task_struct *task) |
| 211 | { |
| 212 | if (task->ioac.write_bytes <= task->ioac.cancelled_write_bytes) |
| 213 | return 0; |
| 214 | |
| 215 | return task->ioac.write_bytes - task->ioac.cancelled_write_bytes; |
| 216 | } |
| 217 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 218 | static void add_uid_io_curr_stats(struct uid_entry *uid_entry, |
| 219 | struct task_struct *task) |
| 220 | { |
| 221 | struct io_stats *io_curr = &uid_entry->io[UID_STATE_TOTAL_CURR]; |
| 222 | |
| 223 | io_curr->read_bytes += task->ioac.read_bytes; |
Jin Qian | 5c837b9 | 2017-02-28 15:09:42 -0800 | [diff] [blame] | 224 | io_curr->write_bytes += compute_write_bytes(task); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 225 | io_curr->rchar += task->ioac.rchar; |
| 226 | io_curr->wchar += task->ioac.wchar; |
Jin Qian | 62eb9f9 | 2017-03-02 13:39:43 -0800 | [diff] [blame] | 227 | io_curr->fsync += task->ioac.syscfs; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void clean_uid_io_last_stats(struct uid_entry *uid_entry, |
| 231 | struct task_struct *task) |
| 232 | { |
| 233 | struct io_stats *io_last = &uid_entry->io[UID_STATE_TOTAL_LAST]; |
| 234 | |
| 235 | io_last->read_bytes -= task->ioac.read_bytes; |
Jin Qian | 5c837b9 | 2017-02-28 15:09:42 -0800 | [diff] [blame] | 236 | io_last->write_bytes -= compute_write_bytes(task); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 237 | io_last->rchar -= task->ioac.rchar; |
| 238 | io_last->wchar -= task->ioac.wchar; |
Jin Qian | 62eb9f9 | 2017-03-02 13:39:43 -0800 | [diff] [blame] | 239 | io_last->fsync -= task->ioac.syscfs; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 242 | static void update_io_stats_all_locked(void) |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 243 | { |
| 244 | struct uid_entry *uid_entry; |
| 245 | struct task_struct *task, *temp; |
| 246 | struct io_stats *io_bucket, *io_curr, *io_last; |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 247 | struct user_namespace *user_ns = current_user_ns(); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 248 | unsigned long bkt; |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 249 | uid_t uid; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 250 | |
| 251 | hash_for_each(hash_table, bkt, uid_entry, hash) |
| 252 | memset(&uid_entry->io[UID_STATE_TOTAL_CURR], 0, |
| 253 | sizeof(struct io_stats)); |
| 254 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 255 | rcu_read_lock(); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 256 | do_each_thread(temp, task) { |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 257 | uid = from_kuid_munged(user_ns, task_uid(task)); |
| 258 | uid_entry = find_or_register_uid(uid); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 259 | if (!uid_entry) |
| 260 | continue; |
| 261 | add_uid_io_curr_stats(uid_entry, task); |
| 262 | } while_each_thread(temp, task); |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 263 | rcu_read_unlock(); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 264 | |
| 265 | hash_for_each(hash_table, bkt, uid_entry, hash) { |
| 266 | io_bucket = &uid_entry->io[uid_entry->state]; |
| 267 | io_curr = &uid_entry->io[UID_STATE_TOTAL_CURR]; |
| 268 | io_last = &uid_entry->io[UID_STATE_TOTAL_LAST]; |
| 269 | |
| 270 | io_bucket->read_bytes += |
| 271 | io_curr->read_bytes - io_last->read_bytes; |
| 272 | io_bucket->write_bytes += |
| 273 | io_curr->write_bytes - io_last->write_bytes; |
| 274 | io_bucket->rchar += io_curr->rchar - io_last->rchar; |
| 275 | io_bucket->wchar += io_curr->wchar - io_last->wchar; |
Jin Qian | 62eb9f9 | 2017-03-02 13:39:43 -0800 | [diff] [blame] | 276 | io_bucket->fsync += io_curr->fsync - io_last->fsync; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 277 | |
| 278 | io_last->read_bytes = io_curr->read_bytes; |
| 279 | io_last->write_bytes = io_curr->write_bytes; |
| 280 | io_last->rchar = io_curr->rchar; |
| 281 | io_last->wchar = io_curr->wchar; |
Jin Qian | 62eb9f9 | 2017-03-02 13:39:43 -0800 | [diff] [blame] | 282 | io_last->fsync = io_curr->fsync; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 286 | static void update_io_stats_uid_locked(uid_t target_uid) |
| 287 | { |
| 288 | struct uid_entry *uid_entry; |
| 289 | struct task_struct *task, *temp; |
| 290 | struct io_stats *io_bucket, *io_curr, *io_last; |
| 291 | struct user_namespace *user_ns = current_user_ns(); |
| 292 | |
| 293 | uid_entry = find_or_register_uid(target_uid); |
| 294 | if (!uid_entry) |
| 295 | return; |
| 296 | |
| 297 | memset(&uid_entry->io[UID_STATE_TOTAL_CURR], 0, |
| 298 | sizeof(struct io_stats)); |
| 299 | |
| 300 | rcu_read_lock(); |
| 301 | do_each_thread(temp, task) { |
| 302 | if (from_kuid_munged(user_ns, task_uid(task)) != target_uid) |
| 303 | continue; |
| 304 | add_uid_io_curr_stats(uid_entry, task); |
| 305 | } while_each_thread(temp, task); |
| 306 | rcu_read_unlock(); |
| 307 | |
| 308 | io_bucket = &uid_entry->io[uid_entry->state]; |
| 309 | io_curr = &uid_entry->io[UID_STATE_TOTAL_CURR]; |
| 310 | io_last = &uid_entry->io[UID_STATE_TOTAL_LAST]; |
| 311 | |
| 312 | io_bucket->read_bytes += |
| 313 | io_curr->read_bytes - io_last->read_bytes; |
| 314 | io_bucket->write_bytes += |
| 315 | io_curr->write_bytes - io_last->write_bytes; |
| 316 | io_bucket->rchar += io_curr->rchar - io_last->rchar; |
| 317 | io_bucket->wchar += io_curr->wchar - io_last->wchar; |
| 318 | io_bucket->fsync += io_curr->fsync - io_last->fsync; |
| 319 | |
| 320 | io_last->read_bytes = io_curr->read_bytes; |
| 321 | io_last->write_bytes = io_curr->write_bytes; |
| 322 | io_last->rchar = io_curr->rchar; |
| 323 | io_last->wchar = io_curr->wchar; |
| 324 | io_last->fsync = io_curr->fsync; |
| 325 | } |
| 326 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 327 | static int uid_io_show(struct seq_file *m, void *v) |
| 328 | { |
| 329 | struct uid_entry *uid_entry; |
| 330 | unsigned long bkt; |
| 331 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 332 | rt_mutex_lock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 333 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 334 | update_io_stats_all_locked(); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 335 | |
| 336 | hash_for_each(hash_table, bkt, uid_entry, hash) { |
Jin Qian | 62eb9f9 | 2017-03-02 13:39:43 -0800 | [diff] [blame] | 337 | seq_printf(m, "%d %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 338 | uid_entry->uid, |
| 339 | uid_entry->io[UID_STATE_FOREGROUND].rchar, |
| 340 | uid_entry->io[UID_STATE_FOREGROUND].wchar, |
| 341 | uid_entry->io[UID_STATE_FOREGROUND].read_bytes, |
| 342 | uid_entry->io[UID_STATE_FOREGROUND].write_bytes, |
| 343 | uid_entry->io[UID_STATE_BACKGROUND].rchar, |
| 344 | uid_entry->io[UID_STATE_BACKGROUND].wchar, |
| 345 | uid_entry->io[UID_STATE_BACKGROUND].read_bytes, |
Jin Qian | 62eb9f9 | 2017-03-02 13:39:43 -0800 | [diff] [blame] | 346 | uid_entry->io[UID_STATE_BACKGROUND].write_bytes, |
| 347 | uid_entry->io[UID_STATE_FOREGROUND].fsync, |
| 348 | uid_entry->io[UID_STATE_BACKGROUND].fsync); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 351 | rt_mutex_unlock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static int uid_io_open(struct inode *inode, struct file *file) |
| 357 | { |
| 358 | return single_open(file, uid_io_show, PDE_DATA(inode)); |
| 359 | } |
| 360 | |
| 361 | static const struct file_operations uid_io_fops = { |
| 362 | .open = uid_io_open, |
| 363 | .read = seq_read, |
| 364 | .llseek = seq_lseek, |
| 365 | .release = single_release, |
| 366 | }; |
| 367 | |
| 368 | static int uid_procstat_open(struct inode *inode, struct file *file) |
| 369 | { |
| 370 | return single_open(file, NULL, NULL); |
| 371 | } |
| 372 | |
| 373 | static ssize_t uid_procstat_write(struct file *file, |
| 374 | const char __user *buffer, size_t count, loff_t *ppos) |
| 375 | { |
| 376 | struct uid_entry *uid_entry; |
| 377 | uid_t uid; |
| 378 | int argc, state; |
| 379 | char input[128]; |
| 380 | |
| 381 | if (count >= sizeof(input)) |
| 382 | return -EINVAL; |
| 383 | |
| 384 | if (copy_from_user(input, buffer, count)) |
| 385 | return -EFAULT; |
| 386 | |
| 387 | input[count] = '\0'; |
| 388 | |
| 389 | argc = sscanf(input, "%u %d", &uid, &state); |
| 390 | if (argc != 2) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | if (state != UID_STATE_BACKGROUND && state != UID_STATE_FOREGROUND) |
| 394 | return -EINVAL; |
| 395 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 396 | rt_mutex_lock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 397 | |
| 398 | uid_entry = find_or_register_uid(uid); |
Jin Qian | 8782d8f | 2017-01-17 17:26:07 -0800 | [diff] [blame] | 399 | if (!uid_entry) { |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 400 | rt_mutex_unlock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 401 | return -EINVAL; |
| 402 | } |
| 403 | |
Jin Qian | 8782d8f | 2017-01-17 17:26:07 -0800 | [diff] [blame] | 404 | if (uid_entry->state == state) { |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 405 | rt_mutex_unlock(&uid_lock); |
Jin Qian | 8782d8f | 2017-01-17 17:26:07 -0800 | [diff] [blame] | 406 | return count; |
| 407 | } |
| 408 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 409 | update_io_stats_uid_locked(uid); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 410 | |
| 411 | uid_entry->state = state; |
| 412 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 413 | rt_mutex_unlock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 414 | |
| 415 | return count; |
| 416 | } |
| 417 | |
| 418 | static const struct file_operations uid_procstat_fops = { |
| 419 | .open = uid_procstat_open, |
| 420 | .release = single_release, |
| 421 | .write = uid_procstat_write, |
| 422 | }; |
| 423 | |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 424 | static int process_notifier(struct notifier_block *self, |
| 425 | unsigned long cmd, void *v) |
| 426 | { |
| 427 | struct task_struct *task = v; |
| 428 | struct uid_entry *uid_entry; |
| 429 | cputime_t utime, stime; |
| 430 | uid_t uid; |
| 431 | |
| 432 | if (!task) |
| 433 | return NOTIFY_OK; |
| 434 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 435 | rt_mutex_lock(&uid_lock); |
Amit Pundir | 48a9906 | 2015-04-15 00:40:21 +0530 | [diff] [blame] | 436 | uid = from_kuid_munged(current_user_ns(), task_uid(task)); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 437 | uid_entry = find_or_register_uid(uid); |
| 438 | if (!uid_entry) { |
| 439 | pr_err("%s: failed to find uid %d\n", __func__, uid); |
| 440 | goto exit; |
| 441 | } |
| 442 | |
| 443 | task_cputime_adjusted(task, &utime, &stime); |
| 444 | uid_entry->utime += utime; |
| 445 | uid_entry->stime += stime; |
| 446 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 447 | update_io_stats_uid_locked(uid); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 448 | clean_uid_io_last_stats(uid_entry, task); |
| 449 | |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 450 | exit: |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 451 | rt_mutex_unlock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 452 | return NOTIFY_OK; |
| 453 | } |
| 454 | |
| 455 | static struct notifier_block process_notifier_block = { |
| 456 | .notifier_call = process_notifier, |
| 457 | }; |
| 458 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 459 | static int __init proc_uid_sys_stats_init(void) |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 460 | { |
| 461 | hash_init(hash_table); |
| 462 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 463 | cpu_parent = proc_mkdir("uid_cputime", NULL); |
| 464 | if (!cpu_parent) { |
| 465 | pr_err("%s: failed to create uid_cputime proc entry\n", |
| 466 | __func__); |
| 467 | goto err; |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 470 | proc_create_data("remove_uid_range", 0222, cpu_parent, |
| 471 | &uid_remove_fops, NULL); |
| 472 | proc_create_data("show_uid_stat", 0444, cpu_parent, |
| 473 | &uid_cputime_fops, NULL); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 474 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 475 | io_parent = proc_mkdir("uid_io", NULL); |
| 476 | if (!io_parent) { |
| 477 | pr_err("%s: failed to create uid_io proc entry\n", |
| 478 | __func__); |
| 479 | goto err; |
| 480 | } |
| 481 | |
| 482 | proc_create_data("stats", 0444, io_parent, |
| 483 | &uid_io_fops, NULL); |
| 484 | |
| 485 | proc_parent = proc_mkdir("uid_procstat", NULL); |
| 486 | if (!proc_parent) { |
| 487 | pr_err("%s: failed to create uid_procstat proc entry\n", |
| 488 | __func__); |
| 489 | goto err; |
| 490 | } |
| 491 | |
| 492 | proc_create_data("set", 0222, proc_parent, |
| 493 | &uid_procstat_fops, NULL); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 494 | |
| 495 | profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block); |
| 496 | |
| 497 | return 0; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 498 | |
| 499 | err: |
| 500 | remove_proc_subtree("uid_cputime", NULL); |
| 501 | remove_proc_subtree("uid_io", NULL); |
| 502 | remove_proc_subtree("uid_procstat", NULL); |
| 503 | return -ENOMEM; |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 506 | early_initcall(proc_uid_sys_stats_init); |