page-types.c 29.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
Wu Fengguang's avatar
Wu Fengguang committed
2 3 4 5
/*
 * page-types: Tool for querying page flags
 *
 * Copyright (C) 2009 Intel corporation
Wu Fengguang's avatar
Wu Fengguang committed
6 7
 *
 * Authors: Wu Fengguang <fengguang.wu@intel.com>
Wu Fengguang's avatar
Wu Fengguang committed
8 9
 */

10 11
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
Wu Fengguang's avatar
Wu Fengguang committed
12 13 14 15 16 17 18 19
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <getopt.h>
#include <limits.h>
20
#include <assert.h>
21 22
#include <ftw.h>
#include <time.h>
23 24
#include <setjmp.h>
#include <signal.h>
Wu Fengguang's avatar
Wu Fengguang committed
25 26 27
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
28 29
#include <sys/mount.h>
#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>
Wu Fengguang's avatar
Wu Fengguang committed
34

35 36 37 38 39 40 41 42 43
#ifndef MAX_PATH
# define MAX_PATH 256
#endif

#ifndef STR
# define _STR(x) #x
# define STR(x) _STR(x)
#endif

44 45 46 47
/*
 * pagemap kernel ABI bits
 */

48 49 50 51
#define PM_ENTRY_BYTES		8
#define PM_PFRAME_BITS		55
#define PM_PFRAME_MASK		((1LL << PM_PFRAME_BITS) - 1)
#define PM_PFRAME(x)		((x) & PM_PFRAME_MASK)
52 53
#define MAX_SWAPFILES_SHIFT	5
#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 57 58
#define PM_FILE			(1ULL << 61)
#define PM_SWAP			(1ULL << 62)
#define PM_PRESENT		(1ULL << 63)
59

Wu Fengguang's avatar
Wu Fengguang committed
60 61 62 63 64 65
/*
 * kernel page flags
 */

#define KPF_BYTES		8
#define PROC_KPAGEFLAGS		"/proc/kpageflags"
66
#define PROC_KPAGECOUNT		"/proc/kpagecount"
67
#define PROC_KPAGECGROUP	"/proc/kpagecgroup"
Wu Fengguang's avatar
Wu Fengguang committed
68

69 70
#define SYS_KERNEL_MM_PAGE_IDLE "/sys/kernel/mm/page_idle/bitmap"

Wu Fengguang's avatar
Wu Fengguang committed
71 72 73 74 75 76 77 78 79
/* [32-] kernel hacking assistances */
#define KPF_RESERVED		32
#define KPF_MLOCKED		33
#define KPF_MAPPEDTODISK	34
#define KPF_PRIVATE		35
#define KPF_PRIVATE_2		36
#define KPF_OWNER_PRIVATE	37
#define KPF_ARCH		38
#define KPF_UNCACHED		39
80
#define KPF_SOFTDIRTY		40
Wu Fengguang's avatar
Wu Fengguang committed
81 82 83 84 85 86 87 88

/* [48-] take some arbitrary free slots for expanding overloaded flags
 * not part of kernel API
 */
#define KPF_READAHEAD		48
#define KPF_SLOB_FREE		49
#define KPF_SLUB_FROZEN		50
#define KPF_SLUB_DEBUG		51
89 90
#define KPF_FILE		61
#define KPF_SWAP		62
91
#define KPF_MMAP_EXCLUSIVE	63
Wu Fengguang's avatar
Wu Fengguang committed
92 93 94 95 96 97 98

#define KPF_ALL_BITS		((uint64_t)~0ULL)
#define KPF_HACKERS_BITS	(0xffffULL << 32)
#define KPF_OVERLOADED_BITS	(0xffffULL << 48)
#define BIT(name)		(1ULL << KPF_##name)
#define BITS_COMPOUND		(BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))

99
static const char * const page_flag_names[] = {
Wu Fengguang's avatar
Wu Fengguang committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	[KPF_LOCKED]		= "L:locked",
	[KPF_ERROR]		= "E:error",
	[KPF_REFERENCED]	= "R:referenced",
	[KPF_UPTODATE]		= "U:uptodate",
	[KPF_DIRTY]		= "D:dirty",
	[KPF_LRU]		= "l:lru",
	[KPF_ACTIVE]		= "A:active",
	[KPF_SLAB]		= "S:slab",
	[KPF_WRITEBACK]		= "W:writeback",
	[KPF_RECLAIM]		= "I:reclaim",
	[KPF_BUDDY]		= "B:buddy",

	[KPF_MMAP]		= "M:mmap",
	[KPF_ANON]		= "a:anonymous",
	[KPF_SWAPCACHE]		= "s:swapcache",
	[KPF_SWAPBACKED]	= "b:swapbacked",
	[KPF_COMPOUND_HEAD]	= "H:compound_head",
	[KPF_COMPOUND_TAIL]	= "T:compound_tail",
	[KPF_HUGE]		= "G:huge",
	[KPF_UNEVICTABLE]	= "u:unevictable",
Wu Fengguang's avatar
Wu Fengguang committed
120
	[KPF_HWPOISON]		= "X:hwpoison",
Wu Fengguang's avatar
Wu Fengguang committed
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",
Wu Fengguang's avatar
Wu Fengguang committed
128 129 130 131 132 133 134 135 136

	[KPF_RESERVED]		= "r:reserved",
	[KPF_MLOCKED]		= "m:mlocked",
	[KPF_MAPPEDTODISK]	= "d:mappedtodisk",
	[KPF_PRIVATE]		= "P:private",
	[KPF_PRIVATE_2]		= "p:private_2",
	[KPF_OWNER_PRIVATE]	= "O:owner_private",
	[KPF_ARCH]		= "h:arch",
	[KPF_UNCACHED]		= "c:uncached",
137
	[KPF_SOFTDIRTY]		= "f:softdirty",
Wu Fengguang's avatar
Wu Fengguang committed
138 139 140 141 142

	[KPF_READAHEAD]		= "I:readahead",
	[KPF_SLOB_FREE]		= "P:slob_free",
	[KPF_SLUB_FROZEN]	= "A:slub_frozen",
	[KPF_SLUB_DEBUG]	= "E:slub_debug",
143 144

	[KPF_FILE]		= "F:file",
145
	[KPF_SWAP]		= "w:swap",
146
	[KPF_MMAP_EXCLUSIVE]	= "1:mmap_exclusive",
Wu Fengguang's avatar
Wu Fengguang committed
147 148 149 150 151 152 153 154 155
};


