page.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 (PageTable(page))
  138. u |= 1 << KPF_PGTABLE;
  139. if (page_is_idle(page))
  140. u |= 1 << KPF_IDLE;
  141. u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
  142. u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
  143. if (PageTail(page) && PageSlab(compound_head(page)))
  144. u |= 1 << KPF_SLAB;
  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. if (PageSwapCache(page))
  154. u |= 1 << KPF_SWAPCACHE;
  155. u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
  156. u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
  157. u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
  158. #ifdef CONFIG_MEMORY_FAILURE
  159. u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
  160. #endif
  161. #ifdef CONFIG_ARCH_USES_PG_UNCACHED
  162. u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
  163. #endif
  164. u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
  165. u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
  166. u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
  167. u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
  168. u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
  169. u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
  170. return u;
  171. };
  172. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  173. size_t count, loff_t *ppos)
  174. {
  175. u64 __user *out = (u64 __user *)buf;
  176. struct page *ppage;
  177. unsigned long src = *ppos;
  178. unsigned long pfn;
  179. ssize_t ret = 0;
  180. pfn = src / KPMSIZE;
  181. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  182. if (src & KPMMASK || count & KPMMASK)
  183. return -EINVAL;
  184. while (count > 0) {
  185. if (pfn_valid(pfn))
  186. ppage = pfn_to_page(pfn);
  187. else
  188. ppage = NULL;
  189. if (put_user(stable_page_flags(ppage), out)) {
  190. ret = -EFAULT;
  191. break;
  192. }
  193. pfn++;
  194. out++;
  195. count -= KPMSIZE;
  196. cond_resched();
  197. }
  198. *ppos += (char __user *)out - buf;
  199. if (!ret)
  200. ret = (char __user *)out - buf;
  201. return ret;
  202. }
  203. static const struct file_operations proc_kpageflags_operations = {
  204. .llseek = mem_lseek,
  205. .read = kpageflags_read,
  206. };
  207. #ifdef CONFIG_MEMCG
  208. static ssize_t kpagecgroup_read(struct file *file, char __user *buf,
  209. size_t count, loff_t *ppos)
  210. {
  211. u64 __user *out = (u64 __user *)buf;
  212. struct page *ppage;
  213. unsigned long src = *ppos;
  214. unsigned long pfn;
  215. ssize_t ret = 0;
  216. u64 ino;
  217. pfn = src / 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 (ppage)
  227. ino = page_cgroup_ino(ppage);
  228. else
  229. ino = 0;
  230. if (put_user(ino, out)) {
  231. ret = -EFAULT;
  232. break;
  233. }
  234. pfn++;
  235. out++;
  236. count -= KPMSIZE;
  237. cond_resched();
  238. }
  239. *ppos += (char __user *)out - buf;
  240. if (!ret)
  241. ret = (char __user *)out - buf;
  242. return ret;
  243. }
  244. static const struct file_operations proc_kpagecgroup_operations = {
  245. .llseek = mem_lseek,
  246. .read = kpagecgroup_read,
  247. };
  248. #endif /* CONFIG_MEMCG */
  249. static int __init proc_page_init(void)
  250. {
  251. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  252. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  253. #ifdef CONFIG_MEMCG
  254. proc_create("kpagecgroup", S_IRUSR, NULL, &proc_kpagecgroup_operations);
  255. #endif
  256. return 0;
  257. }
  258. fs_initcall(proc_page_init);