Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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