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_stram_list(char *);
67 extern int get_exec_domain_list(char *);
69 static int proc_calc_metrics(char *page, char **start, off_t off,
70 int count, int *eof, int len)
72 if (len <= off+count) *eof = 1;
75 if (len>count) len = count;
80 static int fragmentation_open(struct inode *inode, struct file *file)
83 return seq_open(file, &fragmentation_op);
86 static const struct file_operations fragmentation_file_operations = {
87 .open = fragmentation_open,
90 .release = seq_release,
93 static int pagetypeinfo_open(struct inode *inode, struct file *file)
95 return seq_open(file, &pagetypeinfo_op);
98 static const struct file_operations pagetypeinfo_file_ops = {
99 .open = pagetypeinfo_open,
102 .release = seq_release,
105 static int zoneinfo_open(struct inode *inode, struct file *file)
107 return seq_open(file, &zoneinfo_op);
110 static const struct file_operations proc_zoneinfo_file_operations = {
111 .open = zoneinfo_open,
114 .release = seq_release,
117 extern const struct seq_operations cpuinfo_op;
118 static int cpuinfo_open(struct inode *inode, struct file *file)
120 return seq_open(file, &cpuinfo_op);
123 static const struct file_operations proc_cpuinfo_operations = {
124 .open = cpuinfo_open,
127 .release = seq_release,
130 static int devinfo_show(struct seq_file *f, void *v)
132 int i = *(loff_t *) v;
134 if (i < CHRDEV_MAJOR_HASH_SIZE) {
136 seq_printf(f, "Character devices:\n");
141 i -= CHRDEV_MAJOR_HASH_SIZE;
143 seq_printf(f, "\nBlock devices:\n");
150 static void *devinfo_start(struct seq_file *f, loff_t *pos)
152 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
157 static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
160 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
165 static void devinfo_stop(struct seq_file *f, void *v)
170 static const struct seq_operations devinfo_ops = {
171 .start = devinfo_start,
172 .next = devinfo_next,
173 .stop = devinfo_stop,
177 static int devinfo_open(struct inode *inode, struct file *filp)
179 return seq_open(filp, &devinfo_ops);
182 static const struct file_operations proc_devinfo_operations = {
183 .open = devinfo_open,
186 .release = seq_release,
189 static int vmstat_open(struct inode *inode, struct file *file)
191 return seq_open(file, &vmstat_op);
193 static const struct file_operations proc_vmstat_file_operations = {
197 .release = seq_release,
200 #ifdef CONFIG_STRAM_PROC
201 static int stram_read_proc(char *page, char **start, off_t off,
202 int count, int *eof, void *data)
204 int len = get_stram_list(page);
205 return proc_calc_metrics(page, start, off, count, eof, len);
210 static int partitions_open(struct inode *inode, struct file *file)
212 return seq_open(file, &partitions_op);
214 static const struct file_operations proc_partitions_operations = {
215 .open = partitions_open,
218 .release = seq_release,
221 static int diskstats_open(struct inode *inode, struct file *file)
223 return seq_open(file, &diskstats_op);
225 static const struct file_operations proc_diskstats_operations = {
226 .open = diskstats_open,
229 .release = seq_release,
233 #ifdef CONFIG_MODULES
234 extern const struct seq_operations modules_op;
235 static int modules_open(struct inode *inode, struct file *file)
237 return seq_open(file, &modules_op);
239 static const struct file_operations proc_modules_operations = {
240 .open = modules_open,
243 .release = seq_release,
247 #ifdef CONFIG_SLABINFO
248 static int slabinfo_open(struct inode *inode, struct file *file)
250 return seq_open(file, &slabinfo_op);
252 static const struct file_operations proc_slabinfo_operations = {
253 .open = slabinfo_open,
255 .write = slabinfo_write,
257 .release = seq_release,
260 #ifdef CONFIG_DEBUG_SLAB_LEAK
261 extern const struct seq_operations slabstats_op;
262 static int slabstats_open(struct inode *inode, struct file *file)
264 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
267 ret = seq_open(file, &slabstats_op);
269 struct seq_file *m = file->private_data;
270 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
279 static const struct file_operations proc_slabstats_operations = {
280 .open = slabstats_open,
283 .release = seq_release_private,
289 static int vmalloc_open(struct inode *inode, struct file *file)
291 unsigned int *ptr = NULL;
295 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
296 ret = seq_open(file, &vmalloc_op);
298 struct seq_file *m = file->private_data;
305 static const struct file_operations proc_vmalloc_operations = {
306 .open = vmalloc_open,
309 .release = seq_release_private,
313 #ifndef arch_irq_stat_cpu
314 #define arch_irq_stat_cpu(cpu) 0
316 #ifndef arch_irq_stat
317 #define arch_irq_stat() 0
320 static int show_stat(struct seq_file *p, void *v)
324 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
327 struct timespec boottime;
328 unsigned int per_irq_sum;
330 user = nice = system = idle = iowait =
331 irq = softirq = steal = cputime64_zero;
332 guest = cputime64_zero;
333 getboottime(&boottime);
334 jif = boottime.tv_sec;
336 for_each_possible_cpu(i) {
337 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
338 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
339 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
340 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
341 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
342 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
343 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
344 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
345 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
348 sum += kstat_irqs_cpu(j, i);
350 sum += arch_irq_stat_cpu(i);
352 sum += arch_irq_stat();
354 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
355 (unsigned long long)cputime64_to_clock_t(user),
356 (unsigned long long)cputime64_to_clock_t(nice),
357 (unsigned long long)cputime64_to_clock_t(system),
358 (unsigned long long)cputime64_to_clock_t(idle),
359 (unsigned long long)cputime64_to_clock_t(iowait),
360 (unsigned long long)cputime64_to_clock_t(irq),
361 (unsigned long long)cputime64_to_clock_t(softirq),
362 (unsigned long long)cputime64_to_clock_t(steal),
363 (unsigned long long)cputime64_to_clock_t(guest));
364 for_each_online_cpu(i) {
366 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
367 user = kstat_cpu(i).cpustat.user;
368 nice = kstat_cpu(i).cpustat.nice;
369 system = kstat_cpu(i).cpustat.system;
370 idle = kstat_cpu(i).cpustat.idle;
371 iowait = kstat_cpu(i).cpustat.iowait;
372 irq = kstat_cpu(i).cpustat.irq;
373 softirq = kstat_cpu(i).cpustat.softirq;
374 steal = kstat_cpu(i).cpustat.steal;
375 guest = kstat_cpu(i).cpustat.guest;
377 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
379 (unsigned long long)cputime64_to_clock_t(user),
380 (unsigned long long)cputime64_to_clock_t(nice),
381 (unsigned long long)cputime64_to_clock_t(system),
382 (unsigned long long)cputime64_to_clock_t(idle),
383 (unsigned long long)cputime64_to_clock_t(iowait),
384 (unsigned long long)cputime64_to_clock_t(irq),
385 (unsigned long long)cputime64_to_clock_t(softirq),
386 (unsigned long long)cputime64_to_clock_t(steal),
387 (unsigned long long)cputime64_to_clock_t(guest));
389 seq_printf(p, "intr %llu", (unsigned long long)sum);
391 /* sum again ? it could be updated? */
395 for_each_possible_cpu(i)
396 per_irq_sum += kstat_irqs_cpu(j, i);
398 seq_printf(p, " %u", per_irq_sum);
405 "procs_running %lu\n"
406 "procs_blocked %lu\n",
407 nr_context_switches(),
416 static int stat_open(struct inode *inode, struct file *file)
418 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
423 /* don't ask for more than the kmalloc() max size, currently 128 KB */
424 if (size > 128 * 1024)
426 buf = kmalloc(size, GFP_KERNEL);
430 res = single_open(file, show_stat, NULL);
432 m = file->private_data;
439 static const struct file_operations proc_stat_operations = {
443 .release = single_release,
449 static void *int_seq_start(struct seq_file *f, loff_t *pos)
451 return (*pos <= nr_irqs) ? pos : NULL;
455 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
458 return (*pos <= nr_irqs) ? pos : NULL;
461 static void int_seq_stop(struct seq_file *f, void *v)
466 static const struct seq_operations int_seq_ops = {
467 .start = int_seq_start,
468 .next = int_seq_next,
469 .stop = int_seq_stop,
470 .show = show_interrupts
473 static int interrupts_open(struct inode *inode, struct file *filp)
475 return seq_open(filp, &int_seq_ops);
478 static const struct file_operations proc_interrupts_operations = {
479 .open = interrupts_open,
482 .release = seq_release,
485 static int filesystems_read_proc(char *page, char **start, off_t off,
486 int count, int *eof, void *data)
488 int len = get_filesystem_list(page);
489 return proc_calc_metrics(page, start, off, count, eof, len);
492 static int cmdline_read_proc(char *page, char **start, off_t off,
493 int count, int *eof, void *data)
497 len = sprintf(page, "%s\n", saved_command_line);
498 return proc_calc_metrics(page, start, off, count, eof, len);
501 #ifdef CONFIG_FILE_LOCKING
502 static int locks_open(struct inode *inode, struct file *filp)
504 return seq_open(filp, &locks_seq_operations);
507 static const struct file_operations proc_locks_operations = {
511 .release = seq_release,
513 #endif /* CONFIG_FILE_LOCKING */
515 static int execdomains_read_proc(char *page, char **start, off_t off,
516 int count, int *eof, void *data)
518 int len = get_exec_domain_list(page);
519 return proc_calc_metrics(page, start, off, count, eof, len);
522 #ifdef CONFIG_PROC_PAGE_MONITOR
523 #define KPMSIZE sizeof(u64)
524 #define KPMMASK (KPMSIZE - 1)
525 /* /proc/kpagecount - an array exposing page counts
527 * Each entry is a u64 representing the corresponding
528 * physical page count.
530 static ssize_t kpagecount_read(struct file *file, char __user *buf,
531 size_t count, loff_t *ppos)
533 u64 __user *out = (u64 __user *)buf;
535 unsigned long src = *ppos;
541 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
542 if (src & KPMMASK || count & KPMMASK)
548 ppage = pfn_to_page(pfn);
553 pcount = page_mapcount(ppage);
555 if (put_user(pcount, out++)) {
563 *ppos += (char __user *)out - buf;
565 ret = (char __user *)out - buf;
569 static struct file_operations proc_kpagecount_operations = {
571 .read = kpagecount_read,
574 /* /proc/kpageflags - an array exposing page flags
576 * Each entry is a u64 representing the corresponding
577 * physical page flags.
580 /* These macros are used to decouple internal flags from exported ones */
584 #define KPF_REFERENCED 2
585 #define KPF_UPTODATE 3
590 #define KPF_WRITEBACK 8
591 #define KPF_RECLAIM 9
594 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
596 static ssize_t kpageflags_read(struct file *file, char __user *buf,
597 size_t count, loff_t *ppos)
599 u64 __user *out = (u64 __user *)buf;
601 unsigned long src = *ppos;
607 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
608 if (src & KPMMASK || count & KPMMASK)
614 ppage = pfn_to_page(pfn);
619 kflags = ppage->flags;
621 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
622 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
623 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
624 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
625 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
626 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
627 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
628 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
629 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
630 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
631 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
633 if (put_user(uflags, out++)) {
641 *ppos += (char __user *)out - buf;
643 ret = (char __user *)out - buf;
647 static struct file_operations proc_kpageflags_operations = {
649 .read = kpageflags_read,
651 #endif /* CONFIG_PROC_PAGE_MONITOR */
653 struct proc_dir_entry *proc_root_kcore;
655 void __init proc_misc_init(void)
659 int (*read_proc)(char*,char**,off_t,int,int*,void*);
660 } *p, simple_ones[] = {
661 #ifdef CONFIG_STRAM_PROC
662 {"stram", stram_read_proc},
664 {"filesystems", filesystems_read_proc},
665 {"cmdline", cmdline_read_proc},
666 {"execdomains", execdomains_read_proc},
669 for (p = simple_ones; p->name; p++)
670 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
672 proc_symlink("mounts", NULL, "self/mounts");
674 /* And now for trickier ones */
676 proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
678 #ifdef CONFIG_FILE_LOCKING
679 proc_create("locks", 0, NULL, &proc_locks_operations);
681 proc_create("devices", 0, NULL, &proc_devinfo_operations);
682 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
684 proc_create("partitions", 0, NULL, &proc_partitions_operations);
686 proc_create("stat", 0, NULL, &proc_stat_operations);
687 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
688 #ifdef CONFIG_SLABINFO
689 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
690 #ifdef CONFIG_DEBUG_SLAB_LEAK
691 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
695 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
697 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
698 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
699 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
700 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
702 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
704 #ifdef CONFIG_MODULES
705 proc_create("modules", 0, NULL, &proc_modules_operations);
707 #ifdef CONFIG_SCHEDSTATS
708 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
710 #ifdef CONFIG_PROC_KCORE
711 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
713 proc_root_kcore->size =
714 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
716 #ifdef CONFIG_PROC_PAGE_MONITOR
717 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
718 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
720 #ifdef CONFIG_PROC_VMCORE
721 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);