Loading...
1/*
2 * page-types: Tool for querying page flags
3 *
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.
7 *
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.
12 *
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.
16 *
17 * Copyright (C) 2009 Intel corporation
18 *
19 * Authors: Wu Fengguang <fengguang.wu@intel.com>
20 */
21
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 <sys/types.h>
36#include <sys/errno.h>
37#include <sys/fcntl.h>
38#include <sys/mount.h>
39#include <sys/statfs.h>
40#include <sys/mman.h>
41#include "../../include/uapi/linux/magic.h"
42#include "../../include/uapi/linux/kernel-page-flags.h"
43#include <api/fs/debugfs.h>
44
45#ifndef MAX_PATH
46# define MAX_PATH 256
47#endif
48
49#ifndef STR
50# define _STR(x) #x
51# define STR(x) _STR(x)
52#endif
53
54/*
55 * pagemap kernel ABI bits
56 */
57
58#define PM_ENTRY_BYTES sizeof(uint64_t)
59#define PM_STATUS_BITS 3
60#define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
61#define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
62#define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
63#define PM_PSHIFT_BITS 6
64#define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
65#define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
66#define __PM_PSHIFT(x) (((uint64_t) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
67#define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
68#define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
69
70#define __PM_SOFT_DIRTY (1LL)
71#define PM_PRESENT PM_STATUS(4LL)
72#define PM_SWAP PM_STATUS(2LL)
73#define PM_SOFT_DIRTY __PM_PSHIFT(__PM_SOFT_DIRTY)
74
75
76/*
77 * kernel page flags
78 */
79
80#define KPF_BYTES 8
81#define PROC_KPAGEFLAGS "/proc/kpageflags"
82
83/* [32-] kernel hacking assistances */
84#define KPF_RESERVED 32
85#define KPF_MLOCKED 33
86#define KPF_MAPPEDTODISK 34
87#define KPF_PRIVATE 35
88#define KPF_PRIVATE_2 36
89#define KPF_OWNER_PRIVATE 37
90#define KPF_ARCH 38
91#define KPF_UNCACHED 39
92#define KPF_SOFTDIRTY 40
93
94/* [48-] take some arbitrary free slots for expanding overloaded flags
95 * not part of kernel API
96 */
97#define KPF_READAHEAD 48
98#define KPF_SLOB_FREE 49
99#define KPF_SLUB_FROZEN 50
100#define KPF_SLUB_DEBUG 51
101
102#define KPF_ALL_BITS ((uint64_t)~0ULL)
103#define KPF_HACKERS_BITS (0xffffULL << 32)
104#define KPF_OVERLOADED_BITS (0xffffULL << 48)
105#define BIT(name) (1ULL << KPF_##name)
106#define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
107
108static const char * const page_flag_names[] = {
109 [KPF_LOCKED] = "L:locked",
110 [KPF_ERROR] = "E:error",
111 [KPF_REFERENCED] = "R:referenced",
112 [KPF_UPTODATE] = "U:uptodate",
113 [KPF_DIRTY] = "D:dirty",
114 [KPF_LRU] = "l:lru",
115 [KPF_ACTIVE] = "A:active",
116 [KPF_SLAB] = "S:slab",
117 [KPF_WRITEBACK] = "W:writeback",
118 [KPF_RECLAIM] = "I:reclaim",
119 [KPF_BUDDY] = "B:buddy",
120
121 [KPF_MMAP] = "M:mmap",
122 [KPF_ANON] = "a:anonymous",
123 [KPF_SWAPCACHE] = "s:swapcache",
124 [KPF_SWAPBACKED] = "b:swapbacked",
125 [KPF_COMPOUND_HEAD] = "H:compound_head",
126 [KPF_COMPOUND_TAIL] = "T:compound_tail",
127 [KPF_HUGE] = "G:huge",
128 [KPF_UNEVICTABLE] = "u:unevictable",
129 [KPF_HWPOISON] = "X:hwpoison",
130 [KPF_NOPAGE] = "n:nopage",
131 [KPF_KSM] = "x:ksm",
132 [KPF_THP] = "t:thp",
133
134 [KPF_RESERVED] = "r:reserved",
135 [KPF_MLOCKED] = "m:mlocked",
136 [KPF_MAPPEDTODISK] = "d:mappedtodisk",
137 [KPF_PRIVATE] = "P:private",
138 [KPF_PRIVATE_2] = "p:private_2",
139 [KPF_OWNER_PRIVATE] = "O:owner_private",
140 [KPF_ARCH] = "h:arch",
141 [KPF_UNCACHED] = "c:uncached",
142 [KPF_SOFTDIRTY] = "f:softdirty",
143
144 [KPF_READAHEAD] = "I:readahead",
145 [KPF_SLOB_FREE] = "P:slob_free",
146 [KPF_SLUB_FROZEN] = "A:slub_frozen",
147 [KPF_SLUB_DEBUG] = "E:slub_debug",
148};
149
150
151static const char * const debugfs_known_mountpoints[] = {
152 "/sys/kernel/debug",
153 "/debug",
154 0,
155};
156
157/*
158 * data structures
159 */
160
161static int opt_raw; /* for kernel developers */
162static int opt_list; /* list pages (in ranges) */
163static int opt_no_summary; /* don't show summary */
164static pid_t opt_pid; /* process to walk */
165const char * opt_file;
166
167#define MAX_ADDR_RANGES 1024
168static int nr_addr_ranges;
169static unsigned long opt_offset[MAX_ADDR_RANGES];
170static unsigned long opt_size[MAX_ADDR_RANGES];
171
172#define MAX_VMAS 10240
173static int nr_vmas;
174static unsigned long pg_start[MAX_VMAS];
175static unsigned long pg_end[MAX_VMAS];
176
177#define MAX_BIT_FILTERS 64
178static int nr_bit_filters;
179static uint64_t opt_mask[MAX_BIT_FILTERS];
180static uint64_t opt_bits[MAX_BIT_FILTERS];
181
182static int page_size;
183
184static int pagemap_fd;
185static int kpageflags_fd;
186
187static int opt_hwpoison;
188static int opt_unpoison;
189
190static char *hwpoison_debug_fs;
191static int hwpoison_inject_fd;
192static int hwpoison_forget_fd;
193
194#define HASH_SHIFT 13
195#define HASH_SIZE (1 << HASH_SHIFT)
196#define HASH_MASK (HASH_SIZE - 1)
197#define HASH_KEY(flags) (flags & HASH_MASK)
198
199static unsigned long total_pages;
200static unsigned long nr_pages[HASH_SIZE];
201static uint64_t page_flags[HASH_SIZE];
202
203
204/*
205 * helper functions
206 */
207
208#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
209
210#define min_t(type, x, y) ({ \
211 type __min1 = (x); \
212 type __min2 = (y); \
213 __min1 < __min2 ? __min1 : __min2; })
214
215#define max_t(type, x, y) ({ \
216 type __max1 = (x); \
217 type __max2 = (y); \
218 __max1 > __max2 ? __max1 : __max2; })
219
220static unsigned long pages2mb(unsigned long pages)
221{
222 return (pages * page_size) >> 20;
223}
224
225static void fatal(const char *x, ...)
226{
227 va_list ap;
228
229 va_start(ap, x);
230 vfprintf(stderr, x, ap);
231 va_end(ap);
232 exit(EXIT_FAILURE);
233}
234
235static int checked_open(const char *pathname, int flags)
236{
237 int fd = open(pathname, flags);
238
239 if (fd < 0) {
240 perror(pathname);
241 exit(EXIT_FAILURE);
242 }
243
244 return fd;
245}
246
247/*
248 * pagemap/kpageflags routines
249 */
250
251static unsigned long do_u64_read(int fd, char *name,
252 uint64_t *buf,
253 unsigned long index,
254 unsigned long count)
255{
256 long bytes;
257
258 if (index > ULONG_MAX / 8)
259 fatal("index overflow: %lu\n", index);
260
261 bytes = pread(fd, buf, count * 8, (off_t)index * 8);
262 if (bytes < 0) {
263 perror(name);
264 exit(EXIT_FAILURE);
265 }
266 if (bytes % 8)
267 fatal("partial read: %lu bytes\n", bytes);
268
269 return bytes / 8;
270}
271
272static unsigned long kpageflags_read(uint64_t *buf,
273 unsigned long index,
274 unsigned long pages)
275{
276 return do_u64_read(kpageflags_fd, PROC_KPAGEFLAGS, buf, index, pages);
277}
278
279static unsigned long pagemap_read(uint64_t *buf,
280 unsigned long index,
281 unsigned long pages)
282{
283 return do_u64_read(pagemap_fd, "/proc/pid/pagemap", buf, index, pages);
284}
285
286static unsigned long pagemap_pfn(uint64_t val)
287{
288 unsigned long pfn;
289
290 if (val & PM_PRESENT)
291 pfn = PM_PFRAME(val);
292 else
293 pfn = 0;
294
295 return pfn;
296}
297
298
299/*
300 * page flag names
301 */
302
303static char *page_flag_name(uint64_t flags)
304{
305 static char buf[65];
306 int present;
307 size_t i, j;
308
309 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
310 present = (flags >> i) & 1;
311 if (!page_flag_names[i]) {
312 if (present)
313 fatal("unknown flag bit %d\n", i);
314 continue;
315 }
316 buf[j++] = present ? page_flag_names[i][0] : '_';
317 }
318
319 return buf;
320}
321
322static char *page_flag_longname(uint64_t flags)
323{
324 static char buf[1024];
325 size_t i, n;
326
327 for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
328 if (!page_flag_names[i])
329 continue;
330 if ((flags >> i) & 1)
331 n += snprintf(buf + n, sizeof(buf) - n, "%s,",
332 page_flag_names[i] + 2);
333 }
334 if (n)
335 n--;
336 buf[n] = '\0';
337
338 return buf;
339}
340
341
342/*
343 * page list and summary
344 */
345
346static void show_page_range(unsigned long voffset, unsigned long offset,
347 unsigned long size, uint64_t flags)
348{
349 static uint64_t flags0;
350 static unsigned long voff;
351 static unsigned long index;
352 static unsigned long count;
353
354 if (flags == flags0 && offset == index + count &&
355 size && voffset == voff + count) {
356 count += size;
357 return;
358 }
359
360 if (count) {
361 if (opt_pid)
362 printf("%lx\t", voff);
363 if (opt_file)
364 printf("%lu\t", voff);
365 printf("%lx\t%lx\t%s\n",
366 index, count, page_flag_name(flags0));
367 }
368
369 flags0 = flags;
370 index = offset;
371 voff = voffset;
372 count = size;
373}
374
375static void flush_page_range(void)
376{
377 show_page_range(0, 0, 0, 0);
378}
379
380static void show_page(unsigned long voffset,
381 unsigned long offset, uint64_t flags)
382{
383 if (opt_pid)
384 printf("%lx\t", voffset);
385 if (opt_file)
386 printf("%lu\t", voffset);
387 printf("%lx\t%s\n", offset, page_flag_name(flags));
388}
389
390static void show_summary(void)
391{
392 size_t i;
393
394 printf(" flags\tpage-count MB"
395 " symbolic-flags\t\t\tlong-symbolic-flags\n");
396
397 for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
398 if (nr_pages[i])
399 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
400 (unsigned long long)page_flags[i],
401 nr_pages[i],
402 pages2mb(nr_pages[i]),
403 page_flag_name(page_flags[i]),
404 page_flag_longname(page_flags[i]));
405 }
406
407 printf(" total\t%10lu %8lu\n",
408 total_pages, pages2mb(total_pages));
409}
410
411
412/*
413 * page flag filters
414 */
415
416static int bit_mask_ok(uint64_t flags)
417{
418 int i;
419
420 for (i = 0; i < nr_bit_filters; i++) {
421 if (opt_bits[i] == KPF_ALL_BITS) {
422 if ((flags & opt_mask[i]) == 0)
423 return 0;
424 } else {
425 if ((flags & opt_mask[i]) != opt_bits[i])
426 return 0;
427 }
428 }
429
430 return 1;
431}
432
433static uint64_t expand_overloaded_flags(uint64_t flags, uint64_t pme)
434{
435 /* SLOB/SLUB overload several page flags */
436 if (flags & BIT(SLAB)) {
437 if (flags & BIT(PRIVATE))
438 flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
439 if (flags & BIT(ACTIVE))
440 flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
441 if (flags & BIT(ERROR))
442 flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
443 }
444
445 /* PG_reclaim is overloaded as PG_readahead in the read path */
446 if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
447 flags ^= BIT(RECLAIM) | BIT(READAHEAD);
448
449 if (pme & PM_SOFT_DIRTY)
450 flags |= BIT(SOFTDIRTY);
451
452 return flags;
453}
454
455static uint64_t well_known_flags(uint64_t flags)
456{
457 /* hide flags intended only for kernel hacker */
458 flags &= ~KPF_HACKERS_BITS;
459
460 /* hide non-hugeTLB compound pages */
461 if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
462 flags &= ~BITS_COMPOUND;
463
464 return flags;
465}
466
467static uint64_t kpageflags_flags(uint64_t flags, uint64_t pme)
468{
469 if (opt_raw)
470 flags = expand_overloaded_flags(flags, pme);
471 else
472 flags = well_known_flags(flags);
473
474 return flags;
475}
476
477/*
478 * page actions
479 */
480
481static void prepare_hwpoison_fd(void)
482{
483 char buf[MAX_PATH + 1];
484
485 hwpoison_debug_fs = debugfs_mount(NULL);
486 if (!hwpoison_debug_fs) {
487 perror("mount debugfs");
488 exit(EXIT_FAILURE);
489 }
490
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);
495 }
496
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);
501 }
502}
503
504static int hwpoison_page(unsigned long offset)
505{
506 char buf[100];
507 int len;
508
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;
514 }
515 return 0;
516}
517
518static int unpoison_page(unsigned long offset)
519{
520 char buf[100];
521 int len;
522
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;
528 }
529 return 0;
530}
531
532/*
533 * page frame walker
534 */
535
536static size_t hash_slot(uint64_t flags)
537{
538 size_t k = HASH_KEY(flags);
539 size_t i;
540
541 /* Explicitly reserve slot 0 for flags 0: the following logic
542 * cannot distinguish an unoccupied slot from slot (flags==0).
543 */
544 if (flags == 0)
545 return 0;
546
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;
554 }
555 if (page_flags[k] == flags)
556 return k;
557 }
558
559 fatal("hash table full: bump up HASH_SHIFT?\n");
560 exit(EXIT_FAILURE);
561}
562
563static void add_page(unsigned long voffset,
564 unsigned long offset, uint64_t flags, uint64_t pme)
565{
566 flags = kpageflags_flags(flags, pme);
567
568 if (!bit_mask_ok(flags))
569 return;
570
571 if (opt_hwpoison)
572 hwpoison_page(offset);
573 if (opt_unpoison)
574 unpoison_page(offset);
575
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);
580
581 nr_pages[hash_slot(flags)]++;
582 total_pages++;
583}
584
585#define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
586static void walk_pfn(unsigned long voffset,
587 unsigned long index,
588 unsigned long count,
589 uint64_t pme)
590{
591 uint64_t buf[KPAGEFLAGS_BATCH];
592 unsigned long batch;
593 unsigned long pages;
594 unsigned long i;
595
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;
601
602 for (i = 0; i < pages; i++)
603 add_page(voffset + i, index + i, buf[i], pme);
604
605 index += pages;
606 count -= pages;
607 }
608}
609
610#define PAGEMAP_BATCH (64 << 10)
611static void walk_vma(unsigned long index, unsigned long count)
612{
613 uint64_t buf[PAGEMAP_BATCH];
614 unsigned long batch;
615 unsigned long pages;
616 unsigned long pfn;
617 unsigned long i;
618
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;
624
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]);
629 }
630
631 index += pages;
632 count -= pages;
633 }
634}
635
636static void walk_task(unsigned long index, unsigned long count)
637{
638 const unsigned long end = index + count;
639 unsigned long start;
640 int i = 0;
641
642 while (index < end) {
643
644 while (pg_end[i] <= index)
645 if (++i >= nr_vmas)
646 return;
647 if (pg_start[i] >= end)
648 return;
649
650 start = max_t(unsigned long, pg_start[i], index);
651 index = min_t(unsigned long, pg_end[i], end);
652
653 assert(start < index);
654 walk_vma(start, index - start);
655 }
656}
657
658static void add_addr_range(unsigned long offset, unsigned long size)
659{
660 if (nr_addr_ranges >= MAX_ADDR_RANGES)
661 fatal("too many addr ranges\n");
662
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++;
666}
667
668static void walk_addr_ranges(void)
669{
670 int i;
671
672 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
673
674 if (!nr_addr_ranges)
675 add_addr_range(0, ULONG_MAX);
676
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]);
682
683 close(kpageflags_fd);
684}
685
686
687/*
688 * user interface
689 */
690
691static const char *page_flag_type(uint64_t flag)
692{
693 if (flag & KPF_HACKERS_BITS)
694 return "(r)";
695 if (flag & KPF_OVERLOADED_BITS)
696 return "(o)";
697 return " ";
698}
699
700static void usage(void)
701{
702 size_t i, j;
703
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"
734 );
735
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');
744 }
745 }
746 printf("\n "
747 "(r) raw mode bits (o) overloaded bits\n");
748}
749
750static unsigned long long parse_number(const char *str)
751{
752 unsigned long long n;
753
754 n = strtoll(str, NULL, 0);
755
756 if (n == 0 && str[0] != '0')
757 fatal("invalid name or number: %s\n", str);
758
759 return n;
760}
761
762static void parse_pid(const char *str)
763{
764 FILE *file;
765 char buf[5000];
766
767 opt_pid = parse_number(str);
768
769 sprintf(buf, "/proc/%d/pagemap", opt_pid);
770 pagemap_fd = checked_open(buf, O_RDONLY);
771
772 sprintf(buf, "/proc/%d/maps", opt_pid);
773 file = fopen(buf, "r");
774 if (!file) {
775 perror(buf);
776 exit(EXIT_FAILURE);
777 }
778
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;
787
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;
798 }
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;
804 }
805 }
806 fclose(file);
807}
808
809static void show_file(const char *name, const struct stat *st)
810{
811 unsigned long long size = st->st_size;
812 char atime[64], mtime[64];
813 long now = time(NULL);
814
815 printf("%s\tInode: %u\tSize: %llu (%llu pages)\n",
816 name, (unsigned)st->st_ino,
817 size, (size + page_size - 1) / page_size);
818
819 strftime(atime, sizeof(atime), "%c", localtime(&st->st_atime));
820 strftime(mtime, sizeof(mtime), "%c", localtime(&st->st_mtime));
821
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);
825}
826
827static void walk_file(const char *name, const struct stat *st)
828{
829 uint8_t vec[PAGEMAP_BATCH];
830 uint64_t buf[PAGEMAP_BATCH], flags;
831 unsigned long nr_pages, pfn, i;
832 int fd;
833 off_t off;
834 ssize_t len;
835 void *ptr;
836 int first = 1;
837
838 fd = checked_open(name, O_RDONLY|O_NOATIME|O_NOFOLLOW);
839
840 for (off = 0; off < st->st_size; off += len) {
841 nr_pages = (st->st_size - off + page_size - 1) / page_size;
842 if (nr_pages > PAGEMAP_BATCH)
843 nr_pages = PAGEMAP_BATCH;
844 len = nr_pages * page_size;
845
846 ptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, off);
847 if (ptr == MAP_FAILED)
848 fatal("mmap failed: %s", name);
849
850 /* determine cached pages */
851 if (mincore(ptr, len, vec))
852 fatal("mincore failed: %s", name);
853
854 /* turn off readahead */
855 if (madvise(ptr, len, MADV_RANDOM))
856 fatal("madvice failed: %s", name);
857
858 /* populate ptes */
859 for (i = 0; i < nr_pages ; i++) {
860 if (vec[i] & 1)
861 (void)*(volatile int *)(ptr + i * page_size);
862 }
863
864 /* turn off harvesting reference bits */
865 if (madvise(ptr, len, MADV_SEQUENTIAL))
866 fatal("madvice failed: %s", name);
867
868 if (pagemap_read(buf, (unsigned long)ptr / page_size,
869 nr_pages) != nr_pages)
870 fatal("cannot read pagemap");
871
872 munmap(ptr, len);
873
874 for (i = 0; i < nr_pages; i++) {
875 pfn = pagemap_pfn(buf[i]);
876 if (!pfn)
877 continue;
878 if (!kpageflags_read(&flags, pfn, 1))
879 continue;
880 if (first && opt_list) {
881 first = 0;
882 flush_page_range();
883 show_file(name, st);
884 }
885 add_page(off / page_size + i, pfn, flags, buf[i]);
886 }
887 }
888
889 close(fd);
890}
891
892int walk_tree(const char *name, const struct stat *st, int type, struct FTW *f)
893{
894 (void)f;
895 switch (type) {
896 case FTW_F:
897 if (S_ISREG(st->st_mode))
898 walk_file(name, st);
899 break;
900 case FTW_DNR:
901 fprintf(stderr, "cannot read dir: %s\n", name);
902 break;
903 }
904 return 0;
905}
906
907static void walk_page_cache(void)
908{
909 struct stat st;
910
911 kpageflags_fd = checked_open(PROC_KPAGEFLAGS, O_RDONLY);
912 pagemap_fd = checked_open("/proc/self/pagemap", O_RDONLY);
913
914 if (stat(opt_file, &st))
915 fatal("stat failed: %s\n", opt_file);
916
917 if (S_ISREG(st.st_mode)) {
918 walk_file(opt_file, &st);
919 } else if (S_ISDIR(st.st_mode)) {
920 /* do not follow symlinks and mountpoints */
921 if (nftw(opt_file, walk_tree, 64, FTW_MOUNT | FTW_PHYS) < 0)
922 fatal("nftw failed: %s\n", opt_file);
923 } else
924 fatal("unhandled file type: %s\n", opt_file);
925
926 close(kpageflags_fd);
927 close(pagemap_fd);
928}
929
930static void parse_file(const char *name)
931{
932 opt_file = name;
933}
934
935static void parse_addr_range(const char *optarg)
936{
937 unsigned long offset;
938 unsigned long size;
939 char *p;
940
941 p = strchr(optarg, ',');
942 if (!p)
943 p = strchr(optarg, '+');
944
945 if (p == optarg) {
946 offset = 0;
947 size = parse_number(p + 1);
948 } else if (p) {
949 offset = parse_number(optarg);
950 if (p[1] == '\0')
951 size = ULONG_MAX;
952 else {
953 size = parse_number(p + 1);
954 if (*p == ',') {
955 if (size < offset)
956 fatal("invalid range: %lu,%lu\n",
957 offset, size);
958 size -= offset;
959 }
960 }
961 } else {
962 offset = parse_number(optarg);
963 size = 1;
964 }
965
966 add_addr_range(offset, size);
967}
968
969static void add_bits_filter(uint64_t mask, uint64_t bits)
970{
971 if (nr_bit_filters >= MAX_BIT_FILTERS)
972 fatal("too much bit filters\n");
973
974 opt_mask[nr_bit_filters] = mask;
975 opt_bits[nr_bit_filters] = bits;
976 nr_bit_filters++;
977}
978
979static uint64_t parse_flag_name(const char *str, int len)
980{
981 size_t i;
982
983 if (!*str || !len)
984 return 0;
985
986 if (len <= 8 && !strncmp(str, "compound", len))
987 return BITS_COMPOUND;
988
989 for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
990 if (!page_flag_names[i])
991 continue;
992 if (!strncmp(str, page_flag_names[i] + 2, len))
993 return 1ULL << i;
994 }
995
996 return parse_number(str);
997}
998
999static uint64_t parse_flag_names(const char *str, int all)
1000{
1001 const char *p = str;
1002 uint64_t flags = 0;
1003
1004 while (1) {
1005 if (*p == ',' || *p == '=' || *p == '\0') {
1006 if ((*str != '~') || (*str == '~' && all && *++str))
1007 flags |= parse_flag_name(str, p - str);
1008 if (*p != ',')
1009 break;
1010 str = p + 1;
1011 }
1012 p++;
1013 }
1014
1015 return flags;
1016}
1017
1018static void parse_bits_mask(const char *optarg)
1019{
1020 uint64_t mask;
1021 uint64_t bits;
1022 const char *p;
1023
1024 p = strchr(optarg, '=');
1025 if (p == optarg) {
1026 mask = KPF_ALL_BITS;
1027 bits = parse_flag_names(p + 1, 0);
1028 } else if (p) {
1029 mask = parse_flag_names(optarg, 0);
1030 bits = parse_flag_names(p + 1, 0);
1031 } else if (strchr(optarg, '~')) {
1032 mask = parse_flag_names(optarg, 1);
1033 bits = parse_flag_names(optarg, 0);
1034 } else {
1035 mask = parse_flag_names(optarg, 0);
1036 bits = KPF_ALL_BITS;
1037 }
1038
1039 add_bits_filter(mask, bits);
1040}
1041
1042static void describe_flags(const char *optarg)
1043{
1044 uint64_t flags = parse_flag_names(optarg, 0);
1045
1046 printf("0x%016llx\t%s\t%s\n",
1047 (unsigned long long)flags,
1048 page_flag_name(flags),
1049 page_flag_longname(flags));
1050}
1051
1052static const struct option opts[] = {
1053 { "raw" , 0, NULL, 'r' },
1054 { "pid" , 1, NULL, 'p' },
1055 { "file" , 1, NULL, 'f' },
1056 { "addr" , 1, NULL, 'a' },
1057 { "bits" , 1, NULL, 'b' },
1058 { "describe" , 1, NULL, 'd' },
1059 { "list" , 0, NULL, 'l' },
1060 { "list-each" , 0, NULL, 'L' },
1061 { "no-summary", 0, NULL, 'N' },
1062 { "hwpoison" , 0, NULL, 'X' },
1063 { "unpoison" , 0, NULL, 'x' },
1064 { "help" , 0, NULL, 'h' },
1065 { NULL , 0, NULL, 0 }
1066};
1067
1068int main(int argc, char *argv[])
1069{
1070 int c;
1071
1072 page_size = getpagesize();
1073
1074 while ((c = getopt_long(argc, argv,
1075 "rp:f:a:b:d:lLNXxh", opts, NULL)) != -1) {
1076 switch (c) {
1077 case 'r':
1078 opt_raw = 1;
1079 break;
1080 case 'p':
1081 parse_pid(optarg);
1082 break;
1083 case 'f':
1084 parse_file(optarg);
1085 break;
1086 case 'a':
1087 parse_addr_range(optarg);
1088 break;
1089 case 'b':
1090 parse_bits_mask(optarg);
1091 break;
1092 case 'd':
1093 describe_flags(optarg);
1094 exit(0);
1095 case 'l':
1096 opt_list = 1;
1097 break;
1098 case 'L':
1099 opt_list = 2;
1100 break;
1101 case 'N':
1102 opt_no_summary = 1;
1103 break;
1104 case 'X':
1105 opt_hwpoison = 1;
1106 prepare_hwpoison_fd();
1107 break;
1108 case 'x':
1109 opt_unpoison = 1;
1110 prepare_hwpoison_fd();
1111 break;
1112 case 'h':
1113 usage();
1114 exit(0);
1115 default:
1116 usage();
1117 exit(1);
1118 }
1119 }
1120
1121 if (opt_list && opt_pid)
1122 printf("voffset\t");
1123 if (opt_list && opt_file)
1124 printf("foffset\t");
1125 if (opt_list == 1)
1126 printf("offset\tlen\tflags\n");
1127 if (opt_list == 2)
1128 printf("offset\tflags\n");
1129
1130 if (opt_file)
1131 walk_page_cache();
1132 else
1133 walk_addr_ranges();
1134
1135 if (opt_list == 1)
1136 flush_page_range();
1137
1138 if (opt_no_summary)
1139 return 0;
1140
1141 if (opt_list)
1142 printf("\n\n");
1143
1144 show_summary();
1145
1146 return 0;
1147}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * page-types: Tool for querying page flags
4 *
5 * Copyright (C) 2009 Intel corporation
6 *
7 * Authors: Wu Fengguang <fengguang.wu@intel.com>
8 */
9
10#define _FILE_OFFSET_BITS 64
11#define _GNU_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 <ftw.h>
22#include <time.h>
23#include <setjmp.h>
24#include <signal.h>
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>
30#include <sys/mman.h>
31#include "../../include/uapi/linux/magic.h"
32#include "../../include/uapi/linux/kernel-page-flags.h"
33#include <api/fs/fs.h>
34
35#ifndef MAX_PATH
36# define MAX_PATH 256
37#endif
38
39#ifndef STR
40# define _STR(x) #x
41# define STR(x) _STR(x)
42#endif
43
44/*
45 * pagemap kernel ABI bits
46 */
47
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)
59
60/*
61 * kernel page flags
62 */
63
64#define KPF_BYTES 8
65#define PROC_KPAGEFLAGS "/proc/kpageflags"
66#define PROC_KPAGECOUNT "/proc/kpagecount"
67#define PROC_KPAGECGROUP "/proc/kpagecgroup"
68
69#define SYS_KERNEL_MM_PAGE_IDLE "/sys/kernel/mm/page_idle/bitmap"
70
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
78#define KPF_ARCH 38
79#define KPF_UNCACHED 39
80#define KPF_SOFTDIRTY 40
81
82/* [48-] take some arbitrary free slots for expanding overloaded flags
83 * not part of kernel API
84 */
85#define KPF_READAHEAD 48
86#define KPF_SLOB_FREE 49
87#define KPF_SLUB_FROZEN 50
88#define KPF_SLUB_DEBUG 51
89#define KPF_FILE 61
90#define KPF_SWAP 62
91#define KPF_MMAP_EXCLUSIVE 63
92
93#define KPF_ALL_BITS ((uint64_t)~0ULL)
94#define KPF_HACKERS_BITS (0xffffULL << 32)
95#define KPF_OVERLOADED_BITS (0xffffULL << 48)
96#define BIT(name) (1ULL << KPF_##name)
97#define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
98
99static const char * const page_flag_names[] = {
100 [KPF_LOCKED] = "L:locked",
101 [KPF_ERROR] = "E:error",
102 [KPF_REFERENCED] = "R:referenced",
103 [KPF_UPTODATE] = "U:uptodate",
104 [KPF_DIRTY] = "D:dirty",
105 [KPF_LRU] = "l:lru",
106 [KPF_ACTIVE] = "A:active",
107 [KPF_SLAB] = "S:slab",
108 [KPF_WRITEBACK] = "W:writeback",
109 [KPF_RECLAIM] = "I:reclaim",
110 [KPF_BUDDY] = "B:buddy",
111
112 [KPF_MMAP] = "M:mmap",
113 [KPF_ANON] = "a:anonymous",
114 [KPF_SWAPCACHE] = "s:swapcache",
115 [KPF_SWAPBACKED] = "b:swapbacked",
116 [KPF_COMPOUND_HEAD] = "H:compound_head",
117 [KPF_COMPOUND_TAIL] = "T:compound_tail",
118 [KPF_HUGE] = "G:huge",
119 [KPF_UNEVICTABLE] = "u:unevictable",
120 [KPF_HWPOISON] = "X:hwpoison",
121 [KPF_NOPAGE] = "n:nopage",
122 [KPF_KSM] = "x:ksm",
123 [KPF_THP] = "t:thp",
124 [KPF_OFFLINE] = "o:offline",
125 [KPF_PGTABLE] = "g:pgtable",
126 [KPF_ZERO_PAGE] = "z:zero_page",
127 [KPF_IDLE] = "i:idle_page",
128
129 [KPF_RESERVED] = "r:reserved",
130 [KPF_MLOCKED] = "m:mlocked",
131 [KPF_MAPPEDTODISK] = "d:mappedtodisk",
132 [KPF_PRIVATE] = "P:private",
133 [KPF_PRIVATE_2] = "p:private_2",
134 [KPF_OWNER_PRIVATE] = "O:owner_private",
135 [KPF_ARCH] = "h:arch",
136 [KPF_UNCACHED] = "c:uncached",
137 [KPF_SOFTDIRTY] = "f:softdirty",
138
139 [KPF_READAHEAD] = "I:readahead",
140 [KPF_SLOB_FREE] = "P:slob_free",
141 [KPF_SLUB_FROZEN] = "A:slub_frozen",
142 [KPF_SLUB_DEBUG] = "E:slub_debug",
143
144 [KPF_FILE] = "F:file",
145 [KPF_SWAP] = "w:swap",
146 [KPF_MMAP_EXCLUSIVE] = "1:mmap_exclusive",
147};
148
149
150/*
151 * data structures
152 */
153
154static int opt_raw; /* for kernel developers */
155static int opt_list; /* list pages (in ranges) */
156static int opt_mark_idle; /* set accessed bit */
157static int opt_no_summary; /* don't show summary */
158static pid_t opt_pid; /* process to walk */
159const char *opt_file; /* file or directory path */
160static uint64_t opt_cgroup; /* cgroup inode */
161static int opt_list_cgroup;/* list page cgroup */
162static int opt_list_mapcnt;/* list page map count */
163static const char *opt_kpageflags;/* kpageflags file to parse */
164
165#define MAX_ADDR_RANGES 1024
166static int nr_addr_ranges;
167static unsigned long opt_offset[MAX_ADDR_RANGES];
168static unsigned long opt_size[MAX_ADDR_RANGES];
169
170#define MAX_VMAS 10240
171static int nr_vmas;
172static unsigned long pg_start[MAX_VMAS];
173static unsigned long pg_end[MAX_VMAS];
174
175#define MAX_BIT_FILTERS 64
176static int nr_bit_filters;
177static uint64_t opt_mask[MAX_BIT_FILTERS];
178static uint64_t opt_bits[MAX_BIT_FILTERS];
179
180static int page_size;
181
182static int pagemap_fd;
183static int kpageflags_fd;
184static int kpagecount_fd = -1;
185static int kpagecgroup_fd = -1;
186static int page_idle_fd = -1;
187
188static int opt_hwpoison;
189static int opt_unpoison;
190
191static const char *hwpoison_debug_fs;
192static int hwpoison_inject_fd;
193static int hwpoison_forget_fd;
194
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)
199
200static unsigned long total_pages;
201static unsigned long nr_pages[HASH_SIZE];
202static uint64_t page_flags[HASH_SIZE];
203
204
205/*
206 * helper functions
207 */
208
209#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
210
211#define min_t(type, x, y) ({ \
212 type __min1 = (x); \
213 type __min2 = (y); \
214 __min1 < __min2 ? __min1 : __min2; })
215
216#define max_t(type, x, y) ({ \
217 type __max1 = (x); \
218 type __max2 = (y); \
219 __max1 > __max2 ? __max1 : __max2; })
220
221static unsigned long pages2mb(unsigned long pages)
222{
223 return (pages * page_size) >> 20;
224}
225
226static void fatal(const char *x, ...)
227{
228 va_list ap;
229
230 va_start(ap, x);
231 vfprintf(stderr, x, ap);
232 va_end(ap);
233 exit(EXIT_FAILURE);
234}
235
236static int checked_open(const char *pathname, int flags)
237{
238 int fd = open(pathname, flags);
239
240 if (fd < 0) {
241 perror(pathname);
242 exit(EXIT_FAILURE);
243 }
244
245 return fd;
246}
247
248/*
249 * pagemap/kpageflags routines
250 */
251
252static unsigned long do_u64_read(int fd, const char *name,
253 uint64_t *buf,
254 unsigned long index,
255 unsigned long count)
256{
257 long bytes;
258
259 if (index > ULONG_MAX / 8)
260 fatal("index overflow: %lu\n", index);
261
262 bytes = pread(fd, buf, count * 8, (off_t)index * 8);
263 if (bytes < 0) {
264 perror(name);
265 exit(EXIT_FAILURE);
266 }
267 if (bytes % 8)
268 fatal("partial read: %lu bytes\n", bytes);
269
270 return bytes / 8;
271}
272
273static unsigned long kpageflags_read(uint64_t *buf,
274 unsigned long index,
275 unsigned long pages)
276{
277 return do_u64_read(kpageflags_fd, opt_kpageflags, buf, index, pages);
278}
279
280static unsigned long kpagecgroup_read(uint64_t *buf,
281 unsigned long index,
282 unsigned long pages)
283{
284 if (kpagecgroup_fd < 0)
285 return pages;
286
287 return do_u64_read(kpagecgroup_fd, opt_kpageflags, buf, index, pages);
288}
289
290static unsigned long kpagecount_read(uint64_t *buf,
291 unsigned long index,
292 unsigned long pages)
293{
294 return kpagecount_fd < 0 ? pages :
295 do_u64_read(kpagecount_fd, PROC_KPAGECOUNT,
296 buf, index, pages);
297}
298
299static unsigned long pagemap_read(uint64_t *buf,
300 unsigned long index,
301 unsigned long pages)
302{
303 return do_u64_read(pagemap_fd, "/proc/pid/pagemap", buf, index, pages);
304}
305
306static unsigned long pagemap_pfn(uint64_t val)
307{
308 unsigned long pfn;
309
310 if (val & PM_PRESENT)
311 pfn = PM_PFRAME(val);
312 else
313 pfn = 0;
314
315 return pfn;
316}
317
318static unsigned long pagemap_swap_offset(uint64_t val)
319{
320 return val & PM_SWAP ? PM_SWAP_OFFSET(val) : 0;
321}
322
323/*
324 * page flag names
325 */
326
327static char *page_flag_name(uint64_t flags)
328{
329 static char buf[65];
330 int present;
331 size_t i, j;
332
333 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
334 present = (flags >> i) & 1;
335 if (!page_flag_names[i]) {
336 if (present)
337 fatal("unknown flag bit %d\n", i);
338 continue;
339 }
340 buf[j++] = present ? page_flag_names[i][0] : '_';
341 }
342
343 return buf;
344}
345
346static char *page_flag_longname(uint64_t flags)
347{
348 static char buf[1024];
349 size_t i, n;
350
351 for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
352 if (!page_flag_names[i])
353 continue;
354 if ((flags >> i) & 1)
355 n += snprintf(buf + n, sizeof(buf) - n, "%s,",
356 page_flag_names[i] + 2);
357 }
358 if (n)
359 n--;
360 buf[n] = '\0';
361
362 return buf;
363}
364
365
366/*
367 * page list and summary
368 */
369
370static void show_page_range(unsigned long voffset, unsigned long offset,
371 unsigned long size, uint64_t flags,
372 uint64_t cgroup, uint64_t mapcnt)
373{
374 static uint64_t flags0;
375 static uint64_t cgroup0;
376 static uint64_t mapcnt0;
377 static unsigned long voff;
378 static unsigned long index;
379 static unsigned long count;
380
381 if (flags == flags0 && cgroup == cgroup0 && mapcnt == mapcnt0 &&
382 offset == index + count && size && voffset == voff + count) {
383 count += size;
384 return;
385 }
386
387 if (count) {
388 if (opt_pid)
389 printf("%lx\t", voff);
390 if (opt_file)
391 printf("%lu\t", voff);
392 if (opt_list_cgroup)
393 printf("@%llu\t", (unsigned long long)cgroup0);
394 if (opt_list_mapcnt)
395 printf("%lu\t", mapcnt0);
396 printf("%lx\t%lx\t%s\n",
397 index, count, page_flag_name(flags0));
398 }
399
400 flags0 = flags;
401 cgroup0 = cgroup;
402 mapcnt0 = mapcnt;
403 index = offset;
404 voff = voffset;
405 count = size;
406}
407
408static void flush_page_range(void)
409{
410 show_page_range(0, 0, 0, 0, 0, 0);
411}
412
413static void show_page(unsigned long voffset, unsigned long offset,
414 uint64_t flags, uint64_t cgroup, uint64_t mapcnt)
415{
416 if (opt_pid)
417 printf("%lx\t", voffset);
418 if (opt_file)
419 printf("%lu\t", voffset);
420 if (opt_list_cgroup)
421 printf("@%llu\t", (unsigned long long)cgroup);
422 if (opt_list_mapcnt)
423 printf("%lu\t", mapcnt);
424
425 printf("%lx\t%s\n", offset, page_flag_name(flags));
426}
427
428static void show_summary(void)
429{
430 size_t i;
431
432 printf(" flags\tpage-count MB"
433 " symbolic-flags\t\t\tlong-symbolic-flags\n");
434
435 for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
436 if (nr_pages[i])
437 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
438 (unsigned long long)page_flags[i],
439 nr_pages[i],
440 pages2mb(nr_pages[i]),
441 page_flag_name(page_flags[i]),
442 page_flag_longname(page_flags[i]));
443 }
444
445 printf(" total\t%10lu %8lu\n",
446 total_pages, pages2mb(total_pages));
447}
448
449
450/*
451 * page flag filters
452 */
453
454static int bit_mask_ok(uint64_t flags)
455{
456 int i;
457
458 for (i = 0; i < nr_bit_filters; i++) {
459 if (opt_bits[i] == KPF_ALL_BITS) {
460 if ((flags & opt_mask[i]) == 0)
461 return 0;
462 } else {
463 if ((flags & opt_mask[i]) != opt_bits[i])
464 return 0;
465 }
466 }
467
468 return 1;
469}
470
471static uint64_t expand_overloaded_flags(uint64_t flags, uint64_t pme)
472{
473 /* SLOB/SLUB overload several page flags */
474 if (flags & BIT(SLAB)) {
475 if (flags & BIT(PRIVATE))
476 flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
477 if (flags & BIT(ACTIVE))
478 flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
479 if (flags & BIT(ERROR))
480 flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
481 }
482
483 /* PG_reclaim is overloaded as PG_readahead in the read path */
484 if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
485 flags ^= BIT(RECLAIM) | BIT(READAHEAD);
486
487 if (pme & PM_SOFT_DIRTY)
488 flags |= BIT(SOFTDIRTY);
489 if (pme & PM_FILE)
490 flags |= BIT(FILE);
491 if (pme & PM_SWAP)
492 flags |= BIT(SWAP);
493 if (pme & PM_MMAP_EXCLUSIVE)
494 flags |= BIT(MMAP_EXCLUSIVE);
495
496 return flags;
497}
498
499static uint64_t well_known_flags(uint64_t flags)
500{
501 /* hide flags intended only for kernel hacker */
502 flags &= ~KPF_HACKERS_BITS;
503
504 /* hide non-hugeTLB compound pages */
505 if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
506 flags &= ~BITS_COMPOUND;
507
508 return flags;
509}
510
511static uint64_t kpageflags_flags(uint64_t flags, uint64_t pme)
512{
513 if (opt_raw)
514 flags = expand_overloaded_flags(flags, pme);
515 else
516 flags = well_known_flags(flags);
517
518 return flags;
519}
520
521/*
522 * page actions
523 */
524
525static void prepare_hwpoison_fd(void)
526{
527 char buf[MAX_PATH + 1];
528
529 hwpoison_debug_fs = debugfs__mount();
530 if (!hwpoison_debug_fs) {
531 perror("mount debugfs");
532 exit(EXIT_FAILURE);
533 }
534
535 if (opt_hwpoison && !hwpoison_inject_fd) {
536 snprintf(buf, MAX_PATH, "%s/hwpoison/corrupt-pfn",
537 hwpoison_debug_fs);
538 hwpoison_inject_fd = checked_open(buf, O_WRONLY);
539 }
540
541 if (opt_unpoison && !hwpoison_forget_fd) {
542 snprintf(buf, MAX_PATH, "%s/hwpoison/unpoison-pfn",
543 hwpoison_debug_fs);
544 hwpoison_forget_fd = checked_open(buf, O_WRONLY);
545 }
546}
547
548static int hwpoison_page(unsigned long offset)
549{
550 char buf[100];
551 int len;
552
553 len = sprintf(buf, "0x%lx\n", offset);
554 len = write(hwpoison_inject_fd, buf, len);
555 if (len < 0) {
556 perror("hwpoison inject");
557 return len;
558 }
559 return 0;
560}
561
562static int unpoison_page(unsigned long offset)
563{
564 char buf[100];
565 int len;
566
567 len = sprintf(buf, "0x%lx\n", offset);
568 len = write(hwpoison_forget_fd, buf, len);
569 if (len < 0) {
570 perror("hwpoison forget");
571 return len;
572 }
573 return 0;
574}
575
576static int mark_page_idle(unsigned long offset)
577{
578 static unsigned long off;
579 static uint64_t buf;
580 int len;
581
582 if ((offset / 64 == off / 64) || buf == 0) {
583 buf |= 1UL << (offset % 64);
584 off = offset;
585 return 0;
586 }
587
588 len = pwrite(page_idle_fd, &buf, 8, 8 * (off / 64));
589 if (len < 0) {
590 perror("mark page idle");
591 return len;
592 }
593
594 buf = 1UL << (offset % 64);
595 off = offset;
596
597 return 0;
598}
599
600/*
601 * page frame walker
602 */
603
604static size_t hash_slot(uint64_t flags)
605{
606 size_t k = HASH_KEY(flags);
607 size_t i;
608
609 /* Explicitly reserve slot 0 for flags 0: the following logic
610 * cannot distinguish an unoccupied slot from slot (flags==0).
611 */
612 if (flags == 0)
613 return 0;
614
615 /* search through the remaining (HASH_SIZE-1) slots */
616 for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) {
617 if (!k || k >= ARRAY_SIZE(page_flags))
618 k = 1;
619 if (page_flags[k] == 0) {
620 page_flags[k] = flags;
621 return k;
622 }
623 if (page_flags[k] == flags)
624 return k;
625 }
626
627 fatal("hash table full: bump up HASH_SHIFT?\n");
628 exit(EXIT_FAILURE);
629}
630
631static void add_page(unsigned long voffset, unsigned long offset,
632 uint64_t flags, uint64_t cgroup, uint64_t mapcnt,
633 uint64_t pme)
634{
635 flags = kpageflags_flags(flags, pme);
636
637 if (!bit_mask_ok(flags))
638 return;
639
640 if (opt_cgroup && cgroup != (uint64_t)opt_cgroup)
641 return;
642
643 if (opt_hwpoison)
644 hwpoison_page(offset);
645 if (opt_unpoison)
646 unpoison_page(offset);
647
648 if (opt_mark_idle)
649 mark_page_idle(offset);
650
651 if (opt_list == 1)
652 show_page_range(voffset, offset, 1, flags, cgroup, mapcnt);
653 else if (opt_list == 2)
654 show_page(voffset, offset, flags, cgroup, mapcnt);
655
656 nr_pages[hash_slot(flags)]++;
657 total_pages++;
658}
659
660#define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
661static void walk_pfn(unsigned long voffset,
662 unsigned long index,
663 unsigned long count,
664 uint64_t pme)
665{
666 uint64_t buf[KPAGEFLAGS_BATCH];
667 uint64_t cgi[KPAGEFLAGS_BATCH];
668 uint64_t cnt[KPAGEFLAGS_BATCH];
669 unsigned long batch;
670 unsigned long pages;
671 unsigned long i;
672
673 /*
674 * kpagecgroup_read() reads only if kpagecgroup were opened, but
675 * /proc/kpagecgroup might even not exist, so it's better to fill
676 * them with zeros here.
677 */
678 if (count == 1)
679 cgi[0] = 0;
680 else
681 memset(cgi, 0, sizeof cgi);
682
683 while (count) {
684 batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
685 pages = kpageflags_read(buf, index, batch);
686 if (pages == 0)
687 break;
688
689 if (kpagecgroup_read(cgi, index, pages) != pages)
690 fatal("kpagecgroup returned fewer pages than expected");
691
692 if (kpagecount_read(cnt, index, pages) != pages)
693 fatal("kpagecount returned fewer pages than expected");
694
695 for (i = 0; i < pages; i++)
696 add_page(voffset + i, index + i,
697 buf[i], cgi[i], cnt[i], pme);
698
699 index += pages;
700 count -= pages;
701 }
702}
703
704static void walk_swap(unsigned long voffset, uint64_t pme)
705{
706 uint64_t flags = kpageflags_flags(0, pme);
707
708 if (!bit_mask_ok(flags))
709 return;
710
711 if (opt_cgroup)
712 return;
713
714 if (opt_list == 1)
715 show_page_range(voffset, pagemap_swap_offset(pme),
716 1, flags, 0, 0);
717 else if (opt_list == 2)
718 show_page(voffset, pagemap_swap_offset(pme), flags, 0, 0);
719
720 nr_pages[hash_slot(flags)]++;
721 total_pages++;
722}
723
724#define PAGEMAP_BATCH (64 << 10)
725static void walk_vma(unsigned long index, unsigned long count)
726{
727 uint64_t buf[PAGEMAP_BATCH];
728 unsigned long batch;
729 unsigned long pages;
730 unsigned long pfn;
731 unsigned long i;
732
733 while (count) {
734 batch = min_t(unsigned long, count, PAGEMAP_BATCH);
735 pages = pagemap_read(buf, index, batch);
736 if (pages == 0)
737 break;
738
739 for (i = 0; i < pages; i++) {
740 pfn = pagemap_pfn(buf[i]);
741 if (pfn)
742 walk_pfn(index + i, pfn, 1, buf[i]);
743 if (buf[i] & PM_SWAP)
744 walk_swap(index + i, buf[i]);
745 }
746
747 index += pages;
748 count -= pages;
749 }
750}
751
752static void walk_task(unsigned long index, unsigned long count)
753{
754 const unsigned long end = index + count;
755 unsigned long start;
756 int i = 0;
757
758 while (index < end) {
759
760 while (pg_end[i] <= index)
761 if (++i >= nr_vmas)
762 return;
763 if (pg_start[i] >= end)
764 return;
765
766 start = max_t(unsigned long, pg_start[i], index);
767 index = min_t(unsigned long, pg_end[i], end);
768
769 assert(start < index);
770 walk_vma(start, index - start);
771 }
772}
773
774static void add_addr_range(unsigned long offset, unsigned long size)
775{
776 if (nr_addr_ranges >= MAX_ADDR_RANGES)
777 fatal("too many addr ranges\n");
778
779 opt_offset[nr_addr_ranges] = offset;
780 opt_size[nr_addr_ranges] = min_t(unsigned long, size, ULONG_MAX-offset);
781 nr_addr_ranges++;
782}
783
784static void walk_addr_ranges(void)
785{
786 int i;
787
788 kpageflags_fd = checked_open(opt_kpageflags, O_RDONLY);
789
790 if (!nr_addr_ranges)
791 add_addr_range(0, ULONG_MAX);
792
793 for (i = 0; i < nr_addr_ranges; i++)
794 if (!opt_pid)
795 walk_pfn(opt_offset[i], opt_offset[i], opt_size[i], 0);
796 else
797 walk_task(opt_offset[i], opt_size[i]);
798
799 if (opt_mark_idle)
800 mark_page_idle(0);
801
802 close(kpageflags_fd);
803}
804
805
806/*
807 * user interface
808 */
809
810static const char *page_flag_type(uint64_t flag)
811{
812 if (flag & KPF_HACKERS_BITS)
813 return "(r)";
814 if (flag & KPF_OVERLOADED_BITS)
815 return "(o)";
816 return " ";
817}
818
819static void usage(void)
820{
821 size_t i, j;
822
823 printf(
824"page-types [options]\n"
825" -r|--raw Raw mode, for kernel developers\n"
826" -d|--describe flags Describe flags\n"
827" -a|--addr addr-spec Walk a range of pages\n"
828" -b|--bits bits-spec Walk pages with specified bits\n"
829" -c|--cgroup path|@inode Walk pages within memory cgroup\n"
830" -p|--pid pid Walk process address space\n"
831" -f|--file filename Walk file address space\n"
832" -i|--mark-idle Mark pages idle\n"
833" -l|--list Show page details in ranges\n"
834" -L|--list-each Show page details one by one\n"
835" -C|--list-cgroup Show cgroup inode for pages\n"
836" -M|--list-mapcnt Show page map count\n"
837" -N|--no-summary Don't show summary info\n"
838" -X|--hwpoison hwpoison pages\n"
839" -x|--unpoison unpoison pages\n"
840" -F|--kpageflags filename kpageflags file to parse\n"
841" -h|--help Show this usage message\n"
842"flags:\n"
843" 0x10 bitfield format, e.g.\n"
844" anon bit-name, e.g.\n"
845" 0x10,anon comma-separated list, e.g.\n"
846"addr-spec:\n"
847" N one page at offset N (unit: pages)\n"
848" N+M pages range from N to N+M-1\n"
849" N,M pages range from N to M-1\n"
850" N, pages range from N to end\n"
851" ,M pages range from 0 to M-1\n"
852"bits-spec:\n"
853" bit1,bit2 (flags & (bit1|bit2)) != 0\n"
854" bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
855" bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
856" =bit1,bit2 flags == (bit1|bit2)\n"
857"bit-names:\n"
858 );
859
860 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
861 if (!page_flag_names[i])
862 continue;
863 printf("%16s%s", page_flag_names[i] + 2,
864 page_flag_type(1ULL << i));
865 if (++j > 3) {
866 j = 0;
867 putchar('\n');
868 }
869 }
870 printf("\n "
871 "(r) raw mode bits (o) overloaded bits\n");
872}
873
874static unsigned long long parse_number(const char *str)
875{
876 unsigned long long n;
877
878 n = strtoll(str, NULL, 0);
879
880 if (n == 0 && str[0] != '0')
881 fatal("invalid name or number: %s\n", str);
882
883 return n;
884}
885
886static void parse_pid(const char *str)
887{
888 FILE *file;
889 char buf[5000];
890
891 opt_pid = parse_number(str);
892
893 sprintf(buf, "/proc/%d/pagemap", opt_pid);
894 pagemap_fd = checked_open(buf, O_RDONLY);
895
896 sprintf(buf, "/proc/%d/maps", opt_pid);
897 file = fopen(buf, "r");
898 if (!file) {
899 perror(buf);
900 exit(EXIT_FAILURE);
901 }
902
903 while (fgets(buf, sizeof(buf), file) != NULL) {
904 unsigned long vm_start;
905 unsigned long vm_end;
906 unsigned long long pgoff;
907 int major, minor;
908 char r, w, x, s;
909 unsigned long ino;
910 int n;
911
912 n = sscanf(buf, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
913 &vm_start,
914 &vm_end,
915 &r, &w, &x, &s,
916 &pgoff,
917 &major, &minor,
918 &ino);
919 if (n < 10) {
920 fprintf(stderr, "unexpected line: %s\n", buf);
921 continue;
922 }
923 pg_start[nr_vmas] = vm_start / page_size;
924 pg_end[nr_vmas] = vm_end / page_size;
925 if (++nr_vmas >= MAX_VMAS) {
926 fprintf(stderr, "too many VMAs\n");
927 break;
928 }
929 }
930 fclose(file);
931}
932
933static void show_file(const char *name, const struct stat *st)
934{
935 unsigned long long size = st->st_size;
936 char atime[64], mtime[64];
937 long now = time(NULL);
938
939 printf("%s\tInode: %u\tSize: %llu (%llu pages)\n",
940 name, (unsigned)st->st_ino,
941 size, (size + page_size - 1) / page_size);
942
943 strftime(atime, sizeof(atime), "%c", localtime(&st->st_atime));
944 strftime(mtime, sizeof(mtime), "%c", localtime(&st->st_mtime));
945
946 printf("Modify: %s (%ld seconds ago)\nAccess: %s (%ld seconds ago)\n",
947 mtime, now - st->st_mtime,
948 atime, now - st->st_atime);
949}
950
951static sigjmp_buf sigbus_jmp;
952
953static void * volatile sigbus_addr;
954
955static void sigbus_handler(int sig, siginfo_t *info, void *ucontex)
956{
957 (void)sig;
958 (void)ucontex;
959 sigbus_addr = info ? info->si_addr : NULL;
960 siglongjmp(sigbus_jmp, 1);
961}
962
963static struct sigaction sigbus_action = {
964 .sa_sigaction = sigbus_handler,
965 .sa_flags = SA_SIGINFO,
966};
967
968static void walk_file(const char *name, const struct stat *st)
969{
970 uint8_t vec[PAGEMAP_BATCH];
971 uint64_t buf[PAGEMAP_BATCH], flags;
972 uint64_t cgroup = 0;
973 uint64_t mapcnt = 0;
974 unsigned long nr_pages, pfn, i;
975 off_t off, end = st->st_size;
976 int fd;
977 ssize_t len;
978 void *ptr;
979 int first = 1;
980
981 fd = checked_open(name, O_RDONLY|O_NOATIME|O_NOFOLLOW);
982
983 for (off = 0; off < end; off += len) {
984 nr_pages = (end - off + page_size - 1) / page_size;
985 if (nr_pages > PAGEMAP_BATCH)
986 nr_pages = PAGEMAP_BATCH;
987 len = nr_pages * page_size;
988
989 ptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, off);
990 if (ptr == MAP_FAILED)
991 fatal("mmap failed: %s", name);
992
993 /* determine cached pages */
994 if (mincore(ptr, len, vec))
995 fatal("mincore failed: %s", name);
996
997 /* turn off readahead */
998 if (madvise(ptr, len, MADV_RANDOM))
999 fatal("madvice failed: %s", name);
1000
1001 if (sigsetjmp(sigbus_jmp, 1)) {
1002 end = off + sigbus_addr ? sigbus_addr - ptr : 0;
1003 fprintf(stderr, "got sigbus at offset %lld: %s\n",
1004 (long long)end, name);
1005 goto got_sigbus;
1006 }
1007
1008 /* populate ptes */
1009 for (i = 0; i < nr_pages ; i++) {
1010 if (vec[i] & 1)
1011 (void)*(volatile int *)(ptr + i * page_size);
1012 }
1013got_sigbus:
1014
1015 /* turn off harvesting reference bits */
1016 if (madvise(ptr, len, MADV_SEQUENTIAL))
1017 fatal("madvice failed: %s", name);
1018
1019 if (pagemap_read(buf, (unsigned long)ptr / page_size,
1020 nr_pages) != nr_pages)
1021 fatal("cannot read pagemap");
1022
1023 munmap(ptr, len);
1024
1025 for (i = 0; i < nr_pages; i++) {
1026 pfn = pagemap_pfn(buf[i]);
1027 if (!pfn)
1028 continue;
1029 if (!kpageflags_read(&flags, pfn, 1))
1030 continue;
1031 if (!kpagecgroup_read(&cgroup, pfn, 1))
1032 fatal("kpagecgroup_read failed");
1033 if (!kpagecount_read(&mapcnt, pfn, 1))
1034 fatal("kpagecount_read failed");
1035 if (first && opt_list) {
1036 first = 0;
1037 flush_page_range();
1038 show_file(name, st);
1039 }
1040 add_page(off / page_size + i, pfn,
1041 flags, cgroup, mapcnt, buf[i]);
1042 }
1043 }
1044
1045 close(fd);
1046}
1047
1048int walk_tree(const char *name, const struct stat *st, int type, struct FTW *f)
1049{
1050 (void)f;
1051 switch (type) {
1052 case FTW_F:
1053 if (S_ISREG(st->st_mode))
1054 walk_file(name, st);
1055 break;
1056 case FTW_DNR:
1057 fprintf(stderr, "cannot read dir: %s\n", name);
1058 break;
1059 }
1060 return 0;
1061}
1062
1063static void walk_page_cache(void)
1064{
1065 struct stat st;
1066
1067 kpageflags_fd = checked_open(opt_kpageflags, O_RDONLY);
1068 pagemap_fd = checked_open("/proc/self/pagemap", O_RDONLY);
1069 sigaction(SIGBUS, &sigbus_action, NULL);
1070
1071 if (stat(opt_file, &st))
1072 fatal("stat failed: %s\n", opt_file);
1073
1074 if (S_ISREG(st.st_mode)) {
1075 walk_file(opt_file, &st);
1076 } else if (S_ISDIR(st.st_mode)) {
1077 /* do not follow symlinks and mountpoints */
1078 if (nftw(opt_file, walk_tree, 64, FTW_MOUNT | FTW_PHYS) < 0)
1079 fatal("nftw failed: %s\n", opt_file);
1080 } else
1081 fatal("unhandled file type: %s\n", opt_file);
1082
1083 close(kpageflags_fd);
1084 close(pagemap_fd);
1085 signal(SIGBUS, SIG_DFL);
1086}
1087
1088static void parse_file(const char *name)
1089{
1090 opt_file = name;
1091}
1092
1093static void parse_cgroup(const char *path)
1094{
1095 if (path[0] == '@') {
1096 opt_cgroup = parse_number(path + 1);
1097 return;
1098 }
1099
1100 struct stat st;
1101
1102 if (stat(path, &st))
1103 fatal("stat failed: %s: %m\n", path);
1104
1105 if (!S_ISDIR(st.st_mode))
1106 fatal("cgroup supposed to be a directory: %s\n", path);
1107
1108 opt_cgroup = st.st_ino;
1109}
1110
1111static void parse_addr_range(const char *optarg)
1112{
1113 unsigned long offset;
1114 unsigned long size;
1115 char *p;
1116
1117 p = strchr(optarg, ',');
1118 if (!p)
1119 p = strchr(optarg, '+');
1120
1121 if (p == optarg) {
1122 offset = 0;
1123 size = parse_number(p + 1);
1124 } else if (p) {
1125 offset = parse_number(optarg);
1126 if (p[1] == '\0')
1127 size = ULONG_MAX;
1128 else {
1129 size = parse_number(p + 1);
1130 if (*p == ',') {
1131 if (size < offset)
1132 fatal("invalid range: %lu,%lu\n",
1133 offset, size);
1134 size -= offset;
1135 }
1136 }
1137 } else {
1138 offset = parse_number(optarg);
1139 size = 1;
1140 }
1141
1142 add_addr_range(offset, size);
1143}
1144
1145static void add_bits_filter(uint64_t mask, uint64_t bits)
1146{
1147 if (nr_bit_filters >= MAX_BIT_FILTERS)
1148 fatal("too much bit filters\n");
1149
1150 opt_mask[nr_bit_filters] = mask;
1151 opt_bits[nr_bit_filters] = bits;
1152 nr_bit_filters++;
1153}
1154
1155static uint64_t parse_flag_name(const char *str, int len)
1156{
1157 size_t i;
1158
1159 if (!*str || !len)
1160 return 0;
1161
1162 if (len <= 8 && !strncmp(str, "compound", len))
1163 return BITS_COMPOUND;
1164
1165 for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
1166 if (!page_flag_names[i])
1167 continue;
1168 if (!strncmp(str, page_flag_names[i] + 2, len))
1169 return 1ULL << i;
1170 }
1171
1172 return parse_number(str);
1173}
1174
1175static uint64_t parse_flag_names(const char *str, int all)
1176{
1177 const char *p = str;
1178 uint64_t flags = 0;
1179
1180 while (1) {
1181 if (*p == ',' || *p == '=' || *p == '\0') {
1182 if ((*str != '~') || (*str == '~' && all && *++str))
1183 flags |= parse_flag_name(str, p - str);
1184 if (*p != ',')
1185 break;
1186 str = p + 1;
1187 }
1188 p++;
1189 }
1190
1191 return flags;
1192}
1193
1194static void parse_bits_mask(const char *optarg)
1195{
1196 uint64_t mask;
1197 uint64_t bits;
1198 const char *p;
1199
1200 p = strchr(optarg, '=');
1201 if (p == optarg) {
1202 mask = KPF_ALL_BITS;
1203 bits = parse_flag_names(p + 1, 0);
1204 } else if (p) {
1205 mask = parse_flag_names(optarg, 0);
1206 bits = parse_flag_names(p + 1, 0);
1207 } else if (strchr(optarg, '~')) {
1208 mask = parse_flag_names(optarg, 1);
1209 bits = parse_flag_names(optarg, 0);
1210 } else {
1211 mask = parse_flag_names(optarg, 0);
1212 bits = KPF_ALL_BITS;
1213 }
1214
1215 add_bits_filter(mask, bits);
1216}
1217
1218static void parse_kpageflags(const char *name)
1219{
1220 opt_kpageflags = name;
1221}
1222
1223static void describe_flags(const char *optarg)
1224{
1225 uint64_t flags = parse_flag_names(optarg, 0);
1226
1227 printf("0x%016llx\t%s\t%s\n",
1228 (unsigned long long)flags,
1229 page_flag_name(flags),
1230 page_flag_longname(flags));
1231}
1232
1233static const struct option opts[] = {
1234 { "raw" , 0, NULL, 'r' },
1235 { "pid" , 1, NULL, 'p' },
1236 { "file" , 1, NULL, 'f' },
1237 { "addr" , 1, NULL, 'a' },
1238 { "bits" , 1, NULL, 'b' },
1239 { "cgroup" , 1, NULL, 'c' },
1240 { "describe" , 1, NULL, 'd' },
1241 { "mark-idle" , 0, NULL, 'i' },
1242 { "list" , 0, NULL, 'l' },
1243 { "list-each" , 0, NULL, 'L' },
1244 { "list-cgroup", 0, NULL, 'C' },
1245 { "list-mapcnt", 0, NULL, 'M' },
1246 { "no-summary", 0, NULL, 'N' },
1247 { "hwpoison" , 0, NULL, 'X' },
1248 { "unpoison" , 0, NULL, 'x' },
1249 { "kpageflags", 0, NULL, 'F' },
1250 { "help" , 0, NULL, 'h' },
1251 { NULL , 0, NULL, 0 }
1252};
1253
1254int main(int argc, char *argv[])
1255{
1256 int c;
1257
1258 page_size = getpagesize();
1259
1260 while ((c = getopt_long(argc, argv,
1261 "rp:f:a:b:d:c:CilLMNXxF:h",
1262 opts, NULL)) != -1) {
1263 switch (c) {
1264 case 'r':
1265 opt_raw = 1;
1266 break;
1267 case 'p':
1268 parse_pid(optarg);
1269 break;
1270 case 'f':
1271 parse_file(optarg);
1272 break;
1273 case 'a':
1274 parse_addr_range(optarg);
1275 break;
1276 case 'b':
1277 parse_bits_mask(optarg);
1278 break;
1279 case 'c':
1280 parse_cgroup(optarg);
1281 break;
1282 case 'C':
1283 opt_list_cgroup = 1;
1284 break;
1285 case 'd':
1286 describe_flags(optarg);
1287 exit(0);
1288 case 'i':
1289 opt_mark_idle = 1;
1290 break;
1291 case 'l':
1292 opt_list = 1;
1293 break;
1294 case 'L':
1295 opt_list = 2;
1296 break;
1297 case 'M':
1298 opt_list_mapcnt = 1;
1299 break;
1300 case 'N':
1301 opt_no_summary = 1;
1302 break;
1303 case 'X':
1304 opt_hwpoison = 1;
1305 prepare_hwpoison_fd();
1306 break;
1307 case 'x':
1308 opt_unpoison = 1;
1309 prepare_hwpoison_fd();
1310 break;
1311 case 'F':
1312 parse_kpageflags(optarg);
1313 break;
1314 case 'h':
1315 usage();
1316 exit(0);
1317 default:
1318 usage();
1319 exit(1);
1320 }
1321 }
1322
1323 if (!opt_kpageflags)
1324 opt_kpageflags = PROC_KPAGEFLAGS;
1325
1326 if (opt_cgroup || opt_list_cgroup)
1327 kpagecgroup_fd = checked_open(PROC_KPAGECGROUP, O_RDONLY);
1328
1329 if (opt_list && opt_list_mapcnt)
1330 kpagecount_fd = checked_open(PROC_KPAGECOUNT, O_RDONLY);
1331
1332 if (opt_mark_idle && opt_file)
1333 page_idle_fd = checked_open(SYS_KERNEL_MM_PAGE_IDLE, O_RDWR);
1334
1335 if (opt_list && opt_pid)
1336 printf("voffset\t");
1337 if (opt_list && opt_file)
1338 printf("foffset\t");
1339 if (opt_list && opt_list_cgroup)
1340 printf("cgroup\t");
1341 if (opt_list && opt_list_mapcnt)
1342 printf("map-cnt\t");
1343
1344 if (opt_list == 1)
1345 printf("offset\tlen\tflags\n");
1346 if (opt_list == 2)
1347 printf("offset\tflags\n");
1348
1349 if (opt_file)
1350 walk_page_cache();
1351 else
1352 walk_addr_ranges();
1353
1354 if (opt_list == 1)
1355 flush_page_range();
1356
1357 if (opt_no_summary)
1358 return 0;
1359
1360 if (opt_list)
1361 printf("\n\n");
1362
1363 show_summary();
1364
1365 if (opt_list_mapcnt)
1366 close(kpagecount_fd);
1367
1368 if (page_idle_fd >= 0)
1369 close(page_idle_fd);
1370
1371 return 0;
1372}