1 // SPDX-License-Identifier: GPL-2.0
3 * linux/kernel/irq/proc.c
5 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
7 * This file contains the /proc/irq/ handling code.
10 #include <linux/irq.h>
11 #include <linux/gfp.h>
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/mutex.h>
18 #include "internals.h"
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.
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.
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.
37 static struct proc_dir_entry *root_irq_dir;
48 static int show_irq_affinity(int type, struct seq_file *m)
50 struct irq_desc *desc = irq_to_desc((long)m->private);
51 const struct cpumask *mask;
56 mask = desc->irq_common_data.affinity;
57 #ifdef CONFIG_GENERIC_PENDING_IRQ
58 if (irqd_is_setaffinity_pending(&desc->irq_data))
59 mask = desc->pending_mask;
64 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
65 mask = irq_data_get_effective_affinity_mask(&desc->irq_data);
75 seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
79 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
85 static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
87 struct irq_desc *desc = irq_to_desc((long)m->private);
91 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
94 raw_spin_lock_irqsave(&desc->lock, flags);
95 if (desc->affinity_hint)
96 cpumask_copy(mask, desc->affinity_hint);
97 raw_spin_unlock_irqrestore(&desc->lock, flags);
99 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
100 free_cpumask_var(mask);
105 #ifndef is_affinity_mask_valid
106 #define is_affinity_mask_valid(val) 1
110 static int irq_affinity_proc_show(struct seq_file *m, void *v)
112 return show_irq_affinity(AFFINITY, m);
115 static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
117 return show_irq_affinity(AFFINITY_LIST, m);
120 #ifndef CONFIG_AUTO_IRQ_AFFINITY
121 static inline int irq_select_affinity_usr(unsigned int irq)
124 * If the interrupt is started up already then this fails. The
125 * interrupt is assigned to an online CPU already. There is no
126 * point to move it around randomly. Tell user space that the
127 * selected mask is bogus.
129 * If not then any change to the affinity is pointless because the
130 * startup code invokes irq_setup_affinity() which will select
131 * a online CPU anyway.
136 /* ALPHA magic affinity auto selector. Keep it for historical reasons. */
137 static inline int irq_select_affinity_usr(unsigned int irq)
139 return irq_select_affinity(irq);
143 static ssize_t write_irq_affinity(int type, struct file *file,
144 const char __user *buffer, size_t count, loff_t *pos)
146 unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
147 cpumask_var_t new_value;
150 if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
153 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
157 err = cpumask_parselist_user(buffer, count, new_value);
159 err = cpumask_parse_user(buffer, count, new_value);
163 if (!is_affinity_mask_valid(new_value)) {
169 * Do not allow disabling IRQs completely - it's a too easy
170 * way to make the system unusable accidentally :-) At least
171 * one online CPU still has to be targeted.
173 if (!cpumask_intersects(new_value, cpu_online_mask)) {
175 * Special case for empty set - allow the architecture code
176 * to set default SMP affinity.
178 err = irq_select_affinity_usr(irq) ? -EINVAL : count;
180 irq_set_affinity(irq, new_value);
185 free_cpumask_var(new_value);
189 static ssize_t irq_affinity_proc_write(struct file *file,
190 const char __user *buffer, size_t count, loff_t *pos)
192 return write_irq_affinity(0, file, buffer, count, pos);
195 static ssize_t irq_affinity_list_proc_write(struct file *file,
196 const char __user *buffer, size_t count, loff_t *pos)
198 return write_irq_affinity(1, file, buffer, count, pos);
201 static int irq_affinity_proc_open(struct inode *inode, struct file *file)
203 return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
206 static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
208 return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
211 static int irq_affinity_hint_proc_open(struct inode *inode, struct file *file)
213 return single_open(file, irq_affinity_hint_proc_show, PDE_DATA(inode));
216 static const struct file_operations irq_affinity_proc_fops = {
217 .open = irq_affinity_proc_open,
220 .release = single_release,
221 .write = irq_affinity_proc_write,
224 static const struct file_operations irq_affinity_hint_proc_fops = {
225 .open = irq_affinity_hint_proc_open,
228 .release = single_release,
231 static const struct file_operations irq_affinity_list_proc_fops = {
232 .open = irq_affinity_list_proc_open,
235 .release = single_release,
236 .write = irq_affinity_list_proc_write,
239 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
240 static int irq_effective_aff_proc_show(struct seq_file *m, void *v)
242 return show_irq_affinity(EFFECTIVE, m);
245 static int irq_effective_aff_list_proc_show(struct seq_file *m, void *v)
247 return show_irq_affinity(EFFECTIVE_LIST, m);
250 static int irq_effective_aff_proc_open(struct inode *inode, struct file *file)
252 return single_open(file, irq_effective_aff_proc_show, PDE_DATA(inode));
255 static int irq_effective_aff_list_proc_open(struct inode *inode,
258 return single_open(file, irq_effective_aff_list_proc_show,
262 static const struct file_operations irq_effective_aff_proc_fops = {
263 .open = irq_effective_aff_proc_open,
266 .release = single_release,
269 static const struct file_operations irq_effective_aff_list_proc_fops = {
270 .open = irq_effective_aff_list_proc_open,
273 .release = single_release,
277 static int default_affinity_show(struct seq_file *m, void *v)
279 seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
283 static ssize_t default_affinity_write(struct file *file,
284 const char __user *buffer, size_t count, loff_t *ppos)
286 cpumask_var_t new_value;
289 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
292 err = cpumask_parse_user(buffer, count, new_value);
296 if (!is_affinity_mask_valid(new_value)) {
302 * Do not allow disabling IRQs completely - it's a too easy
303 * way to make the system unusable accidentally :-) At least
304 * one online CPU still has to be targeted.
306 if (!cpumask_intersects(new_value, cpu_online_mask)) {
311 cpumask_copy(irq_default_affinity, new_value);
315 free_cpumask_var(new_value);
319 static int default_affinity_open(struct inode *inode, struct file *file)
321 return single_open(file, default_affinity_show, PDE_DATA(inode));
324 static const struct file_operations default_affinity_proc_fops = {
325 .open = default_affinity_open,
328 .release = single_release,
329 .write = default_affinity_write,
332 static int irq_node_proc_show(struct seq_file *m, void *v)
334 struct irq_desc *desc = irq_to_desc((long) m->private);
336 seq_printf(m, "%d\n", irq_desc_get_node(desc));
340 static int irq_node_proc_open(struct inode *inode, struct file *file)
342 return single_open(file, irq_node_proc_show, PDE_DATA(inode));
345 static const struct file_operations irq_node_proc_fops = {
346 .open = irq_node_proc_open,
349 .release = single_release,
353 static int irq_spurious_proc_show(struct seq_file *m, void *v)
355 struct irq_desc *desc = irq_to_desc((long) m->private);
357 seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
358 desc->irq_count, desc->irqs_unhandled,
359 jiffies_to_msecs(desc->last_unhandled));
363 static int irq_spurious_proc_open(struct inode *inode, struct file *file)
365 return single_open(file, irq_spurious_proc_show, PDE_DATA(inode));
368 static const struct file_operations irq_spurious_proc_fops = {
369 .open = irq_spurious_proc_open,
372 .release = single_release,
375 #define MAX_NAMELEN 128
377 static int name_unique(unsigned int irq, struct irqaction *new_action)
379 struct irq_desc *desc = irq_to_desc(irq);
380 struct irqaction *action;
384 raw_spin_lock_irqsave(&desc->lock, flags);
385 for_each_action_of_desc(desc, action) {
386 if ((action != new_action) && action->name &&
387 !strcmp(new_action->name, action->name)) {
392 raw_spin_unlock_irqrestore(&desc->lock, flags);
396 void register_handler_proc(unsigned int irq, struct irqaction *action)
398 char name [MAX_NAMELEN];
399 struct irq_desc *desc = irq_to_desc(irq);
401 if (!desc->dir || action->dir || !action->name ||
402 !name_unique(irq, action))
405 snprintf(name, MAX_NAMELEN, "%s", action->name);
407 /* create /proc/irq/1234/handler/ */
408 action->dir = proc_mkdir(name, desc->dir);
413 #define MAX_NAMELEN 10
415 void register_irq_proc(unsigned int irq, struct irq_desc *desc)
417 static DEFINE_MUTEX(register_lock);
418 void __maybe_unused *irqp = (void *)(unsigned long) irq;
419 char name [MAX_NAMELEN];
421 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
425 * irq directories are registered only when a handler is
426 * added, not when the descriptor is created, so multiple
427 * tasks might try to register at the same time.
429 mutex_lock(®ister_lock);
434 sprintf(name, "%d", irq);
436 /* create /proc/irq/1234 */
437 desc->dir = proc_mkdir(name, root_irq_dir);
442 /* create /proc/irq/<irq>/smp_affinity */
443 proc_create_data("smp_affinity", 0644, desc->dir,
444 &irq_affinity_proc_fops, irqp);
446 /* create /proc/irq/<irq>/affinity_hint */
447 proc_create_data("affinity_hint", 0444, desc->dir,
448 &irq_affinity_hint_proc_fops, irqp);
450 /* create /proc/irq/<irq>/smp_affinity_list */
451 proc_create_data("smp_affinity_list", 0644, desc->dir,
452 &irq_affinity_list_proc_fops, irqp);
454 proc_create_data("node", 0444, desc->dir,
455 &irq_node_proc_fops, irqp);
456 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
457 proc_create_data("effective_affinity", 0444, desc->dir,
458 &irq_effective_aff_proc_fops, irqp);
459 proc_create_data("effective_affinity_list", 0444, desc->dir,
460 &irq_effective_aff_list_proc_fops, irqp);
463 proc_create_data("spurious", 0444, desc->dir,
464 &irq_spurious_proc_fops, (void *)(long)irq);
467 mutex_unlock(®ister_lock);
470 void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
472 char name [MAX_NAMELEN];
474 if (!root_irq_dir || !desc->dir)
477 remove_proc_entry("smp_affinity", desc->dir);
478 remove_proc_entry("affinity_hint", desc->dir);
479 remove_proc_entry("smp_affinity_list", desc->dir);
480 remove_proc_entry("node", desc->dir);
481 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
482 remove_proc_entry("effective_affinity", desc->dir);
483 remove_proc_entry("effective_affinity_list", desc->dir);
486 remove_proc_entry("spurious", desc->dir);
488 sprintf(name, "%u", irq);
489 remove_proc_entry(name, root_irq_dir);
494 void unregister_handler_proc(unsigned int irq, struct irqaction *action)
496 proc_remove(action->dir);
499 static void register_default_affinity_proc(void)
502 proc_create("irq/default_smp_affinity", 0644, NULL,
503 &default_affinity_proc_fops);
507 void init_irq_proc(void)
510 struct irq_desc *desc;
512 /* create /proc/irq */
513 root_irq_dir = proc_mkdir("irq", NULL);
517 register_default_affinity_proc();
520 * Create entries for all existing IRQs.
522 for_each_irq_desc(irq, desc)
523 register_irq_proc(irq, desc);
526 #ifdef CONFIG_GENERIC_IRQ_SHOW
528 int __weak arch_show_interrupts(struct seq_file *p, int prec)
533 #ifndef ACTUAL_NR_IRQS
534 # define ACTUAL_NR_IRQS nr_irqs
537 int show_interrupts(struct seq_file *p, void *v)
541 unsigned long flags, any_count = 0;
542 int i = *(loff_t *) v, j;
543 struct irqaction *action;
544 struct irq_desc *desc;
546 if (i > ACTUAL_NR_IRQS)
549 if (i == ACTUAL_NR_IRQS)
550 return arch_show_interrupts(p, prec);
552 /* print header and calculate the width of the first column */
554 for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
557 seq_printf(p, "%*s", prec + 8, "");
558 for_each_online_cpu(j)
559 seq_printf(p, "CPU%-8d", j);
564 desc = irq_to_desc(i);
568 raw_spin_lock_irqsave(&desc->lock, flags);
569 for_each_online_cpu(j)
570 any_count |= kstat_irqs_cpu(i, j);
571 action = desc->action;
572 if ((!action || irq_desc_is_chained(desc)) && !any_count)
575 seq_printf(p, "%*d: ", prec, i);
576 for_each_online_cpu(j)
577 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
579 if (desc->irq_data.chip) {
580 if (desc->irq_data.chip->irq_print_chip)
581 desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
582 else if (desc->irq_data.chip->name)
583 seq_printf(p, " %8s", desc->irq_data.chip->name);
585 seq_printf(p, " %8s", "-");
587 seq_printf(p, " %8s", "None");
589 if (desc->irq_data.domain)
590 seq_printf(p, " %*d", prec, (int) desc->irq_data.hwirq);
592 seq_printf(p, " %*s", prec, "");
593 #ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
594 seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
597 seq_printf(p, "-%-8s", desc->name);
600 seq_printf(p, " %s", action->name);
601 while ((action = action->next) != NULL)
602 seq_printf(p, ", %s", action->name);
607 raw_spin_unlock_irqrestore(&desc->lock, flags);