page.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include <linux/bootmem.h>
  2. #include <linux/compiler.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/mm.h>
  6. #include <linux/mmzone.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/hugetlb.h>
  10. #include <asm/uaccess.h>
  11. #include "internal.h"
  12. #define KPMSIZE sizeof(u64)
  13. #define KPMMASK (KPMSIZE - 1)
  14. /* /proc/kpagecount - an array exposing page counts
  15. *
  16. * Each entry is a u64 representing the corresponding
  17. * physical page count.
  18. */
  19. static ssize_t kpagecount_read(struct file *file, char __user *buf,
  20. size_t count, loff_t *ppos)
  21. {
  22. u64 __user *out = (u64 __user *)buf;
  23. struct page *ppage;
  24. unsigned long src = *ppos;
  25. unsigned long pfn;
  26. ssize_t ret = 0;
  27. u64 pcount;
  28. pfn = src / KPMSIZE;
  29. count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
  30. if (src & KPMMASK || count & KPMMASK)
  31. return -EINVAL;
  32. while (count > 0) {
  33. if (pfn_valid(pfn))
  34. ppage = pfn_to_page(pfn);
  35. else
  36. ppage = NULL;
  37. if (!ppage)
  38. pcount = 0;
  39. else
  40. pcount = page_mapcount(ppage);
  41. if (put_user(pcount, out)) {
  42. ret = -EFAULT;
  43. break;
  44. }
  45. pfn++;
  46. out++;
  47. count -= KPMSIZE;
  48. }
  49. *ppos += (char __user *)out - buf;
  50. if (!ret)
  51. ret = (char __user *)out - buf;
  52. return ret;
  53. }
  54. static const struct file_operations proc_kpagecount_operations = {
  55. .llseek = mem_lseek,
  56. .read = kpagecount_read,
  57. };
  58. /* /proc/kpageflags - an array exposing page flags
  59. *
  60. * Each entry is a u64 representing the corresponding
  61. * physical page flags.
  62. */
  63. /* These macros are used to decouple internal flags from exported ones */
  64. #define KPF_LOCKED 0
  65. #define KPF_ERROR 1
  66. #define KPF_REFERENCED 2
  67. #define KPF_UPTODATE 3
  68. #define KPF_DIRTY 4
  69. #define KPF_LRU 5
  70. #define KPF_ACTIVE 6
  71. #define KPF_SLAB 7
  72. #define KPF_WRITEBACK 8
  73. #define KPF_RECLAIM 9
  74. #define KPF_BUDDY 10
  75. /* 11-20: new additions in 2.6.31 */
  76. #define KPF_MMAP 11
  77. #define KPF_ANON 12
  78. #define KPF_SWAPCACHE 13
  79. #define KPF_SWAPBACKED 14
  80. #define KPF_COMPOUND_HEAD 15
  81. #define KPF_COMPOUND_TAIL 16
  82. #define KPF_HUGE 17
  83. #define KPF_UNEVICTABLE 18
  84. #define KPF_NOPAGE 20
  85. /* kernel hacking assistances
  86. * WARNING: subject to change, never rely on them!
  87. */
  88. #define KPF_RESERVED 32
  89. #define KPF_MLOCKED 33
  90. #define KPF_MAPPEDTODISK 34
  91. #define KPF_PRIVATE 35
  92. #define KPF_PRIVATE_2 36
  93. #define KPF_OWNER_PRIVATE 37
  94. #define KPF_ARCH 38
  95. #define KPF_UNCACHED 39
  96. static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
  97. {
  98. return ((kflags >> kbit) & 1) << ubit;
  99. }
  100. static u64 get_uflags(struct page *page)
  101. {
  102. u64 k;
  103. u64 u;
  104. /*
  105. * pseudo flag: KPF_NOPAGE
  106. * it differentiates a memory hole from a page with no flags
  107. */
  108. if (!page)
  109. return 1 << KPF_NOPAGE;
  110. k = page->flags;
  111. u = 0;
  112. /*
  113. * pseudo flags for the well known (anonymous) memory mapped pages
  114. *
  115. * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the
  116. * simple test in page_mapped() is not enough.
  117. */
  118. if (!PageSlab(page) && page_mapped(page))
  119. u |= 1 << KPF_MMAP;
  120. if (PageAnon(page))
  121. u |= 1 << KPF_ANON;
  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 (PageHead(page))
  127. u |= 1 << KPF_COMPOUND_HEAD;
  128. if (PageTail(page))
  129. u |= 1 << KPF_COMPOUND_TAIL;
  130. if (PageHuge(page))
  131. u |= 1 << KPF_HUGE;
  132. u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
  133. /*
  134. * Caveats on high order pages:
  135. * PG_buddy will only be set on the head page; SLUB/SLQB do the same
  136. * for PG_slab; SLOB won't set PG_slab at all on compound pages.
  137. */
  138. u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
  139. u |= kpf_copy_bit(k, KPF_BUDDY, PG_buddy);
  140. u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
  141. u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
  142. u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
  143. u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
  144. u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
  145. u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
  146. u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
  147. u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
  148. u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache);
  149. u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
  150. u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
  151. u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
  152. #ifdef CONFIG_IA64_UNCACHED_ALLOCATOR
  153. u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
  154. #endif
  155. u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
  156. u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
  157. u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
  158. u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
  159. u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
  160. u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
  161. return u;
  162. };
  163. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  164. size_t count, loff_t *ppos)
  165. {
  166. u64 __user *out = (u64 __user *)buf;
  167. struct page *ppage;
  168. unsigned long src = *ppos;
  169. unsigned long pfn;
  170. ssize_t ret = 0;
  171. pfn = src / KPMSIZE;
  172. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  173. if (src & KPMMASK || count & KPMMASK)
  174. return -EINVAL;
  175. while (count > 0) {
  176. if (pfn_valid(pfn))
  177. ppage = pfn_to_page(pfn);
  178. else
  179. ppage = NULL;
  180. if (put_user(get_uflags(ppage), out)) {
  181. ret = -EFAULT;
  182. break;
  183. }
  184. pfn++;
  185. out++;
  186. count -= KPMSIZE;
  187. }
  188. *ppos += (char __user *)out - buf;
  189. if (!ret)
  190. ret = (char __user *)out - buf;
  191. return ret;
  192. }
  193. static const struct file_operations proc_kpageflags_operations = {
  194. .llseek = mem_lseek,
  195. .read = kpageflags_read,
  196. };
  197. static int __init proc_page_init(void)
  198. {
  199. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  200. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  201. return 0;
  202. }
  203. module_init(proc_page_init);