brintos

brintos / linux-shallow public Read only

0
0
Text · 11.5 KiB · caba0f5 Raw
299 lines · plain
1=============================2Examining Process Page Tables3=============================4 5pagemap is a new (as of 2.6.25) set of interfaces in the kernel that allow6userspace programs to examine the page tables and related information by7reading files in ``/proc``.8 9There are four components to pagemap:10 11 * ``/proc/pid/pagemap``.  This file lets a userspace process find out which12   physical frame each virtual page is mapped to.  It contains one 64-bit13   value for each virtual page, containing the following data (from14   ``fs/proc/task_mmu.c``, above pagemap_read):15 16    * Bits 0-54  page frame number (PFN) if present17    * Bits 0-4   swap type if swapped18    * Bits 5-54  swap offset if swapped19    * Bit  55    pte is soft-dirty (see20      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) (see23      Documentation/admin-guide/mm/userfaultfd.rst)24    * Bits 58-60 zero25    * Bit  61    page is file-page or shared-anon (since 3.5)26    * Bit  62    page swapped27    * Bit  63    page present28 29   Since Linux 4.0 only users with the CAP_SYS_ADMIN capability can get PFNs.30   In 4.0 and 4.1 opens by unprivileged fail with -EPERM.  Starting from31   4.2 the PFN field is zeroed if the user does not have CAP_SYS_ADMIN.32   Reason: information about PFNs helps in exploiting Rowhammer vulnerability.33 34   If the page is not present but in swap, then the PFN contains an35   encoding of the swap file number and the page's offset into the36   swap. Unmapped pages return a null PFN. This allows determining37   precisely which pages are mapped (or in swap) and comparing mapped38   pages between processes.39 40   Efficient users of this interface will use ``/proc/pid/maps`` to41   determine which areas of memory are actually mapped and llseek to42   skip over unmapped regions.43 44 * ``/proc/kpagecount``.  This file contains a 64-bit count of the number of45   times each page is mapped, indexed by PFN.46 47The page-types tool in the tools/mm directory can be used to query the48number of times a page is mapped.49 50 * ``/proc/kpageflags``.  This file contains a 64-bit set of flags for each51   page, indexed by PFN.52 53   The flags are (from ``fs/proc/page.c``, above kpageflags_read):54 55    0. LOCKED56    1. ERROR57    2. REFERENCED58    3. UPTODATE59    4. DIRTY60    5. LRU61    6. ACTIVE62    7. SLAB63    8. WRITEBACK64    9. RECLAIM65    10. BUDDY66    11. MMAP67    12. ANON68    13. SWAPCACHE69    14. SWAPBACKED70    15. COMPOUND_HEAD71    16. COMPOUND_TAIL72    17. HUGE73    18. UNEVICTABLE74    19. HWPOISON75    20. NOPAGE76    21. KSM77    22. THP78    23. OFFLINE79    24. ZERO_PAGE80    25. IDLE81    26. PGTABLE82 83 * ``/proc/kpagecgroup``.  This file contains a 64-bit inode number of the84   memory cgroup each page is charged to, indexed by PFN. Only available when85   CONFIG_MEMCG is set.86 87Short descriptions to the page flags88====================================89 900 - LOCKED91   The page is being locked for exclusive access, e.g. by undergoing read/write92   IO.937 - SLAB94   The page is managed by the SLAB/SLUB kernel memory allocator.95   When compound page is used, either will only set this flag on the head96   page.9710 - BUDDY98    A free memory block managed by the buddy system allocator.99    The buddy system organizes free memory in blocks of various orders.100    An order N block has 2^N physically contiguous pages, with the BUDDY flag101    set for and _only_ for the first page.10215 - COMPOUND_HEAD103    A compound page with order N consists of 2^N physically contiguous pages.104    A compound page with order 2 takes the form of "HTTT", where H donates its105    head page and T donates its tail page(s).  The major consumers of compound106    pages are hugeTLB pages (Documentation/admin-guide/mm/hugetlbpage.rst),107    the SLUB etc.  memory allocators and various device drivers.108    However in this interface, only huge/giga pages are made visible109    to end users.11016 - COMPOUND_TAIL111    A compound page tail (see description above).11217 - HUGE113    This is an integral part of a HugeTLB page.11419 - HWPOISON115    Hardware detected memory corruption on this page: don't touch the data!11620 - NOPAGE117    No page frame exists at the requested address.11821 - KSM119    Identical memory pages dynamically shared between one or more processes.12022 - THP121    Contiguous pages which construct THP of any size and mapped by any granularity.12223 - OFFLINE123    The page is logically offline.12424 - ZERO_PAGE125    Zero page for pfn_zero or huge_zero page.12625 - IDLE127    The page has not been accessed since it was marked idle (see128    Documentation/admin-guide/mm/idle_page_tracking.rst).129    Note that this flag may be stale in case the page was accessed via130    a PTE. To make sure the flag is up-to-date one has to read131    ``/sys/kernel/mm/page_idle/bitmap`` first.13226 - PGTABLE133    The page is in use as a page table.134 135IO related page flags136---------------------137 1381 - ERROR139   IO error occurred.1403 - UPTODATE141   The page has up-to-date data.142   ie. for file backed page: (in-memory data revision >= on-disk one)1434 - DIRTY144   The page has been written to, hence contains new data.145   i.e. for file backed page: (in-memory data revision >  on-disk one)1468 - WRITEBACK147   The page is being synced to disk.148 149LRU related page flags150----------------------151 1525 - LRU153   The page is in one of the LRU lists.1546 - ACTIVE155   The page is in the active LRU list.15618 - UNEVICTABLE157   The page is in the unevictable (non-)LRU list It is somehow pinned and158   not a candidate for LRU page reclaims, e.g. ramfs pages,159   shmctl(SHM_LOCK) and mlock() memory segments.1602 - REFERENCED161   The page has been referenced since last LRU list enqueue/requeue.1629 - RECLAIM163   The page will be reclaimed soon after its pageout IO completed.16411 - MMAP165   A memory mapped page.16612 - ANON167   A memory mapped page that is not part of a file.16813 - SWAPCACHE169   The page is mapped to swap space, i.e. has an associated swap entry.17014 - SWAPBACKED171   The page is backed by swap/RAM.172 173The page-types tool in the tools/mm directory can be used to query the174above flags.175 176Exceptions for Shared Memory177============================178 179Page table entries for shared pages are cleared when the pages are zapped or180swapped out. This makes swapped out pages indistinguishable from never-allocated181ones.182 183In kernel space, the swap location can still be retrieved from the page cache.184However, values stored only on the normal PTE get lost irretrievably when the185page is swapped out (i.e. SOFT_DIRTY).186 187In user space, whether the page is present, swapped or none can be deduced with188the help of lseek and/or mincore system calls.189 190lseek() can differentiate between accessed pages (present or swapped out) and191holes (none/non-allocated) by specifying the SEEK_DATA flag on the file where192the pages are backed. For anonymous shared pages, the file can be found in193``/proc/pid/map_files/``.194 195mincore() can differentiate between pages in memory (present, including swap196cache) and out of memory (swapped out or none/non-allocated).197 198Other notes199===========200 201Reading from any of the files will return -EINVAL if you are not starting202the read on an 8-byte boundary (e.g., if you sought an odd number of bytes203into the file), or if the size of the read is not a multiple of 8 bytes.204 205Before Linux 3.11 pagemap bits 55-60 were used for "page-shift" (which is206always 12 at most architectures). Since Linux 3.11 their meaning changes207after first clear of soft-dirty bits. Since Linux 4.2 they are used for208flags unconditionally.209 210Pagemap Scan IOCTL211==================212 213The ``PAGEMAP_SCAN`` IOCTL on the pagemap file can be used to get or optionally214clear the info about page table entries. The following operations are supported215in this IOCTL:216 217- Scan the address range and get the memory ranges matching the provided criteria.218  This is performed when the output buffer is specified.219- Write-protect the pages. The ``PM_SCAN_WP_MATCHING`` is used to write-protect220  the pages of interest. The ``PM_SCAN_CHECK_WPASYNC`` aborts the operation if221  non-Async Write Protected pages are found. The ``PM_SCAN_WP_MATCHING`` can be222  used with or without ``PM_SCAN_CHECK_WPASYNC``.223- Both of those operations can be combined into one atomic operation where we can224  get and write protect the pages as well.225 226Following flags about pages are currently supported:227 228- ``PAGE_IS_WPALLOWED`` - Page has async-write-protection enabled229- ``PAGE_IS_WRITTEN`` - Page has been written to from the time it was write protected230- ``PAGE_IS_FILE`` - Page is file backed231- ``PAGE_IS_PRESENT`` - Page is present in the memory232- ``PAGE_IS_SWAPPED`` - Page is in swapped233- ``PAGE_IS_PFNZERO`` - Page has zero PFN234- ``PAGE_IS_HUGE`` - Page is PMD-mapped THP or Hugetlb backed235- ``PAGE_IS_SOFT_DIRTY`` - Page is soft-dirty236 237The ``struct pm_scan_arg`` is used as the argument of the IOCTL.238 239 1. The size of the ``struct pm_scan_arg`` must be specified in the ``size``240    field. This field will be helpful in recognizing the structure if extensions241    are done later.242 2. The flags can be specified in the ``flags`` field. The ``PM_SCAN_WP_MATCHING``243    and ``PM_SCAN_CHECK_WPASYNC`` are the only added flags at this time. The get244    operation is optionally performed depending upon if the output buffer is245    provided or not.246 3. The range is specified through ``start`` and ``end``.247 4. The walk can abort before visiting the complete range such as the user buffer248    can get full etc. The walk ending address is specified in``end_walk``.249 5. The output buffer of ``struct page_region`` array and size is specified in250    ``vec`` and ``vec_len``.251 6. The optional maximum requested pages are specified in the ``max_pages``.252 7. The masks are specified in ``category_mask``, ``category_anyof_mask``,253    ``category_inverted`` and ``return_mask``.254 255Find pages which have been written and WP them as well::256 257   struct pm_scan_arg arg = {258   .size = sizeof(arg),259   .flags = PM_SCAN_CHECK_WPASYNC | PM_SCAN_CHECK_WPASYNC,260   ..261   .category_mask = PAGE_IS_WRITTEN,262   .return_mask = PAGE_IS_WRITTEN,263   };264 265Find pages which have been written, are file backed, not swapped and either266present or huge::267 268   struct pm_scan_arg arg = {269   .size = sizeof(arg),270   .flags = 0,271   ..272   .category_mask = PAGE_IS_WRITTEN | PAGE_IS_SWAPPED,273   .category_inverted = PAGE_IS_SWAPPED,274   .category_anyof_mask = PAGE_IS_PRESENT | PAGE_IS_HUGE,275   .return_mask = PAGE_IS_WRITTEN | PAGE_IS_SWAPPED |276                  PAGE_IS_PRESENT | PAGE_IS_HUGE,277   };278 279The ``PAGE_IS_WRITTEN`` flag can be considered as a better-performing alternative280of soft-dirty flag. It doesn't get affected by VMA merging of the kernel and hence281the user can find the true soft-dirty pages in case of normal pages. (There may282still be extra dirty pages reported for THP or Hugetlb pages.)283 284"PAGE_IS_WRITTEN" category is used with uffd write protect-enabled ranges to285implement memory dirty tracking in userspace:286 287 1. The userfaultfd file descriptor is created with ``userfaultfd`` syscall.288 2. The ``UFFD_FEATURE_WP_UNPOPULATED`` and ``UFFD_FEATURE_WP_ASYNC`` features289    are set by ``UFFDIO_API`` IOCTL.290 3. The memory range is registered with ``UFFDIO_REGISTER_MODE_WP`` mode291    through ``UFFDIO_REGISTER`` IOCTL.292 4. Then any part of the registered memory or the whole memory region must293    be write protected using ``PAGEMAP_SCAN`` IOCTL with flag ``PM_SCAN_WP_MATCHING``294    or the ``UFFDIO_WRITEPROTECT`` IOCTL can be used. Both of these perform the295    same operation. The former is better in terms of performance.296 5. Now the ``PAGEMAP_SCAN`` IOCTL can be used to either just find pages which297    have been written to since they were last marked and/or optionally write protect298    the pages as well.299