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 extern const struct seq_operations cpuinfo_op
;
119 static int cpuinfo_open(struct inode
*inode
, struct file
*file
)
121 return seq_open(file
, &cpuinfo_op
);
124 static const struct file_operations proc_cpuinfo_operations
= {
125 .open
= cpuinfo_open
,
128 .release
= seq_release
,
131 static int devinfo_show(struct seq_file
*f
, void *v
)
133 int i
= *(loff_t
*) v
;
135 if (i
< CHRDEV_MAJOR_HASH_SIZE
) {
137 seq_printf(f
, "Character devices:\n");
142 i
-= CHRDEV_MAJOR_HASH_SIZE
;
144 seq_printf(f
, "\nBlock devices:\n");
151 static void *devinfo_start(struct seq_file
*f
, loff_t
*pos
)
153 if (*pos
< (BLKDEV_MAJOR_HASH_SIZE
+ CHRDEV_MAJOR_HASH_SIZE
))
158 static void *devinfo_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
161 if (*pos
>= (BLKDEV_MAJOR_HASH_SIZE
+ CHRDEV_MAJOR_HASH_SIZE
))
166 static void devinfo_stop(struct seq_file
*f
, void *v
)
171 static const struct seq_operations devinfo_ops
= {
172 .start
= devinfo_start
,
173 .next
= devinfo_next
,
174 .stop
= devinfo_stop
,
178 static int devinfo_open(struct inode
*inode
, struct file
*filp
)
180 return seq_open(filp
, &devinfo_ops
);
183 static const struct file_operations proc_devinfo_operations
= {
184 .open
= devinfo_open
,
187 .release
= seq_release
,
190 static int vmstat_open(struct inode
*inode
, struct file
*file
)
192 return seq_open(file
, &vmstat_op
);
194 static const struct file_operations proc_vmstat_file_operations
= {
198 .release
= seq_release
,
201 #ifdef CONFIG_PROC_HARDWARE
202 static int hardware_read_proc(char *page
, char **start
, off_t off
,
203 int count
, int *eof
, void *data
)
205 int len
= get_hardware_list(page
);
206 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
210 #ifdef CONFIG_STRAM_PROC
211 static int stram_read_proc(char *page
, char **start
, off_t off
,
212 int count
, int *eof
, void *data
)
214 int len
= get_stram_list(page
);
215 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
220 static int partitions_open(struct inode
*inode
, struct file
*file
)
222 return seq_open(file
, &partitions_op
);
224 static const struct file_operations proc_partitions_operations
= {
225 .open
= partitions_open
,
228 .release
= seq_release
,
231 static int diskstats_open(struct inode
*inode
, struct file
*file
)
233 return seq_open(file
, &diskstats_op
);
235 static const struct file_operations proc_diskstats_operations
= {
236 .open
= diskstats_open
,
239 .release
= seq_release
,
243 #ifdef CONFIG_MODULES
244 extern const struct seq_operations modules_op
;
245 static int modules_open(struct inode
*inode
, struct file
*file
)
247 return seq_open(file
, &modules_op
);
249 static const struct file_operations proc_modules_operations
= {
250 .open
= modules_open
,
253 .release
= seq_release
,
257 #ifdef CONFIG_SLABINFO
258 static int slabinfo_open(struct inode
*inode
, struct file
*file
)
260 return seq_open(file
, &slabinfo_op
);
262 static const struct file_operations proc_slabinfo_operations
= {
263 .open
= slabinfo_open
,
265 .write
= slabinfo_write
,
267 .release
= seq_release
,
270 #ifdef CONFIG_DEBUG_SLAB_LEAK
271 extern const struct seq_operations slabstats_op
;
272 static int slabstats_open(struct inode
*inode
, struct file
*file
)
274 unsigned long *n
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
277 ret
= seq_open(file
, &slabstats_op
);
279 struct seq_file
*m
= file
->private_data
;
280 *n
= PAGE_SIZE
/ (2 * sizeof(unsigned long));
289 static const struct file_operations proc_slabstats_operations
= {
290 .open
= slabstats_open
,
293 .release
= seq_release_private
,
299 static int vmalloc_open(struct inode
*inode
, struct file
*file
)
301 unsigned int *ptr
= NULL
;
305 ptr
= kmalloc(nr_node_ids
* sizeof(unsigned int), GFP_KERNEL
);
306 ret
= seq_open(file
, &vmalloc_op
);
308 struct seq_file
*m
= file
->private_data
;
315 static const struct file_operations proc_vmalloc_operations
= {
316 .open
= vmalloc_open
,
319 .release
= seq_release_private
,
323 #ifndef arch_irq_stat_cpu
324 #define arch_irq_stat_cpu(cpu) 0
326 #ifndef arch_irq_stat
327 #define arch_irq_stat() 0
330 static int show_stat(struct seq_file
*p
, void *v
)
334 cputime64_t user
, nice
, system
, idle
, iowait
, irq
, softirq
, steal
;
337 struct timespec boottime
;
338 unsigned int per_irq_sum
;
340 user
= nice
= system
= idle
= iowait
=
341 irq
= softirq
= steal
= cputime64_zero
;
342 guest
= cputime64_zero
;
343 getboottime(&boottime
);
344 jif
= boottime
.tv_sec
;
346 for_each_possible_cpu(i
) {
347 user
= cputime64_add(user
, kstat_cpu(i
).cpustat
.user
);
348 nice
= cputime64_add(nice
, kstat_cpu(i
).cpustat
.nice
);
349 system
= cputime64_add(system
, kstat_cpu(i
).cpustat
.system
);
350 idle
= cputime64_add(idle
, kstat_cpu(i
).cpustat
.idle
);
351 iowait
= cputime64_add(iowait
, kstat_cpu(i
).cpustat
.iowait
);
352 irq
= cputime64_add(irq
, kstat_cpu(i
).cpustat
.irq
);
353 softirq
= cputime64_add(softirq
, kstat_cpu(i
).cpustat
.softirq
);
354 steal
= cputime64_add(steal
, kstat_cpu(i
).cpustat
.steal
);
355 guest
= cputime64_add(guest
, kstat_cpu(i
).cpustat
.guest
);
358 sum
+= kstat_irqs_cpu(j
, i
);
360 sum
+= arch_irq_stat_cpu(i
);
362 sum
+= arch_irq_stat();
364 seq_printf(p
, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
365 (unsigned long long)cputime64_to_clock_t(user
),
366 (unsigned long long)cputime64_to_clock_t(nice
),
367 (unsigned long long)cputime64_to_clock_t(system
),
368 (unsigned long long)cputime64_to_clock_t(idle
),
369 (unsigned long long)cputime64_to_clock_t(iowait
),
370 (unsigned long long)cputime64_to_clock_t(irq
),
371 (unsigned long long)cputime64_to_clock_t(softirq
),
372 (unsigned long long)cputime64_to_clock_t(steal
),
373 (unsigned long long)cputime64_to_clock_t(guest
));
374 for_each_online_cpu(i
) {
376 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
377 user
= kstat_cpu(i
).cpustat
.user
;
378 nice
= kstat_cpu(i
).cpustat
.nice
;
379 system
= kstat_cpu(i
).cpustat
.system
;
380 idle
= kstat_cpu(i
).cpustat
.idle
;
381 iowait
= kstat_cpu(i
).cpustat
.iowait
;
382 irq
= kstat_cpu(i
).cpustat
.irq
;
383 softirq
= kstat_cpu(i
).cpustat
.softirq
;
384 steal
= kstat_cpu(i
).cpustat
.steal
;
385 guest
= kstat_cpu(i
).cpustat
.guest
;
387 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
389 (unsigned long long)cputime64_to_clock_t(user
),
390 (unsigned long long)cputime64_to_clock_t(nice
),
391 (unsigned long long)cputime64_to_clock_t(system
),
392 (unsigned long long)cputime64_to_clock_t(idle
),
393 (unsigned long long)cputime64_to_clock_t(iowait
),
394 (unsigned long long)cputime64_to_clock_t(irq
),
395 (unsigned long long)cputime64_to_clock_t(softirq
),
396 (unsigned long long)cputime64_to_clock_t(steal
),
397 (unsigned long long)cputime64_to_clock_t(guest
));
399 seq_printf(p
, "intr %llu", (unsigned long long)sum
);
401 /* sum again ? it could be updated? */
405 for_each_possible_cpu(i
)
406 per_irq_sum
+= kstat_irqs_cpu(j
, i
);
408 seq_printf(p
, " %u", per_irq_sum
);
415 "procs_running %lu\n"
416 "procs_blocked %lu\n",
417 nr_context_switches(),
426 static int stat_open(struct inode
*inode
, struct file
*file
)
428 unsigned size
= 4096 * (1 + num_possible_cpus() / 32);
433 /* don't ask for more than the kmalloc() max size, currently 128 KB */
434 if (size
> 128 * 1024)
436 buf
= kmalloc(size
, GFP_KERNEL
);
440 res
= single_open(file
, show_stat
, NULL
);
442 m
= file
->private_data
;
449 static const struct file_operations proc_stat_operations
= {
453 .release
= single_release
,
459 static void *int_seq_start(struct seq_file
*f
, loff_t
*pos
)
461 return (*pos
<= nr_irqs
) ? pos
: NULL
;
465 static void *int_seq_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
468 return (*pos
<= nr_irqs
) ? pos
: NULL
;
471 static void int_seq_stop(struct seq_file
*f
, void *v
)
476 static const struct seq_operations int_seq_ops
= {
477 .start
= int_seq_start
,
478 .next
= int_seq_next
,
479 .stop
= int_seq_stop
,
480 .show
= show_interrupts
483 static int interrupts_open(struct inode
*inode
, struct file
*filp
)
485 return seq_open(filp
, &int_seq_ops
);
488 static const struct file_operations proc_interrupts_operations
= {
489 .open
= interrupts_open
,
492 .release
= seq_release
,
495 static int filesystems_read_proc(char *page
, char **start
, off_t off
,
496 int count
, int *eof
, void *data
)
498 int len
= get_filesystem_list(page
);
499 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
502 static int cmdline_read_proc(char *page
, char **start
, off_t off
,
503 int count
, int *eof
, void *data
)
507 len
= sprintf(page
, "%s\n", saved_command_line
);
508 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
511 #ifdef CONFIG_FILE_LOCKING
512 static int locks_open(struct inode
*inode
, struct file
*filp
)
514 return seq_open(filp
, &locks_seq_operations
);
517 static const struct file_operations proc_locks_operations
= {
521 .release
= seq_release
,
523 #endif /* CONFIG_FILE_LOCKING */
525 static int execdomains_read_proc(char *page
, char **start
, off_t off
,
526 int count
, int *eof
, void *data
)
528 int len
= get_exec_domain_list(page
);
529 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
532 #ifdef CONFIG_PROC_PAGE_MONITOR
533 #define KPMSIZE sizeof(u64)
534 #define KPMMASK (KPMSIZE - 1)
535 /* /proc/kpagecount - an array exposing page counts
537 * Each entry is a u64 representing the corresponding
538 * physical page count.
540 static ssize_t
kpagecount_read(struct file
*file
, char __user
*buf
,
541 size_t count
, loff_t
*ppos
)
543 u64 __user
*out
= (u64 __user
*)buf
;
545 unsigned long src
= *ppos
;
551 count
= min_t(size_t, count
, (max_pfn
* KPMSIZE
) - src
);
552 if (src
& KPMMASK
|| count
& KPMMASK
)
558 ppage
= pfn_to_page(pfn
);
563 pcount
= page_mapcount(ppage
);
565 if (put_user(pcount
, out
++)) {
573 *ppos
+= (char __user
*)out
- buf
;
575 ret
= (char __user
*)out
- buf
;
579 static struct file_operations proc_kpagecount_operations
= {
581 .read
= kpagecount_read
,
584 /* /proc/kpageflags - an array exposing page flags
586 * Each entry is a u64 representing the corresponding
587 * physical page flags.
590 /* These macros are used to decouple internal flags from exported ones */
594 #define KPF_REFERENCED 2
595 #define KPF_UPTODATE 3
600 #define KPF_WRITEBACK 8
601 #define KPF_RECLAIM 9
604 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
606 static ssize_t
kpageflags_read(struct file
*file
, char __user
*buf
,
607 size_t count
, loff_t
*ppos
)
609 u64 __user
*out
= (u64 __user
*)buf
;
611 unsigned long src
= *ppos
;
617 count
= min_t(unsigned long, count
, (max_pfn
* KPMSIZE
) - src
);
618 if (src
& KPMMASK
|| count
& KPMMASK
)
624 ppage
= pfn_to_page(pfn
);
629 kflags
= ppage
->flags
;
631 uflags
= kpf_copy_bit(KPF_LOCKED
, PG_locked
, kflags
) |
632 kpf_copy_bit(kflags
, KPF_ERROR
, PG_error
) |
633 kpf_copy_bit(kflags
, KPF_REFERENCED
, PG_referenced
) |
634 kpf_copy_bit(kflags
, KPF_UPTODATE
, PG_uptodate
) |
635 kpf_copy_bit(kflags
, KPF_DIRTY
, PG_dirty
) |
636 kpf_copy_bit(kflags
, KPF_LRU
, PG_lru
) |
637 kpf_copy_bit(kflags
, KPF_ACTIVE
, PG_active
) |
638 kpf_copy_bit(kflags
, KPF_SLAB
, PG_slab
) |
639 kpf_copy_bit(kflags
, KPF_WRITEBACK
, PG_writeback
) |
640 kpf_copy_bit(kflags
, KPF_RECLAIM
, PG_reclaim
) |
641 kpf_copy_bit(kflags
, KPF_BUDDY
, PG_buddy
);
643 if (put_user(uflags
, out
++)) {
651 *ppos
+= (char __user
*)out
- buf
;
653 ret
= (char __user
*)out
- buf
;
657 static struct file_operations proc_kpageflags_operations
= {
659 .read
= kpageflags_read
,
661 #endif /* CONFIG_PROC_PAGE_MONITOR */
663 struct proc_dir_entry
*proc_root_kcore
;
665 void __init
proc_misc_init(void)
669 int (*read_proc
)(char*,char**,off_t
,int,int*,void*);
670 } *p
, simple_ones
[] = {
671 #ifdef CONFIG_PROC_HARDWARE
672 {"hardware", hardware_read_proc
},
674 #ifdef CONFIG_STRAM_PROC
675 {"stram", stram_read_proc
},
677 {"filesystems", filesystems_read_proc
},
678 {"cmdline", cmdline_read_proc
},
679 {"execdomains", execdomains_read_proc
},
682 for (p
= simple_ones
; p
->name
; p
++)
683 create_proc_read_entry(p
->name
, 0, NULL
, p
->read_proc
, NULL
);
685 proc_symlink("mounts", NULL
, "self/mounts");
687 /* And now for trickier ones */
689 proc_create("kmsg", S_IRUSR
, NULL
, &proc_kmsg_operations
);
691 #ifdef CONFIG_FILE_LOCKING
692 proc_create("locks", 0, NULL
, &proc_locks_operations
);
694 proc_create("devices", 0, NULL
, &proc_devinfo_operations
);
695 proc_create("cpuinfo", 0, NULL
, &proc_cpuinfo_operations
);
697 proc_create("partitions", 0, NULL
, &proc_partitions_operations
);
699 proc_create("stat", 0, NULL
, &proc_stat_operations
);
700 proc_create("interrupts", 0, NULL
, &proc_interrupts_operations
);
701 #ifdef CONFIG_SLABINFO
702 proc_create("slabinfo",S_IWUSR
|S_IRUGO
,NULL
,&proc_slabinfo_operations
);
703 #ifdef CONFIG_DEBUG_SLAB_LEAK
704 proc_create("slab_allocators", 0, NULL
, &proc_slabstats_operations
);
708 proc_create("vmallocinfo", S_IRUSR
, NULL
, &proc_vmalloc_operations
);
710 proc_create("buddyinfo", S_IRUGO
, NULL
, &fragmentation_file_operations
);
711 proc_create("pagetypeinfo", S_IRUGO
, NULL
, &pagetypeinfo_file_ops
);
712 proc_create("vmstat", S_IRUGO
, NULL
, &proc_vmstat_file_operations
);
713 proc_create("zoneinfo", S_IRUGO
, NULL
, &proc_zoneinfo_file_operations
);
715 proc_create("diskstats", 0, NULL
, &proc_diskstats_operations
);
717 #ifdef CONFIG_MODULES
718 proc_create("modules", 0, NULL
, &proc_modules_operations
);
720 #ifdef CONFIG_SCHEDSTATS
721 proc_create("schedstat", 0, NULL
, &proc_schedstat_operations
);
723 #ifdef CONFIG_PROC_KCORE
724 proc_root_kcore
= proc_create("kcore", S_IRUSR
, NULL
, &proc_kcore_operations
);
726 proc_root_kcore
->size
=
727 (size_t)high_memory
- PAGE_OFFSET
+ PAGE_SIZE
;
729 #ifdef CONFIG_PROC_PAGE_MONITOR
730 proc_create("kpagecount", S_IRUSR
, NULL
, &proc_kpagecount_operations
);
731 proc_create("kpageflags", S_IRUSR
, NULL
, &proc_kpageflags_operations
);
733 #ifdef CONFIG_PROC_VMCORE
734 proc_vmcore
= proc_create("vmcore", S_IRUSR
, NULL
, &proc_vmcore_operations
);