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 irq_set_affinity(irq
, new_value
);
163 free_cpumask_var(new_value
);
167 static ssize_t
irq_affinity_proc_write(struct file
*file
,
168 const char __user
*buffer
, size_t count
, loff_t
*pos
)
170 return write_irq_affinity(0, file
, buffer
, count
, pos
);
173 static ssize_t
irq_affinity_list_proc_write(struct file
*file
,
174 const char __user
*buffer
, size_t count
, loff_t
*pos
)
176 return write_irq_affinity(1, file
, buffer
, count
, pos
);
179 static int irq_affinity_proc_open(struct inode
*inode
, struct file
*file
)
181 return single_open(file
, irq_affinity_proc_show
, PDE_DATA(inode
));
184 static int irq_affinity_list_proc_open(struct inode
*inode
, struct file
*file
)
186 return single_open(file
, irq_affinity_list_proc_show
, PDE_DATA(inode
));
189 static int irq_affinity_hint_proc_open(struct inode
*inode
, struct file
*file
)
191 return single_open(file
, irq_affinity_hint_proc_show
, PDE_DATA(inode
));
194 static const struct file_operations irq_affinity_proc_fops
= {
195 .open
= irq_affinity_proc_open
,
198 .release
= single_release
,
199 .write
= irq_affinity_proc_write
,
202 static const struct file_operations irq_affinity_hint_proc_fops
= {
203 .open
= irq_affinity_hint_proc_open
,
206 .release
= single_release
,
209 static const struct file_operations irq_affinity_list_proc_fops
= {
210 .open
= irq_affinity_list_proc_open
,
213 .release
= single_release
,
214 .write
= irq_affinity_list_proc_write
,
217 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
218 static int irq_effective_aff_proc_show(struct seq_file
*m
, void *v
)
220 return show_irq_affinity(EFFECTIVE
, m
);
223 static int irq_effective_aff_list_proc_show(struct seq_file
*m
, void *v
)
225 return show_irq_affinity(EFFECTIVE_LIST
, m
);
228 static int irq_effective_aff_proc_open(struct inode
*inode
, struct file
*file
)
230 return single_open(file
, irq_effective_aff_proc_show
, PDE_DATA(inode
));
233 static int irq_effective_aff_list_proc_open(struct inode
*inode
,
236 return single_open(file
, irq_effective_aff_list_proc_show
,
240 static const struct file_operations irq_effective_aff_proc_fops
= {
241 .open
= irq_effective_aff_proc_open
,
244 .release
= single_release
,
247 static const struct file_operations irq_effective_aff_list_proc_fops
= {
248 .open
= irq_effective_aff_list_proc_open
,
251 .release
= single_release
,
255 static int default_affinity_show(struct seq_file
*m
, void *v
)
257 seq_printf(m
, "%*pb\n", cpumask_pr_args(irq_default_affinity
));
261 static ssize_t
default_affinity_write(struct file
*file
,
262 const char __user
*buffer
, size_t count
, loff_t
*ppos
)
264 cpumask_var_t new_value
;
267 if (!alloc_cpumask_var(&new_value
, GFP_KERNEL
))
270 err
= cpumask_parse_user(buffer
, count
, new_value
);
274 if (!is_affinity_mask_valid(new_value
)) {
280 * Do not allow disabling IRQs completely - it's a too easy
281 * way to make the system unusable accidentally :-) At least
282 * one online CPU still has to be targeted.
284 if (!cpumask_intersects(new_value
, cpu_online_mask
)) {
289 cpumask_copy(irq_default_affinity
, new_value
);
293 free_cpumask_var(new_value
);
297 static int default_affinity_open(struct inode
*inode
, struct file
*file
)
299 return single_open(file
, default_affinity_show
, PDE_DATA(inode
));
302 static const struct file_operations default_affinity_proc_fops
= {
303 .open
= default_affinity_open
,
306 .release
= single_release
,
307 .write
= default_affinity_write
,
310 static int irq_node_proc_show(struct seq_file
*m
, void *v
)
312 struct irq_desc
*desc
= irq_to_desc((long) m
->private);
314 seq_printf(m
, "%d\n", irq_desc_get_node(desc
));
318 static int irq_node_proc_open(struct inode
*inode
, struct file
*file
)
320 return single_open(file
, irq_node_proc_show
, PDE_DATA(inode
));
323 static const struct file_operations irq_node_proc_fops
= {
324 .open
= irq_node_proc_open
,
327 .release
= single_release
,
331 static int irq_spurious_proc_show(struct seq_file
*m
, void *v
)
333 struct irq_desc
*desc
= irq_to_desc((long) m
->private);
335 seq_printf(m
, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
336 desc
->irq_count
, desc
->irqs_unhandled
,
337 jiffies_to_msecs(desc
->last_unhandled
));
341 static int irq_spurious_proc_open(struct inode
*inode
, struct file
*file
)
343 return single_open(file
, irq_spurious_proc_show
, PDE_DATA(inode
));
346 static const struct file_operations irq_spurious_proc_fops
= {
347 .open
= irq_spurious_proc_open
,
350 .release
= single_release
,
353 #define MAX_NAMELEN 128
355 static int name_unique(unsigned int irq
, struct irqaction
*new_action
)
357 struct irq_desc
*desc
= irq_to_desc(irq
);
358 struct irqaction
*action
;
362 raw_spin_lock_irqsave(&desc
->lock
, flags
);
363 for_each_action_of_desc(desc
, action
) {
364 if ((action
!= new_action
) && action
->name
&&
365 !strcmp(new_action
->name
, action
->name
)) {
370 raw_spin_unlock_irqrestore(&desc
->lock
, flags
);
374 void register_handler_proc(unsigned int irq
, struct irqaction
*action
)
376 char name
[MAX_NAMELEN
];
377 struct irq_desc
*desc
= irq_to_desc(irq
);
379 if (!desc
->dir
|| action
->dir
|| !action
->name
||
380 !name_unique(irq
, action
))
383 snprintf(name
, MAX_NAMELEN
, "%s", action
->name
);
385 /* create /proc/irq/1234/handler/ */
386 action
->dir
= proc_mkdir(name
, desc
->dir
);
391 #define MAX_NAMELEN 10
393 void register_irq_proc(unsigned int irq
, struct irq_desc
*desc
)
395 static DEFINE_MUTEX(register_lock
);
396 void __maybe_unused
*irqp
= (void *)(unsigned long) irq
;
397 char name
[MAX_NAMELEN
];
399 if (!root_irq_dir
|| (desc
->irq_data
.chip
== &no_irq_chip
))
403 * irq directories are registered only when a handler is
404 * added, not when the descriptor is created, so multiple
405 * tasks might try to register at the same time.
407 mutex_lock(®ister_lock
);
412 sprintf(name
, "%d", irq
);
414 /* create /proc/irq/1234 */
415 desc
->dir
= proc_mkdir(name
, root_irq_dir
);
420 /* create /proc/irq/<irq>/smp_affinity */
421 proc_create_data("smp_affinity", 0644, desc
->dir
,
422 &irq_affinity_proc_fops
, irqp
);
424 /* create /proc/irq/<irq>/affinity_hint */
425 proc_create_data("affinity_hint", 0444, desc
->dir
,
426 &irq_affinity_hint_proc_fops
, irqp
);
428 /* create /proc/irq/<irq>/smp_affinity_list */
429 proc_create_data("smp_affinity_list", 0644, desc
->dir
,
430 &irq_affinity_list_proc_fops
, irqp
);
432 proc_create_data("node", 0444, desc
->dir
,
433 &irq_node_proc_fops
, irqp
);
434 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
435 proc_create_data("effective_affinity", 0444, desc
->dir
,
436 &irq_effective_aff_proc_fops
, irqp
);
437 proc_create_data("effective_affinity_list", 0444, desc
->dir
,
438 &irq_effective_aff_list_proc_fops
, irqp
);
441 proc_create_data("spurious", 0444, desc
->dir
,
442 &irq_spurious_proc_fops
, (void *)(long)irq
);
445 mutex_unlock(®ister_lock
);
448 void unregister_irq_proc(unsigned int irq
, struct irq_desc
*desc
)
450 char name
[MAX_NAMELEN
];
452 if (!root_irq_dir
|| !desc
->dir
)
455 remove_proc_entry("smp_affinity", desc
->dir
);
456 remove_proc_entry("affinity_hint", desc
->dir
);
457 remove_proc_entry("smp_affinity_list", desc
->dir
);
458 remove_proc_entry("node", desc
->dir
);
459 # ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
460 remove_proc_entry("effective_affinity", desc
->dir
);
461 remove_proc_entry("effective_affinity_list", desc
->dir
);
464 remove_proc_entry("spurious", desc
->dir
);
466 sprintf(name
, "%u", irq
);
467 remove_proc_entry(name
, root_irq_dir
);
472 void unregister_handler_proc(unsigned int irq
, struct irqaction
*action
)
474 proc_remove(action
->dir
);
477 static void register_default_affinity_proc(void)
480 proc_create("irq/default_smp_affinity", 0644, NULL
,
481 &default_affinity_proc_fops
);
485 void init_irq_proc(void)
488 struct irq_desc
*desc
;
490 /* create /proc/irq */
491 root_irq_dir
= proc_mkdir("irq", NULL
);
495 register_default_affinity_proc();
498 * Create entries for all existing IRQs.
500 for_each_irq_desc(irq
, desc
)
501 register_irq_proc(irq
, desc
);
504 #ifdef CONFIG_GENERIC_IRQ_SHOW
506 int __weak
arch_show_interrupts(struct seq_file
*p
, int prec
)
511 #ifndef ACTUAL_NR_IRQS
512 # define ACTUAL_NR_IRQS nr_irqs
515 int show_interrupts(struct seq_file
*p
, void *v
)
519 unsigned long flags
, any_count
= 0;
520 int i
= *(loff_t
*) v
, j
;
521 struct irqaction
*action
;
522 struct irq_desc
*desc
;
524 if (i
> ACTUAL_NR_IRQS
)
527 if (i
== ACTUAL_NR_IRQS
)
528 return arch_show_interrupts(p
, prec
);
530 /* print header and calculate the width of the first column */
532 for (prec
= 3, j
= 1000; prec
< 10 && j
<= nr_irqs
; ++prec
)
535 seq_printf(p
, "%*s", prec
+ 8, "");
536 for_each_online_cpu(j
)
537 seq_printf(p
, "CPU%-8d", j
);
542 desc
= irq_to_desc(i
);
546 raw_spin_lock_irqsave(&desc
->lock
, flags
);
547 for_each_online_cpu(j
)
548 any_count
|= kstat_irqs_cpu(i
, j
);
549 action
= desc
->action
;
550 if ((!action
|| irq_desc_is_chained(desc
)) && !any_count
)
553 seq_printf(p
, "%*d: ", prec
, i
);
554 for_each_online_cpu(j
)
555 seq_printf(p
, "%10u ", kstat_irqs_cpu(i
, j
));
557 if (desc
->irq_data
.chip
) {
558 if (desc
->irq_data
.chip
->irq_print_chip
)
559 desc
->irq_data
.chip
->irq_print_chip(&desc
->irq_data
, p
);
560 else if (desc
->irq_data
.chip
->name
)
561 seq_printf(p
, " %8s", desc
->irq_data
.chip
->name
);
563 seq_printf(p
, " %8s", "-");
565 seq_printf(p
, " %8s", "None");
567 if (desc
->irq_data
.domain
)
568 seq_printf(p
, " %*d", prec
, (int) desc
->irq_data
.hwirq
);
570 seq_printf(p
, " %*s", prec
, "");
571 #ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
572 seq_printf(p
, " %-8s", irqd_is_level_type(&desc
->irq_data
) ? "Level" : "Edge");
575 seq_printf(p
, "-%-8s", desc
->name
);
578 seq_printf(p
, " %s", action
->name
);
579 while ((action
= action
->next
) != NULL
)
580 seq_printf(p
, ", %s", action
->name
);
585 raw_spin_unlock_irqrestore(&desc
->lock
, flags
);