]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/delayacct.c
cfg80211: hold bss_lock while updating nontrans_list
[mirror_ubuntu-jammy-kernel.git] / kernel / delayacct.c
CommitLineData
7170066e 1// SPDX-License-Identifier: GPL-2.0-or-later
ca74e92b
SN
2/* delayacct.c - per-task delay accounting
3 *
4 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
ca74e92b
SN
5 */
6
7#include <linux/sched.h>
9164bb4a 8#include <linux/sched/task.h>
32ef5517 9#include <linux/sched/cputime.h>
4b7a08a0 10#include <linux/sched/clock.h>
ca74e92b 11#include <linux/slab.h>
6952b61d 12#include <linux/taskstats.h>
ca74e92b
SN
13#include <linux/sysctl.h>
14#include <linux/delayacct.h>
c9aaa895 15#include <linux/module.h>
ca74e92b 16
e4042ad4
PZ
17DEFINE_STATIC_KEY_FALSE(delayacct_key);
18int delayacct_on __read_mostly; /* Delay accounting turned on/off */
e18b890b 19struct kmem_cache *delayacct_cache;
ca74e92b 20
0cd7c741
PZ
21static void set_delayacct(bool enabled)
22{
23 if (enabled) {
24 static_branch_enable(&delayacct_key);
25 delayacct_on = 1;
26 } else {
27 delayacct_on = 0;
28 static_branch_disable(&delayacct_key);
29 }
30}
31
e4042ad4 32static int __init delayacct_setup_enable(char *str)
ca74e92b 33{
e4042ad4 34 delayacct_on = 1;
ca74e92b
SN
35 return 1;
36}
e4042ad4 37__setup("delayacct", delayacct_setup_enable);
ca74e92b
SN
38
39void delayacct_init(void)
40{
5d097056 41 delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC|SLAB_ACCOUNT);
ca74e92b 42 delayacct_tsk_init(&init_task);
0cd7c741
PZ
43 set_delayacct(delayacct_on);
44}
45
46#ifdef CONFIG_PROC_SYSCTL
47int sysctl_delayacct(struct ctl_table *table, int write, void *buffer,
48 size_t *lenp, loff_t *ppos)
49{
50 int state = delayacct_on;
51 struct ctl_table t;
52 int err;
53
54 if (write && !capable(CAP_SYS_ADMIN))
55 return -EPERM;
56
57 t = *table;
58 t.data = &state;
59 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
60 if (err < 0)
61 return err;
62 if (write)
63 set_delayacct(state);
64 return err;
ca74e92b 65}
0cd7c741 66#endif
ca74e92b
SN
67
68void __delayacct_tsk_init(struct task_struct *tsk)
69{
e94b1766 70 tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL);
ca74e92b 71 if (tsk->delays)
02acc80d 72 raw_spin_lock_init(&tsk->delays->lock);
ca74e92b
SN
73}
74
ca74e92b 75/*
9667a23d
TG
76 * Finish delay accounting for a statistic using its timestamps (@start),
77 * accumalator (@total) and @count
ca74e92b 78 */
4b7a08a0 79static void delayacct_end(raw_spinlock_t *lock, u64 *start, u64 *total, u32 *count)
ca74e92b 80{
4b7a08a0 81 s64 ns = local_clock() - *start;
64efade1 82 unsigned long flags;
ca74e92b 83
9667a23d 84 if (ns > 0) {
02acc80d 85 raw_spin_lock_irqsave(lock, flags);
9667a23d
TG
86 *total += ns;
87 (*count)++;
02acc80d 88 raw_spin_unlock_irqrestore(lock, flags);
9667a23d 89 }
ca74e92b
SN
90}
91
0ff92245
SN
92void __delayacct_blkio_start(void)
93{
4b7a08a0 94 current->delays->blkio_start = local_clock();
0ff92245
SN
95}
96
c96f5471
JS
97/*
98 * We cannot rely on the `current` macro, as we haven't yet switched back to
99 * the process being woken.
100 */
101void __delayacct_blkio_end(struct task_struct *p)
0ff92245 102{
c96f5471
JS
103 struct task_delay_info *delays = p->delays;
104 u64 *total;
105 u32 *count;
106
107 if (p->delays->flags & DELAYACCT_PF_SWAPIN) {
108 total = &delays->swapin_delay;
109 count = &delays->swapin_count;
110 } else {
111 total = &delays->blkio_delay;
112 count = &delays->blkio_count;
113 }
114
115 delayacct_end(&delays->lock, &delays->blkio_start, total, count);
0ff92245 116}
6f44993f 117
e4042ad4 118int delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
6f44993f 119{
dbf3da1c 120 u64 utime, stime, stimescaled, utimescaled;
68f6783d
TG
121 unsigned long long t2, t3;
122 unsigned long flags, t1;
123 s64 tmp;
6f44993f 124
dbf3da1c 125 task_cputime(tsk, &utime, &stime);
68f6783d 126 tmp = (s64)d->cpu_run_real_total;
dbf3da1c 127 tmp += utime + stime;
6f44993f
SN
128 d->cpu_run_real_total = (tmp < (s64)d->cpu_run_real_total) ? 0 : tmp;
129
dbf3da1c 130 task_cputime_scaled(tsk, &utimescaled, &stimescaled);
68f6783d 131 tmp = (s64)d->cpu_scaled_run_real_total;
dbf3da1c 132 tmp += utimescaled + stimescaled;
c66f08be
MN
133 d->cpu_scaled_run_real_total =
134 (tmp < (s64)d->cpu_scaled_run_real_total) ? 0 : tmp;
135
6f44993f
SN
136 /*
137 * No locking available for sched_info (and too expensive to add one)
138 * Mitigate by taking snapshot of values
139 */
2d72376b 140 t1 = tsk->sched_info.pcount;
6f44993f 141 t2 = tsk->sched_info.run_delay;
9c2c4802 142 t3 = tsk->se.sum_exec_runtime;
6f44993f
SN
143
144 d->cpu_count += t1;
145
172ba844 146 tmp = (s64)d->cpu_delay_total + t2;
6f44993f
SN
147 d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
148
172ba844 149 tmp = (s64)d->cpu_run_virtual_total + t3;
6f44993f
SN
150 d->cpu_run_virtual_total =
151 (tmp < (s64)d->cpu_run_virtual_total) ? 0 : tmp;
152
e4042ad4
PZ
153 if (!tsk->delays)
154 return 0;
155
6f44993f
SN
156 /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
157
02acc80d 158 raw_spin_lock_irqsave(&tsk->delays->lock, flags);
6f44993f
SN
159 tmp = d->blkio_delay_total + tsk->delays->blkio_delay;
160 d->blkio_delay_total = (tmp < d->blkio_delay_total) ? 0 : tmp;
161 tmp = d->swapin_delay_total + tsk->delays->swapin_delay;
162 d->swapin_delay_total = (tmp < d->swapin_delay_total) ? 0 : tmp;
016ae219
KK
163 tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
164 d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
b1d29ba8
JW
165 tmp = d->thrashing_delay_total + tsk->delays->thrashing_delay;
166 d->thrashing_delay_total = (tmp < d->thrashing_delay_total) ? 0 : tmp;
6f44993f
SN
167 d->blkio_count += tsk->delays->blkio_count;
168 d->swapin_count += tsk->delays->swapin_count;
016ae219 169 d->freepages_count += tsk->delays->freepages_count;
b1d29ba8 170 d->thrashing_count += tsk->delays->thrashing_count;
02acc80d 171 raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);
6f44993f 172
6f44993f
SN
173 return 0;
174}
25890454
SN
175
176__u64 __delayacct_blkio_ticks(struct task_struct *tsk)
177{
178 __u64 ret;
64efade1 179 unsigned long flags;
25890454 180
02acc80d 181 raw_spin_lock_irqsave(&tsk->delays->lock, flags);
25890454
SN
182 ret = nsec_to_clock_t(tsk->delays->blkio_delay +
183 tsk->delays->swapin_delay);
02acc80d 184 raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);
25890454
SN
185 return ret;
186}
187
873b4771
KK
188void __delayacct_freepages_start(void)
189{
4b7a08a0 190 current->delays->freepages_start = local_clock();
873b4771
KK
191}
192
193void __delayacct_freepages_end(void)
194{
4b7a08a0
PZ
195 delayacct_end(&current->delays->lock,
196 &current->delays->freepages_start,
197 &current->delays->freepages_delay,
198 &current->delays->freepages_count);
873b4771
KK
199}
200
b1d29ba8
JW
201void __delayacct_thrashing_start(void)
202{
4b7a08a0 203 current->delays->thrashing_start = local_clock();
b1d29ba8
JW
204}
205
206void __delayacct_thrashing_end(void)
207{
208 delayacct_end(&current->delays->lock,
209 &current->delays->thrashing_start,
210 &current->delays->thrashing_delay,
211 &current->delays->thrashing_count);
212}