blob: 2281c2cbfe2b4ddeefc5d4417de1e40dd7df4dfb [file] [log] [blame]
Alexey Dobriyan6d80e532008-10-06 14:26:12 +04001#include <linux/bootmem.h>
2#include <linux/compiler.h>
3#include <linux/fs.h>
4#include <linux/init.h>
Hugh Dickins9a840892009-09-21 17:02:01 -07005#include <linux/ksm.h>
Alexey Dobriyan6d80e532008-10-06 14:26:12 +04006#include <linux/mm.h>
7#include <linux/mmzone.h>
8#include <linux/proc_fs.h>
9#include <linux/seq_file.h>
Wu Fengguang20a03072009-06-16 15:32:22 -070010#include <linux/hugetlb.h>
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040011#include <asm/uaccess.h>
12#include "internal.h"
13
14#define KPMSIZE sizeof(u64)
15#define KPMMASK (KPMSIZE - 1)
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070016
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040017/* /proc/kpagecount - an array exposing page counts
18 *
19 * Each entry is a u64 representing the corresponding
20 * physical page count.
21 */
22static 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
32 pfn = src / KPMSIZE;
33 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
34 if (src & KPMMASK || count & KPMMASK)
35 return -EINVAL;
36
37 while (count > 0) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040038 if (pfn_valid(pfn))
39 ppage = pfn_to_page(pfn);
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070040 else
41 ppage = NULL;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040042 if (!ppage)
43 pcount = 0;
44 else
45 pcount = page_mapcount(ppage);
46
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070047 if (put_user(pcount, out)) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040048 ret = -EFAULT;
49 break;
50 }
51
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070052 pfn++;
53 out++;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040054 count -= KPMSIZE;
55 }
56
57 *ppos += (char __user *)out - buf;
58 if (!ret)
59 ret = (char __user *)out - buf;
60 return ret;
61}
62
63static const struct file_operations proc_kpagecount_operations = {
64 .llseek = mem_lseek,
65 .read = kpagecount_read,
66};
67
68/* /proc/kpageflags - an array exposing page flags
69 *
70 * Each entry is a u64 representing the corresponding
71 * physical page flags.
72 */
73
74/* These macros are used to decouple internal flags from exported ones */
75
Wu Fengguang17797542009-06-16 15:32:24 -070076#define KPF_LOCKED 0
77#define KPF_ERROR 1
78#define KPF_REFERENCED 2
79#define KPF_UPTODATE 3
80#define KPF_DIRTY 4
81#define KPF_LRU 5
82#define KPF_ACTIVE 6
83#define KPF_SLAB 7
84#define KPF_WRITEBACK 8
85#define KPF_RECLAIM 9
86#define KPF_BUDDY 10
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040087
Wu Fengguang17797542009-06-16 15:32:24 -070088/* 11-20: new additions in 2.6.31 */
89#define KPF_MMAP 11
90#define KPF_ANON 12
91#define KPF_SWAPCACHE 13
92#define KPF_SWAPBACKED 14
93#define KPF_COMPOUND_HEAD 15
94#define KPF_COMPOUND_TAIL 16
95#define KPF_HUGE 17
96#define KPF_UNEVICTABLE 18
97#define KPF_NOPAGE 20
98
Hugh Dickins9a840892009-09-21 17:02:01 -070099#define KPF_KSM 21
100
Wu Fengguang17797542009-06-16 15:32:24 -0700101/* kernel hacking assistances
102 * WARNING: subject to change, never rely on them!
103 */
104#define KPF_RESERVED 32
105#define KPF_MLOCKED 33
106#define KPF_MAPPEDTODISK 34
107#define KPF_PRIVATE 35
108#define KPF_PRIVATE_2 36
109#define KPF_OWNER_PRIVATE 37
110#define KPF_ARCH 38
111#define KPF_UNCACHED 39
112
113static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
114{
115 return ((kflags >> kbit) & 1) << ubit;
116}
117
118static u64 get_uflags(struct page *page)
119{
120 u64 k;
121 u64 u;
122
123 /*
124 * pseudo flag: KPF_NOPAGE
125 * it differentiates a memory hole from a page with no flags
126 */
127 if (!page)
128 return 1 << KPF_NOPAGE;
129
130 k = page->flags;
131 u = 0;
132
133 /*
134 * pseudo flags for the well known (anonymous) memory mapped pages
135 *
136 * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the
137 * simple test in page_mapped() is not enough.
138 */
139 if (!PageSlab(page) && page_mapped(page))
140 u |= 1 << KPF_MMAP;
141 if (PageAnon(page))
142 u |= 1 << KPF_ANON;
Hugh Dickins9a840892009-09-21 17:02:01 -0700143 if (PageKsm(page))
144 u |= 1 << KPF_KSM;
Wu Fengguang17797542009-06-16 15:32:24 -0700145
146 /*
147 * compound pages: export both head/tail info
148 * they together define a compound page's start/end pos and order
149 */
150 if (PageHead(page))
151 u |= 1 << KPF_COMPOUND_HEAD;
152 if (PageTail(page))
153 u |= 1 << KPF_COMPOUND_TAIL;
154 if (PageHuge(page))
155 u |= 1 << KPF_HUGE;
156
157 u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
158
159 /*
160 * Caveats on high order pages:
161 * PG_buddy will only be set on the head page; SLUB/SLQB do the same
162 * for PG_slab; SLOB won't set PG_slab at all on compound pages.
163 */
164 u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
165 u |= kpf_copy_bit(k, KPF_BUDDY, PG_buddy);
166
167 u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
168 u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
169 u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
170 u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
171
172 u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
173 u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
174 u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
175 u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
176
177 u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache);
178 u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
179
Wu Fengguang17797542009-06-16 15:32:24 -0700180 u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
181 u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
Wu Fengguang17797542009-06-16 15:32:24 -0700182
183#ifdef CONFIG_IA64_UNCACHED_ALLOCATOR
184 u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
185#endif
186
187 u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
188 u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
189 u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
190 u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
191 u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
192 u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
193
194 return u;
195};
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400196
197static ssize_t kpageflags_read(struct file *file, char __user *buf,
198 size_t count, loff_t *ppos)
199{
200 u64 __user *out = (u64 __user *)buf;
201 struct page *ppage;
202 unsigned long src = *ppos;
203 unsigned long pfn;
204 ssize_t ret = 0;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400205
206 pfn = src / KPMSIZE;
207 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
208 if (src & KPMMASK || count & KPMMASK)
209 return -EINVAL;
210
211 while (count > 0) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400212 if (pfn_valid(pfn))
213 ppage = pfn_to_page(pfn);
Wu Fengguanged7ce0f2009-06-16 15:32:23 -0700214 else
215 ppage = NULL;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400216
Wu Fengguang17797542009-06-16 15:32:24 -0700217 if (put_user(get_uflags(ppage), out)) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400218 ret = -EFAULT;
219 break;
220 }
221
Wu Fengguanged7ce0f2009-06-16 15:32:23 -0700222 pfn++;
223 out++;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400224 count -= KPMSIZE;
225 }
226
227 *ppos += (char __user *)out - buf;
228 if (!ret)
229 ret = (char __user *)out - buf;
230 return ret;
231}
232
233static const struct file_operations proc_kpageflags_operations = {
234 .llseek = mem_lseek,
235 .read = kpageflags_read,
236};
237
238static int __init proc_page_init(void)
239{
240 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
241 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
242 return 0;
243}
244module_init(proc_page_init);