page.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/memblock.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/memremap.h>
  14. #include <linux/memcontrol.h>
  15. #include <linux/mmu_notifier.h>
  16. #include <linux/page_idle.h>
  17. #include <linux/kernel-page-flags.h>
  18. #include <linux/uaccess.h>
  19. #include "internal.h"
  20. #define KPMSIZE sizeof(u64)
  21. #define KPMMASK (KPMSIZE - 1)
  22. #define KPMBITS (KPMSIZE * BITS_PER_BYTE)
  23. static inline unsigned long get_max_dump_pfn(void)
  24. {
  25. #ifdef CONFIG_SPARSEMEM
  26. /*
  27. * The memmap of early sections is completely populated and marked
  28. * online even if max_pfn does not fall on a section boundary -
  29. * pfn_to_online_page() will succeed on all pages. Allow inspecting
  30. * these memmaps.
  31. */
  32. return round_up(max_pfn, PAGES_PER_SECTION);
  33. #else
  34. return max_pfn;
  35. #endif
  36. }
  37. /* /proc/kpagecount - an array exposing page mapcounts
  38. *
  39. * Each entry is a u64 representing the corresponding
  40. * physical page mapcount.
  41. */
  42. static ssize_t kpagecount_read(struct file *file, char __user *buf,
  43. size_t count, loff_t *ppos)
  44. {
  45. const unsigned long max_dump_pfn = get_max_dump_pfn();
  46. u64 __user *out = (u64 __user *)buf;
  47. unsigned long src = *ppos;
  48. unsigned long pfn;
  49. ssize_t ret = 0;
  50. pfn = src / KPMSIZE;
  51. if (src & KPMMASK || count & KPMMASK)
  52. return -EINVAL;
  53. if (src >= max_dump_pfn * KPMSIZE)
  54. return 0;
  55. count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);
  56. while (count > 0) {
  57. struct page *page;
  58. u64 mapcount = 0;
  59. /*
  60. * TODO: ZONE_DEVICE support requires to identify
  61. * memmaps that were actually initialized.
  62. */
  63. page = pfn_to_online_page(pfn);
  64. if (page)
  65. mapcount = folio_precise_page_mapcount(page_folio(page),
  66. page);
  67. if (put_user(mapcount, out)) {
  68. ret = -EFAULT;
  69. break;
  70. }
  71. pfn++;
  72. out++;
  73. count -= KPMSIZE;
  74. cond_resched();
  75. }
  76. *ppos += (char __user *)out - buf;
  77. if (!ret)
  78. ret = (char __user *)out - buf;
  79. return ret;
  80. }
  81. static const struct proc_ops kpagecount_proc_ops = {
  82. .proc_flags = PROC_ENTRY_PERMANENT,
  83. .proc_lseek = mem_lseek,
  84. .proc_read = kpagecount_read,
  85. };
  86. /* /proc/kpageflags - an array exposing page flags
  87. *
  88. * Each entry is a u64 representing the corresponding
  89. * physical page flags.
  90. */
  91. static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
  92. {
  93. return ((kflags >> kbit) & 1) << ubit;
  94. }
  95. u64 stable_page_flags(const struct page *page)
  96. {
  97. const struct folio *folio;
  98. unsigned long k;
  99. unsigned long mapping;
  100. bool is_anon;
  101. u64 u = 0;
  102. /*
  103. * pseudo flag: KPF_NOPAGE
  104. * it differentiates a memory hole from a page with no flags
  105. */
  106. if (!page)
  107. return 1 << KPF_NOPAGE;
  108. folio = page_folio(page);
  109. k = folio->flags;
  110. mapping = (unsigned long)folio->mapping;
  111. is_anon = mapping & PAGE_MAPPING_ANON;
  112. /*
  113. * pseudo flags for the well known (anonymous) memory mapped pages
  114. */
  115. if (page_mapped(page))
  116. u |= 1 << KPF_MMAP;
  117. if (is_anon) {
  118. u |= 1 << KPF_ANON;
  119. if (mapping & PAGE_MAPPING_KSM)
  120. u |= 1 << KPF_KSM;
  121. }
  122. /*
  123. * compound pages: export both head/tail info
  124. * they together define a compound page's start/end pos and order
  125. */
  126. if (page == &folio->page)
  127. u |= kpf_copy_bit(k, KPF_COMPOUND_HEAD, PG_head);
  128. else
  129. u |= 1 << KPF_COMPOUND_TAIL;
  130. if (folio_test_hugetlb(folio))
  131. u |= 1 << KPF_HUGE;
  132. else if (folio_test_large(folio) &&
  133. folio_test_large_rmappable(folio)) {
  134. /* Note: we indicate any THPs here, not just PMD-sized ones */
  135. u |= 1 << KPF_THP;
  136. } else if (is_huge_zero_folio(folio)) {
  137. u |= 1 << KPF_ZERO_PAGE;
  138. u |= 1 << KPF_THP;
  139. } else if (is_zero_folio(folio)) {
  140. u |= 1 << KPF_ZERO_PAGE;
  141. }
  142. /*
  143. * Caveats on high order pages: PG_buddy and PG_slab will only be set
  144. * on the head page.
  145. */
  146. if (PageBuddy(page))
  147. u |= 1 << KPF_BUDDY;
  148. else if (page_count(page) == 0 && is_free_buddy_page(page))
  149. u |= 1 << KPF_BUDDY;
  150. if (PageOffline(page))
  151. u |= 1 << KPF_OFFLINE;
  152. if (PageTable(page))
  153. u |= 1 << KPF_PGTABLE;
  154. if (folio_test_slab(folio))
  155. u |= 1 << KPF_SLAB;
  156. #if defined(CONFIG_PAGE_IDLE_FLAG) && defined(CONFIG_64BIT)
  157. u |= kpf_copy_bit(k, KPF_IDLE, PG_idle);
  158. #else
  159. if (folio_test_idle(folio))
  160. u |= 1 << KPF_IDLE;
  161. #endif
  162. u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
  163. u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
  164. u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
  165. u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
  166. u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
  167. u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
  168. u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
  169. u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
  170. #define SWAPCACHE ((1 << PG_swapbacked) | (1 << PG_swapcache))
  171. if ((k & SWAPCACHE) == SWAPCACHE)
  172. u |= 1 << KPF_SWAPCACHE;
  173. u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
  174. u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
  175. u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
  176. #ifdef CONFIG_MEMORY_FAILURE
  177. if (u & (1 << KPF_HUGE))
  178. u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
  179. else
  180. u |= kpf_copy_bit(page->flags, KPF_HWPOISON, PG_hwpoison);
  181. #endif
  182. u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
  183. u |= kpf_copy_bit(k, KPF_OWNER_2, PG_owner_2);
  184. u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
  185. u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
  186. u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
  187. u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
  188. #ifdef CONFIG_ARCH_USES_PG_ARCH_2
  189. u |= kpf_copy_bit(k, KPF_ARCH_2, PG_arch_2);
  190. #endif
  191. #ifdef CONFIG_ARCH_USES_PG_ARCH_3
  192. u |= kpf_copy_bit(k, KPF_ARCH_3, PG_arch_3);
  193. #endif
  194. return u;
  195. };
  196. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  197. size_t count, loff_t *ppos)
  198. {
  199. const unsigned long max_dump_pfn = get_max_dump_pfn();
  200. u64 __user *out = (u64 __user *)buf;
  201. unsigned long src = *ppos;
  202. unsigned long pfn;
  203. ssize_t ret = 0;
  204. pfn = src / KPMSIZE;
  205. if (src & KPMMASK || count & KPMMASK)
  206. return -EINVAL;
  207. if (src >= max_dump_pfn * KPMSIZE)
  208. return 0;
  209. count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);
  210. while (count > 0) {
  211. /*
  212. * TODO: ZONE_DEVICE support requires to identify
  213. * memmaps that were actually initialized.
  214. */
  215. struct page *page = pfn_to_online_page(pfn);
  216. if (put_user(stable_page_flags(page), out)) {
  217. ret = -EFAULT;
  218. break;
  219. }
  220. pfn++;
  221. out++;
  222. count -= KPMSIZE;
  223. cond_resched();
  224. }
  225. *ppos += (char __user *)out - buf;
  226. if (!ret)
  227. ret = (char __user *)out - buf;
  228. return ret;
  229. }
  230. static const struct proc_ops kpageflags_proc_ops = {
  231. .proc_flags = PROC_ENTRY_PERMANENT,
  232. .proc_lseek = mem_lseek,
  233. .proc_read = kpageflags_read,
  234. };
  235. #ifdef CONFIG_MEMCG
  236. static ssize_t kpagecgroup_read(struct file *file, char __user *buf,
  237. size_t count, loff_t *ppos)
  238. {
  239. const unsigned long max_dump_pfn = get_max_dump_pfn();
  240. u64 __user *out = (u64 __user *)buf;
  241. struct page *ppage;
  242. unsigned long src = *ppos;
  243. unsigned long pfn;
  244. ssize_t ret = 0;
  245. u64 ino;
  246. pfn = src / KPMSIZE;
  247. if (src & KPMMASK || count & KPMMASK)
  248. return -EINVAL;
  249. if (src >= max_dump_pfn * KPMSIZE)
  250. return 0;
  251. count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);
  252. while (count > 0) {
  253. /*
  254. * TODO: ZONE_DEVICE support requires to identify
  255. * memmaps that were actually initialized.
  256. */
  257. ppage = pfn_to_online_page(pfn);
  258. if (ppage)
  259. ino = page_cgroup_ino(ppage);
  260. else
  261. ino = 0;
  262. if (put_user(ino, out)) {
  263. ret = -EFAULT;
  264. break;
  265. }
  266. pfn++;
  267. out++;
  268. count -= KPMSIZE;
  269. cond_resched();
  270. }
  271. *ppos += (char __user *)out - buf;
  272. if (!ret)
  273. ret = (char __user *)out - buf;
  274. return ret;
  275. }
  276. static const struct proc_ops kpagecgroup_proc_ops = {
  277. .proc_flags = PROC_ENTRY_PERMANENT,
  278. .proc_lseek = mem_lseek,
  279. .proc_read = kpagecgroup_read,
  280. };
  281. #endif /* CONFIG_MEMCG */
  282. static int __init proc_page_init(void)
  283. {
  284. proc_create("kpagecount", S_IRUSR, NULL, &kpagecount_proc_ops);
  285. proc_create("kpageflags", S_IRUSR, NULL, &kpageflags_proc_ops);
  286. #ifdef CONFIG_MEMCG
  287. proc_create("kpagecgroup", S_IRUSR, NULL, &kpagecgroup_proc_ops);
  288. #endif
  289. return 0;
  290. }
  291. fs_initcall(proc_page_init);