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/proc_fs.h>
28 #include <linux/ioport.h>
30 #include <linux/mmzone.h>
31 #include <linux/pagemap.h>
32 #include <linux/swap.h>
33 #include <linux/slab.h>
34 #include <linux/smp.h>
35 #include <linux/signal.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/smp_lock.h>
39 #include <linux/seq_file.h>
40 #include <linux/times.h>
41 #include <linux/profile.h>
42 #include <linux/utsname.h>
43 #include <linux/blkdev.h>
44 #include <linux/hugetlb.h>
45 #include <linux/jiffies.h>
46 #include <linux/sysrq.h>
47 #include <linux/vmalloc.h>
48 #include <linux/crash_dump.h>
49 #include <linux/pid_namespace.h>
50 #include <asm/uaccess.h>
51 #include <asm/pgtable.h>
54 #include <asm/div64.h>
57 #define LOAD_INT(x) ((x) >> FSHIFT)
58 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
60 * Warning: stuff below (imported functions) assumes that its output will fit
61 * into one page. For some of those functions it may be wrong. Moreover, we
62 * have a way to deal with that gracefully. Right now I used straightforward
63 * wrappers, but this needs further analysis wrt potential overflows.
65 extern int get_hardware_list(char *);
66 extern int get_stram_list(char *);
67 extern int get_filesystem_list(char *);
68 extern int get_exec_domain_list(char *);
69 extern int get_dma_list(char *);
70 extern int get_locks_status (char *, char **, off_t
, int);
72 static int proc_calc_metrics(char *page
, char **start
, off_t off
,
73 int count
, int *eof
, int len
)
75 if (len
<= off
+count
) *eof
= 1;
78 if (len
>count
) len
= count
;
83 static int loadavg_read_proc(char *page
, char **start
, off_t off
,
84 int count
, int *eof
, void *data
)
89 a
= avenrun
[0] + (FIXED_1
/200);
90 b
= avenrun
[1] + (FIXED_1
/200);
91 c
= avenrun
[2] + (FIXED_1
/200);
92 len
= sprintf(page
,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
93 LOAD_INT(a
), LOAD_FRAC(a
),
94 LOAD_INT(b
), LOAD_FRAC(b
),
95 LOAD_INT(c
), LOAD_FRAC(c
),
96 nr_running(), nr_threads
, current
->nsproxy
->pid_ns
->last_pid
);
97 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
100 static int uptime_read_proc(char *page
, char **start
, off_t off
,
101 int count
, int *eof
, void *data
)
103 struct timespec uptime
;
104 struct timespec idle
;
106 cputime_t idletime
= cputime_add(init_task
.utime
, init_task
.stime
);
108 do_posix_clock_monotonic_gettime(&uptime
);
109 cputime_to_timespec(idletime
, &idle
);
110 len
= sprintf(page
,"%lu.%02lu %lu.%02lu\n",
111 (unsigned long) uptime
.tv_sec
,
112 (uptime
.tv_nsec
/ (NSEC_PER_SEC
/ 100)),
113 (unsigned long) idle
.tv_sec
,
114 (idle
.tv_nsec
/ (NSEC_PER_SEC
/ 100)));
116 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
119 static int meminfo_read_proc(char *page
, char **start
, off_t off
,
120 int count
, int *eof
, void *data
)
124 unsigned long inactive
;
125 unsigned long active
;
127 unsigned long committed
;
128 unsigned long allowed
;
129 struct vmalloc_info vmi
;
132 get_zone_counts(&active
, &inactive
, &free
);
135 * display in kilobytes.
137 #define K(x) ((x) << (PAGE_SHIFT - 10))
140 committed
= atomic_read(&vm_committed_space
);
141 allowed
= ((totalram_pages
- hugetlb_total_pages())
142 * sysctl_overcommit_ratio
/ 100) + total_swap_pages
;
144 cached
= global_page_state(NR_FILE_PAGES
) -
145 total_swapcache_pages
- i
.bufferram
;
149 get_vmalloc_info(&vmi
);
152 * Tagged format, for easy grepping and expansion.
155 "MemTotal: %8lu kB\n"
159 "SwapCached: %8lu kB\n"
161 "Inactive: %8lu kB\n"
162 #ifdef CONFIG_HIGHMEM
163 "HighTotal: %8lu kB\n"
164 "HighFree: %8lu kB\n"
165 "LowTotal: %8lu kB\n"
168 "SwapTotal: %8lu kB\n"
169 "SwapFree: %8lu kB\n"
171 "Writeback: %8lu kB\n"
172 "AnonPages: %8lu kB\n"
175 "SReclaimable: %8lu kB\n"
176 "SUnreclaim: %8lu kB\n"
177 "PageTables: %8lu kB\n"
178 "NFS_Unstable: %8lu kB\n"
180 "CommitLimit: %8lu kB\n"
181 "Committed_AS: %8lu kB\n"
182 "VmallocTotal: %8lu kB\n"
183 "VmallocUsed: %8lu kB\n"
184 "VmallocChunk: %8lu kB\n",
189 K(total_swapcache_pages
),
192 #ifdef CONFIG_HIGHMEM
195 K(i
.totalram
-i
.totalhigh
),
196 K(i
.freeram
-i
.freehigh
),
200 K(global_page_state(NR_FILE_DIRTY
)),
201 K(global_page_state(NR_WRITEBACK
)),
202 K(global_page_state(NR_ANON_PAGES
)),
203 K(global_page_state(NR_FILE_MAPPED
)),
204 K(global_page_state(NR_SLAB_RECLAIMABLE
) +
205 global_page_state(NR_SLAB_UNRECLAIMABLE
)),
206 K(global_page_state(NR_SLAB_RECLAIMABLE
)),
207 K(global_page_state(NR_SLAB_UNRECLAIMABLE
)),
208 K(global_page_state(NR_PAGETABLE
)),
209 K(global_page_state(NR_UNSTABLE_NFS
)),
210 K(global_page_state(NR_BOUNCE
)),
213 (unsigned long)VMALLOC_TOTAL
>> 10,
215 vmi
.largest_chunk
>> 10
218 len
+= hugetlb_report_meminfo(page
+ len
);
220 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
224 extern struct seq_operations fragmentation_op
;
225 static int fragmentation_open(struct inode
*inode
, struct file
*file
)
228 return seq_open(file
, &fragmentation_op
);
231 static struct file_operations fragmentation_file_operations
= {
232 .open
= fragmentation_open
,
235 .release
= seq_release
,
238 extern struct seq_operations zoneinfo_op
;
239 static int zoneinfo_open(struct inode
*inode
, struct file
*file
)
241 return seq_open(file
, &zoneinfo_op
);
244 static struct file_operations proc_zoneinfo_file_operations
= {
245 .open
= zoneinfo_open
,
248 .release
= seq_release
,
251 static int version_read_proc(char *page
, char **start
, off_t off
,
252 int count
, int *eof
, void *data
)
256 len
= sprintf(page
, linux_banner
,
257 utsname()->release
, utsname()->version
);
258 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
261 extern struct seq_operations cpuinfo_op
;
262 static int cpuinfo_open(struct inode
*inode
, struct file
*file
)
264 return seq_open(file
, &cpuinfo_op
);
267 static struct file_operations proc_cpuinfo_operations
= {
268 .open
= cpuinfo_open
,
271 .release
= seq_release
,
274 static int devinfo_show(struct seq_file
*f
, void *v
)
276 int i
= *(loff_t
*) v
;
278 if (i
< CHRDEV_MAJOR_HASH_SIZE
) {
280 seq_printf(f
, "Character devices:\n");
285 i
-= CHRDEV_MAJOR_HASH_SIZE
;
287 seq_printf(f
, "\nBlock devices:\n");
294 static void *devinfo_start(struct seq_file
*f
, loff_t
*pos
)
296 if (*pos
< (BLKDEV_MAJOR_HASH_SIZE
+ CHRDEV_MAJOR_HASH_SIZE
))
301 static void *devinfo_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
304 if (*pos
>= (BLKDEV_MAJOR_HASH_SIZE
+ CHRDEV_MAJOR_HASH_SIZE
))
309 static void devinfo_stop(struct seq_file
*f
, void *v
)
314 static struct seq_operations devinfo_ops
= {
315 .start
= devinfo_start
,
316 .next
= devinfo_next
,
317 .stop
= devinfo_stop
,
321 static int devinfo_open(struct inode
*inode
, struct file
*filp
)
323 return seq_open(filp
, &devinfo_ops
);
326 static struct file_operations proc_devinfo_operations
= {
327 .open
= devinfo_open
,
330 .release
= seq_release
,
333 extern struct seq_operations vmstat_op
;
334 static int vmstat_open(struct inode
*inode
, struct file
*file
)
336 return seq_open(file
, &vmstat_op
);
338 static struct file_operations proc_vmstat_file_operations
= {
342 .release
= seq_release
,
345 #ifdef CONFIG_PROC_HARDWARE
346 static int hardware_read_proc(char *page
, char **start
, off_t off
,
347 int count
, int *eof
, void *data
)
349 int len
= get_hardware_list(page
);
350 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
354 #ifdef CONFIG_STRAM_PROC
355 static int stram_read_proc(char *page
, char **start
, off_t off
,
356 int count
, int *eof
, void *data
)
358 int len
= get_stram_list(page
);
359 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
364 extern struct seq_operations partitions_op
;
365 static int partitions_open(struct inode
*inode
, struct file
*file
)
367 return seq_open(file
, &partitions_op
);
369 static struct file_operations proc_partitions_operations
= {
370 .open
= partitions_open
,
373 .release
= seq_release
,
376 extern struct seq_operations diskstats_op
;
377 static int diskstats_open(struct inode
*inode
, struct file
*file
)
379 return seq_open(file
, &diskstats_op
);
381 static struct file_operations proc_diskstats_operations
= {
382 .open
= diskstats_open
,
385 .release
= seq_release
,
389 #ifdef CONFIG_MODULES
390 extern struct seq_operations modules_op
;
391 static int modules_open(struct inode
*inode
, struct file
*file
)
393 return seq_open(file
, &modules_op
);
395 static struct file_operations proc_modules_operations
= {
396 .open
= modules_open
,
399 .release
= seq_release
,
404 extern struct seq_operations slabinfo_op
;
405 extern ssize_t
slabinfo_write(struct file
*, const char __user
*, size_t, loff_t
*);
406 static int slabinfo_open(struct inode
*inode
, struct file
*file
)
408 return seq_open(file
, &slabinfo_op
);
410 static 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 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 int slabstats_release(struct inode
*inode
, struct file
*file
)
439 struct seq_file
*m
= file
->private_data
;
441 return seq_release(inode
, file
);
444 static struct file_operations proc_slabstats_operations
= {
445 .open
= slabstats_open
,
448 .release
= slabstats_release
,
453 static int show_stat(struct seq_file
*p
, void *v
)
457 cputime64_t user
, nice
, system
, idle
, iowait
, irq
, softirq
, steal
;
460 user
= nice
= system
= idle
= iowait
=
461 irq
= softirq
= steal
= cputime64_zero
;
462 jif
= - wall_to_monotonic
.tv_sec
;
463 if (wall_to_monotonic
.tv_nsec
)
466 for_each_possible_cpu(i
) {
469 user
= cputime64_add(user
, kstat_cpu(i
).cpustat
.user
);
470 nice
= cputime64_add(nice
, kstat_cpu(i
).cpustat
.nice
);
471 system
= cputime64_add(system
, kstat_cpu(i
).cpustat
.system
);
472 idle
= cputime64_add(idle
, kstat_cpu(i
).cpustat
.idle
);
473 iowait
= cputime64_add(iowait
, kstat_cpu(i
).cpustat
.iowait
);
474 irq
= cputime64_add(irq
, kstat_cpu(i
).cpustat
.irq
);
475 softirq
= cputime64_add(softirq
, kstat_cpu(i
).cpustat
.softirq
);
476 steal
= cputime64_add(steal
, kstat_cpu(i
).cpustat
.steal
);
477 for (j
= 0 ; j
< NR_IRQS
; j
++)
478 sum
+= kstat_cpu(i
).irqs
[j
];
481 seq_printf(p
, "cpu %llu %llu %llu %llu %llu %llu %llu %llu\n",
482 (unsigned long long)cputime64_to_clock_t(user
),
483 (unsigned long long)cputime64_to_clock_t(nice
),
484 (unsigned long long)cputime64_to_clock_t(system
),
485 (unsigned long long)cputime64_to_clock_t(idle
),
486 (unsigned long long)cputime64_to_clock_t(iowait
),
487 (unsigned long long)cputime64_to_clock_t(irq
),
488 (unsigned long long)cputime64_to_clock_t(softirq
),
489 (unsigned long long)cputime64_to_clock_t(steal
));
490 for_each_online_cpu(i
) {
492 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
493 user
= kstat_cpu(i
).cpustat
.user
;
494 nice
= kstat_cpu(i
).cpustat
.nice
;
495 system
= kstat_cpu(i
).cpustat
.system
;
496 idle
= kstat_cpu(i
).cpustat
.idle
;
497 iowait
= kstat_cpu(i
).cpustat
.iowait
;
498 irq
= kstat_cpu(i
).cpustat
.irq
;
499 softirq
= kstat_cpu(i
).cpustat
.softirq
;
500 steal
= kstat_cpu(i
).cpustat
.steal
;
501 seq_printf(p
, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu\n",
503 (unsigned long long)cputime64_to_clock_t(user
),
504 (unsigned long long)cputime64_to_clock_t(nice
),
505 (unsigned long long)cputime64_to_clock_t(system
),
506 (unsigned long long)cputime64_to_clock_t(idle
),
507 (unsigned long long)cputime64_to_clock_t(iowait
),
508 (unsigned long long)cputime64_to_clock_t(irq
),
509 (unsigned long long)cputime64_to_clock_t(softirq
),
510 (unsigned long long)cputime64_to_clock_t(steal
));
512 seq_printf(p
, "intr %llu", (unsigned long long)sum
);
514 #if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA) && !defined(CONFIG_IA64)
515 for (i
= 0; i
< NR_IRQS
; i
++)
516 seq_printf(p
, " %u", kstat_irqs(i
));
523 "procs_running %lu\n"
524 "procs_blocked %lu\n",
525 nr_context_switches(),
534 static int stat_open(struct inode
*inode
, struct file
*file
)
536 unsigned size
= 4096 * (1 + num_possible_cpus() / 32);
541 /* don't ask for more than the kmalloc() max size, currently 128 KB */
542 if (size
> 128 * 1024)
544 buf
= kmalloc(size
, GFP_KERNEL
);
548 res
= single_open(file
, show_stat
, NULL
);
550 m
= file
->private_data
;
557 static struct file_operations proc_stat_operations
= {
561 .release
= single_release
,
567 static void *int_seq_start(struct seq_file
*f
, loff_t
*pos
)
569 return (*pos
<= NR_IRQS
) ? pos
: NULL
;
572 static void *int_seq_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
580 static void int_seq_stop(struct seq_file
*f
, void *v
)
586 extern int show_interrupts(struct seq_file
*f
, void *v
); /* In arch code */
587 static struct seq_operations int_seq_ops
= {
588 .start
= int_seq_start
,
589 .next
= int_seq_next
,
590 .stop
= int_seq_stop
,
591 .show
= show_interrupts
594 static int interrupts_open(struct inode
*inode
, struct file
*filp
)
596 return seq_open(filp
, &int_seq_ops
);
599 static struct file_operations proc_interrupts_operations
= {
600 .open
= interrupts_open
,
603 .release
= seq_release
,
606 static int filesystems_read_proc(char *page
, char **start
, off_t off
,
607 int count
, int *eof
, void *data
)
609 int len
= get_filesystem_list(page
);
610 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
613 static int cmdline_read_proc(char *page
, char **start
, off_t off
,
614 int count
, int *eof
, void *data
)
618 len
= sprintf(page
, "%s\n", saved_command_line
);
619 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
622 static int locks_read_proc(char *page
, char **start
, off_t off
,
623 int count
, int *eof
, void *data
)
625 int len
= get_locks_status(page
, start
, off
, count
);
632 static int execdomains_read_proc(char *page
, char **start
, off_t off
,
633 int count
, int *eof
, void *data
)
635 int len
= get_exec_domain_list(page
);
636 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
639 #ifdef CONFIG_MAGIC_SYSRQ
641 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
643 static ssize_t
write_sysrq_trigger(struct file
*file
, const char __user
*buf
,
644 size_t count
, loff_t
*ppos
)
649 if (get_user(c
, buf
))
651 __handle_sysrq(c
, NULL
, 0);
656 static struct file_operations proc_sysrq_trigger_operations
= {
657 .write
= write_sysrq_trigger
,
661 struct proc_dir_entry
*proc_root_kcore
;
663 void create_seq_entry(char *name
, mode_t mode
, const struct file_operations
*f
)
665 struct proc_dir_entry
*entry
;
666 entry
= create_proc_entry(name
, mode
, NULL
);
668 entry
->proc_fops
= f
;
671 void __init
proc_misc_init(void)
673 struct proc_dir_entry
*entry
;
676 int (*read_proc
)(char*,char**,off_t
,int,int*,void*);
677 } *p
, simple_ones
[] = {
678 {"loadavg", loadavg_read_proc
},
679 {"uptime", uptime_read_proc
},
680 {"meminfo", meminfo_read_proc
},
681 {"version", version_read_proc
},
682 #ifdef CONFIG_PROC_HARDWARE
683 {"hardware", hardware_read_proc
},
685 #ifdef CONFIG_STRAM_PROC
686 {"stram", stram_read_proc
},
688 {"filesystems", filesystems_read_proc
},
689 {"cmdline", cmdline_read_proc
},
690 {"locks", locks_read_proc
},
691 {"execdomains", execdomains_read_proc
},
694 for (p
= simple_ones
; p
->name
; p
++)
695 create_proc_read_entry(p
->name
, 0, NULL
, p
->read_proc
, NULL
);
697 proc_symlink("mounts", NULL
, "self/mounts");
699 /* And now for trickier ones */
701 entry
= create_proc_entry("kmsg", S_IRUSR
, &proc_root
);
703 entry
->proc_fops
= &proc_kmsg_operations
;
705 create_seq_entry("devices", 0, &proc_devinfo_operations
);
706 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations
);
708 create_seq_entry("partitions", 0, &proc_partitions_operations
);
710 create_seq_entry("stat", 0, &proc_stat_operations
);
711 create_seq_entry("interrupts", 0, &proc_interrupts_operations
);
713 create_seq_entry("slabinfo",S_IWUSR
|S_IRUGO
,&proc_slabinfo_operations
);
714 #ifdef CONFIG_DEBUG_SLAB_LEAK
715 create_seq_entry("slab_allocators", 0 ,&proc_slabstats_operations
);
718 create_seq_entry("buddyinfo",S_IRUGO
, &fragmentation_file_operations
);
719 create_seq_entry("vmstat",S_IRUGO
, &proc_vmstat_file_operations
);
720 create_seq_entry("zoneinfo",S_IRUGO
, &proc_zoneinfo_file_operations
);
722 create_seq_entry("diskstats", 0, &proc_diskstats_operations
);
724 #ifdef CONFIG_MODULES
725 create_seq_entry("modules", 0, &proc_modules_operations
);
727 #ifdef CONFIG_SCHEDSTATS
728 create_seq_entry("schedstat", 0, &proc_schedstat_operations
);
730 #ifdef CONFIG_PROC_KCORE
731 proc_root_kcore
= create_proc_entry("kcore", S_IRUSR
, NULL
);
732 if (proc_root_kcore
) {
733 proc_root_kcore
->proc_fops
= &proc_kcore_operations
;
734 proc_root_kcore
->size
=
735 (size_t)high_memory
- PAGE_OFFSET
+ PAGE_SIZE
;
738 #ifdef CONFIG_PROC_VMCORE
739 proc_vmcore
= create_proc_entry("vmcore", S_IRUSR
, NULL
);
741 proc_vmcore
->proc_fops
= &proc_vmcore_operations
;
743 #ifdef CONFIG_MAGIC_SYSRQ
744 entry
= create_proc_entry("sysrq-trigger", S_IWUSR
, NULL
);
746 entry
->proc_fops
= &proc_sysrq_trigger_operations
;