proc_misc.c 23 KB

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