proc_misc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. static int vmstat_open(struct inode *inode, struct file *file)
  90. {
  91. return seq_open(file, &vmstat_op);
  92. }
  93. static const struct file_operations proc_vmstat_file_operations = {
  94. .open = vmstat_open,
  95. .read = seq_read,
  96. .llseek = seq_lseek,
  97. .release = seq_release,
  98. };
  99. #ifdef CONFIG_BLOCK
  100. static int diskstats_open(struct inode *inode, struct file *file)
  101. {
  102. return seq_open(file, &diskstats_op);
  103. }
  104. static const struct file_operations proc_diskstats_operations = {
  105. .open = diskstats_open,
  106. .read = seq_read,
  107. .llseek = seq_lseek,
  108. .release = seq_release,
  109. };
  110. #endif
  111. #ifdef CONFIG_MODULES
  112. extern const struct seq_operations modules_op;
  113. static int modules_open(struct inode *inode, struct file *file)
  114. {
  115. return seq_open(file, &modules_op);
  116. }
  117. static const struct file_operations proc_modules_operations = {
  118. .open = modules_open,
  119. .read = seq_read,
  120. .llseek = seq_lseek,
  121. .release = seq_release,
  122. };
  123. #endif
  124. #ifdef CONFIG_SLABINFO
  125. static int slabinfo_open(struct inode *inode, struct file *file)
  126. {
  127. return seq_open(file, &slabinfo_op);
  128. }
  129. static const struct file_operations proc_slabinfo_operations = {
  130. .open = slabinfo_open,
  131. .read = seq_read,
  132. .write = slabinfo_write,
  133. .llseek = seq_lseek,
  134. .release = seq_release,
  135. };
  136. #ifdef CONFIG_DEBUG_SLAB_LEAK
  137. extern const struct seq_operations slabstats_op;
  138. static int slabstats_open(struct inode *inode, struct file *file)
  139. {
  140. unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
  141. int ret = -ENOMEM;
  142. if (n) {
  143. ret = seq_open(file, &slabstats_op);
  144. if (!ret) {
  145. struct seq_file *m = file->private_data;
  146. *n = PAGE_SIZE / (2 * sizeof(unsigned long));
  147. m->private = n;
  148. n = NULL;
  149. }
  150. kfree(n);
  151. }
  152. return ret;
  153. }
  154. static const struct file_operations proc_slabstats_operations = {
  155. .open = slabstats_open,
  156. .read = seq_read,
  157. .llseek = seq_lseek,
  158. .release = seq_release_private,
  159. };
  160. #endif
  161. #endif
  162. #ifdef CONFIG_MMU
  163. static int vmalloc_open(struct inode *inode, struct file *file)
  164. {
  165. unsigned int *ptr = NULL;
  166. int ret;
  167. if (NUMA_BUILD)
  168. ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
  169. ret = seq_open(file, &vmalloc_op);
  170. if (!ret) {
  171. struct seq_file *m = file->private_data;
  172. m->private = ptr;
  173. } else
  174. kfree(ptr);
  175. return ret;
  176. }
  177. static const struct file_operations proc_vmalloc_operations = {
  178. .open = vmalloc_open,
  179. .read = seq_read,
  180. .llseek = seq_lseek,
  181. .release = seq_release_private,
  182. };
  183. #endif
  184. /*
  185. * /proc/interrupts
  186. */
  187. static void *int_seq_start(struct seq_file *f, loff_t *pos)
  188. {
  189. return (*pos <= nr_irqs) ? pos : NULL;
  190. }
  191. static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
  192. {
  193. (*pos)++;
  194. return (*pos <= nr_irqs) ? pos : NULL;
  195. }
  196. static void int_seq_stop(struct seq_file *f, void *v)
  197. {
  198. /* Nothing to do */
  199. }
  200. static const struct seq_operations int_seq_ops = {
  201. .start = int_seq_start,
  202. .next = int_seq_next,
  203. .stop = int_seq_stop,
  204. .show = show_interrupts
  205. };
  206. static int interrupts_open(struct inode *inode, struct file *filp)
  207. {
  208. return seq_open(filp, &int_seq_ops);
  209. }
  210. static const struct file_operations proc_interrupts_operations = {
  211. .open = interrupts_open,
  212. .read = seq_read,
  213. .llseek = seq_lseek,
  214. .release = seq_release,
  215. };
  216. #ifdef CONFIG_PROC_PAGE_MONITOR
  217. #define KPMSIZE sizeof(u64)
  218. #define KPMMASK (KPMSIZE - 1)
  219. /* /proc/kpagecount - an array exposing page counts
  220. *
  221. * Each entry is a u64 representing the corresponding
  222. * physical page count.
  223. */
  224. static ssize_t kpagecount_read(struct file *file, char __user *buf,
  225. size_t count, loff_t *ppos)
  226. {
  227. u64 __user *out = (u64 __user *)buf;
  228. struct page *ppage;
  229. unsigned long src = *ppos;
  230. unsigned long pfn;
  231. ssize_t ret = 0;
  232. u64 pcount;
  233. pfn = src / KPMSIZE;
  234. count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
  235. if (src & KPMMASK || count & KPMMASK)
  236. return -EINVAL;
  237. while (count > 0) {
  238. ppage = NULL;
  239. if (pfn_valid(pfn))
  240. ppage = pfn_to_page(pfn);
  241. pfn++;
  242. if (!ppage)
  243. pcount = 0;
  244. else
  245. pcount = page_mapcount(ppage);
  246. if (put_user(pcount, out++)) {
  247. ret = -EFAULT;
  248. break;
  249. }
  250. count -= KPMSIZE;
  251. }
  252. *ppos += (char __user *)out - buf;
  253. if (!ret)
  254. ret = (char __user *)out - buf;
  255. return ret;
  256. }
  257. static struct file_operations proc_kpagecount_operations = {
  258. .llseek = mem_lseek,
  259. .read = kpagecount_read,
  260. };
  261. /* /proc/kpageflags - an array exposing page flags
  262. *
  263. * Each entry is a u64 representing the corresponding
  264. * physical page flags.
  265. */
  266. /* These macros are used to decouple internal flags from exported ones */
  267. #define KPF_LOCKED 0
  268. #define KPF_ERROR 1
  269. #define KPF_REFERENCED 2
  270. #define KPF_UPTODATE 3
  271. #define KPF_DIRTY 4
  272. #define KPF_LRU 5
  273. #define KPF_ACTIVE 6
  274. #define KPF_SLAB 7
  275. #define KPF_WRITEBACK 8
  276. #define KPF_RECLAIM 9
  277. #define KPF_BUDDY 10
  278. #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
  279. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  280. size_t count, loff_t *ppos)
  281. {
  282. u64 __user *out = (u64 __user *)buf;
  283. struct page *ppage;
  284. unsigned long src = *ppos;
  285. unsigned long pfn;
  286. ssize_t ret = 0;
  287. u64 kflags, uflags;
  288. pfn = src / KPMSIZE;
  289. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  290. if (src & KPMMASK || count & KPMMASK)
  291. return -EINVAL;
  292. while (count > 0) {
  293. ppage = NULL;
  294. if (pfn_valid(pfn))
  295. ppage = pfn_to_page(pfn);
  296. pfn++;
  297. if (!ppage)
  298. kflags = 0;
  299. else
  300. kflags = ppage->flags;
  301. uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
  302. kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
  303. kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
  304. kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
  305. kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
  306. kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
  307. kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
  308. kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
  309. kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
  310. kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
  311. kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
  312. if (put_user(uflags, out++)) {
  313. ret = -EFAULT;
  314. break;
  315. }
  316. count -= KPMSIZE;
  317. }
  318. *ppos += (char __user *)out - buf;
  319. if (!ret)
  320. ret = (char __user *)out - buf;
  321. return ret;
  322. }
  323. static struct file_operations proc_kpageflags_operations = {
  324. .llseek = mem_lseek,
  325. .read = kpageflags_read,
  326. };
  327. #endif /* CONFIG_PROC_PAGE_MONITOR */
  328. struct proc_dir_entry *proc_root_kcore;
  329. void __init proc_misc_init(void)
  330. {
  331. proc_symlink("mounts", NULL, "self/mounts");
  332. /* And now for trickier ones */
  333. proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
  334. #ifdef CONFIG_SLABINFO
  335. proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
  336. #ifdef CONFIG_DEBUG_SLAB_LEAK
  337. proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
  338. #endif
  339. #endif
  340. #ifdef CONFIG_MMU
  341. proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
  342. #endif
  343. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  344. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  345. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  346. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  347. #ifdef CONFIG_BLOCK
  348. proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
  349. #endif
  350. #ifdef CONFIG_MODULES
  351. proc_create("modules", 0, NULL, &proc_modules_operations);
  352. #endif
  353. #ifdef CONFIG_SCHEDSTATS
  354. proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
  355. #endif
  356. #ifdef CONFIG_PROC_KCORE
  357. proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
  358. if (proc_root_kcore)
  359. proc_root_kcore->size =
  360. (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
  361. #endif
  362. #ifdef CONFIG_PROC_PAGE_MONITOR
  363. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  364. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  365. #endif
  366. #ifdef CONFIG_PROC_VMCORE
  367. proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
  368. #endif
  369. }