page.c 7.2 KB

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