page.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * Caveats on high order pages: page->_count will only be set
  105. * -1 on the head page; SLUB/SLQB do the same for PG_slab;
  106. * SLOB won't set PG_slab at all on compound pages.
  107. */
  108. if (PageBuddy(page))
  109. u |= 1 << KPF_BUDDY;
  110. u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
  111. u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
  112. u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
  113. u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
  114. u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
  115. u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
  116. u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
  117. u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
  118. u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
  119. u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
  120. u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache);
  121. u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
  122. u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
  123. u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
  124. #ifdef CONFIG_MEMORY_FAILURE
  125. u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
  126. #endif
  127. #ifdef CONFIG_ARCH_USES_PG_UNCACHED
  128. u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
  129. #endif
  130. u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
  131. u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
  132. u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
  133. u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
  134. u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
  135. u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
  136. return u;
  137. };
  138. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  139. size_t count, loff_t *ppos)
  140. {
  141. u64 __user *out = (u64 __user *)buf;
  142. struct page *ppage;
  143. unsigned long src = *ppos;
  144. unsigned long pfn;
  145. ssize_t ret = 0;
  146. pfn = src / KPMSIZE;
  147. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  148. if (src & KPMMASK || count & KPMMASK)
  149. return -EINVAL;
  150. while (count > 0) {
  151. if (pfn_valid(pfn))
  152. ppage = pfn_to_page(pfn);
  153. else
  154. ppage = NULL;
  155. if (put_user(stable_page_flags(ppage), out)) {
  156. ret = -EFAULT;
  157. break;
  158. }
  159. pfn++;
  160. out++;
  161. count -= KPMSIZE;
  162. }
  163. *ppos += (char __user *)out - buf;
  164. if (!ret)
  165. ret = (char __user *)out - buf;
  166. return ret;
  167. }
  168. static const struct file_operations proc_kpageflags_operations = {
  169. .llseek = mem_lseek,
  170. .read = kpageflags_read,
  171. };
  172. static int __init proc_page_init(void)
  173. {
  174. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  175. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  176. return 0;
  177. }
  178. module_init(proc_page_init);