page.c 6.7 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/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->_count 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. if (PageBalloon(page))
  133. u |= 1 << KPF_BALLOON;
  134. if (page_is_idle(page))
  135. u |= 1 << KPF_IDLE;
  136. u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
  137. u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
  138. u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
  139. u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
  140. u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
  141. u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
  142. u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
  143. u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
  144. u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
  145. u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
  146. u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache);
  147. u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
  148. u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
  149. u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
  150. #ifdef CONFIG_MEMORY_FAILURE
  151. u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
  152. #endif
  153. #ifdef CONFIG_ARCH_USES_PG_UNCACHED
  154. u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
  155. #endif
  156. u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
  157. u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
  158. u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
  159. u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
  160. u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
  161. u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
  162. return u;
  163. };
  164. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  165. size_t count, loff_t *ppos)
  166. {
  167. u64 __user *out = (u64 __user *)buf;
  168. struct page *ppage;
  169. unsigned long src = *ppos;
  170. unsigned long pfn;
  171. ssize_t ret = 0;
  172. pfn = src / KPMSIZE;
  173. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  174. if (src & KPMMASK || count & KPMMASK)
  175. return -EINVAL;
  176. while (count > 0) {
  177. if (pfn_valid(pfn))
  178. ppage = pfn_to_page(pfn);
  179. else
  180. ppage = NULL;
  181. if (put_user(stable_page_flags(ppage), out)) {
  182. ret = -EFAULT;
  183. break;
  184. }
  185. pfn++;
  186. out++;
  187. count -= KPMSIZE;
  188. cond_resched();
  189. }
  190. *ppos += (char __user *)out - buf;
  191. if (!ret)
  192. ret = (char __user *)out - buf;
  193. return ret;
  194. }
  195. static const struct file_operations proc_kpageflags_operations = {
  196. .llseek = mem_lseek,
  197. .read = kpageflags_read,
  198. };
  199. #ifdef CONFIG_MEMCG
  200. static ssize_t kpagecgroup_read(struct file *file, char __user *buf,
  201. size_t count, loff_t *ppos)
  202. {
  203. u64 __user *out = (u64 __user *)buf;
  204. struct page *ppage;
  205. unsigned long src = *ppos;
  206. unsigned long pfn;
  207. ssize_t ret = 0;
  208. u64 ino;
  209. pfn = src / KPMSIZE;
  210. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  211. if (src & KPMMASK || count & KPMMASK)
  212. return -EINVAL;
  213. while (count > 0) {
  214. if (pfn_valid(pfn))
  215. ppage = pfn_to_page(pfn);
  216. else
  217. ppage = NULL;
  218. if (ppage)
  219. ino = page_cgroup_ino(ppage);
  220. else
  221. ino = 0;
  222. if (put_user(ino, out)) {
  223. ret = -EFAULT;
  224. break;
  225. }
  226. pfn++;
  227. out++;
  228. count -= KPMSIZE;
  229. cond_resched();
  230. }
  231. *ppos += (char __user *)out - buf;
  232. if (!ret)
  233. ret = (char __user *)out - buf;
  234. return ret;
  235. }
  236. static const struct file_operations proc_kpagecgroup_operations = {
  237. .llseek = mem_lseek,
  238. .read = kpagecgroup_read,
  239. };
  240. #endif /* CONFIG_MEMCG */
  241. static int __init proc_page_init(void)
  242. {
  243. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  244. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  245. #ifdef CONFIG_MEMCG
  246. proc_create("kpagecgroup", S_IRUSR, NULL, &proc_kpagecgroup_operations);
  247. #endif
  248. return 0;
  249. }
  250. fs_initcall(proc_page_init);