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> |
Amit Pundir | 0ea205a | 2017-08-17 18:52:04 +0530 | [diff] [blame] | 22 | #include <linux/mm.h> |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 23 | #include <linux/proc_fs.h> |
| 24 | #include <linux/profile.h> |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 25 | #include <linux/rtmutex.h> |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 26 | #include <linux/sched.h> |
| 27 | #include <linux/seq_file.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/uaccess.h> |
| 30 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 31 | |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 32 | #define UID_HASH_BITS 10 |
| 33 | DECLARE_HASHTABLE(hash_table, UID_HASH_BITS); |
| 34 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 35 | static DEFINE_RT_MUTEX(uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 36 | static struct proc_dir_entry *cpu_parent; |
| 37 | static struct proc_dir_entry *io_parent; |
| 38 | static struct proc_dir_entry *proc_parent; |
| 39 | |
| 40 | struct io_stats { |
| 41 | u64 read_bytes; |
| 42 | u64 write_bytes; |
| 43 | u64 rchar; |
| 44 | u64 wchar; |
Jin Qian | 62eb9f9 | 2017-03-02 13:39:43 -0800 | [diff] [blame] | 45 | u64 fsync; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 46 | }; |
| 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 Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 54 | #define UID_STATE_DEAD_TASKS 4 |
| 55 | #define UID_STATE_SIZE 5 |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 56 | |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 57 | #define MAX_TASK_COMM_LEN 256 |
| 58 | |
| 59 | struct 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 | |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 66 | struct 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 Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 72 | int state; |
| 73 | struct io_stats io[UID_STATE_SIZE]; |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 74 | struct hlist_node hash; |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 75 | #ifdef CONFIG_UID_SYS_STATS_DEBUG |
| 76 | DECLARE_HASHTABLE(task_entries, UID_HASH_BITS); |
| 77 | #endif |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 80 | static 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 | |
| 88 | static 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 |
| 122 | static 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 | |
| 176 | static 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 | |
| 196 | static 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 | |
| 218 | static 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 | |
| 231 | static 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 | |
| 242 | static 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 | |
| 255 | static 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 | |
| 268 | static 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 |
| 291 | static void remove_uid_tasks(struct uid_entry *uid_entry) {}; |
| 292 | static void set_io_uid_tasks_zero(struct uid_entry *uid_entry) {}; |
| 293 | static void add_uid_tasks_io_stats(struct uid_entry *uid_entry, |
| 294 | struct task_struct *task, int slot) {}; |
| 295 | static void compute_io_uid_tasks(struct uid_entry *uid_entry) {}; |
| 296 | static void show_io_uid_tasks(struct seq_file *m, |
| 297 | struct uid_entry *uid_entry) {} |
| 298 | #endif |
| 299 | |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 300 | static 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 | |
| 310 | static 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 Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 323 | #ifdef CONFIG_UID_SYS_STATS_DEBUG |
| 324 | hash_init(uid_entry->task_entries); |
| 325 | #endif |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 326 | hash_add(hash_table, &uid_entry->hash, uid); |
| 327 | |
| 328 | return uid_entry; |
| 329 | } |
| 330 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 331 | static int uid_cputime_show(struct seq_file *m, void *v) |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 332 | { |
Ganesh Mahendran | 2b3da4a | 2017-05-25 15:20:29 +0800 | [diff] [blame] | 333 | struct uid_entry *uid_entry = NULL; |
Ruchi Kandoi | 0a73377 | 2015-07-31 10:17:54 -0700 | [diff] [blame] | 334 | struct task_struct *task, *temp; |
Ganesh Mahendran | e6ef73f | 2017-04-25 18:07:43 +0800 | [diff] [blame] | 335 | struct user_namespace *user_ns = current_user_ns(); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 336 | cputime_t utime; |
| 337 | cputime_t stime; |
| 338 | unsigned long bkt; |
Ganesh Mahendran | e6ef73f | 2017-04-25 18:07:43 +0800 | [diff] [blame] | 339 | uid_t uid; |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 340 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 341 | rt_mutex_lock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 342 | |
| 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 Kandoi | 0a73377 | 2015-07-31 10:17:54 -0700 | [diff] [blame] | 349 | do_each_thread(temp, task) { |
Ganesh Mahendran | e6ef73f | 2017-04-25 18:07:43 +0800 | [diff] [blame] | 350 | uid = from_kuid_munged(user_ns, task_uid(task)); |
Ganesh Mahendran | 2b3da4a | 2017-05-25 15:20:29 +0800 | [diff] [blame] | 351 | if (!uid_entry || uid_entry->uid != uid) |
| 352 | uid_entry = find_or_register_uid(uid); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 353 | if (!uid_entry) { |
| 354 | read_unlock(&tasklist_lock); |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 355 | rt_mutex_unlock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 356 | 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] | 357 | __func__, uid); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 358 | return -ENOMEM; |
| 359 | } |
| 360 | task_cputime_adjusted(task, &utime, &stime); |
| 361 | uid_entry->active_utime += utime; |
| 362 | uid_entry->active_stime += stime; |
Ruchi Kandoi | 0a73377 | 2015-07-31 10:17:54 -0700 | [diff] [blame] | 363 | } while_each_thread(temp, task); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 364 | 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 Pundir | e4395b2 | 2015-12-14 11:56:35 +0530 | [diff] [blame] | 371 | seq_printf(m, "%d: %llu %llu\n", uid_entry->uid, |
Jin Qian | be7074f | 2015-07-13 18:16:55 -0700 | [diff] [blame] | 372 | (unsigned long long)jiffies_to_msecs( |
| 373 | cputime_to_jiffies(total_utime)) * USEC_PER_MSEC, |
| 374 | (unsigned long long)jiffies_to_msecs( |
Amit Pundir | e4395b2 | 2015-12-14 11:56:35 +0530 | [diff] [blame] | 375 | cputime_to_jiffies(total_stime)) * USEC_PER_MSEC); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 378 | rt_mutex_unlock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 382 | static int uid_cputime_open(struct inode *inode, struct file *file) |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 383 | { |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 384 | return single_open(file, uid_cputime_show, PDE_DATA(inode)); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 387 | static const struct file_operations uid_cputime_fops = { |
| 388 | .open = uid_cputime_open, |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 389 | .read = seq_read, |
| 390 | .llseek = seq_lseek, |
| 391 | .release = single_release, |
| 392 | }; |
| 393 | |
| 394 | static int uid_remove_open(struct inode *inode, struct file *file) |
| 395 | { |
| 396 | return single_open(file, NULL, NULL); |
| 397 | } |
| 398 | |
| 399 | static 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 Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 425 | rt_mutex_lock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 426 | |
| 427 | for (; uid_start <= uid_end; uid_start++) { |
| 428 | hash_for_each_possible_safe(hash_table, uid_entry, tmp, |
Ruchi Kandoi | 17f35ea | 2015-10-23 17:49:11 -0700 | [diff] [blame] | 429 | hash, (uid_t)uid_start) { |
| 430 | if (uid_start == uid_entry->uid) { |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 431 | remove_uid_tasks(uid_entry); |
Ruchi Kandoi | 17f35ea | 2015-10-23 17:49:11 -0700 | [diff] [blame] | 432 | hash_del(&uid_entry->hash); |
| 433 | kfree(uid_entry); |
| 434 | } |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 438 | rt_mutex_unlock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 439 | return count; |
| 440 | } |
| 441 | |
| 442 | static const struct file_operations uid_remove_fops = { |
| 443 | .open = uid_remove_open, |
| 444 | .release = single_release, |
| 445 | .write = uid_remove_write, |
| 446 | }; |
| 447 | |
Jin Qian | 5c837b9 | 2017-02-28 15:09:42 -0800 | [diff] [blame] | 448 | |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 449 | static void add_uid_io_stats(struct uid_entry *uid_entry, |
| 450 | struct task_struct *task, int slot) |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 451 | { |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 452 | struct io_stats *io_slot = &uid_entry->io[slot]; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 453 | |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 454 | 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 Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 459 | |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 460 | add_uid_tasks_io_stats(uid_entry, task, slot); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 463 | static void update_io_stats_all_locked(void) |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 464 | { |
Ganesh Mahendran | 2b3da4a | 2017-05-25 15:20:29 +0800 | [diff] [blame] | 465 | struct uid_entry *uid_entry = NULL; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 466 | struct task_struct *task, *temp; |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 467 | struct user_namespace *user_ns = current_user_ns(); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 468 | unsigned long bkt; |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 469 | uid_t uid; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 470 | |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 471 | hash_for_each(hash_table, bkt, uid_entry, hash) { |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 472 | memset(&uid_entry->io[UID_STATE_TOTAL_CURR], 0, |
| 473 | sizeof(struct io_stats)); |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 474 | set_io_uid_tasks_zero(uid_entry); |
| 475 | } |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 476 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 477 | rcu_read_lock(); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 478 | do_each_thread(temp, task) { |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 479 | uid = from_kuid_munged(user_ns, task_uid(task)); |
Ganesh Mahendran | 2b3da4a | 2017-05-25 15:20:29 +0800 | [diff] [blame] | 480 | if (!uid_entry || uid_entry->uid != uid) |
| 481 | uid_entry = find_or_register_uid(uid); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 482 | if (!uid_entry) |
| 483 | continue; |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 484 | add_uid_io_stats(uid_entry, task, UID_STATE_TOTAL_CURR); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 485 | } while_each_thread(temp, task); |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 486 | rcu_read_unlock(); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 487 | |
| 488 | hash_for_each(hash_table, bkt, uid_entry, hash) { |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 489 | compute_io_bucket_stats(&uid_entry->io[uid_entry->state], |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 490 | &uid_entry->io[UID_STATE_TOTAL_CURR], |
| 491 | &uid_entry->io[UID_STATE_TOTAL_LAST], |
| 492 | &uid_entry->io[UID_STATE_DEAD_TASKS]); |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 493 | compute_io_uid_tasks(uid_entry); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 497 | static void update_io_stats_uid_locked(struct uid_entry *uid_entry) |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 498 | { |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 499 | struct task_struct *task, *temp; |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 500 | struct user_namespace *user_ns = current_user_ns(); |
| 501 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 502 | memset(&uid_entry->io[UID_STATE_TOTAL_CURR], 0, |
| 503 | sizeof(struct io_stats)); |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 504 | set_io_uid_tasks_zero(uid_entry); |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 505 | |
| 506 | rcu_read_lock(); |
| 507 | do_each_thread(temp, task) { |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 508 | if (from_kuid_munged(user_ns, task_uid(task)) != uid_entry->uid) |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 509 | continue; |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 510 | add_uid_io_stats(uid_entry, task, UID_STATE_TOTAL_CURR); |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 511 | } while_each_thread(temp, task); |
| 512 | rcu_read_unlock(); |
| 513 | |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 514 | compute_io_bucket_stats(&uid_entry->io[uid_entry->state], |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 515 | &uid_entry->io[UID_STATE_TOTAL_CURR], |
| 516 | &uid_entry->io[UID_STATE_TOTAL_LAST], |
| 517 | &uid_entry->io[UID_STATE_DEAD_TASKS]); |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 518 | compute_io_uid_tasks(uid_entry); |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 521 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 522 | static int uid_io_show(struct seq_file *m, void *v) |
| 523 | { |
| 524 | struct uid_entry *uid_entry; |
| 525 | unsigned long bkt; |
| 526 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 527 | rt_mutex_lock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 528 | |
Jin Qian | 67f252c | 2017-04-13 17:07:58 -0700 | [diff] [blame] | 529 | update_io_stats_all_locked(); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 530 | |
| 531 | hash_for_each(hash_table, bkt, uid_entry, hash) { |
Jin Qian | 62eb9f9 | 2017-03-02 13:39:43 -0800 | [diff] [blame] | 532 | seq_printf(m, "%d %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
Yang Jin | 6e8f1e7 | 2017-07-26 12:52:22 -0700 | [diff] [blame] | 533 | 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 Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 548 | rt_mutex_unlock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static int uid_io_open(struct inode *inode, struct file *file) |
| 553 | { |
| 554 | return single_open(file, uid_io_show, PDE_DATA(inode)); |
| 555 | } |
| 556 | |
| 557 | static 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 | |
| 564 | static int uid_procstat_open(struct inode *inode, struct file *file) |
| 565 | { |
| 566 | return single_open(file, NULL, NULL); |
| 567 | } |
| 568 | |
| 569 | static 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 Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 592 | rt_mutex_lock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 593 | |
| 594 | uid_entry = find_or_register_uid(uid); |
Jin Qian | 8782d8f | 2017-01-17 17:26:07 -0800 | [diff] [blame] | 595 | if (!uid_entry) { |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 596 | rt_mutex_unlock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 597 | return -EINVAL; |
| 598 | } |
| 599 | |
Jin Qian | 8782d8f | 2017-01-17 17:26:07 -0800 | [diff] [blame] | 600 | if (uid_entry->state == state) { |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 601 | rt_mutex_unlock(&uid_lock); |
Jin Qian | 8782d8f | 2017-01-17 17:26:07 -0800 | [diff] [blame] | 602 | return count; |
| 603 | } |
| 604 | |
Jin Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 605 | update_io_stats_uid_locked(uid_entry); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 606 | |
| 607 | uid_entry->state = state; |
| 608 | |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 609 | rt_mutex_unlock(&uid_lock); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 610 | |
| 611 | return count; |
| 612 | } |
| 613 | |
| 614 | static const struct file_operations uid_procstat_fops = { |
| 615 | .open = uid_procstat_open, |
| 616 | .release = single_release, |
| 617 | .write = uid_procstat_write, |
| 618 | }; |
| 619 | |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 620 | static 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 Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 631 | rt_mutex_lock(&uid_lock); |
Amit Pundir | 48a9906 | 2015-04-15 00:40:21 +0530 | [diff] [blame] | 632 | uid = from_kuid_munged(current_user_ns(), task_uid(task)); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 633 | 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 Qian | cd5e19c | 2017-05-22 12:08:06 -0700 | [diff] [blame] | 643 | add_uid_io_stats(uid_entry, task, UID_STATE_DEAD_TASKS); |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 644 | |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 645 | exit: |
Wei Wang | 7f1ad08 | 2017-03-13 12:22:21 -0700 | [diff] [blame] | 646 | rt_mutex_unlock(&uid_lock); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 647 | return NOTIFY_OK; |
| 648 | } |
| 649 | |
| 650 | static struct notifier_block process_notifier_block = { |
| 651 | .notifier_call = process_notifier, |
| 652 | }; |
| 653 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 654 | static int __init proc_uid_sys_stats_init(void) |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 655 | { |
| 656 | hash_init(hash_table); |
| 657 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 658 | 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; |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 665 | 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); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 669 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 670 | 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); |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 689 | |
| 690 | profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block); |
| 691 | |
| 692 | return 0; |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 693 | |
| 694 | err: |
| 695 | remove_proc_subtree("uid_cputime", NULL); |
| 696 | remove_proc_subtree("uid_io", NULL); |
| 697 | remove_proc_subtree("uid_procstat", NULL); |
| 698 | return -ENOMEM; |
jinqian | 6901422 | 2015-03-11 10:44:50 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Jin Qian | 012f200 | 2017-01-10 16:10:35 -0800 | [diff] [blame] | 701 | early_initcall(proc_uid_sys_stats_init); |