proc.c 13.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7 8
/*
 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
 *
 * This file contains the /proc/irq/ handling code.
 */

#include <linux/irq.h>
9
#include <linux/gfp.h>
L
Linus Torvalds 已提交
10
#include <linux/proc_fs.h>
11
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
12
#include <linux/interrupt.h>
13
#include <linux/kernel_stat.h>
14
#include <linux/mutex.h>
L
Linus Torvalds 已提交
15

A
Adrian Bunk 已提交
16 17
#include "internals.h"

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Access rules:
 *
 * procfs protects read/write of /proc/irq/N/ files against a
 * concurrent free of the interrupt descriptor. remove_proc_entry()
 * immediately prevents new read/writes to happen and waits for
 * already running read/write functions to complete.
 *
 * We remove the proc entries first and then delete the interrupt
 * descriptor from the radix tree and free it. So it is guaranteed
 * that irq_to_desc(N) is valid as long as the read/writes are
 * permitted by procfs.
 *
 * The read from /proc/interrupts is a different problem because there
 * is no protection. So the lookup and the access to irqdesc
 * information must be protected by sparse_irq_lock.
 */
35
static struct proc_dir_entry *root_irq_dir;
L
Linus Torvalds 已提交
36 37 38

#ifdef CONFIG_SMP

39 40 41 42 43 44 45
enum {
	AFFINITY,
	AFFINITY_LIST,
	EFFECTIVE,
	EFFECTIVE_LIST,
};

46
static int show_irq_affinity(int type, struct seq_file *m)
L
Linus Torvalds 已提交
47
{
48
	struct irq_desc *desc = irq_to_desc((long)m->private);
49
	const struct cpumask *mask;
50

51 52 53 54
	switch (type) {
	case AFFINITY:
	case AFFINITY_LIST:
		mask = desc->irq_common_data.affinity;
55
#ifdef CONFIG_GENERIC_PENDING_IRQ
56 57
		if (irqd_is_setaffinity_pending(&desc->irq_data))
			mask = desc->pending_mask;
58
#endif
59 60 61 62
		break;
	case EFFECTIVE:
	case EFFECTIVE_LIST:
#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
63
		mask = irq_data_get_effective_affinity_mask(&desc->irq_data);
64 65
		break;
#endif
66 67
	default:
		return -EINVAL;
68
	}
69 70 71 72

	switch (type) {
	case AFFINITY_LIST:
	case EFFECTIVE_LIST:
73
		seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
74 75 76
		break;
	case AFFINITY:
	case EFFECTIVE:
77
		seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
78 79
		break;
	}
80
	return 0;
L
Linus Torvalds 已提交
81 82
}

83 84 85 86 87 88
static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
{
	struct irq_desc *desc = irq_to_desc((long)m->private);
	unsigned long flags;
	cpumask_var_t mask;

89
	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
90 91 92 93 94 95 96
		return -ENOMEM;

	raw_spin_lock_irqsave(&desc->lock, flags);
	if (desc->affinity_hint)
		cpumask_copy(mask, desc->affinity_hint);
	raw_spin_unlock_irqrestore(&desc->lock, flags);

97
	seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
98 99 100 101 102
	free_cpumask_var(mask);

	return 0;
}

L
Linus Torvalds 已提交
103
int no_irq_affinity;
104 105
static int irq_affinity_proc_show(struct seq_file *m, void *v)
{
106
	return show_irq_affinity(AFFINITY, m);
107 108 109 110
}

