Back to home page

OSCL-LXR

 
 

    


0001 .. _unevictable_lru:
0002 
0003 ==============================
0004 Unevictable LRU Infrastructure
0005 ==============================
0006 
0007 .. contents:: :local:
0008 
0009 
0010 Introduction
0011 ============
0012 
0013 This document describes the Linux memory manager's "Unevictable LRU"
0014 infrastructure and the use of this to manage several types of "unevictable"
0015 pages.
0016 
0017 The document attempts to provide the overall rationale behind this mechanism
0018 and the rationale for some of the design decisions that drove the
0019 implementation.  The latter design rationale is discussed in the context of an
0020 implementation description.  Admittedly, one can obtain the implementation
0021 details - the "what does it do?" - by reading the code.  One hopes that the
0022 descriptions below add value by provide the answer to "why does it do that?".
0023 
0024 
0025 
0026 The Unevictable LRU
0027 ===================
0028 
0029 The Unevictable LRU facility adds an additional LRU list to track unevictable
0030 pages and to hide these pages from vmscan.  This mechanism is based on a patch
0031 by Larry Woodman of Red Hat to address several scalability problems with page
0032 reclaim in Linux.  The problems have been observed at customer sites on large
0033 memory x86_64 systems.
0034 
0035 To illustrate this with an example, a non-NUMA x86_64 platform with 128GB of
0036 main memory will have over 32 million 4k pages in a single node.  When a large
0037 fraction of these pages are not evictable for any reason [see below], vmscan
0038 will spend a lot of time scanning the LRU lists looking for the small fraction
0039 of pages that are evictable.  This can result in a situation where all CPUs are
0040 spending 100% of their time in vmscan for hours or days on end, with the system
0041 completely unresponsive.
0042 
0043 The unevictable list addresses the following classes of unevictable pages:
0044 
0045  * Those owned by ramfs.
0046 
0047  * Those mapped into SHM_LOCK'd shared memory regions.
0048 
0049  * Those mapped into VM_LOCKED [mlock()ed] VMAs.
0050 
0051 The infrastructure may also be able to handle other conditions that make pages
0052 unevictable, either by definition or by circumstance, in the future.
0053 
0054 
0055 The Unevictable LRU Page List
0056 -----------------------------
0057 
0058 The Unevictable LRU page list is a lie.  It was never an LRU-ordered list, but a
0059 companion to the LRU-ordered anonymous and file, active and inactive page lists;
0060 and now it is not even a page list.  But following familiar convention, here in
0061 this document and in the source, we often imagine it as a fifth LRU page list.
0062 
0063 The Unevictable LRU infrastructure consists of an additional, per-node, LRU list
0064 called the "unevictable" list and an associated page flag, PG_unevictable, to
0065 indicate that the page is being managed on the unevictable list.
0066 
0067 The PG_unevictable flag is analogous to, and mutually exclusive with, the
0068 PG_active flag in that it indicates on which LRU list a page resides when
0069 PG_lru is set.
0070 
0071 The Unevictable LRU infrastructure maintains unevictable pages as if they were
0072 on an additional LRU list for a few reasons:
0073 
0074  (1) We get to "treat unevictable pages just like we treat other pages in the
0075      system - which means we get to use the same code to manipulate them, the
0076      same code to isolate them (for migrate, etc.), the same code to keep track
0077      of the statistics, etc..." [Rik van Riel]
0078 
0079  (2) We want to be able to migrate unevictable pages between nodes for memory
0080      defragmentation, workload management and memory hotplug.  The Linux kernel
0081      can only migrate pages that it can successfully isolate from the LRU
0082      lists (or "Movable" pages: outside of consideration here).  If we were to
0083      maintain pages elsewhere than on an LRU-like list, where they can be
0084      detected by isolate_lru_page(), we would prevent their migration.
0085 
0086 The unevictable list does not differentiate between file-backed and anonymous,
0087 swap-backed pages.  This differentiation is only important while the pages are,
0088 in fact, evictable.
0089 
0090 The unevictable list benefits from the "arrayification" of the per-node LRU
0091 lists and statistics originally proposed and posted by Christoph Lameter.
0092 
0093 
0094 Memory Control Group Interaction
0095 --------------------------------
0096 
0097 The unevictable LRU facility interacts with the memory control group [aka
0098 memory controller; see Documentation/admin-guide/cgroup-v1/memory.rst] by
0099 extending the lru_list enum.
0100 
0101 The memory controller data structure automatically gets a per-node unevictable
0102 list as a result of the "arrayification" of the per-node LRU lists (one per
0103 lru_list enum element).  The memory controller tracks the movement of pages to
0104 and from the unevictable list.
0105 
0106 When a memory control group comes under memory pressure, the controller will
0107 not attempt to reclaim pages on the unevictable list.  This has a couple of
0108 effects:
0109 
0110  (1) Because the pages are "hidden" from reclaim on the unevictable list, the
0111      reclaim process can be more efficient, dealing only with pages that have a
0112      chance of being reclaimed.
0113 
0114  (2) On the other hand, if too many of the pages charged to the control group
0115      are unevictable, the evictable portion of the working set of the tasks in
0116      the control group may not fit into the available memory.  This can cause
0117      the control group to thrash or to OOM-kill tasks.
0118 
0119 
0120 .. _mark_addr_space_unevict:
0121 
0122 Marking Address Spaces Unevictable
0123 ----------------------------------
0124 
0125 For facilities such as ramfs none of the pages attached to the address space
0126 may be evicted.  To prevent eviction of any such pages, the AS_UNEVICTABLE
0127 address space flag is provided, and this can be manipulated by a filesystem
0128 using a number of wrapper functions:
0129 
0130  * ``void mapping_set_unevictable(struct address_space *mapping);``
0131 
0132         Mark the address space as being completely unevictable.
0133 
0134  * ``void mapping_clear_unevictable(struct address_space *mapping);``
0135 
0136         Mark the address space as being evictable.
0137 
0138  * ``int mapping_unevictable(struct address_space *mapping);``
0139 
0140         Query the address space, and return true if it is completely
0141         unevictable.
0142 
0143 These are currently used in three places in the kernel:
0144 
0145  (1) By ramfs to mark the address spaces of its inodes when they are created,
0146      and this mark remains for the life of the inode.
0147 
0148  (2) By SYSV SHM to mark SHM_LOCK'd address spaces until SHM_UNLOCK is called.
0149      Note that SHM_LOCK is not required to page in the locked pages if they're
0150      swapped out; the application must touch the pages manually if it wants to
0151      ensure they're in memory.
0152 
0153  (3) By the i915 driver to mark pinned address space until it's unpinned. The
0154      amount of unevictable memory marked by i915 driver is roughly the bounded
0155      object size in debugfs/dri/0/i915_gem_objects.
0156 
0157 
0158 Detecting Unevictable Pages
0159 ---------------------------
0160 
0161 The function page_evictable() in mm/internal.h determines whether a page is
0162 evictable or not using the query function outlined above [see section
0163 :ref:`Marking address spaces unevictable <mark_addr_space_unevict>`]
0164 to check the AS_UNEVICTABLE flag.
0165 
0166 For address spaces that are so marked after being populated (as SHM regions
0167 might be), the lock action (e.g. SHM_LOCK) can be lazy, and need not populate
0168 the page tables for the region as does, for example, mlock(), nor need it make
0169 any special effort to push any pages in the SHM_LOCK'd area to the unevictable
0170 list.  Instead, vmscan will do this if and when it encounters the pages during
0171 a reclamation scan.
0172 
0173 On an unlock action (such as SHM_UNLOCK), the unlocker (e.g. shmctl()) must scan
0174 the pages in the region and "rescue" them from the unevictable list if no other
0175 condition is keeping them unevictable.  If an unevictable region is destroyed,
0176 the pages are also "rescued" from the unevictable list in the process of
0177 freeing them.
0178 
0179 page_evictable() also checks for mlocked pages by testing an additional page
0180 flag, PG_mlocked (as wrapped by PageMlocked()), which is set when a page is
0181 faulted into a VM_LOCKED VMA, or found in a VMA being VM_LOCKED.
0182 
0183 
0184 Vmscan's Handling of Unevictable Pages
0185 --------------------------------------
0186 
0187 If unevictable pages are culled in the fault path, or moved to the unevictable
0188 list at mlock() or mmap() time, vmscan will not encounter the pages until they
0189 have become evictable again (via munlock() for example) and have been "rescued"
0190 from the unevictable list.  However, there may be situations where we decide,
0191 for the sake of expediency, to leave an unevictable page on one of the regular
0192 active/inactive LRU lists for vmscan to deal with.  vmscan checks for such
0193 pages in all of the shrink_{active|inactive|page}_list() functions and will
0194 "cull" such pages that it encounters: that is, it diverts those pages to the
0195 unevictable list for the memory cgroup and node being scanned.
0196 
0197 There may be situations where a page is mapped into a VM_LOCKED VMA, but the
0198 page is not marked as PG_mlocked.  Such pages will make it all the way to
0199 shrink_active_list() or shrink_page_list() where they will be detected when
0200 vmscan walks the reverse map in page_referenced() or try_to_unmap().  The page
0201 is culled to the unevictable list when it is released by the shrinker.
0202 
0203 To "cull" an unevictable page, vmscan simply puts the page back on the LRU list
0204 using putback_lru_page() - the inverse operation to isolate_lru_page() - after
0205 dropping the page lock.  Because the condition which makes the page unevictable
0206 may change once the page is unlocked, __pagevec_lru_add_fn() will recheck the
0207 unevictable state of a page before placing it on the unevictable list.
0208 
0209 
0210 MLOCKED Pages
0211 =============
0212 
0213 The unevictable page list is also useful for mlock(), in addition to ramfs and
0214 SYSV SHM.  Note that mlock() is only available in CONFIG_MMU=y situations; in
0215 NOMMU situations, all mappings are effectively mlocked.
0216 
0217 
0218 History
0219 -------
0220 
0221 The "Unevictable mlocked Pages" infrastructure is based on work originally
0222 posted by Nick Piggin in an RFC patch entitled "mm: mlocked pages off LRU".
0223 Nick posted his patch as an alternative to a patch posted by Christoph Lameter
0224 to achieve the same objective: hiding mlocked pages from vmscan.
0225 
0226 In Nick's patch, he used one of the struct page LRU list link fields as a count
0227 of VM_LOCKED VMAs that map the page (Rik van Riel had the same idea three years
0228 earlier).  But this use of the link field for a count prevented the management
0229 of the pages on an LRU list, and thus mlocked pages were not migratable as
0230 isolate_lru_page() could not detect them, and the LRU list link field was not
0231 available to the migration subsystem.
0232 
0233 Nick resolved this by putting mlocked pages back on the LRU list before
0234 attempting to isolate them, thus abandoning the count of VM_LOCKED VMAs.  When
0235 Nick's patch was integrated with the Unevictable LRU work, the count was
0236 replaced by walking the reverse map when munlocking, to determine whether any
0237 other VM_LOCKED VMAs still mapped the page.
0238 
0239 However, walking the reverse map for each page when munlocking was ugly and
0240 inefficient, and could lead to catastrophic contention on a file's rmap lock,
0241 when many processes which had it mlocked were trying to exit.  In 5.18, the
0242 idea of keeping mlock_count in Unevictable LRU list link field was revived and
0243 put to work, without preventing the migration of mlocked pages.  This is why
0244 the "Unevictable LRU list" cannot be a linked list of pages now; but there was
0245 no use for that linked list anyway - though its size is maintained for meminfo.
0246 
0247 
0248 Basic Management
0249 ----------------
0250 
0251 mlocked pages - pages mapped into a VM_LOCKED VMA - are a class of unevictable
0252 pages.  When such a page has been "noticed" by the memory management subsystem,
0253 the page is marked with the PG_mlocked flag.  This can be manipulated using the
0254 PageMlocked() functions.
0255 
0256 A PG_mlocked page will be placed on the unevictable list when it is added to
0257 the LRU.  Such pages can be "noticed" by memory management in several places:
0258 
0259  (1) in the mlock()/mlock2()/mlockall() system call handlers;
0260 
0261  (2) in the mmap() system call handler when mmapping a region with the
0262      MAP_LOCKED flag;
0263 
0264  (3) mmapping a region in a task that has called mlockall() with the MCL_FUTURE
0265      flag;
0266 
0267  (4) in the fault path and when a VM_LOCKED stack segment is expanded; or
0268 
0269  (5) as mentioned above, in vmscan:shrink_page_list() when attempting to
0270      reclaim a page in a VM_LOCKED VMA by page_referenced() or try_to_unmap().
0271 
0272 mlocked pages become unlocked and rescued from the unevictable list when:
0273 
0274  (1) mapped in a range unlocked via the munlock()/munlockall() system calls;
0275 
0276  (2) munmap()'d out of the last VM_LOCKED VMA that maps the page, including
0277      unmapping at task exit;
0278 
0279  (3) when the page is truncated from the last VM_LOCKED VMA of an mmapped file;
0280      or
0281 
0282  (4) before a page is COW'd in a VM_LOCKED VMA.
0283 
0284 
0285 mlock()/mlock2()/mlockall() System Call Handling
0286 ------------------------------------------------
0287 
0288 mlock(), mlock2() and mlockall() system call handlers proceed to mlock_fixup()
0289 for each VMA in the range specified by the call.  In the case of mlockall(),
0290 this is the entire active address space of the task.  Note that mlock_fixup()
0291 is used for both mlocking and munlocking a range of memory.  A call to mlock()
0292 an already VM_LOCKED VMA, or to munlock() a VMA that is not VM_LOCKED, is
0293 treated as a no-op and mlock_fixup() simply returns.
0294 
0295 If the VMA passes some filtering as described in "Filtering Special VMAs"
0296 below, mlock_fixup() will attempt to merge the VMA with its neighbors or split
0297 off a subset of the VMA if the range does not cover the entire VMA.  Any pages
0298 already present in the VMA are then marked as mlocked by mlock_page() via
0299 mlock_pte_range() via walk_page_range() via mlock_vma_pages_range().
0300 
0301 Before returning from the system call, do_mlock() or mlockall() will call
0302 __mm_populate() to fault in the remaining pages via get_user_pages() and to
0303 mark those pages as mlocked as they are faulted.
0304 
0305 Note that the VMA being mlocked might be mapped with PROT_NONE.  In this case,
0306 get_user_pages() will be unable to fault in the pages.  That's okay.  If pages
0307 do end up getting faulted into this VM_LOCKED VMA, they will be handled in the
0308 fault path - which is also how mlock2()'s MLOCK_ONFAULT areas are handled.
0309 
0310 For each PTE (or PMD) being faulted into a VMA, the page add rmap function
0311 calls mlock_vma_page(), which calls mlock_page() when the VMA is VM_LOCKED
0312 (unless it is a PTE mapping of a part of a transparent huge page).  Or when
0313 it is a newly allocated anonymous page, lru_cache_add_inactive_or_unevictable()
0314 calls mlock_new_page() instead: similar to mlock_page(), but can make better
0315 judgments, since this page is held exclusively and known not to be on LRU yet.
0316 
0317 mlock_page() sets PageMlocked immediately, then places the page on the CPU's
0318 mlock pagevec, to batch up the rest of the work to be done under lru_lock by
0319 __mlock_page().  __mlock_page() sets PageUnevictable, initializes mlock_count
0320 and moves the page to unevictable state ("the unevictable LRU", but with
0321 mlock_count in place of LRU threading).  Or if the page was already PageLRU
0322 and PageUnevictable and PageMlocked, it simply increments the mlock_count.
0323 
0324 But in practice that may not work ideally: the page may not yet be on an LRU, or
0325 it may have been temporarily isolated from LRU.  In such cases the mlock_count
0326 field cannot be touched, but will be set to 0 later when __pagevec_lru_add_fn()
0327 returns the page to "LRU".  Races prohibit mlock_count from being set to 1 then:
0328 rather than risk stranding a page indefinitely as unevictable, always err with
0329 mlock_count on the low side, so that when munlocked the page will be rescued to
0330 an evictable LRU, then perhaps be mlocked again later if vmscan finds it in a
0331 VM_LOCKED VMA.
0332 
0333 
0334 Filtering Special VMAs
0335 ----------------------
0336 
0337 mlock_fixup() filters several classes of "special" VMAs:
0338 
0339 1) VMAs with VM_IO or VM_PFNMAP set are skipped entirely.  The pages behind
0340    these mappings are inherently pinned, so we don't need to mark them as
0341    mlocked.  In any case, most of the pages have no struct page in which to so
0342    mark the page.  Because of this, get_user_pages() will fail for these VMAs,
0343    so there is no sense in attempting to visit them.
0344 
0345 2) VMAs mapping hugetlbfs page are already effectively pinned into memory.  We
0346    neither need nor want to mlock() these pages.  But __mm_populate() includes
0347    hugetlbfs ranges, allocating the huge pages and populating the PTEs.
0348 
0349 3) VMAs with VM_DONTEXPAND are generally userspace mappings of kernel pages,
0350    such as the VDSO page, relay channel pages, etc.  These pages are inherently
0351    unevictable and are not managed on the LRU lists.  __mm_populate() includes
0352    these ranges, populating the PTEs if not already populated.
0353 
0354 4) VMAs with VM_MIXEDMAP set are not marked VM_LOCKED, but __mm_populate()
0355    includes these ranges, populating the PTEs if not already populated.
0356 
0357 Note that for all of these special VMAs, mlock_fixup() does not set the
0358 VM_LOCKED flag.  Therefore, we won't have to deal with them later during
0359 munlock(), munmap() or task exit.  Neither does mlock_fixup() account these
0360 VMAs against the task's "locked_vm".
0361 
0362 
0363 munlock()/munlockall() System Call Handling
0364 -------------------------------------------
0365 
0366 The munlock() and munlockall() system calls are handled by the same
0367 mlock_fixup() function as mlock(), mlock2() and mlockall() system calls are.
0368 If called to munlock an already munlocked VMA, mlock_fixup() simply returns.
0369 Because of the VMA filtering discussed above, VM_LOCKED will not be set in
0370 any "special" VMAs.  So, those VMAs will be ignored for munlock.
0371 
0372 If the VMA is VM_LOCKED, mlock_fixup() again attempts to merge or split off the
0373 specified range.  All pages in the VMA are then munlocked by munlock_page() via
0374 mlock_pte_range() via walk_page_range() via mlock_vma_pages_range() - the same
0375 function used when mlocking a VMA range, with new flags for the VMA indicating
0376 that it is munlock() being performed.
0377 
0378 munlock_page() uses the mlock pagevec to batch up work to be done under
0379 lru_lock by  __munlock_page().  __munlock_page() decrements the page's
0380 mlock_count, and when that reaches 0 it clears PageMlocked and clears
0381 PageUnevictable, moving the page from unevictable state to inactive LRU.
0382 
0383 But in practice that may not work ideally: the page may not yet have reached
0384 "the unevictable LRU", or it may have been temporarily isolated from it.  In
0385 those cases its mlock_count field is unusable and must be assumed to be 0: so
0386 that the page will be rescued to an evictable LRU, then perhaps be mlocked
0387 again later if vmscan finds it in a VM_LOCKED VMA.
0388 
0389 
0390 Migrating MLOCKED Pages
0391 -----------------------
0392 
0393 A page that is being migrated has been isolated from the LRU lists and is held
0394 locked across unmapping of the page, updating the page's address space entry
0395 and copying the contents and state, until the page table entry has been
0396 replaced with an entry that refers to the new page.  Linux supports migration
0397 of mlocked pages and other unevictable pages.  PG_mlocked is cleared from the
0398 the old page when it is unmapped from the last VM_LOCKED VMA, and set when the
0399 new page is mapped in place of migration entry in a VM_LOCKED VMA.  If the page
0400 was unevictable because mlocked, PG_unevictable follows PG_mlocked; but if the
0401 page was unevictable for other reasons, PG_unevictable is copied explicitly.
0402 
0403 Note that page migration can race with mlocking or munlocking of the same page.
0404 There is mostly no problem since page migration requires unmapping all PTEs of
0405 the old page (including munlock where VM_LOCKED), then mapping in the new page
0406 (including mlock where VM_LOCKED).  The page table locks provide sufficient
0407 synchronization.
0408 
0409 However, since mlock_vma_pages_range() starts by setting VM_LOCKED on a VMA,
0410 before mlocking any pages already present, if one of those pages were migrated
0411 before mlock_pte_range() reached it, it would get counted twice in mlock_count.
0412 To prevent that, mlock_vma_pages_range() temporarily marks the VMA as VM_IO,
0413 so that mlock_vma_page() will skip it.
0414 
0415 To complete page migration, we place the old and new pages back onto the LRU
0416 afterwards.  The "unneeded" page - old page on success, new page on failure -
0417 is freed when the reference count held by the migration process is released.
0418 
0419 
0420 Compacting MLOCKED Pages
0421 ------------------------
0422 
0423 The memory map can be scanned for compactable regions and the default behavior
0424 is to let unevictable pages be moved.  /proc/sys/vm/compact_unevictable_allowed
0425 controls this behavior (see Documentation/admin-guide/sysctl/vm.rst).  The work
0426 of compaction is mostly handled by the page migration code and the same work
0427 flow as described in Migrating MLOCKED Pages will apply.
0428 
0429 
0430 MLOCKING Transparent Huge Pages
0431 -------------------------------
0432 
0433 A transparent huge page is represented by a single entry on an LRU list.
0434 Therefore, we can only make unevictable an entire compound page, not
0435 individual subpages.
0436 
0437 If a user tries to mlock() part of a huge page, and no user mlock()s the
0438 whole of the huge page, we want the rest of the page to be reclaimable.
0439 
0440 We cannot just split the page on partial mlock() as split_huge_page() can
0441 fail and a new intermittent failure mode for the syscall is undesirable.
0442 
0443 We handle this by keeping PTE-mlocked huge pages on evictable LRU lists:
0444 the PMD on the border of a VM_LOCKED VMA will be split into a PTE table.
0445 
0446 This way the huge page is accessible for vmscan.  Under memory pressure the
0447 page will be split, subpages which belong to VM_LOCKED VMAs will be moved
0448 to the unevictable LRU and the rest can be reclaimed.
0449 
0450 /proc/meminfo's Unevictable and Mlocked amounts do not include those parts
0451 of a transparent huge page which are mapped only by PTEs in VM_LOCKED VMAs.
0452 
0453 
0454 mmap(MAP_LOCKED) System Call Handling
0455 -------------------------------------
0456 
0457 In addition to the mlock(), mlock2() and mlockall() system calls, an application
0458 can request that a region of memory be mlocked by supplying the MAP_LOCKED flag
0459 to the mmap() call.  There is one important and subtle difference here, though.
0460 mmap() + mlock() will fail if the range cannot be faulted in (e.g. because
0461 mm_populate fails) and returns with ENOMEM while mmap(MAP_LOCKED) will not fail.
0462 The mmaped area will still have properties of the locked area - pages will not
0463 get swapped out - but major page faults to fault memory in might still happen.
0464 
0465 Furthermore, any mmap() call or brk() call that expands the heap by a task
0466 that has previously called mlockall() with the MCL_FUTURE flag will result
0467 in the newly mapped memory being mlocked.  Before the unevictable/mlock
0468 changes, the kernel simply called make_pages_present() to allocate pages
0469 and populate the page table.
0470 
0471 To mlock a range of memory under the unevictable/mlock infrastructure,
0472 the mmap() handler and task address space expansion functions call
0473 populate_vma_page_range() specifying the vma and the address range to mlock.
0474 
0475 
0476 munmap()/exit()/exec() System Call Handling
0477 -------------------------------------------
0478 
0479 When unmapping an mlocked region of memory, whether by an explicit call to
0480 munmap() or via an internal unmap from exit() or exec() processing, we must
0481 munlock the pages if we're removing the last VM_LOCKED VMA that maps the pages.
0482 Before the unevictable/mlock changes, mlocking did not mark the pages in any
0483 way, so unmapping them required no processing.
0484 
0485 For each PTE (or PMD) being unmapped from a VMA, page_remove_rmap() calls
0486 munlock_vma_page(), which calls munlock_page() when the VMA is VM_LOCKED
0487 (unless it was a PTE mapping of a part of a transparent huge page).
0488 
0489 munlock_page() uses the mlock pagevec to batch up work to be done under
0490 lru_lock by  __munlock_page().  __munlock_page() decrements the page's
0491 mlock_count, and when that reaches 0 it clears PageMlocked and clears
0492 PageUnevictable, moving the page from unevictable state to inactive LRU.
0493 
0494 But in practice that may not work ideally: the page may not yet have reached
0495 "the unevictable LRU", or it may have been temporarily isolated from it.  In
0496 those cases its mlock_count field is unusable and must be assumed to be 0: so
0497 that the page will be rescued to an evictable LRU, then perhaps be mlocked
0498 again later if vmscan finds it in a VM_LOCKED VMA.
0499 
0500 
0501 Truncating MLOCKED Pages
0502 ------------------------
0503 
0504 File truncation or hole punching forcibly unmaps the deleted pages from
0505 userspace; truncation even unmaps and deletes any private anonymous pages
0506 which had been Copied-On-Write from the file pages now being truncated.
0507 
0508 Mlocked pages can be munlocked and deleted in this way: like with munmap(),
0509 for each PTE (or PMD) being unmapped from a VMA, page_remove_rmap() calls
0510 munlock_vma_page(), which calls munlock_page() when the VMA is VM_LOCKED
0511 (unless it was a PTE mapping of a part of a transparent huge page).
0512 
0513 However, if there is a racing munlock(), since mlock_vma_pages_range() starts
0514 munlocking by clearing VM_LOCKED from a VMA, before munlocking all the pages
0515 present, if one of those pages were unmapped by truncation or hole punch before
0516 mlock_pte_range() reached it, it would not be recognized as mlocked by this VMA,
0517 and would not be counted out of mlock_count.  In this rare case, a page may
0518 still appear as PageMlocked after it has been fully unmapped: and it is left to
0519 release_pages() (or __page_cache_release()) to clear it and update statistics
0520 before freeing (this event is counted in /proc/vmstat unevictable_pgs_cleared,
0521 which is usually 0).
0522 
0523 
0524 Page Reclaim in shrink_*_list()
0525 -------------------------------
0526 
0527 vmscan's shrink_active_list() culls any obviously unevictable pages -
0528 i.e. !page_evictable(page) pages - diverting those to the unevictable list.
0529 However, shrink_active_list() only sees unevictable pages that made it onto the
0530 active/inactive LRU lists.  Note that these pages do not have PageUnevictable
0531 set - otherwise they would be on the unevictable list and shrink_active_list()
0532 would never see them.
0533 
0534 Some examples of these unevictable pages on the LRU lists are:
0535 
0536  (1) ramfs pages that have been placed on the LRU lists when first allocated.
0537 
0538  (2) SHM_LOCK'd shared memory pages.  shmctl(SHM_LOCK) does not attempt to
0539      allocate or fault in the pages in the shared memory region.  This happens
0540      when an application accesses the page the first time after SHM_LOCK'ing
0541      the segment.
0542 
0543  (3) pages still mapped into VM_LOCKED VMAs, which should be marked mlocked,
0544      but events left mlock_count too low, so they were munlocked too early.
0545 
0546 vmscan's shrink_inactive_list() and shrink_page_list() also divert obviously
0547 unevictable pages found on the inactive lists to the appropriate memory cgroup
0548 and node unevictable list.
0549 
0550 rmap's page_referenced_one(), called via vmscan's shrink_active_list() or
0551 shrink_page_list(), and rmap's try_to_unmap_one() called via shrink_page_list(),
0552 check for (3) pages still mapped into VM_LOCKED VMAs, and call mlock_vma_page()
0553 to correct them.  Such pages are culled to the unevictable list when released
0554 by the shrinker.