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 _FILE_OFFSET_BITS 64
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>
43 #include "../../include/uapi/linux/magic.h"
44 #include "../../include/uapi/linux/kernel-page-flags.h"
45 #include <api/fs/fs.h>
53 # define STR(x) _STR(x)
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)
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
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
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",
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",
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",
158 static const char * const debugfs_known_mountpoints
[] = {
168 static int opt_raw
; /* for kernel developers */
169 static int opt_list
; /* list pages (in ranges) */
170 static int opt_no_summary
; /* don't show summary */
171 static pid_t opt_pid
; /* process to walk */
172 const char *opt_file
; /* file or directory path */
173 static uint64_t opt_cgroup
; /* cgroup inode */
174 static int opt_list_cgroup
;/* list page cgroup */
175 static const char *opt_kpageflags
;/* kpageflags file to parse */
177 #define MAX_ADDR_RANGES 1024
178 static int nr_addr_ranges
;
179 static unsigned long opt_offset
[MAX_ADDR_RANGES
];
180 static unsigned long opt_size
[MAX_ADDR_RANGES
];
182 #define MAX_VMAS 10240
184 static unsigned long pg_start
[MAX_VMAS
];
185 static unsigned long pg_end
[MAX_VMAS
];
187 #define MAX_BIT_FILTERS 64
188 static int nr_bit_filters
;
189 static uint64_t opt_mask
[MAX_BIT_FILTERS
];
190 static uint64_t opt_bits
[MAX_BIT_FILTERS
];
192 static int page_size
;
194 static int pagemap_fd
;
195 static int kpageflags_fd
;
196 static int kpagecgroup_fd
= -1;
198 static int opt_hwpoison
;
199 static int opt_unpoison
;
201 static const char *hwpoison_debug_fs
;
202 static int hwpoison_inject_fd
;
203 static int hwpoison_forget_fd
;
205 #define HASH_SHIFT 13
206 #define HASH_SIZE (1 << HASH_SHIFT)
207 #define HASH_MASK (HASH_SIZE - 1)
208 #define HASH_KEY(flags) (flags & HASH_MASK)
210 static unsigned long total_pages
;
211 static unsigned long nr_pages
[HASH_SIZE
];
212 static uint64_t page_flags
[HASH_SIZE
];
219 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
221 #define min_t(type, x, y) ({ \
224 __min1 < __min2 ? __min1 : __min2; })
226 #define max_t(type, x, y) ({ \
229 __max1 > __max2 ? __max1 : __max2; })
231 static unsigned long pages2mb(unsigned long pages
)
233 return (pages
* page_size
) >> 20;
236 static void fatal(const char *x
, ...)
241 vfprintf(stderr
, x
, ap
);
246 static int checked_open(const char *pathname
, int flags
)
248 int fd
= open(pathname
, flags
);
259 * pagemap/kpageflags routines
262 static unsigned long do_u64_read(int fd
, const char *name
,
269 if (index
> ULONG_MAX
/ 8)
270 fatal("index overflow: %lu\n", index
);
272 bytes
= pread(fd
, buf
, count
* 8, (off_t
)index
* 8);
278 fatal("partial read: %lu bytes\n", bytes
);
283 static unsigned long kpageflags_read(uint64_t *buf
,
287 return do_u64_read(kpageflags_fd
, opt_kpageflags
, buf
, index
, pages
);
290 static unsigned long kpagecgroup_read(uint64_t *buf
,
294 if (kpagecgroup_fd
< 0)
297 return do_u64_read(kpagecgroup_fd
, opt_kpageflags
, buf
, index
, pages
);
300 static unsigned long pagemap_read(uint64_t *buf
,
304 return do_u64_read(pagemap_fd
, "/proc/pid/pagemap", buf
, index
, pages
);
307 static unsigned long pagemap_pfn(uint64_t val
)
311 if (val
& PM_PRESENT
)
312 pfn
= PM_PFRAME(val
);
319 static unsigned long pagemap_swap_offset(uint64_t val
)
321 return val
& PM_SWAP
? PM_SWAP_OFFSET(val
) : 0;
328 static char *page_flag_name(uint64_t flags
)
334 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
335 present
= (flags
>> i
) & 1;
336 if (!page_flag_names
[i
]) {
338 fatal("unknown flag bit %d\n", i
);
341 buf
[j
++] = present
? page_flag_names
[i
][0] : '_';
347 static char *page_flag_longname(uint64_t flags
)
349 static char buf
[1024];
352 for (i
= 0, n
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
353 if (!page_flag_names
[i
])
355 if ((flags
>> i
) & 1)
356 n
+= snprintf(buf
+ n
, sizeof(buf
) - n
, "%s,",
357 page_flag_names
[i
] + 2);
368 * page list and summary
371 static void show_page_range(unsigned long voffset
, unsigned long offset
,
372 unsigned long size
, uint64_t flags
, uint64_t cgroup
)
374 static uint64_t flags0
;
375 static uint64_t cgroup0
;
376 static unsigned long voff
;
377 static unsigned long index
;
378 static unsigned long count
;
380 if (flags
== flags0
&& cgroup
== cgroup0
&& offset
== index
+ count
&&
381 size
&& voffset
== voff
+ count
) {
388 printf("%lx\t", voff
);
390 printf("%lu\t", voff
);
392 printf("@%llu\t", (unsigned long long)cgroup0
);
393 printf("%lx\t%lx\t%s\n",
394 index
, count
, page_flag_name(flags0
));
404 static void flush_page_range(void)
406 show_page_range(0, 0, 0, 0, 0);
409 static void show_page(unsigned long voffset
, unsigned long offset
,
410 uint64_t flags
, uint64_t cgroup
)
413 printf("%lx\t", voffset
);
415 printf("%lu\t", voffset
);
417 printf("@%llu\t", (unsigned long long)cgroup
);
418 printf("%lx\t%s\n", offset
, page_flag_name(flags
));
421 static void show_summary(void)
425 printf(" flags\tpage-count MB"
426 " symbolic-flags\t\t\tlong-symbolic-flags\n");
428 for (i
= 0; i
< ARRAY_SIZE(nr_pages
); i
++) {
430 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
431 (unsigned long long)page_flags
[i
],
433 pages2mb(nr_pages
[i
]),
434 page_flag_name(page_flags
[i
]),
435 page_flag_longname(page_flags
[i
]));
438 printf(" total\t%10lu %8lu\n",
439 total_pages
, pages2mb(total_pages
));
447 static int bit_mask_ok(uint64_t flags
)
451 for (i
= 0; i
< nr_bit_filters
; i
++) {
452 if (opt_bits
[i
] == KPF_ALL_BITS
) {
453 if ((flags
& opt_mask
[i
]) == 0)
456 if ((flags
& opt_mask
[i
]) != opt_bits
[i
])
464 static uint64_t expand_overloaded_flags(uint64_t flags
, uint64_t pme
)
466 /* SLOB/SLUB overload several page flags */
467 if (flags
& BIT(SLAB
)) {
468 if (flags
& BIT(PRIVATE
))
469 flags
^= BIT(PRIVATE
) | BIT(SLOB_FREE
);
470 if (flags
& BIT(ACTIVE
))
471 flags
^= BIT(ACTIVE
) | BIT(SLUB_FROZEN
);
472 if (flags
& BIT(ERROR
))
473 flags
^= BIT(ERROR
) | BIT(SLUB_DEBUG
);
476 /* PG_reclaim is overloaded as PG_readahead in the read path */
477 if ((flags
& (BIT(RECLAIM
) | BIT(WRITEBACK
))) == BIT(RECLAIM
))
478 flags
^= BIT(RECLAIM
) | BIT(READAHEAD
);
480 if (pme
& PM_SOFT_DIRTY
)
481 flags
|= BIT(SOFTDIRTY
);
486 if (pme
& PM_MMAP_EXCLUSIVE
)
487 flags
|= BIT(MMAP_EXCLUSIVE
);
492 static uint64_t well_known_flags(uint64_t flags
)
494 /* hide flags intended only for kernel hacker */
495 flags
&= ~KPF_HACKERS_BITS
;
497 /* hide non-hugeTLB compound pages */
498 if ((flags
& BITS_COMPOUND
) && !(flags
& BIT(HUGE
)))
499 flags
&= ~BITS_COMPOUND
;
504 static uint64_t kpageflags_flags(uint64_t flags
, uint64_t pme
)
507 flags
= expand_overloaded_flags(flags
, pme
);
509 flags
= well_known_flags(flags
);
518 static void prepare_hwpoison_fd(void)
520 char buf
[MAX_PATH
+ 1];
522 hwpoison_debug_fs
= debugfs__mount();
523 if (!hwpoison_debug_fs
) {
524 perror("mount debugfs");
528 if (opt_hwpoison
&& !hwpoison_inject_fd
) {
529 snprintf(buf
, MAX_PATH
, "%s/hwpoison/corrupt-pfn",
531 hwpoison_inject_fd
= checked_open(buf
, O_WRONLY
);
534 if (opt_unpoison
&& !hwpoison_forget_fd
) {
535 snprintf(buf
, MAX_PATH
, "%s/hwpoison/unpoison-pfn",
537 hwpoison_forget_fd
= checked_open(buf
, O_WRONLY
);
541 static int hwpoison_page(unsigned long offset
)
546 len
= sprintf(buf
, "0x%lx\n", offset
);
547 len
= write(hwpoison_inject_fd
, buf
, len
);
549 perror("hwpoison inject");
555 static int unpoison_page(unsigned long offset
)
560 len
= sprintf(buf
, "0x%lx\n", offset
);
561 len
= write(hwpoison_forget_fd
, buf
, len
);
563 perror("hwpoison forget");
573 static size_t hash_slot(uint64_t flags
)
575 size_t k
= HASH_KEY(flags
);
578 /* Explicitly reserve slot 0 for flags 0: the following logic
579 * cannot distinguish an unoccupied slot from slot (flags==0).
584 /* search through the remaining (HASH_SIZE-1) slots */
585 for (i
= 1; i
< ARRAY_SIZE(page_flags
); i
++, k
++) {
586 if (!k
|| k
>= ARRAY_SIZE(page_flags
))
588 if (page_flags
[k
] == 0) {
589 page_flags
[k
] = flags
;
592 if (page_flags
[k
] == flags
)
596 fatal("hash table full: bump up HASH_SHIFT?\n");
600 static void add_page(unsigned long voffset
, unsigned long offset
,
601 uint64_t flags
, uint64_t cgroup
, uint64_t pme
)
603 flags
= kpageflags_flags(flags
, pme
);
605 if (!bit_mask_ok(flags
))
608 if (opt_cgroup
&& cgroup
!= (uint64_t)opt_cgroup
)
612 hwpoison_page(offset
);
614 unpoison_page(offset
);
617 show_page_range(voffset
, offset
, 1, flags
, cgroup
);
618 else if (opt_list
== 2)
619 show_page(voffset
, offset
, flags
, cgroup
);
621 nr_pages
[hash_slot(flags
)]++;
625 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
626 static void walk_pfn(unsigned long voffset
,
631 uint64_t buf
[KPAGEFLAGS_BATCH
];
632 uint64_t cgi
[KPAGEFLAGS_BATCH
];
638 * kpagecgroup_read() reads only if kpagecgroup were opened, but
639 * /proc/kpagecgroup might even not exist, so it's better to fill
640 * them with zeros here.
645 memset(cgi
, 0, sizeof cgi
);
648 batch
= min_t(unsigned long, count
, KPAGEFLAGS_BATCH
);
649 pages
= kpageflags_read(buf
, index
, batch
);
653 if (kpagecgroup_read(cgi
, index
, pages
) != pages
)
654 fatal("kpagecgroup returned fewer pages than expected");
656 for (i
= 0; i
< pages
; i
++)
657 add_page(voffset
+ i
, index
+ i
, buf
[i
], cgi
[i
], pme
);
664 static void walk_swap(unsigned long voffset
, uint64_t pme
)
666 uint64_t flags
= kpageflags_flags(0, pme
);
668 if (!bit_mask_ok(flags
))
675 show_page_range(voffset
, pagemap_swap_offset(pme
), 1, flags
, 0);
676 else if (opt_list
== 2)
677 show_page(voffset
, pagemap_swap_offset(pme
), flags
, 0);
679 nr_pages
[hash_slot(flags
)]++;
683 #define PAGEMAP_BATCH (64 << 10)
684 static void walk_vma(unsigned long index
, unsigned long count
)
686 uint64_t buf
[PAGEMAP_BATCH
];
693 batch
= min_t(unsigned long, count
, PAGEMAP_BATCH
);
694 pages
= pagemap_read(buf
, index
, batch
);
698 for (i
= 0; i
< pages
; i
++) {
699 pfn
= pagemap_pfn(buf
[i
]);
701 walk_pfn(index
+ i
, pfn
, 1, buf
[i
]);
702 if (buf
[i
] & PM_SWAP
)
703 walk_swap(index
+ i
, buf
[i
]);
711 static void walk_task(unsigned long index
, unsigned long count
)
713 const unsigned long end
= index
+ count
;
717 while (index
< end
) {
719 while (pg_end
[i
] <= index
)
722 if (pg_start
[i
] >= end
)
725 start
= max_t(unsigned long, pg_start
[i
], index
);
726 index
= min_t(unsigned long, pg_end
[i
], end
);
728 assert(start
< index
);
729 walk_vma(start
, index
- start
);
733 static void add_addr_range(unsigned long offset
, unsigned long size
)
735 if (nr_addr_ranges
>= MAX_ADDR_RANGES
)
736 fatal("too many addr ranges\n");
738 opt_offset
[nr_addr_ranges
] = offset
;
739 opt_size
[nr_addr_ranges
] = min_t(unsigned long, size
, ULONG_MAX
-offset
);
743 static void walk_addr_ranges(void)
747 kpageflags_fd
= checked_open(opt_kpageflags
, O_RDONLY
);
750 add_addr_range(0, ULONG_MAX
);
752 for (i
= 0; i
< nr_addr_ranges
; i
++)
754 walk_pfn(opt_offset
[i
], opt_offset
[i
], opt_size
[i
], 0);
756 walk_task(opt_offset
[i
], opt_size
[i
]);
758 close(kpageflags_fd
);
766 static const char *page_flag_type(uint64_t flag
)
768 if (flag
& KPF_HACKERS_BITS
)
770 if (flag
& KPF_OVERLOADED_BITS
)
775 static void usage(void)
780 "page-types [options]\n"
781 " -r|--raw Raw mode, for kernel developers\n"
782 " -d|--describe flags Describe flags\n"
783 " -a|--addr addr-spec Walk a range of pages\n"
784 " -b|--bits bits-spec Walk pages with specified bits\n"
785 " -c|--cgroup path|@inode Walk pages within memory cgroup\n"
786 " -p|--pid pid Walk process address space\n"
787 " -f|--file filename Walk file address space\n"
788 " -l|--list Show page details in ranges\n"
789 " -L|--list-each Show page details one by one\n"
790 " -C|--list-cgroup Show cgroup inode for pages\n"
791 " -N|--no-summary Don't show summary info\n"
792 " -X|--hwpoison hwpoison pages\n"
793 " -x|--unpoison unpoison pages\n"
794 " -F|--kpageflags filename kpageflags file to parse\n"
795 " -h|--help Show this usage message\n"
797 " 0x10 bitfield format, e.g.\n"
798 " anon bit-name, e.g.\n"
799 " 0x10,anon comma-separated list, e.g.\n"
801 " N one page at offset N (unit: pages)\n"
802 " N+M pages range from N to N+M-1\n"
803 " N,M pages range from N to M-1\n"
804 " N, pages range from N to end\n"
805 " ,M pages range from 0 to M-1\n"
807 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
808 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
809 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
810 " =bit1,bit2 flags == (bit1|bit2)\n"
814 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
815 if (!page_flag_names
[i
])
817 printf("%16s%s", page_flag_names
[i
] + 2,
818 page_flag_type(1ULL << i
));
825 "(r) raw mode bits (o) overloaded bits\n");
828 static unsigned long long parse_number(const char *str
)
830 unsigned long long n
;
832 n
= strtoll(str
, NULL
, 0);
834 if (n
== 0 && str
[0] != '0')
835 fatal("invalid name or number: %s\n", str
);
840 static void parse_pid(const char *str
)
845 opt_pid
= parse_number(str
);
847 sprintf(buf
, "/proc/%d/pagemap", opt_pid
);
848 pagemap_fd
= checked_open(buf
, O_RDONLY
);
850 sprintf(buf
, "/proc/%d/maps", opt_pid
);
851 file
= fopen(buf
, "r");
857 while (fgets(buf
, sizeof(buf
), file
) != NULL
) {
858 unsigned long vm_start
;
859 unsigned long vm_end
;
860 unsigned long long pgoff
;
866 n
= sscanf(buf
, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
874 fprintf(stderr
, "unexpected line: %s\n", buf
);
877 pg_start
[nr_vmas
] = vm_start
/ page_size
;
878 pg_end
[nr_vmas
] = vm_end
/ page_size
;
879 if (++nr_vmas
>= MAX_VMAS
) {
880 fprintf(stderr
, "too many VMAs\n");
887 static void show_file(const char *name
, const struct stat
*st
)
889 unsigned long long size
= st
->st_size
;
890 char atime
[64], mtime
[64];
891 long now
= time(NULL
);
893 printf("%s\tInode: %u\tSize: %llu (%llu pages)\n",
894 name
, (unsigned)st
->st_ino
,
895 size
, (size
+ page_size
- 1) / page_size
);
897 strftime(atime
, sizeof(atime
), "%c", localtime(&st
->st_atime
));
898 strftime(mtime
, sizeof(mtime
), "%c", localtime(&st
->st_mtime
));
900 printf("Modify: %s (%ld seconds ago)\nAccess: %s (%ld seconds ago)\n",
901 mtime
, now
- st
->st_mtime
,
902 atime
, now
- st
->st_atime
);
905 static sigjmp_buf sigbus_jmp
;
907 static void * volatile sigbus_addr
;
909 static void sigbus_handler(int sig
, siginfo_t
*info
, void *ucontex
)
913 sigbus_addr
= info
? info
->si_addr
: NULL
;
914 siglongjmp(sigbus_jmp
, 1);
917 static struct sigaction sigbus_action
= {
918 .sa_sigaction
= sigbus_handler
,
919 .sa_flags
= SA_SIGINFO
,
922 static void walk_file(const char *name
, const struct stat
*st
)
924 uint8_t vec
[PAGEMAP_BATCH
];
925 uint64_t buf
[PAGEMAP_BATCH
], flags
;
927 unsigned long nr_pages
, pfn
, i
;
928 off_t off
, end
= st
->st_size
;
934 fd
= checked_open(name
, O_RDONLY
|O_NOATIME
|O_NOFOLLOW
);
936 for (off
= 0; off
< end
; off
+= len
) {
937 nr_pages
= (end
- off
+ page_size
- 1) / page_size
;
938 if (nr_pages
> PAGEMAP_BATCH
)
939 nr_pages
= PAGEMAP_BATCH
;
940 len
= nr_pages
* page_size
;
942 ptr
= mmap(NULL
, len
, PROT_READ
, MAP_SHARED
, fd
, off
);
943 if (ptr
== MAP_FAILED
)
944 fatal("mmap failed: %s", name
);
946 /* determine cached pages */
947 if (mincore(ptr
, len
, vec
))
948 fatal("mincore failed: %s", name
);
950 /* turn off readahead */
951 if (madvise(ptr
, len
, MADV_RANDOM
))
952 fatal("madvice failed: %s", name
);
954 if (sigsetjmp(sigbus_jmp
, 1)) {
955 end
= off
+ sigbus_addr
? sigbus_addr
- ptr
: 0;
956 fprintf(stderr
, "got sigbus at offset %lld: %s\n",
957 (long long)end
, name
);
962 for (i
= 0; i
< nr_pages
; i
++) {
964 (void)*(volatile int *)(ptr
+ i
* page_size
);
968 /* turn off harvesting reference bits */
969 if (madvise(ptr
, len
, MADV_SEQUENTIAL
))
970 fatal("madvice failed: %s", name
);
972 if (pagemap_read(buf
, (unsigned long)ptr
/ page_size
,
973 nr_pages
) != nr_pages
)
974 fatal("cannot read pagemap");
978 for (i
= 0; i
< nr_pages
; i
++) {
979 pfn
= pagemap_pfn(buf
[i
]);
982 if (!kpageflags_read(&flags
, pfn
, 1))
984 if (!kpagecgroup_read(&cgroup
, pfn
, 1))
985 fatal("kpagecgroup_read failed");
986 if (first
&& opt_list
) {
991 add_page(off
/ page_size
+ i
, pfn
,
992 flags
, cgroup
, buf
[i
]);
999 int walk_tree(const char *name
, const struct stat
*st
, int type
, struct FTW
*f
)
1004 if (S_ISREG(st
->st_mode
))
1005 walk_file(name
, st
);
1008 fprintf(stderr
, "cannot read dir: %s\n", name
);
1014 static void walk_page_cache(void)
1018 kpageflags_fd
= checked_open(opt_kpageflags
, O_RDONLY
);
1019 pagemap_fd
= checked_open("/proc/self/pagemap", O_RDONLY
);
1020 sigaction(SIGBUS
, &sigbus_action
, NULL
);
1022 if (stat(opt_file
, &st
))
1023 fatal("stat failed: %s\n", opt_file
);
1025 if (S_ISREG(st
.st_mode
)) {
1026 walk_file(opt_file
, &st
);
1027 } else if (S_ISDIR(st
.st_mode
)) {
1028 /* do not follow symlinks and mountpoints */
1029 if (nftw(opt_file
, walk_tree
, 64, FTW_MOUNT
| FTW_PHYS
) < 0)
1030 fatal("nftw failed: %s\n", opt_file
);
1032 fatal("unhandled file type: %s\n", opt_file
);
1034 close(kpageflags_fd
);
1036 signal(SIGBUS
, SIG_DFL
);
1039 static void parse_file(const char *name
)
1044 static void parse_cgroup(const char *path
)
1046 if (path
[0] == '@') {
1047 opt_cgroup
= parse_number(path
+ 1);
1053 if (stat(path
, &st
))
1054 fatal("stat failed: %s: %m\n", path
);
1056 if (!S_ISDIR(st
.st_mode
))
1057 fatal("cgroup supposed to be a directory: %s\n", path
);
1059 opt_cgroup
= st
.st_ino
;
1062 static void parse_addr_range(const char *optarg
)
1064 unsigned long offset
;
1068 p
= strchr(optarg
, ',');
1070 p
= strchr(optarg
, '+');
1074 size
= parse_number(p
+ 1);
1076 offset
= parse_number(optarg
);
1080 size
= parse_number(p
+ 1);
1083 fatal("invalid range: %lu,%lu\n",
1089 offset
= parse_number(optarg
);
1093 add_addr_range(offset
, size
);
1096 static void add_bits_filter(uint64_t mask
, uint64_t bits
)
1098 if (nr_bit_filters
>= MAX_BIT_FILTERS
)
1099 fatal("too much bit filters\n");
1101 opt_mask
[nr_bit_filters
] = mask
;
1102 opt_bits
[nr_bit_filters
] = bits
;
1106 static uint64_t parse_flag_name(const char *str
, int len
)
1113 if (len
<= 8 && !strncmp(str
, "compound", len
))
1114 return BITS_COMPOUND
;
1116 for (i
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
1117 if (!page_flag_names
[i
])
1119 if (!strncmp(str
, page_flag_names
[i
] + 2, len
))
1123 return parse_number(str
);
1126 static uint64_t parse_flag_names(const char *str
, int all
)
1128 const char *p
= str
;
1132 if (*p
== ',' || *p
== '=' || *p
== '\0') {
1133 if ((*str
!= '~') || (*str
== '~' && all
&& *++str
))
1134 flags
|= parse_flag_name(str
, p
- str
);
1145 static void parse_bits_mask(const char *optarg
)
1151 p
= strchr(optarg
, '=');
1153 mask
= KPF_ALL_BITS
;
1154 bits
= parse_flag_names(p
+ 1, 0);
1156 mask
= parse_flag_names(optarg
, 0);
1157 bits
= parse_flag_names(p
+ 1, 0);
1158 } else if (strchr(optarg
, '~')) {
1159 mask
= parse_flag_names(optarg
, 1);
1160 bits
= parse_flag_names(optarg
, 0);
1162 mask
= parse_flag_names(optarg
, 0);
1163 bits
= KPF_ALL_BITS
;
1166 add_bits_filter(mask
, bits
);
1169 static void parse_kpageflags(const char *name
)
1171 opt_kpageflags
= name
;
1174 static void describe_flags(const char *optarg
)
1176 uint64_t flags
= parse_flag_names(optarg
, 0);
1178 printf("0x%016llx\t%s\t%s\n",
1179 (unsigned long long)flags
,
1180 page_flag_name(flags
),
1181 page_flag_longname(flags
));
1184 static const struct option opts
[] = {
1185 { "raw" , 0, NULL
, 'r' },
1186 { "pid" , 1, NULL
, 'p' },
1187 { "file" , 1, NULL
, 'f' },
1188 { "addr" , 1, NULL
, 'a' },
1189 { "bits" , 1, NULL
, 'b' },
1190 { "cgroup" , 1, NULL
, 'c' },
1191 { "describe" , 1, NULL
, 'd' },
1192 { "list" , 0, NULL
, 'l' },
1193 { "list-each" , 0, NULL
, 'L' },
1194 { "list-cgroup", 0, NULL
, 'C' },
1195 { "no-summary", 0, NULL
, 'N' },
1196 { "hwpoison" , 0, NULL
, 'X' },
1197 { "unpoison" , 0, NULL
, 'x' },
1198 { "kpageflags", 0, NULL
, 'F' },
1199 { "help" , 0, NULL
, 'h' },
1200 { NULL
, 0, NULL
, 0 }
1203 int main(int argc
, char *argv
[])
1207 page_size
= getpagesize();
1209 while ((c
= getopt_long(argc
, argv
,
1210 "rp:f:a:b:d:c:ClLNXxF:h", opts
, NULL
)) != -1) {
1222 parse_addr_range(optarg
);
1225 parse_bits_mask(optarg
);
1228 parse_cgroup(optarg
);
1231 opt_list_cgroup
= 1;
1234 describe_flags(optarg
);
1247 prepare_hwpoison_fd();
1251 prepare_hwpoison_fd();
1254 parse_kpageflags(optarg
);
1265 if (!opt_kpageflags
)
1266 opt_kpageflags
= PROC_KPAGEFLAGS
;
1268 if (opt_cgroup
|| opt_list_cgroup
)
1269 kpagecgroup_fd
= checked_open(PROC_KPAGECGROUP
, O_RDONLY
);
1271 if (opt_list
&& opt_pid
)
1272 printf("voffset\t");
1273 if (opt_list
&& opt_file
)
1274 printf("foffset\t");
1275 if (opt_list
&& opt_list_cgroup
)
1278 printf("offset\tlen\tflags\n");
1280 printf("offset\tflags\n");