static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
{
111
	return show_irq_affinity(AFFINITY_LIST, m);
112 113
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#ifndef CONFIG_AUTO_IRQ_AFFINITY
static inline int irq_select_affinity_usr(unsigned int irq)
{
	/*
	 * If the interrupt is started up already then this fails. The
	 * interrupt is assigned to an online CPU already. There is no
	 * point to move it around randomly. Tell user space that the
	 * selected mask is bogus.
	 *
	 * If not then any change to the affinity is pointless because the
	 * startup code invokes irq_setup_affinity() which will select
	 * a online CPU anyway.
	 */
	return -EINVAL;
}
#else
/* ALPHA magic affinity auto selector. Keep it for historical reasons. */
static inline int irq_select_affinity_usr(unsigned int irq)
{
	return irq_select_affinity(irq);
}
#endif
136 137

static ssize_t write_irq_affinity(int type, struct file *file,
138
		const char __user *buffer, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
139
{
A
Al Viro 已提交
140
	unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
141
	cpumask_var_t new_value;
142
	int err;
L
Linus Torvalds 已提交
143

144
	if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
L
Linus Torvalds 已提交
145 146
		return -EIO;

147 148 149
	if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
		return -ENOMEM;

150 151 152 153
	if (type)
		err = cpumask_parselist_user(buffer, count, new_value);
	else
		err = cpumask_parse_user(buffer, count, new_value);
L
Linus Torvalds 已提交
154
	if (err)
155
		goto free_cpumask;
L
Linus Torvalds 已提交
156 157 158 159 160 161

	/*
	 * Do not allow disabling IRQs completely - it's a too easy
	 * way to make the system unusable accidentally :-) At least
	 * one online CPU still has to be targeted.
	 */
162
	if (!cpumask_intersects(new_value, cpu_online_mask)) {
163 164 165 166 167
		/*
		 * Special case for empty set - allow the architecture code
		 * to set default SMP affinity.
		 */
		err = irq_select_affinity_usr(irq) ? -EINVAL : count;
168
	} else {
169 170 171
		err = irq_set_affinity(irq, new_value);
		if (!err)
			err = count;
172 173 174 175 176
	}

free_cpumask:
	free_cpumask_var(new_value);
	return err;
L
Linus Torvalds 已提交
177 178
}

179 180 181 182 183 184 185 186 187 188 189 190
static ssize_t irq_affinity_proc_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *pos)
{
	return write_irq_affinity(0, file, buffer, count, pos);
}

static ssize_t irq_affinity_list_proc_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *pos)
{
	return write_irq_affinity(1, file, buffer, count, pos);
}

191
static int irq_affinity_proc_open(struct inode *inode, struct file *file)
192
{
A
Al Viro 已提交
193
	return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
194 195
}

196 197
static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
198
	return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
199 200
}

201 202 203 204 205 206
static const struct proc_ops irq_affinity_proc_ops = {
	.proc_open	= irq_affinity_proc_open,
	.proc_read	= seq_read,
	.proc_lseek	= seq_lseek,
	.proc_release	= single_release,
	.proc_write	= irq_affinity_proc_write,
207 208
};

209 210 211 212 213 214
static const struct proc_ops irq_affinity_list_proc_ops = {
	.proc_open	= irq_affinity_list_proc_open,
	.proc_read	= seq_read,
	.proc_lseek	= seq_lseek,
	.proc_release	= single_release,
	.proc_write	= irq_affinity_list_proc_write,
215 216
};

217 218 219 220 221 222 223 224 225 226 227 228
#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
static int irq_effective_aff_proc_show(struct seq_file *m, void *v)
{
	return show_irq_affinity(EFFECTIVE, m);
}

static int irq_effective_aff_list_proc_show(struct seq_file *m, void *v)
{
	return show_irq_affinity(EFFECTIVE_LIST, m);
}
#endif

229 230
static int default_affinity_show(struct seq_file *m, void *v)
{
231
	seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
232 233 234 235 236
	return 0;
}

static ssize_t default_affinity_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *ppos)
237
{
R
Rusty Russell 已提交
238
	cpumask_var_t new_value;
239
	int err;
240

R
Rusty Russell 已提交
241 242 243 244
	if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
		return -ENOMEM;

	err = cpumask_parse_user(buffer, count, new_value);
245
	if (err)
R
Rusty Russell 已提交
246
		goto out;
247 248 249 250 251 252

	/*
	 * Do not allow disabling IRQs completely - it's a too easy
	 * way to make the system unusable accidentally :-) At least
	 * one online CPU still has to be targeted.
	 */
R
Rusty Russell 已提交
253 254 255 256
	if (!cpumask_intersects(new_value, cpu_online_mask)) {
		err = -EINVAL;
		goto out;
	}
257

R
Rusty Russell 已提交
258 259
	cpumask_copy(irq_default_affinity, new_value);
	err = count;
260

R
Rusty Russell 已提交
261 262 263
out:
	free_cpumask_var(new_value);
	return err;
264
}
265 266 267

static int default_affinity_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
268
	return single_open(file, default_affinity_show, PDE_DATA(inode));
269 270
}

271 272 273 274 275 276
static const struct proc_ops default_affinity_proc_ops = {
	.proc_open	= default_affinity_open,
	.proc_read	= seq_read,
	.proc_lseek	= seq_lseek,
	.proc_release	= single_release,
	.proc_write	= default_affinity_write,
277
};
278 279 280 281 282

static int irq_node_proc_show(struct seq_file *m, void *v)
{
	struct irq_desc *desc = irq_to_desc((long) m->private);

283
	seq_printf(m, "%d\n", irq_desc_get_node(desc));
284 285
	return 0;
}
L
Linus Torvalds 已提交
286 287
#endif

288
static int irq_spurious_proc_show(struct seq_file *m, void *v)
289
{
290 291 292 293 294 295 296 297
	struct irq_desc *desc = irq_to_desc((long) m->private);

	seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
		   desc->irq_count, desc->irqs_unhandled,
		   jiffies_to_msecs(desc->last_unhandled));
	return 0;
}

