pagemap.rst 11 KB

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