Merge tag 'probes-fixes-v6.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / kernel / irq / proc.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
1da177e4
LT
3 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
4 *
5 * This file contains the /proc/irq/ handling code.
6 */
7
8#include <linux/irq.h>
5a0e3ad6 9#include <linux/gfp.h>
1da177e4 10#include <linux/proc_fs.h>
f18e439d 11#include <linux/seq_file.h>
1da177e4 12#include <linux/interrupt.h>
c78b9b65 13#include <linux/kernel_stat.h>
95c2b175 14#include <linux/mutex.h>
1da177e4 15
97a41e26
AB
16#include "internals.h"
17
c291ee62
TG
18/*
19 * Access rules:
20 *
21 * procfs protects read/write of /proc/irq/N/ files against a
22 * concurrent free of the interrupt descriptor. remove_proc_entry()
23 * immediately prevents new read/writes to happen and waits for
24 * already running read/write functions to complete.
25 *
26 * We remove the proc entries first and then delete the interrupt
27 * descriptor from the radix tree and free it. So it is guaranteed
28 * that irq_to_desc(N) is valid as long as the read/writes are
29 * permitted by procfs.
30 *
31 * The read from /proc/interrupts is a different problem because there
32 * is no protection. So the lookup and the access to irqdesc
33 * information must be protected by sparse_irq_lock.
34 */
4a733ee1 35static struct proc_dir_entry *root_irq_dir;
1da177e4
LT
36
37#ifdef CONFIG_SMP
38
0d3f5425
TG
39enum {
40 AFFINITY,
41 AFFINITY_LIST,
42 EFFECTIVE,
43 EFFECTIVE_LIST,
44};
45
047dc633 46static int show_irq_affinity(int type, struct seq_file *m)
1da177e4 47{
08678b08 48 struct irq_desc *desc = irq_to_desc((long)m->private);
0d3f5425 49 const struct cpumask *mask;
42ee2b74 50
0d3f5425
TG
51 switch (type) {
52 case AFFINITY:
53 case AFFINITY_LIST:
54 mask = desc->irq_common_data.affinity;
9012f84e
JR
55 if (irq_move_pending(&desc->irq_data))
56 mask = irq_desc_get_pending_mask(desc);
0d3f5425
TG
57 break;
58 case EFFECTIVE:
59 case EFFECTIVE_LIST:
60#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
6bc6d4ab 61 mask = irq_data_get_effective_affinity_mask(&desc->irq_data);
0d3f5425 62 break;
0d3f5425 63#endif
b33394ba
TG
64 default:
65 return -EINVAL;
ce8bdd69 66 }
0d3f5425
TG
67
68 switch (type) {
69 case AFFINITY_LIST:
70 case EFFECTIVE_LIST:
c1d7f03f 71 seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
0d3f5425
TG
72 break;
73 case AFFINITY:
74 case EFFECTIVE:
c1d7f03f 75 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
0d3f5425
TG
76 break;
77 }
f18e439d 78 return 0;
1da177e4
LT
79}
80
e7a297b0
PWJ
81static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
82{
83 struct irq_desc *desc = irq_to_desc((long)m->private);
e7a297b0
PWJ
84 cpumask_var_t mask;
85
4308ad80 86 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
e7a297b0
PWJ
87 return -ENOMEM;
88
659ff9c9
TG
89 scoped_guard(raw_spinlock_irq, &desc->lock) {
90 if (desc->affinity_hint)
91 cpumask_copy(mask, desc->affinity_hint);
92 }
e7a297b0 93
c1d7f03f 94 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
e7a297b0 95 free_cpumask_var(mask);
e7a297b0
PWJ
96 return 0;
97}
98
1da177e4 99int no_irq_affinity;
4b060420
MT
100static int irq_affinity_proc_show(struct seq_file *m, void *v)
101{
0d3f5425 102 return show_irq_affinity(AFFINITY, m);
4b060420
MT
103}
104
105static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
106{
0d3f5425 107 return show_irq_affinity(AFFINITY_LIST, m);
4b060420
MT
108}
109
cba6437a
TG
110#ifndef CONFIG_AUTO_IRQ_AFFINITY
111static inline int irq_select_affinity_usr(unsigned int irq)
112{
113 /*
114 * If the interrupt is started up already then this fails. The
115 * interrupt is assigned to an online CPU already. There is no
116 * point to move it around randomly. Tell user space that the
117 * selected mask is bogus.
118 *
119 * If not then any change to the affinity is pointless because the
120 * startup code invokes irq_setup_affinity() which will select
121 * a online CPU anyway.
122 */
123 return -EINVAL;
124}
125#else
126/* ALPHA magic affinity auto selector. Keep it for historical reasons. */
127static inline int irq_select_affinity_usr(unsigned int irq)
128{
129 return irq_select_affinity(irq);
130}
131#endif
4b060420
MT
132
133static ssize_t write_irq_affinity(int type, struct file *file,
f18e439d 134 const char __user *buffer, size_t count, loff_t *pos)
1da177e4 135{
359745d7 136 unsigned int irq = (int)(long)pde_data(file_inode(file));
0de26520 137 cpumask_var_t new_value;
f18e439d 138 int err;
1da177e4 139
9c255583 140 if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
eb29369f 141 return -EPERM;
1da177e4 142
c5e3a411 143 if (!zalloc_cpumask_var(&new_value, GFP_KERNEL))
0de26520
RR
144 return -ENOMEM;
145
4b060420
MT
146 if (type)
147 err = cpumask_parselist_user(buffer, count, new_value);
148 else
149 err = cpumask_parse_user(buffer, count, new_value);
1da177e4 150 if (err)
0de26520 151 goto free_cpumask;
1da177e4
LT
152
153 /*
154 * Do not allow disabling IRQs completely - it's a too easy
155 * way to make the system unusable accidentally :-) At least
156 * one online CPU still has to be targeted.
157 */
0de26520 158 if (!cpumask_intersects(new_value, cpu_online_mask)) {
cba4235e
TG
159 /*
160 * Special case for empty set - allow the architecture code
161 * to set default SMP affinity.
162 */
163 err = irq_select_affinity_usr(irq) ? -EINVAL : count;
0de26520 164 } else {
6714796e
WY
165 err = irq_set_affinity(irq, new_value);
166 if (!err)
167 err = count;
0de26520
RR
168 }
169
170free_cpumask:
171 free_cpumask_var(new_value);
172 return err;
1da177e4
LT
173}
174
4b060420
MT
175static ssize_t irq_affinity_proc_write(struct file *file,
176 const char __user *buffer, size_t count, loff_t *pos)
177{
178 return write_irq_affinity(0, file, buffer, count, pos);
179}
180
181static ssize_t irq_affinity_list_proc_write(struct file *file,
182 const char __user *buffer, size_t count, loff_t *pos)
183{
184 return write_irq_affinity(1, file, buffer, count, pos);
185}
186
f18e439d 187static int irq_affinity_proc_open(struct inode *inode, struct file *file)
18404756 188{
359745d7 189 return single_open(file, irq_affinity_proc_show, pde_data(inode));
18404756
MK
190}
191
4b060420
MT
192static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
193{
359745d7 194 return single_open(file, irq_affinity_list_proc_show, pde_data(inode));
4b060420
MT
195}
196
97a32539
AD
197static const struct proc_ops irq_affinity_proc_ops = {
198 .proc_open = irq_affinity_proc_open,
199 .proc_read = seq_read,
200 .proc_lseek = seq_lseek,
201 .proc_release = single_release,
202 .proc_write = irq_affinity_proc_write,
f18e439d
AD
203};
204
97a32539
AD
205static const struct proc_ops irq_affinity_list_proc_ops = {
206 .proc_open = irq_affinity_list_proc_open,
207 .proc_read = seq_read,
208 .proc_lseek = seq_lseek,
209 .proc_release = single_release,
210 .proc_write = irq_affinity_list_proc_write,
4b060420
MT
211};
212
0d3f5425
TG
213#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
214static int irq_effective_aff_proc_show(struct seq_file *m, void *v)
215{
216 return show_irq_affinity(EFFECTIVE, m);
217}
218
219static int irq_effective_aff_list_proc_show(struct seq_file *m, void *v)
220{
221 return show_irq_affinity(EFFECTIVE_LIST, m);
222}
0d3f5425
TG
223#endif
224
f18e439d
AD
225static int default_affinity_show(struct seq_file *m, void *v)
226{
c1d7f03f 227 seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
f18e439d
AD
228 return 0;
229}
230
231static ssize_t default_affinity_write(struct file *file,
232 const char __user *buffer, size_t count, loff_t *ppos)
18404756 233{
d036e67b 234 cpumask_var_t new_value;
f18e439d 235 int err;
18404756 236
c5e3a411 237 if (!zalloc_cpumask_var(&new_value, GFP_KERNEL))
d036e67b
RR
238 return -ENOMEM;
239
240 err = cpumask_parse_user(buffer, count, new_value);
18404756 241 if (err)
d036e67b 242 goto out;
18404756 243
18404756
MK
244 /*
245 * Do not allow disabling IRQs completely - it's a too easy
246 * way to make the system unusable accidentally :-) At least
247 * one online CPU still has to be targeted.
248 */
d036e67b
RR
249 if (!cpumask_intersects(new_value, cpu_online_mask)) {
250 err = -EINVAL;
251 goto out;
252 }
18404756 253
d036e67b
RR
254 cpumask_copy(irq_default_affinity, new_value);
255 err = count;
18404756 256
d036e67b
RR
257out:
258 free_cpumask_var(new_value);
259 return err;
18404756 260}
f18e439d
AD
261
262static int default_affinity_open(struct inode *inode, struct file *file)
263{
359745d7 264 return single_open(file, default_affinity_show, pde_data(inode));
f18e439d
AD
265}
266
97a32539
AD
267static const struct proc_ops default_affinity_proc_ops = {
268 .proc_open = default_affinity_open,
269 .proc_read = seq_read,
270 .proc_lseek = seq_lseek,
271 .proc_release = single_release,
272 .proc_write = default_affinity_write,
f18e439d 273};
92d6b71a
DS
274
275static int irq_node_proc_show(struct seq_file *m, void *v)
276{
277 struct irq_desc *desc = irq_to_desc((long) m->private);
278
6783011b 279 seq_printf(m, "%d\n", irq_desc_get_node(desc));
92d6b71a
DS
280 return 0;
281}
1da177e4
LT
282#endif
283
a1afb637 284static int irq_spurious_proc_show(struct seq_file *m, void *v)
96d97cf0 285{
a1afb637
AD
286 struct irq_desc *desc = irq_to_desc((long) m->private);
287
288 seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
289 desc->irq_count, desc->irqs_unhandled,
290 jiffies_to_msecs(desc->last_unhandled));
291 return 0;
292}
293
1da177e4
LT
294#define MAX_NAMELEN 128
295
659ff9c9 296static bool name_unique(unsigned int irq, struct irqaction *new_action)
1da177e4 297{
08678b08 298 struct irq_desc *desc = irq_to_desc(irq);
1da177e4
LT
299 struct irqaction *action;
300
659ff9c9 301 guard(raw_spinlock_irq)(&desc->lock);
f944b5a7 302 for_each_action_of_desc(desc, action) {
1da177e4 303 if ((action != new_action) && action->name &&
659ff9c9
TG
304 !strcmp(new_action->name, action->name))
305 return false;
d2d9433a 306 }
659ff9c9 307 return true;
1da177e4
LT
308}
309
310void register_handler_proc(unsigned int irq, struct irqaction *action)
311{
47af06c9 312 char name[MAX_NAMELEN];
08678b08 313 struct irq_desc *desc = irq_to_desc(irq);
1da177e4 314
659ff9c9 315 if (!desc->dir || action->dir || !action->name || !name_unique(irq, action))
1da177e4
LT
316 return;
317
1da177e4
LT
318 snprintf(name, MAX_NAMELEN, "%s", action->name);
319
320 /* create /proc/irq/1234/handler/ */
08678b08 321 action->dir = proc_mkdir(name, desc->dir);
1da177e4
LT
322}
323
324#undef MAX_NAMELEN
325
326#define MAX_NAMELEN 10
327
2c6927a3 328void register_irq_proc(unsigned int irq, struct irq_desc *desc)
1da177e4 329{
95c2b175 330 static DEFINE_MUTEX(register_lock);
c1a80386 331 void __maybe_unused *irqp = (void *)(unsigned long) irq;
1da177e4
LT
332 char name [MAX_NAMELEN];
333
95c2b175 334 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
1da177e4
LT
335 return;
336
95c2b175
BH
337 /*
338 * irq directories are registered only when a handler is
339 * added, not when the descriptor is created, so multiple
340 * tasks might try to register at the same time.
341 */
659ff9c9 342 guard(mutex)(&register_lock);
95c2b175
BH
343
344 if (desc->dir)
659ff9c9 345 return;
1da177e4
LT
346
347 /* create /proc/irq/1234 */
47af06c9 348 sprintf(name, "%u", irq);
08678b08 349 desc->dir = proc_mkdir(name, root_irq_dir);
c82a43d4 350 if (!desc->dir)
659ff9c9 351 return;
1da177e4
LT
352
353#ifdef CONFIG_SMP
c7718e5c
JX
354 umode_t umode = S_IRUGO;
355
356 if (irq_can_set_affinity_usr(desc->irq_data.irq))
357 umode |= S_IWUSR;
358
f18e439d 359 /* create /proc/irq/<irq>/smp_affinity */
659ff9c9 360 proc_create_data("smp_affinity", umode, desc->dir, &irq_affinity_proc_ops, irqp);
92d6b71a 361
e7a297b0 362 /* create /proc/irq/<irq>/affinity_hint */
3f3942ac 363 proc_create_single_data("affinity_hint", 0444, desc->dir,
659ff9c9 364 irq_affinity_hint_proc_show, irqp);
e7a297b0 365
4b060420 366 /* create /proc/irq/<irq>/smp_affinity_list */
c7718e5c 367 proc_create_data("smp_affinity_list", umode, desc->dir,
97a32539 368 &irq_affinity_list_proc_ops, irqp);
4b060420 369
659ff9c9 370 proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show, irqp);
0d3f5425 371# ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
3f3942ac 372 proc_create_single_data("effective_affinity", 0444, desc->dir,
659ff9c9 373 irq_effective_aff_proc_show, irqp);
3f3942ac 374 proc_create_single_data("effective_affinity_list", 0444, desc->dir,
659ff9c9 375 irq_effective_aff_list_proc_show, irqp);
0d3f5425 376# endif
1da177e4 377#endif
3f3942ac 378 proc_create_single_data("spurious", 0444, desc->dir,
659ff9c9 379 irq_spurious_proc_show, (void *)(long)irq);
95c2b175 380
1da177e4
LT
381}
382
13bfe99e
TG
383void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
384{
385 char name [MAX_NAMELEN];
386
387 if (!root_irq_dir || !desc->dir)
388 return;
389#ifdef CONFIG_SMP
390 remove_proc_entry("smp_affinity", desc->dir);
391 remove_proc_entry("affinity_hint", desc->dir);
def945ee 392 remove_proc_entry("smp_affinity_list", desc->dir);
13bfe99e 393 remove_proc_entry("node", desc->dir);
0d3f5425
TG
394# ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
395 remove_proc_entry("effective_affinity", desc->dir);
396 remove_proc_entry("effective_affinity_list", desc->dir);
397# endif
13bfe99e
TG
398#endif
399 remove_proc_entry("spurious", desc->dir);
400
13bfe99e
TG
401 sprintf(name, "%u", irq);
402 remove_proc_entry(name, root_irq_dir);
403}
404
1da177e4
LT
405#undef MAX_NAMELEN
406
407void unregister_handler_proc(unsigned int irq, struct irqaction *action)
408{
a8ca16ea 409 proc_remove(action->dir);
1da177e4
LT
410}
411
3786fc71 412static void register_default_affinity_proc(void)
18404756
MK
413{
414#ifdef CONFIG_SMP
bab5c790 415 proc_create("irq/default_smp_affinity", 0644, NULL,
97a32539 416 &default_affinity_proc_ops);
18404756
MK
417#endif
418}
419
1da177e4
LT
420void init_irq_proc(void)
421{
2c6927a3
YL
422 unsigned int irq;
423 struct irq_desc *desc;
1da177e4
LT
424
425 /* create /proc/irq */
426 root_irq_dir = proc_mkdir("irq", NULL);
427 if (!root_irq_dir)
428 return;
429
18404756
MK
430 register_default_affinity_proc();
431
1da177e4
LT
432 /*
433 * Create entries for all existing IRQs.
434 */
fe3464ca 435 for_each_irq_desc(irq, desc)
2c6927a3 436 register_irq_proc(irq, desc);
1da177e4
LT
437}
438
c78b9b65
TG
439#ifdef CONFIG_GENERIC_IRQ_SHOW
440
441int __weak arch_show_interrupts(struct seq_file *p, int prec)
442{
443 return 0;
444}
445
a6e120ed 446#ifndef ACTUAL_NR_IRQS
1ad2048b 447# define ACTUAL_NR_IRQS irq_get_nr_irqs()
a6e120ed
TG
448#endif
449
c78b9b65
TG
450int show_interrupts(struct seq_file *p, void *v)
451{
1ad2048b 452 const unsigned int nr_irqs = irq_get_nr_irqs();
c78b9b65
TG
453 static int prec;
454
c78b9b65
TG
455 int i = *(loff_t *) v, j;
456 struct irqaction *action;
457 struct irq_desc *desc;
458
a6e120ed 459 if (i > ACTUAL_NR_IRQS)
c78b9b65
TG
460 return 0;
461
a6e120ed 462 if (i == ACTUAL_NR_IRQS)
c78b9b65
TG
463 return arch_show_interrupts(p, prec);
464
465 /* print header and calculate the width of the first column */
466 if (i == 0) {
467 for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
468 j *= 10;
469
470 seq_printf(p, "%*s", prec + 8, "");
471 for_each_online_cpu(j)
472 seq_printf(p, "CPU%-8d", j);
473 seq_putc(p, '\n');
474 }
475
659ff9c9 476 guard(rcu)();
c78b9b65 477 desc = irq_to_desc(i);
83cfac95 478 if (!desc || irq_settings_is_hidden(desc))
659ff9c9 479 return 0;
c78b9b65 480
54058877 481 if (!desc->action || irq_desc_is_chained(desc) || !desc->kstat_irqs)
659ff9c9 482 return 0;
c78b9b65 483
f9ed1f7c
DW
484 seq_printf(p, "%*d:", prec, i);
485 for_each_online_cpu(j) {
486 unsigned int cnt = desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0;
487
488 seq_put_decimal_ull_width(p, " ", cnt, 10);
489 }
9d9f204b 490 seq_putc(p, ' ');
ab7798ff 491
659ff9c9 492 guard(raw_spinlock_irq)(&desc->lock);
ab7798ff
TG
493 if (desc->irq_data.chip) {
494 if (desc->irq_data.chip->irq_print_chip)
495 desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
496 else if (desc->irq_data.chip->name)
9d9f204b 497 seq_printf(p, "%8s", desc->irq_data.chip->name);
ab7798ff 498 else
9d9f204b 499 seq_printf(p, "%8s", "-");
ab7798ff 500 } else {
9d9f204b 501 seq_printf(p, "%8s", "None");
ab7798ff 502 }
c12d2f42 503 if (desc->irq_data.domain)
d92df42d 504 seq_printf(p, " %*lu", prec, desc->irq_data.hwirq);
f435da41
HS
505 else
506 seq_printf(p, " %*s", prec, "");
94b2c363 507#ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
ab7798ff
TG
508 seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
509#endif
ee0401ec
TG
510 if (desc->name)
511 seq_printf(p, "-%-8s", desc->name);
c78b9b65 512
74bdf781 513 action = desc->action;
c78b9b65
TG
514 if (action) {
515 seq_printf(p, " %s", action->name);
516 while ((action = action->next) != NULL)
517 seq_printf(p, ", %s", action->name);
518 }
519
520 seq_putc(p, '\n');
c78b9b65
TG
521 return 0;
522}
523#endif