/*
 * data structures
 */

static int		opt_raw;	/* for kernel developers */
static int		opt_list;	/* list pages (in ranges) */
156
static int		opt_mark_idle;	/* set accessed bit */
Wu Fengguang's avatar
Wu Fengguang committed
157 158
static int		opt_no_summary;	/* don't show summary */
static pid_t		opt_pid;	/* process to walk */
159
const char		*opt_file;	/* file or directory path */
160 161
static uint64_t		opt_cgroup;	/* cgroup inode */
static int		opt_list_cgroup;/* list page cgroup */
162
static int		opt_list_mapcnt;/* list page map count */
163
static const char	*opt_kpageflags;/* kpageflags file to parse */
Wu Fengguang's avatar
Wu Fengguang committed
164 165 166 167 168 169

#define MAX_ADDR_RANGES	1024
static int		nr_addr_ranges;
static unsigned long	opt_offset[MAX_ADDR_RANGES];
static unsigned long	opt_size[MAX_ADDR_RANGES];

170 171 172 173 174
#define MAX_VMAS	10240
static int		nr_vmas;
static unsigned long	pg_start[MAX_VMAS];
static unsigned long	pg_end[MAX_VMAS];

Wu Fengguang's avatar
Wu Fengguang committed
175 176 177 178 179 180 181
#define MAX_BIT_FILTERS	64
static int		nr_bit_filters;
static uint64_t		opt_mask[MAX_BIT_FILTERS];
static uint64_t		opt_bits[MAX_BIT_FILTERS];

static int		page_size;

182
static int		pagemap_fd;
Wu Fengguang's avatar
Wu Fengguang committed
183
static int		kpageflags_fd;
184
static int		kpagecount_fd = -1;
185
static int		kpagecgroup_fd = -1;
186
static int		page_idle_fd = -1;
Wu Fengguang's avatar
Wu Fengguang committed
187

188 189 190
static int		opt_hwpoison;
static int		opt_unpoison;

191
static const char	*hwpoison_debug_fs;
192 193 194
static int		hwpoison_inject_fd;
static int		hwpoison_forget_fd;

Wu Fengguang's avatar
Wu Fengguang committed
195 196 197 198 199 200 201
#define HASH_SHIFT	13
#define HASH_SIZE	(1 << HASH_SHIFT)
#define HASH_MASK	(HASH_SIZE - 1)
#define HASH_KEY(flags)	(flags & HASH_MASK)

static unsigned long	total_pages;
static unsigned long	nr_pages[HASH_SIZE];
202
static uint64_t		page_flags[HASH_SIZE];
Wu Fengguang's avatar
Wu Fengguang committed
203 204 205 206 207 208 209 210 211 212 213 214 215


/*
 * helper functions
 */

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

#define min_t(type, x, y) ({			\
	type __min1 = (x);			\
	type __min2 = (y);			\
	__min1 < __min2 ? __min1 : __min2; })

216 217 218 219 220
#define max_t(type, x, y) ({			\
	type __max1 = (x);			\
	type __max2 = (y);			\
	__max1 > __max2 ? __max1 : __max2; })

221
static unsigned long pages2mb(unsigned long pages)
Wu Fengguang's avatar
Wu Fengguang committed
222 223 224 225
{
	return (pages * page_size) >> 20;
}

226
static void fatal(const char *x, ...)
Wu Fengguang's avatar
Wu Fengguang committed
227 228 229 230 231 232 233 234 235
{
	va_list ap;

	va_start(ap, x);
	vfprintf(stderr, x, ap);
	va_end(ap);
	exit(EXIT_FAILURE);
}

236
static int checked_open(const char *pathname, int flags)
237 238 239 240 241 242 243 244 245 246 247
{
	int fd = open(pathname, flags);

	if (fd < 0) {
		perror(pathname);
		exit(EXIT_FAILURE);
	}

	return fd;
}

248 249 250 251
/*
 * pagemap/kpageflags routines
 */

252
static unsigned long do_u64_read(int fd, const char *name,
253 254 255 256 257 258 259 260 261
				 uint64_t *buf,
				 unsigned long index,
				 unsigned long count)
{
	long bytes;

	if (index > ULONG_MAX / 8)
		fatal("index overflow: %lu\n", index);

262
	bytes = pread(fd, buf, count * 8, (off_t)index * 8);
263 264 265 266 267 268 269 270 271 272 273 274 275 276
	if (bytes < 0) {
		perror(name);
		exit(EXIT_FAILURE);
	}
	if (bytes % 8)
		fatal("partial read: %lu bytes\n", bytes);

	return bytes / 8;
}

static unsigned long kpageflags_read(uint64_t *buf,
				     unsigned long index,
				     unsigned long pages)
{
277
	return do_u64_read(kpageflags_fd, opt_kpageflags, buf, index, pages);
278 279
}

280 281 282 283 284 285 286
static unsigned long kpagecgroup_read(uint64_t *buf,
				      unsigned long index,
				      unsigned long pages)
{
	if (kpagecgroup_fd < 0)
		return pages;

287
	return do_u64_read(kpagecgroup_fd, opt_kpageflags, buf, index, pages);
288 289
}

