blob: ff42206c8aed297b816a17f8c177688ea97f05c0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/proc_misc.c
3 *
4 * linux/fs/proc/array.c
5 * Copyright (C) 1992 by Linus Torvalds
6 * based on ideas by Darren Senn
7 *
8 * This used to be the part of array.c. See the rest of history and credits
9 * there. I took this into a separate file and switched the thing to generic
10 * proc_file_inode_operations, leaving in array.c only per-process stuff.
11 * Inumbers allocation made dynamic (via create_proc_entry()). AV, May 1999.
12 *
13 * Changes:
14 * Fulton Green : Encapsulated position metric calculations.
15 * <kernel@FultonGreen.com>
16 */
17
18#include <linux/types.h>
19#include <linux/errno.h>
20#include <linux/time.h>
21#include <linux/kernel.h>
22#include <linux/kernel_stat.h>
Neil Horman7170be52006-01-14 13:20:38 -080023#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/tty.h>
25#include <linux/string.h>
26#include <linux/mman.h>
KOSAKI Motohiro4b856152008-09-02 14:35:53 -070027#include <linux/quicklist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/proc_fs.h>
29#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mm.h>
31#include <linux/mmzone.h>
32#include <linux/pagemap.h>
Yinghai Luc7fb03a2008-08-19 20:50:12 -070033#include <linux/irq.h>
Adrian Bunkf74596d2008-02-06 01:36:35 -080034#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/swap.h>
36#include <linux/slab.h>
Adrian Bunka0db7012008-03-04 11:23:50 +010037#include <linux/genhd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/smp.h>
39#include <linux/signal.h>
40#include <linux/module.h>
41#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/seq_file.h>
43#include <linux/times.h>
44#include <linux/profile.h>
Andrew Morton7bf65382006-12-08 02:41:14 -080045#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/blkdev.h>
47#include <linux/hugetlb.h>
48#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/vmalloc.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070050#include <linux/crash_dump.h>
Sukadev Bhattiprolu61a58c62006-12-08 02:37:58 -080051#include <linux/pid_namespace.h>
Matt Mackall161f47b2008-02-04 22:29:05 -080052#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/uaccess.h>
54#include <asm/pgtable.h>
55#include <asm/io.h>
56#include <asm/tlb.h>
57#include <asm/div64.h>
58#include "internal.h"
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * Warning: stuff below (imported functions) assumes that its output will fit
62 * into one page. For some of those functions it may be wrong. Moreover, we
63 * have a way to deal with that gracefully. Right now I used straightforward
64 * wrappers, but this needs further analysis wrt potential overflows.
65 */
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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70static int proc_calc_metrics(char *page, char **start, off_t off,
71 int count, int *eof, int len)
72{
73 if (len <= off+count) *eof = 1;
74 *start = page + off;
75 len -= off;
76 if (len>count) len = count;
77 if (len<0) len = 0;
78 return len;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int uptime_read_proc(char *page, char **start, off_t off,
82 int count, int *eof, void *data)
83{
84 struct timespec uptime;
85 struct timespec idle;
86 int len;
87 cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
88
89 do_posix_clock_monotonic_gettime(&uptime);
Tomas Janousekd6214142007-07-15 23:39:42 -070090 monotonic_to_bootbased(&uptime);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 cputime_to_timespec(idletime, &idle);
92 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
93 (unsigned long) uptime.tv_sec,
94 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
95 (unsigned long) idle.tv_sec,
96 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
97
98 return proc_calc_metrics(page, start, off, count, eof, len);
99}
100
Andi Kleence0c0e52008-05-02 11:46:49 +0200101int __attribute__((weak)) arch_report_meminfo(char *page)
102{
103 return 0;
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int meminfo_read_proc(char *page, char **start, off_t off,
107 int count, int *eof, void *data)
108{
109 struct sysinfo i;
110 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long committed;
112 unsigned long allowed;
113 struct vmalloc_info vmi;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700114 long cached;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700115 unsigned long pages[NR_LRU_LISTS];
116 int lru;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * display in kilobytes.
120 */
121#define K(x) ((x) << (PAGE_SHIFT - 10))
122 si_meminfo(&i);
123 si_swapinfo(&i);
Alan Cox80119ef2008-05-23 13:04:31 -0700124 committed = atomic_long_read(&vm_committed_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 allowed = ((totalram_pages - hugetlb_total_pages())
126 * sysctl_overcommit_ratio / 100) + total_swap_pages;
127
Christoph Lameter347ce432006-06-30 01:55:35 -0700128 cached = global_page_state(NR_FILE_PAGES) -
129 total_swapcache_pages - i.bufferram;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700130 if (cached < 0)
131 cached = 0;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 get_vmalloc_info(&vmi);
134
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700135 for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
136 pages[lru] = global_page_state(NR_LRU_BASE + lru);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
139 * Tagged format, for easy grepping and expansion.
140 */
141 len = sprintf(page,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700142 "MemTotal: %8lu kB\n"
143 "MemFree: %8lu kB\n"
144 "Buffers: %8lu kB\n"
145 "Cached: %8lu kB\n"
146 "SwapCached: %8lu kB\n"
147 "Active: %8lu kB\n"
148 "Inactive: %8lu kB\n"
149 "Active(anon): %8lu kB\n"
150 "Inactive(anon): %8lu kB\n"
151 "Active(file): %8lu kB\n"
152 "Inactive(file): %8lu kB\n"
Lee Schermerhorn7b854122008-10-18 20:26:40 -0700153#ifdef CONFIG_UNEVICTABLE_LRU
154 "Unevictable: %8lu kB\n"
Nick Piggin5344b7e2008-10-18 20:26:51 -0700155 "Mlocked: %8lu kB\n"
Lee Schermerhorn7b854122008-10-18 20:26:40 -0700156#endif
Christoph Lameter182e8e22006-09-25 23:31:10 -0700157#ifdef CONFIG_HIGHMEM
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700158 "HighTotal: %8lu kB\n"
159 "HighFree: %8lu kB\n"
160 "LowTotal: %8lu kB\n"
161 "LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700162#endif
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700163 "SwapTotal: %8lu kB\n"
164 "SwapFree: %8lu kB\n"
165 "Dirty: %8lu kB\n"
166 "Writeback: %8lu kB\n"
167 "AnonPages: %8lu kB\n"
168 "Mapped: %8lu kB\n"
169 "Slab: %8lu kB\n"
170 "SReclaimable: %8lu kB\n"
171 "SUnreclaim: %8lu kB\n"
172 "PageTables: %8lu kB\n"
Hugh Dickinsd7a3e492008-09-13 02:33:13 -0700173#ifdef CONFIG_QUICKLIST
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700174 "Quicklists: %8lu kB\n"
Hugh Dickinsd7a3e492008-09-13 02:33:13 -0700175#endif
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700176 "NFS_Unstable: %8lu kB\n"
177 "Bounce: %8lu kB\n"
178 "WritebackTmp: %8lu kB\n"
179 "CommitLimit: %8lu kB\n"
180 "Committed_AS: %8lu kB\n"
181 "VmallocTotal: %8lu kB\n"
182 "VmallocUsed: %8lu kB\n"
183 "VmallocChunk: %8lu kB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 K(i.totalram),
185 K(i.freeram),
186 K(i.bufferram),
Martin Hicks4c4c4022005-04-16 15:24:08 -0700187 K(cached),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 K(total_swapcache_pages),
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700189 K(pages[LRU_ACTIVE_ANON] + pages[LRU_ACTIVE_FILE]),
190 K(pages[LRU_INACTIVE_ANON] + pages[LRU_INACTIVE_FILE]),
191 K(pages[LRU_ACTIVE_ANON]),
192 K(pages[LRU_INACTIVE_ANON]),
193 K(pages[LRU_ACTIVE_FILE]),
194 K(pages[LRU_INACTIVE_FILE]),
Lee Schermerhorn7b854122008-10-18 20:26:40 -0700195#ifdef CONFIG_UNEVICTABLE_LRU
196 K(pages[LRU_UNEVICTABLE]),
Nick Piggin5344b7e2008-10-18 20:26:51 -0700197 K(global_page_state(NR_MLOCK)),
Lee Schermerhorn7b854122008-10-18 20:26:40 -0700198#endif
Christoph Lameter182e8e22006-09-25 23:31:10 -0700199#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 K(i.totalhigh),
201 K(i.freehigh),
202 K(i.totalram-i.totalhigh),
203 K(i.freeram-i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 K(i.totalswap),
206 K(i.freeswap),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700207 K(global_page_state(NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -0700208 K(global_page_state(NR_WRITEBACK)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700209 K(global_page_state(NR_ANON_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700210 K(global_page_state(NR_FILE_MAPPED)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700211 K(global_page_state(NR_SLAB_RECLAIMABLE) +
212 global_page_state(NR_SLAB_UNRECLAIMABLE)),
213 K(global_page_state(NR_SLAB_RECLAIMABLE)),
214 K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
Christoph Lameterdf849a12006-06-30 01:55:38 -0700215 K(global_page_state(NR_PAGETABLE)),
Hugh Dickinsd7a3e492008-09-13 02:33:13 -0700216#ifdef CONFIG_QUICKLIST
217 K(quicklist_total_size()),
218#endif
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700219 K(global_page_state(NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700220 K(global_page_state(NR_BOUNCE)),
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700221 K(global_page_state(NR_WRITEBACK_TEMP)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 K(allowed),
223 K(committed),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 (unsigned long)VMALLOC_TOTAL >> 10,
225 vmi.used >> 10,
Hugh Dickinsd7a3e492008-09-13 02:33:13 -0700226 vmi.largest_chunk >> 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 );
228
229 len += hugetlb_report_meminfo(page + len);
230
Andi Kleence0c0e52008-05-02 11:46:49 +0200231 len += arch_report_meminfo(page + len);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return proc_calc_metrics(page, start, off, count, eof, len);
234#undef K
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static int fragmentation_open(struct inode *inode, struct file *file)
238{
239 (void)inode;
240 return seq_open(file, &fragmentation_op);
241}
242
Arjan van de Ven00977a52007-02-12 00:55:34 -0800243static const struct file_operations fragmentation_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .open = fragmentation_open,
245 .read = seq_read,
246 .llseek = seq_lseek,
247 .release = seq_release,
248};
249
Mel Gorman467c9962007-10-16 01:26:02 -0700250static int pagetypeinfo_open(struct inode *inode, struct file *file)
251{
252 return seq_open(file, &pagetypeinfo_op);
253}
254
255static const struct file_operations pagetypeinfo_file_ops = {
256 .open = pagetypeinfo_open,
257 .read = seq_read,
258 .llseek = seq_lseek,
259 .release = seq_release,
260};
261
Nikita Danilov295ab932005-06-21 17:14:38 -0700262static int zoneinfo_open(struct inode *inode, struct file *file)
263{
264 return seq_open(file, &zoneinfo_op);
265}
266
Arjan van de Ven00977a52007-02-12 00:55:34 -0800267static const struct file_operations proc_zoneinfo_file_operations = {
Nikita Danilov295ab932005-06-21 17:14:38 -0700268 .open = zoneinfo_open,
269 .read = seq_read,
270 .llseek = seq_lseek,
271 .release = seq_release,
272};
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static int version_read_proc(char *page, char **start, off_t off,
275 int count, int *eof, void *data)
276{
277 int len;
278
Roman Zippel3eb3c742007-01-10 14:45:28 +0100279 len = snprintf(page, PAGE_SIZE, linux_proc_banner,
Linus Torvalds89937802006-12-11 09:28:46 -0800280 utsname()->sysname,
281 utsname()->release,
282 utsname()->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return proc_calc_metrics(page, start, off, count, eof, len);
284}
285
Jan Engelhardt03a44822008-02-08 04:21:19 -0800286extern const struct seq_operations cpuinfo_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static int cpuinfo_open(struct inode *inode, struct file *file)
288{
289 return seq_open(file, &cpuinfo_op);
290}
Neil Horman7170be52006-01-14 13:20:38 -0800291
Arjan van de Ven00977a52007-02-12 00:55:34 -0800292static const struct file_operations proc_cpuinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800293 .open = cpuinfo_open,
Neil Horman7170be52006-01-14 13:20:38 -0800294 .read = seq_read,
295 .llseek = seq_lseek,
296 .release = seq_release,
297};
298
Joe Korty68eef3b2006-03-31 02:30:32 -0800299static int devinfo_show(struct seq_file *f, void *v)
300{
301 int i = *(loff_t *) v;
302
303 if (i < CHRDEV_MAJOR_HASH_SIZE) {
304 if (i == 0)
305 seq_printf(f, "Character devices:\n");
306 chrdev_show(f, i);
David Howells93614012006-09-30 20:45:40 +0200307 }
308#ifdef CONFIG_BLOCK
309 else {
Joe Korty68eef3b2006-03-31 02:30:32 -0800310 i -= CHRDEV_MAJOR_HASH_SIZE;
311 if (i == 0)
312 seq_printf(f, "\nBlock devices:\n");
313 blkdev_show(f, i);
314 }
David Howells93614012006-09-30 20:45:40 +0200315#endif
Joe Korty68eef3b2006-03-31 02:30:32 -0800316 return 0;
317}
318
319static void *devinfo_start(struct seq_file *f, loff_t *pos)
320{
321 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
322 return pos;
323 return NULL;
324}
325
326static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
327{
328 (*pos)++;
329 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
330 return NULL;
331 return pos;
332}
333
334static void devinfo_stop(struct seq_file *f, void *v)
335{
336 /* Nothing to do */
337}
338
Jan Engelhardt03a44822008-02-08 04:21:19 -0800339static const struct seq_operations devinfo_ops = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800340 .start = devinfo_start,
341 .next = devinfo_next,
342 .stop = devinfo_stop,
343 .show = devinfo_show
344};
345
346static int devinfo_open(struct inode *inode, struct file *filp)
347{
348 return seq_open(filp, &devinfo_ops);
349}
350
Arjan van de Ven00977a52007-02-12 00:55:34 -0800351static const struct file_operations proc_devinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800352 .open = devinfo_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 .read = seq_read,
354 .llseek = seq_lseek,
355 .release = seq_release,
356};
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static int vmstat_open(struct inode *inode, struct file *file)
359{
360 return seq_open(file, &vmstat_op);
361}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800362static const struct file_operations proc_vmstat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 .open = vmstat_open,
364 .read = seq_read,
365 .llseek = seq_lseek,
366 .release = seq_release,
367};
368
369#ifdef CONFIG_PROC_HARDWARE
370static int hardware_read_proc(char *page, char **start, off_t off,
371 int count, int *eof, void *data)
372{
373 int len = get_hardware_list(page);
374 return proc_calc_metrics(page, start, off, count, eof, len);
375}
376#endif
377
378#ifdef CONFIG_STRAM_PROC
379static int stram_read_proc(char *page, char **start, off_t off,
380 int count, int *eof, void *data)
381{
382 int len = get_stram_list(page);
383 return proc_calc_metrics(page, start, off, count, eof, len);
384}
385#endif
386
David Howells93614012006-09-30 20:45:40 +0200387#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static int partitions_open(struct inode *inode, struct file *file)
389{
390 return seq_open(file, &partitions_op);
391}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800392static const struct file_operations proc_partitions_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 .open = partitions_open,
394 .read = seq_read,
395 .llseek = seq_lseek,
396 .release = seq_release,
397};
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399static int diskstats_open(struct inode *inode, struct file *file)
400{
401 return seq_open(file, &diskstats_op);
402}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800403static const struct file_operations proc_diskstats_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .open = diskstats_open,
405 .read = seq_read,
406 .llseek = seq_lseek,
407 .release = seq_release,
408};
David Howells93614012006-09-30 20:45:40 +0200409#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411#ifdef CONFIG_MODULES
Jan Engelhardt03a44822008-02-08 04:21:19 -0800412extern const struct seq_operations modules_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static int modules_open(struct inode *inode, struct file *file)
414{
415 return seq_open(file, &modules_op);
416}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800417static const struct file_operations proc_modules_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 .open = modules_open,
419 .read = seq_read,
420 .llseek = seq_lseek,
421 .release = seq_release,
422};
423#endif
424
Linus Torvalds158a9622008-01-02 13:04:48 -0800425#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static int slabinfo_open(struct inode *inode, struct file *file)
427{
428 return seq_open(file, &slabinfo_op);
429}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800430static const struct file_operations proc_slabinfo_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 .open = slabinfo_open,
432 .read = seq_read,
433 .write = slabinfo_write,
434 .llseek = seq_lseek,
435 .release = seq_release,
436};
Al Viro871751e2006-03-25 03:06:39 -0800437
438#ifdef CONFIG_DEBUG_SLAB_LEAK
Jan Engelhardt03a44822008-02-08 04:21:19 -0800439extern const struct seq_operations slabstats_op;
Al Viro871751e2006-03-25 03:06:39 -0800440static int slabstats_open(struct inode *inode, struct file *file)
441{
442 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
443 int ret = -ENOMEM;
444 if (n) {
445 ret = seq_open(file, &slabstats_op);
446 if (!ret) {
447 struct seq_file *m = file->private_data;
448 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
449 m->private = n;
450 n = NULL;
451 }
452 kfree(n);
453 }
454 return ret;
455}
456
Arjan van de Ven00977a52007-02-12 00:55:34 -0800457static const struct file_operations proc_slabstats_operations = {
Al Viro871751e2006-03-25 03:06:39 -0800458 .open = slabstats_open,
459 .read = seq_read,
460 .llseek = seq_lseek,
Martin Peschke09f08922007-05-08 00:29:26 -0700461 .release = seq_release_private,
Al Viro871751e2006-03-25 03:06:39 -0800462};
463#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800464#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Christoph Lametera10aa572008-04-28 02:12:40 -0700466#ifdef CONFIG_MMU
467static int vmalloc_open(struct inode *inode, struct file *file)
468{
Eric Dumazeta47a1262008-07-23 21:27:38 -0700469 unsigned int *ptr = NULL;
470 int ret;
471
472 if (NUMA_BUILD)
473 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
474 ret = seq_open(file, &vmalloc_op);
475 if (!ret) {
476 struct seq_file *m = file->private_data;
477 m->private = ptr;
478 } else
479 kfree(ptr);
480 return ret;
Christoph Lametera10aa572008-04-28 02:12:40 -0700481}
482
483static const struct file_operations proc_vmalloc_operations = {
484 .open = vmalloc_open,
485 .read = seq_read,
486 .llseek = seq_lseek,
Eric Dumazeta47a1262008-07-23 21:27:38 -0700487 .release = seq_release_private,
Christoph Lametera10aa572008-04-28 02:12:40 -0700488};
489#endif
490
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200491#ifndef arch_irq_stat_cpu
492#define arch_irq_stat_cpu(cpu) 0
493#endif
494#ifndef arch_irq_stat
495#define arch_irq_stat() 0
496#endif
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static int show_stat(struct seq_file *p, void *v)
499{
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700500 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned long jif;
502 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200503 cputime64_t guest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 u64 sum = 0;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700505 struct timespec boottime;
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700506 unsigned int per_irq_sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 user = nice = system = idle = iowait =
509 irq = softirq = steal = cputime64_zero;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200510 guest = cputime64_zero;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700511 getboottime(&boottime);
512 jif = boottime.tv_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800514 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
516 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
517 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
518 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
519 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
520 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
521 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
522 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200523 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700524
Thomas Gleixner2be3b522008-10-16 14:50:27 +0200525 for_each_irq_nr(j)
Thomas Gleixner2cc21ef2008-10-15 14:16:55 +0200526 sum += kstat_irqs_cpu(j, i);
527
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200528 sum += arch_irq_stat_cpu(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200530 sum += arch_irq_stat();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200532 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 (unsigned long long)cputime64_to_clock_t(user),
534 (unsigned long long)cputime64_to_clock_t(nice),
535 (unsigned long long)cputime64_to_clock_t(system),
536 (unsigned long long)cputime64_to_clock_t(idle),
537 (unsigned long long)cputime64_to_clock_t(iowait),
538 (unsigned long long)cputime64_to_clock_t(irq),
539 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200540 (unsigned long long)cputime64_to_clock_t(steal),
541 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 for_each_online_cpu(i) {
543
544 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
545 user = kstat_cpu(i).cpustat.user;
546 nice = kstat_cpu(i).cpustat.nice;
547 system = kstat_cpu(i).cpustat.system;
548 idle = kstat_cpu(i).cpustat.idle;
549 iowait = kstat_cpu(i).cpustat.iowait;
550 irq = kstat_cpu(i).cpustat.irq;
551 softirq = kstat_cpu(i).cpustat.softirq;
552 steal = kstat_cpu(i).cpustat.steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200553 guest = kstat_cpu(i).cpustat.guest;
554 seq_printf(p,
555 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 i,
557 (unsigned long long)cputime64_to_clock_t(user),
558 (unsigned long long)cputime64_to_clock_t(nice),
559 (unsigned long long)cputime64_to_clock_t(system),
560 (unsigned long long)cputime64_to_clock_t(idle),
561 (unsigned long long)cputime64_to_clock_t(iowait),
562 (unsigned long long)cputime64_to_clock_t(irq),
563 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200564 (unsigned long long)cputime64_to_clock_t(steal),
565 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 seq_printf(p, "intr %llu", (unsigned long long)sum);
568
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700569 /* sum again ? it could be updated? */
Thomas Gleixner2be3b522008-10-16 14:50:27 +0200570 for_each_irq_nr(j) {
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700571 per_irq_sum = 0;
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700572
Thomas Gleixner2cc21ef2008-10-15 14:16:55 +0200573 for_each_possible_cpu(i)
574 per_irq_sum += kstat_irqs_cpu(j, i);
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700575
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700576 seq_printf(p, " %u", per_irq_sum);
Yinghai Luc7fb03a2008-08-19 20:50:12 -0700577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 seq_printf(p,
580 "\nctxt %llu\n"
581 "btime %lu\n"
582 "processes %lu\n"
583 "procs_running %lu\n"
584 "procs_blocked %lu\n",
585 nr_context_switches(),
586 (unsigned long)jif,
587 total_forks,
588 nr_running(),
589 nr_iowait());
590
591 return 0;
592}
593
594static int stat_open(struct inode *inode, struct file *file)
595{
596 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
597 char *buf;
598 struct seq_file *m;
599 int res;
600
601 /* don't ask for more than the kmalloc() max size, currently 128 KB */
602 if (size > 128 * 1024)
603 size = 128 * 1024;
604 buf = kmalloc(size, GFP_KERNEL);
605 if (!buf)
606 return -ENOMEM;
607
608 res = single_open(file, show_stat, NULL);
609 if (!res) {
610 m = file->private_data;
611 m->buf = buf;
612 m->size = size;
613 } else
614 kfree(buf);
615 return res;
616}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800617static const struct file_operations proc_stat_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 .open = stat_open,
619 .read = seq_read,
620 .llseek = seq_lseek,
621 .release = single_release,
622};
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/*
625 * /proc/interrupts
626 */
627static void *int_seq_start(struct seq_file *f, loff_t *pos)
628{
Yinghai Luda27c112008-08-19 20:49:56 -0700629 return (*pos <= nr_irqs) ? pos : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Yinghai Lu52b17322008-08-19 20:50:20 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
634{
635 (*pos)++;
Yinghai Lu52b17322008-08-19 20:50:20 -0700636 return (*pos <= nr_irqs) ? pos : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639static void int_seq_stop(struct seq_file *f, void *v)
640{
641 /* Nothing to do */
642}
643
Jan Engelhardt03a44822008-02-08 04:21:19 -0800644static const struct seq_operations int_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 .start = int_seq_start,
646 .next = int_seq_next,
647 .stop = int_seq_stop,
648 .show = show_interrupts
649};
650
651static int interrupts_open(struct inode *inode, struct file *filp)
652{
653 return seq_open(filp, &int_seq_ops);
654}
655
Arjan van de Ven00977a52007-02-12 00:55:34 -0800656static const struct file_operations proc_interrupts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 .open = interrupts_open,
658 .read = seq_read,
659 .llseek = seq_lseek,
660 .release = seq_release,
661};
662
663static int filesystems_read_proc(char *page, char **start, off_t off,
664 int count, int *eof, void *data)
665{
666 int len = get_filesystem_list(page);
667 return proc_calc_metrics(page, start, off, count, eof, len);
668}
669
670static int cmdline_read_proc(char *page, char **start, off_t off,
671 int count, int *eof, void *data)
672{
673 int len;
674
675 len = sprintf(page, "%s\n", saved_command_line);
676 return proc_calc_metrics(page, start, off, count, eof, len);
677}
678
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200679#ifdef CONFIG_FILE_LOCKING
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700680static int locks_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700682 return seq_open(filp, &locks_seq_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700685static const struct file_operations proc_locks_operations = {
686 .open = locks_open,
687 .read = seq_read,
688 .llseek = seq_lseek,
689 .release = seq_release,
690};
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200691#endif /* CONFIG_FILE_LOCKING */
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static int execdomains_read_proc(char *page, char **start, off_t off,
694 int count, int *eof, void *data)
695{
696 int len = get_exec_domain_list(page);
697 return proc_calc_metrics(page, start, off, count, eof, len);
698}
699
Matt Mackall1e883282008-02-04 22:29:07 -0800700#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -0800701#define KPMSIZE sizeof(u64)
702#define KPMMASK (KPMSIZE - 1)
703/* /proc/kpagecount - an array exposing page counts
704 *
705 * Each entry is a u64 representing the corresponding
706 * physical page count.
707 */
708static ssize_t kpagecount_read(struct file *file, char __user *buf,
709 size_t count, loff_t *ppos)
710{
711 u64 __user *out = (u64 __user *)buf;
712 struct page *ppage;
713 unsigned long src = *ppos;
714 unsigned long pfn;
715 ssize_t ret = 0;
716 u64 pcount;
717
718 pfn = src / KPMSIZE;
719 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
720 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700721 return -EINVAL;
Matt Mackall161f47b2008-02-04 22:29:05 -0800722
723 while (count > 0) {
Matt Mackall304daa82008-02-04 22:29:06 -0800724 ppage = NULL;
725 if (pfn_valid(pfn))
726 ppage = pfn_to_page(pfn);
727 pfn++;
Matt Mackall161f47b2008-02-04 22:29:05 -0800728 if (!ppage)
729 pcount = 0;
730 else
Thomas Tuttlebbcdac02008-06-05 22:46:58 -0700731 pcount = page_mapcount(ppage);
Matt Mackall161f47b2008-02-04 22:29:05 -0800732
733 if (put_user(pcount, out++)) {
734 ret = -EFAULT;
735 break;
736 }
737
738 count -= KPMSIZE;
739 }
740
741 *ppos += (char __user *)out - buf;
742 if (!ret)
743 ret = (char __user *)out - buf;
744 return ret;
745}
746
747static struct file_operations proc_kpagecount_operations = {
748 .llseek = mem_lseek,
749 .read = kpagecount_read,
750};
751
Matt Mackall304daa82008-02-04 22:29:06 -0800752/* /proc/kpageflags - an array exposing page flags
753 *
754 * Each entry is a u64 representing the corresponding
755 * physical page flags.
756 */
757
758/* These macros are used to decouple internal flags from exported ones */
759
760#define KPF_LOCKED 0
761#define KPF_ERROR 1
762#define KPF_REFERENCED 2
763#define KPF_UPTODATE 3
764#define KPF_DIRTY 4
765#define KPF_LRU 5
766#define KPF_ACTIVE 6
767#define KPF_SLAB 7
768#define KPF_WRITEBACK 8
769#define KPF_RECLAIM 9
770#define KPF_BUDDY 10
771
772#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
773
774static ssize_t kpageflags_read(struct file *file, char __user *buf,
775 size_t count, loff_t *ppos)
776{
777 u64 __user *out = (u64 __user *)buf;
778 struct page *ppage;
779 unsigned long src = *ppos;
780 unsigned long pfn;
781 ssize_t ret = 0;
782 u64 kflags, uflags;
783
784 pfn = src / KPMSIZE;
785 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
786 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700787 return -EINVAL;
Matt Mackall304daa82008-02-04 22:29:06 -0800788
789 while (count > 0) {
790 ppage = NULL;
791 if (pfn_valid(pfn))
792 ppage = pfn_to_page(pfn);
793 pfn++;
794 if (!ppage)
795 kflags = 0;
796 else
797 kflags = ppage->flags;
798
799 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
800 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
801 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
802 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
803 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
804 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
805 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
806 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
807 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
808 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
809 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
810
811 if (put_user(uflags, out++)) {
812 ret = -EFAULT;
813 break;
814 }
815
816 count -= KPMSIZE;
817 }
818
819 *ppos += (char __user *)out - buf;
820 if (!ret)
821 ret = (char __user *)out - buf;
822 return ret;
823}
824
825static struct file_operations proc_kpageflags_operations = {
826 .llseek = mem_lseek,
827 .read = kpageflags_read,
828};
Matt Mackall1e883282008-02-04 22:29:07 -0800829#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall304daa82008-02-04 22:29:06 -0800830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831struct proc_dir_entry *proc_root_kcore;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833void __init proc_misc_init(void)
834{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 static struct {
836 char *name;
837 int (*read_proc)(char*,char**,off_t,int,int*,void*);
838 } *p, simple_ones[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 {"uptime", uptime_read_proc},
840 {"meminfo", meminfo_read_proc},
841 {"version", version_read_proc},
842#ifdef CONFIG_PROC_HARDWARE
843 {"hardware", hardware_read_proc},
844#endif
845#ifdef CONFIG_STRAM_PROC
846 {"stram", stram_read_proc},
847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 {"filesystems", filesystems_read_proc},
849 {"cmdline", cmdline_read_proc},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 {"execdomains", execdomains_read_proc},
851 {NULL,}
852 };
853 for (p = simple_ones; p->name; p++)
854 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
855
856 proc_symlink("mounts", NULL, "self/mounts");
857
858 /* And now for trickier ones */
Mike Galbraithc36264d2006-12-06 20:37:42 -0800859#ifdef CONFIG_PRINTK
Alexey Dobriyanc74c1202008-04-29 01:01:44 -0700860 proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
Mike Galbraithc36264d2006-12-06 20:37:42 -0800861#endif
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200862#ifdef CONFIG_FILE_LOCKING
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700863 proc_create("locks", 0, NULL, &proc_locks_operations);
Thomas Petazzonibfcd17a2008-08-06 15:12:22 +0200864#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700865 proc_create("devices", 0, NULL, &proc_devinfo_operations);
866 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
David Howells93614012006-09-30 20:45:40 +0200867#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700868 proc_create("partitions", 0, NULL, &proc_partitions_operations);
David Howells93614012006-09-30 20:45:40 +0200869#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700870 proc_create("stat", 0, NULL, &proc_stat_operations);
871 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
Linus Torvalds158a9622008-01-02 13:04:48 -0800872#ifdef CONFIG_SLABINFO
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700873 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
Al Viro871751e2006-03-25 03:06:39 -0800874#ifdef CONFIG_DEBUG_SLAB_LEAK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700875 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
Al Viro871751e2006-03-25 03:06:39 -0800876#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800877#endif
Christoph Lametera10aa572008-04-28 02:12:40 -0700878#ifdef CONFIG_MMU
879 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
880#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700881 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
882 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
883 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
884 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
David Howells93614012006-09-30 20:45:40 +0200885#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700886 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200887#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888#ifdef CONFIG_MODULES
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700889 proc_create("modules", 0, NULL, &proc_modules_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890#endif
891#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700892 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#endif
894#ifdef CONFIG_PROC_KCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700895 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
896 if (proc_root_kcore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 proc_root_kcore->size =
898 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif
Matt Mackall1e883282008-02-04 22:29:07 -0800900#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700901 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
902 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
Matt Mackall1e883282008-02-04 22:29:07 -0800903#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700904#ifdef CONFIG_PROC_VMCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700905 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700906#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}