PM / Domains: Remove pm_genpd_poweron() API
[linux/fpc-iii.git] / tools / vm / page-types.c
blob7f73fa32a590b5f5177affb4b2e6df93dc3e2f2f
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/debugfs.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",
132 [KPF_RESERVED] = "r:reserved",
133 [KPF_MLOCKED] = "m:mlocked",
134 [KPF_MAPPEDTODISK] = "d:mappedtodisk",
135 [KPF_PRIVATE] = "P:private",
136 [KPF_PRIVATE_2] = "p:private_2",
137 [KPF_OWNER_PRIVATE] = "O:owner_private",
138 [KPF_ARCH] = "h:arch",
139 [KPF_UNCACHED] = "c:uncached",
140 [KPF_SOFTDIRTY] = "f:softdirty",
142 [KPF_READAHEAD] = "I:readahead",
143 [KPF_SLOB_FREE] = "P:slob_free",
144 [KPF_SLUB_FROZEN] = "A:slub_frozen",
145 [KPF_SLUB_DEBUG] = "E:slub_debug",
147 [KPF_FILE] = "F:file",
148 [KPF_MMAP_EXCLUSIVE] = "1:mmap_exclusive",
152 static const char * const debugfs_known_mountpoints[] = {
153 "/sys/kernel/debug",
154 "/debug",
159 * data structures
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;
168 #define MAX_ADDR_RANGES 1024
169 static int nr_addr_ranges;
170 static unsigned long opt_offset[MAX_ADDR_RANGES];
171 static unsigned long opt_size[MAX_ADDR_RANGES];
173 #define MAX_VMAS 10240
174 static int nr_vmas;
175 static unsigned long pg_start[MAX_VMAS];
176 static unsigned long pg_end[MAX_VMAS];
178 #define MAX_BIT_FILTERS 64
179 static int nr_bit_filters;
180 static uint64_t opt_mask[MAX_BIT_FILTERS];
181 static uint64_t opt_bits[MAX_BIT_FILTERS];
183 static int page_size;
185 static int pagemap_fd;
186 static int kpageflags_fd;
188 static int opt_hwpoison;
189 static int opt_unpoison;
191 static char *hwpoison_debug_fs;
192 static int hwpoison_inject_fd;
193 static int hwpoison_forget_fd;
195 #define HASH_SHIFT 13
196 #define HASH_SIZE (1 << HASH_SHIFT)
197 #define HASH_MASK (HASH_SIZE - 1)
198 #define HASH_KEY(flags) (flags & HASH_MASK)
200 static unsigned long total_pages;
201 static unsigned long nr_pages[HASH_SIZE];
202 static uint64_t page_flags[HASH_SIZE];
206 * helper functions
209 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
211 #define min_t(type, x, y) ({ \
212 type __min1 = (x); \
213 type __min2 = (y); \
214 __min1 < __min2 ? __min1 : __min2; })
216 #define max_t(type, x, y) ({ \
217 type __max1 = (x); \
218 type __max2 = (y); \
219 __max1 > __max2 ? __max1 : __max2; })
221 static unsigned long pages2mb(unsigned long pages)
223 return (pages * page_size) >> 20;
226 static void fatal(const char *x, ...)
228 va_list ap;
230 va_start(ap, x);
231 vfprintf(stderr, x, ap);
232 va_end(ap);
233 exit(EXIT_FAILURE);
236 static int checked_open(const char *pathname, int flags)
238 int fd = open(pathname, flags);
240 if (fd < 0) {
241 perror(pathname);
242 exit(EXIT_FAILURE);
245 return fd;
249 * pagemap/kpageflags routines
252 static unsigned long do_u64_read(int fd, char *name,
253 uint64_t *buf,
254 unsigned long index,
255 unsigned long count)
257 long bytes;
259 if (index > ULONG_MAX / 8)
260 fatal("index overflow: %lu\n", index);
262 bytes = pread(fd, buf, count * 8, (off_t)index * 8);
263 if (bytes < 0) {
264 perror(name);
265 exit(EXIT_FAILURE);
267 if (bytes % 8)
268 fatal("partial read: %lu bytes\n", bytes);
270 return bytes / 8;
273 static unsigned long kpageflags_read(uint64_t *buf,
274 unsigned long index,
275 unsigned long pages)
277 return do_u64_read(kpageflags_fd, PROC_KPAGEFLAGS, buf, index, pages);
280 static unsigned long pagemap_read(uint64_t *buf,
281 unsigned long index,
282 unsigned long pages)
284 return do_u64_read(pagemap_fd, "/proc/pid/pagemap", buf, index, pages);
287 static unsigned long pagemap_pfn(uint64_t val)
289 unsigned long pfn;
291 if (val & PM_PRESENT)
292 pfn = PM_PFRAME(val);
293 else
294 pfn = 0;
296 return pfn;
301 * page flag names
304 static char *page_flag_name(uint64_t flags)
306 static char buf[65];
307 int present;
308 size_t i, j;
310 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
311 present = (flags >> i) & 1;
312 if (!page_flag_names[i]) {
313 if (present)
314 fatal("unknown flag bit %d\n", i);
315 continue;
317 buf[j++] = present ? page_flag_names[i][0] : '_';
320 return buf;
323 static char *page_flag_longname(uint64_t flags)
325 static char buf[1024];
326 size_t i, n;
328 for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
329 if (!page_flag_names[i])
330 continue;
331 if ((flags >> i) & 1)
332 n += snprintf(buf + n, sizeof(buf) - n, "%s,",
333 page_flag_names[i] + 2);
335 if (n)
336 n--;
337 buf[n] = '\0';
339 return buf;
344 * page list and summary
347 static void show_page_range(unsigned long voffset, unsigned long offset,
348 unsigned long size, uint64_t flags)
350 static uint64_t flags0;
351 static unsigned long voff;
352 static unsigned long index;
353 static unsigned long count;
355 if (flags == flags0 && offset == index + count &&
356 size && voffset == voff + count) {
357 count += size;
358 return;
361 if (count) {
362 if (opt_pid)
363 printf("%lx\t", voff);
364 if (opt_file)
365 printf("%lu\t", voff);
366 printf("%lx\t%lx\t%s\n",
367 index, count, page_flag_name(flags0));
370 flags0 = flags;
371 index = offset;
372 voff = voffset;
373 count = size;
376 static void flush_page_range(void)
378 show_page_range(0, 0, 0, 0);
381 static void show_page(unsigned long voffset,
382 unsigned long offset, uint64_t flags)
384 if (opt_pid)
385 printf("%lx\t", voffset);
386 if (opt_file)
387 printf("%lu\t", voffset);
388 printf("%lx\t%s\n", offset, page_flag_name(flags));
391 static void show_summary(void)
393 size_t i;
395 printf(" flags\tpage-count MB"
396 " symbolic-flags\t\t\tlong-symbolic-flags\n");
398 for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
399 if (nr_pages[i])
400 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
401 (unsigned long long)page_flags[i],
402 nr_pages[i],
403 pages2mb(nr_pages[i]),
404 page_flag_name(page_flags[i]),
405 page_flag_longname(page_flags[i]));
408 printf(" total\t%10lu %8lu\n",
409 total_pages, pages2mb(total_pages));
414 * page flag filters
417 static int bit_mask_ok(uint64_t flags)
419 int i;
421 for (i = 0; i < nr_bit_filters; i++) {
422 if (opt_bits[i] == KPF_ALL_BITS) {
423 if ((flags & opt_mask[i]) == 0)
424 return 0;
425 } else {
426 if ((flags & opt_mask[i]) != opt_bits[i])
427 return 0;
431 return 1;
434 static uint64_t expand_overloaded_flags(uint64_t flags, uint64_t pme)
436 /* SLOB/SLUB overload several page flags */
437 if (flags & BIT(SLAB)) {
438 if (flags & BIT(PRIVATE))
439 flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
440 if (flags & BIT(ACTIVE))
441 flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
442 if (flags & BIT(ERROR))
443 flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
446 /* PG_reclaim is overloaded as PG_readahead in the read path */
447 if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
448 flags ^= BIT(RECLAIM) | BIT(READAHEAD);
450 if (pme & PM_SOFT_DIRTY)
451 flags |= BIT(SOFTDIRTY);
452 if (pme & PM_FILE)
453 flags |= BIT(FILE);
454 if (pme & PM_MMAP_EXCLUSIVE)
455 flags |= BIT(MMAP_EXCLUSIVE);
457 return flags;
460 static uint64_t well_known_flags(uint64_t flags)
462 /* hide flags intended only for kernel hacker */
463 flags &= ~KPF_HACKERS_BITS;
465 /* hide non-hugeTLB compound pages */
466 if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
467 flags &= ~BITS_COMPOUND;
469 return flags;
472 static uint64_t kpageflags_flags(uint64_t flags, uint64_t pme)
474 if (opt_raw)
475 flags = expand_overloaded_flags(flags, pme);
476 else
477 flags = well_known_flags(flags);
479 return flags;
483 * page actions
486 static void prepare_hwpoison_fd(void)
488 char buf[MAX_PATH + 1];
490 hwpoison_debug_fs = debugfs_mount(NULL);
491 if (!hwpoison_debug_fs) {
492 perror("mount debugfs");
493 exit(EXIT_FAILURE);
496 if (opt_hwpoison && !hwpoison_inject_fd) {
497 snprintf(buf, MAX_PATH, "%s/hwpoison/corrupt-pfn",
498 hwpoison_debug_fs);
499 hwpoison_inject_fd = checked_open(buf, O_WRONLY);
502 if (opt_unpoison && !hwpoison_forget_fd) {
503 snprintf(buf, MAX_PATH, "%s/hwpoison/unpoison-pfn",
504 hwpoison_debug_fs);
505 hwpoison_forget_fd = checked_open(buf, O_WRONLY);
509 static int hwpoison_page(unsigned long offset)
511 char buf[100];
512 int len;
514 len = sprintf(buf, "0x%lx\n", offset);
515 len = write(hwpoison_inject_fd, buf, len);
516 if (len < 0) {
517 perror("hwpoison inject");
518 return len;
520 return 0;
523 static int unpoison_page(unsigned long offset)
525 char buf[100];
526 int len;
528 len = sprintf(buf, "0x%lx\n", offset);
529 len = write(hwpoison_forget_fd, buf, len);
530 if (len < 0) {
531 perror("hwpoison forget");
532 return len;
534 return 0;
538 * page frame walker
541 static size_t hash_slot(uint64_t flags)
543 size_t k = HASH_KEY(flags);
544 size_t i;
546 /* Explicitly reserve slot 0 for flags 0: the following logic
547 * cannot distinguish an unoccupied slot from slot (flags==0).
549 if (flags == 0)
550 return 0;
552 /* search through the remaining (HASH_SIZE-1) slots */
553 for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) {
554 if (!k || k >= ARRAY_SIZE(page_flags))
555 k = 1;
556 if (page_flags[k] == 0) {
557 page_flags[k] = flags;
558 return k;
560 if (page_flags[k] == flags)
561 return k;
564 fatal("hash table full: bump up HASH_SHIFT?\n");
565 exit(EXIT_FAILURE);
568 static void add_page(unsigned long voffset,
569 unsigned long offset, uint64_t flags, uint64_t pme)
571 flags = kpageflags_flags(flags, pme);
573 if (!bit_mask_ok(flags))
574 return;
576 if (opt_hwpoison)
577 hwpoison_page(offset);
578 if (opt_unpoison)
579 unpoison_page(offset);
581 if (opt_list == 1)
582 show_page_range(voffset, offset, 1, flags);
583 else if (opt_list == 2)
584 show_page(voffset, offset, flags);
586 nr_pages[hash_slot(flags)]++;
587 total_pages++;
590 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
591 static void walk_pfn(unsigned long voffset,
592 unsigned long index,
593 unsigned long count,
594 uint64_t pme)
596 uint64_t buf[KPAGEFLAGS_BATCH];
597 unsigned long batch;
598 unsigned long pages;
599 unsigned long i;
601 while (count) {
602 batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
603 pages = kpageflags_read(buf, index, batch);
604 if (pages == 0)
605 break;
607 for (i = 0; i < pages; i++)
608 add_page(voffset + i, index + i, buf[i], pme);
610 index += pages;
611 count -= pages;
615 #define PAGEMAP_BATCH (64 << 10)
616 static void walk_vma(unsigned long index, unsigned long count)
618 uint64_t buf[PAGEMAP_BATCH];
619 unsigned long batch;
620 unsigned long pages;
621 unsigned long pfn;
622 unsigned long i;
624 while (count) {
625 batch = min_t(unsigned long, count, PAGEMAP_BATCH);
626 pages = pagemap_read(buf, index, batch);
627 if (pages == 0)
628 break;
630 for (i = 0; i < pages; i++) {
631 pfn = pagemap_pfn(buf[i]);
632 if (pfn)
633 walk_pfn(index + i, pfn, 1, buf[i]);
636 index += pages;
637 count -= pages;
641 static void walk_task(unsigned long index, unsigned long count)
643 const unsigned long end = index + count;
644 unsigned long start;
645 int i = 0;
647 while (index < end) {
649 while (pg_end[i] <= index)
650 if (++i >= nr_vmas)
651 return;
652 if (pg_start[i] >= end)
653 return;
655 start = max_t(unsigned long, pg_start[i], index);
656 index = min_t(unsigned long, pg_end[i], end);
658 assert(start < index);
659 walk_vma(start, index - start);
663 static void add_addr_range(unsigned long offset, unsigned long size)
665 if (nr_addr_ranges >= MAX_ADDR_RANGES)
666 fatal("too many addr ranges\n");
668 opt_offset[nr_addr_ranges] = offset;
669 opt_size[nr_addr_ranges] = min_t(unsigned long, size, ULONG_MAX-offset);
670 nr_addr_ranges++;
673 static void walk_addr_ranges(void)
675 int i;
677 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
679 if (!nr_addr_ranges)
680 add_addr_range(0, ULONG_MAX);
682 for (i = 0; i < nr_addr_ranges; i++)
683 if (!opt_pid)
684 walk_pfn(opt_offset[i], opt_offset[i], opt_size[i], 0);
685 else
686 walk_task(opt_offset[i], opt_size[i]);
688 close(kpageflags_fd);
693 * user interface
696 static const char *page_flag_type(uint64_t flag)
698 if (flag & KPF_HACKERS_BITS)
699 return "(r)";
700 if (flag & KPF_OVERLOADED_BITS)
701 return "(o)";
702 return " ";
705 static void usage(void)
707 size_t i, j;
709 printf(
710 "page-types [options]\n"
711 " -r|--raw Raw mode, for kernel developers\n"
712 " -d|--describe flags Describe flags\n"
713 " -a|--addr addr-spec Walk a range of pages\n"
714 " -b|--bits bits-spec Walk pages with specified bits\n"
715 " -p|--pid pid Walk process address space\n"
716 " -f|--file filename Walk file address space\n"
717 " -l|--list Show page details in ranges\n"
718 " -L|--list-each Show page details one by one\n"
719 " -N|--no-summary Don't show summary info\n"
720 " -X|--hwpoison hwpoison pages\n"
721 " -x|--unpoison unpoison pages\n"
722 " -h|--help Show this usage message\n"
723 "flags:\n"
724 " 0x10 bitfield format, e.g.\n"
725 " anon bit-name, e.g.\n"
726 " 0x10,anon comma-separated list, e.g.\n"
727 "addr-spec:\n"
728 " N one page at offset N (unit: pages)\n"
729 " N+M pages range from N to N+M-1\n"
730 " N,M pages range from N to M-1\n"
731 " N, pages range from N to end\n"
732 " ,M pages range from 0 to M-1\n"
733 "bits-spec:\n"
734 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
735 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
736 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
737 " =bit1,bit2 flags == (bit1|bit2)\n"
738 "bit-names:\n"
741 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
742 if (!page_flag_names[i])
743 continue;
744 printf("%16s%s", page_flag_names[i] + 2,
745 page_flag_type(1ULL << i));
746 if (++j > 3) {
747 j = 0;
748 putchar('\n');
751 printf("\n "
752 "(r) raw mode bits (o) overloaded bits\n");
755 static unsigned long long parse_number(const char *str)
757 unsigned long long n;
759 n = strtoll(str, NULL, 0);
761 if (n == 0 && str[0] != '0')
762 fatal("invalid name or number: %s\n", str);
764 return n;
767 static void parse_pid(const char *str)
769 FILE *file;
770 char buf[5000];
772 opt_pid = parse_number(str);
774 sprintf(buf, "/proc/%d/pagemap", opt_pid);
775 pagemap_fd = checked_open(buf, O_RDONLY);
777 sprintf(buf, "/proc/%d/maps", opt_pid);
778 file = fopen(buf, "r");
779 if (!file) {
780 perror(buf);
781 exit(EXIT_FAILURE);
784 while (fgets(buf, sizeof(buf), file) != NULL) {
785 unsigned long vm_start;
786 unsigned long vm_end;
787 unsigned long long pgoff;
788 int major, minor;
789 char r, w, x, s;
790 unsigned long ino;
791 int n;
793 n = sscanf(buf, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
794 &vm_start,
795 &vm_end,
796 &r, &w, &x, &s,
797 &pgoff,
798 &major, &minor,
799 &ino);
800 if (n < 10) {
801 fprintf(stderr, "unexpected line: %s\n", buf);
802 continue;
804 pg_start[nr_vmas] = vm_start / page_size;
805 pg_end[nr_vmas] = vm_end / page_size;
806 if (++nr_vmas >= MAX_VMAS) {
807 fprintf(stderr, "too many VMAs\n");
808 break;
811 fclose(file);
814 static void show_file(const char *name, const struct stat *st)
816 unsigned long long size = st->st_size;
817 char atime[64], mtime[64];
818 long now = time(NULL);
820 printf("%s\tInode: %u\tSize: %llu (%llu pages)\n",
821 name, (unsigned)st->st_ino,
822 size, (size + page_size - 1) / page_size);
824 strftime(atime, sizeof(atime), "%c", localtime(&st->st_atime));
825 strftime(mtime, sizeof(mtime), "%c", localtime(&st->st_mtime));
827 printf("Modify: %s (%ld seconds ago)\nAccess: %s (%ld seconds ago)\n",
828 mtime, now - st->st_mtime,
829 atime, now - st->st_atime);
832 static sigjmp_buf sigbus_jmp;
834 static void * volatile sigbus_addr;
836 static void sigbus_handler(int sig, siginfo_t *info, void *ucontex)
838 (void)sig;
839 (void)ucontex;
840 sigbus_addr = info ? info->si_addr : NULL;
841 siglongjmp(sigbus_jmp, 1);
844 static struct sigaction sigbus_action = {
845 .sa_sigaction = sigbus_handler,
846 .sa_flags = SA_SIGINFO,
849 static void walk_file(const char *name, const struct stat *st)
851 uint8_t vec[PAGEMAP_BATCH];
852 uint64_t buf[PAGEMAP_BATCH], flags;
853 unsigned long nr_pages, pfn, i;
854 off_t off, end = st->st_size;
855 int fd;
856 ssize_t len;
857 void *ptr;
858 int first = 1;
860 fd = checked_open(name, O_RDONLY|O_NOATIME|O_NOFOLLOW);
862 for (off = 0; off < end; off += len) {
863 nr_pages = (end - off + page_size - 1) / page_size;
864 if (nr_pages > PAGEMAP_BATCH)
865 nr_pages = PAGEMAP_BATCH;
866 len = nr_pages * page_size;
868 ptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, off);
869 if (ptr == MAP_FAILED)
870 fatal("mmap failed: %s", name);
872 /* determine cached pages */
873 if (mincore(ptr, len, vec))
874 fatal("mincore failed: %s", name);
876 /* turn off readahead */
877 if (madvise(ptr, len, MADV_RANDOM))
878 fatal("madvice failed: %s", name);
880 if (sigsetjmp(sigbus_jmp, 1)) {
881 end = off + sigbus_addr ? sigbus_addr - ptr : 0;
882 fprintf(stderr, "got sigbus at offset %lld: %s\n",
883 (long long)end, name);
884 goto got_sigbus;
887 /* populate ptes */
888 for (i = 0; i < nr_pages ; i++) {
889 if (vec[i] & 1)
890 (void)*(volatile int *)(ptr + i * page_size);
892 got_sigbus:
894 /* turn off harvesting reference bits */
895 if (madvise(ptr, len, MADV_SEQUENTIAL))
896 fatal("madvice failed: %s", name);
898 if (pagemap_read(buf, (unsigned long)ptr / page_size,
899 nr_pages) != nr_pages)
900 fatal("cannot read pagemap");
902 munmap(ptr, len);
904 for (i = 0; i < nr_pages; i++) {
905 pfn = pagemap_pfn(buf[i]);
906 if (!pfn)
907 continue;
908 if (!kpageflags_read(&flags, pfn, 1))
909 continue;
910 if (first && opt_list) {
911 first = 0;
912 flush_page_range();
913 show_file(name, st);
915 add_page(off / page_size + i, pfn, flags, buf[i]);
919 close(fd);
922 int walk_tree(const char *name, const struct stat *st, int type, struct FTW *f)
924 (void)f;
925 switch (type) {
926 case FTW_F:
927 if (S_ISREG(st->st_mode))
928 walk_file(name, st);
929 break;
930 case FTW_DNR:
931 fprintf(stderr, "cannot read dir: %s\n", name);
932 break;
934 return 0;
937 static void walk_page_cache(void)
939 struct stat st;
941 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
942 pagemap_fd = checked_open("/proc/self/pagemap", O_RDONLY);
943 sigaction(SIGBUS, &sigbus_action, NULL);
945 if (stat(opt_file, &st))
946 fatal("stat failed: %s\n", opt_file);
948 if (S_ISREG(st.st_mode)) {
949 walk_file(opt_file, &st);
950 } else if (S_ISDIR(st.st_mode)) {
951 /* do not follow symlinks and mountpoints */
952 if (nftw(opt_file, walk_tree, 64, FTW_MOUNT | FTW_PHYS) < 0)
953 fatal("nftw failed: %s\n", opt_file);
954 } else
955 fatal("unhandled file type: %s\n", opt_file);
957 close(kpageflags_fd);
958 close(pagemap_fd);
959 signal(SIGBUS, SIG_DFL);
962 static void parse_file(const char *name)
964 opt_file = name;
967 static void parse_addr_range(const char *optarg)
969 unsigned long offset;
970 unsigned long size;
971 char *p;
973 p = strchr(optarg, ',');
974 if (!p)
975 p = strchr(optarg, '+');
977 if (p == optarg) {
978 offset = 0;
979 size = parse_number(p + 1);
980 } else if (p) {
981 offset = parse_number(optarg);
982 if (p[1] == '\0')
983 size = ULONG_MAX;
984 else {
985 size = parse_number(p + 1);
986 if (*p == ',') {
987 if (size < offset)
988 fatal("invalid range: %lu,%lu\n",
989 offset, size);
990 size -= offset;
993 } else {
994 offset = parse_number(optarg);
995 size = 1;
998 add_addr_range(offset, size);
1001 static void add_bits_filter(uint64_t mask, uint64_t bits)
1003 if (nr_bit_filters >= MAX_BIT_FILTERS)
1004 fatal("too much bit filters\n");
1006 opt_mask[nr_bit_filters] = mask;
1007 opt_bits[nr_bit_filters] = bits;
1008 nr_bit_filters++;
1011 static uint64_t parse_flag_name(const char *str, int len)
1013 size_t i;
1015 if (!*str || !len)
1016 return 0;
1018 if (len <= 8 && !strncmp(str, "compound", len))
1019 return BITS_COMPOUND;
1021 for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
1022 if (!page_flag_names[i])
1023 continue;
1024 if (!strncmp(str, page_flag_names[i] + 2, len))
1025 return 1ULL << i;
1028 return parse_number(str);
1031 static uint64_t parse_flag_names(const char *str, int all)
1033 const char *p = str;
1034 uint64_t flags = 0;
1036 while (1) {
1037 if (*p == ',' || *p == '=' || *p == '\0') {
1038 if ((*str != '~') || (*str == '~' && all && *++str))
1039 flags |= parse_flag_name(str, p - str);
1040 if (*p != ',')
1041 break;
1042 str = p + 1;
1044 p++;
1047 return flags;
1050 static void parse_bits_mask(const char *optarg)
1052 uint64_t mask;
1053 uint64_t bits;
1054 const char *p;
1056 p = strchr(optarg, '=');
1057 if (p == optarg) {
1058 mask = KPF_ALL_BITS;
1059 bits = parse_flag_names(p + 1, 0);
1060 } else if (p) {
1061 mask = parse_flag_names(optarg, 0);
1062 bits = parse_flag_names(p + 1, 0);
1063 } else if (strchr(optarg, '~')) {
1064 mask = parse_flag_names(optarg, 1);
1065 bits = parse_flag_names(optarg, 0);
1066 } else {
1067 mask = parse_flag_names(optarg, 0);
1068 bits = KPF_ALL_BITS;
1071 add_bits_filter(mask, bits);
1074 static void describe_flags(const char *optarg)
1076 uint64_t flags = parse_flag_names(optarg, 0);
1078 printf("0x%016llx\t%s\t%s\n",
1079 (unsigned long long)flags,
1080 page_flag_name(flags),
1081 page_flag_longname(flags));
1084 static const struct option opts[] = {
1085 { "raw" , 0, NULL, 'r' },
1086 { "pid" , 1, NULL, 'p' },
1087 { "file" , 1, NULL, 'f' },
1088 { "addr" , 1, NULL, 'a' },
1089 { "bits" , 1, NULL, 'b' },
1090 { "describe" , 1, NULL, 'd' },
1091 { "list" , 0, NULL, 'l' },
1092 { "list-each" , 0, NULL, 'L' },
1093 { "no-summary", 0, NULL, 'N' },
1094 { "hwpoison" , 0, NULL, 'X' },
1095 { "unpoison" , 0, NULL, 'x' },
1096 { "help" , 0, NULL, 'h' },
1097 { NULL , 0, NULL, 0 }
1100 int main(int argc, char *argv[])
1102 int c;
1104 page_size = getpagesize();
1106 while ((c = getopt_long(argc, argv,
1107 "rp:f:a:b:d:lLNXxh", opts, NULL)) != -1) {
1108 switch (c) {
1109 case 'r':
1110 opt_raw = 1;
1111 break;
1112 case 'p':
1113 parse_pid(optarg);
1114 break;
1115 case 'f':
1116 parse_file(optarg);
1117 break;
1118 case 'a':
1119 parse_addr_range(optarg);
1120 break;
1121 case 'b':
1122 parse_bits_mask(optarg);
1123 break;
1124 case 'd':
1125 describe_flags(optarg);
1126 exit(0);
1127 case 'l':
1128 opt_list = 1;
1129 break;
1130 case 'L':
1131 opt_list = 2;
1132 break;
1133 case 'N':
1134 opt_no_summary = 1;
1135 break;
1136 case 'X':
1137 opt_hwpoison = 1;
1138 prepare_hwpoison_fd();
1139 break;
1140 case 'x':
1141 opt_unpoison = 1;
1142 prepare_hwpoison_fd();
1143 break;
1144 case 'h':
1145 usage();
1146 exit(0);
1147 default:
1148 usage();
1149 exit(1);
1153 if (opt_list && opt_pid)
1154 printf("voffset\t");
1155 if (opt_list && opt_file)
1156 printf("foffset\t");
1157 if (opt_list == 1)
1158 printf("offset\tlen\tflags\n");
1159 if (opt_list == 2)
1160 printf("offset\tflags\n");
1162 if (opt_file)
1163 walk_page_cache();
1164 else
1165 walk_addr_ranges();
1167 if (opt_list == 1)
1168 flush_page_range();
1170 if (opt_no_summary)
1171 return 0;
1173 if (opt_list)
1174 printf("\n\n");
1176 show_summary();
1178 return 0;