blob: a87802fc93130a212e1b06670f6c506b31d4742d [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 -070060static int fragmentation_open(struct inode *inode, struct file *file)
61{
62 (void)inode;
63 return seq_open(file, &fragmentation_op);
64}
65
Arjan van de Ven00977a52007-02-12 00:55:34 -080066static const struct file_operations fragmentation_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .open = fragmentation_open,
68 .read = seq_read,
69 .llseek = seq_lseek,
70 .release = seq_release,
71};
72
Mel Gorman467c9962007-10-16 01:26:02 -070073static int pagetypeinfo_open(struct inode *inode, struct file *file)
74{
75 return seq_open(file, &pagetypeinfo_op);
76}
77
78static const struct file_operations pagetypeinfo_file_ops = {
79 .open = pagetypeinfo_open,
80 .read = seq_read,
81 .llseek = seq_lseek,
82 .release = seq_release,
83};
84
Nikita Danilov295ab932005-06-21 17:14:38 -070085static int zoneinfo_open(struct inode *inode, struct file *file)
86{
87 return seq_open(file, &zoneinfo_op);
88}
89
Arjan van de Ven00977a52007-02-12 00:55:34 -080090static const struct file_operations proc_zoneinfo_file_operations = {
Nikita Danilov295ab932005-06-21 17:14:38 -070091 .open = zoneinfo_open,
92 .read = seq_read,
93 .llseek = seq_lseek,
94 .release = seq_release,
95};
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int vmstat_open(struct inode *inode, struct file *file)
98{
99 return seq_open(file, &vmstat_op);
100}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800101static const struct file_operations proc_vmstat_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .open = vmstat_open,
103 .read = seq_read,
104 .llseek = seq_lseek,
105 .release = seq_release,
106};
107
David Howells93614012006-09-30 20:45:40 +0200108#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int diskstats_open(struct inode *inode, struct file *file)
110{
111 return seq_open(file, &diskstats_op);
112}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800113static const struct file_operations proc_diskstats_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .open = diskstats_open,
115 .read = seq_read,
116 .llseek = seq_lseek,
117 .release = seq_release,
118};
David Howells93614012006-09-30 20:45:40 +0200119#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121#ifdef CONFIG_MODULES
Jan Engelhardt03a44822008-02-08 04:21:19 -0800122extern const struct seq_operations modules_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int modules_open(struct inode *inode, struct file *file)
124{
125 return seq_open(file, &modules_op);
126}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800127static const struct file_operations proc_modules_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .open = modules_open,
129 .read = seq_read,
130 .llseek = seq_lseek,
131 .release = seq_release,
132};
133#endif
134
Linus Torvalds158a9622008-01-02 13:04:48 -0800135#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static int slabinfo_open(struct inode *inode, struct file *file)
137{
138 return seq_open(file, &slabinfo_op);
139}
Arjan van de Ven00977a52007-02-12 00:55:34 -0800140static const struct file_operations proc_slabinfo_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .open = slabinfo_open,
142 .read = seq_read,
143 .write = slabinfo_write,
144 .llseek = seq_lseek,
145 .release = seq_release,
146};
Al Viro871751e2006-03-25 03:06:39 -0800147
148#ifdef CONFIG_DEBUG_SLAB_LEAK
Jan Engelhardt03a44822008-02-08 04:21:19 -0800149extern const struct seq_operations slabstats_op;
Al Viro871751e2006-03-25 03:06:39 -0800150static int slabstats_open(struct inode *inode, struct file *file)
151{
152 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
153 int ret = -ENOMEM;
154 if (n) {
155 ret = seq_open(file, &slabstats_op);
156 if (!ret) {
157 struct seq_file *m = file->private_data;
158 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
159 m->private = n;
160 n = NULL;
161 }
162 kfree(n);
163 }
164 return ret;
165}
166
Arjan van de Ven00977a52007-02-12 00:55:34 -0800167static const struct file_operations proc_slabstats_operations = {
Al Viro871751e2006-03-25 03:06:39 -0800168 .open = slabstats_open,
169 .read = seq_read,
170 .llseek = seq_lseek,
Martin Peschke09f08922007-05-08 00:29:26 -0700171 .release = seq_release_private,
Al Viro871751e2006-03-25 03:06:39 -0800172};
173#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Christoph Lametera10aa572008-04-28 02:12:40 -0700176#ifdef CONFIG_MMU
177static int vmalloc_open(struct inode *inode, struct file *file)
178{
Eric Dumazeta47a1262008-07-23 21:27:38 -0700179 unsigned int *ptr = NULL;
180 int ret;
181
182 if (NUMA_BUILD)
183 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
184 ret = seq_open(file, &vmalloc_op);
185 if (!ret) {
186 struct seq_file *m = file->private_data;
187 m->private = ptr;
188 } else
189 kfree(ptr);
190 return ret;
Christoph Lametera10aa572008-04-28 02:12:40 -0700191}
192
193static const struct file_operations proc_vmalloc_operations = {
194 .open = vmalloc_open,
195 .read = seq_read,
196 .llseek = seq_lseek,
Eric Dumazeta47a1262008-07-23 21:27:38 -0700197 .release = seq_release_private,
Christoph Lametera10aa572008-04-28 02:12:40 -0700198};
199#endif
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
202 * /proc/interrupts
203 */
204static void *int_seq_start(struct seq_file *f, loff_t *pos)
205{
Yinghai Luda27c112008-08-19 20:49:56 -0700206 return (*pos <= nr_irqs) ? pos : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Yinghai Lu52b17322008-08-19 20:50:20 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
211{
212 (*pos)++;
Yinghai Lu52b17322008-08-19 20:50:20 -0700213 return (*pos <= nr_irqs) ? pos : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216static void int_seq_stop(struct seq_file *f, void *v)
217{
218 /* Nothing to do */
219}
220
Jan Engelhardt03a44822008-02-08 04:21:19 -0800221static const struct seq_operations int_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .start = int_seq_start,
223 .next = int_seq_next,
224 .stop = int_seq_stop,
225 .show = show_interrupts
226};
227
228static int interrupts_open(struct inode *inode, struct file *filp)
229{
230 return seq_open(filp, &int_seq_ops);
231}
232
Arjan van de Ven00977a52007-02-12 00:55:34 -0800233static const struct file_operations proc_interrupts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .open = interrupts_open,
235 .read = seq_read,
236 .llseek = seq_lseek,
237 .release = seq_release,
238};
239
Matt Mackall1e883282008-02-04 22:29:07 -0800240#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -0800241#define KPMSIZE sizeof(u64)
242#define KPMMASK (KPMSIZE - 1)
243/* /proc/kpagecount - an array exposing page counts
244 *
245 * Each entry is a u64 representing the corresponding
246 * physical page count.
247 */
248static ssize_t kpagecount_read(struct file *file, char __user *buf,
249 size_t count, loff_t *ppos)
250{
251 u64 __user *out = (u64 __user *)buf;
252 struct page *ppage;
253 unsigned long src = *ppos;
254 unsigned long pfn;
255 ssize_t ret = 0;
256 u64 pcount;
257
258 pfn = src / KPMSIZE;
259 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
260 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700261 return -EINVAL;
Matt Mackall161f47b2008-02-04 22:29:05 -0800262
263 while (count > 0) {
Matt Mackall304daa82008-02-04 22:29:06 -0800264 ppage = NULL;
265 if (pfn_valid(pfn))
266 ppage = pfn_to_page(pfn);
267 pfn++;
Matt Mackall161f47b2008-02-04 22:29:05 -0800268 if (!ppage)
269 pcount = 0;
270 else
Thomas Tuttlebbcdac02008-06-05 22:46:58 -0700271 pcount = page_mapcount(ppage);
Matt Mackall161f47b2008-02-04 22:29:05 -0800272
273 if (put_user(pcount, out++)) {
274 ret = -EFAULT;
275 break;
276 }
277
278 count -= KPMSIZE;
279 }
280
281 *ppos += (char __user *)out - buf;
282 if (!ret)
283 ret = (char __user *)out - buf;
284 return ret;
285}
286
287static struct file_operations proc_kpagecount_operations = {
288 .llseek = mem_lseek,
289 .read = kpagecount_read,
290};
291
Matt Mackall304daa82008-02-04 22:29:06 -0800292/* /proc/kpageflags - an array exposing page flags
293 *
294 * Each entry is a u64 representing the corresponding
295 * physical page flags.
296 */
297
298/* These macros are used to decouple internal flags from exported ones */
299
300#define KPF_LOCKED 0
301#define KPF_ERROR 1
302#define KPF_REFERENCED 2
303#define KPF_UPTODATE 3
304#define KPF_DIRTY 4
305#define KPF_LRU 5
306#define KPF_ACTIVE 6
307#define KPF_SLAB 7
308#define KPF_WRITEBACK 8
309#define KPF_RECLAIM 9
310#define KPF_BUDDY 10
311
312#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
313
314static ssize_t kpageflags_read(struct file *file, char __user *buf,
315 size_t count, loff_t *ppos)
316{
317 u64 __user *out = (u64 __user *)buf;
318 struct page *ppage;
319 unsigned long src = *ppos;
320 unsigned long pfn;
321 ssize_t ret = 0;
322 u64 kflags, uflags;
323
324 pfn = src / KPMSIZE;
325 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
326 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700327 return -EINVAL;
Matt Mackall304daa82008-02-04 22:29:06 -0800328
329 while (count > 0) {
330 ppage = NULL;
331 if (pfn_valid(pfn))
332 ppage = pfn_to_page(pfn);
333 pfn++;
334 if (!ppage)
335 kflags = 0;
336 else
337 kflags = ppage->flags;
338
339 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
340 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
341 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
342 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
343 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
344 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
345 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
346 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
347 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
348 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
349 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
350
351 if (put_user(uflags, out++)) {
352 ret = -EFAULT;
353 break;
354 }
355
356 count -= KPMSIZE;
357 }
358
359 *ppos += (char __user *)out - buf;
360 if (!ret)
361 ret = (char __user *)out - buf;
362 return ret;
363}
364
365static struct file_operations proc_kpageflags_operations = {
366 .llseek = mem_lseek,
367 .read = kpageflags_read,
368};
Matt Mackall1e883282008-02-04 22:29:07 -0800369#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall304daa82008-02-04 22:29:06 -0800370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371struct proc_dir_entry *proc_root_kcore;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373void __init proc_misc_init(void)
374{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 proc_symlink("mounts", NULL, "self/mounts");
376
377 /* And now for trickier ones */
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700378 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
Linus Torvalds158a9622008-01-02 13:04:48 -0800379#ifdef CONFIG_SLABINFO
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700380 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
Al Viro871751e2006-03-25 03:06:39 -0800381#ifdef CONFIG_DEBUG_SLAB_LEAK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700382 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
Al Viro871751e2006-03-25 03:06:39 -0800383#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800384#endif
Christoph Lametera10aa572008-04-28 02:12:40 -0700385#ifdef CONFIG_MMU
386 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
387#endif
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700388 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
389 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
390 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
391 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
David Howells93614012006-09-30 20:45:40 +0200392#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700393 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#ifdef CONFIG_MODULES
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700396 proc_create("modules", 0, NULL, &proc_modules_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif
398#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700399 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#endif
401#ifdef CONFIG_PROC_KCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700402 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
403 if (proc_root_kcore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 proc_root_kcore->size =
405 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#endif
Matt Mackall1e883282008-02-04 22:29:07 -0800407#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700408 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
409 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
Matt Mackall1e883282008-02-04 22:29:07 -0800410#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700411#ifdef CONFIG_PROC_VMCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700412 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700413#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}