2 * linux/fs/proc/proc_misc.c
4 * linux/fs/proc/array.c
5 * Copyright (C) 1992 by Linus Torvalds
6 * based on ideas by Darren Senn
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.
14 * Fulton Green : Encapsulated position metric calculations.
15 * <kernel@FultonGreen.com>
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>
24 #include <linux/tty.h>
25 #include <linux/string.h>
26 #include <linux/mman.h>
27 #include <linux/quicklist.h>
28 #include <linux/proc_fs.h>
29 #include <linux/ioport.h>
31 #include <linux/mmzone.h>
32 #include <linux/pagemap.h>
33 #include <linux/irq.h>
34 #include <linux/interrupt.h>
35 #include <linux/swap.h>
36 #include <linux/slab.h>
37 #include <linux/genhd.h>
38 #include <linux/smp.h>
39 #include <linux/signal.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/seq_file.h>
43 #include <linux/times.h>
44 #include <linux/profile.h>
45 #include <linux/utsname.h>
46 #include <linux/blkdev.h>
47 #include <linux/hugetlb.h>
48 #include <linux/jiffies.h>
49 #include <linux/vmalloc.h>
50 #include <linux/crash_dump.h>
51 #include <linux/pid_namespace.h>
52 #include <linux/bootmem.h>
53 #include <asm/uaccess.h>
54 #include <asm/pgtable.h>
57 #include <asm/div64.h>
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.
66 extern int get_hardware_list(char *);
67 extern int get_stram_list(char *);
68 extern int get_exec_domain_list(char *);
70 static int proc_calc_metrics(char *page, char **start, off_t off,
71 int count, int *eof, int len)
73 if (len <= off+count) *eof = 1;
76 if (len>count) len = count;
81 int __attribute__((weak)) arch_report_meminfo(char *page)
86 static int meminfo_read_proc(char *page, char **start, off_t off,
87 int count, int *eof, void *data)
91 unsigned long committed;
92 unsigned long allowed;
93 struct vmalloc_info vmi;
95 unsigned long pages[NR_LRU_LISTS];
99 * display in kilobytes.
101 #define K(x) ((x) << (PAGE_SHIFT - 10))
104 committed = atomic_long_read(&vm_committed_space);
105 allowed = ((totalram_pages - hugetlb_total_pages())
106 * sysctl_overcommit_ratio / 100) + total_swap_pages;
108 cached = global_page_state(NR_FILE_PAGES) -
109 total_swapcache_pages - i.bufferram;
113 get_vmalloc_info(&vmi);
115 for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
116 pages[lru] = global_page_state(NR_LRU_BASE + lru);
119 * Tagged format, for easy grepping and expansion.
122 "MemTotal: %8lu kB\n"
126 "SwapCached: %8lu kB\n"
128 "Inactive: %8lu kB\n"
129 "Active(anon): %8lu kB\n"
130 "Inactive(anon): %8lu kB\n"
131 "Active(file): %8lu kB\n"
132 "Inactive(file): %8lu kB\n"
133 #ifdef CONFIG_UNEVICTABLE_LRU
134 "Unevictable: %8lu kB\n"
137 #ifdef CONFIG_HIGHMEM
138 "HighTotal: %8lu kB\n"
139 "HighFree: %8lu kB\n"
140 "LowTotal: %8lu kB\n"
143 "SwapTotal: %8lu kB\n"
144 "SwapFree: %8lu kB\n"
146 "Writeback: %8lu kB\n"
147 "AnonPages: %8lu kB\n"
150 "SReclaimable: %8lu kB\n"
151 "SUnreclaim: %8lu kB\n"
152 "PageTables: %8lu kB\n"
153 #ifdef CONFIG_QUICKLIST
154 "Quicklists: %8lu kB\n"
156 "NFS_Unstable: %8lu kB\n"
158 "WritebackTmp: %8lu kB\n"
159 "CommitLimit: %8lu kB\n"
160 "Committed_AS: %8lu kB\n"
161 "VmallocTotal: %8lu kB\n"
162 "VmallocUsed: %8lu kB\n"
163 "VmallocChunk: %8lu kB\n",
168 K(total_swapcache_pages),
169 K(pages[LRU_ACTIVE_ANON] + pages[LRU_ACTIVE_FILE]),
170 K(pages[LRU_INACTIVE_ANON] + pages[LRU_INACTIVE_FILE]),
171 K(pages[LRU_ACTIVE_ANON]),
172 K(pages[LRU_INACTIVE_ANON]),
173 K(pages[LRU_ACTIVE_FILE]),
174 K(pages[LRU_INACTIVE_FILE]),
175 #ifdef CONFIG_UNEVICTABLE_LRU
176 K(pages[LRU_UNEVICTABLE]),
177 K(global_page_state(NR_MLOCK)),
179 #ifdef CONFIG_HIGHMEM
182 K(i.totalram-i.totalhigh),
183 K(i.freeram-i.freehigh),
187 K(global_page_state(NR_FILE_DIRTY)),
188 K(global_page_state(NR_WRITEBACK)),
189 K(global_page_state(NR_ANON_PAGES)),
190 K(global_page_state(NR_FILE_MAPPED)),
191 K(global_page_state(NR_SLAB_RECLAIMABLE) +
192 global_page_state(NR_SLAB_UNRECLAIMABLE)),
193 K(global_page_state(NR_SLAB_RECLAIMABLE)),
194 K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
195 K(global_page_state(NR_PAGETABLE)),
196 #ifdef CONFIG_QUICKLIST
197 K(quicklist_total_size()),
199 K(global_page_state(NR_UNSTABLE_NFS)),
200 K(global_page_state(NR_BOUNCE)),
201 K(global_page_state(NR_WRITEBACK_TEMP)),
204 (unsigned long)VMALLOC_TOTAL >> 10,
206 vmi.largest_chunk >> 10
209 len += hugetlb_report_meminfo(page + len);
211 len += arch_report_meminfo(page + len);
213 return proc_calc_metrics(page, start, off, count, eof, len);
217 static int fragmentation_open(struct inode *inode, struct file *file)
220 return seq_open(file, &fragmentation_op);
223 static const struct file_operations fragmentation_file_operations = {
224 .open = fragmentation_open,
227 .release = seq_release,
230 static int pagetypeinfo_open(struct inode *inode, struct file *file)
232 return seq_open(file, &pagetypeinfo_op);
235 static const struct file_operations pagetypeinfo_file_ops = {
236 .open = pagetypeinfo_open,
239 .release = seq_release,
242 static int zoneinfo_open(struct inode *inode, struct file *file)
244 return seq_open(file, &zoneinfo_op);
247 static const struct file_operations proc_zoneinfo_file_operations = {
248 .open = zoneinfo_open,
251 .release = seq_release,
254 static int version_read_proc(char *page, char **start, off_t off,
255 int count, int *eof, void *data)
259 len = snprintf(page, PAGE_SIZE, linux_proc_banner,
263 return proc_calc_metrics(page, start, off, count, eof, len);
266 extern const struct seq_operations cpuinfo_op;
267 static int cpuinfo_open(struct inode *inode, struct file *file)
269 return seq_open(file, &cpuinfo_op);
272 static const struct file_operations proc_cpuinfo_operations = {
273 .open = cpuinfo_open,
276 .release = seq_release,
279 static int devinfo_show(struct seq_file *f, void *v)
281 int i = *(loff_t *) v;
283 if (i < CHRDEV_MAJOR_HASH_SIZE) {
285 seq_printf(f, "Character devices:\n");
290 i -= CHRDEV_MAJOR_HASH_SIZE;
292 seq_printf(f, "\nBlock devices:\n");
299 static void *devinfo_start(struct seq_file *f, loff_t *pos)
301 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
306 static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
309 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
314 static void devinfo_stop(struct seq_file *f, void *v)
319 static const struct seq_operations devinfo_ops = {
320 .start = devinfo_start,
321 .next = devinfo_next,
322 .stop = devinfo_stop,
326 static int devinfo_open(struct inode *inode, struct file *filp)
328 return seq_open(filp, &devinfo_ops);
331 static const struct file_operations proc_devinfo_operations = {
332 .open = devinfo_open,
335 .release = seq_release,
338 static int vmstat_open(struct inode *inode, struct file *file)
340 return seq_open(file, &vmstat_op);
342 static const struct file_operations proc_vmstat_file_operations = {
346 .release = seq_release,
349 #ifdef CONFIG_PROC_HARDWARE
350 static int hardware_read_proc(char *page, char **start, off_t off,
351 int count, int *eof, void *data)
353 int len = get_hardware_list(page);
354 return proc_calc_metrics(page, start, off, count, eof, len);
358 #ifdef CONFIG_STRAM_PROC
359 static int stram_read_proc(char *page, char **start, off_t off,
360 int count, int *eof, void *data)
362 int len = get_stram_list(page);
363 return proc_calc_metrics(page, start, off, count, eof, len);
368 static int partitions_open(struct inode *inode, struct file *file)
370 return seq_open(file, &partitions_op);
372 static const struct file_operations proc_partitions_operations = {
373 .open = partitions_open,
376 .release = seq_release,
379 static int diskstats_open(struct inode *inode, struct file *file)
381 return seq_open(file, &diskstats_op);
383 static const struct file_operations proc_diskstats_operations = {
384 .open = diskstats_open,
387 .release = seq_release,
391 #ifdef CONFIG_MODULES
392 extern const struct seq_operations modules_op;
393 static int modules_open(struct inode *inode, struct file *file)
395 return seq_open(file, &modules_op);
397 static const struct file_operations proc_modules_operations = {
398 .open = modules_open,
401 .release = seq_release,
405 #ifdef CONFIG_SLABINFO
406 static int slabinfo_open(struct inode *inode, struct file *file)
408 return seq_open(file, &slabinfo_op);
410 static const struct file_operations proc_slabinfo_operations = {
411 .open = slabinfo_open,
413 .write = slabinfo_write,
415 .release = seq_release,
418 #ifdef CONFIG_DEBUG_SLAB_LEAK
419 extern const struct seq_operations slabstats_op;
420 static int slabstats_open(struct inode *inode, struct file *file)
422 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
425 ret = seq_open(file, &slabstats_op);
427 struct seq_file *m = file->private_data;
428 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
437 static const struct file_operations proc_slabstats_operations = {
438 .open = slabstats_open,
441 .release = seq_release_private,
447 static int vmalloc_open(struct inode *inode, struct file *file)
449 unsigned int *ptr = NULL;
453 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
454 ret = seq_open(file, &vmalloc_op);
456 struct seq_file *m = file->private_data;
463 static const struct file_operations proc_vmalloc_operations = {
464 .open = vmalloc_open,
467 .release = seq_release_private,
471 #ifndef arch_irq_stat_cpu
472 #define arch_irq_stat_cpu(cpu) 0
474 #ifndef arch_irq_stat
475 #define arch_irq_stat() 0
478 static int show_stat(struct seq_file *p, void *v)
482 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
485 struct timespec boottime;
486 unsigned int per_irq_sum;
488 user = nice = system = idle = iowait =
489 irq = softirq = steal = cputime64_zero;
490 guest = cputime64_zero;
491 getboottime(&boottime);
492 jif = boottime.tv_sec;
494 for_each_possible_cpu(i) {
495 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
496 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
497 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
498 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
499 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
500 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
501 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
502 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
503 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
506 sum += kstat_irqs_cpu(j, i);
508 sum += arch_irq_stat_cpu(i);
510 sum += arch_irq_stat();
512 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
513 (unsigned long long)cputime64_to_clock_t(user),
514 (unsigned long long)cputime64_to_clock_t(nice),
515 (unsigned long long)cputime64_to_clock_t(system),
516 (unsigned long long)cputime64_to_clock_t(idle),
517 (unsigned long long)cputime64_to_clock_t(iowait),
518 (unsigned long long)cputime64_to_clock_t(irq),
519 (unsigned long long)cputime64_to_clock_t(softirq),
520 (unsigned long long)cputime64_to_clock_t(steal),
521 (unsigned long long)cputime64_to_clock_t(guest));
522 for_each_online_cpu(i) {
524 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
525 user = kstat_cpu(i).cpustat.user;
526 nice = kstat_cpu(i).cpustat.nice;
527 system = kstat_cpu(i).cpustat.system;
528 idle = kstat_cpu(i).cpustat.idle;
529 iowait = kstat_cpu(i).cpustat.iowait;
530 irq = kstat_cpu(i).cpustat.irq;
531 softirq = kstat_cpu(i).cpustat.softirq;
532 steal = kstat_cpu(i).cpustat.steal;
533 guest = kstat_cpu(i).cpustat.guest;
535 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
537 (unsigned long long)cputime64_to_clock_t(user),
538 (unsigned long long)cputime64_to_clock_t(nice),
539 (unsigned long long)cputime64_to_clock_t(system),
540 (unsigned long long)cputime64_to_clock_t(idle),
541 (unsigned long long)cputime64_to_clock_t(iowait),
542 (unsigned long long)cputime64_to_clock_t(irq),
543 (unsigned long long)cputime64_to_clock_t(softirq),
544 (unsigned long long)cputime64_to_clock_t(steal),
545 (unsigned long long)cputime64_to_clock_t(guest));
547 seq_printf(p, "intr %llu", (unsigned long long)sum);
549 /* sum again ? it could be updated? */
553 for_each_possible_cpu(i)
554 per_irq_sum += kstat_irqs_cpu(j, i);
556 seq_printf(p, " %u", per_irq_sum);
563 "procs_running %lu\n"
564 "procs_blocked %lu\n",
565 nr_context_switches(),
574 static int stat_open(struct inode *inode, struct file *file)
576 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
581 /* don't ask for more than the kmalloc() max size, currently 128 KB */
582 if (size > 128 * 1024)
584 buf = kmalloc(size, GFP_KERNEL);
588 res = single_open(file, show_stat, NULL);
590 m = file->private_data;
597 static const struct file_operations proc_stat_operations = {
601 .release = single_release,
607 static void *int_seq_start(struct seq_file *f, loff_t *pos)
609 return (*pos <= nr_irqs) ? pos : NULL;
613 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
616 return (*pos <= nr_irqs) ? pos : NULL;
619 static void int_seq_stop(struct seq_file *f, void *v)
624 static const struct seq_operations int_seq_ops = {
625 .start = int_seq_start,
626 .next = int_seq_next,
627 .stop = int_seq_stop,
628 .show = show_interrupts
631 static int interrupts_open(struct inode *inode, struct file *filp)
633 return seq_open(filp, &int_seq_ops);
636 static const struct file_operations proc_interrupts_operations = {
637 .open = interrupts_open,
640 .release = seq_release,
643 static int filesystems_read_proc(char *page, char **start, off_t off,
644 int count, int *eof, void *data)
646 int len = get_filesystem_list(page);
647 return proc_calc_metrics(page, start, off, count, eof, len);
650 static int cmdline_read_proc(char *page, char **start, off_t off,
651 int count, int *eof, void *data)
655 len = sprintf(page, "%s\n", saved_command_line);
656 return proc_calc_metrics(page, start, off, count, eof, len);
659 #ifdef CONFIG_FILE_LOCKING
660 static int locks_open(struct inode *inode, struct file *filp)
662 return seq_open(filp, &locks_seq_operations);
665 static const struct file_operations proc_locks_operations = {
669 .release = seq_release,
671 #endif /* CONFIG_FILE_LOCKING */
673 static int execdomains_read_proc(char *page, char **start, off_t off,
674 int count, int *eof, void *data)
676 int len = get_exec_domain_list(page);
677 return proc_calc_metrics(page, start, off, count, eof, len);
680 #ifdef CONFIG_PROC_PAGE_MONITOR
681 #define KPMSIZE sizeof(u64)
682 #define KPMMASK (KPMSIZE - 1)
683 /* /proc/kpagecount - an array exposing page counts
685 * Each entry is a u64 representing the corresponding
686 * physical page count.
688 static ssize_t kpagecount_read(struct file *file, char __user *buf,
689 size_t count, loff_t *ppos)
691 u64 __user *out = (u64 __user *)buf;
693 unsigned long src = *ppos;
699 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
700 if (src & KPMMASK || count & KPMMASK)
706 ppage = pfn_to_page(pfn);
711 pcount = page_mapcount(ppage);
713 if (put_user(pcount, out++)) {
721 *ppos += (char __user *)out - buf;
723 ret = (char __user *)out - buf;
727 static struct file_operations proc_kpagecount_operations = {
729 .read = kpagecount_read,
732 /* /proc/kpageflags - an array exposing page flags
734 * Each entry is a u64 representing the corresponding
735 * physical page flags.
738 /* These macros are used to decouple internal flags from exported ones */
742 #define KPF_REFERENCED 2
743 #define KPF_UPTODATE 3
748 #define KPF_WRITEBACK 8
749 #define KPF_RECLAIM 9
752 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
754 static ssize_t kpageflags_read(struct file *file, char __user *buf,
755 size_t count, loff_t *ppos)
757 u64 __user *out = (u64 __user *)buf;
759 unsigned long src = *ppos;
765 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
766 if (src & KPMMASK || count & KPMMASK)
772 ppage = pfn_to_page(pfn);
777 kflags = ppage->flags;
779 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
780 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
781 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
782 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
783 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
784 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
785 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
786 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
787 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
788 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
789 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
791 if (put_user(uflags, out++)) {
799 *ppos += (char __user *)out - buf;
801 ret = (char __user *)out - buf;
805 static struct file_operations proc_kpageflags_operations = {
807 .read = kpageflags_read,
809 #endif /* CONFIG_PROC_PAGE_MONITOR */
811 struct proc_dir_entry *proc_root_kcore;
813 void __init proc_misc_init(void)
817 int (*read_proc)(char*,char**,off_t,int,int*,void*);
818 } *p, simple_ones[] = {
819 {"meminfo", meminfo_read_proc},
820 {"version", version_read_proc},
821 #ifdef CONFIG_PROC_HARDWARE
822 {"hardware", hardware_read_proc},
824 #ifdef CONFIG_STRAM_PROC
825 {"stram", stram_read_proc},
827 {"filesystems", filesystems_read_proc},
828 {"cmdline", cmdline_read_proc},
829 {"execdomains", execdomains_read_proc},
832 for (p = simple_ones; p->name; p++)
833 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
835 proc_symlink("mounts", NULL, "self/mounts");
837 /* And now for trickier ones */
839 proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
841 #ifdef CONFIG_FILE_LOCKING
842 proc_create("locks", 0, NULL, &proc_locks_operations);
844 proc_create("devices", 0, NULL, &proc_devinfo_operations);
845 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
847 proc_create("partitions", 0, NULL, &proc_partitions_operations);
849 proc_create("stat", 0, NULL, &proc_stat_operations);
850 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
851 #ifdef CONFIG_SLABINFO
852 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
853 #ifdef CONFIG_DEBUG_SLAB_LEAK
854 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
858 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
860 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
861 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
862 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
863 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
865 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
867 #ifdef CONFIG_MODULES
868 proc_create("modules", 0, NULL, &proc_modules_operations);
870 #ifdef CONFIG_SCHEDSTATS
871 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
873 #ifdef CONFIG_PROC_KCORE
874 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
876 proc_root_kcore->size =
877 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
879 #ifdef CONFIG_PROC_PAGE_MONITOR
880 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
881 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
883 #ifdef CONFIG_PROC_VMCORE
884 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);