290 291 292 293 294 295 296 297 298
static unsigned long kpagecount_read(uint64_t *buf,
				     unsigned long index,
				     unsigned long pages)
{
	return kpagecount_fd < 0 ? pages :
		do_u64_read(kpagecount_fd, PROC_KPAGECOUNT,
			    buf, index, pages);
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
static unsigned long pagemap_read(uint64_t *buf,
				  unsigned long index,
				  unsigned long pages)
{
	return do_u64_read(pagemap_fd, "/proc/pid/pagemap", buf, index, pages);
}

static unsigned long pagemap_pfn(uint64_t val)
{
	unsigned long pfn;

	if (val & PM_PRESENT)
		pfn = PM_PFRAME(val);
	else
		pfn = 0;

	return pfn;
}

318 319 320 321
static unsigned long pagemap_swap_offset(uint64_t val)
{
	return val & PM_SWAP ? PM_SWAP_OFFSET(val) : 0;
}
Wu Fengguang's avatar
Wu Fengguang committed
322 323 324 325 326

/*
 * page flag names
 */

327
static char *page_flag_name(uint64_t flags)
Wu Fengguang's avatar
Wu Fengguang committed
328 329 330
{
	static char buf[65];
	int present;
331
	size_t i, j;
Wu Fengguang's avatar
Wu Fengguang committed
332 333 334 335 336

	for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
		present = (flags >> i) & 1;
		if (!page_flag_names[i]) {
			if (present)
337
				fatal("unknown flag bit %d\n", i);
Wu Fengguang's avatar
Wu Fengguang committed
338 339 340 341 342 343 344 345
			continue;
		}
		buf[j++] = present ? page_flag_names[i][0] : '_';
	}

	return buf;
}

346
static char *page_flag_longname(uint64_t flags)
Wu Fengguang's avatar
Wu Fengguang committed
347 348
{
	static char buf[1024];
349
	size_t i, n;
Wu Fengguang's avatar
Wu Fengguang committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

	for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
		if (!page_flag_names[i])
			continue;
		if ((flags >> i) & 1)
			n += snprintf(buf + n, sizeof(buf) - n, "%s,",
					page_flag_names[i] + 2);
	}
	if (n)
		n--;
	buf[n] = '\0';

	return buf;
}


/*
 * page list and summary
 */

370
static void show_page_range(unsigned long voffset, unsigned long offset,
371 372
			    unsigned long size, uint64_t flags,
			    uint64_t cgroup, uint64_t mapcnt)
Wu Fengguang's avatar
Wu Fengguang committed
373 374
{
	static uint64_t      flags0;
375
	static uint64_t	     cgroup0;
376
	static uint64_t      mapcnt0;
377
	static unsigned long voff;
Wu Fengguang's avatar
Wu Fengguang committed
378 379 380
	static unsigned long index;
	static unsigned long count;

381 382
	if (flags == flags0 && cgroup == cgroup0 && mapcnt == mapcnt0 &&
	    offset == index + count && size && voffset == voff + count) {
383
		count += size;
Wu Fengguang's avatar
Wu Fengguang committed
384 385 386
		return;
	}

387 388 389
	if (count) {
		if (opt_pid)
			printf("%lx\t", voff);
390 391
		if (opt_file)
			printf("%lu\t", voff);
392 393
		if (opt_list_cgroup)
			printf("@%llu\t", (unsigned long long)cgroup0);
394 395
		if (opt_list_mapcnt)
			printf("%lu\t", mapcnt0);
396
		printf("%lx\t%lx\t%s\n",
Wu Fengguang's avatar
Wu Fengguang committed
397
				index, count, page_flag_name(flags0));
398
	}
Wu Fengguang's avatar
Wu Fengguang committed
399 400

	flags0 = flags;
401 402
	cgroup0 = cgroup;
	mapcnt0 = mapcnt;
Wu Fengguang's avatar
Wu Fengguang committed
403
	index  = offset;
404
	voff   = voffset;
405 406 407 408 409
	count  = size;
}

static void flush_page_range(void)
{
410
	show_page_range(0, 0, 0, 0, 0, 0);
Wu Fengguang's avatar
Wu Fengguang committed
411 412
}

413
static void show_page(unsigned long voffset, unsigned long offset,
414
		      uint64_t flags, uint64_t cgroup, uint64_t mapcnt)
Wu Fengguang's avatar
Wu Fengguang committed
415
{
416 417
	if (opt_pid)
		printf("%lx\t", voffset);
418 419
	if (opt_file)
		printf("%lu\t", voffset);
420 421
	if (opt_list_cgroup)
		printf("@%llu\t", (unsigned long long)cgroup);
422 423 424
	if (opt_list_mapcnt)
		printf("%lu\t", mapcnt);

425
	printf("%lx\t%s\n", offset, page_flag_name(flags));
Wu Fengguang's avatar
Wu Fengguang committed
426 427
}

428
static void show_summary(void)
Wu Fengguang's avatar
Wu Fengguang committed
429
{
430
	size_t i;
Wu Fengguang's avatar
Wu Fengguang committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

	printf("             flags\tpage-count       MB"
		"  symbolic-flags\t\t\tlong-symbolic-flags\n");

	for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
		if (nr_pages[i])
			printf("0x%016llx\t%10lu %8lu  %s\t%s\n",
				(unsigned long long)page_flags[i],
				nr_pages[i],
				pages2mb(nr_pages[i]),
				page_flag_name(page_flags[i]),
				page_flag_longname(page_flags[i]));
	}

	printf("             total\t%10lu %8lu\n",
			total_pages, pages2mb(total_pages));
}


/*
 * page flag filters
 */

454
static int bit_mask_ok(uint64_t flags)
Wu Fengguang's avatar
Wu Fengguang committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
{
	int i;

	for (i = 0; i < nr_bit_filters; i++) {
		if (opt_bits[i] == KPF_ALL_BITS) {
			if ((flags & opt_mask[i]) == 0)
				return 0;
		} else {
			if ((flags & opt_mask[i]) != opt_bits[i])
				return 0;
		}
	}

	return 1;
}

471
static uint64_t expand_overloaded_flags(uint64_t flags, uint64_t pme)
Wu Fengguang's avatar
Wu Fengguang committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
{
	/* SLOB/SLUB overload several page flags */
	if (flags & BIT(SLAB)) {
		if (flags & BIT(PRIVATE))
			flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
		if (flags & BIT(ACTIVE))
			flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
		if (flags & BIT(ERROR))
			flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
	}

	/* PG_reclaim is overloaded as PG_readahead in the read path */
	if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
		flags ^= BIT(RECLAIM) | BIT(READAHEAD);

487 488
	if (pme & PM_SOFT_DIRTY)
		flags |= BIT(SOFTDIRTY);
489 490
	if (pme & PM_FILE)
		flags |= BIT(FILE);
491 492
	if (pme & PM_SWAP)
		flags |= BIT(SWAP);
493 494
	if (pme & PM_MMAP_EXCLUSIVE)
		flags |= BIT(MMAP_EXCLUSIVE);
495

Wu Fengguang's avatar
Wu Fengguang committed
496 497 498
	return flags;
}

