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