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