499
static uint64_t well_known_flags(uint64_t flags)
Wu Fengguang's avatar
Wu Fengguang committed
500 501 502 503 504 505 506 507 508 509 510
{
	/* hide flags intended only for kernel hacker */
	flags &= ~KPF_HACKERS_BITS;

	/* hide non-hugeTLB compound pages */
	if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
		flags &= ~BITS_COMPOUND;

	return flags;
}

511
static uint64_t kpageflags_flags(uint64_t flags, uint64_t pme)
512
{
513 514 515
	if (opt_raw)
		flags = expand_overloaded_flags(flags, pme);
	else
516 517 518 519 520
		flags = well_known_flags(flags);

	return flags;
}

521 522 523 524 525 526
/*
 * page actions
 */

static void prepare_hwpoison_fd(void)
{
527 528
	char buf[MAX_PATH + 1];

529
	hwpoison_debug_fs = debugfs__mount();
530 531 532 533
	if (!hwpoison_debug_fs) {
		perror("mount debugfs");
		exit(EXIT_FAILURE);
	}
534 535

	if (opt_hwpoison && !hwpoison_inject_fd) {
536 537
		snprintf(buf, MAX_PATH, "%s/hwpoison/corrupt-pfn",
			hwpoison_debug_fs);
538 539 540 541
		hwpoison_inject_fd = checked_open(buf, O_WRONLY);
	}

	if (opt_unpoison && !hwpoison_forget_fd) {
542 543
		snprintf(buf, MAX_PATH, "%s/hwpoison/unpoison-pfn",
			hwpoison_debug_fs);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
		hwpoison_forget_fd = checked_open(buf, O_WRONLY);
	}
}

static int hwpoison_page(unsigned long offset)
{
	char buf[100];
	int len;

	len = sprintf(buf, "0x%lx\n", offset);
	len = write(hwpoison_inject_fd, buf, len);
	if (len < 0) {
		perror("hwpoison inject");
		return len;
	}
	return 0;
}

static int unpoison_page(unsigned long offset)
{
	char buf[100];
	int len;

	len = sprintf(buf, "0x%lx\n", offset);
	len = write(hwpoison_forget_fd, buf, len);
	if (len < 0) {
		perror("hwpoison forget");
		return len;
	}
	return 0;
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
static int mark_page_idle(unsigned long offset)
{
	static unsigned long off;
	static uint64_t buf;
	int len;

	if ((offset / 64 == off / 64) || buf == 0) {
		buf |= 1UL << (offset % 64);
		off = offset;
		return 0;
	}

	len = pwrite(page_idle_fd, &buf, 8, 8 * (off / 64));
	if (len < 0) {
		perror("mark page idle");
		return len;
	}

	buf = 1UL << (offset % 64);
	off = offset;

	return 0;
}

Wu Fengguang's avatar
Wu Fengguang committed
600 601 602 603
/*
 * page frame walker
 */

604
static size_t hash_slot(uint64_t flags)
Wu Fengguang's avatar
Wu Fengguang committed
605
{
606 607
	size_t k = HASH_KEY(flags);
	size_t i;
Wu Fengguang's avatar
Wu Fengguang committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

	/* Explicitly reserve slot 0 for flags 0: the following logic
	 * cannot distinguish an unoccupied slot from slot (flags==0).
	 */
	if (flags == 0)
		return 0;

	/* search through the remaining (HASH_SIZE-1) slots */
	for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) {
		if (!k || k >= ARRAY_SIZE(page_flags))
			k = 1;
		if (page_flags[k] == 0) {
			page_flags[k] = flags;
			return k;
		}
		if (page_flags[k] == flags)
			return k;
	}

	fatal("hash table full: bump up HASH_SHIFT?\n");
	exit(EXIT_FAILURE);
}

631
static void add_page(unsigned long voffset, unsigned long offset,
632 633
		     uint64_t flags, uint64_t cgroup, uint64_t mapcnt,
		     uint64_t pme)
Wu Fengguang's avatar
Wu Fengguang committed
634
{
635
	flags = kpageflags_flags(flags, pme);
Wu Fengguang's avatar
Wu Fengguang committed
636 637 638 639

	if (!bit_mask_ok(flags))
		return;

640 641 642
	if (opt_cgroup && cgroup != (uint64_t)opt_cgroup)
		return;

643 644 645 646 647
	if (opt_hwpoison)
		hwpoison_page(offset);
	if (opt_unpoison)
		unpoison_page(offset);

648 649 650
	if (opt_mark_idle)
		mark_page_idle(offset);

Wu Fengguang's avatar
Wu Fengguang committed
651
	if (opt_list == 1)
652
		show_page_range(voffset, offset, 1, flags, cgroup, mapcnt);
Wu Fengguang's avatar
Wu Fengguang committed
653
	else if (opt_list == 2)
654
		show_page(voffset, offset, flags, cgroup, mapcnt);
Wu Fengguang's avatar
Wu Fengguang committed
655 656 657 658 659

	nr_pages[hash_slot(flags)]++;
	total_pages++;
}

660
#define KPAGEFLAGS_BATCH	(64 << 10)	/* 64k pages */
661 662
static void walk_pfn(unsigned long voffset,
		     unsigned long index,
663 664
		     unsigned long count,
		     uint64_t pme)
