[PATCH] meminfo: add Cached underflow check
[linux-3.10.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/tty.h>
24 #include <linux/string.h>
25 #include <linux/mman.h>
26 #include <linux/proc_fs.h>
27 #include <linux/ioport.h>
28 #include <linux/config.h>
29 #include <linux/mm.h>
30 #include <linux/mmzone.h>
31 #include <linux/pagemap.h>
32 #include <linux/swap.h>
33 #include <linux/slab.h>
34 #include <linux/smp.h>
35 #include <linux/signal.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/smp_lock.h>
39 #include <linux/seq_file.h>
40 #include <linux/times.h>
41 #include <linux/profile.h>
42 #include <linux/blkdev.h>
43 #include <linux/hugetlb.h>
44 #include <linux/jiffies.h>
45 #include <linux/sysrq.h>
46 #include <linux/vmalloc.h>
47 #include <asm/uaccess.h>
48 #include <asm/pgtable.h>
49 #include <asm/io.h>
50 #include <asm/tlb.h>
51 #include <asm/div64.h>
52 #include "internal.h"
53
54 #define LOAD_INT(x) ((x) >> FSHIFT)
55 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
56 /*
57  * Warning: stuff below (imported functions) assumes that its output will fit
58  * into one page. For some of those functions it may be wrong. Moreover, we
59  * have a way to deal with that gracefully. Right now I used straightforward
60  * wrappers, but this needs further analysis wrt potential overflows.
61  */
62 extern int get_hardware_list(char *);
63 extern int get_stram_list(char *);
64 extern int get_chrdev_list(char *);
65 extern int get_filesystem_list(char *);
66 extern int get_exec_domain_list(char *);
67 extern int get_dma_list(char *);
68 extern int get_locks_status (char *, char **, off_t, int);
69
70 static int proc_calc_metrics(char *page, char **start, off_t off,
71                                  int count, int *eof, int len)
72 {
73         if (len <= off+count) *eof = 1;
74         *start = page + off;
75         len -= off;
76         if (len>count) len = count;
77         if (len<0) len = 0;
78         return len;
79 }
80
81 static int loadavg_read_proc(char *page, char **start, off_t off,
82                                  int count, int *eof, void *data)
83 {
84         int a, b, c;
85         int len;
86
87         a = avenrun[0] + (FIXED_1/200);
88         b = avenrun[1] + (FIXED_1/200);
89         c = avenrun[2] + (FIXED_1/200);
90         len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
91                 LOAD_INT(a), LOAD_FRAC(a),
92                 LOAD_INT(b), LOAD_FRAC(b),
93                 LOAD_INT(c), LOAD_FRAC(c),
94                 nr_running(), nr_threads, last_pid);
95         return proc_calc_metrics(page, start, off, count, eof, len);
96 }
97
98 static int uptime_read_proc(char *page, char **start, off_t off,
99                                  int count, int *eof, void *data)
100 {
101         struct timespec uptime;
102         struct timespec idle;
103         int len;
104         cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
105
106         do_posix_clock_monotonic_gettime(&uptime);
107         cputime_to_timespec(idletime, &idle);
108         len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
109                         (unsigned long) uptime.tv_sec,
110                         (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
111                         (unsigned long) idle.tv_sec,
112                         (idle.tv_nsec / (NSEC_PER_SEC / 100)));
113
114         return proc_calc_metrics(page, start, off, count, eof, len);
115 }
116
117 static int meminfo_read_proc(char *page, char **start, off_t off,
118                                  int count, int *eof, void *data)
119 {
120         struct sysinfo i;
121         int len;
122         struct page_state ps;
123         unsigned long inactive;
124         unsigned long active;
125         unsigned long free;
126         unsigned long committed;
127         unsigned long allowed;
128         struct vmalloc_info vmi;
129         long cached;
130
131         get_page_state(&ps);
132         get_zone_counts(&active, &inactive, &free);
133
134 /*
135  * display in kilobytes.
136  */
137 #define K(x) ((x) << (PAGE_SHIFT - 10))
138         si_meminfo(&i);
139         si_swapinfo(&i);
140         committed = atomic_read(&vm_committed_space);
141         allowed = ((totalram_pages - hugetlb_total_pages())
142                 * sysctl_overcommit_ratio / 100) + total_swap_pages;
143
144         cached = get_page_cache_size() - total_swapcache_pages - i.bufferram;
145         if (cached < 0)
146                 cached = 0;
147
148         get_vmalloc_info(&vmi);
149
150         /*
151          * Tagged format, for easy grepping and expansion.
152          */
153         len = sprintf(page,
154                 "MemTotal:     %8lu kB\n"
155                 "MemFree:      %8lu kB\n"
156                 "Buffers:      %8lu kB\n"
157                 "Cached:       %8lu kB\n"
158                 "SwapCached:   %8lu kB\n"
159                 "Active:       %8lu kB\n"
160                 "Inactive:     %8lu kB\n"
161                 "HighTotal:    %8lu kB\n"
162                 "HighFree:     %8lu kB\n"
163                 "LowTotal:     %8lu kB\n"
164                 "LowFree:      %8lu kB\n"
165                 "SwapTotal:    %8lu kB\n"
166                 "SwapFree:     %8lu kB\n"
167                 "Dirty:        %8lu kB\n"
168                 "Writeback:    %8lu kB\n"
169                 "Mapped:       %8lu kB\n"
170                 "Slab:         %8lu kB\n"
171                 "CommitLimit:  %8lu kB\n"
172                 "Committed_AS: %8lu kB\n"
173                 "PageTables:   %8lu kB\n"
174                 "VmallocTotal: %8lu kB\n"
175                 "VmallocUsed:  %8lu kB\n"
176                 "VmallocChunk: %8lu kB\n",
177                 K(i.totalram),
178                 K(i.freeram),
179                 K(i.bufferram),
180                 K(cached),
181                 K(total_swapcache_pages),
182                 K(active),
183                 K(inactive),
184                 K(i.totalhigh),
185                 K(i.freehigh),
186                 K(i.totalram-i.totalhigh),
187                 K(i.freeram-i.freehigh),
188                 K(i.totalswap),
189                 K(i.freeswap),
190                 K(ps.nr_dirty),
191                 K(ps.nr_writeback),
192                 K(ps.nr_mapped),
193                 K(ps.nr_slab),
194                 K(allowed),
195                 K(committed),
196                 K(ps.nr_page_table_pages),
197                 (unsigned long)VMALLOC_TOTAL >> 10,
198                 vmi.used >> 10,
199                 vmi.largest_chunk >> 10
200                 );
201
202                 len += hugetlb_report_meminfo(page + len);
203
204         return proc_calc_metrics(page, start, off, count, eof, len);
205 #undef K
206 }
207
208 extern struct seq_operations fragmentation_op;
209 static int fragmentation_open(struct inode *inode, struct file *file)
210 {
211         (void)inode;
212         return seq_open(file, &fragmentation_op);
213 }
214
215 static struct file_operations fragmentation_file_operations = {
216         .open           = fragmentation_open,
217         .read           = seq_read,
218         .llseek         = seq_lseek,
219         .release        = seq_release,
220 };
221
222 static int version_read_proc(char *page, char **start, off_t off,
223                                  int count, int *eof, void *data)
224 {
225         int len;
226
227         strcpy(page, linux_banner);
228         len = strlen(page);
229         return proc_calc_metrics(page, start, off, count, eof, len);
230 }
231
232 extern struct seq_operations cpuinfo_op;
233 static int cpuinfo_open(struct inode *inode, struct file *file)
234 {
235         return seq_open(file, &cpuinfo_op);
236 }
237 static struct file_operations proc_cpuinfo_operations = {
238         .open           = cpuinfo_open,
239         .read           = seq_read,
240         .llseek         = seq_lseek,
241         .release        = seq_release,
242 };
243
244 extern struct seq_operations vmstat_op;
245 static int vmstat_open(struct inode *inode, struct file *file)
246 {
247         return seq_open(file, &vmstat_op);
248 }
249 static struct file_operations proc_vmstat_file_operations = {
250         .open           = vmstat_open,
251         .read           = seq_read,
252         .llseek         = seq_lseek,
253         .release        = seq_release,
254 };
255
256 #ifdef CONFIG_PROC_HARDWARE
257 static int hardware_read_proc(char *page, char **start, off_t off,
258                                  int count, int *eof, void *data)
259 {
260         int len = get_hardware_list(page);
261         return proc_calc_metrics(page, start, off, count, eof, len);
262 }
263 #endif
264
265 #ifdef CONFIG_STRAM_PROC
266 static int stram_read_proc(char *page, char **start, off_t off,
267                                  int count, int *eof, void *data)
268 {
269         int len = get_stram_list(page);
270         return proc_calc_metrics(page, start, off, count, eof, len);
271 }
272 #endif
273
274 extern struct seq_operations partitions_op;
275 static int partitions_open(struct inode *inode, struct file *file)
276 {
277         return seq_open(file, &partitions_op);
278 }
279 static struct file_operations proc_partitions_operations = {
280         .open           = partitions_open,
281         .read           = seq_read,
282         .llseek         = seq_lseek,
283         .release        = seq_release,
284 };
285
286 extern struct seq_operations diskstats_op;
287 static int diskstats_open(struct inode *inode, struct file *file)
288 {
289         return seq_open(file, &diskstats_op);
290 }
291 static struct file_operations proc_diskstats_operations = {
292         .open           = diskstats_open,
293         .read           = seq_read,
294         .llseek         = seq_lseek,
295         .release        = seq_release,
296 };
297
298 #ifdef CONFIG_MODULES
299 extern struct seq_operations modules_op;
300 static int modules_open(struct inode *inode, struct file *file)
301 {
302         return seq_open(file, &modules_op);
303 }
304 static struct file_operations proc_modules_operations = {
305         .open           = modules_open,
306         .read           = seq_read,
307         .llseek         = seq_lseek,
308         .release        = seq_release,
309 };
310 #endif
311
312 extern struct seq_operations slabinfo_op;
313 extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);
314 static int slabinfo_open(struct inode *inode, struct file *file)
315 {
316         return seq_open(file, &slabinfo_op);
317 }
318 static struct file_operations proc_slabinfo_operations = {
319         .open           = slabinfo_open,
320         .read           = seq_read,
321         .write          = slabinfo_write,
322         .llseek         = seq_lseek,
323         .release        = seq_release,
324 };
325
326 static int show_stat(struct seq_file *p, void *v)
327 {
328         int i;
329         unsigned long jif;
330         cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
331         u64 sum = 0;
332
333         user = nice = system = idle = iowait =
334                 irq = softirq = steal = cputime64_zero;
335         jif = - wall_to_monotonic.tv_sec;
336         if (wall_to_monotonic.tv_nsec)
337                 --jif;
338
339         for_each_cpu(i) {
340                 int j;
341
342                 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
343                 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
344                 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
345                 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
346                 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
347                 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
348                 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
349                 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
350                 for (j = 0 ; j < NR_IRQS ; j++)
351                         sum += kstat_cpu(i).irqs[j];
352         }
353
354         seq_printf(p, "cpu  %llu %llu %llu %llu %llu %llu %llu %llu\n",
355                 (unsigned long long)cputime64_to_clock_t(user),
356                 (unsigned long long)cputime64_to_clock_t(nice),
357                 (unsigned long long)cputime64_to_clock_t(system),
358                 (unsigned long long)cputime64_to_clock_t(idle),
359                 (unsigned long long)cputime64_to_clock_t(iowait),
360                 (unsigned long long)cputime64_to_clock_t(irq),
361                 (unsigned long long)cputime64_to_clock_t(softirq),
362                 (unsigned long long)cputime64_to_clock_t(steal));
363         for_each_online_cpu(i) {
364
365                 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
366                 user = kstat_cpu(i).cpustat.user;
367                 nice = kstat_cpu(i).cpustat.nice;
368                 system = kstat_cpu(i).cpustat.system;
369                 idle = kstat_cpu(i).cpustat.idle;
370                 iowait = kstat_cpu(i).cpustat.iowait;
371                 irq = kstat_cpu(i).cpustat.irq;
372                 softirq = kstat_cpu(i).cpustat.softirq;
373                 steal = kstat_cpu(i).cpustat.steal;
374                 seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu\n",
375                         i,
376                         (unsigned long long)cputime64_to_clock_t(user),
377                         (unsigned long long)cputime64_to_clock_t(nice),
378                         (unsigned long long)cputime64_to_clock_t(system),
379                         (unsigned long long)cputime64_to_clock_t(idle),
380                         (unsigned long long)cputime64_to_clock_t(iowait),
381                         (unsigned long long)cputime64_to_clock_t(irq),
382                         (unsigned long long)cputime64_to_clock_t(softirq),
383                         (unsigned long long)cputime64_to_clock_t(steal));
384         }
385         seq_printf(p, "intr %llu", (unsigned long long)sum);
386
387 #if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA)
388         for (i = 0; i < NR_IRQS; i++)
389                 seq_printf(p, " %u", kstat_irqs(i));
390 #endif
391
392         seq_printf(p,
393                 "\nctxt %llu\n"
394                 "btime %lu\n"
395                 "processes %lu\n"
396                 "procs_running %lu\n"
397                 "procs_blocked %lu\n",
398                 nr_context_switches(),
399                 (unsigned long)jif,
400                 total_forks,
401                 nr_running(),
402                 nr_iowait());
403
404         return 0;
405 }
406
407 static int stat_open(struct inode *inode, struct file *file)
408 {
409         unsigned size = 4096 * (1 + num_possible_cpus() / 32);
410         char *buf;
411         struct seq_file *m;
412         int res;
413
414         /* don't ask for more than the kmalloc() max size, currently 128 KB */
415         if (size > 128 * 1024)
416                 size = 128 * 1024;
417         buf = kmalloc(size, GFP_KERNEL);
418         if (!buf)
419                 return -ENOMEM;
420
421         res = single_open(file, show_stat, NULL);
422         if (!res) {
423                 m = file->private_data;
424                 m->buf = buf;
425                 m->size = size;
426         } else
427                 kfree(buf);
428         return res;
429 }
430 static struct file_operations proc_stat_operations = {
431         .open           = stat_open,
432         .read           = seq_read,
433         .llseek         = seq_lseek,
434         .release        = single_release,
435 };
436
437 static int devices_read_proc(char *page, char **start, off_t off,
438                                  int count, int *eof, void *data)
439 {
440         int len = get_chrdev_list(page);
441         len += get_blkdev_list(page+len);
442         return proc_calc_metrics(page, start, off, count, eof, len);
443 }
444
445 /*
446  * /proc/interrupts
447  */
448 static void *int_seq_start(struct seq_file *f, loff_t *pos)
449 {
450         return (*pos <= NR_IRQS) ? pos : NULL;
451 }
452
453 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
454 {
455         (*pos)++;
456         if (*pos > NR_IRQS)
457                 return NULL;
458         return pos;
459 }
460
461 static void int_seq_stop(struct seq_file *f, void *v)
462 {
463         /* Nothing to do */
464 }
465
466
467 extern int show_interrupts(struct seq_file *f, void *v); /* In arch code */
468 static struct seq_operations int_seq_ops = {
469         .start = int_seq_start,
470         .next  = int_seq_next,
471         .stop  = int_seq_stop,
472         .show  = show_interrupts
473 };
474
475 static int interrupts_open(struct inode *inode, struct file *filp)
476 {
477         return seq_open(filp, &int_seq_ops);
478 }
479
480 static struct file_operations proc_interrupts_operations = {
481         .open           = interrupts_open,
482         .read           = seq_read,
483         .llseek         = seq_lseek,
484         .release        = seq_release,
485 };
486
487 static int filesystems_read_proc(char *page, char **start, off_t off,
488                                  int count, int *eof, void *data)
489 {
490         int len = get_filesystem_list(page);
491         return proc_calc_metrics(page, start, off, count, eof, len);
492 }
493
494 static int cmdline_read_proc(char *page, char **start, off_t off,
495                                  int count, int *eof, void *data)
496 {
497         int len;
498
499         len = sprintf(page, "%s\n", saved_command_line);
500         return proc_calc_metrics(page, start, off, count, eof, len);
501 }
502
503 static int locks_read_proc(char *page, char **start, off_t off,
504                                  int count, int *eof, void *data)
505 {
506         int len = get_locks_status(page, start, off, count);
507
508         if (len < count)
509                 *eof = 1;
510         return len;
511 }
512
513 static int execdomains_read_proc(char *page, char **start, off_t off,
514                                  int count, int *eof, void *data)
515 {
516         int len = get_exec_domain_list(page);
517         return proc_calc_metrics(page, start, off, count, eof, len);
518 }
519
520 #ifdef CONFIG_MAGIC_SYSRQ
521 /*
522  * writing 'C' to /proc/sysrq-trigger is like sysrq-C
523  */
524 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
525                                    size_t count, loff_t *ppos)
526 {
527         if (count) {
528                 char c;
529
530                 if (get_user(c, buf))
531                         return -EFAULT;
532                 __handle_sysrq(c, NULL, NULL, 0);
533         }
534         return count;
535 }
536
537 static struct file_operations proc_sysrq_trigger_operations = {
538         .write          = write_sysrq_trigger,
539 };
540 #endif
541
542 struct proc_dir_entry *proc_root_kcore;
543
544 void create_seq_entry(char *name, mode_t mode, struct file_operations *f)
545 {
546         struct proc_dir_entry *entry;
547         entry = create_proc_entry(name, mode, NULL);
548         if (entry)
549                 entry->proc_fops = f;
550 }
551
552 void __init proc_misc_init(void)
553 {
554         struct proc_dir_entry *entry;
555         static struct {
556                 char *name;
557                 int (*read_proc)(char*,char**,off_t,int,int*,void*);
558         } *p, simple_ones[] = {
559                 {"loadavg",     loadavg_read_proc},
560                 {"uptime",      uptime_read_proc},
561                 {"meminfo",     meminfo_read_proc},
562                 {"version",     version_read_proc},
563 #ifdef CONFIG_PROC_HARDWARE
564                 {"hardware",    hardware_read_proc},
565 #endif
566 #ifdef CONFIG_STRAM_PROC
567                 {"stram",       stram_read_proc},
568 #endif
569                 {"devices",     devices_read_proc},
570                 {"filesystems", filesystems_read_proc},
571                 {"cmdline",     cmdline_read_proc},
572                 {"locks",       locks_read_proc},
573                 {"execdomains", execdomains_read_proc},
574                 {NULL,}
575         };
576         for (p = simple_ones; p->name; p++)
577                 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
578
579         proc_symlink("mounts", NULL, "self/mounts");
580
581         /* And now for trickier ones */
582         entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
583         if (entry)
584                 entry->proc_fops = &proc_kmsg_operations;
585         create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
586         create_seq_entry("partitions", 0, &proc_partitions_operations);
587         create_seq_entry("stat", 0, &proc_stat_operations);
588         create_seq_entry("interrupts", 0, &proc_interrupts_operations);
589         create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
590         create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
591         create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
592         create_seq_entry("diskstats", 0, &proc_diskstats_operations);
593 #ifdef CONFIG_MODULES
594         create_seq_entry("modules", 0, &proc_modules_operations);
595 #endif
596 #ifdef CONFIG_SCHEDSTATS
597         create_seq_entry("schedstat", 0, &proc_schedstat_operations);
598 #endif
599 #ifdef CONFIG_PROC_KCORE
600         proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
601         if (proc_root_kcore) {
602                 proc_root_kcore->proc_fops = &proc_kcore_operations;
603                 proc_root_kcore->size =
604                                 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
605         }
606 #endif
607 #ifdef CONFIG_MAGIC_SYSRQ
608         entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
609         if (entry)
610                 entry->proc_fops = &proc_sysrq_trigger_operations;
611 #endif
612 #ifdef CONFIG_PPC32
613         {
614                 extern struct file_operations ppc_htab_operations;
615                 entry = create_proc_entry("ppc_htab", S_IRUGO|S_IWUSR, NULL);
616                 if (entry)
617                         entry->proc_fops = &ppc_htab_operations;
618         }
619 #endif
620 }