page.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_HWPOISON 19
  86. #define KPF_NOPAGE 20
  87. #define KPF_KSM 21
  88. /* kernel hacking assistances
  89. * WARNING: subject to change, never rely on them!
  90. */
  91. #define KPF_RESERVED 32
  92. #define KPF_MLOCKED 33
  93. #define KPF_MAPPEDTODISK 34
  94. #define KPF_PRIVATE 35
  95. #define KPF_PRIVATE_2 36
  96. #define KPF_OWNER_PRIVATE 37
  97. #define KPF_ARCH 38
  98. #define KPF_UNCACHED 39
  99. static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
  100. {
  101. return ((kflags >> kbit) & 1) << ubit;
  102. }
  103. static u64 get_uflags(struct page *page)
  104. {
  105. u64 k;
  106. u64 u;
  107. /*
  108. * pseudo flag: KPF_NOPAGE
  109. * it differentiates a memory hole from a page with no flags
  110. */
  111. if (!page)
  112. return 1 << KPF_NOPAGE;
  113. k = page->flags;
  114. u = 0;
  115. /*
  116. * pseudo flags for the well known (anonymous) memory mapped pages
  117. *
  118. * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the
  119. * simple test in page_mapped() is not enough.
  120. */
  121. if (!PageSlab(page) && page_mapped(page))
  122. u |= 1 << KPF_MMAP;
  123. if (PageAnon(page))
  124. u |= 1 << KPF_ANON;
  125. if (PageKsm(page))
  126. u |= 1 << KPF_KSM;
  127. /*
  128. * compound pages: export both head/tail info
  129. * they together define a compound page's start/end pos and order
  130. */
  131. if (PageHead(page))
  132. u |= 1 << KPF_COMPOUND_HEAD;
  133. if (PageTail(page))
  134. u |= 1 << KPF_COMPOUND_TAIL;
  135. if (PageHuge(page))
  136. u |= 1 << KPF_HUGE;
  137. u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
  138. /*
  139. * Caveats on high order pages:
  140. * PG_buddy will only be set on the head page; SLUB/SLQB do the same
  141. * for PG_slab; SLOB won't set PG_slab at all on compound pages.
  142. */
  143. u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
  144. u |= kpf_copy_bit(k, KPF_BUDDY, PG_buddy);
  145. u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
  146. u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
  147. u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
  148. u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
  149. u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
  150. u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
  151. u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
  152. u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
  153. u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache);
  154. u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
  155. u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
  156. u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
  157. #ifdef CONFIG_MEMORY_FAILURE
  158. u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
  159. #endif
  160. #ifdef CONFIG_IA64_UNCACHED_ALLOCATOR
  161. u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
  162. #endif
  163. u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
  164. u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
  165. u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
  166. u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
  167. u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
  168. u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
  169. return u;
  170. };
  171. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  172. size_t count, loff_t *ppos)
  173. {
  174. u64 __user *out = (u64 __user *)buf;
  175. struct page *ppage;
  176. unsigned long src = *ppos;
  177. unsigned long pfn;
  178. ssize_t ret = 0;
  179. pfn = src / KPMSIZE;
  180. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  181. if (src & KPMMASK || count & KPMMASK)
  182. return -EINVAL;
  183. while (count > 0) {
  184. if (pfn_valid(pfn))
  185. ppage = pfn_to_page(pfn);
  186. else
  187. ppage = NULL;
  188. if (put_user(get_uflags(ppage), out)) {
  189. ret = -EFAULT;
  190. break;
  191. }
  192. pfn++;
  193. out++;
  194. count -= KPMSIZE;
  195. }
  196. *ppos += (char __user *)out - buf;
  197. if (!ret)
  198. ret = (char __user *)out - buf;
  199. return ret;
  200. }
  201. static const struct file_operations proc_kpageflags_operations = {
  202. .llseek = mem_lseek,
  203. .read = kpageflags_read,
  204. };
  205. static int __init proc_page_init(void)
  206. {
  207. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  208. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  209. return 0;
  210. }
  211. module_init(proc_page_init);