uid_sys_stats.c 18 KB

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