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
);
121 static ssize_t
write_irq_affinity(int type
, struct file
*file
,
122 const char __user
*buffer
, size_t count
, loff_t
*pos
)
124 unsigned int irq
= (int)(long)PDE_DATA(file_inode(file
));
125 cpumask_var_t new_value
;
128 if (!irq_can_set_affinity_usr(irq
) || no_irq_affinity
)
131 if (!alloc_cpumask_var(&new_value
, GFP_KERNEL
))
135 err
= cpumask_parselist_user(buffer
, count
, new_value
);
137 err
= cpumask_parse_user(buffer
, count
, new_value
);
141 if (!is_affinity_mask_valid(new_value
)) {
147 * Do not allow disabling IRQs completely - it's a too easy
148 * way to make the system unusable accidentally :-) At least
149 * one online CPU still has to be targeted.
151 if (!cpumask_intersects(new_value
, cpu_online_mask
)) {
153 * Special case for empty set - allow the architecture code
154 * to set default SMP affinity.
156 err
= irq_select_affinity_usr(irq
) ? -EINVAL
: count
;
158 err
= irq_set_affinity(irq
, new_value
);
164 free_cpumask_var(new_value
);
168 static ssize_t
irq_affinity_proc_write(struct file
*file
,
169 const char __user
*buffer
, size_t count
, loff_t
*pos
)
171 return write_irq_affinity(0, file
, buffer
, count
, pos
);
174 static ssize_t
irq_affinity_list_proc_write(struct file
*file
,
175 const char __user
*buffer
, size_t count
, loff_t
*pos
)
177 return write_irq_affinity(1, file
, buffer
, count
, pos
);
180 static int irq_affinity_proc_open(struct inode
*inode
, struct file
*file
)
182 return single_open(file
, irq_affinity_proc_show
, PDE_DATA(inode
));
185 static int irq_affinity_list_proc_open(struct inode
*inode
, struct file
*file
)
187 return single_open(file
, irq_affinity_list_proc_show
, PDE_DATA(inode
));
190 static int irq_affinity_hint_proc_open(struct inode
*inode
, struct file
*file
)
192 return single_open(file
, irq_affinity_hint_proc_show
, PDE_DATA(inode
));
195 static const struct file_operations irq_affinity_proc_fops
= {
196 .open
= irq_affinity_proc_open
,
199 .release
= single_release
,
200 .write
= irq_affinity_proc_write
,
203 static const struct file_operations irq_affinity_hint_proc_fops
= {
204 .open
= irq_affinity_hint_proc_open
,
207 .release
= single_release
,
210 static const struct file_operations irq_affinity_list_proc_fops
= {
211 .open
= irq_affinity_list_proc_open
,
214 .release
= single_release
,
215 .write
= irq_affinity_list_proc_write
,
218 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
219 static int irq_effective_aff_proc_show(struct seq_file
*m
, void *v
)
221 return show_irq_affinity(EFFECTIVE
, m
);
224 static int irq_effective_aff_list_proc_show(struct seq_file
*m
, void *v
)
226 return show_irq_affinity(EFFECTIVE_LIST
, m
);
229 static int irq_effective_aff_proc_open(struct inode
*inode
, struct file
*file
)
231 return single_open(file
, irq_effective_aff_proc_show
, PDE_DATA(inode
));
234 static int irq_effective_aff_list_proc_open(struct inode
*inode
,
237 return single_open(file
, irq_effective_aff_list_proc_show
,
241 static const struct file_operations irq_effective_aff_proc_fops
= {
242 .open
= irq_effective_aff_proc_open
,
245 .release
= single_release
,
248 static const struct file_operations irq_effective_aff_list_proc_fops
= {
249 .open
= irq_effective_aff_list_proc_open
,
252 .release
= single_release
,
256 static int default_affinity_show(struct seq_file
*m
, void *v
)
258 seq_printf(m
, "%*pb\n", cpumask_pr_args(irq_default_affinity
));
262 static ssize_t
default_affinity_write(struct file
*file
,
263 const char __user
*buffer
, size_t count
, loff_t
*ppos
)
265 cpumask_var_t new_value
;
268 if (!alloc_cpumask_var(&new_value
, GFP_KERNEL
))
271 err
= cpumask_parse_user(buffer
, count
, new_value
);
275 if (!is_affinity_mask_valid(new_value
)) {
281 * Do not allow disabling IRQs completely - it's a too easy
282 * way to make the system unusable accidentally :-) At least
283 * one online CPU still has to be targeted.
285 if (!cpumask_intersects(new_value
, cpu_online_mask
)) {
290 cpumask_copy(irq_default_affinity
, new_value
);
294 free_cpumask_var(new_value
);
298 static int default_affinity_open(struct inode
*inode
, struct file
*file
)
300 return single_open(file
, default_affinity_show
, PDE_DATA(inode
));
303 static const struct file_operations default_affinity_proc_fops
= {
304 .open
= default_affinity_open
,
307 .release
= single_release
,
308 .write
= default_affinity_write
,
311 static int irq_node_proc_show(struct seq_file
*m
, void *v
)
313 struct irq_desc
*desc
= irq_to_desc((long) m
->private);
315 seq_printf(m
, "%d\n", irq_desc_get_node(desc
));
319 static int irq_node_proc_open(struct inode
*inode
, struct file
*file
)
321 return single_open(file
, irq_node_proc_show
, PDE_DATA(inode
));
324 static const struct file_operations irq_node_proc_fops
= {
325 .open
= irq_node_proc_open
,
328 .release
= single_release
,
332 static int irq_spurious_proc_show(struct seq_file
*m
, void *v
)
334 struct irq_desc
*desc
= irq_to_desc((long) m
->private);
336 seq_printf(m
, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
337 desc
->irq_count
, desc
->irqs_unhandled
,
338 jiffies_to_msecs(desc
->last_unhandled
));
342 static int irq_spurious_proc_open(struct inode
*inode
, struct file
*file
)
344 return single_open(file
, irq_spurious_proc_show
, PDE_DATA(inode
));
347 static const struct file_operations irq_spurious_proc_fops
= {
348 .open
= irq_spurious_proc_open
,
351 .release
= single_release
,
354 #define MAX_NAMELEN 128
356 static int name_unique(unsigned int irq
, struct irqaction
*new_action
)
358 struct irq_desc
*desc
= irq_to_desc(irq
);
359 struct irqaction
*action
;
363 raw_spin_lock_irqsave(&desc
->lock
, flags
);
364 for_each_action_of_desc(desc
, action
) {
365 if ((action
!= new_action
) && action
->name
&&
366 !strcmp(new_action
->name
, action
->name
)) {
371 raw_spin_unlock_irqrestore(&desc
->lock
, flags
);
375 void register_handler_proc(unsigned int irq
, struct irqaction
*action
)
377 char name
[MAX_NAMELEN
];
378 struct irq_desc
*desc
= irq_to_desc(irq
);
380 if (!desc
->dir
|| action
->dir
|| !action
->name
||
381 !name_unique(irq
, action
))
384 snprintf(name
, MAX_NAMELEN
, "%s", action
->name
);
386 /* create /proc/irq/1234/handler/ */
387 action
->dir
= proc_mkdir(name
, desc
->dir
);
392 #define MAX_NAMELEN 10
394 void register_irq_proc(unsigned int irq
, struct irq_desc
*desc
)
396 static DEFINE_MUTEX(register_lock
);
397 void __maybe_unused
*irqp
= (void *)(unsigned long) irq
;
398 char name
[MAX_NAMELEN
];
400 if (!root_irq_dir
|| (desc
->irq_data
.chip
== &no_irq_chip
))
404 * irq directories are registered only when a handler is
405 * added, not when the descriptor is created, so multiple
406 * tasks might try to register at the same time.
408 mutex_lock(®ister_lock
);
413 sprintf(name
, "%d", irq
);
415 /* create /proc/irq/1234 */
416 desc
->dir
= proc_mkdir(name
, root_irq_dir
);
421 /* create /proc/irq/<irq>/smp_affinity */
422 proc_create_data("smp_affinity", 0644, desc
->dir
,
423 &irq_affinity_proc_fops
, irqp
);
425 /* create /proc/irq/<irq>/affinity_hint */
426 proc_create_data("affinity_hint", 0444, desc
->dir
,
427 &irq_affinity_hint_proc_fops
, irqp
);
429 /* create /proc/irq/<irq>/smp_affinity_list */
430 proc_create_data("smp_affinity_list", 0644, desc
->dir
,
431 &irq_affinity_list_proc_fops
, irqp
);
433 proc_create_data("node", 0444, desc
->dir
,
434 &irq_node_proc_fops
, irqp
);
435 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
436 proc_create_data("effective_affinity", 0444, desc
->dir
,
437 &irq_effective_aff_proc_fops
, irqp
);
438 proc_create_data("effective_affinity_list", 0444, desc
->dir
,
439 &irq_effective_aff_list_proc_fops
, irqp
);
442 proc_create_data("spurious", 0444, desc
->dir
,
443 &irq_spurious_proc_fops
, (void *)(long)irq
);
446 mutex_unlock(®ister_lock
);
449 void unregister_irq_proc(unsigned int irq
, struct irq_desc
*desc
)
451 char name
[MAX_NAMELEN
];
453 if (!root_irq_dir
|| !desc
->dir
)
456 remove_proc_entry("smp_affinity", desc
->dir
);
457 remove_proc_entry("affinity_hint", desc
->dir
);
458 remove_proc_entry("smp_affinity_list", desc
->dir
);
459 remove_proc_entry("node", desc
->dir
);
460 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
461 remove_proc_entry("effective_affinity", desc
->dir
);
462 remove_proc_entry("effective_affinity_list", desc
->dir
);
465 remove_proc_entry("spurious", desc
->dir
);
467 sprintf(name
, "%u", irq
);
468 remove_proc_entry(name
, root_irq_dir
);
473 void unregister_handler_proc(unsigned int irq
, struct irqaction
*action
)
475 proc_remove(action
->dir
);
478 static void register_default_affinity_proc(void)
481 proc_create("irq/default_smp_affinity", 0644, NULL
,
482 &default_affinity_proc_fops
);
486 void init_irq_proc(void)
489 struct irq_desc
*desc
;
491 /* create /proc/irq */
492 root_irq_dir
= proc_mkdir("irq", NULL
);
496 register_default_affinity_proc();
499 * Create entries for all existing IRQs.
501 for_each_irq_desc(irq
, desc
)
502 register_irq_proc(irq
, desc
);
505 #ifdef CONFIG_GENERIC_IRQ_SHOW
507 int __weak
arch_show_interrupts(struct seq_file
*p
, int prec
)
512 #ifndef ACTUAL_NR_IRQS
513 # define ACTUAL_NR_IRQS nr_irqs
516 int show_interrupts(struct seq_file
*p
, void *v
)
520 unsigned long flags
, any_count
= 0;
521 int i
= *(loff_t
*) v
, j
;
522 struct irqaction
*action
;
523 struct irq_desc
*desc
;
525 if (i
> ACTUAL_NR_IRQS
)
528 if (i
== ACTUAL_NR_IRQS
)
529 return arch_show_interrupts(p
, prec
);
531 /* print header and calculate the width of the first column */
533 for (prec
= 3, j
= 1000; prec
< 10 && j
<= nr_irqs
; ++prec
)
536 seq_printf(p
, "%*s", prec
+ 8, "");
537 for_each_online_cpu(j
)
538 seq_printf(p
, "CPU%-8d", j
);
543 desc
= irq_to_desc(i
);
547 raw_spin_lock_irqsave(&desc
->lock
, flags
);
548 for_each_online_cpu(j
)
549 any_count
|= kstat_irqs_cpu(i
, j
);
550 action
= desc
->action
;
551 if ((!action
|| irq_desc_is_chained(desc
)) && !any_count
)
554 seq_printf(p
, "%*d: ", prec
, i
);
555 for_each_online_cpu(j
)
556 seq_printf(p
, "%10u ", kstat_irqs_cpu(i
, j
));
558 if (desc
->irq_data
.chip
) {
559 if (desc
->irq_data
.chip
->irq_print_chip
)
560 desc
->irq_data
.chip
->irq_print_chip(&desc
->irq_data
, p
);
561 else if (desc
->irq_data
.chip
->name
)
562 seq_printf(p
, " %8s", desc
->irq_data
.chip
->name
);
564 seq_printf(p
, " %8s", "-");
566 seq_printf(p
, " %8s", "None");
568 if (desc
->irq_data
.domain
)
569 seq_printf(p
, " %*d", prec
, (int) desc
->irq_data
.hwirq
);
571 seq_printf(p
, " %*s", prec
, "");
572 #ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
573 seq_printf(p
, " %-8s", irqd_is_level_type(&desc
->irq_data
) ? "Level" : "Edge");
576 seq_printf(p
, "-%-8s", desc
->name
);
579 seq_printf(p
, " %s", action
->name
);
580 while ((action
= action
->next
) != NULL
)
581 seq_printf(p
, ", %s", action
->name
);
586 raw_spin_unlock_irqrestore(&desc
->lock
, flags
);