1 // SPDX-License-Identifier: GPL-2.0-only
3 * page-types: Tool for querying page flags
5 * Copyright (C) 2009 Intel corporation
7 * Authors: Wu Fengguang <fengguang.wu@intel.com>
10 #define _FILE_OFFSET_BITS 64
25 #include <sys/types.h>
26 #include <sys/errno.h>
27 #include <sys/fcntl.h>
28 #include <sys/mount.h>
29 #include <sys/statfs.h>
31 #include "../../include/uapi/linux/magic.h"
32 #include "../../include/uapi/linux/kernel-page-flags.h"
33 #include <api/fs/fs.h>
41 # define STR(x) _STR(x)
45 * pagemap kernel ABI bits
48 #define PM_ENTRY_BYTES 8
49 #define PM_PFRAME_BITS 55
50 #define PM_PFRAME_MASK ((1LL << PM_PFRAME_BITS) - 1)
51 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
52 #define MAX_SWAPFILES_SHIFT 5
53 #define PM_SWAP_OFFSET(x) (((x) & PM_PFRAME_MASK) >> MAX_SWAPFILES_SHIFT)
54 #define PM_SOFT_DIRTY (1ULL << 55)
55 #define PM_MMAP_EXCLUSIVE (1ULL << 56)
56 #define PM_FILE (1ULL << 61)
57 #define PM_SWAP (1ULL << 62)
58 #define PM_PRESENT (1ULL << 63)
65 #define PROC_KPAGEFLAGS "/proc/kpageflags"
66 #define PROC_KPAGECOUNT "/proc/kpagecount"
67 #define PROC_KPAGECGROUP "/proc/kpagecgroup"
69 #define SYS_KERNEL_MM_PAGE_IDLE "/sys/kernel/mm/page_idle/bitmap"
71 /* [32-] kernel hacking assistances */
72 #define KPF_RESERVED 32
73 #define KPF_MLOCKED 33
74 #define KPF_MAPPEDTODISK 34
75 #define KPF_PRIVATE 35
76 #define KPF_PRIVATE_2 36
77 #define KPF_OWNER_PRIVATE 37
79 #define KPF_UNCACHED 39
80 #define KPF_SOFTDIRTY 40
83 /* [48-] take some arbitrary free slots for expanding overloaded flags
84 * not part of kernel API
86 #define KPF_READAHEAD 48
87 #define KPF_SLOB_FREE 49
88 #define KPF_SLUB_FROZEN 50
89 #define KPF_SLUB_DEBUG 51
92 #define KPF_MMAP_EXCLUSIVE 63
94 #define KPF_ALL_BITS ((uint64_t)~0ULL)
95 #define KPF_HACKERS_BITS (0xffffULL << 32)
96 #define KPF_OVERLOADED_BITS (0xffffULL << 48)
97 #define BIT(name) (1ULL << KPF_##name)
98 #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
100 static const char * const page_flag_names
[] = {
101 [KPF_LOCKED
] = "L:locked",
102 [KPF_ERROR
] = "E:error",
103 [KPF_REFERENCED
] = "R:referenced",
104 [KPF_UPTODATE
] = "U:uptodate",
105 [KPF_DIRTY
] = "D:dirty",
107 [KPF_ACTIVE
] = "A:active",
108 [KPF_SLAB
] = "S:slab",
109 [KPF_WRITEBACK
] = "W:writeback",
110 [KPF_RECLAIM
] = "I:reclaim",
111 [KPF_BUDDY
] = "B:buddy",
113 [KPF_MMAP
] = "M:mmap",
114 [KPF_ANON
] = "a:anonymous",
115 [KPF_SWAPCACHE
] = "s:swapcache",
116 [KPF_SWAPBACKED
] = "b:swapbacked",
117 [KPF_COMPOUND_HEAD
] = "H:compound_head",
118 [KPF_COMPOUND_TAIL
] = "T:compound_tail",
119 [KPF_HUGE
] = "G:huge",
120 [KPF_UNEVICTABLE
] = "u:unevictable",
121 [KPF_HWPOISON
] = "X:hwpoison",
122 [KPF_NOPAGE
] = "n:nopage",
125 [KPF_OFFLINE
] = "o:offline",
126 [KPF_PGTABLE
] = "g:pgtable",
127 [KPF_ZERO_PAGE
] = "z:zero_page",
128 [KPF_IDLE
] = "i:idle_page",
130 [KPF_RESERVED
] = "r:reserved",
131 [KPF_MLOCKED
] = "m:mlocked",
132 [KPF_MAPPEDTODISK
] = "d:mappedtodisk",
133 [KPF_PRIVATE
] = "P:private",
134 [KPF_PRIVATE_2
] = "p:private_2",
135 [KPF_OWNER_PRIVATE
] = "O:owner_private",
136 [KPF_ARCH
] = "h:arch",
137 [KPF_UNCACHED
] = "c:uncached",
138 [KPF_SOFTDIRTY
] = "f:softdirty",
139 [KPF_ARCH_2
] = "H:arch_2",
141 [KPF_READAHEAD
] = "I:readahead",
142 [KPF_SLOB_FREE
] = "P:slob_free",
143 [KPF_SLUB_FROZEN
] = "A:slub_frozen",
144 [KPF_SLUB_DEBUG
] = "E:slub_debug",
146 [KPF_FILE
] = "F:file",
147 [KPF_SWAP
] = "w:swap",
148 [KPF_MMAP_EXCLUSIVE
] = "1:mmap_exclusive",
156 static int opt_raw
; /* for kernel developers */
157 static int opt_list
; /* list pages (in ranges) */
158 static int opt_mark_idle
; /* set accessed bit */
159 static int opt_no_summary
; /* don't show summary */
160 static pid_t opt_pid
; /* process to walk */
161 const char *opt_file
; /* file or directory path */
162 static uint64_t opt_cgroup
; /* cgroup inode */
163 static int opt_list_cgroup
;/* list page cgroup */
164 static int opt_list_mapcnt
;/* list page map count */
165 static const char *opt_kpageflags
;/* kpageflags file to parse */
167 #define MAX_ADDR_RANGES 1024
168 static int nr_addr_ranges
;
169 static unsigned long opt_offset
[MAX_ADDR_RANGES
];
170 static unsigned long opt_size
[MAX_ADDR_RANGES
];
172 #define MAX_VMAS 10240
174 static unsigned long pg_start
[MAX_VMAS
];
175 static unsigned long pg_end
[MAX_VMAS
];
177 #define MAX_BIT_FILTERS 64
178 static int nr_bit_filters
;
179 static uint64_t opt_mask
[MAX_BIT_FILTERS
];
180 static uint64_t opt_bits
[MAX_BIT_FILTERS
];
182 static int page_size
;
184 static int pagemap_fd
;
185 static int kpageflags_fd
;
186 static int kpagecount_fd
= -1;
187 static int kpagecgroup_fd
= -1;
188 static int page_idle_fd
= -1;
190 static int opt_hwpoison
;
191 static int opt_unpoison
;
193 static const char *hwpoison_debug_fs
;
194 static int hwpoison_inject_fd
;
195 static int hwpoison_forget_fd
;
197 #define HASH_SHIFT 13
198 #define HASH_SIZE (1 << HASH_SHIFT)
199 #define HASH_MASK (HASH_SIZE - 1)
200 #define HASH_KEY(flags) (flags & HASH_MASK)
202 static unsigned long total_pages
;
203 static unsigned long nr_pages
[HASH_SIZE
];
204 static uint64_t page_flags
[HASH_SIZE
];
211 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
213 #define min_t(type, x, y) ({ \
216 __min1 < __min2 ? __min1 : __min2; })
218 #define max_t(type, x, y) ({ \
221 __max1 > __max2 ? __max1 : __max2; })
223 static unsigned long pages2mb(unsigned long pages
)
225 return (pages
* page_size
) >> 20;
228 static void fatal(const char *x
, ...)
233 vfprintf(stderr
, x
, ap
);
238 static int checked_open(const char *pathname
, int flags
)
240 int fd
= open(pathname
, flags
);
251 * pagemap/kpageflags routines
254 static unsigned long do_u64_read(int fd
, const char *name
,
261 if (index
> ULONG_MAX
/ 8)
262 fatal("index overflow: %lu\n", index
);
264 bytes
= pread(fd
, buf
, count
* 8, (off_t
)index
* 8);
270 fatal("partial read: %lu bytes\n", bytes
);
275 static unsigned long kpageflags_read(uint64_t *buf
,
279 return do_u64_read(kpageflags_fd
, opt_kpageflags
, buf
, index
, pages
);
282 static unsigned long kpagecgroup_read(uint64_t *buf
,
286 if (kpagecgroup_fd
< 0)
289 return do_u64_read(kpagecgroup_fd
, opt_kpageflags
, buf
, index
, pages
);
292 static unsigned long kpagecount_read(uint64_t *buf
,
296 return kpagecount_fd
< 0 ? pages
:
297 do_u64_read(kpagecount_fd
, PROC_KPAGECOUNT
,
301 static unsigned long pagemap_read(uint64_t *buf
,
305 return do_u64_read(pagemap_fd
, "/proc/pid/pagemap", buf
, index
, pages
);
308 static unsigned long pagemap_pfn(uint64_t val
)
312 if (val
& PM_PRESENT
)
313 pfn
= PM_PFRAME(val
);
320 static unsigned long pagemap_swap_offset(uint64_t val
)
322 return val
& PM_SWAP
? PM_SWAP_OFFSET(val
) : 0;
329 static char *page_flag_name(uint64_t flags
)
335 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
336 present
= (flags
>> i
) & 1;
337 if (!page_flag_names
[i
]) {
339 fatal("unknown flag bit %d\n", i
);
342 buf
[j
++] = present
? page_flag_names
[i
][0] : '_';
348 static char *page_flag_longname(uint64_t flags
)
350 static char buf
[1024];
353 for (i
= 0, n
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
354 if (!page_flag_names
[i
])
356 if ((flags
>> i
) & 1)
357 n
+= snprintf(buf
+ n
, sizeof(buf
) - n
, "%s,",
358 page_flag_names
[i
] + 2);
369 * page list and summary
372 static void show_page_range(unsigned long voffset
, unsigned long offset
,
373 unsigned long size
, uint64_t flags
,
374 uint64_t cgroup
, uint64_t mapcnt
)
376 static uint64_t flags0
;
377 static uint64_t cgroup0
;
378 static uint64_t mapcnt0
;
379 static unsigned long voff
;
380 static unsigned long index
;
381 static unsigned long count
;
383 if (flags
== flags0
&& cgroup
== cgroup0
&& mapcnt
== mapcnt0
&&
384 offset
== index
+ count
&& size
&& voffset
== voff
+ count
) {
391 printf("%lx\t", voff
);
393 printf("%lu\t", voff
);
395 printf("@%llu\t", (unsigned long long)cgroup0
);
397 printf("%lu\t", mapcnt0
);
398 printf("%lx\t%lx\t%s\n",
399 index
, count
, page_flag_name(flags0
));
410 static void flush_page_range(void)
412 show_page_range(0, 0, 0, 0, 0, 0);
415 static void show_page(unsigned long voffset
, unsigned long offset
,
416 uint64_t flags
, uint64_t cgroup
, uint64_t mapcnt
)
419 printf("%lx\t", voffset
);
421 printf("%lu\t", voffset
);
423 printf("@%llu\t", (unsigned long long)cgroup
);
425 printf("%lu\t", mapcnt
);
427 printf("%lx\t%s\n", offset
, page_flag_name(flags
));
430 static void show_summary(void)
434 printf(" flags\tpage-count MB"
435 " symbolic-flags\t\t\tlong-symbolic-flags\n");
437 for (i
= 0; i
< ARRAY_SIZE(nr_pages
); i
++) {
439 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
440 (unsigned long long)page_flags
[i
],
442 pages2mb(nr_pages
[i
]),
443 page_flag_name(page_flags
[i
]),
444 page_flag_longname(page_flags
[i
]));
447 printf(" total\t%10lu %8lu\n",
448 total_pages
, pages2mb(total_pages
));
456 static int bit_mask_ok(uint64_t flags
)
460 for (i
= 0; i
< nr_bit_filters
; i
++) {
461 if (opt_bits
[i
] == KPF_ALL_BITS
) {
462 if ((flags
& opt_mask
[i
]) == 0)
465 if ((flags
& opt_mask
[i
]) != opt_bits
[i
])
473 static uint64_t expand_overloaded_flags(uint64_t flags
, uint64_t pme
)
475 /* SLOB/SLUB overload several page flags */
476 if (flags
& BIT(SLAB
)) {
477 if (flags
& BIT(PRIVATE
))
478 flags
^= BIT(PRIVATE
) | BIT(SLOB_FREE
);
479 if (flags
& BIT(ACTIVE
))
480 flags
^= BIT(ACTIVE
) | BIT(SLUB_FROZEN
);
481 if (flags
& BIT(ERROR
))
482 flags
^= BIT(ERROR
) | BIT(SLUB_DEBUG
);
485 /* PG_reclaim is overloaded as PG_readahead in the read path */
486 if ((flags
& (BIT(RECLAIM
) | BIT(WRITEBACK
))) == BIT(RECLAIM
))
487 flags
^= BIT(RECLAIM
) | BIT(READAHEAD
);
489 if (pme
& PM_SOFT_DIRTY
)
490 flags
|= BIT(SOFTDIRTY
);
495 if (pme
& PM_MMAP_EXCLUSIVE
)
496 flags
|= BIT(MMAP_EXCLUSIVE
);
501 static uint64_t well_known_flags(uint64_t flags
)
503 /* hide flags intended only for kernel hacker */
504 flags
&= ~KPF_HACKERS_BITS
;
506 /* hide non-hugeTLB compound pages */
507 if ((flags
& BITS_COMPOUND
) && !(flags
& BIT(HUGE
)))
508 flags
&= ~BITS_COMPOUND
;
513 static uint64_t kpageflags_flags(uint64_t flags
, uint64_t pme
)
516 flags
= expand_overloaded_flags(flags
, pme
);
518 flags
= well_known_flags(flags
);
527 static void prepare_hwpoison_fd(void)
529 char buf
[MAX_PATH
+ 1];
531 hwpoison_debug_fs
= debugfs__mount();
532 if (!hwpoison_debug_fs
) {
533 perror("mount debugfs");
537 if (opt_hwpoison
&& !hwpoison_inject_fd
) {
538 snprintf(buf
, MAX_PATH
, "%s/hwpoison/corrupt-pfn",
540 hwpoison_inject_fd
= checked_open(buf
, O_WRONLY
);
543 if (opt_unpoison
&& !hwpoison_forget_fd
) {
544 snprintf(buf
, MAX_PATH
, "%s/hwpoison/unpoison-pfn",
546 hwpoison_forget_fd
= checked_open(buf
, O_WRONLY
);
550 static int hwpoison_page(unsigned long offset
)
555 len
= sprintf(buf
, "0x%lx\n", offset
);
556 len
= write(hwpoison_inject_fd
, buf
, len
);
558 perror("hwpoison inject");
564 static int unpoison_page(unsigned long offset
)
569 len
= sprintf(buf
, "0x%lx\n", offset
);
570 len
= write(hwpoison_forget_fd
, buf
, len
);
572 perror("hwpoison forget");
578 static int mark_page_idle(unsigned long offset
)
580 static unsigned long off
;
584 if ((offset
/ 64 == off
/ 64) || buf
== 0) {
585 buf
|= 1UL << (offset
% 64);
590 len
= pwrite(page_idle_fd
, &buf
, 8, 8 * (off
/ 64));
592 perror("mark page idle");
596 buf
= 1UL << (offset
% 64);
606 static size_t hash_slot(uint64_t flags
)
608 size_t k
= HASH_KEY(flags
);
611 /* Explicitly reserve slot 0 for flags 0: the following logic
612 * cannot distinguish an unoccupied slot from slot (flags==0).
617 /* search through the remaining (HASH_SIZE-1) slots */
618 for (i
= 1; i
< ARRAY_SIZE(page_flags
); i
++, k
++) {
619 if (!k
|| k
>= ARRAY_SIZE(page_flags
))
621 if (page_flags
[k
] == 0) {
622 page_flags
[k
] = flags
;
625 if (page_flags
[k
] == flags
)
629 fatal("hash table full: bump up HASH_SHIFT?\n");
633 static void add_page(unsigned long voffset
, unsigned long offset
,
634 uint64_t flags
, uint64_t cgroup
, uint64_t mapcnt
,
637 flags
= kpageflags_flags(flags
, pme
);
639 if (!bit_mask_ok(flags
))
642 if (opt_cgroup
&& cgroup
!= (uint64_t)opt_cgroup
)
646 hwpoison_page(offset
);
648 unpoison_page(offset
);
651 mark_page_idle(offset
);
654 show_page_range(voffset
, offset
, 1, flags
, cgroup
, mapcnt
);
655 else if (opt_list
== 2)
656 show_page(voffset
, offset
, flags
, cgroup
, mapcnt
);
658 nr_pages
[hash_slot(flags
)]++;
662 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
663 static void walk_pfn(unsigned long voffset
,
668 uint64_t buf
[KPAGEFLAGS_BATCH
];
669 uint64_t cgi
[KPAGEFLAGS_BATCH
];
670 uint64_t cnt
[KPAGEFLAGS_BATCH
];
676 * kpagecgroup_read() reads only if kpagecgroup were opened, but
677 * /proc/kpagecgroup might even not exist, so it's better to fill
678 * them with zeros here.
683 memset(cgi
, 0, sizeof cgi
);
686 batch
= min_t(unsigned long, count
, KPAGEFLAGS_BATCH
);
687 pages
= kpageflags_read(buf
, index
, batch
);
691 if (kpagecgroup_read(cgi
, index
, pages
) != pages
)
692 fatal("kpagecgroup returned fewer pages than expected");
694 if (kpagecount_read(cnt
, index
, pages
) != pages
)
695 fatal("kpagecount returned fewer pages than expected");
697 for (i
= 0; i
< pages
; i
++)
698 add_page(voffset
+ i
, index
+ i
,
699 buf
[i
], cgi
[i
], cnt
[i
], pme
);
706 static void walk_swap(unsigned long voffset
, uint64_t pme
)
708 uint64_t flags
= kpageflags_flags(0, pme
);
710 if (!bit_mask_ok(flags
))
717 show_page_range(voffset
, pagemap_swap_offset(pme
),
719 else if (opt_list
== 2)
720 show_page(voffset
, pagemap_swap_offset(pme
), flags
, 0, 0);
722 nr_pages
[hash_slot(flags
)]++;
726 #define PAGEMAP_BATCH (64 << 10)
727 static void walk_vma(unsigned long index
, unsigned long count
)
729 uint64_t buf
[PAGEMAP_BATCH
];
736 batch
= min_t(unsigned long, count
, PAGEMAP_BATCH
);
737 pages
= pagemap_read(buf
, index
, batch
);
741 for (i
= 0; i
< pages
; i
++) {
742 pfn
= pagemap_pfn(buf
[i
]);
744 walk_pfn(index
+ i
, pfn
, 1, buf
[i
]);
745 if (buf
[i
] & PM_SWAP
)
746 walk_swap(index
+ i
, buf
[i
]);
754 static void walk_task(unsigned long index
, unsigned long count
)
756 const unsigned long end
= index
+ count
;
760 while (index
< end
) {
762 while (pg_end
[i
] <= index
)
765 if (pg_start
[i
] >= end
)
768 start
= max_t(unsigned long, pg_start
[i
], index
);
769 index
= min_t(unsigned long, pg_end
[i
], end
);
771 assert(start
< index
);
772 walk_vma(start
, index
- start
);
776 static void add_addr_range(unsigned long offset
, unsigned long size
)
778 if (nr_addr_ranges
>= MAX_ADDR_RANGES
)
779 fatal("too many addr ranges\n");
781 opt_offset
[nr_addr_ranges
] = offset
;
782 opt_size
[nr_addr_ranges
] = min_t(unsigned long, size
, ULONG_MAX
-offset
);
786 static void walk_addr_ranges(void)
790 kpageflags_fd
= checked_open(opt_kpageflags
, O_RDONLY
);
793 add_addr_range(0, ULONG_MAX
);
795 for (i
= 0; i
< nr_addr_ranges
; i
++)
797 walk_pfn(opt_offset
[i
], opt_offset
[i
], opt_size
[i
], 0);
799 walk_task(opt_offset
[i
], opt_size
[i
]);
804 close(kpageflags_fd
);
812 static const char *page_flag_type(uint64_t flag
)
814 if (flag
& KPF_HACKERS_BITS
)
816 if (flag
& KPF_OVERLOADED_BITS
)
821 static void usage(void)
826 "page-types [options]\n"
827 " -r|--raw Raw mode, for kernel developers\n"
828 " -d|--describe flags Describe flags\n"
829 " -a|--addr addr-spec Walk a range of pages\n"
830 " -b|--bits bits-spec Walk pages with specified bits\n"
831 " -c|--cgroup path|@inode Walk pages within memory cgroup\n"
832 " -p|--pid pid Walk process address space\n"
833 " -f|--file filename Walk file address space\n"
834 " -i|--mark-idle Mark pages idle\n"
835 " -l|--list Show page details in ranges\n"
836 " -L|--list-each Show page details one by one\n"
837 " -C|--list-cgroup Show cgroup inode for pages\n"
838 " -M|--list-mapcnt Show page map count\n"
839 " -N|--no-summary Don't show summary info\n"
840 " -X|--hwpoison hwpoison pages\n"
841 " -x|--unpoison unpoison pages\n"
842 " -F|--kpageflags filename kpageflags file to parse\n"
843 " -h|--help Show this usage message\n"
845 " 0x10 bitfield format, e.g.\n"
846 " anon bit-name, e.g.\n"
847 " 0x10,anon comma-separated list, e.g.\n"
849 " N one page at offset N (unit: pages)\n"
850 " N+M pages range from N to N+M-1\n"
851 " N,M pages range from N to M-1\n"
852 " N, pages range from N to end\n"
853 " ,M pages range from 0 to M-1\n"
855 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
856 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
857 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
858 " =bit1,bit2 flags == (bit1|bit2)\n"
862 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
863 if (!page_flag_names
[i
])
865 printf("%16s%s", page_flag_names
[i
] + 2,
866 page_flag_type(1ULL << i
));
873 "(r) raw mode bits (o) overloaded bits\n");
876 static unsigned long long parse_number(const char *str
)
878 unsigned long long n
;
880 n
= strtoll(str
, NULL
, 0);
882 if (n
== 0 && str
[0] != '0')
883 fatal("invalid name or number: %s\n", str
);
888 static void parse_pid(const char *str
)
893 opt_pid
= parse_number(str
);
895 sprintf(buf
, "/proc/%d/pagemap", opt_pid
);
896 pagemap_fd
= checked_open(buf
, O_RDONLY
);
898 sprintf(buf
, "/proc/%d/maps", opt_pid
);
899 file
= fopen(buf
, "r");
905 while (fgets(buf
, sizeof(buf
), file
) != NULL
) {
906 unsigned long vm_start
;
907 unsigned long vm_end
;
908 unsigned long long pgoff
;
914 n
= sscanf(buf
, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
922 fprintf(stderr
, "unexpected line: %s\n", buf
);
925 pg_start
[nr_vmas
] = vm_start
/ page_size
;
926 pg_end
[nr_vmas
] = vm_end
/ page_size
;
927 if (++nr_vmas
>= MAX_VMAS
) {
928 fprintf(stderr
, "too many VMAs\n");
935 static void show_file(const char *name
, const struct stat
*st
)
937 unsigned long long size
= st
->st_size
;
938 char atime
[64], mtime
[64];
939 long now
= time(NULL
);
941 printf("%s\tInode: %u\tSize: %llu (%llu pages)\n",
942 name
, (unsigned)st
->st_ino
,
943 size
, (size
+ page_size
- 1) / page_size
);
945 strftime(atime
, sizeof(atime
), "%c", localtime(&st
->st_atime
));
946 strftime(mtime
, sizeof(mtime
), "%c", localtime(&st
->st_mtime
));
948 printf("Modify: %s (%ld seconds ago)\nAccess: %s (%ld seconds ago)\n",
949 mtime
, now
- st
->st_mtime
,
950 atime
, now
- st
->st_atime
);
953 static sigjmp_buf sigbus_jmp
;
955 static void * volatile sigbus_addr
;
957 static void sigbus_handler(int sig
, siginfo_t
*info
, void *ucontex
)
961 sigbus_addr
= info
? info
->si_addr
: NULL
;
962 siglongjmp(sigbus_jmp
, 1);
965 static struct sigaction sigbus_action
= {
966 .sa_sigaction
= sigbus_handler
,
967 .sa_flags
= SA_SIGINFO
,
970 static void walk_file(const char *name
, const struct stat
*st
)
972 uint8_t vec
[PAGEMAP_BATCH
];
973 uint64_t buf
[PAGEMAP_BATCH
], flags
;
976 unsigned long nr_pages
, pfn
, i
;
977 off_t off
, end
= st
->st_size
;
983 fd
= checked_open(name
, O_RDONLY
|O_NOATIME
|O_NOFOLLOW
);
985 for (off
= 0; off
< end
; off
+= len
) {
986 nr_pages
= (end
- off
+ page_size
- 1) / page_size
;
987 if (nr_pages
> PAGEMAP_BATCH
)
988 nr_pages
= PAGEMAP_BATCH
;
989 len
= nr_pages
* page_size
;
991 ptr
= mmap(NULL
, len
, PROT_READ
, MAP_SHARED
, fd
, off
);
992 if (ptr
== MAP_FAILED
)
993 fatal("mmap failed: %s", name
);
995 /* determine cached pages */
996 if (mincore(ptr
, len
, vec
))
997 fatal("mincore failed: %s", name
);
999 /* turn off readahead */
1000 if (madvise(ptr
, len
, MADV_RANDOM
))
1001 fatal("madvice failed: %s", name
);
1003 if (sigsetjmp(sigbus_jmp
, 1)) {
1004 end
= off
+ sigbus_addr
? sigbus_addr
- ptr
: 0;
1005 fprintf(stderr
, "got sigbus at offset %lld: %s\n",
1006 (long long)end
, name
);
1011 for (i
= 0; i
< nr_pages
; i
++) {
1013 (void)*(volatile int *)(ptr
+ i
* page_size
);
1017 /* turn off harvesting reference bits */
1018 if (madvise(ptr
, len
, MADV_SEQUENTIAL
))
1019 fatal("madvice failed: %s", name
);
1021 if (pagemap_read(buf
, (unsigned long)ptr
/ page_size
,
1022 nr_pages
) != nr_pages
)
1023 fatal("cannot read pagemap");
1027 for (i
= 0; i
< nr_pages
; i
++) {
1028 pfn
= pagemap_pfn(buf
[i
]);
1031 if (!kpageflags_read(&flags
, pfn
, 1))
1033 if (!kpagecgroup_read(&cgroup
, pfn
, 1))
1034 fatal("kpagecgroup_read failed");
1035 if (!kpagecount_read(&mapcnt
, pfn
, 1))
1036 fatal("kpagecount_read failed");
1037 if (first
&& opt_list
) {
1040 show_file(name
, st
);
1042 add_page(off
/ page_size
+ i
, pfn
,
1043 flags
, cgroup
, mapcnt
, buf
[i
]);
1050 int walk_tree(const char *name
, const struct stat
*st
, int type
, struct FTW
*f
)
1055 if (S_ISREG(st
->st_mode
))
1056 walk_file(name
, st
);
1059 fprintf(stderr
, "cannot read dir: %s\n", name
);
1065 static void walk_page_cache(void)
1069 kpageflags_fd
= checked_open(opt_kpageflags
, O_RDONLY
);
1070 pagemap_fd
= checked_open("/proc/self/pagemap", O_RDONLY
);
1071 sigaction(SIGBUS
, &sigbus_action
, NULL
);
1073 if (stat(opt_file
, &st
))
1074 fatal("stat failed: %s\n", opt_file
);
1076 if (S_ISREG(st
.st_mode
)) {
1077 walk_file(opt_file
, &st
);
1078 } else if (S_ISDIR(st
.st_mode
)) {
1079 /* do not follow symlinks and mountpoints */
1080 if (nftw(opt_file
, walk_tree
, 64, FTW_MOUNT
| FTW_PHYS
) < 0)
1081 fatal("nftw failed: %s\n", opt_file
);
1083 fatal("unhandled file type: %s\n", opt_file
);
1085 close(kpageflags_fd
);
1087 signal(SIGBUS
, SIG_DFL
);
1090 static void parse_file(const char *name
)
1095 static void parse_cgroup(const char *path
)
1097 if (path
[0] == '@') {
1098 opt_cgroup
= parse_number(path
+ 1);
1104 if (stat(path
, &st
))
1105 fatal("stat failed: %s: %m\n", path
);
1107 if (!S_ISDIR(st
.st_mode
))
1108 fatal("cgroup supposed to be a directory: %s\n", path
);
1110 opt_cgroup
= st
.st_ino
;
1113 static void parse_addr_range(const char *optarg
)
1115 unsigned long offset
;
1119 p
= strchr(optarg
, ',');
1121 p
= strchr(optarg
, '+');
1125 size
= parse_number(p
+ 1);
1127 offset
= parse_number(optarg
);
1131 size
= parse_number(p
+ 1);
1134 fatal("invalid range: %lu,%lu\n",
1140 offset
= parse_number(optarg
);
1144 add_addr_range(offset
, size
);
1147 static void add_bits_filter(uint64_t mask
, uint64_t bits
)
1149 if (nr_bit_filters
>= MAX_BIT_FILTERS
)
1150 fatal("too much bit filters\n");
1152 opt_mask
[nr_bit_filters
] = mask
;
1153 opt_bits
[nr_bit_filters
] = bits
;
1157 static uint64_t parse_flag_name(const char *str
, int len
)
1164 if (len
<= 8 && !strncmp(str
, "compound", len
))
1165 return BITS_COMPOUND
;
1167 for (i
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
1168 if (!page_flag_names
[i
])
1170 if (!strncmp(str
, page_flag_names
[i
] + 2, len
))
1174 return parse_number(str
);
1177 static uint64_t parse_flag_names(const char *str
, int all
)
1179 const char *p
= str
;
1183 if (*p
== ',' || *p
== '=' || *p
== '\0') {
1184 if ((*str
!= '~') || (*str
== '~' && all
&& *++str
))
1185 flags
|= parse_flag_name(str
, p
- str
);
1196 static void parse_bits_mask(const char *optarg
)
1202 p
= strchr(optarg
, '=');
1204 mask
= KPF_ALL_BITS
;
1205 bits
= parse_flag_names(p
+ 1, 0);
1207 mask
= parse_flag_names(optarg
, 0);
1208 bits
= parse_flag_names(p
+ 1, 0);
1209 } else if (strchr(optarg
, '~')) {
1210 mask
= parse_flag_names(optarg
, 1);
1211 bits
= parse_flag_names(optarg
, 0);
1213 mask
= parse_flag_names(optarg
, 0);
1214 bits
= KPF_ALL_BITS
;
1217 add_bits_filter(mask
, bits
);
1220 static void parse_kpageflags(const char *name
)
1222 opt_kpageflags
= name
;
1225 static void describe_flags(const char *optarg
)
1227 uint64_t flags
= parse_flag_names(optarg
, 0);
1229 printf("0x%016llx\t%s\t%s\n",
1230 (unsigned long long)flags
,
1231 page_flag_name(flags
),
1232 page_flag_longname(flags
));
1235 static const struct option opts
[] = {
1236 { "raw" , 0, NULL
, 'r' },
1237 { "pid" , 1, NULL
, 'p' },
1238 { "file" , 1, NULL
, 'f' },
1239 { "addr" , 1, NULL
, 'a' },
1240 { "bits" , 1, NULL
, 'b' },
1241 { "cgroup" , 1, NULL
, 'c' },
1242 { "describe" , 1, NULL
, 'd' },
1243 { "mark-idle" , 0, NULL
, 'i' },
1244 { "list" , 0, NULL
, 'l' },
1245 { "list-each" , 0, NULL
, 'L' },
1246 { "list-cgroup", 0, NULL
, 'C' },
1247 { "list-mapcnt", 0, NULL
, 'M' },
1248 { "no-summary", 0, NULL
, 'N' },
1249 { "hwpoison" , 0, NULL
, 'X' },
1250 { "unpoison" , 0, NULL
, 'x' },
1251 { "kpageflags", 0, NULL
, 'F' },
1252 { "help" , 0, NULL
, 'h' },
1253 { NULL
, 0, NULL
, 0 }
1256 int main(int argc
, char *argv
[])
1260 page_size
= getpagesize();
1262 while ((c
= getopt_long(argc
, argv
,
1263 "rp:f:a:b:d:c:CilLMNXxF:h",
1264 opts
, NULL
)) != -1) {
1276 parse_addr_range(optarg
);
1279 parse_bits_mask(optarg
);
1282 parse_cgroup(optarg
);
1285 opt_list_cgroup
= 1;
1288 describe_flags(optarg
);
1300 opt_list_mapcnt
= 1;
1307 prepare_hwpoison_fd();
1311 prepare_hwpoison_fd();
1314 parse_kpageflags(optarg
);
1325 if (!opt_kpageflags
)
1326 opt_kpageflags
= PROC_KPAGEFLAGS
;
1328 if (opt_cgroup
|| opt_list_cgroup
)
1329 kpagecgroup_fd
= checked_open(PROC_KPAGECGROUP
, O_RDONLY
);
1331 if (opt_list
&& opt_list_mapcnt
)
1332 kpagecount_fd
= checked_open(PROC_KPAGECOUNT
, O_RDONLY
);
1334 if (opt_mark_idle
&& opt_file
)
1335 page_idle_fd
= checked_open(SYS_KERNEL_MM_PAGE_IDLE
, O_RDWR
);
1337 if (opt_list
&& opt_pid
)
1338 printf("voffset\t");
1339 if (opt_list
&& opt_file
)
1340 printf("foffset\t");
1341 if (opt_list
&& opt_list_cgroup
)
1343 if (opt_list
&& opt_list_mapcnt
)
1344 printf("map-cnt\t");
1347 printf("offset\tlen\tflags\n");
1349 printf("offset\tflags\n");
1367 if (opt_list_mapcnt
)
1368 close(kpagecount_fd
);
1370 if (page_idle_fd
>= 0)
1371 close(page_idle_fd
);