proc_misc.c 23 KB

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