Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/proc/proc_misc.c |
| 3 | * |
| 4 | * linux/fs/proc/array.c |
| 5 | * Copyright (C) 1992 by Linus Torvalds |
| 6 | * based on ideas by Darren Senn |
| 7 | * |
| 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. |
| 12 | * |
| 13 | * Changes: |
| 14 | * Fulton Green : Encapsulated position metric calculations. |
| 15 | * <kernel@FultonGreen.com> |
| 16 | */ |
| 17 | |
| 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> |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 23 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/tty.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/mman.h> |
KOSAKI Motohiro | 4b85615 | 2008-09-02 14:35:53 -0700 | [diff] [blame] | 27 | #include <linux/quicklist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/proc_fs.h> |
| 29 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/mm.h> |
| 31 | #include <linux/mmzone.h> |
| 32 | #include <linux/pagemap.h> |
Yinghai Lu | c7fb03a | 2008-08-19 20:50:12 -0700 | [diff] [blame] | 33 | #include <linux/irq.h> |
Adrian Bunk | f74596d | 2008-02-06 01:36:35 -0800 | [diff] [blame] | 34 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/swap.h> |
| 36 | #include <linux/slab.h> |
Adrian Bunk | a0db701 | 2008-03-04 11:23:50 +0100 | [diff] [blame] | 37 | #include <linux/genhd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/smp.h> |
| 39 | #include <linux/signal.h> |
| 40 | #include <linux/module.h> |
| 41 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/seq_file.h> |
| 43 | #include <linux/times.h> |
| 44 | #include <linux/profile.h> |
Andrew Morton | 7bf6538 | 2006-12-08 02:41:14 -0800 | [diff] [blame] | 45 | #include <linux/utsname.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/blkdev.h> |
| 47 | #include <linux/hugetlb.h> |
| 48 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/vmalloc.h> |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 50 | #include <linux/crash_dump.h> |
Sukadev Bhattiprolu | 61a58c6 | 2006-12-08 02:37:58 -0800 | [diff] [blame] | 51 | #include <linux/pid_namespace.h> |
Matt Mackall | 161f47b | 2008-02-04 22:29:05 -0800 | [diff] [blame] | 52 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <asm/uaccess.h> |
| 54 | #include <asm/pgtable.h> |
| 55 | #include <asm/io.h> |
| 56 | #include <asm/tlb.h> |
| 57 | #include <asm/div64.h> |
| 58 | #include "internal.h" |
| 59 | |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 60 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Matt Mackall | 161f47b | 2008-02-04 22:29:05 -0800 | [diff] [blame] | 61 | #define KPMSIZE sizeof(u64) |
| 62 | #define KPMMASK (KPMSIZE - 1) |
| 63 | /* /proc/kpagecount - an array exposing page counts |
| 64 | * |
| 65 | * Each entry is a u64 representing the corresponding |
| 66 | * physical page count. |
| 67 | */ |
| 68 | static ssize_t kpagecount_read(struct file *file, char __user *buf, |
| 69 | size_t count, loff_t *ppos) |
| 70 | { |
| 71 | u64 __user *out = (u64 __user *)buf; |
| 72 | struct page *ppage; |
| 73 | unsigned long src = *ppos; |
| 74 | unsigned long pfn; |
| 75 | ssize_t ret = 0; |
| 76 | u64 pcount; |
| 77 | |
| 78 | pfn = src / KPMSIZE; |
| 79 | count = min_t(size_t, count, (max_pfn * KPMSIZE) - src); |
| 80 | if (src & KPMMASK || count & KPMMASK) |
Thomas Tuttle | 4710d1a | 2008-06-05 22:46:58 -0700 | [diff] [blame] | 81 | return -EINVAL; |
Matt Mackall | 161f47b | 2008-02-04 22:29:05 -0800 | [diff] [blame] | 82 | |
| 83 | while (count > 0) { |
Matt Mackall | 304daa8 | 2008-02-04 22:29:06 -0800 | [diff] [blame] | 84 | ppage = NULL; |
| 85 | if (pfn_valid(pfn)) |
| 86 | ppage = pfn_to_page(pfn); |
| 87 | pfn++; |
Matt Mackall | 161f47b | 2008-02-04 22:29:05 -0800 | [diff] [blame] | 88 | if (!ppage) |
| 89 | pcount = 0; |
| 90 | else |
Thomas Tuttle | bbcdac0 | 2008-06-05 22:46:58 -0700 | [diff] [blame] | 91 | pcount = page_mapcount(ppage); |
Matt Mackall | 161f47b | 2008-02-04 22:29:05 -0800 | [diff] [blame] | 92 | |
| 93 | if (put_user(pcount, out++)) { |
| 94 | ret = -EFAULT; |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | count -= KPMSIZE; |
| 99 | } |
| 100 | |
| 101 | *ppos += (char __user *)out - buf; |
| 102 | if (!ret) |
| 103 | ret = (char __user *)out - buf; |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static struct file_operations proc_kpagecount_operations = { |
| 108 | .llseek = mem_lseek, |
| 109 | .read = kpagecount_read, |
| 110 | }; |
| 111 | |
Matt Mackall | 304daa8 | 2008-02-04 22:29:06 -0800 | [diff] [blame] | 112 | /* /proc/kpageflags - an array exposing page flags |
| 113 | * |
| 114 | * Each entry is a u64 representing the corresponding |
| 115 | * physical page flags. |
| 116 | */ |
| 117 | |
| 118 | /* These macros are used to decouple internal flags from exported ones */ |
| 119 | |
| 120 | #define KPF_LOCKED 0 |
| 121 | #define KPF_ERROR 1 |
| 122 | #define KPF_REFERENCED 2 |
| 123 | #define KPF_UPTODATE 3 |
| 124 | #define KPF_DIRTY 4 |
| 125 | #define KPF_LRU 5 |
| 126 | #define KPF_ACTIVE 6 |
| 127 | #define KPF_SLAB 7 |
| 128 | #define KPF_WRITEBACK 8 |
| 129 | #define KPF_RECLAIM 9 |
| 130 | #define KPF_BUDDY 10 |
| 131 | |
| 132 | #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos) |
| 133 | |
| 134 | static ssize_t kpageflags_read(struct file *file, char __user *buf, |
| 135 | size_t count, loff_t *ppos) |
| 136 | { |
| 137 | u64 __user *out = (u64 __user *)buf; |
| 138 | struct page *ppage; |
| 139 | unsigned long src = *ppos; |
| 140 | unsigned long pfn; |
| 141 | ssize_t ret = 0; |
| 142 | u64 kflags, uflags; |
| 143 | |
| 144 | pfn = src / KPMSIZE; |
| 145 | count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src); |
| 146 | if (src & KPMMASK || count & KPMMASK) |
Thomas Tuttle | 4710d1a | 2008-06-05 22:46:58 -0700 | [diff] [blame] | 147 | return -EINVAL; |
Matt Mackall | 304daa8 | 2008-02-04 22:29:06 -0800 | [diff] [blame] | 148 | |
| 149 | while (count > 0) { |
| 150 | ppage = NULL; |
| 151 | if (pfn_valid(pfn)) |
| 152 | ppage = pfn_to_page(pfn); |
| 153 | pfn++; |
| 154 | if (!ppage) |
| 155 | kflags = 0; |
| 156 | else |
| 157 | kflags = ppage->flags; |
| 158 | |
| 159 | uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) | |
| 160 | kpf_copy_bit(kflags, KPF_ERROR, PG_error) | |
| 161 | kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) | |
| 162 | kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) | |
| 163 | kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) | |
| 164 | kpf_copy_bit(kflags, KPF_LRU, PG_lru) | |
| 165 | kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) | |
| 166 | kpf_copy_bit(kflags, KPF_SLAB, PG_slab) | |
| 167 | kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) | |
| 168 | kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) | |
| 169 | kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy); |
| 170 | |
| 171 | if (put_user(uflags, out++)) { |
| 172 | ret = -EFAULT; |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | count -= KPMSIZE; |
| 177 | } |
| 178 | |
| 179 | *ppos += (char __user *)out - buf; |
| 180 | if (!ret) |
| 181 | ret = (char __user *)out - buf; |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static struct file_operations proc_kpageflags_operations = { |
| 186 | .llseek = mem_lseek, |
| 187 | .read = kpageflags_read, |
| 188 | }; |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 189 | #endif /* CONFIG_PROC_PAGE_MONITOR */ |
Matt Mackall | 304daa8 | 2008-02-04 22:29:06 -0800 | [diff] [blame] | 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | struct proc_dir_entry *proc_root_kcore; |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | void __init proc_misc_init(void) |
| 194 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | proc_symlink("mounts", NULL, "self/mounts"); |
| 196 | |
| 197 | /* And now for trickier ones */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | #ifdef CONFIG_SCHEDSTATS |
Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 199 | proc_create("schedstat", 0, NULL, &proc_schedstat_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | #endif |
| 201 | #ifdef CONFIG_PROC_KCORE |
Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 202 | proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations); |
| 203 | if (proc_root_kcore) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | proc_root_kcore->size = |
| 205 | (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | #endif |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 207 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 208 | proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations); |
| 209 | proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations); |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 210 | #endif |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 211 | #ifdef CONFIG_PROC_VMCORE |
Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 212 | proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 213 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |