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) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * tsacct.c - System accounting over taskstats interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) Jay Lan,	<jlan@sgi.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/sched/cputime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/tsacct_kern.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/acct.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * fill in basic accounting fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) void bacct_add_tsk(struct user_namespace *user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		   struct pid_namespace *pid_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		   struct taskstats *stats, struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	const struct cred *tcred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	u64 utime, stime, utimescaled, stimescaled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	u64 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	time64_t btime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	BUILD_BUG_ON(TS_COMM_LEN < TASK_COMM_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	/* calculate task elapsed time in nsec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	delta = ktime_get_ns() - tsk->start_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	/* Convert to micro seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	do_div(delta, NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	stats->ac_etime = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	/* Convert to seconds for btime (note y2106 limit) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	btime = ktime_get_real_seconds() - div_u64(delta, USEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	stats->ac_btime = clamp_t(time64_t, btime, 0, U32_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	stats->ac_btime64 = btime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (tsk->flags & PF_EXITING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		stats->ac_exitcode = tsk->exit_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (thread_group_leader(tsk) && (tsk->flags & PF_FORKNOEXEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		stats->ac_flag |= AFORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (tsk->flags & PF_SUPERPRIV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		stats->ac_flag |= ASU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (tsk->flags & PF_DUMPCORE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		stats->ac_flag |= ACORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (tsk->flags & PF_SIGNALED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		stats->ac_flag |= AXSIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	stats->ac_nice	 = task_nice(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	stats->ac_sched	 = tsk->policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	stats->ac_pid	 = task_pid_nr_ns(tsk, pid_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	tcred = __task_cred(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	stats->ac_uid	 = from_kuid_munged(user_ns, tcred->uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	stats->ac_gid	 = from_kgid_munged(user_ns, tcred->gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	stats->ac_ppid	 = pid_alive(tsk) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		task_tgid_nr_ns(rcu_dereference(tsk->real_parent), pid_ns) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	task_cputime(tsk, &utime, &stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	stats->ac_utime = div_u64(utime, NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	stats->ac_stime = div_u64(stime, NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	task_cputime_scaled(tsk, &utimescaled, &stimescaled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	stats->ac_utimescaled = div_u64(utimescaled, NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	stats->ac_stimescaled = div_u64(stimescaled, NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	stats->ac_minflt = tsk->min_flt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	stats->ac_majflt = tsk->maj_flt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	strncpy(stats->ac_comm, tsk->comm, sizeof(stats->ac_comm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #ifdef CONFIG_TASK_XACCT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #define KB 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define MB (1024*KB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define KB_MASK (~(KB-1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * fill in extended accounting fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct mm_struct *mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	/* convert pages-nsec/1024 to Mbyte-usec, see __acct_update_integrals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	stats->coremem = p->acct_rss_mem1 * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	do_div(stats->coremem, 1000 * KB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	do_div(stats->virtmem, 1000 * KB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	mm = get_task_mm(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (mm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		/* adjust to KB unit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		stats->hiwater_rss   = get_mm_hiwater_rss(mm) * PAGE_SIZE / KB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		stats->hiwater_vm    = get_mm_hiwater_vm(mm)  * PAGE_SIZE / KB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		mmput(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	stats->read_char	= p->ioac.rchar & KB_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	stats->write_char	= p->ioac.wchar & KB_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	stats->read_syscalls	= p->ioac.syscr & KB_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	stats->write_syscalls	= p->ioac.syscw & KB_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #ifdef CONFIG_TASK_IO_ACCOUNTING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	stats->read_bytes	= p->ioac.read_bytes & KB_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	stats->write_bytes	= p->ioac.write_bytes & KB_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	stats->cancelled_write_bytes = p->ioac.cancelled_write_bytes & KB_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	stats->read_bytes	= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	stats->write_bytes	= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	stats->cancelled_write_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #undef KB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #undef MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void __acct_update_integrals(struct task_struct *tsk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				    u64 utime, u64 stime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	u64 time, delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (!likely(tsk->mm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	time = stime + utime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	delta = time - tsk->acct_timexpd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (delta < TICK_NSEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	tsk->acct_timexpd = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	 * Divide by 1024 to avoid overflow, and to avoid division.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * The final unit reported to userspace is Mbyte-usecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 * the rest of the math is done in xacct_add_tsk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm) >> 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	tsk->acct_vm_mem1 += delta * tsk->mm->total_vm >> 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * acct_update_integrals - update mm integral fields in task_struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * @tsk: task_struct for accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) void acct_update_integrals(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	u64 utime, stime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	task_cputime(tsk, &utime, &stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	__acct_update_integrals(tsk, utime, stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * acct_account_cputime - update mm integral after cputime update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * @tsk: task_struct for accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) void acct_account_cputime(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	__acct_update_integrals(tsk, tsk->utime, tsk->stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * acct_clear_integrals - clear the mm integral fields in task_struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * @tsk: task_struct whose accounting fields are cleared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) void acct_clear_integrals(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	tsk->acct_timexpd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	tsk->acct_rss_mem1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	tsk->acct_vm_mem1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #endif