pagemap.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. pagemap, from the userspace perspective
  2. ---------------------------------------
  3. pagemap is a new (as of 2.6.25) set of interfaces in the kernel that allow
  4. userspace programs to examine the page tables and related information by
  5. reading files in /proc.
  6. There are four components to pagemap:
  7. * /proc/pid/pagemap. This file lets a userspace process find out which
  8. physical frame each virtual page is mapped to. It contains one 64-bit
  9. value for each virtual page, containing the following data (from
  10. fs/proc/task_mmu.c, above pagemap_read):
  11. * Bits 0-54 page frame number (PFN) if present
  12. * Bits 0-4 swap type if swapped
  13. * Bits 5-54 swap offset if swapped
  14. * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
  15. * Bit 56 page exclusively mapped (since 4.2)
  16. * Bits 57-60 zero
  17. * Bit 61 page is file-page or shared-anon (since 3.5)
  18. * Bit 62 page swapped
  19. * Bit 63 page present
  20. Since Linux 4.0 only users with the CAP_SYS_ADMIN capability can get PFNs.
  21. In 4.0 and 4.1 opens by unprivileged fail with -EPERM. Starting from
  22. 4.2 the PFN field is zeroed if the user does not have CAP_SYS_ADMIN.
  23. Reason: information about PFNs helps in exploiting Rowhammer vulnerability.
  24. If the page is not present but in swap, then the PFN contains an
  25. encoding of the swap file number and the page's offset into the
  26. swap. Unmapped pages return a null PFN. This allows determining
  27. precisely which pages are mapped (or in swap) and comparing mapped
  28. pages between processes.
  29. Efficient users of this interface will use /proc/pid/maps to
  30. determine which areas of memory are actually mapped and llseek to
  31. skip over unmapped regions.
  32. * /proc/kpagecount. This file contains a 64-bit count of the number of
  33. times each page is mapped, indexed by PFN.
  34. * /proc/kpageflags. This file contains a 64-bit set of flags for each
  35. page, indexed by PFN.
  36. The flags are (from fs/proc/page.c, above kpageflags_read):
  37. 0. LOCKED
  38. 1. ERROR
  39. 2. REFERENCED
  40. 3. UPTODATE
  41. 4. DIRTY
  42. 5. LRU
  43. 6. ACTIVE
  44. 7. SLAB
  45. 8. WRITEBACK
  46. 9. RECLAIM
  47. 10. BUDDY
  48. 11. MMAP
  49. 12. ANON
  50. 13. SWAPCACHE
  51. 14. SWAPBACKED
  52. 15. COMPOUND_HEAD
  53. 16. COMPOUND_TAIL
  54. 17. HUGE
  55. 18. UNEVICTABLE
  56. 19. HWPOISON
  57. 20. NOPAGE
  58. 21. KSM
  59. 22. THP
  60. 23. BALLOON
  61. 24. ZERO_PAGE
  62. 25. IDLE
  63. * /proc/kpagecgroup. This file contains a 64-bit inode number of the
  64. memory cgroup each page is charged to, indexed by PFN. Only available when
  65. CONFIG_MEMCG is set.
  66. Short descriptions to the page flags:
  67. 0. LOCKED
  68. page is being locked for exclusive access, eg. by undergoing read/write IO
  69. 7. SLAB
  70. page is managed by the SLAB/SLOB/SLUB/SLQB kernel memory allocator
  71. When compound page is used, SLUB/SLQB will only set this flag on the head
  72. page; SLOB will not flag it at all.
  73. 10. BUDDY
  74. a free memory block managed by the buddy system allocator
  75. The buddy system organizes free memory in blocks of various orders.
  76. An order N block has 2^N physically contiguous pages, with the BUDDY flag
  77. set for and _only_ for the first page.
  78. 15. COMPOUND_HEAD
  79. 16. COMPOUND_TAIL
  80. A compound page with order N consists of 2^N physically contiguous pages.
  81. A compound page with order 2 takes the form of "HTTT", where H donates its
  82. head page and T donates its tail page(s). The major consumers of compound
  83. pages are hugeTLB pages (Documentation/vm/hugetlbpage.txt), the SLUB etc.
  84. memory allocators and various device drivers. However in this interface,
  85. only huge/giga pages are made visible to end users.
  86. 17. HUGE
  87. this is an integral part of a HugeTLB page
  88. 19. HWPOISON
  89. hardware detected memory corruption on this page: don't touch the data!
  90. 20. NOPAGE
  91. no page frame exists at the requested address
  92. 21. KSM
  93. identical memory pages dynamically shared between one or more processes
  94. 22. THP
  95. contiguous pages which construct transparent hugepages
  96. 23. BALLOON
  97. balloon compaction page
  98. 24. ZERO_PAGE
  99. zero page for pfn_zero or huge_zero page
  100. 25. IDLE
  101. page has not been accessed since it was marked idle (see
  102. Documentation/vm/idle_page_tracking.txt). Note that this flag may be
  103. stale in case the page was accessed via a PTE. To make sure the flag
  104. is up-to-date one has to read /sys/kernel/mm/page_idle/bitmap first.
  105. [IO related page flags]
  106. 1. ERROR IO error occurred
  107. 3. UPTODATE page has up-to-date data
  108. ie. for file backed page: (in-memory data revision >= on-disk one)
  109. 4. DIRTY page has been written to, hence contains new data
  110. ie. for file backed page: (in-memory data revision > on-disk one)
  111. 8. WRITEBACK page is being synced to disk
  112. [LRU related page flags]
  113. 5. LRU page is in one of the LRU lists
  114. 6. ACTIVE page is in the active LRU list
  115. 18. UNEVICTABLE page is in the unevictable (non-)LRU list
  116. It is somehow pinned and not a candidate for LRU page reclaims,
  117. eg. ramfs pages, shmctl(SHM_LOCK) and mlock() memory segments
  118. 2. REFERENCED page has been referenced since last LRU list enqueue/requeue
  119. 9. RECLAIM page will be reclaimed soon after its pageout IO completed
  120. 11. MMAP a memory mapped page
  121. 12. ANON a memory mapped page that is not part of a file
  122. 13. SWAPCACHE page is mapped to swap space, ie. has an associated swap entry
  123. 14. SWAPBACKED page is backed by swap/RAM
  124. The page-types tool in the tools/vm directory can be used to query the
  125. above flags.
  126. Using pagemap to do something useful:
  127. The general procedure for using pagemap to find out about a process' memory
  128. usage goes like this:
  129. 1. Read /proc/pid/maps to determine which parts of the memory space are
  130. mapped to what.
  131. 2. Select the maps you are interested in -- all of them, or a particular
  132. library, or the stack or the heap, etc.
  133. 3. Open /proc/pid/pagemap and seek to the pages you would like to examine.
  134. 4. Read a u64 for each page from pagemap.
  135. 5. Open /proc/kpagecount and/or /proc/kpageflags. For each PFN you just
  136. read, seek to that entry in the file, and read the data you want.
  137. For example, to find the "unique set size" (USS), which is the amount of
  138. memory that a process is using that is not shared with any other process,
  139. you can go through every map in the process, find the PFNs, look those up
  140. in kpagecount, and tally up the number of pages that are only referenced
  141. once.
  142. Other notes:
  143. Reading from any of the files will return -EINVAL if you are not starting
  144. the read on an 8-byte boundary (e.g., if you sought an odd number of bytes
  145. into the file), or if the size of the read is not a multiple of 8 bytes.
  146. Before Linux 3.11 pagemap bits 55-60 were used for "page-shift" (which is
  147. always 12 at most architectures). Since Linux 3.11 their meaning changes
  148. after first clear of soft-dirty bits. Since Linux 4.2 they are used for
  149. flags unconditionally.