L
Linus Torvalds 已提交
298 299 300 301
#define MAX_NAMELEN 128

static int name_unique(unsigned int irq, struct irqaction *new_action)
{
302
	struct irq_desc *desc = irq_to_desc(irq);
L
Linus Torvalds 已提交
303
	struct irqaction *action;
304 305
	unsigned long flags;
	int ret = 1;
L
Linus Torvalds 已提交
306

307
	raw_spin_lock_irqsave(&desc->lock, flags);
308
	for_each_action_of_desc(desc, action) {
L
Linus Torvalds 已提交
309
		if ((action != new_action) && action->name &&
310 311 312 313 314
				!strcmp(new_action->name, action->name)) {
			ret = 0;
			break;
		}
	}
315
	raw_spin_unlock_irqrestore(&desc->lock, flags);
316
	return ret;
L
Linus Torvalds 已提交
317 318 319 320 321
}

void register_handler_proc(unsigned int irq, struct irqaction *action)
{
	char name [MAX_NAMELEN];
322
	struct irq_desc *desc = irq_to_desc(irq);
L
Linus Torvalds 已提交
323

324
	if (!desc->dir || action->dir || !action->name ||
L
Linus Torvalds 已提交
325 326 327 328 329 330
					!name_unique(irq, action))
		return;

	snprintf(name, MAX_NAMELEN, "%s", action->name);

	/* create /proc/irq/1234/handler/ */
331
	action->dir = proc_mkdir(name, desc->dir);
L
Linus Torvalds 已提交
332 333 334 335 336 337
}

#undef MAX_NAMELEN

#define MAX_NAMELEN 10

338
void register_irq_proc(unsigned int irq, struct irq_desc *desc)
L
Linus Torvalds 已提交
339
{
340
	static DEFINE_MUTEX(register_lock);
341
	void __maybe_unused *irqp = (void *)(unsigned long) irq;
L
Linus Torvalds 已提交
342 343
	char name [MAX_NAMELEN];

344
	if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
L
Linus Torvalds 已提交
345 346
		return;

347 348 349 350 351 352 353 354 355 356
	/*
	 * irq directories are registered only when a handler is
	 * added, not when the descriptor is created, so multiple
	 * tasks might try to register at the same time.
	 */
	mutex_lock(&register_lock);

	if (desc->dir)
		goto out_unlock;

L
Linus Torvalds 已提交
357 358 359
	sprintf(name, "%d", irq);

	/* create /proc/irq/1234 */
360
	desc->dir = proc_mkdir(name, root_irq_dir);
361
	if (!desc->dir)
362
		goto out_unlock;
L
Linus Torvalds 已提交
363 364

#ifdef CONFIG_SMP
365
	/* create /proc/irq/<irq>/smp_affinity */
366
	proc_create_data("smp_affinity", 0644, desc->dir,
367
			 &irq_affinity_proc_ops, irqp);
368

369
	/* create /proc/irq/<irq>/affinity_hint */
370 371
	proc_create_single_data("affinity_hint", 0444, desc->dir,
			irq_affinity_hint_proc_show, irqp);
372

373
	/* create /proc/irq/<irq>/smp_affinity_list */
374
	proc_create_data("smp_affinity_list", 0644, desc->dir,
375
			 &irq_affinity_list_proc_ops, irqp);
376

377 378
	proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show,
			irqp);
379
# ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
380 381 382 383
	proc_create_single_data("effective_affinity", 0444, desc->dir,
			irq_effective_aff_proc_show, irqp);
	proc_create_single_data("effective_affinity_list", 0444, desc->dir,
			irq_effective_aff_list_proc_show, irqp);
384
# endif
L
Linus Torvalds 已提交
385
#endif
386 387
	proc_create_single_data("spurious", 0444, desc->dir,
			irq_spurious_proc_show, (void *)(long)irq);
388 389 390

out_unlock:
	mutex_unlock(&register_lock);
L
Linus Torvalds 已提交
391 392
}

393 394 395 396 397 398 399 400 401
void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
{
	char name [MAX_NAMELEN];

	if (!root_irq_dir || !desc->dir)
		return;
#ifdef CONFIG_SMP
	remove_proc_entry("smp_affinity", desc->dir);
	remove_proc_entry("affinity_hint", desc->dir);
402
	remove_proc_entry("smp_affinity_list", desc->dir);
403
	remove_proc_entry("node", desc->dir);
404 405 406 407
# ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
	remove_proc_entry("effective_affinity", desc->dir);
	remove_proc_entry("effective_affinity_list", desc->dir);
# endif
408 409 410 411 412 413 414
#endif
	remove_proc_entry("spurious", desc->dir);

	sprintf(name, "%u", irq);
	remove_proc_entry(name, root_irq_dir);
}

