page.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/kernel-page-flags.h>
  13. #include <asm/uaccess.h>
  14. #include "internal.h"
  15. #define KPMSIZE sizeof(u64)
  16. #define KPMMASK (KPMSIZE - 1)
  17. /* /proc/kpagecount - an array exposing page counts
  18. *
  19. * Each entry is a u64 representing the corresponding
  20. * physical page count.
  21. */
  22. static ssize_t kpagecount_read(struct file *file, char __user *buf,
  23. size_t count, loff_t *ppos)
  24. {
  25. u64 __user *out = (u64 __user *)buf;
  26. struct page *ppage;
  27. unsigned long src = *ppos;
  28. unsigned long pfn;
  29. ssize_t ret = 0;
  30. u64 pcount;
  31. pfn = src / KPMSIZE;
  32. count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
  33. if (src & KPMMASK || count & KPMMASK)
  34. return -EINVAL;
  35. while (count > 0) {
  36. if (pfn_valid(pfn))
  37. ppage = pfn_to_page(pfn);
  38. else
  39. ppage = NULL;
  40. if (!ppage || PageSlab(ppage))
  41. pcount = 0;
  42. else
  43. pcount = page_mapcount(ppage);
  44. if (put_user(pcount, out)) {
  45. ret = -EFAULT;
  46. break;
  47. }
  48. pfn++;
  49. out++;
  50. count -= KPMSIZE;
  51. }
  52. *ppos += (char __user *)out - buf;
  53. if (!ret)
  54. ret = (char __user *)out - buf;
  55. return ret;
  56. }
  57. static const struct file_operations proc_kpagecount_operations = {
  58. .llseek = mem_lseek,
  59. .read = kpagecount_read,
  60. };
  61. /* /proc/kpageflags - an array exposing page flags
  62. *
  63. * Each entry is a u64 representing the corresponding
  64. * physical page flags.
  65. */
  66. static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
  67. {
  68. return ((kflags >> kbit) & 1) << ubit;
  69. }
  70. u64 stable_page_flags(struct page *page)
  71. {
  72. u64 k;
  73. u64 u;
  74. /*
  75. * pseudo flag: KPF_NOPAGE
  76. * it differentiates a memory hole from a page with no flags
  77. */
  78. if (!page)
  79. return 1 << KPF_NOPAGE;
  80. k = page->flags;
  81. u = 0;
  82. /*
  83. * pseudo flags for the well known (anonymous) memory mapped pages
  84. *
  85. * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the
  86. * simple test in page_mapped() is not enough.
  87. */
  88. if (!PageSlab(page) && page_mapped(page))
  89. u |= 1 << KPF_MMAP;
  90. if (PageAnon(page))
  91. u |= 1 << KPF_ANON;
  92. if (PageKsm(page))
  93. u |= 1 << KPF_KSM;
  94. /*
  95. * compound pages: export both head/tail info
  96. * they together define a compound page's start/end pos and order
  97. */
  98. if (PageHead(page))
  99. u |= 1 << KPF_COMPOUND_HEAD;
  100. if (PageTail(page))
  101. u |= 1 << KPF_COMPOUND_TAIL;
  102. if (PageHuge(page))
  103. u |= 1 << KPF_HUGE;
  104. /*
  105. * PageTransCompound can be true for non-huge compound pages (slab
  106. * pages or pages allocated by drivers with __GFP_COMP) because it
  107. * just checks PG_head/PG_tail, so we need to check PageLRU/PageAnon
  108. * to make sure a given page is a thp, not a non-huge compound page.
  109. */
  110. else if (PageTransCompound(page)) {
  111. struct page *head = compound_head(page);
  112. if (PageLRU(head) || PageAnon(head))
  113. u |= 1 << KPF_THP;
  114. else if (is_huge_zero_page(head)) {
  115. u |= 1 << KPF_ZERO_PAGE;
  116. u |= 1 << KPF_THP;
  117. }
  118. } else if (is_zero_pfn(page_to_pfn(page)))
  119. u |= 1 << KPF_ZERO_PAGE;
  120. /*
  121. * Caveats on high order pages: page->_count will only be set
  122. * -1 on the head page; SLUB/SLQB do the same for PG_slab;
  123. * SLOB won't set PG_slab at all on compound pages.
  124. */
  125. if (PageBuddy(page))
  126. u |= 1 << KPF_BUDDY;
  127. if (PageBalloon(page))
  128. u |= 1 << KPF_BALLOON;
  129. u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
  130. u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
  131. u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
  132. u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
  133. u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
  134. u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
  135. u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
  136. u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
  137. u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
  138. u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
  139. u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache);
  140. u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
  141. u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
  142. u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
  143. #ifdef CONFIG_MEMORY_FAILURE
  144. u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
  145. #endif
  146. #ifdef CONFIG_ARCH_USES_PG_UNCACHED
  147. u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
  148. #endif
  149. u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
  150. u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
  151. u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
  152. u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
  153. u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
  154. u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
  155. return u;
  156. };
  157. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  158. size_t count, loff_t *ppos)
  159. {
  160. u64 __user *out = (u64 __user *)buf;
  161. struct page *ppage;
  162. unsigned long src = *ppos;
  163. unsigned long pfn;
  164. ssize_t ret = 0;
  165. pfn = src / KPMSIZE;
  166. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  167. if (src & KPMMASK || count & KPMMASK)
  168. return -EINVAL;
  169. while (count > 0) {
  170. if (pfn_valid(pfn))
  171. ppage = pfn_to_page(pfn);
  172. else
  173. ppage = NULL;
  174. if (put_user(stable_page_flags(ppage), out)) {
  175. ret = -EFAULT;
  176. break;
  177. }
  178. pfn++;
  179. out++;
  180. count -= KPMSIZE;
  181. }
  182. *ppos += (char __user *)out - buf;
  183. if (!ret)
  184. ret = (char __user *)out - buf;
  185. return ret;
  186. }
  187. static const struct file_operations proc_kpageflags_operations = {
  188. .llseek = mem_lseek,
  189. .read = kpageflags_read,
  190. };
  191. static int __init proc_page_init(void)
  192. {
  193. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  194. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  195. return 0;
  196. }
  197. fs_initcall(proc_page_init);