0001
0002 #include <linux/memblock.h>
0003 #include <linux/compiler.h>
0004 #include <linux/fs.h>
0005 #include <linux/init.h>
0006 #include <linux/ksm.h>
0007 #include <linux/mm.h>
0008 #include <linux/mmzone.h>
0009 #include <linux/huge_mm.h>
0010 #include <linux/proc_fs.h>
0011 #include <linux/seq_file.h>
0012 #include <linux/hugetlb.h>
0013 #include <linux/memremap.h>
0014 #include <linux/memcontrol.h>
0015 #include <linux/mmu_notifier.h>
0016 #include <linux/page_idle.h>
0017 #include <linux/kernel-page-flags.h>
0018 #include <linux/uaccess.h>
0019 #include "internal.h"
0020
0021 #define KPMSIZE sizeof(u64)
0022 #define KPMMASK (KPMSIZE - 1)
0023 #define KPMBITS (KPMSIZE * BITS_PER_BYTE)
0024
0025 static inline unsigned long get_max_dump_pfn(void)
0026 {
0027 #ifdef CONFIG_SPARSEMEM
0028
0029
0030
0031
0032
0033
0034 return round_up(max_pfn, PAGES_PER_SECTION);
0035 #else
0036 return max_pfn;
0037 #endif
0038 }
0039
0040
0041
0042
0043
0044
0045 static ssize_t kpagecount_read(struct file *file, char __user *buf,
0046 size_t count, loff_t *ppos)
0047 {
0048 const unsigned long max_dump_pfn = get_max_dump_pfn();
0049 u64 __user *out = (u64 __user *)buf;
0050 struct page *ppage;
0051 unsigned long src = *ppos;
0052 unsigned long pfn;
0053 ssize_t ret = 0;
0054 u64 pcount;
0055
0056 pfn = src / KPMSIZE;
0057 if (src & KPMMASK || count & KPMMASK)
0058 return -EINVAL;
0059 if (src >= max_dump_pfn * KPMSIZE)
0060 return 0;
0061 count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);
0062
0063 while (count > 0) {
0064
0065
0066
0067
0068 ppage = pfn_to_online_page(pfn);
0069
0070 if (!ppage || PageSlab(ppage) || page_has_type(ppage))
0071 pcount = 0;
0072 else
0073 pcount = page_mapcount(ppage);
0074
0075 if (put_user(pcount, out)) {
0076 ret = -EFAULT;
0077 break;
0078 }
0079
0080 pfn++;
0081 out++;
0082 count -= KPMSIZE;
0083
0084 cond_resched();
0085 }
0086
0087 *ppos += (char __user *)out - buf;
0088 if (!ret)
0089 ret = (char __user *)out - buf;
0090 return ret;
0091 }
0092
0093 static const struct proc_ops kpagecount_proc_ops = {
0094 .proc_lseek = mem_lseek,
0095 .proc_read = kpagecount_read,
0096 };
0097
0098
0099
0100
0101
0102
0103
0104 static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
0105 {
0106 return ((kflags >> kbit) & 1) << ubit;
0107 }
0108
0109 u64 stable_page_flags(struct page *page)
0110 {
0111 u64 k;
0112 u64 u;
0113
0114
0115
0116
0117
0118 if (!page)
0119 return 1 << KPF_NOPAGE;
0120
0121 k = page->flags;
0122 u = 0;
0123
0124
0125
0126
0127
0128
0129
0130 if (!PageSlab(page) && page_mapped(page))
0131 u |= 1 << KPF_MMAP;
0132 if (PageAnon(page))
0133 u |= 1 << KPF_ANON;
0134 if (PageKsm(page))
0135 u |= 1 << KPF_KSM;
0136
0137
0138
0139
0140
0141 if (PageHead(page))
0142 u |= 1 << KPF_COMPOUND_HEAD;
0143 if (PageTail(page))
0144 u |= 1 << KPF_COMPOUND_TAIL;
0145 if (PageHuge(page))
0146 u |= 1 << KPF_HUGE;
0147
0148
0149
0150
0151
0152
0153 else if (PageTransCompound(page)) {
0154 struct page *head = compound_head(page);
0155
0156 if (PageLRU(head) || PageAnon(head))
0157 u |= 1 << KPF_THP;
0158 else if (is_huge_zero_page(head)) {
0159 u |= 1 << KPF_ZERO_PAGE;
0160 u |= 1 << KPF_THP;
0161 }
0162 } else if (is_zero_pfn(page_to_pfn(page)))
0163 u |= 1 << KPF_ZERO_PAGE;
0164
0165
0166
0167
0168
0169
0170
0171 if (PageBuddy(page))
0172 u |= 1 << KPF_BUDDY;
0173 else if (page_count(page) == 0 && is_free_buddy_page(page))
0174 u |= 1 << KPF_BUDDY;
0175
0176 if (PageOffline(page))
0177 u |= 1 << KPF_OFFLINE;
0178 if (PageTable(page))
0179 u |= 1 << KPF_PGTABLE;
0180
0181 if (page_is_idle(page))
0182 u |= 1 << KPF_IDLE;
0183
0184 u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
0185
0186 u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
0187 if (PageTail(page) && PageSlab(compound_head(page)))
0188 u |= 1 << KPF_SLAB;
0189
0190 u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
0191 u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
0192 u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
0193 u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
0194
0195 u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
0196 u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
0197 u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
0198 u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
0199
0200 if (PageSwapCache(page))
0201 u |= 1 << KPF_SWAPCACHE;
0202 u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
0203
0204 u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
0205 u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
0206
0207 #ifdef CONFIG_MEMORY_FAILURE
0208 u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
0209 #endif
0210
0211 #ifdef CONFIG_ARCH_USES_PG_UNCACHED
0212 u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
0213 #endif
0214
0215 u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
0216 u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
0217 u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
0218 u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
0219 u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
0220 u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
0221 #ifdef CONFIG_64BIT
0222 u |= kpf_copy_bit(k, KPF_ARCH_2, PG_arch_2);
0223 #endif
0224
0225 return u;
0226 };
0227
0228 static ssize_t kpageflags_read(struct file *file, char __user *buf,
0229 size_t count, loff_t *ppos)
0230 {
0231 const unsigned long max_dump_pfn = get_max_dump_pfn();
0232 u64 __user *out = (u64 __user *)buf;
0233 struct page *ppage;
0234 unsigned long src = *ppos;
0235 unsigned long pfn;
0236 ssize_t ret = 0;
0237
0238 pfn = src / KPMSIZE;
0239 if (src & KPMMASK || count & KPMMASK)
0240 return -EINVAL;
0241 if (src >= max_dump_pfn * KPMSIZE)
0242 return 0;
0243 count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);
0244
0245 while (count > 0) {
0246
0247
0248
0249
0250 ppage = pfn_to_online_page(pfn);
0251
0252 if (put_user(stable_page_flags(ppage), out)) {
0253 ret = -EFAULT;
0254 break;
0255 }
0256
0257 pfn++;
0258 out++;
0259 count -= KPMSIZE;
0260
0261 cond_resched();
0262 }
0263
0264 *ppos += (char __user *)out - buf;
0265 if (!ret)
0266 ret = (char __user *)out - buf;
0267 return ret;
0268 }
0269
0270 static const struct proc_ops kpageflags_proc_ops = {
0271 .proc_lseek = mem_lseek,
0272 .proc_read = kpageflags_read,
0273 };
0274
0275 #ifdef CONFIG_MEMCG
0276 static ssize_t kpagecgroup_read(struct file *file, char __user *buf,
0277 size_t count, loff_t *ppos)
0278 {
0279 const unsigned long max_dump_pfn = get_max_dump_pfn();
0280 u64 __user *out = (u64 __user *)buf;
0281 struct page *ppage;
0282 unsigned long src = *ppos;
0283 unsigned long pfn;
0284 ssize_t ret = 0;
0285 u64 ino;
0286
0287 pfn = src / KPMSIZE;
0288 if (src & KPMMASK || count & KPMMASK)
0289 return -EINVAL;
0290 if (src >= max_dump_pfn * KPMSIZE)
0291 return 0;
0292 count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);
0293
0294 while (count > 0) {
0295
0296
0297
0298
0299 ppage = pfn_to_online_page(pfn);
0300
0301 if (ppage)
0302 ino = page_cgroup_ino(ppage);
0303 else
0304 ino = 0;
0305
0306 if (put_user(ino, out)) {
0307 ret = -EFAULT;
0308 break;
0309 }
0310
0311 pfn++;
0312 out++;
0313 count -= KPMSIZE;
0314
0315 cond_resched();
0316 }
0317
0318 *ppos += (char __user *)out - buf;
0319 if (!ret)
0320 ret = (char __user *)out - buf;
0321 return ret;
0322 }
0323
0324 static const struct proc_ops kpagecgroup_proc_ops = {
0325 .proc_lseek = mem_lseek,
0326 .proc_read = kpagecgroup_read,
0327 };
0328 #endif
0329
0330 static int __init proc_page_init(void)
0331 {
0332 proc_create("kpagecount", S_IRUSR, NULL, &kpagecount_proc_ops);
0333 proc_create("kpageflags", S_IRUSR, NULL, &kpageflags_proc_ops);
0334 #ifdef CONFIG_MEMCG
0335 proc_create("kpagecgroup", S_IRUSR, NULL, &kpagecgroup_proc_ops);
0336 #endif
0337 return 0;
0338 }
0339 fs_initcall(proc_page_init);