page.c 6.9 KB

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