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) /* delayacct.c - per-task delay accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) Shailabh Nagar, IBM Corp. 2006
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/sched/cputime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/taskstats.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/delayacct.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) int delayacct_on __read_mostly = 1;	/* Delay accounting turned on/off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) EXPORT_SYMBOL_GPL(delayacct_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) struct kmem_cache *delayacct_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static int __init delayacct_setup_disable(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	delayacct_on = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) __setup("nodelayacct", delayacct_setup_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) void delayacct_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC|SLAB_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	delayacct_tsk_init(&init_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) void __delayacct_tsk_init(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (tsk->delays)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		raw_spin_lock_init(&tsk->delays->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * Finish delay accounting for a statistic using its timestamps (@start),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * accumalator (@total) and @count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static void delayacct_end(raw_spinlock_t *lock, u64 *start, u64 *total,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			  u32 *count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	s64 ns = ktime_get_ns() - *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (ns > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		raw_spin_lock_irqsave(lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		*total += ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		(*count)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		raw_spin_unlock_irqrestore(lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) void __delayacct_blkio_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	current->delays->blkio_start = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * We cannot rely on the `current` macro, as we haven't yet switched back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * the process being woken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) void __delayacct_blkio_end(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct task_delay_info *delays = p->delays;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	u64 *total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	u32 *count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (p->delays->flags & DELAYACCT_PF_SWAPIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		total = &delays->swapin_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		count = &delays->swapin_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		total = &delays->blkio_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		count = &delays->blkio_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	delayacct_end(&delays->lock, &delays->blkio_start, total, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	u64 utime, stime, stimescaled, utimescaled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	unsigned long long t2, t3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	unsigned long flags, t1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	s64 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	task_cputime(tsk, &utime, &stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	tmp = (s64)d->cpu_run_real_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	tmp += utime + stime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	d->cpu_run_real_total = (tmp < (s64)d->cpu_run_real_total) ? 0 : tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	task_cputime_scaled(tsk, &utimescaled, &stimescaled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	tmp = (s64)d->cpu_scaled_run_real_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	tmp += utimescaled + stimescaled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	d->cpu_scaled_run_real_total =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		(tmp < (s64)d->cpu_scaled_run_real_total) ? 0 : tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	 * No locking available for sched_info (and too expensive to add one)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * Mitigate by taking snapshot of values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	t1 = tsk->sched_info.pcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	t2 = tsk->sched_info.run_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	t3 = tsk->se.sum_exec_runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	d->cpu_count += t1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	tmp = (s64)d->cpu_delay_total + t2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	tmp = (s64)d->cpu_run_virtual_total + t3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	d->cpu_run_virtual_total =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		(tmp < (s64)d->cpu_run_virtual_total) ?	0 : tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	/* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	raw_spin_lock_irqsave(&tsk->delays->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	tmp = d->blkio_delay_total + tsk->delays->blkio_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	d->blkio_delay_total = (tmp < d->blkio_delay_total) ? 0 : tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	tmp = d->swapin_delay_total + tsk->delays->swapin_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	d->swapin_delay_total = (tmp < d->swapin_delay_total) ? 0 : tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	tmp = d->thrashing_delay_total + tsk->delays->thrashing_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	d->thrashing_delay_total = (tmp < d->thrashing_delay_total) ? 0 : tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	d->blkio_count += tsk->delays->blkio_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	d->swapin_count += tsk->delays->swapin_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	d->freepages_count += tsk->delays->freepages_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	d->thrashing_count += tsk->delays->thrashing_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) __u64 __delayacct_blkio_ticks(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	__u64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	raw_spin_lock_irqsave(&tsk->delays->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	ret = nsec_to_clock_t(tsk->delays->blkio_delay +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				tsk->delays->swapin_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return ret;
^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) void __delayacct_freepages_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	current->delays->freepages_start = ktime_get_ns();
^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) void __delayacct_freepages_end(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	delayacct_end(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		&current->delays->lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		&current->delays->freepages_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		&current->delays->freepages_delay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		&current->delays->freepages_count);
^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) void __delayacct_thrashing_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	current->delays->thrashing_start = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) void __delayacct_thrashing_end(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	delayacct_end(&current->delays->lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		      &current->delays->thrashing_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		      &current->delays->thrashing_delay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		      &current->delays->thrashing_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }