proc.c 12.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/*
 * linux/kernel/irq/proc.c
 *
 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
 *
 * This file contains the /proc/irq/ handling code.
 */

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

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

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * 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.
 */
36
static struct proc_dir_entry *root_irq_dir;
L
Linus Torvalds 已提交
37 38 39

#ifdef CONFIG_SMP

40
static int show_irq_affinity(int type, struct seq_file *m, void *v)
L
Linus Torvalds 已提交
41
{
42
	struct irq_desc *desc = irq_to_desc((long)m->private);
43
	const struct cpumask *mask = desc->irq_common_data.affinity;
44 45

#ifdef CONFIG_GENERIC_PENDING_IRQ
46
	if (irqd_is_setaffinity_pending(&desc->irq_data))
47
		mask = desc->pending_mask;
48
#endif
49
	if (type)
50
		seq_printf(m, "%*pbl\n", cpumask_pr_args(mask));
51
	else
52
		seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
53
	return 0;
L
Linus Torvalds 已提交
54 55
}

56 57 58 59 60 61
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;

62
	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
63 64 65 66 67 68 69
		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);

70
	seq_printf(m, "%*pb\n", cpumask_pr_args(mask));
71 72 73 74 75
	free_cpumask_var(mask);

	return 0;
}

76 77 78 79
#ifndef is_affinity_mask_valid
#define is_affinity_mask_valid(val) 1
#endif

L
Linus Torvalds 已提交
80
int no_irq_affinity;
81 82 83 84 85 86 87 88 89 90 91 92
static int irq_affinity_proc_show(struct seq_file *m, void *v)
{
	return show_irq_affinity(0, m, v);
}

static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
{
	return show_irq_affinity(1, m, v);
}


static ssize_t write_irq_affinity(int type, struct file *file,
93
		const char __user *buffer, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
94
{
A
Al Viro 已提交
95
	unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
96
	cpumask_var_t new_value;
97
	int err;
L
Linus Torvalds 已提交
98

99
	if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
L
Linus Torvalds 已提交
100 101
		return -EIO;

102 103 104
	if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
		return -ENOMEM;

105 106 107 108
	if (type)
		err = cpumask_parselist_user(buffer, count, new_value);
	else
		err = cpumask_parse_user(buffer, count, new_value);
L
Linus Torvalds 已提交
109
	if (err)
110
		goto free_cpumask;
L
Linus Torvalds 已提交
111

112
	if (!is_affinity_mask_valid(new_value)) {
113 114 115
		err = -EINVAL;
		goto free_cpumask;
	}
116

L
Linus Torvalds 已提交
117 118 119 120 121
	/*
	 * 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.
	 */
122
	if (!cpumask_intersects(new_value, cpu_online_mask)) {
123 124
		/* Special case for empty set - allow the architecture
		   code to set default SMP affinity. */
125
		err = irq_select_affinity_usr(irq, new_value) ? -EINVAL : count;
126 127 128 129 130 131 132 133
	} else {
		irq_set_affinity(irq, new_value);
		err = count;
	}

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

136 137 138 139 140 141 142 143 144 145 146 147
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);
}

148
static int irq_affinity_proc_open(struct inode *inode, struct file *file)
149
{
A
Al Viro 已提交
150
	return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
151 152
}

153 154
static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
155
	return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
156 157
}

158 159
static int irq_affinity_hint_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
160
	return single_open(file, irq_affinity_hint_proc_show, PDE_DATA(inode));
161 162
}

163 164 165 166 167 168 169 170
static const struct file_operations irq_affinity_proc_fops = {
	.open		= irq_affinity_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= irq_affinity_proc_write,
};

171 172 173 174 175 176 177
static const struct file_operations irq_affinity_hint_proc_fops = {
	.open		= irq_affinity_hint_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

178 179 180 181 182 183 184 185
static const struct file_operations irq_affinity_list_proc_fops = {
	.open		= irq_affinity_list_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= irq_affinity_list_proc_write,
};

186 187
static int default_affinity_show(struct seq_file *m, void *v)
{
188
	seq_printf(m, "%*pb\n", cpumask_pr_args(irq_default_affinity));
189 190 191 192 193
	return 0;
}

static ssize_t default_affinity_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *ppos)
194
{
R
Rusty Russell 已提交
195
	cpumask_var_t new_value;
196
	int err;
197

R
Rusty Russell 已提交
198 199 200 201
	if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
		return -ENOMEM;

	err = cpumask_parse_user(buffer, count, new_value);
202
	if (err)
R
Rusty Russell 已提交
203
		goto out;
204

R
Rusty Russell 已提交
205 206 207 208
	if (!is_affinity_mask_valid(new_value)) {
		err = -EINVAL;
		goto out;
	}
209 210 211 212 213 214

	/*
	 * 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 已提交
215 216 217 218
	if (!cpumask_intersects(new_value, cpu_online_mask)) {
		err = -EINVAL;
		goto out;
	}
219

R
Rusty Russell 已提交
220 221
	cpumask_copy(irq_default_affinity, new_value);
	err = count;
222

R
Rusty Russell 已提交
223 224 225
out:
	free_cpumask_var(new_value);
	return err;
226
}
227 228 229

static int default_affinity_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
230
	return single_open(file, default_affinity_show, PDE_DATA(inode));
231 232 233 234 235 236 237 238 239
}

static const struct file_operations default_affinity_proc_fops = {
	.open		= default_affinity_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= default_affinity_write,
};
240 241 242 243 244

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

245
	seq_printf(m, "%d\n", irq_desc_get_node(desc));
246 247 248 249 250
	return 0;
}

static int irq_node_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
251
	return single_open(file, irq_node_proc_show, PDE_DATA(inode));
252 253 254 255 256 257 258 259
}

static const struct file_operations irq_node_proc_fops = {
	.open		= irq_node_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
L
Linus Torvalds 已提交
260 261
#endif

262
static int irq_spurious_proc_show(struct seq_file *m, void *v)
263
{
264 265 266 267 268 269 270 271 272 273
	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;
}

static int irq_spurious_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
274
	return single_open(file, irq_spurious_proc_show, PDE_DATA(inode));
275 276
}

277 278 279 280 281 282 283
static const struct file_operations irq_spurious_proc_fops = {
	.open		= irq_spurious_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

L
Linus Torvalds 已提交
284 285 286 287
#define MAX_NAMELEN 128

static int name_unique(unsigned int irq, struct irqaction *new_action)
{
288
	struct irq_desc *desc = irq_to_desc(irq);
L
Linus Torvalds 已提交
289
	struct irqaction *action;
290 291
	unsigned long flags;
	int ret = 1;
L
Linus Torvalds 已提交
292

293
	raw_spin_lock_irqsave(&desc->lock, flags);
294
	for_each_action_of_desc(desc, action) {
L
Linus Torvalds 已提交
295
		if ((action != new_action) && action->name &&
296 297 298 299 300
				!strcmp(new_action->name, action->name)) {
			ret = 0;
			break;
		}
	}
301
	raw_spin_unlock_irqrestore(&desc->lock, flags);
302
	return ret;
L
Linus Torvalds 已提交
303 304 305 306 307
}

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

310
	if (!desc->dir || action->dir || !action->name ||
L
Linus Torvalds 已提交
311 312 313 314 315 316
					!name_unique(irq, action))
		return;

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

	/* create /proc/irq/1234/handler/ */
317
	action->dir = proc_mkdir(name, desc->dir);
L
Linus Torvalds 已提交
318 319 320 321 322 323
}

#undef MAX_NAMELEN

#define MAX_NAMELEN 10

