genirq: Fix race in register_irq_proc()
[pandora-kernel.git] / kernel / irq / proc.c
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>
10 #include <linux/gfp.h>
11 #include <linux/proc_fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/mutex.h>
16
17 #include "internals.h"
18
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  */
36 static struct proc_dir_entry *root_irq_dir;
37
38 #ifdef CONFIG_SMP
39
40 static int show_irq_affinity(int type, struct seq_file *m, void *v)
41 {
42         struct irq_desc *desc = irq_to_desc((long)m->private);
43         const struct cpumask *mask = desc->irq_data.affinity;
44
45 #ifdef CONFIG_GENERIC_PENDING_IRQ
46         if (irqd_is_setaffinity_pending(&desc->irq_data))
47                 mask = desc->pending_mask;
48 #endif
49         if (type)
50                 seq_cpumask_list(m, mask);
51         else
52                 seq_cpumask(m, mask);
53         seq_putc(m, '\n');
54         return 0;
55 }
56
57 static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
58 {
59         struct irq_desc *desc = irq_to_desc((long)m->private);
60         unsigned long flags;
61         cpumask_var_t mask;
62
63         if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
64                 return -ENOMEM;
65
66         raw_spin_lock_irqsave(&desc->lock, flags);
67         if (desc->affinity_hint)
68                 cpumask_copy(mask, desc->affinity_hint);
69         raw_spin_unlock_irqrestore(&desc->lock, flags);
70
71         seq_cpumask(m, mask);
72         seq_putc(m, '\n');
73         free_cpumask_var(mask);
74
75         return 0;
76 }
77
78 #ifndef is_affinity_mask_valid
79 #define is_affinity_mask_valid(val) 1
80 #endif
81
82 int no_irq_affinity;
83 static int irq_affinity_proc_show(struct seq_file *m, void *v)
84 {
85         return show_irq_affinity(0, m, v);
86 }
87
88 static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
89 {
90         return show_irq_affinity(1, m, v);
91 }
92
93
94 static ssize_t write_irq_affinity(int type, struct file *file,
95                 const char __user *buffer, size_t count, loff_t *pos)
96 {
97         unsigned int irq = (int)(long)PDE(file->f_path.dentry->d_inode)->data;
98         cpumask_var_t new_value;
99         int err;
100
101         if (!irq_can_set_affinity(irq) || no_irq_affinity)
102                 return -EIO;
103
104         if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
105                 return -ENOMEM;
106
107         if (type)
108                 err = cpumask_parselist_user(buffer, count, new_value);
109         else
110                 err = cpumask_parse_user(buffer, count, new_value);
111         if (err)
112                 goto free_cpumask;
113
114         if (!is_affinity_mask_valid(new_value)) {
115                 err = -EINVAL;
116                 goto free_cpumask;
117         }
118
119         /*
120          * Do not allow disabling IRQs completely - it's a too easy
121          * way to make the system unusable accidentally :-) At least
122          * one online CPU still has to be targeted.
123          */
124         if (!cpumask_intersects(new_value, cpu_online_mask)) {
125                 /* Special case for empty set - allow the architecture
126                    code to set default SMP affinity. */
127                 err = irq_select_affinity_usr(irq, new_value) ? -EINVAL : count;
128         } else {
129                 irq_set_affinity(irq, new_value);
130                 err = count;
131         }
132
133 free_cpumask:
134         free_cpumask_var(new_value);
135         return err;
136 }
137
138 static ssize_t irq_affinity_proc_write(struct file *file,
139                 const char __user *buffer, size_t count, loff_t *pos)
140 {
141         return write_irq_affinity(0, file, buffer, count, pos);
142 }
143
144 static ssize_t irq_affinity_list_proc_write(struct file *file,
145                 const char __user *buffer, size_t count, loff_t *pos)
146 {
147         return write_irq_affinity(1, file, buffer, count, pos);
148 }
149
150 static int irq_affinity_proc_open(struct inode *inode, struct file *file)
151 {
152         return single_open(file, irq_affinity_proc_show, PDE(inode)->data);
153 }
154
155 static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
156 {
157         return single_open(file, irq_affinity_list_proc_show, PDE(inode)->data);
158 }
159
160 static int irq_affinity_hint_proc_open(struct inode *inode, struct file *file)
161 {
162         return single_open(file, irq_affinity_hint_proc_show, PDE(inode)->data);
163 }
164
165 static const struct file_operations irq_affinity_proc_fops = {
166         .open           = irq_affinity_proc_open,
167         .read           = seq_read,
168         .llseek         = seq_lseek,
169         .release        = single_release,
170         .write          = irq_affinity_proc_write,
171 };
172
173 static const struct file_operations irq_affinity_hint_proc_fops = {
174         .open           = irq_affinity_hint_proc_open,
175         .read           = seq_read,
176         .llseek         = seq_lseek,
177         .release        = single_release,
178 };
179
180 static const struct file_operations irq_affinity_list_proc_fops = {
181         .open           = irq_affinity_list_proc_open,
182         .read           = seq_read,
183         .llseek         = seq_lseek,
184         .release        = single_release,
185         .write          = irq_affinity_list_proc_write,
186 };
187
188 static int default_affinity_show(struct seq_file *m, void *v)
189 {
190         seq_cpumask(m, irq_default_affinity);
191         seq_putc(m, '\n');
192         return 0;
193 }
194
195 static ssize_t default_affinity_write(struct file *file,
196                 const char __user *buffer, size_t count, loff_t *ppos)
197 {
198         cpumask_var_t new_value;
199         int err;
200
201         if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
202                 return -ENOMEM;
203
204         err = cpumask_parse_user(buffer, count, new_value);
205         if (err)
206                 goto out;
207
208         if (!is_affinity_mask_valid(new_value)) {
209                 err = -EINVAL;
210                 goto out;
211         }
212
213         /*
214          * Do not allow disabling IRQs completely - it's a too easy
215          * way to make the system unusable accidentally :-) At least
216          * one online CPU still has to be targeted.
217          */
218         if (!cpumask_intersects(new_value, cpu_online_mask)) {
219                 err = -EINVAL;
220                 goto out;
221         }
222
223         cpumask_copy(irq_default_affinity, new_value);
224         err = count;
225
226 out:
227         free_cpumask_var(new_value);
228         return err;
229 }
230
231 static int default_affinity_open(struct inode *inode, struct file *file)
232 {
233         return single_open(file, default_affinity_show, PDE(inode)->data);
234 }
235
236 static const struct file_operations default_affinity_proc_fops = {
237         .open           = default_affinity_open,
238         .read           = seq_read,
239         .llseek         = seq_lseek,
240         .release        = single_release,
241         .write          = default_affinity_write,
242 };
243
244 static int irq_node_proc_show(struct seq_file *m, void *v)
245 {
246         struct irq_desc *desc = irq_to_desc((long) m->private);
247
248         seq_printf(m, "%d\n", desc->irq_data.node);
249         return 0;
250 }
251
252 static int irq_node_proc_open(struct inode *inode, struct file *file)
253 {
254         return single_open(file, irq_node_proc_show, PDE(inode)->data);
255 }
256
257 static const struct file_operations irq_node_proc_fops = {
258         .open           = irq_node_proc_open,
259         .read           = seq_read,
260         .llseek         = seq_lseek,
261         .release        = single_release,
262 };
263 #endif
264
265 static int irq_spurious_proc_show(struct seq_file *m, void *v)
266 {
267         struct irq_desc *desc = irq_to_desc((long) m->private);
268
269         seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
270                    desc->irq_count, desc->irqs_unhandled,
271                    jiffies_to_msecs(desc->last_unhandled));
272         return 0;
273 }
274
275 static int irq_spurious_proc_open(struct inode *inode, struct file *file)
276 {
277         return single_open(file, irq_spurious_proc_show, PDE(inode)->data);
278 }
279
280 static const struct file_operations irq_spurious_proc_fops = {
281         .open           = irq_spurious_proc_open,
282         .read           = seq_read,
283         .llseek         = seq_lseek,
284         .release        = single_release,
285 };
286
287 #define MAX_NAMELEN 128
288
289 static int name_unique(unsigned int irq, struct irqaction *new_action)
290 {
291         struct irq_desc *desc = irq_to_desc(irq);
292         struct irqaction *action;
293         unsigned long flags;
294         int ret = 1;
295
296         raw_spin_lock_irqsave(&desc->lock, flags);
297         for (action = desc->action ; action; action = action->next) {
298                 if ((action != new_action) && action->name &&
299                                 !strcmp(new_action->name, action->name)) {
300                         ret = 0;
301                         break;
302                 }
303         }
304         raw_spin_unlock_irqrestore(&desc->lock, flags);
305         return ret;
306 }
307
308 void register_handler_proc(unsigned int irq, struct irqaction *action)
309 {
310         char name [MAX_NAMELEN];
311         struct irq_desc *desc = irq_to_desc(irq);
312
313         if (!desc->dir || action->dir || !action->name ||
314                                         !name_unique(irq, action))
315                 return;
316
317         memset(name, 0, MAX_NAMELEN);
318         snprintf(name, MAX_NAMELEN, "%s", action->name);
319
320         /* create /proc/irq/1234/handler/ */
321         action->dir = proc_mkdir(name, desc->dir);
322 }
323
324 #undef MAX_NAMELEN
325
326 #define MAX_NAMELEN 10
327
328 void register_irq_proc(unsigned int irq, struct irq_desc *desc)
329 {
330         static DEFINE_MUTEX(register_lock);
331         char name [MAX_NAMELEN];
332
333         if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
334                 return;
335
336         /*
337          * irq directories are registered only when a handler is
338          * added, not when the descriptor is created, so multiple
339          * tasks might try to register at the same time.
340          */
341         mutex_lock(&register_lock);
342
343         if (desc->dir)
344                 goto out_unlock;
345
346         memset(name, 0, MAX_NAMELEN);
347         sprintf(name, "%d", irq);
348
349         /* create /proc/irq/1234 */
350         desc->dir = proc_mkdir(name, root_irq_dir);
351         if (!desc->dir)
352                 goto out_unlock;
353
354 #ifdef CONFIG_SMP
355         /* create /proc/irq/<irq>/smp_affinity */
356         proc_create_data("smp_affinity", 0600, desc->dir,
357                          &irq_affinity_proc_fops, (void *)(long)irq);
358
359         /* create /proc/irq/<irq>/affinity_hint */
360         proc_create_data("affinity_hint", 0400, desc->dir,
361                          &irq_affinity_hint_proc_fops, (void *)(long)irq);
362
363         /* create /proc/irq/<irq>/smp_affinity_list */
364         proc_create_data("smp_affinity_list", 0600, desc->dir,
365                          &irq_affinity_list_proc_fops, (void *)(long)irq);
366
367         proc_create_data("node", 0444, desc->dir,
368                          &irq_node_proc_fops, (void *)(long)irq);
369 #endif
370
371         proc_create_data("spurious", 0444, desc->dir,
372                          &irq_spurious_proc_fops, (void *)(long)irq);
373
374 out_unlock:
375         mutex_unlock(&register_lock);
376 }
377
378 void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
379 {
380         char name [MAX_NAMELEN];
381
382         if (!root_irq_dir || !desc->dir)
383                 return;
384 #ifdef CONFIG_SMP
385         remove_proc_entry("smp_affinity", desc->dir);
386         remove_proc_entry("affinity_hint", desc->dir);
387         remove_proc_entry("smp_affinity_list", desc->dir);
388         remove_proc_entry("node", desc->dir);
389 #endif
390         remove_proc_entry("spurious", desc->dir);
391
392         memset(name, 0, MAX_NAMELEN);
393         sprintf(name, "%u", irq);
394         remove_proc_entry(name, root_irq_dir);
395 }
396
397 #undef MAX_NAMELEN
398
399 void unregister_handler_proc(unsigned int irq, struct irqaction *action)
400 {
401         if (action->dir) {
402                 struct irq_desc *desc = irq_to_desc(irq);
403
404                 remove_proc_entry(action->dir->name, desc->dir);
405         }
406 }
407
408 static void register_default_affinity_proc(void)
409 {
410 #ifdef CONFIG_SMP
411         proc_create("irq/default_smp_affinity", 0600, NULL,
412                     &default_affinity_proc_fops);
413 #endif
414 }
415
416 void init_irq_proc(void)
417 {
418         unsigned int irq;
419         struct irq_desc *desc;
420
421         /* create /proc/irq */
422         root_irq_dir = proc_mkdir("irq", NULL);
423         if (!root_irq_dir)
424                 return;
425
426         register_default_affinity_proc();
427
428         /*
429          * Create entries for all existing IRQs.
430          */
431         for_each_irq_desc(irq, desc) {
432                 if (!desc)
433                         continue;
434
435                 register_irq_proc(irq, desc);
436         }
437 }
438
439 #ifdef CONFIG_GENERIC_IRQ_SHOW
440
441 int __weak arch_show_interrupts(struct seq_file *p, int prec)
442 {
443         return 0;
444 }
445
446 #ifndef ACTUAL_NR_IRQS
447 # define ACTUAL_NR_IRQS nr_irqs
448 #endif
449
450 int show_interrupts(struct seq_file *p, void *v)
451 {
452         static int prec;
453
454         unsigned long flags, any_count = 0;
455         int i = *(loff_t *) v, j;
456         struct irqaction *action;
457         struct irq_desc *desc;
458
459         if (i > ACTUAL_NR_IRQS)
460                 return 0;
461
462         if (i == ACTUAL_NR_IRQS)
463                 return arch_show_interrupts(p, prec);
464
465         /* print header and calculate the width of the first column */
466         if (i == 0) {
467                 for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
468                         j *= 10;
469
470                 seq_printf(p, "%*s", prec + 8, "");
471                 for_each_online_cpu(j)
472                         seq_printf(p, "CPU%-8d", j);
473                 seq_putc(p, '\n');
474         }
475
476         irq_lock_sparse();
477         desc = irq_to_desc(i);
478         if (!desc)
479                 goto outsparse;
480
481         raw_spin_lock_irqsave(&desc->lock, flags);
482         for_each_online_cpu(j)
483                 any_count |= kstat_irqs_cpu(i, j);
484         action = desc->action;
485         if (!action && !any_count)
486                 goto out;
487
488         seq_printf(p, "%*d: ", prec, i);
489         for_each_online_cpu(j)
490                 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
491
492         if (desc->irq_data.chip) {
493                 if (desc->irq_data.chip->irq_print_chip)
494                         desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
495                 else if (desc->irq_data.chip->name)
496                         seq_printf(p, " %8s", desc->irq_data.chip->name);
497                 else
498                         seq_printf(p, " %8s", "-");
499         } else {
500                 seq_printf(p, " %8s", "None");
501         }
502 #ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
503         seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
504 #endif
505         if (desc->name)
506                 seq_printf(p, "-%-8s", desc->name);
507
508         if (action) {
509                 seq_printf(p, "  %s", action->name);
510                 while ((action = action->next) != NULL)
511                         seq_printf(p, ", %s", action->name);
512         }
513
514         seq_putc(p, '\n');
515 out:
516         raw_spin_unlock_irqrestore(&desc->lock, flags);
517 outsparse:
518         irq_unlock_sparse();
519         return 0;
520 }
521 #endif