Wu Fengguang's avatar
Wu Fengguang committed
665
{
666
	uint64_t buf[KPAGEFLAGS_BATCH];
667
	uint64_t cgi[KPAGEFLAGS_BATCH];
668
	uint64_t cnt[KPAGEFLAGS_BATCH];
Wu Fengguang's avatar
Wu Fengguang committed
669
	unsigned long batch;
670
	unsigned long pages;
Wu Fengguang's avatar
Wu Fengguang committed
671 672
	unsigned long i;

673 674 675 676 677 678 679 680 681
	/*
	 * kpagecgroup_read() reads only if kpagecgroup were opened, but
	 * /proc/kpagecgroup might even not exist, so it's better to fill
	 * them with zeros here.
	 */
	if (count == 1)
		cgi[0] = 0;
	else
		memset(cgi, 0, sizeof cgi);
682

Wu Fengguang's avatar
Wu Fengguang committed
683
	while (count) {
684 685 686
		batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
		pages = kpageflags_read(buf, index, batch);
		if (pages == 0)
Wu Fengguang's avatar
Wu Fengguang committed
687 688
			break;

689 690 691
		if (kpagecgroup_read(cgi, index, pages) != pages)
			fatal("kpagecgroup returned fewer pages than expected");

692
		if (kpagecount_read(cnt, index, pages) != pages)
693 694
			fatal("kpagecount returned fewer pages than expected");

695
		for (i = 0; i < pages; i++)
696 697
			add_page(voffset + i, index + i,
				 buf[i], cgi[i], cnt[i], pme);
Wu Fengguang's avatar
Wu Fengguang committed
698

699 700
		index += pages;
		count -= pages;
Wu Fengguang's avatar
Wu Fengguang committed
701 702 703
	}
}

704 705 706 707 708 709 710
static void walk_swap(unsigned long voffset, uint64_t pme)
{
	uint64_t flags = kpageflags_flags(0, pme);

	if (!bit_mask_ok(flags))
		return;

711 712 713
	if (opt_cgroup)
		return;

714
	if (opt_list == 1)
715 716
		show_page_range(voffset, pagemap_swap_offset(pme),
				1, flags, 0, 0);
717
	else if (opt_list == 2)
718
		show_page(voffset, pagemap_swap_offset(pme), flags, 0, 0);
719 720 721 722 723

	nr_pages[hash_slot(flags)]++;
	total_pages++;
}

724 725
#define PAGEMAP_BATCH	(64 << 10)
static void walk_vma(unsigned long index, unsigned long count)
726
{
727 728 729 730 731
	uint64_t buf[PAGEMAP_BATCH];
	unsigned long batch;
	unsigned long pages;
	unsigned long pfn;
	unsigned long i;
732

733 734 735 736 737
	while (count) {
		batch = min_t(unsigned long, count, PAGEMAP_BATCH);
		pages = pagemap_read(buf, index, batch);
		if (pages == 0)
			break;
738

739 740 741
		for (i = 0; i < pages; i++) {
			pfn = pagemap_pfn(buf[i]);
			if (pfn)
742
				walk_pfn(index + i, pfn, 1, buf[i]);
743 744
			if (buf[i] & PM_SWAP)
				walk_swap(index + i, buf[i]);
745 746 747 748 749
		}

		index += pages;
		count -= pages;
	}
750 751 752 753 754
}

static void walk_task(unsigned long index, unsigned long count)
{
	const unsigned long end = index + count;
755 756
	unsigned long start;
	int i = 0;
757 758 759 760 761 762 763 764 765

	while (index < end) {

		while (pg_end[i] <= index)
			if (++i >= nr_vmas)
				return;
		if (pg_start[i] >= end)
			return;

766 767
		start = max_t(unsigned long, pg_start[i], index);
		index = min_t(unsigned long, pg_end[i], end);
768

769 770
		assert(start < index);
		walk_vma(start, index - start);
771 772 773 774 775 776 777 778 779 780 781 782 783
	}
}

static void add_addr_range(unsigned long offset, unsigned long size)
{
	if (nr_addr_ranges >= MAX_ADDR_RANGES)
		fatal("too many addr ranges\n");

	opt_offset[nr_addr_ranges] = offset;
	opt_size[nr_addr_ranges] = min_t(unsigned long, size, ULONG_MAX-offset);
	nr_addr_ranges++;
}

784
static void walk_addr_ranges(void)
Wu Fengguang's avatar
Wu Fengguang committed
785 786 787
{
	int i;

788
	kpageflags_fd = checked_open(opt_kpageflags, O_RDONLY);
Wu Fengguang's avatar
Wu Fengguang committed
789 790

	if (!nr_addr_ranges)
791
		add_addr_range(0, ULONG_MAX);
Wu Fengguang's avatar
Wu Fengguang committed
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 797
		else
			walk_task(opt_offset[i], opt_size[i]);
Wu Fengguang's avatar
Wu Fengguang committed
798

799 800 801
	if (opt_mark_idle)
		mark_page_idle(0);

Wu Fengguang's avatar
Wu Fengguang committed
802 803 804 805 806 807 808 809
	close(kpageflags_fd);
}


/*
 * user interface
 */

810
static const char *page_flag_type(uint64_t flag)
Wu Fengguang's avatar
Wu Fengguang committed
811 812 813 814 815 816 817 818
{
	if (flag & KPF_HACKERS_BITS)
		return "(r)";
	if (flag & KPF_OVERLOADED_BITS)
		return "(o)";
	return "   ";
}

819
static void usage(void)
Wu Fengguang's avatar
Wu Fengguang committed
820
{
821
	size_t i, j;
Wu Fengguang's avatar
Wu Fengguang committed
822 823 824

	printf(
"page-types [options]\n"
Alex Chiang's avatar
Alex Chiang committed
825
"            -r|--raw                   Raw mode, for kernel developers\n"
826
"            -d|--describe flags        Describe flags\n"
Alex Chiang's avatar
Alex Chiang committed
827 828
"            -a|--addr    addr-spec     Walk a range of pages\n"
"            -b|--bits    bits-spec     Walk pages with specified bits\n"
829
"            -c|--cgroup  path|@inode   Walk pages within memory cgroup\n"
Alex Chiang's avatar
Alex Chiang committed
830 831
"            -p|--pid     pid           Walk process address space\n"
"            -f|--file    filename      Walk file address space\n"
832
"            -i|--mark-idle             Mark pages idle\n"
Alex Chiang's avatar
Alex Chiang committed
833 834
"            -l|--list                  Show page details in ranges\n"
"            -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"
Alex Chiang's avatar
Alex Chiang committed
838 839
"            -X|--hwpoison              hwpoison pages\n"
"            -x|--unpoison              unpoison pages\n"
840
"            -F|--kpageflags filename   kpageflags file to parse\n"
Alex Chiang's avatar
Alex Chiang committed
841
"            -h|--help                  Show this usage message\n"
842
"flags:\n"
Alex Chiang's avatar
Alex Chiang committed
843 844 845
"            0x10                       bitfield format, e.g.\n"
"            anon                       bit-name, e.g.\n"
"            0x10,anon                  comma-separated list, e.g.\n"
Wu Fengguang's avatar
Wu Fengguang committed
846
"addr-spec:\n"
Alex Chiang's avatar
Alex Chiang committed
847 848 849 850 851
"            N                          one page at offset N (unit: pages)\n"
"            N+M                        pages range from N to N+M-1\n"
"            N,M                        pages range from N to M-1\n"
"            N,                         pages range from N to end\n"
"            ,M                         pages range from 0 to M-1\n"
Wu Fengguang's avatar
Wu Fengguang committed
852
"bits-spec:\n"
Alex Chiang's avatar
Alex Chiang committed
853 854 855 856
"            bit1,bit2                  (flags & (bit1|bit2)) != 0\n"
"            bit1,bit2=bit1             (flags & (bit1|bit2)) == bit1\n"
"            bit1,~bit2                 (flags & (bit1|bit2)) == bit1\n"
"            =bit1,bit2                 flags == (bit1|bit2)\n"
Wu Fengguang's avatar
Wu Fengguang committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
"bit-names:\n"
	);

	for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
		if (!page_flag_names[i])
			continue;
		printf("%16s%s", page_flag_names[i] + 2,
				 page_flag_type(1ULL << i));
		if (++j > 3) {
			j = 0;
			putchar('\n');
		}
	}
	printf("\n                                   "
		"(r) raw mode bits  (o) overloaded bits\n");
}

874
static unsigned long long parse_number(const char *str)
Wu Fengguang's avatar
Wu Fengguang committed
875 876 877 878 879 880 881 882 883 884 885
{
	unsigned long long n;

	n = strtoll(str, NULL, 0);

	if (n == 0 && str[0] != '0')
		fatal("invalid name or number: %s\n", str);

	return n;
}

886
static void parse_pid(const char *str)
Wu Fengguang's avatar
Wu Fengguang committed
887
{
888 889 890
	FILE *file;
	char buf[5000];

Wu Fengguang's avatar
Wu Fengguang committed
891 892
	opt_pid = parse_number(str);

893
	sprintf(buf, "/proc/%d/pagemap", opt_pid);
894
	pagemap_fd = checked_open(buf, O_RDONLY);
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

	sprintf(buf, "/proc/%d/maps", opt_pid);
	file = fopen(buf, "r");
	if (!file) {
		perror(buf);
		exit(EXIT_FAILURE);
	}

	while (fgets(buf, sizeof(buf), file) != NULL) {
		unsigned long vm_start;
		unsigned long vm_end;
		unsigned long long pgoff;
		int major, minor;
		char r, w, x, s;
		unsigned long ino;
		int n;

		n = sscanf(buf, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
			   &vm_start,
			   &vm_end,
			   &r, &w, &x, &s,
			   &pgoff,
			   &major, &minor,
			   &ino);
		if (n < 10) {
			fprintf(stderr, "unexpected line: %s\n", buf);
			continue;
		}
		pg_start[nr_vmas] = vm_start / page_size;
		pg_end[nr_vmas] = vm_end / page_size;
		if (++nr_vmas >= MAX_VMAS) {
			fprintf(stderr, "too many VMAs\n");
			break;
		}
	}
	fclose(file);
Wu Fengguang's avatar
Wu Fengguang committed
931 932
}

933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
static void show_file(const char *name, const struct stat *st)
{
	unsigned long long size = st->st_size;
	char atime[64], mtime[64];
	long now = time(NULL);

	printf("%s\tInode: %u\tSize: %llu (%llu pages)\n",
			name, (unsigned)st->st_ino,
			size, (size + page_size - 1) / page_size);

	strftime(atime, sizeof(atime), "%c", localtime(&st->st_atime));
	strftime(mtime, sizeof(mtime), "%c", localtime(&st->st_mtime));

	printf("Modify: %s (%ld seconds ago)\nAccess: %s (%ld seconds ago)\n",
			mtime, now - st->st_mtime,
			atime, now - st->st_atime);
}

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
static sigjmp_buf sigbus_jmp;

static void * volatile sigbus_addr;

static void sigbus_handler(int sig, siginfo_t *info, void *ucontex)
{
	(void)sig;
	(void)ucontex;
	sigbus_addr = info ? info->si_addr : NULL;
	siglongjmp(sigbus_jmp, 1);
}

static struct sigaction sigbus_action = {
	.sa_sigaction = sigbus_handler,
	.sa_flags = SA_SIGINFO,
};

