x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / tools / vm / page-types.c
blob6d5bcbaf61935f2f664856331e5f2ed125a66fb3
1 /*
2 * page-types: Tool for querying page flags
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.
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.
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.
17 * Copyright (C) 2009 Intel corporation
19 * Authors: Wu Fengguang <fengguang.wu@intel.com>
22 #define _FILE_OFFSET_BITS 64
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdint.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <getopt.h>
31 #include <limits.h>
32 #include <assert.h>
33 #include <ftw.h>
34 #include <time.h>
35 #include <setjmp.h>
36 #include <signal.h>
37 #include <sys/types.h>
38 #include <sys/errno.h>
39 #include <sys/fcntl.h>
40 #include <sys/mount.h>
41 #include <sys/statfs.h>
42 #include <sys/mman.h>
43 #include "../../include/uapi/linux/magic.h"
44 #include "../../include/uapi/linux/kernel-page-flags.h"
45 #include <api/fs/fs.h>
47 #ifndef MAX_PATH
48 # define MAX_PATH 256
49 #endif
51 #ifndef STR
52 # define _STR(x) #x
53 # define STR(x) _STR(x)
54 #endif
57 * pagemap kernel ABI bits
60 #define PM_ENTRY_BYTES 8
61 #define PM_PFRAME_BITS 55
62 #define PM_PFRAME_MASK ((1LL << PM_PFRAME_BITS) - 1)
63 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
64 #define MAX_SWAPFILES_SHIFT 5
65 #define PM_SWAP_OFFSET(x) (((x) & PM_PFRAME_MASK) >> MAX_SWAPFILES_SHIFT)
66 #define PM_SOFT_DIRTY (1ULL << 55)
67 #define PM_MMAP_EXCLUSIVE (1ULL << 56)
68 #define PM_FILE (1ULL << 61)
69 #define PM_SWAP (1ULL << 62)
70 #define PM_PRESENT (1ULL << 63)
73 * kernel page flags
76 #define KPF_BYTES 8
77 #define PROC_KPAGEFLAGS "/proc/kpageflags"
78 #define PROC_KPAGECGROUP "/proc/kpagecgroup"
80 /* [32-] kernel hacking assistances */
81 #define KPF_RESERVED 32
82 #define KPF_MLOCKED 33
83 #define KPF_MAPPEDTODISK 34
84 #define KPF_PRIVATE 35
85 #define KPF_PRIVATE_2 36
86 #define KPF_OWNER_PRIVATE 37
87 #define KPF_ARCH 38
88 #define KPF_UNCACHED 39
89 #define KPF_SOFTDIRTY 40
91 /* [48-] take some arbitrary free slots for expanding overloaded flags
92 * not part of kernel API
94 #define KPF_READAHEAD 48
95 #define KPF_SLOB_FREE 49
96 #define KPF_SLUB_FROZEN 50
97 #define KPF_SLUB_DEBUG 51
98 #define KPF_FILE 61
99 #define KPF_SWAP 62
100 #define KPF_MMAP_EXCLUSIVE 63
102 #define KPF_ALL_BITS ((uint64_t)~0ULL)
103 #define KPF_HACKERS_BITS (0xffffULL << 32)
104 #define KPF_OVERLOADED_BITS (0xffffULL << 48)
105 #define BIT(name) (1ULL << KPF_##name)
106 #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
108 static const char * const page_flag_names[] = {
109 [KPF_LOCKED] = "L:locked",
110 [KPF_ERROR] = "E:error",
111 [KPF_REFERENCED] = "R:referenced",
112 [KPF_UPTODATE] = "U:uptodate",
113 [KPF_DIRTY] = "D:dirty",
114 [KPF_LRU] = "l:lru",
115 [KPF_ACTIVE] = "A:active",
116 [KPF_SLAB] = "S:slab",
117 [KPF_WRITEBACK] = "W:writeback",
118 [KPF_RECLAIM] = "I:reclaim",
119 [KPF_BUDDY] = "B:buddy",
121 [KPF_MMAP] = "M:mmap",
122 [KPF_ANON] = "a:anonymous",
123 [KPF_SWAPCACHE] = "s:swapcache",
124 [KPF_SWAPBACKED] = "b:swapbacked",
125 [KPF_COMPOUND_HEAD] = "H:compound_head",
126 [KPF_COMPOUND_TAIL] = "T:compound_tail",
127 [KPF_HUGE] = "G:huge",
128 [KPF_UNEVICTABLE] = "u:unevictable",
129 [KPF_HWPOISON] = "X:hwpoison",
130 [KPF_NOPAGE] = "n:nopage",
131 [KPF_KSM] = "x:ksm",
132 [KPF_THP] = "t:thp",
133 [KPF_BALLOON] = "o:balloon",
134 [KPF_ZERO_PAGE] = "z:zero_page",
135 [KPF_IDLE] = "i:idle_page",
137 [KPF_RESERVED] = "r:reserved",
138 [KPF_MLOCKED] = "m:mlocked",
139 [KPF_MAPPEDTODISK] = "d:mappedtodisk",
140 [KPF_PRIVATE] = "P:private",
141 [KPF_PRIVATE_2] = "p:private_2",
142 [KPF_OWNER_PRIVATE] = "O:owner_private",
143 [KPF_ARCH] = "h:arch",
144 [KPF_UNCACHED] = "c:uncached",
145 [KPF_SOFTDIRTY] = "f:softdirty",
147 [KPF_READAHEAD] = "I:readahead",
148 [KPF_SLOB_FREE] = "P:slob_free",
149 [KPF_SLUB_FROZEN] = "A:slub_frozen",
150 [KPF_SLUB_DEBUG] = "E:slub_debug",
152 [KPF_FILE] = "F:file",
153 [KPF_SWAP] = "w:swap",
154 [KPF_MMAP_EXCLUSIVE] = "1:mmap_exclusive",
159 * data structures
162 static int opt_raw; /* for kernel developers */
163 static int opt_list; /* list pages (in ranges) */
164 static int opt_no_summary; /* don't show summary */
165 static pid_t opt_pid; /* process to walk */
166 const char * opt_file; /* file or directory path */
167 static uint64_t opt_cgroup; /* cgroup inode */
168 static int opt_list_cgroup;/* list page cgroup */
170 #define MAX_ADDR_RANGES 1024
171 static int nr_addr_ranges;
172 static unsigned long opt_offset[MAX_ADDR_RANGES];
173 static unsigned long opt_size[MAX_ADDR_RANGES];
175 #define MAX_VMAS 10240
176 static int nr_vmas;
177 static unsigned long pg_start[MAX_VMAS];
178 static unsigned long pg_end[MAX_VMAS];
180 #define MAX_BIT_FILTERS 64
181 static int nr_bit_filters;
182 static uint64_t opt_mask[MAX_BIT_FILTERS];
183 static uint64_t opt_bits[MAX_BIT_FILTERS];
185 static int page_size;
187 static int pagemap_fd;
188 static int kpageflags_fd;
189 static int kpagecgroup_fd = -1;
191 static int opt_hwpoison;
192 static int opt_unpoison;
194 static const char *hwpoison_debug_fs;
195 static int hwpoison_inject_fd;
196 static int hwpoison_forget_fd;
198 #define HASH_SHIFT 13
199 #define HASH_SIZE (1 << HASH_SHIFT)
200 #define HASH_MASK (HASH_SIZE - 1)
201 #define HASH_KEY(flags) (flags & HASH_MASK)
203 static unsigned long total_pages;
204 static unsigned long nr_pages[HASH_SIZE];
205 static uint64_t page_flags[HASH_SIZE];
209 * helper functions
212 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
214 #define min_t(type, x, y) ({ \
215 type __min1 = (x); \
216 type __min2 = (y); \
217 __min1 < __min2 ? __min1 : __min2; })
219 #define max_t(type, x, y) ({ \
220 type __max1 = (x); \
221 type __max2 = (y); \
222 __max1 > __max2 ? __max1 : __max2; })
224 static unsigned long pages2mb(unsigned long pages)
226 return (pages * page_size) >> 20;
229 static void fatal(const char *x, ...)
231 va_list ap;
233 va_start(ap, x);
234 vfprintf(stderr, x, ap);
235 va_end(ap);
236 exit(EXIT_FAILURE);
239 static int checked_open(const char *pathname, int flags)
241 int fd = open(pathname, flags);
243 if (fd < 0) {
244 perror(pathname);
245 exit(EXIT_FAILURE);
248 return fd;
252 * pagemap/kpageflags routines
255 static unsigned long do_u64_read(int fd, char *name,
256 uint64_t *buf,
257 unsigned long index,
258 unsigned long count)
260 long bytes;
262 if (index > ULONG_MAX / 8)
263 fatal("index overflow: %lu\n", index);
265 bytes = pread(fd, buf, count * 8, (off_t)index * 8);
266 if (bytes < 0) {
267 perror(name);
268 exit(EXIT_FAILURE);
270 if (bytes % 8)
271 fatal("partial read: %lu bytes\n", bytes);
273 return bytes / 8;
276 static unsigned long kpageflags_read(uint64_t *buf,
277 unsigned long index,
278 unsigned long pages)
280 return do_u64_read(kpageflags_fd, PROC_KPAGEFLAGS, buf, index, pages);
283 static unsigned long kpagecgroup_read(uint64_t *buf,
284 unsigned long index,
285 unsigned long pages)
287 if (kpagecgroup_fd < 0)
288 return pages;
290 return do_u64_read(kpagecgroup_fd, PROC_KPAGEFLAGS, buf, index, pages);
293 static unsigned long pagemap_read(uint64_t *buf,
294 unsigned long index,
295 unsigned long pages)
297 return do_u64_read(pagemap_fd, "/proc/pid/pagemap", buf, index, pages);
300 static unsigned long pagemap_pfn(uint64_t val)
302 unsigned long pfn;
304 if (val & PM_PRESENT)
305 pfn = PM_PFRAME(val);
306 else
307 pfn = 0;
309 return pfn;
312 static unsigned long pagemap_swap_offset(uint64_t val)
314 return val & PM_SWAP ? PM_SWAP_OFFSET(val) : 0;
318 * page flag names
321 static char *page_flag_name(uint64_t flags)
323 static char buf[65];
324 int present;
325 size_t i, j;
327 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
328 present = (flags >> i) & 1;
329 if (!page_flag_names[i]) {
330 if (present)
331 fatal("unknown flag bit %d\n", i);
332 continue;
334 buf[j++] = present ? page_flag_names[i][0] : '_';
337 return buf;
340 static char *page_flag_longname(uint64_t flags)
342 static char buf[1024];
343 size_t i, n;
345 for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
346 if (!page_flag_names[i])
347 continue;
348 if ((flags >> i) & 1)
349 n += snprintf(buf + n, sizeof(buf) - n, "%s,",
350 page_flag_names[i] + 2);
352 if (n)
353 n--;
354 buf[n] = '\0';
356 return buf;
361 * page list and summary
364 static void show_page_range(unsigned long voffset, unsigned long offset,
365 unsigned long size, uint64_t flags, uint64_t cgroup)
367 static uint64_t flags0;
368 static uint64_t cgroup0;
369 static unsigned long voff;
370 static unsigned long index;
371 static unsigned long count;
373 if (flags == flags0 && cgroup == cgroup0 && offset == index + count &&
374 size && voffset == voff + count) {
375 count += size;
376 return;
379 if (count) {
380 if (opt_pid)
381 printf("%lx\t", voff);
382 if (opt_file)
383 printf("%lu\t", voff);
384 if (opt_list_cgroup)
385 printf("@%llu\t", (unsigned long long)cgroup0);
386 printf("%lx\t%lx\t%s\n",
387 index, count, page_flag_name(flags0));
390 flags0 = flags;
391 cgroup0= cgroup;
392 index = offset;
393 voff = voffset;
394 count = size;
397 static void flush_page_range(void)
399 show_page_range(0, 0, 0, 0, 0);
402 static void show_page(unsigned long voffset, unsigned long offset,
403 uint64_t flags, uint64_t cgroup)
405 if (opt_pid)
406 printf("%lx\t", voffset);
407 if (opt_file)
408 printf("%lu\t", voffset);
409 if (opt_list_cgroup)
410 printf("@%llu\t", (unsigned long long)cgroup);
411 printf("%lx\t%s\n", offset, page_flag_name(flags));
414 static void show_summary(void)
416 size_t i;
418 printf(" flags\tpage-count MB"
419 " symbolic-flags\t\t\tlong-symbolic-flags\n");
421 for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
422 if (nr_pages[i])
423 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
424 (unsigned long long)page_flags[i],
425 nr_pages[i],
426 pages2mb(nr_pages[i]),
427 page_flag_name(page_flags[i]),
428 page_flag_longname(page_flags[i]));
431 printf(" total\t%10lu %8lu\n",
432 total_pages, pages2mb(total_pages));
437 * page flag filters
440 static int bit_mask_ok(uint64_t flags)
442 int i;
444 for (i = 0; i < nr_bit_filters; i++) {
445 if (opt_bits[i] == KPF_ALL_BITS) {
446 if ((flags & opt_mask[i]) == 0)
447 return 0;
448 } else {
449 if ((flags & opt_mask[i]) != opt_bits[i])
450 return 0;
454 return 1;
457 static uint64_t expand_overloaded_flags(uint64_t flags, uint64_t pme)
459 /* SLOB/SLUB overload several page flags */
460 if (flags & BIT(SLAB)) {
461 if (flags & BIT(PRIVATE))
462 flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
463 if (flags & BIT(ACTIVE))
464 flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
465 if (flags & BIT(ERROR))
466 flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
469 /* PG_reclaim is overloaded as PG_readahead in the read path */
470 if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
471 flags ^= BIT(RECLAIM) | BIT(READAHEAD);
473 if (pme & PM_SOFT_DIRTY)
474 flags |= BIT(SOFTDIRTY);
475 if (pme & PM_FILE)
476 flags |= BIT(FILE);
477 if (pme & PM_SWAP)
478 flags |= BIT(SWAP);
479 if (pme & PM_MMAP_EXCLUSIVE)
480 flags |= BIT(MMAP_EXCLUSIVE);
482 return flags;
485 static uint64_t well_known_flags(uint64_t flags)
487 /* hide flags intended only for kernel hacker */
488 flags &= ~KPF_HACKERS_BITS;
490 /* hide non-hugeTLB compound pages */
491 if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
492 flags &= ~BITS_COMPOUND;
494 return flags;
497 static uint64_t kpageflags_flags(uint64_t flags, uint64_t pme)
499 if (opt_raw)
500 flags = expand_overloaded_flags(flags, pme);
501 else
502 flags = well_known_flags(flags);
504 return flags;
508 * page actions
511 static void prepare_hwpoison_fd(void)
513 char buf[MAX_PATH + 1];
515 hwpoison_debug_fs = debugfs__mount();
516 if (!hwpoison_debug_fs) {
517 perror("mount debugfs");
518 exit(EXIT_FAILURE);
521 if (opt_hwpoison && !hwpoison_inject_fd) {
522 snprintf(buf, MAX_PATH, "%s/hwpoison/corrupt-pfn",
523 hwpoison_debug_fs);
524 hwpoison_inject_fd = checked_open(buf, O_WRONLY);
527 if (opt_unpoison && !hwpoison_forget_fd) {
528 snprintf(buf, MAX_PATH, "%s/hwpoison/unpoison-pfn",
529 hwpoison_debug_fs);
530 hwpoison_forget_fd = checked_open(buf, O_WRONLY);
534 static int hwpoison_page(unsigned long offset)
536 char buf[100];
537 int len;
539 len = sprintf(buf, "0x%lx\n", offset);
540 len = write(hwpoison_inject_fd, buf, len);
541 if (len < 0) {
542 perror("hwpoison inject");
543 return len;
545 return 0;
548 static int unpoison_page(unsigned long offset)
550 char buf[100];
551 int len;
553 len = sprintf(buf, "0x%lx\n", offset);
554 len = write(hwpoison_forget_fd, buf, len);
555 if (len < 0) {
556 perror("hwpoison forget");
557 return len;
559 return 0;
563 * page frame walker
566 static size_t hash_slot(uint64_t flags)
568 size_t k = HASH_KEY(flags);
569 size_t i;
571 /* Explicitly reserve slot 0 for flags 0: the following logic
572 * cannot distinguish an unoccupied slot from slot (flags==0).
574 if (flags == 0)
575 return 0;
577 /* search through the remaining (HASH_SIZE-1) slots */
578 for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) {
579 if (!k || k >= ARRAY_SIZE(page_flags))
580 k = 1;
581 if (page_flags[k] == 0) {
582 page_flags[k] = flags;
583 return k;
585 if (page_flags[k] == flags)
586 return k;
589 fatal("hash table full: bump up HASH_SHIFT?\n");
590 exit(EXIT_FAILURE);
593 static void add_page(unsigned long voffset, unsigned long offset,
594 uint64_t flags, uint64_t cgroup, uint64_t pme)
596 flags = kpageflags_flags(flags, pme);
598 if (!bit_mask_ok(flags))
599 return;
601 if (opt_cgroup && cgroup != (uint64_t)opt_cgroup)
602 return;
604 if (opt_hwpoison)
605 hwpoison_page(offset);
606 if (opt_unpoison)
607 unpoison_page(offset);
609 if (opt_list == 1)
610 show_page_range(voffset, offset, 1, flags, cgroup);
611 else if (opt_list == 2)
612 show_page(voffset, offset, flags, cgroup);
614 nr_pages[hash_slot(flags)]++;
615 total_pages++;
618 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
619 static void walk_pfn(unsigned long voffset,
620 unsigned long index,
621 unsigned long count,
622 uint64_t pme)
624 uint64_t buf[KPAGEFLAGS_BATCH];
625 uint64_t cgi[KPAGEFLAGS_BATCH];
626 unsigned long batch;
627 unsigned long pages;
628 unsigned long i;
631 * kpagecgroup_read() reads only if kpagecgroup were opened, but
632 * /proc/kpagecgroup might even not exist, so it's better to fill
633 * them with zeros here.
635 if (count == 1)
636 cgi[0] = 0;
637 else
638 memset(cgi, 0, sizeof cgi);
640 while (count) {
641 batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
642 pages = kpageflags_read(buf, index, batch);
643 if (pages == 0)
644 break;
646 if (kpagecgroup_read(cgi, index, pages) != pages)
647 fatal("kpagecgroup returned fewer pages than expected");
649 for (i = 0; i < pages; i++)
650 add_page(voffset + i, index + i, buf[i], cgi[i], pme);
652 index += pages;
653 count -= pages;
657 static void walk_swap(unsigned long voffset, uint64_t pme)
659 uint64_t flags = kpageflags_flags(0, pme);
661 if (!bit_mask_ok(flags))
662 return;
664 if (opt_cgroup)
665 return;
667 if (opt_list == 1)
668 show_page_range(voffset, pagemap_swap_offset(pme), 1, flags, 0);
669 else if (opt_list == 2)
670 show_page(voffset, pagemap_swap_offset(pme), flags, 0);
672 nr_pages[hash_slot(flags)]++;
673 total_pages++;
676 #define PAGEMAP_BATCH (64 << 10)
677 static void walk_vma(unsigned long index, unsigned long count)
679 uint64_t buf[PAGEMAP_BATCH];
680 unsigned long batch;
681 unsigned long pages;
682 unsigned long pfn;
683 unsigned long i;
685 while (count) {
686 batch = min_t(unsigned long, count, PAGEMAP_BATCH);
687 pages = pagemap_read(buf, index, batch);
688 if (pages == 0)
689 break;
691 for (i = 0; i < pages; i++) {
692 pfn = pagemap_pfn(buf[i]);
693 if (pfn)
694 walk_pfn(index + i, pfn, 1, buf[i]);
695 if (buf[i] & PM_SWAP)
696 walk_swap(index + i, buf[i]);
699 index += pages;
700 count -= pages;
704 static void walk_task(unsigned long index, unsigned long count)
706 const unsigned long end = index + count;
707 unsigned long start;
708 int i = 0;
710 while (index < end) {
712 while (pg_end[i] <= index)
713 if (++i >= nr_vmas)
714 return;
715 if (pg_start[i] >= end)
716 return;
718 start = max_t(unsigned long, pg_start[i], index);
719 index = min_t(unsigned long, pg_end[i], end);
721 assert(start < index);
722 walk_vma(start, index - start);
726 static void add_addr_range(unsigned long offset, unsigned long size)
728 if (nr_addr_ranges >= MAX_ADDR_RANGES)
729 fatal("too many addr ranges\n");
731 opt_offset[nr_addr_ranges] = offset;
732 opt_size[nr_addr_ranges] = min_t(unsigned long, size, ULONG_MAX-offset);
733 nr_addr_ranges++;
736 static void walk_addr_ranges(void)
738 int i;
740 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
742 if (!nr_addr_ranges)
743 add_addr_range(0, ULONG_MAX);
745 for (i = 0; i < nr_addr_ranges; i++)
746 if (!opt_pid)
747 walk_pfn(opt_offset[i], opt_offset[i], opt_size[i], 0);
748 else
749 walk_task(opt_offset[i], opt_size[i]);
751 close(kpageflags_fd);
756 * user interface
759 static const char *page_flag_type(uint64_t flag)
761 if (flag & KPF_HACKERS_BITS)
762 return "(r)";
763 if (flag & KPF_OVERLOADED_BITS)
764 return "(o)";
765 return " ";
768 static void usage(void)
770 size_t i, j;
772 printf(
773 "page-types [options]\n"
774 " -r|--raw Raw mode, for kernel developers\n"
775 " -d|--describe flags Describe flags\n"
776 " -a|--addr addr-spec Walk a range of pages\n"
777 " -b|--bits bits-spec Walk pages with specified bits\n"
778 " -c|--cgroup path|@inode Walk pages within memory cgroup\n"
779 " -p|--pid pid Walk process address space\n"
780 " -f|--file filename Walk file address space\n"
781 " -l|--list Show page details in ranges\n"
782 " -L|--list-each Show page details one by one\n"
783 " -C|--list-cgroup Show cgroup inode for pages\n"
784 " -N|--no-summary Don't show summary info\n"
785 " -X|--hwpoison hwpoison pages\n"
786 " -x|--unpoison unpoison pages\n"
787 " -h|--help Show this usage message\n"
788 "flags:\n"
789 " 0x10 bitfield format, e.g.\n"
790 " anon bit-name, e.g.\n"
791 " 0x10,anon comma-separated list, e.g.\n"
792 "addr-spec:\n"
793 " N one page at offset N (unit: pages)\n"
794 " N+M pages range from N to N+M-1\n"
795 " N,M pages range from N to M-1\n"
796 " N, pages range from N to end\n"
797 " ,M pages range from 0 to M-1\n"
798 "bits-spec:\n"
799 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
800 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
801 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
802 " =bit1,bit2 flags == (bit1|bit2)\n"
803 "bit-names:\n"
806 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
807 if (!page_flag_names[i])
808 continue;
809 printf("%16s%s", page_flag_names[i] + 2,
810 page_flag_type(1ULL << i));
811 if (++j > 3) {
812 j = 0;
813 putchar('\n');
816 printf("\n "
817 "(r) raw mode bits (o) overloaded bits\n");
820 static unsigned long long parse_number(const char *str)
822 unsigned long long n;
824 n = strtoll(str, NULL, 0);
826 if (n == 0 && str[0] != '0')
827 fatal("invalid name or number: %s\n", str);
829 return n;
832 static void parse_pid(const char *str)
834 FILE *file;
835 char buf[5000];
837 opt_pid = parse_number(str);
839 sprintf(buf, "/proc/%d/pagemap", opt_pid);
840 pagemap_fd = checked_open(buf, O_RDONLY);
842 sprintf(buf, "/proc/%d/maps", opt_pid);
843 file = fopen(buf, "r");
844 if (!file) {
845 perror(buf);
846 exit(EXIT_FAILURE);
849 while (fgets(buf, sizeof(buf), file) != NULL) {
850 unsigned long vm_start;
851 unsigned long vm_end;
852 unsigned long long pgoff;
853 int major, minor;
854 char r, w, x, s;
855 unsigned long ino;
856 int n;
858 n = sscanf(buf, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
859 &vm_start,
860 &vm_end,
861 &r, &w, &x, &s,
862 &pgoff,
863 &major, &minor,
864 &ino);
865 if (n < 10) {
866 fprintf(stderr, "unexpected line: %s\n", buf);
867 continue;
869 pg_start[nr_vmas] = vm_start / page_size;
870 pg_end[nr_vmas] = vm_end / page_size;
871 if (++nr_vmas >= MAX_VMAS) {
872 fprintf(stderr, "too many VMAs\n");
873 break;
876 fclose(file);
879 static void show_file(const char *name, const struct stat *st)
881 unsigned long long size = st->st_size;
882 char atime[64], mtime[64];
883 long now = time(NULL);
885 printf("%s\tInode: %u\tSize: %llu (%llu pages)\n",
886 name, (unsigned)st->st_ino,
887 size, (size + page_size - 1) / page_size);
889 strftime(atime, sizeof(atime), "%c", localtime(&st->st_atime));
890 strftime(mtime, sizeof(mtime), "%c", localtime(&st->st_mtime));
892 printf("Modify: %s (%ld seconds ago)\nAccess: %s (%ld seconds ago)\n",
893 mtime, now - st->st_mtime,
894 atime, now - st->st_atime);
897 static sigjmp_buf sigbus_jmp;
899 static void * volatile sigbus_addr;
901 static void sigbus_handler(int sig, siginfo_t *info, void *ucontex)
903 (void)sig;
904 (void)ucontex;
905 sigbus_addr = info ? info->si_addr : NULL;
906 siglongjmp(sigbus_jmp, 1);
909 static struct sigaction sigbus_action = {
910 .sa_sigaction = sigbus_handler,
911 .sa_flags = SA_SIGINFO,
914 static void walk_file(const char *name, const struct stat *st)
916 uint8_t vec[PAGEMAP_BATCH];
917 uint64_t buf[PAGEMAP_BATCH], flags;
918 uint64_t cgroup = 0;
919 unsigned long nr_pages, pfn, i;
920 off_t off, end = st->st_size;
921 int fd;
922 ssize_t len;
923 void *ptr;
924 int first = 1;
926 fd = checked_open(name, O_RDONLY|O_NOATIME|O_NOFOLLOW);
928 for (off = 0; off < end; off += len) {
929 nr_pages = (end - off + page_size - 1) / page_size;
930 if (nr_pages > PAGEMAP_BATCH)
931 nr_pages = PAGEMAP_BATCH;
932 len = nr_pages * page_size;
934 ptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, off);
935 if (ptr == MAP_FAILED)
936 fatal("mmap failed: %s", name);
938 /* determine cached pages */
939 if (mincore(ptr, len, vec))
940 fatal("mincore failed: %s", name);
942 /* turn off readahead */
943 if (madvise(ptr, len, MADV_RANDOM))
944 fatal("madvice failed: %s", name);
946 if (sigsetjmp(sigbus_jmp, 1)) {
947 end = off + sigbus_addr ? sigbus_addr - ptr : 0;
948 fprintf(stderr, "got sigbus at offset %lld: %s\n",
949 (long long)end, name);
950 goto got_sigbus;
953 /* populate ptes */
954 for (i = 0; i < nr_pages ; i++) {
955 if (vec[i] & 1)
956 (void)*(volatile int *)(ptr + i * page_size);
958 got_sigbus:
960 /* turn off harvesting reference bits */
961 if (madvise(ptr, len, MADV_SEQUENTIAL))
962 fatal("madvice failed: %s", name);
964 if (pagemap_read(buf, (unsigned long)ptr / page_size,
965 nr_pages) != nr_pages)
966 fatal("cannot read pagemap");
968 munmap(ptr, len);
970 for (i = 0; i < nr_pages; i++) {
971 pfn = pagemap_pfn(buf[i]);
972 if (!pfn)
973 continue;
974 if (!kpageflags_read(&flags, pfn, 1))
975 continue;
976 if (!kpagecgroup_read(&cgroup, pfn, 1))
977 fatal("kpagecgroup_read failed");
978 if (first && opt_list) {
979 first = 0;
980 flush_page_range();
981 show_file(name, st);
983 add_page(off / page_size + i, pfn,
984 flags, cgroup, buf[i]);
988 close(fd);
991 int walk_tree(const char *name, const struct stat *st, int type, struct FTW *f)
993 (void)f;
994 switch (type) {
995 case FTW_F:
996 if (S_ISREG(st->st_mode))
997 walk_file(name, st);
998 break;
999 case FTW_DNR:
1000 fprintf(stderr, "cannot read dir: %s\n", name);
1001 break;
1003 return 0;
1006 static void walk_page_cache(void)
1008 struct stat st;
1010 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
1011 pagemap_fd = checked_open("/proc/self/pagemap", O_RDONLY);
1012 sigaction(SIGBUS, &sigbus_action, NULL);
1014 if (stat(opt_file, &st))
1015 fatal("stat failed: %s\n", opt_file);
1017 if (S_ISREG(st.st_mode)) {
1018 walk_file(opt_file, &st);
1019 } else if (S_ISDIR(st.st_mode)) {
1020 /* do not follow symlinks and mountpoints */
1021 if (nftw(opt_file, walk_tree, 64, FTW_MOUNT | FTW_PHYS) < 0)
1022 fatal("nftw failed: %s\n", opt_file);
1023 } else
1024 fatal("unhandled file type: %s\n", opt_file);
1026 close(kpageflags_fd);
1027 close(pagemap_fd);
1028 signal(SIGBUS, SIG_DFL);
1031 static void parse_file(const char *name)
1033 opt_file = name;
1036 static void parse_cgroup(const char *path)
1038 if (path[0] == '@') {
1039 opt_cgroup = parse_number(path + 1);
1040 return;
1043 struct stat st;
1045 if (stat(path, &st))
1046 fatal("stat failed: %s: %m\n", path);
1048 if (!S_ISDIR(st.st_mode))
1049 fatal("cgroup supposed to be a directory: %s\n", path);
1051 opt_cgroup = st.st_ino;
1054 static void parse_addr_range(const char *optarg)
1056 unsigned long offset;
1057 unsigned long size;
1058 char *p;
1060 p = strchr(optarg, ',');
1061 if (!p)
1062 p = strchr(optarg, '+');
1064 if (p == optarg) {
1065 offset = 0;
1066 size = parse_number(p + 1);
1067 } else if (p) {
1068 offset = parse_number(optarg);
1069 if (p[1] == '\0')
1070 size = ULONG_MAX;
1071 else {
1072 size = parse_number(p + 1);
1073 if (*p == ',') {
1074 if (size < offset)
1075 fatal("invalid range: %lu,%lu\n",
1076 offset, size);
1077 size -= offset;
1080 } else {
1081 offset = parse_number(optarg);
1082 size = 1;
1085 add_addr_range(offset, size);
1088 static void add_bits_filter(uint64_t mask, uint64_t bits)
1090 if (nr_bit_filters >= MAX_BIT_FILTERS)
1091 fatal("too much bit filters\n");
1093 opt_mask[nr_bit_filters] = mask;
1094 opt_bits[nr_bit_filters] = bits;
1095 nr_bit_filters++;
1098 static uint64_t parse_flag_name(const char *str, int len)
1100 size_t i;
1102 if (!*str || !len)
1103 return 0;
1105 if (len <= 8 && !strncmp(str, "compound", len))
1106 return BITS_COMPOUND;
1108 for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
1109 if (!page_flag_names[i])
1110 continue;
1111 if (!strncmp(str, page_flag_names[i] + 2, len))
1112 return 1ULL << i;
1115 return parse_number(str);
1118 static uint64_t parse_flag_names(const char *str, int all)
1120 const char *p = str;
1121 uint64_t flags = 0;
1123 while (1) {
1124 if (*p == ',' || *p == '=' || *p == '\0') {
1125 if ((*str != '~') || (*str == '~' && all && *++str))
1126 flags |= parse_flag_name(str, p - str);
1127 if (*p != ',')
1128 break;
1129 str = p + 1;
1131 p++;
1134 return flags;
1137 static void parse_bits_mask(const char *optarg)
1139 uint64_t mask;
1140 uint64_t bits;
1141 const char *p;
1143 p = strchr(optarg, '=');
1144 if (p == optarg) {
1145 mask = KPF_ALL_BITS;
1146 bits = parse_flag_names(p + 1, 0);
1147 } else if (p) {
1148 mask = parse_flag_names(optarg, 0);
1149 bits = parse_flag_names(p + 1, 0);
1150 } else if (strchr(optarg, '~')) {
1151 mask = parse_flag_names(optarg, 1);
1152 bits = parse_flag_names(optarg, 0);
1153 } else {
1154 mask = parse_flag_names(optarg, 0);
1155 bits = KPF_ALL_BITS;
1158 add_bits_filter(mask, bits);
1161 static void describe_flags(const char *optarg)
1163 uint64_t flags = parse_flag_names(optarg, 0);
1165 printf("0x%016llx\t%s\t%s\n",
1166 (unsigned long long)flags,
1167 page_flag_name(flags),
1168 page_flag_longname(flags));
1171 static const struct option opts[] = {
1172 { "raw" , 0, NULL, 'r' },
1173 { "pid" , 1, NULL, 'p' },
1174 { "file" , 1, NULL, 'f' },
1175 { "addr" , 1, NULL, 'a' },
1176 { "bits" , 1, NULL, 'b' },
1177 { "cgroup" , 1, NULL, 'c' },
1178 { "describe" , 1, NULL, 'd' },
1179 { "list" , 0, NULL, 'l' },
1180 { "list-each" , 0, NULL, 'L' },
1181 { "list-cgroup", 0, NULL, 'C' },
1182 { "no-summary", 0, NULL, 'N' },
1183 { "hwpoison" , 0, NULL, 'X' },
1184 { "unpoison" , 0, NULL, 'x' },
1185 { "help" , 0, NULL, 'h' },
1186 { NULL , 0, NULL, 0 }
1189 int main(int argc, char *argv[])
1191 int c;
1193 page_size = getpagesize();
1195 while ((c = getopt_long(argc, argv,
1196 "rp:f:a:b:d:c:ClLNXxh", opts, NULL)) != -1) {
1197 switch (c) {
1198 case 'r':
1199 opt_raw = 1;
1200 break;
1201 case 'p':
1202 parse_pid(optarg);
1203 break;
1204 case 'f':
1205 parse_file(optarg);
1206 break;
1207 case 'a':
1208 parse_addr_range(optarg);
1209 break;
1210 case 'b':
1211 parse_bits_mask(optarg);
1212 break;
1213 case 'c':
1214 parse_cgroup(optarg);
1215 break;
1216 case 'C':
1217 opt_list_cgroup = 1;
1218 break;
1219 case 'd':
1220 describe_flags(optarg);
1221 exit(0);
1222 case 'l':
1223 opt_list = 1;
1224 break;
1225 case 'L':
1226 opt_list = 2;
1227 break;
1228 case 'N':
1229 opt_no_summary = 1;
1230 break;
1231 case 'X':
1232 opt_hwpoison = 1;
1233 prepare_hwpoison_fd();
1234 break;
1235 case 'x':
1236 opt_unpoison = 1;
1237 prepare_hwpoison_fd();
1238 break;
1239 case 'h':
1240 usage();
1241 exit(0);
1242 default:
1243 usage();
1244 exit(1);
1248 if (opt_cgroup || opt_list_cgroup)
1249 kpagecgroup_fd = checked_open(PROC_KPAGECGROUP, O_RDONLY);
1251 if (opt_list && opt_pid)
1252 printf("voffset\t");
1253 if (opt_list && opt_file)
1254 printf("foffset\t");
1255 if (opt_list && opt_list_cgroup)
1256 printf("cgroup\t");
1257 if (opt_list == 1)
1258 printf("offset\tlen\tflags\n");
1259 if (opt_list == 2)
1260 printf("offset\tflags\n");
1262 if (opt_file)
1263 walk_page_cache();
1264 else
1265 walk_addr_ranges();
1267 if (opt_list == 1)
1268 flush_page_range();
1270 if (opt_no_summary)
1271 return 0;
1273 if (opt_list)
1274 printf("\n\n");
1276 show_summary();
1278 return 0;