proc_misc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * linux/fs/proc/proc_misc.c
  3. *
  4. * linux/fs/proc/array.c
  5. * Copyright (C) 1992 by Linus Torvalds
  6. * based on ideas by Darren Senn
  7. *
  8. * This used to be the part of array.c. See the rest of history and credits
  9. * there. I took this into a separate file and switched the thing to generic
  10. * proc_file_inode_operations, leaving in array.c only per-process stuff.
  11. * Inumbers allocation made dynamic (via create_proc_entry()). AV, May 1999.
  12. *
  13. * Changes:
  14. * Fulton Green : Encapsulated position metric calculations.
  15. * <kernel@FultonGreen.com>
  16. */
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/time.h>
  20. #include <linux/kernel.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/fs.h>
  23. #include <linux/tty.h>
  24. #include <linux/string.h>
  25. #include <linux/mman.h>
  26. #include <linux/quicklist.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/ioport.h>
  29. #include <linux/mm.h>
  30. #include <linux/mmzone.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/irq.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/swap.h>
  35. #include <linux/slab.h>
  36. #include <linux/genhd.h>
  37. #include <linux/smp.h>
  38. #include <linux/signal.h>
  39. #include <linux/module.h>
  40. #include <linux/init.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/times.h>
  43. #include <linux/profile.h>
  44. #include <linux/utsname.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/hugetlb.h>
  47. #include <linux/jiffies.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/crash_dump.h>
  50. #include <linux/pid_namespace.h>
  51. #include <linux/bootmem.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/pgtable.h>
  54. #include <asm/io.h>
  55. #include <asm/tlb.h>
  56. #include <asm/div64.h>
  57. #include "internal.h"
  58. static int fragmentation_open(struct inode *inode, struct file *file)
  59. {
  60. (void)inode;
  61. return seq_open(file, &fragmentation_op);
  62. }
  63. static const struct file_operations fragmentation_file_operations = {
  64. .open = fragmentation_open,
  65. .read = seq_read,
  66. .llseek = seq_lseek,
  67. .release = seq_release,
  68. };
  69. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  70. {
  71. return seq_open(file, &pagetypeinfo_op);
  72. }
  73. static const struct file_operations pagetypeinfo_file_ops = {
  74. .open = pagetypeinfo_open,
  75. .read = seq_read,
  76. .llseek = seq_lseek,
  77. .release = seq_release,
  78. };
  79. static int zoneinfo_open(struct inode *inode, struct file *file)
  80. {
  81. return seq_open(file, &zoneinfo_op);
  82. }
  83. static const struct file_operations proc_zoneinfo_file_operations = {
  84. .open = zoneinfo_open,
  85. .read = seq_read,
  86. .llseek = seq_lseek,
  87. .release = seq_release,
  88. };
  89. extern const struct seq_operations cpuinfo_op;
  90. static int cpuinfo_open(struct inode *inode, struct file *file)
  91. {
  92. return seq_open(file, &cpuinfo_op);
  93. }
  94. static const struct file_operations proc_cpuinfo_operations = {
  95. .open = cpuinfo_open,
  96. .read = seq_read,
  97. .llseek = seq_lseek,
  98. .release = seq_release,
  99. };
  100. static int vmstat_open(struct inode *inode, struct file *file)
  101. {
  102. return seq_open(file, &vmstat_op);
  103. }
  104. static const struct file_operations proc_vmstat_file_operations = {
  105. .open = vmstat_open,
  106. .read = seq_read,
  107. .llseek = seq_lseek,
  108. .release = seq_release,
  109. };
  110. #ifdef CONFIG_BLOCK
  111. static int partitions_open(struct inode *inode, struct file *file)
  112. {
  113. return seq_open(file, &partitions_op);
  114. }
  115. static const struct file_operations proc_partitions_operations = {
  116. .open = partitions_open,
  117. .read = seq_read,
  118. .llseek = seq_lseek,
  119. .release = seq_release,
  120. };
  121. static int diskstats_open(struct inode *inode, struct file *file)
  122. {
  123. return seq_open(file, &diskstats_op);
  124. }
  125. static const struct file_operations proc_diskstats_operations = {
  126. .open = diskstats_open,
  127. .read = seq_read,
  128. .llseek = seq_lseek,
  129. .release = seq_release,
  130. };
  131. #endif
  132. #ifdef CONFIG_MODULES
  133. extern const struct seq_operations modules_op;
  134. static int modules_open(struct inode *inode, struct file *file)
  135. {
  136. return seq_open(file, &modules_op);
  137. }
  138. static const struct file_operations proc_modules_operations = {
  139. .open = modules_open,
  140. .read = seq_read,
  141. .llseek = seq_lseek,
  142. .release = seq_release,
  143. };
  144. #endif
  145. #ifdef CONFIG_SLABINFO
  146. static int slabinfo_open(struct inode *inode, struct file *file)
  147. {
  148. return seq_open(file, &slabinfo_op);
  149. }
  150. static const struct file_operations proc_slabinfo_operations = {
  151. .open = slabinfo_open,
  152. .read = seq_read,
  153. .write = slabinfo_write,
  154. .llseek = seq_lseek,
  155. .release = seq_release,
  156. };
  157. #ifdef CONFIG_DEBUG_SLAB_LEAK
  158. extern const struct seq_operations slabstats_op;
  159. static int slabstats_open(struct inode *inode, struct file *file)
  160. {
  161. unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
  162. int ret = -ENOMEM;
  163. if (n) {
  164. ret = seq_open(file, &slabstats_op);
  165. if (!ret) {
  166. struct seq_file *m = file->private_data;
  167. *n = PAGE_SIZE / (2 * sizeof(unsigned long));
  168. m->private = n;
  169. n = NULL;
  170. }
  171. kfree(n);
  172. }
  173. return ret;
  174. }
  175. static const struct file_operations proc_slabstats_operations = {
  176. .open = slabstats_open,
  177. .read = seq_read,
  178. .llseek = seq_lseek,
  179. .release = seq_release_private,
  180. };
  181. #endif
  182. #endif
  183. #ifdef CONFIG_MMU
  184. static int vmalloc_open(struct inode *inode, struct file *file)
  185. {
  186. unsigned int *ptr = NULL;
  187. int ret;
  188. if (NUMA_BUILD)
  189. ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
  190. ret = seq_open(file, &vmalloc_op);
  191. if (!ret) {
  192. struct seq_file *m = file->private_data;
  193. m->private = ptr;
  194. } else
  195. kfree(ptr);
  196. return ret;
  197. }
  198. static const struct file_operations proc_vmalloc_operations = {
  199. .open = vmalloc_open,
  200. .read = seq_read,
  201. .llseek = seq_lseek,
  202. .release = seq_release_private,
  203. };
  204. #endif
  205. #ifndef arch_irq_stat_cpu
  206. #define arch_irq_stat_cpu(cpu) 0
  207. #endif
  208. #ifndef arch_irq_stat
  209. #define arch_irq_stat() 0
  210. #endif
  211. static int show_stat(struct seq_file *p, void *v)
  212. {
  213. int i, j;
  214. unsigned long jif;
  215. cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
  216. cputime64_t guest;
  217. u64 sum = 0;
  218. struct timespec boottime;
  219. unsigned int per_irq_sum;
  220. user = nice = system = idle = iowait =
  221. irq = softirq = steal = cputime64_zero;
  222. guest = cputime64_zero;
  223. getboottime(&boottime);
  224. jif = boottime.tv_sec;
  225. for_each_possible_cpu(i) {
  226. user = cputime64_add(user, kstat_cpu(i).cpustat.user);
  227. nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
  228. system = cputime64_add(system, kstat_cpu(i).cpustat.system);
  229. idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
  230. iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
  231. irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
  232. softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
  233. steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
  234. guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
  235. for_each_irq_nr(j)
  236. sum += kstat_irqs_cpu(j, i);
  237. sum += arch_irq_stat_cpu(i);
  238. }
  239. sum += arch_irq_stat();
  240. seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
  241. (unsigned long long)cputime64_to_clock_t(user),
  242. (unsigned long long)cputime64_to_clock_t(nice),
  243. (unsigned long long)cputime64_to_clock_t(system),
  244. (unsigned long long)cputime64_to_clock_t(idle),
  245. (unsigned long long)cputime64_to_clock_t(iowait),
  246. (unsigned long long)cputime64_to_clock_t(irq),
  247. (unsigned long long)cputime64_to_clock_t(softirq),
  248. (unsigned long long)cputime64_to_clock_t(steal),
  249. (unsigned long long)cputime64_to_clock_t(guest));
  250. for_each_online_cpu(i) {
  251. /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
  252. user = kstat_cpu(i).cpustat.user;
  253. nice = kstat_cpu(i).cpustat.nice;
  254. system = kstat_cpu(i).cpustat.system;
  255. idle = kstat_cpu(i).cpustat.idle;
  256. iowait = kstat_cpu(i).cpustat.iowait;
  257. irq = kstat_cpu(i).cpustat.irq;
  258. softirq = kstat_cpu(i).cpustat.softirq;
  259. steal = kstat_cpu(i).cpustat.steal;
  260. guest = kstat_cpu(i).cpustat.guest;
  261. seq_printf(p,
  262. "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
  263. i,
  264. (unsigned long long)cputime64_to_clock_t(user),
  265. (unsigned long long)cputime64_to_clock_t(nice),
  266. (unsigned long long)cputime64_to_clock_t(system),
  267. (unsigned long long)cputime64_to_clock_t(idle),
  268. (unsigned long long)cputime64_to_clock_t(iowait),
  269. (unsigned long long)cputime64_to_clock_t(irq),
  270. (unsigned long long)cputime64_to_clock_t(softirq),
  271. (unsigned long long)cputime64_to_clock_t(steal),
  272. (unsigned long long)cputime64_to_clock_t(guest));
  273. }
  274. seq_printf(p, "intr %llu", (unsigned long long)sum);
  275. /* sum again ? it could be updated? */
  276. for_each_irq_nr(j) {
  277. per_irq_sum = 0;
  278. for_each_possible_cpu(i)
  279. per_irq_sum += kstat_irqs_cpu(j, i);
  280. seq_printf(p, " %u", per_irq_sum);
  281. }
  282. seq_printf(p,
  283. "\nctxt %llu\n"
  284. "btime %lu\n"
  285. "processes %lu\n"
  286. "procs_running %lu\n"
  287. "procs_blocked %lu\n",
  288. nr_context_switches(),
  289. (unsigned long)jif,
  290. total_forks,
  291. nr_running(),
  292. nr_iowait());
  293. return 0;
  294. }
  295. static int stat_open(struct inode *inode, struct file *file)
  296. {
  297. unsigned size = 4096 * (1 + num_possible_cpus() / 32);
  298. char *buf;
  299. struct seq_file *m;
  300. int res;
  301. /* don't ask for more than the kmalloc() max size, currently 128 KB */
  302. if (size > 128 * 1024)
  303. size = 128 * 1024;
  304. buf = kmalloc(size, GFP_KERNEL);
  305. if (!buf)
  306. return -ENOMEM;
  307. res = single_open(file, show_stat, NULL);
  308. if (!res) {
  309. m = file->private_data;
  310. m->buf = buf;
  311. m->size = size;
  312. } else
  313. kfree(buf);
  314. return res;
  315. }
  316. static const struct file_operations proc_stat_operations = {
  317. .open = stat_open,
  318. .read = seq_read,
  319. .llseek = seq_lseek,
  320. .release = single_release,
  321. };
  322. /*
  323. * /proc/interrupts
  324. */
  325. static void *int_seq_start(struct seq_file *f, loff_t *pos)
  326. {
  327. return (*pos <= nr_irqs) ? pos : NULL;
  328. }
  329. static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
  330. {
  331. (*pos)++;
  332. return (*pos <= nr_irqs) ? pos : NULL;
  333. }
  334. static void int_seq_stop(struct seq_file *f, void *v)
  335. {
  336. /* Nothing to do */
  337. }
  338. static const struct seq_operations int_seq_ops = {
  339. .start = int_seq_start,
  340. .next = int_seq_next,
  341. .stop = int_seq_stop,
  342. .show = show_interrupts
  343. };
  344. static int interrupts_open(struct inode *inode, struct file *filp)
  345. {
  346. return seq_open(filp, &int_seq_ops);
  347. }
  348. static const struct file_operations proc_interrupts_operations = {
  349. .open = interrupts_open,
  350. .read = seq_read,
  351. .llseek = seq_lseek,
  352. .release = seq_release,
  353. };
  354. #ifdef CONFIG_PROC_PAGE_MONITOR
  355. #define KPMSIZE sizeof(u64)
  356. #define KPMMASK (KPMSIZE - 1)
  357. /* /proc/kpagecount - an array exposing page counts
  358. *
  359. * Each entry is a u64 representing the corresponding
  360. * physical page count.
  361. */
  362. static ssize_t kpagecount_read(struct file *file, char __user *buf,
  363. size_t count, loff_t *ppos)
  364. {
  365. u64 __user *out = (u64 __user *)buf;
  366. struct page *ppage;
  367. unsigned long src = *ppos;
  368. unsigned long pfn;
  369. ssize_t ret = 0;
  370. u64 pcount;
  371. pfn = src / KPMSIZE;
  372. count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
  373. if (src & KPMMASK || count & KPMMASK)
  374. return -EINVAL;
  375. while (count > 0) {
  376. ppage = NULL;
  377. if (pfn_valid(pfn))
  378. ppage = pfn_to_page(pfn);
  379. pfn++;
  380. if (!ppage)
  381. pcount = 0;
  382. else
  383. pcount = page_mapcount(ppage);
  384. if (put_user(pcount, out++)) {
  385. ret = -EFAULT;
  386. break;
  387. }
  388. count -= KPMSIZE;
  389. }
  390. *ppos += (char __user *)out - buf;
  391. if (!ret)
  392. ret = (char __user *)out - buf;
  393. return ret;
  394. }
  395. static struct file_operations proc_kpagecount_operations = {
  396. .llseek = mem_lseek,
  397. .read = kpagecount_read,
  398. };
  399. /* /proc/kpageflags - an array exposing page flags
  400. *
  401. * Each entry is a u64 representing the corresponding
  402. * physical page flags.
  403. */
  404. /* These macros are used to decouple internal flags from exported ones */
  405. #define KPF_LOCKED 0
  406. #define KPF_ERROR 1
  407. #define KPF_REFERENCED 2
  408. #define KPF_UPTODATE 3
  409. #define KPF_DIRTY 4
  410. #define KPF_LRU 5
  411. #define KPF_ACTIVE 6
  412. #define KPF_SLAB 7
  413. #define KPF_WRITEBACK 8
  414. #define KPF_RECLAIM 9
  415. #define KPF_BUDDY 10
  416. #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
  417. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  418. size_t count, loff_t *ppos)
  419. {
  420. u64 __user *out = (u64 __user *)buf;
  421. struct page *ppage;
  422. unsigned long src = *ppos;
  423. unsigned long pfn;
  424. ssize_t ret = 0;
  425. u64 kflags, uflags;
  426. pfn = src / KPMSIZE;
  427. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  428. if (src & KPMMASK || count & KPMMASK)
  429. return -EINVAL;
  430. while (count > 0) {
  431. ppage = NULL;
  432. if (pfn_valid(pfn))
  433. ppage = pfn_to_page(pfn);
  434. pfn++;
  435. if (!ppage)
  436. kflags = 0;
  437. else
  438. kflags = ppage->flags;
  439. uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
  440. kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
  441. kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
  442. kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
  443. kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
  444. kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
  445. kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
  446. kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
  447. kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
  448. kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
  449. kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
  450. if (put_user(uflags, out++)) {
  451. ret = -EFAULT;
  452. break;
  453. }
  454. count -= KPMSIZE;
  455. }
  456. *ppos += (char __user *)out - buf;
  457. if (!ret)
  458. ret = (char __user *)out - buf;
  459. return ret;
  460. }
  461. static struct file_operations proc_kpageflags_operations = {
  462. .llseek = mem_lseek,
  463. .read = kpageflags_read,
  464. };
  465. #endif /* CONFIG_PROC_PAGE_MONITOR */
  466. struct proc_dir_entry *proc_root_kcore;
  467. void __init proc_misc_init(void)
  468. {
  469. proc_symlink("mounts", NULL, "self/mounts");
  470. /* And now for trickier ones */
  471. proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
  472. #ifdef CONFIG_BLOCK
  473. proc_create("partitions", 0, NULL, &proc_partitions_operations);
  474. #endif
  475. proc_create("stat", 0, NULL, &proc_stat_operations);
  476. proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
  477. #ifdef CONFIG_SLABINFO
  478. proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
  479. #ifdef CONFIG_DEBUG_SLAB_LEAK
  480. proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
  481. #endif
  482. #endif
  483. #ifdef CONFIG_MMU
  484. proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
  485. #endif
  486. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  487. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  488. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  489. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  490. #ifdef CONFIG_BLOCK
  491. proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
  492. #endif
  493. #ifdef CONFIG_MODULES
  494. proc_create("modules", 0, NULL, &proc_modules_operations);
  495. #endif
  496. #ifdef CONFIG_SCHEDSTATS
  497. proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
  498. #endif
  499. #ifdef CONFIG_PROC_KCORE
  500. proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
  501. if (proc_root_kcore)
  502. proc_root_kcore->size =
  503. (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
  504. #endif
  505. #ifdef CONFIG_PROC_PAGE_MONITOR
  506. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  507. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  508. #endif
  509. #ifdef CONFIG_PROC_VMCORE
  510. proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
  511. #endif
  512. }