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