1 // SPDX-License-Identifier: GPL-2.0 !! 1 #include <linux/bootmem.h> 2 #include <linux/memblock.h> << 3 #include <linux/compiler.h> 2 #include <linux/compiler.h> 4 #include <linux/fs.h> 3 #include <linux/fs.h> 5 #include <linux/init.h> 4 #include <linux/init.h> 6 #include <linux/ksm.h> 5 #include <linux/ksm.h> 7 #include <linux/mm.h> 6 #include <linux/mm.h> 8 #include <linux/mmzone.h> 7 #include <linux/mmzone.h> 9 #include <linux/huge_mm.h> 8 #include <linux/huge_mm.h> 10 #include <linux/proc_fs.h> 9 #include <linux/proc_fs.h> 11 #include <linux/seq_file.h> 10 #include <linux/seq_file.h> 12 #include <linux/hugetlb.h> 11 #include <linux/hugetlb.h> 13 #include <linux/memremap.h> << 14 #include <linux/memcontrol.h> 12 #include <linux/memcontrol.h> 15 #include <linux/mmu_notifier.h> 13 #include <linux/mmu_notifier.h> 16 #include <linux/page_idle.h> 14 #include <linux/page_idle.h> 17 #include <linux/kernel-page-flags.h> 15 #include <linux/kernel-page-flags.h> 18 #include <linux/uaccess.h> 16 #include <linux/uaccess.h> 19 #include "internal.h" 17 #include "internal.h" 20 18 21 #define KPMSIZE sizeof(u64) 19 #define KPMSIZE sizeof(u64) 22 #define KPMMASK (KPMSIZE - 1) 20 #define KPMMASK (KPMSIZE - 1) 23 #define KPMBITS (KPMSIZE * BITS_PER_BYTE) 21 #define KPMBITS (KPMSIZE * BITS_PER_BYTE) 24 22 25 static inline unsigned long get_max_dump_pfn(v !! 23 /* /proc/kpagecount - an array exposing page counts 26 { << 27 #ifdef CONFIG_SPARSEMEM << 28 /* << 29 * The memmap of early sections is com << 30 * online even if max_pfn does not fal << 31 * pfn_to_online_page() will succeed o << 32 * these memmaps. << 33 */ << 34 return round_up(max_pfn, PAGES_PER_SEC << 35 #else << 36 return max_pfn; << 37 #endif << 38 } << 39 << 40 /* /proc/kpagecount - an array exposing page m << 41 * 24 * 42 * Each entry is a u64 representing the corres 25 * Each entry is a u64 representing the corresponding 43 * physical page mapcount. !! 26 * physical page count. 44 */ 27 */ 45 static ssize_t kpagecount_read(struct file *fi 28 static ssize_t kpagecount_read(struct file *file, char __user *buf, 46 size_t count, lof 29 size_t count, loff_t *ppos) 47 { 30 { 48 const unsigned long max_dump_pfn = get << 49 u64 __user *out = (u64 __user *)buf; 31 u64 __user *out = (u64 __user *)buf; >> 32 struct page *ppage; 50 unsigned long src = *ppos; 33 unsigned long src = *ppos; 51 unsigned long pfn; 34 unsigned long pfn; 52 ssize_t ret = 0; 35 ssize_t ret = 0; >> 36 u64 pcount; 53 37 54 pfn = src / KPMSIZE; 38 pfn = src / KPMSIZE; >> 39 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src); 55 if (src & KPMMASK || count & KPMMASK) 40 if (src & KPMMASK || count & KPMMASK) 56 return -EINVAL; 41 return -EINVAL; 57 if (src >= max_dump_pfn * KPMSIZE) << 58 return 0; << 59 count = min_t(unsigned long, count, (m << 60 42 61 while (count > 0) { 43 while (count > 0) { 62 struct page *page; !! 44 if (pfn_valid(pfn)) 63 u64 mapcount = 0; !! 45 ppage = pfn_to_page(pfn); 64 !! 46 else 65 /* !! 47 ppage = NULL; 66 * TODO: ZONE_DEVICE support r !! 48 if (!ppage || PageSlab(ppage)) 67 * memmaps that were actually !! 49 pcount = 0; 68 */ !! 50 else 69 page = pfn_to_online_page(pfn) !! 51 pcount = page_mapcount(ppage); 70 if (page) << 71 mapcount = folio_preci << 72 << 73 52 74 if (put_user(mapcount, out)) { !! 53 if (put_user(pcount, out)) { 75 ret = -EFAULT; 54 ret = -EFAULT; 76 break; 55 break; 77 } 56 } 78 57 79 pfn++; 58 pfn++; 80 out++; 59 out++; 81 count -= KPMSIZE; 60 count -= KPMSIZE; 82 61 83 cond_resched(); 62 cond_resched(); 84 } 63 } 85 64 86 *ppos += (char __user *)out - buf; 65 *ppos += (char __user *)out - buf; 87 if (!ret) 66 if (!ret) 88 ret = (char __user *)out - buf 67 ret = (char __user *)out - buf; 89 return ret; 68 return ret; 90 } 69 } 91 70 92 static const struct proc_ops kpagecount_proc_o !! 71 static const struct file_operations proc_kpagecount_operations = { 93 .proc_flags = PROC_ENTRY_PERMANENT !! 72 .llseek = mem_lseek, 94 .proc_lseek = mem_lseek, !! 73 .read = kpagecount_read, 95 .proc_read = kpagecount_read, << 96 }; 74 }; 97 75 98 /* /proc/kpageflags - an array exposing page f 76 /* /proc/kpageflags - an array exposing page flags 99 * 77 * 100 * Each entry is a u64 representing the corres 78 * Each entry is a u64 representing the corresponding 101 * physical page flags. 79 * physical page flags. 102 */ 80 */ 103 81 104 static inline u64 kpf_copy_bit(u64 kflags, int 82 static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit) 105 { 83 { 106 return ((kflags >> kbit) & 1) << ubit; 84 return ((kflags >> kbit) & 1) << ubit; 107 } 85 } 108 86 109 u64 stable_page_flags(const struct page *page) !! 87 u64 stable_page_flags(struct page *page) 110 { 88 { 111 const struct folio *folio; !! 89 u64 k; 112 unsigned long k; !! 90 u64 u; 113 unsigned long mapping; << 114 bool is_anon; << 115 u64 u = 0; << 116 91 117 /* 92 /* 118 * pseudo flag: KPF_NOPAGE 93 * pseudo flag: KPF_NOPAGE 119 * it differentiates a memory hole fro 94 * it differentiates a memory hole from a page with no flags 120 */ 95 */ 121 if (!page) 96 if (!page) 122 return 1 << KPF_NOPAGE; 97 return 1 << KPF_NOPAGE; 123 folio = page_folio(page); << 124 98 125 k = folio->flags; !! 99 k = page->flags; 126 mapping = (unsigned long)folio->mappin !! 100 u = 0; 127 is_anon = mapping & PAGE_MAPPING_ANON; << 128 101 129 /* 102 /* 130 * pseudo flags for the well known (an 103 * pseudo flags for the well known (anonymous) memory mapped pages >> 104 * >> 105 * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the >> 106 * simple test in page_mapped() is not enough. 131 */ 107 */ 132 if (page_mapped(page)) !! 108 if (!PageSlab(page) && page_mapped(page)) 133 u |= 1 << KPF_MMAP; 109 u |= 1 << KPF_MMAP; 134 if (is_anon) { !! 110 if (PageAnon(page)) 135 u |= 1 << KPF_ANON; 111 u |= 1 << KPF_ANON; 136 if (mapping & PAGE_MAPPING_KSM !! 112 if (PageKsm(page)) 137 u |= 1 << KPF_KSM; !! 113 u |= 1 << KPF_KSM; 138 } << 139 114 140 /* 115 /* 141 * compound pages: export both head/ta 116 * compound pages: export both head/tail info 142 * they together define a compound pag 117 * they together define a compound page's start/end pos and order 143 */ 118 */ 144 if (page == &folio->page) !! 119 if (PageHead(page)) 145 u |= kpf_copy_bit(k, KPF_COMPO !! 120 u |= 1 << KPF_COMPOUND_HEAD; 146 else !! 121 if (PageTail(page)) 147 u |= 1 << KPF_COMPOUND_TAIL; 122 u |= 1 << KPF_COMPOUND_TAIL; 148 if (folio_test_hugetlb(folio)) !! 123 if (PageHuge(page)) 149 u |= 1 << KPF_HUGE; 124 u |= 1 << KPF_HUGE; 150 else if (folio_test_large(folio) && !! 125 /* 151 folio_test_large_rmappable(fo !! 126 * PageTransCompound can be true for non-huge compound pages (slab 152 /* Note: we indicate any THPs !! 127 * pages or pages allocated by drivers with __GFP_COMP) because it 153 u |= 1 << KPF_THP; !! 128 * just checks PG_head/PG_tail, so we need to check PageLRU/PageAnon 154 } else if (is_huge_zero_folio(folio)) !! 129 * to make sure a given page is a thp, not a non-huge compound page. 155 u |= 1 << KPF_ZERO_PAGE; !! 130 */ 156 u |= 1 << KPF_THP; !! 131 else if (PageTransCompound(page)) { 157 } else if (is_zero_folio(folio)) { !! 132 struct page *head = compound_head(page); >> 133 >> 134 if (PageLRU(head) || PageAnon(head)) >> 135 u |= 1 << KPF_THP; >> 136 else if (is_huge_zero_page(head)) { >> 137 u |= 1 << KPF_ZERO_PAGE; >> 138 u |= 1 << KPF_THP; >> 139 } >> 140 } else if (is_zero_pfn(page_to_pfn(page))) 158 u |= 1 << KPF_ZERO_PAGE; 141 u |= 1 << KPF_ZERO_PAGE; 159 } !! 142 160 143 161 /* 144 /* 162 * Caveats on high order pages: PG_bud !! 145 * Caveats on high order pages: page->_refcount will only be set 163 * on the head page. !! 146 * -1 on the head page; SLUB/SLQB do the same for PG_slab; >> 147 * SLOB won't set PG_slab at all on compound pages. 164 */ 148 */ 165 if (PageBuddy(page)) 149 if (PageBuddy(page)) 166 u |= 1 << KPF_BUDDY; 150 u |= 1 << KPF_BUDDY; 167 else if (page_count(page) == 0 && is_f 151 else if (page_count(page) == 0 && is_free_buddy_page(page)) 168 u |= 1 << KPF_BUDDY; 152 u |= 1 << KPF_BUDDY; 169 153 170 if (PageOffline(page)) !! 154 if (PageBalloon(page)) 171 u |= 1 << KPF_OFFLINE; !! 155 u |= 1 << KPF_BALLOON; 172 if (PageTable(page)) << 173 u |= 1 << KPF_PGTABLE; << 174 if (folio_test_slab(folio)) << 175 u |= 1 << KPF_SLAB; << 176 156 177 #if defined(CONFIG_PAGE_IDLE_FLAG) && defined( !! 157 if (page_is_idle(page)) 178 u |= kpf_copy_bit(k, KPF_IDLE, << 179 #else << 180 if (folio_test_idle(folio)) << 181 u |= 1 << KPF_IDLE; 158 u |= 1 << KPF_IDLE; 182 #endif << 183 159 184 u |= kpf_copy_bit(k, KPF_LOCKED, 160 u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked); >> 161 >> 162 u |= kpf_copy_bit(k, KPF_SLAB, PG_slab); >> 163 if (PageTail(page) && PageSlab(compound_head(page))) >> 164 u |= 1 << KPF_SLAB; >> 165 >> 166 u |= kpf_copy_bit(k, KPF_ERROR, PG_error); 185 u |= kpf_copy_bit(k, KPF_DIRTY, 167 u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty); 186 u |= kpf_copy_bit(k, KPF_UPTODATE, 168 u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate); 187 u |= kpf_copy_bit(k, KPF_WRITEBACK, 169 u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback); 188 170 189 u |= kpf_copy_bit(k, KPF_LRU, 171 u |= kpf_copy_bit(k, KPF_LRU, PG_lru); 190 u |= kpf_copy_bit(k, KPF_REFERENCED, 172 u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced); 191 u |= kpf_copy_bit(k, KPF_ACTIVE, 173 u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active); 192 u |= kpf_copy_bit(k, KPF_RECLAIM, 174 u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim); 193 175 194 #define SWAPCACHE ((1 << PG_swapbacked) | (1 < !! 176 if (PageSwapCache(page)) 195 if ((k & SWAPCACHE) == SWAPCACHE) << 196 u |= 1 << KPF_SWAPCACHE; 177 u |= 1 << KPF_SWAPCACHE; 197 u |= kpf_copy_bit(k, KPF_SWAPBACKED, 178 u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked); 198 179 199 u |= kpf_copy_bit(k, KPF_UNEVICTABLE, 180 u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable); 200 u |= kpf_copy_bit(k, KPF_MLOCKED, 181 u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked); 201 182 202 #ifdef CONFIG_MEMORY_FAILURE 183 #ifdef CONFIG_MEMORY_FAILURE 203 if (u & (1 << KPF_HUGE)) !! 184 u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison); 204 u |= kpf_copy_bit(k, KPF_HWPOI !! 185 #endif 205 else !! 186 206 u |= kpf_copy_bit(page->flags, !! 187 #ifdef CONFIG_ARCH_USES_PG_UNCACHED >> 188 u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached); 207 #endif 189 #endif 208 190 209 u |= kpf_copy_bit(k, KPF_RESERVED, 191 u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved); 210 u |= kpf_copy_bit(k, KPF_OWNER_2, !! 192 u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk); 211 u |= kpf_copy_bit(k, KPF_PRIVATE, 193 u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private); 212 u |= kpf_copy_bit(k, KPF_PRIVATE_2, 194 u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2); 213 u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE 195 u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1); 214 u |= kpf_copy_bit(k, KPF_ARCH, 196 u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1); 215 #ifdef CONFIG_ARCH_USES_PG_ARCH_2 << 216 u |= kpf_copy_bit(k, KPF_ARCH_2, << 217 #endif << 218 #ifdef CONFIG_ARCH_USES_PG_ARCH_3 << 219 u |= kpf_copy_bit(k, KPF_ARCH_3, << 220 #endif << 221 197 222 return u; 198 return u; 223 }; 199 }; 224 200 225 static ssize_t kpageflags_read(struct file *fi 201 static ssize_t kpageflags_read(struct file *file, char __user *buf, 226 size_t count, lof 202 size_t count, loff_t *ppos) 227 { 203 { 228 const unsigned long max_dump_pfn = get << 229 u64 __user *out = (u64 __user *)buf; 204 u64 __user *out = (u64 __user *)buf; >> 205 struct page *ppage; 230 unsigned long src = *ppos; 206 unsigned long src = *ppos; 231 unsigned long pfn; 207 unsigned long pfn; 232 ssize_t ret = 0; 208 ssize_t ret = 0; 233 209 234 pfn = src / KPMSIZE; 210 pfn = src / KPMSIZE; >> 211 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src); 235 if (src & KPMMASK || count & KPMMASK) 212 if (src & KPMMASK || count & KPMMASK) 236 return -EINVAL; 213 return -EINVAL; 237 if (src >= max_dump_pfn * KPMSIZE) << 238 return 0; << 239 count = min_t(unsigned long, count, (m << 240 214 241 while (count > 0) { 215 while (count > 0) { 242 /* !! 216 if (pfn_valid(pfn)) 243 * TODO: ZONE_DEVICE support r !! 217 ppage = pfn_to_page(pfn); 244 * memmaps that were actually !! 218 else 245 */ !! 219 ppage = NULL; 246 struct page *page = pfn_to_onl << 247 220 248 if (put_user(stable_page_flags !! 221 if (put_user(stable_page_flags(ppage), out)) { 249 ret = -EFAULT; 222 ret = -EFAULT; 250 break; 223 break; 251 } 224 } 252 225 253 pfn++; 226 pfn++; 254 out++; 227 out++; 255 count -= KPMSIZE; 228 count -= KPMSIZE; 256 229 257 cond_resched(); 230 cond_resched(); 258 } 231 } 259 232 260 *ppos += (char __user *)out - buf; 233 *ppos += (char __user *)out - buf; 261 if (!ret) 234 if (!ret) 262 ret = (char __user *)out - buf 235 ret = (char __user *)out - buf; 263 return ret; 236 return ret; 264 } 237 } 265 238 266 static const struct proc_ops kpageflags_proc_o !! 239 static const struct file_operations proc_kpageflags_operations = { 267 .proc_flags = PROC_ENTRY_PERMANENT !! 240 .llseek = mem_lseek, 268 .proc_lseek = mem_lseek, !! 241 .read = kpageflags_read, 269 .proc_read = kpageflags_read, << 270 }; 242 }; 271 243 272 #ifdef CONFIG_MEMCG 244 #ifdef CONFIG_MEMCG 273 static ssize_t kpagecgroup_read(struct file *f 245 static ssize_t kpagecgroup_read(struct file *file, char __user *buf, 274 size_t count, 246 size_t count, loff_t *ppos) 275 { 247 { 276 const unsigned long max_dump_pfn = get << 277 u64 __user *out = (u64 __user *)buf; 248 u64 __user *out = (u64 __user *)buf; 278 struct page *ppage; 249 struct page *ppage; 279 unsigned long src = *ppos; 250 unsigned long src = *ppos; 280 unsigned long pfn; 251 unsigned long pfn; 281 ssize_t ret = 0; 252 ssize_t ret = 0; 282 u64 ino; 253 u64 ino; 283 254 284 pfn = src / KPMSIZE; 255 pfn = src / KPMSIZE; >> 256 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src); 285 if (src & KPMMASK || count & KPMMASK) 257 if (src & KPMMASK || count & KPMMASK) 286 return -EINVAL; 258 return -EINVAL; 287 if (src >= max_dump_pfn * KPMSIZE) << 288 return 0; << 289 count = min_t(unsigned long, count, (m << 290 259 291 while (count > 0) { 260 while (count > 0) { 292 /* !! 261 if (pfn_valid(pfn)) 293 * TODO: ZONE_DEVICE support r !! 262 ppage = pfn_to_page(pfn); 294 * memmaps that were actually !! 263 else 295 */ !! 264 ppage = NULL; 296 ppage = pfn_to_online_page(pfn << 297 265 298 if (ppage) 266 if (ppage) 299 ino = page_cgroup_ino( 267 ino = page_cgroup_ino(ppage); 300 else 268 else 301 ino = 0; 269 ino = 0; 302 270 303 if (put_user(ino, out)) { 271 if (put_user(ino, out)) { 304 ret = -EFAULT; 272 ret = -EFAULT; 305 break; 273 break; 306 } 274 } 307 275 308 pfn++; 276 pfn++; 309 out++; 277 out++; 310 count -= KPMSIZE; 278 count -= KPMSIZE; 311 279 312 cond_resched(); 280 cond_resched(); 313 } 281 } 314 282 315 *ppos += (char __user *)out - buf; 283 *ppos += (char __user *)out - buf; 316 if (!ret) 284 if (!ret) 317 ret = (char __user *)out - buf 285 ret = (char __user *)out - buf; 318 return ret; 286 return ret; 319 } 287 } 320 288 321 static const struct proc_ops kpagecgroup_proc_ !! 289 static const struct file_operations proc_kpagecgroup_operations = { 322 .proc_flags = PROC_ENTRY_PERMANENT !! 290 .llseek = mem_lseek, 323 .proc_lseek = mem_lseek, !! 291 .read = kpagecgroup_read, 324 .proc_read = kpagecgroup_read, << 325 }; 292 }; 326 #endif /* CONFIG_MEMCG */ 293 #endif /* CONFIG_MEMCG */ 327 294 328 static int __init proc_page_init(void) 295 static int __init proc_page_init(void) 329 { 296 { 330 proc_create("kpagecount", S_IRUSR, NUL !! 297 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations); 331 proc_create("kpageflags", S_IRUSR, NUL !! 298 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations); 332 #ifdef CONFIG_MEMCG 299 #ifdef CONFIG_MEMCG 333 proc_create("kpagecgroup", S_IRUSR, NU !! 300 proc_create("kpagecgroup", S_IRUSR, NULL, &proc_kpagecgroup_operations); 334 #endif 301 #endif 335 return 0; 302 return 0; 336 } 303 } 337 fs_initcall(proc_page_init); 304 fs_initcall(proc_page_init); 338 305
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.