blob: 70d23245dd43942d6c3e7691f5f07ba8aa7deaaf [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>
Wang, Yalin56873f42015-02-11 15:24:51 -08008#include <linux/huge_mm.h>
Alexey Dobriyan6d80e532008-10-06 14:26:12 +04009#include <linux/proc_fs.h>
10#include <linux/seq_file.h>
Wu Fengguang20a03072009-06-16 15:32:22 -070011#include <linux/hugetlb.h>
Vladimir Davydov80ae2fd2015-09-09 15:35:38 -070012#include <linux/memcontrol.h>
Wu Fengguang1a9b5b72009-12-16 12:19:59 +010013#include <linux/kernel-page-flags.h>
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040014#include <asm/uaccess.h>
15#include "internal.h"
16
17#define KPMSIZE sizeof(u64)
18#define KPMMASK (KPMSIZE - 1)
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070019
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040020/* /proc/kpagecount - an array exposing page counts
21 *
22 * Each entry is a u64 representing the corresponding
23 * physical page count.
24 */
25static 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) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040041 if (pfn_valid(pfn))
42 ppage = pfn_to_page(pfn);
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070043 else
44 ppage = NULL;
Petr Holaseka6fc86d2011-01-12 17:00:34 -080045 if (!ppage || PageSlab(ppage))
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040046 pcount = 0;
47 else
48 pcount = page_mapcount(ppage);
49
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070050 if (put_user(pcount, out)) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040051 ret = -EFAULT;
52 break;
53 }
54
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070055 pfn++;
56 out++;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040057 count -= KPMSIZE;
58 }
59
60 *ppos += (char __user *)out - buf;
61 if (!ret)
62 ret = (char __user *)out - buf;
63 return ret;
64}
65
66static 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
Wu Fengguang17797542009-06-16 15:32:24 -070077static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
78{
79 return ((kflags >> kbit) & 1) << ubit;
80}
81
Wu Fengguang1a9b5b72009-12-16 12:19:59 +010082u64 stable_page_flags(struct page *page)
Wu Fengguang17797542009-06-16 15:32:24 -070083{
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;
Hugh Dickins9a840892009-09-21 17:02:01 -0700107 if (PageKsm(page))
108 u |= 1 << KPF_KSM;
Wu Fengguang17797542009-06-16 15:32:24 -0700109
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;
Naoya Horiguchi7a719322012-10-08 16:33:47 -0700120 /*
121 * PageTransCompound can be true for non-huge compound pages (slab
122 * pages or pages allocated by drivers with __GFP_COMP) because it
Naoya Horiguchie3bba3c2014-01-23 15:52:53 -0800123 * 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.
Naoya Horiguchi7a719322012-10-08 16:33:47 -0700125 */
Wang, Yalin56873f42015-02-11 15:24:51 -0800126 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
Wu Fengguang17797542009-06-16 15:32:24 -0700138
Andrea Arcangeli5f24ce52011-01-13 15:47:00 -0800139 /*
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.
143 */
144 if (PageBuddy(page))
145 u |= 1 << KPF_BUDDY;
146
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -0700147 if (PageBalloon(page))
148 u |= 1 << KPF_BALLOON;
149
Wu Fengguang17797542009-06-16 15:32:24 -0700150 u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
151
Wu Fengguang17797542009-06-16 15:32:24 -0700152 u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
Wu Fengguang17797542009-06-16 15:32:24 -0700153
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
Wu Fengguang17797542009-06-16 15:32:24 -0700167 u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
168 u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
Wu Fengguang17797542009-06-16 15:32:24 -0700169
Wu Fengguang253fb022009-10-07 16:32:27 -0700170#ifdef CONFIG_MEMORY_FAILURE
171 u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
172#endif
173
Takashi Iwaied430fe2010-09-09 16:37:36 -0700174#ifdef CONFIG_ARCH_USES_PG_UNCACHED
Wu Fengguang17797542009-06-16 15:32:24 -0700175 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};
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400187
188static 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;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400196
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) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400203 if (pfn_valid(pfn))
204 ppage = pfn_to_page(pfn);
Wu Fengguanged7ce0f2009-06-16 15:32:23 -0700205 else
206 ppage = NULL;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400207
Wu Fengguang1a9b5b72009-12-16 12:19:59 +0100208 if (put_user(stable_page_flags(ppage), out)) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400209 ret = -EFAULT;
210 break;
211 }
212
Wu Fengguanged7ce0f2009-06-16 15:32:23 -0700213 pfn++;
214 out++;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400215 count -= KPMSIZE;
216 }
217
218 *ppos += (char __user *)out - buf;
219 if (!ret)
220 ret = (char __user *)out - buf;
221 return ret;
222}
223
224static const struct file_operations proc_kpageflags_operations = {
225 .llseek = mem_lseek,
226 .read = kpageflags_read,
227};
228
Vladimir Davydov80ae2fd2015-09-09 15:35:38 -0700229#ifdef CONFIG_MEMCG
230static 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
272static const struct file_operations proc_kpagecgroup_operations = {
273 .llseek = mem_lseek,
274 .read = kpagecgroup_read,
275};
276#endif /* CONFIG_MEMCG */
277
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400278static 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);
Vladimir Davydov80ae2fd2015-09-09 15:35:38 -0700282#ifdef CONFIG_MEMCG
283 proc_create("kpagecgroup", S_IRUSR, NULL, &proc_kpagecgroup_operations);
284#endif
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400285 return 0;
286}
Paul Gortmakerabaf3782014-01-23 15:55:45 -0800287fs_initcall(proc_page_init);