projects/linux·blob master · refs

678 lines · 16.8 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
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>
9#include <linux/gfp.h>
10#include <linux/proc_fs.h>
11#include <linux/seq_file.h>
12#include <linux/interrupt.h>
13#include <linux/kernel.h>
14#include <linux/kernel_stat.h>
15#include <linux/mutex.h>
16#include <linux/string.h>
17
18#include "internals.h"
19
20/*
21 * Access rules:
22 *
23 * procfs protects read/write of /proc/irq/N/ files against a
24 * concurrent free of the interrupt descriptor. remove_proc_entry()
25 * immediately prevents new read/writes to happen and waits for
26 * already running read/write functions to complete.
27 *
28 * We remove the proc entries first and then delete the interrupt
29 * descriptor from the radix tree and free it. So it is guaranteed
30 * that irq_to_desc(N) is valid as long as the read/writes are
31 * permitted by procfs.
32 *
33 * The read from /proc/interrupts is a different problem because there
34 * is no protection. So the lookup and the access to irqdesc
35 * information must be protected by sparse_irq_lock.
36 */
37static struct proc_dir_entry *root_irq_dir;
38
39#ifdef CONFIG_SMP
40
41enum {
42 AFFINITY,
43 AFFINITY_LIST,
44 EFFECTIVE,
45 EFFECTIVE_LIST,
46};
47
48static int show_irq_affinity(int type, struct seq_file *m)
49{
50 struct irq_desc *desc = irq_to_desc((long)m->private);
51 const struct cpumask *mask;
52
53 guard(raw_spinlock_irq)(&desc->lock);
54
55 switch (type) {
56 case AFFINITY:
57 case AFFINITY_LIST:
58 mask = desc->irq_common_data.affinity;
59 if (irq_move_pending(&desc->irq_data))
60 mask = irq_desc_get_pending_mask(desc);
61 break;
62 case EFFECTIVE:
63 case EFFECTIVE_LIST:
64#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
65 mask = irq_data_get_effective_affinity_mask(&desc->irq_data);
66 break;
67#endif
68 default:
69 return -EINVAL;
70 }
71
72 switch (type) {
73 case AFFINITY_LIST:
74 case EFFECTIVE_LIST:
75 seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
76 break;
77 case AFFINITY:
78 case EFFECTIVE:
79 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
80 break;
81 }
82 return 0;
83}
84
85static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
86{
87 struct irq_desc *desc = irq_to_desc((long)m->private);
88 cpumask_var_t mask;
89
90 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
91 return -ENOMEM;
92
93 scoped_guard(raw_spinlock_irq, &desc->lock) {
94 if (desc->affinity_hint)
95 cpumask_copy(mask, desc->affinity_hint);
96 }
97
98 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
99 free_cpumask_var(mask);
100 return 0;
101}
102
103int no_irq_affinity;
104static int irq_affinity_proc_show(struct seq_file *m, void *v)
105{
106 return show_irq_affinity(AFFINITY, m);
107}
108
109static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
110{
111 return show_irq_affinity(AFFINITY_LIST, m);
112}
113
114#ifndef CONFIG_AUTO_IRQ_AFFINITY
115static inline int irq_select_affinity_usr(unsigned int irq)
116{
117 /*
118 * If the interrupt is started up already then this fails. The
119 * interrupt is assigned to an online CPU already. There is no
120 * point to move it around randomly. Tell user space that the
121 * selected mask is bogus.
122 *
123 * If not then any change to the affinity is pointless because the
124 * startup code invokes irq_setup_affinity() which will select
125 * a online CPU anyway.
126 */
127 return -EINVAL;
128}
129#else
130/* ALPHA magic affinity auto selector. Keep it for historical reasons. */
131static inline int irq_select_affinity_usr(unsigned int irq)
132{
133 return irq_select_affinity(irq);
134}
135#endif
136
137static ssize_t write_irq_affinity(int type, struct file *file,
138 const char __user *buffer, size_t count, loff_t *pos)
139{
140 unsigned int irq = (int)(long)pde_data(file_inode(file));
141 cpumask_var_t new_value;
142 int err;
143
144 if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
145 return -EPERM;
146
147 if (!zalloc_cpumask_var(&new_value, GFP_KERNEL))
148 return -ENOMEM;
149
150 if (type)
151 err = cpumask_parselist_user(buffer, count, new_value);
152 else
153 err = cpumask_parse_user(buffer, count, new_value);
154 if (err)
155 goto free_cpumask;
156
157 /*
158 * Do not allow disabling IRQs completely - it's a too easy
159 * way to make the system unusable accidentally :-) At least
160 * one online CPU still has to be targeted.
161 */
162 if (!cpumask_intersects(new_value, cpu_online_mask)) {
163 /*
164 * Special case for empty set - allow the architecture code
165 * to set default SMP affinity.
166 */
167 err = irq_select_affinity_usr(irq) ? -EINVAL : count;
168 } else {
169 err = irq_set_affinity(irq, new_value);
170 if (!err)
171 err = count;
172 }
173
174free_cpumask:
175 free_cpumask_var(new_value);
176 return err;
177}
178
179static ssize_t irq_affinity_proc_write(struct file *file,
180 const char __user *buffer, size_t count, loff_t *pos)
181{
182 return write_irq_affinity(0, file, buffer, count, pos);
183}
184
185static ssize_t irq_affinity_list_proc_write(struct file *file,
186 const char __user *buffer, size_t count, loff_t *pos)
187{
188 return write_irq_affinity(1, file, buffer, count, pos);
189}
190
191static int irq_affinity_proc_open(struct inode *inode, struct file *file)
192{
193 return single_open(file, irq_affinity_proc_show, pde_data(inode));
194}
195
196static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
197{
198 return single_open(file, irq_affinity_list_proc_show, pde_data(inode));
199}
200
201static const struct proc_ops irq_affinity_proc_ops = {
202 .proc_open = irq_affinity_proc_open,
203 .proc_read = seq_read,
204 .proc_lseek = seq_lseek,
205 .proc_release = single_release,
206 .proc_write = irq_affinity_proc_write,
207};
208
209static const struct proc_ops irq_affinity_list_proc_ops = {
210 .proc_open = irq_affinity_list_proc_open,
211 .proc_read = seq_read,
212 .proc_lseek = seq_lseek,
213 .proc_release = single_release,
214 .proc_write = irq_affinity_list_proc_write,
215};
216
217#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
218static int irq_effective_aff_proc_show(struct seq_file *m, void *v)
219{
220 return show_irq_affinity(EFFECTIVE, m);
221}
222
223static int irq_effective_aff_list_proc_show(struct seq_file *m, void *v)
224{
225 return show_irq_affinity(EFFECTIVE_LIST, m);
226}
227#endif
228
229static int default_affinity_show(struct seq_file *m, void *v)
230{
231 seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
232 return 0;
233}
234
235static ssize_t default_affinity_write(struct file *file,
236 const char __user *buffer, size_t count, loff_t *ppos)
237{
238 cpumask_var_t new_value;
239 int err;
240
241 if (!zalloc_cpumask_var(&new_value, GFP_KERNEL))
242 return -ENOMEM;
243
244 err = cpumask_parse_user(buffer, count, new_value);
245 if (err)
246 goto out;
247
248 /*
249 * Do not allow disabling IRQs completely - it's a too easy
250 * way to make the system unusable accidentally :-) At least
251 * one online CPU still has to be targeted.
252 */
253 if (!cpumask_intersects(new_value, cpu_online_mask)) {
254 err = -EINVAL;
255 goto out;
256 }
257
258 cpumask_copy(irq_default_affinity, new_value);
259 err = count;
260
261out:
262 free_cpumask_var(new_value);
263 return err;
264}
265
266static int default_affinity_open(struct inode *inode, struct file *file)
267{
268 return single_open(file, default_affinity_show, pde_data(inode));
269}
270
271static const struct proc_ops default_affinity_proc_ops = {
272 .proc_open = default_affinity_open,
273 .proc_read = seq_read,
274 .proc_lseek = seq_lseek,
275 .proc_release = single_release,
276 .proc_write = default_affinity_write,
277};
278
279static int irq_node_proc_show(struct seq_file *m, void *v)
280{
281 struct irq_desc *desc = irq_to_desc((long) m->private);
282
283 seq_printf(m, "%d\n", irq_desc_get_node(desc));
284 return 0;
285}
286#endif
287
288static int irq_spurious_proc_show(struct seq_file *m, void *v)
289{
290 struct irq_desc *desc = irq_to_desc((long) m->private);
291
292 seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
293 desc->irq_count, desc->irqs_unhandled,
294 jiffies_to_msecs(desc->last_unhandled));
295 return 0;
296}
297
298#define MAX_NAMELEN 128
299
300static bool name_unique(unsigned int irq, struct irqaction *new_action)
301{
302 struct irq_desc *desc = irq_to_desc(irq);
303 struct irqaction *action;
304
305 guard(raw_spinlock_irq)(&desc->lock);
306 for_each_action_of_desc(desc, action) {
307 if ((action != new_action) && action->name &&
308 !strcmp(new_action->name, action->name))
309 return false;
310 }
311 return true;
312}
313
314void register_handler_proc(unsigned int irq, struct irqaction *action)
315{
316 char name[MAX_NAMELEN];
317 struct irq_desc *desc = irq_to_desc(irq);
318
319 if (!desc->dir || action->dir || !action->name || !name_unique(irq, action))
320 return;
321
322 strscpy(name, action->name);
323
324 /* create /proc/irq/1234/handler/ */
325 action->dir = proc_mkdir(name, desc->dir);
326}
327
328#undef MAX_NAMELEN
329
330#define MAX_NAMELEN 11
331
332void register_irq_proc(unsigned int irq, struct irq_desc *desc)
333{
334 static DEFINE_MUTEX(register_lock);
335 void __maybe_unused *irqp = (void *)(unsigned long) irq;
336 char name [MAX_NAMELEN];
337
338 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
339 return;
340
341 /*
342 * irq directories are registered only when a handler is
343 * added, not when the descriptor is created, so multiple
344 * tasks might try to register at the same time.
345 */
346 guard(mutex)(&register_lock);
347
348 if (desc->dir)
349 return;
350
351 /* create /proc/irq/1234 */
352 snprintf(name, MAX_NAMELEN, "%u", irq);
353 desc->dir = proc_mkdir(name, root_irq_dir);
354 if (!desc->dir)
355 return;
356
357#ifdef CONFIG_SMP
358 umode_t umode = S_IRUGO;
359
360 if (irq_can_set_affinity_usr(desc->irq_data.irq))
361 umode |= S_IWUSR;
362
363 /* create /proc/irq/<irq>/smp_affinity */
364 proc_create_data("smp_affinity", umode, desc->dir, &irq_affinity_proc_ops, irqp);
365
366 /* create /proc/irq/<irq>/affinity_hint */
367 proc_create_single_data("affinity_hint", 0444, desc->dir,
368 irq_affinity_hint_proc_show, irqp);
369
370 /* create /proc/irq/<irq>/smp_affinity_list */
371 proc_create_data("smp_affinity_list", umode, desc->dir,
372 &irq_affinity_list_proc_ops, irqp);
373
374 proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show, irqp);
375# ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
376 proc_create_single_data("effective_affinity", 0444, desc->dir,
377 irq_effective_aff_proc_show, irqp);
378 proc_create_single_data("effective_affinity_list", 0444, desc->dir,
379 irq_effective_aff_list_proc_show, irqp);
380# endif
381#endif
382 proc_create_single_data("spurious", 0444, desc->dir,
383 irq_spurious_proc_show, (void *)(long)irq);
384
385}
386
387void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
388{
389 char name [MAX_NAMELEN];
390
391 if (!root_irq_dir || !desc->dir)
392 return;
393#ifdef CONFIG_SMP
394 remove_proc_entry("smp_affinity", desc->dir);
395 remove_proc_entry("affinity_hint", desc->dir);
396 remove_proc_entry("smp_affinity_list", desc->dir);
397 remove_proc_entry("node", desc->dir);
398# ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
399 remove_proc_entry("effective_affinity", desc->dir);
400 remove_proc_entry("effective_affinity_list", desc->dir);
401# endif
402#endif
403 remove_proc_entry("spurious", desc->dir);
404
405 snprintf(name, MAX_NAMELEN, "%u", irq);
406 remove_proc_entry(name, root_irq_dir);
407}
408
409#undef MAX_NAMELEN
410
411void unregister_handler_proc(unsigned int irq, struct irqaction *action)
412{
413 proc_remove(action->dir);
414}
415
416static void register_default_affinity_proc(void)
417{
418#ifdef CONFIG_SMP
419 proc_create("irq/default_smp_affinity", 0644, NULL,
420 &default_affinity_proc_ops);
421#endif
422}
423
424void init_irq_proc(void)
425{
426 unsigned int irq;
427 struct irq_desc *desc;
428
429 /* create /proc/irq */
430 root_irq_dir = proc_mkdir("irq", NULL);
431 if (!root_irq_dir)
432 return;
433
434 register_default_affinity_proc();
435
436 /*
437 * Create entries for all existing IRQs.
438 */
439 for_each_irq_desc(irq, desc)
440 register_irq_proc(irq, desc);
441}
442
443void irq_proc_update_valid(struct irq_desc *desc)
444{
445 u32 set = _IRQ_PROC_VALID;
446
447 if (irq_settings_is_hidden(desc) || irq_desc_is_chained(desc) || !desc->action)
448 set = 0;
449
450 irq_settings_update_proc_valid(desc, set);
451}
452
453#ifdef CONFIG_GENERIC_IRQ_SHOW
454
455#define ARCH_PROC_IRQDESC ((void *)0x00001111)
456
457int __weak arch_show_interrupts(struct seq_file *p, int prec)
458{
459 return 0;
460}
461
462static DEFINE_RAW_SPINLOCK(irq_proc_constraints_lock);
463
464static struct irq_proc_constraints {
465 bool print_header;
466 unsigned int num_prec;
467 unsigned int chip_width;
468} irq_proc_constraints __read_mostly = {
469 .num_prec = 4,
470 .chip_width = 8,
471};
472
473#ifndef ACTUAL_NR_IRQS
474# define ACTUAL_NR_IRQS total_nr_irqs
475#endif
476
477void irq_proc_calc_prec(void)
478{
479 unsigned int prec, n;
480
481 for (prec = 4, n = 10000; prec < 10 && n <= total_nr_irqs; ++prec)
482 n *= 10;
483
484 guard(raw_spinlock_irqsave)(&irq_proc_constraints_lock);
485 if (prec > irq_proc_constraints.num_prec)
486 WRITE_ONCE(irq_proc_constraints.num_prec, prec);
487}
488
489void irq_proc_update_chip(const struct irq_chip *chip)
490{
491 unsigned int len = chip && chip->name ? strlen(chip->name) : 0;
492
493 if (!len || len <= READ_ONCE(irq_proc_constraints.chip_width))
494 return;
495
496 /* Can be invoked from interrupt disabled contexts */
497 guard(raw_spinlock_irqsave)(&irq_proc_constraints_lock);
498 if (len > irq_proc_constraints.chip_width)
499 WRITE_ONCE(irq_proc_constraints.chip_width, len);
500}
501
502/* Same as seq_put_decimal_ull_width(p, " ", cnt, 10) */
503#define ZSTR1 " 0"
504#define ZSTR1_LEN (sizeof(ZSTR1) - 1)
505#define ZSTR16 ZSTR1 ZSTR1 ZSTR1 ZSTR1 ZSTR1 ZSTR1 ZSTR1 ZSTR1 \
506 ZSTR1 ZSTR1 ZSTR1 ZSTR1 ZSTR1 ZSTR1 ZSTR1 ZSTR1
507#define ZSTR256 ZSTR16 ZSTR16 ZSTR16 ZSTR16 ZSTR16 ZSTR16 ZSTR16 ZSTR16 \
508 ZSTR16 ZSTR16 ZSTR16 ZSTR16 ZSTR16 ZSTR16 ZSTR16 ZSTR16
509
510static inline void irq_proc_emit_zero_counts(struct seq_file *p, unsigned int zeros)
511{
512 if (!zeros)
513 return;
514
515 for (unsigned int n = min(zeros, 256); n; zeros -= n, n = min(zeros, 256))
516 seq_write(p, ZSTR256, n * ZSTR1_LEN);
517}
518
519static inline unsigned int irq_proc_emit_count(struct seq_file *p, unsigned int cnt,
520 unsigned int zeros)
521{
522 if (!cnt)
523 return zeros + 1;
524
525 irq_proc_emit_zero_counts(p, zeros);
526 seq_put_decimal_ull_width(p, " ", cnt, 10);
527 return 0;
528}
529
530void irq_proc_emit_counts(struct seq_file *p, unsigned int __percpu *cnts)
531{
532 unsigned int cpu, zeros = 0;
533
534 for_each_online_cpu(cpu)
535 zeros = irq_proc_emit_count(p, per_cpu(*cnts, cpu), zeros);
536 irq_proc_emit_zero_counts(p, zeros);
537}
538
539static int irq_seq_show(struct seq_file *p, void *v)
540{
541 struct irq_proc_constraints *constr = p->private;
542 struct irq_desc *desc = v;
543 struct irqaction *action;
544
545 /* Print header for the first interrupt? */
546 if (constr->print_header) {
547 unsigned int cpu;
548
549 seq_printf(p, "%*s", constr->num_prec + 8, "");
550 for_each_online_cpu(cpu)
551 seq_printf(p, "CPU%-8d", cpu);
552 seq_putc(p, '\n');
553 constr->print_header = false;
554 }
555
556 if (desc == ARCH_PROC_IRQDESC)
557 return arch_show_interrupts(p, constr->num_prec);
558
559 seq_put_decimal_ull_width(p, "", irq_desc_get_irq(desc), constr->num_prec);
560 seq_putc(p, ':');
561
562 /*
563 * Always output per CPU interrupts. Output device interrupts only when
564 * desc::tot_count is not zero.
565 */
566 if (irq_settings_is_per_cpu(desc) || irq_settings_is_per_cpu_devid(desc) ||
567 data_race(desc->tot_count))
568 irq_proc_emit_counts(p, &desc->kstat_irqs->cnt);
569 else
570 irq_proc_emit_zero_counts(p, num_online_cpus());
571
572 /* Enforce a visual gap */
573 seq_write(p, " ", 2);
574
575 guard(raw_spinlock_irq)(&desc->lock);
576 if (desc->irq_data.chip) {
577 if (desc->irq_data.chip->irq_print_chip)
578 desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
579 else if (desc->irq_data.chip->name)
580 seq_printf(p, "%-*s", constr->chip_width, desc->irq_data.chip->name);
581 else
582 seq_printf(p, "%-*s", constr->chip_width, "-");
583 } else {
584 seq_printf(p, "%-*s", constr->chip_width, "None");
585 }
586
587 seq_putc(p, ' ');
588 if (desc->irq_data.domain)
589 seq_put_decimal_ull_width(p, "", desc->irq_data.hwirq, constr->num_prec);
590 else
591 seq_printf(p, " %*s", constr->num_prec, "");
592
593 if (IS_ENABLED(CONFIG_GENERIC_IRQ_SHOW_LEVEL))
594 seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
595
596 if (desc->name)
597 seq_printf(p, "-%-8s", desc->name);
598
599 action = desc->action;
600 if (action) {
601 seq_printf(p, " %s", action->name);
602 while ((action = action->next) != NULL)
603 seq_printf(p, ", %s", action->name);
604 }
605
606 seq_putc(p, '\n');
607 return 0;
608}
609
610static void *irq_seq_next_desc(loff_t *pos)
611{
612 if (*pos > total_nr_irqs)
613 return NULL;
614
615 guard(rcu)();
616 for (;;) {
617 struct irq_desc *desc = irq_find_desc_at_or_after((unsigned int) *pos);
618
619 if (desc) {
620 *pos = irq_desc_get_irq(desc);
621 /*
622 * If valid for output then try to acquire a reference
623 * count on the descriptor so that it can't be freed
624 * after dropping RCU read lock on return.
625 */
626 if (irq_settings_proc_valid(desc) && irq_desc_get_ref(desc))
627 return desc;
628 (*pos)++;
629 } else {
630 *pos = total_nr_irqs;
631 return ARCH_PROC_IRQDESC;
632 }
633 }
634}
635
636static void *irq_seq_start(struct seq_file *f, loff_t *pos)
637{
638 if (!*pos) {
639 struct irq_proc_constraints *constr = f->private;
640
641 constr->num_prec = READ_ONCE(irq_proc_constraints.num_prec);
642 constr->chip_width = READ_ONCE(irq_proc_constraints.chip_width);
643 constr->print_header = true;
644 }
645 return irq_seq_next_desc(pos);
646}
647
648static void *irq_seq_next(struct seq_file *f, void *v, loff_t *pos)
649{
650 if (v && v != ARCH_PROC_IRQDESC)
651 irq_desc_put_ref(v);
652
653 (*pos)++;
654 return irq_seq_next_desc(pos);
655}
656
657static void irq_seq_stop(struct seq_file *f, void *v)
658{
659 if (v && v != ARCH_PROC_IRQDESC)
660 irq_desc_put_ref(v);
661}
662
663static const struct seq_operations irq_seq_ops = {
664 .start = irq_seq_start,
665 .next = irq_seq_next,
666 .stop = irq_seq_stop,
667 .show = irq_seq_show,
668};
669
670static int __init irq_proc_init(void)
671{
672 proc_create_seq_private("interrupts", 0, NULL, &irq_seq_ops,
673 sizeof(irq_proc_constraints), NULL);
674 return 0;
675}
676fs_initcall(irq_proc_init);
677
678#endif