Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * page-types: Tool for querying page flags |
| 3 | * |
Wu Fengguang | 0e9052e | 2009-12-16 12:19:57 +0100 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the Free |
| 6 | * Software Foundation; version 2. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should find a copy of v2 of the GNU General Public License somewhere on |
| 14 | * your Linux system; if not, write to the Free Software Foundation, Inc., 59 |
| 15 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 16 | * |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 17 | * Copyright (C) 2009 Intel corporation |
Wu Fengguang | 0c57eff | 2009-10-07 16:32:28 -0700 | [diff] [blame] | 18 | * |
| 19 | * Authors: Wu Fengguang <fengguang.wu@intel.com> |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 22 | #define _LARGEFILE64_SOURCE |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdint.h> |
| 27 | #include <stdarg.h> |
| 28 | #include <string.h> |
| 29 | #include <getopt.h> |
| 30 | #include <limits.h> |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 31 | #include <assert.h> |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <sys/errno.h> |
| 34 | #include <sys/fcntl.h> |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 35 | #include <sys/mount.h> |
| 36 | #include <sys/statfs.h> |
| 37 | #include "../../include/linux/magic.h" |
Ulrich Drepper | 9295b7a | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 38 | #include "../../include/linux/kernel-page-flags.h" |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 39 | |
| 40 | |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 41 | #ifndef MAX_PATH |
| 42 | # define MAX_PATH 256 |
| 43 | #endif |
| 44 | |
| 45 | #ifndef STR |
| 46 | # define _STR(x) #x |
| 47 | # define STR(x) _STR(x) |
| 48 | #endif |
| 49 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 50 | /* |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 51 | * pagemap kernel ABI bits |
| 52 | */ |
| 53 | |
| 54 | #define PM_ENTRY_BYTES sizeof(uint64_t) |
| 55 | #define PM_STATUS_BITS 3 |
| 56 | #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS) |
| 57 | #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET) |
| 58 | #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK) |
| 59 | #define PM_PSHIFT_BITS 6 |
| 60 | #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS) |
| 61 | #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET) |
| 62 | #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK) |
| 63 | #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1) |
| 64 | #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK) |
| 65 | |
| 66 | #define PM_PRESENT PM_STATUS(4LL) |
| 67 | #define PM_SWAP PM_STATUS(2LL) |
| 68 | |
| 69 | |
| 70 | /* |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 71 | * kernel page flags |
| 72 | */ |
| 73 | |
| 74 | #define KPF_BYTES 8 |
| 75 | #define PROC_KPAGEFLAGS "/proc/kpageflags" |
| 76 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 77 | /* [32-] kernel hacking assistances */ |
| 78 | #define KPF_RESERVED 32 |
| 79 | #define KPF_MLOCKED 33 |
| 80 | #define KPF_MAPPEDTODISK 34 |
| 81 | #define KPF_PRIVATE 35 |
| 82 | #define KPF_PRIVATE_2 36 |
| 83 | #define KPF_OWNER_PRIVATE 37 |
| 84 | #define KPF_ARCH 38 |
| 85 | #define KPF_UNCACHED 39 |
| 86 | |
| 87 | /* [48-] take some arbitrary free slots for expanding overloaded flags |
| 88 | * not part of kernel API |
| 89 | */ |
| 90 | #define KPF_READAHEAD 48 |
| 91 | #define KPF_SLOB_FREE 49 |
| 92 | #define KPF_SLUB_FROZEN 50 |
| 93 | #define KPF_SLUB_DEBUG 51 |
| 94 | |
| 95 | #define KPF_ALL_BITS ((uint64_t)~0ULL) |
| 96 | #define KPF_HACKERS_BITS (0xffffULL << 32) |
| 97 | #define KPF_OVERLOADED_BITS (0xffffULL << 48) |
| 98 | #define BIT(name) (1ULL << KPF_##name) |
| 99 | #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL)) |
| 100 | |
Dave Young | c6dd897 | 2012-03-28 14:42:55 -0700 | [diff] [blame] | 101 | static const char * const page_flag_names[] = { |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 102 | [KPF_LOCKED] = "L:locked", |
| 103 | [KPF_ERROR] = "E:error", |
| 104 | [KPF_REFERENCED] = "R:referenced", |
| 105 | [KPF_UPTODATE] = "U:uptodate", |
| 106 | [KPF_DIRTY] = "D:dirty", |
| 107 | [KPF_LRU] = "l:lru", |
| 108 | [KPF_ACTIVE] = "A:active", |
| 109 | [KPF_SLAB] = "S:slab", |
| 110 | [KPF_WRITEBACK] = "W:writeback", |
| 111 | [KPF_RECLAIM] = "I:reclaim", |
| 112 | [KPF_BUDDY] = "B:buddy", |
| 113 | |
| 114 | [KPF_MMAP] = "M:mmap", |
| 115 | [KPF_ANON] = "a:anonymous", |
| 116 | [KPF_SWAPCACHE] = "s:swapcache", |
| 117 | [KPF_SWAPBACKED] = "b:swapbacked", |
| 118 | [KPF_COMPOUND_HEAD] = "H:compound_head", |
| 119 | [KPF_COMPOUND_TAIL] = "T:compound_tail", |
| 120 | [KPF_HUGE] = "G:huge", |
| 121 | [KPF_UNEVICTABLE] = "u:unevictable", |
Wu Fengguang | 253fb02 | 2009-10-07 16:32:27 -0700 | [diff] [blame] | 122 | [KPF_HWPOISON] = "X:hwpoison", |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 123 | [KPF_NOPAGE] = "n:nopage", |
Wu Fengguang | a1bbb5e | 2009-10-07 16:32:28 -0700 | [diff] [blame] | 124 | [KPF_KSM] = "x:ksm", |
Naoya Horiguchi | 807f0cc | 2012-03-21 16:33:58 -0700 | [diff] [blame] | 125 | [KPF_THP] = "t:thp", |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 126 | |
| 127 | [KPF_RESERVED] = "r:reserved", |
| 128 | [KPF_MLOCKED] = "m:mlocked", |
| 129 | [KPF_MAPPEDTODISK] = "d:mappedtodisk", |
| 130 | [KPF_PRIVATE] = "P:private", |
| 131 | [KPF_PRIVATE_2] = "p:private_2", |
| 132 | [KPF_OWNER_PRIVATE] = "O:owner_private", |
| 133 | [KPF_ARCH] = "h:arch", |
| 134 | [KPF_UNCACHED] = "c:uncached", |
| 135 | |
| 136 | [KPF_READAHEAD] = "I:readahead", |
| 137 | [KPF_SLOB_FREE] = "P:slob_free", |
| 138 | [KPF_SLUB_FROZEN] = "A:slub_frozen", |
| 139 | [KPF_SLUB_DEBUG] = "E:slub_debug", |
| 140 | }; |
| 141 | |
| 142 | |
Dave Young | c6dd897 | 2012-03-28 14:42:55 -0700 | [diff] [blame] | 143 | static const char * const debugfs_known_mountpoints[] = { |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 144 | "/sys/kernel/debug", |
| 145 | "/debug", |
| 146 | 0, |
| 147 | }; |
| 148 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 149 | /* |
| 150 | * data structures |
| 151 | */ |
| 152 | |
| 153 | static int opt_raw; /* for kernel developers */ |
| 154 | static int opt_list; /* list pages (in ranges) */ |
| 155 | static int opt_no_summary; /* don't show summary */ |
| 156 | static pid_t opt_pid; /* process to walk */ |
| 157 | |
| 158 | #define MAX_ADDR_RANGES 1024 |
| 159 | static int nr_addr_ranges; |
| 160 | static unsigned long opt_offset[MAX_ADDR_RANGES]; |
| 161 | static unsigned long opt_size[MAX_ADDR_RANGES]; |
| 162 | |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 163 | #define MAX_VMAS 10240 |
| 164 | static int nr_vmas; |
| 165 | static unsigned long pg_start[MAX_VMAS]; |
| 166 | static unsigned long pg_end[MAX_VMAS]; |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 167 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 168 | #define MAX_BIT_FILTERS 64 |
| 169 | static int nr_bit_filters; |
| 170 | static uint64_t opt_mask[MAX_BIT_FILTERS]; |
| 171 | static uint64_t opt_bits[MAX_BIT_FILTERS]; |
| 172 | |
| 173 | static int page_size; |
| 174 | |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 175 | static int pagemap_fd; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 176 | static int kpageflags_fd; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 177 | |
Wu Fengguang | a54fed9 | 2009-10-07 16:32:32 -0700 | [diff] [blame] | 178 | static int opt_hwpoison; |
| 179 | static int opt_unpoison; |
| 180 | |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 181 | static char hwpoison_debug_fs[MAX_PATH+1]; |
Wu Fengguang | a54fed9 | 2009-10-07 16:32:32 -0700 | [diff] [blame] | 182 | static int hwpoison_inject_fd; |
| 183 | static int hwpoison_forget_fd; |
| 184 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 185 | #define HASH_SHIFT 13 |
| 186 | #define HASH_SIZE (1 << HASH_SHIFT) |
| 187 | #define HASH_MASK (HASH_SIZE - 1) |
| 188 | #define HASH_KEY(flags) (flags & HASH_MASK) |
| 189 | |
| 190 | static unsigned long total_pages; |
| 191 | static unsigned long nr_pages[HASH_SIZE]; |
Dave Young | c6dd897 | 2012-03-28 14:42:55 -0700 | [diff] [blame] | 192 | static uint64_t page_flags[HASH_SIZE]; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 193 | |
| 194 | |
| 195 | /* |
| 196 | * helper functions |
| 197 | */ |
| 198 | |
| 199 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 200 | |
| 201 | #define min_t(type, x, y) ({ \ |
| 202 | type __min1 = (x); \ |
| 203 | type __min2 = (y); \ |
| 204 | __min1 < __min2 ? __min1 : __min2; }) |
| 205 | |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 206 | #define max_t(type, x, y) ({ \ |
| 207 | type __max1 = (x); \ |
| 208 | type __max2 = (y); \ |
| 209 | __max1 > __max2 ? __max1 : __max2; }) |
| 210 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 211 | static unsigned long pages2mb(unsigned long pages) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 212 | { |
| 213 | return (pages * page_size) >> 20; |
| 214 | } |
| 215 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 216 | static void fatal(const char *x, ...) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 217 | { |
| 218 | va_list ap; |
| 219 | |
| 220 | va_start(ap, x); |
| 221 | vfprintf(stderr, x, ap); |
| 222 | va_end(ap); |
| 223 | exit(EXIT_FAILURE); |
| 224 | } |
| 225 | |
Josh Triplett | 1b98c00 | 2009-10-16 14:06:13 -0700 | [diff] [blame] | 226 | static int checked_open(const char *pathname, int flags) |
Wu Fengguang | 31bbf66 | 2009-10-07 16:32:29 -0700 | [diff] [blame] | 227 | { |
| 228 | int fd = open(pathname, flags); |
| 229 | |
| 230 | if (fd < 0) { |
| 231 | perror(pathname); |
| 232 | exit(EXIT_FAILURE); |
| 233 | } |
| 234 | |
| 235 | return fd; |
| 236 | } |
| 237 | |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 238 | /* |
| 239 | * pagemap/kpageflags routines |
| 240 | */ |
| 241 | |
| 242 | static unsigned long do_u64_read(int fd, char *name, |
| 243 | uint64_t *buf, |
| 244 | unsigned long index, |
| 245 | unsigned long count) |
| 246 | { |
| 247 | long bytes; |
| 248 | |
| 249 | if (index > ULONG_MAX / 8) |
| 250 | fatal("index overflow: %lu\n", index); |
| 251 | |
| 252 | if (lseek(fd, index * 8, SEEK_SET) < 0) { |
| 253 | perror(name); |
| 254 | exit(EXIT_FAILURE); |
| 255 | } |
| 256 | |
| 257 | bytes = read(fd, buf, count * 8); |
| 258 | if (bytes < 0) { |
| 259 | perror(name); |
| 260 | exit(EXIT_FAILURE); |
| 261 | } |
| 262 | if (bytes % 8) |
| 263 | fatal("partial read: %lu bytes\n", bytes); |
| 264 | |
| 265 | return bytes / 8; |
| 266 | } |
| 267 | |
| 268 | static unsigned long kpageflags_read(uint64_t *buf, |
| 269 | unsigned long index, |
| 270 | unsigned long pages) |
| 271 | { |
| 272 | return do_u64_read(kpageflags_fd, PROC_KPAGEFLAGS, buf, index, pages); |
| 273 | } |
| 274 | |
| 275 | static unsigned long pagemap_read(uint64_t *buf, |
| 276 | unsigned long index, |
| 277 | unsigned long pages) |
| 278 | { |
| 279 | return do_u64_read(pagemap_fd, "/proc/pid/pagemap", buf, index, pages); |
| 280 | } |
| 281 | |
| 282 | static unsigned long pagemap_pfn(uint64_t val) |
| 283 | { |
| 284 | unsigned long pfn; |
| 285 | |
| 286 | if (val & PM_PRESENT) |
| 287 | pfn = PM_PFRAME(val); |
| 288 | else |
| 289 | pfn = 0; |
| 290 | |
| 291 | return pfn; |
| 292 | } |
| 293 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * page flag names |
| 297 | */ |
| 298 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 299 | static char *page_flag_name(uint64_t flags) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 300 | { |
| 301 | static char buf[65]; |
| 302 | int present; |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 303 | size_t i, j; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 304 | |
| 305 | for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) { |
| 306 | present = (flags >> i) & 1; |
| 307 | if (!page_flag_names[i]) { |
| 308 | if (present) |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 309 | fatal("unknown flag bit %d\n", i); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 310 | continue; |
| 311 | } |
| 312 | buf[j++] = present ? page_flag_names[i][0] : '_'; |
| 313 | } |
| 314 | |
| 315 | return buf; |
| 316 | } |
| 317 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 318 | static char *page_flag_longname(uint64_t flags) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 319 | { |
| 320 | static char buf[1024]; |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 321 | size_t i, n; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 322 | |
| 323 | for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) { |
| 324 | if (!page_flag_names[i]) |
| 325 | continue; |
| 326 | if ((flags >> i) & 1) |
| 327 | n += snprintf(buf + n, sizeof(buf) - n, "%s,", |
| 328 | page_flag_names[i] + 2); |
| 329 | } |
| 330 | if (n) |
| 331 | n--; |
| 332 | buf[n] = '\0'; |
| 333 | |
| 334 | return buf; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /* |
| 339 | * page list and summary |
| 340 | */ |
| 341 | |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 342 | static void show_page_range(unsigned long voffset, |
| 343 | unsigned long offset, uint64_t flags) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 344 | { |
| 345 | static uint64_t flags0; |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 346 | static unsigned long voff; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 347 | static unsigned long index; |
| 348 | static unsigned long count; |
| 349 | |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 350 | if (flags == flags0 && offset == index + count && |
| 351 | (!opt_pid || voffset == voff + count)) { |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 352 | count++; |
| 353 | return; |
| 354 | } |
| 355 | |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 356 | if (count) { |
| 357 | if (opt_pid) |
| 358 | printf("%lx\t", voff); |
| 359 | printf("%lx\t%lx\t%s\n", |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 360 | index, count, page_flag_name(flags0)); |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 361 | } |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 362 | |
| 363 | flags0 = flags; |
| 364 | index = offset; |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 365 | voff = voffset; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 366 | count = 1; |
| 367 | } |
| 368 | |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 369 | static void show_page(unsigned long voffset, |
| 370 | unsigned long offset, uint64_t flags) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 371 | { |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 372 | if (opt_pid) |
| 373 | printf("%lx\t", voffset); |
| 374 | printf("%lx\t%s\n", offset, page_flag_name(flags)); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 377 | static void show_summary(void) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 378 | { |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 379 | size_t i; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 380 | |
| 381 | printf(" flags\tpage-count MB" |
| 382 | " symbolic-flags\t\t\tlong-symbolic-flags\n"); |
| 383 | |
| 384 | for (i = 0; i < ARRAY_SIZE(nr_pages); i++) { |
| 385 | if (nr_pages[i]) |
| 386 | printf("0x%016llx\t%10lu %8lu %s\t%s\n", |
| 387 | (unsigned long long)page_flags[i], |
| 388 | nr_pages[i], |
| 389 | pages2mb(nr_pages[i]), |
| 390 | page_flag_name(page_flags[i]), |
| 391 | page_flag_longname(page_flags[i])); |
| 392 | } |
| 393 | |
| 394 | printf(" total\t%10lu %8lu\n", |
| 395 | total_pages, pages2mb(total_pages)); |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /* |
| 400 | * page flag filters |
| 401 | */ |
| 402 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 403 | static int bit_mask_ok(uint64_t flags) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 404 | { |
| 405 | int i; |
| 406 | |
| 407 | for (i = 0; i < nr_bit_filters; i++) { |
| 408 | if (opt_bits[i] == KPF_ALL_BITS) { |
| 409 | if ((flags & opt_mask[i]) == 0) |
| 410 | return 0; |
| 411 | } else { |
| 412 | if ((flags & opt_mask[i]) != opt_bits[i]) |
| 413 | return 0; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return 1; |
| 418 | } |
| 419 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 420 | static uint64_t expand_overloaded_flags(uint64_t flags) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 421 | { |
| 422 | /* SLOB/SLUB overload several page flags */ |
| 423 | if (flags & BIT(SLAB)) { |
| 424 | if (flags & BIT(PRIVATE)) |
| 425 | flags ^= BIT(PRIVATE) | BIT(SLOB_FREE); |
| 426 | if (flags & BIT(ACTIVE)) |
| 427 | flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN); |
| 428 | if (flags & BIT(ERROR)) |
| 429 | flags ^= BIT(ERROR) | BIT(SLUB_DEBUG); |
| 430 | } |
| 431 | |
| 432 | /* PG_reclaim is overloaded as PG_readahead in the read path */ |
| 433 | if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM)) |
| 434 | flags ^= BIT(RECLAIM) | BIT(READAHEAD); |
| 435 | |
| 436 | return flags; |
| 437 | } |
| 438 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 439 | static uint64_t well_known_flags(uint64_t flags) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 440 | { |
| 441 | /* hide flags intended only for kernel hacker */ |
| 442 | flags &= ~KPF_HACKERS_BITS; |
| 443 | |
| 444 | /* hide non-hugeTLB compound pages */ |
| 445 | if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE))) |
| 446 | flags &= ~BITS_COMPOUND; |
| 447 | |
| 448 | return flags; |
| 449 | } |
| 450 | |
Wu Fengguang | 48640d6 | 2009-10-07 16:32:31 -0700 | [diff] [blame] | 451 | static uint64_t kpageflags_flags(uint64_t flags) |
| 452 | { |
| 453 | flags = expand_overloaded_flags(flags); |
| 454 | |
| 455 | if (!opt_raw) |
| 456 | flags = well_known_flags(flags); |
| 457 | |
| 458 | return flags; |
| 459 | } |
| 460 | |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 461 | /* verify that a mountpoint is actually a debugfs instance */ |
| 462 | static int debugfs_valid_mountpoint(const char *debugfs) |
| 463 | { |
| 464 | struct statfs st_fs; |
| 465 | |
| 466 | if (statfs(debugfs, &st_fs) < 0) |
| 467 | return -ENOENT; |
| 468 | else if (st_fs.f_type != (long) DEBUGFS_MAGIC) |
| 469 | return -ENOENT; |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | /* find the path to the mounted debugfs */ |
| 475 | static const char *debugfs_find_mountpoint(void) |
| 476 | { |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 477 | const char *const *ptr; |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 478 | char type[100]; |
| 479 | FILE *fp; |
| 480 | |
| 481 | ptr = debugfs_known_mountpoints; |
| 482 | while (*ptr) { |
| 483 | if (debugfs_valid_mountpoint(*ptr) == 0) { |
| 484 | strcpy(hwpoison_debug_fs, *ptr); |
| 485 | return hwpoison_debug_fs; |
| 486 | } |
| 487 | ptr++; |
| 488 | } |
| 489 | |
| 490 | /* give up and parse /proc/mounts */ |
| 491 | fp = fopen("/proc/mounts", "r"); |
| 492 | if (fp == NULL) |
| 493 | perror("Can't open /proc/mounts for read"); |
| 494 | |
| 495 | while (fscanf(fp, "%*s %" |
| 496 | STR(MAX_PATH) |
| 497 | "s %99s %*s %*d %*d\n", |
| 498 | hwpoison_debug_fs, type) == 2) { |
| 499 | if (strcmp(type, "debugfs") == 0) |
| 500 | break; |
| 501 | } |
| 502 | fclose(fp); |
| 503 | |
| 504 | if (strcmp(type, "debugfs") != 0) |
| 505 | return NULL; |
| 506 | |
| 507 | return hwpoison_debug_fs; |
| 508 | } |
| 509 | |
| 510 | /* mount the debugfs somewhere if it's not mounted */ |
| 511 | |
| 512 | static void debugfs_mount(void) |
| 513 | { |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 514 | const char *const *ptr; |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 515 | |
| 516 | /* see if it's already mounted */ |
| 517 | if (debugfs_find_mountpoint()) |
| 518 | return; |
| 519 | |
| 520 | ptr = debugfs_known_mountpoints; |
| 521 | while (*ptr) { |
| 522 | if (mount(NULL, *ptr, "debugfs", 0, NULL) == 0) { |
| 523 | /* save the mountpoint */ |
| 524 | strcpy(hwpoison_debug_fs, *ptr); |
| 525 | break; |
| 526 | } |
| 527 | ptr++; |
| 528 | } |
| 529 | |
| 530 | if (*ptr == NULL) { |
| 531 | perror("mount debugfs"); |
| 532 | exit(EXIT_FAILURE); |
| 533 | } |
| 534 | } |
| 535 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 536 | /* |
Wu Fengguang | a54fed9 | 2009-10-07 16:32:32 -0700 | [diff] [blame] | 537 | * page actions |
| 538 | */ |
| 539 | |
| 540 | static void prepare_hwpoison_fd(void) |
| 541 | { |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 542 | char buf[MAX_PATH + 1]; |
| 543 | |
| 544 | debugfs_mount(); |
Wu Fengguang | a54fed9 | 2009-10-07 16:32:32 -0700 | [diff] [blame] | 545 | |
| 546 | if (opt_hwpoison && !hwpoison_inject_fd) { |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 547 | snprintf(buf, MAX_PATH, "%s/hwpoison/corrupt-pfn", |
| 548 | hwpoison_debug_fs); |
Wu Fengguang | a54fed9 | 2009-10-07 16:32:32 -0700 | [diff] [blame] | 549 | hwpoison_inject_fd = checked_open(buf, O_WRONLY); |
| 550 | } |
| 551 | |
| 552 | if (opt_unpoison && !hwpoison_forget_fd) { |
Chen Gong | 12da58b | 2011-03-22 16:35:03 -0700 | [diff] [blame] | 553 | snprintf(buf, MAX_PATH, "%s/hwpoison/unpoison-pfn", |
| 554 | hwpoison_debug_fs); |
Wu Fengguang | a54fed9 | 2009-10-07 16:32:32 -0700 | [diff] [blame] | 555 | hwpoison_forget_fd = checked_open(buf, O_WRONLY); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | static int hwpoison_page(unsigned long offset) |
| 560 | { |
| 561 | char buf[100]; |
| 562 | int len; |
| 563 | |
| 564 | len = sprintf(buf, "0x%lx\n", offset); |
| 565 | len = write(hwpoison_inject_fd, buf, len); |
| 566 | if (len < 0) { |
| 567 | perror("hwpoison inject"); |
| 568 | return len; |
| 569 | } |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static int unpoison_page(unsigned long offset) |
| 574 | { |
| 575 | char buf[100]; |
| 576 | int len; |
| 577 | |
| 578 | len = sprintf(buf, "0x%lx\n", offset); |
| 579 | len = write(hwpoison_forget_fd, buf, len); |
| 580 | if (len < 0) { |
| 581 | perror("hwpoison forget"); |
| 582 | return len; |
| 583 | } |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | /* |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 588 | * page frame walker |
| 589 | */ |
| 590 | |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 591 | static size_t hash_slot(uint64_t flags) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 592 | { |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 593 | size_t k = HASH_KEY(flags); |
| 594 | size_t i; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 595 | |
| 596 | /* Explicitly reserve slot 0 for flags 0: the following logic |
| 597 | * cannot distinguish an unoccupied slot from slot (flags==0). |
| 598 | */ |
| 599 | if (flags == 0) |
| 600 | return 0; |
| 601 | |
| 602 | /* search through the remaining (HASH_SIZE-1) slots */ |
| 603 | for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) { |
| 604 | if (!k || k >= ARRAY_SIZE(page_flags)) |
| 605 | k = 1; |
| 606 | if (page_flags[k] == 0) { |
| 607 | page_flags[k] = flags; |
| 608 | return k; |
| 609 | } |
| 610 | if (page_flags[k] == flags) |
| 611 | return k; |
| 612 | } |
| 613 | |
| 614 | fatal("hash table full: bump up HASH_SHIFT?\n"); |
| 615 | exit(EXIT_FAILURE); |
| 616 | } |
| 617 | |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 618 | static void add_page(unsigned long voffset, |
| 619 | unsigned long offset, uint64_t flags) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 620 | { |
Wu Fengguang | 48640d6 | 2009-10-07 16:32:31 -0700 | [diff] [blame] | 621 | flags = kpageflags_flags(flags); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 622 | |
| 623 | if (!bit_mask_ok(flags)) |
| 624 | return; |
| 625 | |
Wu Fengguang | a54fed9 | 2009-10-07 16:32:32 -0700 | [diff] [blame] | 626 | if (opt_hwpoison) |
| 627 | hwpoison_page(offset); |
| 628 | if (opt_unpoison) |
| 629 | unpoison_page(offset); |
| 630 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 631 | if (opt_list == 1) |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 632 | show_page_range(voffset, offset, flags); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 633 | else if (opt_list == 2) |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 634 | show_page(voffset, offset, flags); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 635 | |
| 636 | nr_pages[hash_slot(flags)]++; |
| 637 | total_pages++; |
| 638 | } |
| 639 | |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 640 | #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */ |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 641 | static void walk_pfn(unsigned long voffset, |
| 642 | unsigned long index, |
| 643 | unsigned long count) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 644 | { |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 645 | uint64_t buf[KPAGEFLAGS_BATCH]; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 646 | unsigned long batch; |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 647 | unsigned long pages; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 648 | unsigned long i; |
| 649 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 650 | while (count) { |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 651 | batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH); |
| 652 | pages = kpageflags_read(buf, index, batch); |
| 653 | if (pages == 0) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 654 | break; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 655 | |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 656 | for (i = 0; i < pages; i++) |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 657 | add_page(voffset + i, index + i, buf[i]); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 658 | |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 659 | index += pages; |
| 660 | count -= pages; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 664 | #define PAGEMAP_BATCH (64 << 10) |
| 665 | static void walk_vma(unsigned long index, unsigned long count) |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 666 | { |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 667 | uint64_t buf[PAGEMAP_BATCH]; |
| 668 | unsigned long batch; |
| 669 | unsigned long pages; |
| 670 | unsigned long pfn; |
| 671 | unsigned long i; |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 672 | |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 673 | while (count) { |
| 674 | batch = min_t(unsigned long, count, PAGEMAP_BATCH); |
| 675 | pages = pagemap_read(buf, index, batch); |
| 676 | if (pages == 0) |
| 677 | break; |
| 678 | |
| 679 | for (i = 0; i < pages; i++) { |
| 680 | pfn = pagemap_pfn(buf[i]); |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 681 | if (pfn) |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 682 | walk_pfn(index + i, pfn, 1); |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 683 | } |
Wu Fengguang | 4a1b672 | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 684 | |
| 685 | index += pages; |
| 686 | count -= pages; |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 687 | } |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static void walk_task(unsigned long index, unsigned long count) |
| 691 | { |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 692 | const unsigned long end = index + count; |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 693 | unsigned long start; |
| 694 | int i = 0; |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 695 | |
| 696 | while (index < end) { |
| 697 | |
| 698 | while (pg_end[i] <= index) |
| 699 | if (++i >= nr_vmas) |
| 700 | return; |
| 701 | if (pg_start[i] >= end) |
| 702 | return; |
| 703 | |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 704 | start = max_t(unsigned long, pg_start[i], index); |
| 705 | index = min_t(unsigned long, pg_end[i], end); |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 706 | |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 707 | assert(start < index); |
| 708 | walk_vma(start, index - start); |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | static void add_addr_range(unsigned long offset, unsigned long size) |
| 713 | { |
| 714 | if (nr_addr_ranges >= MAX_ADDR_RANGES) |
| 715 | fatal("too many addr ranges\n"); |
| 716 | |
| 717 | opt_offset[nr_addr_ranges] = offset; |
| 718 | opt_size[nr_addr_ranges] = min_t(unsigned long, size, ULONG_MAX-offset); |
| 719 | nr_addr_ranges++; |
| 720 | } |
| 721 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 722 | static void walk_addr_ranges(void) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 723 | { |
| 724 | int i; |
| 725 | |
Wu Fengguang | 31bbf66 | 2009-10-07 16:32:29 -0700 | [diff] [blame] | 726 | kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 727 | |
| 728 | if (!nr_addr_ranges) |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 729 | add_addr_range(0, ULONG_MAX); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 730 | |
| 731 | for (i = 0; i < nr_addr_ranges; i++) |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 732 | if (!opt_pid) |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 733 | walk_pfn(0, opt_offset[i], opt_size[i]); |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 734 | else |
| 735 | walk_task(opt_offset[i], opt_size[i]); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 736 | |
| 737 | close(kpageflags_fd); |
| 738 | } |
| 739 | |
| 740 | |
| 741 | /* |
| 742 | * user interface |
| 743 | */ |
| 744 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 745 | static const char *page_flag_type(uint64_t flag) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 746 | { |
| 747 | if (flag & KPF_HACKERS_BITS) |
| 748 | return "(r)"; |
| 749 | if (flag & KPF_OVERLOADED_BITS) |
| 750 | return "(o)"; |
| 751 | return " "; |
| 752 | } |
| 753 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 754 | static void usage(void) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 755 | { |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 756 | size_t i, j; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 757 | |
| 758 | printf( |
| 759 | "page-types [options]\n" |
Alex Chiang | 9fdcd88 | 2009-12-14 17:57:53 -0800 | [diff] [blame] | 760 | " -r|--raw Raw mode, for kernel developers\n" |
Alex Chiang | dcfe730 | 2009-12-14 17:57:52 -0800 | [diff] [blame] | 761 | " -d|--describe flags Describe flags\n" |
Alex Chiang | 9fdcd88 | 2009-12-14 17:57:53 -0800 | [diff] [blame] | 762 | " -a|--addr addr-spec Walk a range of pages\n" |
| 763 | " -b|--bits bits-spec Walk pages with specified bits\n" |
| 764 | " -p|--pid pid Walk process address space\n" |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 765 | #if 0 /* planned features */ |
Alex Chiang | 9fdcd88 | 2009-12-14 17:57:53 -0800 | [diff] [blame] | 766 | " -f|--file filename Walk file address space\n" |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 767 | #endif |
Alex Chiang | 9fdcd88 | 2009-12-14 17:57:53 -0800 | [diff] [blame] | 768 | " -l|--list Show page details in ranges\n" |
| 769 | " -L|--list-each Show page details one by one\n" |
Tommi Rantala | c7825cf | 2010-08-05 11:23:11 -0700 | [diff] [blame] | 770 | " -N|--no-summary Don't show summary info\n" |
Alex Chiang | 9fdcd88 | 2009-12-14 17:57:53 -0800 | [diff] [blame] | 771 | " -X|--hwpoison hwpoison pages\n" |
| 772 | " -x|--unpoison unpoison pages\n" |
| 773 | " -h|--help Show this usage message\n" |
Alex Chiang | dcfe730 | 2009-12-14 17:57:52 -0800 | [diff] [blame] | 774 | "flags:\n" |
Alex Chiang | 9fdcd88 | 2009-12-14 17:57:53 -0800 | [diff] [blame] | 775 | " 0x10 bitfield format, e.g.\n" |
| 776 | " anon bit-name, e.g.\n" |
| 777 | " 0x10,anon comma-separated list, e.g.\n" |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 778 | "addr-spec:\n" |
Alex Chiang | 9fdcd88 | 2009-12-14 17:57:53 -0800 | [diff] [blame] | 779 | " N one page at offset N (unit: pages)\n" |
| 780 | " N+M pages range from N to N+M-1\n" |
| 781 | " N,M pages range from N to M-1\n" |
| 782 | " N, pages range from N to end\n" |
| 783 | " ,M pages range from 0 to M-1\n" |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 784 | "bits-spec:\n" |
Alex Chiang | 9fdcd88 | 2009-12-14 17:57:53 -0800 | [diff] [blame] | 785 | " bit1,bit2 (flags & (bit1|bit2)) != 0\n" |
| 786 | " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n" |
| 787 | " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n" |
| 788 | " =bit1,bit2 flags == (bit1|bit2)\n" |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 789 | "bit-names:\n" |
| 790 | ); |
| 791 | |
| 792 | for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) { |
| 793 | if (!page_flag_names[i]) |
| 794 | continue; |
| 795 | printf("%16s%s", page_flag_names[i] + 2, |
| 796 | page_flag_type(1ULL << i)); |
| 797 | if (++j > 3) { |
| 798 | j = 0; |
| 799 | putchar('\n'); |
| 800 | } |
| 801 | } |
| 802 | printf("\n " |
| 803 | "(r) raw mode bits (o) overloaded bits\n"); |
| 804 | } |
| 805 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 806 | static unsigned long long parse_number(const char *str) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 807 | { |
| 808 | unsigned long long n; |
| 809 | |
| 810 | n = strtoll(str, NULL, 0); |
| 811 | |
| 812 | if (n == 0 && str[0] != '0') |
| 813 | fatal("invalid name or number: %s\n", str); |
| 814 | |
| 815 | return n; |
| 816 | } |
| 817 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 818 | static void parse_pid(const char *str) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 819 | { |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 820 | FILE *file; |
| 821 | char buf[5000]; |
| 822 | |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 823 | opt_pid = parse_number(str); |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 824 | |
| 825 | sprintf(buf, "/proc/%d/pagemap", opt_pid); |
Wu Fengguang | 31bbf66 | 2009-10-07 16:32:29 -0700 | [diff] [blame] | 826 | pagemap_fd = checked_open(buf, O_RDONLY); |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 827 | |
| 828 | sprintf(buf, "/proc/%d/maps", opt_pid); |
| 829 | file = fopen(buf, "r"); |
| 830 | if (!file) { |
| 831 | perror(buf); |
| 832 | exit(EXIT_FAILURE); |
| 833 | } |
| 834 | |
| 835 | while (fgets(buf, sizeof(buf), file) != NULL) { |
| 836 | unsigned long vm_start; |
| 837 | unsigned long vm_end; |
| 838 | unsigned long long pgoff; |
| 839 | int major, minor; |
| 840 | char r, w, x, s; |
| 841 | unsigned long ino; |
| 842 | int n; |
| 843 | |
| 844 | n = sscanf(buf, "%lx-%lx %c%c%c%c %llx %x:%x %lu", |
| 845 | &vm_start, |
| 846 | &vm_end, |
| 847 | &r, &w, &x, &s, |
| 848 | &pgoff, |
| 849 | &major, &minor, |
| 850 | &ino); |
| 851 | if (n < 10) { |
| 852 | fprintf(stderr, "unexpected line: %s\n", buf); |
| 853 | continue; |
| 854 | } |
| 855 | pg_start[nr_vmas] = vm_start / page_size; |
| 856 | pg_end[nr_vmas] = vm_end / page_size; |
| 857 | if (++nr_vmas >= MAX_VMAS) { |
| 858 | fprintf(stderr, "too many VMAs\n"); |
| 859 | break; |
| 860 | } |
| 861 | } |
| 862 | fclose(file); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 865 | static void parse_file(const char *name) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 866 | { |
| 867 | } |
| 868 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 869 | static void parse_addr_range(const char *optarg) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 870 | { |
| 871 | unsigned long offset; |
| 872 | unsigned long size; |
| 873 | char *p; |
| 874 | |
| 875 | p = strchr(optarg, ','); |
| 876 | if (!p) |
| 877 | p = strchr(optarg, '+'); |
| 878 | |
| 879 | if (p == optarg) { |
| 880 | offset = 0; |
| 881 | size = parse_number(p + 1); |
| 882 | } else if (p) { |
| 883 | offset = parse_number(optarg); |
| 884 | if (p[1] == '\0') |
| 885 | size = ULONG_MAX; |
| 886 | else { |
| 887 | size = parse_number(p + 1); |
| 888 | if (*p == ',') { |
| 889 | if (size < offset) |
| 890 | fatal("invalid range: %lu,%lu\n", |
| 891 | offset, size); |
| 892 | size -= offset; |
| 893 | } |
| 894 | } |
| 895 | } else { |
| 896 | offset = parse_number(optarg); |
| 897 | size = 1; |
| 898 | } |
| 899 | |
| 900 | add_addr_range(offset, size); |
| 901 | } |
| 902 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 903 | static void add_bits_filter(uint64_t mask, uint64_t bits) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 904 | { |
| 905 | if (nr_bit_filters >= MAX_BIT_FILTERS) |
| 906 | fatal("too much bit filters\n"); |
| 907 | |
| 908 | opt_mask[nr_bit_filters] = mask; |
| 909 | opt_bits[nr_bit_filters] = bits; |
| 910 | nr_bit_filters++; |
| 911 | } |
| 912 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 913 | static uint64_t parse_flag_name(const char *str, int len) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 914 | { |
Ulrich Drepper | e30d539 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 915 | size_t i; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 916 | |
| 917 | if (!*str || !len) |
| 918 | return 0; |
| 919 | |
| 920 | if (len <= 8 && !strncmp(str, "compound", len)) |
| 921 | return BITS_COMPOUND; |
| 922 | |
| 923 | for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) { |
| 924 | if (!page_flag_names[i]) |
| 925 | continue; |
| 926 | if (!strncmp(str, page_flag_names[i] + 2, len)) |
| 927 | return 1ULL << i; |
| 928 | } |
| 929 | |
| 930 | return parse_number(str); |
| 931 | } |
| 932 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 933 | static uint64_t parse_flag_names(const char *str, int all) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 934 | { |
| 935 | const char *p = str; |
| 936 | uint64_t flags = 0; |
| 937 | |
| 938 | while (1) { |
| 939 | if (*p == ',' || *p == '=' || *p == '\0') { |
| 940 | if ((*str != '~') || (*str == '~' && all && *++str)) |
| 941 | flags |= parse_flag_name(str, p - str); |
| 942 | if (*p != ',') |
| 943 | break; |
| 944 | str = p + 1; |
| 945 | } |
| 946 | p++; |
| 947 | } |
| 948 | |
| 949 | return flags; |
| 950 | } |
| 951 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 952 | static void parse_bits_mask(const char *optarg) |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 953 | { |
| 954 | uint64_t mask; |
| 955 | uint64_t bits; |
| 956 | const char *p; |
| 957 | |
| 958 | p = strchr(optarg, '='); |
| 959 | if (p == optarg) { |
| 960 | mask = KPF_ALL_BITS; |
| 961 | bits = parse_flag_names(p + 1, 0); |
| 962 | } else if (p) { |
| 963 | mask = parse_flag_names(optarg, 0); |
| 964 | bits = parse_flag_names(p + 1, 0); |
| 965 | } else if (strchr(optarg, '~')) { |
| 966 | mask = parse_flag_names(optarg, 1); |
| 967 | bits = parse_flag_names(optarg, 0); |
| 968 | } else { |
| 969 | mask = parse_flag_names(optarg, 0); |
| 970 | bits = KPF_ALL_BITS; |
| 971 | } |
| 972 | |
| 973 | add_bits_filter(mask, bits); |
| 974 | } |
| 975 | |
Alex Chiang | dcfe730 | 2009-12-14 17:57:52 -0800 | [diff] [blame] | 976 | static void describe_flags(const char *optarg) |
| 977 | { |
| 978 | uint64_t flags = parse_flag_names(optarg, 0); |
| 979 | |
| 980 | printf("0x%016llx\t%s\t%s\n", |
| 981 | (unsigned long long)flags, |
| 982 | page_flag_name(flags), |
| 983 | page_flag_longname(flags)); |
| 984 | } |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 985 | |
Tommi Rantala | 3428838 | 2009-12-14 17:57:48 -0800 | [diff] [blame] | 986 | static const struct option opts[] = { |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 987 | { "raw" , 0, NULL, 'r' }, |
| 988 | { "pid" , 1, NULL, 'p' }, |
| 989 | { "file" , 1, NULL, 'f' }, |
| 990 | { "addr" , 1, NULL, 'a' }, |
| 991 | { "bits" , 1, NULL, 'b' }, |
Alex Chiang | dcfe730 | 2009-12-14 17:57:52 -0800 | [diff] [blame] | 992 | { "describe" , 1, NULL, 'd' }, |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 993 | { "list" , 0, NULL, 'l' }, |
| 994 | { "list-each" , 0, NULL, 'L' }, |
| 995 | { "no-summary", 0, NULL, 'N' }, |
Wu Fengguang | a54fed9 | 2009-10-07 16:32:32 -0700 | [diff] [blame] | 996 | { "hwpoison" , 0, NULL, 'X' }, |
| 997 | { "unpoison" , 0, NULL, 'x' }, |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 998 | { "help" , 0, NULL, 'h' }, |
| 999 | { NULL , 0, NULL, 0 } |
| 1000 | }; |
| 1001 | |
| 1002 | int main(int argc, char *argv[]) |
| 1003 | { |
| 1004 | int c; |
| 1005 | |
| 1006 | page_size = getpagesize(); |
| 1007 | |
| 1008 | while ((c = getopt_long(argc, argv, |
Alex Chiang | dcfe730 | 2009-12-14 17:57:52 -0800 | [diff] [blame] | 1009 | "rp:f:a:b:d:lLNXxh", opts, NULL)) != -1) { |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 1010 | switch (c) { |
| 1011 | case 'r': |
| 1012 | opt_raw = 1; |
| 1013 | break; |
| 1014 | case 'p': |
| 1015 | parse_pid(optarg); |
| 1016 | break; |
| 1017 | case 'f': |
| 1018 | parse_file(optarg); |
| 1019 | break; |
| 1020 | case 'a': |
| 1021 | parse_addr_range(optarg); |
| 1022 | break; |
| 1023 | case 'b': |
| 1024 | parse_bits_mask(optarg); |
| 1025 | break; |
Alex Chiang | dcfe730 | 2009-12-14 17:57:52 -0800 | [diff] [blame] | 1026 | case 'd': |
Alex Chiang | dcfe730 | 2009-12-14 17:57:52 -0800 | [diff] [blame] | 1027 | describe_flags(optarg); |
Alex Chiang | bb86a73 | 2009-12-14 17:57:54 -0800 | [diff] [blame] | 1028 | exit(0); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 1029 | case 'l': |
| 1030 | opt_list = 1; |
| 1031 | break; |
| 1032 | case 'L': |
| 1033 | opt_list = 2; |
| 1034 | break; |
| 1035 | case 'N': |
| 1036 | opt_no_summary = 1; |
| 1037 | break; |
Wu Fengguang | a54fed9 | 2009-10-07 16:32:32 -0700 | [diff] [blame] | 1038 | case 'X': |
| 1039 | opt_hwpoison = 1; |
| 1040 | prepare_hwpoison_fd(); |
| 1041 | break; |
| 1042 | case 'x': |
| 1043 | opt_unpoison = 1; |
| 1044 | prepare_hwpoison_fd(); |
| 1045 | break; |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 1046 | case 'h': |
| 1047 | usage(); |
| 1048 | exit(0); |
| 1049 | default: |
| 1050 | usage(); |
| 1051 | exit(1); |
| 1052 | } |
| 1053 | } |
| 1054 | |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 1055 | if (opt_list && opt_pid) |
| 1056 | printf("voffset\t"); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 1057 | if (opt_list == 1) |
Wu Fengguang | 0b4b2ad | 2009-09-23 15:56:16 -0700 | [diff] [blame] | 1058 | printf("offset\tlen\tflags\n"); |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 1059 | if (opt_list == 2) |
| 1060 | printf("offset\tflags\n"); |
| 1061 | |
| 1062 | walk_addr_ranges(); |
| 1063 | |
| 1064 | if (opt_list == 1) |
Wu Fengguang | e577ebd | 2009-10-07 16:32:30 -0700 | [diff] [blame] | 1065 | show_page_range(0, 0, 0); /* drain the buffer */ |
Wu Fengguang | 35efa5e | 2009-06-16 15:32:27 -0700 | [diff] [blame] | 1066 | |
| 1067 | if (opt_no_summary) |
| 1068 | return 0; |
| 1069 | |
| 1070 | if (opt_list) |
| 1071 | printf("\n\n"); |
| 1072 | |
| 1073 | show_summary(); |
| 1074 | |
| 1075 | return 0; |
| 1076 | } |