proc_misc.c 23 KB

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