blob: 15257d4b1b912cf221dd332d20f4a0dc72d2e391 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/proc_misc.c
3 *
4 * linux/fs/proc/array.c
5 * Copyright (C) 1992 by Linus Torvalds
6 * based on ideas by Darren Senn
7 *
8 * This used to be the part of array.c. See the rest of history and credits
9 * there. I took this into a separate file and switched the thing to generic
10 * proc_file_inode_operations, leaving in array.c only per-process stuff.
11 * Inumbers allocation made dynamic (via create_proc_entry()). AV, May 1999.
12 *
13 * Changes:
14 * Fulton Green : Encapsulated position metric calculations.
15 * <kernel@FultonGreen.com>
16 */
17
18#include <linux/types.h>
19#include <linux/errno.h>
20#include <linux/time.h>
21#include <linux/kernel.h>
22#include <linux/kernel_stat.h>
Neil Horman7170be52006-01-14 13:20:38 -080023#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/tty.h>
25#include <linux/string.h>
26#include <linux/mman.h>
KOSAKI Motohiro4b856152008-09-02 14:35:53 -070027#include <linux/quicklist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/proc_fs.h>
29#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mm.h>
31#include <linux/mmzone.h>
32#include <linux/pagemap.h>
Yinghai Luc7fb03a2008-08-19 20:50:12 -070033#include <linux/irq.h>
Adrian Bunkf74596d2008-02-06 01:36:35 -080034#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/swap.h>
36#include <linux/slab.h>
Adrian Bunka0db7012008-03-04 11:23:50 +010037#include <linux/genhd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/smp.h>
39#include <linux/signal.h>
40#include <linux/module.h>
41#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/seq_file.h>
43#include <linux/times.h>
44#include <linux/profile.h>
Andrew Morton7bf65382006-12-08 02:41:14 -080045#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/blkdev.h>
47#include <linux/hugetlb.h>
48#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/vmalloc.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070050#include <linux/crash_dump.h>
Sukadev Bhattiprolu61a58c62006-12-08 02:37:58 -080051#include <linux/pid_namespace.h>
Matt Mackall161f47b2008-02-04 22:29:05 -080052#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/uaccess.h>
54#include <asm/pgtable.h>
55#include <asm/io.h>
56#include <asm/tlb.h>
57#include <asm/div64.h>
58#include "internal.h"
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * Warning: stuff below (imported functions) assumes that its output will fit
62 * into one page. For some of those functions it may be wrong. Moreover, we
63 * have a way to deal with that gracefully. Right now I used straightforward
64 * wrappers, but this needs further analysis wrt potential overflows.
65 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066extern int get_exec_domain_list(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static int proc_calc_metrics(char *page, char **start, off_t off,
69 int count, int *eof, int len)
70{
71 if (len <= off+count) *eof = 1;
72 *start = page + off;
73 len -= off;
74 if (len>count) len = count;
75 if (len<0) len = 0;
76 return len;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int fragmentation_open(struct inode *inode, struct file *file)
80{
81 (void)inode;
82 return seq_open(file, &fragmentation_op);
83}
84
Arjan van de Ven00977a52007-02-12 00:55:34 -080085static const struct file_operations fragmentation_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .open = fragmentation_open,
87 .read = seq_read,
88 .llseek = seq_lseek,
89 .release = seq_release,
90};
91
Mel Gorman467c9962007-10-16 01:26:02 -070092static int pagetypeinfo_open(struct inode *inode, struct file *file)
93{
94 return seq_open(file, &pagetypeinfo_op);
95}
96
97static const struct file_operations pagetypeinfo_file_ops = {
98 .open = pagetypeinfo_open,
99 .read = seq_read,
100 .llseek = seq_lseek,
101 .release = seq_release,
102};
103
Nikita Danilov295ab932005-06-21 17:14:38 -0700104static int zoneinfo_open(struct inode *inode, struct file *file)
105{
106 return seq_open(file, &zoneinfo_op);
107}
108
Arjan van de Ven00977a52007-02-12 00:55:34 -0800109static const struct file_operations proc_zoneinfo_file_operations = {
Nikita Danilov295ab932005-06-21 17:14:38 -0700110 .open = zoneinfo_open,
111 .read = seq_read,
112 .llseek = seq_lseek,
113 .release = seq_release,
114};
115
Jan Engelhardt03a44822008-02-08 04:21:19 -0800116extern const struct seq_operations cpuinfo_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static int cpuinfo_open(struct inode *inode, struct file *file)
118{
119 return seq_open(file, &cpuinfo_op);
120}
Neil Horman7170be52006-01-14 13:20:38 -0800121
Arjan van de Ven00977a52007-02-12 00:55:34 -0800122static const struct file_operations proc_cpuinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800123 .open = cpuinfo_open,
Neil Horman7170be52006-01-14 13:20:38 -0800124 .read = seq_read,
125 .llseek = seq_lseek,
126 .release = seq_release,
127};
128
Joe Korty68eef3b2006-03-31 02:30:32 -0800129static int devinfo_show(struct seq_file *f, void *v)
130{
131 int i = *(loff_t *) v;
132
133 if (i < CHRDEV_MAJOR_HASH_SIZE) {
134 if (i == 0)
135 seq_printf(f, "Character devices:\n");
136 chrdev_show(f, i);
David Howells93614012006-09-30 20:45:40 +0200137 }
138#ifdef CONFIG_BLOCK
139 else {
Joe Korty68eef3b2006-03-31 02:30:32 -0800140 i -= CHRDEV_MAJOR_HASH_SIZE;
141 if (i == 0)
142 seq_printf(f, "\nBlock devices:\n");
143 blkdev_show(f, i);
144 }
David Howells93614012006-09-30 20:45:40 +0200145#endif
Joe Korty68eef3b2006-03-31 02:30:32 -0800146 return 0;
147}
148
149static void *devinfo_start(struct seq_file *f, loff_t *pos)
150{
151 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
152 return pos;
153 return NULL;
154}
155
156static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
157{
158 (*pos)++;
159 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
160 return NULL;
161 return pos;
162}
163
164static void devinfo_stop(struct seq_file *f, void *v)
165{
166 /* Nothing to do */
167}
168
Jan Engelhardt03a44822008-02-08 04:21:19 -0800169static const struct seq_operations devinfo_ops = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800170 .start = devinfo_start,
171 .next = devinfo_next,
172 .stop = devinfo_stop,
173 .show = devinfo_show
174};
175
176static int devinfo_open(struct inode *inode, struct file *filp)
177{
178 return seq_open(filp, &devinfo_ops);
179}
180
Arjan van de Ven00977a52007-02-12 00:55:34 -0800181static const struct file_operations proc_devinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800182 .open = devinfo_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .read = seq_read,
184 .llseek = seq_lseek,
185 .release = seq_release,
186};
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static int vmstat_open(struct inode *inode, struct file *file)
189{
190 return seq_open(file, &vmstat_op);
191}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800192static const struct file_operations proc_vmstat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .open = vmstat_open,
194 .read = seq_read,
195 .llseek = seq_lseek,
196 .release = seq_release,
197};
198
David Howells93614012006-09-30 20:45:40 +0200199#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static int partitions_open(struct inode *inode, struct file *file)
201{
202 return seq_open(file, &partitions_op);
203}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800204static const struct file_operations proc_partitions_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .open = partitions_open,
206 .read = seq_read,
207 .llseek = seq_lseek,
208 .release = seq_release,
209};
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static int diskstats_open(struct inode *inode, struct file *file)
212{
213 return seq_open(file, &diskstats_op);
214}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800215static const struct file_operations proc_diskstats_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .open = diskstats_open,
217 .read = seq_read,
218 .llseek = seq_lseek,
219 .release = seq_release,
220};
David Howells93614012006-09-30 20:45:40 +0200221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223#ifdef CONFIG_MODULES
Jan Engelhardt03a44822008-02-08 04:21:19 -0800224extern const struct seq_operations modules_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int modules_open(struct inode *inode, struct file *file)
226{
227 return seq_open(file, &modules_op);
228}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800229static const struct file_operations proc_modules_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .open = modules_open,
231 .read = seq_read,
232 .llseek = seq_lseek,
233 .release = seq_release,
234};
235#endif
236
Linus Torvalds158a9622008-01-02 13:04:48 -0800237#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static int slabinfo_open(struct inode *inode, struct file *file)
239{
240 return seq_open(file, &slabinfo_op);
241}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800242static const struct file_operations proc_slabinfo_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .open = slabinfo_open,
244 .read = seq_read,
245 .write = slabinfo_write,
246 .llseek = seq_lseek,
247 .release = seq_release,
248};
Al Viro871751e2006-03-25 03:06:39 -0800249
250#ifdef CONFIG_DEBUG_SLAB_LEAK
Jan Engelhardt03a44822008-02-08 04:21:19 -0800251extern const struct seq_operations slabstats_op;
Al Viro871751e2006-03-25 03:06:39 -0800252static int slabstats_open(struct inode *inode, struct file *file)
253{
254 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
255 int ret = -ENOMEM;
256 if (n) {
257 ret = seq_open(file, &slabstats_op);
258 if (!ret) {
259 struct seq_file *m = file->private_data;
260 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
261 m->private = n;
262 n = NULL;
263 }
264 kfree(n);
265 }
266 return ret;
267}
268
Arjan van de Ven00977a52007-02-12 00:55:34 -0800269static const struct file_operations proc_slabstats_operations = {
Al Viro871751e2006-03-25 03:06:39 -0800270 .open = slabstats_open,
271 .read = seq_read,
272 .llseek = seq_lseek,
Martin Peschke09f08922007-05-08 00:29:26 -0700273 .release = seq_release_private,
Al Viro871751e2006-03-25 03:06:39 -0800274};
275#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800276#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Christoph Lametera10aa572008-04-28 02:12:40 -0700278#ifdef CONFIG_MMU
279static int vmalloc_open(struct inode *inode, struct file *file)
280{
Eric Dumazeta47a1262008-07-23 21:27:38 -0700281 unsigned int *ptr = NULL;
282 int ret;
283
284 if (NUMA_BUILD)
285 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
286 ret = seq_open(file, &vmalloc_op);
287 if (!ret) {
288 struct seq_file *m = file->private_data;
289 m->private = ptr;
290 } else
291 kfree(ptr);
292 return ret;
Christoph Lametera10aa572008-04-28 02:12:40 -0700293}
294
295static const struct file_operations proc_vmalloc_operations = {
296 .open = vmalloc_open,
297 .read = seq_read,
298 .llseek = seq_lseek,
Eric Dumazeta47a1262008-07-23 21:27:38 -0700299 .release = seq_release_private,
Christoph Lametera10aa572008-04-28 02:12:40 -0700300};
301#endif
302
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200303#ifndef arch_irq_stat_cpu
304#define arch_irq_stat_cpu(cpu) 0
305#endif
306#ifndef arch_irq_stat
307#define arch_irq_stat() 0
308#endif
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static int show_stat(struct seq_file *p, void *v)
311{
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700312 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned long jif;
314 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200315 cputime64_t guest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 u64 sum = 0;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700317 struct timespec boottime;
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700318 unsigned int per_irq_sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 user = nice = system = idle = iowait =
321 irq = softirq = steal = cputime64_zero;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200322 guest = cputime64_zero;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700323 getboottime(&boottime);
324 jif = boottime.tv_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800326 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
328 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
329 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
330 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
331 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
332 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
333 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
334 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200335 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700336
Thomas Gleixner2be3b522008-10-16 14:50:27 +0200337 for_each_irq_nr(j)
Thomas Gleixner2cc21ef2008-10-15 14:16:55 +0200338 sum += kstat_irqs_cpu(j, i);
339
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200340 sum += arch_irq_stat_cpu(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200342 sum += arch_irq_stat();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200344 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 (unsigned long long)cputime64_to_clock_t(user),
346 (unsigned long long)cputime64_to_clock_t(nice),
347 (unsigned long long)cputime64_to_clock_t(system),
348 (unsigned long long)cputime64_to_clock_t(idle),
349 (unsigned long long)cputime64_to_clock_t(iowait),
350 (unsigned long long)cputime64_to_clock_t(irq),
351 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200352 (unsigned long long)cputime64_to_clock_t(steal),
353 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 for_each_online_cpu(i) {
355
356 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
357 user = kstat_cpu(i).cpustat.user;
358 nice = kstat_cpu(i).cpustat.nice;
359 system = kstat_cpu(i).cpustat.system;
360 idle = kstat_cpu(i).cpustat.idle;
361 iowait = kstat_cpu(i).cpustat.iowait;
362 irq = kstat_cpu(i).cpustat.irq;
363 softirq = kstat_cpu(i).cpustat.softirq;
364 steal = kstat_cpu(i).cpustat.steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200365 guest = kstat_cpu(i).cpustat.guest;
366 seq_printf(p,
367 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 i,
369 (unsigned long long)cputime64_to_clock_t(user),
370 (unsigned long long)cputime64_to_clock_t(nice),
371 (unsigned long long)cputime64_to_clock_t(system),
372 (unsigned long long)cputime64_to_clock_t(idle),
373 (unsigned long long)cputime64_to_clock_t(iowait),
374 (unsigned long long)cputime64_to_clock_t(irq),
375 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200376 (unsigned long long)cputime64_to_clock_t(steal),
377 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 seq_printf(p, "intr %llu", (unsigned long long)sum);
380
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700381 /* sum again ? it could be updated? */
Thomas Gleixner2be3b522008-10-16 14:50:27 +0200382 for_each_irq_nr(j) {
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700383 per_irq_sum = 0;
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700384
Thomas Gleixner2cc21ef2008-10-15 14:16:55 +0200385 for_each_possible_cpu(i)
386 per_irq_sum += kstat_irqs_cpu(j, i);
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700387
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700388 seq_printf(p, " %u", per_irq_sum);
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 seq_printf(p,
392 "\nctxt %llu\n"
393 "btime %lu\n"
394 "processes %lu\n"
395 "procs_running %lu\n"
396 "procs_blocked %lu\n",
397 nr_context_switches(),
398 (unsigned long)jif,
399 total_forks,
400 nr_running(),
401 nr_iowait());
402
403 return 0;
404}
405
406static int stat_open(struct inode *inode, struct file *file)
407{
408 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
409 char *buf;
410 struct seq_file *m;
411 int res;
412
413 /* don't ask for more than the kmalloc() max size, currently 128 KB */
414 if (size > 128 * 1024)
415 size = 128 * 1024;
416 buf = kmalloc(size, GFP_KERNEL);
417 if (!buf)
418 return -ENOMEM;
419
420 res = single_open(file, show_stat, NULL);
421 if (!res) {
422 m = file->private_data;
423 m->buf = buf;
424 m->size = size;
425 } else
426 kfree(buf);
427 return res;
428}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800429static const struct file_operations proc_stat_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 .open = stat_open,
431 .read = seq_read,
432 .llseek = seq_lseek,
433 .release = single_release,
434};
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/*
437 * /proc/interrupts
438 */
439static void *int_seq_start(struct seq_file *f, loff_t *pos)
440{
Yinghai Luda27c112008-08-19 20:49:56 -0700441 return (*pos <= nr_irqs) ? pos : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Yinghai Lu52b17322008-08-19 20:50:20 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
446{
447 (*pos)++;
Yinghai Lu52b17322008-08-19 20:50:20 -0700448 return (*pos <= nr_irqs) ? pos : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451static void int_seq_stop(struct seq_file *f, void *v)
452{
453 /* Nothing to do */
454}
455
Jan Engelhardt03a44822008-02-08 04:21:19 -0800456static const struct seq_operations int_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .start = int_seq_start,
458 .next = int_seq_next,
459 .stop = int_seq_stop,
460 .show = show_interrupts
461};
462
463static int interrupts_open(struct inode *inode, struct file *filp)
464{
465 return seq_open(filp, &int_seq_ops);
466}
467
Arjan van de Ven00977a52007-02-12 00:55:34 -0800468static const struct file_operations proc_interrupts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 .open = interrupts_open,
470 .read = seq_read,
471 .llseek = seq_lseek,
472 .release = seq_release,
473};
474
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200475#ifdef CONFIG_FILE_LOCKING
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700476static int locks_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700478 return seq_open(filp, &locks_seq_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700481static const struct file_operations proc_locks_operations = {
482 .open = locks_open,
483 .read = seq_read,
484 .llseek = seq_lseek,
485 .release = seq_release,
486};
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200487#endif /* CONFIG_FILE_LOCKING */
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static int execdomains_read_proc(char *page, char **start, off_t off,
490 int count, int *eof, void *data)
491{
492 int len = get_exec_domain_list(page);
493 return proc_calc_metrics(page, start, off, count, eof, len);
494}
495
Matt Mackall1e883282008-02-04 22:29:07 -0800496#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -0800497#define KPMSIZE sizeof(u64)
498#define KPMMASK (KPMSIZE - 1)
499/* /proc/kpagecount - an array exposing page counts
500 *
501 * Each entry is a u64 representing the corresponding
502 * physical page count.
503 */
504static ssize_t kpagecount_read(struct file *file, char __user *buf,
505 size_t count, loff_t *ppos)
506{
507 u64 __user *out = (u64 __user *)buf;
508 struct page *ppage;
509 unsigned long src = *ppos;
510 unsigned long pfn;
511 ssize_t ret = 0;
512 u64 pcount;
513
514 pfn = src / KPMSIZE;
515 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
516 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700517 return -EINVAL;
Matt Mackall161f47b2008-02-04 22:29:05 -0800518
519 while (count > 0) {
Matt Mackall304daa82008-02-04 22:29:06 -0800520 ppage = NULL;
521 if (pfn_valid(pfn))
522 ppage = pfn_to_page(pfn);
523 pfn++;
Matt Mackall161f47b2008-02-04 22:29:05 -0800524 if (!ppage)
525 pcount = 0;
526 else
Thomas Tuttlebbcdac02008-06-05 22:46:58 -0700527 pcount = page_mapcount(ppage);
Matt Mackall161f47b2008-02-04 22:29:05 -0800528
529 if (put_user(pcount, out++)) {
530 ret = -EFAULT;
531 break;
532 }
533
534 count -= KPMSIZE;
535 }
536
537 *ppos += (char __user *)out - buf;
538 if (!ret)
539 ret = (char __user *)out - buf;
540 return ret;
541}
542
543static struct file_operations proc_kpagecount_operations = {
544 .llseek = mem_lseek,
545 .read = kpagecount_read,
546};
547
Matt Mackall304daa82008-02-04 22:29:06 -0800548/* /proc/kpageflags - an array exposing page flags
549 *
550 * Each entry is a u64 representing the corresponding
551 * physical page flags.
552 */
553
554/* These macros are used to decouple internal flags from exported ones */
555
556#define KPF_LOCKED 0
557#define KPF_ERROR 1
558#define KPF_REFERENCED 2
559#define KPF_UPTODATE 3
560#define KPF_DIRTY 4
561#define KPF_LRU 5
562#define KPF_ACTIVE 6
563#define KPF_SLAB 7
564#define KPF_WRITEBACK 8
565#define KPF_RECLAIM 9
566#define KPF_BUDDY 10
567
568#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
569
570static ssize_t kpageflags_read(struct file *file, char __user *buf,
571 size_t count, loff_t *ppos)
572{
573 u64 __user *out = (u64 __user *)buf;
574 struct page *ppage;
575 unsigned long src = *ppos;
576 unsigned long pfn;
577 ssize_t ret = 0;
578 u64 kflags, uflags;
579
580 pfn = src / KPMSIZE;
581 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
582 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700583 return -EINVAL;
Matt Mackall304daa82008-02-04 22:29:06 -0800584
585 while (count > 0) {
586 ppage = NULL;
587 if (pfn_valid(pfn))
588 ppage = pfn_to_page(pfn);
589 pfn++;
590 if (!ppage)
591 kflags = 0;
592 else
593 kflags = ppage->flags;
594
595 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
596 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
597 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
598 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
599 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
600 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
601 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
602 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
603 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
604 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
605 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
606
607 if (put_user(uflags, out++)) {
608 ret = -EFAULT;
609 break;
610 }
611
612 count -= KPMSIZE;
613 }
614
615 *ppos += (char __user *)out - buf;
616 if (!ret)
617 ret = (char __user *)out - buf;
618 return ret;
619}
620
621static struct file_operations proc_kpageflags_operations = {
622 .llseek = mem_lseek,
623 .read = kpageflags_read,
624};
Matt Mackall1e883282008-02-04 22:29:07 -0800625#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall304daa82008-02-04 22:29:06 -0800626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627struct proc_dir_entry *proc_root_kcore;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629void __init proc_misc_init(void)
630{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 static struct {
632 char *name;
633 int (*read_proc)(char*,char**,off_t,int,int*,void*);
634 } *p, simple_ones[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 {"execdomains", execdomains_read_proc},
636 {NULL,}
637 };
638 for (p = simple_ones; p->name; p++)
639 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
640
641 proc_symlink("mounts", NULL, "self/mounts");
642
643 /* And now for trickier ones */
Mike Galbraithc36264d2006-12-06 20:37:42 -0800644#ifdef CONFIG_PRINTK
Alexey Dobriyanc74c1202008-04-29 01:01:44 -0700645 proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
Mike Galbraithc36264d2006-12-06 20:37:42 -0800646#endif
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200647#ifdef CONFIG_FILE_LOCKING
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700648 proc_create("locks", 0, NULL, &proc_locks_operations);
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200649#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700650 proc_create("devices", 0, NULL, &proc_devinfo_operations);
651 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
David Howells93614012006-09-30 20:45:40 +0200652#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700653 proc_create("partitions", 0, NULL, &proc_partitions_operations);
David Howells93614012006-09-30 20:45:40 +0200654#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700655 proc_create("stat", 0, NULL, &proc_stat_operations);
656 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
Linus Torvalds158a9622008-01-02 13:04:48 -0800657#ifdef CONFIG_SLABINFO
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700658 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
Al Viro871751e2006-03-25 03:06:39 -0800659#ifdef CONFIG_DEBUG_SLAB_LEAK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700660 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
Al Viro871751e2006-03-25 03:06:39 -0800661#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800662#endif
Christoph Lametera10aa572008-04-28 02:12:40 -0700663#ifdef CONFIG_MMU
664 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
665#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700666 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
667 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
668 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
669 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
David Howells93614012006-09-30 20:45:40 +0200670#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700671 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200672#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673#ifdef CONFIG_MODULES
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700674 proc_create("modules", 0, NULL, &proc_modules_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675#endif
676#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700677 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678#endif
679#ifdef CONFIG_PROC_KCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700680 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
681 if (proc_root_kcore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 proc_root_kcore->size =
683 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#endif
Matt Mackall1e883282008-02-04 22:29:07 -0800685#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700686 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
687 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
Matt Mackall1e883282008-02-04 22:29:07 -0800688#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700689#ifdef CONFIG_PROC_VMCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700690 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}