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>
60 static int fragmentation_open(struct inode *inode, struct file *file)
63 return seq_open(file, &fragmentation_op);
66 static const struct file_operations fragmentation_file_operations = {
67 .open = fragmentation_open,
70 .release = seq_release,
73 static int pagetypeinfo_open(struct inode *inode, struct file *file)
75 return seq_open(file, &pagetypeinfo_op);
78 static const struct file_operations pagetypeinfo_file_ops = {
79 .open = pagetypeinfo_open,
82 .release = seq_release,
85 static int zoneinfo_open(struct inode *inode, struct file *file)
87 return seq_open(file, &zoneinfo_op);
90 static const struct file_operations proc_zoneinfo_file_operations = {
91 .open = zoneinfo_open,
94 .release = seq_release,
97 extern const struct seq_operations cpuinfo_op;
98 static int cpuinfo_open(struct inode *inode, struct file *file)
100 return seq_open(file, &cpuinfo_op);
103 static const struct file_operations proc_cpuinfo_operations = {
104 .open = cpuinfo_open,
107 .release = seq_release,
110 static int devinfo_show(struct seq_file *f, void *v)
112 int i = *(loff_t *) v;
114 if (i < CHRDEV_MAJOR_HASH_SIZE) {
116 seq_printf(f, "Character devices:\n");
121 i -= CHRDEV_MAJOR_HASH_SIZE;
123 seq_printf(f, "\nBlock devices:\n");
130 static void *devinfo_start(struct seq_file *f, loff_t *pos)
132 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
137 static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
140 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
145 static void devinfo_stop(struct seq_file *f, void *v)
150 static const struct seq_operations devinfo_ops = {
151 .start = devinfo_start,
152 .next = devinfo_next,
153 .stop = devinfo_stop,
157 static int devinfo_open(struct inode *inode, struct file *filp)
159 return seq_open(filp, &devinfo_ops);
162 static const struct file_operations proc_devinfo_operations = {
163 .open = devinfo_open,
166 .release = seq_release,
169 static int vmstat_open(struct inode *inode, struct file *file)
171 return seq_open(file, &vmstat_op);
173 static const struct file_operations proc_vmstat_file_operations = {
177 .release = seq_release,
181 static int partitions_open(struct inode *inode, struct file *file)
183 return seq_open(file, &partitions_op);
185 static const struct file_operations proc_partitions_operations = {
186 .open = partitions_open,
189 .release = seq_release,
192 static int diskstats_open(struct inode *inode, struct file *file)
194 return seq_open(file, &diskstats_op);
196 static const struct file_operations proc_diskstats_operations = {
197 .open = diskstats_open,
200 .release = seq_release,
204 #ifdef CONFIG_MODULES
205 extern const struct seq_operations modules_op;
206 static int modules_open(struct inode *inode, struct file *file)
208 return seq_open(file, &modules_op);
210 static const struct file_operations proc_modules_operations = {
211 .open = modules_open,
214 .release = seq_release,
218 #ifdef CONFIG_SLABINFO
219 static int slabinfo_open(struct inode *inode, struct file *file)
221 return seq_open(file, &slabinfo_op);
223 static const struct file_operations proc_slabinfo_operations = {
224 .open = slabinfo_open,
226 .write = slabinfo_write,
228 .release = seq_release,
231 #ifdef CONFIG_DEBUG_SLAB_LEAK
232 extern const struct seq_operations slabstats_op;
233 static int slabstats_open(struct inode *inode, struct file *file)
235 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
238 ret = seq_open(file, &slabstats_op);
240 struct seq_file *m = file->private_data;
241 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
250 static const struct file_operations proc_slabstats_operations = {
251 .open = slabstats_open,
254 .release = seq_release_private,
260 static int vmalloc_open(struct inode *inode, struct file *file)
262 unsigned int *ptr = NULL;
266 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
267 ret = seq_open(file, &vmalloc_op);
269 struct seq_file *m = file->private_data;
276 static const struct file_operations proc_vmalloc_operations = {
277 .open = vmalloc_open,
280 .release = seq_release_private,
284 #ifndef arch_irq_stat_cpu
285 #define arch_irq_stat_cpu(cpu) 0
287 #ifndef arch_irq_stat
288 #define arch_irq_stat() 0
291 static int show_stat(struct seq_file *p, void *v)
295 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
298 struct timespec boottime;
299 unsigned int per_irq_sum;
301 user = nice = system = idle = iowait =
302 irq = softirq = steal = cputime64_zero;
303 guest = cputime64_zero;
304 getboottime(&boottime);
305 jif = boottime.tv_sec;
307 for_each_possible_cpu(i) {
308 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
309 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
310 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
311 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
312 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
313 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
314 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
315 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
316 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
319 sum += kstat_irqs_cpu(j, i);
321 sum += arch_irq_stat_cpu(i);
323 sum += arch_irq_stat();
325 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
326 (unsigned long long)cputime64_to_clock_t(user),
327 (unsigned long long)cputime64_to_clock_t(nice),
328 (unsigned long long)cputime64_to_clock_t(system),
329 (unsigned long long)cputime64_to_clock_t(idle),
330 (unsigned long long)cputime64_to_clock_t(iowait),
331 (unsigned long long)cputime64_to_clock_t(irq),
332 (unsigned long long)cputime64_to_clock_t(softirq),
333 (unsigned long long)cputime64_to_clock_t(steal),
334 (unsigned long long)cputime64_to_clock_t(guest));
335 for_each_online_cpu(i) {
337 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
338 user = kstat_cpu(i).cpustat.user;
339 nice = kstat_cpu(i).cpustat.nice;
340 system = kstat_cpu(i).cpustat.system;
341 idle = kstat_cpu(i).cpustat.idle;
342 iowait = kstat_cpu(i).cpustat.iowait;
343 irq = kstat_cpu(i).cpustat.irq;
344 softirq = kstat_cpu(i).cpustat.softirq;
345 steal = kstat_cpu(i).cpustat.steal;
346 guest = kstat_cpu(i).cpustat.guest;
348 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
350 (unsigned long long)cputime64_to_clock_t(user),
351 (unsigned long long)cputime64_to_clock_t(nice),
352 (unsigned long long)cputime64_to_clock_t(system),
353 (unsigned long long)cputime64_to_clock_t(idle),
354 (unsigned long long)cputime64_to_clock_t(iowait),
355 (unsigned long long)cputime64_to_clock_t(irq),
356 (unsigned long long)cputime64_to_clock_t(softirq),
357 (unsigned long long)cputime64_to_clock_t(steal),
358 (unsigned long long)cputime64_to_clock_t(guest));
360 seq_printf(p, "intr %llu", (unsigned long long)sum);
362 /* sum again ? it could be updated? */
366 for_each_possible_cpu(i)
367 per_irq_sum += kstat_irqs_cpu(j, i);
369 seq_printf(p, " %u", per_irq_sum);
376 "procs_running %lu\n"
377 "procs_blocked %lu\n",
378 nr_context_switches(),
387 static int stat_open(struct inode *inode, struct file *file)
389 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
394 /* don't ask for more than the kmalloc() max size, currently 128 KB */
395 if (size > 128 * 1024)
397 buf = kmalloc(size, GFP_KERNEL);
401 res = single_open(file, show_stat, NULL);
403 m = file->private_data;
410 static const struct file_operations proc_stat_operations = {
414 .release = single_release,
420 static void *int_seq_start(struct seq_file *f, loff_t *pos)
422 return (*pos <= nr_irqs) ? pos : NULL;
426 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
429 return (*pos <= nr_irqs) ? pos : NULL;
432 static void int_seq_stop(struct seq_file *f, void *v)
437 static const struct seq_operations int_seq_ops = {
438 .start = int_seq_start,
439 .next = int_seq_next,
440 .stop = int_seq_stop,
441 .show = show_interrupts
444 static int interrupts_open(struct inode *inode, struct file *filp)
446 return seq_open(filp, &int_seq_ops);
449 static const struct file_operations proc_interrupts_operations = {
450 .open = interrupts_open,
453 .release = seq_release,
456 #ifdef CONFIG_PROC_PAGE_MONITOR
457 #define KPMSIZE sizeof(u64)
458 #define KPMMASK (KPMSIZE - 1)
459 /* /proc/kpagecount - an array exposing page counts
461 * Each entry is a u64 representing the corresponding
462 * physical page count.
464 static ssize_t kpagecount_read(struct file *file, char __user *buf,
465 size_t count, loff_t *ppos)
467 u64 __user *out = (u64 __user *)buf;
469 unsigned long src = *ppos;
475 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
476 if (src & KPMMASK || count & KPMMASK)
482 ppage = pfn_to_page(pfn);
487 pcount = page_mapcount(ppage);
489 if (put_user(pcount, out++)) {
497 *ppos += (char __user *)out - buf;
499 ret = (char __user *)out - buf;
503 static struct file_operations proc_kpagecount_operations = {
505 .read = kpagecount_read,
508 /* /proc/kpageflags - an array exposing page flags
510 * Each entry is a u64 representing the corresponding
511 * physical page flags.
514 /* These macros are used to decouple internal flags from exported ones */
518 #define KPF_REFERENCED 2
519 #define KPF_UPTODATE 3
524 #define KPF_WRITEBACK 8
525 #define KPF_RECLAIM 9
528 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
530 static ssize_t kpageflags_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(unsigned long, count, (max_pfn * KPMSIZE) - src);
542 if (src & KPMMASK || count & KPMMASK)
548 ppage = pfn_to_page(pfn);
553 kflags = ppage->flags;
555 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
556 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
557 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
558 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
559 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
560 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
561 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
562 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
563 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
564 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
565 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
567 if (put_user(uflags, out++)) {
575 *ppos += (char __user *)out - buf;
577 ret = (char __user *)out - buf;
581 static struct file_operations proc_kpageflags_operations = {
583 .read = kpageflags_read,
585 #endif /* CONFIG_PROC_PAGE_MONITOR */
587 struct proc_dir_entry *proc_root_kcore;
589 void __init proc_misc_init(void)
591 proc_symlink("mounts", NULL, "self/mounts");
593 /* And now for trickier ones */
594 proc_create("devices", 0, NULL, &proc_devinfo_operations);
595 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
597 proc_create("partitions", 0, NULL, &proc_partitions_operations);
599 proc_create("stat", 0, NULL, &proc_stat_operations);
600 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
601 #ifdef CONFIG_SLABINFO
602 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
603 #ifdef CONFIG_DEBUG_SLAB_LEAK
604 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
608 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
610 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
611 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
612 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
613 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
615 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
617 #ifdef CONFIG_MODULES
618 proc_create("modules", 0, NULL, &proc_modules_operations);
620 #ifdef CONFIG_SCHEDSTATS
621 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
623 #ifdef CONFIG_PROC_KCORE
624 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
626 proc_root_kcore->size =
627 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
629 #ifdef CONFIG_PROC_PAGE_MONITOR
630 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
631 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
633 #ifdef CONFIG_PROC_VMCORE
634 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);