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 static int fragmentation_open(struct inode
*inode
, struct file
*file
)
84 return seq_open(file
, &fragmentation_op
);
87 static const struct file_operations fragmentation_file_operations
= {
88 .open
= fragmentation_open
,
91 .release
= seq_release
,
94 static int pagetypeinfo_open(struct inode
*inode
, struct file
*file
)
96 return seq_open(file
, &pagetypeinfo_op
);
99 static const struct file_operations pagetypeinfo_file_ops
= {
100 .open
= pagetypeinfo_open
,
103 .release
= seq_release
,
106 static int zoneinfo_open(struct inode
*inode
, struct file
*file
)
108 return seq_open(file
, &zoneinfo_op
);
111 static const struct file_operations proc_zoneinfo_file_operations
= {
112 .open
= zoneinfo_open
,
115 .release
= seq_release
,
118 static int version_read_proc(char *page
, char **start
, off_t off
,
119 int count
, int *eof
, void *data
)
123 len
= snprintf(page
, PAGE_SIZE
, linux_proc_banner
,
127 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
130 extern const struct seq_operations cpuinfo_op
;
131 static int cpuinfo_open(struct inode
*inode
, struct file
*file
)
133 return seq_open(file
, &cpuinfo_op
);
136 static const struct file_operations proc_cpuinfo_operations
= {
137 .open
= cpuinfo_open
,
140 .release
= seq_release
,
143 static int devinfo_show(struct seq_file
*f
, void *v
)
145 int i
= *(loff_t
*) v
;
147 if (i
< CHRDEV_MAJOR_HASH_SIZE
) {
149 seq_printf(f
, "Character devices:\n");
154 i
-= CHRDEV_MAJOR_HASH_SIZE
;
156 seq_printf(f
, "\nBlock devices:\n");
163 static void *devinfo_start(struct seq_file
*f
, loff_t
*pos
)
165 if (*pos
< (BLKDEV_MAJOR_HASH_SIZE
+ CHRDEV_MAJOR_HASH_SIZE
))
170 static void *devinfo_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
173 if (*pos
>= (BLKDEV_MAJOR_HASH_SIZE
+ CHRDEV_MAJOR_HASH_SIZE
))
178 static void devinfo_stop(struct seq_file
*f
, void *v
)
183 static const struct seq_operations devinfo_ops
= {
184 .start
= devinfo_start
,
185 .next
= devinfo_next
,
186 .stop
= devinfo_stop
,
190 static int devinfo_open(struct inode
*inode
, struct file
*filp
)
192 return seq_open(filp
, &devinfo_ops
);
195 static const struct file_operations proc_devinfo_operations
= {
196 .open
= devinfo_open
,
199 .release
= seq_release
,
202 static int vmstat_open(struct inode
*inode
, struct file
*file
)
204 return seq_open(file
, &vmstat_op
);
206 static const struct file_operations proc_vmstat_file_operations
= {
210 .release
= seq_release
,
213 #ifdef CONFIG_PROC_HARDWARE
214 static int hardware_read_proc(char *page
, char **start
, off_t off
,
215 int count
, int *eof
, void *data
)
217 int len
= get_hardware_list(page
);
218 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
222 #ifdef CONFIG_STRAM_PROC
223 static int stram_read_proc(char *page
, char **start
, off_t off
,
224 int count
, int *eof
, void *data
)
226 int len
= get_stram_list(page
);
227 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
232 static int partitions_open(struct inode
*inode
, struct file
*file
)
234 return seq_open(file
, &partitions_op
);
236 static const struct file_operations proc_partitions_operations
= {
237 .open
= partitions_open
,
240 .release
= seq_release
,
243 static int diskstats_open(struct inode
*inode
, struct file
*file
)
245 return seq_open(file
, &diskstats_op
);
247 static const struct file_operations proc_diskstats_operations
= {
248 .open
= diskstats_open
,
251 .release
= seq_release
,
255 #ifdef CONFIG_MODULES
256 extern const struct seq_operations modules_op
;
257 static int modules_open(struct inode
*inode
, struct file
*file
)
259 return seq_open(file
, &modules_op
);
261 static const struct file_operations proc_modules_operations
= {
262 .open
= modules_open
,
265 .release
= seq_release
,
269 #ifdef CONFIG_SLABINFO
270 static int slabinfo_open(struct inode
*inode
, struct file
*file
)
272 return seq_open(file
, &slabinfo_op
);
274 static const struct file_operations proc_slabinfo_operations
= {
275 .open
= slabinfo_open
,
277 .write
= slabinfo_write
,
279 .release
= seq_release
,
282 #ifdef CONFIG_DEBUG_SLAB_LEAK
283 extern const struct seq_operations slabstats_op
;
284 static int slabstats_open(struct inode
*inode
, struct file
*file
)
286 unsigned long *n
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
289 ret
= seq_open(file
, &slabstats_op
);
291 struct seq_file
*m
= file
->private_data
;
292 *n
= PAGE_SIZE
/ (2 * sizeof(unsigned long));
301 static const struct file_operations proc_slabstats_operations
= {
302 .open
= slabstats_open
,
305 .release
= seq_release_private
,
311 static int vmalloc_open(struct inode
*inode
, struct file
*file
)
313 unsigned int *ptr
= NULL
;
317 ptr
= kmalloc(nr_node_ids
* sizeof(unsigned int), GFP_KERNEL
);
318 ret
= seq_open(file
, &vmalloc_op
);
320 struct seq_file
*m
= file
->private_data
;
327 static const struct file_operations proc_vmalloc_operations
= {
328 .open
= vmalloc_open
,
331 .release
= seq_release_private
,
335 #ifndef arch_irq_stat_cpu
336 #define arch_irq_stat_cpu(cpu) 0
338 #ifndef arch_irq_stat
339 #define arch_irq_stat() 0
342 static int show_stat(struct seq_file
*p
, void *v
)
346 cputime64_t user
, nice
, system
, idle
, iowait
, irq
, softirq
, steal
;
349 struct timespec boottime
;
350 unsigned int per_irq_sum
;
352 user
= nice
= system
= idle
= iowait
=
353 irq
= softirq
= steal
= cputime64_zero
;
354 guest
= cputime64_zero
;
355 getboottime(&boottime
);
356 jif
= boottime
.tv_sec
;
358 for_each_possible_cpu(i
) {
359 user
= cputime64_add(user
, kstat_cpu(i
).cpustat
.user
);
360 nice
= cputime64_add(nice
, kstat_cpu(i
).cpustat
.nice
);
361 system
= cputime64_add(system
, kstat_cpu(i
).cpustat
.system
);
362 idle
= cputime64_add(idle
, kstat_cpu(i
).cpustat
.idle
);
363 iowait
= cputime64_add(iowait
, kstat_cpu(i
).cpustat
.iowait
);
364 irq
= cputime64_add(irq
, kstat_cpu(i
).cpustat
.irq
);
365 softirq
= cputime64_add(softirq
, kstat_cpu(i
).cpustat
.softirq
);
366 steal
= cputime64_add(steal
, kstat_cpu(i
).cpustat
.steal
);
367 guest
= cputime64_add(guest
, kstat_cpu(i
).cpustat
.guest
);
370 sum
+= kstat_irqs_cpu(j
, i
);
372 sum
+= arch_irq_stat_cpu(i
);
374 sum
+= arch_irq_stat();
376 seq_printf(p
, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
377 (unsigned long long)cputime64_to_clock_t(user
),
378 (unsigned long long)cputime64_to_clock_t(nice
),
379 (unsigned long long)cputime64_to_clock_t(system
),
380 (unsigned long long)cputime64_to_clock_t(idle
),
381 (unsigned long long)cputime64_to_clock_t(iowait
),
382 (unsigned long long)cputime64_to_clock_t(irq
),
383 (unsigned long long)cputime64_to_clock_t(softirq
),
384 (unsigned long long)cputime64_to_clock_t(steal
),
385 (unsigned long long)cputime64_to_clock_t(guest
));
386 for_each_online_cpu(i
) {
388 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
389 user
= kstat_cpu(i
).cpustat
.user
;
390 nice
= kstat_cpu(i
).cpustat
.nice
;
391 system
= kstat_cpu(i
).cpustat
.system
;
392 idle
= kstat_cpu(i
).cpustat
.idle
;
393 iowait
= kstat_cpu(i
).cpustat
.iowait
;
394 irq
= kstat_cpu(i
).cpustat
.irq
;
395 softirq
= kstat_cpu(i
).cpustat
.softirq
;
396 steal
= kstat_cpu(i
).cpustat
.steal
;
397 guest
= kstat_cpu(i
).cpustat
.guest
;
399 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
401 (unsigned long long)cputime64_to_clock_t(user
),
402 (unsigned long long)cputime64_to_clock_t(nice
),
403 (unsigned long long)cputime64_to_clock_t(system
),
404 (unsigned long long)cputime64_to_clock_t(idle
),
405 (unsigned long long)cputime64_to_clock_t(iowait
),
406 (unsigned long long)cputime64_to_clock_t(irq
),
407 (unsigned long long)cputime64_to_clock_t(softirq
),
408 (unsigned long long)cputime64_to_clock_t(steal
),
409 (unsigned long long)cputime64_to_clock_t(guest
));
411 seq_printf(p
, "intr %llu", (unsigned long long)sum
);
413 /* sum again ? it could be updated? */
417 for_each_possible_cpu(i
)
418 per_irq_sum
+= kstat_irqs_cpu(j
, i
);
420 seq_printf(p
, " %u", per_irq_sum
);
427 "procs_running %lu\n"
428 "procs_blocked %lu\n",
429 nr_context_switches(),
438 static int stat_open(struct inode
*inode
, struct file
*file
)
440 unsigned size
= 4096 * (1 + num_possible_cpus() / 32);
445 /* don't ask for more than the kmalloc() max size, currently 128 KB */
446 if (size
> 128 * 1024)
448 buf
= kmalloc(size
, GFP_KERNEL
);
452 res
= single_open(file
, show_stat
, NULL
);
454 m
= file
->private_data
;
461 static const struct file_operations proc_stat_operations
= {
465 .release
= single_release
,
471 static void *int_seq_start(struct seq_file
*f
, loff_t
*pos
)
473 return (*pos
<= nr_irqs
) ? pos
: NULL
;
477 static void *int_seq_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
480 return (*pos
<= nr_irqs
) ? pos
: NULL
;
483 static void int_seq_stop(struct seq_file
*f
, void *v
)
488 static const struct seq_operations int_seq_ops
= {
489 .start
= int_seq_start
,
490 .next
= int_seq_next
,
491 .stop
= int_seq_stop
,
492 .show
= show_interrupts
495 static int interrupts_open(struct inode
*inode
, struct file
*filp
)
497 return seq_open(filp
, &int_seq_ops
);
500 static const struct file_operations proc_interrupts_operations
= {
501 .open
= interrupts_open
,
504 .release
= seq_release
,
507 static int filesystems_read_proc(char *page
, char **start
, off_t off
,
508 int count
, int *eof
, void *data
)
510 int len
= get_filesystem_list(page
);
511 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
514 static int cmdline_read_proc(char *page
, char **start
, off_t off
,
515 int count
, int *eof
, void *data
)
519 len
= sprintf(page
, "%s\n", saved_command_line
);
520 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
523 #ifdef CONFIG_FILE_LOCKING
524 static int locks_open(struct inode
*inode
, struct file
*filp
)
526 return seq_open(filp
, &locks_seq_operations
);
529 static const struct file_operations proc_locks_operations
= {
533 .release
= seq_release
,
535 #endif /* CONFIG_FILE_LOCKING */
537 static int execdomains_read_proc(char *page
, char **start
, off_t off
,
538 int count
, int *eof
, void *data
)
540 int len
= get_exec_domain_list(page
);
541 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
544 #ifdef CONFIG_PROC_PAGE_MONITOR
545 #define KPMSIZE sizeof(u64)
546 #define KPMMASK (KPMSIZE - 1)
547 /* /proc/kpagecount - an array exposing page counts
549 * Each entry is a u64 representing the corresponding
550 * physical page count.
552 static ssize_t
kpagecount_read(struct file
*file
, char __user
*buf
,
553 size_t count
, loff_t
*ppos
)
555 u64 __user
*out
= (u64 __user
*)buf
;
557 unsigned long src
= *ppos
;
563 count
= min_t(size_t, count
, (max_pfn
* KPMSIZE
) - src
);
564 if (src
& KPMMASK
|| count
& KPMMASK
)
570 ppage
= pfn_to_page(pfn
);
575 pcount
= page_mapcount(ppage
);
577 if (put_user(pcount
, out
++)) {
585 *ppos
+= (char __user
*)out
- buf
;
587 ret
= (char __user
*)out
- buf
;
591 static struct file_operations proc_kpagecount_operations
= {
593 .read
= kpagecount_read
,
596 /* /proc/kpageflags - an array exposing page flags
598 * Each entry is a u64 representing the corresponding
599 * physical page flags.
602 /* These macros are used to decouple internal flags from exported ones */
606 #define KPF_REFERENCED 2
607 #define KPF_UPTODATE 3
612 #define KPF_WRITEBACK 8
613 #define KPF_RECLAIM 9
616 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
618 static ssize_t
kpageflags_read(struct file
*file
, char __user
*buf
,
619 size_t count
, loff_t
*ppos
)
621 u64 __user
*out
= (u64 __user
*)buf
;
623 unsigned long src
= *ppos
;
629 count
= min_t(unsigned long, count
, (max_pfn
* KPMSIZE
) - src
);
630 if (src
& KPMMASK
|| count
& KPMMASK
)
636 ppage
= pfn_to_page(pfn
);
641 kflags
= ppage
->flags
;
643 uflags
= kpf_copy_bit(KPF_LOCKED
, PG_locked
, kflags
) |
644 kpf_copy_bit(kflags
, KPF_ERROR
, PG_error
) |
645 kpf_copy_bit(kflags
, KPF_REFERENCED
, PG_referenced
) |
646 kpf_copy_bit(kflags
, KPF_UPTODATE
, PG_uptodate
) |
647 kpf_copy_bit(kflags
, KPF_DIRTY
, PG_dirty
) |
648 kpf_copy_bit(kflags
, KPF_LRU
, PG_lru
) |
649 kpf_copy_bit(kflags
, KPF_ACTIVE
, PG_active
) |
650 kpf_copy_bit(kflags
, KPF_SLAB
, PG_slab
) |
651 kpf_copy_bit(kflags
, KPF_WRITEBACK
, PG_writeback
) |
652 kpf_copy_bit(kflags
, KPF_RECLAIM
, PG_reclaim
) |
653 kpf_copy_bit(kflags
, KPF_BUDDY
, PG_buddy
);
655 if (put_user(uflags
, out
++)) {
663 *ppos
+= (char __user
*)out
- buf
;
665 ret
= (char __user
*)out
- buf
;
669 static struct file_operations proc_kpageflags_operations
= {
671 .read
= kpageflags_read
,
673 #endif /* CONFIG_PROC_PAGE_MONITOR */
675 struct proc_dir_entry
*proc_root_kcore
;
677 void __init
proc_misc_init(void)
681 int (*read_proc
)(char*,char**,off_t
,int,int*,void*);
682 } *p
, simple_ones
[] = {
683 {"version", version_read_proc
},
684 #ifdef CONFIG_PROC_HARDWARE
685 {"hardware", hardware_read_proc
},
687 #ifdef CONFIG_STRAM_PROC
688 {"stram", stram_read_proc
},
690 {"filesystems", filesystems_read_proc
},
691 {"cmdline", cmdline_read_proc
},
692 {"execdomains", execdomains_read_proc
},
695 for (p
= simple_ones
; p
->name
; p
++)
696 create_proc_read_entry(p
->name
, 0, NULL
, p
->read_proc
, NULL
);
698 proc_symlink("mounts", NULL
, "self/mounts");
700 /* And now for trickier ones */
702 proc_create("kmsg", S_IRUSR
, NULL
, &proc_kmsg_operations
);
704 #ifdef CONFIG_FILE_LOCKING
705 proc_create("locks", 0, NULL
, &proc_locks_operations
);
707 proc_create("devices", 0, NULL
, &proc_devinfo_operations
);
708 proc_create("cpuinfo", 0, NULL
, &proc_cpuinfo_operations
);
710 proc_create("partitions", 0, NULL
, &proc_partitions_operations
);
712 proc_create("stat", 0, NULL
, &proc_stat_operations
);
713 proc_create("interrupts", 0, NULL
, &proc_interrupts_operations
);
714 #ifdef CONFIG_SLABINFO
715 proc_create("slabinfo",S_IWUSR
|S_IRUGO
,NULL
,&proc_slabinfo_operations
);
716 #ifdef CONFIG_DEBUG_SLAB_LEAK
717 proc_create("slab_allocators", 0, NULL
, &proc_slabstats_operations
);
721 proc_create("vmallocinfo", S_IRUSR
, NULL
, &proc_vmalloc_operations
);
723 proc_create("buddyinfo", S_IRUGO
, NULL
, &fragmentation_file_operations
);
724 proc_create("pagetypeinfo", S_IRUGO
, NULL
, &pagetypeinfo_file_ops
);
725 proc_create("vmstat", S_IRUGO
, NULL
, &proc_vmstat_file_operations
);
726 proc_create("zoneinfo", S_IRUGO
, NULL
, &proc_zoneinfo_file_operations
);
728 proc_create("diskstats", 0, NULL
, &proc_diskstats_operations
);
730 #ifdef CONFIG_MODULES
731 proc_create("modules", 0, NULL
, &proc_modules_operations
);
733 #ifdef CONFIG_SCHEDSTATS
734 proc_create("schedstat", 0, NULL
, &proc_schedstat_operations
);
736 #ifdef CONFIG_PROC_KCORE
737 proc_root_kcore
= proc_create("kcore", S_IRUSR
, NULL
, &proc_kcore_operations
);
739 proc_root_kcore
->size
=
740 (size_t)high_memory
- PAGE_OFFSET
+ PAGE_SIZE
;
742 #ifdef CONFIG_PROC_PAGE_MONITOR
743 proc_create("kpagecount", S_IRUSR
, NULL
, &proc_kpagecount_operations
);
744 proc_create("kpageflags", S_IRUSR
, NULL
, &proc_kpageflags_operations
);
746 #ifdef CONFIG_PROC_VMCORE
747 proc_vmcore
= proc_create("vmcore", S_IRUSR
, NULL
, &proc_vmcore_operations
);