proc_misc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #ifdef CONFIG_BLOCK
  59. static int diskstats_open(struct inode *inode, struct file *file)
  60. {
  61. return seq_open(file, &diskstats_op);
  62. }
  63. static const struct file_operations proc_diskstats_operations = {
  64. .open = diskstats_open,
  65. .read = seq_read,
  66. .llseek = seq_lseek,
  67. .release = seq_release,
  68. };
  69. #endif
  70. #ifdef CONFIG_MODULES
  71. extern const struct seq_operations modules_op;
  72. static int modules_open(struct inode *inode, struct file *file)
  73. {
  74. return seq_open(file, &modules_op);
  75. }
  76. static const struct file_operations proc_modules_operations = {
  77. .open = modules_open,
  78. .read = seq_read,
  79. .llseek = seq_lseek,
  80. .release = seq_release,
  81. };
  82. #endif
  83. #ifdef CONFIG_PROC_PAGE_MONITOR
  84. #define KPMSIZE sizeof(u64)
  85. #define KPMMASK (KPMSIZE - 1)
  86. /* /proc/kpagecount - an array exposing page counts
  87. *
  88. * Each entry is a u64 representing the corresponding
  89. * physical page count.
  90. */
  91. static ssize_t kpagecount_read(struct file *file, char __user *buf,
  92. size_t count, loff_t *ppos)
  93. {
  94. u64 __user *out = (u64 __user *)buf;
  95. struct page *ppage;
  96. unsigned long src = *ppos;
  97. unsigned long pfn;
  98. ssize_t ret = 0;
  99. u64 pcount;
  100. pfn = src / KPMSIZE;
  101. count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
  102. if (src & KPMMASK || count & KPMMASK)
  103. return -EINVAL;
  104. while (count > 0) {
  105. ppage = NULL;
  106. if (pfn_valid(pfn))
  107. ppage = pfn_to_page(pfn);
  108. pfn++;
  109. if (!ppage)
  110. pcount = 0;
  111. else
  112. pcount = page_mapcount(ppage);
  113. if (put_user(pcount, out++)) {
  114. ret = -EFAULT;
  115. break;
  116. }
  117. count -= KPMSIZE;
  118. }
  119. *ppos += (char __user *)out - buf;
  120. if (!ret)
  121. ret = (char __user *)out - buf;
  122. return ret;
  123. }
  124. static struct file_operations proc_kpagecount_operations = {
  125. .llseek = mem_lseek,
  126. .read = kpagecount_read,
  127. };
  128. /* /proc/kpageflags - an array exposing page flags
  129. *
  130. * Each entry is a u64 representing the corresponding
  131. * physical page flags.
  132. */
  133. /* These macros are used to decouple internal flags from exported ones */
  134. #define KPF_LOCKED 0
  135. #define KPF_ERROR 1
  136. #define KPF_REFERENCED 2
  137. #define KPF_UPTODATE 3
  138. #define KPF_DIRTY 4
  139. #define KPF_LRU 5
  140. #define KPF_ACTIVE 6
  141. #define KPF_SLAB 7
  142. #define KPF_WRITEBACK 8
  143. #define KPF_RECLAIM 9
  144. #define KPF_BUDDY 10
  145. #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
  146. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  147. size_t count, loff_t *ppos)
  148. {
  149. u64 __user *out = (u64 __user *)buf;
  150. struct page *ppage;
  151. unsigned long src = *ppos;
  152. unsigned long pfn;
  153. ssize_t ret = 0;
  154. u64 kflags, uflags;
  155. pfn = src / KPMSIZE;
  156. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  157. if (src & KPMMASK || count & KPMMASK)
  158. return -EINVAL;
  159. while (count > 0) {
  160. ppage = NULL;
  161. if (pfn_valid(pfn))
  162. ppage = pfn_to_page(pfn);
  163. pfn++;
  164. if (!ppage)
  165. kflags = 0;
  166. else
  167. kflags = ppage->flags;
  168. uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
  169. kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
  170. kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
  171. kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
  172. kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
  173. kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
  174. kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
  175. kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
  176. kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
  177. kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
  178. kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
  179. if (put_user(uflags, out++)) {
  180. ret = -EFAULT;
  181. break;
  182. }
  183. count -= KPMSIZE;
  184. }
  185. *ppos += (char __user *)out - buf;
  186. if (!ret)
  187. ret = (char __user *)out - buf;
  188. return ret;
  189. }
  190. static struct file_operations proc_kpageflags_operations = {
  191. .llseek = mem_lseek,
  192. .read = kpageflags_read,
  193. };
  194. #endif /* CONFIG_PROC_PAGE_MONITOR */
  195. struct proc_dir_entry *proc_root_kcore;
  196. void __init proc_misc_init(void)
  197. {
  198. proc_symlink("mounts", NULL, "self/mounts");
  199. /* And now for trickier ones */
  200. #ifdef CONFIG_BLOCK
  201. proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
  202. #endif
  203. #ifdef CONFIG_MODULES
  204. proc_create("modules", 0, NULL, &proc_modules_operations);
  205. #endif
  206. #ifdef CONFIG_SCHEDSTATS
  207. proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
  208. #endif
  209. #ifdef CONFIG_PROC_KCORE
  210. proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
  211. if (proc_root_kcore)
  212. proc_root_kcore->size =
  213. (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
  214. #endif
  215. #ifdef CONFIG_PROC_PAGE_MONITOR
  216. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  217. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  218. #endif
  219. #ifdef CONFIG_PROC_VMCORE
  220. proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
  221. #endif
  222. }