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