]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/irq/proc.c
userns: prevent speculative execution
[mirror_ubuntu-artful-kernel.git] / kernel / irq / proc.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/irq/proc.c
3 *
4 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
5 *
6 * This file contains the /proc/irq/ handling code.
7 */
8
9#include <linux/irq.h>
5a0e3ad6 10#include <linux/gfp.h>
1da177e4 11#include <linux/proc_fs.h>
f18e439d 12#include <linux/seq_file.h>
1da177e4 13#include <linux/interrupt.h>
c78b9b65 14#include <linux/kernel_stat.h>
95c2b175 15#include <linux/mutex.h>
1da177e4 16
97a41e26
AB
17#include "internals.h"
18
c291ee62
TG
19/*
20 * Access rules:
21 *
22 * procfs protects read/write of /proc/irq/N/ files against a
23 * concurrent free of the interrupt descriptor. remove_proc_entry()
24 * immediately prevents new read/writes to happen and waits for
25 * already running read/write functions to complete.
26 *
27 * We remove the proc entries first and then delete the interrupt
28 * descriptor from the radix tree and free it. So it is guaranteed
29 * that irq_to_desc(N) is valid as long as the read/writes are
30 * permitted by procfs.
31 *
32 * The read from /proc/interrupts is a different problem because there
33 * is no protection. So the lookup and the access to irqdesc
34 * information must be protected by sparse_irq_lock.
35 */
4a733ee1 36static struct proc_dir_entry *root_irq_dir;
1da177e4
LT
37
38#ifdef CONFIG_SMP
39
0d3f5425
TG
40enum {
41 AFFINITY,
42 AFFINITY_LIST,
43 EFFECTIVE,
44 EFFECTIVE_LIST,
45};
46
047dc633 47static int show_irq_affinity(int type, struct seq_file *m)
1da177e4 48{
08678b08 49 struct irq_desc *desc = irq_to_desc((long)m->private);
0d3f5425 50 const struct cpumask *mask;
42ee2b74 51
0d3f5425
TG
52 switch (type) {
53 case AFFINITY:
54 case AFFINITY_LIST:
55 mask = desc->irq_common_data.affinity;
42ee2b74 56#ifdef CONFIG_GENERIC_PENDING_IRQ
0d3f5425
TG
57 if (irqd_is_setaffinity_pending(&desc->irq_data))
58 mask = desc->pending_mask;
42ee2b74 59#endif
0d3f5425
TG
60 break;
61 case EFFECTIVE:
62 case EFFECTIVE_LIST:
63#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
64 mask = desc->irq_common_data.effective_affinity;
65 break;
66#else
67 return -EINVAL;
68#endif
69 };
70
71 switch (type) {
72 case AFFINITY_LIST:
73 case EFFECTIVE_LIST:
c1d7f03f 74 seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
0d3f5425
TG
75 break;
76 case AFFINITY:
77 case EFFECTIVE:
c1d7f03f 78 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
0d3f5425
TG
79 break;
80 }
f18e439d 81 return 0;
1da177e4
LT
82}
83
e7a297b0
PWJ
84static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
85{
86 struct irq_desc *desc = irq_to_desc((long)m->private);
87 unsigned long flags;
88 cpumask_var_t mask;
89
4308ad80 90 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
e7a297b0
PWJ
91 return -ENOMEM;
92
93 raw_spin_lock_irqsave(&desc->lock, flags);
94 if (desc->affinity_hint)
95 cpumask_copy(mask, desc->affinity_hint);
e7a297b0
PWJ
96 raw_spin_unlock_irqrestore(&desc->lock, flags);
97
c1d7f03f 98 seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
e7a297b0
PWJ
99 free_cpumask_var(mask);
100
101 return 0;
102}
103
25d61578
JK
104#ifndef is_affinity_mask_valid
105#define is_affinity_mask_valid(val) 1
106#endif
107
1da177e4 108int no_irq_affinity;
4b060420
MT
109static int irq_affinity_proc_show(struct seq_file *m, void *v)
110{
0d3f5425 111 return show_irq_affinity(AFFINITY, m);
4b060420
MT
112}
113
114static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
115{
0d3f5425 116 return show_irq_affinity(AFFINITY_LIST, m);
4b060420
MT
117}
118
119
120static ssize_t write_irq_affinity(int type, struct file *file,
f18e439d 121 const char __user *buffer, size_t count, loff_t *pos)
1da177e4 122{
d9dda78b 123 unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
0de26520 124 cpumask_var_t new_value;
f18e439d 125 int err;
1da177e4 126
9c255583 127 if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
1da177e4
LT
128 return -EIO;
129
0de26520
RR
130 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
131 return -ENOMEM;
132
4b060420
MT
133 if (type)
134 err = cpumask_parselist_user(buffer, count, new_value);
135 else
136 err = cpumask_parse_user(buffer, count, new_value);
1da177e4 137 if (err)
0de26520 138 goto free_cpumask;
1da177e4 139
6bdf197b 140 if (!is_affinity_mask_valid(new_value)) {
0de26520
RR
141 err = -EINVAL;
142 goto free_cpumask;
143 }
25d61578 144
1da177e4
LT
145 /*
146 * Do not allow disabling IRQs completely - it's a too easy
147 * way to make the system unusable accidentally :-) At least
148 * one online CPU still has to be targeted.
149 */
0de26520 150 if (!cpumask_intersects(new_value, cpu_online_mask)) {
cba4235e
TG
151 /*
152 * Special case for empty set - allow the architecture code
153 * to set default SMP affinity.
154 */
155 err = irq_select_affinity_usr(irq) ? -EINVAL : count;
0de26520
RR
156 } else {
157 irq_set_affinity(irq, new_value);
158 err = count;
159 }
160
161free_cpumask:
162 free_cpumask_var(new_value);
163 return err;
1da177e4
LT
164}
165
4b060420
MT
166static ssize_t irq_affinity_proc_write(struct file *file,
167 const char __user *buffer, size_t count, loff_t *pos)
168{
169 return write_irq_affinity(0, file, buffer, count, pos);
170}
171
172static ssize_t irq_affinity_list_proc_write(struct file *file,
173 const char __user *buffer, size_t count, loff_t *pos)
174{
175 return write_irq_affinity(1, file, buffer, count, pos);
176}
177
f18e439d 178static int irq_affinity_proc_open(struct inode *inode, struct file *file)
18404756 179{
d9dda78b 180 return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
18404756
MK
181}
182
4b060420
MT
183static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
184{
d9dda78b 185 return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
4b060420
MT
186}
187
e7a297b0
PWJ
188static int irq_affinity_hint_proc_open(struct inode *inode, struct file *file)
189{
d9dda78b 190 return single_open(file, irq_affinity_hint_proc_show, PDE_DATA(inode));
e7a297b0
PWJ
191}
192
f18e439d
AD
193static const struct file_operations irq_affinity_proc_fops = {
194 .open = irq_affinity_proc_open,
195 .read = seq_read,
196 .llseek = seq_lseek,
197 .release = single_release,
198 .write = irq_affinity_proc_write,
199};
200
e7a297b0
PWJ
201static const struct file_operations irq_affinity_hint_proc_fops = {
202 .open = irq_affinity_hint_proc_open,
203 .read = seq_read,
204 .llseek = seq_lseek,
205 .release = single_release,
206};
207
4b060420
MT
208static const struct file_operations irq_affinity_list_proc_fops = {
209 .open = irq_affinity_list_proc_open,
210 .read = seq_read,
211 .llseek = seq_lseek,
212 .release = single_release,
213 .write = irq_affinity_list_proc_write,
214};
215
0d3f5425
TG
216#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
217static int irq_effective_aff_proc_show(struct seq_file *m, void *v)
218{
219 return show_irq_affinity(EFFECTIVE, m);
220}
221
222static int irq_effective_aff_list_proc_show(struct seq_file *m, void *v)
223{
224 return show_irq_affinity(EFFECTIVE_LIST, m);
225}
226
227static int irq_effective_aff_proc_open(struct inode *inode, struct file *file)
228{
229 return single_open(file, irq_effective_aff_proc_show, PDE_DATA(inode));
230}
231
232static int irq_effective_aff_list_proc_open(struct inode *inode,
233 struct file *file)
234{
235 return single_open(file, irq_effective_aff_list_proc_show,
236 PDE_DATA(inode));
237}
238
239static const struct file_operations irq_effective_aff_proc_fops = {
240 .open = irq_effective_aff_proc_open,
241 .read = seq_read,
242 .llseek = seq_lseek,
243 .release = single_release,
244};
245
246static const struct file_operations irq_effective_aff_list_proc_fops = {
247 .open = irq_effective_aff_list_proc_open,
248 .read = seq_read,
249 .llseek = seq_lseek,
250 .release = single_release,
251};
252#endif
253
f18e439d
AD
254static int default_affinity_show(struct seq_file *m, void *v)
255{
c1d7f03f 256 seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
f18e439d
AD
257 return 0;
258}
259
260static ssize_t default_affinity_write(struct file *file,
261 const char __user *buffer, size_t count, loff_t *ppos)
18404756 262{
d036e67b 263 cpumask_var_t new_value;
f18e439d 264 int err;
18404756 265
d036e67b
RR
266 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
267 return -ENOMEM;
268
269 err = cpumask_parse_user(buffer, count, new_value);
18404756 270 if (err)
d036e67b 271 goto out;
18404756 272
d036e67b
RR
273 if (!is_affinity_mask_valid(new_value)) {
274 err = -EINVAL;
275 goto out;
276 }
18404756
MK
277
278 /*
279 * Do not allow disabling IRQs completely - it's a too easy
280 * way to make the system unusable accidentally :-) At least
281 * one online CPU still has to be targeted.
282 */
d036e67b
RR
283 if (!cpumask_intersects(new_value, cpu_online_mask)) {
284 err = -EINVAL;
285 goto out;
286 }
18404756 287
d036e67b
RR
288 cpumask_copy(irq_default_affinity, new_value);
289 err = count;
18404756 290
d036e67b
RR
291out:
292 free_cpumask_var(new_value);
293 return err;
18404756 294}
f18e439d
AD
295
296static int default_affinity_open(struct inode *inode, struct file *file)
297{
d9dda78b 298 return single_open(file, default_affinity_show, PDE_DATA(inode));
f18e439d
AD
299}
300
301static const struct file_operations default_affinity_proc_fops = {
302 .open = default_affinity_open,
303 .read = seq_read,
304 .llseek = seq_lseek,
305 .release = single_release,
306 .write = default_affinity_write,
307};
92d6b71a
DS
308
309static int irq_node_proc_show(struct seq_file *m, void *v)
310{
311 struct irq_desc *desc = irq_to_desc((long) m->private);
312
6783011b 313 seq_printf(m, "%d\n", irq_desc_get_node(desc));
92d6b71a
DS
314 return 0;
315}
316
317static int irq_node_proc_open(struct inode *inode, struct file *file)
318{
d9dda78b 319 return single_open(file, irq_node_proc_show, PDE_DATA(inode));
92d6b71a
DS
320}
321
322static const struct file_operations irq_node_proc_fops = {
323 .open = irq_node_proc_open,
324 .read = seq_read,
325 .llseek = seq_lseek,
326 .release = single_release,
327};
1da177e4
LT
328#endif
329
a1afb637 330static int irq_spurious_proc_show(struct seq_file *m, void *v)
96d97cf0 331{
a1afb637
AD
332 struct irq_desc *desc = irq_to_desc((long) m->private);
333
334 seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
335 desc->irq_count, desc->irqs_unhandled,
336 jiffies_to_msecs(desc->last_unhandled));
337 return 0;
338}
339
340static int irq_spurious_proc_open(struct inode *inode, struct file *file)
341{
d9dda78b 342 return single_open(file, irq_spurious_proc_show, PDE_DATA(inode));
96d97cf0
AK
343}
344
a1afb637
AD
345static const struct file_operations irq_spurious_proc_fops = {
346 .open = irq_spurious_proc_open,
347 .read = seq_read,
348 .llseek = seq_lseek,
349 .release = single_release,
350};
351
1da177e4
LT
352#define MAX_NAMELEN 128
353
354static int name_unique(unsigned int irq, struct irqaction *new_action)
355{
08678b08 356 struct irq_desc *desc = irq_to_desc(irq);
1da177e4 357 struct irqaction *action;
d2d9433a
DA
358 unsigned long flags;
359 int ret = 1;
1da177e4 360
239007b8 361 raw_spin_lock_irqsave(&desc->lock, flags);
f944b5a7 362 for_each_action_of_desc(desc, action) {
1da177e4 363 if ((action != new_action) && action->name &&
d2d9433a
DA
364 !strcmp(new_action->name, action->name)) {
365 ret = 0;
366 break;
367 }
368 }
239007b8 369 raw_spin_unlock_irqrestore(&desc->lock, flags);
d2d9433a 370 return ret;
1da177e4
LT
371}
372
373void register_handler_proc(unsigned int irq, struct irqaction *action)
374{
375 char name [MAX_NAMELEN];
08678b08 376 struct irq_desc *desc = irq_to_desc(irq);
1da177e4 377
08678b08 378 if (!desc->dir || action->dir || !action->name ||
1da177e4
LT
379 !name_unique(irq, action))
380 return;
381
1da177e4
LT
382 snprintf(name, MAX_NAMELEN, "%s", action->name);
383
384 /* create /proc/irq/1234/handler/ */
08678b08 385 action->dir = proc_mkdir(name, desc->dir);
1da177e4
LT
386}
387
388#undef MAX_NAMELEN
389
390#define MAX_NAMELEN 10
391
2c6927a3 392void register_irq_proc(unsigned int irq, struct irq_desc *desc)
1da177e4 393{
95c2b175 394 static DEFINE_MUTEX(register_lock);
c1a80386 395 void __maybe_unused *irqp = (void *)(unsigned long) irq;
1da177e4
LT
396 char name [MAX_NAMELEN];
397
95c2b175 398 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
1da177e4
LT
399 return;
400
95c2b175
BH
401 /*
402 * irq directories are registered only when a handler is
403 * added, not when the descriptor is created, so multiple
404 * tasks might try to register at the same time.
405 */
406 mutex_lock(&register_lock);
407
408 if (desc->dir)
409 goto out_unlock;
410
1da177e4
LT
411 sprintf(name, "%d", irq);
412
413 /* create /proc/irq/1234 */
08678b08 414 desc->dir = proc_mkdir(name, root_irq_dir);
c82a43d4 415 if (!desc->dir)
95c2b175 416 goto out_unlock;
1da177e4
LT
417
418#ifdef CONFIG_SMP
f18e439d 419 /* create /proc/irq/<irq>/smp_affinity */
bab5c790 420 proc_create_data("smp_affinity", 0644, desc->dir,
c1a80386 421 &irq_affinity_proc_fops, irqp);
92d6b71a 422
e7a297b0 423 /* create /proc/irq/<irq>/affinity_hint */
bab5c790 424 proc_create_data("affinity_hint", 0444, desc->dir,
c1a80386 425 &irq_affinity_hint_proc_fops, irqp);
e7a297b0 426
4b060420 427 /* create /proc/irq/<irq>/smp_affinity_list */
bab5c790 428 proc_create_data("smp_affinity_list", 0644, desc->dir,
c1a80386 429 &irq_affinity_list_proc_fops, irqp);
4b060420 430
92d6b71a 431 proc_create_data("node", 0444, desc->dir,
c1a80386 432 &irq_node_proc_fops, irqp);
0d3f5425
TG
433# ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
434 proc_create_data("effective_affinity", 0444, desc->dir,
435 &irq_effective_aff_proc_fops, irqp);
436 proc_create_data("effective_affinity_list", 0444, desc->dir,
437 &irq_effective_aff_list_proc_fops, irqp);
438# endif
1da177e4 439#endif
a1afb637
AD
440 proc_create_data("spurious", 0444, desc->dir,
441 &irq_spurious_proc_fops, (void *)(long)irq);
95c2b175
BH
442
443out_unlock:
444 mutex_unlock(&register_lock);
1da177e4
LT
445}
446
13bfe99e
TG
447void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
448{
449 char name [MAX_NAMELEN];
450
451 if (!root_irq_dir || !desc->dir)
452 return;
453#ifdef CONFIG_SMP
454 remove_proc_entry("smp_affinity", desc->dir);
455 remove_proc_entry("affinity_hint", desc->dir);
def945ee 456 remove_proc_entry("smp_affinity_list", desc->dir);
13bfe99e 457 remove_proc_entry("node", desc->dir);
0d3f5425
TG
458# ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
459 remove_proc_entry("effective_affinity", desc->dir);
460 remove_proc_entry("effective_affinity_list", desc->dir);
461# endif
13bfe99e
TG
462#endif
463 remove_proc_entry("spurious", desc->dir);
464
13bfe99e
TG
465 sprintf(name, "%u", irq);
466 remove_proc_entry(name, root_irq_dir);
467}
468
1da177e4
LT
469#undef MAX_NAMELEN
470
471void unregister_handler_proc(unsigned int irq, struct irqaction *action)
472{
a8ca16ea 473 proc_remove(action->dir);
1da177e4
LT
474}
475
3786fc71 476static void register_default_affinity_proc(void)
18404756
MK
477{
478#ifdef CONFIG_SMP
bab5c790 479 proc_create("irq/default_smp_affinity", 0644, NULL,
f18e439d 480 &default_affinity_proc_fops);
18404756
MK
481#endif
482}
483
1da177e4
LT
484void init_irq_proc(void)
485{
2c6927a3
YL
486 unsigned int irq;
487 struct irq_desc *desc;
1da177e4
LT
488
489 /* create /proc/irq */
490 root_irq_dir = proc_mkdir("irq", NULL);
491 if (!root_irq_dir)
492 return;
493
18404756
MK
494 register_default_affinity_proc();
495
1da177e4
LT
496 /*
497 * Create entries for all existing IRQs.
498 */
fe3464ca 499 for_each_irq_desc(irq, desc)
2c6927a3 500 register_irq_proc(irq, desc);
1da177e4
LT
501}
502
c78b9b65
TG
503#ifdef CONFIG_GENERIC_IRQ_SHOW
504
505int __weak arch_show_interrupts(struct seq_file *p, int prec)
506{
507 return 0;
508}
509
a6e120ed
TG
510#ifndef ACTUAL_NR_IRQS
511# define ACTUAL_NR_IRQS nr_irqs
512#endif
513
c78b9b65
TG
514int show_interrupts(struct seq_file *p, void *v)
515{
516 static int prec;
517
518 unsigned long flags, any_count = 0;
519 int i = *(loff_t *) v, j;
520 struct irqaction *action;
521 struct irq_desc *desc;
522
a6e120ed 523 if (i > ACTUAL_NR_IRQS)
c78b9b65
TG
524 return 0;
525
a6e120ed 526 if (i == ACTUAL_NR_IRQS)
c78b9b65
TG
527 return arch_show_interrupts(p, prec);
528
529 /* print header and calculate the width of the first column */
530 if (i == 0) {
531 for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
532 j *= 10;
533
534 seq_printf(p, "%*s", prec + 8, "");
535 for_each_online_cpu(j)
536 seq_printf(p, "CPU%-8d", j);
537 seq_putc(p, '\n');
538 }
539
c291ee62 540 irq_lock_sparse();
c78b9b65
TG
541 desc = irq_to_desc(i);
542 if (!desc)
c291ee62 543 goto outsparse;
c78b9b65
TG
544
545 raw_spin_lock_irqsave(&desc->lock, flags);
546 for_each_online_cpu(j)
547 any_count |= kstat_irqs_cpu(i, j);
548 action = desc->action;
4717f133 549 if ((!action || irq_desc_is_chained(desc)) && !any_count)
c78b9b65
TG
550 goto out;
551
552 seq_printf(p, "%*d: ", prec, i);
553 for_each_online_cpu(j)
554 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
ab7798ff
TG
555
556 if (desc->irq_data.chip) {
557 if (desc->irq_data.chip->irq_print_chip)
558 desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
559 else if (desc->irq_data.chip->name)
560 seq_printf(p, " %8s", desc->irq_data.chip->name);
561 else
562 seq_printf(p, " %8s", "-");
563 } else {
564 seq_printf(p, " %8s", "None");
565 }
c12d2f42
GL
566 if (desc->irq_data.domain)
567 seq_printf(p, " %*d", prec, (int) desc->irq_data.hwirq);
f435da41
HS
568 else
569 seq_printf(p, " %*s", prec, "");
94b2c363 570#ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
ab7798ff
TG
571 seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
572#endif
ee0401ec
TG
573 if (desc->name)
574 seq_printf(p, "-%-8s", desc->name);
c78b9b65
TG
575
576 if (action) {
577 seq_printf(p, " %s", action->name);
578 while ((action = action->next) != NULL)
579 seq_printf(p, ", %s", action->name);
580 }
581
582 seq_putc(p, '\n');
583out:
584 raw_spin_unlock_irqrestore(&desc->lock, flags);
c291ee62
TG
585outsparse:
586 irq_unlock_sparse();
c78b9b65
TG
587 return 0;
588}
589#endif