pagemap.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. =============================
  2. Examining Process Page Tables
  3. =============================
  4. pagemap is a new (as of 2.6.25) set of interfaces in the kernel that allow
  5. userspace programs to examine the page tables and related information by
  6. reading files in ``/proc``.
  7. There are four components to pagemap:
  8. * ``/proc/pid/pagemap``. This file lets a userspace process find out which
  9. physical frame each virtual page is mapped to. It contains one 64-bit
  10. value for each virtual page, containing the following data (from
  11. ``fs/proc/task_mmu.c``, above pagemap_read):
  12. * Bits 0-54 page frame number (PFN) if present
  13. * Bits 0-4 swap type if swapped
  14. * Bits 5-54 swap offset if swapped
  15. * Bit 55 pte is soft-dirty (see
  16. Documentation/admin-guide/mm/soft-dirty.rst)
  17. * Bit 56 page exclusively mapped (since 4.2)
  18. * Bit 57 pte is uffd-wp write-protected (since 5.13) (see
  19. Documentation/admin-guide/mm/userfaultfd.rst)
  20. * Bit 58 pte is a guard region (since 6.15) (see madvise (2) man page)
  21. * Bits 59-60 zero
  22. * Bit 61 page is file-page or shared-anon (since 3.5)
  23. * Bit 62 page swapped
  24. * Bit 63 page present
  25. Since Linux 4.0 only users with the CAP_SYS_ADMIN capability can get PFNs.
  26. In 4.0 and 4.1 opens by unprivileged fail with -EPERM. Starting from
  27. 4.2 the PFN field is zeroed if the user does not have CAP_SYS_ADMIN.
  28. Reason: information about PFNs helps in exploiting Rowhammer vulnerability.
  29. If the page is not present but in swap, then the PFN contains an
  30. encoding of the swap file number and the page's offset into the
  31. swap. Unmapped pages return a null PFN. This allows determining
  32. precisely which pages are mapped (or in swap) and comparing mapped
  33. pages between processes.
  34. Traditionally, bit 56 indicates that a page is mapped exactly once and bit
  35. 56 is clear when a page is mapped multiple times, even when mapped in the
  36. same process multiple times. In some kernel configurations, the semantics
  37. for pages part of a larger allocation (e.g., THP) can differ: bit 56 is set
  38. if all pages part of the corresponding large allocation are *certainly*
  39. mapped in the same process, even if the page is mapped multiple times in that
  40. process. Bit 56 is clear when any page page of the larger allocation
  41. is *maybe* mapped in a different process. In some cases, a large allocation
  42. might be treated as "maybe mapped by multiple processes" even though this
  43. is no longer the case.
  44. Efficient users of this interface will use ``/proc/pid/maps`` to
  45. determine which areas of memory are actually mapped and llseek to
  46. skip over unmapped regions.
  47. * ``/proc/kpagecount``. This file contains a 64-bit count of the number of
  48. times each page is mapped, indexed by PFN. Some kernel configurations do
  49. not track the precise number of times a page part of a larger allocation
  50. (e.g., THP) is mapped. In these configurations, the average number of
  51. mappings per page in this larger allocation is returned instead. However,
  52. if any page of the large allocation is mapped, the returned value will
  53. be at least 1.
  54. The page-types tool in the tools/mm directory can be used to query the
  55. number of times a page is mapped.
  56. * ``/proc/kpageflags``. This file contains a 64-bit set of flags for each
  57. page, indexed by PFN.
  58. The flags are (from ``fs/proc/page.c``, above kpageflags_read):
  59. 0. LOCKED
  60. 1. ERROR
  61. 2. REFERENCED
  62. 3. UPTODATE
  63. 4. DIRTY
  64. 5. LRU
  65. 6. ACTIVE
  66. 7. SLAB
  67. 8. WRITEBACK
  68. 9. RECLAIM
  69. 10. BUDDY
  70. 11. MMAP
  71. 12. ANON
  72. 13. SWAPCACHE
  73. 14. SWAPBACKED
  74. 15. COMPOUND_HEAD
  75. 16. COMPOUND_TAIL
  76. 17. HUGE
  77. 18. UNEVICTABLE
  78. 19. HWPOISON
  79. 20. NOPAGE
  80. 21. KSM
  81. 22. THP
  82. 23. OFFLINE
  83. 24. ZERO_PAGE
  84. 25. IDLE
  85. 26. PGTABLE
  86. * ``/proc/kpagecgroup``. This file contains a 64-bit inode number of the
  87. memory cgroup each page is charged to, indexed by PFN. Only available when
  88. CONFIG_MEMCG is set.
  89. Short descriptions to the page flags
  90. ====================================
  91. 0 - LOCKED
  92. The page is being locked for exclusive access, e.g. by undergoing read/write
  93. IO.
  94. 7 - SLAB
  95. The page is managed by the SLAB/SLUB kernel memory allocator.
  96. When compound page is used, either will only set this flag on the head
  97. page.
  98. 10 - BUDDY
  99. A free memory block managed by the buddy system allocator.
  100. The buddy system organizes free memory in blocks of various orders.
  101. An order N block has 2^N physically contiguous pages, with the BUDDY flag
  102. set for all pages.
  103. Before 4.6 only the first page of the block had the flag set.
  104. 15 - COMPOUND_HEAD
  105. A compound page with order N consists of 2^N physically contiguous pages.
  106. A compound page with order 2 takes the form of "HTTT", where H donates its
  107. head page and T donates its tail page(s). The major consumers of compound
  108. pages are hugeTLB pages (Documentation/admin-guide/mm/hugetlbpage.rst),
  109. the SLUB etc. memory allocators and various device drivers.
  110. However in this interface, only huge/giga pages are made visible
  111. to end users.
  112. 16 - COMPOUND_TAIL
  113. A compound page tail (see description above).
  114. 17 - HUGE
  115. This is an integral part of a HugeTLB page.
  116. 19 - HWPOISON
  117. Hardware detected memory corruption on this page: don't touch the data!
  118. 20 - NOPAGE
  119. No page frame exists at the requested address.
  120. 21 - KSM
  121. Identical memory pages dynamically shared between one or more processes.
  122. 22 - THP
  123. Contiguous pages which construct THP of any size and mapped by any granularity.
  124. 23 - OFFLINE
  125. The page is logically offline.
  126. 24 - ZERO_PAGE
  127. Zero page for pfn_zero or huge_zero page.
  128. 25 - IDLE
  129. The page has not been accessed since it was marked idle (see
  130. Documentation/admin-guide/mm/idle_page_tracking.rst).
  131. Note that this flag may be stale in case the page was accessed via
  132. a PTE. To make sure the flag is up-to-date one has to read
  133. ``/sys/kernel/mm/page_idle/bitmap`` first.
  134. 26 - PGTABLE
  135. The page is in use as a page table.
  136. IO related page flags
  137. ---------------------
  138. 1 - ERROR
  139. IO error occurred.
  140. 3 - UPTODATE
  141. The page has up-to-date data.
  142. ie. for file backed page: (in-memory data revision >= on-disk one)
  143. 4 - DIRTY
  144. The page has been written to, hence contains new data.
  145. i.e. for file backed page: (in-memory data revision > on-disk one)
  146. 8 - WRITEBACK
  147. The page is being synced to disk.
  148. LRU related page flags
  149. ----------------------
  150. 5 - LRU
  151. The page is in one of the LRU lists.
  152. 6 - ACTIVE
  153. The page is in the active LRU list.
  154. 18 - UNEVICTABLE
  155. The page is in the unevictable (non-)LRU list It is somehow pinned and
  156. not a candidate for LRU page reclaims, e.g. ramfs pages,
  157. shmctl(SHM_LOCK) and mlock() memory segments.
  158. 2 - REFERENCED
  159. The page has been referenced since last LRU list enqueue/requeue.
  160. 9 - RECLAIM
  161. The page will be reclaimed soon after its pageout IO completed.
  162. 11 - MMAP
  163. A memory mapped page.
  164. 12 - ANON
  165. A memory mapped page that is not part of a file.
  166. 13 - SWAPCACHE
  167. The page is mapped to swap space, i.e. has an associated swap entry.
  168. 14 - SWAPBACKED
  169. The page is backed by swap/RAM.
  170. The page-types tool in the tools/mm directory can be used to query the
  171. above flags.
  172. Exceptions for Shared Memory
  173. ============================
  174. Page table entries for shared pages are cleared when the pages are zapped or
  175. swapped out. This makes swapped out pages indistinguishable from never-allocated
  176. ones.
  177. In kernel space, the swap location can still be retrieved from the page cache.
  178. However, values stored only on the normal PTE get lost irretrievably when the
  179. page is swapped out (i.e. SOFT_DIRTY).
  180. In user space, whether the page is present, swapped or none can be deduced with
  181. the help of lseek and/or mincore system calls.
  182. lseek() can differentiate between accessed pages (present or swapped out) and
  183. holes (none/non-allocated) by specifying the SEEK_DATA flag on the file where
  184. the pages are backed. For anonymous shared pages, the file can be found in
  185. ``/proc/pid/map_files/``.
  186. mincore() can differentiate between pages in memory (present, including swap
  187. cache) and out of memory (swapped out or none/non-allocated).
  188. Other notes
  189. ===========
  190. Reading from any of the files will return -EINVAL if you are not starting
  191. the read on an 8-byte boundary (e.g., if you sought an odd number of bytes
  192. into the file), or if the size of the read is not a multiple of 8 bytes.
  193. Before Linux 3.11 pagemap bits 55-60 were used for "page-shift" (which is
  194. always 12 at most architectures). Since Linux 3.11 their meaning changes
  195. after first clear of soft-dirty bits. Since Linux 4.2 they are used for
  196. flags unconditionally.
  197. Pagemap Scan IOCTL
  198. ==================
  199. The ``PAGEMAP_SCAN`` IOCTL on the pagemap file can be used to get or optionally
  200. clear the info about page table entries. The following operations are supported
  201. in this IOCTL:
  202. - Scan the address range and get the memory ranges matching the provided criteria.
  203. This is performed when the output buffer is specified.
  204. - Write-protect the pages. The ``PM_SCAN_WP_MATCHING`` is used to write-protect
  205. the pages of interest. The ``PM_SCAN_CHECK_WPASYNC`` aborts the operation if
  206. non-Async Write Protected pages are found. The ``PM_SCAN_WP_MATCHING`` can be
  207. used with or without ``PM_SCAN_CHECK_WPASYNC``.
  208. - Both of those operations can be combined into one atomic operation where we can
  209. get and write protect the pages as well.
  210. Following flags about pages are currently supported:
  211. - ``PAGE_IS_WPALLOWED`` - Page has async-write-protection enabled
  212. - ``PAGE_IS_WRITTEN`` - Page has been written to from the time it was write protected
  213. - ``PAGE_IS_FILE`` - Page is file backed
  214. - ``PAGE_IS_PRESENT`` - Page is present in the memory
  215. - ``PAGE_IS_SWAPPED`` - Page is in swapped
  216. - ``PAGE_IS_PFNZERO`` - Page has zero PFN
  217. - ``PAGE_IS_HUGE`` - Page is PMD-mapped THP or Hugetlb backed
  218. - ``PAGE_IS_SOFT_DIRTY`` - Page is soft-dirty
  219. - ``PAGE_IS_GUARD`` - Page is a part of a guard region
  220. The ``struct pm_scan_arg`` is used as the argument of the IOCTL.
  221. 1. The size of the ``struct pm_scan_arg`` must be specified in the ``size``
  222. field. This field will be helpful in recognizing the structure if extensions
  223. are done later.
  224. 2. The flags can be specified in the ``flags`` field. The ``PM_SCAN_WP_MATCHING``
  225. and ``PM_SCAN_CHECK_WPASYNC`` are the only added flags at this time. The get
  226. operation is optionally performed depending upon if the output buffer is
  227. provided or not.
  228. 3. The range is specified through ``start`` and ``end``.
  229. 4. The walk can abort before visiting the complete range such as the user buffer
  230. can get full etc. The walk ending address is specified in``end_walk``.
  231. 5. The output buffer of ``struct page_region`` array and size is specified in
  232. ``vec`` and ``vec_len``.
  233. 6. The optional maximum requested pages are specified in the ``max_pages``.
  234. 7. The masks are specified in ``category_mask``, ``category_anyof_mask``,
  235. ``category_inverted`` and ``return_mask``.
  236. Find pages which have been written and WP them as well::
  237. struct pm_scan_arg arg = {
  238. .size = sizeof(arg),
  239. .flags = PM_SCAN_CHECK_WPASYNC | PM_SCAN_CHECK_WPASYNC,
  240. ..
  241. .category_mask = PAGE_IS_WRITTEN,
  242. .return_mask = PAGE_IS_WRITTEN,
  243. };
  244. Find pages which have been written, are file backed, not swapped and either
  245. present or huge::
  246. struct pm_scan_arg arg = {
  247. .size = sizeof(arg),
  248. .flags = 0,
  249. ..
  250. .category_mask = PAGE_IS_WRITTEN | PAGE_IS_SWAPPED,
  251. .category_inverted = PAGE_IS_SWAPPED,
  252. .category_anyof_mask = PAGE_IS_PRESENT | PAGE_IS_HUGE,
  253. .return_mask = PAGE_IS_WRITTEN | PAGE_IS_SWAPPED |
  254. PAGE_IS_PRESENT | PAGE_IS_HUGE,
  255. };
  256. The ``PAGE_IS_WRITTEN`` flag can be considered as a better-performing alternative
  257. of soft-dirty flag. It doesn't get affected by VMA merging of the kernel and hence
  258. the user can find the true soft-dirty pages in case of normal pages. (There may
  259. still be extra dirty pages reported for THP or Hugetlb pages.)
  260. "PAGE_IS_WRITTEN" category is used with uffd write protect-enabled ranges to
  261. implement memory dirty tracking in userspace:
  262. 1. The userfaultfd file descriptor is created with ``userfaultfd`` syscall.
  263. 2. The ``UFFD_FEATURE_WP_UNPOPULATED`` and ``UFFD_FEATURE_WP_ASYNC`` features
  264. are set by ``UFFDIO_API`` IOCTL.
  265. 3. The memory range is registered with ``UFFDIO_REGISTER_MODE_WP`` mode
  266. through ``UFFDIO_REGISTER`` IOCTL.
  267. 4. Then any part of the registered memory or the whole memory region must
  268. be write protected using ``PAGEMAP_SCAN`` IOCTL with flag ``PM_SCAN_WP_MATCHING``
  269. or the ``UFFDIO_WRITEPROTECT`` IOCTL can be used. Both of these perform the
  270. same operation. The former is better in terms of performance.
  271. 5. Now the ``PAGEMAP_SCAN`` IOCTL can be used to either just find pages which
  272. have been written to since they were last marked and/or optionally write protect
  273. the pages as well.