proc.c 15 KB

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