]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/proc/proc_misc.c
8974809be5f6f00db276df84d1fd525073a96b73
[linux-2.6-omap-h63xx.git] / fs / proc / proc_misc.c
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>
23 #include <linux/fs.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>
30 #include <linux/mm.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>
55 #include <asm/io.h>
56 #include <asm/tlb.h>
57 #include <asm/div64.h>
58 #include "internal.h"
59
60 static int fragmentation_open(struct inode *inode, struct file *file)
61 {
62         (void)inode;
63         return seq_open(file, &fragmentation_op);
64 }
65
66 static const struct file_operations fragmentation_file_operations = {
67         .open           = fragmentation_open,
68         .read           = seq_read,
69         .llseek         = seq_lseek,
70         .release        = seq_release,
71 };
72
73 static int pagetypeinfo_open(struct inode *inode, struct file *file)
74 {
75         return seq_open(file, &pagetypeinfo_op);
76 }
77
78 static const struct file_operations pagetypeinfo_file_ops = {
79         .open           = pagetypeinfo_open,
80         .read           = seq_read,
81         .llseek         = seq_lseek,
82         .release        = seq_release,
83 };
84
85 static int zoneinfo_open(struct inode *inode, struct file *file)
86 {
87         return seq_open(file, &zoneinfo_op);
88 }
89
90 static const struct file_operations proc_zoneinfo_file_operations = {
91         .open           = zoneinfo_open,
92         .read           = seq_read,
93         .llseek         = seq_lseek,
94         .release        = seq_release,
95 };
96
97 static int vmstat_open(struct inode *inode, struct file *file)
98 {
99         return seq_open(file, &vmstat_op);
100 }
101 static const struct file_operations proc_vmstat_file_operations = {
102         .open           = vmstat_open,
103         .read           = seq_read,
104         .llseek         = seq_lseek,
105         .release        = seq_release,
106 };
107
108 #ifdef CONFIG_BLOCK
109 static int partitions_open(struct inode *inode, struct file *file)
110 {
111         return seq_open(file, &partitions_op);
112 }
113 static const struct file_operations proc_partitions_operations = {
114         .open           = partitions_open,
115         .read           = seq_read,
116         .llseek         = seq_lseek,
117         .release        = seq_release,
118 };
119
120 static int diskstats_open(struct inode *inode, struct file *file)
121 {
122         return seq_open(file, &diskstats_op);
123 }
124 static const struct file_operations proc_diskstats_operations = {
125         .open           = diskstats_open,
126         .read           = seq_read,
127         .llseek         = seq_lseek,
128         .release        = seq_release,
129 };
130 #endif
131
132 #ifdef CONFIG_MODULES
133 extern const struct seq_operations modules_op;
134 static int modules_open(struct inode *inode, struct file *file)
135 {
136         return seq_open(file, &modules_op);
137 }
138 static const struct file_operations proc_modules_operations = {
139         .open           = modules_open,
140         .read           = seq_read,
141         .llseek         = seq_lseek,
142         .release        = seq_release,
143 };
144 #endif
145
146 #ifdef CONFIG_SLABINFO
147 static int slabinfo_open(struct inode *inode, struct file *file)
148 {
149         return seq_open(file, &slabinfo_op);
150 }
151 static const struct file_operations proc_slabinfo_operations = {
152         .open           = slabinfo_open,
153         .read           = seq_read,
154         .write          = slabinfo_write,
155         .llseek         = seq_lseek,
156         .release        = seq_release,
157 };
158
159 #ifdef CONFIG_DEBUG_SLAB_LEAK
160 extern const struct seq_operations slabstats_op;
161 static int slabstats_open(struct inode *inode, struct file *file)
162 {
163         unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
164         int ret = -ENOMEM;
165         if (n) {
166                 ret = seq_open(file, &slabstats_op);
167                 if (!ret) {
168                         struct seq_file *m = file->private_data;
169                         *n = PAGE_SIZE / (2 * sizeof(unsigned long));
170                         m->private = n;
171                         n = NULL;
172                 }
173                 kfree(n);
174         }
175         return ret;
176 }
177
178 static const struct file_operations proc_slabstats_operations = {
179         .open           = slabstats_open,
180         .read           = seq_read,
181         .llseek         = seq_lseek,
182         .release        = seq_release_private,
183 };
184 #endif
185 #endif
186
187 #ifdef CONFIG_MMU
188 static int vmalloc_open(struct inode *inode, struct file *file)
189 {
190         unsigned int *ptr = NULL;
191         int ret;
192
193         if (NUMA_BUILD)
194                 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
195         ret = seq_open(file, &vmalloc_op);
196         if (!ret) {
197                 struct seq_file *m = file->private_data;
198                 m->private = ptr;
199         } else
200                 kfree(ptr);
201         return ret;
202 }
203
204 static const struct file_operations proc_vmalloc_operations = {
205         .open           = vmalloc_open,
206         .read           = seq_read,
207         .llseek         = seq_lseek,
208         .release        = seq_release_private,
209 };
210 #endif
211
212 #ifndef arch_irq_stat_cpu
213 #define arch_irq_stat_cpu(cpu) 0
214 #endif
215 #ifndef arch_irq_stat
216 #define arch_irq_stat() 0
217 #endif
218
219 static int show_stat(struct seq_file *p, void *v)
220 {
221         int i, j;
222         unsigned long jif;
223         cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
224         cputime64_t guest;
225         u64 sum = 0;
226         struct timespec boottime;
227         unsigned int per_irq_sum;
228
229         user = nice = system = idle = iowait =
230                 irq = softirq = steal = cputime64_zero;
231         guest = cputime64_zero;
232         getboottime(&boottime);
233         jif = boottime.tv_sec;
234
235         for_each_possible_cpu(i) {
236                 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
237                 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
238                 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
239                 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
240                 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
241                 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
242                 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
243                 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
244                 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
245
246                 for_each_irq_nr(j)
247                         sum += kstat_irqs_cpu(j, i);
248
249                 sum += arch_irq_stat_cpu(i);
250         }
251         sum += arch_irq_stat();
252
253         seq_printf(p, "cpu  %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
254                 (unsigned long long)cputime64_to_clock_t(user),
255                 (unsigned long long)cputime64_to_clock_t(nice),
256                 (unsigned long long)cputime64_to_clock_t(system),
257                 (unsigned long long)cputime64_to_clock_t(idle),
258                 (unsigned long long)cputime64_to_clock_t(iowait),
259                 (unsigned long long)cputime64_to_clock_t(irq),
260                 (unsigned long long)cputime64_to_clock_t(softirq),
261                 (unsigned long long)cputime64_to_clock_t(steal),
262                 (unsigned long long)cputime64_to_clock_t(guest));
263         for_each_online_cpu(i) {
264
265                 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
266                 user = kstat_cpu(i).cpustat.user;
267                 nice = kstat_cpu(i).cpustat.nice;
268                 system = kstat_cpu(i).cpustat.system;
269                 idle = kstat_cpu(i).cpustat.idle;
270                 iowait = kstat_cpu(i).cpustat.iowait;
271                 irq = kstat_cpu(i).cpustat.irq;
272                 softirq = kstat_cpu(i).cpustat.softirq;
273                 steal = kstat_cpu(i).cpustat.steal;
274                 guest = kstat_cpu(i).cpustat.guest;
275                 seq_printf(p,
276                         "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
277                         i,
278                         (unsigned long long)cputime64_to_clock_t(user),
279                         (unsigned long long)cputime64_to_clock_t(nice),
280                         (unsigned long long)cputime64_to_clock_t(system),
281                         (unsigned long long)cputime64_to_clock_t(idle),
282                         (unsigned long long)cputime64_to_clock_t(iowait),
283                         (unsigned long long)cputime64_to_clock_t(irq),
284                         (unsigned long long)cputime64_to_clock_t(softirq),
285                         (unsigned long long)cputime64_to_clock_t(steal),
286                         (unsigned long long)cputime64_to_clock_t(guest));
287         }
288         seq_printf(p, "intr %llu", (unsigned long long)sum);
289
290         /* sum again ? it could be updated? */
291         for_each_irq_nr(j) {
292                 per_irq_sum = 0;
293
294                 for_each_possible_cpu(i)
295                         per_irq_sum += kstat_irqs_cpu(j, i);
296
297                 seq_printf(p, " %u", per_irq_sum);
298         }
299
300         seq_printf(p,
301                 "\nctxt %llu\n"
302                 "btime %lu\n"
303                 "processes %lu\n"
304                 "procs_running %lu\n"
305                 "procs_blocked %lu\n",
306                 nr_context_switches(),
307                 (unsigned long)jif,
308                 total_forks,
309                 nr_running(),
310                 nr_iowait());
311
312         return 0;
313 }
314
315 static int stat_open(struct inode *inode, struct file *file)
316 {
317         unsigned size = 4096 * (1 + num_possible_cpus() / 32);
318         char *buf;
319         struct seq_file *m;
320         int res;
321
322         /* don't ask for more than the kmalloc() max size, currently 128 KB */
323         if (size > 128 * 1024)
324                 size = 128 * 1024;
325         buf = kmalloc(size, GFP_KERNEL);
326         if (!buf)
327                 return -ENOMEM;
328
329         res = single_open(file, show_stat, NULL);
330         if (!res) {
331                 m = file->private_data;
332                 m->buf = buf;
333                 m->size = size;
334         } else
335                 kfree(buf);
336         return res;
337 }
338 static const struct file_operations proc_stat_operations = {
339         .open           = stat_open,
340         .read           = seq_read,
341         .llseek         = seq_lseek,
342         .release        = single_release,
343 };
344
345 /*
346  * /proc/interrupts
347  */
348 static void *int_seq_start(struct seq_file *f, loff_t *pos)
349 {
350         return (*pos <= nr_irqs) ? pos : NULL;
351 }
352
353
354 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
355 {
356         (*pos)++;
357         return (*pos <= nr_irqs) ? pos : NULL;
358 }
359
360 static void int_seq_stop(struct seq_file *f, void *v)
361 {
362         /* Nothing to do */
363 }
364
365 static const struct seq_operations int_seq_ops = {
366         .start = int_seq_start,
367         .next  = int_seq_next,
368         .stop  = int_seq_stop,
369         .show  = show_interrupts
370 };
371
372 static int interrupts_open(struct inode *inode, struct file *filp)
373 {
374         return seq_open(filp, &int_seq_ops);
375 }
376
377 static const struct file_operations proc_interrupts_operations = {
378         .open           = interrupts_open,
379         .read           = seq_read,
380         .llseek         = seq_lseek,
381         .release        = seq_release,
382 };
383
384 #ifdef CONFIG_PROC_PAGE_MONITOR
385 #define KPMSIZE sizeof(u64)
386 #define KPMMASK (KPMSIZE - 1)
387 /* /proc/kpagecount - an array exposing page counts
388  *
389  * Each entry is a u64 representing the corresponding
390  * physical page count.
391  */
392 static ssize_t kpagecount_read(struct file *file, char __user *buf,
393                              size_t count, loff_t *ppos)
394 {
395         u64 __user *out = (u64 __user *)buf;
396         struct page *ppage;
397         unsigned long src = *ppos;
398         unsigned long pfn;
399         ssize_t ret = 0;
400         u64 pcount;
401
402         pfn = src / KPMSIZE;
403         count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
404         if (src & KPMMASK || count & KPMMASK)
405                 return -EINVAL;
406
407         while (count > 0) {
408                 ppage = NULL;
409                 if (pfn_valid(pfn))
410                         ppage = pfn_to_page(pfn);
411                 pfn++;
412                 if (!ppage)
413                         pcount = 0;
414                 else
415                         pcount = page_mapcount(ppage);
416
417                 if (put_user(pcount, out++)) {
418                         ret = -EFAULT;
419                         break;
420                 }
421
422                 count -= KPMSIZE;
423         }
424
425         *ppos += (char __user *)out - buf;
426         if (!ret)
427                 ret = (char __user *)out - buf;
428         return ret;
429 }
430
431 static struct file_operations proc_kpagecount_operations = {
432         .llseek = mem_lseek,
433         .read = kpagecount_read,
434 };
435
436 /* /proc/kpageflags - an array exposing page flags
437  *
438  * Each entry is a u64 representing the corresponding
439  * physical page flags.
440  */
441
442 /* These macros are used to decouple internal flags from exported ones */
443
444 #define KPF_LOCKED     0
445 #define KPF_ERROR      1
446 #define KPF_REFERENCED 2
447 #define KPF_UPTODATE   3
448 #define KPF_DIRTY      4
449 #define KPF_LRU        5
450 #define KPF_ACTIVE     6
451 #define KPF_SLAB       7
452 #define KPF_WRITEBACK  8
453 #define KPF_RECLAIM    9
454 #define KPF_BUDDY     10
455
456 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
457
458 static ssize_t kpageflags_read(struct file *file, char __user *buf,
459                              size_t count, loff_t *ppos)
460 {
461         u64 __user *out = (u64 __user *)buf;
462         struct page *ppage;
463         unsigned long src = *ppos;
464         unsigned long pfn;
465         ssize_t ret = 0;
466         u64 kflags, uflags;
467
468         pfn = src / KPMSIZE;
469         count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
470         if (src & KPMMASK || count & KPMMASK)
471                 return -EINVAL;
472
473         while (count > 0) {
474                 ppage = NULL;
475                 if (pfn_valid(pfn))
476                         ppage = pfn_to_page(pfn);
477                 pfn++;
478                 if (!ppage)
479                         kflags = 0;
480                 else
481                         kflags = ppage->flags;
482
483                 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
484                         kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
485                         kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
486                         kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
487                         kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
488                         kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
489                         kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
490                         kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
491                         kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
492                         kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
493                         kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
494
495                 if (put_user(uflags, out++)) {
496                         ret = -EFAULT;
497                         break;
498                 }
499
500                 count -= KPMSIZE;
501         }
502
503         *ppos += (char __user *)out - buf;
504         if (!ret)
505                 ret = (char __user *)out - buf;
506         return ret;
507 }
508
509 static struct file_operations proc_kpageflags_operations = {
510         .llseek = mem_lseek,
511         .read = kpageflags_read,
512 };
513 #endif /* CONFIG_PROC_PAGE_MONITOR */
514
515 struct proc_dir_entry *proc_root_kcore;
516
517 void __init proc_misc_init(void)
518 {
519         proc_symlink("mounts", NULL, "self/mounts");
520
521         /* And now for trickier ones */
522 #ifdef CONFIG_BLOCK
523         proc_create("partitions", 0, NULL, &proc_partitions_operations);
524 #endif
525         proc_create("stat", 0, NULL, &proc_stat_operations);
526         proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
527 #ifdef CONFIG_SLABINFO
528         proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
529 #ifdef CONFIG_DEBUG_SLAB_LEAK
530         proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
531 #endif
532 #endif
533 #ifdef CONFIG_MMU
534         proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
535 #endif
536         proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
537         proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
538         proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
539         proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
540 #ifdef CONFIG_BLOCK
541         proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
542 #endif
543 #ifdef CONFIG_MODULES
544         proc_create("modules", 0, NULL, &proc_modules_operations);
545 #endif
546 #ifdef CONFIG_SCHEDSTATS
547         proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
548 #endif
549 #ifdef CONFIG_PROC_KCORE
550         proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
551         if (proc_root_kcore)
552                 proc_root_kcore->size =
553                                 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
554 #endif
555 #ifdef CONFIG_PROC_PAGE_MONITOR
556         proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
557         proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
558 #endif
559 #ifdef CONFIG_PROC_VMCORE
560         proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
561 #endif
562 }