968 969 970 971
static void walk_file(const char *name, const struct stat *st)
{
	uint8_t vec[PAGEMAP_BATCH];
	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 977 978 979 980 981 982
	int fd;
	ssize_t len;
	void *ptr;
	int first = 1;

	fd = checked_open(name, O_RDONLY|O_NOATIME|O_NOFOLLOW);

983 984
	for (off = 0; off < end; off += len) {
		nr_pages = (end - off + page_size - 1) / page_size;
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
		if (nr_pages > PAGEMAP_BATCH)
			nr_pages = PAGEMAP_BATCH;
		len = nr_pages * page_size;

		ptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, off);
		if (ptr == MAP_FAILED)
			fatal("mmap failed: %s", name);

		/* determine cached pages */
		if (mincore(ptr, len, vec))
			fatal("mincore failed: %s", name);

		/* turn off readahead */
		if (madvise(ptr, len, MADV_RANDOM))
			fatal("madvice failed: %s", name);

1001 1002 1003 1004 1005 1006 1007
		if (sigsetjmp(sigbus_jmp, 1)) {
			end = off + sigbus_addr ? sigbus_addr - ptr : 0;
			fprintf(stderr, "got sigbus at offset %lld: %s\n",
					(long long)end, name);
			goto got_sigbus;
		}

1008 1009 1010 1011 1012
		/* populate ptes */
		for (i = 0; i < nr_pages ; i++) {
			if (vec[i] & 1)
				(void)*(volatile int *)(ptr + i * page_size);
		}
1013
got_sigbus:
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030

		/* turn off harvesting reference bits */
		if (madvise(ptr, len, MADV_SEQUENTIAL))
			fatal("madvice failed: %s", name);

		if (pagemap_read(buf, (unsigned long)ptr / page_size,
					nr_pages) != nr_pages)
			fatal("cannot read pagemap");

		munmap(ptr, len);

		for (i = 0; i < nr_pages; i++) {
			pfn = pagemap_pfn(buf[i]);
			if (!pfn)
				continue;
			if (!kpageflags_read(&flags, pfn, 1))
				continue;
1031 1032
			if (!kpagecgroup_read(&cgroup, pfn, 1))
				fatal("kpagecgroup_read failed");
1033 1034
			if (!kpagecount_read(&mapcnt, pfn, 1))
				fatal("kpagecount_read failed");
1035 1036 1037 1038 1039
			if (first && opt_list) {
				first = 0;
				flush_page_range();
				show_file(name, st);
			}
1040
			add_page(off / page_size + i, pfn,
1041
				 flags, cgroup, mapcnt, buf[i]);
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
		}
	}

	close(fd);
}

int walk_tree(const char *name, const struct stat *st, int type, struct FTW *f)
{
	(void)f;
	switch (type) {
	case FTW_F:
		if (S_ISREG(st->st_mode))
			walk_file(name, st);
		break;
	case FTW_DNR:
		fprintf(stderr, "cannot read dir: %s\n", name);
		break;
	}
	return 0;
}

static void walk_page_cache(void)
{
	struct stat st;

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 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084

	if (stat(opt_file, &st))
		fatal("stat failed: %s\n", opt_file);

	if (S_ISREG(st.st_mode)) {
		walk_file(opt_file, &st);
	} else if (S_ISDIR(st.st_mode)) {
		/* do not follow symlinks and mountpoints */
		if (nftw(opt_file, walk_tree, 64, FTW_MOUNT | FTW_PHYS) < 0)
			fatal("nftw failed: %s\n", opt_file);
	} else
		fatal("unhandled file type: %s\n", opt_file);

	close(kpageflags_fd);
	close(pagemap_fd);
1085
	signal(SIGBUS, SIG_DFL);
1086 1087
}

1088
static void parse_file(const char *name)
Wu Fengguang's avatar
Wu Fengguang committed
1089
{
1090
	opt_file = name;
Wu Fengguang's avatar
Wu Fengguang committed
1091 1092
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
static void parse_cgroup(const char *path)
{
	if (path[0] == '@') {
		opt_cgroup = parse_number(path + 1);
		return;
	}

	struct stat st;

	if (stat(path, &st))
		fatal("stat failed: %s: %m\n", path);

	if (!S_ISDIR(st.st_mode))
		fatal("cgroup supposed to be a directory: %s\n", path);

	opt_cgroup = st.st_ino;
}

1111
static void parse_addr_range(const char *optarg)
Wu Fengguang's avatar
Wu Fengguang committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
{
	unsigned long offset;
	unsigned long size;
	char *p;

	p = strchr(optarg, ',');
	if (!p)
		p = strchr(optarg, '+');

	if (p == optarg) {
		offset = 0;
		size   = parse_number(p + 1);
	} else if (p) {
		offset = parse_number(optarg);
		if (p[1] == '\0')
			size = ULONG_MAX;
		else {
			size = parse_number(p + 1);
			if (*p == ',') {
				if (size < offset)
					fatal("invalid range: %lu,%lu\n",
							offset, size);
				size -= offset;
			}
		}
	} else {
		offset = parse_number(optarg);
		size   = 1;
	}

	add_addr_range(offset, size);
}

1145
static void add_bits_filter(uint64_t mask, uint64_t bits)
Wu Fengguang's avatar
Wu Fengguang committed
1146 1147 1148 1149 1150 1151 1152 1153 1154
{
	if (nr_bit_filters >= MAX_BIT_FILTERS)
		fatal("too much bit filters\n");

	opt_mask[nr_bit_filters] = mask;
	opt_bits[nr_bit_filters] = bits;
	nr_bit_filters++;
}

1155
static uint64_t parse_flag_name(const char *str, int len)
Wu Fengguang's avatar
Wu Fengguang committed
1156
{
1157
	size_t i;
Wu Fengguang's avatar
Wu Fengguang committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

	if (!*str || !len)
		return 0;

	if (len <= 8 && !strncmp(str, "compound", len))
		return BITS_COMPOUND;

	for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
		if (!page_flag_names[i])
			continue;
		if (!strncmp(str, page_flag_names[i] + 2, len))
			return 1ULL << i;
	}

	return parse_number(str);
}

1175
static uint64_t parse_flag_names(const char *str, int all)
Wu Fengguang's avatar
Wu Fengguang committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
{
	const char *p    = str;
	uint64_t   flags = 0;

	while (1) {
		if (*p == ',' || *p == '=' || *p == '\0') {
			if ((*str != '~') || (*str == '~' && all && *++str))
				flags |= parse_flag_name(str, p - str);
			if (*p != ',')
				break;
			str = p + 1;
		}
		p++;
	}

	return flags;
}

1194
static void parse_bits_mask(const char *optarg)
Wu Fengguang's avatar
Wu Fengguang committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
{
	uint64_t mask;
	uint64_t bits;
	const char *p;

	p = strchr(optarg, '=');
	if (p == optarg) {
		mask = KPF_ALL_BITS;
		bits = parse_flag_names(p + 1, 0);
	} else if (p) {
		mask = parse_flag_names(optarg, 0);
		bits = parse_flag_names(p + 1, 0);
	} else if (strchr(optarg, '~')) {
		mask = parse_flag_names(optarg, 1);
		bits = parse_flag_names(optarg, 0);
	} else {
		mask = parse_flag_names(optarg, 0);
		bits = KPF_ALL_BITS;
	}

	add_bits_filter(mask, bits);
}

