proc_misc.c 24 KB

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