blob: 59ea42e1ef03a0a4acdf3f73283d913b740415fd [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>
Adrian Bunkf74596d2008-02-06 01:36:35 -080033#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/swap.h>
35#include <linux/slab.h>
Adrian Bunka0db7012008-03-04 11:23:50 +010036#include <linux/genhd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/smp.h>
38#include <linux/signal.h>
39#include <linux/module.h>
40#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/seq_file.h>
42#include <linux/times.h>
43#include <linux/profile.h>
Andrew Morton7bf65382006-12-08 02:41:14 -080044#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/blkdev.h>
46#include <linux/hugetlb.h>
47#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#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 *);
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
Andi Kleence0c0e52008-05-02 11:46:49 +0200125int __attribute__((weak)) arch_report_meminfo(char *page)
126{
127 return 0;
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static int meminfo_read_proc(char *page, char **start, off_t off,
131 int count, int *eof, void *data)
132{
133 struct sysinfo i;
134 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned long committed;
136 unsigned long allowed;
137 struct vmalloc_info vmi;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700138 long cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * display in kilobytes.
142 */
143#define K(x) ((x) << (PAGE_SHIFT - 10))
144 si_meminfo(&i);
145 si_swapinfo(&i);
Alan Cox80119ef2008-05-23 13:04:31 -0700146 committed = atomic_long_read(&vm_committed_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 allowed = ((totalram_pages - hugetlb_total_pages())
148 * sysctl_overcommit_ratio / 100) + total_swap_pages;
149
Christoph Lameter347ce432006-06-30 01:55:35 -0700150 cached = global_page_state(NR_FILE_PAGES) -
151 total_swapcache_pages - i.bufferram;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700152 if (cached < 0)
153 cached = 0;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 get_vmalloc_info(&vmi);
156
157 /*
158 * Tagged format, for easy grepping and expansion.
159 */
160 len = sprintf(page,
161 "MemTotal: %8lu kB\n"
162 "MemFree: %8lu kB\n"
163 "Buffers: %8lu kB\n"
164 "Cached: %8lu kB\n"
165 "SwapCached: %8lu kB\n"
166 "Active: %8lu kB\n"
167 "Inactive: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700168#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 "HighTotal: %8lu kB\n"
170 "HighFree: %8lu kB\n"
171 "LowTotal: %8lu kB\n"
172 "LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 "SwapTotal: %8lu kB\n"
175 "SwapFree: %8lu kB\n"
176 "Dirty: %8lu kB\n"
177 "Writeback: %8lu kB\n"
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700178 "AnonPages: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 "Mapped: %8lu kB\n"
180 "Slab: %8lu kB\n"
Christoph Lameter972d1a72006-09-25 23:31:51 -0700181 "SReclaimable: %8lu kB\n"
182 "SUnreclaim: %8lu kB\n"
Christoph Lameterdf849a12006-06-30 01:55:38 -0700183 "PageTables: %8lu kB\n"
Hugh Dickinsd7a3e492008-09-13 02:33:13 -0700184#ifdef CONFIG_QUICKLIST
185 "Quicklists: %8lu kB\n"
186#endif
Andrew Mortonf5ef68d2006-08-27 01:23:58 -0700187 "NFS_Unstable: %8lu kB\n"
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700188 "Bounce: %8lu kB\n"
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700189 "WritebackTmp: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 "CommitLimit: %8lu kB\n"
191 "Committed_AS: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 "VmallocTotal: %8lu kB\n"
193 "VmallocUsed: %8lu kB\n"
Hugh Dickinsd7a3e492008-09-13 02:33:13 -0700194 "VmallocChunk: %8lu kB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 K(i.totalram),
196 K(i.freeram),
197 K(i.bufferram),
Martin Hicks4c4c4022005-04-16 15:24:08 -0700198 K(cached),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 K(total_swapcache_pages),
Christoph Lameter65e458d42007-02-10 01:43:05 -0800200 K(global_page_state(NR_ACTIVE)),
201 K(global_page_state(NR_INACTIVE)),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700202#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 K(i.totalhigh),
204 K(i.freehigh),
205 K(i.totalram-i.totalhigh),
206 K(i.freeram-i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 K(i.totalswap),
209 K(i.freeswap),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700210 K(global_page_state(NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -0700211 K(global_page_state(NR_WRITEBACK)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700212 K(global_page_state(NR_ANON_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700213 K(global_page_state(NR_FILE_MAPPED)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700214 K(global_page_state(NR_SLAB_RECLAIMABLE) +
215 global_page_state(NR_SLAB_UNRECLAIMABLE)),
216 K(global_page_state(NR_SLAB_RECLAIMABLE)),
217 K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
Christoph Lameterdf849a12006-06-30 01:55:38 -0700218 K(global_page_state(NR_PAGETABLE)),
Hugh Dickinsd7a3e492008-09-13 02:33:13 -0700219#ifdef CONFIG_QUICKLIST
220 K(quicklist_total_size()),
221#endif
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700222 K(global_page_state(NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700223 K(global_page_state(NR_BOUNCE)),
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700224 K(global_page_state(NR_WRITEBACK_TEMP)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 K(allowed),
226 K(committed),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 (unsigned long)VMALLOC_TOTAL >> 10,
228 vmi.used >> 10,
Hugh Dickinsd7a3e492008-09-13 02:33:13 -0700229 vmi.largest_chunk >> 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 );
231
232 len += hugetlb_report_meminfo(page + len);
233
Andi Kleence0c0e52008-05-02 11:46:49 +0200234 len += arch_report_meminfo(page + len);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return proc_calc_metrics(page, start, off, count, eof, len);
237#undef K
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static int fragmentation_open(struct inode *inode, struct file *file)
241{
242 (void)inode;
243 return seq_open(file, &fragmentation_op);
244}
245
Arjan van de Ven00977a52007-02-12 00:55:34 -0800246static const struct file_operations fragmentation_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .open = fragmentation_open,
248 .read = seq_read,
249 .llseek = seq_lseek,
250 .release = seq_release,
251};
252
Mel Gorman467c9962007-10-16 01:26:02 -0700253static int pagetypeinfo_open(struct inode *inode, struct file *file)
254{
255 return seq_open(file, &pagetypeinfo_op);
256}
257
258static const struct file_operations pagetypeinfo_file_ops = {
259 .open = pagetypeinfo_open,
260 .read = seq_read,
261 .llseek = seq_lseek,
262 .release = seq_release,
263};
264
Nikita Danilov295ab932005-06-21 17:14:38 -0700265static int zoneinfo_open(struct inode *inode, struct file *file)
266{
267 return seq_open(file, &zoneinfo_op);
268}
269
Arjan van de Ven00977a52007-02-12 00:55:34 -0800270static const struct file_operations proc_zoneinfo_file_operations = {
Nikita Danilov295ab932005-06-21 17:14:38 -0700271 .open = zoneinfo_open,
272 .read = seq_read,
273 .llseek = seq_lseek,
274 .release = seq_release,
275};
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static int version_read_proc(char *page, char **start, off_t off,
278 int count, int *eof, void *data)
279{
280 int len;
281
Roman Zippel3eb3c742007-01-10 14:45:28 +0100282 len = snprintf(page, PAGE_SIZE, linux_proc_banner,
Linus Torvalds89937802006-12-11 09:28:46 -0800283 utsname()->sysname,
284 utsname()->release,
285 utsname()->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return proc_calc_metrics(page, start, off, count, eof, len);
287}
288
Jan Engelhardt03a44822008-02-08 04:21:19 -0800289extern const struct seq_operations cpuinfo_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static int cpuinfo_open(struct inode *inode, struct file *file)
291{
292 return seq_open(file, &cpuinfo_op);
293}
Neil Horman7170be52006-01-14 13:20:38 -0800294
Arjan van de Ven00977a52007-02-12 00:55:34 -0800295static const struct file_operations proc_cpuinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800296 .open = cpuinfo_open,
Neil Horman7170be52006-01-14 13:20:38 -0800297 .read = seq_read,
298 .llseek = seq_lseek,
299 .release = seq_release,
300};
301
Joe Korty68eef3b2006-03-31 02:30:32 -0800302static int devinfo_show(struct seq_file *f, void *v)
303{
304 int i = *(loff_t *) v;
305
306 if (i < CHRDEV_MAJOR_HASH_SIZE) {
307 if (i == 0)
308 seq_printf(f, "Character devices:\n");
309 chrdev_show(f, i);
David Howells93614012006-09-30 20:45:40 +0200310 }
311#ifdef CONFIG_BLOCK
312 else {
Joe Korty68eef3b2006-03-31 02:30:32 -0800313 i -= CHRDEV_MAJOR_HASH_SIZE;
314 if (i == 0)
315 seq_printf(f, "\nBlock devices:\n");
316 blkdev_show(f, i);
317 }
David Howells93614012006-09-30 20:45:40 +0200318#endif
Joe Korty68eef3b2006-03-31 02:30:32 -0800319 return 0;
320}
321
322static void *devinfo_start(struct seq_file *f, loff_t *pos)
323{
324 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
325 return pos;
326 return NULL;
327}
328
329static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
330{
331 (*pos)++;
332 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
333 return NULL;
334 return pos;
335}
336
337static void devinfo_stop(struct seq_file *f, void *v)
338{
339 /* Nothing to do */
340}
341
Jan Engelhardt03a44822008-02-08 04:21:19 -0800342static const struct seq_operations devinfo_ops = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800343 .start = devinfo_start,
344 .next = devinfo_next,
345 .stop = devinfo_stop,
346 .show = devinfo_show
347};
348
349static int devinfo_open(struct inode *inode, struct file *filp)
350{
351 return seq_open(filp, &devinfo_ops);
352}
353
Arjan van de Ven00977a52007-02-12 00:55:34 -0800354static const struct file_operations proc_devinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800355 .open = devinfo_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 .read = seq_read,
357 .llseek = seq_lseek,
358 .release = seq_release,
359};
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static int vmstat_open(struct inode *inode, struct file *file)
362{
363 return seq_open(file, &vmstat_op);
364}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800365static const struct file_operations proc_vmstat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 .open = vmstat_open,
367 .read = seq_read,
368 .llseek = seq_lseek,
369 .release = seq_release,
370};
371
372#ifdef CONFIG_PROC_HARDWARE
373static int hardware_read_proc(char *page, char **start, off_t off,
374 int count, int *eof, void *data)
375{
376 int len = get_hardware_list(page);
377 return proc_calc_metrics(page, start, off, count, eof, len);
378}
379#endif
380
381#ifdef CONFIG_STRAM_PROC
382static int stram_read_proc(char *page, char **start, off_t off,
383 int count, int *eof, void *data)
384{
385 int len = get_stram_list(page);
386 return proc_calc_metrics(page, start, off, count, eof, len);
387}
388#endif
389
David Howells93614012006-09-30 20:45:40 +0200390#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static int partitions_open(struct inode *inode, struct file *file)
392{
393 return seq_open(file, &partitions_op);
394}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800395static const struct file_operations proc_partitions_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 .open = partitions_open,
397 .read = seq_read,
398 .llseek = seq_lseek,
399 .release = seq_release,
400};
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static int diskstats_open(struct inode *inode, struct file *file)
403{
404 return seq_open(file, &diskstats_op);
405}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800406static const struct file_operations proc_diskstats_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 .open = diskstats_open,
408 .read = seq_read,
409 .llseek = seq_lseek,
410 .release = seq_release,
411};
David Howells93614012006-09-30 20:45:40 +0200412#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414#ifdef CONFIG_MODULES
Jan Engelhardt03a44822008-02-08 04:21:19 -0800415extern const struct seq_operations modules_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static int modules_open(struct inode *inode, struct file *file)
417{
418 return seq_open(file, &modules_op);
419}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800420static const struct file_operations proc_modules_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 .open = modules_open,
422 .read = seq_read,
423 .llseek = seq_lseek,
424 .release = seq_release,
425};
426#endif
427
Linus Torvalds158a9622008-01-02 13:04:48 -0800428#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static int slabinfo_open(struct inode *inode, struct file *file)
430{
431 return seq_open(file, &slabinfo_op);
432}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800433static const struct file_operations proc_slabinfo_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .open = slabinfo_open,
435 .read = seq_read,
436 .write = slabinfo_write,
437 .llseek = seq_lseek,
438 .release = seq_release,
439};
Al Viro871751e2006-03-25 03:06:39 -0800440
441#ifdef CONFIG_DEBUG_SLAB_LEAK
Jan Engelhardt03a44822008-02-08 04:21:19 -0800442extern const struct seq_operations slabstats_op;
Al Viro871751e2006-03-25 03:06:39 -0800443static int slabstats_open(struct inode *inode, struct file *file)
444{
445 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
446 int ret = -ENOMEM;
447 if (n) {
448 ret = seq_open(file, &slabstats_op);
449 if (!ret) {
450 struct seq_file *m = file->private_data;
451 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
452 m->private = n;
453 n = NULL;
454 }
455 kfree(n);
456 }
457 return ret;
458}
459
Arjan van de Ven00977a52007-02-12 00:55:34 -0800460static const struct file_operations proc_slabstats_operations = {
Al Viro871751e2006-03-25 03:06:39 -0800461 .open = slabstats_open,
462 .read = seq_read,
463 .llseek = seq_lseek,
Martin Peschke09f08922007-05-08 00:29:26 -0700464 .release = seq_release_private,
Al Viro871751e2006-03-25 03:06:39 -0800465};
466#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800467#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Christoph Lametera10aa572008-04-28 02:12:40 -0700469#ifdef CONFIG_MMU
470static int vmalloc_open(struct inode *inode, struct file *file)
471{
Eric Dumazeta47a1262008-07-23 21:27:38 -0700472 unsigned int *ptr = NULL;
473 int ret;
474
475 if (NUMA_BUILD)
476 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
477 ret = seq_open(file, &vmalloc_op);
478 if (!ret) {
479 struct seq_file *m = file->private_data;
480 m->private = ptr;
481 } else
482 kfree(ptr);
483 return ret;
Christoph Lametera10aa572008-04-28 02:12:40 -0700484}
485
486static const struct file_operations proc_vmalloc_operations = {
487 .open = vmalloc_open,
488 .read = seq_read,
489 .llseek = seq_lseek,
Eric Dumazeta47a1262008-07-23 21:27:38 -0700490 .release = seq_release_private,
Christoph Lametera10aa572008-04-28 02:12:40 -0700491};
492#endif
493
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200494#ifndef arch_irq_stat_cpu
495#define arch_irq_stat_cpu(cpu) 0
496#endif
497#ifndef arch_irq_stat
498#define arch_irq_stat() 0
499#endif
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static int show_stat(struct seq_file *p, void *v)
502{
503 int i;
504 unsigned long jif;
505 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200506 cputime64_t guest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 u64 sum = 0;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700508 struct timespec boottime;
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700509 unsigned int *per_irq_sum;
510
511 per_irq_sum = kzalloc(sizeof(unsigned int)*NR_IRQS, GFP_KERNEL);
512 if (!per_irq_sum)
513 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 user = nice = system = idle = iowait =
516 irq = softirq = steal = cputime64_zero;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200517 guest = cputime64_zero;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700518 getboottime(&boottime);
519 jif = boottime.tv_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800521 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int j;
523
524 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
525 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
526 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
527 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
528 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
529 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
530 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
531 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200532 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700533 for (j = 0; j < NR_IRQS; j++) {
534 unsigned int temp = kstat_cpu(i).irqs[j];
535 sum += temp;
536 per_irq_sum[j] += temp;
537 }
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200538 sum += arch_irq_stat_cpu(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200540 sum += arch_irq_stat();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200542 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 (unsigned long long)cputime64_to_clock_t(user),
544 (unsigned long long)cputime64_to_clock_t(nice),
545 (unsigned long long)cputime64_to_clock_t(system),
546 (unsigned long long)cputime64_to_clock_t(idle),
547 (unsigned long long)cputime64_to_clock_t(iowait),
548 (unsigned long long)cputime64_to_clock_t(irq),
549 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200550 (unsigned long long)cputime64_to_clock_t(steal),
551 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 for_each_online_cpu(i) {
553
554 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
555 user = kstat_cpu(i).cpustat.user;
556 nice = kstat_cpu(i).cpustat.nice;
557 system = kstat_cpu(i).cpustat.system;
558 idle = kstat_cpu(i).cpustat.idle;
559 iowait = kstat_cpu(i).cpustat.iowait;
560 irq = kstat_cpu(i).cpustat.irq;
561 softirq = kstat_cpu(i).cpustat.softirq;
562 steal = kstat_cpu(i).cpustat.steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200563 guest = kstat_cpu(i).cpustat.guest;
564 seq_printf(p,
565 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 i,
567 (unsigned long long)cputime64_to_clock_t(user),
568 (unsigned long long)cputime64_to_clock_t(nice),
569 (unsigned long long)cputime64_to_clock_t(system),
570 (unsigned long long)cputime64_to_clock_t(idle),
571 (unsigned long long)cputime64_to_clock_t(iowait),
572 (unsigned long long)cputime64_to_clock_t(irq),
573 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200574 (unsigned long long)cputime64_to_clock_t(steal),
575 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 seq_printf(p, "intr %llu", (unsigned long long)sum);
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 for (i = 0; i < NR_IRQS; i++)
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700580 seq_printf(p, " %u", per_irq_sum[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 seq_printf(p,
583 "\nctxt %llu\n"
584 "btime %lu\n"
585 "processes %lu\n"
586 "procs_running %lu\n"
587 "procs_blocked %lu\n",
588 nr_context_switches(),
589 (unsigned long)jif,
590 total_forks,
591 nr_running(),
592 nr_iowait());
593
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700594 kfree(per_irq_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 0;
596}
597
598static int stat_open(struct inode *inode, struct file *file)
599{
600 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
601 char *buf;
602 struct seq_file *m;
603 int res;
604
605 /* don't ask for more than the kmalloc() max size, currently 128 KB */
606 if (size > 128 * 1024)
607 size = 128 * 1024;
608 buf = kmalloc(size, GFP_KERNEL);
609 if (!buf)
610 return -ENOMEM;
611
612 res = single_open(file, show_stat, NULL);
613 if (!res) {
614 m = file->private_data;
615 m->buf = buf;
616 m->size = size;
617 } else
618 kfree(buf);
619 return res;
620}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800621static const struct file_operations proc_stat_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 .open = stat_open,
623 .read = seq_read,
624 .llseek = seq_lseek,
625 .release = single_release,
626};
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/*
629 * /proc/interrupts
630 */
631static void *int_seq_start(struct seq_file *f, loff_t *pos)
632{
633 return (*pos <= NR_IRQS) ? pos : NULL;
634}
635
636static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
637{
638 (*pos)++;
639 if (*pos > NR_IRQS)
640 return NULL;
641 return pos;
642}
643
644static void int_seq_stop(struct seq_file *f, void *v)
645{
646 /* Nothing to do */
647}
648
649
Jan Engelhardt03a44822008-02-08 04:21:19 -0800650static const struct seq_operations int_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 .start = int_seq_start,
652 .next = int_seq_next,
653 .stop = int_seq_stop,
654 .show = show_interrupts
655};
656
657static int interrupts_open(struct inode *inode, struct file *filp)
658{
659 return seq_open(filp, &int_seq_ops);
660}
661
Arjan van de Ven00977a52007-02-12 00:55:34 -0800662static const struct file_operations proc_interrupts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .open = interrupts_open,
664 .read = seq_read,
665 .llseek = seq_lseek,
666 .release = seq_release,
667};
668
669static int filesystems_read_proc(char *page, char **start, off_t off,
670 int count, int *eof, void *data)
671{
672 int len = get_filesystem_list(page);
673 return proc_calc_metrics(page, start, off, count, eof, len);
674}
675
676static int cmdline_read_proc(char *page, char **start, off_t off,
677 int count, int *eof, void *data)
678{
679 int len;
680
681 len = sprintf(page, "%s\n", saved_command_line);
682 return proc_calc_metrics(page, start, off, count, eof, len);
683}
684
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200685#ifdef CONFIG_FILE_LOCKING
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700686static int locks_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700688 return seq_open(filp, &locks_seq_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700691static const struct file_operations proc_locks_operations = {
692 .open = locks_open,
693 .read = seq_read,
694 .llseek = seq_lseek,
695 .release = seq_release,
696};
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200697#endif /* CONFIG_FILE_LOCKING */
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static int execdomains_read_proc(char *page, char **start, off_t off,
700 int count, int *eof, void *data)
701{
702 int len = get_exec_domain_list(page);
703 return proc_calc_metrics(page, start, off, count, eof, len);
704}
705
Matt Mackall1e883282008-02-04 22:29:07 -0800706#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -0800707#define KPMSIZE sizeof(u64)
708#define KPMMASK (KPMSIZE - 1)
709/* /proc/kpagecount - an array exposing page counts
710 *
711 * Each entry is a u64 representing the corresponding
712 * physical page count.
713 */
714static ssize_t kpagecount_read(struct file *file, char __user *buf,
715 size_t count, loff_t *ppos)
716{
717 u64 __user *out = (u64 __user *)buf;
718 struct page *ppage;
719 unsigned long src = *ppos;
720 unsigned long pfn;
721 ssize_t ret = 0;
722 u64 pcount;
723
724 pfn = src / KPMSIZE;
725 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
726 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700727 return -EINVAL;
Matt Mackall161f47b2008-02-04 22:29:05 -0800728
729 while (count > 0) {
Matt Mackall304daa82008-02-04 22:29:06 -0800730 ppage = NULL;
731 if (pfn_valid(pfn))
732 ppage = pfn_to_page(pfn);
733 pfn++;
Matt Mackall161f47b2008-02-04 22:29:05 -0800734 if (!ppage)
735 pcount = 0;
736 else
Thomas Tuttlebbcdac02008-06-05 22:46:58 -0700737 pcount = page_mapcount(ppage);
Matt Mackall161f47b2008-02-04 22:29:05 -0800738
739 if (put_user(pcount, out++)) {
740 ret = -EFAULT;
741 break;
742 }
743
744 count -= KPMSIZE;
745 }
746
747 *ppos += (char __user *)out - buf;
748 if (!ret)
749 ret = (char __user *)out - buf;
750 return ret;
751}
752
753static struct file_operations proc_kpagecount_operations = {
754 .llseek = mem_lseek,
755 .read = kpagecount_read,
756};
757
Matt Mackall304daa82008-02-04 22:29:06 -0800758/* /proc/kpageflags - an array exposing page flags
759 *
760 * Each entry is a u64 representing the corresponding
761 * physical page flags.
762 */
763
764/* These macros are used to decouple internal flags from exported ones */
765
766#define KPF_LOCKED 0
767#define KPF_ERROR 1
768#define KPF_REFERENCED 2
769#define KPF_UPTODATE 3
770#define KPF_DIRTY 4
771#define KPF_LRU 5
772#define KPF_ACTIVE 6
773#define KPF_SLAB 7
774#define KPF_WRITEBACK 8
775#define KPF_RECLAIM 9
776#define KPF_BUDDY 10
777
778#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
779
780static ssize_t kpageflags_read(struct file *file, char __user *buf,
781 size_t count, loff_t *ppos)
782{
783 u64 __user *out = (u64 __user *)buf;
784 struct page *ppage;
785 unsigned long src = *ppos;
786 unsigned long pfn;
787 ssize_t ret = 0;
788 u64 kflags, uflags;
789
790 pfn = src / KPMSIZE;
791 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
792 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700793 return -EINVAL;
Matt Mackall304daa82008-02-04 22:29:06 -0800794
795 while (count > 0) {
796 ppage = NULL;
797 if (pfn_valid(pfn))
798 ppage = pfn_to_page(pfn);
799 pfn++;
800 if (!ppage)
801 kflags = 0;
802 else
803 kflags = ppage->flags;
804
805 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
806 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
807 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
808 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
809 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
810 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
811 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
812 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
813 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
814 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
815 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
816
817 if (put_user(uflags, out++)) {
818 ret = -EFAULT;
819 break;
820 }
821
822 count -= KPMSIZE;
823 }
824
825 *ppos += (char __user *)out - buf;
826 if (!ret)
827 ret = (char __user *)out - buf;
828 return ret;
829}
830
831static struct file_operations proc_kpageflags_operations = {
832 .llseek = mem_lseek,
833 .read = kpageflags_read,
834};
Matt Mackall1e883282008-02-04 22:29:07 -0800835#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall304daa82008-02-04 22:29:06 -0800836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837struct proc_dir_entry *proc_root_kcore;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839void __init proc_misc_init(void)
840{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 static struct {
842 char *name;
843 int (*read_proc)(char*,char**,off_t,int,int*,void*);
844 } *p, simple_ones[] = {
845 {"loadavg", loadavg_read_proc},
846 {"uptime", uptime_read_proc},
847 {"meminfo", meminfo_read_proc},
848 {"version", version_read_proc},
849#ifdef CONFIG_PROC_HARDWARE
850 {"hardware", hardware_read_proc},
851#endif
852#ifdef CONFIG_STRAM_PROC
853 {"stram", stram_read_proc},
854#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 {"filesystems", filesystems_read_proc},
856 {"cmdline", cmdline_read_proc},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 {"execdomains", execdomains_read_proc},
858 {NULL,}
859 };
860 for (p = simple_ones; p->name; p++)
861 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
862
863 proc_symlink("mounts", NULL, "self/mounts");
864
865 /* And now for trickier ones */
Mike Galbraithc36264d2006-12-06 20:37:42 -0800866#ifdef CONFIG_PRINTK
Alexey Dobriyanc74c1202008-04-29 01:01:44 -0700867 proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
Mike Galbraithc36264d2006-12-06 20:37:42 -0800868#endif
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200869#ifdef CONFIG_FILE_LOCKING
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700870 proc_create("locks", 0, NULL, &proc_locks_operations);
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200871#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700872 proc_create("devices", 0, NULL, &proc_devinfo_operations);
873 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
David Howells93614012006-09-30 20:45:40 +0200874#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700875 proc_create("partitions", 0, NULL, &proc_partitions_operations);
David Howells93614012006-09-30 20:45:40 +0200876#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700877 proc_create("stat", 0, NULL, &proc_stat_operations);
878 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
Linus Torvalds158a9622008-01-02 13:04:48 -0800879#ifdef CONFIG_SLABINFO
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700880 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
Al Viro871751e2006-03-25 03:06:39 -0800881#ifdef CONFIG_DEBUG_SLAB_LEAK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700882 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
Al Viro871751e2006-03-25 03:06:39 -0800883#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800884#endif
Christoph Lametera10aa572008-04-28 02:12:40 -0700885#ifdef CONFIG_MMU
886 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
887#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700888 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
889 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
890 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
891 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
David Howells93614012006-09-30 20:45:40 +0200892#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700893 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#ifdef CONFIG_MODULES
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700896 proc_create("modules", 0, NULL, &proc_modules_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#endif
898#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700899 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900#endif
901#ifdef CONFIG_PROC_KCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700902 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
903 if (proc_root_kcore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 proc_root_kcore->size =
905 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#endif
Matt Mackall1e883282008-02-04 22:29:07 -0800907#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700908 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
909 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
Matt Mackall1e883282008-02-04 22:29:07 -0800910#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700911#ifdef CONFIG_PROC_VMCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700912 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700913#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}