blob: 903e617bec585c669de8ffb0853dcbbdf4cf29fa [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>
Adrian Bunka0db7012008-03-04 11:23:50 +010035#include <linux/genhd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/smp.h>
37#include <linux/signal.h>
38#include <linux/module.h>
39#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/seq_file.h>
41#include <linux/times.h>
42#include <linux/profile.h>
Andrew Morton7bf65382006-12-08 02:41:14 -080043#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/blkdev.h>
45#include <linux/hugetlb.h>
46#include <linux/jiffies.h>
47#include <linux/sysrq.h>
48#include <linux/vmalloc.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070049#include <linux/crash_dump.h>
Sukadev Bhattiprolu61a58c62006-12-08 02:37:58 -080050#include <linux/pid_namespace.h>
Matt Mackall161f47b2008-02-04 22:29:05 -080051#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/uaccess.h>
53#include <asm/pgtable.h>
54#include <asm/io.h>
55#include <asm/tlb.h>
56#include <asm/div64.h>
57#include "internal.h"
58
59#define LOAD_INT(x) ((x) >> FSHIFT)
60#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
61/*
62 * Warning: stuff below (imported functions) assumes that its output will fit
63 * into one page. For some of those functions it may be wrong. Moreover, we
64 * have a way to deal with that gracefully. Right now I used straightforward
65 * wrappers, but this needs further analysis wrt potential overflows.
66 */
67extern int get_hardware_list(char *);
68extern int get_stram_list(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069extern int get_exec_domain_list(char *);
70extern int get_dma_list(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static int proc_calc_metrics(char *page, char **start, off_t off,
73 int count, int *eof, int len)
74{
75 if (len <= off+count) *eof = 1;
76 *start = page + off;
77 len -= off;
78 if (len>count) len = count;
79 if (len<0) len = 0;
80 return len;
81}
82
83static int loadavg_read_proc(char *page, char **start, off_t off,
84 int count, int *eof, void *data)
85{
86 int a, b, c;
87 int len;
Michal Schmidt07a154b2008-02-06 01:37:07 -080088 unsigned long seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Michal Schmidt07a154b2008-02-06 01:37:07 -080090 do {
91 seq = read_seqbegin(&xtime_lock);
92 a = avenrun[0] + (FIXED_1/200);
93 b = avenrun[1] + (FIXED_1/200);
94 c = avenrun[2] + (FIXED_1/200);
95 } while (read_seqretry(&xtime_lock, seq));
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
98 LOAD_INT(a), LOAD_FRAC(a),
99 LOAD_INT(b), LOAD_FRAC(b),
100 LOAD_INT(c), LOAD_FRAC(c),
Sukadev Bhattiprolu2894d6502007-10-18 23:39:49 -0700101 nr_running(), nr_threads,
102 task_active_pid_ns(current)->last_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return proc_calc_metrics(page, start, off, count, eof, len);
104}
105
106static int uptime_read_proc(char *page, char **start, off_t off,
107 int count, int *eof, void *data)
108{
109 struct timespec uptime;
110 struct timespec idle;
111 int len;
112 cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
113
114 do_posix_clock_monotonic_gettime(&uptime);
Tomas Janousekd6214142007-07-15 23:39:42 -0700115 monotonic_to_bootbased(&uptime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 cputime_to_timespec(idletime, &idle);
117 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
118 (unsigned long) uptime.tv_sec,
119 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
120 (unsigned long) idle.tv_sec,
121 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
122
123 return proc_calc_metrics(page, start, off, count, eof, len);
124}
125
126static int meminfo_read_proc(char *page, char **start, off_t off,
127 int count, int *eof, void *data)
128{
129 struct sysinfo i;
130 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned long committed;
132 unsigned long allowed;
133 struct vmalloc_info vmi;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700134 long cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * display in kilobytes.
138 */
139#define K(x) ((x) << (PAGE_SHIFT - 10))
140 si_meminfo(&i);
141 si_swapinfo(&i);
142 committed = atomic_read(&vm_committed_space);
143 allowed = ((totalram_pages - hugetlb_total_pages())
144 * sysctl_overcommit_ratio / 100) + total_swap_pages;
145
Christoph Lameter347ce432006-06-30 01:55:35 -0700146 cached = global_page_state(NR_FILE_PAGES) -
147 total_swapcache_pages - i.bufferram;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700148 if (cached < 0)
149 cached = 0;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 get_vmalloc_info(&vmi);
152
153 /*
154 * Tagged format, for easy grepping and expansion.
155 */
156 len = sprintf(page,
157 "MemTotal: %8lu kB\n"
158 "MemFree: %8lu kB\n"
159 "Buffers: %8lu kB\n"
160 "Cached: %8lu kB\n"
161 "SwapCached: %8lu kB\n"
162 "Active: %8lu kB\n"
163 "Inactive: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700164#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 "HighTotal: %8lu kB\n"
166 "HighFree: %8lu kB\n"
167 "LowTotal: %8lu kB\n"
168 "LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 "SwapTotal: %8lu kB\n"
171 "SwapFree: %8lu kB\n"
172 "Dirty: %8lu kB\n"
173 "Writeback: %8lu kB\n"
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700174 "AnonPages: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 "Mapped: %8lu kB\n"
176 "Slab: %8lu kB\n"
Christoph Lameter972d1a72006-09-25 23:31:51 -0700177 "SReclaimable: %8lu kB\n"
178 "SUnreclaim: %8lu kB\n"
Christoph Lameterdf849a12006-06-30 01:55:38 -0700179 "PageTables: %8lu kB\n"
Andrew Mortonf5ef68d2006-08-27 01:23:58 -0700180 "NFS_Unstable: %8lu kB\n"
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700181 "Bounce: %8lu kB\n"
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700182 "WritebackTmp: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 "CommitLimit: %8lu kB\n"
184 "Committed_AS: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 "VmallocTotal: %8lu kB\n"
186 "VmallocUsed: %8lu kB\n"
187 "VmallocChunk: %8lu kB\n",
188 K(i.totalram),
189 K(i.freeram),
190 K(i.bufferram),
Martin Hicks4c4c4022005-04-16 15:24:08 -0700191 K(cached),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 K(total_swapcache_pages),
Christoph Lameter65e458d42007-02-10 01:43:05 -0800193 K(global_page_state(NR_ACTIVE)),
194 K(global_page_state(NR_INACTIVE)),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700195#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 K(i.totalhigh),
197 K(i.freehigh),
198 K(i.totalram-i.totalhigh),
199 K(i.freeram-i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 K(i.totalswap),
202 K(i.freeswap),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700203 K(global_page_state(NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -0700204 K(global_page_state(NR_WRITEBACK)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700205 K(global_page_state(NR_ANON_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700206 K(global_page_state(NR_FILE_MAPPED)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700207 K(global_page_state(NR_SLAB_RECLAIMABLE) +
208 global_page_state(NR_SLAB_UNRECLAIMABLE)),
209 K(global_page_state(NR_SLAB_RECLAIMABLE)),
210 K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
Christoph Lameterdf849a12006-06-30 01:55:38 -0700211 K(global_page_state(NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700212 K(global_page_state(NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700213 K(global_page_state(NR_BOUNCE)),
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700214 K(global_page_state(NR_WRITEBACK_TEMP)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 K(allowed),
216 K(committed),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 (unsigned long)VMALLOC_TOTAL >> 10,
218 vmi.used >> 10,
219 vmi.largest_chunk >> 10
220 );
221
222 len += hugetlb_report_meminfo(page + len);
223
224 return proc_calc_metrics(page, start, off, count, eof, len);
225#undef K
226}
227
Jan Engelhardt03a44822008-02-08 04:21:19 -0800228extern const struct seq_operations fragmentation_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int fragmentation_open(struct inode *inode, struct file *file)
230{
231 (void)inode;
232 return seq_open(file, &fragmentation_op);
233}
234
Arjan van de Ven00977a52007-02-12 00:55:34 -0800235static const struct file_operations fragmentation_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .open = fragmentation_open,
237 .read = seq_read,
238 .llseek = seq_lseek,
239 .release = seq_release,
240};
241
Jan Engelhardt03a44822008-02-08 04:21:19 -0800242extern const struct seq_operations pagetypeinfo_op;
Mel Gorman467c9962007-10-16 01:26:02 -0700243static int pagetypeinfo_open(struct inode *inode, struct file *file)
244{
245 return seq_open(file, &pagetypeinfo_op);
246}
247
248static const struct file_operations pagetypeinfo_file_ops = {
249 .open = pagetypeinfo_open,
250 .read = seq_read,
251 .llseek = seq_lseek,
252 .release = seq_release,
253};
254
Jan Engelhardt03a44822008-02-08 04:21:19 -0800255extern const struct seq_operations zoneinfo_op;
Nikita Danilov295ab932005-06-21 17:14:38 -0700256static int zoneinfo_open(struct inode *inode, struct file *file)
257{
258 return seq_open(file, &zoneinfo_op);
259}
260
Arjan van de Ven00977a52007-02-12 00:55:34 -0800261static const struct file_operations proc_zoneinfo_file_operations = {
Nikita Danilov295ab932005-06-21 17:14:38 -0700262 .open = zoneinfo_open,
263 .read = seq_read,
264 .llseek = seq_lseek,
265 .release = seq_release,
266};
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static int version_read_proc(char *page, char **start, off_t off,
269 int count, int *eof, void *data)
270{
271 int len;
272
Roman Zippel3eb3c742007-01-10 14:45:28 +0100273 len = snprintf(page, PAGE_SIZE, linux_proc_banner,
Linus Torvalds89937802006-12-11 09:28:46 -0800274 utsname()->sysname,
275 utsname()->release,
276 utsname()->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return proc_calc_metrics(page, start, off, count, eof, len);
278}
279
Jan Engelhardt03a44822008-02-08 04:21:19 -0800280extern const struct seq_operations cpuinfo_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static int cpuinfo_open(struct inode *inode, struct file *file)
282{
283 return seq_open(file, &cpuinfo_op);
284}
Neil Horman7170be52006-01-14 13:20:38 -0800285
Arjan van de Ven00977a52007-02-12 00:55:34 -0800286static const struct file_operations proc_cpuinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800287 .open = cpuinfo_open,
Neil Horman7170be52006-01-14 13:20:38 -0800288 .read = seq_read,
289 .llseek = seq_lseek,
290 .release = seq_release,
291};
292
Joe Korty68eef3b2006-03-31 02:30:32 -0800293static int devinfo_show(struct seq_file *f, void *v)
294{
295 int i = *(loff_t *) v;
296
297 if (i < CHRDEV_MAJOR_HASH_SIZE) {
298 if (i == 0)
299 seq_printf(f, "Character devices:\n");
300 chrdev_show(f, i);
David Howells93614012006-09-30 20:45:40 +0200301 }
302#ifdef CONFIG_BLOCK
303 else {
Joe Korty68eef3b2006-03-31 02:30:32 -0800304 i -= CHRDEV_MAJOR_HASH_SIZE;
305 if (i == 0)
306 seq_printf(f, "\nBlock devices:\n");
307 blkdev_show(f, i);
308 }
David Howells93614012006-09-30 20:45:40 +0200309#endif
Joe Korty68eef3b2006-03-31 02:30:32 -0800310 return 0;
311}
312
313static void *devinfo_start(struct seq_file *f, loff_t *pos)
314{
315 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
316 return pos;
317 return NULL;
318}
319
320static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
321{
322 (*pos)++;
323 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
324 return NULL;
325 return pos;
326}
327
328static void devinfo_stop(struct seq_file *f, void *v)
329{
330 /* Nothing to do */
331}
332
Jan Engelhardt03a44822008-02-08 04:21:19 -0800333static const struct seq_operations devinfo_ops = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800334 .start = devinfo_start,
335 .next = devinfo_next,
336 .stop = devinfo_stop,
337 .show = devinfo_show
338};
339
340static int devinfo_open(struct inode *inode, struct file *filp)
341{
342 return seq_open(filp, &devinfo_ops);
343}
344
Arjan van de Ven00977a52007-02-12 00:55:34 -0800345static const struct file_operations proc_devinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800346 .open = devinfo_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 .read = seq_read,
348 .llseek = seq_lseek,
349 .release = seq_release,
350};
351
Jan Engelhardt03a44822008-02-08 04:21:19 -0800352extern const struct seq_operations vmstat_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static int vmstat_open(struct inode *inode, struct file *file)
354{
355 return seq_open(file, &vmstat_op);
356}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800357static const struct file_operations proc_vmstat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .open = vmstat_open,
359 .read = seq_read,
360 .llseek = seq_lseek,
361 .release = seq_release,
362};
363
364#ifdef CONFIG_PROC_HARDWARE
365static int hardware_read_proc(char *page, char **start, off_t off,
366 int count, int *eof, void *data)
367{
368 int len = get_hardware_list(page);
369 return proc_calc_metrics(page, start, off, count, eof, len);
370}
371#endif
372
373#ifdef CONFIG_STRAM_PROC
374static int stram_read_proc(char *page, char **start, off_t off,
375 int count, int *eof, void *data)
376{
377 int len = get_stram_list(page);
378 return proc_calc_metrics(page, start, off, count, eof, len);
379}
380#endif
381
David Howells93614012006-09-30 20:45:40 +0200382#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static int partitions_open(struct inode *inode, struct file *file)
384{
385 return seq_open(file, &partitions_op);
386}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800387static const struct file_operations proc_partitions_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 .open = partitions_open,
389 .read = seq_read,
390 .llseek = seq_lseek,
391 .release = seq_release,
392};
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static int diskstats_open(struct inode *inode, struct file *file)
395{
396 return seq_open(file, &diskstats_op);
397}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800398static const struct file_operations proc_diskstats_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 .open = diskstats_open,
400 .read = seq_read,
401 .llseek = seq_lseek,
402 .release = seq_release,
403};
David Howells93614012006-09-30 20:45:40 +0200404#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406#ifdef CONFIG_MODULES
Jan Engelhardt03a44822008-02-08 04:21:19 -0800407extern const struct seq_operations modules_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static int modules_open(struct inode *inode, struct file *file)
409{
410 return seq_open(file, &modules_op);
411}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800412static const struct file_operations proc_modules_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 .open = modules_open,
414 .read = seq_read,
415 .llseek = seq_lseek,
416 .release = seq_release,
417};
418#endif
419
Linus Torvalds158a9622008-01-02 13:04:48 -0800420#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static int slabinfo_open(struct inode *inode, struct file *file)
422{
423 return seq_open(file, &slabinfo_op);
424}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800425static const struct file_operations proc_slabinfo_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 .open = slabinfo_open,
427 .read = seq_read,
428 .write = slabinfo_write,
429 .llseek = seq_lseek,
430 .release = seq_release,
431};
Al Viro871751e2006-03-25 03:06:39 -0800432
433#ifdef CONFIG_DEBUG_SLAB_LEAK
Jan Engelhardt03a44822008-02-08 04:21:19 -0800434extern const struct seq_operations slabstats_op;
Al Viro871751e2006-03-25 03:06:39 -0800435static int slabstats_open(struct inode *inode, struct file *file)
436{
437 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
438 int ret = -ENOMEM;
439 if (n) {
440 ret = seq_open(file, &slabstats_op);
441 if (!ret) {
442 struct seq_file *m = file->private_data;
443 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
444 m->private = n;
445 n = NULL;
446 }
447 kfree(n);
448 }
449 return ret;
450}
451
Arjan van de Ven00977a52007-02-12 00:55:34 -0800452static const struct file_operations proc_slabstats_operations = {
Al Viro871751e2006-03-25 03:06:39 -0800453 .open = slabstats_open,
454 .read = seq_read,
455 .llseek = seq_lseek,
Martin Peschke09f08922007-05-08 00:29:26 -0700456 .release = seq_release_private,
Al Viro871751e2006-03-25 03:06:39 -0800457};
458#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Christoph Lametera10aa572008-04-28 02:12:40 -0700461#ifdef CONFIG_MMU
462static int vmalloc_open(struct inode *inode, struct file *file)
463{
464 return seq_open(file, &vmalloc_op);
465}
466
467static const struct file_operations proc_vmalloc_operations = {
468 .open = vmalloc_open,
469 .read = seq_read,
470 .llseek = seq_lseek,
471 .release = seq_release,
472};
473#endif
474
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200475#ifndef arch_irq_stat_cpu
476#define arch_irq_stat_cpu(cpu) 0
477#endif
478#ifndef arch_irq_stat
479#define arch_irq_stat() 0
480#endif
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static int show_stat(struct seq_file *p, void *v)
483{
484 int i;
485 unsigned long jif;
486 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200487 cputime64_t guest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 u64 sum = 0;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700489 struct timespec boottime;
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700490 unsigned int *per_irq_sum;
491
492 per_irq_sum = kzalloc(sizeof(unsigned int)*NR_IRQS, GFP_KERNEL);
493 if (!per_irq_sum)
494 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 user = nice = system = idle = iowait =
497 irq = softirq = steal = cputime64_zero;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200498 guest = cputime64_zero;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700499 getboottime(&boottime);
500 jif = boottime.tv_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800502 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int j;
504
505 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
506 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
507 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
508 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
509 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
510 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
511 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
512 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200513 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700514 for (j = 0; j < NR_IRQS; j++) {
515 unsigned int temp = kstat_cpu(i).irqs[j];
516 sum += temp;
517 per_irq_sum[j] += temp;
518 }
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200519 sum += arch_irq_stat_cpu(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200521 sum += arch_irq_stat();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200523 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 (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 for_each_online_cpu(i) {
534
535 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
536 user = kstat_cpu(i).cpustat.user;
537 nice = kstat_cpu(i).cpustat.nice;
538 system = kstat_cpu(i).cpustat.system;
539 idle = kstat_cpu(i).cpustat.idle;
540 iowait = kstat_cpu(i).cpustat.iowait;
541 irq = kstat_cpu(i).cpustat.irq;
542 softirq = kstat_cpu(i).cpustat.softirq;
543 steal = kstat_cpu(i).cpustat.steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200544 guest = kstat_cpu(i).cpustat.guest;
545 seq_printf(p,
546 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 i,
548 (unsigned long long)cputime64_to_clock_t(user),
549 (unsigned long long)cputime64_to_clock_t(nice),
550 (unsigned long long)cputime64_to_clock_t(system),
551 (unsigned long long)cputime64_to_clock_t(idle),
552 (unsigned long long)cputime64_to_clock_t(iowait),
553 (unsigned long long)cputime64_to_clock_t(irq),
554 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200555 (unsigned long long)cputime64_to_clock_t(steal),
556 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 seq_printf(p, "intr %llu", (unsigned long long)sum);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 for (i = 0; i < NR_IRQS; i++)
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700561 seq_printf(p, " %u", per_irq_sum[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 seq_printf(p,
564 "\nctxt %llu\n"
565 "btime %lu\n"
566 "processes %lu\n"
567 "procs_running %lu\n"
568 "procs_blocked %lu\n",
569 nr_context_switches(),
570 (unsigned long)jif,
571 total_forks,
572 nr_running(),
573 nr_iowait());
574
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700575 kfree(per_irq_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
577}
578
579static int stat_open(struct inode *inode, struct file *file)
580{
581 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
582 char *buf;
583 struct seq_file *m;
584 int res;
585
586 /* don't ask for more than the kmalloc() max size, currently 128 KB */
587 if (size > 128 * 1024)
588 size = 128 * 1024;
589 buf = kmalloc(size, GFP_KERNEL);
590 if (!buf)
591 return -ENOMEM;
592
593 res = single_open(file, show_stat, NULL);
594 if (!res) {
595 m = file->private_data;
596 m->buf = buf;
597 m->size = size;
598 } else
599 kfree(buf);
600 return res;
601}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800602static const struct file_operations proc_stat_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 .open = stat_open,
604 .read = seq_read,
605 .llseek = seq_lseek,
606 .release = single_release,
607};
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*
610 * /proc/interrupts
611 */
612static void *int_seq_start(struct seq_file *f, loff_t *pos)
613{
614 return (*pos <= NR_IRQS) ? pos : NULL;
615}
616
617static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
618{
619 (*pos)++;
620 if (*pos > NR_IRQS)
621 return NULL;
622 return pos;
623}
624
625static void int_seq_stop(struct seq_file *f, void *v)
626{
627 /* Nothing to do */
628}
629
630
Jan Engelhardt03a44822008-02-08 04:21:19 -0800631static const struct seq_operations int_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 .start = int_seq_start,
633 .next = int_seq_next,
634 .stop = int_seq_stop,
635 .show = show_interrupts
636};
637
638static int interrupts_open(struct inode *inode, struct file *filp)
639{
640 return seq_open(filp, &int_seq_ops);
641}
642
Arjan van de Ven00977a52007-02-12 00:55:34 -0800643static const struct file_operations proc_interrupts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 .open = interrupts_open,
645 .read = seq_read,
646 .llseek = seq_lseek,
647 .release = seq_release,
648};
649
650static int filesystems_read_proc(char *page, char **start, off_t off,
651 int count, int *eof, void *data)
652{
653 int len = get_filesystem_list(page);
654 return proc_calc_metrics(page, start, off, count, eof, len);
655}
656
657static int cmdline_read_proc(char *page, char **start, off_t off,
658 int count, int *eof, void *data)
659{
660 int len;
661
662 len = sprintf(page, "%s\n", saved_command_line);
663 return proc_calc_metrics(page, start, off, count, eof, len);
664}
665
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700666static int locks_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700668 return seq_open(filp, &locks_seq_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700671static const struct file_operations proc_locks_operations = {
672 .open = locks_open,
673 .read = seq_read,
674 .llseek = seq_lseek,
675 .release = seq_release,
676};
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static int execdomains_read_proc(char *page, char **start, off_t off,
679 int count, int *eof, void *data)
680{
681 int len = get_exec_domain_list(page);
682 return proc_calc_metrics(page, start, off, count, eof, len);
683}
684
685#ifdef CONFIG_MAGIC_SYSRQ
686/*
687 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
688 */
689static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
690 size_t count, loff_t *ppos)
691{
692 if (count) {
693 char c;
694
695 if (get_user(c, buf))
696 return -EFAULT;
David Howells7d12e782006-10-05 14:55:46 +0100697 __handle_sysrq(c, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 return count;
700}
701
Arjan van de Ven00977a52007-02-12 00:55:34 -0800702static const struct file_operations proc_sysrq_trigger_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 .write = write_sysrq_trigger,
704};
705#endif
706
Matt Mackall1e883282008-02-04 22:29:07 -0800707#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -0800708#define KPMSIZE sizeof(u64)
709#define KPMMASK (KPMSIZE - 1)
710/* /proc/kpagecount - an array exposing page counts
711 *
712 * Each entry is a u64 representing the corresponding
713 * physical page count.
714 */
715static ssize_t kpagecount_read(struct file *file, char __user *buf,
716 size_t count, loff_t *ppos)
717{
718 u64 __user *out = (u64 __user *)buf;
719 struct page *ppage;
720 unsigned long src = *ppos;
721 unsigned long pfn;
722 ssize_t ret = 0;
723 u64 pcount;
724
725 pfn = src / KPMSIZE;
726 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
727 if (src & KPMMASK || count & KPMMASK)
728 return -EIO;
729
730 while (count > 0) {
Matt Mackall304daa82008-02-04 22:29:06 -0800731 ppage = NULL;
732 if (pfn_valid(pfn))
733 ppage = pfn_to_page(pfn);
734 pfn++;
Matt Mackall161f47b2008-02-04 22:29:05 -0800735 if (!ppage)
736 pcount = 0;
737 else
738 pcount = atomic_read(&ppage->_count);
739
740 if (put_user(pcount, out++)) {
741 ret = -EFAULT;
742 break;
743 }
744
745 count -= KPMSIZE;
746 }
747
748 *ppos += (char __user *)out - buf;
749 if (!ret)
750 ret = (char __user *)out - buf;
751 return ret;
752}
753
754static struct file_operations proc_kpagecount_operations = {
755 .llseek = mem_lseek,
756 .read = kpagecount_read,
757};
758
Matt Mackall304daa82008-02-04 22:29:06 -0800759/* /proc/kpageflags - an array exposing page flags
760 *
761 * Each entry is a u64 representing the corresponding
762 * physical page flags.
763 */
764
765/* These macros are used to decouple internal flags from exported ones */
766
767#define KPF_LOCKED 0
768#define KPF_ERROR 1
769#define KPF_REFERENCED 2
770#define KPF_UPTODATE 3
771#define KPF_DIRTY 4
772#define KPF_LRU 5
773#define KPF_ACTIVE 6
774#define KPF_SLAB 7
775#define KPF_WRITEBACK 8
776#define KPF_RECLAIM 9
777#define KPF_BUDDY 10
778
779#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
780
781static ssize_t kpageflags_read(struct file *file, char __user *buf,
782 size_t count, loff_t *ppos)
783{
784 u64 __user *out = (u64 __user *)buf;
785 struct page *ppage;
786 unsigned long src = *ppos;
787 unsigned long pfn;
788 ssize_t ret = 0;
789 u64 kflags, uflags;
790
791 pfn = src / KPMSIZE;
792 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
793 if (src & KPMMASK || count & KPMMASK)
794 return -EIO;
795
796 while (count > 0) {
797 ppage = NULL;
798 if (pfn_valid(pfn))
799 ppage = pfn_to_page(pfn);
800 pfn++;
801 if (!ppage)
802 kflags = 0;
803 else
804 kflags = ppage->flags;
805
806 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
807 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
808 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
809 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
810 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
811 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
812 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
813 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
814 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
815 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
816 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
817
818 if (put_user(uflags, out++)) {
819 ret = -EFAULT;
820 break;
821 }
822
823 count -= KPMSIZE;
824 }
825
826 *ppos += (char __user *)out - buf;
827 if (!ret)
828 ret = (char __user *)out - buf;
829 return ret;
830}
831
832static struct file_operations proc_kpageflags_operations = {
833 .llseek = mem_lseek,
834 .read = kpageflags_read,
835};
Matt Mackall1e883282008-02-04 22:29:07 -0800836#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall304daa82008-02-04 22:29:06 -0800837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838struct proc_dir_entry *proc_root_kcore;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840void __init proc_misc_init(void)
841{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 static struct {
843 char *name;
844 int (*read_proc)(char*,char**,off_t,int,int*,void*);
845 } *p, simple_ones[] = {
846 {"loadavg", loadavg_read_proc},
847 {"uptime", uptime_read_proc},
848 {"meminfo", meminfo_read_proc},
849 {"version", version_read_proc},
850#ifdef CONFIG_PROC_HARDWARE
851 {"hardware", hardware_read_proc},
852#endif
853#ifdef CONFIG_STRAM_PROC
854 {"stram", stram_read_proc},
855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 {"filesystems", filesystems_read_proc},
857 {"cmdline", cmdline_read_proc},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 {"execdomains", execdomains_read_proc},
859 {NULL,}
860 };
861 for (p = simple_ones; p->name; p++)
862 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
863
864 proc_symlink("mounts", NULL, "self/mounts");
865
866 /* And now for trickier ones */
Mike Galbraithc36264d2006-12-06 20:37:42 -0800867#ifdef CONFIG_PRINTK
Alexey Dobriyanc74c1202008-04-29 01:01:44 -0700868 proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
Mike Galbraithc36264d2006-12-06 20:37:42 -0800869#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700870 proc_create("locks", 0, NULL, &proc_locks_operations);
871 proc_create("devices", 0, NULL, &proc_devinfo_operations);
872 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
David Howells93614012006-09-30 20:45:40 +0200873#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700874 proc_create("partitions", 0, NULL, &proc_partitions_operations);
David Howells93614012006-09-30 20:45:40 +0200875#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700876 proc_create("stat", 0, NULL, &proc_stat_operations);
877 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
Linus Torvalds158a9622008-01-02 13:04:48 -0800878#ifdef CONFIG_SLABINFO
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700879 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
Al Viro871751e2006-03-25 03:06:39 -0800880#ifdef CONFIG_DEBUG_SLAB_LEAK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700881 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
Al Viro871751e2006-03-25 03:06:39 -0800882#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800883#endif
Christoph Lametera10aa572008-04-28 02:12:40 -0700884#ifdef CONFIG_MMU
885 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
886#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700887 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
888 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
889 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
890 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
David Howells93614012006-09-30 20:45:40 +0200891#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700892 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200893#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#ifdef CONFIG_MODULES
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700895 proc_create("modules", 0, NULL, &proc_modules_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896#endif
897#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700898 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif
900#ifdef CONFIG_PROC_KCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700901 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
902 if (proc_root_kcore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 proc_root_kcore->size =
904 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#endif
Matt Mackall1e883282008-02-04 22:29:07 -0800906#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700907 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
908 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
Matt Mackall1e883282008-02-04 22:29:07 -0800909#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700910#ifdef CONFIG_PROC_VMCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700911 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700912#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913#ifdef CONFIG_MAGIC_SYSRQ
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700914 proc_create("sysrq-trigger", S_IWUSR, NULL, &proc_sysrq_trigger_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}