blob: 2686592dbcb2b01b29108da7587361afce9fa6c3 [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>
27#include <linux/proc_fs.h>
28#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mm.h>
30#include <linux/mmzone.h>
31#include <linux/pagemap.h>
Adrian Bunkf74596d2008-02-06 01:36:35 -080032#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/swap.h>
34#include <linux/slab.h>
35#include <linux/smp.h>
36#include <linux/signal.h>
37#include <linux/module.h>
38#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/seq_file.h>
40#include <linux/times.h>
41#include <linux/profile.h>
Andrew Morton7bf65382006-12-08 02:41:14 -080042#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/blkdev.h>
44#include <linux/hugetlb.h>
45#include <linux/jiffies.h>
46#include <linux/sysrq.h>
47#include <linux/vmalloc.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070048#include <linux/crash_dump.h>
Sukadev Bhattiprolu61a58c62006-12-08 02:37:58 -080049#include <linux/pid_namespace.h>
Matt Mackall161f47b2008-02-04 22:29:05 -080050#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/uaccess.h>
52#include <asm/pgtable.h>
53#include <asm/io.h>
54#include <asm/tlb.h>
55#include <asm/div64.h>
56#include "internal.h"
57
58#define LOAD_INT(x) ((x) >> FSHIFT)
59#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
60/*
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 */
66extern int get_hardware_list(char *);
67extern int get_stram_list(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068extern int get_exec_domain_list(char *);
69extern int get_dma_list(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static int proc_calc_metrics(char *page, char **start, off_t off,
72 int count, int *eof, int len)
73{
74 if (len <= off+count) *eof = 1;
75 *start = page + off;
76 len -= off;
77 if (len>count) len = count;
78 if (len<0) len = 0;
79 return len;
80}
81
82static int loadavg_read_proc(char *page, char **start, off_t off,
83 int count, int *eof, void *data)
84{
85 int a, b, c;
86 int len;
Michal Schmidt07a154b2008-02-06 01:37:07 -080087 unsigned long seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Michal Schmidt07a154b2008-02-06 01:37:07 -080089 do {
90 seq = read_seqbegin(&xtime_lock);
91 a = avenrun[0] + (FIXED_1/200);
92 b = avenrun[1] + (FIXED_1/200);
93 c = avenrun[2] + (FIXED_1/200);
94 } while (read_seqretry(&xtime_lock, seq));
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
97 LOAD_INT(a), LOAD_FRAC(a),
98 LOAD_INT(b), LOAD_FRAC(b),
99 LOAD_INT(c), LOAD_FRAC(c),
Sukadev Bhattiprolu2894d6502007-10-18 23:39:49 -0700100 nr_running(), nr_threads,
101 task_active_pid_ns(current)->last_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return proc_calc_metrics(page, start, off, count, eof, len);
103}
104
105static int uptime_read_proc(char *page, char **start, off_t off,
106 int count, int *eof, void *data)
107{
108 struct timespec uptime;
109 struct timespec idle;
110 int len;
111 cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
112
113 do_posix_clock_monotonic_gettime(&uptime);
Tomas Janousekd6214142007-07-15 23:39:42 -0700114 monotonic_to_bootbased(&uptime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 cputime_to_timespec(idletime, &idle);
116 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
117 (unsigned long) uptime.tv_sec,
118 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
119 (unsigned long) idle.tv_sec,
120 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
121
122 return proc_calc_metrics(page, start, off, count, eof, len);
123}
124
125static int meminfo_read_proc(char *page, char **start, off_t off,
126 int count, int *eof, void *data)
127{
128 struct sysinfo i;
129 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unsigned long committed;
131 unsigned long allowed;
132 struct vmalloc_info vmi;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700133 long cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * display in kilobytes.
137 */
138#define K(x) ((x) << (PAGE_SHIFT - 10))
139 si_meminfo(&i);
140 si_swapinfo(&i);
141 committed = atomic_read(&vm_committed_space);
142 allowed = ((totalram_pages - hugetlb_total_pages())
143 * sysctl_overcommit_ratio / 100) + total_swap_pages;
144
Christoph Lameter347ce432006-06-30 01:55:35 -0700145 cached = global_page_state(NR_FILE_PAGES) -
146 total_swapcache_pages - i.bufferram;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700147 if (cached < 0)
148 cached = 0;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 get_vmalloc_info(&vmi);
151
152 /*
153 * Tagged format, for easy grepping and expansion.
154 */
155 len = sprintf(page,
156 "MemTotal: %8lu kB\n"
157 "MemFree: %8lu kB\n"
158 "Buffers: %8lu kB\n"
159 "Cached: %8lu kB\n"
160 "SwapCached: %8lu kB\n"
161 "Active: %8lu kB\n"
162 "Inactive: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700163#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 "HighTotal: %8lu kB\n"
165 "HighFree: %8lu kB\n"
166 "LowTotal: %8lu kB\n"
167 "LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700168#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 "SwapTotal: %8lu kB\n"
170 "SwapFree: %8lu kB\n"
171 "Dirty: %8lu kB\n"
172 "Writeback: %8lu kB\n"
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700173 "AnonPages: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 "Mapped: %8lu kB\n"
175 "Slab: %8lu kB\n"
Christoph Lameter972d1a72006-09-25 23:31:51 -0700176 "SReclaimable: %8lu kB\n"
177 "SUnreclaim: %8lu kB\n"
Christoph Lameterdf849a12006-06-30 01:55:38 -0700178 "PageTables: %8lu kB\n"
Andrew Mortonf5ef68d2006-08-27 01:23:58 -0700179 "NFS_Unstable: %8lu kB\n"
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700180 "Bounce: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 "CommitLimit: %8lu kB\n"
182 "Committed_AS: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 "VmallocTotal: %8lu kB\n"
184 "VmallocUsed: %8lu kB\n"
185 "VmallocChunk: %8lu kB\n",
186 K(i.totalram),
187 K(i.freeram),
188 K(i.bufferram),
Martin Hicks4c4c4022005-04-16 15:24:08 -0700189 K(cached),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 K(total_swapcache_pages),
Christoph Lameter65e458d42007-02-10 01:43:05 -0800191 K(global_page_state(NR_ACTIVE)),
192 K(global_page_state(NR_INACTIVE)),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700193#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 K(i.totalhigh),
195 K(i.freehigh),
196 K(i.totalram-i.totalhigh),
197 K(i.freeram-i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 K(i.totalswap),
200 K(i.freeswap),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700201 K(global_page_state(NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -0700202 K(global_page_state(NR_WRITEBACK)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700203 K(global_page_state(NR_ANON_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700204 K(global_page_state(NR_FILE_MAPPED)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700205 K(global_page_state(NR_SLAB_RECLAIMABLE) +
206 global_page_state(NR_SLAB_UNRECLAIMABLE)),
207 K(global_page_state(NR_SLAB_RECLAIMABLE)),
208 K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
Christoph Lameterdf849a12006-06-30 01:55:38 -0700209 K(global_page_state(NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700210 K(global_page_state(NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700211 K(global_page_state(NR_BOUNCE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 K(allowed),
213 K(committed),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 (unsigned long)VMALLOC_TOTAL >> 10,
215 vmi.used >> 10,
216 vmi.largest_chunk >> 10
217 );
218
219 len += hugetlb_report_meminfo(page + len);
220
221 return proc_calc_metrics(page, start, off, count, eof, len);
222#undef K
223}
224
225extern struct seq_operations fragmentation_op;
226static int fragmentation_open(struct inode *inode, struct file *file)
227{
228 (void)inode;
229 return seq_open(file, &fragmentation_op);
230}
231
Arjan van de Ven00977a52007-02-12 00:55:34 -0800232static const struct file_operations fragmentation_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .open = fragmentation_open,
234 .read = seq_read,
235 .llseek = seq_lseek,
236 .release = seq_release,
237};
238
Mel Gorman467c9962007-10-16 01:26:02 -0700239extern struct seq_operations pagetypeinfo_op;
240static int pagetypeinfo_open(struct inode *inode, struct file *file)
241{
242 return seq_open(file, &pagetypeinfo_op);
243}
244
245static const struct file_operations pagetypeinfo_file_ops = {
246 .open = pagetypeinfo_open,
247 .read = seq_read,
248 .llseek = seq_lseek,
249 .release = seq_release,
250};
251
Nikita Danilov295ab932005-06-21 17:14:38 -0700252extern struct seq_operations zoneinfo_op;
253static int zoneinfo_open(struct inode *inode, struct file *file)
254{
255 return seq_open(file, &zoneinfo_op);
256}
257
Arjan van de Ven00977a52007-02-12 00:55:34 -0800258static const struct file_operations proc_zoneinfo_file_operations = {
Nikita Danilov295ab932005-06-21 17:14:38 -0700259 .open = zoneinfo_open,
260 .read = seq_read,
261 .llseek = seq_lseek,
262 .release = seq_release,
263};
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static int version_read_proc(char *page, char **start, off_t off,
266 int count, int *eof, void *data)
267{
268 int len;
269
Roman Zippel3eb3c742007-01-10 14:45:28 +0100270 len = snprintf(page, PAGE_SIZE, linux_proc_banner,
Linus Torvalds89937802006-12-11 09:28:46 -0800271 utsname()->sysname,
272 utsname()->release,
273 utsname()->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return proc_calc_metrics(page, start, off, count, eof, len);
275}
276
277extern struct seq_operations cpuinfo_op;
278static int cpuinfo_open(struct inode *inode, struct file *file)
279{
280 return seq_open(file, &cpuinfo_op);
281}
Neil Horman7170be52006-01-14 13:20:38 -0800282
Arjan van de Ven00977a52007-02-12 00:55:34 -0800283static const struct file_operations proc_cpuinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800284 .open = cpuinfo_open,
Neil Horman7170be52006-01-14 13:20:38 -0800285 .read = seq_read,
286 .llseek = seq_lseek,
287 .release = seq_release,
288};
289
Joe Korty68eef3b2006-03-31 02:30:32 -0800290static int devinfo_show(struct seq_file *f, void *v)
291{
292 int i = *(loff_t *) v;
293
294 if (i < CHRDEV_MAJOR_HASH_SIZE) {
295 if (i == 0)
296 seq_printf(f, "Character devices:\n");
297 chrdev_show(f, i);
David Howells93614012006-09-30 20:45:40 +0200298 }
299#ifdef CONFIG_BLOCK
300 else {
Joe Korty68eef3b2006-03-31 02:30:32 -0800301 i -= CHRDEV_MAJOR_HASH_SIZE;
302 if (i == 0)
303 seq_printf(f, "\nBlock devices:\n");
304 blkdev_show(f, i);
305 }
David Howells93614012006-09-30 20:45:40 +0200306#endif
Joe Korty68eef3b2006-03-31 02:30:32 -0800307 return 0;
308}
309
310static void *devinfo_start(struct seq_file *f, loff_t *pos)
311{
312 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
313 return pos;
314 return NULL;
315}
316
317static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
318{
319 (*pos)++;
320 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
321 return NULL;
322 return pos;
323}
324
325static void devinfo_stop(struct seq_file *f, void *v)
326{
327 /* Nothing to do */
328}
329
330static struct seq_operations devinfo_ops = {
331 .start = devinfo_start,
332 .next = devinfo_next,
333 .stop = devinfo_stop,
334 .show = devinfo_show
335};
336
337static int devinfo_open(struct inode *inode, struct file *filp)
338{
339 return seq_open(filp, &devinfo_ops);
340}
341
Arjan van de Ven00977a52007-02-12 00:55:34 -0800342static const struct file_operations proc_devinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800343 .open = devinfo_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 .read = seq_read,
345 .llseek = seq_lseek,
346 .release = seq_release,
347};
348
349extern struct seq_operations vmstat_op;
350static int vmstat_open(struct inode *inode, struct file *file)
351{
352 return seq_open(file, &vmstat_op);
353}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800354static const struct file_operations proc_vmstat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 .open = vmstat_open,
356 .read = seq_read,
357 .llseek = seq_lseek,
358 .release = seq_release,
359};
360
361#ifdef CONFIG_PROC_HARDWARE
362static int hardware_read_proc(char *page, char **start, off_t off,
363 int count, int *eof, void *data)
364{
365 int len = get_hardware_list(page);
366 return proc_calc_metrics(page, start, off, count, eof, len);
367}
368#endif
369
370#ifdef CONFIG_STRAM_PROC
371static int stram_read_proc(char *page, char **start, off_t off,
372 int count, int *eof, void *data)
373{
374 int len = get_stram_list(page);
375 return proc_calc_metrics(page, start, off, count, eof, len);
376}
377#endif
378
David Howells93614012006-09-30 20:45:40 +0200379#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380extern struct seq_operations partitions_op;
381static int partitions_open(struct inode *inode, struct file *file)
382{
383 return seq_open(file, &partitions_op);
384}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800385static const struct file_operations proc_partitions_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 .open = partitions_open,
387 .read = seq_read,
388 .llseek = seq_lseek,
389 .release = seq_release,
390};
391
392extern struct seq_operations diskstats_op;
393static int diskstats_open(struct inode *inode, struct file *file)
394{
395 return seq_open(file, &diskstats_op);
396}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800397static const struct file_operations proc_diskstats_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 .open = diskstats_open,
399 .read = seq_read,
400 .llseek = seq_lseek,
401 .release = seq_release,
402};
David Howells93614012006-09-30 20:45:40 +0200403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405#ifdef CONFIG_MODULES
406extern struct seq_operations modules_op;
407static int modules_open(struct inode *inode, struct file *file)
408{
409 return seq_open(file, &modules_op);
410}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800411static const struct file_operations proc_modules_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 .open = modules_open,
413 .read = seq_read,
414 .llseek = seq_lseek,
415 .release = seq_release,
416};
417#endif
418
Linus Torvalds158a9622008-01-02 13:04:48 -0800419#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static int slabinfo_open(struct inode *inode, struct file *file)
421{
422 return seq_open(file, &slabinfo_op);
423}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800424static const struct file_operations proc_slabinfo_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 .open = slabinfo_open,
426 .read = seq_read,
427 .write = slabinfo_write,
428 .llseek = seq_lseek,
429 .release = seq_release,
430};
Al Viro871751e2006-03-25 03:06:39 -0800431
432#ifdef CONFIG_DEBUG_SLAB_LEAK
433extern struct seq_operations slabstats_op;
434static int slabstats_open(struct inode *inode, struct file *file)
435{
436 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
437 int ret = -ENOMEM;
438 if (n) {
439 ret = seq_open(file, &slabstats_op);
440 if (!ret) {
441 struct seq_file *m = file->private_data;
442 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
443 m->private = n;
444 n = NULL;
445 }
446 kfree(n);
447 }
448 return ret;
449}
450
Arjan van de Ven00977a52007-02-12 00:55:34 -0800451static const struct file_operations proc_slabstats_operations = {
Al Viro871751e2006-03-25 03:06:39 -0800452 .open = slabstats_open,
453 .read = seq_read,
454 .llseek = seq_lseek,
Martin Peschke09f08922007-05-08 00:29:26 -0700455 .release = seq_release_private,
Al Viro871751e2006-03-25 03:06:39 -0800456};
457#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460static int show_stat(struct seq_file *p, void *v)
461{
462 int i;
463 unsigned long jif;
464 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200465 cputime64_t guest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 u64 sum = 0;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700467 struct timespec boottime;
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700468 unsigned int *per_irq_sum;
469
470 per_irq_sum = kzalloc(sizeof(unsigned int)*NR_IRQS, GFP_KERNEL);
471 if (!per_irq_sum)
472 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 user = nice = system = idle = iowait =
475 irq = softirq = steal = cputime64_zero;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200476 guest = cputime64_zero;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700477 getboottime(&boottime);
478 jif = boottime.tv_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800480 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int j;
482
483 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
484 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
485 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
486 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
487 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
488 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
489 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
490 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200491 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700492 for (j = 0; j < NR_IRQS; j++) {
493 unsigned int temp = kstat_cpu(i).irqs[j];
494 sum += temp;
495 per_irq_sum[j] += temp;
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200499 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 (unsigned long long)cputime64_to_clock_t(user),
501 (unsigned long long)cputime64_to_clock_t(nice),
502 (unsigned long long)cputime64_to_clock_t(system),
503 (unsigned long long)cputime64_to_clock_t(idle),
504 (unsigned long long)cputime64_to_clock_t(iowait),
505 (unsigned long long)cputime64_to_clock_t(irq),
506 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200507 (unsigned long long)cputime64_to_clock_t(steal),
508 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 for_each_online_cpu(i) {
510
511 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
512 user = kstat_cpu(i).cpustat.user;
513 nice = kstat_cpu(i).cpustat.nice;
514 system = kstat_cpu(i).cpustat.system;
515 idle = kstat_cpu(i).cpustat.idle;
516 iowait = kstat_cpu(i).cpustat.iowait;
517 irq = kstat_cpu(i).cpustat.irq;
518 softirq = kstat_cpu(i).cpustat.softirq;
519 steal = kstat_cpu(i).cpustat.steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200520 guest = kstat_cpu(i).cpustat.guest;
521 seq_printf(p,
522 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 i,
524 (unsigned long long)cputime64_to_clock_t(user),
525 (unsigned long long)cputime64_to_clock_t(nice),
526 (unsigned long long)cputime64_to_clock_t(system),
527 (unsigned long long)cputime64_to_clock_t(idle),
528 (unsigned long long)cputime64_to_clock_t(iowait),
529 (unsigned long long)cputime64_to_clock_t(irq),
530 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200531 (unsigned long long)cputime64_to_clock_t(steal),
532 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 seq_printf(p, "intr %llu", (unsigned long long)sum);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 for (i = 0; i < NR_IRQS; i++)
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700537 seq_printf(p, " %u", per_irq_sum[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 seq_printf(p,
540 "\nctxt %llu\n"
541 "btime %lu\n"
542 "processes %lu\n"
543 "procs_running %lu\n"
544 "procs_blocked %lu\n",
545 nr_context_switches(),
546 (unsigned long)jif,
547 total_forks,
548 nr_running(),
549 nr_iowait());
550
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700551 kfree(per_irq_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return 0;
553}
554
555static int stat_open(struct inode *inode, struct file *file)
556{
557 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
558 char *buf;
559 struct seq_file *m;
560 int res;
561
562 /* don't ask for more than the kmalloc() max size, currently 128 KB */
563 if (size > 128 * 1024)
564 size = 128 * 1024;
565 buf = kmalloc(size, GFP_KERNEL);
566 if (!buf)
567 return -ENOMEM;
568
569 res = single_open(file, show_stat, NULL);
570 if (!res) {
571 m = file->private_data;
572 m->buf = buf;
573 m->size = size;
574 } else
575 kfree(buf);
576 return res;
577}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800578static const struct file_operations proc_stat_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 .open = stat_open,
580 .read = seq_read,
581 .llseek = seq_lseek,
582 .release = single_release,
583};
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
586 * /proc/interrupts
587 */
588static void *int_seq_start(struct seq_file *f, loff_t *pos)
589{
590 return (*pos <= NR_IRQS) ? pos : NULL;
591}
592
593static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
594{
595 (*pos)++;
596 if (*pos > NR_IRQS)
597 return NULL;
598 return pos;
599}
600
601static void int_seq_stop(struct seq_file *f, void *v)
602{
603 /* Nothing to do */
604}
605
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607static struct seq_operations int_seq_ops = {
608 .start = int_seq_start,
609 .next = int_seq_next,
610 .stop = int_seq_stop,
611 .show = show_interrupts
612};
613
614static int interrupts_open(struct inode *inode, struct file *filp)
615{
616 return seq_open(filp, &int_seq_ops);
617}
618
Arjan van de Ven00977a52007-02-12 00:55:34 -0800619static const struct file_operations proc_interrupts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 .open = interrupts_open,
621 .read = seq_read,
622 .llseek = seq_lseek,
623 .release = seq_release,
624};
625
626static int filesystems_read_proc(char *page, char **start, off_t off,
627 int count, int *eof, void *data)
628{
629 int len = get_filesystem_list(page);
630 return proc_calc_metrics(page, start, off, count, eof, len);
631}
632
633static int cmdline_read_proc(char *page, char **start, off_t off,
634 int count, int *eof, void *data)
635{
636 int len;
637
638 len = sprintf(page, "%s\n", saved_command_line);
639 return proc_calc_metrics(page, start, off, count, eof, len);
640}
641
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700642static int locks_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700644 return seq_open(filp, &locks_seq_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700647static const struct file_operations proc_locks_operations = {
648 .open = locks_open,
649 .read = seq_read,
650 .llseek = seq_lseek,
651 .release = seq_release,
652};
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static int execdomains_read_proc(char *page, char **start, off_t off,
655 int count, int *eof, void *data)
656{
657 int len = get_exec_domain_list(page);
658 return proc_calc_metrics(page, start, off, count, eof, len);
659}
660
661#ifdef CONFIG_MAGIC_SYSRQ
662/*
663 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
664 */
665static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
666 size_t count, loff_t *ppos)
667{
668 if (count) {
669 char c;
670
671 if (get_user(c, buf))
672 return -EFAULT;
David Howells7d12e782006-10-05 14:55:46 +0100673 __handle_sysrq(c, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675 return count;
676}
677
Arjan van de Ven00977a52007-02-12 00:55:34 -0800678static const struct file_operations proc_sysrq_trigger_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 .write = write_sysrq_trigger,
680};
681#endif
682
Matt Mackall1e883282008-02-04 22:29:07 -0800683#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -0800684#define KPMSIZE sizeof(u64)
685#define KPMMASK (KPMSIZE - 1)
686/* /proc/kpagecount - an array exposing page counts
687 *
688 * Each entry is a u64 representing the corresponding
689 * physical page count.
690 */
691static ssize_t kpagecount_read(struct file *file, char __user *buf,
692 size_t count, loff_t *ppos)
693{
694 u64 __user *out = (u64 __user *)buf;
695 struct page *ppage;
696 unsigned long src = *ppos;
697 unsigned long pfn;
698 ssize_t ret = 0;
699 u64 pcount;
700
701 pfn = src / KPMSIZE;
702 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
703 if (src & KPMMASK || count & KPMMASK)
704 return -EIO;
705
706 while (count > 0) {
Matt Mackall304daa82008-02-04 22:29:06 -0800707 ppage = NULL;
708 if (pfn_valid(pfn))
709 ppage = pfn_to_page(pfn);
710 pfn++;
Matt Mackall161f47b2008-02-04 22:29:05 -0800711 if (!ppage)
712 pcount = 0;
713 else
714 pcount = atomic_read(&ppage->_count);
715
716 if (put_user(pcount, out++)) {
717 ret = -EFAULT;
718 break;
719 }
720
721 count -= KPMSIZE;
722 }
723
724 *ppos += (char __user *)out - buf;
725 if (!ret)
726 ret = (char __user *)out - buf;
727 return ret;
728}
729
730static struct file_operations proc_kpagecount_operations = {
731 .llseek = mem_lseek,
732 .read = kpagecount_read,
733};
734
Matt Mackall304daa82008-02-04 22:29:06 -0800735/* /proc/kpageflags - an array exposing page flags
736 *
737 * Each entry is a u64 representing the corresponding
738 * physical page flags.
739 */
740
741/* These macros are used to decouple internal flags from exported ones */
742
743#define KPF_LOCKED 0
744#define KPF_ERROR 1
745#define KPF_REFERENCED 2
746#define KPF_UPTODATE 3
747#define KPF_DIRTY 4
748#define KPF_LRU 5
749#define KPF_ACTIVE 6
750#define KPF_SLAB 7
751#define KPF_WRITEBACK 8
752#define KPF_RECLAIM 9
753#define KPF_BUDDY 10
754
755#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
756
757static ssize_t kpageflags_read(struct file *file, char __user *buf,
758 size_t count, loff_t *ppos)
759{
760 u64 __user *out = (u64 __user *)buf;
761 struct page *ppage;
762 unsigned long src = *ppos;
763 unsigned long pfn;
764 ssize_t ret = 0;
765 u64 kflags, uflags;
766
767 pfn = src / KPMSIZE;
768 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
769 if (src & KPMMASK || count & KPMMASK)
770 return -EIO;
771
772 while (count > 0) {
773 ppage = NULL;
774 if (pfn_valid(pfn))
775 ppage = pfn_to_page(pfn);
776 pfn++;
777 if (!ppage)
778 kflags = 0;
779 else
780 kflags = ppage->flags;
781
782 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
783 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
784 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
785 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
786 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
787 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
788 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
789 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
790 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
791 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
792 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
793
794 if (put_user(uflags, out++)) {
795 ret = -EFAULT;
796 break;
797 }
798
799 count -= KPMSIZE;
800 }
801
802 *ppos += (char __user *)out - buf;
803 if (!ret)
804 ret = (char __user *)out - buf;
805 return ret;
806}
807
808static struct file_operations proc_kpageflags_operations = {
809 .llseek = mem_lseek,
810 .read = kpageflags_read,
811};
Matt Mackall1e883282008-02-04 22:29:07 -0800812#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall304daa82008-02-04 22:29:06 -0800813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814struct proc_dir_entry *proc_root_kcore;
815
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800816void create_seq_entry(char *name, mode_t mode, const struct file_operations *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 struct proc_dir_entry *entry;
819 entry = create_proc_entry(name, mode, NULL);
820 if (entry)
821 entry->proc_fops = f;
822}
823
824void __init proc_misc_init(void)
825{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 static struct {
827 char *name;
828 int (*read_proc)(char*,char**,off_t,int,int*,void*);
829 } *p, simple_ones[] = {
830 {"loadavg", loadavg_read_proc},
831 {"uptime", uptime_read_proc},
832 {"meminfo", meminfo_read_proc},
833 {"version", version_read_proc},
834#ifdef CONFIG_PROC_HARDWARE
835 {"hardware", hardware_read_proc},
836#endif
837#ifdef CONFIG_STRAM_PROC
838 {"stram", stram_read_proc},
839#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 {"filesystems", filesystems_read_proc},
841 {"cmdline", cmdline_read_proc},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 {"execdomains", execdomains_read_proc},
843 {NULL,}
844 };
845 for (p = simple_ones; p->name; p++)
846 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
847
848 proc_symlink("mounts", NULL, "self/mounts");
849
850 /* And now for trickier ones */
Mike Galbraithc36264d2006-12-06 20:37:42 -0800851#ifdef CONFIG_PRINTK
Andrew Morton100bb932007-02-10 01:45:51 -0800852 {
853 struct proc_dir_entry *entry;
854 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
855 if (entry)
856 entry->proc_fops = &proc_kmsg_operations;
857 }
Mike Galbraithc36264d2006-12-06 20:37:42 -0800858#endif
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700859 create_seq_entry("locks", 0, &proc_locks_operations);
Neil Horman7170be52006-01-14 13:20:38 -0800860 create_seq_entry("devices", 0, &proc_devinfo_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
David Howells93614012006-09-30 20:45:40 +0200862#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 create_seq_entry("partitions", 0, &proc_partitions_operations);
David Howells93614012006-09-30 20:45:40 +0200864#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 create_seq_entry("stat", 0, &proc_stat_operations);
866 create_seq_entry("interrupts", 0, &proc_interrupts_operations);
Linus Torvalds158a9622008-01-02 13:04:48 -0800867#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
Al Viro871751e2006-03-25 03:06:39 -0800869#ifdef CONFIG_DEBUG_SLAB_LEAK
870 create_seq_entry("slab_allocators", 0 ,&proc_slabstats_operations);
871#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800872#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
Mel Gorman467c9962007-10-16 01:26:02 -0700874 create_seq_entry("pagetypeinfo", S_IRUGO, &pagetypeinfo_file_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
Nikita Danilov295ab932005-06-21 17:14:38 -0700876 create_seq_entry("zoneinfo",S_IRUGO, &proc_zoneinfo_file_operations);
David Howells93614012006-09-30 20:45:40 +0200877#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 create_seq_entry("diskstats", 0, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880#ifdef CONFIG_MODULES
881 create_seq_entry("modules", 0, &proc_modules_operations);
882#endif
883#ifdef CONFIG_SCHEDSTATS
884 create_seq_entry("schedstat", 0, &proc_schedstat_operations);
885#endif
886#ifdef CONFIG_PROC_KCORE
887 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
888 if (proc_root_kcore) {
889 proc_root_kcore->proc_fops = &proc_kcore_operations;
890 proc_root_kcore->size =
891 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
892 }
893#endif
Matt Mackall1e883282008-02-04 22:29:07 -0800894#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -0800895 create_seq_entry("kpagecount", S_IRUSR, &proc_kpagecount_operations);
Matt Mackall304daa82008-02-04 22:29:06 -0800896 create_seq_entry("kpageflags", S_IRUSR, &proc_kpageflags_operations);
Matt Mackall1e883282008-02-04 22:29:07 -0800897#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700898#ifdef CONFIG_PROC_VMCORE
899 proc_vmcore = create_proc_entry("vmcore", S_IRUSR, NULL);
900 if (proc_vmcore)
901 proc_vmcore->proc_fops = &proc_vmcore_operations;
902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903#ifdef CONFIG_MAGIC_SYSRQ
Andrew Morton100bb932007-02-10 01:45:51 -0800904 {
905 struct proc_dir_entry *entry;
906 entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
907 if (entry)
908 entry->proc_fops = &proc_sysrq_trigger_operations;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}