proc.c 12 KB

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