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