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 <linux/compile.h>
51 #include <asm/uaccess.h>
52 #include <asm/pgtable.h>
55 #include <asm/div64.h>
58 #define LOAD_INT(x) ((x) >> FSHIFT)
59 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
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_filesystem_list(char *);
69 extern int get_exec_domain_list(char *);
70 extern int get_dma_list(char *);
71 extern int get_locks_status (char *, char **, off_t
, int);
73 static int proc_calc_metrics(char *page
, char **start
, off_t off
,
74 int count
, int *eof
, int len
)
76 if (len
<= off
+count
) *eof
= 1;
79 if (len
>count
) len
= count
;
84 static int loadavg_read_proc(char *page
, char **start
, off_t off
,
85 int count
, int *eof
, void *data
)
90 a
= avenrun
[0] + (FIXED_1
/200);
91 b
= avenrun
[1] + (FIXED_1
/200);
92 c
= avenrun
[2] + (FIXED_1
/200);
93 len
= sprintf(page
,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
94 LOAD_INT(a
), LOAD_FRAC(a
),
95 LOAD_INT(b
), LOAD_FRAC(b
),
96 LOAD_INT(c
), LOAD_FRAC(c
),
97 nr_running(), nr_threads
, current
->nsproxy
->pid_ns
->last_pid
);
98 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
101 static int uptime_read_proc(char *page
, char **start
, off_t off
,
102 int count
, int *eof
, void *data
)
104 struct timespec uptime
;
105 struct timespec idle
;
107 cputime_t idletime
= cputime_add(init_task
.utime
, init_task
.stime
);
109 do_posix_clock_monotonic_gettime(&uptime
);
110 cputime_to_timespec(idletime
, &idle
);
111 len
= sprintf(page
,"%lu.%02lu %lu.%02lu\n",
112 (unsigned long) uptime
.tv_sec
,
113 (uptime
.tv_nsec
/ (NSEC_PER_SEC
/ 100)),
114 (unsigned long) idle
.tv_sec
,
115 (idle
.tv_nsec
/ (NSEC_PER_SEC
/ 100)));
117 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
120 static int meminfo_read_proc(char *page
, char **start
, off_t off
,
121 int count
, int *eof
, void *data
)
125 unsigned long inactive
;
126 unsigned long active
;
128 unsigned long committed
;
129 unsigned long allowed
;
130 struct vmalloc_info vmi
;
133 get_zone_counts(&active
, &inactive
, &free
);
136 * display in kilobytes.
138 #define K(x) ((x) << (PAGE_SHIFT - 10))
141 committed
= atomic_read(&vm_committed_space
);
142 allowed
= ((totalram_pages
- hugetlb_total_pages())
143 * sysctl_overcommit_ratio
/ 100) + total_swap_pages
;
145 cached
= global_page_state(NR_FILE_PAGES
) -
146 total_swapcache_pages
- i
.bufferram
;
150 get_vmalloc_info(&vmi
);
153 * Tagged format, for easy grepping and expansion.
156 "MemTotal: %8lu kB\n"
160 "SwapCached: %8lu kB\n"
162 "Inactive: %8lu kB\n"
163 #ifdef CONFIG_HIGHMEM
164 "HighTotal: %8lu kB\n"
165 "HighFree: %8lu kB\n"
166 "LowTotal: %8lu kB\n"
169 "SwapTotal: %8lu kB\n"
170 "SwapFree: %8lu kB\n"
172 "Writeback: %8lu kB\n"
173 "AnonPages: %8lu kB\n"
176 "SReclaimable: %8lu kB\n"
177 "SUnreclaim: %8lu kB\n"
178 "PageTables: %8lu kB\n"
179 "NFS_Unstable: %8lu kB\n"
181 "CommitLimit: %8lu kB\n"
182 "Committed_AS: %8lu kB\n"
183 "VmallocTotal: %8lu kB\n"
184 "VmallocUsed: %8lu kB\n"
185 "VmallocChunk: %8lu kB\n",
190 K(total_swapcache_pages
),
193 #ifdef CONFIG_HIGHMEM
196 K(i
.totalram
-i
.totalhigh
),
197 K(i
.freeram
-i
.freehigh
),
201 K(global_page_state(NR_FILE_DIRTY
)),
202 K(global_page_state(NR_WRITEBACK
)),
203 K(global_page_state(NR_ANON_PAGES
)),
204 K(global_page_state(NR_FILE_MAPPED
)),
205 K(global_page_state(NR_SLAB_RECLAIMABLE
) +
206 global_page_state(NR_SLAB_UNRECLAIMABLE
)),
207 K(global_page_state(NR_SLAB_RECLAIMABLE
)),
208 K(global_page_state(NR_SLAB_UNRECLAIMABLE
)),
209 K(global_page_state(NR_PAGETABLE
)),
210 K(global_page_state(NR_UNSTABLE_NFS
)),
211 K(global_page_state(NR_BOUNCE
)),
214 (unsigned long)VMALLOC_TOTAL
>> 10,
216 vmi
.largest_chunk
>> 10
219 len
+= hugetlb_report_meminfo(page
+ len
);
221 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
225 extern struct seq_operations fragmentation_op
;
226 static int fragmentation_open(struct inode
*inode
, struct file
*file
)
229 return seq_open(file
, &fragmentation_op
);
232 static struct file_operations fragmentation_file_operations
= {
233 .open
= fragmentation_open
,
236 .release
= seq_release
,
239 extern struct seq_operations zoneinfo_op
;
240 static int zoneinfo_open(struct inode
*inode
, struct file
*file
)
242 return seq_open(file
, &zoneinfo_op
);
245 static struct file_operations proc_zoneinfo_file_operations
= {
246 .open
= zoneinfo_open
,
249 .release
= seq_release
,
252 static int version_read_proc(char *page
, char **start
, off_t off
,
253 int count
, int *eof
, void *data
)
257 /* FIXED STRING! Don't touch! */
258 len
= snprintf(page
, PAGE_SIZE
,
260 " (" LINUX_COMPILE_BY
"@" LINUX_COMPILE_HOST
")"
261 " (" LINUX_COMPILER
")"
266 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
269 extern struct seq_operations cpuinfo_op
;
270 static int cpuinfo_open(struct inode
*inode
, struct file
*file
)
272 return seq_open(file
, &cpuinfo_op
);
275 static struct file_operations proc_cpuinfo_operations
= {
276 .open
= cpuinfo_open
,
279 .release
= seq_release
,
282 static int devinfo_show(struct seq_file
*f
, void *v
)
284 int i
= *(loff_t
*) v
;
286 if (i
< CHRDEV_MAJOR_HASH_SIZE
) {
288 seq_printf(f
, "Character devices:\n");
293 i
-= CHRDEV_MAJOR_HASH_SIZE
;
295 seq_printf(f
, "\nBlock devices:\n");
302 static void *devinfo_start(struct seq_file
*f
, loff_t
*pos
)
304 if (*pos
< (BLKDEV_MAJOR_HASH_SIZE
+ CHRDEV_MAJOR_HASH_SIZE
))
309 static void *devinfo_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
312 if (*pos
>= (BLKDEV_MAJOR_HASH_SIZE
+ CHRDEV_MAJOR_HASH_SIZE
))
317 static void devinfo_stop(struct seq_file
*f
, void *v
)
322 static struct seq_operations devinfo_ops
= {
323 .start
= devinfo_start
,
324 .next
= devinfo_next
,
325 .stop
= devinfo_stop
,
329 static int devinfo_open(struct inode
*inode
, struct file
*filp
)
331 return seq_open(filp
, &devinfo_ops
);
334 static struct file_operations proc_devinfo_operations
= {
335 .open
= devinfo_open
,
338 .release
= seq_release
,
341 extern struct seq_operations vmstat_op
;
342 static int vmstat_open(struct inode
*inode
, struct file
*file
)
344 return seq_open(file
, &vmstat_op
);
346 static struct file_operations proc_vmstat_file_operations
= {
350 .release
= seq_release
,
353 #ifdef CONFIG_PROC_HARDWARE
354 static int hardware_read_proc(char *page
, char **start
, off_t off
,
355 int count
, int *eof
, void *data
)
357 int len
= get_hardware_list(page
);
358 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
362 #ifdef CONFIG_STRAM_PROC
363 static int stram_read_proc(char *page
, char **start
, off_t off
,
364 int count
, int *eof
, void *data
)
366 int len
= get_stram_list(page
);
367 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
372 extern struct seq_operations partitions_op
;
373 static int partitions_open(struct inode
*inode
, struct file
*file
)
375 return seq_open(file
, &partitions_op
);
377 static struct file_operations proc_partitions_operations
= {
378 .open
= partitions_open
,
381 .release
= seq_release
,
384 extern struct seq_operations diskstats_op
;
385 static int diskstats_open(struct inode
*inode
, struct file
*file
)
387 return seq_open(file
, &diskstats_op
);
389 static struct file_operations proc_diskstats_operations
= {
390 .open
= diskstats_open
,
393 .release
= seq_release
,
397 #ifdef CONFIG_MODULES
398 extern struct seq_operations modules_op
;
399 static int modules_open(struct inode
*inode
, struct file
*file
)
401 return seq_open(file
, &modules_op
);
403 static struct file_operations proc_modules_operations
= {
404 .open
= modules_open
,
407 .release
= seq_release
,
412 extern struct seq_operations slabinfo_op
;
413 extern ssize_t
slabinfo_write(struct file
*, const char __user
*, size_t, loff_t
*);
414 static int slabinfo_open(struct inode
*inode
, struct file
*file
)
416 return seq_open(file
, &slabinfo_op
);
418 static struct file_operations proc_slabinfo_operations
= {
419 .open
= slabinfo_open
,
421 .write
= slabinfo_write
,
423 .release
= seq_release
,
426 #ifdef CONFIG_DEBUG_SLAB_LEAK
427 extern struct seq_operations slabstats_op
;
428 static int slabstats_open(struct inode
*inode
, struct file
*file
)
430 unsigned long *n
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
433 ret
= seq_open(file
, &slabstats_op
);
435 struct seq_file
*m
= file
->private_data
;
436 *n
= PAGE_SIZE
/ (2 * sizeof(unsigned long));
445 static int slabstats_release(struct inode
*inode
, struct file
*file
)
447 struct seq_file
*m
= file
->private_data
;
449 return seq_release(inode
, file
);
452 static struct file_operations proc_slabstats_operations
= {
453 .open
= slabstats_open
,
456 .release
= slabstats_release
,
461 static int show_stat(struct seq_file
*p
, void *v
)
465 cputime64_t user
, nice
, system
, idle
, iowait
, irq
, softirq
, steal
;
468 user
= nice
= system
= idle
= iowait
=
469 irq
= softirq
= steal
= cputime64_zero
;
470 jif
= - wall_to_monotonic
.tv_sec
;
471 if (wall_to_monotonic
.tv_nsec
)
474 for_each_possible_cpu(i
) {
477 user
= cputime64_add(user
, kstat_cpu(i
).cpustat
.user
);
478 nice
= cputime64_add(nice
, kstat_cpu(i
).cpustat
.nice
);
479 system
= cputime64_add(system
, kstat_cpu(i
).cpustat
.system
);
480 idle
= cputime64_add(idle
, kstat_cpu(i
).cpustat
.idle
);
481 iowait
= cputime64_add(iowait
, kstat_cpu(i
).cpustat
.iowait
);
482 irq
= cputime64_add(irq
, kstat_cpu(i
).cpustat
.irq
);
483 softirq
= cputime64_add(softirq
, kstat_cpu(i
).cpustat
.softirq
);
484 steal
= cputime64_add(steal
, kstat_cpu(i
).cpustat
.steal
);
485 for (j
= 0 ; j
< NR_IRQS
; j
++)
486 sum
+= kstat_cpu(i
).irqs
[j
];
489 seq_printf(p
, "cpu %llu %llu %llu %llu %llu %llu %llu %llu\n",
490 (unsigned long long)cputime64_to_clock_t(user
),
491 (unsigned long long)cputime64_to_clock_t(nice
),
492 (unsigned long long)cputime64_to_clock_t(system
),
493 (unsigned long long)cputime64_to_clock_t(idle
),
494 (unsigned long long)cputime64_to_clock_t(iowait
),
495 (unsigned long long)cputime64_to_clock_t(irq
),
496 (unsigned long long)cputime64_to_clock_t(softirq
),
497 (unsigned long long)cputime64_to_clock_t(steal
));
498 for_each_online_cpu(i
) {
500 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
501 user
= kstat_cpu(i
).cpustat
.user
;
502 nice
= kstat_cpu(i
).cpustat
.nice
;
503 system
= kstat_cpu(i
).cpustat
.system
;
504 idle
= kstat_cpu(i
).cpustat
.idle
;
505 iowait
= kstat_cpu(i
).cpustat
.iowait
;
506 irq
= kstat_cpu(i
).cpustat
.irq
;
507 softirq
= kstat_cpu(i
).cpustat
.softirq
;
508 steal
= kstat_cpu(i
).cpustat
.steal
;
509 seq_printf(p
, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu\n",
511 (unsigned long long)cputime64_to_clock_t(user
),
512 (unsigned long long)cputime64_to_clock_t(nice
),
513 (unsigned long long)cputime64_to_clock_t(system
),
514 (unsigned long long)cputime64_to_clock_t(idle
),
515 (unsigned long long)cputime64_to_clock_t(iowait
),
516 (unsigned long long)cputime64_to_clock_t(irq
),
517 (unsigned long long)cputime64_to_clock_t(softirq
),
518 (unsigned long long)cputime64_to_clock_t(steal
));
520 seq_printf(p
, "intr %llu", (unsigned long long)sum
);
522 #if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA) && !defined(CONFIG_IA64)
523 for (i
= 0; i
< NR_IRQS
; i
++)
524 seq_printf(p
, " %u", kstat_irqs(i
));
531 "procs_running %lu\n"
532 "procs_blocked %lu\n",
533 nr_context_switches(),
542 static int stat_open(struct inode
*inode
, struct file
*file
)
544 unsigned size
= 4096 * (1 + num_possible_cpus() / 32);
549 /* don't ask for more than the kmalloc() max size, currently 128 KB */
550 if (size
> 128 * 1024)
552 buf
= kmalloc(size
, GFP_KERNEL
);
556 res
= single_open(file
, show_stat
, NULL
);
558 m
= file
->private_data
;
565 static struct file_operations proc_stat_operations
= {
569 .release
= single_release
,
575 static void *int_seq_start(struct seq_file
*f
, loff_t
*pos
)
577 return (*pos
<= NR_IRQS
) ? pos
: NULL
;
580 static void *int_seq_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
588 static void int_seq_stop(struct seq_file
*f
, void *v
)
594 extern int show_interrupts(struct seq_file
*f
, void *v
); /* In arch code */
595 static struct seq_operations int_seq_ops
= {
596 .start
= int_seq_start
,
597 .next
= int_seq_next
,
598 .stop
= int_seq_stop
,
599 .show
= show_interrupts
602 static int interrupts_open(struct inode
*inode
, struct file
*filp
)
604 return seq_open(filp
, &int_seq_ops
);
607 static struct file_operations proc_interrupts_operations
= {
608 .open
= interrupts_open
,
611 .release
= seq_release
,
614 static int filesystems_read_proc(char *page
, char **start
, off_t off
,
615 int count
, int *eof
, void *data
)
617 int len
= get_filesystem_list(page
);
618 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
621 static int cmdline_read_proc(char *page
, char **start
, off_t off
,
622 int count
, int *eof
, void *data
)
626 len
= sprintf(page
, "%s\n", saved_command_line
);
627 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
630 static int locks_read_proc(char *page
, char **start
, off_t off
,
631 int count
, int *eof
, void *data
)
633 int len
= get_locks_status(page
, start
, off
, count
);
640 static int execdomains_read_proc(char *page
, char **start
, off_t off
,
641 int count
, int *eof
, void *data
)
643 int len
= get_exec_domain_list(page
);
644 return proc_calc_metrics(page
, start
, off
, count
, eof
, len
);
647 #ifdef CONFIG_MAGIC_SYSRQ
649 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
651 static ssize_t
write_sysrq_trigger(struct file
*file
, const char __user
*buf
,
652 size_t count
, loff_t
*ppos
)
657 if (get_user(c
, buf
))
659 __handle_sysrq(c
, NULL
, 0);
664 static struct file_operations proc_sysrq_trigger_operations
= {
665 .write
= write_sysrq_trigger
,
669 struct proc_dir_entry
*proc_root_kcore
;
671 void create_seq_entry(char *name
, mode_t mode
, const struct file_operations
*f
)
673 struct proc_dir_entry
*entry
;
674 entry
= create_proc_entry(name
, mode
, NULL
);
676 entry
->proc_fops
= f
;
679 void __init
proc_misc_init(void)
681 struct proc_dir_entry
*entry
;
684 int (*read_proc
)(char*,char**,off_t
,int,int*,void*);
685 } *p
, simple_ones
[] = {
686 {"loadavg", loadavg_read_proc
},
687 {"uptime", uptime_read_proc
},
688 {"meminfo", meminfo_read_proc
},
689 {"version", version_read_proc
},
690 #ifdef CONFIG_PROC_HARDWARE
691 {"hardware", hardware_read_proc
},
693 #ifdef CONFIG_STRAM_PROC
694 {"stram", stram_read_proc
},
696 {"filesystems", filesystems_read_proc
},
697 {"cmdline", cmdline_read_proc
},
698 {"locks", locks_read_proc
},
699 {"execdomains", execdomains_read_proc
},
702 for (p
= simple_ones
; p
->name
; p
++)
703 create_proc_read_entry(p
->name
, 0, NULL
, p
->read_proc
, NULL
);
705 proc_symlink("mounts", NULL
, "self/mounts");
707 /* And now for trickier ones */
709 entry
= create_proc_entry("kmsg", S_IRUSR
, &proc_root
);
711 entry
->proc_fops
= &proc_kmsg_operations
;
713 create_seq_entry("devices", 0, &proc_devinfo_operations
);
714 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations
);
716 create_seq_entry("partitions", 0, &proc_partitions_operations
);
718 create_seq_entry("stat", 0, &proc_stat_operations
);
719 create_seq_entry("interrupts", 0, &proc_interrupts_operations
);
721 create_seq_entry("slabinfo",S_IWUSR
|S_IRUGO
,&proc_slabinfo_operations
);
722 #ifdef CONFIG_DEBUG_SLAB_LEAK
723 create_seq_entry("slab_allocators", 0 ,&proc_slabstats_operations
);
726 create_seq_entry("buddyinfo",S_IRUGO
, &fragmentation_file_operations
);
727 create_seq_entry("vmstat",S_IRUGO
, &proc_vmstat_file_operations
);
728 create_seq_entry("zoneinfo",S_IRUGO
, &proc_zoneinfo_file_operations
);
730 create_seq_entry("diskstats", 0, &proc_diskstats_operations
);
732 #ifdef CONFIG_MODULES
733 create_seq_entry("modules", 0, &proc_modules_operations
);
735 #ifdef CONFIG_SCHEDSTATS
736 create_seq_entry("schedstat", 0, &proc_schedstat_operations
);
738 #ifdef CONFIG_PROC_KCORE
739 proc_root_kcore
= create_proc_entry("kcore", S_IRUSR
, NULL
);
740 if (proc_root_kcore
) {
741 proc_root_kcore
->proc_fops
= &proc_kcore_operations
;
742 proc_root_kcore
->size
=
743 (size_t)high_memory
- PAGE_OFFSET
+ PAGE_SIZE
;
746 #ifdef CONFIG_PROC_VMCORE
747 proc_vmcore
= create_proc_entry("vmcore", S_IRUSR
, NULL
);
749 proc_vmcore
->proc_fops
= &proc_vmcore_operations
;
751 #ifdef CONFIG_MAGIC_SYSRQ
752 entry
= create_proc_entry("sysrq-trigger", S_IWUSR
, NULL
);
754 entry
->proc_fops
= &proc_sysrq_trigger_operations
;