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
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 _LARGEFILE64_SOURCE
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/fcntl.h>
38 * pagemap kernel ABI bits
41 #define PM_ENTRY_BYTES sizeof(uint64_t)
42 #define PM_STATUS_BITS 3
43 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
44 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
45 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
46 #define PM_PSHIFT_BITS 6
47 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
48 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
49 #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
50 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
51 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
53 #define PM_PRESENT PM_STATUS(4LL)
54 #define PM_SWAP PM_STATUS(2LL)
62 #define PROC_KPAGEFLAGS "/proc/kpageflags"
64 /* copied from kpageflags_read() */
67 #define KPF_REFERENCED 2
68 #define KPF_UPTODATE 3
73 #define KPF_WRITEBACK 8
77 /* [11-20] new additions in 2.6.31 */
80 #define KPF_SWAPCACHE 13
81 #define KPF_SWAPBACKED 14
82 #define KPF_COMPOUND_HEAD 15
83 #define KPF_COMPOUND_TAIL 16
85 #define KPF_UNEVICTABLE 18
86 #define KPF_HWPOISON 19
90 /* [32-] kernel hacking assistances */
91 #define KPF_RESERVED 32
92 #define KPF_MLOCKED 33
93 #define KPF_MAPPEDTODISK 34
94 #define KPF_PRIVATE 35
95 #define KPF_PRIVATE_2 36
96 #define KPF_OWNER_PRIVATE 37
98 #define KPF_UNCACHED 39
100 /* [48-] take some arbitrary free slots for expanding overloaded flags
101 * not part of kernel API
103 #define KPF_READAHEAD 48
104 #define KPF_SLOB_FREE 49
105 #define KPF_SLUB_FROZEN 50
106 #define KPF_SLUB_DEBUG 51
108 #define KPF_ALL_BITS ((uint64_t)~0ULL)
109 #define KPF_HACKERS_BITS (0xffffULL << 32)
110 #define KPF_OVERLOADED_BITS (0xffffULL << 48)
111 #define BIT(name) (1ULL << KPF_##name)
112 #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
114 static const char *page_flag_names
[] = {
115 [KPF_LOCKED
] = "L:locked",
116 [KPF_ERROR
] = "E:error",
117 [KPF_REFERENCED
] = "R:referenced",
118 [KPF_UPTODATE
] = "U:uptodate",
119 [KPF_DIRTY
] = "D:dirty",
121 [KPF_ACTIVE
] = "A:active",
122 [KPF_SLAB
] = "S:slab",
123 [KPF_WRITEBACK
] = "W:writeback",
124 [KPF_RECLAIM
] = "I:reclaim",
125 [KPF_BUDDY
] = "B:buddy",
127 [KPF_MMAP
] = "M:mmap",
128 [KPF_ANON
] = "a:anonymous",
129 [KPF_SWAPCACHE
] = "s:swapcache",
130 [KPF_SWAPBACKED
] = "b:swapbacked",
131 [KPF_COMPOUND_HEAD
] = "H:compound_head",
132 [KPF_COMPOUND_TAIL
] = "T:compound_tail",
133 [KPF_HUGE
] = "G:huge",
134 [KPF_UNEVICTABLE
] = "u:unevictable",
135 [KPF_HWPOISON
] = "X:hwpoison",
136 [KPF_NOPAGE
] = "n:nopage",
139 [KPF_RESERVED
] = "r:reserved",
140 [KPF_MLOCKED
] = "m:mlocked",
141 [KPF_MAPPEDTODISK
] = "d:mappedtodisk",
142 [KPF_PRIVATE
] = "P:private",
143 [KPF_PRIVATE_2
] = "p:private_2",
144 [KPF_OWNER_PRIVATE
] = "O:owner_private",
145 [KPF_ARCH
] = "h:arch",
146 [KPF_UNCACHED
] = "c:uncached",
148 [KPF_READAHEAD
] = "I:readahead",
149 [KPF_SLOB_FREE
] = "P:slob_free",
150 [KPF_SLUB_FROZEN
] = "A:slub_frozen",
151 [KPF_SLUB_DEBUG
] = "E:slub_debug",
159 static int opt_raw
; /* for kernel developers */
160 static int opt_list
; /* list pages (in ranges) */
161 static int opt_no_summary
; /* don't show summary */
162 static pid_t opt_pid
; /* process to walk */
164 #define MAX_ADDR_RANGES 1024
165 static int nr_addr_ranges
;
166 static unsigned long opt_offset
[MAX_ADDR_RANGES
];
167 static unsigned long opt_size
[MAX_ADDR_RANGES
];
169 #define MAX_VMAS 10240
171 static unsigned long pg_start
[MAX_VMAS
];
172 static unsigned long pg_end
[MAX_VMAS
];
174 #define MAX_BIT_FILTERS 64
175 static int nr_bit_filters
;
176 static uint64_t opt_mask
[MAX_BIT_FILTERS
];
177 static uint64_t opt_bits
[MAX_BIT_FILTERS
];
179 static int page_size
;
181 static int pagemap_fd
;
182 static int kpageflags_fd
;
184 static int opt_hwpoison
;
185 static int opt_unpoison
;
187 static const char hwpoison_debug_fs
[] = "/debug/hwpoison";
188 static int hwpoison_inject_fd
;
189 static int hwpoison_forget_fd
;
191 #define HASH_SHIFT 13
192 #define HASH_SIZE (1 << HASH_SHIFT)
193 #define HASH_MASK (HASH_SIZE - 1)
194 #define HASH_KEY(flags) (flags & HASH_MASK)
196 static unsigned long total_pages
;
197 static unsigned long nr_pages
[HASH_SIZE
];
198 static uint64_t page_flags
[HASH_SIZE
];
205 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
207 #define min_t(type, x, y) ({ \
210 __min1 < __min2 ? __min1 : __min2; })
212 #define max_t(type, x, y) ({ \
215 __max1 > __max2 ? __max1 : __max2; })
217 static unsigned long pages2mb(unsigned long pages
)
219 return (pages
* page_size
) >> 20;
222 static void fatal(const char *x
, ...)
227 vfprintf(stderr
, x
, ap
);
232 static int checked_open(const char *pathname
, int flags
)
234 int fd
= open(pathname
, flags
);
245 * pagemap/kpageflags routines
248 static unsigned long do_u64_read(int fd
, char *name
,
255 if (index
> ULONG_MAX
/ 8)
256 fatal("index overflow: %lu\n", index
);
258 if (lseek(fd
, index
* 8, SEEK_SET
) < 0) {
263 bytes
= read(fd
, buf
, count
* 8);
269 fatal("partial read: %lu bytes\n", bytes
);
274 static unsigned long kpageflags_read(uint64_t *buf
,
278 return do_u64_read(kpageflags_fd
, PROC_KPAGEFLAGS
, buf
, index
, pages
);
281 static unsigned long pagemap_read(uint64_t *buf
,
285 return do_u64_read(pagemap_fd
, "/proc/pid/pagemap", buf
, index
, pages
);
288 static unsigned long pagemap_pfn(uint64_t val
)
292 if (val
& PM_PRESENT
)
293 pfn
= PM_PFRAME(val
);
305 static char *page_flag_name(uint64_t flags
)
311 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
312 present
= (flags
>> i
) & 1;
313 if (!page_flag_names
[i
]) {
315 fatal("unknown flag bit %d\n", i
);
318 buf
[j
++] = present
? page_flag_names
[i
][0] : '_';
324 static char *page_flag_longname(uint64_t flags
)
326 static char buf
[1024];
329 for (i
= 0, n
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
330 if (!page_flag_names
[i
])
332 if ((flags
>> i
) & 1)
333 n
+= snprintf(buf
+ n
, sizeof(buf
) - n
, "%s,",
334 page_flag_names
[i
] + 2);
345 * page list and summary
348 static void show_page_range(unsigned long voffset
,
349 unsigned long offset
, uint64_t flags
)
351 static uint64_t flags0
;
352 static unsigned long voff
;
353 static unsigned long index
;
354 static unsigned long count
;
356 if (flags
== flags0
&& offset
== index
+ count
&&
357 (!opt_pid
|| voffset
== voff
+ count
)) {
364 printf("%lx\t", voff
);
365 printf("%lx\t%lx\t%s\n",
366 index
, count
, page_flag_name(flags0
));
375 static void show_page(unsigned long voffset
,
376 unsigned long offset
, uint64_t flags
)
379 printf("%lx\t", voffset
);
380 printf("%lx\t%s\n", offset
, page_flag_name(flags
));
383 static void show_summary(void)
387 printf(" flags\tpage-count MB"
388 " symbolic-flags\t\t\tlong-symbolic-flags\n");
390 for (i
= 0; i
< ARRAY_SIZE(nr_pages
); i
++) {
392 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
393 (unsigned long long)page_flags
[i
],
395 pages2mb(nr_pages
[i
]),
396 page_flag_name(page_flags
[i
]),
397 page_flag_longname(page_flags
[i
]));
400 printf(" total\t%10lu %8lu\n",
401 total_pages
, pages2mb(total_pages
));
409 static int bit_mask_ok(uint64_t flags
)
413 for (i
= 0; i
< nr_bit_filters
; i
++) {
414 if (opt_bits
[i
] == KPF_ALL_BITS
) {
415 if ((flags
& opt_mask
[i
]) == 0)
418 if ((flags
& opt_mask
[i
]) != opt_bits
[i
])
426 static uint64_t expand_overloaded_flags(uint64_t flags
)
428 /* SLOB/SLUB overload several page flags */
429 if (flags
& BIT(SLAB
)) {
430 if (flags
& BIT(PRIVATE
))
431 flags
^= BIT(PRIVATE
) | BIT(SLOB_FREE
);
432 if (flags
& BIT(ACTIVE
))
433 flags
^= BIT(ACTIVE
) | BIT(SLUB_FROZEN
);
434 if (flags
& BIT(ERROR
))
435 flags
^= BIT(ERROR
) | BIT(SLUB_DEBUG
);
438 /* PG_reclaim is overloaded as PG_readahead in the read path */
439 if ((flags
& (BIT(RECLAIM
) | BIT(WRITEBACK
))) == BIT(RECLAIM
))
440 flags
^= BIT(RECLAIM
) | BIT(READAHEAD
);
445 static uint64_t well_known_flags(uint64_t flags
)
447 /* hide flags intended only for kernel hacker */
448 flags
&= ~KPF_HACKERS_BITS
;
450 /* hide non-hugeTLB compound pages */
451 if ((flags
& BITS_COMPOUND
) && !(flags
& BIT(HUGE
)))
452 flags
&= ~BITS_COMPOUND
;
457 static uint64_t kpageflags_flags(uint64_t flags
)
459 flags
= expand_overloaded_flags(flags
);
462 flags
= well_known_flags(flags
);
471 static void prepare_hwpoison_fd(void)
475 if (opt_hwpoison
&& !hwpoison_inject_fd
) {
476 sprintf(buf
, "%s/corrupt-pfn", hwpoison_debug_fs
);
477 hwpoison_inject_fd
= checked_open(buf
, O_WRONLY
);
480 if (opt_unpoison
&& !hwpoison_forget_fd
) {
481 sprintf(buf
, "%s/renew-pfn", hwpoison_debug_fs
);
482 hwpoison_forget_fd
= checked_open(buf
, O_WRONLY
);
486 static int hwpoison_page(unsigned long offset
)
491 len
= sprintf(buf
, "0x%lx\n", offset
);
492 len
= write(hwpoison_inject_fd
, buf
, len
);
494 perror("hwpoison inject");
500 static int unpoison_page(unsigned long offset
)
505 len
= sprintf(buf
, "0x%lx\n", offset
);
506 len
= write(hwpoison_forget_fd
, buf
, len
);
508 perror("hwpoison forget");
518 static int hash_slot(uint64_t flags
)
520 int k
= HASH_KEY(flags
);
523 /* Explicitly reserve slot 0 for flags 0: the following logic
524 * cannot distinguish an unoccupied slot from slot (flags==0).
529 /* search through the remaining (HASH_SIZE-1) slots */
530 for (i
= 1; i
< ARRAY_SIZE(page_flags
); i
++, k
++) {
531 if (!k
|| k
>= ARRAY_SIZE(page_flags
))
533 if (page_flags
[k
] == 0) {
534 page_flags
[k
] = flags
;
537 if (page_flags
[k
] == flags
)
541 fatal("hash table full: bump up HASH_SHIFT?\n");
545 static void add_page(unsigned long voffset
,
546 unsigned long offset
, uint64_t flags
)
548 flags
= kpageflags_flags(flags
);
550 if (!bit_mask_ok(flags
))
554 hwpoison_page(offset
);
556 unpoison_page(offset
);
559 show_page_range(voffset
, offset
, flags
);
560 else if (opt_list
== 2)
561 show_page(voffset
, offset
, flags
);
563 nr_pages
[hash_slot(flags
)]++;
567 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
568 static void walk_pfn(unsigned long voffset
,
572 uint64_t buf
[KPAGEFLAGS_BATCH
];
578 batch
= min_t(unsigned long, count
, KPAGEFLAGS_BATCH
);
579 pages
= kpageflags_read(buf
, index
, batch
);
583 for (i
= 0; i
< pages
; i
++)
584 add_page(voffset
+ i
, index
+ i
, buf
[i
]);
591 #define PAGEMAP_BATCH (64 << 10)
592 static void walk_vma(unsigned long index
, unsigned long count
)
594 uint64_t buf
[PAGEMAP_BATCH
];
601 batch
= min_t(unsigned long, count
, PAGEMAP_BATCH
);
602 pages
= pagemap_read(buf
, index
, batch
);
606 for (i
= 0; i
< pages
; i
++) {
607 pfn
= pagemap_pfn(buf
[i
]);
609 walk_pfn(index
+ i
, pfn
, 1);
617 static void walk_task(unsigned long index
, unsigned long count
)
619 const unsigned long end
= index
+ count
;
623 while (index
< end
) {
625 while (pg_end
[i
] <= index
)
628 if (pg_start
[i
] >= end
)
631 start
= max_t(unsigned long, pg_start
[i
], index
);
632 index
= min_t(unsigned long, pg_end
[i
], end
);
634 assert(start
< index
);
635 walk_vma(start
, index
- start
);
639 static void add_addr_range(unsigned long offset
, unsigned long size
)
641 if (nr_addr_ranges
>= MAX_ADDR_RANGES
)
642 fatal("too many addr ranges\n");
644 opt_offset
[nr_addr_ranges
] = offset
;
645 opt_size
[nr_addr_ranges
] = min_t(unsigned long, size
, ULONG_MAX
-offset
);
649 static void walk_addr_ranges(void)
653 kpageflags_fd
= checked_open(PROC_KPAGEFLAGS
, O_RDONLY
);
656 add_addr_range(0, ULONG_MAX
);
658 for (i
= 0; i
< nr_addr_ranges
; i
++)
660 walk_pfn(0, opt_offset
[i
], opt_size
[i
]);
662 walk_task(opt_offset
[i
], opt_size
[i
]);
664 close(kpageflags_fd
);
672 static const char *page_flag_type(uint64_t flag
)
674 if (flag
& KPF_HACKERS_BITS
)
676 if (flag
& KPF_OVERLOADED_BITS
)
681 static void usage(void)
686 "page-types [options]\n"
687 " -r|--raw Raw mode, for kernel developers\n"
688 " -d|--describe flags Describe flags\n"
689 " -a|--addr addr-spec Walk a range of pages\n"
690 " -b|--bits bits-spec Walk pages with specified bits\n"
691 " -p|--pid pid Walk process address space\n"
692 #if 0 /* planned features */
693 " -f|--file filename Walk file address space\n"
695 " -l|--list Show page details in ranges\n"
696 " -L|--list-each Show page details one by one\n"
697 " -N|--no-summary Don't show summay info\n"
698 " -X|--hwpoison hwpoison pages\n"
699 " -x|--unpoison unpoison pages\n"
700 " -h|--help Show this usage message\n"
702 " 0x10 bitfield format, e.g.\n"
703 " anon bit-name, e.g.\n"
704 " 0x10,anon comma-separated list, e.g.\n"
706 " N one page at offset N (unit: pages)\n"
707 " N+M pages range from N to N+M-1\n"
708 " N,M pages range from N to M-1\n"
709 " N, pages range from N to end\n"
710 " ,M pages range from 0 to M-1\n"
712 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
713 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
714 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
715 " =bit1,bit2 flags == (bit1|bit2)\n"
719 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
720 if (!page_flag_names
[i
])
722 printf("%16s%s", page_flag_names
[i
] + 2,
723 page_flag_type(1ULL << i
));
730 "(r) raw mode bits (o) overloaded bits\n");
733 static unsigned long long parse_number(const char *str
)
735 unsigned long long n
;
737 n
= strtoll(str
, NULL
, 0);
739 if (n
== 0 && str
[0] != '0')
740 fatal("invalid name or number: %s\n", str
);
745 static void parse_pid(const char *str
)
750 opt_pid
= parse_number(str
);
752 sprintf(buf
, "/proc/%d/pagemap", opt_pid
);
753 pagemap_fd
= checked_open(buf
, O_RDONLY
);
755 sprintf(buf
, "/proc/%d/maps", opt_pid
);
756 file
= fopen(buf
, "r");
762 while (fgets(buf
, sizeof(buf
), file
) != NULL
) {
763 unsigned long vm_start
;
764 unsigned long vm_end
;
765 unsigned long long pgoff
;
771 n
= sscanf(buf
, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
779 fprintf(stderr
, "unexpected line: %s\n", buf
);
782 pg_start
[nr_vmas
] = vm_start
/ page_size
;
783 pg_end
[nr_vmas
] = vm_end
/ page_size
;
784 if (++nr_vmas
>= MAX_VMAS
) {
785 fprintf(stderr
, "too many VMAs\n");
792 static void parse_file(const char *name
)
796 static void parse_addr_range(const char *optarg
)
798 unsigned long offset
;
802 p
= strchr(optarg
, ',');
804 p
= strchr(optarg
, '+');
808 size
= parse_number(p
+ 1);
810 offset
= parse_number(optarg
);
814 size
= parse_number(p
+ 1);
817 fatal("invalid range: %lu,%lu\n",
823 offset
= parse_number(optarg
);
827 add_addr_range(offset
, size
);
830 static void add_bits_filter(uint64_t mask
, uint64_t bits
)
832 if (nr_bit_filters
>= MAX_BIT_FILTERS
)
833 fatal("too much bit filters\n");
835 opt_mask
[nr_bit_filters
] = mask
;
836 opt_bits
[nr_bit_filters
] = bits
;
840 static uint64_t parse_flag_name(const char *str
, int len
)
847 if (len
<= 8 && !strncmp(str
, "compound", len
))
848 return BITS_COMPOUND
;
850 for (i
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
851 if (!page_flag_names
[i
])
853 if (!strncmp(str
, page_flag_names
[i
] + 2, len
))
857 return parse_number(str
);
860 static uint64_t parse_flag_names(const char *str
, int all
)
866 if (*p
== ',' || *p
== '=' || *p
== '\0') {
867 if ((*str
!= '~') || (*str
== '~' && all
&& *++str
))
868 flags
|= parse_flag_name(str
, p
- str
);
879 static void parse_bits_mask(const char *optarg
)
885 p
= strchr(optarg
, '=');
888 bits
= parse_flag_names(p
+ 1, 0);
890 mask
= parse_flag_names(optarg
, 0);
891 bits
= parse_flag_names(p
+ 1, 0);
892 } else if (strchr(optarg
, '~')) {
893 mask
= parse_flag_names(optarg
, 1);
894 bits
= parse_flag_names(optarg
, 0);
896 mask
= parse_flag_names(optarg
, 0);
900 add_bits_filter(mask
, bits
);
903 static void describe_flags(const char *optarg
)
905 uint64_t flags
= parse_flag_names(optarg
, 0);
907 printf("0x%016llx\t%s\t%s\n",
908 (unsigned long long)flags
,
909 page_flag_name(flags
),
910 page_flag_longname(flags
));
913 static const struct option opts
[] = {
914 { "raw" , 0, NULL
, 'r' },
915 { "pid" , 1, NULL
, 'p' },
916 { "file" , 1, NULL
, 'f' },
917 { "addr" , 1, NULL
, 'a' },
918 { "bits" , 1, NULL
, 'b' },
919 { "describe" , 1, NULL
, 'd' },
920 { "list" , 0, NULL
, 'l' },
921 { "list-each" , 0, NULL
, 'L' },
922 { "no-summary", 0, NULL
, 'N' },
923 { "hwpoison" , 0, NULL
, 'X' },
924 { "unpoison" , 0, NULL
, 'x' },
925 { "help" , 0, NULL
, 'h' },
926 { NULL
, 0, NULL
, 0 }
929 int main(int argc
, char *argv
[])
933 page_size
= getpagesize();
935 while ((c
= getopt_long(argc
, argv
,
936 "rp:f:a:b:d:lLNXxh", opts
, NULL
)) != -1) {
948 parse_addr_range(optarg
);
951 parse_bits_mask(optarg
);
954 describe_flags(optarg
);
967 prepare_hwpoison_fd();
971 prepare_hwpoison_fd();
982 if (opt_list
&& opt_pid
)
985 printf("offset\tlen\tflags\n");
987 printf("offset\tflags\n");
992 show_page_range(0, 0, 0); /* drain the buffer */