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