proc_misc.c 24 KB

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