proc_misc.c 23 KB

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