blob: b14f43d25e9eeb5705d9dbc484c839f855481132 [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
Andi Kleence0c0e52008-05-02 11:46:49 +0200126int __attribute__((weak)) arch_report_meminfo(char *page)
127{
128 return 0;
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static int meminfo_read_proc(char *page, char **start, off_t off,
132 int count, int *eof, void *data)
133{
134 struct sysinfo i;
135 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long committed;
137 unsigned long allowed;
138 struct vmalloc_info vmi;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700139 long cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
142 * display in kilobytes.
143 */
144#define K(x) ((x) << (PAGE_SHIFT - 10))
145 si_meminfo(&i);
146 si_swapinfo(&i);
Alan Cox80119ef2008-05-23 13:04:31 -0700147 committed = atomic_long_read(&vm_committed_space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 allowed = ((totalram_pages - hugetlb_total_pages())
149 * sysctl_overcommit_ratio / 100) + total_swap_pages;
150
Christoph Lameter347ce432006-06-30 01:55:35 -0700151 cached = global_page_state(NR_FILE_PAGES) -
152 total_swapcache_pages - i.bufferram;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700153 if (cached < 0)
154 cached = 0;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 get_vmalloc_info(&vmi);
157
158 /*
159 * Tagged format, for easy grepping and expansion.
160 */
161 len = sprintf(page,
162 "MemTotal: %8lu kB\n"
163 "MemFree: %8lu kB\n"
164 "Buffers: %8lu kB\n"
165 "Cached: %8lu kB\n"
166 "SwapCached: %8lu kB\n"
167 "Active: %8lu kB\n"
168 "Inactive: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700169#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 "HighTotal: %8lu kB\n"
171 "HighFree: %8lu kB\n"
172 "LowTotal: %8lu kB\n"
173 "LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 "SwapTotal: %8lu kB\n"
176 "SwapFree: %8lu kB\n"
177 "Dirty: %8lu kB\n"
178 "Writeback: %8lu kB\n"
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700179 "AnonPages: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 "Mapped: %8lu kB\n"
181 "Slab: %8lu kB\n"
Christoph Lameter972d1a72006-09-25 23:31:51 -0700182 "SReclaimable: %8lu kB\n"
183 "SUnreclaim: %8lu kB\n"
Christoph Lameterdf849a12006-06-30 01:55:38 -0700184 "PageTables: %8lu kB\n"
Andrew Mortonf5ef68d2006-08-27 01:23:58 -0700185 "NFS_Unstable: %8lu kB\n"
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700186 "Bounce: %8lu kB\n"
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700187 "WritebackTmp: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 "CommitLimit: %8lu kB\n"
189 "Committed_AS: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 "VmallocTotal: %8lu kB\n"
191 "VmallocUsed: %8lu kB\n"
192 "VmallocChunk: %8lu kB\n",
193 K(i.totalram),
194 K(i.freeram),
195 K(i.bufferram),
Martin Hicks4c4c4022005-04-16 15:24:08 -0700196 K(cached),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 K(total_swapcache_pages),
Christoph Lameter65e458d42007-02-10 01:43:05 -0800198 K(global_page_state(NR_ACTIVE)),
199 K(global_page_state(NR_INACTIVE)),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700200#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 K(i.totalhigh),
202 K(i.freehigh),
203 K(i.totalram-i.totalhigh),
204 K(i.freeram-i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 K(i.totalswap),
207 K(i.freeswap),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700208 K(global_page_state(NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -0700209 K(global_page_state(NR_WRITEBACK)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700210 K(global_page_state(NR_ANON_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700211 K(global_page_state(NR_FILE_MAPPED)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700212 K(global_page_state(NR_SLAB_RECLAIMABLE) +
213 global_page_state(NR_SLAB_UNRECLAIMABLE)),
214 K(global_page_state(NR_SLAB_RECLAIMABLE)),
215 K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
Christoph Lameterdf849a12006-06-30 01:55:38 -0700216 K(global_page_state(NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700217 K(global_page_state(NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700218 K(global_page_state(NR_BOUNCE)),
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700219 K(global_page_state(NR_WRITEBACK_TEMP)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 K(allowed),
221 K(committed),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 (unsigned long)VMALLOC_TOTAL >> 10,
223 vmi.used >> 10,
224 vmi.largest_chunk >> 10
225 );
226
227 len += hugetlb_report_meminfo(page + len);
228
Andi Kleence0c0e52008-05-02 11:46:49 +0200229 len += arch_report_meminfo(page + len);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return proc_calc_metrics(page, start, off, count, eof, len);
232#undef K
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static int fragmentation_open(struct inode *inode, struct file *file)
236{
237 (void)inode;
238 return seq_open(file, &fragmentation_op);
239}
240
Arjan van de Ven00977a52007-02-12 00:55:34 -0800241static const struct file_operations fragmentation_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .open = fragmentation_open,
243 .read = seq_read,
244 .llseek = seq_lseek,
245 .release = seq_release,
246};
247
Mel Gorman467c9962007-10-16 01:26:02 -0700248static int pagetypeinfo_open(struct inode *inode, struct file *file)
249{
250 return seq_open(file, &pagetypeinfo_op);
251}
252
253static const struct file_operations pagetypeinfo_file_ops = {
254 .open = pagetypeinfo_open,
255 .read = seq_read,
256 .llseek = seq_lseek,
257 .release = seq_release,
258};
259
Nikita Danilov295ab932005-06-21 17:14:38 -0700260static int zoneinfo_open(struct inode *inode, struct file *file)
261{
262 return seq_open(file, &zoneinfo_op);
263}
264
Arjan van de Ven00977a52007-02-12 00:55:34 -0800265static const struct file_operations proc_zoneinfo_file_operations = {
Nikita Danilov295ab932005-06-21 17:14:38 -0700266 .open = zoneinfo_open,
267 .read = seq_read,
268 .llseek = seq_lseek,
269 .release = seq_release,
270};
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static int version_read_proc(char *page, char **start, off_t off,
273 int count, int *eof, void *data)
274{
275 int len;
276
Roman Zippel3eb3c742007-01-10 14:45:28 +0100277 len = snprintf(page, PAGE_SIZE, linux_proc_banner,
Linus Torvalds89937802006-12-11 09:28:46 -0800278 utsname()->sysname,
279 utsname()->release,
280 utsname()->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return proc_calc_metrics(page, start, off, count, eof, len);
282}
283
Jan Engelhardt03a44822008-02-08 04:21:19 -0800284extern const struct seq_operations cpuinfo_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int cpuinfo_open(struct inode *inode, struct file *file)
286{
287 return seq_open(file, &cpuinfo_op);
288}
Neil Horman7170be52006-01-14 13:20:38 -0800289
Arjan van de Ven00977a52007-02-12 00:55:34 -0800290static const struct file_operations proc_cpuinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800291 .open = cpuinfo_open,
Neil Horman7170be52006-01-14 13:20:38 -0800292 .read = seq_read,
293 .llseek = seq_lseek,
294 .release = seq_release,
295};
296
Joe Korty68eef3b2006-03-31 02:30:32 -0800297static int devinfo_show(struct seq_file *f, void *v)
298{
299 int i = *(loff_t *) v;
300
301 if (i < CHRDEV_MAJOR_HASH_SIZE) {
302 if (i == 0)
303 seq_printf(f, "Character devices:\n");
304 chrdev_show(f, i);
David Howells93614012006-09-30 20:45:40 +0200305 }
306#ifdef CONFIG_BLOCK
307 else {
Joe Korty68eef3b2006-03-31 02:30:32 -0800308 i -= CHRDEV_MAJOR_HASH_SIZE;
309 if (i == 0)
310 seq_printf(f, "\nBlock devices:\n");
311 blkdev_show(f, i);
312 }
David Howells93614012006-09-30 20:45:40 +0200313#endif
Joe Korty68eef3b2006-03-31 02:30:32 -0800314 return 0;
315}
316
317static void *devinfo_start(struct seq_file *f, loff_t *pos)
318{
319 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
320 return pos;
321 return NULL;
322}
323
324static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
325{
326 (*pos)++;
327 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
328 return NULL;
329 return pos;
330}
331
332static void devinfo_stop(struct seq_file *f, void *v)
333{
334 /* Nothing to do */
335}
336
Jan Engelhardt03a44822008-02-08 04:21:19 -0800337static const struct seq_operations devinfo_ops = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800338 .start = devinfo_start,
339 .next = devinfo_next,
340 .stop = devinfo_stop,
341 .show = devinfo_show
342};
343
344static int devinfo_open(struct inode *inode, struct file *filp)
345{
346 return seq_open(filp, &devinfo_ops);
347}
348
Arjan van de Ven00977a52007-02-12 00:55:34 -0800349static const struct file_operations proc_devinfo_operations = {
Joe Korty68eef3b2006-03-31 02:30:32 -0800350 .open = devinfo_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 .read = seq_read,
352 .llseek = seq_lseek,
353 .release = seq_release,
354};
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static int vmstat_open(struct inode *inode, struct file *file)
357{
358 return seq_open(file, &vmstat_op);
359}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800360static const struct file_operations proc_vmstat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 .open = vmstat_open,
362 .read = seq_read,
363 .llseek = seq_lseek,
364 .release = seq_release,
365};
366
367#ifdef CONFIG_PROC_HARDWARE
368static int hardware_read_proc(char *page, char **start, off_t off,
369 int count, int *eof, void *data)
370{
371 int len = get_hardware_list(page);
372 return proc_calc_metrics(page, start, off, count, eof, len);
373}
374#endif
375
376#ifdef CONFIG_STRAM_PROC
377static int stram_read_proc(char *page, char **start, off_t off,
378 int count, int *eof, void *data)
379{
380 int len = get_stram_list(page);
381 return proc_calc_metrics(page, start, off, count, eof, len);
382}
383#endif
384
David Howells93614012006-09-30 20:45:40 +0200385#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static int partitions_open(struct inode *inode, struct file *file)
387{
388 return seq_open(file, &partitions_op);
389}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800390static const struct file_operations proc_partitions_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 .open = partitions_open,
392 .read = seq_read,
393 .llseek = seq_lseek,
394 .release = seq_release,
395};
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397static int diskstats_open(struct inode *inode, struct file *file)
398{
399 return seq_open(file, &diskstats_op);
400}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800401static const struct file_operations proc_diskstats_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 .open = diskstats_open,
403 .read = seq_read,
404 .llseek = seq_lseek,
405 .release = seq_release,
406};
David Howells93614012006-09-30 20:45:40 +0200407#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409#ifdef CONFIG_MODULES
Jan Engelhardt03a44822008-02-08 04:21:19 -0800410extern const struct seq_operations modules_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static int modules_open(struct inode *inode, struct file *file)
412{
413 return seq_open(file, &modules_op);
414}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800415static const struct file_operations proc_modules_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 .open = modules_open,
417 .read = seq_read,
418 .llseek = seq_lseek,
419 .release = seq_release,
420};
421#endif
422
Linus Torvalds158a9622008-01-02 13:04:48 -0800423#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static int slabinfo_open(struct inode *inode, struct file *file)
425{
426 return seq_open(file, &slabinfo_op);
427}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800428static const struct file_operations proc_slabinfo_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 .open = slabinfo_open,
430 .read = seq_read,
431 .write = slabinfo_write,
432 .llseek = seq_lseek,
433 .release = seq_release,
434};
Al Viro871751e2006-03-25 03:06:39 -0800435
436#ifdef CONFIG_DEBUG_SLAB_LEAK
Jan Engelhardt03a44822008-02-08 04:21:19 -0800437extern const struct seq_operations slabstats_op;
Al Viro871751e2006-03-25 03:06:39 -0800438static int slabstats_open(struct inode *inode, struct file *file)
439{
440 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
441 int ret = -ENOMEM;
442 if (n) {
443 ret = seq_open(file, &slabstats_op);
444 if (!ret) {
445 struct seq_file *m = file->private_data;
446 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
447 m->private = n;
448 n = NULL;
449 }
450 kfree(n);
451 }
452 return ret;
453}
454
Arjan van de Ven00977a52007-02-12 00:55:34 -0800455static const struct file_operations proc_slabstats_operations = {
Al Viro871751e2006-03-25 03:06:39 -0800456 .open = slabstats_open,
457 .read = seq_read,
458 .llseek = seq_lseek,
Martin Peschke09f08922007-05-08 00:29:26 -0700459 .release = seq_release_private,
Al Viro871751e2006-03-25 03:06:39 -0800460};
461#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800462#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Christoph Lametera10aa572008-04-28 02:12:40 -0700464#ifdef CONFIG_MMU
465static int vmalloc_open(struct inode *inode, struct file *file)
466{
467 return seq_open(file, &vmalloc_op);
468}
469
470static const struct file_operations proc_vmalloc_operations = {
471 .open = vmalloc_open,
472 .read = seq_read,
473 .llseek = seq_lseek,
474 .release = seq_release,
475};
476#endif
477
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200478#ifndef arch_irq_stat_cpu
479#define arch_irq_stat_cpu(cpu) 0
480#endif
481#ifndef arch_irq_stat
482#define arch_irq_stat() 0
483#endif
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485static int show_stat(struct seq_file *p, void *v)
486{
487 int i;
488 unsigned long jif;
489 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200490 cputime64_t guest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 u64 sum = 0;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700492 struct timespec boottime;
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700493 unsigned int *per_irq_sum;
494
495 per_irq_sum = kzalloc(sizeof(unsigned int)*NR_IRQS, GFP_KERNEL);
496 if (!per_irq_sum)
497 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 user = nice = system = idle = iowait =
500 irq = softirq = steal = cputime64_zero;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200501 guest = cputime64_zero;
Tomas Janousek924b42d2007-07-15 23:39:42 -0700502 getboottime(&boottime);
503 jif = boottime.tv_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800505 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int j;
507
508 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
509 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
510 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
511 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
512 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
513 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
514 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
515 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200516 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700517 for (j = 0; j < NR_IRQS; j++) {
518 unsigned int temp = kstat_cpu(i).irqs[j];
519 sum += temp;
520 per_irq_sum[j] += temp;
521 }
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200522 sum += arch_irq_stat_cpu(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200524 sum += arch_irq_stat();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200526 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 (unsigned long long)cputime64_to_clock_t(user),
528 (unsigned long long)cputime64_to_clock_t(nice),
529 (unsigned long long)cputime64_to_clock_t(system),
530 (unsigned long long)cputime64_to_clock_t(idle),
531 (unsigned long long)cputime64_to_clock_t(iowait),
532 (unsigned long long)cputime64_to_clock_t(irq),
533 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200534 (unsigned long long)cputime64_to_clock_t(steal),
535 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 for_each_online_cpu(i) {
537
538 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
539 user = kstat_cpu(i).cpustat.user;
540 nice = kstat_cpu(i).cpustat.nice;
541 system = kstat_cpu(i).cpustat.system;
542 idle = kstat_cpu(i).cpustat.idle;
543 iowait = kstat_cpu(i).cpustat.iowait;
544 irq = kstat_cpu(i).cpustat.irq;
545 softirq = kstat_cpu(i).cpustat.softirq;
546 steal = kstat_cpu(i).cpustat.steal;
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200547 guest = kstat_cpu(i).cpustat.guest;
548 seq_printf(p,
549 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 i,
551 (unsigned long long)cputime64_to_clock_t(user),
552 (unsigned long long)cputime64_to_clock_t(nice),
553 (unsigned long long)cputime64_to_clock_t(system),
554 (unsigned long long)cputime64_to_clock_t(idle),
555 (unsigned long long)cputime64_to_clock_t(iowait),
556 (unsigned long long)cputime64_to_clock_t(irq),
557 (unsigned long long)cputime64_to_clock_t(softirq),
Laurent Vivier5e84cfd2007-10-15 17:00:19 +0200558 (unsigned long long)cputime64_to_clock_t(steal),
559 (unsigned long long)cputime64_to_clock_t(guest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 seq_printf(p, "intr %llu", (unsigned long long)sum);
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 for (i = 0; i < NR_IRQS; i++)
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700564 seq_printf(p, " %u", per_irq_sum[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 seq_printf(p,
567 "\nctxt %llu\n"
568 "btime %lu\n"
569 "processes %lu\n"
570 "procs_running %lu\n"
571 "procs_blocked %lu\n",
572 nr_context_switches(),
573 (unsigned long)jif,
574 total_forks,
575 nr_running(),
576 nr_iowait());
577
Ravikiran G Thirumalai4004c692007-07-19 01:47:53 -0700578 kfree(per_irq_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580}
581
582static int stat_open(struct inode *inode, struct file *file)
583{
584 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
585 char *buf;
586 struct seq_file *m;
587 int res;
588
589 /* don't ask for more than the kmalloc() max size, currently 128 KB */
590 if (size > 128 * 1024)
591 size = 128 * 1024;
592 buf = kmalloc(size, GFP_KERNEL);
593 if (!buf)
594 return -ENOMEM;
595
596 res = single_open(file, show_stat, NULL);
597 if (!res) {
598 m = file->private_data;
599 m->buf = buf;
600 m->size = size;
601 } else
602 kfree(buf);
603 return res;
604}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800605static const struct file_operations proc_stat_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 .open = stat_open,
607 .read = seq_read,
608 .llseek = seq_lseek,
609 .release = single_release,
610};
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * /proc/interrupts
614 */
615static void *int_seq_start(struct seq_file *f, loff_t *pos)
616{
617 return (*pos <= NR_IRQS) ? pos : NULL;
618}
619
620static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
621{
622 (*pos)++;
623 if (*pos > NR_IRQS)
624 return NULL;
625 return pos;
626}
627
628static void int_seq_stop(struct seq_file *f, void *v)
629{
630 /* Nothing to do */
631}
632
633
Jan Engelhardt03a44822008-02-08 04:21:19 -0800634static const struct seq_operations int_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 .start = int_seq_start,
636 .next = int_seq_next,
637 .stop = int_seq_stop,
638 .show = show_interrupts
639};
640
641static int interrupts_open(struct inode *inode, struct file *filp)
642{
643 return seq_open(filp, &int_seq_ops);
644}
645
Arjan van de Ven00977a52007-02-12 00:55:34 -0800646static const struct file_operations proc_interrupts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 .open = interrupts_open,
648 .read = seq_read,
649 .llseek = seq_lseek,
650 .release = seq_release,
651};
652
653static int filesystems_read_proc(char *page, char **start, off_t off,
654 int count, int *eof, void *data)
655{
656 int len = get_filesystem_list(page);
657 return proc_calc_metrics(page, start, off, count, eof, len);
658}
659
660static int cmdline_read_proc(char *page, char **start, off_t off,
661 int count, int *eof, void *data)
662{
663 int len;
664
665 len = sprintf(page, "%s\n", saved_command_line);
666 return proc_calc_metrics(page, start, off, count, eof, len);
667}
668
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700669static int locks_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700671 return seq_open(filp, &locks_seq_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -0700674static const struct file_operations proc_locks_operations = {
675 .open = locks_open,
676 .read = seq_read,
677 .llseek = seq_lseek,
678 .release = seq_release,
679};
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static int execdomains_read_proc(char *page, char **start, off_t off,
682 int count, int *eof, void *data)
683{
684 int len = get_exec_domain_list(page);
685 return proc_calc_metrics(page, start, off, count, eof, len);
686}
687
688#ifdef CONFIG_MAGIC_SYSRQ
689/*
690 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
691 */
692static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
693 size_t count, loff_t *ppos)
694{
695 if (count) {
696 char c;
697
698 if (get_user(c, buf))
699 return -EFAULT;
David Howells7d12e782006-10-05 14:55:46 +0100700 __handle_sysrq(c, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 return count;
703}
704
Arjan van de Ven00977a52007-02-12 00:55:34 -0800705static const struct file_operations proc_sysrq_trigger_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 .write = write_sysrq_trigger,
707};
708#endif
709
Matt Mackall1e883282008-02-04 22:29:07 -0800710#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -0800711#define KPMSIZE sizeof(u64)
712#define KPMMASK (KPMSIZE - 1)
713/* /proc/kpagecount - an array exposing page counts
714 *
715 * Each entry is a u64 representing the corresponding
716 * physical page count.
717 */
718static ssize_t kpagecount_read(struct file *file, char __user *buf,
719 size_t count, loff_t *ppos)
720{
721 u64 __user *out = (u64 __user *)buf;
722 struct page *ppage;
723 unsigned long src = *ppos;
724 unsigned long pfn;
725 ssize_t ret = 0;
726 u64 pcount;
727
728 pfn = src / KPMSIZE;
729 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
730 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700731 return -EINVAL;
Matt Mackall161f47b2008-02-04 22:29:05 -0800732
733 while (count > 0) {
Matt Mackall304daa82008-02-04 22:29:06 -0800734 ppage = NULL;
735 if (pfn_valid(pfn))
736 ppage = pfn_to_page(pfn);
737 pfn++;
Matt Mackall161f47b2008-02-04 22:29:05 -0800738 if (!ppage)
739 pcount = 0;
740 else
Thomas Tuttlebbcdac02008-06-05 22:46:58 -0700741 pcount = page_mapcount(ppage);
Matt Mackall161f47b2008-02-04 22:29:05 -0800742
743 if (put_user(pcount, out++)) {
744 ret = -EFAULT;
745 break;
746 }
747
748 count -= KPMSIZE;
749 }
750
751 *ppos += (char __user *)out - buf;
752 if (!ret)
753 ret = (char __user *)out - buf;
754 return ret;
755}
756
757static struct file_operations proc_kpagecount_operations = {
758 .llseek = mem_lseek,
759 .read = kpagecount_read,
760};
761
Matt Mackall304daa82008-02-04 22:29:06 -0800762/* /proc/kpageflags - an array exposing page flags
763 *
764 * Each entry is a u64 representing the corresponding
765 * physical page flags.
766 */
767
768/* These macros are used to decouple internal flags from exported ones */
769
770#define KPF_LOCKED 0
771#define KPF_ERROR 1
772#define KPF_REFERENCED 2
773#define KPF_UPTODATE 3
774#define KPF_DIRTY 4
775#define KPF_LRU 5
776#define KPF_ACTIVE 6
777#define KPF_SLAB 7
778#define KPF_WRITEBACK 8
779#define KPF_RECLAIM 9
780#define KPF_BUDDY 10
781
782#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
783
784static ssize_t kpageflags_read(struct file *file, char __user *buf,
785 size_t count, loff_t *ppos)
786{
787 u64 __user *out = (u64 __user *)buf;
788 struct page *ppage;
789 unsigned long src = *ppos;
790 unsigned long pfn;
791 ssize_t ret = 0;
792 u64 kflags, uflags;
793
794 pfn = src / KPMSIZE;
795 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
796 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700797 return -EINVAL;
Matt Mackall304daa82008-02-04 22:29:06 -0800798
799 while (count > 0) {
800 ppage = NULL;
801 if (pfn_valid(pfn))
802 ppage = pfn_to_page(pfn);
803 pfn++;
804 if (!ppage)
805 kflags = 0;
806 else
807 kflags = ppage->flags;
808
809 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
810 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
811 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
812 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
813 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
814 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
815 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
816 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
817 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
818 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
819 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
820
821 if (put_user(uflags, out++)) {
822 ret = -EFAULT;
823 break;
824 }
825
826 count -= KPMSIZE;
827 }
828
829 *ppos += (char __user *)out - buf;
830 if (!ret)
831 ret = (char __user *)out - buf;
832 return ret;
833}
834
835static struct file_operations proc_kpageflags_operations = {
836 .llseek = mem_lseek,
837 .read = kpageflags_read,
838};
Matt Mackall1e883282008-02-04 22:29:07 -0800839#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall304daa82008-02-04 22:29:06 -0800840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841struct proc_dir_entry *proc_root_kcore;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843void __init proc_misc_init(void)
844{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 static struct {
846 char *name;
847 int (*read_proc)(char*,char**,off_t,int,int*,void*);
848 } *p, simple_ones[] = {
849 {"loadavg", loadavg_read_proc},
850 {"uptime", uptime_read_proc},
851 {"meminfo", meminfo_read_proc},
852 {"version", version_read_proc},
853#ifdef CONFIG_PROC_HARDWARE
854 {"hardware", hardware_read_proc},
855#endif
856#ifdef CONFIG_STRAM_PROC
857 {"stram", stram_read_proc},
858#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 {"filesystems", filesystems_read_proc},
860 {"cmdline", cmdline_read_proc},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 {"execdomains", execdomains_read_proc},
862 {NULL,}
863 };
864 for (p = simple_ones; p->name; p++)
865 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
866
867 proc_symlink("mounts", NULL, "self/mounts");
868
869 /* And now for trickier ones */
Mike Galbraithc36264d2006-12-06 20:37:42 -0800870#ifdef CONFIG_PRINTK
Alexey Dobriyanc74c1202008-04-29 01:01:44 -0700871 proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
Mike Galbraithc36264d2006-12-06 20:37:42 -0800872#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700873 proc_create("locks", 0, NULL, &proc_locks_operations);
874 proc_create("devices", 0, NULL, &proc_devinfo_operations);
875 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
David Howells93614012006-09-30 20:45:40 +0200876#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700877 proc_create("partitions", 0, NULL, &proc_partitions_operations);
David Howells93614012006-09-30 20:45:40 +0200878#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700879 proc_create("stat", 0, NULL, &proc_stat_operations);
880 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
Linus Torvalds158a9622008-01-02 13:04:48 -0800881#ifdef CONFIG_SLABINFO
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700882 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
Al Viro871751e2006-03-25 03:06:39 -0800883#ifdef CONFIG_DEBUG_SLAB_LEAK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700884 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
Al Viro871751e2006-03-25 03:06:39 -0800885#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800886#endif
Christoph Lametera10aa572008-04-28 02:12:40 -0700887#ifdef CONFIG_MMU
888 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
889#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700890 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
891 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
892 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
893 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
David Howells93614012006-09-30 20:45:40 +0200894#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700895 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#ifdef CONFIG_MODULES
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700898 proc_create("modules", 0, NULL, &proc_modules_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif
900#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700901 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#endif
903#ifdef CONFIG_PROC_KCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700904 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
905 if (proc_root_kcore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 proc_root_kcore->size =
907 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908#endif
Matt Mackall1e883282008-02-04 22:29:07 -0800909#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700910 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
911 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
Matt Mackall1e883282008-02-04 22:29:07 -0800912#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700913#ifdef CONFIG_PROC_VMCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700914 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700915#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916#ifdef CONFIG_MAGIC_SYSRQ
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700917 proc_create("sysrq-trigger", S_IWUSR, NULL, &proc_sysrq_trigger_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}