page.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include <linux/bootmem.h>
  2. #include <linux/compiler.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/ksm.h>
  6. #include <linux/mm.h>
  7. #include <linux/mmzone.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/hugetlb.h>
  11. #include <asm/uaccess.h>
  12. #include "internal.h"
  13. #define KPMSIZE sizeof(u64)
  14. #define KPMMASK (KPMSIZE - 1)
  15. /* /proc/kpagecount - an array exposing page counts
  16. *
  17. * Each entry is a u64 representing the corresponding
  18. * physical page count.
  19. */
  20. static ssize_t kpagecount_read(struct file *file, char __user *buf,
  21. size_t count, loff_t *ppos)
  22. {
  23. u64 __user *out = (u64 __user *)buf;
  24. struct page *ppage;
  25. unsigned long src = *ppos;
  26. unsigned long pfn;
  27. ssize_t ret = 0;
  28. u64 pcount;
  29. pfn = src / KPMSIZE;
  30. count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
  31. if (src & KPMMASK || count & KPMMASK)
  32. return -EINVAL;
  33. while (count > 0) {
  34. if (pfn_valid(pfn))
  35. ppage = pfn_to_page(pfn);
  36. else
  37. ppage = NULL;
  38. if (!ppage)
  39. pcount = 0;
  40. else
  41. pcount = page_mapcount(ppage);
  42. if (put_user(pcount, out)) {
  43. ret = -EFAULT;
  44. break;
  45. }
  46. pfn++;
  47. out++;
  48. count -= KPMSIZE;
  49. }
  50. *ppos += (char __user *)out - buf;
  51. if (!ret)
  52. ret = (char __user *)out - buf;
  53. return ret;
  54. }
  55. static const struct file_operations proc_kpagecount_operations = {
  56. .llseek = mem_lseek,
  57. .read = kpagecount_read,
  58. };
  59. /* /proc/kpageflags - an array exposing page flags
  60. *
  61. * Each entry is a u64 representing the corresponding
  62. * physical page flags.
  63. */
  64. /* These macros are used to decouple internal flags from exported ones */
  65. #define KPF_LOCKED 0
  66. #define KPF_ERROR 1
  67. #define KPF_REFERENCED 2
  68. #define KPF_UPTODATE 3
  69. #define KPF_DIRTY 4
  70. #define KPF_LRU 5
  71. #define KPF_ACTIVE 6
  72. #define KPF_SLAB 7
  73. #define KPF_WRITEBACK 8
  74. #define KPF_RECLAIM 9
  75. #define KPF_BUDDY 10
  76. /* 11-20: new additions in 2.6.31 */
  77. #define KPF_MMAP 11
  78. #define KPF_ANON 12
  79. #define KPF_SWAPCACHE 13
  80. #define KPF_SWAPBACKED 14
  81. #define KPF_COMPOUND_HEAD 15
  82. #define KPF_COMPOUND_TAIL 16
  83. #define KPF_HUGE 17
  84. #define KPF_UNEVICTABLE 18
  85. #define KPF_NOPAGE 20
  86. #define KPF_KSM 21
  87. /* kernel hacking assistances
  88. * WARNING: subject to change, never rely on them!
  89. */
  90. #define KPF_RESERVED 32
  91. #define KPF_MLOCKED 33
  92. #define KPF_MAPPEDTODISK 34
  93. #define KPF_PRIVATE 35
  94. #define KPF_PRIVATE_2 36
  95. #define KPF_OWNER_PRIVATE 37
  96. #define KPF_ARCH 38
  97. #define KPF_UNCACHED 39
  98. static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
  99. {
  100. return ((kflags >> kbit) & 1) << ubit;
  101. }
  102. static u64 get_uflags(struct page *page)
  103. {
  104. u64 k;
  105. u64 u;
  106. /*
  107. * pseudo flag: KPF_NOPAGE
  108. * it differentiates a memory hole from a page with no flags
  109. */
  110. if (!page)
  111. return 1 << KPF_NOPAGE;
  112. k = page->flags;
  113. u = 0;
  114. /*
  115. * pseudo flags for the well known (anonymous) memory mapped pages
  116. *
  117. * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the
  118. * simple test in page_mapped() is not enough.
  119. */
  120. if (!PageSlab(page) && page_mapped(page))
  121. u |= 1 << KPF_MMAP;
  122. if (PageAnon(page))
  123. u |= 1 << KPF_ANON;
  124. if (PageKsm(page))
  125. u |= 1 << KPF_KSM;
  126. /*
  127. * compound pages: export both head/tail info
  128. * they together define a compound page's start/end pos and order
  129. */
  130. if (PageHead(page))
  131. u |= 1 << KPF_COMPOUND_HEAD;
  132. if (PageTail(page))
  133. u |= 1 << KPF_COMPOUND_TAIL;
  134. if (PageHuge(page))
  135. u |= 1 << KPF_HUGE;
  136. u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
  137. /*
  138. * Caveats on high order pages:
  139. * PG_buddy will only be set on the head page; SLUB/SLQB do the same
  140. * for PG_slab; SLOB won't set PG_slab at all on compound pages.
  141. */
  142. u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
  143. u |= kpf_copy_bit(k, KPF_BUDDY, PG_buddy);
  144. u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
  145. u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
  146. u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
  147. u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
  148. u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
  149. u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
  150. u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
  151. u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
  152. u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache);
  153. u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
  154. u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
  155. u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
  156. #ifdef CONFIG_IA64_UNCACHED_ALLOCATOR
  157. u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
  158. #endif
  159. u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
  160. u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
  161. u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
  162. u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
  163. u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
  164. u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
  165. return u;
  166. };
  167. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  168. size_t count, loff_t *ppos)
  169. {
  170. u64 __user *out = (u64 __user *)buf;
  171. struct page *ppage;
  172. unsigned long src = *ppos;
  173. unsigned long pfn;
  174. ssize_t ret = 0;
  175. pfn = src / KPMSIZE;
  176. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  177. if (src & KPMMASK || count & KPMMASK)
  178. return -EINVAL;
  179. while (count > 0) {
  180. if (pfn_valid(pfn))
  181. ppage = pfn_to_page(pfn);
  182. else
  183. ppage = NULL;
  184. if (put_user(get_uflags(ppage), out)) {
  185. ret = -EFAULT;
  186. break;
  187. }
  188. pfn++;
  189. out++;
  190. count -= KPMSIZE;
  191. }
  192. *ppos += (char __user *)out - buf;
  193. if (!ret)
  194. ret = (char __user *)out - buf;
  195. return ret;
  196. }
  197. static const struct file_operations proc_kpageflags_operations = {
  198. .llseek = mem_lseek,
  199. .read = kpageflags_read,
  200. };
  201. static int __init proc_page_init(void)
  202. {
  203. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  204. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  205. return 0;
  206. }
  207. module_init(proc_page_init);