proc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. #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 "internals.h"
  15. /*
  16. * Access rules:
  17. *
  18. * procfs protects read/write of /proc/irq/N/ files against a
  19. * concurrent free of the interrupt descriptor. remove_proc_entry()
  20. * immediately prevents new read/writes to happen and waits for
  21. * already running read/write functions to complete.
  22. *
  23. * We remove the proc entries first and then delete the interrupt
  24. * descriptor from the radix tree and free it. So it is guaranteed
  25. * that irq_to_desc(N) is valid as long as the read/writes are
  26. * permitted by procfs.
  27. *
  28. * The read from /proc/interrupts is a different problem because there
  29. * is no protection. So the lookup and the access to irqdesc
  30. * information must be protected by sparse_irq_lock.
  31. */
  32. static struct proc_dir_entry *root_irq_dir;
  33. #ifdef CONFIG_SMP
  34. static int show_irq_affinity(int type, struct seq_file *m, void *v)
  35. {
  36. struct irq_desc *desc = irq_to_desc((long)m->private);
  37. const struct cpumask *mask = desc->irq_data.affinity;
  38. #ifdef CONFIG_GENERIC_PENDING_IRQ
  39. if (irqd_is_setaffinity_pending(&desc->irq_data))
  40. mask = desc->pending_mask;
  41. #endif
  42. if (type)
  43. seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
  44. else
  45. seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
  46. return 0;
  47. }
  48. static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
  49. {
  50. struct irq_desc *desc = irq_to_desc((long)m->private);
  51. unsigned long flags;
  52. cpumask_var_t mask;
  53. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  54. return -ENOMEM;
  55. raw_spin_lock_irqsave(&desc->lock, flags);
  56. if (desc->affinity_hint)
  57. cpumask_copy(mask, desc->affinity_hint);
  58. raw_spin_unlock_irqrestore(&desc->lock, flags);
  59. seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
  60. free_cpumask_var(mask);
  61. return 0;
  62. }
  63. #ifndef is_affinity_mask_valid
  64. #define is_affinity_mask_valid(val) 1
  65. #endif
  66. int no_irq_affinity;
  67. static int irq_affinity_proc_show(struct seq_file *m, void *v)
  68. {
  69. return show_irq_affinity(0, m, v);
  70. }
  71. static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
  72. {
  73. return show_irq_affinity(1, m, v);
  74. }
  75. static ssize_t write_irq_affinity(int type, struct file *file,
  76. const char __user *buffer, size_t count, loff_t *pos)
  77. {
  78. unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
  79. cpumask_var_t new_value;
  80. int err;
  81. if (!irq_can_set_affinity(irq) || no_irq_affinity)
  82. return -EIO;
  83. if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
  84. return -ENOMEM;
  85. if (type)
  86. err = cpumask_parselist_user(buffer, count, new_value);
  87. else
  88. err = cpumask_parse_user(buffer, count, new_value);
  89. if (err)
  90. goto free_cpumask;
  91. if (!is_affinity_mask_valid(new_value)) {
  92. err = -EINVAL;
  93. goto free_cpumask;
  94. }
  95. /*
  96. * Do not allow disabling IRQs completely - it's a too easy
  97. * way to make the system unusable accidentally :-) At least
  98. * one online CPU still has to be targeted.
  99. */
  100. if (!cpumask_intersects(new_value, cpu_online_mask)) {
  101. /* Special case for empty set - allow the architecture
  102. code to set default SMP affinity. */
  103. err = irq_select_affinity_usr(irq, new_value) ? -EINVAL : count;
  104. } else {
  105. irq_set_affinity(irq, new_value);
  106. err = count;
  107. }
  108. free_cpumask:
  109. free_cpumask_var(new_value);
  110. return err;
  111. }
  112. static ssize_t irq_affinity_proc_write(struct file *file,
  113. const char __user *buffer, size_t count, loff_t *pos)
  114. {
  115. return write_irq_affinity(0, file, buffer, count, pos);
  116. }
  117. static ssize_t irq_affinity_list_proc_write(struct file *file,
  118. const char __user *buffer, size_t count, loff_t *pos)
  119. {
  120. return write_irq_affinity(1, file, buffer, count, pos);
  121. }
  122. static int irq_affinity_proc_open(struct inode *inode, struct file *file)
  123. {
  124. return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
  125. }
  126. static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
  127. {
  128. return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
  129. }
  130. static int irq_affinity_hint_proc_open(struct inode *inode, struct file *file)
  131. {
  132. return single_open(file, irq_affinity_hint_proc_show, PDE_DATA(inode));
  133. }
  134. static const struct file_operations irq_affinity_proc_fops = {
  135. .open = irq_affinity_proc_open,
  136. .read = seq_read,
  137. .llseek = seq_lseek,
  138. .release = single_release,
  139. .write = irq_affinity_proc_write,
  140. };
  141. static const struct file_operations irq_affinity_hint_proc_fops = {
  142. .open = irq_affinity_hint_proc_open,
  143. .read = seq_read,
  144. .llseek = seq_lseek,
  145. .release = single_release,
  146. };
  147. static const struct file_operations irq_affinity_list_proc_fops = {
  148. .open = irq_affinity_list_proc_open,
  149. .read = seq_read,
  150. .llseek = seq_lseek,
  151. .release = single_release,
  152. .write = irq_affinity_list_proc_write,
  153. };
  154. static int default_affinity_show(struct seq_file *m, void *v)
  155. {
  156. seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
  157. return 0;
  158. }
  159. static ssize_t default_affinity_write(struct file *file,
  160. const char __user *buffer, size_t count, loff_t *ppos)
  161. {
  162. cpumask_var_t new_value;
  163. int err;
  164. if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
  165. return -ENOMEM;
  166. err = cpumask_parse_user(buffer, count, new_value);
  167. if (err)
  168. goto out;
  169. if (!is_affinity_mask_valid(new_value)) {
  170. err = -EINVAL;
  171. goto out;
  172. }
  173. /*
  174. * Do not allow disabling IRQs completely - it's a too easy
  175. * way to make the system unusable accidentally :-) At least
  176. * one online CPU still has to be targeted.
  177. */
  178. if (!cpumask_intersects(new_value, cpu_online_mask)) {
  179. err = -EINVAL;
  180. goto out;
  181. }
  182. cpumask_copy(irq_default_affinity, new_value);
  183. err = count;
  184. out:
  185. free_cpumask_var(new_value);
  186. return err;
  187. }
  188. static int default_affinity_open(struct inode *inode, struct file *file)
  189. {
  190. return single_open(file, default_affinity_show, PDE_DATA(inode));
  191. }
  192. static const struct file_operations default_affinity_proc_fops = {
  193. .open = default_affinity_open,
  194. .read = seq_read,
  195. .llseek = seq_lseek,
  196. .release = single_release,
  197. .write = default_affinity_write,
  198. };
  199. static int irq_node_proc_show(struct seq_file *m, void *v)
  200. {
  201. struct irq_desc *desc = irq_to_desc((long) m->private);
  202. seq_printf(m, "%d\n", desc->irq_data.node);
  203. return 0;
  204. }
  205. static int irq_node_proc_open(struct inode *inode, struct file *file)
  206. {
  207. return single_open(file, irq_node_proc_show, PDE_DATA(inode));
  208. }
  209. static const struct file_operations irq_node_proc_fops = {
  210. .open = irq_node_proc_open,
  211. .read = seq_read,
  212. .llseek = seq_lseek,
  213. .release = single_release,
  214. };
  215. #endif
  216. static int irq_spurious_proc_show(struct seq_file *m, void *v)
  217. {
  218. struct irq_desc *desc = irq_to_desc((long) m->private);
  219. seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
  220. desc->irq_count, desc->irqs_unhandled,
  221. jiffies_to_msecs(desc->last_unhandled));
  222. return 0;
  223. }
  224. static int irq_spurious_proc_open(struct inode *inode, struct file *file)
  225. {
  226. return single_open(file, irq_spurious_proc_show, PDE_DATA(inode));
  227. }
  228. static const struct file_operations irq_spurious_proc_fops = {
  229. .open = irq_spurious_proc_open,
  230. .read = seq_read,
  231. .llseek = seq_lseek,
  232. .release = single_release,
  233. };
  234. #define MAX_NAMELEN 128
  235. static int name_unique(unsigned int irq, struct irqaction *new_action)
  236. {
  237. struct irq_desc *desc = irq_to_desc(irq);
  238. struct irqaction *action;
  239. unsigned long flags;
  240. int ret = 1;
  241. raw_spin_lock_irqsave(&desc->lock, flags);
  242. for (action = desc->action ; action; action = action->next) {
  243. if ((action != new_action) && action->name &&
  244. !strcmp(new_action->name, action->name)) {
  245. ret = 0;
  246. break;
  247. }
  248. }
  249. raw_spin_unlock_irqrestore(&desc->lock, flags);
  250. return ret;
  251. }
  252. void register_handler_proc(unsigned int irq, struct irqaction *action)
  253. {
  254. char name [MAX_NAMELEN];
  255. struct irq_desc *desc = irq_to_desc(irq);
  256. if (!desc->dir || action->dir || !action->name ||
  257. !name_unique(irq, action))
  258. return;
  259. memset(name, 0, MAX_NAMELEN);
  260. snprintf(name, MAX_NAMELEN, "%s", action->name);
  261. /* create /proc/irq/1234/handler/ */
  262. action->dir = proc_mkdir(name, desc->dir);
  263. }
  264. #undef MAX_NAMELEN
  265. #define MAX_NAMELEN 10
  266. void register_irq_proc(unsigned int irq, struct irq_desc *desc)
  267. {
  268. char name [MAX_NAMELEN];
  269. if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip) || desc->dir)
  270. return;
  271. memset(name, 0, MAX_NAMELEN);
  272. sprintf(name, "%d", irq);
  273. /* create /proc/irq/1234 */
  274. desc->dir = proc_mkdir(name, root_irq_dir);
  275. if (!desc->dir)
  276. return;
  277. #ifdef CONFIG_SMP
  278. /* create /proc/irq/<irq>/smp_affinity */
  279. proc_create_data("smp_affinity", 0644, desc->dir,
  280. &irq_affinity_proc_fops, (void *)(long)irq);
  281. /* create /proc/irq/<irq>/affinity_hint */
  282. proc_create_data("affinity_hint", 0444, desc->dir,
  283. &irq_affinity_hint_proc_fops, (void *)(long)irq);
  284. /* create /proc/irq/<irq>/smp_affinity_list */
  285. proc_create_data("smp_affinity_list", 0644, desc->dir,
  286. &irq_affinity_list_proc_fops, (void *)(long)irq);
  287. proc_create_data("node", 0444, desc->dir,
  288. &irq_node_proc_fops, (void *)(long)irq);
  289. #endif
  290. proc_create_data("spurious", 0444, desc->dir,
  291. &irq_spurious_proc_fops, (void *)(long)irq);
  292. }
  293. void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
  294. {
  295. char name [MAX_NAMELEN];
  296. if (!root_irq_dir || !desc->dir)
  297. return;
  298. #ifdef CONFIG_SMP
  299. remove_proc_entry("smp_affinity", desc->dir);
  300. remove_proc_entry("affinity_hint", desc->dir);
  301. remove_proc_entry("smp_affinity_list", desc->dir);
  302. remove_proc_entry("node", desc->dir);
  303. #endif
  304. remove_proc_entry("spurious", desc->dir);
  305. memset(name, 0, MAX_NAMELEN);
  306. sprintf(name, "%u", irq);
  307. remove_proc_entry(name, root_irq_dir);
  308. }
  309. #undef MAX_NAMELEN
  310. void unregister_handler_proc(unsigned int irq, struct irqaction *action)
  311. {
  312. proc_remove(action->dir);
  313. }
  314. static void register_default_affinity_proc(void)
  315. {
  316. #ifdef CONFIG_SMP
  317. proc_create("irq/default_smp_affinity", 0644, NULL,
  318. &default_affinity_proc_fops);
  319. #endif
  320. }
  321. void init_irq_proc(void)
  322. {
  323. unsigned int irq;
  324. struct irq_desc *desc;
  325. /* create /proc/irq */
  326. root_irq_dir = proc_mkdir("irq", NULL);
  327. if (!root_irq_dir)
  328. return;
  329. register_default_affinity_proc();
  330. /*
  331. * Create entries for all existing IRQs.
  332. */
  333. for_each_irq_desc(irq, desc) {
  334. if (!desc)
  335. continue;
  336. register_irq_proc(irq, desc);
  337. }
  338. }
  339. #ifdef CONFIG_GENERIC_IRQ_SHOW
  340. int __weak arch_show_interrupts(struct seq_file *p, int prec)
  341. {
  342. return 0;
  343. }
  344. #ifndef ACTUAL_NR_IRQS
  345. # define ACTUAL_NR_IRQS nr_irqs
  346. #endif
  347. int show_interrupts(struct seq_file *p, void *v)
  348. {
  349. static int prec;
  350. unsigned long flags, any_count = 0;
  351. int i = *(loff_t *) v, j;
  352. struct irqaction *action;
  353. struct irq_desc *desc;
  354. if (i > ACTUAL_NR_IRQS)
  355. return 0;
  356. if (i == ACTUAL_NR_IRQS)
  357. return arch_show_interrupts(p, prec);
  358. /* print header and calculate the width of the first column */
  359. if (i == 0) {
  360. for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
  361. j *= 10;
  362. seq_printf(p, "%*s", prec + 8, "");
  363. for_each_online_cpu(j)
  364. seq_printf(p, "CPU%-8d", j);
  365. seq_putc(p, '\n');
  366. }
  367. irq_lock_sparse();
  368. desc = irq_to_desc(i);
  369. if (!desc)
  370. goto outsparse;
  371. raw_spin_lock_irqsave(&desc->lock, flags);
  372. for_each_online_cpu(j)
  373. any_count |= kstat_irqs_cpu(i, j);
  374. action = desc->action;
  375. if (!action && !any_count)
  376. goto out;
  377. seq_printf(p, "%*d: ", prec, i);
  378. for_each_online_cpu(j)
  379. seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
  380. if (desc->irq_data.chip) {
  381. if (desc->irq_data.chip->irq_print_chip)
  382. desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
  383. else if (desc->irq_data.chip->name)
  384. seq_printf(p, " %8s", desc->irq_data.chip->name);
  385. else
  386. seq_printf(p, " %8s", "-");
  387. } else {
  388. seq_printf(p, " %8s", "None");
  389. }
  390. if (desc->irq_data.domain)
  391. seq_printf(p, " %*d", prec, (int) desc->irq_data.hwirq);
  392. #ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
  393. seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
  394. #endif
  395. if (desc->name)
  396. seq_printf(p, "-%-8s", desc->name);
  397. if (action) {
  398. seq_printf(p, " %s", action->name);
  399. while ((action = action->next) != NULL)
  400. seq_printf(p, ", %s", action->name);
  401. }
  402. seq_putc(p, '\n');
  403. out:
  404. raw_spin_unlock_irqrestore(&desc->lock, flags);
  405. outsparse:
  406. irq_unlock_sparse();
  407. return 0;
  408. }
  409. #endif