uid_sys_stats.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /* drivers/misc/uid_sys_stats.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. #include <linux/atomic.h>
  16. #include <linux/err.h>
  17. #include <linux/hashtable.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/list.h>
  21. #include <linux/mm.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/profile.h>
  24. #include <linux/rtmutex.h>
  25. #include <linux/sched/cputime.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/slab.h>
  28. #include <linux/uaccess.h>
  29. #define UID_HASH_BITS 10
  30. DECLARE_HASHTABLE(hash_table, UID_HASH_BITS);
  31. static DEFINE_RT_MUTEX(uid_lock);
  32. static struct proc_dir_entry *cpu_parent;
  33. static struct proc_dir_entry *io_parent;
  34. static struct proc_dir_entry *proc_parent;
  35. struct io_stats {
  36. u64 read_bytes;
  37. u64 write_bytes;
  38. u64 rchar;
  39. u64 wchar;
  40. u64 fsync;
  41. };
  42. #define UID_STATE_FOREGROUND 0
  43. #define UID_STATE_BACKGROUND 1
  44. #define UID_STATE_BUCKET_SIZE 2
  45. #define UID_STATE_TOTAL_CURR 2
  46. #define UID_STATE_TOTAL_LAST 3
  47. #define UID_STATE_DEAD_TASKS 4
  48. #define UID_STATE_SIZE 5
  49. #define MAX_TASK_COMM_LEN 256
  50. struct task_entry {
  51. char comm[MAX_TASK_COMM_LEN];
  52. pid_t pid;
  53. struct io_stats io[UID_STATE_SIZE];
  54. struct hlist_node hash;
  55. };
  56. struct uid_entry {
  57. uid_t uid;
  58. u64 utime;
  59. u64 stime;
  60. u64 active_utime;
  61. u64 active_stime;
  62. int state;
  63. struct io_stats io[UID_STATE_SIZE];
  64. struct hlist_node hash;
  65. #ifdef CONFIG_UID_SYS_STATS_DEBUG
  66. DECLARE_HASHTABLE(task_entries, UID_HASH_BITS);
  67. #endif
  68. };
  69. static u64 compute_write_bytes(struct task_struct *task)
  70. {
  71. if (task->ioac.write_bytes <= task->ioac.cancelled_write_bytes)
  72. return 0;
  73. return task->ioac.write_bytes - task->ioac.cancelled_write_bytes;
  74. }
  75. static void compute_io_bucket_stats(struct io_stats *io_bucket,
  76. struct io_stats *io_curr,
  77. struct io_stats *io_last,
  78. struct io_stats *io_dead)
  79. {
  80. /* tasks could switch to another uid group, but its io_last in the
  81. * previous uid group could still be positive.
  82. * therefore before each update, do an overflow check first
  83. */
  84. int64_t delta;
  85. delta = io_curr->read_bytes + io_dead->read_bytes -
  86. io_last->read_bytes;
  87. io_bucket->read_bytes += delta > 0 ? delta : 0;
  88. delta = io_curr->write_bytes + io_dead->write_bytes -
  89. io_last->write_bytes;
  90. io_bucket->write_bytes += delta > 0 ? delta : 0;
  91. delta = io_curr->rchar + io_dead->rchar - io_last->rchar;
  92. io_bucket->rchar += delta > 0 ? delta : 0;
  93. delta = io_curr->wchar + io_dead->wchar - io_last->wchar;
  94. io_bucket->wchar += delta > 0 ? delta : 0;
  95. delta = io_curr->fsync + io_dead->fsync - io_last->fsync;
  96. io_bucket->fsync += delta > 0 ? delta : 0;
  97. io_last->read_bytes = io_curr->read_bytes;
  98. io_last->write_bytes = io_curr->write_bytes;
  99. io_last->rchar = io_curr->rchar;
  100. io_last->wchar = io_curr->wchar;
  101. io_last->fsync = io_curr->fsync;
  102. memset(io_dead, 0, sizeof(struct io_stats));
  103. }
  104. #ifdef CONFIG_UID_SYS_STATS_DEBUG
  105. static void get_full_task_comm(struct task_entry *task_entry,
  106. struct task_struct *task)
  107. {
  108. int i = 0, offset = 0, len = 0;
  109. /* save one byte for terminating null character */
  110. int unused_len = MAX_TASK_COMM_LEN - TASK_COMM_LEN - 1;
  111. char buf[MAX_TASK_COMM_LEN - TASK_COMM_LEN - 1];
  112. struct mm_struct *mm = task->mm;
  113. /* fill the first TASK_COMM_LEN bytes with thread name */
  114. __get_task_comm(task_entry->comm, TASK_COMM_LEN, task);
  115. i = strlen(task_entry->comm);
  116. while (i < TASK_COMM_LEN)
  117. task_entry->comm[i++] = ' ';
  118. /* next the executable file name */
  119. if (mm) {
  120. mmap_write_lock(mm);
  121. if (mm->exe_file) {
  122. char *pathname = d_path(&mm->exe_file->f_path, buf,
  123. unused_len);
  124. if (!IS_ERR(pathname)) {
  125. len = strlcpy(task_entry->comm + i, pathname,
  126. unused_len);
  127. i += len;
  128. task_entry->comm[i++] = ' ';
  129. unused_len--;
  130. }
  131. }
  132. mmap_write_unlock(mm);
  133. }
  134. unused_len -= len;
  135. /* fill the rest with command line argument
  136. * replace each null or new line character
  137. * between args in argv with whitespace */
  138. len = get_cmdline(task, buf, unused_len);
  139. while (offset < len) {
  140. if (buf[offset] != '\0' && buf[offset] != '\n')
  141. task_entry->comm[i++] = buf[offset];
  142. else
  143. task_entry->comm[i++] = ' ';
  144. offset++;
  145. }
  146. /* get rid of trailing whitespaces in case when arg is memset to
  147. * zero before being reset in userspace
  148. */
  149. while (task_entry->comm[i-1] == ' ')
  150. i--;
  151. task_entry->comm[i] = '\0';
  152. }
  153. static struct task_entry *find_task_entry(struct uid_entry *uid_entry,
  154. struct task_struct *task)
  155. {
  156. struct task_entry *task_entry;
  157. hash_for_each_possible(uid_entry->task_entries, task_entry, hash,
  158. task->pid) {
  159. if (task->pid == task_entry->pid) {
  160. /* if thread name changed, update the entire command */
  161. int len = strnchr(task_entry->comm, ' ', TASK_COMM_LEN)
  162. - task_entry->comm;
  163. if (strncmp(task_entry->comm, task->comm, len))
  164. get_full_task_comm(task_entry, task);
  165. return task_entry;
  166. }
  167. }
  168. return NULL;
  169. }
  170. static struct task_entry *find_or_register_task(struct uid_entry *uid_entry,
  171. struct task_struct *task)
  172. {
  173. struct task_entry *task_entry;
  174. pid_t pid = task->pid;
  175. task_entry = find_task_entry(uid_entry, task);
  176. if (task_entry)
  177. return task_entry;
  178. task_entry = kzalloc(sizeof(struct task_entry), GFP_ATOMIC);
  179. if (!task_entry)
  180. return NULL;
  181. get_full_task_comm(task_entry, task);
  182. task_entry->pid = pid;
  183. hash_add(uid_entry->task_entries, &task_entry->hash, (unsigned int)pid);
  184. return task_entry;
  185. }
  186. static void remove_uid_tasks(struct uid_entry *uid_entry)
  187. {
  188. struct task_entry *task_entry;
  189. unsigned long bkt_task;
  190. struct hlist_node *tmp_task;
  191. hash_for_each_safe(uid_entry->task_entries, bkt_task,
  192. tmp_task, task_entry, hash) {
  193. hash_del(&task_entry->hash);
  194. kfree(task_entry);
  195. }
  196. }
  197. static void set_io_uid_tasks_zero(struct uid_entry *uid_entry)
  198. {
  199. struct task_entry *task_entry;
  200. unsigned long bkt_task;
  201. hash_for_each(uid_entry->task_entries, bkt_task, task_entry, hash) {
  202. memset(&task_entry->io[UID_STATE_TOTAL_CURR], 0,
  203. sizeof(struct io_stats));
  204. }
  205. }
  206. static void add_uid_tasks_io_stats(struct uid_entry *uid_entry,
  207. struct task_struct *task, int slot)
  208. {
  209. struct task_entry *task_entry = find_or_register_task(uid_entry, task);
  210. struct io_stats *task_io_slot = &task_entry->io[slot];
  211. task_io_slot->read_bytes += task->ioac.read_bytes;
  212. task_io_slot->write_bytes += compute_write_bytes(task);
  213. task_io_slot->rchar += task->ioac.rchar;
  214. task_io_slot->wchar += task->ioac.wchar;
  215. task_io_slot->fsync += task->ioac.syscfs;
  216. }
  217. static void compute_io_uid_tasks(struct uid_entry *uid_entry)
  218. {
  219. struct task_entry *task_entry;
  220. unsigned long bkt_task;
  221. hash_for_each(uid_entry->task_entries, bkt_task, task_entry, hash) {
  222. compute_io_bucket_stats(&task_entry->io[uid_entry->state],
  223. &task_entry->io[UID_STATE_TOTAL_CURR],
  224. &task_entry->io[UID_STATE_TOTAL_LAST],
  225. &task_entry->io[UID_STATE_DEAD_TASKS]);
  226. }
  227. }
  228. static void show_io_uid_tasks(struct seq_file *m, struct uid_entry *uid_entry)
  229. {
  230. struct task_entry *task_entry;
  231. unsigned long bkt_task;
  232. hash_for_each(uid_entry->task_entries, bkt_task, task_entry, hash) {
  233. /* Separated by comma because space exists in task comm */
  234. seq_printf(m, "task,%s,%lu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu\n",
  235. task_entry->comm,
  236. (unsigned long)task_entry->pid,
  237. task_entry->io[UID_STATE_FOREGROUND].rchar,
  238. task_entry->io[UID_STATE_FOREGROUND].wchar,
  239. task_entry->io[UID_STATE_FOREGROUND].read_bytes,
  240. task_entry->io[UID_STATE_FOREGROUND].write_bytes,
  241. task_entry->io[UID_STATE_BACKGROUND].rchar,
  242. task_entry->io[UID_STATE_BACKGROUND].wchar,
  243. task_entry->io[UID_STATE_BACKGROUND].read_bytes,
  244. task_entry->io[UID_STATE_BACKGROUND].write_bytes,
  245. task_entry->io[UID_STATE_FOREGROUND].fsync,
  246. task_entry->io[UID_STATE_BACKGROUND].fsync);
  247. }
  248. }
  249. #else
  250. static void remove_uid_tasks(struct uid_entry *uid_entry) {};
  251. static void set_io_uid_tasks_zero(struct uid_entry *uid_entry) {};
  252. static void add_uid_tasks_io_stats(struct uid_entry *uid_entry,
  253. struct task_struct *task, int slot) {};
  254. static void compute_io_uid_tasks(struct uid_entry *uid_entry) {};
  255. static void show_io_uid_tasks(struct seq_file *m,
  256. struct uid_entry *uid_entry) {}
  257. #endif
  258. static struct uid_entry *find_uid_entry(uid_t uid)
  259. {
  260. struct uid_entry *uid_entry;
  261. hash_for_each_possible(hash_table, uid_entry, hash, uid) {
  262. if (uid_entry->uid == uid)
  263. return uid_entry;
  264. }
  265. return NULL;
  266. }
  267. static struct uid_entry *find_or_register_uid(uid_t uid)
  268. {
  269. struct uid_entry *uid_entry;
  270. uid_entry = find_uid_entry(uid);
  271. if (uid_entry)
  272. return uid_entry;
  273. uid_entry = kzalloc(sizeof(struct uid_entry), GFP_ATOMIC);
  274. if (!uid_entry)
  275. return NULL;
  276. uid_entry->uid = uid;
  277. #ifdef CONFIG_UID_SYS_STATS_DEBUG
  278. hash_init(uid_entry->task_entries);
  279. #endif
  280. hash_add(hash_table, &uid_entry->hash, uid);
  281. return uid_entry;
  282. }
  283. static int uid_cputime_show(struct seq_file *m, void *v)
  284. {
  285. struct uid_entry *uid_entry = NULL;
  286. struct task_struct *task, *temp;
  287. struct user_namespace *user_ns = current_user_ns();
  288. u64 utime;
  289. u64 stime;
  290. unsigned long bkt;
  291. uid_t uid;
  292. rt_mutex_lock(&uid_lock);
  293. hash_for_each(hash_table, bkt, uid_entry, hash) {
  294. uid_entry->active_stime = 0;
  295. uid_entry->active_utime = 0;
  296. }
  297. rcu_read_lock();
  298. do_each_thread(temp, task) {
  299. uid = from_kuid_munged(user_ns, task_uid(task));
  300. if (!uid_entry || uid_entry->uid != uid)
  301. uid_entry = find_or_register_uid(uid);
  302. if (!uid_entry) {
  303. rcu_read_unlock();
  304. rt_mutex_unlock(&uid_lock);
  305. pr_err("%s: failed to find the uid_entry for uid %d\n",
  306. __func__, uid);
  307. return -ENOMEM;
  308. }
  309. /* avoid double accounting of dying threads */
  310. if (!(task->flags & PF_EXITING)) {
  311. task_cputime_adjusted(task, &utime, &stime);
  312. uid_entry->active_utime += utime;
  313. uid_entry->active_stime += stime;
  314. }
  315. } while_each_thread(temp, task);
  316. rcu_read_unlock();
  317. hash_for_each(hash_table, bkt, uid_entry, hash) {
  318. u64 total_utime = uid_entry->utime +
  319. uid_entry->active_utime;
  320. u64 total_stime = uid_entry->stime +
  321. uid_entry->active_stime;
  322. seq_printf(m, "%d: %llu %llu\n", uid_entry->uid,
  323. ktime_to_us(total_utime), ktime_to_us(total_stime));
  324. }
  325. rt_mutex_unlock(&uid_lock);
  326. return 0;
  327. }
  328. static int uid_cputime_open(struct inode *inode, struct file *file)
  329. {
  330. return single_open(file, uid_cputime_show, PDE_DATA(inode));
  331. }
  332. static const struct proc_ops uid_cputime_fops = {
  333. .proc_open = uid_cputime_open,
  334. .proc_read = seq_read,
  335. .proc_lseek = seq_lseek,
  336. .proc_release = single_release,
  337. };
  338. static int uid_remove_open(struct inode *inode, struct file *file)
  339. {
  340. return single_open(file, NULL, NULL);
  341. }
  342. static ssize_t uid_remove_write(struct file *file,
  343. const char __user *buffer, size_t count, loff_t *ppos)
  344. {
  345. struct uid_entry *uid_entry;
  346. struct hlist_node *tmp;
  347. char uids[128];
  348. char *start_uid, *end_uid = NULL;
  349. long int uid_start = 0, uid_end = 0;
  350. if (count >= sizeof(uids))
  351. count = sizeof(uids) - 1;
  352. if (copy_from_user(uids, buffer, count))
  353. return -EFAULT;
  354. uids[count] = '\0';
  355. end_uid = uids;
  356. start_uid = strsep(&end_uid, "-");
  357. if (!start_uid || !end_uid)
  358. return -EINVAL;
  359. if (kstrtol(start_uid, 10, &uid_start) != 0 ||
  360. kstrtol(end_uid, 10, &uid_end) != 0) {
  361. return -EINVAL;
  362. }
  363. rt_mutex_lock(&uid_lock);
  364. for (; uid_start <= uid_end; uid_start++) {
  365. hash_for_each_possible_safe(hash_table, uid_entry, tmp,
  366. hash, (uid_t)uid_start) {
  367. if (uid_start == uid_entry->uid) {
  368. remove_uid_tasks(uid_entry);
  369. hash_del(&uid_entry->hash);
  370. kfree(uid_entry);
  371. }
  372. }
  373. }
  374. rt_mutex_unlock(&uid_lock);
  375. return count;
  376. }
  377. static const struct proc_ops uid_remove_fops = {
  378. .proc_open = uid_remove_open,
  379. .proc_release = single_release,
  380. .proc_write = uid_remove_write,
  381. };
  382. static void add_uid_io_stats(struct uid_entry *uid_entry,
  383. struct task_struct *task, int slot)
  384. {
  385. struct io_stats *io_slot = &uid_entry->io[slot];
  386. /* avoid double accounting of dying threads */
  387. if (slot != UID_STATE_DEAD_TASKS && (task->flags & PF_EXITING))
  388. return;
  389. io_slot->read_bytes += task->ioac.read_bytes;
  390. io_slot->write_bytes += compute_write_bytes(task);
  391. io_slot->rchar += task->ioac.rchar;
  392. io_slot->wchar += task->ioac.wchar;
  393. io_slot->fsync += task->ioac.syscfs;
  394. add_uid_tasks_io_stats(uid_entry, task, slot);
  395. }
  396. static void update_io_stats_all_locked(void)
  397. {
  398. struct uid_entry *uid_entry = NULL;
  399. struct task_struct *task, *temp;
  400. struct user_namespace *user_ns = current_user_ns();
  401. unsigned long bkt;
  402. uid_t uid;
  403. hash_for_each(hash_table, bkt, uid_entry, hash) {
  404. memset(&uid_entry->io[UID_STATE_TOTAL_CURR], 0,
  405. sizeof(struct io_stats));
  406. set_io_uid_tasks_zero(uid_entry);
  407. }
  408. rcu_read_lock();
  409. do_each_thread(temp, task) {
  410. uid = from_kuid_munged(user_ns, task_uid(task));
  411. if (!uid_entry || uid_entry->uid != uid)
  412. uid_entry = find_or_register_uid(uid);
  413. if (!uid_entry)
  414. continue;
  415. add_uid_io_stats(uid_entry, task, UID_STATE_TOTAL_CURR);
  416. } while_each_thread(temp, task);
  417. rcu_read_unlock();
  418. hash_for_each(hash_table, bkt, uid_entry, hash) {
  419. compute_io_bucket_stats(&uid_entry->io[uid_entry->state],
  420. &uid_entry->io[UID_STATE_TOTAL_CURR],
  421. &uid_entry->io[UID_STATE_TOTAL_LAST],
  422. &uid_entry->io[UID_STATE_DEAD_TASKS]);
  423. compute_io_uid_tasks(uid_entry);
  424. }
  425. }
  426. static void update_io_stats_uid_locked(struct uid_entry *uid_entry)
  427. {
  428. struct task_struct *task, *temp;
  429. struct user_namespace *user_ns = current_user_ns();
  430. memset(&uid_entry->io[UID_STATE_TOTAL_CURR], 0,
  431. sizeof(struct io_stats));
  432. set_io_uid_tasks_zero(uid_entry);
  433. rcu_read_lock();
  434. do_each_thread(temp, task) {
  435. if (from_kuid_munged(user_ns, task_uid(task)) != uid_entry->uid)
  436. continue;
  437. add_uid_io_stats(uid_entry, task, UID_STATE_TOTAL_CURR);
  438. } while_each_thread(temp, task);
  439. rcu_read_unlock();
  440. compute_io_bucket_stats(&uid_entry->io[uid_entry->state],
  441. &uid_entry->io[UID_STATE_TOTAL_CURR],
  442. &uid_entry->io[UID_STATE_TOTAL_LAST],
  443. &uid_entry->io[UID_STATE_DEAD_TASKS]);
  444. compute_io_uid_tasks(uid_entry);
  445. }
  446. static int uid_io_show(struct seq_file *m, void *v)
  447. {
  448. struct uid_entry *uid_entry;
  449. unsigned long bkt;
  450. rt_mutex_lock(&uid_lock);
  451. update_io_stats_all_locked();
  452. hash_for_each(hash_table, bkt, uid_entry, hash) {
  453. seq_printf(m, "%d %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
  454. uid_entry->uid,
  455. uid_entry->io[UID_STATE_FOREGROUND].rchar,
  456. uid_entry->io[UID_STATE_FOREGROUND].wchar,
  457. uid_entry->io[UID_STATE_FOREGROUND].read_bytes,
  458. uid_entry->io[UID_STATE_FOREGROUND].write_bytes,
  459. uid_entry->io[UID_STATE_BACKGROUND].rchar,
  460. uid_entry->io[UID_STATE_BACKGROUND].wchar,
  461. uid_entry->io[UID_STATE_BACKGROUND].read_bytes,
  462. uid_entry->io[UID_STATE_BACKGROUND].write_bytes,
  463. uid_entry->io[UID_STATE_FOREGROUND].fsync,
  464. uid_entry->io[UID_STATE_BACKGROUND].fsync);
  465. show_io_uid_tasks(m, uid_entry);
  466. }
  467. rt_mutex_unlock(&uid_lock);
  468. return 0;
  469. }
  470. static int uid_io_open(struct inode *inode, struct file *file)
  471. {
  472. return single_open(file, uid_io_show, PDE_DATA(inode));
  473. }
  474. static const struct proc_ops uid_io_fops = {
  475. .proc_open = uid_io_open,
  476. .proc_read = seq_read,
  477. .proc_lseek = seq_lseek,
  478. .proc_release = single_release,
  479. };
  480. static int uid_procstat_open(struct inode *inode, struct file *file)
  481. {
  482. return single_open(file, NULL, NULL);
  483. }
  484. static ssize_t uid_procstat_write(struct file *file,
  485. const char __user *buffer, size_t count, loff_t *ppos)
  486. {
  487. struct uid_entry *uid_entry;
  488. uid_t uid;
  489. int argc, state;
  490. char input[128];
  491. if (count >= sizeof(input))
  492. return -EINVAL;
  493. if (copy_from_user(input, buffer, count))
  494. return -EFAULT;
  495. input[count] = '\0';
  496. argc = sscanf(input, "%u %d", &uid, &state);
  497. if (argc != 2)
  498. return -EINVAL;
  499. if (state != UID_STATE_BACKGROUND && state != UID_STATE_FOREGROUND)
  500. return -EINVAL;
  501. rt_mutex_lock(&uid_lock);
  502. uid_entry = find_or_register_uid(uid);
  503. if (!uid_entry) {
  504. rt_mutex_unlock(&uid_lock);
  505. return -EINVAL;
  506. }
  507. if (uid_entry->state == state) {
  508. rt_mutex_unlock(&uid_lock);
  509. return count;
  510. }
  511. update_io_stats_uid_locked(uid_entry);
  512. uid_entry->state = state;
  513. rt_mutex_unlock(&uid_lock);
  514. return count;
  515. }
  516. static const struct proc_ops uid_procstat_fops = {
  517. .proc_open = uid_procstat_open,
  518. .proc_release = single_release,
  519. .proc_write = uid_procstat_write,
  520. };
  521. static int process_notifier(struct notifier_block *self,
  522. unsigned long cmd, void *v)
  523. {
  524. struct task_struct *task = v;
  525. struct uid_entry *uid_entry;
  526. u64 utime, stime;
  527. uid_t uid;
  528. if (!task)
  529. return NOTIFY_OK;
  530. rt_mutex_lock(&uid_lock);
  531. uid = from_kuid_munged(current_user_ns(), task_uid(task));
  532. uid_entry = find_or_register_uid(uid);
  533. if (!uid_entry) {
  534. pr_err("%s: failed to find uid %d\n", __func__, uid);
  535. goto exit;
  536. }
  537. task_cputime_adjusted(task, &utime, &stime);
  538. uid_entry->utime += utime;
  539. uid_entry->stime += stime;
  540. add_uid_io_stats(uid_entry, task, UID_STATE_DEAD_TASKS);
  541. exit:
  542. rt_mutex_unlock(&uid_lock);
  543. return NOTIFY_OK;
  544. }
  545. static struct notifier_block process_notifier_block = {
  546. .notifier_call = process_notifier,
  547. };
  548. static int __init proc_uid_sys_stats_init(void)
  549. {
  550. hash_init(hash_table);
  551. cpu_parent = proc_mkdir("uid_cputime", NULL);
  552. if (!cpu_parent) {
  553. pr_err("%s: failed to create uid_cputime proc entry\n",
  554. __func__);
  555. goto err;
  556. }
  557. proc_create_data("remove_uid_range", 0222, cpu_parent,
  558. &uid_remove_fops, NULL);
  559. proc_create_data("show_uid_stat", 0444, cpu_parent,
  560. &uid_cputime_fops, NULL);
  561. io_parent = proc_mkdir("uid_io", NULL);
  562. if (!io_parent) {
  563. pr_err("%s: failed to create uid_io proc entry\n",
  564. __func__);
  565. goto err;
  566. }
  567. proc_create_data("stats", 0444, io_parent,
  568. &uid_io_fops, NULL);
  569. proc_parent = proc_mkdir("uid_procstat", NULL);
  570. if (!proc_parent) {
  571. pr_err("%s: failed to create uid_procstat proc entry\n",
  572. __func__);
  573. goto err;
  574. }
  575. proc_create_data("set", 0222, proc_parent,
  576. &uid_procstat_fops, NULL);
  577. profile_event_register(PROFILE_TASK_EXIT, &process_notifier_block);
  578. return 0;
  579. err:
  580. remove_proc_subtree("uid_cputime", NULL);
  581. remove_proc_subtree("uid_io", NULL);
  582. remove_proc_subtree("uid_procstat", NULL);
  583. return -ENOMEM;
  584. }
  585. early_initcall(proc_uid_sys_stats_init);