License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / kernel / irq / proc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/kernel/irq/proc.c
4  *
5  * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
6  *
7  * This file contains the /proc/irq/ handling code.
8  */
9
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>
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  */
37 static struct proc_dir_entry *root_irq_dir;
38
39 #ifdef CONFIG_SMP
40
41 enum {
42         AFFINITY,
43         AFFINITY_LIST,
44         EFFECTIVE,
45         EFFECTIVE_LIST,
46 };
47
48 static 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         switch (type) {
54         case AFFINITY:
55         case AFFINITY_LIST:
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;
60 #endif
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
85 static 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         unsigned long flags;
89         cpumask_var_t mask;
90
91         if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
92                 return -ENOMEM;
93
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);
98
99         seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
100         free_cpumask_var(mask);
101
102         return 0;
103 }
104
105 #ifndef is_affinity_mask_valid
106 #define is_affinity_mask_valid(val) 1
107 #endif
108
109 int no_irq_affinity;
110 static int irq_affinity_proc_show(struct seq_file *m, void *v)
111 {
112         return show_irq_affinity(AFFINITY, m);
113 }
114
115 static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
116 {
117         return show_irq_affinity(AFFINITY_LIST, m);
118 }
119
120
121 static ssize_t write_irq_affinity(int type, struct file *file,
122                 const char __user *buffer, size_t count, loff_t *pos)
123 {
124         unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
125         cpumask_var_t new_value;
126         int err;
127
128         if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
129                 return -EIO;
130
131         if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
132                 return -ENOMEM;
133
134         if (type)
135                 err = cpumask_parselist_user(buffer, count, new_value);
136         else
137                 err = cpumask_parse_user(buffer, count, new_value);
138         if (err)
139                 goto free_cpumask;
140
141         if (!is_affinity_mask_valid(new_value)) {
142                 err = -EINVAL;
143                 goto free_cpumask;
144         }
145
146         /*
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.
150          */
151         if (!cpumask_intersects(new_value, cpu_online_mask)) {
152                 /*
153                  * Special case for empty set - allow the architecture code
154                  * to set default SMP affinity.
155                  */
156                 err = irq_select_affinity_usr(irq) ? -EINVAL : count;
157         } else {
158                 irq_set_affinity(irq, new_value);
159                 err = count;
160         }
161
162 free_cpumask:
163         free_cpumask_var(new_value);
164         return err;
165 }
166
167 static ssize_t irq_affinity_proc_write(struct file *file,
168                 const char __user *buffer, size_t count, loff_t *pos)
169 {
170         return write_irq_affinity(0, file, buffer, count, pos);
171 }
172
173 static ssize_t irq_affinity_list_proc_write(struct file *file,
174                 const char __user *buffer, size_t count, loff_t *pos)
175 {
176         return write_irq_affinity(1, file, buffer, count, pos);
177 }
178
179 static int irq_affinity_proc_open(struct inode *inode, struct file *file)
180 {
181         return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
182 }
183
184 static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
185 {
186         return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
187 }
188
189 static int irq_affinity_hint_proc_open(struct inode *inode, struct file *file)
190 {
191         return single_open(file, irq_affinity_hint_proc_show, PDE_DATA(inode));
192 }
193
194 static const struct file_operations irq_affinity_proc_fops = {
195         .open           = irq_affinity_proc_open,
196         .read           = seq_read,
197         .llseek         = seq_lseek,
198         .release        = single_release,
199         .write          = irq_affinity_proc_write,
200 };
201
202 static const struct file_operations irq_affinity_hint_proc_fops = {
203         .open           = irq_affinity_hint_proc_open,
204         .read           = seq_read,
205         .llseek         = seq_lseek,
206         .release        = single_release,
207 };
208
209 static const struct file_operations irq_affinity_list_proc_fops = {
210         .open           = irq_affinity_list_proc_open,
211         .read           = seq_read,
212         .llseek         = seq_lseek,
213         .release        = single_release,
214         .write          = irq_affinity_list_proc_write,
215 };
216
217 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
218 static int irq_effective_aff_proc_show(struct seq_file *m, void *v)
219 {
220         return show_irq_affinity(EFFECTIVE, m);
221 }
222
223 static int irq_effective_aff_list_proc_show(struct seq_file *m, void *v)
224 {
225         return show_irq_affinity(EFFECTIVE_LIST, m);
226 }
227
228 static int irq_effective_aff_proc_open(struct inode *inode, struct file *file)
229 {
230         return single_open(file, irq_effective_aff_proc_show, PDE_DATA(inode));
231 }
232
233 static int irq_effective_aff_list_proc_open(struct inode *inode,
234                                             struct file *file)
235 {
236         return single_open(file, irq_effective_aff_list_proc_show,
237                            PDE_DATA(inode));
238 }
239
240 static const struct file_operations irq_effective_aff_proc_fops = {
241         .open           = irq_effective_aff_proc_open,
242         .read           = seq_read,
243         .llseek         = seq_lseek,
244         .release        = single_release,
245 };
246
247 static const struct file_operations irq_effective_aff_list_proc_fops = {
248         .open           = irq_effective_aff_list_proc_open,
249         .read           = seq_read,
250         .llseek         = seq_lseek,
251         .release        = single_release,
252 };
253 #endif
254
255 static int default_affinity_show(struct seq_file *m, void *v)
256 {
257         seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
258         return 0;
259 }
260
261 static ssize_t default_affinity_write(struct file *file,
262                 const char __user *buffer, size_t count, loff_t *ppos)
263 {
264         cpumask_var_t new_value;
265         int err;
266
267         if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
268                 return -ENOMEM;
269
270         err = cpumask_parse_user(buffer, count, new_value);
271         if (err)
272                 goto out;
273
274         if (!is_affinity_mask_valid(new_value)) {
275                 err = -EINVAL;
276                 goto out;
277         }
278
279         /*
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.
283          */
284         if (!cpumask_intersects(new_value, cpu_online_mask)) {
285                 err = -EINVAL;
286                 goto out;
287         }
288
289         cpumask_copy(irq_default_affinity, new_value);
290         err = count;
291
292 out:
293         free_cpumask_var(new_value);
294         return err;
295 }
296
297 static int default_affinity_open(struct inode *inode, struct file *file)
298 {
299         return single_open(file, default_affinity_show, PDE_DATA(inode));
300 }
301
302 static const struct file_operations default_affinity_proc_fops = {
303         .open           = default_affinity_open,
304         .read           = seq_read,
305         .llseek         = seq_lseek,
306         .release        = single_release,
307         .write          = default_affinity_write,
308 };
309
310 static int irq_node_proc_show(struct seq_file *m, void *v)
311 {
312         struct irq_desc *desc = irq_to_desc((long) m->private);
313
314         seq_printf(m, "%d\n", irq_desc_get_node(desc));
315         return 0;
316 }
317
318 static int irq_node_proc_open(struct inode *inode, struct file *file)
319 {
320         return single_open(file, irq_node_proc_show, PDE_DATA(inode));
321 }
322
323 static const struct file_operations irq_node_proc_fops = {
324         .open           = irq_node_proc_open,
325         .read           = seq_read,
326         .llseek         = seq_lseek,
327         .release        = single_release,
328 };
329 #endif
330
331 static int irq_spurious_proc_show(struct seq_file *m, void *v)
332 {
333         struct irq_desc *desc = irq_to_desc((long) m->private);
334
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));
338         return 0;
339 }
340
341 static int irq_spurious_proc_open(struct inode *inode, struct file *file)
342 {
343         return single_open(file, irq_spurious_proc_show, PDE_DATA(inode));
344 }
345
346 static const struct file_operations irq_spurious_proc_fops = {
347         .open           = irq_spurious_proc_open,
348         .read           = seq_read,
349         .llseek         = seq_lseek,
350         .release        = single_release,
351 };
352
353 #define MAX_NAMELEN 128
354
355 static int name_unique(unsigned int irq, struct irqaction *new_action)
356 {
357         struct irq_desc *desc = irq_to_desc(irq);
358         struct irqaction *action;
359         unsigned long flags;
360         int ret = 1;
361
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)) {
366                         ret = 0;
367                         break;
368                 }
369         }
370         raw_spin_unlock_irqrestore(&desc->lock, flags);
371         return ret;
372 }
373
374 void register_handler_proc(unsigned int irq, struct irqaction *action)
375 {
376         char name [MAX_NAMELEN];
377         struct irq_desc *desc = irq_to_desc(irq);
378
379         if (!desc->dir || action->dir || !action->name ||
380                                         !name_unique(irq, action))
381                 return;
382
383         snprintf(name, MAX_NAMELEN, "%s", action->name);
384
385         /* create /proc/irq/1234/handler/ */
386         action->dir = proc_mkdir(name, desc->dir);
387 }
388
389 #undef MAX_NAMELEN
390
391 #define MAX_NAMELEN 10
392
393 void register_irq_proc(unsigned int irq, struct irq_desc *desc)
394 {
395         static DEFINE_MUTEX(register_lock);
396         void __maybe_unused *irqp = (void *)(unsigned long) irq;
397         char name [MAX_NAMELEN];
398
399         if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
400                 return;
401
402         /*
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.
406          */
407         mutex_lock(&register_lock);
408
409         if (desc->dir)
410                 goto out_unlock;
411
412         sprintf(name, "%d", irq);
413
414         /* create /proc/irq/1234 */
415         desc->dir = proc_mkdir(name, root_irq_dir);
416         if (!desc->dir)
417                 goto out_unlock;
418
419 #ifdef CONFIG_SMP
420         /* create /proc/irq/<irq>/smp_affinity */
421         proc_create_data("smp_affinity", 0644, desc->dir,
422                          &irq_affinity_proc_fops, irqp);
423
424         /* create /proc/irq/<irq>/affinity_hint */
425         proc_create_data("affinity_hint", 0444, desc->dir,
426                          &irq_affinity_hint_proc_fops, irqp);
427
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);
431
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);
439 # endif
440 #endif
441         proc_create_data("spurious", 0444, desc->dir,
442                          &irq_spurious_proc_fops, (void *)(long)irq);
443
444 out_unlock:
445         mutex_unlock(&register_lock);
446 }
447
448 void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
449 {
450         char name [MAX_NAMELEN];
451
452         if (!root_irq_dir || !desc->dir)
453                 return;
454 #ifdef CONFIG_SMP
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);
462 # endif
463 #endif
464         remove_proc_entry("spurious", desc->dir);
465
466         sprintf(name, "%u", irq);
467         remove_proc_entry(name, root_irq_dir);
468 }
469
470 #undef MAX_NAMELEN
471
472 void unregister_handler_proc(unsigned int irq, struct irqaction *action)
473 {
474         proc_remove(action->dir);
475 }
476
477 static void register_default_affinity_proc(void)
478 {
479 #ifdef CONFIG_SMP
480         proc_create("irq/default_smp_affinity", 0644, NULL,
481                     &default_affinity_proc_fops);
482 #endif
483 }
484
485 void init_irq_proc(void)
486 {
487         unsigned int irq;
488         struct irq_desc *desc;
489
490         /* create /proc/irq */
491         root_irq_dir = proc_mkdir("irq", NULL);
492         if (!root_irq_dir)
493                 return;
494
495         register_default_affinity_proc();
496
497         /*
498          * Create entries for all existing IRQs.
499          */
500         for_each_irq_desc(irq, desc)
501                 register_irq_proc(irq, desc);
502 }
503
504 #ifdef CONFIG_GENERIC_IRQ_SHOW
505
506 int __weak arch_show_interrupts(struct seq_file *p, int prec)
507 {
508         return 0;
509 }
510
511 #ifndef ACTUAL_NR_IRQS
512 # define ACTUAL_NR_IRQS nr_irqs
513 #endif
514
515 int show_interrupts(struct seq_file *p, void *v)
516 {
517         static int prec;
518
519         unsigned long flags, any_count = 0;
520         int i = *(loff_t *) v, j;
521         struct irqaction *action;
522         struct irq_desc *desc;
523
524         if (i > ACTUAL_NR_IRQS)
525                 return 0;
526
527         if (i == ACTUAL_NR_IRQS)
528                 return arch_show_interrupts(p, prec);
529
530         /* print header and calculate the width of the first column */
531         if (i == 0) {
532                 for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
533                         j *= 10;
534
535                 seq_printf(p, "%*s", prec + 8, "");
536                 for_each_online_cpu(j)
537                         seq_printf(p, "CPU%-8d", j);
538                 seq_putc(p, '\n');
539         }
540
541         irq_lock_sparse();
542         desc = irq_to_desc(i);
543         if (!desc)
544                 goto outsparse;
545
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)
551                 goto out;
552
553         seq_printf(p, "%*d: ", prec, i);
554         for_each_online_cpu(j)
555                 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
556
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);
562                 else
563                         seq_printf(p, " %8s", "-");
564         } else {
565                 seq_printf(p, " %8s", "None");
566         }
567         if (desc->irq_data.domain)
568                 seq_printf(p, " %*d", prec, (int) desc->irq_data.hwirq);
569         else
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");
573 #endif
574         if (desc->name)
575                 seq_printf(p, "-%-8s", desc->name);
576
577         if (action) {
578                 seq_printf(p, "  %s", action->name);
579                 while ((action = action->next) != NULL)
580                         seq_printf(p, ", %s", action->name);
581         }
582
583         seq_putc(p, '\n');
584 out:
585         raw_spin_unlock_irqrestore(&desc->lock, flags);
586 outsparse:
587         irq_unlock_sparse();
588         return 0;
589 }
590 #endif