proc.c 14 KB

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