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",
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
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
];
212 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
214 #define min_t(type, x, y) ({ \
217 __min1 < __min2 ? __min1 : __min2; })
219 #define max_t(type, x, 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
, ...)
234 vfprintf(stderr
, x
, ap
);
239 static int checked_open(const char *pathname
, int flags
)
241 int fd
= open(pathname
, flags
);
252 * pagemap/kpageflags routines
255 static unsigned long do_u64_read(int fd
, char *name
,
262 if (index
> ULONG_MAX
/ 8)
263 fatal("index overflow: %lu\n", index
);
265 bytes
= pread(fd
, buf
, count
* 8, (off_t
)index
* 8);
271 fatal("partial read: %lu bytes\n", bytes
);
276 static unsigned long kpageflags_read(uint64_t *buf
,
280 return do_u64_read(kpageflags_fd
, PROC_KPAGEFLAGS
, buf
, index
, pages
);
283 static unsigned long kpagecgroup_read(uint64_t *buf
,
287 if (kpagecgroup_fd
< 0)
290 return do_u64_read(kpagecgroup_fd
, PROC_KPAGEFLAGS
, buf
, index
, pages
);
293 static unsigned long pagemap_read(uint64_t *buf
,
297 return do_u64_read(pagemap_fd
, "/proc/pid/pagemap", buf
, index
, pages
);
300 static unsigned long pagemap_pfn(uint64_t val
)
304 if (val
& PM_PRESENT
)
305 pfn
= PM_PFRAME(val
);
312 static unsigned long pagemap_swap_offset(uint64_t val
)
314 return val
& PM_SWAP
? PM_SWAP_OFFSET(val
) : 0;
321 static char *page_flag_name(uint64_t flags
)
327 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
328 present
= (flags
>> i
) & 1;
329 if (!page_flag_names
[i
]) {
331 fatal("unknown flag bit %d\n", i
);
334 buf
[j
++] = present
? page_flag_names
[i
][0] : '_';
340 static char *page_flag_longname(uint64_t flags
)
342 static char buf
[1024];
345 for (i
= 0, n
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
346 if (!page_flag_names
[i
])
348 if ((flags
>> i
) & 1)
349 n
+= snprintf(buf
+ n
, sizeof(buf
) - n
, "%s,",
350 page_flag_names
[i
] + 2);
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
) {
381 printf("%lx\t", voff
);
383 printf("%lu\t", voff
);
385 printf("@%llu\t", (unsigned long long)cgroup0
);
386 printf("%lx\t%lx\t%s\n",
387 index
, count
, page_flag_name(flags0
));
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
)
406 printf("%lx\t", voffset
);
408 printf("%lu\t", voffset
);
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)
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
++) {
423 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
424 (unsigned long long)page_flags
[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
));
440 static int bit_mask_ok(uint64_t flags
)
444 for (i
= 0; i
< nr_bit_filters
; i
++) {
445 if (opt_bits
[i
] == KPF_ALL_BITS
) {
446 if ((flags
& opt_mask
[i
]) == 0)
449 if ((flags
& opt_mask
[i
]) != opt_bits
[i
])
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
);
479 if (pme
& PM_MMAP_EXCLUSIVE
)
480 flags
|= BIT(MMAP_EXCLUSIVE
);
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
;
497 static uint64_t kpageflags_flags(uint64_t flags
, uint64_t pme
)
500 flags
= expand_overloaded_flags(flags
, pme
);
502 flags
= well_known_flags(flags
);
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");
521 if (opt_hwpoison
&& !hwpoison_inject_fd
) {
522 snprintf(buf
, MAX_PATH
, "%s/hwpoison/corrupt-pfn",
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",
530 hwpoison_forget_fd
= checked_open(buf
, O_WRONLY
);
534 static int hwpoison_page(unsigned long offset
)
539 len
= sprintf(buf
, "0x%lx\n", offset
);
540 len
= write(hwpoison_inject_fd
, buf
, len
);
542 perror("hwpoison inject");
548 static int unpoison_page(unsigned long offset
)
553 len
= sprintf(buf
, "0x%lx\n", offset
);
554 len
= write(hwpoison_forget_fd
, buf
, len
);
556 perror("hwpoison forget");
566 static size_t hash_slot(uint64_t flags
)
568 size_t k
= HASH_KEY(flags
);
571 /* Explicitly reserve slot 0 for flags 0: the following logic
572 * cannot distinguish an unoccupied slot from slot (flags==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
))
581 if (page_flags
[k
] == 0) {
582 page_flags
[k
] = flags
;
585 if (page_flags
[k
] == flags
)
589 fatal("hash table full: bump up HASH_SHIFT?\n");
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
))
601 if (opt_cgroup
&& cgroup
!= (uint64_t)opt_cgroup
)
605 hwpoison_page(offset
);
607 unpoison_page(offset
);
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
)]++;
618 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
619 static void walk_pfn(unsigned long voffset
,
624 uint64_t buf
[KPAGEFLAGS_BATCH
];
625 uint64_t cgi
[KPAGEFLAGS_BATCH
];
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.
638 memset(cgi
, 0, sizeof cgi
);
641 batch
= min_t(unsigned long, count
, KPAGEFLAGS_BATCH
);
642 pages
= kpageflags_read(buf
, index
, batch
);
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
);
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
))
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
)]++;
676 #define PAGEMAP_BATCH (64 << 10)
677 static void walk_vma(unsigned long index
, unsigned long count
)
679 uint64_t buf
[PAGEMAP_BATCH
];
686 batch
= min_t(unsigned long, count
, PAGEMAP_BATCH
);
687 pages
= pagemap_read(buf
, index
, batch
);
691 for (i
= 0; i
< pages
; i
++) {
692 pfn
= pagemap_pfn(buf
[i
]);
694 walk_pfn(index
+ i
, pfn
, 1, buf
[i
]);
695 if (buf
[i
] & PM_SWAP
)
696 walk_swap(index
+ i
, buf
[i
]);
704 static void walk_task(unsigned long index
, unsigned long count
)
706 const unsigned long end
= index
+ count
;
710 while (index
< end
) {
712 while (pg_end
[i
] <= index
)
715 if (pg_start
[i
] >= end
)
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
);
736 static void walk_addr_ranges(void)
740 kpageflags_fd
= checked_open(PROC_KPAGEFLAGS
, O_RDONLY
);
743 add_addr_range(0, ULONG_MAX
);
745 for (i
= 0; i
< nr_addr_ranges
; i
++)
747 walk_pfn(opt_offset
[i
], opt_offset
[i
], opt_size
[i
], 0);
749 walk_task(opt_offset
[i
], opt_size
[i
]);
751 close(kpageflags_fd
);
759 static const char *page_flag_type(uint64_t flag
)
761 if (flag
& KPF_HACKERS_BITS
)
763 if (flag
& KPF_OVERLOADED_BITS
)
768 static void usage(void)
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"
789 " 0x10 bitfield format, e.g.\n"
790 " anon bit-name, e.g.\n"
791 " 0x10,anon comma-separated list, e.g.\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"
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"
806 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
807 if (!page_flag_names
[i
])
809 printf("%16s%s", page_flag_names
[i
] + 2,
810 page_flag_type(1ULL << i
));
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
);
832 static void parse_pid(const char *str
)
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");
849 while (fgets(buf
, sizeof(buf
), file
) != NULL
) {
850 unsigned long vm_start
;
851 unsigned long vm_end
;
852 unsigned long long pgoff
;
858 n
= sscanf(buf
, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
866 fprintf(stderr
, "unexpected line: %s\n", buf
);
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");
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
)
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
;
919 unsigned long nr_pages
, pfn
, i
;
920 off_t off
, end
= st
->st_size
;
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
);
954 for (i
= 0; i
< nr_pages
; i
++) {
956 (void)*(volatile int *)(ptr
+ i
* page_size
);
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");
970 for (i
= 0; i
< nr_pages
; i
++) {
971 pfn
= pagemap_pfn(buf
[i
]);
974 if (!kpageflags_read(&flags
, pfn
, 1))
976 if (!kpagecgroup_read(&cgroup
, pfn
, 1))
977 fatal("kpagecgroup_read failed");
978 if (first
&& opt_list
) {
983 add_page(off
/ page_size
+ i
, pfn
,
984 flags
, cgroup
, buf
[i
]);
991 int walk_tree(const char *name
, const struct stat
*st
, int type
, struct FTW
*f
)
996 if (S_ISREG(st
->st_mode
))
1000 fprintf(stderr
, "cannot read dir: %s\n", name
);
1006 static void walk_page_cache(void)
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
);
1024 fatal("unhandled file type: %s\n", opt_file
);
1026 close(kpageflags_fd
);
1028 signal(SIGBUS
, SIG_DFL
);
1031 static void parse_file(const char *name
)
1036 static void parse_cgroup(const char *path
)
1038 if (path
[0] == '@') {
1039 opt_cgroup
= parse_number(path
+ 1);
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
;
1060 p
= strchr(optarg
, ',');
1062 p
= strchr(optarg
, '+');
1066 size
= parse_number(p
+ 1);
1068 offset
= parse_number(optarg
);
1072 size
= parse_number(p
+ 1);
1075 fatal("invalid range: %lu,%lu\n",
1081 offset
= parse_number(optarg
);
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
;
1098 static uint64_t parse_flag_name(const char *str
, int len
)
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
])
1111 if (!strncmp(str
, page_flag_names
[i
] + 2, len
))
1115 return parse_number(str
);
1118 static uint64_t parse_flag_names(const char *str
, int all
)
1120 const char *p
= str
;
1124 if (*p
== ',' || *p
== '=' || *p
== '\0') {
1125 if ((*str
!= '~') || (*str
== '~' && all
&& *++str
))
1126 flags
|= parse_flag_name(str
, p
- str
);
1137 static void parse_bits_mask(const char *optarg
)
1143 p
= strchr(optarg
, '=');
1145 mask
= KPF_ALL_BITS
;
1146 bits
= parse_flag_names(p
+ 1, 0);
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);
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
[])
1193 page_size
= getpagesize();
1195 while ((c
= getopt_long(argc
, argv
,
1196 "rp:f:a:b:d:c:ClLNXxh", opts
, NULL
)) != -1) {
1208 parse_addr_range(optarg
);
1211 parse_bits_mask(optarg
);
1214 parse_cgroup(optarg
);
1217 opt_list_cgroup
= 1;
1220 describe_flags(optarg
);
1233 prepare_hwpoison_fd();
1237 prepare_hwpoison_fd();
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
)
1258 printf("offset\tlen\tflags\n");
1260 printf("offset\tflags\n");