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_exec_domain_list(char *);
68 static int proc_calc_metrics(char *page, char **start, off_t off,
69 int count, int *eof, int len)
71 if (len <= off+count) *eof = 1;
74 if (len>count) len = count;
79 static int fragmentation_open(struct inode *inode, struct file *file)
82 return seq_open(file, &fragmentation_op);
85 static const struct file_operations fragmentation_file_operations = {
86 .open = fragmentation_open,
89 .release = seq_release,
92 static int pagetypeinfo_open(struct inode *inode, struct file *file)
94 return seq_open(file, &pagetypeinfo_op);
97 static const struct file_operations pagetypeinfo_file_ops = {
98 .open = pagetypeinfo_open,
101 .release = seq_release,
104 static int zoneinfo_open(struct inode *inode, struct file *file)
106 return seq_open(file, &zoneinfo_op);
109 static const struct file_operations proc_zoneinfo_file_operations = {
110 .open = zoneinfo_open,
113 .release = seq_release,
116 extern const struct seq_operations cpuinfo_op;
117 static int cpuinfo_open(struct inode *inode, struct file *file)
119 return seq_open(file, &cpuinfo_op);
122 static const struct file_operations proc_cpuinfo_operations = {
123 .open = cpuinfo_open,
126 .release = seq_release,
129 static int devinfo_show(struct seq_file *f, void *v)
131 int i = *(loff_t *) v;
133 if (i < CHRDEV_MAJOR_HASH_SIZE) {
135 seq_printf(f, "Character devices:\n");
140 i -= CHRDEV_MAJOR_HASH_SIZE;
142 seq_printf(f, "\nBlock devices:\n");
149 static void *devinfo_start(struct seq_file *f, loff_t *pos)
151 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
156 static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
159 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
164 static void devinfo_stop(struct seq_file *f, void *v)
169 static const struct seq_operations devinfo_ops = {
170 .start = devinfo_start,
171 .next = devinfo_next,
172 .stop = devinfo_stop,
176 static int devinfo_open(struct inode *inode, struct file *filp)
178 return seq_open(filp, &devinfo_ops);
181 static const struct file_operations proc_devinfo_operations = {
182 .open = devinfo_open,
185 .release = seq_release,
188 static int vmstat_open(struct inode *inode, struct file *file)
190 return seq_open(file, &vmstat_op);
192 static const struct file_operations proc_vmstat_file_operations = {
196 .release = seq_release,
200 static int partitions_open(struct inode *inode, struct file *file)
202 return seq_open(file, &partitions_op);
204 static const struct file_operations proc_partitions_operations = {
205 .open = partitions_open,
208 .release = seq_release,
211 static int diskstats_open(struct inode *inode, struct file *file)
213 return seq_open(file, &diskstats_op);
215 static const struct file_operations proc_diskstats_operations = {
216 .open = diskstats_open,
219 .release = seq_release,
223 #ifdef CONFIG_MODULES
224 extern const struct seq_operations modules_op;
225 static int modules_open(struct inode *inode, struct file *file)
227 return seq_open(file, &modules_op);
229 static const struct file_operations proc_modules_operations = {
230 .open = modules_open,
233 .release = seq_release,
237 #ifdef CONFIG_SLABINFO
238 static int slabinfo_open(struct inode *inode, struct file *file)
240 return seq_open(file, &slabinfo_op);
242 static const struct file_operations proc_slabinfo_operations = {
243 .open = slabinfo_open,
245 .write = slabinfo_write,
247 .release = seq_release,
250 #ifdef CONFIG_DEBUG_SLAB_LEAK
251 extern const struct seq_operations slabstats_op;
252 static int slabstats_open(struct inode *inode, struct file *file)
254 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
257 ret = seq_open(file, &slabstats_op);
259 struct seq_file *m = file->private_data;
260 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
269 static const struct file_operations proc_slabstats_operations = {
270 .open = slabstats_open,
273 .release = seq_release_private,
279 static int vmalloc_open(struct inode *inode, struct file *file)
281 unsigned int *ptr = NULL;
285 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
286 ret = seq_open(file, &vmalloc_op);
288 struct seq_file *m = file->private_data;
295 static const struct file_operations proc_vmalloc_operations = {
296 .open = vmalloc_open,
299 .release = seq_release_private,
303 #ifndef arch_irq_stat_cpu
304 #define arch_irq_stat_cpu(cpu) 0
306 #ifndef arch_irq_stat
307 #define arch_irq_stat() 0
310 static int show_stat(struct seq_file *p, void *v)
314 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
317 struct timespec boottime;
318 unsigned int per_irq_sum;
320 user = nice = system = idle = iowait =
321 irq = softirq = steal = cputime64_zero;
322 guest = cputime64_zero;
323 getboottime(&boottime);
324 jif = boottime.tv_sec;
326 for_each_possible_cpu(i) {
327 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
328 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
329 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
330 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
331 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
332 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
333 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
334 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
335 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
338 sum += kstat_irqs_cpu(j, i);
340 sum += arch_irq_stat_cpu(i);
342 sum += arch_irq_stat();
344 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
345 (unsigned long long)cputime64_to_clock_t(user),
346 (unsigned long long)cputime64_to_clock_t(nice),
347 (unsigned long long)cputime64_to_clock_t(system),
348 (unsigned long long)cputime64_to_clock_t(idle),
349 (unsigned long long)cputime64_to_clock_t(iowait),
350 (unsigned long long)cputime64_to_clock_t(irq),
351 (unsigned long long)cputime64_to_clock_t(softirq),
352 (unsigned long long)cputime64_to_clock_t(steal),
353 (unsigned long long)cputime64_to_clock_t(guest));
354 for_each_online_cpu(i) {
356 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
357 user = kstat_cpu(i).cpustat.user;
358 nice = kstat_cpu(i).cpustat.nice;
359 system = kstat_cpu(i).cpustat.system;
360 idle = kstat_cpu(i).cpustat.idle;
361 iowait = kstat_cpu(i).cpustat.iowait;
362 irq = kstat_cpu(i).cpustat.irq;
363 softirq = kstat_cpu(i).cpustat.softirq;
364 steal = kstat_cpu(i).cpustat.steal;
365 guest = kstat_cpu(i).cpustat.guest;
367 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
369 (unsigned long long)cputime64_to_clock_t(user),
370 (unsigned long long)cputime64_to_clock_t(nice),
371 (unsigned long long)cputime64_to_clock_t(system),
372 (unsigned long long)cputime64_to_clock_t(idle),
373 (unsigned long long)cputime64_to_clock_t(iowait),
374 (unsigned long long)cputime64_to_clock_t(irq),
375 (unsigned long long)cputime64_to_clock_t(softirq),
376 (unsigned long long)cputime64_to_clock_t(steal),
377 (unsigned long long)cputime64_to_clock_t(guest));
379 seq_printf(p, "intr %llu", (unsigned long long)sum);
381 /* sum again ? it could be updated? */
385 for_each_possible_cpu(i)
386 per_irq_sum += kstat_irqs_cpu(j, i);
388 seq_printf(p, " %u", per_irq_sum);
395 "procs_running %lu\n"
396 "procs_blocked %lu\n",
397 nr_context_switches(),
406 static int stat_open(struct inode *inode, struct file *file)
408 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
413 /* don't ask for more than the kmalloc() max size, currently 128 KB */
414 if (size > 128 * 1024)
416 buf = kmalloc(size, GFP_KERNEL);
420 res = single_open(file, show_stat, NULL);
422 m = file->private_data;
429 static const struct file_operations proc_stat_operations = {
433 .release = single_release,
439 static void *int_seq_start(struct seq_file *f, loff_t *pos)
441 return (*pos <= nr_irqs) ? pos : NULL;
445 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
448 return (*pos <= nr_irqs) ? pos : NULL;
451 static void int_seq_stop(struct seq_file *f, void *v)
456 static const struct seq_operations int_seq_ops = {
457 .start = int_seq_start,
458 .next = int_seq_next,
459 .stop = int_seq_stop,
460 .show = show_interrupts
463 static int interrupts_open(struct inode *inode, struct file *filp)
465 return seq_open(filp, &int_seq_ops);
468 static const struct file_operations proc_interrupts_operations = {
469 .open = interrupts_open,
472 .release = seq_release,
475 #ifdef CONFIG_FILE_LOCKING
476 static int locks_open(struct inode *inode, struct file *filp)
478 return seq_open(filp, &locks_seq_operations);
481 static const struct file_operations proc_locks_operations = {
485 .release = seq_release,
487 #endif /* CONFIG_FILE_LOCKING */
489 static int execdomains_read_proc(char *page, char **start, off_t off,
490 int count, int *eof, void *data)
492 int len = get_exec_domain_list(page);
493 return proc_calc_metrics(page, start, off, count, eof, len);
496 #ifdef CONFIG_PROC_PAGE_MONITOR
497 #define KPMSIZE sizeof(u64)
498 #define KPMMASK (KPMSIZE - 1)
499 /* /proc/kpagecount - an array exposing page counts
501 * Each entry is a u64 representing the corresponding
502 * physical page count.
504 static ssize_t kpagecount_read(struct file *file, char __user *buf,
505 size_t count, loff_t *ppos)
507 u64 __user *out = (u64 __user *)buf;
509 unsigned long src = *ppos;
515 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
516 if (src & KPMMASK || count & KPMMASK)
522 ppage = pfn_to_page(pfn);
527 pcount = page_mapcount(ppage);
529 if (put_user(pcount, out++)) {
537 *ppos += (char __user *)out - buf;
539 ret = (char __user *)out - buf;
543 static struct file_operations proc_kpagecount_operations = {
545 .read = kpagecount_read,
548 /* /proc/kpageflags - an array exposing page flags
550 * Each entry is a u64 representing the corresponding
551 * physical page flags.
554 /* These macros are used to decouple internal flags from exported ones */
558 #define KPF_REFERENCED 2
559 #define KPF_UPTODATE 3
564 #define KPF_WRITEBACK 8
565 #define KPF_RECLAIM 9
568 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
570 static ssize_t kpageflags_read(struct file *file, char __user *buf,
571 size_t count, loff_t *ppos)
573 u64 __user *out = (u64 __user *)buf;
575 unsigned long src = *ppos;
581 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
582 if (src & KPMMASK || count & KPMMASK)
588 ppage = pfn_to_page(pfn);
593 kflags = ppage->flags;
595 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
596 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
597 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
598 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
599 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
600 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
601 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
602 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
603 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
604 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
605 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
607 if (put_user(uflags, out++)) {
615 *ppos += (char __user *)out - buf;
617 ret = (char __user *)out - buf;
621 static struct file_operations proc_kpageflags_operations = {
623 .read = kpageflags_read,
625 #endif /* CONFIG_PROC_PAGE_MONITOR */
627 struct proc_dir_entry *proc_root_kcore;
629 void __init proc_misc_init(void)
633 int (*read_proc)(char*,char**,off_t,int,int*,void*);
634 } *p, simple_ones[] = {
635 {"execdomains", execdomains_read_proc},
638 for (p = simple_ones; p->name; p++)
639 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
641 proc_symlink("mounts", NULL, "self/mounts");
643 /* And now for trickier ones */
645 proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
647 #ifdef CONFIG_FILE_LOCKING
648 proc_create("locks", 0, NULL, &proc_locks_operations);
650 proc_create("devices", 0, NULL, &proc_devinfo_operations);
651 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
653 proc_create("partitions", 0, NULL, &proc_partitions_operations);
655 proc_create("stat", 0, NULL, &proc_stat_operations);
656 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
657 #ifdef CONFIG_SLABINFO
658 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
659 #ifdef CONFIG_DEBUG_SLAB_LEAK
660 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
664 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
666 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
667 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
668 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
669 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
671 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
673 #ifdef CONFIG_MODULES
674 proc_create("modules", 0, NULL, &proc_modules_operations);
676 #ifdef CONFIG_SCHEDSTATS
677 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
679 #ifdef CONFIG_PROC_KCORE
680 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
682 proc_root_kcore->size =
683 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
685 #ifdef CONFIG_PROC_PAGE_MONITOR
686 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
687 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
689 #ifdef CONFIG_PROC_VMCORE
690 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);