L
Linus Torvalds 已提交
415 416 417 418
#undef MAX_NAMELEN

void unregister_handler_proc(unsigned int irq, struct irqaction *action)
{
419
	proc_remove(action->dir);
L
Linus Torvalds 已提交
420 421
}

R
roel kluin 已提交
422
static void register_default_affinity_proc(void)
423 424
{
#ifdef CONFIG_SMP
425
	proc_create("irq/default_smp_affinity", 0644, NULL,
426
		    &default_affinity_proc_ops);
427 428 429
#endif
}

L
Linus Torvalds 已提交
430 431
void init_irq_proc(void)
{
432 433
	unsigned int irq;
	struct irq_desc *desc;
L
Linus Torvalds 已提交
434 435 436 437 438 439

	/* create /proc/irq */
	root_irq_dir = proc_mkdir("irq", NULL);
	if (!root_irq_dir)
		return;

440 441
	register_default_affinity_proc();

L
Linus Torvalds 已提交
442 443 444
	/*
	 * Create entries for all existing IRQs.
	 */
445
	for_each_irq_desc(irq, desc)
446
		register_irq_proc(irq, desc);
L
Linus Torvalds 已提交
447 448
}

449 450 451 452 453 454 455
#ifdef CONFIG_GENERIC_IRQ_SHOW

int __weak arch_show_interrupts(struct seq_file *p, int prec)
{
	return 0;
}

456 457 458 459
#ifndef ACTUAL_NR_IRQS
# define ACTUAL_NR_IRQS nr_irqs
#endif

460 461 462 463 464 465 466 467 468
int show_interrupts(struct seq_file *p, void *v)
{
	static int prec;

	unsigned long flags, any_count = 0;
	int i = *(loff_t *) v, j;
	struct irqaction *action;
	struct irq_desc *desc;

469
	if (i > ACTUAL_NR_IRQS)
470 471
		return 0;

472
	if (i == ACTUAL_NR_IRQS)
473 474 475 476 477 478 479 480 481 482 483 484 485
		return arch_show_interrupts(p, prec);

	/* print header and calculate the width of the first column */
	if (i == 0) {
		for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
			j *= 10;

		seq_printf(p, "%*s", prec + 8, "");
		for_each_online_cpu(j)
			seq_printf(p, "CPU%-8d", j);
		seq_putc(p, '\n');
	}

E
Eric Dumazet 已提交
486
	rcu_read_lock();
487 488
	desc = irq_to_desc(i);
	if (!desc)
489
		goto outsparse;
490

E
Eric Dumazet 已提交
491 492 493 494 495 496
	if (desc->kstat_irqs)
		for_each_online_cpu(j)
			any_count |= *per_cpu_ptr(desc->kstat_irqs, j);

	if ((!desc->action || irq_desc_is_chained(desc)) && !any_count)
		goto outsparse;
497 498 499

	seq_printf(p, "%*d: ", prec, i);
	for_each_online_cpu(j)
E
Eric Dumazet 已提交
500 501
		seq_printf(p, "%10u ", desc->kstat_irqs ?
					*per_cpu_ptr(desc->kstat_irqs, j) : 0);
502

E
Eric Dumazet 已提交
503
	raw_spin_lock_irqsave(&desc->lock, flags);
504 505 506 507 508 509 510 511 512 513
	if (desc->irq_data.chip) {
		if (desc->irq_data.chip->irq_print_chip)
			desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
		else if (desc->irq_data.chip->name)
			seq_printf(p, " %8s", desc->irq_data.chip->name);
		else
			seq_printf(p, " %8s", "-");
	} else {
		seq_printf(p, " %8s", "None");
	}
514 515
	if (desc->irq_data.domain)
		seq_printf(p, " %*d", prec, (int) desc->irq_data.hwirq);
516 517
	else
		seq_printf(p, " %*s", prec, "");
518
#ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
519 520
	seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
#endif
521 522
	if (desc->name)
		seq_printf(p, "-%-8s", desc->name);
523

E
Eric Dumazet 已提交
524
	action = desc->action;
525 526 527 528 529 530 531 532
	if (action) {
		seq_printf(p, "  %s", action->name);
		while ((action = action->next) != NULL)
			seq_printf(p, ", %s", action->name);
	}

	seq_putc(p, '\n');
	raw_spin_unlock_irqrestore(&desc->lock, flags);
533
outsparse:
E
Eric Dumazet 已提交
534
	rcu_read_unlock();
535 536 537
	return 0;
}
#endif