page-types.c 30 KB

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