Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 1 | #include <linux/bootmem.h> |
| 2 | #include <linux/compiler.h> |
| 3 | #include <linux/fs.h> |
| 4 | #include <linux/init.h> |
Hugh Dickins | 9a84089 | 2009-09-21 17:02:01 -0700 | [diff] [blame] | 5 | #include <linux/ksm.h> |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 6 | #include <linux/mm.h> |
| 7 | #include <linux/mmzone.h> |
Wang, Yalin | 56873f4 | 2015-02-11 15:24:51 -0800 | [diff] [blame] | 8 | #include <linux/huge_mm.h> |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 9 | #include <linux/proc_fs.h> |
| 10 | #include <linux/seq_file.h> |
Wu Fengguang | 20a0307 | 2009-06-16 15:32:22 -0700 | [diff] [blame] | 11 | #include <linux/hugetlb.h> |
Vladimir Davydov | 80ae2fd | 2015-09-09 15:35:38 -0700 | [diff] [blame] | 12 | #include <linux/memcontrol.h> |
Wu Fengguang | 1a9b5b7 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 13 | #include <linux/kernel-page-flags.h> |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 14 | #include <asm/uaccess.h> |
| 15 | #include "internal.h" |
| 16 | |
| 17 | #define KPMSIZE sizeof(u64) |
| 18 | #define KPMMASK (KPMSIZE - 1) |
Wu Fengguang | ed7ce0f | 2009-06-16 15:32:23 -0700 | [diff] [blame] | 19 | |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 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) { |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 41 | if (pfn_valid(pfn)) |
| 42 | ppage = pfn_to_page(pfn); |
Wu Fengguang | ed7ce0f | 2009-06-16 15:32:23 -0700 | [diff] [blame] | 43 | else |
| 44 | ppage = NULL; |
Petr Holasek | a6fc86d | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 45 | if (!ppage || PageSlab(ppage)) |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 46 | pcount = 0; |
| 47 | else |
| 48 | pcount = page_mapcount(ppage); |
| 49 | |
Wu Fengguang | ed7ce0f | 2009-06-16 15:32:23 -0700 | [diff] [blame] | 50 | if (put_user(pcount, out)) { |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 51 | ret = -EFAULT; |
| 52 | break; |
| 53 | } |
| 54 | |
Wu Fengguang | ed7ce0f | 2009-06-16 15:32:23 -0700 | [diff] [blame] | 55 | pfn++; |
| 56 | out++; |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 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 | |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 77 | static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit) |
| 78 | { |
| 79 | return ((kflags >> kbit) & 1) << ubit; |
| 80 | } |
| 81 | |
Wu Fengguang | 1a9b5b7 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 82 | u64 stable_page_flags(struct page *page) |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 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; |
Hugh Dickins | 9a84089 | 2009-09-21 17:02:01 -0700 | [diff] [blame] | 107 | if (PageKsm(page)) |
| 108 | u |= 1 << KPF_KSM; |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 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; |
Naoya Horiguchi | 7a71932 | 2012-10-08 16:33:47 -0700 | [diff] [blame] | 120 | /* |
| 121 | * PageTransCompound can be true for non-huge compound pages (slab |
| 122 | * pages or pages allocated by drivers with __GFP_COMP) because it |
Naoya Horiguchi | e3bba3c | 2014-01-23 15:52:53 -0800 | [diff] [blame] | 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. |
Naoya Horiguchi | 7a71932 | 2012-10-08 16:33:47 -0700 | [diff] [blame] | 125 | */ |
Wang, Yalin | 56873f4 | 2015-02-11 15:24:51 -0800 | [diff] [blame] | 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 | |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 138 | |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 139 | /* |
| 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 Khlebnikov | 09316c0 | 2014-10-09 15:29:32 -0700 | [diff] [blame] | 147 | if (PageBalloon(page)) |
| 148 | u |= 1 << KPF_BALLOON; |
| 149 | |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 150 | u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked); |
| 151 | |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 152 | u |= kpf_copy_bit(k, KPF_SLAB, PG_slab); |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 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 | |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 167 | u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable); |
| 168 | u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked); |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 169 | |
Wu Fengguang | 253fb02 | 2009-10-07 16:32:27 -0700 | [diff] [blame] | 170 | #ifdef CONFIG_MEMORY_FAILURE |
| 171 | u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison); |
| 172 | #endif |
| 173 | |
Takashi Iwai | ed430fe | 2010-09-09 16:37:36 -0700 | [diff] [blame] | 174 | #ifdef CONFIG_ARCH_USES_PG_UNCACHED |
Wu Fengguang | 1779754 | 2009-06-16 15:32:24 -0700 | [diff] [blame] | 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 | }; |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 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; |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 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) { |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 203 | if (pfn_valid(pfn)) |
| 204 | ppage = pfn_to_page(pfn); |
Wu Fengguang | ed7ce0f | 2009-06-16 15:32:23 -0700 | [diff] [blame] | 205 | else |
| 206 | ppage = NULL; |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 207 | |
Wu Fengguang | 1a9b5b7 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 208 | if (put_user(stable_page_flags(ppage), out)) { |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 209 | ret = -EFAULT; |
| 210 | break; |
| 211 | } |
| 212 | |
Wu Fengguang | ed7ce0f | 2009-06-16 15:32:23 -0700 | [diff] [blame] | 213 | pfn++; |
| 214 | out++; |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 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 | |
Vladimir Davydov | 80ae2fd | 2015-09-09 15:35:38 -0700 | [diff] [blame] | 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 | |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 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); |
Vladimir Davydov | 80ae2fd | 2015-09-09 15:35:38 -0700 | [diff] [blame] | 282 | #ifdef CONFIG_MEMCG |
| 283 | proc_create("kpagecgroup", S_IRUSR, NULL, &proc_kpagecgroup_operations); |
| 284 | #endif |
Alexey Dobriyan | 6d80e53 | 2008-10-06 14:26:12 +0400 | [diff] [blame] | 285 | return 0; |
| 286 | } |
Paul Gortmaker | abaf378 | 2014-01-23 15:55:45 -0800 | [diff] [blame] | 287 | fs_initcall(proc_page_init); |