page.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <linux/kernel-page-flags.h>
  12. #include <asm/uaccess.h>
  13. #include "internal.h"
  14. #ifdef CONFIG_SWAP
  15. #include <linux/swap.h>
  16. #include <linux/swapops.h>
  17. #endif
  18. #define KPMSIZE sizeof(u64)
  19. #define KPMMASK (KPMSIZE - 1)
  20. /* /proc/kpagecount - an array exposing page counts
  21. *
  22. * Each entry is a u64 representing the corresponding
  23. * physical page count.
  24. */
  25. static ssize_t kpagecount_read(struct file *file, char __user *buf,
  26. size_t count, loff_t *ppos)
  27. {
  28. u64 __user *out = (u64 __user *)buf;
  29. struct page *ppage;
  30. unsigned long src = *ppos;
  31. unsigned long pfn;
  32. unsigned long max_pfn_kpmsize = max_pfn * KPMSIZE;
  33. ssize_t ret = 0;
  34. u64 pcount;
  35. pfn = src / KPMSIZE;
  36. if (src != max_pfn_kpmsize)
  37. count = min_t(size_t, count, max_pfn_kpmsize - src);
  38. if (src & KPMMASK || count & KPMMASK)
  39. return -EINVAL;
  40. while (count > 0) {
  41. if (pfn_valid(pfn))
  42. ppage = pfn_to_page(pfn);
  43. else
  44. ppage = NULL;
  45. if (!ppage || PageSlab(ppage))
  46. pcount = 0;
  47. else
  48. pcount = page_mapcount(ppage);
  49. if (put_user(pcount, out)) {
  50. ret = -EFAULT;
  51. break;
  52. }
  53. pfn++;
  54. out++;
  55. count -= KPMSIZE;
  56. }
  57. *ppos += (char __user *)out - buf;
  58. if (!ret)
  59. ret = (char __user *)out - buf;
  60. return ret;
  61. }
  62. static const struct file_operations proc_kpagecount_operations = {
  63. .llseek = mem_lseek,
  64. .read = kpagecount_read,
  65. };
  66. /* M for pswap interface */
  67. #ifdef CONFIG_SWAP
  68. static inline unsigned char swap_count(unsigned char ent)
  69. {
  70. return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */
  71. }
  72. /* /proc/kpageswapn - an array exposing page swap counts
  73. *
  74. * Each entry is a u64 representing the corresponding
  75. * physical page swap count.
  76. */
  77. static ssize_t kpageswapn_read(struct file *file, char __user *buf,
  78. size_t count, loff_t *ppos)
  79. {
  80. u64 __user *out = (u64 __user *)buf;
  81. unsigned long src = *ppos, dst;
  82. swp_entry_t swap_entry;
  83. ssize_t ret = 0;
  84. struct swap_info_struct *p;
  85. dst = src / KPMSIZE;
  86. /* Format the swap entry from the corresponding pagemap value */
  87. swap_entry = swp_entry(dst >> (SWP_TYPE_SHIFT(swap_entry) + RADIX_TREE_EXCEPTIONAL_SHIFT),
  88. dst & SWP_OFFSET_MASK(swap_entry));
  89. /*pr_info("kpageswapn_read src: %lx\n", src); */
  90. /*pr_info("kpageswapn_read swap entry: %lx\n", swap_entry.val); */
  91. if (src & KPMMASK || count & KPMMASK) {
  92. pr_err("kpageswapn_read return EINVAL\n");
  93. return -EINVAL;
  94. }
  95. p = swap_info_get(swap_entry);
  96. if (p) {
  97. u64 swapcount = swap_count(p->swap_map[swp_offset(swap_entry)]);
  98. if (put_user(swapcount, out)) {
  99. pr_err("pageswapn_read put user failed\n");
  100. ret = -EFAULT;
  101. }
  102. swap_info_unlock(p);
  103. } else {
  104. pr_err("kpageswapn_read swap_info_get failed\n");
  105. ret = -EFAULT;
  106. }
  107. if (!ret) {
  108. *ppos += KPMSIZE;
  109. ret = KPMSIZE;
  110. }
  111. return ret;
  112. }
  113. static const struct file_operations proc_kpageswapn_operations = {
  114. .llseek = mem_lseek,
  115. .read = kpageswapn_read,
  116. };
  117. #endif /* CONFIG_SWAP*/
  118. /* /proc/kpageflags - an array exposing page flags
  119. *
  120. * Each entry is a u64 representing the corresponding
  121. * physical page flags.
  122. */
  123. static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
  124. {
  125. return ((kflags >> kbit) & 1) << ubit;
  126. }
  127. u64 stable_page_flags(struct page *page)
  128. {
  129. u64 k;
  130. u64 u;
  131. /*
  132. * pseudo flag: KPF_NOPAGE
  133. * it differentiates a memory hole from a page with no flags
  134. */
  135. if (!page)
  136. return 1 << KPF_NOPAGE;
  137. k = page->flags;
  138. u = 0;
  139. /*
  140. * pseudo flags for the well known (anonymous) memory mapped pages
  141. *
  142. * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the
  143. * simple test in page_mapped() is not enough.
  144. */
  145. if (!PageSlab(page) && page_mapped(page))
  146. u |= 1 << KPF_MMAP;
  147. if (PageAnon(page))
  148. u |= 1 << KPF_ANON;
  149. if (PageKsm(page))
  150. u |= 1 << KPF_KSM;
  151. /*
  152. * compound pages: export both head/tail info
  153. * they together define a compound page's start/end pos and order
  154. */
  155. if (PageHead(page))
  156. u |= 1 << KPF_COMPOUND_HEAD;
  157. if (PageTail(page))
  158. u |= 1 << KPF_COMPOUND_TAIL;
  159. if (PageHuge(page))
  160. u |= 1 << KPF_HUGE;
  161. /*
  162. * PageTransCompound can be true for non-huge compound pages (slab
  163. * pages or pages allocated by drivers with __GFP_COMP) because it
  164. * just checks PG_head/PG_tail, so we need to check PageLRU/PageAnon
  165. * to make sure a given page is a thp, not a non-huge compound page.
  166. */
  167. else if (PageTransCompound(page) && (PageLRU(compound_head(page)) ||
  168. PageAnon(compound_head(page))))
  169. u |= 1 << KPF_THP;
  170. /*
  171. * Caveats on high order pages: page->_count will only be set
  172. * -1 on the head page; SLUB/SLQB do the same for PG_slab;
  173. * SLOB won't set PG_slab at all on compound pages.
  174. */
  175. if (PageBuddy(page))
  176. u |= 1 << KPF_BUDDY;
  177. if (PageBalloon(page))
  178. u |= 1 << KPF_BALLOON;
  179. u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
  180. u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
  181. u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
  182. u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
  183. u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
  184. u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
  185. u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
  186. u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
  187. u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
  188. u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
  189. u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache);
  190. u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
  191. u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
  192. u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
  193. #ifdef CONFIG_MEMORY_FAILURE
  194. u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
  195. #endif
  196. #ifdef CONFIG_ARCH_USES_PG_UNCACHED
  197. u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
  198. #endif
  199. u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
  200. u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
  201. u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
  202. u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
  203. u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
  204. u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
  205. return u;
  206. };
  207. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  208. size_t count, loff_t *ppos)
  209. {
  210. u64 __user *out = (u64 __user *)buf;
  211. struct page *ppage;
  212. unsigned long src = *ppos;
  213. unsigned long pfn;
  214. unsigned long max_pfn_kpmsize = max_pfn * KPMSIZE;
  215. ssize_t ret = 0;
  216. pfn = src / KPMSIZE;
  217. if (src != max_pfn_kpmsize)
  218. count = min_t(unsigned long, count, max_pfn_kpmsize - src);
  219. if (src & KPMMASK || count & KPMMASK)
  220. return -EINVAL;
  221. while (count > 0) {
  222. if (pfn_valid(pfn))
  223. ppage = pfn_to_page(pfn);
  224. else
  225. ppage = NULL;
  226. if (put_user(stable_page_flags(ppage), out)) {
  227. ret = -EFAULT;
  228. break;
  229. }
  230. pfn++;
  231. out++;
  232. count -= KPMSIZE;
  233. }
  234. *ppos += (char __user *)out - buf;
  235. if (!ret)
  236. ret = (char __user *)out - buf;
  237. return ret;
  238. }
  239. static const struct file_operations proc_kpageflags_operations = {
  240. .llseek = mem_lseek,
  241. .read = kpageflags_read,
  242. };
  243. static int __init proc_page_init(void)
  244. {
  245. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  246. #ifdef CONFIG_SWAP /* M for pswap interface */
  247. proc_create("kpageswapn", S_IRUSR, NULL, &proc_kpageswapn_operations);
  248. #endif /* CONFIG_SWAP*/
  249. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  250. return 0;
  251. }
  252. fs_initcall(proc_page_init);