2 * page-types: Tool for querying page flags
4 * Copyright (C) 2009 Intel corporation
6 * Authors: Wu Fengguang <fengguang.wu@intel.com>
8 * Released under the General Public License (GPL).
11 #define _LARGEFILE64_SOURCE
21 #include <sys/types.h>
22 #include <sys/errno.h>
23 #include <sys/fcntl.h>
27 * pagemap kernel ABI bits
30 #define PM_ENTRY_BYTES sizeof(uint64_t)
31 #define PM_STATUS_BITS 3
32 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
33 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
34 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
35 #define PM_PSHIFT_BITS 6
36 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
37 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
38 #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
39 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
40 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
42 #define PM_PRESENT PM_STATUS(4LL)
43 #define PM_SWAP PM_STATUS(2LL)
51 #define PROC_KPAGEFLAGS "/proc/kpageflags"
53 /* copied from kpageflags_read() */
56 #define KPF_REFERENCED 2
57 #define KPF_UPTODATE 3
62 #define KPF_WRITEBACK 8
66 /* [11-20] new additions in 2.6.31 */
69 #define KPF_SWAPCACHE 13
70 #define KPF_SWAPBACKED 14
71 #define KPF_COMPOUND_HEAD 15
72 #define KPF_COMPOUND_TAIL 16
74 #define KPF_UNEVICTABLE 18
75 #define KPF_HWPOISON 19
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
87 #define KPF_UNCACHED 39
89 /* [48-] take some arbitrary free slots for expanding overloaded flags
90 * not part of kernel API
92 #define KPF_READAHEAD 48
93 #define KPF_SLOB_FREE 49
94 #define KPF_SLUB_FROZEN 50
95 #define KPF_SLUB_DEBUG 51
97 #define KPF_ALL_BITS ((uint64_t)~0ULL)
98 #define KPF_HACKERS_BITS (0xffffULL << 32)
99 #define KPF_OVERLOADED_BITS (0xffffULL << 48)
100 #define BIT(name) (1ULL << KPF_##name)
101 #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
103 static char *page_flag_names
[] = {
104 [KPF_LOCKED
] = "L:locked",
105 [KPF_ERROR
] = "E:error",
106 [KPF_REFERENCED
] = "R:referenced",
107 [KPF_UPTODATE
] = "U:uptodate",
108 [KPF_DIRTY
] = "D:dirty",
110 [KPF_ACTIVE
] = "A:active",
111 [KPF_SLAB
] = "S:slab",
112 [KPF_WRITEBACK
] = "W:writeback",
113 [KPF_RECLAIM
] = "I:reclaim",
114 [KPF_BUDDY
] = "B:buddy",
116 [KPF_MMAP
] = "M:mmap",
117 [KPF_ANON
] = "a:anonymous",
118 [KPF_SWAPCACHE
] = "s:swapcache",
119 [KPF_SWAPBACKED
] = "b:swapbacked",
120 [KPF_COMPOUND_HEAD
] = "H:compound_head",
121 [KPF_COMPOUND_TAIL
] = "T:compound_tail",
122 [KPF_HUGE
] = "G:huge",
123 [KPF_UNEVICTABLE
] = "u:unevictable",
124 [KPF_HWPOISON
] = "X:hwpoison",
125 [KPF_NOPAGE
] = "n:nopage",
128 [KPF_RESERVED
] = "r:reserved",
129 [KPF_MLOCKED
] = "m:mlocked",
130 [KPF_MAPPEDTODISK
] = "d:mappedtodisk",
131 [KPF_PRIVATE
] = "P:private",
132 [KPF_PRIVATE_2
] = "p:private_2",
133 [KPF_OWNER_PRIVATE
] = "O:owner_private",
134 [KPF_ARCH
] = "h:arch",
135 [KPF_UNCACHED
] = "c:uncached",
137 [KPF_READAHEAD
] = "I:readahead",
138 [KPF_SLOB_FREE
] = "P:slob_free",
139 [KPF_SLUB_FROZEN
] = "A:slub_frozen",
140 [KPF_SLUB_DEBUG
] = "E:slub_debug",
148 static int opt_raw
; /* for kernel developers */
149 static int opt_list
; /* list pages (in ranges) */
150 static int opt_no_summary
; /* don't show summary */
151 static pid_t opt_pid
; /* process to walk */
153 #define MAX_ADDR_RANGES 1024
154 static int nr_addr_ranges
;
155 static unsigned long opt_offset
[MAX_ADDR_RANGES
];
156 static unsigned long opt_size
[MAX_ADDR_RANGES
];
158 #define MAX_VMAS 10240
160 static unsigned long pg_start
[MAX_VMAS
];
161 static unsigned long pg_end
[MAX_VMAS
];
163 #define MAX_BIT_FILTERS 64
164 static int nr_bit_filters
;
165 static uint64_t opt_mask
[MAX_BIT_FILTERS
];
166 static uint64_t opt_bits
[MAX_BIT_FILTERS
];
168 static int page_size
;
170 static int pagemap_fd
;
171 static int kpageflags_fd
;
173 static int opt_hwpoison
;
174 static int opt_unpoison
;
176 static char *hwpoison_debug_fs
= "/debug/hwpoison";
177 static int hwpoison_inject_fd
;
178 static int hwpoison_forget_fd
;
180 #define HASH_SHIFT 13
181 #define HASH_SIZE (1 << HASH_SHIFT)
182 #define HASH_MASK (HASH_SIZE - 1)
183 #define HASH_KEY(flags) (flags & HASH_MASK)
185 static unsigned long total_pages
;
186 static unsigned long nr_pages
[HASH_SIZE
];
187 static uint64_t page_flags
[HASH_SIZE
];
194 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
196 #define min_t(type, x, y) ({ \
199 __min1 < __min2 ? __min1 : __min2; })
201 #define max_t(type, x, y) ({ \
204 __max1 > __max2 ? __max1 : __max2; })
206 static unsigned long pages2mb(unsigned long pages
)
208 return (pages
* page_size
) >> 20;
211 static void fatal(const char *x
, ...)
216 vfprintf(stderr
, x
, ap
);
221 static int checked_open(const char *pathname
, int flags
)
223 int fd
= open(pathname
, flags
);
234 * pagemap/kpageflags routines
237 static unsigned long do_u64_read(int fd
, char *name
,
244 if (index
> ULONG_MAX
/ 8)
245 fatal("index overflow: %lu\n", index
);
247 if (lseek(fd
, index
* 8, SEEK_SET
) < 0) {
252 bytes
= read(fd
, buf
, count
* 8);
258 fatal("partial read: %lu bytes\n", bytes
);
263 static unsigned long kpageflags_read(uint64_t *buf
,
267 return do_u64_read(kpageflags_fd
, PROC_KPAGEFLAGS
, buf
, index
, pages
);
270 static unsigned long pagemap_read(uint64_t *buf
,
274 return do_u64_read(pagemap_fd
, "/proc/pid/pagemap", buf
, index
, pages
);
277 static unsigned long pagemap_pfn(uint64_t val
)
281 if (val
& PM_PRESENT
)
282 pfn
= PM_PFRAME(val
);
294 static char *page_flag_name(uint64_t flags
)
300 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
301 present
= (flags
>> i
) & 1;
302 if (!page_flag_names
[i
]) {
304 fatal("unknown flag bit %d\n", i
);
307 buf
[j
++] = present
? page_flag_names
[i
][0] : '_';
313 static char *page_flag_longname(uint64_t flags
)
315 static char buf
[1024];
318 for (i
= 0, n
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
319 if (!page_flag_names
[i
])
321 if ((flags
>> i
) & 1)
322 n
+= snprintf(buf
+ n
, sizeof(buf
) - n
, "%s,",
323 page_flag_names
[i
] + 2);
334 * page list and summary
337 static void show_page_range(unsigned long voffset
,
338 unsigned long offset
, uint64_t flags
)
340 static uint64_t flags0
;
341 static unsigned long voff
;
342 static unsigned long index
;
343 static unsigned long count
;
345 if (flags
== flags0
&& offset
== index
+ count
&&
346 (!opt_pid
|| voffset
== voff
+ count
)) {
353 printf("%lx\t", voff
);
354 printf("%lx\t%lx\t%s\n",
355 index
, count
, page_flag_name(flags0
));
364 static void show_page(unsigned long voffset
,
365 unsigned long offset
, uint64_t flags
)
368 printf("%lx\t", voffset
);
369 printf("%lx\t%s\n", offset
, page_flag_name(flags
));
372 static void show_summary(void)
376 printf(" flags\tpage-count MB"
377 " symbolic-flags\t\t\tlong-symbolic-flags\n");
379 for (i
= 0; i
< ARRAY_SIZE(nr_pages
); i
++) {
381 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
382 (unsigned long long)page_flags
[i
],
384 pages2mb(nr_pages
[i
]),
385 page_flag_name(page_flags
[i
]),
386 page_flag_longname(page_flags
[i
]));
389 printf(" total\t%10lu %8lu\n",
390 total_pages
, pages2mb(total_pages
));
398 static int bit_mask_ok(uint64_t flags
)
402 for (i
= 0; i
< nr_bit_filters
; i
++) {
403 if (opt_bits
[i
] == KPF_ALL_BITS
) {
404 if ((flags
& opt_mask
[i
]) == 0)
407 if ((flags
& opt_mask
[i
]) != opt_bits
[i
])
415 static uint64_t expand_overloaded_flags(uint64_t flags
)
417 /* SLOB/SLUB overload several page flags */
418 if (flags
& BIT(SLAB
)) {
419 if (flags
& BIT(PRIVATE
))
420 flags
^= BIT(PRIVATE
) | BIT(SLOB_FREE
);
421 if (flags
& BIT(ACTIVE
))
422 flags
^= BIT(ACTIVE
) | BIT(SLUB_FROZEN
);
423 if (flags
& BIT(ERROR
))
424 flags
^= BIT(ERROR
) | BIT(SLUB_DEBUG
);
427 /* PG_reclaim is overloaded as PG_readahead in the read path */
428 if ((flags
& (BIT(RECLAIM
) | BIT(WRITEBACK
))) == BIT(RECLAIM
))
429 flags
^= BIT(RECLAIM
) | BIT(READAHEAD
);
434 static uint64_t well_known_flags(uint64_t flags
)
436 /* hide flags intended only for kernel hacker */
437 flags
&= ~KPF_HACKERS_BITS
;
439 /* hide non-hugeTLB compound pages */
440 if ((flags
& BITS_COMPOUND
) && !(flags
& BIT(HUGE
)))
441 flags
&= ~BITS_COMPOUND
;
446 static uint64_t kpageflags_flags(uint64_t flags
)
448 flags
= expand_overloaded_flags(flags
);
451 flags
= well_known_flags(flags
);
460 static void prepare_hwpoison_fd(void)
464 if (opt_hwpoison
&& !hwpoison_inject_fd
) {
465 sprintf(buf
, "%s/corrupt-pfn", hwpoison_debug_fs
);
466 hwpoison_inject_fd
= checked_open(buf
, O_WRONLY
);
469 if (opt_unpoison
&& !hwpoison_forget_fd
) {
470 sprintf(buf
, "%s/renew-pfn", hwpoison_debug_fs
);
471 hwpoison_forget_fd
= checked_open(buf
, O_WRONLY
);
475 static int hwpoison_page(unsigned long offset
)
480 len
= sprintf(buf
, "0x%lx\n", offset
);
481 len
= write(hwpoison_inject_fd
, buf
, len
);
483 perror("hwpoison inject");
489 static int unpoison_page(unsigned long offset
)
494 len
= sprintf(buf
, "0x%lx\n", offset
);
495 len
= write(hwpoison_forget_fd
, buf
, len
);
497 perror("hwpoison forget");
507 static int hash_slot(uint64_t flags
)
509 int k
= HASH_KEY(flags
);
512 /* Explicitly reserve slot 0 for flags 0: the following logic
513 * cannot distinguish an unoccupied slot from slot (flags==0).
518 /* search through the remaining (HASH_SIZE-1) slots */
519 for (i
= 1; i
< ARRAY_SIZE(page_flags
); i
++, k
++) {
520 if (!k
|| k
>= ARRAY_SIZE(page_flags
))
522 if (page_flags
[k
] == 0) {
523 page_flags
[k
] = flags
;
526 if (page_flags
[k
] == flags
)
530 fatal("hash table full: bump up HASH_SHIFT?\n");
534 static void add_page(unsigned long voffset
,
535 unsigned long offset
, uint64_t flags
)
537 flags
= kpageflags_flags(flags
);
539 if (!bit_mask_ok(flags
))
543 hwpoison_page(offset
);
545 unpoison_page(offset
);
548 show_page_range(voffset
, offset
, flags
);
549 else if (opt_list
== 2)
550 show_page(voffset
, offset
, flags
);
552 nr_pages
[hash_slot(flags
)]++;
556 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
557 static void walk_pfn(unsigned long voffset
,
561 uint64_t buf
[KPAGEFLAGS_BATCH
];
567 batch
= min_t(unsigned long, count
, KPAGEFLAGS_BATCH
);
568 pages
= kpageflags_read(buf
, index
, batch
);
572 for (i
= 0; i
< pages
; i
++)
573 add_page(voffset
+ i
, index
+ i
, buf
[i
]);
580 #define PAGEMAP_BATCH (64 << 10)
581 static void walk_vma(unsigned long index
, unsigned long count
)
583 uint64_t buf
[PAGEMAP_BATCH
];
590 batch
= min_t(unsigned long, count
, PAGEMAP_BATCH
);
591 pages
= pagemap_read(buf
, index
, batch
);
595 for (i
= 0; i
< pages
; i
++) {
596 pfn
= pagemap_pfn(buf
[i
]);
598 walk_pfn(index
+ i
, pfn
, 1);
606 static void walk_task(unsigned long index
, unsigned long count
)
608 const unsigned long end
= index
+ count
;
612 while (index
< end
) {
614 while (pg_end
[i
] <= index
)
617 if (pg_start
[i
] >= end
)
620 start
= max_t(unsigned long, pg_start
[i
], index
);
621 index
= min_t(unsigned long, pg_end
[i
], end
);
623 assert(start
< index
);
624 walk_vma(start
, index
- start
);
628 static void add_addr_range(unsigned long offset
, unsigned long size
)
630 if (nr_addr_ranges
>= MAX_ADDR_RANGES
)
631 fatal("too many addr ranges\n");
633 opt_offset
[nr_addr_ranges
] = offset
;
634 opt_size
[nr_addr_ranges
] = min_t(unsigned long, size
, ULONG_MAX
-offset
);
638 static void walk_addr_ranges(void)
642 kpageflags_fd
= checked_open(PROC_KPAGEFLAGS
, O_RDONLY
);
645 add_addr_range(0, ULONG_MAX
);
647 for (i
= 0; i
< nr_addr_ranges
; i
++)
649 walk_pfn(0, opt_offset
[i
], opt_size
[i
]);
651 walk_task(opt_offset
[i
], opt_size
[i
]);
653 close(kpageflags_fd
);
661 static const char *page_flag_type(uint64_t flag
)
663 if (flag
& KPF_HACKERS_BITS
)
665 if (flag
& KPF_OVERLOADED_BITS
)
670 static void usage(void)
675 "page-types [options]\n"
676 " -r|--raw Raw mode, for kernel developers\n"
677 " -a|--addr addr-spec Walk a range of pages\n"
678 " -b|--bits bits-spec Walk pages with specified bits\n"
679 " -p|--pid pid Walk process address space\n"
680 #if 0 /* planned features */
681 " -f|--file filename Walk file address space\n"
683 " -l|--list Show page details in ranges\n"
684 " -L|--list-each Show page details one by one\n"
685 " -N|--no-summary Don't show summay info\n"
686 " -X|--hwpoison hwpoison pages\n"
687 " -x|--unpoison unpoison pages\n"
688 " -h|--help Show this usage message\n"
690 " N one page at offset N (unit: pages)\n"
691 " N+M pages range from N to N+M-1\n"
692 " N,M pages range from N to M-1\n"
693 " N, pages range from N to end\n"
694 " ,M pages range from 0 to M-1\n"
696 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
697 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
698 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
699 " =bit1,bit2 flags == (bit1|bit2)\n"
703 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
704 if (!page_flag_names
[i
])
706 printf("%16s%s", page_flag_names
[i
] + 2,
707 page_flag_type(1ULL << i
));
714 "(r) raw mode bits (o) overloaded bits\n");
717 static unsigned long long parse_number(const char *str
)
719 unsigned long long n
;
721 n
= strtoll(str
, NULL
, 0);
723 if (n
== 0 && str
[0] != '0')
724 fatal("invalid name or number: %s\n", str
);
729 static void parse_pid(const char *str
)
734 opt_pid
= parse_number(str
);
736 sprintf(buf
, "/proc/%d/pagemap", opt_pid
);
737 pagemap_fd
= checked_open(buf
, O_RDONLY
);
739 sprintf(buf
, "/proc/%d/maps", opt_pid
);
740 file
= fopen(buf
, "r");
746 while (fgets(buf
, sizeof(buf
), file
) != NULL
) {
747 unsigned long vm_start
;
748 unsigned long vm_end
;
749 unsigned long long pgoff
;
755 n
= sscanf(buf
, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
763 fprintf(stderr
, "unexpected line: %s\n", buf
);
766 pg_start
[nr_vmas
] = vm_start
/ page_size
;
767 pg_end
[nr_vmas
] = vm_end
/ page_size
;
768 if (++nr_vmas
>= MAX_VMAS
) {
769 fprintf(stderr
, "too many VMAs\n");
776 static void parse_file(const char *name
)
780 static void parse_addr_range(const char *optarg
)
782 unsigned long offset
;
786 p
= strchr(optarg
, ',');
788 p
= strchr(optarg
, '+');
792 size
= parse_number(p
+ 1);
794 offset
= parse_number(optarg
);
798 size
= parse_number(p
+ 1);
801 fatal("invalid range: %lu,%lu\n",
807 offset
= parse_number(optarg
);
811 add_addr_range(offset
, size
);
814 static void add_bits_filter(uint64_t mask
, uint64_t bits
)
816 if (nr_bit_filters
>= MAX_BIT_FILTERS
)
817 fatal("too much bit filters\n");
819 opt_mask
[nr_bit_filters
] = mask
;
820 opt_bits
[nr_bit_filters
] = bits
;
824 static uint64_t parse_flag_name(const char *str
, int len
)
831 if (len
<= 8 && !strncmp(str
, "compound", len
))
832 return BITS_COMPOUND
;
834 for (i
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
835 if (!page_flag_names
[i
])
837 if (!strncmp(str
, page_flag_names
[i
] + 2, len
))
841 return parse_number(str
);
844 static uint64_t parse_flag_names(const char *str
, int all
)
850 if (*p
== ',' || *p
== '=' || *p
== '\0') {
851 if ((*str
!= '~') || (*str
== '~' && all
&& *++str
))
852 flags
|= parse_flag_name(str
, p
- str
);
863 static void parse_bits_mask(const char *optarg
)
869 p
= strchr(optarg
, '=');
872 bits
= parse_flag_names(p
+ 1, 0);
874 mask
= parse_flag_names(optarg
, 0);
875 bits
= parse_flag_names(p
+ 1, 0);
876 } else if (strchr(optarg
, '~')) {
877 mask
= parse_flag_names(optarg
, 1);
878 bits
= parse_flag_names(optarg
, 0);
880 mask
= parse_flag_names(optarg
, 0);
884 add_bits_filter(mask
, bits
);
888 static struct option opts
[] = {
889 { "raw" , 0, NULL
, 'r' },
890 { "pid" , 1, NULL
, 'p' },
891 { "file" , 1, NULL
, 'f' },
892 { "addr" , 1, NULL
, 'a' },
893 { "bits" , 1, NULL
, 'b' },
894 { "list" , 0, NULL
, 'l' },
895 { "list-each" , 0, NULL
, 'L' },
896 { "no-summary", 0, NULL
, 'N' },
897 { "hwpoison" , 0, NULL
, 'X' },
898 { "unpoison" , 0, NULL
, 'x' },
899 { "help" , 0, NULL
, 'h' },
900 { NULL
, 0, NULL
, 0 }
903 int main(int argc
, char *argv
[])
907 page_size
= getpagesize();
909 while ((c
= getopt_long(argc
, argv
,
910 "rp:f:a:b:lLNXxh", opts
, NULL
)) != -1) {
922 parse_addr_range(optarg
);
925 parse_bits_mask(optarg
);
938 prepare_hwpoison_fd();
942 prepare_hwpoison_fd();
953 if (opt_list
&& opt_pid
)
956 printf("offset\tlen\tflags\n");
958 printf("offset\tflags\n");
963 show_page_range(0, 0, 0); /* drain the buffer */