| 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 | |
| Mel Gorman | 467c996 | 2007-10-16 01:26:02 -0700 | [diff] [blame] | 60 | static int pagetypeinfo_open(struct inode *inode, struct file *file) |
| 61 | { |
| 62 | return seq_open(file, &pagetypeinfo_op); |
| 63 | } |
| 64 | |
| 65 | static const struct file_operations pagetypeinfo_file_ops = { |
| 66 | .open = pagetypeinfo_open, |
| 67 | .read = seq_read, |
| 68 | .llseek = seq_lseek, |
| 69 | .release = seq_release, |
| 70 | }; |
| 71 | |
| Nikita Danilov | 295ab93 | 2005-06-21 17:14:38 -0700 | [diff] [blame] | 72 | static int zoneinfo_open(struct inode *inode, struct file *file) |
| 73 | { |
| 74 | return seq_open(file, &zoneinfo_op); |
| 75 | } |
| 76 | |
| Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 77 | static const struct file_operations proc_zoneinfo_file_operations = { |
| Nikita Danilov | 295ab93 | 2005-06-21 17:14:38 -0700 | [diff] [blame] | 78 | .open = zoneinfo_open, |
| 79 | .read = seq_read, |
| 80 | .llseek = seq_lseek, |
| 81 | .release = seq_release, |
| 82 | }; |
| 83 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | static int vmstat_open(struct inode *inode, struct file *file) |
| 85 | { |
| 86 | return seq_open(file, &vmstat_op); |
| 87 | } |
| Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 88 | static const struct file_operations proc_vmstat_file_operations = { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | .open = vmstat_open, |
| 90 | .read = seq_read, |
| 91 | .llseek = seq_lseek, |
| 92 | .release = seq_release, |
| 93 | }; |
| 94 | |
| David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 95 | #ifdef CONFIG_BLOCK |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | static int diskstats_open(struct inode *inode, struct file *file) |
| 97 | { |
| 98 | return seq_open(file, &diskstats_op); |
| 99 | } |
| Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 100 | static const struct file_operations proc_diskstats_operations = { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .open = diskstats_open, |
| 102 | .read = seq_read, |
| 103 | .llseek = seq_lseek, |
| 104 | .release = seq_release, |
| 105 | }; |
| David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 106 | #endif |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | #ifdef CONFIG_MODULES |
| Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 109 | extern const struct seq_operations modules_op; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | static int modules_open(struct inode *inode, struct file *file) |
| 111 | { |
| 112 | return seq_open(file, &modules_op); |
| 113 | } |
| Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 114 | static const struct file_operations proc_modules_operations = { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .open = modules_open, |
| 116 | .read = seq_read, |
| 117 | .llseek = seq_lseek, |
| 118 | .release = seq_release, |
| 119 | }; |
| 120 | #endif |
| 121 | |
| Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 122 | #ifdef CONFIG_PROC_PAGE_MONITOR |
| Matt Mackall | 161f47b | 2008-02-04 22:29:05 -0800 | [diff] [blame] | 123 | #define KPMSIZE sizeof(u64) |
| 124 | #define KPMMASK (KPMSIZE - 1) |
| 125 | /* /proc/kpagecount - an array exposing page counts |
| 126 | * |
| 127 | * Each entry is a u64 representing the corresponding |
| 128 | * physical page count. |
| 129 | */ |
| 130 | static ssize_t kpagecount_read(struct file *file, char __user *buf, |
| 131 | size_t count, loff_t *ppos) |
| 132 | { |
| 133 | u64 __user *out = (u64 __user *)buf; |
| 134 | struct page *ppage; |
| 135 | unsigned long src = *ppos; |
| 136 | unsigned long pfn; |
| 137 | ssize_t ret = 0; |
| 138 | u64 pcount; |
| 139 | |
| 140 | pfn = src / KPMSIZE; |
| 141 | count = min_t(size_t, count, (max_pfn * KPMSIZE) - src); |
| 142 | if (src & KPMMASK || count & KPMMASK) |
| Thomas Tuttle | 4710d1a | 2008-06-05 22:46:58 -0700 | [diff] [blame] | 143 | return -EINVAL; |
| Matt Mackall | 161f47b | 2008-02-04 22:29:05 -0800 | [diff] [blame] | 144 | |
| 145 | while (count > 0) { |
| Matt Mackall | 304daa8 | 2008-02-04 22:29:06 -0800 | [diff] [blame] | 146 | ppage = NULL; |
| 147 | if (pfn_valid(pfn)) |
| 148 | ppage = pfn_to_page(pfn); |
| 149 | pfn++; |
| Matt Mackall | 161f47b | 2008-02-04 22:29:05 -0800 | [diff] [blame] | 150 | if (!ppage) |
| 151 | pcount = 0; |
| 152 | else |
| Thomas Tuttle | bbcdac0 | 2008-06-05 22:46:58 -0700 | [diff] [blame] | 153 | pcount = page_mapcount(ppage); |
| Matt Mackall | 161f47b | 2008-02-04 22:29:05 -0800 | [diff] [blame] | 154 | |
| 155 | if (put_user(pcount, out++)) { |
| 156 | ret = -EFAULT; |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | count -= KPMSIZE; |
| 161 | } |
| 162 | |
| 163 | *ppos += (char __user *)out - buf; |
| 164 | if (!ret) |
| 165 | ret = (char __user *)out - buf; |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static struct file_operations proc_kpagecount_operations = { |
| 170 | .llseek = mem_lseek, |
| 171 | .read = kpagecount_read, |
| 172 | }; |
| 173 | |
| Matt Mackall | 304daa8 | 2008-02-04 22:29:06 -0800 | [diff] [blame] | 174 | /* /proc/kpageflags - an array exposing page flags |
| 175 | * |
| 176 | * Each entry is a u64 representing the corresponding |
| 177 | * physical page flags. |
| 178 | */ |
| 179 | |
| 180 | /* These macros are used to decouple internal flags from exported ones */ |
| 181 | |
| 182 | #define KPF_LOCKED 0 |
| 183 | #define KPF_ERROR 1 |
| 184 | #define KPF_REFERENCED 2 |
| 185 | #define KPF_UPTODATE 3 |
| 186 | #define KPF_DIRTY 4 |
| 187 | #define KPF_LRU 5 |
| 188 | #define KPF_ACTIVE 6 |
| 189 | #define KPF_SLAB 7 |
| 190 | #define KPF_WRITEBACK 8 |
| 191 | #define KPF_RECLAIM 9 |
| 192 | #define KPF_BUDDY 10 |
| 193 | |
| 194 | #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos) |
| 195 | |
| 196 | static ssize_t kpageflags_read(struct file *file, char __user *buf, |
| 197 | size_t count, loff_t *ppos) |
| 198 | { |
| 199 | u64 __user *out = (u64 __user *)buf; |
| 200 | struct page *ppage; |
| 201 | unsigned long src = *ppos; |
| 202 | unsigned long pfn; |
| 203 | ssize_t ret = 0; |
| 204 | u64 kflags, uflags; |
| 205 | |
| 206 | pfn = src / KPMSIZE; |
| 207 | count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src); |
| 208 | if (src & KPMMASK || count & KPMMASK) |
| Thomas Tuttle | 4710d1a | 2008-06-05 22:46:58 -0700 | [diff] [blame] | 209 | return -EINVAL; |
| Matt Mackall | 304daa8 | 2008-02-04 22:29:06 -0800 | [diff] [blame] | 210 | |
| 211 | while (count > 0) { |
| 212 | ppage = NULL; |
| 213 | if (pfn_valid(pfn)) |
| 214 | ppage = pfn_to_page(pfn); |
| 215 | pfn++; |
| 216 | if (!ppage) |
| 217 | kflags = 0; |
| 218 | else |
| 219 | kflags = ppage->flags; |
| 220 | |
| 221 | uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) | |
| 222 | kpf_copy_bit(kflags, KPF_ERROR, PG_error) | |
| 223 | kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) | |
| 224 | kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) | |
| 225 | kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) | |
| 226 | kpf_copy_bit(kflags, KPF_LRU, PG_lru) | |
| 227 | kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) | |
| 228 | kpf_copy_bit(kflags, KPF_SLAB, PG_slab) | |
| 229 | kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) | |
| 230 | kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) | |
| 231 | kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy); |
| 232 | |
| 233 | if (put_user(uflags, out++)) { |
| 234 | ret = -EFAULT; |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | count -= KPMSIZE; |
| 239 | } |
| 240 | |
| 241 | *ppos += (char __user *)out - buf; |
| 242 | if (!ret) |
| 243 | ret = (char __user *)out - buf; |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static struct file_operations proc_kpageflags_operations = { |
| 248 | .llseek = mem_lseek, |
| 249 | .read = kpageflags_read, |
| 250 | }; |
| Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 251 | #endif /* CONFIG_PROC_PAGE_MONITOR */ |
| Matt Mackall | 304daa8 | 2008-02-04 22:29:06 -0800 | [diff] [blame] | 252 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | struct proc_dir_entry *proc_root_kcore; |
| 254 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | void __init proc_misc_init(void) |
| 256 | { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | proc_symlink("mounts", NULL, "self/mounts"); |
| 258 | |
| 259 | /* And now for trickier ones */ |
| Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 260 | proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops); |
| 261 | proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations); |
| 262 | proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations); |
| David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 263 | #ifdef CONFIG_BLOCK |
| Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 264 | proc_create("diskstats", 0, NULL, &proc_diskstats_operations); |
| David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 265 | #endif |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | #ifdef CONFIG_MODULES |
| Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 267 | proc_create("modules", 0, NULL, &proc_modules_operations); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | #endif |
| 269 | #ifdef CONFIG_SCHEDSTATS |
| Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 270 | proc_create("schedstat", 0, NULL, &proc_schedstat_operations); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | #endif |
| 272 | #ifdef CONFIG_PROC_KCORE |
| Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 273 | proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations); |
| 274 | if (proc_root_kcore) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | proc_root_kcore->size = |
| 276 | (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | #endif |
| Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 278 | #ifdef CONFIG_PROC_PAGE_MONITOR |
| Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 279 | proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations); |
| 280 | proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations); |
| Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 281 | #endif |
| Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 282 | #ifdef CONFIG_PROC_VMCORE |
| Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 283 | proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations); |
| Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 284 | #endif |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |