page-types.c 28 KB

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