blob: a43eeee30e8e12ae97aecce07e12f846240b6f67 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* drivers/cpufreq/cpufreq_times.c
2 *
3 * Copyright (C) 2018 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
16#include <linux/cpufreq.h>
17#include <linux/cpufreq_times.h>
18#include <linux/hashtable.h>
19#include <linux/init.h>
20#include <linux/jiffies.h>
21#include <linux/proc_fs.h>
22#include <linux/sched.h>
23#include <linux/seq_file.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
26#include <linux/threads.h>
27
28#define UID_HASH_BITS 10
29
30static DECLARE_HASHTABLE(uid_hash_table, UID_HASH_BITS);
31
32static DEFINE_SPINLOCK(task_time_in_state_lock); /* task->time_in_state */
33static DEFINE_SPINLOCK(uid_lock); /* uid_hash_table */
34
35struct uid_entry {
36 uid_t uid;
37 unsigned int max_state;
38 struct hlist_node hash;
39 struct rcu_head rcu;
40 u64 time_in_state[0];
41};
42
43/**
44 * struct cpu_freqs - per-cpu frequency information
45 * @offset: start of these freqs' stats in task time_in_state array
46 * @max_state: number of entries in freq_table
47 * @last_index: index in freq_table of last frequency switched to
48 * @freq_table: list of available frequencies
49 */
50struct cpu_freqs {
51 unsigned int offset;
52 unsigned int max_state;
53 unsigned int last_index;
54 unsigned int freq_table[0];
55};
56
57static struct cpu_freqs *all_freqs[NR_CPUS];
58
59static unsigned int next_offset;
60
61
62/* Caller must hold rcu_read_lock() */
63static struct uid_entry *find_uid_entry_rcu(uid_t uid)
64{
65 struct uid_entry *uid_entry;
66
67 hash_for_each_possible_rcu(uid_hash_table, uid_entry, hash, uid) {
68 if (uid_entry->uid == uid)
69 return uid_entry;
70 }
71 return NULL;
72}
73
74/* Caller must hold uid lock */
75static struct uid_entry *find_uid_entry_locked(uid_t uid)
76{
77 struct uid_entry *uid_entry;
78
79 hash_for_each_possible(uid_hash_table, uid_entry, hash, uid) {
80 if (uid_entry->uid == uid)
81 return uid_entry;
82 }
83 return NULL;
84}
85
86/* Caller must hold uid lock */
87static struct uid_entry *find_or_register_uid_locked(uid_t uid)
88{
89 struct uid_entry *uid_entry, *temp;
90 unsigned int max_state = READ_ONCE(next_offset);
91 size_t alloc_size = sizeof(*uid_entry) + max_state *
92 sizeof(uid_entry->time_in_state[0]);
93
94 uid_entry = find_uid_entry_locked(uid);
95 if (uid_entry) {
96 if (uid_entry->max_state == max_state)
97 return uid_entry;
98 /* uid_entry->time_in_state is too small to track all freqs, so
99 * expand it.
100 */
101 temp = __krealloc(uid_entry, alloc_size, GFP_ATOMIC);
102 if (!temp)
103 return uid_entry;
104 temp->max_state = max_state;
105 memset(temp->time_in_state + uid_entry->max_state, 0,
106 (max_state - uid_entry->max_state) *
107 sizeof(uid_entry->time_in_state[0]));
108 if (temp != uid_entry) {
109 hlist_replace_rcu(&uid_entry->hash, &temp->hash);
110 kfree_rcu(uid_entry, rcu);
111 }
112 return temp;
113 }
114
115 uid_entry = kzalloc(alloc_size, GFP_ATOMIC);
116 if (!uid_entry)
117 return NULL;
118
119 uid_entry->uid = uid;
120 uid_entry->max_state = max_state;
121
122 hash_add_rcu(uid_hash_table, &uid_entry->hash, uid);
123
124 return uid_entry;
125}
126
127static bool freq_index_invalid(unsigned int index)
128{
129 unsigned int cpu;
130 struct cpu_freqs *freqs;
131
132 for_each_possible_cpu(cpu) {
133 freqs = all_freqs[cpu];
134 if (!freqs || index < freqs->offset ||
135 freqs->offset + freqs->max_state <= index)
136 continue;
137 return freqs->freq_table[index - freqs->offset] ==
138 CPUFREQ_ENTRY_INVALID;
139 }
140 return true;
141}
142
143static int single_uid_time_in_state_show(struct seq_file *m, void *ptr)
144{
145 struct uid_entry *uid_entry;
146 unsigned int i;
147 u64 time;
148 uid_t uid = from_kuid_munged(current_user_ns(), *(kuid_t *)m->private);
149
150 if (uid == overflowuid)
151 return -EINVAL;
152
153 rcu_read_lock();
154
155 uid_entry = find_uid_entry_rcu(uid);
156 if (!uid_entry) {
157 rcu_read_unlock();
158 return 0;
159 }
160
161 for (i = 0; i < uid_entry->max_state; ++i) {
162 if (freq_index_invalid(i))
163 continue;
164 time = nsec_to_clock_t(uid_entry->time_in_state[i]);
165 seq_write(m, &time, sizeof(time));
166 }
167
168 rcu_read_unlock();
169
170 return 0;
171}
172
173static void *uid_seq_start(struct seq_file *seq, loff_t *pos)
174{
175 if (*pos >= HASH_SIZE(uid_hash_table))
176 return NULL;
177
178 return &uid_hash_table[*pos];
179}
180
181static void *uid_seq_next(struct seq_file *seq, void *v, loff_t *pos)
182{
183 (*pos)++;
184
185 if (*pos >= HASH_SIZE(uid_hash_table))
186 return NULL;
187
188 return &uid_hash_table[*pos];
189}
190
191static void uid_seq_stop(struct seq_file *seq, void *v) { }
192
193static int uid_time_in_state_seq_show(struct seq_file *m, void *v)
194{
195 struct uid_entry *uid_entry;
196 struct cpu_freqs *freqs, *last_freqs = NULL;
197 int i, cpu;
198
199 if (v == uid_hash_table) {
200 seq_puts(m, "uid:");
201 for_each_possible_cpu(cpu) {
202 freqs = all_freqs[cpu];
203 if (!freqs || freqs == last_freqs)
204 continue;
205 last_freqs = freqs;
206 for (i = 0; i < freqs->max_state; i++) {
207 if (freqs->freq_table[i] ==
208 CPUFREQ_ENTRY_INVALID)
209 continue;
210 seq_printf(m, " %d", freqs->freq_table[i]);
211 }
212 }
213 seq_putc(m, '\n');
214 }
215
216 rcu_read_lock();
217
218 hlist_for_each_entry_rcu(uid_entry, (struct hlist_head *)v, hash) {
219 if (uid_entry->max_state)
220 seq_printf(m, "%d:", uid_entry->uid);
221 for (i = 0; i < uid_entry->max_state; ++i) {
222 if (freq_index_invalid(i))
223 continue;
224 seq_printf(m, " %lu", (unsigned long)nsec_to_clock_t(
225 uid_entry->time_in_state[i]));
226 }
227 if (uid_entry->max_state)
228 seq_putc(m, '\n');
229 }
230
231 rcu_read_unlock();
232 return 0;
233}
234
235void cpufreq_task_times_init(struct task_struct *p)
236{
237 unsigned long flags;
238
239 spin_lock_irqsave(&task_time_in_state_lock, flags);
240 p->time_in_state = NULL;
241 spin_unlock_irqrestore(&task_time_in_state_lock, flags);
242 p->max_state = 0;
243}
244
245void cpufreq_task_times_alloc(struct task_struct *p)
246{
247 void *temp;
248 unsigned long flags;
249 unsigned int max_state = READ_ONCE(next_offset);
250
251 /* We use one array to avoid multiple allocs per task */
252 temp = kcalloc(max_state, sizeof(p->time_in_state[0]), GFP_ATOMIC);
253 if (!temp)
254 return;
255
256 spin_lock_irqsave(&task_time_in_state_lock, flags);
257 p->time_in_state = temp;
258 spin_unlock_irqrestore(&task_time_in_state_lock, flags);
259 p->max_state = max_state;
260}
261
262/* Caller must hold task_time_in_state_lock */
263static int cpufreq_task_times_realloc_locked(struct task_struct *p)
264{
265 void *temp;
266 unsigned int max_state = READ_ONCE(next_offset);
267
268 temp = krealloc(p->time_in_state, max_state * sizeof(u64), GFP_ATOMIC);
269 if (!temp)
270 return -ENOMEM;
271 p->time_in_state = temp;
272 memset(p->time_in_state + p->max_state, 0,
273 (max_state - p->max_state) * sizeof(u64));
274 p->max_state = max_state;
275 return 0;
276}
277
278void cpufreq_task_times_exit(struct task_struct *p)
279{
280 unsigned long flags;
281 void *temp;
282
283 if (!p->time_in_state)
284 return;
285
286 spin_lock_irqsave(&task_time_in_state_lock, flags);
287 temp = p->time_in_state;
288 p->time_in_state = NULL;
289 spin_unlock_irqrestore(&task_time_in_state_lock, flags);
290 kfree(temp);
291}
292
293int proc_time_in_state_show(struct seq_file *m, struct pid_namespace *ns,
294 struct pid *pid, struct task_struct *p)
295{
296 unsigned int cpu, i;
297 u64 cputime;
298 unsigned long flags;
299 struct cpu_freqs *freqs;
300 struct cpu_freqs *last_freqs = NULL;
301
302 spin_lock_irqsave(&task_time_in_state_lock, flags);
303 for_each_possible_cpu(cpu) {
304 freqs = all_freqs[cpu];
305 if (!freqs || freqs == last_freqs)
306 continue;
307 last_freqs = freqs;
308
309 seq_printf(m, "cpu%u\n", cpu);
310 for (i = 0; i < freqs->max_state; i++) {
311 if (freqs->freq_table[i] == CPUFREQ_ENTRY_INVALID)
312 continue;
313 cputime = 0;
314 if (freqs->offset + i < p->max_state &&
315 p->time_in_state)
316 cputime = p->time_in_state[freqs->offset + i];
317 seq_printf(m, "%u %lu\n", freqs->freq_table[i],
318 (unsigned long)nsec_to_clock_t(cputime));
319 }
320 }
321 spin_unlock_irqrestore(&task_time_in_state_lock, flags);
322 return 0;
323}
324
325void cpufreq_acct_update_power(struct task_struct *p, u64 cputime)
326{
327 unsigned long flags;
328 unsigned int state;
329 struct uid_entry *uid_entry;
330 struct cpu_freqs *freqs = all_freqs[task_cpu(p)];
331 uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
332
333 if (!freqs || p->flags & PF_EXITING)
334 return;
335
336 state = freqs->offset + READ_ONCE(freqs->last_index);
337
338 spin_lock_irqsave(&task_time_in_state_lock, flags);
339 if ((state < p->max_state || !cpufreq_task_times_realloc_locked(p)) &&
340 p->time_in_state)
341 p->time_in_state[state] += cputime;
342 spin_unlock_irqrestore(&task_time_in_state_lock, flags);
343
344 spin_lock_irqsave(&uid_lock, flags);
345 uid_entry = find_or_register_uid_locked(uid);
346 if (uid_entry && state < uid_entry->max_state)
347 uid_entry->time_in_state[state] += cputime;
348 spin_unlock_irqrestore(&uid_lock, flags);
349}
350
351void cpufreq_times_create_policy(struct cpufreq_policy *policy)
352{
353 int cpu, index;
354 unsigned int count = 0;
355 struct cpufreq_frequency_table *pos, *table;
356 struct cpu_freqs *freqs;
357 void *tmp;
358
359 if (all_freqs[policy->cpu])
360 return;
361
362 table = policy->freq_table;
363 if (!table)
364 return;
365
366 cpufreq_for_each_entry(pos, table)
367 count++;
368
369 tmp = kzalloc(sizeof(*freqs) + sizeof(freqs->freq_table[0]) * count,
370 GFP_KERNEL);
371 if (!tmp)
372 return;
373
374 freqs = tmp;
375 freqs->max_state = count;
376
377 index = cpufreq_frequency_table_get_index(policy, policy->cur);
378 if (index >= 0)
379 WRITE_ONCE(freqs->last_index, index);
380
381 cpufreq_for_each_entry(pos, table)
382 freqs->freq_table[pos - table] = pos->frequency;
383
384 freqs->offset = next_offset;
385 WRITE_ONCE(next_offset, freqs->offset + count);
386 for_each_cpu(cpu, policy->related_cpus)
387 all_freqs[cpu] = freqs;
388}
389
390void cpufreq_task_times_remove_uids(uid_t uid_start, uid_t uid_end)
391{
392 struct uid_entry *uid_entry;
393 struct hlist_node *tmp;
394 unsigned long flags;
395
396 spin_lock_irqsave(&uid_lock, flags);
397
398 for (; uid_start <= uid_end; uid_start++) {
399 hash_for_each_possible_safe(uid_hash_table, uid_entry, tmp,
400 hash, uid_start) {
401 if (uid_start == uid_entry->uid) {
402 hash_del_rcu(&uid_entry->hash);
403 kfree_rcu(uid_entry, rcu);
404 }
405 }
406 }
407
408 spin_unlock_irqrestore(&uid_lock, flags);
409}
410
411void cpufreq_times_record_transition(struct cpufreq_freqs *freq)
412{
413 int index;
414 struct cpu_freqs *freqs = all_freqs[freq->cpu];
415 struct cpufreq_policy *policy;
416
417 if (!freqs)
418 return;
419
420 policy = cpufreq_cpu_get(freq->cpu);
421 if (!policy)
422 return;
423
424 index = cpufreq_frequency_table_get_index(policy, freq->new);
425 if (index >= 0)
426 WRITE_ONCE(freqs->last_index, index);
427
428 cpufreq_cpu_put(policy);
429}
430
431static const struct seq_operations uid_time_in_state_seq_ops = {
432 .start = uid_seq_start,
433 .next = uid_seq_next,
434 .stop = uid_seq_stop,
435 .show = uid_time_in_state_seq_show,
436};
437
438static int uid_time_in_state_open(struct inode *inode, struct file *file)
439{
440 return seq_open(file, &uid_time_in_state_seq_ops);
441}
442
443int single_uid_time_in_state_open(struct inode *inode, struct file *file)
444{
445 return single_open(file, single_uid_time_in_state_show,
446 &(inode->i_uid));
447}
448
449static const struct file_operations uid_time_in_state_fops = {
450 .open = uid_time_in_state_open,
451 .read = seq_read,
452 .llseek = seq_lseek,
453 .release = seq_release,
454};
455
456static int __init cpufreq_times_init(void)
457{
458 proc_create_data("uid_time_in_state", 0444, NULL,
459 &uid_time_in_state_fops, NULL);
460
461 return 0;
462}
463
464early_initcall(cpufreq_times_init);