GNU Linux-libre 4.19.211-gnu1
[releases.git] / kernel / irq / proc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
4  *
5  * This file contains the /proc/irq/ handling code.
6  */
7
8 #include <linux/irq.h>
9 #include <linux/gfp.h>
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>
15
16 #include "internals.h"
17
18 /*
19  * Access rules:
20  *
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.
25  *
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.
30  *
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.
34  */
35 static struct proc_dir_entry *root_irq_dir;
36
37 #ifdef CONFIG_SMP
38
39 enum {
40         AFFINITY,
41         AFFINITY_LIST,
42         EFFECTIVE,
43         EFFECTIVE_LIST,
44 };
45
46 static int show_irq_affinity(int type, struct seq_file *m)
47 {
48         struct irq_desc *desc = irq_to_desc((long)m->private);
49         const struct cpumask *mask;
50
51         switch (type) {
52         case AFFINITY:
53         case AFFINITY_LIST:
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;
58 #endif
59                 break;
60         case EFFECTIVE:
61         case EFFECTIVE_LIST:
62 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
63                 mask = irq_data_get_effective_affinity_mask(&desc->irq_data);
64                 break;
65 #endif
66         default:
67                 return -EINVAL;
68         }
69
70         switch (type) {
71         case AFFINITY_LIST:
72         case EFFECTIVE_LIST:
73                 seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
74                 break;
75         case AFFINITY:
76         case EFFECTIVE:
77                 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
78                 break;
79         }
80         return 0;
81 }
82
83 static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
84 {
85         struct irq_desc *desc = irq_to_desc((long)m->private);
86         unsigned long flags;
87         cpumask_var_t mask;
88
89         if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
90                 return -ENOMEM;
91
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);
96
97         seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
98         free_cpumask_var(mask);
99
100         return 0;
101 }
102
103 #ifndef is_affinity_mask_valid
104 #define is_affinity_mask_valid(val) 1
105 #endif
106
107 int no_irq_affinity;
108 static int irq_affinity_proc_show(struct seq_file *m, void *v)
109 {
110         return show_irq_affinity(AFFINITY, m);
111 }
112
113 static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
114 {
115         return show_irq_affinity(AFFINITY_LIST, m);
116 }
117
118 #ifndef CONFIG_AUTO_IRQ_AFFINITY
119 static inline int irq_select_affinity_usr(unsigned int irq)
120 {
121         /*
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.
126          *
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.
130          */
131         return -EINVAL;
132 }
133 #else
134 /* ALPHA magic affinity auto selector. Keep it for historical reasons. */
135 static inline int irq_select_affinity_usr(unsigned int irq)
136 {
137         return irq_select_affinity(irq);
138 }
139 #endif
140
141 static ssize_t write_irq_affinity(int type, struct file *file,
142                 const char __user *buffer, size_t count, loff_t *pos)
143 {
144         unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
145         cpumask_var_t new_value;
146         int err;
147
148         if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
149                 return -EIO;
150
151         if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
152                 return -ENOMEM;
153
154         if (type)
155                 err = cpumask_parselist_user(buffer, count, new_value);
156         else
157                 err = cpumask_parse_user(buffer, count, new_value);
158         if (err)
159                 goto free_cpumask;
160
161         if (!is_affinity_mask_valid(new_value)) {
162                 err = -EINVAL;
163                 goto free_cpumask;
164         }
165
166         /*
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.
170          */
171         if (!cpumask_intersects(new_value, cpu_online_mask)) {
172                 /*
173                  * Special case for empty set - allow the architecture code
174                  * to set default SMP affinity.
175                  */
176                 err = irq_select_affinity_usr(irq) ? -EINVAL : count;
177         } else {
178                 err = irq_set_affinity(irq, new_value);
179                 if (!err)
180                         err = count;
181         }
182
183 free_cpumask:
184         free_cpumask_var(new_value);
185         return err;
186 }
187
188 static ssize_t irq_affinity_proc_write(struct file *file,
189                 const char __user *buffer, size_t count, loff_t *pos)
190 {
191         return write_irq_affinity(0, file, buffer, count, pos);
192 }
193
194 static ssize_t irq_affinity_list_proc_write(struct file *file,
195                 const char __user *buffer, size_t count, loff_t *pos)
196 {
197         return write_irq_affinity(1, file, buffer, count, pos);
198 }
199
200 static int irq_affinity_proc_open(struct inode *inode, struct file *file)
201 {
202         return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
203 }
204
205 static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
206 {
207         return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
208 }
209
210 static const struct file_operations irq_affinity_proc_fops = {
211         .open           = irq_affinity_proc_open,
212         .read           = seq_read,
213         .llseek         = seq_lseek,
214         .release        = single_release,
215         .write          = irq_affinity_proc_write,
216 };
217
218 static const struct file_operations irq_affinity_list_proc_fops = {
219         .open           = irq_affinity_list_proc_open,
220         .read           = seq_read,
221         .llseek         = seq_lseek,
222         .release        = single_release,
223         .write          = irq_affinity_list_proc_write,
224 };
225
226 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
227 static int irq_effective_aff_proc_show(struct seq_file *m, void *v)
228 {
229         return show_irq_affinity(EFFECTIVE, m);
230 }
231
232 static int irq_effective_aff_list_proc_show(struct seq_file *m, void *v)
233 {
234         return show_irq_affinity(EFFECTIVE_LIST, m);
235 }
236 #endif
237
238 static int default_affinity_show(struct seq_file *m, void *v)
239 {
240         seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
241         return 0;
242 }
243
244 static ssize_t default_affinity_write(struct file *file,
245                 const char __user *buffer, size_t count, loff_t *ppos)
246 {
247         cpumask_var_t new_value;
248         int err;
249
250         if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
251                 return -ENOMEM;
252
253         err = cpumask_parse_user(buffer, count, new_value);
254         if (err)
255                 goto out;
256
257         if (!is_affinity_mask_valid(new_value)) {
258                 err = -EINVAL;
259                 goto out;
260         }
261
262         /*
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.
266          */
267         if (!cpumask_intersects(new_value, cpu_online_mask)) {
268                 err = -EINVAL;
269                 goto out;
270         }
271
272         cpumask_copy(irq_default_affinity, new_value);
273         err = count;
274
275 out:
276         free_cpumask_var(new_value);
277         return err;
278 }
279
280 static int default_affinity_open(struct inode *inode, struct file *file)
281 {
282         return single_open(file, default_affinity_show, PDE_DATA(inode));
283 }
284
285 static const struct file_operations default_affinity_proc_fops = {
286         .open           = default_affinity_open,
287         .read           = seq_read,
288         .llseek         = seq_lseek,
289         .release        = single_release,
290         .write          = default_affinity_write,
291 };
292
293 static int irq_node_proc_show(struct seq_file *m, void *v)
294 {
295         struct irq_desc *desc = irq_to_desc((long) m->private);
296
297         seq_printf(m, "%d\n", irq_desc_get_node(desc));
298         return 0;
299 }
300 #endif
301
302 static int irq_spurious_proc_show(struct seq_file *m, void *v)
303 {
304         struct irq_desc *desc = irq_to_desc((long) m->private);
305
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));
309         return 0;
310 }
311
312 #define MAX_NAMELEN 128
313
314 static int name_unique(unsigned int irq, struct irqaction *new_action)
315 {
316         struct irq_desc *desc = irq_to_desc(irq);
317         struct irqaction *action;
318         unsigned long flags;
319         int ret = 1;
320
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)) {
325                         ret = 0;
326                         break;
327                 }
328         }
329         raw_spin_unlock_irqrestore(&desc->lock, flags);
330         return ret;
331 }
332
333 void register_handler_proc(unsigned int irq, struct irqaction *action)
334 {
335         char name [MAX_NAMELEN];
336         struct irq_desc *desc = irq_to_desc(irq);
337
338         if (!desc->dir || action->dir || !action->name ||
339                                         !name_unique(irq, action))
340                 return;
341
342         snprintf(name, MAX_NAMELEN, "%s", action->name);
343
344         /* create /proc/irq/1234/handler/ */
345         action->dir = proc_mkdir(name, desc->dir);
346 }
347
348 #undef MAX_NAMELEN
349
350 #define MAX_NAMELEN 10
351
352 void register_irq_proc(unsigned int irq, struct irq_desc *desc)
353 {
354         static DEFINE_MUTEX(register_lock);
355         void __maybe_unused *irqp = (void *)(unsigned long) irq;
356         char name [MAX_NAMELEN];
357
358         if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
359                 return;
360
361         /*
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.
365          */
366         mutex_lock(&register_lock);
367
368         if (desc->dir)
369                 goto out_unlock;
370
371         sprintf(name, "%d", irq);
372
373         /* create /proc/irq/1234 */
374         desc->dir = proc_mkdir(name, root_irq_dir);
375         if (!desc->dir)
376                 goto out_unlock;
377
378 #ifdef CONFIG_SMP
379         /* create /proc/irq/<irq>/smp_affinity */
380         proc_create_data("smp_affinity", 0644, desc->dir,
381                          &irq_affinity_proc_fops, irqp);
382
383         /* create /proc/irq/<irq>/affinity_hint */
384         proc_create_single_data("affinity_hint", 0444, desc->dir,
385                         irq_affinity_hint_proc_show, irqp);
386
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);
390
391         proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show,
392                         irqp);
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);
398 # endif
399 #endif
400         proc_create_single_data("spurious", 0444, desc->dir,
401                         irq_spurious_proc_show, (void *)(long)irq);
402
403 out_unlock:
404         mutex_unlock(&register_lock);
405 }
406
407 void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
408 {
409         char name [MAX_NAMELEN];
410
411         if (!root_irq_dir || !desc->dir)
412                 return;
413 #ifdef CONFIG_SMP
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);
421 # endif
422 #endif
423         remove_proc_entry("spurious", desc->dir);
424
425         sprintf(name, "%u", irq);
426         remove_proc_entry(name, root_irq_dir);
427 }
428
429 #undef MAX_NAMELEN
430
431 void unregister_handler_proc(unsigned int irq, struct irqaction *action)
432 {
433         proc_remove(action->dir);
434 }
435
436 static void register_default_affinity_proc(void)
437 {
438 #ifdef CONFIG_SMP
439         proc_create("irq/default_smp_affinity", 0644, NULL,
440                     &default_affinity_proc_fops);
441 #endif
442 }
443
444 void init_irq_proc(void)
445 {
446         unsigned int irq;
447         struct irq_desc *desc;
448
449         /* create /proc/irq */
450         root_irq_dir = proc_mkdir("irq", NULL);
451         if (!root_irq_dir)
452                 return;
453
454         register_default_affinity_proc();
455
456         /*
457          * Create entries for all existing IRQs.
458          */
459         for_each_irq_desc(irq, desc)
460                 register_irq_proc(irq, desc);
461 }
462
463 #ifdef CONFIG_GENERIC_IRQ_SHOW
464
465 int __weak arch_show_interrupts(struct seq_file *p, int prec)
466 {
467         return 0;
468 }
469
470 #ifndef ACTUAL_NR_IRQS
471 # define ACTUAL_NR_IRQS nr_irqs
472 #endif
473
474 int show_interrupts(struct seq_file *p, void *v)
475 {
476         static int prec;
477
478         unsigned long flags, any_count = 0;
479         int i = *(loff_t *) v, j;
480         struct irqaction *action;
481         struct irq_desc *desc;
482
483         if (i > ACTUAL_NR_IRQS)
484                 return 0;
485
486         if (i == ACTUAL_NR_IRQS)
487                 return arch_show_interrupts(p, prec);
488
489         /* print header and calculate the width of the first column */
490         if (i == 0) {
491                 for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
492                         j *= 10;
493
494                 seq_printf(p, "%*s", prec + 8, "");
495                 for_each_online_cpu(j)
496                         seq_printf(p, "CPU%-8d", j);
497                 seq_putc(p, '\n');
498         }
499
500         rcu_read_lock();
501         desc = irq_to_desc(i);
502         if (!desc)
503                 goto outsparse;
504
505         if (desc->kstat_irqs)
506                 for_each_online_cpu(j)
507                         any_count |= *per_cpu_ptr(desc->kstat_irqs, j);
508
509         if ((!desc->action || irq_desc_is_chained(desc)) && !any_count)
510                 goto outsparse;
511
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);
516
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);
523                 else
524                         seq_printf(p, " %8s", "-");
525         } else {
526                 seq_printf(p, " %8s", "None");
527         }
528         if (desc->irq_data.domain)
529                 seq_printf(p, " %*d", prec, (int) desc->irq_data.hwirq);
530         else
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");
534 #endif
535         if (desc->name)
536                 seq_printf(p, "-%-8s", desc->name);
537
538         action = desc->action;
539         if (action) {
540                 seq_printf(p, "  %s", action->name);
541                 while ((action = action->next) != NULL)
542                         seq_printf(p, ", %s", action->name);
543         }
544
545         seq_putc(p, '\n');
546         raw_spin_unlock_irqrestore(&desc->lock, flags);
547 outsparse:
548         rcu_read_unlock();
549         return 0;
550 }
551 #endif