Commit | Line | Data |
---|---|---|
6d80e53f AD |
1 | #include <linux/bootmem.h> |
2 | #include <linux/compiler.h> | |
3 | #include <linux/fs.h> | |
4 | #include <linux/init.h> | |
9a840895 | 5 | #include <linux/ksm.h> |
6d80e53f AD |
6 | #include <linux/mm.h> |
7 | #include <linux/mmzone.h> | |
56873f43 | 8 | #include <linux/huge_mm.h> |
6d80e53f AD |
9 | #include <linux/proc_fs.h> |
10 | #include <linux/seq_file.h> | |
20a0307c | 11 | #include <linux/hugetlb.h> |
80ae2fdc | 12 | #include <linux/memcontrol.h> |
1a9b5b7f | 13 | #include <linux/kernel-page-flags.h> |
6d80e53f AD |
14 | #include <asm/uaccess.h> |
15 | #include "internal.h" | |
16 | ||
17 | #define KPMSIZE sizeof(u64) | |
18 | #define KPMMASK (KPMSIZE - 1) | |
ed7ce0f1 | 19 | |
6d80e53f AD |
20 | /* /proc/kpagecount - an array exposing page counts |
21 | * | |
22 | * Each entry is a u64 representing the corresponding | |
23 | * physical page count. | |
24 | */ | |
25 | static ssize_t kpagecount_read(struct file *file, char __user *buf, | |
26 | size_t count, loff_t *ppos) | |
27 | { | |
28 | u64 __user *out = (u64 __user *)buf; | |
29 | struct page *ppage; | |
30 | unsigned long src = *ppos; | |
31 | unsigned long pfn; | |
32 | ssize_t ret = 0; | |
33 | u64 pcount; | |
34 | ||
35 | pfn = src / KPMSIZE; | |
36 | count = min_t(size_t, count, (max_pfn * KPMSIZE) - src); | |
37 | if (src & KPMMASK || count & KPMMASK) | |
38 | return -EINVAL; | |
39 | ||
40 | while (count > 0) { | |
6d80e53f AD |
41 | if (pfn_valid(pfn)) |
42 | ppage = pfn_to_page(pfn); | |
ed7ce0f1 WF |
43 | else |
44 | ppage = NULL; | |
a6fc86d2 | 45 | if (!ppage || PageSlab(ppage)) |
6d80e53f AD |
46 | pcount = 0; |
47 | else | |
48 | pcount = page_mapcount(ppage); | |
49 | ||
ed7ce0f1 | 50 | if (put_user(pcount, out)) { |
6d80e53f AD |
51 | ret = -EFAULT; |
52 | break; | |
53 | } | |
54 | ||
ed7ce0f1 WF |
55 | pfn++; |
56 | out++; | |
6d80e53f AD |
57 | count -= KPMSIZE; |
58 | } | |
59 | ||
60 | *ppos += (char __user *)out - buf; | |
61 | if (!ret) | |
62 | ret = (char __user *)out - buf; | |
63 | return ret; | |
64 | } | |
65 | ||
66 | static const struct file_operations proc_kpagecount_operations = { | |
67 | .llseek = mem_lseek, | |
68 | .read = kpagecount_read, | |
69 | }; | |
70 | ||
71 | /* /proc/kpageflags - an array exposing page flags | |
72 | * | |
73 | * Each entry is a u64 representing the corresponding | |
74 | * physical page flags. | |
75 | */ | |
76 | ||
17797549 WF |
77 | static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit) |
78 | { | |
79 | return ((kflags >> kbit) & 1) << ubit; | |
80 | } | |
81 | ||
1a9b5b7f | 82 | u64 stable_page_flags(struct page *page) |
17797549 WF |
83 | { |
84 | u64 k; | |
85 | u64 u; | |
86 | ||
87 | /* | |
88 | * pseudo flag: KPF_NOPAGE | |
89 | * it differentiates a memory hole from a page with no flags | |
90 | */ | |
91 | if (!page) | |
92 | return 1 << KPF_NOPAGE; | |
93 | ||
94 | k = page->flags; | |
95 | u = 0; | |
96 | ||
97 | /* | |
98 | * pseudo flags for the well known (anonymous) memory mapped pages | |
99 | * | |
100 | * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the | |
101 | * simple test in page_mapped() is not enough. | |
102 | */ | |
103 | if (!PageSlab(page) && page_mapped(page)) | |
104 | u |= 1 << KPF_MMAP; | |
105 | if (PageAnon(page)) | |
106 | u |= 1 << KPF_ANON; | |
9a840895 HD |
107 | if (PageKsm(page)) |
108 | u |= 1 << KPF_KSM; | |
17797549 WF |
109 | |
110 | /* | |
111 | * compound pages: export both head/tail info | |
112 | * they together define a compound page's start/end pos and order | |
113 | */ | |
114 | if (PageHead(page)) | |
115 | u |= 1 << KPF_COMPOUND_HEAD; | |
116 | if (PageTail(page)) | |
117 | u |= 1 << KPF_COMPOUND_TAIL; | |
118 | if (PageHuge(page)) | |
119 | u |= 1 << KPF_HUGE; | |
7a71932d NH |
120 | /* |
121 | * PageTransCompound can be true for non-huge compound pages (slab | |
122 | * pages or pages allocated by drivers with __GFP_COMP) because it | |
e3bba3c3 NH |
123 | * just checks PG_head/PG_tail, so we need to check PageLRU/PageAnon |
124 | * to make sure a given page is a thp, not a non-huge compound page. | |
7a71932d | 125 | */ |
56873f43 WY |
126 | else if (PageTransCompound(page)) { |
127 | struct page *head = compound_head(page); | |
128 | ||
129 | if (PageLRU(head) || PageAnon(head)) | |
130 | u |= 1 << KPF_THP; | |
131 | else if (is_huge_zero_page(head)) { | |
132 | u |= 1 << KPF_ZERO_PAGE; | |
133 | u |= 1 << KPF_THP; | |
134 | } | |
135 | } else if (is_zero_pfn(page_to_pfn(page))) | |
136 | u |= 1 << KPF_ZERO_PAGE; | |
137 | ||
17797549 | 138 | |
17797549 | 139 | /* |
5f24ce5f AA |
140 | * Caveats on high order pages: page->_count will only be set |
141 | * -1 on the head page; SLUB/SLQB do the same for PG_slab; | |
142 | * SLOB won't set PG_slab at all on compound pages. | |
17797549 | 143 | */ |
5f24ce5f AA |
144 | if (PageBuddy(page)) |
145 | u |= 1 << KPF_BUDDY; | |
146 | ||
09316c09 KK |
147 | if (PageBalloon(page)) |
148 | u |= 1 << KPF_BALLOON; | |
149 | ||
5f24ce5f AA |
150 | u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked); |
151 | ||
17797549 | 152 | u |= kpf_copy_bit(k, KPF_SLAB, PG_slab); |
17797549 WF |
153 | |
154 | u |= kpf_copy_bit(k, KPF_ERROR, PG_error); | |
155 | u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty); | |
156 | u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate); | |
157 | u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback); | |
158 | ||
159 | u |= kpf_copy_bit(k, KPF_LRU, PG_lru); | |
160 | u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced); | |
161 | u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active); | |
162 | u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim); | |
163 | ||
164 | u |= kpf_copy_bit(k, KPF_SWAPCACHE, PG_swapcache); | |
165 | u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked); | |
166 | ||
17797549 WF |
167 | u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable); |
168 | u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked); | |
17797549 | 169 | |
253fb02d WF |
170 | #ifdef CONFIG_MEMORY_FAILURE |
171 | u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison); | |
172 | #endif | |
173 | ||
ed430fec | 174 | #ifdef CONFIG_ARCH_USES_PG_UNCACHED |
17797549 WF |
175 | u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached); |
176 | #endif | |
177 | ||
178 | u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved); | |
179 | u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk); | |
180 | u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private); | |
181 | u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2); | |
182 | u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1); | |
183 | u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1); | |
184 | ||
185 | return u; | |
186 | }; | |
6d80e53f AD |
187 | |
188 | static ssize_t kpageflags_read(struct file *file, char __user *buf, | |
189 | size_t count, loff_t *ppos) | |
190 | { | |
191 | u64 __user *out = (u64 __user *)buf; | |
192 | struct page *ppage; | |
193 | unsigned long src = *ppos; | |
194 | unsigned long pfn; | |
195 | ssize_t ret = 0; | |
6d80e53f AD |
196 | |
197 | pfn = src / KPMSIZE; | |
198 | count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src); | |
199 | if (src & KPMMASK || count & KPMMASK) | |
200 | return -EINVAL; | |
201 | ||
202 | while (count > 0) { | |
6d80e53f AD |
203 | if (pfn_valid(pfn)) |
204 | ppage = pfn_to_page(pfn); | |
ed7ce0f1 WF |
205 | else |
206 | ppage = NULL; | |
17797549 | 207 | |
1a9b5b7f | 208 | if (put_user(stable_page_flags(ppage), out)) { |
6d80e53f AD |
209 | ret = -EFAULT; |
210 | break; | |
211 | } | |
212 | ||
ed7ce0f1 WF |
213 | pfn++; |
214 | out++; | |
6d80e53f AD |
215 | count -= KPMSIZE; |
216 | } | |
217 | ||
218 | *ppos += (char __user *)out - buf; | |
219 | if (!ret) | |
220 | ret = (char __user *)out - buf; | |
221 | return ret; | |
222 | } | |
223 | ||
224 | static const struct file_operations proc_kpageflags_operations = { | |
225 | .llseek = mem_lseek, | |
226 | .read = kpageflags_read, | |
227 | }; | |
228 | ||
80ae2fdc VD |
229 | #ifdef CONFIG_MEMCG |
230 | static ssize_t kpagecgroup_read(struct file *file, char __user *buf, | |
231 | size_t count, loff_t *ppos) | |
232 | { | |
233 | u64 __user *out = (u64 __user *)buf; | |
234 | struct page *ppage; | |
235 | unsigned long src = *ppos; | |
236 | unsigned long pfn; | |
237 | ssize_t ret = 0; | |
238 | u64 ino; | |
239 | ||
240 | pfn = src / KPMSIZE; | |
241 | count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src); | |
242 | if (src & KPMMASK || count & KPMMASK) | |
243 | return -EINVAL; | |
244 | ||
245 | while (count > 0) { | |
246 | if (pfn_valid(pfn)) | |
247 | ppage = pfn_to_page(pfn); | |
248 | else | |
249 | ppage = NULL; | |
250 | ||
251 | if (ppage) | |
252 | ino = page_cgroup_ino(ppage); | |
253 | else | |
254 | ino = 0; | |
255 | ||
256 | if (put_user(ino, out)) { | |
257 | ret = -EFAULT; | |
258 | break; | |
259 | } | |
260 | ||
261 | pfn++; | |
262 | out++; | |
263 | count -= KPMSIZE; | |
264 | } | |
265 | ||
266 | *ppos += (char __user *)out - buf; | |
267 | if (!ret) | |
268 | ret = (char __user *)out - buf; | |
269 | return ret; | |
270 | } | |
271 | ||
272 | static const struct file_operations proc_kpagecgroup_operations = { | |
273 | .llseek = mem_lseek, | |
274 | .read = kpagecgroup_read, | |
275 | }; | |
276 | #endif /* CONFIG_MEMCG */ | |
277 | ||
6d80e53f AD |
278 | static int __init proc_page_init(void) |
279 | { | |
280 | proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations); | |
281 | proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations); | |
80ae2fdc VD |
282 | #ifdef CONFIG_MEMCG |
283 | proc_create("kpagecgroup", S_IRUSR, NULL, &proc_kpagecgroup_operations); | |
284 | #endif | |
6d80e53f AD |
285 | return 0; |
286 | } | |
abaf3787 | 287 | fs_initcall(proc_page_init); |