1218 1219 1220 1221 1222
static void parse_kpageflags(const char *name)
{
	opt_kpageflags = name;
}

1223 1224 1225 1226 1227 1228 1229 1230 1231
static void describe_flags(const char *optarg)
{
	uint64_t flags = parse_flag_names(optarg, 0);

	printf("0x%016llx\t%s\t%s\n",
		(unsigned long long)flags,
		page_flag_name(flags),
		page_flag_longname(flags));
}
Wu Fengguang's avatar
Wu Fengguang committed
1232

1233
static const struct option opts[] = {
Wu Fengguang's avatar
Wu Fengguang committed
1234 1235 1236 1237 1238
	{ "raw"       , 0, NULL, 'r' },
	{ "pid"       , 1, NULL, 'p' },
	{ "file"      , 1, NULL, 'f' },
	{ "addr"      , 1, NULL, 'a' },
	{ "bits"      , 1, NULL, 'b' },
1239
	{ "cgroup"    , 1, NULL, 'c' },
1240
	{ "describe"  , 1, NULL, 'd' },
1241
	{ "mark-idle" , 0, NULL, 'i' },
Wu Fengguang's avatar
Wu Fengguang committed
1242 1243
	{ "list"      , 0, NULL, 'l' },
	{ "list-each" , 0, NULL, 'L' },
1244
	{ "list-cgroup", 0, NULL, 'C' },
1245
	{ "list-mapcnt", 0, NULL, 'M' },
Wu Fengguang's avatar
Wu Fengguang committed
1246
	{ "no-summary", 0, NULL, 'N' },
1247 1248
	{ "hwpoison"  , 0, NULL, 'X' },
	{ "unpoison"  , 0, NULL, 'x' },
1249
	{ "kpageflags", 0, NULL, 'F' },
Wu Fengguang's avatar
Wu Fengguang committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
	{ "help"      , 0, NULL, 'h' },
	{ NULL        , 0, NULL, 0 }
};

int main(int argc, char *argv[])
{
	int c;

	page_size = getpagesize();

	while ((c = getopt_long(argc, argv,
1261
				"rp:f:a:b:d:c:CilLMNXxF:h",
1262
				opts, NULL)) != -1) {
Wu Fengguang's avatar
Wu Fengguang committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
		switch (c) {
		case 'r':
			opt_raw = 1;
			break;
		case 'p':
			parse_pid(optarg);
			break;
		case 'f':
			parse_file(optarg);
			break;
		case 'a':
			parse_addr_range(optarg);
			break;
		case 'b':
			parse_bits_mask(optarg);
			break;
1279 1280 1281 1282 1283 1284
		case 'c':
			parse_cgroup(optarg);
			break;
		case 'C':
			opt_list_cgroup = 1;
			break;
1285 1286
		case 'd':
			describe_flags(optarg);
1287
			exit(0);
1288 1289 1290
		case 'i':
			opt_mark_idle = 1;
			break;
Wu Fengguang's avatar
Wu Fengguang committed
1291 1292 1293 1294 1295 1296
		case 'l':
			opt_list = 1;
			break;
		case 'L':
			opt_list = 2;
			break;
1297 1298 1299
		case 'M':
			opt_list_mapcnt = 1;
			break;
Wu Fengguang's avatar
Wu Fengguang committed
1300 1301 1302
		case 'N':
			opt_no_summary = 1;
			break;
1303 1304 1305 1306 1307 1308 1309 1310
		case 'X':
			opt_hwpoison = 1;
			prepare_hwpoison_fd();
			break;
		case 'x':
			opt_unpoison = 1;
			prepare_hwpoison_fd();
			break;
1311 1312 1313
		case 'F':
			parse_kpageflags(optarg);
			break;
Wu Fengguang's avatar
Wu Fengguang committed
1314 1315 1316 1317 1318 1319 1320 1321 1322
		case 'h':
			usage();
			exit(0);
		default:
			usage();
			exit(1);
		}
	}

1323 1324 1325
	if (!opt_kpageflags)
		opt_kpageflags = PROC_KPAGEFLAGS;

1326 1327 1328
	if (opt_cgroup || opt_list_cgroup)
		kpagecgroup_fd = checked_open(PROC_KPAGECGROUP, O_RDONLY);

1329 1330 1331
	if (opt_list && opt_list_mapcnt)
		kpagecount_fd = checked_open(PROC_KPAGECOUNT, O_RDONLY);

1332 1333 1334
	if (opt_mark_idle && opt_file)
		page_idle_fd = checked_open(SYS_KERNEL_MM_PAGE_IDLE, O_RDWR);

1335 1336
	if (opt_list && opt_pid)
		printf("voffset\t");
1337 1338
	if (opt_list && opt_file)
		printf("foffset\t");
1339 1340
	if (opt_list && opt_list_cgroup)
		printf("cgroup\t");
1341 1342 1343
	if (opt_list && opt_list_mapcnt)
		printf("map-cnt\t");

Wu Fengguang's avatar
Wu Fengguang committed
1344
	if (opt_list == 1)
1345
		printf("offset\tlen\tflags\n");
Wu Fengguang's avatar
Wu Fengguang committed
1346 1347 1348
	if (opt_list == 2)
		printf("offset\tflags\n");

1349 1350 1351 1352
	if (opt_file)
		walk_page_cache();
	else
		walk_addr_ranges();
Wu Fengguang's avatar
Wu Fengguang committed
1353 1354

	if (opt_list == 1)
1355
		flush_page_range();
Wu Fengguang's avatar
Wu Fengguang committed
1356 1357 1358 1359 1360 1361 1362 1363 1364

	if (opt_no_summary)
		return 0;

	if (opt_list)
		printf("\n\n");

	show_summary();

1365 1366 1367
	if (opt_list_mapcnt)
		close(kpagecount_fd);

1368 1369 1370
	if (page_idle_fd >= 0)
		close(page_idle_fd);

Wu Fengguang's avatar
Wu Fengguang committed
1371 1372
	return 0;
}