1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
5 * This file contains the /proc/irq/ handling code.
10 #include <linux/proc_fs.h>
11 #include <linux/seq_file.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/mutex.h>
16 #include "internals.h"
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.
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.
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.
35 static struct proc_dir_entry *root_irq_dir;
46 static int show_irq_affinity(int type, struct seq_file *m)
48 struct irq_desc *desc = irq_to_desc((long)m->private);
49 const struct cpumask *mask;
54 mask = desc->irq_common_data.affinity;
55 #ifdef CONFIG_GENERIC_PENDING_IRQ
56 if (irqd_is_setaffinity_pending(&desc->irq_data))
57 mask = desc->pending_mask;
62 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
63 mask = irq_data_get_effective_affinity_mask(&desc->irq_data);
73 seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
77 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
83 static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
85 struct irq_desc *desc = irq_to_desc((long)m->private);
89 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
92 raw_spin_lock_irqsave(&desc->lock, flags);
93 if (desc->affinity_hint)
94 cpumask_copy(mask, desc->affinity_hint);
95 raw_spin_unlock_irqrestore(&desc->lock, flags);
97 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
98 free_cpumask_var(mask);
103 #ifndef is_affinity_mask_valid
104 #define is_affinity_mask_valid(val) 1
108 static int irq_affinity_proc_show(struct seq_file *m, void *v)
110 return show_irq_affinity(AFFINITY, m);
113 static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
115 return show_irq_affinity(AFFINITY_LIST, m);
118 #ifndef CONFIG_AUTO_IRQ_AFFINITY
119 static inline int irq_select_affinity_usr(unsigned int irq)
122 * If the interrupt is started up already then this fails. The
123 * interrupt is assigned to an online CPU already. There is no
124 * point to move it around randomly. Tell user space that the
125 * selected mask is bogus.
127 * If not then any change to the affinity is pointless because the
128 * startup code invokes irq_setup_affinity() which will select
129 * a online CPU anyway.
134 /* ALPHA magic affinity auto selector. Keep it for historical reasons. */
135 static inline int irq_select_affinity_usr(unsigned int irq)
137 return irq_select_affinity(irq);
141 static ssize_t write_irq_affinity(int type, struct file *file,
142 const char __user *buffer, size_t count, loff_t *pos)
144 unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
145 cpumask_var_t new_value;
148 if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
151 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
155 err = cpumask_parselist_user(buffer, count, new_value);
157 err = cpumask_parse_user(buffer, count, new_value);
161 if (!is_affinity_mask_valid(new_value)) {
167 * Do not allow disabling IRQs completely - it's a too easy
168 * way to make the system unusable accidentally :-) At least
169 * one online CPU still has to be targeted.
171 if (!cpumask_intersects(new_value, cpu_online_mask)) {
173 * Special case for empty set - allow the architecture code
174 * to set default SMP affinity.
176 err = irq_select_affinity_usr(irq) ? -EINVAL : count;
178 err = irq_set_affinity(irq, new_value);
184 free_cpumask_var(new_value);
188 static ssize_t irq_affinity_proc_write(struct file *file,
189 const char __user *buffer, size_t count, loff_t *pos)
191 return write_irq_affinity(0, file, buffer, count, pos);
194 static ssize_t irq_affinity_list_proc_write(struct file *file,
195 const char __user *buffer, size_t count, loff_t *pos)
197 return write_irq_affinity(1, file, buffer, count, pos);
200 static int irq_affinity_proc_open(struct inode *inode, struct file *file)
202 return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
205 static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
207 return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
210 static const struct file_operations irq_affinity_proc_fops = {
211 .open = irq_affinity_proc_open,
214 .release = single_release,
215 .write = irq_affinity_proc_write,
218 static const struct file_operations irq_affinity_list_proc_fops = {
219 .open = irq_affinity_list_proc_open,
222 .release = single_release,
223 .write = irq_affinity_list_proc_write,
226 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
227 static int irq_effective_aff_proc_show(struct seq_file *m, void *v)
229 return show_irq_affinity(EFFECTIVE, m);
232 static int irq_effective_aff_list_proc_show(struct seq_file *m, void *v)
234 return show_irq_affinity(EFFECTIVE_LIST, m);
238 static int default_affinity_show(struct seq_file *m, void *v)
240 seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
244 static ssize_t default_affinity_write(struct file *file,
245 const char __user *buffer, size_t count, loff_t *ppos)
247 cpumask_var_t new_value;
250 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
253 err = cpumask_parse_user(buffer, count, new_value);
257 if (!is_affinity_mask_valid(new_value)) {
263 * Do not allow disabling IRQs completely - it's a too easy
264 * way to make the system unusable accidentally :-) At least
265 * one online CPU still has to be targeted.
267 if (!cpumask_intersects(new_value, cpu_online_mask)) {
272 cpumask_copy(irq_default_affinity, new_value);
276 free_cpumask_var(new_value);
280 static int default_affinity_open(struct inode *inode, struct file *file)
282 return single_open(file, default_affinity_show, PDE_DATA(inode));
285 static const struct file_operations default_affinity_proc_fops = {
286 .open = default_affinity_open,
289 .release = single_release,
290 .write = default_affinity_write,
293 static int irq_node_proc_show(struct seq_file *m, void *v)
295 struct irq_desc *desc = irq_to_desc((long) m->private);
297 seq_printf(m, "%d\n", irq_desc_get_node(desc));
302 static int irq_spurious_proc_show(struct seq_file *m, void *v)
304 struct irq_desc *desc = irq_to_desc((long) m->private);
306 seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
307 desc->irq_count, desc->irqs_unhandled,
308 jiffies_to_msecs(desc->last_unhandled));
312 #define MAX_NAMELEN 128
314 static int name_unique(unsigned int irq, struct irqaction *new_action)
316 struct irq_desc *desc = irq_to_desc(irq);
317 struct irqaction *action;
321 raw_spin_lock_irqsave(&desc->lock, flags);
322 for_each_action_of_desc(desc, action) {
323 if ((action != new_action) && action->name &&
324 !strcmp(new_action->name, action->name)) {
329 raw_spin_unlock_irqrestore(&desc->lock, flags);
333 void register_handler_proc(unsigned int irq, struct irqaction *action)
335 char name [MAX_NAMELEN];
336 struct irq_desc *desc = irq_to_desc(irq);
338 if (!desc->dir || action->dir || !action->name ||
339 !name_unique(irq, action))
342 snprintf(name, MAX_NAMELEN, "%s", action->name);
344 /* create /proc/irq/1234/handler/ */
345 action->dir = proc_mkdir(name, desc->dir);
350 #define MAX_NAMELEN 10
352 void register_irq_proc(unsigned int irq, struct irq_desc *desc)
354 static DEFINE_MUTEX(register_lock);
355 void __maybe_unused *irqp = (void *)(unsigned long) irq;
356 char name [MAX_NAMELEN];
358 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
362 * irq directories are registered only when a handler is
363 * added, not when the descriptor is created, so multiple
364 * tasks might try to register at the same time.
366 mutex_lock(®ister_lock);
371 sprintf(name, "%d", irq);
373 /* create /proc/irq/1234 */
374 desc->dir = proc_mkdir(name, root_irq_dir);
379 /* create /proc/irq/<irq>/smp_affinity */
380 proc_create_data("smp_affinity", 0644, desc->dir,
381 &irq_affinity_proc_fops, irqp);
383 /* create /proc/irq/<irq>/affinity_hint */
384 proc_create_single_data("affinity_hint", 0444, desc->dir,
385 irq_affinity_hint_proc_show, irqp);
387 /* create /proc/irq/<irq>/smp_affinity_list */
388 proc_create_data("smp_affinity_list", 0644, desc->dir,
389 &irq_affinity_list_proc_fops, irqp);
391 proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show,
393 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
394 proc_create_single_data("effective_affinity", 0444, desc->dir,
395 irq_effective_aff_proc_show, irqp);
396 proc_create_single_data("effective_affinity_list", 0444, desc->dir,
397 irq_effective_aff_list_proc_show, irqp);
400 proc_create_single_data("spurious", 0444, desc->dir,
401 irq_spurious_proc_show, (void *)(long)irq);
404 mutex_unlock(®ister_lock);
407 void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
409 char name [MAX_NAMELEN];
411 if (!root_irq_dir || !desc->dir)
414 remove_proc_entry("smp_affinity", desc->dir);
415 remove_proc_entry("affinity_hint", desc->dir);
416 remove_proc_entry("smp_affinity_list", desc->dir);
417 remove_proc_entry("node", desc->dir);
418 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
419 remove_proc_entry("effective_affinity", desc->dir);
420 remove_proc_entry("effective_affinity_list", desc->dir);
423 remove_proc_entry("spurious", desc->dir);
425 sprintf(name, "%u", irq);
426 remove_proc_entry(name, root_irq_dir);
431 void unregister_handler_proc(unsigned int irq, struct irqaction *action)
433 proc_remove(action->dir);
436 static void register_default_affinity_proc(void)
439 proc_create("irq/default_smp_affinity", 0644, NULL,
440 &default_affinity_proc_fops);
444 void init_irq_proc(void)
447 struct irq_desc *desc;
449 /* create /proc/irq */
450 root_irq_dir = proc_mkdir("irq", NULL);
454 register_default_affinity_proc();
457 * Create entries for all existing IRQs.
459 for_each_irq_desc(irq, desc)
460 register_irq_proc(irq, desc);
463 #ifdef CONFIG_GENERIC_IRQ_SHOW
465 int __weak arch_show_interrupts(struct seq_file *p, int prec)
470 #ifndef ACTUAL_NR_IRQS
471 # define ACTUAL_NR_IRQS nr_irqs
474 int show_interrupts(struct seq_file *p, void *v)
478 unsigned long flags, any_count = 0;
479 int i = *(loff_t *) v, j;
480 struct irqaction *action;
481 struct irq_desc *desc;
483 if (i > ACTUAL_NR_IRQS)
486 if (i == ACTUAL_NR_IRQS)
487 return arch_show_interrupts(p, prec);
489 /* print header and calculate the width of the first column */
491 for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
494 seq_printf(p, "%*s", prec + 8, "");
495 for_each_online_cpu(j)
496 seq_printf(p, "CPU%-8d", j);
501 desc = irq_to_desc(i);
505 if (desc->kstat_irqs)
506 for_each_online_cpu(j)
507 any_count |= *per_cpu_ptr(desc->kstat_irqs, j);
509 if ((!desc->action || irq_desc_is_chained(desc)) && !any_count)
512 seq_printf(p, "%*d: ", prec, i);
513 for_each_online_cpu(j)
514 seq_printf(p, "%10u ", desc->kstat_irqs ?
515 *per_cpu_ptr(desc->kstat_irqs, j) : 0);
517 raw_spin_lock_irqsave(&desc->lock, flags);
518 if (desc->irq_data.chip) {
519 if (desc->irq_data.chip->irq_print_chip)
520 desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
521 else if (desc->irq_data.chip->name)
522 seq_printf(p, " %8s", desc->irq_data.chip->name);
524 seq_printf(p, " %8s", "-");
526 seq_printf(p, " %8s", "None");
528 if (desc->irq_data.domain)
529 seq_printf(p, " %*d", prec, (int) desc->irq_data.hwirq);
531 seq_printf(p, " %*s", prec, "");
532 #ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
533 seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
536 seq_printf(p, "-%-8s", desc->name);
538 action = desc->action;
540 seq_printf(p, " %s", action->name);
541 while ((action = action->next) != NULL)
542 seq_printf(p, ", %s", action->name);
546 raw_spin_unlock_irqrestore(&desc->lock, flags);