1 #include <linux/bootmem.h>
2 #include <linux/compiler.h>
4 #include <linux/init.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 <asm/uaccess.h>
14 #define KPMSIZE sizeof(u64)
15 #define KPMMASK (KPMSIZE - 1)
17 /* /proc/kpagecount - an array exposing page counts
19 * Each entry is a u64 representing the corresponding
20 * physical page count.
22 static ssize_t kpagecount_read(struct file *file, char __user *buf,
23 size_t count, loff_t *ppos)
25 u64 __user *out = (u64 __user *)buf;
27 unsigned long src = *ppos;
33 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
34 if (src & KPMMASK || count & KPMMASK)
39 ppage = pfn_to_page(pfn);
45 pcount = page_mapcount(ppage);
47 if (put_user(pcount, out)) {
57 *ppos += (char __user *)out - buf;
59 ret = (char __user *)out - buf;
63 static const struct file_operations proc_kpagecount_operations = {
65 .read = kpagecount_read,
68 /* /proc/kpageflags - an array exposing page flags
70 * Each entry is a u64 representing the corresponding
71 * physical page flags.
74 /* These macros are used to decouple internal flags from exported ones */
78 #define KPF_REFERENCED 2
79 #define KPF_UPTODATE 3
84 #define KPF_WRITEBACK 8
88 /* 11-20: new additions in 2.6.31 */
91 #define KPF_SWAPCACHE 13
92 #define KPF_SWAPBACKED 14
93 #define KPF_COMPOUND_HEAD 15
94 #define KPF_COMPOUND_TAIL 16
96 #define KPF_UNEVICTABLE 18
97 #define KPF_HWPOISON 19
102 /* kernel hacking assistances
103 * WARNING: subject to change, never rely on them!
105 #define KPF_RESERVED 32
106 #define KPF_MLOCKED 33
107 #define KPF_MAPPEDTODISK 34
108 #define KPF_PRIVATE 35
109 #define KPF_PRIVATE_2 36
110 #define KPF_OWNER_PRIVATE 37
112 #define KPF_UNCACHED 39
114 static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
116 return ((kflags >> kbit) & 1) << ubit;
119 static u64 get_uflags(struct page *page)
125 * pseudo flag: KPF_NOPAGE
126 * it differentiates a memory hole from a page with no flags
129 return 1 << KPF_NOPAGE;
135 * pseudo flags for the well known (anonymous) memory mapped pages
137 * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the
138 * simple test in page_mapped() is not enough.
140 if (!PageSlab(page) && page_mapped(page))
148 * compound pages: export both head/tail info
149 * they together define a compound page's start/end pos and order
152 u |= 1 << KPF_COMPOUND_HEAD;
154 u |= 1 << KPF_COMPOUND_TAIL;
158 u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
161 * Caveats on high order pages:
162 * PG_buddy will only be set on the head page; SLUB/SLQB do the same
163 * for PG_slab; SLOB won't set PG_slab at all on compound pages.
165 u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
166 u |= kpf_copy_bit(k, KPF_BUDDY, PG_buddy);
168 u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
169 u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
170 u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
171 u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
173 u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
174 u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
175 u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
176 u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
178 u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache);
179 u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
181 u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
182 u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
184 #ifdef CONFIG_MEMORY_FAILURE
185 u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
188 #ifdef CONFIG_IA64_UNCACHED_ALLOCATOR
189 u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
192 u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
193 u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
194 u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
195 u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
196 u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
197 u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
202 static ssize_t kpageflags_read(struct file *file, char __user *buf,
203 size_t count, loff_t *ppos)
205 u64 __user *out = (u64 __user *)buf;
207 unsigned long src = *ppos;
212 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
213 if (src & KPMMASK || count & KPMMASK)
218 ppage = pfn_to_page(pfn);
222 if (put_user(get_uflags(ppage), out)) {
232 *ppos += (char __user *)out - buf;
234 ret = (char __user *)out - buf;
238 static const struct file_operations proc_kpageflags_operations = {
240 .read = kpageflags_read,
243 static int __init proc_page_init(void)
245 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
246 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
249 module_init(proc_page_init);