proc.c 14 KB

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