324
void register_irq_proc(unsigned int irq, struct irq_desc *desc)
L
Linus Torvalds 已提交
325
{
326
	static DEFINE_MUTEX(register_lock);
L
Linus Torvalds 已提交
327 328
	char name [MAX_NAMELEN];

329
	if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
L
Linus Torvalds 已提交
330 331
		return;

332 333 334 335 336 337 338 339 340 341
	/*
	 * 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 已提交
342 343 344
	sprintf(name, "%d", irq);

	/* create /proc/irq/1234 */
345
	desc->dir = proc_mkdir(name, root_irq_dir);
346
	if (!desc->dir)
347
		goto out_unlock;
L
Linus Torvalds 已提交
348 349

#ifdef CONFIG_SMP
350
	/* create /proc/irq/<irq>/smp_affinity */
351
	proc_create_data("smp_affinity", 0644, desc->dir,
352
			 &irq_affinity_proc_fops, (void *)(long)irq);
353

354
	/* create /proc/irq/<irq>/affinity_hint */
355
	proc_create_data("affinity_hint", 0444, desc->dir,
356 357
			 &irq_affinity_hint_proc_fops, (void *)(long)irq);

358
	/* create /proc/irq/<irq>/smp_affinity_list */
359
	proc_create_data("smp_affinity_list", 0644, desc->dir,
360 361
			 &irq_affinity_list_proc_fops, (void *)(long)irq);

362 363
	proc_create_data("node", 0444, desc->dir,
			 &irq_node_proc_fops, (void *)(long)irq);
L
Linus Torvalds 已提交
364
#endif
365

366 367
	proc_create_data("spurious", 0444, desc->dir,
			 &irq_spurious_proc_fops, (void *)(long)irq);
368 369 370

out_unlock:
	mutex_unlock(&register_lock);
L
Linus Torvalds 已提交
371 372
}

373 374 375 376 377 378 379 380 381
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);
382
	remove_proc_entry("smp_affinity_list", desc->dir);
383 384 385 386 387 388 389 390
	remove_proc_entry("node", desc->dir);
#endif
	remove_proc_entry("spurious", desc->dir);

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

L
Linus Torvalds 已提交
391 392 393 394
#undef MAX_NAMELEN

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

R
roel kluin 已提交
398
static void register_default_affinity_proc(void)
399 400
{
#ifdef CONFIG_SMP
401
	proc_create("irq/default_smp_affinity", 0644, NULL,
402
		    &default_affinity_proc_fops);
403 404 405
#endif
}

L
Linus Torvalds 已提交
406 407
void init_irq_proc(void)
{
408 409
	unsigned int irq;
	struct irq_desc *desc;
L
Linus Torvalds 已提交
410 411 412 413 414 415

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

416 417
	register_default_affinity_proc();

L
Linus Torvalds 已提交
418 419 420
	/*
	 * Create entries for all existing IRQs.
	 */
421
	for_each_irq_desc(irq, desc)
422
		register_irq_proc(irq, desc);
L
Linus Torvalds 已提交
423 424
}

425 426 427 428 429 430 431
#ifdef CONFIG_GENERIC_IRQ_SHOW

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

432 433 434 435
#ifndef ACTUAL_NR_IRQS
# define ACTUAL_NR_IRQS nr_irqs
#endif

436 437 438 439 440 441 442 443 444
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;

445
	if (i > ACTUAL_NR_IRQS)
446 447
		return 0;

448
	if (i == ACTUAL_NR_IRQS)
449 450 451 452 453 454 455 456 457 458 459 460 461
		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');
	}

462
	irq_lock_sparse();
463 464
	desc = irq_to_desc(i);
	if (!desc)
465
		goto outsparse;
466 467 468 469 470

	raw_spin_lock_irqsave(&desc->lock, flags);
	for_each_online_cpu(j)
		any_count |= kstat_irqs_cpu(i, j);
	action = desc->action;
471
	if ((!action || irq_desc_is_chained(desc)) && !any_count)
472 473 474 475 476
		goto out;

	seq_printf(p, "%*d: ", prec, i);
	for_each_online_cpu(j)
		seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
477 478 479 480 481 482 483 484 485 486 487

	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");
	}
488 489
	if (desc->irq_data.domain)
		seq_printf(p, " %*d", prec, (int) desc->irq_data.hwirq);
490
#ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
491 492
	seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
#endif
493 494
	if (desc->name)
		seq_printf(p, "-%-8s", desc->name);
495 496 497 498 499 500 501 502 503 504

	if (action) {
		seq_printf(p, "  %s", action->name);
		while ((action = action->next) != NULL)
			seq_printf(p, ", %s", action->name);
	}

	seq_putc(p, '\n');
out:
	raw_spin_unlock_irqrestore(&desc->lock, flags);
505 506
outsparse:
	irq_unlock_sparse();
507 508 509
	return 0;
}
#endif