dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / tools / vm / page-types.c
blobc7fcc84fc0c09dd01e6695ba248eb929d345033d
1 /*
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
11 * more details.
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
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdint.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <getopt.h>
31 #include <limits.h>
32 #include <assert.h>
33 #include <ftw.h>
34 #include <time.h>
35 #include <setjmp.h>
36 #include <signal.h>
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>
42 #include <sys/mman.h>
43 #include "../../include/uapi/linux/magic.h"
44 #include "../../include/uapi/linux/kernel-page-flags.h"
45 #include <api/fs/fs.h>
47 #ifndef MAX_PATH
48 # define MAX_PATH 256
49 #endif
51 #ifndef STR
52 # define _STR(x) #x
53 # define STR(x) _STR(x)
54 #endif
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 PM_SOFT_DIRTY (1ULL << 55)
65 #define PM_MMAP_EXCLUSIVE (1ULL << 56)
66 #define PM_FILE (1ULL << 61)
67 #define PM_SWAP (1ULL << 62)
68 #define PM_PRESENT (1ULL << 63)
71 * kernel page flags
74 #define KPF_BYTES 8
75 #define PROC_KPAGEFLAGS "/proc/kpageflags"
77 /* [32-] kernel hacking assistances */
78 #define KPF_RESERVED 32
79 #define KPF_MLOCKED 33
80 #define KPF_MAPPEDTODISK 34
81 #define KPF_PRIVATE 35
82 #define KPF_PRIVATE_2 36
83 #define KPF_OWNER_PRIVATE 37
84 #define KPF_ARCH 38
85 #define KPF_UNCACHED 39
86 #define KPF_SOFTDIRTY 40
88 /* [48-] take some arbitrary free slots for expanding overloaded flags
89 * not part of kernel API
91 #define KPF_READAHEAD 48
92 #define KPF_SLOB_FREE 49
93 #define KPF_SLUB_FROZEN 50
94 #define KPF_SLUB_DEBUG 51
95 #define KPF_FILE 62
96 #define KPF_MMAP_EXCLUSIVE 63
98 #define KPF_ALL_BITS ((uint64_t)~0ULL)
99 #define KPF_HACKERS_BITS (0xffffULL << 32)
100 #define KPF_OVERLOADED_BITS (0xffffULL << 48)
101 #define BIT(name) (1ULL << KPF_##name)
102 #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
104 static const char * const page_flag_names[] = {
105 [KPF_LOCKED] = "L:locked",
106 [KPF_ERROR] = "E:error",
107 [KPF_REFERENCED] = "R:referenced",
108 [KPF_UPTODATE] = "U:uptodate",
109 [KPF_DIRTY] = "D:dirty",
110 [KPF_LRU] = "l:lru",
111 [KPF_ACTIVE] = "A:active",
112 [KPF_SLAB] = "S:slab",
113 [KPF_WRITEBACK] = "W:writeback",
114 [KPF_RECLAIM] = "I:reclaim",
115 [KPF_BUDDY] = "B:buddy",
117 [KPF_MMAP] = "M:mmap",
118 [KPF_ANON] = "a:anonymous",
119 [KPF_SWAPCACHE] = "s:swapcache",
120 [KPF_SWAPBACKED] = "b:swapbacked",
121 [KPF_COMPOUND_HEAD] = "H:compound_head",
122 [KPF_COMPOUND_TAIL] = "T:compound_tail",
123 [KPF_HUGE] = "G:huge",
124 [KPF_UNEVICTABLE] = "u:unevictable",
125 [KPF_HWPOISON] = "X:hwpoison",
126 [KPF_NOPAGE] = "n:nopage",
127 [KPF_KSM] = "x:ksm",
128 [KPF_THP] = "t:thp",
129 [KPF_BALLOON] = "o:balloon",
130 [KPF_ZERO_PAGE] = "z:zero_page",
131 [KPF_IDLE] = "i:idle_page",
133 [KPF_RESERVED] = "r:reserved",
134 [KPF_MLOCKED] = "m:mlocked",
135 [KPF_MAPPEDTODISK] = "d:mappedtodisk",
136 [KPF_PRIVATE] = "P:private",
137 [KPF_PRIVATE_2] = "p:private_2",
138 [KPF_OWNER_PRIVATE] = "O:owner_private",
139 [KPF_ARCH] = "h:arch",
140 [KPF_UNCACHED] = "c:uncached",
141 [KPF_SOFTDIRTY] = "f:softdirty",
143 [KPF_READAHEAD] = "I:readahead",
144 [KPF_SLOB_FREE] = "P:slob_free",
145 [KPF_SLUB_FROZEN] = "A:slub_frozen",
146 [KPF_SLUB_DEBUG] = "E:slub_debug",
148 [KPF_FILE] = "F:file",
149 [KPF_MMAP_EXCLUSIVE] = "1:mmap_exclusive",
154 * data structures
157 static int opt_raw; /* for kernel developers */
158 static int opt_list; /* list pages (in ranges) */
159 static int opt_no_summary; /* don't show summary */
160 static pid_t opt_pid; /* process to walk */
161 const char * opt_file;
163 #define MAX_ADDR_RANGES 1024
164 static int nr_addr_ranges;
165 static unsigned long opt_offset[MAX_ADDR_RANGES];
166 static unsigned long opt_size[MAX_ADDR_RANGES];
168 #define MAX_VMAS 10240
169 static int nr_vmas;
170 static unsigned long pg_start[MAX_VMAS];
171 static unsigned long pg_end[MAX_VMAS];
173 #define MAX_BIT_FILTERS 64
174 static int nr_bit_filters;
175 static uint64_t opt_mask[MAX_BIT_FILTERS];
176 static uint64_t opt_bits[MAX_BIT_FILTERS];
178 static int page_size;
180 static int pagemap_fd;
181 static int kpageflags_fd;
183 static int opt_hwpoison;
184 static int opt_unpoison;
186 static const char *hwpoison_debug_fs;
187 static int hwpoison_inject_fd;
188 static int hwpoison_forget_fd;
190 #define HASH_SHIFT 13
191 #define HASH_SIZE (1 << HASH_SHIFT)
192 #define HASH_MASK (HASH_SIZE - 1)
193 #define HASH_KEY(flags) (flags & HASH_MASK)
195 static unsigned long total_pages;
196 static unsigned long nr_pages[HASH_SIZE];
197 static uint64_t page_flags[HASH_SIZE];
201 * helper functions
204 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
206 #define min_t(type, x, y) ({ \
207 type __min1 = (x); \
208 type __min2 = (y); \
209 __min1 < __min2 ? __min1 : __min2; })
211 #define max_t(type, x, y) ({ \
212 type __max1 = (x); \
213 type __max2 = (y); \
214 __max1 > __max2 ? __max1 : __max2; })
216 static unsigned long pages2mb(unsigned long pages)
218 return (pages * page_size) >> 20;
221 static void fatal(const char *x, ...)
223 va_list ap;
225 va_start(ap, x);
226 vfprintf(stderr, x, ap);
227 va_end(ap);
228 exit(EXIT_FAILURE);
231 static int checked_open(const char *pathname, int flags)
233 int fd = open(pathname, flags);
235 if (fd < 0) {
236 perror(pathname);
237 exit(EXIT_FAILURE);
240 return fd;
244 * pagemap/kpageflags routines
247 static unsigned long do_u64_read(int fd, char *name,
248 uint64_t *buf,
249 unsigned long index,
250 unsigned long count)
252 long bytes;
254 if (index > ULONG_MAX / 8)
255 fatal("index overflow: %lu\n", index);
257 bytes = pread(fd, buf, count * 8, (off_t)index * 8);
258 if (bytes < 0) {
259 perror(name);
260 exit(EXIT_FAILURE);
262 if (bytes % 8)
263 fatal("partial read: %lu bytes\n", bytes);
265 return bytes / 8;
268 static unsigned long kpageflags_read(uint64_t *buf,
269 unsigned long index,
270 unsigned long pages)
272 return do_u64_read(kpageflags_fd, PROC_KPAGEFLAGS, buf, index, pages);
275 static unsigned long pagemap_read(uint64_t *buf,
276 unsigned long index,
277 unsigned long pages)
279 return do_u64_read(pagemap_fd, "/proc/pid/pagemap", buf, index, pages);
282 static unsigned long pagemap_pfn(uint64_t val)
284 unsigned long pfn;
286 if (val & PM_PRESENT)
287 pfn = PM_PFRAME(val);
288 else
289 pfn = 0;
291 return pfn;
296 * page flag names
299 static char *page_flag_name(uint64_t flags)
301 static char buf[65];
302 int present;
303 size_t i, j;
305 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
306 present = (flags >> i) & 1;
307 if (!page_flag_names[i]) {
308 if (present)
309 fatal("unknown flag bit %d\n", i);
310 continue;
312 buf[j++] = present ? page_flag_names[i][0] : '_';
315 return buf;
318 static char *page_flag_longname(uint64_t flags)
320 static char buf[1024];
321 size_t i, n;
323 for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
324 if (!page_flag_names[i])
325 continue;
326 if ((flags >> i) & 1)
327 n += snprintf(buf + n, sizeof(buf) - n, "%s,",
328 page_flag_names[i] + 2);
330 if (n)
331 n--;
332 buf[n] = '\0';
334 return buf;
339 * page list and summary
342 static void show_page_range(unsigned long voffset, unsigned long offset,
343 unsigned long size, uint64_t flags)
345 static uint64_t flags0;
346 static unsigned long voff;
347 static unsigned long index;
348 static unsigned long count;
350 if (flags == flags0 && offset == index + count &&
351 size && voffset == voff + count) {
352 count += size;
353 return;
356 if (count) {
357 if (opt_pid)
358 printf("%lx\t", voff);
359 if (opt_file)
360 printf("%lu\t", voff);
361 printf("%lx\t%lx\t%s\n",
362 index, count, page_flag_name(flags0));
365 flags0 = flags;
366 index = offset;
367 voff = voffset;
368 count = size;
371 static void flush_page_range(void)
373 show_page_range(0, 0, 0, 0);
376 static void show_page(unsigned long voffset,
377 unsigned long offset, uint64_t flags)
379 if (opt_pid)
380 printf("%lx\t", voffset);
381 if (opt_file)
382 printf("%lu\t", voffset);
383 printf("%lx\t%s\n", offset, page_flag_name(flags));
386 static void show_summary(void)
388 size_t i;
390 printf(" flags\tpage-count MB"
391 " symbolic-flags\t\t\tlong-symbolic-flags\n");
393 for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
394 if (nr_pages[i])
395 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
396 (unsigned long long)page_flags[i],
397 nr_pages[i],
398 pages2mb(nr_pages[i]),
399 page_flag_name(page_flags[i]),
400 page_flag_longname(page_flags[i]));
403 printf(" total\t%10lu %8lu\n",
404 total_pages, pages2mb(total_pages));
409 * page flag filters
412 static int bit_mask_ok(uint64_t flags)
414 int i;
416 for (i = 0; i < nr_bit_filters; i++) {
417 if (opt_bits[i] == KPF_ALL_BITS) {
418 if ((flags & opt_mask[i]) == 0)
419 return 0;
420 } else {
421 if ((flags & opt_mask[i]) != opt_bits[i])
422 return 0;
426 return 1;
429 static uint64_t expand_overloaded_flags(uint64_t flags, uint64_t pme)
431 /* SLOB/SLUB overload several page flags */
432 if (flags & BIT(SLAB)) {
433 if (flags & BIT(PRIVATE))
434 flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
435 if (flags & BIT(ACTIVE))
436 flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
437 if (flags & BIT(ERROR))
438 flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
441 /* PG_reclaim is overloaded as PG_readahead in the read path */
442 if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
443 flags ^= BIT(RECLAIM) | BIT(READAHEAD);
445 if (pme & PM_SOFT_DIRTY)
446 flags |= BIT(SOFTDIRTY);
447 if (pme & PM_FILE)
448 flags |= BIT(FILE);
449 if (pme & PM_MMAP_EXCLUSIVE)
450 flags |= BIT(MMAP_EXCLUSIVE);
452 return flags;
455 static uint64_t well_known_flags(uint64_t flags)
457 /* hide flags intended only for kernel hacker */
458 flags &= ~KPF_HACKERS_BITS;
460 /* hide non-hugeTLB compound pages */
461 if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
462 flags &= ~BITS_COMPOUND;
464 return flags;
467 static uint64_t kpageflags_flags(uint64_t flags, uint64_t pme)
469 if (opt_raw)
470 flags = expand_overloaded_flags(flags, pme);
471 else
472 flags = well_known_flags(flags);
474 return flags;
478 * page actions
481 static void prepare_hwpoison_fd(void)
483 char buf[MAX_PATH + 1];
485 hwpoison_debug_fs = debugfs__mount();
486 if (!hwpoison_debug_fs) {
487 perror("mount debugfs");
488 exit(EXIT_FAILURE);
491 if (opt_hwpoison && !hwpoison_inject_fd) {
492 snprintf(buf, MAX_PATH, "%s/hwpoison/corrupt-pfn",
493 hwpoison_debug_fs);
494 hwpoison_inject_fd = checked_open(buf, O_WRONLY);
497 if (opt_unpoison && !hwpoison_forget_fd) {
498 snprintf(buf, MAX_PATH, "%s/hwpoison/unpoison-pfn",
499 hwpoison_debug_fs);
500 hwpoison_forget_fd = checked_open(buf, O_WRONLY);
504 static int hwpoison_page(unsigned long offset)
506 char buf[100];
507 int len;
509 len = sprintf(buf, "0x%lx\n", offset);
510 len = write(hwpoison_inject_fd, buf, len);
511 if (len < 0) {
512 perror("hwpoison inject");
513 return len;
515 return 0;
518 static int unpoison_page(unsigned long offset)
520 char buf[100];
521 int len;
523 len = sprintf(buf, "0x%lx\n", offset);
524 len = write(hwpoison_forget_fd, buf, len);
525 if (len < 0) {
526 perror("hwpoison forget");
527 return len;
529 return 0;
533 * page frame walker
536 static size_t hash_slot(uint64_t flags)
538 size_t k = HASH_KEY(flags);
539 size_t i;
541 /* Explicitly reserve slot 0 for flags 0: the following logic
542 * cannot distinguish an unoccupied slot from slot (flags==0).
544 if (flags == 0)
545 return 0;
547 /* search through the remaining (HASH_SIZE-1) slots */
548 for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) {
549 if (!k || k >= ARRAY_SIZE(page_flags))
550 k = 1;
551 if (page_flags[k] == 0) {
552 page_flags[k] = flags;
553 return k;
555 if (page_flags[k] == flags)
556 return k;
559 fatal("hash table full: bump up HASH_SHIFT?\n");
560 exit(EXIT_FAILURE);
563 static void add_page(unsigned long voffset,
564 unsigned long offset, uint64_t flags, uint64_t pme)
566 flags = kpageflags_flags(flags, pme);
568 if (!bit_mask_ok(flags))
569 return;
571 if (opt_hwpoison)
572 hwpoison_page(offset);
573 if (opt_unpoison)
574 unpoison_page(offset);
576 if (opt_list == 1)
577 show_page_range(voffset, offset, 1, flags);
578 else if (opt_list == 2)
579 show_page(voffset, offset, flags);
581 nr_pages[hash_slot(flags)]++;
582 total_pages++;
585 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
586 static void walk_pfn(unsigned long voffset,
587 unsigned long index,
588 unsigned long count,
589 uint64_t pme)
591 uint64_t buf[KPAGEFLAGS_BATCH];
592 unsigned long batch;
593 unsigned long pages;
594 unsigned long i;
596 while (count) {
597 batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
598 pages = kpageflags_read(buf, index, batch);
599 if (pages == 0)
600 break;
602 for (i = 0; i < pages; i++)
603 add_page(voffset + i, index + i, buf[i], pme);
605 index += pages;
606 count -= pages;
610 #define PAGEMAP_BATCH (64 << 10)
611 static void walk_vma(unsigned long index, unsigned long count)
613 uint64_t buf[PAGEMAP_BATCH];
614 unsigned long batch;
615 unsigned long pages;
616 unsigned long pfn;
617 unsigned long i;
619 while (count) {
620 batch = min_t(unsigned long, count, PAGEMAP_BATCH);
621 pages = pagemap_read(buf, index, batch);
622 if (pages == 0)
623 break;
625 for (i = 0; i < pages; i++) {
626 pfn = pagemap_pfn(buf[i]);
627 if (pfn)
628 walk_pfn(index + i, pfn, 1, buf[i]);
631 index += pages;
632 count -= pages;
636 static void walk_task(unsigned long index, unsigned long count)
638 const unsigned long end = index + count;
639 unsigned long start;
640 int i = 0;
642 while (index < end) {
644 while (pg_end[i] <= index)
645 if (++i >= nr_vmas)
646 return;
647 if (pg_start[i] >= end)
648 return;
650 start = max_t(unsigned long, pg_start[i], index);
651 index = min_t(unsigned long, pg_end[i], end);
653 assert(start < index);
654 walk_vma(start, index - start);
658 static void add_addr_range(unsigned long offset, unsigned long size)
660 if (nr_addr_ranges >= MAX_ADDR_RANGES)
661 fatal("too many addr ranges\n");
663 opt_offset[nr_addr_ranges] = offset;
664 opt_size[nr_addr_ranges] = min_t(unsigned long, size, ULONG_MAX-offset);
665 nr_addr_ranges++;
668 static void walk_addr_ranges(void)
670 int i;
672 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
674 if (!nr_addr_ranges)
675 add_addr_range(0, ULONG_MAX);
677 for (i = 0; i < nr_addr_ranges; i++)
678 if (!opt_pid)
679 walk_pfn(opt_offset[i], opt_offset[i], opt_size[i], 0);
680 else
681 walk_task(opt_offset[i], opt_size[i]);
683 close(kpageflags_fd);
688 * user interface
691 static const char *page_flag_type(uint64_t flag)
693 if (flag & KPF_HACKERS_BITS)
694 return "(r)";
695 if (flag & KPF_OVERLOADED_BITS)
696 return "(o)";
697 return " ";
700 static void usage(void)
702 size_t i, j;
704 printf(
705 "page-types [options]\n"
706 " -r|--raw Raw mode, for kernel developers\n"
707 " -d|--describe flags Describe flags\n"
708 " -a|--addr addr-spec Walk a range of pages\n"
709 " -b|--bits bits-spec Walk pages with specified bits\n"
710 " -p|--pid pid Walk process address space\n"
711 " -f|--file filename Walk file address space\n"
712 " -l|--list Show page details in ranges\n"
713 " -L|--list-each Show page details one by one\n"
714 " -N|--no-summary Don't show summary info\n"
715 " -X|--hwpoison hwpoison pages\n"
716 " -x|--unpoison unpoison pages\n"
717 " -h|--help Show this usage message\n"
718 "flags:\n"
719 " 0x10 bitfield format, e.g.\n"
720 " anon bit-name, e.g.\n"
721 " 0x10,anon comma-separated list, e.g.\n"
722 "addr-spec:\n"
723 " N one page at offset N (unit: pages)\n"
724 " N+M pages range from N to N+M-1\n"
725 " N,M pages range from N to M-1\n"
726 " N, pages range from N to end\n"
727 " ,M pages range from 0 to M-1\n"
728 "bits-spec:\n"
729 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
730 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
731 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
732 " =bit1,bit2 flags == (bit1|bit2)\n"
733 "bit-names:\n"
736 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
737 if (!page_flag_names[i])
738 continue;
739 printf("%16s%s", page_flag_names[i] + 2,
740 page_flag_type(1ULL << i));
741 if (++j > 3) {
742 j = 0;
743 putchar('\n');
746 printf("\n "
747 "(r) raw mode bits (o) overloaded bits\n");
750 static unsigned long long parse_number(const char *str)
752 unsigned long long n;
754 n = strtoll(str, NULL, 0);
756 if (n == 0 && str[0] != '0')
757 fatal("invalid name or number: %s\n", str);
759 return n;
762 static void parse_pid(const char *str)
764 FILE *file;
765 char buf[5000];
767 opt_pid = parse_number(str);
769 sprintf(buf, "/proc/%d/pagemap", opt_pid);
770 pagemap_fd = checked_open(buf, O_RDONLY);
772 sprintf(buf, "/proc/%d/maps", opt_pid);
773 file = fopen(buf, "r");
774 if (!file) {
775 perror(buf);
776 exit(EXIT_FAILURE);
779 while (fgets(buf, sizeof(buf), file) != NULL) {
780 unsigned long vm_start;
781 unsigned long vm_end;
782 unsigned long long pgoff;
783 int major, minor;
784 char r, w, x, s;
785 unsigned long ino;
786 int n;
788 n = sscanf(buf, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
789 &vm_start,
790 &vm_end,
791 &r, &w, &x, &s,
792 &pgoff,
793 &major, &minor,
794 &ino);
795 if (n < 10) {
796 fprintf(stderr, "unexpected line: %s\n", buf);
797 continue;
799 pg_start[nr_vmas] = vm_start / page_size;
800 pg_end[nr_vmas] = vm_end / page_size;
801 if (++nr_vmas >= MAX_VMAS) {
802 fprintf(stderr, "too many VMAs\n");
803 break;
806 fclose(file);
809 static void show_file(const char *name, const struct stat *st)
811 unsigned long long size = st->st_size;
812 char atime[64], mtime[64];
813 long now = time(NULL);
815 printf("%s\tInode: %u\tSize: %llu (%llu pages)\n",
816 name, (unsigned)st->st_ino,
817 size, (size + page_size - 1) / page_size);
819 strftime(atime, sizeof(atime), "%c", localtime(&st->st_atime));
820 strftime(mtime, sizeof(mtime), "%c", localtime(&st->st_mtime));
822 printf("Modify: %s (%ld seconds ago)\nAccess: %s (%ld seconds ago)\n",
823 mtime, now - st->st_mtime,
824 atime, now - st->st_atime);
827 static sigjmp_buf sigbus_jmp;
829 static void * volatile sigbus_addr;
831 static void sigbus_handler(int sig, siginfo_t *info, void *ucontex)
833 (void)sig;
834 (void)ucontex;
835 sigbus_addr = info ? info->si_addr : NULL;
836 siglongjmp(sigbus_jmp, 1);
839 static struct sigaction sigbus_action = {
840 .sa_sigaction = sigbus_handler,
841 .sa_flags = SA_SIGINFO,
844 static void walk_file(const char *name, const struct stat *st)
846 uint8_t vec[PAGEMAP_BATCH];
847 uint64_t buf[PAGEMAP_BATCH], flags;
848 unsigned long nr_pages, pfn, i;
849 off_t off, end = st->st_size;
850 int fd;
851 ssize_t len;
852 void *ptr;
853 int first = 1;
855 fd = checked_open(name, O_RDONLY|O_NOATIME|O_NOFOLLOW);
857 for (off = 0; off < end; off += len) {
858 nr_pages = (end - off + page_size - 1) / page_size;
859 if (nr_pages > PAGEMAP_BATCH)
860 nr_pages = PAGEMAP_BATCH;
861 len = nr_pages * page_size;
863 ptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, off);
864 if (ptr == MAP_FAILED)
865 fatal("mmap failed: %s", name);
867 /* determine cached pages */
868 if (mincore(ptr, len, vec))
869 fatal("mincore failed: %s", name);
871 /* turn off readahead */
872 if (madvise(ptr, len, MADV_RANDOM))
873 fatal("madvice failed: %s", name);
875 if (sigsetjmp(sigbus_jmp, 1)) {
876 end = off + sigbus_addr ? sigbus_addr - ptr : 0;
877 fprintf(stderr, "got sigbus at offset %lld: %s\n",
878 (long long)end, name);
879 goto got_sigbus;
882 /* populate ptes */
883 for (i = 0; i < nr_pages ; i++) {
884 if (vec[i] & 1)
885 (void)*(volatile int *)(ptr + i * page_size);
887 got_sigbus:
889 /* turn off harvesting reference bits */
890 if (madvise(ptr, len, MADV_SEQUENTIAL))
891 fatal("madvice failed: %s", name);
893 if (pagemap_read(buf, (unsigned long)ptr / page_size,
894 nr_pages) != nr_pages)
895 fatal("cannot read pagemap");
897 munmap(ptr, len);
899 for (i = 0; i < nr_pages; i++) {
900 pfn = pagemap_pfn(buf[i]);
901 if (!pfn)
902 continue;
903 if (!kpageflags_read(&flags, pfn, 1))
904 continue;
905 if (first && opt_list) {
906 first = 0;
907 flush_page_range();
908 show_file(name, st);
910 add_page(off / page_size + i, pfn, flags, buf[i]);
914 close(fd);
917 int walk_tree(const char *name, const struct stat *st, int type, struct FTW *f)
919 (void)f;
920 switch (type) {
921 case FTW_F:
922 if (S_ISREG(st->st_mode))
923 walk_file(name, st);
924 break;
925 case FTW_DNR:
926 fprintf(stderr, "cannot read dir: %s\n", name);
927 break;
929 return 0;
932 static void walk_page_cache(void)
934 struct stat st;
936 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
937 pagemap_fd = checked_open("/proc/self/pagemap", O_RDONLY);
938 sigaction(SIGBUS, &sigbus_action, NULL);
940 if (stat(opt_file, &st))
941 fatal("stat failed: %s\n", opt_file);
943 if (S_ISREG(st.st_mode)) {
944 walk_file(opt_file, &st);
945 } else if (S_ISDIR(st.st_mode)) {
946 /* do not follow symlinks and mountpoints */
947 if (nftw(opt_file, walk_tree, 64, FTW_MOUNT | FTW_PHYS) < 0)
948 fatal("nftw failed: %s\n", opt_file);
949 } else
950 fatal("unhandled file type: %s\n", opt_file);
952 close(kpageflags_fd);
953 close(pagemap_fd);
954 signal(SIGBUS, SIG_DFL);
957 static void parse_file(const char *name)
959 opt_file = name;
962 static void parse_addr_range(const char *optarg)
964 unsigned long offset;
965 unsigned long size;
966 char *p;
968 p = strchr(optarg, ',');
969 if (!p)
970 p = strchr(optarg, '+');
972 if (p == optarg) {
973 offset = 0;
974 size = parse_number(p + 1);
975 } else if (p) {
976 offset = parse_number(optarg);
977 if (p[1] == '\0')
978 size = ULONG_MAX;
979 else {
980 size = parse_number(p + 1);
981 if (*p == ',') {
982 if (size < offset)
983 fatal("invalid range: %lu,%lu\n",
984 offset, size);
985 size -= offset;
988 } else {
989 offset = parse_number(optarg);
990 size = 1;
993 add_addr_range(offset, size);
996 static void add_bits_filter(uint64_t mask, uint64_t bits)
998 if (nr_bit_filters >= MAX_BIT_FILTERS)
999 fatal("too much bit filters\n");
1001 opt_mask[nr_bit_filters] = mask;
1002 opt_bits[nr_bit_filters] = bits;
1003 nr_bit_filters++;
1006 static uint64_t parse_flag_name(const char *str, int len)
1008 size_t i;
1010 if (!*str || !len)
1011 return 0;
1013 if (len <= 8 && !strncmp(str, "compound", len))
1014 return BITS_COMPOUND;
1016 for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
1017 if (!page_flag_names[i])
1018 continue;
1019 if (!strncmp(str, page_flag_names[i] + 2, len))
1020 return 1ULL << i;
1023 return parse_number(str);
1026 static uint64_t parse_flag_names(const char *str, int all)
1028 const char *p = str;
1029 uint64_t flags = 0;
1031 while (1) {
1032 if (*p == ',' || *p == '=' || *p == '\0') {
1033 if ((*str != '~') || (*str == '~' && all && *++str))
1034 flags |= parse_flag_name(str, p - str);
1035 if (*p != ',')
1036 break;
1037 str = p + 1;
1039 p++;
1042 return flags;
1045 static void parse_bits_mask(const char *optarg)
1047 uint64_t mask;
1048 uint64_t bits;
1049 const char *p;
1051 p = strchr(optarg, '=');
1052 if (p == optarg) {
1053 mask = KPF_ALL_BITS;
1054 bits = parse_flag_names(p + 1, 0);
1055 } else if (p) {
1056 mask = parse_flag_names(optarg, 0);
1057 bits = parse_flag_names(p + 1, 0);
1058 } else if (strchr(optarg, '~')) {
1059 mask = parse_flag_names(optarg, 1);
1060 bits = parse_flag_names(optarg, 0);
1061 } else {
1062 mask = parse_flag_names(optarg, 0);
1063 bits = KPF_ALL_BITS;
1066 add_bits_filter(mask, bits);
1069 static void describe_flags(const char *optarg)
1071 uint64_t flags = parse_flag_names(optarg, 0);
1073 printf("0x%016llx\t%s\t%s\n",
1074 (unsigned long long)flags,
1075 page_flag_name(flags),
1076 page_flag_longname(flags));
1079 static const struct option opts[] = {
1080 { "raw" , 0, NULL, 'r' },
1081 { "pid" , 1, NULL, 'p' },
1082 { "file" , 1, NULL, 'f' },
1083 { "addr" , 1, NULL, 'a' },
1084 { "bits" , 1, NULL, 'b' },
1085 { "describe" , 1, NULL, 'd' },
1086 { "list" , 0, NULL, 'l' },
1087 { "list-each" , 0, NULL, 'L' },
1088 { "no-summary", 0, NULL, 'N' },
1089 { "hwpoison" , 0, NULL, 'X' },
1090 { "unpoison" , 0, NULL, 'x' },
1091 { "help" , 0, NULL, 'h' },
1092 { NULL , 0, NULL, 0 }
1095 int main(int argc, char *argv[])
1097 int c;
1099 page_size = getpagesize();
1101 while ((c = getopt_long(argc, argv,
1102 "rp:f:a:b:d:lLNXxh", opts, NULL)) != -1) {
1103 switch (c) {
1104 case 'r':
1105 opt_raw = 1;
1106 break;
1107 case 'p':
1108 parse_pid(optarg);
1109 break;
1110 case 'f':
1111 parse_file(optarg);
1112 break;
1113 case 'a':
1114 parse_addr_range(optarg);
1115 break;
1116 case 'b':
1117 parse_bits_mask(optarg);
1118 break;
1119 case 'd':
1120 describe_flags(optarg);
1121 exit(0);
1122 case 'l':
1123 opt_list = 1;
1124 break;
1125 case 'L':
1126 opt_list = 2;
1127 break;
1128 case 'N':
1129 opt_no_summary = 1;
1130 break;
1131 case 'X':
1132 opt_hwpoison = 1;
1133 prepare_hwpoison_fd();
1134 break;
1135 case 'x':
1136 opt_unpoison = 1;
1137 prepare_hwpoison_fd();
1138 break;
1139 case 'h':
1140 usage();
1141 exit(0);
1142 default:
1143 usage();
1144 exit(1);
1148 if (opt_list && opt_pid)
1149 printf("voffset\t");
1150 if (opt_list && opt_file)
1151 printf("foffset\t");
1152 if (opt_list == 1)
1153 printf("offset\tlen\tflags\n");
1154 if (opt_list == 2)
1155 printf("offset\tflags\n");
1157 if (opt_file)
1158 walk_page_cache();
1159 else
1160 walk_addr_ranges();
1162 if (opt_list == 1)
1163 flush_page_range();
1165 if (opt_no_summary)
1166 return 0;
1168 if (opt_list)
1169 printf("\n\n");
1171 show_summary();
1173 return 0;