page.c 6.9 KB

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