proc: loadavg reading race
[deliverable/linux.git] / fs / proc / proc_misc.c
CommitLineData
1da177e4
LT
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>
7170be5f 23#include <linux/fs.h>
1da177e4
LT
24#include <linux/tty.h>
25#include <linux/string.h>
26#include <linux/mman.h>
27#include <linux/proc_fs.h>
28#include <linux/ioport.h>
1da177e4
LT
29#include <linux/mm.h>
30#include <linux/mmzone.h>
31#include <linux/pagemap.h>
f74596d0 32#include <linux/interrupt.h>
1da177e4
LT
33#include <linux/swap.h>
34#include <linux/slab.h>
35#include <linux/smp.h>
36#include <linux/signal.h>
37#include <linux/module.h>
38#include <linux/init.h>
1da177e4
LT
39#include <linux/seq_file.h>
40#include <linux/times.h>
41#include <linux/profile.h>
7bf65382 42#include <linux/utsname.h>
1da177e4
LT
43#include <linux/blkdev.h>
44#include <linux/hugetlb.h>
45#include <linux/jiffies.h>
46#include <linux/sysrq.h>
47#include <linux/vmalloc.h>
666bfddb 48#include <linux/crash_dump.h>
61a58c6c 49#include <linux/pid_namespace.h>
161f47bf 50#include <linux/bootmem.h>
1da177e4
LT
51#include <asm/uaccess.h>
52#include <asm/pgtable.h>
53#include <asm/io.h>
54#include <asm/tlb.h>
55#include <asm/div64.h>
56#include "internal.h"
57
58#define LOAD_INT(x) ((x) >> FSHIFT)
59#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
60/*
61 * Warning: stuff below (imported functions) assumes that its output will fit
62 * into one page. For some of those functions it may be wrong. Moreover, we
63 * have a way to deal with that gracefully. Right now I used straightforward
64 * wrappers, but this needs further analysis wrt potential overflows.
65 */
66extern int get_hardware_list(char *);
67extern int get_stram_list(char *);
1da177e4
LT
68extern int get_exec_domain_list(char *);
69extern int get_dma_list(char *);
1da177e4
LT
70
71static int proc_calc_metrics(char *page, char **start, off_t off,
72 int count, int *eof, int len)
73{
74 if (len <= off+count) *eof = 1;
75 *start = page + off;
76 len -= off;
77 if (len>count) len = count;
78 if (len<0) len = 0;
79 return len;
80}
81
82static int loadavg_read_proc(char *page, char **start, off_t off,
83 int count, int *eof, void *data)
84{
85 int a, b, c;
86 int len;
07a154b2
MS
87 unsigned long seq;
88
89 do {
90 seq = read_seqbegin(&xtime_lock);
91 a = avenrun[0] + (FIXED_1/200);
92 b = avenrun[1] + (FIXED_1/200);
93 c = avenrun[2] + (FIXED_1/200);
94 } while (read_seqretry(&xtime_lock, seq));
1da177e4 95
1da177e4
LT
96 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
97 LOAD_INT(a), LOAD_FRAC(a),
98 LOAD_INT(b), LOAD_FRAC(b),
99 LOAD_INT(c), LOAD_FRAC(c),
2894d650
SB
100 nr_running(), nr_threads,
101 task_active_pid_ns(current)->last_pid);
1da177e4
LT
102 return proc_calc_metrics(page, start, off, count, eof, len);
103}
104
105static int uptime_read_proc(char *page, char **start, off_t off,
106 int count, int *eof, void *data)
107{
108 struct timespec uptime;
109 struct timespec idle;
110 int len;
111 cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
112
113 do_posix_clock_monotonic_gettime(&uptime);
d6214141 114 monotonic_to_bootbased(&uptime);
1da177e4
LT
115 cputime_to_timespec(idletime, &idle);
116 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
117 (unsigned long) uptime.tv_sec,
118 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
119 (unsigned long) idle.tv_sec,
120 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
121
122 return proc_calc_metrics(page, start, off, count, eof, len);
123}
124
125static int meminfo_read_proc(char *page, char **start, off_t off,
126 int count, int *eof, void *data)
127{
128 struct sysinfo i;
129 int len;
1da177e4
LT
130 unsigned long committed;
131 unsigned long allowed;
132 struct vmalloc_info vmi;
4c4c402d 133 long cached;
1da177e4 134
1da177e4
LT
135/*
136 * display in kilobytes.
137 */
138#define K(x) ((x) << (PAGE_SHIFT - 10))
139 si_meminfo(&i);
140 si_swapinfo(&i);
141 committed = atomic_read(&vm_committed_space);
142 allowed = ((totalram_pages - hugetlb_total_pages())
143 * sysctl_overcommit_ratio / 100) + total_swap_pages;
144
347ce434
CL
145 cached = global_page_state(NR_FILE_PAGES) -
146 total_swapcache_pages - i.bufferram;
4c4c402d
MH
147 if (cached < 0)
148 cached = 0;
149
1da177e4
LT
150 get_vmalloc_info(&vmi);
151
152 /*
153 * Tagged format, for easy grepping and expansion.
154 */
155 len = sprintf(page,
156 "MemTotal: %8lu kB\n"
157 "MemFree: %8lu kB\n"
158 "Buffers: %8lu kB\n"
159 "Cached: %8lu kB\n"
160 "SwapCached: %8lu kB\n"
161 "Active: %8lu kB\n"
162 "Inactive: %8lu kB\n"
182e8e23 163#ifdef CONFIG_HIGHMEM
1da177e4
LT
164 "HighTotal: %8lu kB\n"
165 "HighFree: %8lu kB\n"
166 "LowTotal: %8lu kB\n"
167 "LowFree: %8lu kB\n"
182e8e23 168#endif
1da177e4
LT
169 "SwapTotal: %8lu kB\n"
170 "SwapFree: %8lu kB\n"
171 "Dirty: %8lu kB\n"
172 "Writeback: %8lu kB\n"
f3dbd344 173 "AnonPages: %8lu kB\n"
1da177e4
LT
174 "Mapped: %8lu kB\n"
175 "Slab: %8lu kB\n"
972d1a7b
CL
176 "SReclaimable: %8lu kB\n"
177 "SUnreclaim: %8lu kB\n"
df849a15 178 "PageTables: %8lu kB\n"
f5ef68da 179 "NFS_Unstable: %8lu kB\n"
d2c5e30c 180 "Bounce: %8lu kB\n"
1da177e4
LT
181 "CommitLimit: %8lu kB\n"
182 "Committed_AS: %8lu kB\n"
1da177e4
LT
183 "VmallocTotal: %8lu kB\n"
184 "VmallocUsed: %8lu kB\n"
185 "VmallocChunk: %8lu kB\n",
186 K(i.totalram),
187 K(i.freeram),
188 K(i.bufferram),
4c4c402d 189 K(cached),
1da177e4 190 K(total_swapcache_pages),
65e458d4
CL
191 K(global_page_state(NR_ACTIVE)),
192 K(global_page_state(NR_INACTIVE)),
182e8e23 193#ifdef CONFIG_HIGHMEM
1da177e4
LT
194 K(i.totalhigh),
195 K(i.freehigh),
196 K(i.totalram-i.totalhigh),
197 K(i.freeram-i.freehigh),
182e8e23 198#endif
1da177e4
LT
199 K(i.totalswap),
200 K(i.freeswap),
b1e7a8fd 201 K(global_page_state(NR_FILE_DIRTY)),
ce866b34 202 K(global_page_state(NR_WRITEBACK)),
f3dbd344 203 K(global_page_state(NR_ANON_PAGES)),
65ba55f5 204 K(global_page_state(NR_FILE_MAPPED)),
972d1a7b
CL
205 K(global_page_state(NR_SLAB_RECLAIMABLE) +
206 global_page_state(NR_SLAB_UNRECLAIMABLE)),
207 K(global_page_state(NR_SLAB_RECLAIMABLE)),
208 K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
df849a15 209 K(global_page_state(NR_PAGETABLE)),
fd39fc85 210 K(global_page_state(NR_UNSTABLE_NFS)),
d2c5e30c 211 K(global_page_state(NR_BOUNCE)),
1da177e4
LT
212 K(allowed),
213 K(committed),
1da177e4
LT
214 (unsigned long)VMALLOC_TOTAL >> 10,
215 vmi.used >> 10,
216 vmi.largest_chunk >> 10
217 );
218
219 len += hugetlb_report_meminfo(page + len);
220
221 return proc_calc_metrics(page, start, off, count, eof, len);
222#undef K
223}
224
225extern struct seq_operations fragmentation_op;
226static int fragmentation_open(struct inode *inode, struct file *file)
227{
228 (void)inode;
229 return seq_open(file, &fragmentation_op);
230}
231
00977a59 232static const struct file_operations fragmentation_file_operations = {
1da177e4
LT
233 .open = fragmentation_open,
234 .read = seq_read,
235 .llseek = seq_lseek,
236 .release = seq_release,
237};
238
467c996c
MG
239extern struct seq_operations pagetypeinfo_op;
240static int pagetypeinfo_open(struct inode *inode, struct file *file)
241{
242 return seq_open(file, &pagetypeinfo_op);
243}
244
245static const struct file_operations pagetypeinfo_file_ops = {
246 .open = pagetypeinfo_open,
247 .read = seq_read,
248 .llseek = seq_lseek,
249 .release = seq_release,
250};
251
295ab934
ND
252extern struct seq_operations zoneinfo_op;
253static int zoneinfo_open(struct inode *inode, struct file *file)
254{
255 return seq_open(file, &zoneinfo_op);
256}
257
00977a59 258static const struct file_operations proc_zoneinfo_file_operations = {
295ab934
ND
259 .open = zoneinfo_open,
260 .read = seq_read,
261 .llseek = seq_lseek,
262 .release = seq_release,
263};
264
1da177e4
LT
265static int version_read_proc(char *page, char **start, off_t off,
266 int count, int *eof, void *data)
267{
268 int len;
269
3eb3c740 270 len = snprintf(page, PAGE_SIZE, linux_proc_banner,
8993780a
LT
271 utsname()->sysname,
272 utsname()->release,
273 utsname()->version);
1da177e4
LT
274 return proc_calc_metrics(page, start, off, count, eof, len);
275}
276
277extern struct seq_operations cpuinfo_op;
278static int cpuinfo_open(struct inode *inode, struct file *file)
279{
280 return seq_open(file, &cpuinfo_op);
281}
7170be5f 282
00977a59 283static const struct file_operations proc_cpuinfo_operations = {
68eef3b4
JK
284 .open = cpuinfo_open,
285 .read = seq_read,
286 .llseek = seq_lseek,
287 .release = seq_release,
7170be5f
NH
288};
289
68eef3b4 290static int devinfo_show(struct seq_file *f, void *v)
7170be5f 291{
68eef3b4
JK
292 int i = *(loff_t *) v;
293
294 if (i < CHRDEV_MAJOR_HASH_SIZE) {
295 if (i == 0)
296 seq_printf(f, "Character devices:\n");
297 chrdev_show(f, i);
9361401e
DH
298 }
299#ifdef CONFIG_BLOCK
300 else {
68eef3b4
JK
301 i -= CHRDEV_MAJOR_HASH_SIZE;
302 if (i == 0)
303 seq_printf(f, "\nBlock devices:\n");
304 blkdev_show(f, i);
7170be5f 305 }
9361401e 306#endif
68eef3b4 307 return 0;
7170be5f
NH
308}
309
68eef3b4 310static void *devinfo_start(struct seq_file *f, loff_t *pos)
7170be5f 311{
68eef3b4
JK
312 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
313 return pos;
314 return NULL;
7170be5f
NH
315}
316
68eef3b4 317static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
7170be5f 318{
68eef3b4
JK
319 (*pos)++;
320 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
321 return NULL;
322 return pos;
7170be5f
NH
323}
324
68eef3b4 325static void devinfo_stop(struct seq_file *f, void *v)
7170be5f 326{
68eef3b4 327 /* Nothing to do */
7170be5f
NH
328}
329
68eef3b4
JK
330static struct seq_operations devinfo_ops = {
331 .start = devinfo_start,
332 .next = devinfo_next,
333 .stop = devinfo_stop,
334 .show = devinfo_show
7170be5f
NH
335};
336
68eef3b4 337static int devinfo_open(struct inode *inode, struct file *filp)
7170be5f 338{
68eef3b4 339 return seq_open(filp, &devinfo_ops);
7170be5f
NH
340}
341
00977a59 342static const struct file_operations proc_devinfo_operations = {
7170be5f
NH
343 .open = devinfo_open,
344 .read = seq_read,
345 .llseek = seq_lseek,
346 .release = seq_release,
347};
348
1da177e4
LT
349extern struct seq_operations vmstat_op;
350static int vmstat_open(struct inode *inode, struct file *file)
351{
352 return seq_open(file, &vmstat_op);
353}
00977a59 354static const struct file_operations proc_vmstat_file_operations = {
1da177e4
LT
355 .open = vmstat_open,
356 .read = seq_read,
357 .llseek = seq_lseek,
358 .release = seq_release,
359};
360
361#ifdef CONFIG_PROC_HARDWARE
362static int hardware_read_proc(char *page, char **start, off_t off,
363 int count, int *eof, void *data)
364{
365 int len = get_hardware_list(page);
366 return proc_calc_metrics(page, start, off, count, eof, len);
367}
368#endif
369
370#ifdef CONFIG_STRAM_PROC
371static int stram_read_proc(char *page, char **start, off_t off,
372 int count, int *eof, void *data)
373{
374 int len = get_stram_list(page);
375 return proc_calc_metrics(page, start, off, count, eof, len);
376}
377#endif
378
9361401e 379#ifdef CONFIG_BLOCK
1da177e4
LT
380extern struct seq_operations partitions_op;
381static int partitions_open(struct inode *inode, struct file *file)
382{
383 return seq_open(file, &partitions_op);
384}
00977a59 385static const struct file_operations proc_partitions_operations = {
1da177e4
LT
386 .open = partitions_open,
387 .read = seq_read,
388 .llseek = seq_lseek,
389 .release = seq_release,
390};
391
392extern struct seq_operations diskstats_op;
393static int diskstats_open(struct inode *inode, struct file *file)
394{
395 return seq_open(file, &diskstats_op);
396}
00977a59 397static const struct file_operations proc_diskstats_operations = {
1da177e4
LT
398 .open = diskstats_open,
399 .read = seq_read,
400 .llseek = seq_lseek,
401 .release = seq_release,
402};
9361401e 403#endif
1da177e4
LT
404
405#ifdef CONFIG_MODULES
406extern struct seq_operations modules_op;
407static int modules_open(struct inode *inode, struct file *file)
408{
409 return seq_open(file, &modules_op);
410}
00977a59 411static const struct file_operations proc_modules_operations = {
1da177e4
LT
412 .open = modules_open,
413 .read = seq_read,
414 .llseek = seq_lseek,
415 .release = seq_release,
416};
417#endif
418
158a9624 419#ifdef CONFIG_SLABINFO
1da177e4
LT
420static int slabinfo_open(struct inode *inode, struct file *file)
421{
422 return seq_open(file, &slabinfo_op);
423}
00977a59 424static const struct file_operations proc_slabinfo_operations = {
1da177e4
LT
425 .open = slabinfo_open,
426 .read = seq_read,
427 .write = slabinfo_write,
428 .llseek = seq_lseek,
429 .release = seq_release,
430};
871751e2
AV
431
432#ifdef CONFIG_DEBUG_SLAB_LEAK
433extern struct seq_operations slabstats_op;
434static int slabstats_open(struct inode *inode, struct file *file)
435{
436 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
437 int ret = -ENOMEM;
438 if (n) {
439 ret = seq_open(file, &slabstats_op);
440 if (!ret) {
441 struct seq_file *m = file->private_data;
442 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
443 m->private = n;
444 n = NULL;
445 }
446 kfree(n);
447 }
448 return ret;
449}
450
00977a59 451static const struct file_operations proc_slabstats_operations = {
871751e2
AV
452 .open = slabstats_open,
453 .read = seq_read,
454 .llseek = seq_lseek,
09f0892e 455 .release = seq_release_private,
871751e2
AV
456};
457#endif
10cef602 458#endif
1da177e4
LT
459
460static int show_stat(struct seq_file *p, void *v)
461{
462 int i;
463 unsigned long jif;
464 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
5e84cfde 465 cputime64_t guest;
1da177e4 466 u64 sum = 0;
924b42d5 467 struct timespec boottime;
4004c69a
RT
468 unsigned int *per_irq_sum;
469
470 per_irq_sum = kzalloc(sizeof(unsigned int)*NR_IRQS, GFP_KERNEL);
471 if (!per_irq_sum)
472 return -ENOMEM;
1da177e4
LT
473
474 user = nice = system = idle = iowait =
475 irq = softirq = steal = cputime64_zero;
5e84cfde 476 guest = cputime64_zero;
924b42d5
TJ
477 getboottime(&boottime);
478 jif = boottime.tv_sec;
1da177e4 479
0a945022 480 for_each_possible_cpu(i) {
1da177e4
LT
481 int j;
482
483 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
484 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
485 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
486 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
487 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
488 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
489 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
490 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
5e84cfde 491 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
4004c69a
RT
492 for (j = 0; j < NR_IRQS; j++) {
493 unsigned int temp = kstat_cpu(i).irqs[j];
494 sum += temp;
495 per_irq_sum[j] += temp;
496 }
1da177e4
LT
497 }
498
5e84cfde 499 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
1da177e4
LT
500 (unsigned long long)cputime64_to_clock_t(user),
501 (unsigned long long)cputime64_to_clock_t(nice),
502 (unsigned long long)cputime64_to_clock_t(system),
503 (unsigned long long)cputime64_to_clock_t(idle),
504 (unsigned long long)cputime64_to_clock_t(iowait),
505 (unsigned long long)cputime64_to_clock_t(irq),
506 (unsigned long long)cputime64_to_clock_t(softirq),
5e84cfde
LV
507 (unsigned long long)cputime64_to_clock_t(steal),
508 (unsigned long long)cputime64_to_clock_t(guest));
1da177e4
LT
509 for_each_online_cpu(i) {
510
511 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
512 user = kstat_cpu(i).cpustat.user;
513 nice = kstat_cpu(i).cpustat.nice;
514 system = kstat_cpu(i).cpustat.system;
515 idle = kstat_cpu(i).cpustat.idle;
516 iowait = kstat_cpu(i).cpustat.iowait;
517 irq = kstat_cpu(i).cpustat.irq;
518 softirq = kstat_cpu(i).cpustat.softirq;
519 steal = kstat_cpu(i).cpustat.steal;
5e84cfde
LV
520 guest = kstat_cpu(i).cpustat.guest;
521 seq_printf(p,
522 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
1da177e4
LT
523 i,
524 (unsigned long long)cputime64_to_clock_t(user),
525 (unsigned long long)cputime64_to_clock_t(nice),
526 (unsigned long long)cputime64_to_clock_t(system),
527 (unsigned long long)cputime64_to_clock_t(idle),
528 (unsigned long long)cputime64_to_clock_t(iowait),
529 (unsigned long long)cputime64_to_clock_t(irq),
530 (unsigned long long)cputime64_to_clock_t(softirq),
5e84cfde
LV
531 (unsigned long long)cputime64_to_clock_t(steal),
532 (unsigned long long)cputime64_to_clock_t(guest));
1da177e4
LT
533 }
534 seq_printf(p, "intr %llu", (unsigned long long)sum);
535
1da177e4 536 for (i = 0; i < NR_IRQS; i++)
4004c69a 537 seq_printf(p, " %u", per_irq_sum[i]);
1da177e4
LT
538
539 seq_printf(p,
540 "\nctxt %llu\n"
541 "btime %lu\n"
542 "processes %lu\n"
543 "procs_running %lu\n"
544 "procs_blocked %lu\n",
545 nr_context_switches(),
546 (unsigned long)jif,
547 total_forks,
548 nr_running(),
549 nr_iowait());
550
4004c69a 551 kfree(per_irq_sum);
1da177e4
LT
552 return 0;
553}
554
555static int stat_open(struct inode *inode, struct file *file)
556{
557 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
558 char *buf;
559 struct seq_file *m;
560 int res;
561
562 /* don't ask for more than the kmalloc() max size, currently 128 KB */
563 if (size > 128 * 1024)
564 size = 128 * 1024;
565 buf = kmalloc(size, GFP_KERNEL);
566 if (!buf)
567 return -ENOMEM;
568
569 res = single_open(file, show_stat, NULL);
570 if (!res) {
571 m = file->private_data;
572 m->buf = buf;
573 m->size = size;
574 } else
575 kfree(buf);
576 return res;
577}
00977a59 578static const struct file_operations proc_stat_operations = {
1da177e4
LT
579 .open = stat_open,
580 .read = seq_read,
581 .llseek = seq_lseek,
582 .release = single_release,
583};
584
1da177e4
LT
585/*
586 * /proc/interrupts
587 */
588static void *int_seq_start(struct seq_file *f, loff_t *pos)
589{
590 return (*pos <= NR_IRQS) ? pos : NULL;
591}
592
593static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
594{
595 (*pos)++;
596 if (*pos > NR_IRQS)
597 return NULL;
598 return pos;
599}
600
601static void int_seq_stop(struct seq_file *f, void *v)
602{
603 /* Nothing to do */
604}
605
606
1da177e4
LT
607static struct seq_operations int_seq_ops = {
608 .start = int_seq_start,
609 .next = int_seq_next,
610 .stop = int_seq_stop,
611 .show = show_interrupts
612};
613
614static int interrupts_open(struct inode *inode, struct file *filp)
615{
616 return seq_open(filp, &int_seq_ops);
617}
618
00977a59 619static const struct file_operations proc_interrupts_operations = {
1da177e4
LT
620 .open = interrupts_open,
621 .read = seq_read,
622 .llseek = seq_lseek,
623 .release = seq_release,
624};
625
626static int filesystems_read_proc(char *page, char **start, off_t off,
627 int count, int *eof, void *data)
628{
629 int len = get_filesystem_list(page);
630 return proc_calc_metrics(page, start, off, count, eof, len);
631}
632
633static int cmdline_read_proc(char *page, char **start, off_t off,
634 int count, int *eof, void *data)
635{
636 int len;
637
638 len = sprintf(page, "%s\n", saved_command_line);
639 return proc_calc_metrics(page, start, off, count, eof, len);
640}
641
7f8ada98 642static int locks_open(struct inode *inode, struct file *filp)
1da177e4 643{
7f8ada98 644 return seq_open(filp, &locks_seq_operations);
1da177e4
LT
645}
646
7f8ada98
PE
647static const struct file_operations proc_locks_operations = {
648 .open = locks_open,
649 .read = seq_read,
650 .llseek = seq_lseek,
651 .release = seq_release,
652};
653
1da177e4
LT
654static int execdomains_read_proc(char *page, char **start, off_t off,
655 int count, int *eof, void *data)
656{
657 int len = get_exec_domain_list(page);
658 return proc_calc_metrics(page, start, off, count, eof, len);
659}
660
661#ifdef CONFIG_MAGIC_SYSRQ
662/*
663 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
664 */
665static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
666 size_t count, loff_t *ppos)
667{
668 if (count) {
669 char c;
670
671 if (get_user(c, buf))
672 return -EFAULT;
7d12e780 673 __handle_sysrq(c, NULL, 0);
1da177e4
LT
674 }
675 return count;
676}
677
00977a59 678static const struct file_operations proc_sysrq_trigger_operations = {
1da177e4
LT
679 .write = write_sysrq_trigger,
680};
681#endif
682
1e883281 683#ifdef CONFIG_PROC_PAGE_MONITOR
161f47bf
MM
684#define KPMSIZE sizeof(u64)
685#define KPMMASK (KPMSIZE - 1)
686/* /proc/kpagecount - an array exposing page counts
687 *
688 * Each entry is a u64 representing the corresponding
689 * physical page count.
690 */
691static ssize_t kpagecount_read(struct file *file, char __user *buf,
692 size_t count, loff_t *ppos)
693{
694 u64 __user *out = (u64 __user *)buf;
695 struct page *ppage;
696 unsigned long src = *ppos;
697 unsigned long pfn;
698 ssize_t ret = 0;
699 u64 pcount;
700
701 pfn = src / KPMSIZE;
702 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
703 if (src & KPMMASK || count & KPMMASK)
704 return -EIO;
705
706 while (count > 0) {
304daa81
MM
707 ppage = NULL;
708 if (pfn_valid(pfn))
709 ppage = pfn_to_page(pfn);
710 pfn++;
161f47bf
MM
711 if (!ppage)
712 pcount = 0;
713 else
714 pcount = atomic_read(&ppage->_count);
715
716 if (put_user(pcount, out++)) {
717 ret = -EFAULT;
718 break;
719 }
720
721 count -= KPMSIZE;
722 }
723
724 *ppos += (char __user *)out - buf;
725 if (!ret)
726 ret = (char __user *)out - buf;
727 return ret;
728}
729
730static struct file_operations proc_kpagecount_operations = {
731 .llseek = mem_lseek,
732 .read = kpagecount_read,
733};
734
304daa81
MM
735/* /proc/kpageflags - an array exposing page flags
736 *
737 * Each entry is a u64 representing the corresponding
738 * physical page flags.
739 */
740
741/* These macros are used to decouple internal flags from exported ones */
742
743#define KPF_LOCKED 0
744#define KPF_ERROR 1
745#define KPF_REFERENCED 2
746#define KPF_UPTODATE 3
747#define KPF_DIRTY 4
748#define KPF_LRU 5
749#define KPF_ACTIVE 6
750#define KPF_SLAB 7
751#define KPF_WRITEBACK 8
752#define KPF_RECLAIM 9
753#define KPF_BUDDY 10
754
755#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
756
757static ssize_t kpageflags_read(struct file *file, char __user *buf,
758 size_t count, loff_t *ppos)
759{
760 u64 __user *out = (u64 __user *)buf;
761 struct page *ppage;
762 unsigned long src = *ppos;
763 unsigned long pfn;
764 ssize_t ret = 0;
765 u64 kflags, uflags;
766
767 pfn = src / KPMSIZE;
768 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
769 if (src & KPMMASK || count & KPMMASK)
770 return -EIO;
771
772 while (count > 0) {
773 ppage = NULL;
774 if (pfn_valid(pfn))
775 ppage = pfn_to_page(pfn);
776 pfn++;
777 if (!ppage)
778 kflags = 0;
779 else
780 kflags = ppage->flags;
781
782 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
783 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
784 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
785 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
786 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
787 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
788 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
789 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
790 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
791 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
792 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
793
794 if (put_user(uflags, out++)) {
795 ret = -EFAULT;
796 break;
797 }
798
799 count -= KPMSIZE;
800 }
801
802 *ppos += (char __user *)out - buf;
803 if (!ret)
804 ret = (char __user *)out - buf;
805 return ret;
806}
807
808static struct file_operations proc_kpageflags_operations = {
809 .llseek = mem_lseek,
810 .read = kpageflags_read,
811};
1e883281 812#endif /* CONFIG_PROC_PAGE_MONITOR */
304daa81 813
1da177e4
LT
814struct proc_dir_entry *proc_root_kcore;
815
99ac48f5 816void create_seq_entry(char *name, mode_t mode, const struct file_operations *f)
1da177e4
LT
817{
818 struct proc_dir_entry *entry;
819 entry = create_proc_entry(name, mode, NULL);
820 if (entry)
821 entry->proc_fops = f;
822}
823
824void __init proc_misc_init(void)
825{
1da177e4
LT
826 static struct {
827 char *name;
828 int (*read_proc)(char*,char**,off_t,int,int*,void*);
829 } *p, simple_ones[] = {
830 {"loadavg", loadavg_read_proc},
831 {"uptime", uptime_read_proc},
832 {"meminfo", meminfo_read_proc},
833 {"version", version_read_proc},
834#ifdef CONFIG_PROC_HARDWARE
835 {"hardware", hardware_read_proc},
836#endif
837#ifdef CONFIG_STRAM_PROC
838 {"stram", stram_read_proc},
839#endif
1da177e4
LT
840 {"filesystems", filesystems_read_proc},
841 {"cmdline", cmdline_read_proc},
1da177e4
LT
842 {"execdomains", execdomains_read_proc},
843 {NULL,}
844 };
845 for (p = simple_ones; p->name; p++)
846 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
847
848 proc_symlink("mounts", NULL, "self/mounts");
849
850 /* And now for trickier ones */
c36264df 851#ifdef CONFIG_PRINTK
100bb934
AM
852 {
853 struct proc_dir_entry *entry;
854 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
855 if (entry)
856 entry->proc_fops = &proc_kmsg_operations;
857 }
c36264df 858#endif
7f8ada98 859 create_seq_entry("locks", 0, &proc_locks_operations);
7170be5f 860 create_seq_entry("devices", 0, &proc_devinfo_operations);
1da177e4 861 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
9361401e 862#ifdef CONFIG_BLOCK
1da177e4 863 create_seq_entry("partitions", 0, &proc_partitions_operations);
9361401e 864#endif
1da177e4
LT
865 create_seq_entry("stat", 0, &proc_stat_operations);
866 create_seq_entry("interrupts", 0, &proc_interrupts_operations);
158a9624 867#ifdef CONFIG_SLABINFO
1da177e4 868 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
871751e2
AV
869#ifdef CONFIG_DEBUG_SLAB_LEAK
870 create_seq_entry("slab_allocators", 0 ,&proc_slabstats_operations);
871#endif
10cef602 872#endif
1da177e4 873 create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
467c996c 874 create_seq_entry("pagetypeinfo", S_IRUGO, &pagetypeinfo_file_ops);
1da177e4 875 create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
295ab934 876 create_seq_entry("zoneinfo",S_IRUGO, &proc_zoneinfo_file_operations);
9361401e 877#ifdef CONFIG_BLOCK
1da177e4 878 create_seq_entry("diskstats", 0, &proc_diskstats_operations);
9361401e 879#endif
1da177e4
LT
880#ifdef CONFIG_MODULES
881 create_seq_entry("modules", 0, &proc_modules_operations);
882#endif
883#ifdef CONFIG_SCHEDSTATS
884 create_seq_entry("schedstat", 0, &proc_schedstat_operations);
885#endif
886#ifdef CONFIG_PROC_KCORE
887 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
888 if (proc_root_kcore) {
889 proc_root_kcore->proc_fops = &proc_kcore_operations;
890 proc_root_kcore->size =
891 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
892 }
893#endif
1e883281 894#ifdef CONFIG_PROC_PAGE_MONITOR
161f47bf 895 create_seq_entry("kpagecount", S_IRUSR, &proc_kpagecount_operations);
304daa81 896 create_seq_entry("kpageflags", S_IRUSR, &proc_kpageflags_operations);
1e883281 897#endif
666bfddb
VG
898#ifdef CONFIG_PROC_VMCORE
899 proc_vmcore = create_proc_entry("vmcore", S_IRUSR, NULL);
900 if (proc_vmcore)
901 proc_vmcore->proc_fops = &proc_vmcore_operations;
902#endif
1da177e4 903#ifdef CONFIG_MAGIC_SYSRQ
100bb934
AM
904 {
905 struct proc_dir_entry *entry;
906 entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
907 if (entry)
908 entry->proc_fops = &proc_sysrq_trigger_operations;
909 }
1da177e4 910#endif
1da177e4 911}
This page took 0.553934 seconds and 5 git commands to generate.