fed up with those stupid warnings
[mmotm.git] / Documentation / vm / page-types.c
blob39baed1ddb1b27c7210b6305cb493c51eb5859f4
1 /*
2 * page-types: Tool for querying page flags
4 * Copyright (C) 2009 Intel corporation
6 * Authors: Wu Fengguang <fengguang.wu@intel.com>
8 * Released under the General Public License (GPL).
9 */
11 #define _LARGEFILE64_SOURCE
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdint.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <getopt.h>
19 #include <limits.h>
20 #include <assert.h>
21 #include <sys/types.h>
22 #include <sys/errno.h>
23 #include <sys/fcntl.h>
27 * pagemap kernel ABI bits
30 #define PM_ENTRY_BYTES sizeof(uint64_t)
31 #define PM_STATUS_BITS 3
32 #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
33 #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
34 #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
35 #define PM_PSHIFT_BITS 6
36 #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
37 #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
38 #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
39 #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
40 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
42 #define PM_PRESENT PM_STATUS(4LL)
43 #define PM_SWAP PM_STATUS(2LL)
47 * kernel page flags
50 #define KPF_BYTES 8
51 #define PROC_KPAGEFLAGS "/proc/kpageflags"
53 /* copied from kpageflags_read() */
54 #define KPF_LOCKED 0
55 #define KPF_ERROR 1
56 #define KPF_REFERENCED 2
57 #define KPF_UPTODATE 3
58 #define KPF_DIRTY 4
59 #define KPF_LRU 5
60 #define KPF_ACTIVE 6
61 #define KPF_SLAB 7
62 #define KPF_WRITEBACK 8
63 #define KPF_RECLAIM 9
64 #define KPF_BUDDY 10
66 /* [11-20] new additions in 2.6.31 */
67 #define KPF_MMAP 11
68 #define KPF_ANON 12
69 #define KPF_SWAPCACHE 13
70 #define KPF_SWAPBACKED 14
71 #define KPF_COMPOUND_HEAD 15
72 #define KPF_COMPOUND_TAIL 16
73 #define KPF_HUGE 17
74 #define KPF_UNEVICTABLE 18
75 #define KPF_HWPOISON 19
76 #define KPF_NOPAGE 20
77 #define KPF_KSM 21
79 /* [32-] kernel hacking assistances */
80 #define KPF_RESERVED 32
81 #define KPF_MLOCKED 33
82 #define KPF_MAPPEDTODISK 34
83 #define KPF_PRIVATE 35
84 #define KPF_PRIVATE_2 36
85 #define KPF_OWNER_PRIVATE 37
86 #define KPF_ARCH 38
87 #define KPF_UNCACHED 39
89 /* [48-] take some arbitrary free slots for expanding overloaded flags
90 * not part of kernel API
92 #define KPF_READAHEAD 48
93 #define KPF_SLOB_FREE 49
94 #define KPF_SLUB_FROZEN 50
95 #define KPF_SLUB_DEBUG 51
97 #define KPF_ALL_BITS ((uint64_t)~0ULL)
98 #define KPF_HACKERS_BITS (0xffffULL << 32)
99 #define KPF_OVERLOADED_BITS (0xffffULL << 48)
100 #define BIT(name) (1ULL << KPF_##name)
101 #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
103 static const char *page_flag_names[] = {
104 [KPF_LOCKED] = "L:locked",
105 [KPF_ERROR] = "E:error",
106 [KPF_REFERENCED] = "R:referenced",
107 [KPF_UPTODATE] = "U:uptodate",
108 [KPF_DIRTY] = "D:dirty",
109 [KPF_LRU] = "l:lru",
110 [KPF_ACTIVE] = "A:active",
111 [KPF_SLAB] = "S:slab",
112 [KPF_WRITEBACK] = "W:writeback",
113 [KPF_RECLAIM] = "I:reclaim",
114 [KPF_BUDDY] = "B:buddy",
116 [KPF_MMAP] = "M:mmap",
117 [KPF_ANON] = "a:anonymous",
118 [KPF_SWAPCACHE] = "s:swapcache",
119 [KPF_SWAPBACKED] = "b:swapbacked",
120 [KPF_COMPOUND_HEAD] = "H:compound_head",
121 [KPF_COMPOUND_TAIL] = "T:compound_tail",
122 [KPF_HUGE] = "G:huge",
123 [KPF_UNEVICTABLE] = "u:unevictable",
124 [KPF_HWPOISON] = "X:hwpoison",
125 [KPF_NOPAGE] = "n:nopage",
126 [KPF_KSM] = "x:ksm",
128 [KPF_RESERVED] = "r:reserved",
129 [KPF_MLOCKED] = "m:mlocked",
130 [KPF_MAPPEDTODISK] = "d:mappedtodisk",
131 [KPF_PRIVATE] = "P:private",
132 [KPF_PRIVATE_2] = "p:private_2",
133 [KPF_OWNER_PRIVATE] = "O:owner_private",
134 [KPF_ARCH] = "h:arch",
135 [KPF_UNCACHED] = "c:uncached",
137 [KPF_READAHEAD] = "I:readahead",
138 [KPF_SLOB_FREE] = "P:slob_free",
139 [KPF_SLUB_FROZEN] = "A:slub_frozen",
140 [KPF_SLUB_DEBUG] = "E:slub_debug",
145 * data structures
148 static int opt_raw; /* for kernel developers */
149 static int opt_list; /* list pages (in ranges) */
150 static int opt_no_summary; /* don't show summary */
151 static pid_t opt_pid; /* process to walk */
153 #define MAX_ADDR_RANGES 1024
154 static int nr_addr_ranges;
155 static unsigned long opt_offset[MAX_ADDR_RANGES];
156 static unsigned long opt_size[MAX_ADDR_RANGES];
158 #define MAX_VMAS 10240
159 static int nr_vmas;
160 static unsigned long pg_start[MAX_VMAS];
161 static unsigned long pg_end[MAX_VMAS];
163 #define MAX_BIT_FILTERS 64
164 static int nr_bit_filters;
165 static uint64_t opt_mask[MAX_BIT_FILTERS];
166 static uint64_t opt_bits[MAX_BIT_FILTERS];
168 static int page_size;
170 static int pagemap_fd;
171 static int kpageflags_fd;
173 static int opt_hwpoison;
174 static int opt_unpoison;
176 static const char hwpoison_debug_fs[] = "/debug/hwpoison";
177 static int hwpoison_inject_fd;
178 static int hwpoison_forget_fd;
180 #define HASH_SHIFT 13
181 #define HASH_SIZE (1 << HASH_SHIFT)
182 #define HASH_MASK (HASH_SIZE - 1)
183 #define HASH_KEY(flags) (flags & HASH_MASK)
185 static unsigned long total_pages;
186 static unsigned long nr_pages[HASH_SIZE];
187 static uint64_t page_flags[HASH_SIZE];
191 * helper functions
194 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
196 #define min_t(type, x, y) ({ \
197 type __min1 = (x); \
198 type __min2 = (y); \
199 __min1 < __min2 ? __min1 : __min2; })
201 #define max_t(type, x, y) ({ \
202 type __max1 = (x); \
203 type __max2 = (y); \
204 __max1 > __max2 ? __max1 : __max2; })
206 static unsigned long pages2mb(unsigned long pages)
208 return (pages * page_size) >> 20;
211 static void fatal(const char *x, ...)
213 va_list ap;
215 va_start(ap, x);
216 vfprintf(stderr, x, ap);
217 va_end(ap);
218 exit(EXIT_FAILURE);
221 int checked_open(const char *pathname, int flags)
223 int fd = open(pathname, flags);
225 if (fd < 0) {
226 perror(pathname);
227 exit(EXIT_FAILURE);
230 return fd;
234 * pagemap/kpageflags routines
237 static unsigned long do_u64_read(int fd, char *name,
238 uint64_t *buf,
239 unsigned long index,
240 unsigned long count)
242 long bytes;
244 if (index > ULONG_MAX / 8)
245 fatal("index overflow: %lu\n", index);
247 if (lseek(fd, index * 8, SEEK_SET) < 0) {
248 perror(name);
249 exit(EXIT_FAILURE);
252 bytes = read(fd, buf, count * 8);
253 if (bytes < 0) {
254 perror(name);
255 exit(EXIT_FAILURE);
257 if (bytes % 8)
258 fatal("partial read: %lu bytes\n", bytes);
260 return bytes / 8;
263 static unsigned long kpageflags_read(uint64_t *buf,
264 unsigned long index,
265 unsigned long pages)
267 return do_u64_read(kpageflags_fd, PROC_KPAGEFLAGS, buf, index, pages);
270 static unsigned long pagemap_read(uint64_t *buf,
271 unsigned long index,
272 unsigned long pages)
274 return do_u64_read(pagemap_fd, "/proc/pid/pagemap", buf, index, pages);
277 static unsigned long pagemap_pfn(uint64_t val)
279 unsigned long pfn;
281 if (val & PM_PRESENT)
282 pfn = PM_PFRAME(val);
283 else
284 pfn = 0;
286 return pfn;
291 * page flag names
294 static char *page_flag_name(uint64_t flags)
296 static char buf[65];
297 int present;
298 int i, j;
300 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
301 present = (flags >> i) & 1;
302 if (!page_flag_names[i]) {
303 if (present)
304 fatal("unknown flag bit %d\n", i);
305 continue;
307 buf[j++] = present ? page_flag_names[i][0] : '_';
310 return buf;
313 static char *page_flag_longname(uint64_t flags)
315 static char buf[1024];
316 int i, n;
318 for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
319 if (!page_flag_names[i])
320 continue;
321 if ((flags >> i) & 1)
322 n += snprintf(buf + n, sizeof(buf) - n, "%s,",
323 page_flag_names[i] + 2);
325 if (n)
326 n--;
327 buf[n] = '\0';
329 return buf;
334 * page list and summary
337 static void show_page_range(unsigned long voffset,
338 unsigned long offset, uint64_t flags)
340 static uint64_t flags0;
341 static unsigned long voff;
342 static unsigned long index;
343 static unsigned long count;
345 if (flags == flags0 && offset == index + count &&
346 (!opt_pid || voffset == voff + count)) {
347 count++;
348 return;
351 if (count) {
352 if (opt_pid)
353 printf("%lx\t", voff);
354 printf("%lx\t%lx\t%s\n",
355 index, count, page_flag_name(flags0));
358 flags0 = flags;
359 index = offset;
360 voff = voffset;
361 count = 1;
364 static void show_page(unsigned long voffset,
365 unsigned long offset, uint64_t flags)
367 if (opt_pid)
368 printf("%lx\t", voffset);
369 printf("%lx\t%s\n", offset, page_flag_name(flags));
372 static void show_summary(void)
374 int i;
376 printf(" flags\tpage-count MB"
377 " symbolic-flags\t\t\tlong-symbolic-flags\n");
379 for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
380 if (nr_pages[i])
381 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
382 (unsigned long long)page_flags[i],
383 nr_pages[i],
384 pages2mb(nr_pages[i]),
385 page_flag_name(page_flags[i]),
386 page_flag_longname(page_flags[i]));
389 printf(" total\t%10lu %8lu\n",
390 total_pages, pages2mb(total_pages));
395 * page flag filters
398 static int bit_mask_ok(uint64_t flags)
400 int i;
402 for (i = 0; i < nr_bit_filters; i++) {
403 if (opt_bits[i] == KPF_ALL_BITS) {
404 if ((flags & opt_mask[i]) == 0)
405 return 0;
406 } else {
407 if ((flags & opt_mask[i]) != opt_bits[i])
408 return 0;
412 return 1;
415 static uint64_t expand_overloaded_flags(uint64_t flags)
417 /* SLOB/SLUB overload several page flags */
418 if (flags & BIT(SLAB)) {
419 if (flags & BIT(PRIVATE))
420 flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
421 if (flags & BIT(ACTIVE))
422 flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
423 if (flags & BIT(ERROR))
424 flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
427 /* PG_reclaim is overloaded as PG_readahead in the read path */
428 if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
429 flags ^= BIT(RECLAIM) | BIT(READAHEAD);
431 return flags;
434 static uint64_t well_known_flags(uint64_t flags)
436 /* hide flags intended only for kernel hacker */
437 flags &= ~KPF_HACKERS_BITS;
439 /* hide non-hugeTLB compound pages */
440 if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
441 flags &= ~BITS_COMPOUND;
443 return flags;
446 static uint64_t kpageflags_flags(uint64_t flags)
448 flags = expand_overloaded_flags(flags);
450 if (!opt_raw)
451 flags = well_known_flags(flags);
453 return flags;
457 * page actions
460 static void prepare_hwpoison_fd(void)
462 char buf[100];
464 if (opt_hwpoison && !hwpoison_inject_fd) {
465 sprintf(buf, "%s/corrupt-pfn", hwpoison_debug_fs);
466 hwpoison_inject_fd = checked_open(buf, O_WRONLY);
469 if (opt_unpoison && !hwpoison_forget_fd) {
470 sprintf(buf, "%s/renew-pfn", hwpoison_debug_fs);
471 hwpoison_forget_fd = checked_open(buf, O_WRONLY);
475 static int hwpoison_page(unsigned long offset)
477 char buf[100];
478 int len;
480 len = sprintf(buf, "0x%lx\n", offset);
481 len = write(hwpoison_inject_fd, buf, len);
482 if (len < 0) {
483 perror("hwpoison inject");
484 return len;
486 return 0;
489 static int unpoison_page(unsigned long offset)
491 char buf[100];
492 int len;
494 len = sprintf(buf, "0x%lx\n", offset);
495 len = write(hwpoison_forget_fd, buf, len);
496 if (len < 0) {
497 perror("hwpoison forget");
498 return len;
500 return 0;
504 * page frame walker
507 static int hash_slot(uint64_t flags)
509 int k = HASH_KEY(flags);
510 int i;
512 /* Explicitly reserve slot 0 for flags 0: the following logic
513 * cannot distinguish an unoccupied slot from slot (flags==0).
515 if (flags == 0)
516 return 0;
518 /* search through the remaining (HASH_SIZE-1) slots */
519 for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) {
520 if (!k || k >= ARRAY_SIZE(page_flags))
521 k = 1;
522 if (page_flags[k] == 0) {
523 page_flags[k] = flags;
524 return k;
526 if (page_flags[k] == flags)
527 return k;
530 fatal("hash table full: bump up HASH_SHIFT?\n");
531 exit(EXIT_FAILURE);
534 static void add_page(unsigned long voffset,
535 unsigned long offset, uint64_t flags)
537 flags = kpageflags_flags(flags);
539 if (!bit_mask_ok(flags))
540 return;
542 if (opt_hwpoison)
543 hwpoison_page(offset);
544 if (opt_unpoison)
545 unpoison_page(offset);
547 if (opt_list == 1)
548 show_page_range(voffset, offset, flags);
549 else if (opt_list == 2)
550 show_page(voffset, offset, flags);
552 nr_pages[hash_slot(flags)]++;
553 total_pages++;
556 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
557 static void walk_pfn(unsigned long voffset,
558 unsigned long index,
559 unsigned long count)
561 uint64_t buf[KPAGEFLAGS_BATCH];
562 unsigned long batch;
563 long pages;
564 unsigned long i;
566 while (count) {
567 batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
568 pages = kpageflags_read(buf, index, batch);
569 if (pages == 0)
570 break;
572 for (i = 0; i < pages; i++)
573 add_page(voffset + i, index + i, buf[i]);
575 index += pages;
576 count -= pages;
580 #define PAGEMAP_BATCH (64 << 10)
581 static void walk_vma(unsigned long index, unsigned long count)
583 uint64_t buf[PAGEMAP_BATCH];
584 unsigned long batch;
585 unsigned long pages;
586 unsigned long pfn;
587 unsigned long i;
589 while (count) {
590 batch = min_t(unsigned long, count, PAGEMAP_BATCH);
591 pages = pagemap_read(buf, index, batch);
592 if (pages == 0)
593 break;
595 for (i = 0; i < pages; i++) {
596 pfn = pagemap_pfn(buf[i]);
597 if (pfn)
598 walk_pfn(index + i, pfn, 1);
601 index += pages;
602 count -= pages;
606 static void walk_task(unsigned long index, unsigned long count)
608 const unsigned long end = index + count;
609 unsigned long start;
610 int i = 0;
612 while (index < end) {
614 while (pg_end[i] <= index)
615 if (++i >= nr_vmas)
616 return;
617 if (pg_start[i] >= end)
618 return;
620 start = max_t(unsigned long, pg_start[i], index);
621 index = min_t(unsigned long, pg_end[i], end);
623 assert(start < index);
624 walk_vma(start, index - start);
628 static void add_addr_range(unsigned long offset, unsigned long size)
630 if (nr_addr_ranges >= MAX_ADDR_RANGES)
631 fatal("too many addr ranges\n");
633 opt_offset[nr_addr_ranges] = offset;
634 opt_size[nr_addr_ranges] = min_t(unsigned long, size, ULONG_MAX-offset);
635 nr_addr_ranges++;
638 static void walk_addr_ranges(void)
640 int i;
642 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
644 if (!nr_addr_ranges)
645 add_addr_range(0, ULONG_MAX);
647 for (i = 0; i < nr_addr_ranges; i++)
648 if (!opt_pid)
649 walk_pfn(0, opt_offset[i], opt_size[i]);
650 else
651 walk_task(opt_offset[i], opt_size[i]);
653 close(kpageflags_fd);
658 * user interface
661 static const char *page_flag_type(uint64_t flag)
663 if (flag & KPF_HACKERS_BITS)
664 return "(r)";
665 if (flag & KPF_OVERLOADED_BITS)
666 return "(o)";
667 return " ";
670 static void usage(void)
672 int i, j;
674 printf(
675 "page-types [options]\n"
676 " -r|--raw Raw mode, for kernel developers\n"
677 " -a|--addr addr-spec Walk a range of pages\n"
678 " -b|--bits bits-spec Walk pages with specified bits\n"
679 " -p|--pid pid Walk process address space\n"
680 #if 0 /* planned features */
681 " -f|--file filename Walk file address space\n"
682 #endif
683 " -l|--list Show page details in ranges\n"
684 " -L|--list-each Show page details one by one\n"
685 " -N|--no-summary Don't show summary info\n"
686 " -X|--hwpoison hwpoison pages\n"
687 " -x|--unpoison unpoison pages\n"
688 " -h|--help Show this usage message\n"
689 "addr-spec:\n"
690 " N one page at offset N (unit: pages)\n"
691 " N+M pages range from N to N+M-1\n"
692 " N,M pages range from N to M-1\n"
693 " N, pages range from N to end\n"
694 " ,M pages range from 0 to M-1\n"
695 "bits-spec:\n"
696 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
697 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
698 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
699 " =bit1,bit2 flags == (bit1|bit2)\n"
700 "bit-names:\n"
703 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
704 if (!page_flag_names[i])
705 continue;
706 printf("%16s%s", page_flag_names[i] + 2,
707 page_flag_type(1ULL << i));
708 if (++j > 3) {
709 j = 0;
710 putchar('\n');
713 printf("\n "
714 "(r) raw mode bits (o) overloaded bits\n");
717 static unsigned long long parse_number(const char *str)
719 unsigned long long n;
721 n = strtoll(str, NULL, 0);
723 if (n == 0 && str[0] != '0')
724 fatal("invalid name or number: %s\n", str);
726 return n;
729 static void parse_pid(const char *str)
731 FILE *file;
732 char buf[5000];
734 opt_pid = parse_number(str);
736 sprintf(buf, "/proc/%d/pagemap", opt_pid);
737 pagemap_fd = checked_open(buf, O_RDONLY);
739 sprintf(buf, "/proc/%d/maps", opt_pid);
740 file = fopen(buf, "r");
741 if (!file) {
742 perror(buf);
743 exit(EXIT_FAILURE);
746 while (fgets(buf, sizeof(buf), file) != NULL) {
747 unsigned long vm_start;
748 unsigned long vm_end;
749 unsigned long long pgoff;
750 int major, minor;
751 char r, w, x, s;
752 unsigned long ino;
753 int n;
755 n = sscanf(buf, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
756 &vm_start,
757 &vm_end,
758 &r, &w, &x, &s,
759 &pgoff,
760 &major, &minor,
761 &ino);
762 if (n < 10) {
763 fprintf(stderr, "unexpected line: %s\n", buf);
764 continue;
766 pg_start[nr_vmas] = vm_start / page_size;
767 pg_end[nr_vmas] = vm_end / page_size;
768 if (++nr_vmas >= MAX_VMAS) {
769 fprintf(stderr, "too many VMAs\n");
770 break;
773 fclose(file);
776 static void parse_file(const char *name)
780 static void parse_addr_range(const char *optarg)
782 unsigned long offset;
783 unsigned long size;
784 char *p;
786 p = strchr(optarg, ',');
787 if (!p)
788 p = strchr(optarg, '+');
790 if (p == optarg) {
791 offset = 0;
792 size = parse_number(p + 1);
793 } else if (p) {
794 offset = parse_number(optarg);
795 if (p[1] == '\0')
796 size = ULONG_MAX;
797 else {
798 size = parse_number(p + 1);
799 if (*p == ',') {
800 if (size < offset)
801 fatal("invalid range: %lu,%lu\n",
802 offset, size);
803 size -= offset;
806 } else {
807 offset = parse_number(optarg);
808 size = 1;
811 add_addr_range(offset, size);
814 static void add_bits_filter(uint64_t mask, uint64_t bits)
816 if (nr_bit_filters >= MAX_BIT_FILTERS)
817 fatal("too much bit filters\n");
819 opt_mask[nr_bit_filters] = mask;
820 opt_bits[nr_bit_filters] = bits;
821 nr_bit_filters++;
824 static uint64_t parse_flag_name(const char *str, int len)
826 int i;
828 if (!*str || !len)
829 return 0;
831 if (len <= 8 && !strncmp(str, "compound", len))
832 return BITS_COMPOUND;
834 for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
835 if (!page_flag_names[i])
836 continue;
837 if (!strncmp(str, page_flag_names[i] + 2, len))
838 return 1ULL << i;
841 return parse_number(str);
844 static uint64_t parse_flag_names(const char *str, int all)
846 const char *p = str;
847 uint64_t flags = 0;
849 while (1) {
850 if (*p == ',' || *p == '=' || *p == '\0') {
851 if ((*str != '~') || (*str == '~' && all && *++str))
852 flags |= parse_flag_name(str, p - str);
853 if (*p != ',')
854 break;
855 str = p + 1;
857 p++;
860 return flags;
863 static void parse_bits_mask(const char *optarg)
865 uint64_t mask;
866 uint64_t bits;
867 const char *p;
869 p = strchr(optarg, '=');
870 if (p == optarg) {
871 mask = KPF_ALL_BITS;
872 bits = parse_flag_names(p + 1, 0);
873 } else if (p) {
874 mask = parse_flag_names(optarg, 0);
875 bits = parse_flag_names(p + 1, 0);
876 } else if (strchr(optarg, '~')) {
877 mask = parse_flag_names(optarg, 1);
878 bits = parse_flag_names(optarg, 0);
879 } else {
880 mask = parse_flag_names(optarg, 0);
881 bits = KPF_ALL_BITS;
884 add_bits_filter(mask, bits);
888 static const struct option opts[] = {
889 { "raw" , 0, NULL, 'r' },
890 { "pid" , 1, NULL, 'p' },
891 { "file" , 1, NULL, 'f' },
892 { "addr" , 1, NULL, 'a' },
893 { "bits" , 1, NULL, 'b' },
894 { "list" , 0, NULL, 'l' },
895 { "list-each" , 0, NULL, 'L' },
896 { "no-summary", 0, NULL, 'N' },
897 { "hwpoison" , 0, NULL, 'X' },
898 { "unpoison" , 0, NULL, 'x' },
899 { "help" , 0, NULL, 'h' },
900 { NULL , 0, NULL, 0 }
903 int main(int argc, char *argv[])
905 int c;
907 page_size = getpagesize();
909 while ((c = getopt_long(argc, argv,
910 "rp:f:a:b:lLNXxh", opts, NULL)) != -1) {
911 switch (c) {
912 case 'r':
913 opt_raw = 1;
914 break;
915 case 'p':
916 parse_pid(optarg);
917 break;
918 case 'f':
919 parse_file(optarg);
920 break;
921 case 'a':
922 parse_addr_range(optarg);
923 break;
924 case 'b':
925 parse_bits_mask(optarg);
926 break;
927 case 'l':
928 opt_list = 1;
929 break;
930 case 'L':
931 opt_list = 2;
932 break;
933 case 'N':
934 opt_no_summary = 1;
935 break;
936 case 'X':
937 opt_hwpoison = 1;
938 prepare_hwpoison_fd();
939 break;
940 case 'x':
941 opt_unpoison = 1;
942 prepare_hwpoison_fd();
943 break;
944 case 'h':
945 usage();
946 exit(0);
947 default:
948 usage();
949 exit(1);
953 if (opt_list && opt_pid)
954 printf("voffset\t");
955 if (opt_list == 1)
956 printf("offset\tlen\tflags\n");
957 if (opt_list == 2)
958 printf("offset\tflags\n");
960 walk_addr_ranges();
962 if (opt_list == 1)
963 show_page_range(0, 0, 0); /* drain the buffer */
965 if (opt_no_summary)
966 return 0;
968 if (opt_list)
969 printf("\n\n");
971 show_summary();
973 return 0;