page.c 5.2 KB

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